5 # end-relative range of operands that are rvalues
27 # if it is in byte_opcodes it is treated as byte and has no suffix
49 # if any operand is in byte_operands it is treated as byte and has suffix
50 # in this case it is mandatory that no operand also be in word_operands
70 '0x12': 'cpu_z80_fetch_byte(self)',
71 '0x34': 'cpu_z80_fetch_byte(self)',
72 'a': 'self->regs.byte.a',
73 'b': 'self->regs.byte.b',
74 'c': 'self->regs.byte.c',
75 'd': 'self->regs.byte.d',
76 'e': 'self->regs.byte.e',
77 'l': 'self->regs.byte.l',
78 'h': 'self->regs.byte.h',
79 'ixl': 'self->regs.byte.ixl',
80 'ixh': 'self->regs.byte.ixh',
81 'iyl': 'self->regs.byte.iyl',
82 'iyh': 'self->regs.byte.iyh',
83 'i': 'self->regs.byte.i',
84 'r': 'self->regs.byte.r',
85 '(0x0012)': 'cpu_z80_in_byte(self, cpu_z80_port_word(self))',
86 '(c)': 'cpu_z80_in_byte(self, self->regs.word.bc)',
97 'ixl': 'CPU_Z80_EA_IXL',
98 'ixh': 'CPU_Z80_EA_IXH',
99 'iyl': 'CPU_Z80_EA_IYL',
100 'iyh': 'CPU_Z80_EA_IYH',
103 'sink': 'CPU_Z80_EA_SINK',
104 '(0x3412)': 'cpu_z80_fetch_word(self)',
105 '(bc)': 'self->regs.word.bc',
106 '(de)': 'self->regs.word.de',
107 '(hl)': 'self->regs.word.hl',
108 '(ix+0x12)': 'cpu_z80_displacement(self, self->regs.word.ix)',
109 '(iy+0x12)': 'cpu_z80_displacement(self, self->regs.word.iy)',
110 '(0x0012)': 'cpu_z80_fetch_byte(self)',
111 '(c)': 'self->regs.word.bc',
114 # if it is in word_opcodes it is treated as word and has no suffix
115 # in this case the operands "c" and "(hl)" do not imply a byte opcode
126 # if any operand is in word_operands it is treated as word and has suffix
127 # in this case it is mandatory that no operand also be in byte_operands
138 word_rvalue_modes = {
139 '0x0014': 'cpu_z80_relative(self)',
140 '0x0015': 'cpu_z80_relative(self)',
141 '0x3412': 'cpu_z80_fetch_word(self)',
142 'af': 'self->regs.word.af',
143 'bc': 'self->regs.word.bc',
144 'de': 'self->regs.word.de',
145 'hl': 'self->regs.word.hl',
146 'ix': 'self->regs.word.ix',
147 'iy': 'self->regs.word.iy',
148 'sp': 'self->regs.word.sp',
150 word_lvalue_modes = {
151 'z': 'self->regs.bit.zf',
152 'nz': '!self->regs.bit.zf',
153 'c': 'self->regs.bit.cf',
154 'nc': '!self->regs.bit.cf',
155 'm': 'self->regs.bit.sf',
156 'p': '!self->regs.bit.sf',
157 'pe': 'self->regs.bit.pvf',
158 'po': '!self->regs.bit.pvf',
159 'af': 'CPU_Z80_EA_AF',
160 'af\'': 'CPU_Z80_EA_AF_PRIME',
161 'bc': 'CPU_Z80_EA_BC',
162 'de': 'CPU_Z80_EA_DE',
163 'hl': 'CPU_Z80_EA_HL',
164 'ix': 'CPU_Z80_EA_IX',
165 'iy': 'CPU_Z80_EA_IY',
166 'sp': 'CPU_Z80_EA_SP',
167 '(0x3412)': 'cpu_z80_fetch_word(self)',
168 '(bc)': 'self->regs.word.bc',
169 '(de)': 'self->regs.word.de',
170 '(hl)': 'self->regs.word.hl',
171 '(ix)': 'self->regs.word.ix',
172 '(iy)': 'self->regs.word.iy',
173 '(sp)': 'self->regs.word.sp',
176 prefixes = [[], [0xcb], [0xdd], [0xdd, 0xcb], [0xed], [0xfd], [0xfd, 0xcb]]
178 line = sys.stdin.readline().strip()
179 assert line == 'opcodes{0:s}'.format(''.join([f' 0x{j:02x}' for j in i]))
182 'void cpu_z80_execute{0:s}(struct cpu_z80 *self) {{'.format(
183 ''.join([f'_{j:02x}' for j in i])
186 print(' switch (cpu_z80_fetch_byte(self)) {')
188 for j in range(0x100):
189 line = sys.stdin.readline().strip()
191 print(f' case 0x{j:02x}:')
195 ' cpu_z80_execute{0:s}(self);'.format(
196 ''.join([f'_{k:02x}' for k in k])
203 elif len(instr) >= 2:
204 instr[1:] = [l for k in instr[1:] for l in k.split(',')]
207 # detect operation size (byte or word)
209 if instr[0] not in byte_opcodes and instr[0] not in word_opcodes:
211 if k in byte_operands:
212 assert suffix != '_word'
214 elif k in word_operands:
215 assert suffix != '_byte'
218 # work out which operands are rvalue
219 k = len(instr) + rvalue_opcodes.get(instr[0], 0)
222 if suffix == '_byte' or instr[0] in byte_opcodes:
223 # operands [1, k) are lvalue
224 for l in range(1, k):
225 if instr[l] in byte_lvalue_modes:
226 instr[l] = byte_lvalue_modes[instr[l]]
228 # operands [k, n) are rvalue
229 for l in range(k, len(instr)):
230 if instr[l] in byte_rvalue_modes:
231 instr[l] = byte_rvalue_modes[instr[l]]
232 elif instr[l] in byte_lvalue_modes:
233 instr[l] = 'cpu_z80_read_byte(self, {0:s})'.format(
234 byte_lvalue_modes[instr[l]]
236 elif suffix == '_word' or instr[0] in word_opcodes:
237 # operands [1, k] are lvalue
238 for l in range(1, k):
239 if instr[l] in word_lvalue_modes:
240 instr[l] = word_lvalue_modes[instr[l]]
242 # operands [k, n) are rvalue
243 for l in range(k, len(instr)):
244 if instr[l] in word_rvalue_modes:
245 instr[l] = word_rvalue_modes[instr[l]]
246 elif instr[l] in word_lvalue_modes:
247 instr[l] = 'cpu_z80_read_word(self, {0:s})'.format(
248 word_lvalue_modes[instr[l]]
252 ' cpu_z80_{0:s}{1:s}(self{2:s});'.format(
255 ''.join([', ' + k for k in instr[1:]])
260 line = sys.stdin.readline().strip()
261 assert len(line) == 0