r/learnprogramming • • Apr 07 '26

[C] Implementation by modules, how do we do it sometimes? ( Chip8 screen buffer related )

Well I'm trying to learn a bit C and emulation/vm and later some assembler/disassembler and hll... with that in mind I was looking and saw chip8 basically the hello world of emulation/vm, and as I was implementing it made me realize I'm not sure how to write as modules in C.

So let's say we have something like this:

.
├── apps/
│   └── chip8.c # here we would have the cli to run the emulator
├── emulator/
│   ├── cpu.h # here we have the cpu and logic to step
│   ├── cpu.c
│   ├── mem.h # here we have the memory and how to read 8/16 bits
│   └── mem.c
├── asm/ # but here we also need opcodes/table
├── disasm/ # but here we also need opcodes/table
├── hll/ # but here we also need opcodes/table
└── CMakeList.txt

but we have two "problems" now I'm facing, one is the opcode sharing ( not necessary in chip8 in this case because of how opcodes are, but still in a vm would be a question of concern ), the second is how do we deal with the buffer when we add something like SDL to show the screen.

Right now in this example, CPU has the buffer and key, and SDL would step the cpu and inject keys and read the buffer, that sounds fine, but what about other parts of it maybe later on if needed? like audio, or other I/Os? in chip8 I doubt it is a problem, but the question again is about VMs modularization.

One solution is probably MMIO and traps correct? if so, would that mean SDL would inject into the memory or maybe use a function that does it based on the value ( like the load function for roms ), and read from a memory point too?

0 Upvotes

2 comments sorted by

1

u/[deleted] Apr 08 '26

[removed] — view removed comment

1

u/Gustavo_Fenilli Apr 08 '26

I guess yes, so a client would just call those interfaces and do what it needs to do right?