-
Notifications
You must be signed in to change notification settings - Fork 24
/
terakdisk.c
executable file
·218 lines (205 loc) · 4.94 KB
/
terakdisk.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
#include "defines.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <libintl.h>
#define _(String) gettext (String)
/* Terak floppy images are 128*26*76 = 252928 bytes, single-sided */
#define SECSIZE 64 /* words */
#define SECPERTRACK 26
#define MAXTRACK 76
typedef enum {
nopD, rtcD, stepinD, stepoutD, readtsD, readD, writeD, delD
} disk_cmd;
typedef enum {
enF = 1, headF = 020, intrF = 0100, doneF = 0200,
track0F = 01000, delF = 02000, wrprF = 04000, crcF = 010000,
lateF = 020000, syncF = 040000, errF = 0100000
} disk_flg;
/* Why bother, let's memory-map the files! */
typedef struct {
unsigned length;
unsigned short * image;
unsigned short * ptr;
unsigned char track;
disk_cmd cmd;
unsigned char ro;
unsigned char motor;
unsigned char inprogress;
unsigned char crc;
unsigned char cursec;
} tdisk_t;
tdisk_t tdisks[4];
static int selected = -1;
void tdisk_open(tdisk_t * pdt, char * name) {
int fd = open(name, O_RDWR);
if (fd == -1) {
pdt->ro = 1;
fd = open(name, O_RDONLY);
}
if (fd == -1) {
perror(name);
return;
}
pdt->length = lseek(fd, 0, SEEK_END);
if (pdt->length % SECSIZE) {
fprintf(stderr, _("%s is not an integer number of blocks\n"), name);
close(fd);
return;
}
pdt->image = mmap(0, pdt->length, PROT_READ | (pdt->ro ? 0 : PROT_WRITE), MAP_SHARED, fd, 0);
if (pdt->image == MAP_FAILED) {
pdt->image = 0;
perror(name);
}
if (pdt->ro) {
fprintf(stderr, _("%s will be read only\n"), name);
}
}
/* Are there any interrupts to open or close ? */
int tdisk_init() {
static char init_done = 0;
int i;
if (!init_done) {
disk_open(&tdisks[0], floppyA);
disk_open(&tdisks[1], floppyB);
disk_open(&tdisks[2], floppyC);
disk_open(&tdisks[3], floppyD);
init_done = 1;
}
for (i = 0; i < 4; i++) {
tdisks[i].ptr = NULL;
tdisks[i].track =
tdisks[i].motor =
tdisks[i].inprogress = 0;
}
selected = -1;
return OK;
}
void tdisk_finish() {
int i;
for (i = 0; i < 4; i++) {
if (!tdisks[i].image)
continue;
munmap(tdisks[i].image, tdisks[i].length);
}
}
static inline unsigned unit(d_word word) {
return (word >> 8) & 3;
}
static inline disk_cmd cmd(d_word word) {
return (word >> 1) & 7;
}
int
tdisk_read(c_addr addr, d_word *word) {
d_word offset = addr - TERAK_DISK_REG;
tdisk_t * pdt = &tdisks[selected];
int index;
switch(offset) {
case 0: /* status */
if (selected == -1) {
*word = errF | doneF;
break;
}
*word = (pdt->track ? 0 : track0F) | (pdt->ro ? wrprF : 0) | headF | doneF;
if (!pdt->inprogress) {
/* no operation started yet */
return OK;
} else switch (pdt->cmd) {
case nopD:
case rtcD:
case writeD:
case delD:
break;
case stepinD:
if (pdt->track == MAXTRACK)
*word |= errF;
else if (pdt->inprogress) {
pdt->track++;
fprintf(stderr, "trk = %d\n", pdt->track);
}
break;
case stepoutD:
if (pdt->track == 0)
*word |= errF;
else if (pdt->inprogress) {
pdt->track--;
fprintf(stderr, "trk = %d\n", pdt->track);
}
break;
case readtsD:
/* present all sectors round-robin */
if (pdt->inprogress) {
pdt->cursec %= SECPERTRACK;
pdt->cursec++;
}
break;
case readD:
fprintf(stderr, "Reading track %d, sector %d\n",
pdt->track, pdt->cursec);
pdt->ptr = pdt->image +
pdt->track*SECPERTRACK*SECSIZE +
(pdt->cursec-1)*SECSIZE;
break;
}
pdt->inprogress = 0;
break;
case 2: /* data */
switch (pdt->cmd) {
case readtsD:
*word = pdt->cursec << 8 | pdt->track;
fprintf(stderr, "Trk/sec = %d/%d\n", pdt->track, pdt->cursec);
break;
case readD:
*word = *pdt->ptr++;
break;
default:
*word = 0;
}
break;
}
return OK;
}
int
tdisk_write(c_addr addr, d_word word) {
d_word offset = addr - TERAK_DISK_REG;
tdisk_t * pdt;
switch (offset) {
case 0: /* control port */
if (word) {
/* Print a message if something other than turning
* all drives off is requested
*/
// fprintf(stderr, _("Writing disk cmd, data %06o\n"), word);
}
selected = unit(word);
if (selected >= 0) {
pdt = &tdisks[selected];
if (pdt->inprogress)
return BUS_ERROR;
pdt->inprogress = word & enF;
pdt->cmd = cmd(word);
if (pdt->inprogress && word & intrF) switch (pdt->cmd) {
case nopD:
ev_register(TTY_PRI, service, TICK_RATE*100/25, 0250);
break;
case rtcD:
ev_register(TTY_PRI, service, TICK_RATE/50, 0250);
break;
default:
fprintf(stderr, "Interrupt requested\n");
ev_register(TTY_PRI, service, TICK_RATE/1000, 0250);
}
}
break;
case 2: /* data port */
fprintf(stderr, _("Writing disk data reg, data %06o\n"), word);
break;
}
return OK;
}
int
tdisk_bwrite(c_addr addr, d_byte byte) {
return disk_write(addr & ~1, byte);
}