I won't post the code here right now - mainly because it's a mess
- though I don't mind letting you have a look at ABUG. This seems to be what I did though :
*RUN the parasite side loader, which then :
- *LOADs the host side loader
- Poke the run address of the host side loader into &20e via OSWORD 6
- Call OSWRCH with a dummy value, which will cause the host side to enter the loader
- Disable interrupts (which are never re-enabled)
- Clear out all the tube ULA parasite input ports (probably unnecessary)
- Write a sync value (&a5) to output port 4
- Enter the main loop, polling for host commands on input port 4
The host side loader is entered following OSWRCH, and then :
- Disables all Tube interrupts by writing $0f to $ffe0
- Polls input port $ffe7 for the sync byte
- Then does all the actual loading
In-game, all communication uses polling - the host sends commands through port 4, reads & writes data to and from port 1, and synchronises with the parasite through port 2. Within a command, port 2 is initialised to 0 on both sides at the beginning, then incremented on the parasite side for every packet sent and on the host side for every packet sent. Port 1 parasite->host has a 24 byte FIFO, I've split this into 8 byte packets so the parasite is allowed to get 2 packets ahead of the host before it has to wait.
You could probably handle interrupts via IRQ1V, which would prevent the Tube ROM from interfering, but I didn't try this. If you do though, don't forget that port 3 actually triggers NMIs!