2 * WD1793/uPD765 emulator
3 * Copyright (c) 2009-..., SAM style
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is furnished
10 * to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
17 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
20 * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * changes by Ketmar // Invisible Vector
24 * Understanding is not required. Only obedience.
26 // FDC emulators, and high-level disk interface API
30 #include "bdicommon.h"
38 #define FDC_FAST (0x01u)
47 #define BDI_SYS (0xff)
48 #define FDC_COM (0x1f)
49 #define FDC_TRK (0x3f)
50 #define FDC_SEC (0x5f)
51 #define FDC_DATA (0x7f)
53 typedef struct FDC FDC
;
54 typedef void (*fdcCall
) (FDC
*);
58 unsigned irq
:1; // VG93:irq; uPD765:exec
59 unsigned drq
:1; // 0:data request
60 unsigned dir
:1; // drq dir: 0 - from CPU; 1 - to CPU
61 unsigned mr
:1; // master reset
64 unsigned step
:1; // step dir
69 unsigned optionTurbo
:1;
71 uint8_t trk
; // registers
75 uint8_t state
; // vp1-128 : mode (seek/rd/wr)
79 Floppy
*flop
[4]; // four floppy drives, always inited by `difCreate()`
80 Floppy
*flp
; // current floppy ptr
81 uint16_t crc
; // calculated crc
82 uint16_t fcrc
; // crc get from floppy
86 int wait
; // pause (ns)
88 const fdcCall
*plan
; // current task
89 unsigned pos
; // pos in plan
91 unsigned dma
:1; // not implemented yet
92 unsigned intr
:1; // uPD765 interrupt pending. reset @ com08
93 int hlt
; // head load time (all ns)
94 int hut
; // head unload time
95 int srt
; // step rate time
96 uint8_t comBuf
[8]; // uPD765 command args
97 int comCnt
; // arg count for command
98 int comPos
; // pos in comBuf
99 uint8_t resBuf
[8]; // uPD765 response
102 uint8_t sr0
, sr1
, sr2
, sr3
; // status registers
104 /* BDI: last out to sysreg */
107 /* used only in uPD765 emulator */
112 typedef struct DiskHW_t DiskHW
;
113 typedef struct DiskIF_t DiskIF
;
116 // create disk interface with the given type
120 // it alwasy creates four floppy drives
121 // invalid types will become "none"
122 // default active drive is 0
123 // default mode is non-turbo
124 DiskIF
*difCreate (int type
);
126 // destroy disk interface
127 void difDestroy (DiskIF
*dif
);
129 // set (force) hardware type for the given interface
133 // invalid types will become "none"
134 void difSetHW (DiskIF
*dif
, int type
);
136 // returns hardware type
140 int difGetHW (const DiskIF
*dif
);
142 // returns pointer to FDC struct (or NULL)
143 FDC
*difGetFDC (DiskIF
*dif
);
145 // returns pointer to Floppy struct (or NULL)
146 // `driveidx` is [0..3]
147 Floppy
*difGetFloppy (DiskIF
*dif
, int driveidx
);
149 // returns pointer to Floppy struct (or NULL)
150 Floppy
*difGetCurrentFloppy (DiskIF
*dif
);
153 void difSetTurbo (DiskIF
*dif
, int v
);
156 int difGetTurbo (const DiskIF
*dif
);
158 // reset disk interface (call on machine reset)
159 void difReset (DiskIF
*dif
);
161 // tstates: tstates passed
162 // cpufreq: 3500000 for ZX Spectrum
163 int difCalcNSFromTStates (int tstates
, int cpufreq
);
165 // update disk interface (up to the given `ns`)
166 // call this in emulation loop
167 // `ns` is time passed from the last call (in nanoseconds)
168 // it can be calculated with `difCalcNSFromTStates()`
169 // sync disk interface before in/out, and on each frame
170 void difSync (DiskIF
*dif
, int ns
);
172 // write value into FDC port
173 // WARNING: call only when FDC ports are active!
174 // returns `-1` if not an FDC port
175 // note that `0` means success!
176 int difOut (DiskIF
*dif
, int port
, int val
);
178 // write value into FDC port
179 // WARNING: call only when FDC ports are active!
180 // returns `-1` if not an FDC port
181 int difIn (DiskIF
*dif
, int port
);
183 // utility function used by both FDCs
184 //void fdcUpdateCRC16 (FDC *fdc, uint8_t val);