uroof: cosmetix
[urasm.git] / src / libfdc / emu_fdc.h
blob0f49ec8c793e70a6fb9e4c59b38a786c1d8a1250
1 /*
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
27 #ifndef LIBFDC_FDC_H
28 #define LIBFDC_FDC_H
30 #include "bdicommon.h"
31 #include "floppy.h"
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
38 #define FDC_FAST (0x01u)
40 enum {
41 DIF_NONE = 0,
42 DIF_BDI,
43 DIF_P3DOS,
44 DIF_END = -1
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 *);
56 struct FDC {
57 const int id;
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
62 unsigned block:1;
63 unsigned side:1;
64 unsigned step:1; // step dir
65 unsigned mfm:1;
66 unsigned idle:1;
67 unsigned crchi:1;
68 // options
69 unsigned optionTurbo:1;
70 // other things
71 uint8_t trk; // registers
72 uint8_t sec;
73 uint8_t data;
74 uint8_t com;
75 uint8_t state; // vp1-128 : mode (seek/rd/wr)
76 uint8_t tmp;
77 uint16_t wdata;
78 uint16_t tdata;
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
83 uint8_t buf[6];
84 int fmode;
85 int cnt;
86 int wait; // pause (ns)
87 int tns;
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
100 int resCnt;
101 int resPos;
102 uint8_t sr0, sr1, sr2, sr3; // status registers
104 /* BDI: last out to sysreg */
105 uint8_t sysreg;
107 /* used only in uPD765 emulator */
108 FDCSector slst[64];
109 int scnt;
112 typedef struct DiskHW_t DiskHW;
113 typedef struct DiskIF_t DiskIF;
116 // create disk interface with the given type
117 // DIF_NONE
118 // DIF_BDI
119 // DIF_P3DOS
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
130 // DIF_NONE
131 // DIF_BDI
132 // DIF_P3DOS
133 // invalid types will become "none"
134 void difSetHW (DiskIF *dif, int type);
136 // returns hardware type
137 // DIF_NONE
138 // DIF_BDI
139 // DIF_P3DOS
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);
152 // set "turbo mode"
153 void difSetTurbo (DiskIF *dif, int v);
155 // get "turbo mode"
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);
186 #ifdef __cplusplus
188 #endif
189 #endif