-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.py
96 lines (80 loc) · 1.84 KB
/
loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import sys
import time
def emit(line):
delay = 0.001
for char in line:
sys.stdout.write(char)
time.sleep(delay)
sys.stdout.write("\r")
time.sleep(delay)
sys.stdout.write("\n")
time.sleep(delay)
lineno = 100
def emit_data(items):
global lineno
buf = "%d data " % lineno
buf += ", ".join(items)
emit(buf)
lineno += 10
loader = [
#10 LET ADDR = <starting address>
"20 READ BYTE",
"30 POKE ADDR,BYTE",
"40 ADDR = ADDR + 1",
"50 GOTO 20",
]
launcher = [
#10 LET LOW = <low byte of starting address>
#20 LET HIGH = <high byte of starting address>
"30 POKE &h8049, LOW",
"40 POKE &h804a, HIGH",
"50 PRINT USR(0)",
]
def load(filename, offset):
emit("NEW")
time.sleep(0.1)
for line in loader:
emit(line)
items_per_line = 16
with open(filename, "rb") as f:
item = 0
buf = ""
byte = ""
items = []
for byte in f.read():
if item > 0 and ((item % items_per_line) == 0):
emit_data(items)
items = []
items.append(str(ord(byte)))
item += 1
if len(items) > 0:
emit_data(items)
emit("10 LET ADDR = &h%x" % int(offset, 16))
time.sleep(0.1)
emit("RUN")
def run(offset):
emit("NEW")
time.sleep(0.1)
for line in launcher:
emit(line)
org = int(offset, 16)
low = org & 0xff
high = (org & 0xff00) >> 8
emit("10 LET LOW = &h%x" % low)
emit("20 LET HIGH = &h%x" % high)
emit("RUN")
args = sys.argv[1:]
sys.stderr.write("Processing args [%s]\r\n" % " ".join(args))
while len(args):
if args[0] == 'load':
sys.stderr.write("Loading \"%s\" at %s\r\n" % (args[1], args[2]))
load(args[1], args[2])
args = args[3:]
elif args[0] == 'run':
sys.stderr.write("Running from %s\r\n" % (args[1]))
run(args[1])
args = args[2:]
else:
sys.stderr.write("Unknown argument at [%s]\r\n" % " ".join(args))
break
time.sleep(0.5)