4 ms·
I've seen code for emulators that is very long functions and a lot of globals; I'd also argue they're OK if your program flow is very stable / predictable and s
by Cthulhu_ 2y ago
I've seen code for emulators that is very long functions and a lot of globals; I'd also argue they're OK if your program flow is very stable / predictable and synchronous, and especially older consoles and game engines have predictable phases. Getting information from your "tick" to your "render" stage is a lot easier if you have global state.
- pjc50 2y agoEmulators are often emulating "hardware globals", things like machine registers or GPU state where in the thing being emulated there really is only one instance.
- flohofwoe 2y agoUnless it's an arcade machine like Bombjack which has two Z80s and three AY sound chips ;) It's still possible to wrap all that as nested state in a single global, and it works just fine: https://github.com/floooh/chips-test/blob/613dbfe907104f4110681a6c883c23c9c5cdb4e8/examples/sokol/bombjack.c#L38-L47 https://github.com/floooh/chips-test/blob/613dbfe907104f4110... That's a single mutable global with all application state, but inside that global are nested structs and the code working on those structs takes essentially a 'this' pointer. This approach works well since it enforces a strictly hierarchical code and data structure with a single root-struct (and also a single root-source-file) at the top.
- Joker_vD 2y agoA useless microbenchmark question: what is faster, accessing a global variable (which has to go through the GOT nowadays) or dereferencing a field in a structure that was passed to you by a pointer (a straightforward register+offset load)?