-
Notifications
You must be signed in to change notification settings - Fork 25
/
disk_pravetz.c
577 lines (478 loc) · 14.4 KB
/
disk_pravetz.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation, version 2
** of the License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
/*
** Pravetz 8D disk drive emulation
** Copyright (C) 2009-2013 iss
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "system.h"
#include "6502.h"
#include "via.h"
#include "8912.h"
#include "gui.h"
#include "disk.h"
#include "disk_pravetz.h"
#include "monitor.h"
#include "6551.h"
#include "machine.h"
#define GENERAL_DISK_DEBUG 0
#if GENERAL_DISK_DEBUG
static int last_track = -1;
static int last_sector = -1;
#endif
static Uint8 disk_pravetz_read_selected(struct machine *oric);
static void disk_pravetz_write_selected(struct machine *oric, Uint8 data);
static void disk_pravetz_switch(struct machine *oric, Uint16 addr);
Uint8 disk_pravetz_read(struct machine *oric, Uint16 addr)
{
disk_pravetz_switch(oric, addr-PRAV_DISK_OFFSET);
/* odd addresses don't produce anything */
return (!(addr & 1))? disk_pravetz_read_selected(oric) : 0;
}
void disk_pravetz_write(struct machine *oric, Uint16 addr, Uint8 data)
{
disk_pravetz_switch(oric, addr-PRAV_DISK_OFFSET);
/* even addresses don't write anything */
if(addr & 1)
{
disk_pravetz_write_selected(oric, data);
}
}
/* Skewing-Table */
static int interleave[16] =
{
0, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 15
};
/* Translation Table */
static int translate[256] =
{
0x96, 0x97, 0x9a, 0x9b, 0x9d, 0x9e, 0x9f, 0xa6,
0xa7, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb2, 0xb3,
0xb4, 0xb5, 0xb6, 0xb7, 0xb9, 0xba, 0xbb, 0xbc,
0xbd, 0xbe, 0xbf, 0xcb, 0xcd, 0xce, 0xcf, 0xd3,
0xd6, 0xd7, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
0xdf, 0xe5, 0xe6, 0xe7, 0xe9, 0xea, 0xeb, 0xec,
0xed, 0xee, 0xef, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
0xf7, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x01,
0x80, 0x80, 0x02, 0x03, 0x80, 0x04, 0x05, 0x06,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x07, 0x08,
0x80, 0x80, 0x80, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0x80, 0x80, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
0x80, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x1b, 0x80, 0x1c, 0x1d, 0x1e,
0x80, 0x80, 0x80, 0x1f, 0x80, 0x80, 0x20, 0x21,
0x80, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x80, 0x80, 0x80, 0x80, 0x80, 0x29, 0x2a, 0x2b,
0x80, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32,
0x80, 0x80, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x80, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
};
static void disk_pravetz_update_position (struct pravetz_drive *drv)
{
if (drv->motor_on)
{
drv->byte = (1 + drv->byte) % PRAV_RAW_TRACK_SIZE;
}
}
Uint8 disk_pravetz_image_raw_byte(struct machine *oric, int drive, Uint16 t_idx, Uint16 s_idx, Uint16 b_idx)
{
long f_pos;
static Uint8 old;
static Uint8 eor;
static Uint8 raw;
static Uint8 check;
struct pravetz_drive *drv = &oric->pravetz.drv[drive];
raw = 0xFF;
switch (b_idx)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 20:
case 21:
case 22:
case 23:
case 24:
/* sync bytes */
return 0xFF;
case 6:
case 25:
/* sector header byte #1 */
return 0xD5;
case 7:
case 26:
/* sector header byte #2 */
return 0xAA;
case 8:
/* address header byte */
return 0x96;
case 9:
/* volume byte #1 */
check = drv->volume;
return 0xAA | (drv->volume >> 1);
case 10:
/* volume */
return 0xAA | drv->volume;
case 11:
/* track byte #1 */
check ^= t_idx;
return 0xAA | (t_idx >> 1);
case 12:
/* track byte #2 */
return 0xAA | t_idx;
case 13:
/* sector byte #1 */
check ^= s_idx;
return 0xAA | (s_idx >> 1);
case 14:
/* sector byte #2 */
return 0xAA | s_idx;
case 15:
/* checksum byte #1 */
return 0xAA | (check >> 1);
case 16:
/* checksum byte #2 */
return 0xAA | check;
case 17:
case 371:
/* address/data trailer byte #1 */
return 0xDE;
case 18:
case 372:
/* address/data trailer byte #2 */
return 0xAA;
case 19:
case 373:
/* address/data trailer byte #3 */
return 0xEB;
case 27:
/* data header */
eor = 0;
/* Read the coming sector */
f_pos = (256 * 16 * t_idx) + (256 * interleave[s_idx]);
if (drv->pimg)
drv->sector_ptr = &drv->pimg->rawimage[f_pos];
else
drv->sector_ptr = NULL;
return 0xAD;
case 370:
/* checksum */
return translate[eor & 0x3F];
default:
b_idx -= 28;
if (b_idx >= 0x56)
{
/* 6 Bit */
old = drv->sector_ptr[b_idx - 0x56];
old = old >> 2;
eor ^= old;
raw = translate[eor & 0x3F];
eor = old;
}
else
{
/* 3 * 2 Bit */
old = (drv->sector_ptr[b_idx] & 0x01) << 1;
old |= (drv->sector_ptr[b_idx] & 0x02) >> 1;
old |= (drv->sector_ptr[b_idx + 0x56] & 0x01) << 3;
old |= (drv->sector_ptr[b_idx + 0x56] & 0x02) << 1;
old |= (drv->sector_ptr[b_idx + 0xAC] & 0x01) << 5;
old |= (drv->sector_ptr[b_idx + 0xAC] & 0x02) << 3;
eor ^= old;
raw = translate[eor & 0x3F];
eor = old;
}
break;
}
return raw;
}
/**
** Returns the next byte from the 'disk'
*/
static Uint8 disk_pravetz_readbyte(struct machine *oric)
{
struct pravetz_drive *drv = &oric->pravetz.drv[oric->wddisk.c_drive];
/* make sure there's a 'disk in the drive' */
if (!drv->pimg)
return 0xFF;
disk_pravetz_update_position(drv);
/**/
#if GENERAL_DISK_DEBUG
if(last_sector != drv->byte / PRAV_RAW_BYTES_PER_SECTOR || last_track != drv->half_track)
{
printf("{R} fdu: T:%.2d S:%.2X.%.3d [%.2X]\n",
drv->half_track / 2,
drv->byte / PRAV_RAW_BYTES_PER_SECTOR,
drv->byte % PRAV_RAW_BYTES_PER_SECTOR,
drv->image[drv->half_track / 2][drv->byte]);
last_sector = drv->byte / PRAV_RAW_BYTES_PER_SECTOR;
last_track = drv->half_track;
}
#endif
/**/
return drv->image[drv->half_track / 2][drv->byte];
}
/**
** Returns the appropriate disk controller (IWM) register based on the
** state of the Q6 and Q7 select bits.
*/
static Uint8 disk_pravetz_read_selected(struct machine *oric)
{
struct pravetz_drive *drv = &oric->pravetz.drv[oric->wddisk.c_drive];
if (!drv->write_ready)
{
drv->write_ready = 0x80;
}
switch (drv->select)
{
case 0:
return disk_pravetz_readbyte(oric);
case 1:
return drv->prot | drv->motor_on;
case 2:
return drv->write_ready;
}
return 0;
}
static Uint8 temp_sector_buffer[PRAV_BYTES_PER_SECTOR + 2];
void disk_pravetz_write_image(struct pravetz_drive *d_ptr)
{
Uint8 eor;
Uint8 s_idx;
int sector_search_count;
int t_idx;
int b_idx;
int tb_idx;
int sector_count, f_pos;
if (!d_ptr)
return;
if (!d_ptr->dirty)
return;
tb_idx = 0;
for (t_idx = 0; t_idx < PRAV_TRACKS_PER_DISK; t_idx++)
{
sector_count = 16;
find_sector:
/* look for the sector header */
sector_search_count = 0;
while (0xD5 != d_ptr->image[t_idx][tb_idx])
{
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
if (++sector_search_count > PRAV_RAW_TRACK_SIZE) return;
}
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
if (0xAA != d_ptr->image[t_idx][tb_idx])
{
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
goto find_sector;
}
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
if (0x96 != d_ptr->image[t_idx][tb_idx])
{
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
goto find_sector;
}
/* found the sector header */
/* skip 'volume' and 'track' */
tb_idx = (tb_idx + 5) % PRAV_RAW_TRACK_SIZE;
/* sector byte #1 */
s_idx = 0x55 | (d_ptr->image[t_idx][tb_idx] << 1);
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
s_idx &= d_ptr->image[t_idx][tb_idx];
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
/* look for the sector data */
while (0xD5 != d_ptr->image[t_idx][tb_idx])
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
if (0xAA != d_ptr->image[t_idx][tb_idx])
{
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
goto find_sector;
}
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
if (0xAD != d_ptr->image[t_idx][tb_idx])
{
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
goto find_sector;
}
/* found the sector data */
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
eor = 0;
for (b_idx = 0; b_idx < 342; b_idx++)
{
eor ^= translate[d_ptr->image[t_idx][tb_idx]];
tb_idx = (tb_idx + 1) % PRAV_RAW_TRACK_SIZE;
if (b_idx >= 0x56)
{
/* 6 Bit */
temp_sector_buffer[b_idx - 0x56] |= (eor << 2);
}
else
{
/* 3 * 2 Bit */
temp_sector_buffer[b_idx] = (eor & 0x01) << 1;
temp_sector_buffer[b_idx] |= (eor & 0x02) >> 1;
temp_sector_buffer[b_idx + 0x56] = (eor & 0x04) >> 1;
temp_sector_buffer[b_idx + 0x56] |= (eor & 0x08) >> 3;
temp_sector_buffer[b_idx + 0xAC] = (eor & 0x10) >> 3;
temp_sector_buffer[b_idx + 0xAC] |= (eor & 0x20) >> 5;
}
}
/* write the sector */
f_pos = (PRAV_BYTES_PER_SECTOR * PRAV_SECTORS_PER_TRACK * t_idx) +
(PRAV_BYTES_PER_SECTOR * interleave[s_idx]);
memcpy(&d_ptr->pimg->rawimage[f_pos], temp_sector_buffer, PRAV_BYTES_PER_SECTOR);
d_ptr->pimg->modified = SDL_TRUE;
d_ptr->pimg->modified_time = 0;
sector_count--;
if (sector_count)
goto find_sector;
}
d_ptr->dirty = SDL_FALSE;
}
/**
** Writes a byte to the next position on the 'disk'
*/
static void disk_pravetz_writebyte(struct machine *oric, Uint8 w_byte)
{
struct pravetz_drive *drv = &oric->pravetz.drv[oric->wddisk.c_drive];
disk_pravetz_update_position(drv);
/* make sure there's a 'disk in the drive' */
if (!drv->pimg)
return;
if (drv->prot)
return;
/* don't allow impossible bytes */
if (w_byte < 0x96)
return;
drv->dirty = SDL_TRUE;
drv->image[drv->half_track / 2][drv->byte] = w_byte;
/**/
#if GENERAL_DISK_DEBUG
if(last_sector != drv->byte / PRAV_RAW_BYTES_PER_SECTOR || last_track != drv->half_track)
{
printf("{W} fdu: T:%.2d S:%.2X.%.3d [%.2X]\n",
drv->half_track / 2,
drv->byte / PRAV_RAW_BYTES_PER_SECTOR,
drv->byte % PRAV_RAW_BYTES_PER_SECTOR,
w_byte);
last_sector = drv->byte / PRAV_RAW_BYTES_PER_SECTOR;
last_track = drv->half_track;
}
#endif
/**/
}
/**
** Writes to the appropriate disk controller (IWM) register based on the
** state of the Q6 and Q7 select bits.
*/
static void disk_pravetz_write_selected(struct machine *oric, Uint8 w_byte)
{
struct pravetz_drive *drv = &oric->pravetz.drv[oric->wddisk.c_drive];
if (3 == drv->select)
{
if (drv->motor_on)
{
if (drv->write_ready)
{
disk_pravetz_writebyte(oric, w_byte);
drv->write_ready = 0;
}
}
}
}
static void disk_pravetz_switch (struct machine *oric, Uint16 addr)
{
struct pravetz_drive *drv = &oric->pravetz.drv[oric->wddisk.c_drive];
switch (addr & 0xF)
{
case 0x08:
drv->motor_on = 0;
disk_pravetz_write_image(drv);
break;
case 0x09:
drv->motor_on = 0x20;
break;
/* select drive 0/1 */
case 0x0A:
case 0x0B:
if (oric->wddisk.c_drive != (addr&1))
{
disk_pravetz_write_image(drv);
oric->wddisk.c_drive = addr&1;
}
break;
case 0x0C:
drv->select &= 0xFE;
break;
case 0x0D:
drv->select |= 0x01;
break;
case 0x0E:
drv->select &= 0xFD;
break;
case 0x0F:
drv->select |= 0x02;
break;
default:
{
int phase = (addr & 0x06) >> 1;
/* move the head in and out by stepping motor */
if (addr & 0x01)
{
phase += 4;
phase -= (drv->half_track % 4);
phase %= 4;
if (1 == phase)
{
if (drv->half_track < (2 * PRAV_TRACKS_PER_DISK - 2))
{
drv->half_track++;
}
}
else if (3 == phase)
{
if (drv->half_track > 0)
{
drv->half_track--;
}
}
}
}
break;
}
}