{{mbox |type=caution |text=This is a '''work-in-progress''' article section for hardware-verified behaviours of FDS registers. Contributions in the form of forum discussions and low-level [[emulator tests]] are appreciated.}} The information currently available for [[FDS]] registers has unfortunately become a mess of legacy and current research. This work-in-progress page is comprised of register information which has been verified through die scan analysis and hardware testing, intended to replace the wiki article contents once it is largely complete. Developers are encouraged to create [[emulator tests]] to verify behavior, especially low-level tests with minimal reliance on the [[FDS BIOS]]. == Registers == === Timer IRQ reload value low ($4020) === 7 bit 0 --------- LLLL LLLL |||| |||| ++++-++++- 8 LSB of timer IRQ reload value === Timer IRQ reload value high ($4021) === 7 bit 0 --------- LLLL LLLL |||| |||| ++++-++++- 8 MSB of timer IRQ reload value Unlike $4022, $4020 and $4021 are not affected by the $4023.0 (disk registers enabled) flag - the reload value can be altered even when disk registers are disabled. === Timer IRQ control ($4022) === 7 bit 0 --------- xxxx xxER || |-- Timer IRQ Repeat Flag +-- Timer IRQ Enabled When $4022 is written to with bit 1 (IRQ enabled) set, the reload value is copied into the IRQ's counter. Each CPU clock cycle the counter is decremented by one if the enable flag is set. When the counter's value is 0 and the IRQ enable flag is on, the following happens on every CPU cycle: * An IRQ is generated. * The IRQ counter is reset to its reload value (contained in $4020+$4021) * If the IRQ repeat flag is NOT set, the IRQ enabled flag is cleared and the counter stops. Notes: * This register is affected by the $4023.0 (Enable disk I/O registers) flag - if disk registers are disabled, it is impossible to start the IRQ counter (writing to $4022 has no effect). * Clearing $4023.0 will immediately stop the IRQ counter and acknowledge any pending timer IRQs. * Writing to $4022 with bit 1 (IRQ enabled) cleared will stop the IRQ counter and acknowledge any pending timer IRQs. * Enabling timer IRQs when the reload value is set to 0 will cause an IRQ immediately. Doing this with the repeat flag enabled will cause an infinite loop of IRQs on every CPU cycle. * Since the disk transfer routine also uses IRQs, it's very important to disable timer IRQs before doing any access to the disk. There are only 3 known ways to acknowledge the timer IRQ: * Read $4030 * Disable timer IRQs by writing to $4022 * Disable disk registers by writing to $4023 === I/O Enable ($4023) === 7 bit 0 --------- Wxxx xxSD | || | |+- Enable disk I/O registers (1: enable, 0: reset & disable writes) | +-- Enable audio registers (1: enable, 0: reset audio) +--------- Enable DRAM Refresh Watchdog Interrupts (1: enable) Clearing bit 7 will acknowledge pending IRQs from the DRAM refresh watchdog and mask further interrupts from this source, but does not affect the watchdog state itself. ==== Disk I/O reset ==== The disk I/O register toggle only affects its ''write-only'' registers. '''Read-only registers are always mapped/active, regardless of the state of this register.''' The following things occur while disk registers are reset/disabled: *$4025 is set to $06 (%00000110). Note that bit 3 is reflected in $4030.D3 when this occurs. *The byte transfer flag, timer, and DRAM refresh watchdog IRQ sources are all disabled. (i.e. also acknowledges said IRQs) **TODO: Disk/byte transfer flag state? **TODO: $4022 is probably set to $00, but the reload value is still writeable? Confirm on die scan. **The DRAM refresh watchdog's CPU cycle timer is set to all 1s = 4095, and the refresh counter is set to 0. The watchdog only resumes normal operation once disk registers are re-enabled. *$4026 is set to $7F (%01111111) and the [[FDS expansion port pinout|external connector]] output is disabled (note external pull-ups on EXT0..EXT6). The drive power/battery status in bit 7 is still observable in $4033. ==== Audio reset ==== The audio register toggle merely triggers a reset for the wavetable audio generation. The wavetable sound output is paused/halted until sound registers are re-enabled (state reflected in $4090-$4097): *Volume envelope is forced to 0 (likely $4080 = $80?) *Wave accumulator is reset to 0. *Mod counter is forced to 0, but can be briefly changed by new writes. *TODO: Mod table is initialised to all 0s? *TODO: Mod table writes ($4088) don't work properly? *TODO: $4095 is gradually forced to $1F? *TODO: $4096 is forced to $7F? ($3F excluding open bus bits) TODO: The safe duration for pausing & resuming the audio seems to depend on how long $4023.D1 was set to a given value? Is this analogue decay behavior? '''The remaining audio registers are always mapped/accessible, regardless of the state of this register''': *Wavetable RAM ($4040-$407F) *Wave frequency ($4082-$4083) *Mod envelope ($4084) *Mod frequency + mod counter halt ($4086-$4087) *Master volume + wavetable RAM write enable ($4089) *Envelope speed ($408A) *Readable audio registers at $4090-$4097 === Disk Data Output ($4024) === The data written to this register is loaded into an internal shift register the next time the byte transfer flag raises. The shift register contents are then shifted out and written to the disk drive via pin 5 of the RAM adapter cable (2C33 pin 52) when write mode is enabled. Writing to this register clears the byte transfer flag and acknowledges its IRQs. === FDS Control ($4025) === 7 bit 0 --------- I?1? MTDS |||| |||| |||| |||+- Scan Disk (0: reset, 1: scan) |||| ||+-- Drive Motor Control (0: start, 1: stop) |||| |+--- Transfer Mode (0: write, 1: read) |||| +---- Nametable Arrangement |||| 0: Horizontal ("Vertical Mirroring") |||| 1: Vertical ("Horizontal Mirroring") |||+------ ||+------- Unknown, always set to '1' |+-------- +--------- Interrupt Enabled ("disk IRQ") 1: Generate an IRQ every time the byte transfer flag is raised. The BIOS and commercial software are known to always write 1 to bit 5. Writing 0 to it alters bits 1 and 2 of the external connector ($4026/$4033) but its purpose is currently unknown. TODO: Does writing to this register acknowledge disk IRQs? Would it be based on which bits are toggled? === External Connector Output ($4026) === Output to [[FDS expansion port pinout|expansion terminal]] where there's a shutter on the back of the RAM adapter. The outputs of $4026 (open-collector with 4.7K ohm pull-ups (except on bit 7)) are shared with the inputs on $4033. TODO: Setting $4025.D5 to 0 alters bits 1 and 2 of the output, which are also reflected in $4033. Its purpose is currently unknown. === FDS Status ($4030) === 7 bit 0 --------- B?x? MxWT || | | || || | | |+- Timer IRQ (1: an IRQ occurred) || | | +-- DRAM Refresh Watchdog (1: an IRQ occurred) || | +---- Nametable Arrangement (from $4025.D3) || +------ |+-------- TODO: End of Head? It appears that this bit can be set well before the end of a disk side. +--------- Byte Transfer Flag Reading this register acknowledges all IRQs from the FDS ''except for the byte transfer flag'', so interrupt handlers must save the value read in order to check for each source. ==== Byte Transfer Flag ==== The byte transfer flag is set once 8 bits ''would'' have been transferred between the RAM adaptor & disk drive (i.e. it does ''not'' depend on the shift register contents corresponding to $4024/$4031). This occurs every 1792 cycles of the 21.4772 MHz master clock, or after every 149+1/3 CPU cycles. The following conditions must be met for the byte transfer flag to be updated at all, otherwise it will always return 0: * Write mode ($4025.D2 = 0): CRC Enabled ($4025.D6 = 1), $4025.D5 = 1 (TODO: otherwise it only gets set once?), motor state is irrelevant. Some games for the [[Super Magic Card]] and other [[RAM cartridge#Unlicensed RAM cartridges|unlicensed RAM cartridges]] use this with $4025.D7 set in order to use the byte transfer flag as an IRQ source for frame timing. * Read mode ($4025.D2 = 1): CRC Enabled ($4025.D6 = 1), $4025.D5 = 1 (TODO: what happens when this is cleared?), motor on ($4025.D1 = 0). TODO: Read mode also appears to be dependant on the disk inserted/ready flags? The byte transfer flag can be polled regardless of if interrupts are enabled: *''Tonkachi Editor'' forgoes the Interrupt Enabled flag in $4025.D7 and manually polls $4030.D7 during disk accesses. *''FMC Disk Card Checker'' briefly performs a manual poll of $4030.D7 before reading/writing data, with a timeout loop present for unknown reasons. (TODO: is this a replacement for polling $4032.D1?) If interrupts are enabled in $4025.D7, then an IRQ is asserted when the byte transfer flag is set. The flag, and thus the IRQ it generates, is cleared/acknowledged when $4024/$4031 has been serviced. Contrary to previous documentation, reading $4030 does ''not'' clear the byte transfer flag nor acknowledge its IRQs (if enabled). The byte transfer flag and IRQ can still be set/asserted past the end of a disk side: *The FDS BIOS relies on this for detecting truncated disk files (i.e. when a disk side is full?), as it reads the full file data block before checking bit 6. *''Kosodate Gokko'' (unlicensed disk copier) appears to rely on this for detecting the end of a disk side, as it unconditionally seeks for file header/data blocks until the number of read files is >= the value in the file amount block. ==== DRAM Refresh Watchdog ==== The 2C33 refreshes DRAM rows on CPU cycles spent accessing areas outside of PRG-RAM in order to account for DRAM chips which lack hidden refreshes (such as the Hitachi HM48416). The DRAM Refresh Watchdog tracks these refresh cycles (/RAS-only refreshes) 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. Note that both counters will continue to run while the IRQ is asserted. Ignoring the watchdog (by using SEI instructions, or disabling it via $4023) appears to result in DRAM decaying during the aforementioned scenarios. The decay behavior is analog and varies between RAM adapters and DRAM manufacturers. 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' [[FDS_BIOS#Interrupt/Reset_vector_controls|default IRQ handler]] performs a similar operation, thus it is suitable for programs which do not use other IRQ sources. === Disk Data Input ($4031) === This register is loaded with the contents of an internal shift register every time the byte transfer flag raises. The shift register receives its serial data from the disk drive via pin 9 of the RAM adapter cable (2C33 pin 51). Reading this register clears the byte transfer flag and acknowledges its IRQs. === Disk Drive Status ($4032) === 7 bit 0 --------- xxxx xPRD ||| ||+- Disk flag (0: Disk inserted; 1: Disk not inserted) |+-- Ready flag (0: Disk readу; 1: Disk not ready) +--- Protect flag (0: Not write protected; 1: Write protected or disk ejected) Notes: *TODO: There may be some bouncing behavior present on physical disk drives? ''Aspic'' might be accidentally relying on this for disk swap/flip detection. *TODO: Ready flag behavior. *The Protect flag corresponds to the write protect tab present on the upper-left corner of the inserted disk side. It is set to 1 if the tab is broken. Contrary to previous documentation, reading this register does not clear the byte transfer flag nor acknowledge its IRQs. === External Connector Input ($4033) === 7 bit 0 --------- BIII IIII |||| |||| |+++-++++- Input from [[FDS expansion port pinout|expansion terminal]] where there's a shutter on the back of the RAM adapter. +--------- Drive power/battery status (0: Voltage is low; 1: Good). A '0' bit in $4026 will cause the corresponding $4033 bit to be read as '0' (excluding the battery status) because of how open collector input works. The drive power/battery bit must be checked while the motor is on ($4025.D1 = 0), otherwise it will always be read as 0. TODO: The drive power/battery bit seems to be related to the LED present on disk drives. Determine the drive-side logic which controls this. === Sound ($4040-$4097) === For details on sound information, see [[FDS_audio|FDS audio]]. == References == * [//forums.nesdev.org/viewtopic.php?t=18668 Forum discussion]: FDS Emulation * [//forums.nesdev.org/viewtopic.php?t=26313 Forum thread]: FDS $4023 Testing * [//forums.nesdev.org/viewtopic.php?t=26359 Forum thread]: FDS DRAM Refresh Watchdog * [//forums.nesdev.org/download/file.php?id=10240 FdsIrqTests(v7)] * [https://github.com/TakuikaNinja/FDS-Mirroring-Test/releases/latest/download/FDS-Mirroring-Test.zip FDS-Mirroring-Test]