-
Notifications
You must be signed in to change notification settings - Fork 1
/
disk.cc
194 lines (174 loc) · 4.01 KB
/
disk.cc
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
/*
* UAE - The Unusable Amiga Emulator
*
* disk support v0.0
* v0.0: 950312
*
* (c) 1995 Bernd Schmidt
*/
#include <iostream>
using namespace std;
#include <iomanip>
#include <unistd.h>
#include <stdio.h>
#include "amiga.h"
#include "disk.h"
static int side, direction, step;
static UBYTE selected = 15;
static bool dskready;
class drive {
FILE *diskfile;
unsigned int track;
unsigned int motoroff;
unsigned int trackoffs,insecpos;
bool secok;
UBYTE secbuf[544];
UWORD mfmbuf[544];
ULONG getseculong(unsigned int offset);
ULONG getmfmulong(unsigned int offset);
void readsec();
public:
drive(char *fname=0);
void eject();
void insert(char *fname);
void step();
bool track0() { return track == 0; }
bool empty() { return diskfile == 0; }
void motor(int off);
void GetData(UWORD &,UWORD &);
};
drive::drive(char *fname)
{
diskfile = 0;
if (fname != 0) insert(fname);
track = 0;
motoroff = 1;
}
void drive::eject()
{
if (diskfile != 0) fclose(diskfile);
diskfile = 0;
}
void drive::insert(char *fname)
{
diskfile = fopen(fname,"rb+");
}
void drive::step()
{
if (direction) {
if (track) track--;
} else {
if(track<85) track++;
}
}
void drive::motor(int off)
{
if (motoroff && !off) { trackoffs = 0; insecpos = 0; secok = false; }
motoroff = off;
}
ULONG drive::getmfmulong(unsigned int offs)
{
return (mfmbuf[offs] << 16) | mfmbuf[offs+1];
}
ULONG drive::getseculong(unsigned int offs)
{
return (secbuf[offs] << 24) | (secbuf[offs+1] << 16) | (secbuf[offs+2] << 8)
| (secbuf[offs+3]);
}
void drive::readsec()
{
int i;
secbuf[0] = secbuf[1] = 0x00;
secbuf[2] = secbuf[3] = 0xa1;
secbuf[4] = 0xff;
secbuf[5] = track*2+side;
secbuf[6] = trackoffs;
secbuf[7] = 10-trackoffs;
for(i=8;i<24;i++) secbuf[i] = 0;
fseek(diskfile,((track*2+side)*11+trackoffs)*512,SEEK_SET);
fread(&secbuf[32],sizeof(UBYTE),512,diskfile);
mfmbuf[0] = mfmbuf[1] = 0xaaaa;
mfmbuf[2] = mfmbuf[3] = 0x4489;
ULONG deven,dodd;
deven = getseculong(4); dodd = deven >> 1;
deven &= 0x55555555; dodd &= 0x55555555;
mfmbuf[4] = dodd >> 16; mfmbuf[5] = dodd;
mfmbuf[6] = deven>> 16; mfmbuf[7] = deven;
for (i=8;i<48;i++) mfmbuf[i] = 0;
for (i=0;i<512;i+=4){
deven = getseculong(i+32);
dodd = deven >> 1;
deven &= 0x55555555; dodd &= 0x55555555;
mfmbuf[(i>>1)+32] = dodd >> 16; mfmbuf[(i>>1)+33] = dodd;
mfmbuf[(i>>1)+256+32] = deven>> 16; mfmbuf[(i>>1)+256+33] = deven;
}
ULONG hck=0,dck=0;
for(i=4;i<24;i+=2) hck += getmfmulong(i);
deven = dodd = hck; dodd >>= 1;
mfmbuf[24] = dodd >> 16; mfmbuf[25] = dodd;
mfmbuf[26] = deven>> 16; mfmbuf[27] = deven;
for(i=32;i<544;i+=2) dck += getmfmulong(i);
deven = dodd = dck; dodd >>= 1;
mfmbuf[28] = dodd >> 16; mfmbuf[29] = dodd;
mfmbuf[30] = deven>> 16; mfmbuf[31] = deven;
secok = true;
}
void drive::GetData(UWORD &mfm,UWORD &byt)
{
if (!secok) readsec();
mfm = mfmbuf[insecpos]; byt = secbuf[insecpos++];
if (insecpos == 544){
secok = false;
if (++trackoffs == 11) trackoffs = 0;
}
}
static drive floppy[4];
void DISK_init()
{
floppy[0].insert("wb13.adf");
}
void DISK_select(UBYTE data)
{
int step_pulse;
if (selected != ((data >> 3) & 15)) dskready = false;
selected = (data >> 3) & 15;
side = (data >> 2) & 1;
direction = (data >> 1) & 1;
step_pulse = data & 1;
if (step != step_pulse) {
step = step_pulse;
if (step == 0){
for (int dr=0;dr<4;dr++){
if (!(selected & (1<<dr))) {
floppy[dr].step();
}
}
}
}
for (int dr=0;dr<4;dr++){
if (!(selected & (1<<dr))) {
floppy[dr].motor(data >> 7);
}
}
}
UBYTE DISK_status()
{
UBYTE st = 0x3c;
if (dskready) st &= ~0x20;
dskready = true;
for (int dr=0;dr<4;dr++){
if (!(selected & (1 << dr))) {
if (floppy[dr].track0()) { st &= ~0x10; }
if (floppy[dr].empty()) { st &= ~0x4; }
}
}
return st;
}
void DISK_GetData(UWORD &mfm,UWORD &byt)
{
for (int dr=0;dr<4;dr++){
if (!(selected & (1<<dr))) {
floppy[dr].GetData(mfm,byt);
}
}
}