First Commit of my working state
[simh.git] / NOVA / nova_dsk.c
1 /* nova_dsk.c: 4019 fixed head disk simulator
2
3 Copyright (c) 1993-2008, Robert M. Supnik
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 Robert M Supnik 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 Robert M Supnik.
25
26 dsk fixed head disk
27
28 04-Jul-07 BKR device name changed to DG's DSK from DEC's DK,
29 DEV_xxx macros now used for consistency,
30 added secret DG DIC function,
31 fixed boot info table size calculation
32 15-May-06 RMS Fixed bug in autosize attach (reported by David Gesswein)
33 04-Jan-04 RMS Changed sim_fsize calling sequence
34 26-Jul-03 RMS Fixed bug in set size routine
35 14-Mar-03 RMS Fixed variable capacity interaction with save/restore
36 03-Mar-03 RMS Fixed variable capacity and autosizing
37 03-Oct-02 RMS Added DIB
38 06-Jan-02 RMS Revised enable/disable support
39 23-Aug-01 RMS Fixed bug in write watermarking
40 26-Apr-01 RMS Added device enable/disable support
41 10-Dec-00 RMS Added Eclipse support
42 15-Oct-00 RMS Editorial changes
43 14-Apr-99 RMS Changed t_addr to unsigned
44
45 The 4019 is a head-per-track disk. To minimize overhead, the entire disk
46 is buffered in memory.
47 */
48
49 #include "nova_defs.h"
50 #include <math.h>
51
52 #define UNIT_V_AUTO (UNIT_V_UF + 0) /* autosize */
53 #define UNIT_V_PLAT (UNIT_V_UF + 1) /* #platters - 1 */
54 #define UNIT_M_PLAT 07
55 #define UNIT_PLAT (UNIT_M_PLAT << UNIT_V_PLAT)
56 #define UNIT_GETP(x) ((((x) >> UNIT_V_PLAT) & UNIT_M_PLAT) + 1)
57 #define UNIT_AUTO (1 << UNIT_V_AUTO)
58 #define UNIT_PLAT (UNIT_M_PLAT << UNIT_V_PLAT)
59
60 /* Constants */
61
62 #define DSK_NUMWD 256 /* words/sector */
63 #define DSK_NUMSC 8 /* sectors/track */
64 #define DSK_NUMTR 128 /* tracks/disk */
65 #define DSK_DKSIZE (DSK_NUMTR*DSK_NUMSC*DSK_NUMWD) /* words/disk */
66 #define DSK_AMASK ((DSK_NUMDK*DSK_NUMTR*DSK_NUMSC) - 1)
67 /* address mask */
68 #define DSK_NUMDK 8 /* disks/controller */
69 #define GET_DISK(x) (((x) / (DSK_NUMSC * DSK_NUMTR)) & (DSK_NUMDK - 1))
70
71 /* Parameters in the unit descriptor */
72
73 #define FUNC u4 /* function */
74
75 /* Status register */
76
77 #define DSKS_WLS 020 /* write lock status */
78 #define DSKS_DLT 010 /* data late error */
79 #define DSKS_NSD 004 /* non-existent disk */
80 #define DSKS_CRC 002 /* parity error */
81 #define DSKS_ERR 001 /* error summary */
82 #define DSKS_ALLERR (DSKS_WLS | DSKS_DLT | DSKS_NSD | DSKS_CRC | DSKS_ERR)
83
84 /* Map logical sector numbers to physical sector numbers
85 (indexed by track<2:0>'sector)
86 */
87
88 static const int32 sector_map[] = {
89 0, 2, 4, 6, 1, 3, 5, 7, 1, 3, 5, 7, 2, 4, 6, 0,
90 2, 4, 6, 0, 3, 5, 7, 1, 3, 5, 7, 1, 4, 6, 0, 2,
91 4, 6, 0, 2, 5, 7, 1, 3, 5, 7, 1, 3, 6, 0, 2, 4,
92 6, 0, 2, 4, 7, 1, 3, 5, 7, 1, 3, 5, 0, 2, 4, 6
93 };
94
95 #define DSK_MMASK 077
96 #define GET_SECTOR(x) ((int) fmod (sim_gtime() / ((double) (x)), \
97 ((double) DSK_NUMSC)))
98
99 extern uint16 M[];
100 extern UNIT cpu_unit;
101 extern int32 int_req, dev_busy, dev_done, dev_disable;
102 extern int32 saved_PC, SR, AMASK;
103
104 int32 dsk_stat = 0; /* status register */
105 int32 dsk_da = 0; /* disk address */
106 int32 dsk_ma = 0; /* memory address */
107 int32 dsk_wlk = 0; /* wrt lock switches */
108 int32 dsk_stopioe = 0; /* stop on error */
109 int32 dsk_time = 100; /* time per sector */
110
111 DEVICE dsk_dev;
112 int32 dsk (int32 pulse, int32 code, int32 AC);
113 t_stat dsk_svc (UNIT *uptr);
114 t_stat dsk_reset (DEVICE *dptr);
115 t_stat dsk_boot (int32 unitno, DEVICE *dptr);
116 t_stat dsk_attach (UNIT *uptr, char *cptr);
117 t_stat dsk_set_size (UNIT *uptr, int32 val, char *cptr, void *desc);
118
119 /* DSK data structures
120
121 dsk_dev device descriptor
122 dsk_unit unit descriptor
123 dsk_reg register list
124 */
125
126 DIB dsk_dib = { DEV_DSK, INT_DSK, PI_DSK, &dsk };
127
128 UNIT dsk_unit = {
129 UDATA (&dsk_svc, UNIT_FIX+UNIT_ATTABLE+UNIT_BUFABLE+UNIT_MUSTBUF,
130 DSK_DKSIZE)
131 };
132
133 REG dsk_reg[] = {
134 { ORDATA (STAT, dsk_stat, 16) },
135 { ORDATA (DA, dsk_da, 16) },
136 { ORDATA (MA, dsk_ma, 16) },
137 { FLDATA (BUSY, dev_busy, INT_V_DSK) },
138 { FLDATA (DONE, dev_done, INT_V_DSK) },
139 { FLDATA (DISABLE, dev_disable, INT_V_DSK) },
140 { FLDATA (INT, int_req, INT_V_DSK) },
141 { ORDATA (WLK, dsk_wlk, 8) },
142 { DRDATA (TIME, dsk_time, 24), REG_NZ + PV_LEFT },
143 { FLDATA (STOP_IOE, dsk_stopioe, 0) },
144 { NULL }
145 };
146
147 MTAB dsk_mod[] = {
148 { UNIT_PLAT, (0 << UNIT_V_PLAT), NULL, "1P", &dsk_set_size },
149 { UNIT_PLAT, (1 << UNIT_V_PLAT), NULL, "2P", &dsk_set_size },
150 { UNIT_PLAT, (2 << UNIT_V_PLAT), NULL, "3P", &dsk_set_size },
151 { UNIT_PLAT, (3 << UNIT_V_PLAT), NULL, "4P", &dsk_set_size },
152 { UNIT_PLAT, (4 << UNIT_V_PLAT), NULL, "5P", &dsk_set_size },
153 { UNIT_PLAT, (5 << UNIT_V_PLAT), NULL, "6P", &dsk_set_size },
154 { UNIT_PLAT, (6 << UNIT_V_PLAT), NULL, "7P", &dsk_set_size },
155 { UNIT_PLAT, (7 << UNIT_V_PLAT), NULL, "8P", &dsk_set_size },
156 { UNIT_AUTO, UNIT_AUTO, "autosize", "AUTOSIZE", NULL },
157 { 0 }
158 };
159
160 DEVICE dsk_dev = {
161 "DSK", &dsk_unit, dsk_reg, dsk_mod,
162 1, 8, 21, 1, 8, 16,
163 NULL, NULL, &dsk_reset,
164 &dsk_boot, &dsk_attach, NULL,
165 &dsk_dib, DEV_DISABLE
166 };
167
168
169 /* IOT routine */
170
171 int32 dsk (int32 pulse, int32 code, int32 AC)
172 {
173 int32 t, rval;
174
175 rval = 0;
176 switch (code) { /* decode IR<5:7> */
177
178 case ioDIA: /* DIA */
179 rval = dsk_stat & DSKS_ALLERR; /* read status */
180 break;
181
182 case ioDOA: /* DOA */
183 dsk_da = AC & DSK_AMASK; /* save disk addr */
184 break;
185
186 case ioDIB: /* DIB */
187 rval = dsk_ma & AMASK; /* read mem addr */
188 break;
189
190 case ioDOB: /* DOB */
191 dsk_ma = AC & AMASK; /* save mem addr */
192 break;
193
194 case ioDIC: /* DIC - undocumented DG feature(!) */
195 rval = 256 ; /* return fixed sector size for DG for now */
196 break ;
197 } /* end switch code */
198
199 if (pulse) { /* any pulse? */
200 DEV_CLR_BUSY( INT_DSK ) ;
201 DEV_CLR_DONE( INT_DSK ) ;
202 DEV_UPDATE_INTR ;
203 dsk_stat = 0; /* clear status */
204 sim_cancel (&dsk_unit); /* stop I/O */
205 }
206
207 if ((pulse == iopP) && ((dsk_wlk >> GET_DISK (dsk_da)) & 1)) { /* wrt lock? */
208 DEV_SET_DONE( INT_DSK ) ;
209 DEV_UPDATE_INTR ;
210 dsk_stat = DSKS_ERR + DSKS_WLS; /* set status */
211 return rval;
212 }
213
214 if (pulse & 1) { /* read or write? */
215 if (((uint32) (dsk_da * DSK_NUMWD)) >= dsk_unit.capac) { /* inv sev? */
216 DEV_SET_DONE( INT_DSK ) ;
217 DEV_UPDATE_INTR ;
218 dsk_stat = DSKS_ERR + DSKS_NSD; /* set status */
219 return rval; /* done */
220 }
221 dsk_unit.FUNC = pulse; /* save command */
222 DEV_SET_BUSY( INT_DSK ) ;
223 DEV_UPDATE_INTR ;
224 t = sector_map[dsk_da & DSK_MMASK] - GET_SECTOR (dsk_time);
225 if (t < 0) t = t + DSK_NUMSC;
226 sim_activate (&dsk_unit, t * dsk_time); /* activate */
227 }
228 return rval;
229 }
230
231
232 /* Unit service */
233
234 t_stat dsk_svc (UNIT *uptr)
235 {
236 int32 i, da, pa;
237 int16 *fbuf = uptr->filebuf;
238
239 DEV_CLR_BUSY( INT_DSK ) ;
240 DEV_SET_DONE( INT_DSK ) ;
241 DEV_UPDATE_INTR ;
242
243 if ((uptr->flags & UNIT_BUF) == 0) { /* not buf? abort */
244 dsk_stat = DSKS_ERR + DSKS_NSD; /* set status */
245 return IORETURN (dsk_stopioe, SCPE_UNATT);
246 }
247
248 da = dsk_da * DSK_NUMWD; /* calc disk addr */
249 if (uptr->FUNC == iopS) { /* read? */
250 for (i = 0; i < DSK_NUMWD; i++) { /* copy sector */
251 pa = MapAddr (0, (dsk_ma + i) & AMASK); /* map address */
252 if (MEM_ADDR_OK (pa)) M[pa] = fbuf[da + i];
253 }
254 dsk_ma = (dsk_ma + DSK_NUMWD) & AMASK;
255 }
256 else if (uptr->FUNC == iopP) { /* write? */
257 for (i = 0; i < DSK_NUMWD; i++) { /* copy sector */
258 pa = MapAddr (0, (dsk_ma + i) & AMASK); /* map address */
259 fbuf[da + i] = M[pa];
260 }
261 if (((uint32) (da + i)) >= uptr->hwmark) /* past end? */
262 uptr->hwmark = da + i + 1; /* upd hwmark */
263 dsk_ma = (dsk_ma + DSK_NUMWD + 3) & AMASK;
264 }
265
266 dsk_stat = 0; /* set status */
267 return SCPE_OK;
268 }
269
270
271 /* Reset routine */
272
273 t_stat dsk_reset (DEVICE *dptr)
274 {
275 dsk_stat = dsk_da = dsk_ma = 0;
276 DEV_CLR_BUSY( INT_DSK ) ;
277 DEV_CLR_DONE( INT_DSK ) ;
278 DEV_UPDATE_INTR ;
279 sim_cancel (&dsk_unit);
280 return SCPE_OK;
281 }
282
283
284 /* Bootstrap routine */
285
286 #define BOOT_START 0375
287 #define BOOT_LEN (sizeof (boot_rom) / sizeof (int32))
288
289 static const int32 boot_rom[] = {
290 0062677 /* IORST ; reset the I/O system */
291 , 0060120 /* NIOS DSK ; start the disk */
292 , 0000377 /* JMP 377 ; wait for the world */
293 } ;
294
295
296 t_stat dsk_boot (int32 unitno, DEVICE *dptr)
297 {
298 int32 i;
299
300 for (i = 0; i < BOOT_LEN; i++) M[BOOT_START + i] = (uint16) boot_rom[i];
301 saved_PC = BOOT_START;
302 SR = 0100000 + DEV_DSK;
303 return SCPE_OK;
304 }
305
306
307 /* Attach routine */
308
309 t_stat dsk_attach (UNIT *uptr, char *cptr)
310 {
311 uint32 sz, p;
312 uint32 ds_bytes = DSK_DKSIZE * sizeof (int16);
313
314 if ((uptr->flags & UNIT_AUTO) && (sz = sim_fsize_name (cptr))) {
315 p = (sz + ds_bytes - 1) / ds_bytes;
316 if (p >= DSK_NUMDK) p = DSK_NUMDK - 1;
317 uptr->flags = (uptr->flags & ~UNIT_PLAT) | (p << UNIT_V_PLAT);
318 }
319 uptr->capac = UNIT_GETP (uptr->flags) * DSK_DKSIZE; /* set capacity */
320 return attach_unit (uptr, cptr);
321 }
322
323
324 /* Change disk size */
325
326 t_stat dsk_set_size (UNIT *uptr, int32 val, char *cptr, void *desc)
327 {
328 if (val < 0) return SCPE_IERR;
329 if (uptr->flags & UNIT_ATT) return SCPE_ALATT;
330 uptr->capac = UNIT_GETP (val) * DSK_DKSIZE;
331 uptr->flags = uptr->flags & ~UNIT_AUTO;
332 return SCPE_OK;
333 }