z80/z80make.py: Revisited z80 code generator

This commit is contained in:
Andrei Holub 2025-04-09 10:49:09 -04:00
parent cff4924beb
commit d5e541c9e8
2 changed files with 162 additions and 97 deletions

View File

@ -4,7 +4,7 @@
# macros
##########################################################
macro nomreq_addr
if (!m_nomreq_cb.isunset())
if (nomemrq_en)
m_nomreq_cb(TADR, 0x00, 0xff);
+ 1
@ -47,7 +47,7 @@ macro wm16_sp
macro rop
m_m1_cycles-2 !! TDAT8 = opcode_read();
if (!m_refresh_cb.isunset())
if (refresh_en)
m_refresh_cb((I << 8) | (R2 & 0x80) | (R & 0x7f), 0x00, 0xff);
+ 2
PC++; R++; Q = QT; QT = YF | XF;
@ -508,11 +508,11 @@ macro r800:otdr
macro jump %opcode
m_ref = 0x%opcode00;
continue;
goto process;
macro jump_prefixed %prefix
m_ref = (%prefix << 16) | (TDAT8 << 8);
continue;
goto process;
macro take_nmi
// Check if processor was halted
@ -671,20 +671,21 @@ macro ncs800:check_interrupts
# ROP
##########################################################
ffff
rop:
m_ref = 0xffff00;
if (m_icount <= 0) return;
if (m_busrq_state) {
if (!m_busack_state) {
m_busack_state = 1;
m_busack_cb(1);
if (busack_en) {
if (m_busrq_state) {
if (!m_busack_state) {
m_busack_state = 1;
m_busack_cb(1);
}
if (m_icount > 0)
m_icount = 0;
return;
} else if (m_busack_state) {
m_busack_state = 0;
m_busack_cb(0);
}
if (m_icount > 0)
m_icount = 0;
return;
} else if (m_busack_state) {
m_busack_state = 0;
m_busack_cb(0);
}
call check_interrupts
m_after_ei = false;
@ -693,13 +694,13 @@ ffff
debugger_wait_hook();
call rop
PC--;
goto rop;
continue;
} else {
PRVPC = PC;
debugger_instruction_hook(PC);
call rop
m_ref = (0x00 << 16) | (TDAT8 << 8);
continue;
goto process;
}

View File

@ -49,59 +49,91 @@ class IndStr:
class Opcode:
def __init__(self, code):
def __init__(self, prefix, code):
self.prefix = prefix
self.code = code
self.source = []
def add_source_lines(self, lines):
self.source.extend(lines)
def save_dasm(self, f):
code = self.code
has_steps = len(self.source) > 1
def with_steps(self):
for i in range(0, len(self.source)):
tokens = self.source[i].line().split()
if (tokens[0] == '+') or (len(tokens) > 2 and tokens[1] == "!!"):
return True
return False
def save_dasm(self, step_switch: bool, f):
has_steps = self.with_steps()
if has_steps:
print("\t\tswitch (u8(m_ref))", file=f)
print("\t\t{", file=f)
print("\t\tcase 0x00:", file=f)
if step_switch:
print("\t\tswitch (u8(m_ref))", file=f)
print("\t\t{", file=f)
print("\t\tcase 0x00:", file=f)
else:
print("\t\t//case 0x00:", file=f)
step = 0
for i in range(0, len(self.source)):
il = self.source[i]
if not has_steps:
if not has_steps or not step_switch:
il.indent = ""
line = il.line()
tokens = line.split()
last_line = i + 1 == len(self.source)
if tokens[0] == '+':
il.print("m_icount -= %s;" % (" ".join(tokens[1:])), f)
step += 1
to_step = "0x%s" % (hex(256 + step)[3:])
il.print("if (m_icount <= 0) {", f)
il.print(" m_ref = (m_ref & 0xffff00) | %s;" % (to_step), f)
il.print(" return;", f)
il.print("}", f)
il.print("[[fallthrough]];", f)
print("\t\tcase %s:" % (to_step), file=f)
if not last_line:
to_step = hex(256 + step)[3:]
il.print(" m_ref = 0x%s%s%s;" % (self.prefix, self.code, to_step), f)
il.print(" return;", f)
il.print("}", f)
if step_switch:
il.print("[[fallthrough]];", f)
print("\t\tcase 0x%s:" % (to_step), file=f)
else:
print("\t\t//case 0x%s:" % (to_step), file=f)
else:
il.print(" m_ref = 0xffff00;", f)
il.print(" return;", f)
il.print("}", f)
elif (len(tokens) > 2 and tokens[1] == "!!"):
il.print("[[fallthrough]];", f)
step += 1;
print("\t\tcase 0x%s:" % (hex(256 + step)[3:]), file=f)
if step_switch:
il.print("[[fallthrough]];", f)
print("\t\tcase 0x%s:" % (hex(256 + step)[3:]), file=f)
else:
print("\t\t//case 0x%s:" % (hex(256 + step)[3:]), file=f)
il.print("%s" % " ".join(tokens[2:]), f)
il.print("m_icount -= %s;" % (tokens[0]), f)
il.print("if (m_icount <= 0) {", f)
il.print(" if (access_to_be_redone()) {", f)
il.print(" m_icount += %s;" % (tokens[0]), f)
il.print(" m_ref = (m_ref & 0xffff00) | 0x%s;" % (hex(256 + step)[3:]), f)
il.print(" } else", f)
step += 1
to_step = "0x%s" % (hex(256 + step)[3:])
il.print(" m_ref = (m_ref & 0xffff00) | %s;" % (to_step), f)
il.print(" return;", f)
il.print("}", f)
il.print("[[fallthrough]];", f)
print("\t\tcase %s:" % (to_step), file=f)
il.print(" m_ref = 0x%s%s%s;" % (self.prefix, self.code, hex(256 + step)[3:]), f)
il.print(" } else {", f)
if not last_line:
step += 1
to_step = hex(256 + step)[3:]
il.print(" m_ref = 0x%s%s%s;" % (self.prefix, self.code, to_step), f)
il.print(" }", f)
il.print(" return;", f)
il.print("}", f)
if step_switch:
il.print("[[fallthrough]];", f)
print("\t\tcase 0x%s:" % (to_step), file=f)
else:
print("\t\t//case 0x%s:" % (to_step), file=f)
else:
il.print(" m_ref = 0xffff00;", f)
il.print(" }", f)
il.print(" return;", f)
il.print("}", f)
else:
il.print("%s" % line, f)
if has_steps:
print("\t\t\tbreak;\n", file=f)
if has_steps and step_switch:
print("\t\t}", file=f)
class Macro:
@ -122,7 +154,7 @@ class Macro:
class OpcodeList:
def __init__(self, gen, fname):
self.gen = gen
self.opcode_info = []
self.opcode_info = {} # prefix -> [Opcode]
self.macros = {}
try:
@ -167,33 +199,32 @@ class OpcodeList:
self.macros[nnames[0]] = inf
else:
ntokens = tokens[0].split(":")
if len(ntokens) == 2:
inf = Opcode(ntokens[1])
if ntokens[0] == self.gen:
# Replace in list when already present, otherwise append
found = False
found_index = 0
for i in range(len(self.opcode_info)):
if self.opcode_info[i].code == inf.code:
found = True
found_index = i
if found:
self.opcode_info[found_index] = inf
else:
self.opcode_info.append(inf)
else:
inf = Opcode(ntokens[0])
if None == self.gen:
self.opcode_info.append(inf)
else:
# Only place in list when not already present
found = False
for i in range(len(self.opcode_info)):
if self.opcode_info[i].code == inf.code:
found = True
if not found:
self.opcode_info.append(inf)
gen = None if len(ntokens) == 1 else ntokens[0]
prefix = ntokens[0][:2] if len(ntokens) == 1 else ntokens[1][:2]
opcode = ntokens[0][2:] if len(ntokens) == 1 else ntokens[1][2:]
if self.opcode_info.get(prefix) is None:
self.opcode_info[prefix] = []
opcodes = self.opcode_info[prefix]
if None == gen:
inf = Opcode(prefix, opcode)
opcodes.append(inf)
elif gen == self.gen:
# Replace for ext generator
found = False
found_index = 0
for i in range(len(opcodes)):
if opcodes[i].code == opcode:
found = True
found_index = i
if found:
inf = Opcode(prefix, opcode)
opcodes[found_index] = inf
else:
sys.stderr.write("[%s] Cannot find opcode: %s%s\n" % (gen, prefix, opcode))
sys.exit(1)
else:
inf = Opcode(prefix, '/dev/null')
def pre_process(self, iline):
out = []
@ -220,39 +251,72 @@ class OpcodeList:
out.append(iline.with_str(line))
return out
def switch_prefix(self, prefixes, reenter: bool, f):
prefix_switch = len(prefixes) > 1
if prefix_switch:
print("switch (u8(m_ref >> 16)) // prefix", file=f)
print("{", file=f)
for prefix in prefixes:
is_rop = prefix == 'ff'
if prefix_switch:
print("case 0x%s:" % (prefix), file=f)
print("{", file=f)
if not is_rop:
print("\tswitch (u8(m_ref >> 8)) // opcode", file=f)
print("\t{", file=f)
for opc in self.opcode_info[prefix]:
# reenter loop only process steps > 0
if not reenter or opc.with_steps():
if not is_rop:
print("\tcase 0x%s:" % (opc.code), file=f)
opc.save_dasm(step_switch=reenter, f=f)
print("\t\tcontinue;", file=f)
print("", file=f)
if not is_rop:
print("\t}", file=f)
if prefix_switch:
print("} break; // prefix: %s" % (prefix), file=f)
print("", file=f)
if prefix_switch:
print("} // switch prefix", file=f)
def save_exec(self, f):
prefix = None
print("if (m_wait_state)", file=f)
print("{", file=f)
print("\tm_icount = 0; // stalled", file=f)
print("\treturn;", file=f)
print("}", file=f)
print("while (true) {", file=f)
print("switch (u8(m_ref >> 16)) // prefix", file=f)
print("{", file=f)
for opc in self.opcode_info:
if (opc.code[:2]) != prefix:
if prefix is not None:
print("\n\t}", file=f)
print("\t\tbreak;", file=f)
print("", file=f)
print("}", file=f)
print("break; // prefix: 0x%s" % (prefix), file=f)
print("", file=f)
prefix = opc.code[:2]
print("case 0x%s:" % (opc.code[:2]), file=f)
print("{", file=f)
print("\tswitch (u8(m_ref >> 8)) // opcode", file=f)
print("\t{", file=f)
print("\tcase 0x%s:" % (opc.code[2:]), file=f)
opc.save_dasm(f)
print("\t\tgoto rop;", file=f)
print("", file=f)
print("\t} // switch opcode", file=f)
print("}", file=f)
print("break; // prefix: 0x%s" % (prefix), file=f)
print("", file=f)
print("} // switch prefix", file=f)
print("const bool nomemrq_en = !m_nomreq_cb.isunset();", file=f)
print("[[maybe_unused]] const bool refresh_en = !m_refresh_cb.isunset();", file=f)
print("const bool busack_en = !m_busack_cb.isunset();", file=f)
print("", file=f)
print("bool interrupted = true;", file=f)
print("while (u8(m_ref) != 0x00) {", file=f)
print("\t// workaround to simulate main loop behavior where continue statement relays on having it set after", file=f)
print("\tif (!interrupted) {", file=f)
print("\t\tm_ref = 0xffff00;", file=f)
print("\t\tcontinue;", file=f)
print("\t}", file=f)
print("\tinterrupted = false;", file=f)
print("", file=f)
self.switch_prefix(self.opcode_info.keys(), reenter=True, f=f)
print("", file=f)
print('assert((void("switch statement above must cover all possible cases!"), false));', file=f)
print("}", file=f)
print("", file=f)
print("if (m_ref != 0xffff00) goto process;", file=f)
print("", file=f)
print("while (true) {", file=f)
print("", file=f)
print("\t\t// unwrapped ff prefix", file=f)
self.switch_prefix(['ff'], reenter=False, f=f)
print("", file=f)
print("process:", file=f)
self.switch_prefix(self.opcode_info.keys() - ['ff'], reenter=False, f=f)
print("", file=f)
print("} // while (true)", file=f)