Open mortenjc opened 9 months ago
(This was originally discussed in a private email thread; I replicate the response here for visibility.)
Hi Morten,
If you just want to verbalise the instruction that is about to be executed, then I think something like the following should do.
Thanks for asking -- I think I should add some example code disassembling and executing individual instructions. It would also be nice to have means to run a disassembled instruction directly without even having it in memory!
def main():
m = z80.Z80Machine()
# This part of the emulator is still under development,
# hence the underscore in the name.
b = z80._Z80InstrBuilder()
load(m, "../../mjcgit/Q1/src/roms/IC25.BIN", 0x0000)
load(m, "../../mjcgit/Q1/src/roms/IC26.BIN", 0x0400)
load(m, "../../mjcgit/Q1/src/roms/IC27.BIN", 0x0800)
load(m, "../../mjcgit/Q1/src/roms/IC28.BIN", 0x0C00)
while True:
# Decode the instruction.
MAX_INSTR_SIZE = 4
instr = b.build_instr(m.pc, bytes(m.memory[m.pc:m.pc + MAX_INSTR_SIZE]))
# Get and verbalise the instruction bytes.
instr_bytes = bytes(m.memory[instr.addr:instr.addr + instr.size])
instr_bytes = ' '.join(f'{b:02X}' for b in instr_bytes)
# Execute the instruction.
# Limit runs to a single tick so each time we execute exactly one instruction.
m.ticks_to_stop = 1
m.run()
# Print the instruction, its address and bytes and registers after execution.
# print('0000 C3 E5 01 ; JP 01E5 | PC:01E5, SP:0000, A:00, BC:0000, DE:0000 HL:0000, S Z PV N: 0 0 0 0')
print(f'{instr.addr:04X} {instr_bytes:12} ; {str(instr).upper():14} '
f'| PC:{m.pc:04X}, SP:{m.sp:04X} {m.memory[m.pc]:02X} ...')
data = m.memory[m.pc] + (m.memory[m.pc+1] << 8) + (m.memory[m.pc+2] << 16) + (m.memory[m.pc+3] << 24)
if data == 0:
print(f'all zeroes at {m.pc:04x}, exiting ...')
sys.exit()
I just downloaded your emulator which I plan to use for understanding an ancient computer called Q1. This is part of a danish computer history project.
I managed to write a brief loader code to load the ROMs into memory and then use the single_step python example to get started.
However it is not exactly clear to me how to customise this to my needs and maybe you could advise me on this?
The code currently looks like this:
And produces output like this:
However I'd like to be able to produce output like this:
which is from an early attempt to write my own emulator. I realised that a) I was probably not smart enough to do this correctly and 2b) there are plenty of emulators 'out there', this being one of them :-)
But I could not understand from looking at your code how I can adapt the single_step code to print out 1) just the actually used bytes 1, 2, 3 or 4 according to the opcode and 2) how to integrate the disassembler to print out the mnemonics
I hope you can help to shed some light on this.
Thanks for making this project available
Best
Morten