-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathibm1130_cpu.c
1872 lines (1541 loc) · 58 KB
/
ibm1130_cpu.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ibm1130_cpu.c: IBM 1130 CPU simulator
Based on the SIMH package written by Robert M Supnik
* (C) Copyright 2002, Brian Knittel.
* You may freely use this program, but: it offered strictly on an AS-IS, AT YOUR OWN
* RISK basis, there is no warranty of fitness for any purpose, and the rest of the
* usual yada-yada. Please keep this notice and the copyright in any distributions
* or modifications.
*
* This is not a supported product, but I welcome bug reports and fixes.
* Mail to [email protected]
25-Jun-01 BLK Written
10-May-02 BLK Fixed bug in MDX instruction
27-Mar-02 BLK Made BOSC work even in short form
16-Aug-02 BLK Fixed bug in multiply instruction; didn't work with negative values
18-Mar-03 BLK Fixed bug in divide instruction; didn't work with negative values
23-Jul-03 BLK Prevented tti polling in CGI mode
The register state for the IBM 1130 CPU is:
IAR instruction address register
ACC accumulator
EXT accumulator extension
Oflow overflow bit
Carry carry bit
CES console entry switches
ipl current interrupt level, -1 = non interrupt
iplpending bitmap of pending interrupts
wait_state current CPU state: running or waiting
DSW console run/stop switch device status word
RUNMODE processor step/run mode (may also imply IntRun)
BREAK breakpoint address
WRU simulator-break character
IntRun Int Run flag (causes level 5 interrupt after every instruction)
ILSW0..5 interrupt level status words
The SAR (storage address register) and SBR (storage buffer register) are updated
but not saved in the CPU state; they matter only to the GUI.
Interrupt handling: interrupts occur when any device on any level has an
active interrupt. XIO commands can clear specific IRQ bits. When this
happens, we have to evaluate all devices on the same IRQ level for remaining
indicators. The flag int_req is set with a bit corresponding to the IRQ level
when any interrupt indicator is activated.
The 1130 console has a switch that controls several run modes: SS (single processor
step), SCLK (single clock step), SINST (single instruction step), INT_RUN
(IRQ 5 after each non interrupt-handler instruction) and RUN (normal operation).
This simulator does not implement SS and SCLK. The simulator GUI console handles
SINST, so we only have to worry about INT_RUN. The console command SET CPU IntRun sets
the tmode (trace mode) flag; this causes a level 5 interrupt after each
instruction.
The IBM 1130 instruction formats are
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| opcode | F| T | | general format
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| opcode | 0| T | DISPLACEMENT | short instruction
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| opcode | 1| T | I| MODIFIER | long instruction
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ADDRESS |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
opcode in MSBits
F = format. 0 = short (1 word), 1 = long (2 word) instruction
T = Tag 00 = no index register (e.g. IAR relative)
01 = use index register 1 (e.g. core address 1 = M[1])
02 = use index register 2 (e.g. core address 2 = M[2])
03 = use index register 3 (e.g. core address 3 = M[3])
DISPLACEMENT = two's complement (must be sign-extended)
I = Indirect
Note that IAR = instruction address+1 when instruction is being decoded.
In normal addressing mode, effective address (EA) is computed as follows:
F = 0 T = 0 EA = IAR + DISPLACEMENT
0 1 IAR + DISPLACEMENT + M[1]
0 2 IAR + DISPLACEMENT + M[2]
0 3 IAR + DISPLACEMENT + M[3]
F = 1 T = 0 I = 0 EA = ADDRESS
1 1 0 ADDRESS + M[1]
1 2 0 ADDRESS + M[2]
1 3 0 ADDRESS + M[3]
1 0 1 M[ADDRESS]
1 1 1 M[ADDRESS + M[1]]
1 2 1 M[ADDRESS + M[2]]
1 3 1 M[ADDRESS + M[3]]
Loads or stores are then made to/from MEM[EA]. Some instructions have special
weird addressing modes. Simulator code precomputes standard addressing for
all instructions though it's not always used.
General notes:
Adding I/O devices requires modifications to three modules:
ibm1130_defs.h add interrupt request definitions
ibm1130_cpu.c add XIO command linkages
ibm1130_sys.c add to sim_devices
*/
/* ------------------------------------------------------------------------
* Definitions
* ------------------------------------------------------------------------ */
#include <stdarg.h>
#include "ibm1130_defs.h"
#define save_ibkpt (cpu_unit.u3) /* will be SAVEd */
#define UPDATE_BY_TIMER
#define ENABLE_BACKTRACE
#define CGI_SUPPORT
static void cgi_start(void);
static void cgi_stop(t_stat reason);
// hook pointers from scp.c
void (*sim_vm_init) (void) = &sim_init;
extern char* (*sim_vm_read) (char *ptr, int32 size, FILE *stream);
extern void (*sim_vm_post) (t_bool from_scp);
extern CTAB *sim_vm_cmd;
// space to store extra simulator-specific commands
#define MAX_EXTRA_COMMANDS 10
CTAB x_cmds[MAX_EXTRA_COMMANDS];
#ifdef WIN32
# define CRLF "\r\n"
#else
# define CRLF "\n"
#endif
/* ------------------------------------------------------------------------
* initializers for globals
* ------------------------------------------------------------------------ */
#define SIGN_BIT(v) ((v) & 0x8000)
#define DWSIGN_BIT(v) ((v) & 0x80000000)
uint16 M[MAXMEMSIZE]; /* core memory, up to 32Kwords (note: don't even think about trying 64K) */
uint16 ILSW[6] = {0,0,0,0,0,0}; /* interrupt level status words */
int32 IAR; /* instruction address register */
int32 prev_IAR; /* instruction address register at start of current instruction */
int32 SAR, SBR; /* storage address/buffer registers */
int32 OP, TAG, CCC; /* instruction decoded pieces */
int32 CES; /* console entry switches */
int32 ACC, EXT; /* accumulator and extension */
int32 RUNMODE; /* processor run/step mode */
int32 ipl = -1; /* current interrupt level (-1 = not handling irq) */
int32 iplpending = 0; /* interrupted IPL's */
int32 tbit = 0; /* trace flag (causes level 5 IRQ after each instr) */
int32 V = 0, C = 0; /* condition codes */
int32 wait_state = 0; /* wait state (waiting for an IRQ) */
int32 wait_lamp = TRUE; /* alternate indicator to light the wait lamp on the GUI */
int32 int_req = 0; /* sum of interrupt request levels active */
int32 int_lamps = 0; /* accumulated version of int_req - gives lamp persistence */
int32 int_mask; /* current active interrupt mask (ipl sensitive) */
int32 mem_mask;
int32 cpu_dsw = 0; /* CPU device status word */
int32 ibkpt_addr = -1; /* breakpoint addr */
int32 sim_gui = TRUE; /* enable gui */
t_bool running = FALSE; /* TRUE if CPU is running */
t_bool power = TRUE; /* TRUE if CPU power is on */
t_bool cgi = FALSE; /* TRUE if we are running as a CGI program */
t_stat reason; /* CPU execution loop control */
static int32 int_masks[6] = {
0x00, 0x20, 0x30, 0x38, 0x3C, 0x3E /* IPL 0 is highest prio (sees no other interrupts) */
};
/* ------------------------------------------------------------------------
* Function declarations
* ------------------------------------------------------------------------ */
t_stat cpu_ex (t_value *vptr, t_addr addr, UNIT *uptr, int32 sw);
t_stat cpu_dep (t_value val, t_addr addr, UNIT *uptr, int32 sw);
t_stat cpu_reset (DEVICE *dptr);
t_stat cpu_svc (UNIT *uptr);
t_stat cpu_set_size (UNIT *uptr, int32 value, char *cptr, void *desc);
void calc_ints (void);
extern t_stat ts_wr (int32 data, int32 addr, int32 access);
extern t_stat detach_cmd (int flags, char *cptr);
extern UNIT cr_unit;
extern int32 sim_switches;
#ifdef ENABLE_BACKTRACE
static void archive_backtrace(char *inst);
static void reset_backtrace (void);
static void show_backtrace (int nshow);
static t_stat backtrace_cmd (int flag, char *cptr);
#else
#define archive_backtrace(inst)
#define reset_backtrace()
#define show_backtrace(ntrace)
#endif
static void init_console_window (void);
static void destroy_console_window (void);
static t_stat view_cmd (int flag, char *cptr);
static t_stat cgi_cmd (int flag, char *cptr);
static t_stat cpu_attach (UNIT *uptr, char *cptr);
static t_bool bsctest (int32 DSPLC, t_bool reset_V);
static void exit_irq (void);
static void trace_instruction (void);
/* ------------------------------------------------------------------------
* CPU data structures:
* cpu_dev CPU device descriptor
* cpu_unit CPU unit descriptor
* cpu_reg CPU register list
* cpu_mod CPU modifier list
* ------------------------------------------------------------------------ */
UNIT cpu_unit = { UDATA (&cpu_svc, UNIT_FIX | UNIT_BINK | UNIT_ATTABLE | UNIT_SEQ, INIMEMSIZE) };
REG cpu_reg[] = {
{ HRDATA (IAR, IAR, 32) },
{ HRDATA (ACC, ACC, 32) },
{ HRDATA (EXT, EXT, 32) },
{ FLDATA (Oflow, V, 1) },
{ FLDATA (Carry, C, 1) },
{ HRDATA (CES, CES, 32) },
{ HRDATA (ipl, ipl, 32), REG_RO },
{ HRDATA (iplpending, iplpending, 32), REG_RO },
{ HRDATA (wait_state, wait_state, 32)},
{ HRDATA (DSW, cpu_dsw, 32), REG_RO },
{ HRDATA (RUNMODE, RUNMODE, 32) },
{ HRDATA (BREAK, ibkpt_addr, 32) },
{ ORDATA (WRU, sim_int_char, 8) },
{ FLDATA (IntRun, tbit, 1) },
{ HRDATA (ILSW0, ILSW[0], 32), REG_RO },
{ HRDATA (ILSW1, ILSW[1], 32), REG_RO },
{ HRDATA (ILSW2, ILSW[2], 32), REG_RO },
{ HRDATA (ILSW3, ILSW[3], 32), REG_RO },
{ HRDATA (ILSW4, ILSW[4], 32), REG_RO },
{ HRDATA (ILSW5, ILSW[5], 32), REG_RO },
{ NULL}
};
MTAB cpu_mod[] = {
{ UNIT_MSIZE, 4096, NULL, "4KW", &cpu_set_size},
{ UNIT_MSIZE, 8192, NULL, "8KW", &cpu_set_size},
{ UNIT_MSIZE, 16384, NULL, "16KW", &cpu_set_size},
{ UNIT_MSIZE, 32768, NULL, "32KW", &cpu_set_size},
{ 0 } };
DEVICE cpu_dev = {
"CPU", &cpu_unit, cpu_reg, cpu_mod,
1, 16, 16, 1, 16, 16,
&cpu_ex, &cpu_dep, &cpu_reset,
NULL, cpu_attach, NULL}; // attaching to CPU creates cpu log file
/* ------------------------------------------------------------------------
Memory read/write -- save SAR and SBR on the way in and out
* ------------------------------------------------------------------------ */
int32 ReadW (int32 a)
{
SAR = a;
SBR = (int32) M[(a) & mem_mask];
return SBR;
}
void WriteW (int32 a, int32 d)
{
SAR = a;
SBR = d;
M[a & mem_mask] = (int16) d;
}
/* ------------------------------------------------------------------------
* upcase - force a string to uppercase (ASCII)
* ------------------------------------------------------------------------ */
char *upcase (char *str)
{
char *s;
for (s = str; *s; s++) {
if (*s >= 'a' && *s <= 'z')
*s -= 32;
}
return str;
}
/* ------------------------------------------------------------------------
* calc_ints - set appropriate bits in int_req if any interrupts are pending on given levels
*
* int_req:
* bit 5 4 3 2 1 0
* \ \ \ \ \ \
* \ \ \ \ \ interrupt level 5 pending (lowest priority)
* \ . . .
* interrupt level 0 pending (highest priority)
*
* int_mask is set according to current interrupt level (ipl)
*
* 0 0 0 0 0 0 ipl = 0 (currently servicing highest priority interrupt)
* 1 0 0 0 0 0 1
* 1 1 0 0 0 0 2
* 1 1 1 0 0 0 3
* 1 1 1 1 0 0 4
* 1 1 1 1 1 0 5 (currently servicing lowest priority interrupt)
* 1 1 1 1 1 1 -1 (not servicing an interrupt)
* ------------------------------------------------------------------------ */
void calc_ints (void)
{
register int i;
register int32 newbits = 0;
GUI_BEGIN_CRITICAL_SECTION // using critical section here so we don't mislead the GUI thread
for (i = 6; --i >= 0; ) {
newbits >>= 1;
if (ILSW[i])
newbits |= 0x20;
}
int_req = newbits;
int_lamps |= int_req;
int_mask = (ipl < 0) ? 0xFFFF : int_masks[ipl]; /* be sure this is set correctly */
GUI_END_CRITICAL_SECTION
}
/* ------------------------------------------------------------------------
* instruction processor
* ------------------------------------------------------------------------ */
#define INCREMENT_IAR IAR = (IAR + 1) & mem_mask
#define DECREMENT_IAR IAR = (IAR - 1) & mem_mask
void bail (char *msg)
{
printf("%s\n", msg);
exit(1);
}
static void weirdop (char *msg, int offset)
{
printf("Weird opcode: %s at %04x\n", msg, IAR+offset);
}
static char *xio_devs[] = {
"0?", "console", "1142card", "1134papertape",
"dsk0", "1627plot", "1132print", "switches",
"1231omr", "2501card", "comm", "b?",
"sys7", "d?", "e?", "f?",
"10?", "dsk1", "dsk2", "dsk3",
"dsk4", "dsk5", "dsk6", "dsk7+",
"18?", "2250disp", "1a?", "1b",
"1c?", "1d?", "1e?", "1f?"
};
static char *xio_funcs[] = {
"0?", "write", "read", "sense_irq",
"control", "initw", "initr", "sense"
};
t_stat sim_instr (void)
{
extern int32 sim_interval;
extern UNIT *sim_clock_queue;
int32 i, eaddr, INDIR, IR, F, DSPLC, word2, oldval, newval, src, src2, dst, abit, xbit;
int32 iocc_addr, iocc_op, iocc_dev, iocc_func, iocc_mod;
char msg[50];
int cwincount = 0, status;
static long ninstr = 0;
static char *intlabel[] = {"INT0","INT1","INT2","INT3","INT4","INT5"};
#ifdef CGI_SUPPORT
if (cgi)
cgi_start();
#endif
if (running) /* this is definitely not reentrant */
return -1;
if (! power) /* this matters only to the GUI */
return STOP_POWER_OFF;
running = TRUE;
mem_mask = MEMSIZE - 1; /* set other useful variables */
calc_ints();
/* Main instruction fetch/decode loop */
reason = 0;
wait_lamp = 0; /* release lock on wait lamp */
#ifdef GUI_SUPPORT
update_gui(TRUE);
gui_run(TRUE);
#endif
while (reason == 0) {
IAR &= mem_mask;
#ifdef GUI_SUPPORT
#ifndef UPDATE_BY_TIMER
#if (UPDATE_INTERVAL > 0)
if (--cwincount <= 0) {
update_gui(FALSE); /* update console lamps only every so many instructions */
cwincount = UPDATE_INTERVAL + (rand() % MIN(UPDATE_INTERVAL, 32));
}
#else
update_gui(FALSE);
#endif // ifdef UPDATE_INTERVAL
#endif // ifndef UPDATE_BY_TIMER
#endif // ifdef GUI_SUPPORT
if (sim_interval <= 0) { /* any events timed out? */
if (sim_clock_queue != NULL) {
if ((status = sim_process_event()) != 0)
reason = status;
calc_ints();
continue;
}
}
if (int_req & int_mask) { /* any pending interrupts? */
for (i = 0; i <= 5; i++) /* find highest pending interrupt */
if ((int_req & int_mask) & (0x20 >> i))
break;
if (i >= 6) { /* nothing to do? */
calc_ints(); /* weird. recalculate */
continue; /* back to fetch */
}
GUI_BEGIN_CRITICAL_SECTION
if (ipl >= 0) /* save previous IPL in bit stack */
iplpending |= (0x20 >> ipl);
ipl = i; /* set new interrupt level */
int_mask = int_masks[i]; /* set appropriate mask */
GUI_END_CRITICAL_SECTION
wait_state = 0; /* exit wait state */
eaddr = ReadW(8+i); /* get IRQ vector */
archive_backtrace(intlabel[i]);
WriteW(eaddr, IAR); /* save IAR */
IAR = (eaddr+1) & mem_mask; /* go to next address */
continue; /* now continue processing */
} /* end if int_req */
if (wait_state) { /* waiting? */
sim_interval = 0; /* run the clock out */
if (sim_qcount() <= (cgi ? 0 : 1)) { /* one routine queued? we're waiting for keyboard only */
if (keyboard_is_busy()) { /* we are actually waiting for a keystroke */
if ((status = sim_process_event()) != 0) /* get it with wait_state still set */
reason = status;
}
else { /* CPU is not expecting a keystroke (keyboard interrupt) */
if (wait_state == WAIT_OP)
reason = STOP_WAIT; /* end the simulation */
else
reason = STOP_INVALID_INSTR;
}
}
if (gdu_active()) /* but don't stop simulator if 2250 GDU is running */
reason = 0;
continue;
}
if (IAR == ibkpt_addr) { /* simulator breakpoint? */
save_ibkpt = ibkpt_addr; /* save bkpt */
ibkpt_addr = ibkpt_addr | ILL_ADR_FLAG; /* disable */
sim_activate(&cpu_unit, 1); /* sched re-enable after next instruction */
reason = STOP_IBKPT; /* stop simulation */
cwincount = 0;
continue;
}
ninstr++;
if (cpu_unit.flags & UNIT_ATT)
trace_instruction(); /* log CPU details if logging is enabled */
prev_IAR = IAR; /* save IAR before incrementing it */
IR = ReadW(IAR); /* fetch 1st word of instruction */
INCREMENT_IAR;
sim_interval = sim_interval - 1; /* this constitutes one tick of the simulation clock */
OP = (IR >> 11) & 0x1F; /* opcode */
F = IR & 0x0400; /* format bit: 1 = long instr */
TAG = IR & 0x0300; /* tag bits: index reg x */
if (TAG)
TAG >>= 8;
// here I compute the usual effective address on the assumption that the instruction will need it. Some don't.
if (F) { /* long instruction, ASSUME it's valid (have to decrement IAR if not) */
INDIR = IR & 0x0080; /* indirect bit */
DSPLC = IR & 0x007F; /* displacement or modifier */
if (DSPLC & 0x0040)
DSPLC |= ~ 0x7F; /* sign extend */
word2 = ReadW(IAR); /* get reference address */
INCREMENT_IAR; /* bump the instruction address register */
eaddr = word2; /* assume standard addressing & compute effective address */
if (TAG) /* if indexed */
eaddr += ReadW(TAG); /* add index register value (stored in core) */
if (INDIR) /* if indirect addressing */
eaddr = ReadW(eaddr); /* pick up referenced address */
}
else { /* short instruction, use displacement */
INDIR = 0; /* never indirect */
DSPLC = IR & 0x00FF; /* get displacement */
if (DSPLC & 0x0080)
DSPLC |= ~ 0xFF;
if (TAG) /* if indexed */
eaddr = ReadW(TAG) + DSPLC; /* add index register value (stored in core) */
else
eaddr = IAR + DSPLC; /* otherwise relative to IAR after fetch */
}
switch (OP) { /* decode instruction */
case 0x01: /* --- XIO --- */
iocc_addr = ReadW(eaddr); /* get IOCC packet */
iocc_op = ReadW(eaddr|1); /* note 'or' not plus, address must be even for proper operation */
iocc_dev = (iocc_op >> 11) & 0x001F;
iocc_func = (iocc_op >> 8) & 0x0007;
iocc_mod = iocc_op & 0x00FF;
if (cpu_unit.flags & UNIT_ATT)
trace_io("* XIO %s %s mod %02x addr %04x", xio_funcs[iocc_func], xio_devs[iocc_dev], iocc_mod, iocc_addr);
ACC = 0; /* ACC is destroyed, and default XIO_SENSE_DEV result is 0 */
switch (iocc_func) {
case XIO_UNUSED:
sprintf(msg, "Unknown op %x on device %02x", iocc_func, iocc_dev);
xio_error(msg);
break;
case XIO_SENSE_IRQ: /* examine current Interrupt Level Status Word */
ACC = (ipl >= 0) ? ILSW[ipl] : 0;
break;
default: /* perform device-specific operation */
switch (iocc_dev) {
case 0x01: /* console keyboard and printer */
xio_1131_console(iocc_addr, iocc_func, iocc_mod);
break;
case 0x02: /* 1142 card reader/punch */
xio_1142_card(iocc_addr, iocc_func, iocc_mod);
break;
case 0x03: /* 1134 paper tape reader/punch */
xio_1134_papertape(iocc_addr, iocc_func, iocc_mod);
break;
case 0x04: /* CPU disk storage */
xio_disk(iocc_addr, iocc_func, iocc_mod, 0);
break;
case 0x05: /* 1627 plotter */
xio_1627_plotter(iocc_addr, iocc_func, iocc_mod);
break;
case 0x06: /* 1132 Printer */
xio_1132_printer(iocc_addr, iocc_func, iocc_mod);
break;
case 0x07: /* console switches, stop key, run mode */
xio_1131_switches(iocc_addr, iocc_func, iocc_mod);
break;
case 0x08: /* 1231 optical mark reader */
xio_1231_optical(iocc_addr, iocc_func, iocc_mod);
break;
case 0x09: /* 2501 card reader */
xio_2501_card(iocc_addr, iocc_func, iocc_mod);
break;
case 0x0a: /* synchronous comm adapter */
xio_1131_synch(iocc_addr, iocc_func, iocc_mod);
break;
case 0x0c: /* IBM System/7 interprocessor link */
xio_system7(iocc_addr, iocc_func, iocc_mod);
break;
case 0x11: /* 2310 Disk Storage, Drive 1, or 2311 Disk Storage Drive. Drive 1, Disk 1 */
xio_disk(iocc_addr, iocc_func, iocc_mod, 1);
break;
case 0x12: /* 2310 Disk Storage, Drive 2, or 2311 Disk Storage Drive. Drive 1, Disk 2 */
xio_disk(iocc_addr, iocc_func, iocc_mod, 2);
break;
case 0x13: /* 2310 Disk Storage, Drive 3, or 2311 Disk Storage Drive. Drive 1, Disk 3 */
xio_disk(iocc_addr, iocc_func, iocc_mod, 3);
break;
case 0x14: /* 2310 Disk Storage, Drive 4, or 2311 Disk Storage Drive. Drive 1, Disk 4 */
xio_disk(iocc_addr, iocc_func, iocc_mod, 4);
break;
case 0x15: /* 1403 Printer */
xio_1403_printer(iocc_addr, iocc_func, iocc_mod);
break;
case 0x16: /* 2311 Disk Storage Drive. Drive 1, Disk 5 */
xio_disk(iocc_addr, iocc_func, iocc_mod, -1);
break;
case 0x17: /* 2311 Disk Storage Drive, Drive 2, Disk 1 through 5 */
xio_disk(iocc_addr, iocc_func, iocc_mod, -1);
break;
case 0x19: /* 2250 Display Unit */
xio_2250_display(iocc_addr, iocc_func, iocc_mod);
break;
default:
sprintf(msg, "unknown device %02x", iocc_dev);
xio_error(msg);
break;
}
}
calc_ints(); /* after every XIO, reset int_mask just in case */
break;
case 0x02: /* --- SLA,SLT,SLC,SLCA,NOP - Shift Left family --- */
if (F) {
weirdop("Long Left Shift", -2);
DECREMENT_IAR;
}
CCC = ((TAG == 0) ? DSPLC : ReadW(TAG)) & 0x003F;
if (CCC == 0)
break; /* shift of zero is a NOP */
switch (IR & 0x00C0) {
case 0x0040: /* SLCA */
if (TAG) {
while (CCC > 0 && (ACC & 0x8000) == 0) {
ACC <<= 1;
CCC--;
}
C = (CCC != 0);
WriteW(TAG, ReadW(TAG) & 0xFF00 | CCC); /* put low 6 bits back into index register and zero bits 8 and 9 */
break;
}
/* if TAG == 0, fall through and treat like normal shift SLA */
case 0x0000: /* SLA */
while (CCC > 0) {
C = (ACC & 0x8000);
ACC = (ACC << 1) & 0xFFFF;
CCC--;
}
break;
case 0x00C0: /* SLC */
if (TAG) {
while (CCC > 0 && (ACC & 0x8000) == 0) {
abit = (EXT & 0x8000) >> 15;
ACC = ((ACC << 1) & 0xFFFF) | abit;
EXT = (EXT << 1);
CCC--;
}
C = (CCC != 0);
WriteW(TAG, ReadW(TAG) & 0xFF00 | CCC); /* put 6 bits back into low byte of index register */
break;
}
/* if TAG == 0, fall through and treat like normal shift SLT */
case 0x0080: /* SLT */
while (CCC > 0) {
C = (ACC & 0x8000);
abit = (EXT & 0x8000) >> 15;
ACC = ((ACC << 1) & 0xFFFF) | abit;
EXT = (EXT << 1) & 0xFFFF;
CCC--;
}
break;
default:
bail("SLA switch, can't happen");
break;
}
break;
case 0x03: /* --- SRA, SRT, RTE - Shift Right family --- */
if (F) {
weirdop("Long Right Shift", -2);
DECREMENT_IAR;
}
CCC = ((TAG == 0) ? DSPLC : ReadW(TAG)) & 0x3F;
if (CCC == 0)
break; /* NOP */
switch (IR & 0x00C0) {
case 0x0000: /* SRA */
ACC = (CCC < 16) ? ((ACC & 0xFFFF) >> CCC) : 0;
CCC = 0;
break;
case 0x0040: /* invalid */
wait_state = WAIT_INVALID_OP;
break;
case 0x0080: /* SRT */
while (CCC > 0) {
xbit = (ACC & 0x0001) << 15;
abit = (ACC & 0x8000);
ACC = (ACC >> 1) & 0x7FFF | abit;
EXT = (EXT >> 1) & 0x7FFF | xbit;
CCC--;
}
break;
case 0x00C0: /* RTE */
while (CCC > 0) {
abit = (EXT & 0x0001) << 15;
xbit = (ACC & 0x0001) << 15;
ACC = (ACC >> 1) & 0x7FFF | abit;
EXT = (EXT >> 1) & 0x7FFF | xbit;
CCC--;
}
break;
default:
bail("SRA switch, can't happen");
break;
}
break;
case 0x04: /* --- LDS - Load Status --- */
if (F) { /* never fetches second word */
weirdop("Long LDS", -2);
DECREMENT_IAR;
}
V = (DSPLC & 1);
C = (DSPLC & 2) >> 1;
break;
case 0x05: /* --- STS - Store Status --- */
newval = ReadW(eaddr) & 0xFF00;
if (C)
newval |= 2;
if (V)
newval |= 1;
WriteW(eaddr, newval);
C = V = 0; /* clear flags after storing */
break;
case 0x06: /* --- WAIT --- */
wait_state = WAIT_OP;
/* note: not valid in long mode, but what happens if we try? */
if (F) {
weirdop("Long WAIT", -2);
DECREMENT_IAR; /* assume it wouldn't have fetched 2nd word */
}
break;
case 0x08: /* --- BSI - Branch and store IAR --- */
if (F) {
if (bsctest(IR, F)) /* do standard BSC long format testing */
break; /* if any condition is true, do nothing */
}
WriteW(eaddr, IAR); /* do subroutine call */
archive_backtrace("BSI"); /* save info in back-trace buffer */
IAR = (eaddr + 1) & mem_mask;
break;
case 0x09: /* --- BSC - Branch and skip on Condition --- */
if (F) {
if (bsctest(IR, F)) /* long format; any indicator cancels branch */
break;
archive_backtrace((DSPLC & 0x40) ? "BOSC" : "BSC"); /* save info in back-trace buffer */
IAR = eaddr; /* no indicator means branch taken */
}
else { /* short format: skip if any indicator hits */
if (bsctest(IR, F)) {
archive_backtrace((DSPLC & 0x40) ? "BOSC" : "BSC"); /* save info in back-trace buffer */
INCREMENT_IAR;
}
}
// 27Mar02: moved this test out of the (F) condition; BOSC works even in the
// short form. The displacement field in this instruction is always the set of
// condition bits, and the interrupt clear bit doesn't collide.
if (DSPLC & 0x40) { /* BOSC = exit from interrupt handler */
exit_irq();
cwincount = 0;
}
break;
case 0x0c: /* --- LDX - Load Index --- */
if (F)
eaddr = (INDIR) ? ReadW(word2) : word2;
else
eaddr = DSPLC;
if (TAG)
WriteW(TAG, eaddr);
else {
archive_backtrace("LDX"); /* save info in back-trace buffer */
IAR = eaddr; /* what happens in short form? can onlyjump to low addresses? */
}
break;
case 0x0d: /* --- STX - Store Index --- */
if (F) { /* compute EA without any indexing */
eaddr = (INDIR) ? ReadW(word2) : word2;
}
else {
eaddr = IAR + DSPLC;
}
WriteW(eaddr, TAG ? ReadW(TAG) : IAR);
break;
case 0x0e: /* --- MDX - Modify Index and Skip --- */
if (F) { /* long mode: adjust memory location */
if (TAG) {
oldval = ReadW(TAG); /* add word2 to index */
newval = oldval + (INDIR ? ReadW(word2) : word2);
WriteW(TAG, newval);
}
else {
oldval = ReadW(word2);
DSPLC = IR & 0x00FF; /* use extended displacement (includes INDIR bit) */
if (DSPLC & 0x0080)
DSPLC |= ~ 0xFF;
newval = oldval + DSPLC; /* add modifier to @word2 */
WriteW(word2, newval);
}
}
else { /* short mode: adust IAR or index */
if (TAG) {
oldval = ReadW(TAG);/* add displacement to index */
newval = oldval + DSPLC;
WriteW(TAG, newval);
}
else {
oldval = IAR; /* add displacement to IAR */
newval = IAR + DSPLC;
archive_backtrace("MDX");
IAR = newval & mem_mask;
}
}
if ((F || TAG) && (((newval & 0xFFFF) == 0) || ((oldval & 0x8000) != (newval & 0x8000)))) {
archive_backtrace("SKP");
INCREMENT_IAR; /* skip if index sign change or zero */
}
break;
case 0x10: /* --- A - Add --- */
/* in adds and subtracts, carry is set or cleared, overflow is set only */
src = ReadW(eaddr);
src2 = ACC;
ACC = (ACC + src) & 0xFFFF;
C = ACC < src;
if (! V)
V = SIGN_BIT((~src ^ src2) & (src ^ ACC));
break;
case 0x11: /* --- AD - Add Double --- */
src = ((ACC << 16) | (EXT & 0xFFFF));
src2 = (ReadW(eaddr) << 16) + ReadW(eaddr|1);
dst = src + src2;
ACC = (dst >> 16) & 0xFFFF;
EXT = dst & 0xFFFF;
C = (unsigned int32) dst < (unsigned int32) src;
if (! V)
V = DWSIGN_BIT((~src ^ src2) & (src ^ dst));
break;
case 0x12: /* --- S - Subtract --- */
src = ACC;
src2 = ReadW(eaddr);
ACC = (ACC-src2) & 0xFFFF;
C = src2 < src;
if (! V)
V = SIGN_BIT((src ^ src2) & (src ^ ACC));
break;
case 0x13: /* --- SD - Subtract Double --- */
src = ((ACC << 16) | (EXT & 0xFFFF));
src2 = (ReadW(eaddr) << 16) + ReadW(eaddr|1);
dst = src - src2;
ACC = (dst >> 16) & 0xFFFF;
EXT = dst & 0xFFFF;
C = (unsigned int32) src2 < (unsigned int32) src;
if (! V)
V = DWSIGN_BIT((src ^ src2) & (src ^ dst));
break;
case 0x14: /* --- M - Multiply --- */
if ((src = ACC & 0xFFFF) & 0x8000) /* sign extend the values */
src |= ~0xFFFF;
if ((src2 = ReadW(eaddr)) & 0x8000)
src2 |= ~0xFFFF;
dst = src * src2;
ACC = (dst >> 16) & 0xFFFF; /* split the results */
EXT = dst & 0xFFFF;
break;
case 0x15: /* --- D - Divide --- */
src = ((ACC << 16) | (EXT & 0xFFFF));
if ((src2 = ReadW(eaddr)) & 0x8000)
src2 |= ~0xFFFF; /* oops: sign extend was missing, fixed 18Mar03 */
if (src2 == 0)
V = 1; /* divide by zero just sets overflow, ACC & EXT are undefined */
else {
ACC = (src / src2) & 0xFFFF;
EXT = (src % src2) & 0xFFFF;
}
break;
case 0x18: /* --- LD - Load ACC --- */
ACC = ReadW(eaddr);
break;
case 0x19: /* --- LDD - Load Double --- */
ACC = ReadW(eaddr);
EXT = ReadW(eaddr|1); /* notice address is |1 not +1 */
break;
case 0x1a: /* --- STO - Store ACC --- */
WriteW(eaddr, ACC);
break;
case 0x1b: /* --- STD - Store Double --- */
WriteW(eaddr|1, EXT);
WriteW(eaddr, ACC); /* order is important: if odd addr, only ACC is stored */
break;
case 0x1c: /* --- AND - Logical AND --- */
ACC &= ReadW(eaddr);
break;
case 0x1d: /* --- OR - Logical OR --- */
ACC |= ReadW(eaddr);
break;
case 0x1e: /* --- EOR - Logical Excl OR --- */
ACC ^= ReadW(eaddr);
break;
default:
/* all invalid instructions act like waits */
/* case 0x00: */
/* case 0x07: */
/* case 0x0a: */
/* case 0x0b: */
/* case 0x0e: */
/* case 0x0f: */
/* case 0x16: */
/* case 0x17: */
/* case 0x1f: */
wait_state = WAIT_INVALID_OP;
if (F)
DECREMENT_IAR; /* assume it wouldn't have fetched 2nd word? */
break;
} /* end instruction decode switch */
if (RUNMODE != MODE_RUN && RUNMODE != MODE_INT_RUN)
reason = STOP_WAIT;
if (tbit && (ipl < 0)) { /* if INT_RUN mode, set IRQ5 after this instr */
GUI_BEGIN_CRITICAL_SECTION
SETBIT(cpu_dsw, CPU_DSW_INT_RUN);
SETBIT(ILSW[5], ILSW_5_INT_RUN);
int_req |= INT_REQ_5;
GUI_END_CRITICAL_SECTION
}
} /* end main loop */
#ifdef GUI_SUPPORT
gui_run(FALSE);