Z80 Simulator on BBC BASIC
Michael Grunditz (467) 531 posts |
I have started to write a Z80 simulator with BBC BASIC.. As a simple start I wrote support for three instructions: “LD A” “INC A” and “JP” Just because of fun! Source: http://www.update.uu.se/~micken/z80.txt Some test assembler and simulator output: |
nemo (145) 2546 posts |
Here’s some suggestions: 1. Use an array of bytes (a DIM) instead of an array of integers, and read your whole memory snapshot in one go: DIMmem%65535 FORZ%=0TO65535:mem%?Z%=ihalt%:NEXT F%=OPENIN"z80test/bin" SYS"OS_GBPB",3,F%,mem%,mem%+65535 CLOSE#F% 2. CASE/WHEN/ENDCASE is better than lots of IFs pc%=0:halt%=FALSE REPEAT CASE mem%?pc% OF WHENilda% ... WHENiinca% ... WHENijp% ... WHENihalt%:halt%=TRUE ENDCASE pc%=(pc%+1)AND65535 UNTILhalt% The fun bit will be getting the right flag results for each operator. Basic is much too slow of course, but the other reason for writing an emulator like this in machine code is that the ARM chip will set a lot of the flags for free. That’s a bit less work for the programmer. |
Rick Murray (539) 13840 posts |
It’s worse than that – it’s an array of floats! But, yes, you can do byte indirection as nemo has demonstrated, so that’s the best way forward. It also makes loading and saving snapshots easy as you can just block read and block write the memory.
Does the Z80 offer a fruitcake BCD mode like the 6502? I didn’t bother to include that in my (rubbish) 6502 core, I’m pretty sure the FileStore code doesn’t use it. The fun bit, for me, would be sorting out what all the addressing modes are doing and having it run at a useful speed (another reason why loading data into an array isn’t a great idea).
No, write it in machine code and the programmer’s work is spread out over time. Namely each time something in the ARM changes that the code upsets, plus as I’ve said almost endlessly to some people, when you’re coding in assembler you have to pay attention to everything. Not just your program and what you want it to do, but a ton of stupid stuff like what gets stacked and unstacked, what the flags are, when to preserve stuff, blah blah. The sort of issues that we invented compilers to deal with.
Of course. Because nobody ever created an ARM processor emulation in BBC BASIC. No, nobody would be that silly. Personally I’d write it in C, you could use a big CASE for the opcodes, define them all in order with no fall through and any compiler worth using ought to turn that into a jump table. That said, the advantage of BASIC is that it is easy to understand. If Michael is starting from scratch, it may be best to use an easy and familiar language to allow him to concentrate on the concepts and how best to do stuff (the 6502 has some interesting side effects of how it accesses memory, the Z80 no doubt has its own quirks). Then when the core is proven and it runs stuff (even if slowly), that’s the time to think of optimisation or rewriting it in C or whatever. Go for it Michael. ;-) |
nemo (145) 2546 posts |
GameBoy was a bad choice – it uses a weird processor that’s half-way between an 8080 and a Z80. Here are some good Z80 emulations. |
Michael Grunditz (467) 531 posts |
Initial idea was to do 1 to 1 emulation in assembler . Just wanted to see how simple it would be in basic. Gah writing on phone, will edit later. |
Chris Evans (457) 1614 posts |
I knew Acorn during the design stage of ARM1/2 created an ARM processor emulation for the Master Turbo but I didn’t realise it was in BASIC! |
Steffen Huber (91) 1953 posts |
http://z80-heaven.wikidot.com/instructions-set:daa About Z80 emulators: I’d have a look at CPCEmu by Andreas Stroiczek (who also did a Gameboy emulator). Its Z80 CPU emulation should be cycle correct and complete (including the “illegal” opcodes). 26bit only of course: http://www.huggers-world.de/files/cpcemu121.zip |
Michael Grunditz (467) 531 posts |
I have extended a bit: I am running this asm:
I get this output: W How can I print without linebreaks? |
Michael Grunditz (467) 531 posts |
AH PRINT “xx”; >RUN |
nemo (145) 2546 posts |
Instead of PRINT CHR$(a); just use VDU a
|
Michael Grunditz (467) 531 posts |
Yes VDU is better for simulating a terminal. I haven’t done any of the BASIC improvements mentioned in this thread , yet. But I will do that, if I don’t switch to assembler directly. |