Skip to content

Emulator core (AcidBoy.Core)

The core is the emulator itself: CPU, memory, video, sound, timer, joypad, serial port and cartridges. It has no dependencies and no platform code, so it runs the same in the desktop app, in headless mode and in tests.

Component Status
CPU (SM83) All opcodes; M-cycle accurate memory timing; HALT bug, EI delay, illegal-opcode lockup
PPU Scanline renderer with per-dot mode/STAT timing; window, sprites, DMG and CGB priority rules
CGB VRAM/WRAM banking, color palettes, BG attributes, HDMA (general and HBlank), double speed
SGB Command packets, palettes and per-cell attributes, VRAM transfers, 256x224 border, MASK_EN, 2-4 player multi-tap
APU 2 square channels (one with sweep), wave, noise; stereo panning; high-pass output filter
Timer, joypad, serial, OAM DMA Yes
Cartridges ROM only, MBC1, MBC3 (with the real-time clock and MBC30’s 4 MiB ROMs), MBC5 (with rumble)

Verified with Blargg cpu_instrs, instr_timing, mem_timing, halt_bug; dmg-acid2 and cgb-acid2; Mooneye emulator-only/mbc5/* and emulator-only/mbc1/* (except the MBC1M multicart test).

flowchart LR
    CPU["Cpu.Step()<br/>one instruction"] -- every memory access<br/>or internal delay --> Tick["Bus.Tick()<br/>one M-cycle"]
    Tick --> Timer & Serial & PPU & APU & Cart["Cartridge<br/>(if RequiresTick)"]
  • The CPU drives everything. Cpu.Step() runs one instruction, and every memory access or internal delay calls Bus.Tick(). That advances the timer, serial port, PPU, APU and, for cartridges with their own clock, the cartridge, by one M-cycle. Timing is therefore exact at memory-access granularity.
  • Time is counted in dots (the 4 MHz master clock; Bus.ElapsedDots). In CGB double speed the PPU and APU get 2 dots per M-cycle, while the timer and serial port follow the CPU clock.
  • Only two entry points run the emulator: GameBoy.RunFrame() and GameBoy.RunDots(n). Both return early while Serial.WaitingForLink is set (a network partner’s byte is late; see Link cable).
var cartridge = Cartridge.Load("game.gb");
var gb = new GameBoy(cartridge); // picks CGB for color games, SGB for SGB games, else DMG
gb.RunFrame(); // 70224 dots

Every difference between consoles goes through Hardware.HardwareModel. Components never ask “is this a Game Boy Color?”; they ask the model for a capability (Model.Supports(HardwareFeatures.X)).

Member Meaning
Features ColorPalettes, TileAttributes, CgbPriority, DoubleSpeed, VramDma, FastSerial, SuperGameBoy
WorkRamBanks / VideoRamBanks Banked memory sizes (enable SVBK / VBK)
GetPostBootRegisters(header) / PostBootDivider The state the boot ROM leaves behind; no boot ROM image is needed
ClockRate Master clock (the original SGB runs about 2.4% fast)
CanRun(header) Whether a game is compatible (CGB-only games need color hardware)
Model Console Notes
HardwareModel.Dmg Game Boy Default for monochrome games
HardwareModel.Mgb Game Boy Pocket DMG with A = 0xFF at boot
HardwareModel.Cgb Game Boy Color Default for color-aware games
HardwareModel.Agb Game Boy Advance CGB with B bit 0 set at boot (games use it to detect a GBA)
HardwareModel.Sgb Super Game Boy Default for SGB-enhanced games; 4.295 MHz SNES-derived clock
HardwareModel.Sgb2 Super Game Boy 2 SGB with A = 0xFF at boot and the standard clock

To add a model, derive from HardwareModel (or extend DmgModel/CgbModel and override what differs). Validate() rejects inconsistent combinations, such as TileAttributes with one VRAM bank.

  • Packets: SgbPacketReceiver decodes command packets from joypad register writes.
  • Commands: SuperGameBoy runs PAL01/23/03/12, PAL_SET/PAL_TRN, ATTR_BLK/LIN/DIV/CHR, ATTR_TRN/ATTR_SET, MASK_EN, MLT_REQ, CHR_TRN and PCT_TRN. VRAM transfers read the next displayed frame, as on hardware.
  • Colorizing: the PPU exposes its monochrome shades (Ppu.ShadeFrame), which are colorized per 8x8 cell and, with the border on, framed in a 256x224 picture. Use GameBoy.FrameWidth/FrameHeight, not the screen constants, because the output size varies.
  • Limits:
    • Commands are honored only when the header enables SGB functions (0x146 = 0x03, 0x14B = 0x33).
    • The SNES-side features (sound effects, code upload, OBJ_TRN) and the built-in border artwork are not emulated.
  • Cartridge.Load reads the header (CartridgeHeader: title, CGB/SGB flags, type, sizes, version, checksums) and creates the right mapper. Unsupported mappers are rejected with a clear error.
  • Saves: battery RAM is saved as <rom>.sav. MBC3’s clock is appended in the common 48-byte VBA-M/BGB footer, so saves move between those emulators. The clock runs on emulated time and catches up on the real time that passed since the save was written.
  • Rumble (MBC5 types 0x1C-0x1E) is exposed as Cartridge.RumbleActive; the desktop app vibrates player 1’s controllers.
Section titled “Serial port and link cable (Serial.cs, Link/)”
  • ISerialLink.TransferStarted(byte) is called when the internally clocked side (the master) starts a transfer.
  • The partner answers through Serial.ReceiveFromMaster(byte), which returns its SB and completes its transfer only if it was armed with the external clock.
  • The master gets the answer through SetReply(byte). If it hasn’t arrived when the 8 bits are done, WaitingForLink pauses the console until it does.
  • LocalLink wires two consoles in one process together. Transfers move whole bytes; individual bits are not clocked.

The network link built on this is described in Link cable.

Bus.Peek(address) reads work RAM (any bank, plus echo RAM) and high RAM without side effects. Bus.SnapshotRam() copies all of it. The win detection trackers and the RAM dump tool use these.

  • Fixed-length mode 3 (no sprite or scroll penalties), so effects that depend on exact mid-scanline timing may be slightly off.
  • No CGB compatibility mode: monochrome games on the CGB/AGB models use the CGB feature set instead of the boot ROM’s colorization.
  • No save states, and no MBC1 multicart (MBC1M) detection.
  • MBC2, MMM01, HuC1/HuC3, MBC6/MBC7 and the Pocket Camera are not supported.