1 /* altair_dsk.c: MITS Altair 88-DISK Simulator
3 Copyright (c) 1997-2005, Charles E. Owen
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:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
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.
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.
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
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).
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
44 Drive Select Out (Device 10 OUT):
46 +---+---+---+---+---+---+---+---+
47 | C | X | X | X | Device |
48 +---+---+---+---+---+---+---+---+
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.
54 Device = value zero thru 15, selects drive to be controlled.
56 Drive Status In (Device 10 IN):
58 +---+---+---+---+---+---+---+---+
59 | R | Z | I | X | X | H | M | W |
60 +---+---+---+---+---+---+---+---+
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
70 Drive Control (Device 11 OUT):
72 +---+---+---+---+---+---+---+---+
73 | W | C | D | E | U | H | O | I |
74 +---+---+---+---+---+---+---+---+
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
92 Sector Position (Device 11 IN):
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.
97 +---+---+---+---+---+---+---+---+
98 | X | X | Sector Number | T |
99 +---+---+---+---+---+---+---+---+
102 Sector number = binary of the sector number currently under the
104 T = Sector True, is a 1 when the sector is positioned to read or
111 #include "altair_defs.h"
113 #define UNIT_V_ENABLE (UNIT_V_UF + 0) /* Write Enable */
114 #define UNIT_ENABLE (1 << UNIT_V_ENABLE)
116 #define DSK_SECTSIZE 137
118 #define DSK_TRACSIZE 4384
121 #define DSK_SIZE (DSK_SECT * DSK_SURF * DSK_CYL * DSK_SECTSIZE)
123 t_stat
dsk_svc (UNIT
*uptr
);
124 t_stat
dsk_reset (DEVICE
*dptr
);
129 /* Global data on status */
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};
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 */
141 int32 dsk_rwait
= 100; /* rotate latency */
144 /* 88DSK Standard I/O Data Structures */
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
) }
158 { ORDATA (DISK
, cur_disk
, 4) },
163 "DSK", dsk_unit
, dsk_reg
, NULL
,
165 NULL
, NULL
, &dsk_reset
,
169 /* Service routines to handle simlulator functions */
171 /* service routine - actually gets char & places in buffer */
173 t_stat
dsk_svc (UNIT
*uptr
)
180 t_stat
dsk_reset (DEVICE
*dptr
)
186 /* I/O instruction handlers, called from the CPU module when an
187 IN or OUT instruction is issued.
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
195 /* Disk Controller Status/Select */
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.
205 int32
dsk10(int32 io
, int32 data
)
208 if (io
== 0) { /* IN: return flags */
209 return ((~cur_flags
[cur_disk
]) & 0xFF); /* Return the COMPLEMENT! */
212 /* OUT: Controller set/reset/enable/disable */
217 /*printf("\n[%o] OUT 10: %x", PCX, data);*/
218 cur_disk
= data
& 0x0F;
220 cur_flags
[cur_disk
] = 0; /* Disable drive */
221 cur_sect
[cur_disk
= 0377];
222 cur_byte
[cur_disk
= 0377];
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 */
233 /* Disk Drive Status/Functions */
235 int32
dsk11(int32 io
, int32 data
)
239 if (io
== 0) { /* Read sector position */
240 /*printf("\n[%o] IN 11", PCX);*/
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 */
253 return (0); /* head not loaded - return 0 */
257 /* Drive functions */
260 return (0); /* no drive selected - can do nothin */
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;
269 cur_sect
[cur_disk
] = 0377;
270 cur_byte
[cur_disk
] = 0377;
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 */
281 cur_sect
[cur_disk
] = 0377;
282 cur_byte
[cur_disk
] = 0377;
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 */
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;
300 /* Interrupts & head current are ignored */
302 if (data
& 0x80) { /* write sequence start */
303 cur_byte
[cur_disk
] = 0;
304 cur_flags
[cur_disk
] |= 0x01; /* enter new write data on */
309 /* Disk Data In/Out*/
311 int32
dsk12(int32 io
, int32 data
)
317 uptr
= dsk_dev
.units
+ cur_disk
;
319 if ((i
= cur_byte
[cur_disk
]) < 138) { /* just get from buffer */
320 cur_byte
[cur_disk
]++;
321 return (dskbuf
[i
] & 0xFF);
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);
333 if (cur_byte
[cur_disk
] > 136) {
334 i
= cur_byte
[cur_disk
];
335 dskbuf
[i
] = data
& 0xFF;
339 i
= cur_byte
[cur_disk
];
342 dskbuf
[i
] = data
& 0xFF;
343 cur_byte
[cur_disk
]++;
353 i
= cur_byte
[cur_disk
]; /* null-fill rest of sector if any */
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;