First Commit of my working state
[simh.git] / ALTAIR / altair_dsk.c
1 /* altair_dsk.c: MITS Altair 88-DISK Simulator
2
3 Copyright (c) 1997-2005, Charles E. Owen
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of Charles E. Owen shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from Charles E. Owen.
25
26 The 88_DISK is a 8-inch floppy controller which can control up
27 to 16 daisy-chained Pertec FD-400 hard-sectored floppy drives.
28 Each diskette has physically 77 tracks of 32 137-byte sectors
29 each.
30
31 The controller is interfaced to the CPU by use of 3 I/O addreses,
32 standardly, these are device numbers 10, 11, and 12 (octal).
33
34 Address Mode Function
35 ------- ---- --------
36
37 10 Out Selects and enables Controller and Drive
38 10 In Indicates status of Drive and Controller
39 11 Out Controls Disk Function
40 11 In Indicates current sector position of disk
41 12 Out Write data
42 12 In Read data
43
44 Drive Select Out (Device 10 OUT):
45
46 +---+---+---+---+---+---+---+---+
47 | C | X | X | X | Device |
48 +---+---+---+---+---+---+---+---+
49
50 C = If this bit is 1, the disk controller selected by 'device' is
51 cleared. If the bit is zero, 'device' is selected as the
52 device being controlled by subsequent I/O operations.
53 X = not used
54 Device = value zero thru 15, selects drive to be controlled.
55
56 Drive Status In (Device 10 IN):
57
58 +---+---+---+---+---+---+---+---+
59 | R | Z | I | X | X | H | M | W |
60 +---+---+---+---+---+---+---+---+
61
62 W - When 0, write circuit ready to write another byte.
63 M - When 0, head movement is allowed
64 H - When 0, indicates head is loaded for read/write
65 X - not used (will be 0)
66 I - When 0, indicates interrupts enabled (not used this simulator)
67 Z - When 0, indicates head is on track 0
68 R - When 0, indicates that read circuit has new byte to read
69
70 Drive Control (Device 11 OUT):
71
72 +---+---+---+---+---+---+---+---+
73 | W | C | D | E | U | H | O | I |
74 +---+---+---+---+---+---+---+---+
75
76 I - When 1, steps head IN one track
77 O - When 1, steps head OUT out track
78 H - When 1, loads head to drive surface
79 U - When 1, unloads head
80 E - Enables interrupts (ignored this simulator)
81 D - Disables interrupts (ignored this simulator)
82 C - When 1 lowers head current (ignored this simulator)
83 W - When 1, starts Write Enable sequence: W bit on device 10
84 (see above) will go 1 and data will be read from port 12
85 until 137 bytes have been read by the controller from
86 that port. The W bit will go off then, and the sector data
87 will be written to disk. Before you do this, you must have
88 stepped the track to the desired number, and waited until
89 the right sector number is presented on device 11 IN, then
90 set this bit.
91
92 Sector Position (Device 11 IN):
93
94 As the sectors pass by the read head, they are counted and the
95 number of the current one is available in this register.
96
97 +---+---+---+---+---+---+---+---+
98 | X | X | Sector Number | T |
99 +---+---+---+---+---+---+---+---+
100
101 X = Not used
102 Sector number = binary of the sector number currently under the
103 head, 0-31.
104 T = Sector True, is a 1 when the sector is positioned to read or
105 write.
106
107 */
108
109 #include <stdio.h>
110
111 #include "altair_defs.h"
112
113 #define UNIT_V_ENABLE (UNIT_V_UF + 0) /* Write Enable */
114 #define UNIT_ENABLE (1 << UNIT_V_ENABLE)
115
116 #define DSK_SECTSIZE 137
117 #define DSK_SECT 32
118 #define DSK_TRACSIZE 4384
119 #define DSK_SURF 1
120 #define DSK_CYL 77
121 #define DSK_SIZE (DSK_SECT * DSK_SURF * DSK_CYL * DSK_SECTSIZE)
122
123 t_stat dsk_svc (UNIT *uptr);
124 t_stat dsk_reset (DEVICE *dptr);
125 void writebuf();
126
127 extern int32 PCX;
128
129 /* Global data on status */
130
131 int32 cur_disk = 8; /* Currently selected drive */
132 int32 cur_track[9] = {0, 0, 0, 0, 0, 0, 0, 0, 377};
133 int32 cur_sect[9] = {0, 0, 0, 0, 0, 0, 0, 0, 377};
134 int32 cur_byte[9] = {0, 0, 0, 0, 0, 0, 0, 0, 377};
135 int32 cur_flags[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
136
137 char dskbuf[137]; /* Data Buffer */
138 int32 dirty = 0; /* 1 when buffer has unwritten data in it */
139 UNIT *dptr; /* fileref to write dirty buffer to */
140
141 int32 dsk_rwait = 100; /* rotate latency */
142
143
144 /* 88DSK Standard I/O Data Structures */
145
146 UNIT dsk_unit[] = {
147 { UDATA (&dsk_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, DSK_SIZE) },
148 { UDATA (&dsk_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, DSK_SIZE) },
149 { UDATA (&dsk_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, DSK_SIZE) },
150 { UDATA (&dsk_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, DSK_SIZE) },
151 { UDATA (&dsk_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, DSK_SIZE) },
152 { UDATA (&dsk_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, DSK_SIZE) },
153 { UDATA (&dsk_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, DSK_SIZE) },
154 { UDATA (&dsk_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_DISABLE, DSK_SIZE) }
155 };
156
157 REG dsk_reg[] = {
158 { ORDATA (DISK, cur_disk, 4) },
159 { NULL }
160 };
161
162 DEVICE dsk_dev = {
163 "DSK", dsk_unit, dsk_reg, NULL,
164 8, 10, 31, 1, 8, 8,
165 NULL, NULL, &dsk_reset,
166 NULL, NULL, NULL
167 };
168
169 /* Service routines to handle simlulator functions */
170
171 /* service routine - actually gets char & places in buffer */
172
173 t_stat dsk_svc (UNIT *uptr)
174 {
175 return SCPE_OK;
176 }
177
178 /* Reset routine */
179
180 t_stat dsk_reset (DEVICE *dptr)
181 {
182 cur_disk = 0;
183 return SCPE_OK;
184 }
185
186 /* I/O instruction handlers, called from the CPU module when an
187 IN or OUT instruction is issued.
188
189 Each function is passed an 'io' flag, where 0 means a read from
190 the port, and 1 means a write to the port. On input, the actual
191 input is passed as the return value, on output, 'data' is written
192 to the device.
193 */
194
195 /* Disk Controller Status/Select */
196
197 /* IMPORTANT: The status flags read by port 8 IN instruction are
198 INVERTED, that is, 0 is true and 1 is false. To handle this, the
199 simulator keeps it's own status flags as 0=false, 1=true; and
200 returns the COMPLEMENT of the status flags when read. This makes
201 setting/testing of the flag bits more logical, yet meets the
202 simulation requirement that they are reversed in hardware.
203 */
204
205 int32 dsk10(int32 io, int32 data)
206 {
207
208 if (io == 0) { /* IN: return flags */
209 return ((~cur_flags[cur_disk]) & 0xFF); /* Return the COMPLEMENT! */
210 }
211
212 /* OUT: Controller set/reset/enable/disable */
213
214 if (dirty == 1)
215 writebuf();
216
217 /*printf("\n[%o] OUT 10: %x", PCX, data);*/
218 cur_disk = data & 0x0F;
219 if (data & 0x80) {
220 cur_flags[cur_disk] = 0; /* Disable drive */
221 cur_sect[cur_disk = 0377];
222 cur_byte[cur_disk = 0377];
223 return (0);
224 }
225 cur_flags[cur_disk] = 0x1A; /* Enable: head move true */
226 cur_sect[cur_disk] = 0377; /* reset internal counters */
227 cur_byte[cur_disk] = 0377;
228 if (cur_track[cur_disk] == 0)
229 cur_flags[cur_disk] |= 0x40; /* track 0 if there */
230 return (0);
231 }
232
233 /* Disk Drive Status/Functions */
234
235 int32 dsk11(int32 io, int32 data)
236 {
237 int32 stat;
238
239 if (io == 0) { /* Read sector position */
240 /*printf("\n[%o] IN 11", PCX);*/
241 if (dirty == 1)
242 writebuf();
243 if (cur_flags[cur_disk] & 0x04) { /* head loaded? */
244 cur_sect[cur_disk]++;
245 if (cur_sect[cur_disk] > 31)
246 cur_sect[cur_disk] = 0;
247 cur_byte[cur_disk] = 0377;
248 stat = cur_sect[cur_disk] << 1;
249 stat &= 0x3E; /* return 'sector true' bit = 0 (true) */
250 stat |= 0xC0; /* set on 'unused' bits */
251 return (stat);
252 } else {
253 return (0); /* head not loaded - return 0 */
254 }
255 }
256
257 /* Drive functions */
258
259 if (cur_disk > 7)
260 return (0); /* no drive selected - can do nothin */
261
262 /*printf("\n[%o] OUT 11: %x", PCX, data);*/
263 if (data & 0x01) { /* Step head in */
264 cur_track[cur_disk]++;
265 if (cur_track[cur_disk] > 76 )
266 cur_track[cur_disk] = 76;
267 if (dirty == 1)
268 writebuf();
269 cur_sect[cur_disk] = 0377;
270 cur_byte[cur_disk] = 0377;
271 }
272
273 if (data & 0x02) { /* Step head out */
274 cur_track[cur_disk]--;
275 if (cur_track[cur_disk] < 0) {
276 cur_track[cur_disk] = 0;
277 cur_flags[cur_disk] |= 0x40; /* track 0 if there */
278 }
279 if (dirty == 1)
280 writebuf();
281 cur_sect[cur_disk] = 0377;
282 cur_byte[cur_disk] = 0377;
283 }
284
285 if (dirty == 1)
286 writebuf();
287
288 if (data & 0x04) { /* Head load */
289 cur_flags[cur_disk] |= 0x04; /* turn on head loaded bit */
290 cur_flags[cur_disk] |= 0x80; /* turn on 'read data available */
291 }
292
293 if (data & 0x08) { /* Head Unload */
294 cur_flags[cur_disk] &= 0xFB; /* off on 'head loaded' */
295 cur_flags[cur_disk] &= 0x7F; /* off on 'read data avail */
296 cur_sect[cur_disk] = 0377;
297 cur_byte[cur_disk] = 0377;
298 }
299
300 /* Interrupts & head current are ignored */
301
302 if (data & 0x80) { /* write sequence start */
303 cur_byte[cur_disk] = 0;
304 cur_flags[cur_disk] |= 0x01; /* enter new write data on */
305 }
306 return 0;
307 }
308
309 /* Disk Data In/Out*/
310
311 int32 dsk12(int32 io, int32 data)
312 {
313 static int32 rtn, i;
314 static long pos;
315 UNIT *uptr;
316
317 uptr = dsk_dev.units + cur_disk;
318 if (io == 0) {
319 if ((i = cur_byte[cur_disk]) < 138) { /* just get from buffer */
320 cur_byte[cur_disk]++;
321 return (dskbuf[i] & 0xFF);
322 }
323 /* physically read the sector */
324 /*printf("\n[%o] IN 12 (READ) T%d S%d", PCX, cur_track[cur_disk],
325 cur_sect[cur_disk]);*/
326 pos = DSK_TRACSIZE * cur_track[cur_disk];
327 pos += DSK_SECTSIZE * cur_sect[cur_disk];
328 rtn = fseek(uptr -> fileref, pos, 0);
329 rtn = fread(dskbuf, 137, 1, uptr -> fileref);
330 cur_byte[cur_disk] = 1;
331 return (dskbuf[0] & 0xFF);
332 } else {
333 if (cur_byte[cur_disk] > 136) {
334 i = cur_byte[cur_disk];
335 dskbuf[i] = data & 0xFF;
336 writebuf();
337 return (0);
338 }
339 i = cur_byte[cur_disk];
340 dirty = 1;
341 dptr = uptr;
342 dskbuf[i] = data & 0xFF;
343 cur_byte[cur_disk]++;
344 return (0);
345 }
346 }
347
348 void writebuf()
349 {
350 long pos;
351 int32 rtn, i;
352
353 i = cur_byte[cur_disk]; /* null-fill rest of sector if any */
354 while (i < 138) {
355 dskbuf[i] = 0;
356 i++;
357 }
358 /*printf("\n[%o] OUT 12 (WRITE) T%d S%d", PCX, cur_track[cur_disk],
359 cur_sect[cur_disk]); i = getch(); */
360 pos = DSK_TRACSIZE * cur_track[cur_disk]; /* calc file pos */
361 pos += DSK_SECTSIZE * cur_sect[cur_disk];
362 rtn = fseek(dptr -> fileref, pos, 0);
363 rtn = fwrite(dskbuf, 137, 1, dptr -> fileref);
364 cur_flags[cur_disk] &= 0xFE; /* ENWD off */
365 cur_byte[cur_disk] = 0377;
366 dirty = 0;
367 return;
368 }