-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopcode_impl.py
More file actions
executable file
·301 lines (233 loc) · 5.84 KB
/
opcode_impl.py
File metadata and controls
executable file
·301 lines (233 loc) · 5.84 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python2.7
# Read an input asm file and expand (fill in) some opcode handlers.
import re, sys, inspect
# A little metaprogramming magic...
expansions = {}
def match(pattern):
def register(f):
expansions[re.compile(pattern)] = f
return f
return register
# ASM generation helpers
def load(r):
if (r == '(hl)'):
print "mov dpl, erl"
print "mov dph, erh"
print "movx a, @dptr"
else:
print "mov a, er" + r
def save(r):
if (r == '(hl)'):
print "movx @dptr, a"
else:
print "mov er" + r + ", a"
# Now we can decorate each expansion function with the regex to match,
# and they'll even get the match groups passed in as separate args.
@match(r'INC (\w)(\w)')
def inc16(r1, r2):
r1 = r1.lower()
r2 = r2.lower()
print "mov a, #0x01"
print "mov ercf, c"
print "add a, er" + r2
print "mov er" + r2 + ", a"
print "mov a, #0x00"
print "addc a, er" + r1
print "mov er" + r1 + ", a"
print "mov c, ercf"
print "ljmp done"
@match(r'DEC (\w)(\w)')
def dec16(r1, r2):
r1 = r1.lower()
r2 = r2.lower()
print "mov a, er" + r2
print "clr c"
print "subb a, #0x01"
print "mov er" + r2 + ", a"
print "mov a, er" + r1
print "subb a, #0x01"
print "mov er" + r1 + ", a"
print "ljmp done"
@match(r'LD (\w),(\w)\s')
def ld(r1, r2):
r1 = r1.lower()
r2 = r2.lower()
print "mov a, er" + r2
print "mov er" + r1 + ", a"
print "ljmp done"
@match(r'LD (\w),\(HL\)')
def ldrhl(r):
r = r.lower()
print "mov dpl, erl"
print "mov dph, erh"
print "movx a, @dptr"
print "mov er" + r + ", a"
print "ljmp done"
@match(r'LD \(HL\),(\w)')
def ldhlr(r):
r = r.lower()
print "mov a, er" + r
print "mov dpl, erl"
print "mov dph, erh"
print "movx @dptr, a"
print "ljmp done"
@match(r'LD (\w),d8')
def ld8(r):
r = r.lower()
print "inc dptr"
print "movx a, @dptr"
print "mov er" + r + ", a"
print "ljmp done"
# Bit opcodes
@match(r'BIT (\d),(\w)')
def bit(bit, r):
bit = int(bit)
r = r.lower()
print "mov a, er" + r
print "anl a, #" + hex(2**bit)
print "mov erzf, a"
print "ljmp done"
@match(r'BIT (\d),\(HL\)')
def bithl(bit):
bit = int(bit)
print "mov dpl, erl"
print "mov dph, erh"
print "movx a, @dptr"
print "anl a, #" + hex(2**bit)
print "mov erzf, a"
print "ljmp done"
@match(r'SET (\d),(\w)')
def set(bit, r):
bit = int(bit)
r = r.lower()
print "mov a, er" + r
print "orl a, #" + hex(2**bit)
print "mov er" + r + ", a"
print "ljmp done"
@match(r'SET (\d),\(HL\)')
def sethl(bit):
bit = int(bit)
print "mov erh, dph"
print "mov erl, dpl"
print "movx a, @dptr"
print "anl a, #" + hex(2**bit)
print "movx @dptr, a"
print "ljmp done"
@match(r'RES (\d),(\w)')
def reset(bit, r):
bit = int(bit)
r = r.lower()
print "mov a, er" + r
print "orl a, #" + hex(0xff - 2**bit)
print "mov er" + r + ", a"
print "ljmp done"
@match(r'RES (\d),\(HL\)')
def reshl(bit):
bit = int(bit)
print "mov erh, dph"
print "mov erl, dpl"
print "movx a, @dptr"
print "orl a, #" + hex(0xff - 2**bit)
print "movx @dptr, a"
print "ljmp done"
@match(r'RLC ([\w\(\)]*)')
def rlc(r):
r = r.lower()
load(r)
print "mov b, a"
print "add a, #0x80" # Get the carry bit set properly
print "mov a, b"
print "rl a"
print "mov erzf, a"
save(r)
print "ljmp done"
@match(r'RRC ([\w\(\)]*)')
def rrc(r):
r = r.lower()
load(r)
print "rr a"
save(r)
print "mov erzf, a"
print "add a, #0x80"
print "ljmp done"
@match(r'RL ([\w\(\)]*)')
def rl(r):
r = r.lower()
load(r)
print "rlc a"
save(r)
print "mov erzf, a"
print "ljmp done"
@match(r'RR ([\w\(\)]*)')
def rr(r):
r = r.lower()
load(r)
print "rrc a"
save(r)
print "mov erzf, a"
print "ljmp done"
@match(r'SLA ([\w\(\)]*)')
def sla(r):
r = r.lower()
load(r)
print "clr c"
print "rlc a"
print "mov erzf, a"
save(r)
print "ljmp done"
@match(r'SRA ([\w\(\)]*)')
def sra(r):
r = r.lower()
load(r)
print "mov b, a"
print "add a, #0x80"
print "mov a, b"
print "rrc a"
print "mov erzf, a"
save(r)
print "ljmp done"
@match(r'SRL ([\w\(\)]*)')
def srl(r):
r = r.lower()
load(r)
print "clr c"
print "rrc a"
print "mov erzf, a"
save(r)
print "ljmp done"
@match(r'SWAP ([\w\(\)]*)')
def swap(r):
r = r.lower()
load(r)
print "swap a"
print "mov erzf, a"
print "clr c"
save(r)
print "ljmp done"
# Go through the input, matching each line against our expander regexen.
# Each line should only match one expansion -- behavior otherwise is undefined.
def expand(lines):
i = 0
while i < len(lines):
print lines[i],
for pattern, func in expansions.iteritems():
match = re.search(pattern, lines[i])
if match is None:
continue
argc = len(inspect.getargspec(func)[0])
args = [match.group(j) for j in range(1, argc+1)]
i += 1 # Skip through comments
while (lines[i][0] == ';'):
print lines[i],
i += 1
if (lines[i] != "\n"):
raise ValueError, "Attempting to expand into non-blank line (" + \
str(i) + "): " + lines[i]
func(*args) # Expand
break
else: # No match for this line
i += 1
if __name__ == '__main__':
f = open(sys.argv[1])
lines = f.readlines()
expand(lines)