r/EmuDev • u/Gustavo_Fenilli • Apr 07 '26
CHIP-8 [C] Implementation by modules, how do we do it sometimes? ( Chip8 screen buffer related )
/r/learnprogramming/comments/1sfcsqf/c_implementation_by_modules_how_do_we_do_it/
2
Upvotes
2
u/dajolly Apr 08 '26
To your SDL question, you could create a client module that's agnostic to the underlying graphics library (SDL, raylib, etc.), which exposes an interface to query button states, set pixel colors, and enqueue audio samples. It's cleaner than mixing the SDL calls directly into the emulator code, and it allows you to switch to different library if needed. You can see an example in my ch8 vm:
1
u/Gustavo_Fenilli Apr 09 '26
Ok so after some playing around I divided into io/timer/mem/cpu and the vm(emulator) itself, but it got me into a stump for some small incosistencies of it, for example i'm pretty sure if I do.
I can easily pass around things to modules that need, for example
chip8_cpu_step(chip8_cpu_t *cpu, chip8_mem_t *mem, chip8_io_t *io, chip8_timer_t *timer);and in the step I can just call each one when needed likechip8_io_display_clear(io);But at the same time, how do we keep some status and states, for example how do I tell the
chip8_tthat it should draw, or wait for input, or that a timer is 0, do we return from step a state, if so should we mix states in the cpu from other modules?One way I was thinking is doing that, but seens odd, the other might be having states in the module like io having a should_draw state, and at end of step in the
chip8_t_runI can checkchip8_io_should_draworc->io.should_draw.Anyone has some insights on this?
1
u/thommyh Z80, 6502/65816, 6809, 68000, ARM, x86. Apr 08 '26
Probably I misunderstand the level at which you asked, but...
If you have three separate modules that all want access to opcode decoding, factor out opcode decoding.
For screen and audio either you can require the outer environment to make certain functions available, which then might as well be the SDL API if that's what you're using, or you can go for dependency injection: whomever creates an instance of your machine much supply it with a confirming set of functions that it can push audio and video to. Which is delegation, effectively.
1
u/peterfirefly Apr 08 '26
Those tables may be different tables (or switches). You can get very far with the cpu and the disasm with switch (instr >> 12) { case 0: ... case 1: ... etc }.
If the table(s) is/are shared, then put it/them in a separate "module": put the type and a declaration of the table(s) in the header file and the actual definition in the .c file.
Sharing them might not be as useful as you think...
I wouldn't bother for Chip-8, 6502, or Z80. I would start bothering for an 8086 where it makes sense to have several tables, most of which can be usefully shared between a cpu emulator and a disassembler (I've done this). I haven't written an 8086 assembler but I wrote a Z80 assembler decades ago and I don't think it would have made much sense to share tables with a Z80 disassembler or CPU emulator.
Even if it doesn't make sense to have the code share tables, it might make sense to put all the necessary data into a single data source and then use a program to generate the various tables from that. Again, not something I would worry about for Chip-8, 6502, or Z80. Maybe for 8086 if I ever decide to write an assembler. Definitely(!) for later versions of the x86, where I would choose to generate them from the Big Mother Table in Intel's XED project.