# Z80 A fast and accurate instruction-stepped Z80 emulator written in C11 and ported to Rust with c2rust and manual refactoring. This emulator passes both the ZEXDOC and ZEXALL Z80 instruction tests. It has also been verified correct against VisualZ80, and is known to run the entire ColecoVision, SG-1000, Master System, and Game Gear libraries. Additionally, it has been successfully integrated into Mega Drive/Genesis and Neo Geo emulators with unknown (but likely high or perfect) compatibility. ## Licensing This project is under the MIT license, except the files in `roms` which are provided for convenience to test the z80 core implementation. These files authors' and licenses can be seen in the source files (.z80/.src files). This emulator is a port of [carmiker/jgz80](https://github.com/carmiker/jgz80) ## Tests You can run the tests by running `cargo test --release -- --nocapture`, which outputs: ``` *** TEST: roms/prelim.com Preliminary tests complete *** 899 instructions executed on 8721 cycles (expected=8721, diff=0) *** TEST: roms/zexdoc.cim Z80doc instruction exerciser hl,.... OK add hl,.......... OK add ix,.......... OK add iy,.......... OK aluop a,nn.................... OK aluop a,.. OK aluop a,..... OK aluop a,(+1)........... OK bit n,(+1)............. OK bit n,.... OK cpd........................ OK cpi........................ OK ............. OK a................... OK b................... OK bc.................. OK c................... OK d................... OK de.................. OK e................... OK h................... OK hl.................. OK ix.................. OK iy.................. OK l................... OK (hl)................ OK sp.................. OK (+1)......... OK ixh................. OK ixl................. OK iyh................. OK iyl................. OK ld ,(nnnn)............. OK ld hl,(nnnn).................. OK ld sp,(nnnn).................. OK ld ,(nnnn)............. OK ld (nnnn),............. OK ld (nnnn),hl.................. OK ld (nnnn),sp.................. OK ld (nnnn),............. OK ld ,nnnn......... OK ld ,nnnn............... OK ld a,<(bc),(de)>.............. OK ld ,nn.... OK ld (+1),nn............. OK ld ,(+1)...... OK ld ,(+1).......... OK ld a,(+1).............. OK ld ,nn....... OK ld ,........ OK ld ,........ OK ld a,(nnnn) / ld (nnnn),a..... OK ldd (1).................... OK ldd (2).................... OK ldi (1).................... OK ldi (2).................... OK neg........................... OK ..................... OK ........... OK shf/rot (+1)........... OK shf/rot .. OK n,..... OK n,(+1)....... OK ld (+1),...... OK ld (+1),.......... OK ld (+1),a.............. OK ld (),a................ OK Tests complete *** 5764169747 instructions executed on 46734978649 cycles (expected=46734978649, diff=0) *** TEST: roms/zexall.cim Z80all instruction exerciser hl,.... OK add hl,.......... OK add ix,.......... OK add iy,.......... OK aluop a,nn.................... OK aluop a,.. OK aluop a,..... OK aluop a,(+1)........... OK bit n,(+1)............. OK bit n,.... OK cpd........................ OK cpi........................ OK ............. OK a................... OK b................... OK bc.................. OK c................... OK d................... OK de.................. OK e................... OK h................... OK hl.................. OK ix.................. OK iy.................. OK l................... OK (hl)................ OK sp.................. OK (+1)......... OK ixh................. OK ixl................. OK iyh................. OK iyl................. OK ld ,(nnnn)............. OK ld hl,(nnnn).................. OK ld sp,(nnnn).................. OK ld ,(nnnn)............. OK ld (nnnn),............. OK ld (nnnn),hl.................. OK ld (nnnn),sp.................. OK ld (nnnn),............. OK ld ,nnnn......... OK ld ,nnnn............... OK ld a,<(bc),(de)>.............. OK ld ,nn.... OK ld (+1),nn............. OK ld ,(+1)...... OK ld ,(+1).......... OK ld a,(+1).............. OK ld ,nn....... OK ld ,........ OK ld ,........ OK ld a,(nnnn) / ld (nnnn),a..... OK ldd (1).................... OK ldd (2).................... OK ldi (1).................... OK ldi (2).................... OK neg........................... OK ..................... OK ........... OK shf/rot (+1)........... OK shf/rot .. OK n,..... OK n,(+1)....... OK ld (+1),...... OK ld (+1),.......... OK ld (+1),a.............. OK ld (),a................ OK Tests complete *** 5764169747 instructions executed on 46734978649 cycles (expected=46734978649, diff=0) ``` ## Resources - [Z80 CPU User Manual](http://z80.info/zip/z80cpu_um.pdf) - [Z80 instruction set (+ timings)](http://map.grauw.nl/resources/z80instr.php) - [Z80 timings](https://docs.google.com/spreadsheets/d/1eygwsPkhpBi6oLQI1mre7kxyN11o1j0TmvXCz1aBLLY/edit?usp=sharing) - [Decoding Z80 Opcodes](http://z80.info/decoding.htm) - [anotherlin/z80emu](https://github.com/anotherlin/z80emu)