-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_out.c
339 lines (321 loc) · 8.1 KB
/
in_out.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
This file is part of nd - The Nova Debugger
Copyright (C) 2006-7 Emil Sarlija
e-mail: emil AT chookfest DOT net
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Read the file GPL for more information
Nova I/O instruction simulation routine
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "version.h"
#include "reg_flags.h"
int ctrl_func(int cont, int devnum);
int in_out(unsigned int instruction)
{
int generic_io(int acc, int op, int ctrl, int dev);
unsigned int accum, opcode, control, device, temp;
int count;
accum = (instruction >> 11) & 3;
opcode = (instruction >> 8) & 7;
control = (instruction >> 6) & 3;
device = instruction & 0x3f;
if (device == 077)
{
switch(opcode) /* all the spiffy CPU specific I/O instructions */
{
case 0: /* Enable or disable interrupts */
switch(control)
{
case 1: /* INTEN */
dev_busy[077] = 1; /* ION = 1 */
break;
case 2: /* INTDS */
dev_busy[077] = 0; /* ION = 0 */
break;
default: /* Undocumented - perform generic I/O operation */
generic_io(accum, opcode, control, device);
break;
}
break;
case 1: /* READS */
AC[accum] = SR;
ctrl_func(control, device);
break;
case 2: /* Undocumented - perform generic I/O operation */
generic_io(accum, opcode, control, device);
break;
case 3: /* INTA */
/* do nothing until I implement interrupt handling code */
break;
case 4: /* MSKO */
/* Priority mask code would go here */
break;
case 5: /* IORST */
for(count = 0; count < 64; count++) /* clear all device Busy and Done flags */
{
dev_busy[count] = 0;
dev_done[count] = 0;
}
/* Priority mask code would go here */
ctrl_func(control, device);
break;
case 6: /* HALT */
ctrl_func(control, device);
run = 0; /* stop emulation */
printf("\nHALT instruction encountered.\n");
halt = 1;
break;
case 7: /* SKP */
switch(control)
{
case 0: /* BN */
if (dev_busy[077]) /* is ION = 1 ? */
PC++;
break;
case 1: /* BZ */
if (!dev_busy[077]) /* is ION = 0 ? */
PC++;
break;
case 2: /* DN */
break; /* Never skip, PSU is infallible, Power Fail Flag is always 0 */
case 3: /* DZ */
PC++; /* Always skip, PSU is infallible, Power Fail Flag is always 0 */
break;
}
break;
}
}
else if (device == 1)
{
switch(opcode) /* all the spiffy MUL/DIV and stack specific I/O instructions */
{
case 0: /* Frame Pointer instructions */
switch(control)
{
case 0: /* MTFP */
FP = AC[accum] & 077777;
break;
case 2: /* MFFP */
AC[accum] = FP & 077777;
break;
default: /* Undocumented - perform generic I/O operation */
generic_io(accum, opcode, control, device);
break;
}
break;
case 2: /* Stack Pointer instructions */
switch(control)
{
case 0: /* MTSP */
SP = AC[accum] & 077777;
break;
case 2: /* MFSP */
AC[accum] = SP & 077777;
break;
default: /* Undocumented - perform generic I/O operation */
generic_io(accum, opcode, control, device);
break;
}
break;
case 3: /* Push and Pop instructions */
switch(control)
{
case 0: /* PSHA */
++SP;
SP &= 077777;
RAM[SP] = AC[accum];
break;
case 2: /* POPA */
AC[accum] = RAM[SP];
--SP;
SP &= 077777;
break;
default: /* Undocumented - perform generic I/O operation */
generic_io(accum, opcode, control, device);
break;
}
break;
case 5: /* Save and Return instructions */
switch(control)
{
case 0: /* SAV */
for(count = 0; count < 3; ++count) /* push AC0-2 on the stack */
{
++SP;
SP &= 077777;
RAM[SP] = AC[count];
}
++SP;
SP &= 077777;
RAM[SP] = FP & 077777; /* push Frame Pointer on the stack */
++SP;
SP &= 077777;
RAM[SP] = AC[3] & 077777; /* push bits 1-15 of AC3 on the stack */
if (C)
RAM[SP] |= 0100000; /* set MSB of the pushed word if Carry is 1 */
FP = SP; /* place Stack Pointer in the Frame Pointer */
AC[3] = SP; /* place Stack Pointer in AC3 */
break;
case 2: /* RET */
SP = FP; /* place Frame Pointer in the Stack Pointer */
PC = (RAM[SP] & 077777) -1; /* pop the Program Counter */
if ((RAM[SP] & 0100000) == 0100000)
C = 1; /* set Carry id MSB of the popped word is 1 */
--SP;
SP &= 077777;
FP = RAM[SP] & 077777; /* pop the frame pointer off the stack, clear MSB */
AC[3] = RAM[SP]; /* the full word goes into AC3 */
--SP;
SP &= 077777;
for(count = 2; count >= 0; --count) /* pop AC0-3 off the stack */
{
AC[count] = RAM[SP];
--SP;
SP &= 077777;
}
break;
default: /* Undocumented - perform generic I/O operation */
generic_io(accum, opcode, control, device);
break;
}
break;
case 6: /* Divide and Multiply instructions */
switch(control)
{
case 1: /* DIV */
if (AC[0] >= AC[2])
C = 1;
else
{
temp = (AC[0] << 16) | AC[1];
AC[1] = temp / AC[2]; /* quotient */
AC[0] = temp % AC[2]; /* remainder */
C = 0;
}
break;
case 3: /* MUL */
temp = AC[1] * AC[2];
temp += AC[0];
AC[1] = temp & 0xffff;
AC[0] = (temp >> 16) & 0xffff;
break;
default: /* Undocumented - perform generic I/O operation */
generic_io(accum, opcode, control, device);
break;
}
default: /* Undocumented - perform generic I/O operation */
generic_io(accum, opcode, control, device);
break;
}
}
else
{
generic_io(accum, opcode, control, device);
switch(device)
{
case 010: /* TTI device */
/* insert code here */
break;
case 011: /* TTO device */
if (opcode == 2) /* check to see if it's a DOA */
{
dev_A[device] = dev_A[device] & 0177;
putchar(dev_A[device]);
fflush(stdout); // buffering doesn't really help us, I think
dev_busy[device] = 0;
dev_done[device] = 1;
}
break;
default:
break;
}
}
return 0;
}
int generic_io(int acc, int op, int ctrl, int dev)
{
switch(op)
{
case 0: /* NIO */
ctrl_func(ctrl, dev);
break;
case 1: /* DIA */
AC[acc] = dev_A[dev];
ctrl_func(ctrl, dev);
break;
case 2: /* DOA */
dev_A[dev] = AC[acc];
ctrl_func(ctrl, dev);
break;
case 3: /* DIB */
AC[acc] = dev_B[dev];
ctrl_func(ctrl, dev);
break;
case 4: /* DOB */
dev_B[dev] = AC[acc];
ctrl_func(ctrl, dev);
break;
case 5: /* DIC */
AC[acc] = dev_C[dev];
ctrl_func(ctrl, dev);
break;
case 6: /* DOC */
dev_C[dev] = AC[acc];
ctrl_func(ctrl, dev);
break;
case 7: /* SKP */
switch(ctrl)
{
case 0: /* BN */
if (dev_busy[dev])
PC++;
break;
case 1: /* BZ */
if (!dev_busy[dev])
PC++;
break;
case 2: /*DN */
if (dev_done[dev])
PC++;
break;
case 3: /* DZ */
if (!dev_done[dev])
PC++;
break;
}
break;
}
return 0;
}
int ctrl_func(int cont, int devnum)
{
switch(cont)
{
case 0:
break;
case 1: /* Start */
dev_busy[devnum]=1;
dev_done[devnum]=0;
break;
case 2:/*Clear */
dev_busy[devnum]=0;
dev_done[devnum]=0;
break;
case 3: /* Pulse */
dev_pulse[devnum]=1;
break;
}
return 0;
}