54 Commits
Author SHA1 Message Date
SourMesen 142884b598 NES: Ignore consecutive reads to 4016/4017 (#185)
The NES (and AV Famicom) both ignore consecutive reads to 4016/4017 (when the reads on 2 consecutive CPU cycles). This implements that behavior when the corresponding console type is selected.
These changes also simplify the DMA code, because a lot of extra code was in the DMA logic to simulate the same result. Moving this logic to the 4016/4017 reads makes the code simpler and allows it to work properly when a regular instruction reads from 4016/4017 twice in a row, too.
2026-06-21 19:17:51 -07:00
SourMesen 0cef745d88 Debugger: NES - Fixed "Break on crash" only being triggered once, even after rewinding (#172)
Also fixed the assembler not allowing "STP" as a valid instruction
2026-06-06 22:34:04 -07:00
zeta0134 76db4f61ff Merge pull request #75 from nesdev-org/SourFixOverclockDmcDmaTiming
NES: Fixed overclocking causing the APU to desynchronize from the CPU
2026-05-09 21:36:19 -04:00
Fiskbit 8530ad3198 NES: Improve DMC sample duplication emulation. (#80)
On late-G and H CPUs, starting a 1-byte sample just before the end of a DMC period can cause the sample to be fetched and played twice. This change fixes 2 issues related to this.

First, new research found that this bug is because there is normally at least a 1-APU cycle gap between the end of one DMC DMA and the start of the next DMC DMA on earlier CPUs, but these later CPUs remove that gap, allowing the aborted DMA behavior to occur sooner such that it can't be aborted.

But this means it actually affects any sample that is started at the end of a DMC period, not just ones that trigger the extra DMA. Mesen was previously only doing this in the case of 1-byte non-looping samples. This change fixes this issue. This was validated with AccuracyCoin's Implicit DMA Abort test and with dmc_dma_stop_test_v3 test F.

Second, when there are two DMC DMA's back to back while reading a joypad register, both of them should cause an extra clock of the joypads when using NES behavior (unless suppressed by the APU register activation bug). However, we were only seeing extra clocks with HVC-001 behavior (which is handled on every DMA cycle always) because Mesen only runs its halt cycle logic for the first of the two DMAs. This change makes ProcessPendingDma call itself in the case where we handle DMC DMA back to back so it can do this extra joypad clock if need be.

This second change was validated with dmc_dma_start_test_v2 test C, dmc_dma_implicit_stop_level_test, and a variant of apu_register_activation_test.
2026-05-08 16:39:11 -07:00
Fiskbit c927a7451d NES: Fix open bus after DMA/joypad collision. (#71)
When the CPU is reading from $4000-$401F and DMA occurs, the DMA can cause 2A03 registers to erroneously activate. When joypad registers are activated in this way, this causes a bus conflict between the driven joypad bits and the DMA value. The DMA unit sees the result of this conflict and uses it.

However, if you inspect external open bus afterward, you do not see this value. This is because conflicting devices stop driving the bus at different times, so the last device to drive a bit wins on that bit for external open bus. Joypads stop driving later than the cartridge does, so its bits win.

This change fixes it so the DMA still sees the conflicting value, but the resulting open bus value uses the driven joypad bits, instead.

This fixes AccuracyCoin test "DMC DMA Bus Conflicts".

This change also makes it so reads that *don't* activate readable 2A03 registers can still read values from $4000-$4014 and $401B-$401F (ignoring the readable normal and test mode register addresses). In particular, this means OAM DMA can read $4011 from mappers that support IPCM.
2026-05-02 23:58:17 -07:00
Sour 687300a2d7 NES: Fixed issue with overclocking causing the APU to desynchronize from the CPU
This could cause DMC DMA to happen on the wrong cycle parity, causing issues with code that uses sprite DMA to align 4016 reads to prevent DMC DMA from corrupting the read value.
2026-04-30 22:33:05 +09:00
SourMesen e391fd8f9a Code: Add auto-formatting rules for C++ (clang-format) and enforce in CI via "clang-format" and "dotnet format" (#78)
* Code: Add auto-formatting rules for C++ projects (clang-format) and enforce code style via "clang-format" and "dotnet format"

* fix ui format warnings
2026-04-30 06:03:55 -07:00
Fiskbit 86c81867fc NES: Fix handling of internal and external open buses. (#62)
When reading from $4015, the internal and external CPU buses are isolated. This means the CPU reads the value from the internal $4015 register, which is not repeated onto the external bus, and any response to the read from an external device is not seen by the CPU (not even in the form of a bus conflict).

As a result, the internal $4015 result updates only the internal open bus value, and any external result updates only the external open bus value. If the buses are isolated again on the next CPU cycle, then open bus bits on the internal bus read will not be affected by the external open bus value.

This has been verified in a new test that lands a DMA-that-activates-$4015 on a $4015 read, so the two buses diverge on the DMA read cycle and $4015 is then read by the CPU on the next cycle to verify that the internal open bus was not affected by the DMA read. Furthermore, if the CPU then reads from external open bus afterward, it gets the DMA value from external open bus, which persisted but wasn't previously accessible due to bus isolation.

Mesen previously had a bug where its GetInternalOpenBus function was returning the wrong bus value (_externalOpenBus instead of _internalOpenBus). Fixing this was not enough to fix the test, because SetOpenBus would update both buses on any external read. This change fixes the GetInternalOpenBus bug and changes SetOpenBus to be able to update either bus independently.

This was tested with the new AccuracyCoin "Internal Data Bus" test.
2026-04-26 18:16:31 -07:00
Fiskbit 66f3d36619 NES: Predictive breakpoints can ack $4015 frame counter IRQ. (#51)
Predictive breakpoints use a dummy CPU for prediction, and that dummy CPU needs to not cause side effects. However, there was no way to peek at the state of the frame counter IRQ, so prediction could cause this flag to be unexpectedly cleared.

This change adds peeking. It has a limitation in that it may mispredict the value of the flag on double reads and break when it shouldn't.

This change also fixes a related issue on PAL with dummy CPUs processing DMA.

Thanks to Sour for identifying the code for both issues.
2026-04-26 03:27:35 -07:00
Fiskbit 2e7c5492e0 NES: Changes various dummy PC reads to stack reads.
Many 6502 instructions read from memory and discard the value, called dummy reads. Mesen implemented many of these with a DummyRead function that reads from PC. However, many of these should actually read from the stack, instead. This change changes DummyRead to DummyPcRead, adds a DummyStackRead function, and fixes instructions to use the stack version where relevant.

Tested: No change in current AccuracyCoin. No change in Mesen automated test results.
2026-04-10 14:03:55 -07:00
Sour 1b76310cdc NES: Mark opcodes 93/9B/9F as unstable + add implementation for 8B 2025-05-16 09:38:04 +09:00
Sour 761bde6d9f NES: Stop CPU permanently when a halt/stop instruction is executed 2025-05-15 19:35:48 +09:00
Sour b25cc28482 NES: Fixed order of operations for JSR/RTS
Jumping to an open bus address with JSR should leave the MSB of the target address on the data bus, not the LSB of the current PC which is the last write to the stack
2025-05-09 15:44:38 +09:00
Sour 8c00a1660a NES: Improved timing for PAL DMA
On PAL CPUs, DMA only starts at the beginning of the next instruction, which also prevents controller input from getting corrupted if DMC DMA happens on a controller read
Fixes video glitches in "Retrocoders - Years behind" when playing the boomjunk track
See:
https://forums.nesdev.org/viewtopic.php?p=300912
https://forums.nesdev.org/viewtopic.php?p=214971
2025-04-04 21:38:17 +09:00
Sour ad548465c6 Debugger: NES - Added new "break on..." options
Invalid VRAM access, Invalid OAM write, DMA controller read, Unstable opcodes
2025-03-27 21:16:02 +09:00
Sour 75b667d2cc NES: Fixed behavior for SHX, SHY, SHA, TAS 2024-12-11 18:27:06 +09:00
Sour 64933699a8 NES: Reset newly added internal flags on console reset 2024-09-29 20:19:43 +09:00
Sour 31c545a680 NES: Fixed OAM DMA getting skipped when DMC DMA was aborted on the same cycle as OAM DMA was triggered 2024-09-29 11:42:43 +09:00
Sour 3860e39615 NES: Halt cycle for an aborted DMA should not count as a read on input ports if the CPU is halted on a 4016/4017 read
The CPU will read 4016/4017 for 2 consecutive cycles, which the controllers will see a single read (except on the original famicom), so skip the dummy read to reproduce this behavior
2024-09-28 20:22:08 +09:00
Sour efdf18dadb NES: DMAs interrupted during a write shouldn't start after the write 2024-09-28 20:15:26 +09:00
Sour ab8f9f60da NES: Implemented DMC glitch that causes an additional DMA to run fully or partially, depending the 4015 write's timing 2024-09-28 16:44:34 +09:00
Sour 484b61e395 NES: Implemented DMA bug when disabling the DMC channel with a 4015 write 2024-09-28 09:48:35 +09:00
Sour 10aa708c07 NES: Tweak behavior of halt/dummy reads during DMA
These halt/dummy reads shouldn't trigger the DMA issues - only actual DMA reads should
2023-11-15 22:31:36 +09:00
Sour 1cd6d078a7 Debugger: NES - Fixed/improved display for DMC/OAM DMA reads 2023-07-18 18:21:03 +09:00
Sour 04d0a89b4d NES: Implemented DMA read glitch when CPU is halted on a read in the 4000-401F range 2023-03-01 00:28:57 -05:00
Sour a157edb670 NES: Tweaked DMA behavior for PAL/Famicom 2023-01-05 17:48:31 +09:00
Sour 7108bdde36 Refactoring - Moved some leftover files out of the root folder 2022-09-08 21:48:49 -04:00
Sour 152abf690b Rename stdafx.h to pch.h 2022-09-08 21:29:52 -04:00
Sour af6d4f0da2 Debugger: Refactoring for specific break options (+ added nes break on 2000 scroll glitch option) 2022-08-21 16:11:22 -04:00
Sour dbcde7be69 Debugger: Added "isDma" and "isDummy" values in expr. evaluator 2022-08-07 20:40:30 -04:00
Sour 0e36b496b0 Debugger: Lua - Added measureString, added support for text wrapping in drawString 2022-08-01 20:48:06 -04:00
Sour 6163158637 Debugger: Lua - Implemented getState + setState 2022-07-31 15:00:53 -04:00
Sour b456170551 Save states: Serializer rework/rewrite 2022-07-20 00:40:32 -04:00
Sour 1c055bb4af Save States: Load/save emulation settings for netplay/rewind states, fixes some TODOs, fixes some NES issues when loading a state taken in a different region 2022-03-31 14:36:15 -04:00
Sour d5a28f1403 Debugger: (WIP) Evaluate breakpoints before an instruction starts executing (ProcessInstruction + calculate upcoming reads/writes) 2022-02-19 09:55:33 -05:00
Sour 85482d34f1 Debugger: SNES - Added coprocessor debuggers 2022-01-30 20:20:24 -05:00
Sour 3615ef0eb1 Core: Enabled warning level 4, fixed/disabled warnings 2022-01-23 12:36:35 -05:00
Sour 854cce4362 Debugger: Support for switching roms/console type while tools are opened
+ Close all tools when game stopped
+ Misc. performance fixes
+ UI code refactoring
+ Load/save position for all tools
2022-01-17 21:05:06 -05:00
Sour 368455e503 Tilemap viewer: Refresh scanline/cycle config 2021-12-28 20:53:39 -05:00
Sour 5f2524fe60 Fix Linux build (and most clang warnings) 2021-05-13 00:25:00 -04:00
Sour 262cd83c6a NSF - Fixed issues with playback, fixed flickering in UI 2021-05-02 23:45:17 -04:00
Sour 768a3f6e17 NSF support (WIP) 2021-04-30 20:41:26 -04:00
Sour c2df0ef553 Console region refactoring/changes 2021-04-27 23:30:00 -04:00
Sour e4aeb381eb Fixed some minor todos in NES code 2021-04-27 21:56:40 -04:00
Sour 0479d6f90a VS system/dualsystem (missing palette and proper audio/video option support) 2021-04-25 14:12:10 -04:00
Sour 12bf5c5e1b NES/GB/SNES config, audio filters 2021-04-11 18:50:31 -04:00
Sour e27e511558 File reorganization 2021-04-09 21:45:04 -04:00
Sour 2a3a955dc3 Reorganize files (debugger + netplay) 2021-04-09 20:01:50 -04:00
Sour c7ef756b3f Code reorganization (GB) 2021-04-09 19:13:47 -04:00
Sour 27aec67ff7 CPU state rework 2021-04-07 23:29:12 -04:00