Hacker News new | ask | show | jobs
by Dwedit 25 days ago
Monster Max on Game Boy happens to use unrolled "ld de,NNNN / push de" loops constructed in main memory to write to video ram.

For those unfamiliar with Monster Max (1994, Game Boy), it's basically the last of the British Isometric Platformers, a genre started by Knight Lore on ZX spectrum.

2 comments

Ooh, might steal that for my gameboy experiments if I get stuck in some perf-problem (right now I have asynchronous upload queueus that works most of the time since things are queued well ahead of time so it's updates outside of the screen).

For reference, unlike the PC,C64,etc f.ex. the Gameboy (and NES) only allows users to write memory during VBlanks/HBlanks (or when the screen is turned off), so instead of delaying and adding complicated logic for that due to memory usage contention the CPU is simply cut-off from the memory (thus reads/writes at the wrong time are just nonsense/discarded iirc).

A fast memcpy for the Game Boy makes use of pop instructions for the same purpose. Notably not push instructions, because pop is actually faster and you only have one stack pointer so you're better off using it for the source address (popping 16 bits into two registers at a time) than the dest.

You can also get savings by using the "store and increment HL" combined instruction, which saves time over "inc HL", but this requires you to move from the popped registers into A (this instruction only works with the A register). You can't pop directly into the A register because that pop variant, "pop AF", pops into A plus the hidden flags register which is not helpful.

So it looks like:

  pop DE
  ld A, D
  ld [HL+], A
  ld A, E
  ld [HL+], A