-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_msx.py
205 lines (150 loc) · 5.2 KB
/
compile_msx.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
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
# Written by Folkert van Heusden
# Released in the public domain
# www.vanheusden.com
import sys
from compile_x86 import CompileToX86
class CompileToMSX(CompileToX86):
loopNr = 0
hlNr = 0
lnrs = []
functionsFirst = False
def header(self):
print('Brainfuck to MSX-ASM compiler.', file=sys.stderr)
def get_name():
return ('msx', 'Z80 assembly targeting MSX')
def genindent(self, level):
return ' ' * (level * 4)
def invokeFunction(self, funcNr):
print('%sCALL f%d' % (self.genindent(1), funcNr))
def addToDataPtr(self, n, dot, position):
ind = self.genindent(1)
while n > 0:
print('; add to pointer')
if n <= 3:
cur = n
for i in range(0, n):
print('%sINC HL' % ind) # 6
else:
cur = min(n, 255)
print('%sLD A,%d' % (ind, cur)) # 7
print('%sLD C,A' % ind) # 4
print('%sADD HL,BC' % ind) # 11
n -= cur
def subFromDataPtr(self, n, dot, position):
ind = self.genindent(1)
while n > 0:
print('; sub from pointer')
if n <= 5:
cur = n
for i in range(0, n):
print('%sDEC HL' % ind) # 6
else:
cur = min(n, 255)
print('%sAND A' % ind) # clear carry flag, 4
print('%sLD A,%d' % (ind, cur)) # 7
print('%sLD C,A' % ind) # 4
print('%sSBC HL,BC' % ind) # 15
n -= cur
def addToData(self, n, dot, position):
ind = self.genindent(1)
print('; add to data')
if n == 1:
print('%sINC (HL)' % ind) # 11
else:
print('%sLD A,%d' % (ind, n)) # 7
print('%sADD A,(HL)' % ind) # 7
print('%sLD (HL),A' % ind) # 7
def subFromData(self, n, dot, position):
ind = self.genindent(1)
print('; sub from data')
if n == 1:
print('%sDEC (hl)' % ind) # 11
else:
print('%sLD A,(HL)' % ind) # 7
print('%sSUB %d' % (ind, n)) # 7
print('%sLD (HL),A' % ind) # 7
def emitCharacter(self, n, dot):
ind = self.genindent(1)
for i in range(0, n):
print('; print char')
print('%sCALL prtchr' % ind)
print('')
def startLoop(self, n, position):
for j in range(0, n):
print('; start loop')
loopName = 'wloop_%d' % self.loopNr
self.loopNr += 1
self.lnrs.append(loopName)
print('%s%s:' % (self.genindent(0), loopName))
ind = self.genindent(1)
print('%sLD A,(HL)' % ind)
print('%sAND A' % ind)
print('%sJP Z,%s_e' % (ind, loopName))
def finishLoop(self, n, dot, position):
for j in range(0, n):
print('; end loop')
jb_label = self.lnrs.pop(-1) # jump bakc label
print('%sJP %s' % (self.genindent(1), jb_label))
jb_label_e = jb_label + "_e" # break out of while label
print('%s:' % jb_label_e)
def addComment(self, s):
print('; %s' % s)
def multilineCommentStart(self):
print(';')
def multilineCommentLine(self, s):
print('; %s' % s)
def multilineCommentEnd(self):
print(';')
def emitFunctions(self):
self.lindentlevel += 1
for blkLoop in range(0, len(self.blocks)):
print('f%d:' % blkLoop)
self.translate(self.blocks[blkLoop][0], self.blocks[blkLoop][1])
print('%sRET' % self.genindent(1))
self.lindentlevel -= 1
def emitProgramTail(self):
ind = self.genindent(1)
print('%sdata_mem: DB "dummy",0' % ind)
print('ending: equ $-1')
def emitMainFunction(self):
ind = self.genindent(1)
print('%sORG $8050-7' % ind)
print('%sDB $FE' % ind)
print('%sDW start,ending,start' % ind)
self.addComments(self.copyrightNotice)
print('start:')
print('%sLD HL,data_mem' % ind)
print('')
print('%sLD D, 0' % ind)
print('%sLD BC, freemem' % ind)
print('clear:')
print('%sLD (HL),D' % ind)
print('%sINC HL' % ind)
print('%sDEC BC' % ind)
print('%sLD A,B' % ind)
print('%sCP D' % ind)
print('%sJP NZ,clear' % ind)
print('%sLD A,C' % ind)
print('%sCP D' % ind)
print('%sJP NZ,clear' % ind)
print('')
print('%sLD HL,data_mem' % ind)
print('%sLD B,0' % ind)
self.translate(0, len(self.allCode))
print('%sRET' % ind)
print('prtchr:')
print('%sLD A,(HL)' % ind)
print('%sCALL $A2' % ind)
self.addComment('Add CR when emitting an LF')
print('%sLD D,$0A' % ind)
print('%sCP D' % ind)
label = 'nocarry%d' % self.hlNr
self.hlNr += 1
print('%sJP nc,%s' % (ind, label))
print('%sLD A,$0d' % ind)
print('%sCALL $A2' % ind)
print('%s:' % (label))
print('%sLD B,0' % ind)
print('%sRET' % ind)
print('')
print('freemem: equ 2048')