-
Notifications
You must be signed in to change notification settings - Fork 8
/
disk.c
320 lines (292 loc) · 7.16 KB
/
disk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright 2023 S. V. Nickolas.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following condition: The
* above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#ifdef __MSDOS__
#define diag_printf(...)
#else
#define diag_printf printf
#endif
typedef enum
{
DISK_NONE,
DISK_525SS,
DISK_525DS,
DISK_35DS
} DISKTYPE;
static FILE *disk[2];
static DISKTYPE disktype[2];
static uint8_t trk, sec, dat, stat;
static uint8_t ctrk;
int disksys_light;
/* Out of band data for a 200K floppy */
static uint8_t oob200[38]={
0xA1, 0xA1, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E,
0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x4E, 0x00, 0x00,
0x00, 0x00, 0x00, 0x28, 0x00, 0x03, 0x07, 0x00,
0xC2, 0x00, 0x5F, 0x00, 0xE0, 0x00, 0x00, 0x18,
0x01, 0x00, 0x03, 0x07, 0x4E, 0xFB
};
/*
* We only try to do a very sloppy emulation of the controller sufficient to
* run CP/M 3.1 from Leo Binkowski's disks.
*
* Every so often we need to generate an index pulse so our disk code (e.g.,
* OpenNabu's FD-IPL) can tell that a disk is present in the drive. If we are
* asked for the index hole status, we can check:
* * ((((unsigned)(disksys_light-1))<2)&&(!tick))
* * (disk[((unsigned)(disksys_light-1))]!=NULL)
* and if both of these are true, then there's an index hole.
*/
static int tick, subtick;
#define DSK_ENRDY 0x80 /* Drive not ready */
#define DSK_WRPRT 0x40 /* Write protect */
#define DSK_ETYPE 0x20 /* Data mark deleted */
#define DSK_EWFAU 0x20 /* Write fault */
#define DSK_HLOAD 0x20 /* Head loaded */
#define DSK_ESEEK 0x10 /* Seek error/Sector not found */
#define DSK_ECRC 0x08 /* Data CRC error */
#define DSK_ELOST 0x04 /* Data was lost */
#define DSK_TRK0 0x04 /* Head has reached track 0 */
#define DSK_DRQ 0x02 /* Ready for data read/write */
#define DSK_INDEX 0x02 /* Index hole detected */
#define DSK_BUSY 0x01 /* Busy */
#define DM_NONE 0
#define DM_RDSEC 1
static int mode;
static uint8_t buf[1024];
static int bufptr;
static int buflen;
static void disksys_do (uint8_t data)
{
unsigned d;
size_t off;
d=disksys_light-1;
switch (data)
{
case 0x07:
case 0x09:
diag_printf ("FDC: RESTORE\n");
trk=0;
stat&=(~(DSK_BUSY|DSK_ENRDY));
return;
case 0x59:
diag_printf ("FDC: tick up\n");
trk++;
return;
case 0x88: /* RDSEC LEN=0400 */
stat&=(~(DSK_ENRDY|DSK_ESEEK));
if (d>=2)
{
diag_printf ("FDC: read from bad drive\n");
stat|=DSK_ENRDY;
return;
}
if ((!sec)||(sec>5))
{
diag_printf ("FDC: invalid sector number $%02X\n", sec);
stat|=DSK_ESEEK;
return;
}
/* XXX: account for double side? */
off=trk;
off*=5;
off+=(sec-1);
off<<=10; /* *1024 */
fseek(disk[d], off, SEEK_SET);
bufptr=0;
diag_printf ("FDC: read from %c: T%02X S%02X\n", d+'A', trk, sec);
fread(buf, 1, 1024, disk[d]);
stat|=DSK_DRQ|DSK_BUSY;
mode=DM_RDSEC;
buflen=1024;
return;
case 0xC0:
diag_printf ("FDC: status\n");
buf[0]=trk;
buf[1]=0; /* side */
buf[2]=sec;
buf[3]=0x03; /* XXX is this correct? - 1024 BPS */
buf[4]=buf[5]=0; /* "CRC" */
buflen=6;
mode=DM_RDSEC;
return;
case 0xD0:
diag_printf ("FDC: IRQ\n");
stat&=(~(DSK_BUSY|DSK_ENRDY));
return;
case 0xE0: /* You dirty, dirty rat! */
printf ("FDC: dirty hack: sent OOB data\n");
memcpy(buf, oob200, 38);
bufptr=0;
buflen=38;
stat|=DSK_DRQ|DSK_BUSY;
mode=DM_RDSEC;
return;
}
diag_printf ("FDC: command $%02X, T=$%02X S=$%02X D=$%02X\n", data,
trk, sec, dat);
}
uint8_t disksys_read (uint8_t port)
{
switch (port&0x0F)
{
case 0x0:
return stat;
case 0x1:
return trk;
case 0x2:
return sec;
case 0x3:
if (mode==DM_RDSEC)
{
if (bufptr==buflen-1)
{
mode=0;
stat&=(~(DSK_DRQ|DSK_BUSY));
}
else
{
return buf[bufptr++];
}
}
return dat;
case 0xF:
return 0x10;
}
diag_printf ("FDC: IN: access to unknown port $%02X\n", port);
return 255;
}
void disksys_write (uint8_t port, uint8_t data)
{
switch (port&0x0F)
{
case 0x0:
disksys_do(data);
break;
case 0x1:
trk=data;
break;
case 0x2:
sec=data;
break;
case 0x3:
dat=data;
break;
case 0xF:
disksys_light=(data&0x06)>>1;
diag_printf ("FDC CARD: received message $%02X\n", data);
break;
default:
diag_printf ("FDC: OUT: access to unknown port $%02X with data $%02X\n",
port, data);
break;
}
}
void disksys_tick (void)
{
while (tick>512) tick-=512;
if (!mode)
{
unsigned d;
d=disksys_light-1;
stat &= (~DSK_INDEX);
if (d<2)
{
if (disk[d]&&(!tick))
{
stat|=DSK_INDEX;
}
}
}
}
void disksys_eject (int drive)
{
if ((drive!=0)&&(drive!=1)) return;
if (disk[drive])
{
fclose(disk[drive]);
disk[drive]=0;
disktype[drive]=DISK_NONE;
printf ("Ejected disk in drive %c:\n", drive+'A');
}
else
printf ("Drive %c: is already empty. Denied!\n", drive+'A');
}
int disksys_insert (int drive, char *filename)
{
size_t s;
if (!filename) return -1;
if (!*filename) return -1;
if ((drive!=0)&&(drive!=1)) return -1;
if (disk[drive])
{
diag_printf ("There's already a disk in that drive. Denied!\n");
return -1;
}
disk[drive]=fopen(filename, "r+b");
if (!disk[drive])
{
perror(filename);
return -1;
}
fseek(disk[drive], 0, SEEK_END);
s=ftell(disk[drive]);
fseek(disk[drive], 0, SEEK_SET);
switch (s)
{
case 204800:
disktype[drive]=DISK_525SS;
break;
case 409600:
disktype[drive]=DISK_525DS;
break;
case 819200:
disktype[drive]=DISK_35DS;
break;
default:
diag_printf ("That's not a disk image. Denied!\n");
fclose(disk[drive]);
disk[drive]=0;
return -1;
}
diag_printf ("Inserted '%s' in virtual drive %c:\n", filename, drive+'A');
return 0;
}
int disksys_init (void)
{
disk[0]=disk[1]=NULL;
disktype[0]=disktype[1]=DISK_NONE;
diag_printf ("Initializing disk system\n");
disksys_light=0;
mode=tick=subtick=0;
return 0;
}
int disksys_deinit (void)
{
if (disk[0]) fclose(disk[0]);
if (disk[1]) fclose(disk[1]);
disktype[0]=disktype[1]=DISK_NONE;
diag_printf ("Shutting down disk system\n");
disksys_light=0;
return 0;
}