90 Commits

Author SHA1 Message Date
Matthew Laux e1603127be Merge remote-tracking branch 'origin/master' 2026-02-16 17:06:09 -06:00
Matthew Laux 8e30a2bff5 add extra cycles for taken branch 2026-02-16 17:05:48 -06:00
Tanner Fokkens c0a30de0b2 Fix inc/dec clobbering carry flag, breaking Pokemon Crystal menus
The GB inc/dec instructions must preserve the carry flag and only
modify Z. The JIT was capturing both Z and C from the 68k CCR,
causing dec to incorrectly set carry on borrow (e.g. 0-1=FF).

This broke Pokemon Crystal's xor_a_dec_a helper which returns with
C=0 to signal "no action". The corrupted C=1 caused the scrolling
menu to exit prematurely when pressing down past CANCEL, showing
phantom items and garbage prices.

Fix compile_set_z_flag to merge new Z with old C, and
compile_set_c_flag to merge new C with old Z. Move and/or/xor
immediates to compile_set_zc_flags since they should clear C.
2026-02-06 17:11:15 -08:00
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
Tanner Fokkens 4decd1de16 Fix ld sp,hl/imm16 to use page table for switchable WRAM banks
ld sp,hl and ld sp,imm16 computed the native SP pointer using a fixed
  base (dmg->main_ram), which always resolved to WRAM bank 1 for the
  $D000-$DFFF range. On CGB, this range is switchable (banks 1-7 via
  SVBK). Games like Pokemon Crystal that use the SP trick to bulk-copy
  data from switchable WRAM to VRAM would read from the wrong bank,
  causing VRAM tile corruption.

  Use the read page table at runtime instead, which is kept in sync with
  the current WRAM bank by cgb_update_wram_bank().
2026-02-06 13:08:37 -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 30cccd4d59 Address PR review feedback
- Revert branches.c slow paths
  - Simplify compiler.c block overflow check (src_ptr approach)
  - Revert LY calculation to iterative
  - Remove preferences migration code
  - Remove VRAM/external RAM PC rejection (debugging code)
2026-02-01 12:45:17 -08:00
Tanner Fokkens 6b4225aa8f Optimize CGB rendering with LUT-based color lookup
- Add combined cgb_color_lut[16][4] to eliminate per-pixel conditional
  branches and multiply operations in final draw functions
- Add tile_decode_cgb[256] LUT to replace 8-iteration bit extraction
  loops with 2 table lookups
- Add dirty palette tracking (bg_palette_dirty, obj_palette_dirty) to
  skip expensive Color2Index calls when palettes haven't changed
- Replace multiply-based pixel_shift calculation with 4-byte LUT

Also fixes test harness to initialize JIT_CTX_GB_SP for slow path
stack operations.
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 62d406a81d link -lm for linux 2026-01-29 10:35:21 -06:00
Matthew Laux fea4272ff6 unignore makefile... 2026-01-29 10:32:41 -06:00
Tanner Fokkens 3514215b9d Remove POP AF fix, oops 2026-01-26 15:29:24 -08:00
Tanner Fokkens 4a43238030 Fix POP AF and DAA for INC/DEC A
- POP AF: mask lower 4 bits of F register to 0 (noticed it in Blargg)
  - INC A / DEC A: add DAA state tracking for BCD arithmetic
2026-01-26 15:17:36 -08:00
Tanner Fokkens 8a30a2030b Extract compile_daa function, add DAA tests
- Move DAA implementation to dedicated compile_daa() function
  - Add 5 DAA tests covering addition/subtraction BCD adjustment
  - Fix bug where CCR was clobbered after testing N flag
2026-01-26 11:06:02 -08:00
Tanner Fokkens d1ff4462e5 Add DAA instruction support for BCD arithmetic 2026-01-26 09:59:15 -08:00
Matthew Laux 23f752af23 move timing functions to their own file, LY wait on registers too 2026-01-25 22:16:22 -06:00
Matthew Laux 882ace0d13 make LY read faster, revert wack HALT change 2026-01-25 20:53:56 -06:00
Matthew Laux da7a7b31cd add "dec sp" - missed that one - allows DK Land to boot, not pretty though 2026-01-25 19:18:06 -06:00
Matthew Laux ebb343cdbd support stack in HRAM (fixes DK '94) 2026-01-25 19:03:28 -06:00
Matthew Laux 6972ac5cc3 calculate mid-frame LY read correctly instead of cycling through - fixes Metroid enemies 2026-01-25 17:42:29 -06:00
Matthew Laux d9efe79aa1 fix bit instruction affecting C flag (fixes Metroid II) 2026-01-25 12:26:30 -06:00
Matthew Laux dbf619b285 remove sm83 json tests 2026-01-22 01:43:42 -06:00
Matthew Laux 49611878ff split test_exec into files by test categories 2026-01-22 01:42:55 -06:00
Matthew Laux 8e17d86cfc use git sha for version info 2026-01-22 01:18:46 -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 e9000a3c84 rewrite stack for the millionth time 2026-01-15 21:09:57 -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 814a9ccf0b dmg_write16 instead of two dmg_write for stack ops 2026-01-13 23:40:28 -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 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 1fd912fdba save/restore D2 across dmg_read 2026-01-11 18:47:54 -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 eaef7441ed color mac support 2026-01-10 02:43:00 -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 f472ce46bd use 68k ccr instead of GB flag format. it's getting crashy 2026-01-09 00:42:26 -06:00
Matthew Laux b69b784bd7 fully remove N and H support 2026-01-08 22:57:37 -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 da0e9dffca use key mappings, load/save key mappings, experiment with interrupt check every 16 scanlines 2026-01-08 10:28:09 -06:00
Matthew Laux 9969a0b483 split out stack funcs to separate file, start on key mappings dialog 2026-01-07 23:27:33 -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