Commit Graph
127 Commits
Author SHA1 Message Date
Matthew Laux 88dfbf7409 fix stack=d000, update HDMA to use the new timing code 2026-08-19 23:33:55 -05:00
Matthew Laux 005fb5ed3b remove small loop "optimization", no longer necessary and was messing up games that waited on cp [hl] to change 2026-08-19 20:45:14 -05:00
Matthew Laux f89ff7f0a9 rename main_ram -> wram and video_ram -> vram, optimize dmg struct layout 2026-08-12 20:13:20 -05:00
Matthew Laux 4e424aeb4f rename zero_page to hram 2026-08-12 19:55:32 -05:00
Matthew Laux 9395b336e9 make wram_base and hram_base required, make dmg_new take RAM arrays, simplify and de-hack stack paths 2026-08-12 19:49:00 -05:00
Matthew Laux 8bfb3cf5bd clean up unused code and stale comments 2026-08-08 00:44:56 -05:00
Matthew Laux b9a4e32fbb remove unused 68020 mode 2026-08-08 00:25:48 -05:00
Matthew Laux 3b5529d8c1 move memory loads to their own file, was getting long 2026-08-08 00:06:12 -05:00
Matthew Laux b5caa18677 fix self modifying code edge case where a block spans pages 2026-08-07 23:37:17 -05:00
Matthew Laux 9142963262 migrate to 4k page size - bank switch optimization pt 2 2026-08-07 20:06:24 -05:00
Matthew Laux 084e45c920 WIP bank switch optimize 2026-08-07 19:25:35 -05:00
Matthew Laux 6ca1ae93be make cycles in the JIT count up again, counting down was more complicated and didn't help performance at all 2026-08-06 23:13:45 -05:00
Matthew Laux 4af08af085 fix miscompiling data after loop end as code - fixes Pokemon Yellow 2026-08-04 23:15:06 -05:00
Matthew Laux ad1d7b392c fix noise being too loud, fix duty cycle of square waves, cache joypad value for fast read 2026-08-01 14:03:03 -05:00
Matthew Laux 4382e3f7ce version 2.1.0 2026-07-28 21:26:51 -05:00
Matthew Laux 985666d727 "deadline-gating budget_touch" omg 2026-07-27 16:49:53 -05:00
Matthew Laux f621a0d703 use GetKeys to avoid wasting time in GetNextEvent, use shared memory access helpers instead of inlining 2026-07-27 16:42:16 -05:00
Matthew Laux 42acc41a0d WIP compiler improvements 2026-07-27 11:24:31 -05:00
Matthew Laux db7a18ec31 don't call patch helper at all for HRAM, v2.0.1 2026-07-25 23:49:58 -05:00
Matthew Laux ceeb2a6cc7 asm version of bg tile inner loop, optimize multiplies in audio 2026-07-24 22:03:40 -05:00
Matthew Laux 0b7c9e7501 fix spurious writes into the cgb_color_lut 2026-07-24 14:34:29 -05:00
Matthew Laux aa0bf50bfa handle timing edge cases, don;t loop if condition is false on entry (fixes trainer entry in pokered) 2026-07-22 20:50:25 -05:00
Matthew Laux 4029b832bc shift optimization on page table lookup, replace all fixed branch targets with patching 2026-07-14 18:37:59 -05:00
Matthew Laux 71bb51ec88 instruction optimization round 1 2026-07-14 17:52:49 -05:00
Matthew Laux f51b251d9b fix overly strict CGB detection 2026-07-14 16:05:30 -05:00
Matthew Laux efae2406d0 remove fused branches, complexity wasn't worth it 2026-07-14 14:46:12 -05:00
Matthew Laux a778e8a79b track cycles in the block then count them at the end. kind of a wash? 2026-07-14 14:27:49 -05:00
Matthew Laux b3a1be15c6 add instruction trace... not too useful because it only traces inside JIT 2026-07-13 19:40:21 -05:00
Matthew Laux 00de54b442 add slow path for call/ret/rst (fixes SML2) 2026-07-12 23:38:46 -05:00
Matthew Laux 3d6d618420 fix constant folding in banked area (fixes DW 1+2) 2026-07-12 20:07:40 -05:00
Matthew Laux a48ae61a82 SP correctness fixes 2026-07-12 19:40:34 -05:00
Matthew Laux 5089339121 optimize memory usage by reclaiming unused block bytes 2026-07-12 18:48:03 -05:00
Matthew Laux faddf5d469 make m68k_offsets static (code block size -= 256) 2026-07-12 17:49:07 -05:00
Matthew Laux ed37a7c185 half-remove cycles_per_exit, optimize STAT interrupts, but will remove 2026-07-12 17:18:06 -05:00
Matthew Laux a4293c0fd2 deadline scheduler for events, improve timer, and add no-op serial support - fix FFL, FFA, alleyway
カエルのために is now slower?
2026-07-12 00:14:56 -05:00
Matthew Laux f6a9a1fd56 host side runner (gb6run) with tests from mooneye/blargg/games 2026-07-11 22:04:11 -05:00
Matthew Laux 7610d3ce96 biased page tables, scaled index on 68020+, interrupt fixes (that may or may not have actually improved compatibility) 2026-07-08 20:12:32 -05:00
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