-
Notifications
You must be signed in to change notification settings - Fork 7
/
Floppy.c
444 lines (307 loc) · 12.4 KB
/
Floppy.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
//
// Floppy.c
// The Omega Project
// https://github.com/h5n1xp/Omega
//
// Created by Matt Parsons on 02/02/2019.
// Copyright © 2019 Matt Parsons. All rights reserved.
// <[email protected]>
//
// Clocking and Checksum Code contributed by Dirk Hoffmann
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include "Floppy.h"
#include "Chipset.h"
#include "Memory.h"
#include <unistd.h>
#include <fcntl.h>
void encodeBlock(uint8_t* source, uint8_t* destination,int size){
for(int i=0;i<size;i++){
destination[i] = (source[i] >> 1) & 0x55;
destination[i+size] = (source[i]) & 0x55;
}
}
uint8_t addClockBits(uint8_t previous, uint8_t value) {
// Clear all previously set clock bits
value &= 0x55;
// Compute clock bits (clock bit values are inverted)
uint8_t lShifted = (value << 1);
uint8_t rShifted = (value >> 1) | (previous << 7);
uint8_t cBitsInv = lShifted | rShifted;
// Reverse the computed clock bits
uint64_t cBits = cBitsInv ^ 0xAA;
// Return original value with the clock bits added
return value | cBits;
}
void ADF2MFM(int fd, uint8_t* mfm){
int size =(int) lseek(fd, 0, SEEK_END);
//printf("FloppySize: %d\n",size);
//512 bytes per sector
int sectors = size/512;
//22 sectors per track (each side 11 sectors)
int tracks = (sectors / 22);
//each track is 12798 bytes in size, multiplied by 2 because there are 2 sides
//int mfmSize = tracks * (12798 * 2);
uint8_t adf[size];
// uint8_t mfm[mfmSize];
uint8_t lowlevelSector[544]; //bytes per low level sector
lseek(fd, 0, SEEK_SET);
read(fd, adf, size);
int count = 0;
int s = 0;
for(int track = 0;track<tracks;track++){
for(int side=0;side<2;side++){
for(int sector=0;sector<11;sector++){
//int secCountDown = 11 - sector;
//printf("%d: Track %d, Side: %d, Sector %d (%d)\n",s,track,side,sector,secCountDown);
//Build the sector
lowlevelSector[0] = 0x0;
lowlevelSector[1] = 0x0;
lowlevelSector[2] = 0xA1; // will be a sync mark
lowlevelSector[3] = 0xA1; // will be a sync mark
//sector info
lowlevelSector[4] = 0xFF;
lowlevelSector[5] = track << 1 | side;
lowlevelSector[6] = sector;
lowlevelSector[7] = 11 - sector;
//Sector label
lowlevelSector[8] = 0x0;
lowlevelSector[9] = 0x0;
lowlevelSector[10] = 0x0;
lowlevelSector[11] = 0x0;
lowlevelSector[12] = 0x0;
lowlevelSector[13] = 0x0;
lowlevelSector[14] = 0x0;
lowlevelSector[15] = 0x0;
lowlevelSector[16] = 0x0;
lowlevelSector[17] = 0x0;
lowlevelSector[18] = 0x0;
lowlevelSector[19] = 0x0;
lowlevelSector[20] = 0x0;
lowlevelSector[21] = 0x0;
lowlevelSector[22] = 0x0;
lowlevelSector[23] = 0x0;
//data
for(int i=0;i<512;++i){
lowlevelSector[32+i] = adf[i+(count*512)];
}
//***************
//Encode
mfm[s+0] = 0xAA;
mfm[s+1] = 0xAA;
mfm[s+2] = 0xAA;
mfm[s+3] = 0xAA;
mfm[s+4] = 0x44;
mfm[s+5] = 0x89;
mfm[s+6] = 0x44;
mfm[s+7] = 0x89;
//info
encodeBlock(&lowlevelSector[4], &mfm[s+8], 4); // adds 8 bytes
//Disklabel
encodeBlock(&lowlevelSector[8], &mfm[s+16], 16);//adds 32 bytes
//Data section
encodeBlock(&lowlevelSector[32], &mfm[s+64], 512);
//Header checksum
uint8_t hcheck[4] = { 0, 0, 0, 0 };
for(unsigned i = 8; i < 48; i += 4) {
hcheck[0] ^= mfm[s+i];
hcheck[1] ^= mfm[s+i+1];
hcheck[2] ^= mfm[s+i+2];
hcheck[3] ^= mfm[s+i+3];
}
lowlevelSector[24] = hcheck[0];
lowlevelSector[25] = hcheck[1];
lowlevelSector[26] = hcheck[2];
lowlevelSector[27] = hcheck[3];
//header checksum
encodeBlock(&lowlevelSector[24], &mfm[s+48], 4); //adds 8 bytes
// Data checksum
uint8_t dcheck[4] = { 0, 0, 0, 0 };
for(unsigned i = 64; i < 1088; i += 4) {
dcheck[0] ^= mfm[s+i];
dcheck[1] ^= mfm[s+i+1];
dcheck[2] ^= mfm[s+i+2];
dcheck[3] ^= mfm[s+i+3];
}
lowlevelSector[28] = dcheck[0];
lowlevelSector[29] = dcheck[1];
lowlevelSector[30] = dcheck[2];
lowlevelSector[31] = dcheck[3];
//Encode Data checksum
encodeBlock(&lowlevelSector[28], &mfm[s+56], 4); //adds 8 bytes
//Add clocking bits
for(int i=8;i<1088;i++){
uint8_t previous = mfm[s+i-1];
mfm[s+i] = addClockBits(previous,mfm[s+i]);
//mfm[s+i] = clocking(previous, mfm[s+i]);
}
s += 1088; //Why not 1088, which is the size of the data we've produced
count +=1;
}
//Add clocking bits to the track gap
mfm[s] = addClockBits(mfm[s-1],0);
mfm[s+1] = 0xA8;
mfm[s+2] = 0x55;
mfm[s+3] = 0x55;
mfm[s+4] = 0xAA;
for(int i=5;i<700;i++){
uint8_t previous = mfm[s+i-1];
mfm[s+i] = addClockBits(previous,0);
//mfm[s+i] = clocking(previous, mfm[s+i]);
}
s += 830; //pad track to make 12798 bytes to meet the ADF-EXT spec.
//printf("\n");
}
}
//printf("loaded");
}
//*******************************
int floppySync = 0;
int driveSelected=0;
Fd_t df[4];
void floppyIndexReset(){
df[driveSelected].index = 4;
}
uint8_t floppyDataRead(){ //this function should be called by the DMA
int position = (df[driveSelected].cylinder * (12798 * 2)) + (df[driveSelected].side * 12798) + df[driveSelected].index;
df[driveSelected].index +=1;
if(df[driveSelected].index>12667){ //ADF track has 12798 bytes, but on a normal AmigaOS disk 12668 are used.
df[driveSelected].index= 0;
CIAIndex(&CIAB); // generate CIAB index interupt
//int stinkmog = (df[driveSelected].track * (12798 * 2)) + (df[driveSelected].side * 12798) + df[driveSelected].index;
//uint8_t* dri = &df[driveSelected].mfmData[stinkmog];
//printf("wartest du!");
}
uint8_t retVal = df[driveSelected].mfmData[position];
/*
if(retVal==0){
//printf("Uh oh!");
}
*/
return retVal;
}
void floppyInsert(int drive){
if(df[drive].idMode !=0){
//Only Vaild drives have an ID Mode == 0
return;
}
if( (df[drive].pra & 0x4) == 0x4){
df[drive].pra &= 0xFB; // eject disk;
printf("Disk ejected from df%d:\n",drive);
}else{
df[drive].pra |= 0x04; // insert disk
printf("Disk inserted in df%d:\n",drive);
}
}
uint8_t* floppyInit(int drive){
df[drive].idMode = -1;
df[drive].index = 0;
df[drive].cylinder = 0;
df[drive].side = 0;
df[driveSelected].pra &= 0xFB; // no disk;
df[driveSelected].pra &= 0xEF; //cylinder 0
return df[drive].mfmData;
}
void floppyState(){
CIAA.pra &= 0xC3;
CIAA.pra |= df[driveSelected].pra;
}
void floppySetState(){ //To be called when Writes to CIAB prb happen.
static uint8_t PRB;
static int count = 0;
PRB = CIAB.prb;
switch (PRB & 0x78) {
case 0x78:
CIAA.pra &=0xC3; //Need to take the Drives off the floppy bus.
return;
break;
case 0x70:
driveSelected = 0;
break;
case 0x68:
driveSelected = 1;
break;
case 0x58:
driveSelected = 2;
break;
case 0x38:
driveSelected = 3;
break;
default:
break;
}
//count++;
//printf("%d: ",count);
//ID mode... to identify external drives...
if(df[driveSelected].idMode>0){ // Id mode
//printf("DF%d ID Mode: %d\n",driveSelected,df[driveSelected].idMode); // The Disk Ready sitnal is pusled 32 times to signle a drive is present on the bus
df[driveSelected].pra &= 0xDF; //Drive ready flag signals the drive is there
df[driveSelected].idMode -=1;
CIAA.pra |= df[driveSelected].pra & 0x3C;
return;
}
// If no change in state just return
if(PRB == df[driveSelected].prb){
floppyState();
//printf("---\n");
return;
}
//printf("DF%d - ",driveSelected);
if(PRB & 0x80){
//printf(" Motor Off ");
//df[driveSelected].prb |= 0x80;
if(df[driveSelected].idMode==-1){ //is this the first time the motor has been turned off?
df[driveSelected].idMode = 32; //if so activate ID Mode
}
df[driveSelected].pra |= 0x20; //Drive not ready
}else{
//printf(" Motor On ");
//df[driveSelected].prb &= 0x7F;
if(df[driveSelected].pra & 0x4){ // if disk is inserted then
df[driveSelected].pra &= 0xDF; // Drive ready
}
//floppySync=0;
}
//Step head (don't step again if we've already stepped)
if( (PRB & 0x1) && !(df[driveSelected].prb & 0x1) ){
printf("%04x - DF%d Click\n",count,driveSelected);
if(PRB & 0x2){
df[driveSelected].cylinder -=1;
//printf("DF%d Head Stepped back: %d\n",driveSelected,df[driveSelected].cylinder);
}else{
df[driveSelected].cylinder +=1;
//printf("DF%d Head Stepped forward: %d\n",driveSelected,df[driveSelected].cylinder);
}
if(df[driveSelected].cylinder < 0){
df[driveSelected].cylinder = 0;
}
if(df[driveSelected].cylinder >79){ //not sure why sometimes the drive tries to go up to track 80.. with ks1.3
df[driveSelected].cylinder = 79;
}
//floppySync = 0;
//printf(" to track %d|",df[driveSelected].track);
}
if(df[driveSelected].cylinder==0){
df[driveSelected].pra &= 0xEF; //Track 0 reached
}else{
df[driveSelected].pra |= 0X10; // not track 0;
}
if(PRB & 0x4){
df[driveSelected].side = 0;
//df[driveSelected].prb |= 0x4;
//printf(" -lower surface.\n");
//floppySync = 0;
}else{
df[driveSelected].side = 1;
//df[driveSelected].prb &= 0xFB;
//printf(" -upper surface.\n");
//floppySync = 0;
}
df[driveSelected].prb = PRB;
//printf("\n");
CIAA.pra &= 0xC3;
CIAA.pra |= df[driveSelected].pra;
//CIAB.prb |= df[driveSelected].prb & 0x87;
}