FDS DRAM Refresh Watchdog

Discuss emulation of the Nintendo Entertainment System and Famicom.
Post Reply
User avatar
TakuikaNinja
Posts: 664
Joined: Mon Jan 09, 2023 6:42 pm
Location: New Zealand
Contact:

FDS DRAM Refresh Watchdog

Post by TakuikaNinja »

Or more bluntly, "$4030.D1 reports a previously undocumented DRAM refresh watchdog IRQ, not the byte transfer flag".

This test is a culmination of the research I've done since this post in the general FDS emulation thread: viewtopic.php?p=302540#p302540

Here's the summary I have for it on my WIP article for hardware-verified register behaviours:
The 2C33 refreshes DRAM rows on CPU cycles spent accessing areas outside of PRG-RAM. The DRAM Refresh Watchdog tracks these refresh cycles along with CPU cycles. If interrupts are enabled in $4023.D7, the watchdog will assert an interrupt after a certain number of CPU cycles without enough refresh cycles. This is primarily triggered by small infinite loops (e.g. "everything in NMI") and long stretches of code which only access PRG-RAM. The currently known low-level implementation is as follows:
  • 7-bit refresh counter - reset to 0 while $4023.D0 = 0, increment on every CPU cycle spent accessing memory outside of PRG-RAM, pause on CPU cycles accessing PRG-RAM. This counter determines the DRAM row to refresh, much like how the Z80's R register is typically used.
  • 12-bit CPU cycle timer - reset to all 1s (4095) while $4023.D0 = 0, reload with 3245 when refresh counter overflows, otherwise decrement on every CPU cycle. IRQ is asserted when the cycle timer underflows while $4023.D7 = 1.
Ignoring the watchdog (by using SEI instructions, or disabling it via $4023) appears to result in DRAM decaying during the aforementioned scenarios. The exact behavior is currently not well understood and likely varies between RAM adapters. In particular, RAM adapters containing 4 DRAM chips may behave differently from RAM adapters containing only 1 DRAM chip.

The safest known approach for handling the watchdog is to check for $4030.D1 in the IRQ handler and call Delay131 ($E149) if the bit is set. The BIOS' default IRQ handler performs a similar operation, thus it is suitable for programs which do not use other IRQ sources.
The discovery of this watchdog has been a fairly major revelation for me, since it means that commercial FDS games have quite literally been held together by a previously unknown feature this whole time. (as in, the watchdog is the only thing stopping games from executing decayed PRG-RAM contents). I've been working hard to research/document it so future FDS homebrew devs aren't caught out by this when testing their programs on real hardware. The ideal outcome is for emulators like Mesen2 to at least simulate the IRQs in order to aid with debugging.

Now to the program itself. This brand new FDS-4030D1-Addr program combines two of my previous programs into one package:
  • FDS-4030D1-Test - raster display for Delay131 execution in IRQ handler
  • older versions of FDS-4030D1-Addr - print IRQ return addresses during NOPslide execution
Source code: https://github.com/TakuikaNinja/FDS-4030D1-Addr
(file attachment + GitHub release will be made soon)

Example capture from my Sharp Twin Famicom:
FDS-4030D1-Addr_TwinFC.png
Attachments
FDS-4030D1-Addr.zip
(1.8 KiB) Downloaded 6 times
User avatar
TakuikaNinja
Posts: 664
Joined: Mon Jan 09, 2023 6:42 pm
Location: New Zealand
Contact:

Re: FDS DRAM Refresh Watchdog

Post by TakuikaNinja »

This is already in the repo's README but: I'd like to thank SCSR on the Discord for discovering the watchdog within the die scan in the first place. I've already had people describe me as an expert on the FDS because of my research output but I wouldn't have gotten this far without his analysis.
User avatar
forple
Posts: 66
Joined: Sat Dec 30, 2017 2:09 pm

Re: FDS DRAM Refresh Watchdog

Post by forple »

Some early 01/Early Revision BIOS RAM adapters contain Hitachi HM48416AP-12 DRAM chips. Unlike every other DRAM chip from Mitsubishi and Fujitsu these randomly decided to not have the hidden refresh feature (because cost?), which is seemingly what this entire watchdog system is for.
certified_fds_moment.jpg
According to the official Hitachi datasheet the timing lines up exactly with what the watchdog timing is looking for. (128 rows, 2ms)
Pokun
Posts: 3802
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: FDS DRAM Refresh Watchdog

Post by Pokun »

TakuikaNinja wrote: Sun Nov 30, 2025 2:06 am The safest known approach for handling the watchdog is to check for $4030.D1 in the IRQ handler and call Delay131 ($E149) if the bit is set. The BIOS' default IRQ handler performs a similar operation, thus it is suitable for programs which do not use other IRQ sources.
So this is what we need to know when programming an FDS game as far as we know now?



BTW could this watchdog or the DRAM refresh failing possibly be related to that ancient unknown problem with some RAM-adapters that no one ever had a good explanation for?
It's mentioned in various old threads on Famicom World like here, here and here. They also goes into Nesdev here.
User avatar
TakuikaNinja
Posts: 664
Joined: Mon Jan 09, 2023 6:42 pm
Location: New Zealand
Contact:

Re: FDS DRAM Refresh Watchdog

Post by TakuikaNinja »

Pokun wrote: Fri Jan 09, 2026 4:26 pm So this is what we need to know when programming an FDS game as far as we know now?
Yes. I've already put that in my WIP programming guide for the FDS, complete with an example custom IRQ handler. Just keep the I flag clear after reset and either ensure the IRQ flag stack variable points to the default BIOS one (which performs the ack + delay), or include the delay routine call in the custom IRQ handler.
BTW could this watchdog or the DRAM refresh failing possibly be related to that ancient unknown problem with some RAM-adapters that no one ever had a good explanation for?
It's mentioned in various old threads on Famicom World like here, here and here. They also goes into Nesdev here.
AFAIK, that is OAM corruption induced by signal reflections and has nothing to do with DRAM decay. (Commercially licensed games should not be causing DRAM decay in the first place - I'd imagine Nintendo had warnings/QA checks for this) The RAM adapter is a common licensed cartridge with this signal reflection problem, which is why I made an FDS port of Fiskbit's OAM corruption stress test some time ago.
Pokun
Posts: 3802
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: FDS DRAM Refresh Watchdog

Post by Pokun »

I see, thanks!

Those RAM-adapters with signal reflection problems may also need a guide how to fix them, something about adding termination resistors.
Fiskbit
Site Admin
Posts: 1665
Joined: Sat Nov 18, 2017 9:15 pm

Re: FDS DRAM Refresh Watchdog

Post by Fiskbit »

The correct way to fix the signal reflection problem is to add 30-100 ohm resistors in series on the CPU data lines in the cartridge. These act as termination resistors and suppress the reflections without side effects.

Unfortunately, people who encounter this problem with the FDS instead fix it incorrectly by pulling the CPU data lines to ground. While this does apparently solve the problem, it also eliminates CPU open bus, which may break any games that rely on it.
Pokun
Posts: 3802
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: FDS DRAM Refresh Watchdog

Post by Pokun »

In series? So I need to cut apart all 8 data lines somewhere and tie the ends together again with a termination resistor on each line?

I guess eliminating the open bus effectively means changing the specifications of the RAM-adapter and causing potential compatibility problems.
Post Reply