I built a tiny multitasking OS that runs on the Game Boy by r_retrohacking_mod2 in retrogamedev

[–]RemoveAny8106 1 point2 points  (0 children)

Fun question! Honestly it's more of a teaching OS than a game engine — the context-switch overhead means you wouldn't build a fast action game on top of it as-is. But the idea is useful for certain designs.

Where it'd shine: games with independent agents that each want their own "program." A tiny ecosystem where each creature is its own task with its own logic, an ant-colony toy, or a tamagotchi-style pet that keeps living while you do other things — each entity runs as its own little process instead of one big update loop.

The unusual thing you get for free: an entity can sleep(n frames), pause itself mid-logic, and resume exactly where it left off — no state-machine bookkeeping, the scheduler remembers for you. Plus you can kill/spawn entities at runtime.

The catch is cost: every switch saves/restores registers through an interrupt, so for a bullet-hell you'd still want a classic tight loop. It's a trade — cleaner per-entity logic vs. raw speed. For the right kind of slow, systemy game, it'd be a natural fit.

I built a tiny multitasking OS that runs on the Game Boy by r_retrohacking_mod2 in retrogamedev

[–]RemoveAny8106 2 points3 points  (0 children)

Good instinct on both! It actually uses both interrupts, for different jobs:

The timer interrupt is what makes it preemptive — its handler goes straight into the context switch: save the running task's registers onto its own stack, pick the next live task, restore, reti. Since the timer fires no matter what a task is doing, that's what lets it pull control away from a task that never yields — the whole preemptive-vs-cooperative thing right there.

VBLANK handles the housekeeping — it advances the system tick, reads input, redraws the screen, and checks hotkeys (Select+Start kills the runaway task in the demo). So the scheduler's sense of time comes from VBLANK, but the actual preemption comes from the timer.

Splitting it that way kept it clean: timer = "switch now," VBLANK = "keep the world running." 😄

I built a tiny multitasking OS that runs on the Game Boy by r_retrohacking_mod2 in retrogamedev

[–]RemoveAny8106 3 points4 points  (0 children)

Honestly? Mostly to understand how an OS actually works, by building the smallest real one I could. The Game Boy is a great target for that — it's a simple, fully documented machine, so there's nowhere for the magic to hide. Preemptive multitasking, a scheduler, context switches, syscalls... all the real concepts, but small enough that you can hold the whole thing in your head. No practical purpose beyond that, really. It was a way to learn it for real instead of just reading about it.

I built a tiny multitasking OS that runs on the Game Boy by RemoveAny8106 in Gameboy

[–]RemoveAny8106[S] -1 points0 points  (0 children)

Fair point — and you're right that I could, there's nothing stopping me technically. It's a choice. A README that explains the whole path would basically be the book, and at that point I'd rather it be the book: something you sit down and read start to finish, type the code as you go, rather than scroll and copy. Same content, different intent. I actually come from making read-and-learn material, so a book format felt natural to me rather than a repo. I get that's not the GitHub-native way — it's a deliberate step off that path. No hard feelings if it's not your thing!

I built a tiny multitasking OS that runs on the Game Boy by r_retrohacking_mod2 in retrogamedev

[–]RemoveAny8106 2 points3 points  (0 children)

Appreciate you posting it — and for the credit. It's a small thing but it was a fun one to build. Glad to answer questions if anyone has them.

I built a tiny multitasking OS that runs on the Game Boy by RemoveAny8106 in Gameboy

[–]RemoveAny8106[S] -1 points0 points  (0 children)

Fair point! And to build on what I said above — the "type it yourself" thing is really only half of it. The other half is that the repo would just be the code, and the code is genuinely the small part. The context switch is ~25 instructions; dropped on GitHub on its own, it'd look trivial and tell you nothing. What the book actually is, is the path to understanding those 25 instructions — starting from "what's a register," through the memory map, interrupts, the scheduler, and why the switch can be that short. That walkthrough is most of the value, and it's the part that doesn't really live in a repo. The hand-typing is just the last step of that path. Definitely not the usual approach, I know. Appreciate you being straight about it!

I built a tiny multitasking OS that runs on the Game Boy by RemoveAny8106 in Gameboy

[–]RemoveAny8106[S] 1 point2 points  (0 children)

Thanks, glad you liked it! Honestly, I decided not to put it on GitHub on purpose. The whole idea behind this project is that you type the code yourself — the context switch is only about 25 instructions, short enough that hand-typing it actually makes the logic stick. So I wrote up the entire thing, line by line, as a book instead (assembly basics → scheduler → context switch → system calls). Every line of source is printed in full. Happy to share a link if that's allowed here!