46 Commits

Author SHA1 Message Date
Tanner Fokkens 7caca2f056 Fix LY wait and HALT timing in CGB double speed mode
In double speed mode, dmg_sync_hw() halves CPU cycles before adding
to frame_cycles. The timing functions (compile_ly_wait, compile_ly_wait_reg,
compile_halt) compute D2 in PPU-domain units, but only half gets applied
to frame_cycles, causing the target LY to never be reached and an
infinite re-entry loop during game init.

Add effective_double_speed byte to JIT context and emit a runtime check
in all three timing functions to double D2 when double speed is active.
2026-02-06 16:06:36 -08:00
Matthew Laux 1eb1209097 optimize register usage for write, add patch system 2026-02-03 18:29:48 -06:00
Tanner Fokkens 39d9dcca74 Forgot to add other files... 2026-02-01 15:29:19 -08:00
Tanner Fokkens c5fb8064aa Fix CGB detection: set A=0x11 for CGB mode 2026-02-01 01:12:15 -08:00
Tanner Fokkens 14e7b65dde Remove debug counters and optimize LY calculation
- Remove dmg_reads/dmg_writes debug counters that ran on every memory
  access, saving 2-4 cycles per read/write operation
- Replace O(n) LY calculation loop with O(1) division-based formula,
  eliminating up to 154 iterations for games that poll LY infrequently
2026-02-01 00:58:48 -08:00
Tanner Fokkens 3cbc9337f8 Fix stack corruption in CALL/RET/RST and add STOP instruction support
Major fixes:
- CALL, RET, RST, and their conditional variants now properly check
  stack_in_ram before accessing memory via A3. Previously they assumed
  A3 was always a valid native pointer, causing memory corruption when
  in slow mode (A3 held raw GB SP value like 0xFFFE).

- Fixed initial SP setup to use fast mode with A3 pointing to actual
  HRAM memory instead of holding the raw GB SP value.

- STOP instruction now calls runtime handler to check for CGB speed
  switch instead of immediately halting. Enables double-speed mode.

- HDMA cancellation: writing to HDMA5 with bit 7=0 while HDMA is active
  now cancels the transfer (fixes Pokemon Crystal).

Safety improvements:
- Added src_ptr bounds check to prevent m68k_offsets array overflow
- Added PC validation to reject execution in VRAM/external RAM
- Added PC history ring buffer for crash debugging

Tested: Klax, Link's Awakening DX, Wario Land 3 now boot successfully.
2026-02-01 00:58:48 -08:00
Tanner Fokkens 9bb5705759 Add Game Boy Color support
- New CGB state management (cgb.c/cgb.h)
  - VRAM banking (VBK register)
  - WRAM banking (SVBK register, banks 1-7)
  - Speed switching (KEY1 register)
  - HDMA transfers (HDMA1-5 registers)
  - CGB palette registers (BCPS/BCPD, OCPS/OCPD)

- CGB rendering (lcd_cgb.c, lcd_mac_cgb.c)
  - Tile attributes from VRAM bank 1
  - Per-tile palettes, H/V flip, priority
  - 8 background and 8 sprite palettes
  - RGB555 to Mac indexed color conversion

- JIT initialization sets A=$11 for CGB mode detection

- UI: "Run as GBC" menu option, .gbc file filter

Working: Tetris DX, Pokemon Gold
Known issues: Crystal, SMB Deluxe, LADX hang at boot
2026-02-01 00:57:25 -08:00
Matthew Laux 2f23570674 fix SP init (fixes random crashes on exit) 2026-01-31 21:56:34 -06:00
Matthew Laux 77dd744b1e reset ly_read_cycle (fixes pokemon crystal infinite loop) 2026-01-31 20:44:24 -06:00
Matthew Laux 806728a708 remove unused cpu struct 2026-01-28 13:56:38 -06:00
Matthew Laux ebb343cdbd support stack in HRAM (fixes DK '94) 2026-01-25 19:03:28 -06:00
Matthew Laux 6d5ffbf7f7 recover from OOM on banked cache allocation 2026-01-24 23:26:21 -06:00
Matthew Laux 1cbad8b28f second round of polish. v1.2.0 2026-01-24 20:30:13 -06:00
Matthew Laux 8b78a9345b sync audio with GB execution - still not sure if i like this better 2026-01-24 02:20:26 -06:00
Matthew Laux 70ff0224cb convert machine code arrays to gcc inline assembler 2026-01-23 00:22:01 -06:00
Matthew Laux 0037fae05a fix instruction caching issue on 68040 2026-01-22 23:16:30 -06:00
Matthew Laux d627b7265b re-add SP range detection, avoid copying lcd pixels twice 2026-01-21 21:20:42 -06:00
Matthew Laux 230a9c929b write a README, add screenshots, audio tweaks with band limited square waves 2026-01-17 00:14:05 -06:00
Matthew Laux e9000a3c84 rewrite stack for the millionth time 2026-01-15 21:09:57 -06:00
Matthew Laux bf46e65f5a dumb "force draw sprites" that doesn't work 2026-01-15 19:27:44 -06:00
Matthew Laux f8e508ecd2 LY wait detection, fix block overflow bug, add new menus, MBC5 2026-01-15 16:46:17 -06:00
Matthew Laux 14474a0197 use A5/A6 as page table pointers, UI polish 2026-01-15 01:27:50 -06:00
Matthew Laux 1927fc04db arena allocator for blocks, remove duplicate blocks by tracking backward branches 2026-01-14 23:48:19 -06:00
Matthew Laux fb9c430397 clean up externs, make indentation consistent in jit, add some comments 2026-01-14 17:33:01 -06:00
Matthew Laux 0ce2ffd60e simplify cb ops, basic HALT, fast path for dmg_read/write16 2026-01-14 17:06:15 -06:00
Matthew Laux 74f91d802e prepare for dmg_read/write16, rename functions, simplify interop 2026-01-13 22:37:04 -06:00
Matthew Laux 9f91bd7526 add 1x scale, audio refactor and fix wave frequency issue, remove crazy SP detection and always go through dmg_read/write 2026-01-13 21:47:25 -06:00
Matthew Laux 9abe1c4bb1 remove bogus O 2026-01-13 00:24:59 -06:00
Matthew Laux e0e8e5d967 remove timing hacks, add audio menu option, try to measure audio overhead 2026-01-13 00:20:52 -06:00
Matthew Laux ba248a1640 remove useless lru 2026-01-12 22:13:16 -06:00
Matthew Laux c611c553ca fix flag bugs (wow that was hard to find), make DIV better, add color icons 2026-01-12 19:24:03 -06:00
Matthew Laux 74a8f10c25 test audio 2026-01-11 19:12:20 -06:00
Matthew Laux 8f0100d322 flush instruction cache after patching block... this fixed so many random crashes 2026-01-10 21:06:23 -06:00
Matthew Laux 6a2ec58fbd fix "dec h", precompute LCD palette on palette change, make profiling percent based 2026-01-09 19:35:08 -06:00
Matthew Laux eab85d0205 optimize ldh 2026-01-08 22:30:28 -06:00
Matthew Laux ea687bed84 finish options dialog 2026-01-08 20:28:10 -06:00
Matthew Laux a36c1dd72e inline reads/writes for stuff in paged areas 2026-01-07 20:43:55 -06:00
Matthew Laux 68937eb3f3 patch blocks to jump directly to next block 2026-01-07 18:53:53 -06:00
Matthew Laux bdeb43c0b3 more timing............................ 2026-01-05 15:09:39 -06:00
Matthew Laux f3864fef08 keep track of cycles per instruction, remove Mac timing hack 2026-01-04 23:30:22 -06:00
Matthew Laux 8e5db869a5 temp timing instrumentation 2026-01-04 19:27:25 -06:00
Matthew Laux 5b3ad01b68 move next_pc to d3 2026-01-04 18:11:43 -06:00
Matthew Laux 698fe41688 more efficient context switch into JIT, remove old compiler files, fix vblank flag bug 2026-01-04 17:45:52 -06:00
Matthew Laux a198b4218f split out debug logging 2026-01-03 15:59:03 -06:00
Matthew Laux 3d6156fa57 split out jit harness into its own file 2026-01-03 15:49:56 -06:00
Matthew Laux c6377b8b3d remove pointless unity build from compiler version 2026-01-03 14:26:04 -06:00