Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to enable 'hooks' for out and in instructions #56

Open
mortenjc opened this issue Jul 5, 2024 · 6 comments
Open

How to enable 'hooks' for out and in instructions #56

mortenjc opened this issue Jul 5, 2024 · 6 comments
Labels

Comments

@mortenjc
Copy link

mortenjc commented Jul 5, 2024

Hey

I am using this emulator to help revive an old Z80 based minicomputer (Q1 Lite).

I have modified the single_stepping.py example to created a disassembler and also a use it to run the code (have 8 ROM images that I can load).

Now it seems like I am likely to get stuck on the out and in instructions.

Do you have a hint on how I could emulate an IO device. For example by having a python function called
on every in and out instruction?

Thanks for the project
Morten

@mortenjc mortenjc changed the title How to enabe 'hooks' for out and in instructions How to enable 'hooks' for out and in instructions Jul 5, 2024
@kosarev
Copy link
Owner

kosarev commented Jul 5, 2024

Hi Morten, for input there's Z80Machine.set_input_callback() that sets a hook taking the port address. There's no such function for output yet, but I think I can add one -- let me know.

Alternatively, if you disassemble instructions (like in #55), you could just catch IN and OUT instructions and read/update registers as necessary on them.

@mortenjc
Copy link
Author

mortenjc commented Jul 5, 2024

Hey,
Thanks for the answer. I used the set_input_callback() - had to do some debugging to learn
that it gets a 16 bit integer where the most significant byte is the register value at the time
of the callback and the least significant is the IO address.
Ths helped me progress a little further!

@mortenjc mortenjc closed this as completed Jul 5, 2024
@mortenjc
Copy link
Author

mortenjc commented Jul 6, 2024

Just one comment. Yes it would be very useful to have a set_output_callback as it would allow me to emulate IO devices!

I've already had to do some hacking in order to capture data written to the display:

instr = b.build_instr(m.pc, bytes(m.memory[m.pc:m.pc + MAX_INSTR_SIZE]))
        instr_str = f"{instr}"
        if instr_str[:9] == 'out (0x3)':
            out03 += chr(m.a)
            print(f'; out (0x3): 0x{m.a:02X} char: ({chr(m.a)})')

But as mentioned I am very happy with the project as it helps me move forward with bringing an old and forgotten computer to life..

@kosarev
Copy link
Owner

kosarev commented Jul 17, 2024

27af8c1 adds the support for set_output_callback(). Hope it helps.

@mortenjc
Copy link
Author

Just a quick comment.
The set_output_callback() works for me. Luckily I had the same
abstraction so the code change was minimal. This is a much cleaner way.

However I do get a segmentation fault (on macos) if I run the emulator without registering the output callback.

@mortenjc mortenjc reopened this Jul 19, 2024
@kosarev
Copy link
Owner

kosarev commented Jul 19, 2024

However I do get a segmentation fault (on macos) if I run the emulator without registering the output callback.

Oops. Fixed in 99ef518.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants