-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfloppy_stx.c
430 lines (363 loc) · 10.6 KB
/
floppy_stx.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "common.h"
#include "mmu.h"
#include "floppy.h"
#include "floppy_stx.h"
#include "diag.h"
#define MAXSIDES 2
#define MAXTRACKS 86
struct sector {
int nr;
int size;
int offset;
int is_fuzzy;
BYTE *data;
BYTE *fuzzy_mask;
};
struct track {
int nr;
int side;
int size;
int fuzzy_size;
int sector_count;
struct sector *sectors;
int track_data_size; /* Can differ from total size */
BYTE *track_data;
};
struct floppy_stx {
struct track tracks[MAXTRACKS+128]; /* Both sides */
};
#define SECSIZE 512
#define TRKSIZE_MIN 6250
#define TRKSIZE 6256
/* Indexes for generating tracks */
#define TRKGEN_SEC9 0
#define TRKGEN_SEC10 1
#define TRKGEN_SEC11 2
#define TRKGEN_GAP1 0 /* 0x4e */
#define TRKGEN_GAP2A 1 /* 0x00 */
#define TRKGEN_GAP2B 2 /* 0xa1 */
#define TRKGEN_GAP3A 3 /* 0x4e */
#define TRKGEN_GAP3B1 4 /* 0x00 */
#define TRKGEN_GAP3B2 5 /* 0xa1 */
#define TRKGEN_GAP4 6 /* 0x4e */
#define TRKGEN_GAP5 7 /* 0x4e */
HANDLE_DIAGNOSTICS(floppy_stx);
static int read_sector(struct floppy *fl, int track, int side, int sector, LONG addr, int count)
{
struct floppy_stx *image = fl->image_data;
struct track *tracks;
int i,j,dst_pos;
struct sector *track_sectors;
BYTE *fuzzy_mask;
int track_side_num;
int sector_count;
int sec_num, sec_size;
int start_sector = -1;
tracks = image->tracks;
track_side_num = track | side << 7;
sector_count = tracks[track_side_num].sector_count;
track_sectors = tracks[track_side_num].sectors;
for(i=0;i<sector_count;i++) {
if(track_sectors[i].nr == sector) {
start_sector = i;
}
}
if(start_sector == -1) {
return FLOPPY_ERROR;
}
DEBUG("ReadSec: T: %d Sd: %d S: %d C: %d", track, side, sector, count);
dst_pos = addr;
for(i=0;i<count;i++) {
sec_num = start_sector + i;
if(sec_num > sector_count) return FLOPPY_ERROR;
sec_size = track_sectors[sec_num].size;
fuzzy_mask = track_sectors[sec_num].fuzzy_mask;
for(j=0;j<sec_size;j++) {
BYTE data;
data = track_sectors[sec_num].data[j];
if(track_sectors[sec_num].is_fuzzy) {
data = (data&fuzzy_mask[j])|(rand()&~fuzzy_mask[j]);
}
bus_write_byte(dst_pos, data);
dst_pos++;
}
}
return FLOPPY_OK;
}
static int write_sector(struct floppy *fl, int track, int side, int sector, LONG addr, int count)
{
return FLOPPY_ERROR;
}
static int read_track(struct floppy *fl, int track_num, int side, LONG addr, int dma_count)
{
struct floppy_stx *image = fl->image_data;
struct track *tracks,*track;
int track_side_num;
int size;
int i;
DEBUG("ReadTrk: T: %d Sd: %d A: %06x C: %d", track_num, side, addr, dma_count);
tracks = image->tracks;
track_side_num = track_num | side << 7;
track = &tracks[track_side_num];
if(!track) {
return FLOPPY_ERROR;
}
size = track->track_data_size;
/* Do not read more than was requested by DMA */
if(dma_count * SECSIZE < size) {
size = dma_count * SECSIZE;
}
/* Do not read more than a maximum track */
if(size > TRKSIZE) {
size = TRKSIZE;
}
for(i=0;i<size;i++) {
bus_write_byte(addr + i, track->track_data[i]);
}
return FLOPPY_OK;
}
static uint16_t stx_word(BYTE *x)
{
return x[0] + (x[1] << 8);
}
static uint32_t stx_long(BYTE *x)
{
return stx_word(x) + (stx_word(x + 2) << 16);
}
/* track_data is a pointer to the entire data block. Actual sector data is located on an offset within that area.
* fuzzy_mask is a pointer to the currently unused fuzzy_mask data block. Actual fuzzy data is always the first.
*/
static void init_sector_with_header(struct sector *sector_data, int index, BYTE *header, BYTE *track_data, BYTE *fuzzy_mask)
{
sector_data->offset = stx_long(header);
sector_data->nr = header[0x0a];
sector_data->size = 128 << (header[0x0b]&0x3);
if(header[0x0e] & 0x80) {
sector_data->is_fuzzy = 1;
sector_data->fuzzy_mask = xmalloc(sector_data->size);
memcpy(sector_data->fuzzy_mask, fuzzy_mask, sector_data->size);
} else {
sector_data->is_fuzzy = 0;
}
}
static void init_sector_without_header(struct sector *sector_data, int sector_index, BYTE *header, BYTE *track_data, BYTE *fuzzy)
{
sector_data->offset = sector_index * SECSIZE;
sector_data->nr = sector_index + 1;
sector_data->size = SECSIZE;
sector_data->is_fuzzy = 0; /* Without header, it will never be fuzzy */
}
static WORD crc16(int size, BYTE *buffer)
{
int i,j,byte;
WORD crc = 0xffff;
for(i=0;i<size;i++) {
byte = buffer[i];
crc ^= byte<<8;
for(j=0;j<8;j++) {
if(crc&0x8000) {
crc = (crc<<1)^0x1021;
} else {
crc <<= 1;
}
byte <<= 1;
}
}
return crc;
}
static void generate_track_data(int track, int side, BYTE *buffer, int sector_count, struct sector *sectors)
{
int structure[3][8] = {
{60, 12, 3, 22, 12, 3, 40, 664}, /* 9 sectors */
{60, 12, 3, 22, 12, 3, 40, 50}, /* 10 sectors */
{10, 3, 3, 22, 12, 3, 1, 20} /* 11 sectors */
};
int i,sec;
WORD crc;
int strnum;
int bufpos = 0;
BYTE *data;
if(sector_count < 9 || sector_count > 11) {
FATAL("Unable to generate track data for disks with other sector counts than 9, 10 or 11");
}
strnum = sector_count-9;
/* Gap 1 */
for(i=0;i<structure[strnum][TRKGEN_GAP1];i++) {
buffer[bufpos++] = 0x4e;
}
/* For each sector: */
for(sec=0;sec<sector_count;sec++) {
data = sectors[sec].data;
/* Gap 2 */
for(i=0;i<structure[strnum][TRKGEN_GAP2A];i++) {
buffer[bufpos++] = 0x00;
}
for(i=0;i<structure[strnum][TRKGEN_GAP2B];i++) {
buffer[bufpos++] = 0xa1;
}
/* Index */
buffer[bufpos++] = 0xfe;
buffer[bufpos++] = track;
buffer[bufpos++] = side;
buffer[bufpos++] = sec + 1;
buffer[bufpos++] = 2; /* 512 bytes */
crc = crc16(8, &buffer[bufpos]-8);
buffer[bufpos++] = (crc>>8)&0xff;
buffer[bufpos++] = crc&0xff;
/* Gap 3 */
for(i=0;i<structure[strnum][TRKGEN_GAP3A];i++) {
buffer[bufpos++] = 0x4e;
}
for(i=0;i<structure[strnum][TRKGEN_GAP3B1];i++) {
buffer[bufpos++] = 0x00;
}
for(i=0;i<structure[strnum][TRKGEN_GAP3B2];i++) {
buffer[bufpos++] = 0xa1;
}
/* Data */
buffer[bufpos++] = 0xfb;
for(i=0;i<SECSIZE;i++) {
buffer[bufpos++] = data[i];
}
crc = crc16(SECSIZE, data);
buffer[bufpos++] = (crc>>8)&0xff;
buffer[bufpos++] = crc&0xff;
/* Gap 4 */
for(i=0;i<structure[strnum][TRKGEN_GAP4];i++) {
buffer[bufpos++] = 0x4e;
}
}
/* Gap 5 */
for(i=0;i<structure[strnum][TRKGEN_GAP5];i++) {
buffer[bufpos++] = 0x4e;
}
if(bufpos < TRKSIZE_MIN || bufpos > TRKSIZE) {
FATAL("Generated track size was %d bytes, not %d-%d. This is fatal.",
bufpos, TRKSIZE_MIN, TRKSIZE);
}
}
static void load_track(struct floppy *fl, FILE *fp)
{
struct floppy_stx *image = fl->image_data;
void (*init_sector)(struct sector *, int, BYTE *, BYTE *, BYTE *);
struct track *tracks;
int track_side_num,track_num,track_side;
int sectors, fuzzy_size, track_image, track_size, track_data_size;
BYTE header[16] = {};
BYTE data[50000];
BYTE *fuzzy_pos;
BYTE *data_pos;
int track_image_offset __attribute__((unused)) = 0;
int i;
if(fread(header, 16, 1, fp) != 1) {
ERROR("Couldn't read from %s", fl->filename);
fclose(fp);
fl->fp = NULL;
return;
}
track_side_num = header[0x0e];
track_num = header[0x0e]&0x7f;
track_side = (header[0x0e]&0x80) >> 7;
track_size = stx_long(header);
track_data_size = stx_word(&header[0x0c]);
fuzzy_size = stx_long(header + 4);
sectors = stx_word(header + 8);
track_image = header[10] & 0xC0;
if(sectors == 0) {
fclose(fp);
fl->fp = NULL;
return;
}
if(fread(data, track_size - 16, 1, fp) != 1) {
ERROR("Couldn't read from %s", fl->filename);
fclose(fp);
fl->fp = NULL;
return;
}
data_pos = data;
tracks = image->tracks;
tracks[track_side_num].nr = track_num;
tracks[track_side_num].side = track_side;
tracks[track_side_num].size = track_size;
tracks[track_side_num].track_data_size = track_data_size;
tracks[track_side_num].sector_count = sectors;
tracks[track_side_num].fuzzy_size = fuzzy_size;
tracks[track_side_num].track_data = NULL;
tracks[track_side_num].sectors = xmalloc(sizeof(struct sector) * sectors);
fuzzy_pos = data;
if(header[10]&1) {
fuzzy_pos += sectors * 16;
}
data_pos = fuzzy_pos + fuzzy_size;
DEBUG("LoadTrk: T: %d Sd: %d Fl: %02x Tsz: %d", track_num, track_side, header[10], track_data_size);
if(track_image & 0x40) {
BYTE *ti_pos = data_pos;
if (track_image & 0x80) {
track_image_offset = stx_word(ti_pos);
ti_pos += 2;
}
track_data_size = stx_word(ti_pos);
TRACE("TI: Off: %d Tsz: %d", track_image_offset, track_data_size);
tracks[track_side_num].track_data = xmalloc(track_data_size);
memcpy(tracks[track_side_num].track_data, data_pos, track_data_size);
}
if(header[10] & 1)
init_sector = init_sector_with_header;
else
init_sector = init_sector_without_header;
for(i = 0; i < sectors; i++) {
struct sector *current_sector = &tracks[track_side_num].sectors[i];
init_sector(current_sector, i, data + i * 16, data_pos, fuzzy_pos);
current_sector->data = xmalloc(current_sector->size);
memcpy(current_sector->data, data_pos + current_sector->offset, current_sector->size);
DEBUG("LoadSec: Nr: %d Sz: %d Fz: %d Off: %d", current_sector->nr, current_sector->size, current_sector->is_fuzzy, current_sector->offset);
if(current_sector->is_fuzzy) {
fuzzy_pos += current_sector->size;
}
}
if(!tracks[track_side_num].track_data) {
tracks[track_side_num].track_data = xmalloc(TRKSIZE);
generate_track_data(track_num, track_side, tracks[track_side_num].track_data, sectors, tracks[track_side_num].sectors);
}
}
static void load_file(struct floppy *fl, FILE *fp)
{
BYTE header[16];
int tracks;
int i;
if(fread(header, 16, 1, fp) != 1) {
ERROR("Couldn't read from %s", fl->filename);
fclose(fp);
fl->fp = NULL;
return;
}
tracks = header[10];
fl->sectors = 11;
fl->sides = 1;
fl->tracks = tracks;
for(i = 0; i < tracks; i++) {
load_track(fl, fp);
if(!fl->fp) {
break;
}
}
if(fl->fp) {
fclose(fp);
}
fl->fp = NULL;
fl->inserted = 1;
return;
}
void floppy_stx_init(struct floppy *fl)
{
FILE *fp;
HANDLE_DIAGNOSTICS_NON_MMU_DEVICE(floppy_stx, "FSTX");
fl->image_data = (void *)calloc(1, sizeof(struct floppy_stx));
fp = fopen(fl->filename, "rb");
if(!fp) return;
fl->fp = fp;
load_file(fl, fp);
fl->read_sector = read_sector;
fl->write_sector = write_sector;
fl->read_track = read_track;
}