Skip to content

Commit

Permalink
minor code cleanup in z80_gen.py
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Jan 2, 2025
1 parent 51c9ef1 commit 24f31f2
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions codegen/z80_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ def __init__(self, name, cond, flags):
self.prefix = ''
self.multiple = False
self.multiple_first_op_index = -1
self.num_cycles = 0
self.num_steps = 0
self.step_index = -1
self.extra_step_index = -1
self.mcycles = []
Expand Down Expand Up @@ -263,13 +261,6 @@ def expand_optable():
op_index += 1; stampout_op('', -1, op_index, find_opdesc('int_im2'))
op_index += 1; stampout_op('', -1, op_index, find_opdesc('nmi'))

# compute number of tcycles in an instruction
def compute_tcycles(op):
cycles = 0
for mcycle in op.mcycles:
cycles += mcycle.tcycles
return cycles

# generate code for one op
def gen_decoder():
indent = 2
Expand Down Expand Up @@ -298,40 +289,39 @@ def add(action):
if flag(op, 'redundant'):
next_step = OPS[op.multiple_first_op_index].extra_step_index
action = action.replace("$NEXTSTEP", f'{next_step}')
l(f'case {cur_step:4}: {action}_goto({next_step}); // {op.name} T:{op_step}')
l(f'case {cur_step:4}: {action}_goto({next_step}); // {op.name} ({op_step})')
cur_step += 1
else:
# do not write a payload for redundant ops
if not flag(op, 'redundant'):
next_step = cur_extra_step + 1
action = action.replace("$NEXTSTEP", f'{next_step}')
lx(f'case {cur_extra_step:4}: {action}_goto({next_step}); // {op.name} T:{op_step}')
lx(f'case {cur_extra_step:4}: {action}_goto({next_step}); // {op.name} ({op_step})')
cur_extra_step += 1
op_step += 1

def add_fetch(action):
nonlocal cur_step, cur_extra_step, op_step, op
if op_step == 0 and not flag(op, 'special'):
l(f'case {cur_step:4}: {action}_fetch(); // {op.name} T:{op_step}')
l(f'case {cur_step:4}: {action}_fetch(); // {op.name} ({op_step})')
cur_step += 1
else:
lx(f'case {cur_extra_step:4}: {action}_fetch(); // {op.name} T:{op_step}')
lx(f'case {cur_extra_step:4}: {action}_fetch(); // {op.name} ({op_step})')
cur_extra_step += 1
op_step += 1

def add_stepto(action):
nonlocal cur_step, cur_extra_step, op_step, op
if op_step == 0:
l(f'case {cur_step:4}: {action}goto step_to; // {op.name} T:{op_step}')
l(f'case {cur_step:4}: {action}goto step_to; // {op.name} ({op_step})')
cur_step += 1
else:
lx(f'case {cur_extra_step:4}: {action}goto step_to; // {op.name} T:{op_step}')
lx(f'case {cur_extra_step:4}: {action}goto step_to; // {op.name} ({op_step})')
cur_extra_step += 1
op_step += 1

for op in OPS:
op_step = 0
op.num_cycles = compute_tcycles(op)
op.step_index = cur_step
op.extra_step_index = cur_extra_step

Expand All @@ -341,7 +331,7 @@ def add_stepto(action):
pass
elif mcycle.type == 'mread':
addr = mcycle.items['ab']
store = mcycle.items['dst'].replace('_X_', '_gd()')
store = mcycle.items['dst']
add('')
add(f'_wait();_mread({addr});')
add(f'{store}=_gd();{action}')
Expand All @@ -357,7 +347,7 @@ def add_stepto(action):
add('')
elif mcycle.type == 'ioread':
addr = mcycle.items['ab']
store = mcycle.items['dst'].replace('_X_', '_gd()')
store = mcycle.items['dst']
add('')
add('')
add(f'_wait();_ioread({addr});')
Expand Down Expand Up @@ -390,7 +380,6 @@ def add_stepto(action):
else:
# regular case, jump to the shared fetch block after the
add_fetch(f'{action}')
op.num_steps = op_step
return { 'out_lines': out_lines + out_extra_lines, 'max_step': cur_extra_step }

def extra_step_defines_string(max_step):
Expand Down

0 comments on commit 24f31f2

Please sign in to comment.