What does HackerNews think of chips?
8-bit chip and system emulators in standalone C headers
It's a good way to document old hardware with emulation code.
8-bit chip and system emulators in standalone C headers - https://github.com/floooh/chips
Instead of a pure data description I have python scripts which generate C source code. The 6502 is perfect for code generation because instructions are very uniform, and the "interesting" part of instructions are the addressing modes which always run the same sequence of operations in front of the actual instruction-specific "payload". The Z80 instruction set has many more special cases, but it can be decoded "algorithmically" as well, see here:
http://www.z80.info/decoding.htm
My Z80 emulator basically implements this "recipe" in python, and generates a huge "unrolled" switch-case statement with one case-branch per instruction (ok not quite, the CB prefix instruction range is still decoded algorithmically to reduce the resulting binary code size a bit).
Complex instruction logic like DAA are still essentially hand-written C functions though, the code generation mainly helps with the "mundane" parts of an instruction, like opcode fetch, and regular memory load/store machine cycles.
One nice side effect of using code generation is that it is very easy to create variations of the emulator. For instance I created a cycle-stepped version of my 6502 emulator (versus the previous instruction-stepped version) with surprisingly few changes to the code-generation script.
PS: the whole stuff is here:
https://github.com/floooh/chips
PPS: an interesting approach (which I haven't tried) for 6502 emulation would be to use the 6502's decode ROM (aka PLA) as the "base-data" for code generation.
The emulators are written in C, not Rust, but I bet this approach would also work perfectly with Rust's borrow checker (since there's no shared state in memory the borrow checker shouldn't even "activate"). An emulator is essentially built from functions which take a 64-bit integer as input, and return another 64-bit integer.
Right now the emulators are all in JavaScript (except for MAME) which makes integrating with the IDE's debugging tools a little easier. It's performant enough for now, since we only have 8-bit platforms at low clock speeds.
Integrating a C emulation library like https://github.com/floooh/chips should be doable, since it doesn't have any dependencies.
Shameless plug: I've started writing easy to integrate 8-bit chip emulators in standalone C headers a little while ago:
https://github.com/floooh/chips
For instance used in these WASM emulators:
https://github.com/floooh/chips-test/blob/sokol-app/examples...
The pure 8-bit chip emulators are here:
https://github.com/floooh/chips
You can also start with the 'proper' emulator this is based on:
https://github.com/floooh/yakc
This has all the links to the underlying software.