joncampbell123 / dosbox-x

DOSBox-X fork of the DOSBox project
GNU General Public License v2.0
2.81k stars 383 forks source link

PC-98 FDC controller emulation #1186

Open joncampbell123 opened 5 years ago

joncampbell123 commented 5 years ago

Describe the solution you'd like Adapt floppy.cpp to operate as the typical NEC-based floppy controller, adapted to the I/O port layout and wiring of the controller in PC-98 mode.

Additional context The floppy controller in PC-98 is the same NEC controller used in IBM PC systems, though on different I/O ports and on a different interrupt, and some I/O ports arrange the bits differently.

According to the Undocumented PC-98 reference, there are two floppy controllers, at 90h and the other at C8h, each with their own interrupt, INT41 and INT42 (IRQ 10 and IRQ 11).

The FDC at 90h is documented for use with 1MB floppies (1.2MB 3-mode, and 1.44MB on later models), and the one at C8h is documented for use with 640KB (and IBM-compatible 720KB in 1.44MB mode?) floppies.

At least two games in the test collection I have directly talk to the floppy controller after boot: Xanandu-scenario and Dragon Buster. These games will not run in DOSBox-X until FDC emulation is implemented.

joncampbell123 commented 5 years ago

EDIT: Though not well translated by Google Translate, here is how they cooperate.

■ Floppy disk interface Explanation o The floppy disk interface (except for the 320KB FD I / F) of the PC-9800 The μPD765A is used for FDC (Floppy Disk Controller). o The PC-9800 series has two 640KB interfaces and 1MB interface There is an interface. In PC-9801 first generation · E · F · M, these two interfaces Can coexist, I / O port address, external interrupt number, DMA channel The number and BIOS work area all use different ones. PC-9801VM (to be exact Dual-use FD I / F is supported on C-98XA first) and later, 640 KB on internal FDD Both format and 1MB format can be used. o For dual-use FD I / F, for compatibility with the conventional interface, 1 MB FD only I / O port address same as I / F, external interrupt number, DMA channel number, BIOS password Mode (1 MB I / F mode) using the same area and the same I / O port as the 640 KB FD dedicated I / F. A mode (640 KB I / F mode) that uses a server address or the like is prepared. I / F Is the state of DIP SW 3-1 and 3-2 at startup, and the media used for boot It is decided by etc. 1 MB in the factory default condition (DIP SW 3-1 and 3-2 both OFF) When booted from format FD, it is in 1 MB I / F mode, FD of 640KB format It is an automatic mode that is automatically set to 640KB I / F mode when started from.


Model I / O port used
1 MB dedicated interface 0090 h, 0092 h, 0094 h
640 KB dedicated I / F 00C8h, 00CAh, 00CCh
Dual purpose FD I / F (1 MB I / F mode) 0090 h, 0092 h, 0094 h, 00 BE h
Dual purpose FD I / F (640KB I / F mode) 00C8h, 00CAh, 00CCh, 00BEh
Dual use FD I / F for 1.44 MB (1 MB I / F mode) 0090h, 0092h, 0094h, 00BEh, 04BEh
Dual use FD I / F for 1.44 MB (640 KB I / F mode) 00C8h, 00CAh, 00CCh, 00BEh, 04BEh

joncampbell123 commented 5 years ago

I will hold off on this feature until after the August 1st release.

joncampbell123 commented 5 years ago

Also, despite using the same FDC the on-disk format is different from IBM PC. Instead of using 512 bytes per sector as IBM PC does, PC-98 uses 1024 bytes per sector. The disk RPM differs as well, enough so a 1.2MB 3-mode in an IBM PC produces a data rate out of range of any of the rate selection bits available on IBM's implementation. DOSLIB FDC testing shows that there is one rate on IBM PC/AT FDCs that can be used to almost read 3-mode, but very unreliable. However if later PC-9821 systems configure their FDC for 3.5" 1.44MB floppies, disks written in one are readable on IBM PC.

cracyc commented 5 years ago

Also, the pc98 uses ready polling for disk change detection in the bios so that might have to be implemented if there's software which looks for it (the ready line in XT/ATs is tied high so the drives appear to always be ready and is the cause of the 4 irqs after controller reset).

The FDC at 90h is documented for use with 1MB floppies (1.2MB 3-mode, and 1.44MB on later models), and the one at C8h is documented for use with 640KB (and IBM-compatible 720KB in 1.44MB mode?) floppies.

FWIW, I have seen no software which expects the 2DD controller to be available and the bioses in newer machines don't even seem to check for it.

joncampbell123 commented 5 years ago

@cracyc Then I will implement only the FDC at port 90h, since the two games mentioned are working test cases, and leave a note to implement the second one at C8h if it turns out there is a game that uses it.

joncampbell123 commented 5 years ago

@cracyc I don't see a ready bit in the I/O port listing but I do see a "busy" bit, do you mean the software polls that bit until it clears?

cracyc commented 5 years ago

Nope, from page 5 of https://archive.org/details/bitsavers_necdatashe79_1461697

After the Specify Command has been sent to the uPD765, the unit select lines USO (pin 29) and US1 (pin 28) will automatically go into a scan mode. All four FDD's will be scanned by the uPD765 which will be looking for a change in the ready line from any of the drives. If the ready line from any of the drives should change state (such as due to the door opening or closing) then the uPD765 will generate an interrupt. When status register (STO) is read (after Sense Interrupt Status instruction has been issued) one will find that one of the drives will be in the "not ready" mode. This scanning of the ready line by the uPD765 occurs continuously between instructions, thus allowing the processor to know which drives are "off line".

Edit: to be clear, though, this is only for disk change detection and might not be necessary for any software.

joncampbell123 commented 5 years ago

@cracyc According to some others here, there are PC-98 games that do expect an interrupt to occur when disks are changed, so this is important information to keep in mind when implementing. There are at least 4 games in the test suite that I cannot run because they do not see any disk change despite issuing "swap floppy" from the mapper.

joncampbell123 commented 5 years ago

Very basic FDC emulation is implemented. Unfortunately it appears the reason the two games I wrote it for (Xanadu and Dragon Buster) talk directly to the FDC precisely because they use weird sector formats and out of range C/H/S values (for example, appears to try to read sectors with H=1 from the first side that normally have H=0). Dragon Buster appears to assume port 90h and DMA=3 and is not reading the correct data (and crashes).

So perhaps the last part to look at is a disk change interrupt if that helps.

cracyc commented 5 years ago

Dragon Buster appears to assume port 90h and DMA=3 and is not reading the correct data (and crashes).

I just checked in mame and it's only using dma channel 2, 3 isn't touched, so something else must be going wrong here.