On that subject I note that some of the temporaries used during the execution of an emulated instruction are also globals. Compared to offsets from the stack pointer, as might be the case in unoptimised code, these would be faster but with local variables allocated to registers local variables would be faster.hoglet wrote:I guess part of the reason for Sarah duplicating the 6502.c and 65tube.c was for efficiency, so the registers end up being global variables (known at link time) rather than members of a struct accessed via a pointer. It saves a level of indirection.
One way to have the same code executing with a different set of global variables would be to use a common name in a body of code and the #define this to different actual variables in two further files each of which #includes the first file. I don't know if that particular boat has sailed, though, in that now 6502.c and 6502tube.c have their common ground and their differences.
I was suggesting the 65816 code for the tube 6502 because there can presumably be only one tube processor executing at a time, so if two of more logical tube processors, because they share code, share the registers that wouldn't be a practical problem and other things that are referenced in places through the code, like incrementing cycle counts, checking for interrupts, should be similar.
Bringing the main 6502 into the scheme would be harder because then we do need to have more than once instance of the same code running at the same time referring to different variables. That either means some macros and the #include trick or a struct to save state to when the processor is not executing (externalise, I assume, from your Z80 example) and then load these back into local variables (internalise), which we hope will be CPU registers, when this particular CPU runs again.
There is also the issue of polltime, i.e. checking if other modules need to be called because of the number of clock ticks that have passed.
One possibility would be a file that contains small, static, functions for all the instructions common to both one tube processor and one main processor, which is probably most of them. That could be #included in both 65816.c and in 6502.c each of which use #define to adjust for such differences as what globals are used for the registers, which cycle count to increase, polltime, and the memory access functions.