Menu

#131 Allow the Spectrum to talk to the emulator

future
open
None
5
2018-06-28
2018-06-23
thrice
No

Would it be possible, and if so what would the approach be, to allow the Spectrum to talk to the emulator?

I have a particular usecase in mind: when the program I'm developing hits an assert() I want it to cause the emulator to dump the contents of the Spectrum's memory out to a file so I can inspect it using tools on Linux. Fuse has the memory dump feature (File->Save binary data) but it needs to be run manually which isn't too efficient in my usecase.

So I was thinking, could the assert() code running on the Spectrum's Z80 somehow signal to the emulator that something should happen? Ideally the "something" should be programmable or configurable, since I can see not everyone would want a memory dump.

From the lay perspective, it feels it should be possible to have the emulator "plug in to the Spectrum's edge connector" and act like a piece of hardware which can be written to or utilised. I don't know if this is practical, or whether there's another way which is easier?

Discussion

  • Philip Kendall

    Philip Kendall - 2018-06-24

    We're actually quite close to this at the moment. If you start fuse with the --debugger-command option, you can pass something like

    break <address of assert failure>
    commands 1
    <do stuff>
    end
    

    Unfortunately, "do stuff" doesn't yet support "save binary data" so this doesn't quite do what you want it to. It would be relatively easy to add that, but rather than making Fuse's debugger a fully featured scripting language, I'm looking at ways to call Python code on a breakpoint... watch this space.

     
  • Philip Kendall

    Philip Kendall - 2018-06-27
    • assigned_to: Philip Kendall
     
  • Philip Kendall

    Philip Kendall - 2018-06-27

    OK, here we go :-)

    • Ensure you've got the Python development headers installed on your system (python3-dev if you're a Debian person). Tested with Python 3, might work with Python 2.
    • Check out the feature-131-embedded-python branch from git
    • Run Fuse as PYTHONPATH=. ./fuse breakpoint.tap --debugger-command $'break 49153\nhook 1 1\nbreak 49154\nhook 2 1'
    • RUN the not very complicated BASIC.
    • Watch the screen turn blue and the attributes area be dumped to a file.

    To customize what happens, edit debugger.py - the breakpoint function will be called every time a Fuse breakpoint is hit with the ID of the breakpoint hit.

    This is obviously very much WIP at the moment, but I think it demonstrates what can be done relatively easily - extending this further isn't going to be hard.

    Let me know if this is the sort of thing you're after.

    [ Update 2018-06-28: no special arguments needing when configuring any more, now detected via autotools ]
    [ Update 2018-06-30: need to explicitly enable Python hooks ]
    [ Update 2018-07-02: file writing now needs to happen in Python, see debugger.py for example ]

     

    Last edit: Philip Kendall 2018-07-02
  • thrice

    thrice - 2018-06-28

    Goodness. I like your service... :)

    That works a treat. In my game loop code I added this where I appear to have a problem:

    void gl_assert(void)
    {
      while(1);
    }
    ...
        case MOVE_LEFT:
    gl_assert();
          game_state->runner->xpos--;
    

    then pick the address of the assert function from the compiler's map file with this:

    PYTHONPATH=. ./fuse --debugger-command "break $(grep -P '^_gl_assert' zxrunner.map | perl -ne '/(\$\w\w\w\w)/ && print "$1"')"
    

    I tweaked your little python script to dump the whole memory space (I use the ROM area for a trace table, which is the data I actually need) and, well, it just worked. For what I'm doing, that's going to be incredibly useful.

    I looked at the code you've added and see that you appear to have used my request as a single getting-started sort of example. I guess the work now is to devise a generic interface for Python which will allow it to interact with the emulator in the way other people might find useful.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.