279 Commits

Author SHA1 Message Date
Matthew Laux f3e872c53b fix patches.h conflicting with system Patches.h v1.4.1 2026-02-16 17:20:17 -06:00
Matthew Laux 7626dd2452 version 1.4.1 2026-02-16 17:07:44 -06: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
Matthew Laux 88371b7b99 Merge pull request #9 from tannerfokkens-maker/stack-fixes 2026-02-11 08:41:37 -06:00
Tanner Fokkens de2b956a7f Disable changing double speed in-game. 2026-02-10 23:51:49 -08:00
Tanner Fokkens 6e6f26d459 Flush pending HDMA transfers on cancel (partial fix for sourcrystal)
When HBlank HDMA is started and cancelled within a single JIT session,
hdma_sync (which processes batched HDMA transfers) never runs between
them, so zero transfers occur and VRAM data is lost.

Fix: when HDMA5 is written with bit 7=0 to cancel an active HDMA,
temporarily adjust frame_cycles with in-flight JIT cycles and call
hdma_sync to process all pending HBlank transfers before clearing
hdma_active.

Also add hdma_completed flag to guard against spurious GPDMA when
batched processing completes the HDMA before the game's res 7,[hl]
cancel write arrives (Pokemon Crystal's HDMATransfer pattern).

This is a partial fix — most tile corruption in sourcrystal is resolved
but some glitches remain (top row tile corruption when opening/closing
the start menu, stray tile on the continue menu screen).
2026-02-07 00:12:35 -08: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 28f4ff2d02 optimize window drawing (don't draw tiles that are covered by window) 2026-02-05 00:15:51 -06:00
Matthew Laux 1eb1209097 optimize register usage for write, add patch system v1.4.0 2026-02-03 18:29:48 -06:00
Matthew Laux d43868a279 add menu items to resedit, v1.4.0 2026-02-01 20:02:14 -06:00
Matthew Laux 806353705d Merge pull request #8 from tannerfokkens-maker/cgb-support
Gameboy Color Support
2026-02-01 18:05:22 -06:00
Tanner Fokkens ba1cbc979a Add ly changes back 2026-02-01 15:55:24 -08: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 c5fb8064aa Fix CGB detection: set A=0x11 for CGB mode 2026-02-01 01:12:15 -08:00
Tanner Fokkens d9f04cfe12 HDMA Reimplementation
Rewrote HDMA (HBlank DMA) support for Game Boy Color with proper
handling of edge cases documented in jsgroth's emulator blog:

- GPDMA: Immediate transfer when HDMA5 bit 7 = 0
- HDMA: Per-scanline 16-byte transfers during HBlank (LY 0-143)
- HDMA cancellation: Writing HDMA5 with bit 7 = 0 while active
- HDMA5 reads: Bit 7 = 0 when active, remaining blocks in bits 0-6
- Immediate first chunk when HDMA started during HBlank
- Destination address wraps within VRAM (doesn't terminate)
- HDMA5 writes with bit 7 = 1 update active HDMA length

Known issue: Pokemon sprites still corrupted (VRAM population issue)
2026-02-01 00:58:48 -08:00
Matthew Laux 93ae963ea5 reset ly_read_cycle (fixes pokemon crystal infinite loop) 2026-02-01 00:58:48 -08:00
Tanner Fokkens 83054f8e45 Add Ignore Double Speed option for CGB performance
Games running in double-speed mode (8MHz) cause the emulator to execute
twice as many instructions per frame. This option ignores the double-speed
flag, treating all execution as normal speed for better performance on
slower Macs while maintaining correct frame timing.

- Add menu toggle in Options menu
- Save/load preference to resource file
- Check flag in dmg_sync_hw() when calculating PPU cycles
2026-02-01 00:58:48 -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 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 fe694cf354 version 1.3.1 v1.3.1 2026-01-31 22:11:55 -06:00
Matthew Laux 2f23570674 fix SP init (fixes random crashes on exit) 2026-01-31 21:56:34 -06:00
Matthew Laux 5282a16639 reenable palettes menu 2026-01-31 21:25:20 -06:00
Matthew Laux 77dd744b1e reset ly_read_cycle (fixes pokemon crystal infinite loop) 2026-01-31 20:44:24 -06:00
Matthew Laux f18a940da2 add mmu register access functions... 2026-01-30 00:26:15 -06: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
Matthew Laux 99056c4e28 Merge remote-tracking branch 'origin/master' 2026-01-29 10:25:28 -06:00
Matthew Laux 8456acc749 test ci 2026-01-29 10:25:13 -06:00
Matthew Laux af756bb10d Merge pull request #6 from tannerfokkens-maker/fix-apu-dac-disable
Fix sounds playing indefinitely by implementing DAC disable
2026-01-29 08:57:40 -06:00
Matthew Laux 3a78378b1f add palette picker 2026-01-28 23:16:35 -06:00
Tanner Fokkens f861389734 Fix sounds playing indefinitely by implementing DAC disable
When NRx2 (envelope register) is written with bits 3-7 all zero,
  the channel's DAC should turn off and the channel should be disabled.
  Games use this to stop sounds by writing 0x00 to the envelope register.

  Also prevent trigger from enabling a channel when DAC is off.

  Fixes issues like powder fire sound and
  rock crash in Zelda playing forever.
2026-01-28 17:55:54 -08:00
Matthew Laux 806728a708 remove unused cpu struct v1.3.0 2026-01-28 13:56:38 -06:00
Matthew Laux 743f20cac7 add another apple doc 2026-01-28 13:49:06 -06:00
Matthew Laux 5066768fd7 bump to version 1.3.0, rearrange menus a bit, make sure menus are always up-to-date 2026-01-28 13:45:48 -06:00
Matthew Laux fa69243fe7 Merge pull request #4 from tannerfokkens-maker/autosave-feature 2026-01-27 01:21:43 -06:00
Tanner Fokkens 173ad52885 Remove screenshot shortcut 2026-01-26 23:16:55 -08:00
Tanner Fokkens 681ba098ae Replace autosave toggle with manual Write Savefile (Cmd+S) 2026-01-26 23:05:11 -08:00
Tanner Fokkens 050d84c230 Remove backwards compatibility. 2026-01-26 22:09:29 -08:00
Matthew Laux 3aaf654360 Merge pull request #5 from tannerfokkens-maker/fix-rtc-load
Fix uninitialized file_size in RTC load
2026-01-26 23:37:15 -06:00
Tanner Fokkens f5baaad745 Fix uninitialized file_size in RTC load
The file_size variable was declared but never assigned before
  being checked, causing RTC data to never load from save files.
  Time would reset instead of advancing based on elapsed real time.
2026-01-26 21:28:58 -08:00
Tanner Fokkens 6b447d425f This is an menu-toggle autosave feature. It saves every minute, but only when cart RAM has been marked dirty. This hopefully avoids excessive disk writes. The old way, every 15 seconds regardless of SRAM state, was causing a lot of stuttering on real hardware. 2026-01-26 20:53:28 -08:00
Matthew Laux 149e1dc29b Merge pull request #3 from tannerfokkens-maker/rtc-support
Add MBC3 RTC support using Mac system time
2026-01-26 22:20:49 -06:00
Tanner Fokkens 0d82b1f72f Remove old save format support. 2026-01-26 20:19:32 -08:00
Tanner Fokkens e83f348cdf Add MBC3 RTC support using Mac system time
Query Mac time on-demand when RTC is latched (zero per-frame overhead).
  Persist RTC state in save files with backward compatibility for old saves.
2026-01-26 17:37:14 -08:00