Login
You're viewing the code4lib.social public feed.
  • Nov 24, 2026, 5:49 PM

    attached is a relevant screenshot from the schematic of the game boy pocket, which is functionally identical to the DMG in this regard. and here's the community documentation on the software interface to the joypad gbdev.io/pandocs/Joypad_Input. (the $ff00 i/o register is more or less directly connected to the P10, P11, P12, P13, P14 and P15 i/o pins of the SoC; P10–P13 are inputs, P14 and P15 are outputs)

    screenshot of the game boy pocket schematic, showing eight spst switches, whose right inputs are connected to one of P10, P11, P12, P13; half of them are connected to diodes controlled by P14, the other half to diodes controlled by P15
    💬 1🔄 0⭐ 0

Replies

  • Nov 24, 2026, 5:52 PM

    (strictly speaking, imo, emulating the SoC only requires emulating these pins as inputs/outputs, not emulating their "semantics" as button inputs, or emulating the external support hardware on the PCB. i think i'll leave the latter to the interactive input/display component of the emulator, rather than baking it into the SoC component itself, i.e., the front-end will need to look at the emulator state to know which buttons to send to which inputs)

    💬 1🔄 0⭐ 0
  • Nov 24, 2026, 6:01 PM

    what's actually vexing me right now is this bit from the gate-level reverse engineer of the DMG SoC, which implements the joypad interrupt (INT_JP). (the official docs say that the joypad interrupt is triggered when any button is pressed.) the PXX_IN wires come straight from the SoC's input pins, so the OR gate on the left is active when any button is pressed. BUT then there are four flipflops fed from the OR gate, connected up like a 4-bit counter...

    screenshot of a schematic, as described in post
    💬 2🔄 0⭐ 0
  • Nov 24, 2026, 6:09 PM

    ... but only the 1st and 4th bit of the counter go to the AND gate that triggers the interrupt. the effect, if i'm reading it right, is that to trigger the joypad interrupt, a button must be pressed *now* but also *3 clocks ago*. this *looks* like an anti-bounce measure, but it leads to a weird edge case: for those middle two cycles—which aren't connected to the AND gate—the joypad reg might show a button press, BUT a corresponding interrupt might not get triggered?

    💬 1🔄 0⭐ 0
  • Nov 24, 2026, 6:18 PM

    this is not a problem in practice, since apparently the joypad interrupt is rarely used in commercial ROMs (bc of bounce issues!). BUT in my plans to make a GB SoC-based home computer, I'd been thinking about repurposing the joypad inputs/interrupt as a way for peripherals to trigger interrupts (each could have its own input, or i could multiplex 16 onto the four inputs). but the need to pull the input low for four cycles to reliably trigger the interrupt complicates that plan

    💬 1🔄 0⭐ 0
  • Nov 24, 2026, 6:22 PM

    (also interrupt handling on the SM83 CPU is already pretty slow, requiring six cycles or so to actually run the handler, and usually more than that, since from a practical standpoint, you almost always want to push registers onto the stack, which takes even more cycles. the extra four cycles to trigger the joypad interrupt could make it impractical to use interrupts for high speed peripherals? i dunno)

    💬 1🔄 0⭐ 0
  • Nov 24, 2026, 9:15 PM

    oh wow I just tested this behavior in the simulator and... it works exactly like i thought it would. so weird! p10–p15 are the i/o pins; i made a little loop in the simulator setup that "pushes" the buttons in either a 1010 pattern or a 1001 pattern at 1MHz. "jp_ticker" is the state of the four flipflops i mentioned earlier, and int_jp going high triggers the joypad interrupt. first image is 1010, and no interrupt happens; second image is 1001, and voila, the interrupt

    gtkwave screenshot, as described in post text: green horizontal lines indicating high or low values, sometimes superimposed with numbers (jp_ticker=1010)
    gtkwave screenshot, as described in post text: green horizontal lines indicating high or low values, sometimes superimposed with numbers (jp_ticker=1001)
    💬 2🔄 0⭐ 0
  • Nov 28, 2026, 4:07 AM

    so blargg's cpu test rom was showing weird 8px lines in a few tests. it took a bit but i tracked down the problem: the "ld sp, hl" instruction tucks the value of the hl register on the cpu address bus for one cycle—so when the test for "ld sp, hl" runs, with $8000 in hl, my code to check whether VRAM should be readable says "no" ($8000 is a VRAM address, and the cpu can't access VRAM during rendering). so in that one cycle the PPU reads $ff off the vram bus

    game boy emulator screenshot. text at the bottom: "cpu_instrs / 01:ok 02:ok 03." there are two errant 8px horizontal lines in the lower third of the screen
    game boy emulator screenshot. text at the bottom: "cpu_instrs / 01:ok 02:ok 03." there are two errant 8px horizontal lines in the upper third of the screen
    💬 1🔄 0⭐ 0
  • Nov 28, 2026, 4:15 AM

    the fix: change the code for determining whether or not vram is readable, so that vram access is blocked only if the CPU is specifically trying to *read* from an address in vram, not just if there is an address in vram on the cpu address bus. that way, the ppu renderer can keep accessing vram whenever it needs to. this is actually how it works in the silicon, i just didn't read the schematic right, whoops. to be fair, the schematic for vram cs/rd/wr is a mess

    low res screenshot of a schematic with a lot of gates and wires going around in different directions, some with mysterious input names like "TACU" and "SP_BP_CYS"
    💬 1🔄 0⭐ 0
  • Nov 28, 2026, 10:33 PM

    and only seven months after starting to work on this game boy emulator, i can at last present... the tetris copyright screen

    game boy emulator screenshot, showing the tetris copyright screen (blocky text reading "TM AND (C)1987 ELORG, TETRIS LICENSED TO BULLET-PROOF SOFTWARE..."
    💬 1🔄 4⭐ 0
  • 💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 1
  • 💬 0🔄 0⭐ 0
  • Nov 28, 2026, 10:53 PM

    @scott it does eventually time out to the title screen (though it takes a long time because my emulator is still very slow). apparently my joypad emulation is not perfect yet 🙃

    💬 0🔄 0⭐ 0
  • Nov 24, 2026, 6:10 PM

    @aparrish When BATU is loaded high, the int line will go high 3 clk cycles later. When BATU is loaded low, the int line will go low immediately.

    Maybe it's a debouncer?

    💬 0🔄 0⭐ 0
  • Nov 24, 2026, 6:26 PM

    @cr1901 that was my thought as well, see the follow-up toot! it's weird to me that they wouldn't use and AND gate with four inputs and connect all of the flipflops' output to that, which seems like it would be a more effective debouncer?

    💬 1🔄 0⭐ 0
  • Nov 25, 2026, 3:38 AM

    @aparrish Since both press and depress cause bouncing, I'm not sure an AND gate would make much of a difference. But I would have to sketch it out to be sure, I don't like running these types of sims in my head.

    💬 0🔄 0⭐ 1
  • 💬 0🔄 0⭐ 0