/
CPU704.cs
1756 lines (1709 loc) · 65.8 KB
/
CPU704.cs
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
using System;
using System.Text;
namespace Sim704
{
struct W1
{
const int wlen = 1; /* Word length */
const uint wmask = (1U << wlen) - 1U; /* mask for word */
uint w; /* 1 bit Word stored in 32 bit uint*/
public W1(uint value)
{
w = value & wmask;
}
public static implicit operator uint(W1 d)
{
return d.w;
}
public static explicit operator W1(uint d)
{
return new W1(d);
}
public override string ToString()
{
return Convert.ToString(w);
}
}
struct W3
{
const int wlen = 3; /* Word length */
const uint wmask = (1U << wlen) - 1U; /* mask for word */
uint w; /* 3 bit Word stored in 32 bit uint*/
public W3(uint value)
{
w = value & wmask;
}
public static implicit operator uint(W3 d)
{
return d.w;
}
public static explicit operator W3(uint d)
{
return new W3(d);
}
public override string ToString()
{
return Convert.ToString(w);
}
}
struct W8
{
const int wlen = 8; /* Word length */
const uint wmask = (1U << wlen) - 1U; /* mask for word */
uint w; /* 8 bit Word stored in 32 bit uint*/
public W8(uint value)
{
w = value & wmask;
}
public static implicit operator uint(W8 d)
{
return d.w;
}
public static explicit operator W8(uint d)
{
return new W8(d);
}
public override string ToString()
{
return Convert.ToString(w, 8).PadLeft(3, '0');
}
}
struct W10
{
const int wlen = 10; /* Word length */
const uint wmask = (1U << wlen) - 1U; /* mask for word */
uint w; /* 10 bit Word stored in 32 bit uint*/
public W10(uint value)
{
w = value & wmask;
}
public static implicit operator uint(W10 d)
{
return d.w;
}
public static explicit operator W10(uint d)
{
return new W10(d);
}
public override string ToString()
{
return Convert.ToString(w, 8).PadLeft(4, '0');
}
}
struct W15
{
const int wlen = 15; /* Word length */
const uint wmask = (1U << wlen) - 1U; /* mask for word */
uint w; /* 15 bit Word stored in 32 bit uint*/
public W15(uint value)
{
w = value & wmask;
}
public static implicit operator uint(W15 d)
{
return d.w;
}
public static explicit operator W15(uint d)
{
return new W15(d);
}
public override string ToString()
{
return Convert.ToString(w, 8).PadLeft(5, '0');
}
}
struct W18
{
const int wlen = 18; /* Word length */
const uint wmask = (1U << wlen) - 1U; /* mask for word */
uint w; /* 18 bit Word stored in 32 bit uint*/
public W18(uint value)
{
w = value & wmask;
}
public static implicit operator uint(W18 d)
{
return d.w;
}
public static explicit operator W18(uint d)
{
return new W18(d);
}
public override string ToString()
{
return Convert.ToString(w, 8).PadLeft(6, '0');
}
}
struct W27
{
const int wlen = 27; /* Word length */
const uint wmask = (1U << wlen) - 1U; /* mask for word */
uint w; /* 27 bit Word stored in 32 bit uint*/
public W27(uint value)
{
w = value & wmask;
}
public static implicit operator uint(W27 d)
{
return d.w;
}
public static explicit operator W27(uint d)
{
return new W27(d);
}
public override string ToString()
{
return Convert.ToString(w, 8).PadLeft(9, '0');
}
}
struct W35
{
const int wlen = 35; /* Word length */
const ulong wmask = (1UL << wlen) - 1UL; /* mask for word */
ulong w; /* 35 bit Word stored in 64 bit ulong*/
public W35(ulong value)
{
w = value & wmask;
}
public static implicit operator ulong(W35 d)
{
return d.w;
}
public static explicit operator W35(ulong d)
{
return new W35(d);
}
public override string ToString()
{
return Convert.ToString((long)w, 8).PadLeft(12, '0');
}
}
struct W36
{
const int alen = 15; /* Adress length */
const int tlen = 3; /* Tag length */
const int dlen = 15; /* Decrement length */
const int plen = 3; /* Prefix length */
const int wlen = alen + tlen + dlen + plen; /* Word length */
const int slen = 1; /* Sign length */
const int mlen = wlen - slen; /* Magnitude length */
const int b1len = 1; /* lenght of Bit 1 */
const int llen = 18; /* length of left half */
const int flen = 27; /* lenght of the fraction */
const int clen = 8; /* lenght of the characteristic */
const int b9len = 1; /* lenght of Bit 9 */
const int apos = 0; /* bit pos for Adress*/
const int tpos = alen; /* bit pos for Tag*/
const int dpos = tpos + tlen; /* bit pos for Decrement*/
const int ppos = dpos + dlen; /* bit pos for Prefix*/
const int mpos = 0; /* bit pos for Magniude */
const int spos = wlen - slen; /* bit pos for Sign */
const int b1pos = spos - b1len; /* bit pos for Bit 1 */
const int lpos = 18; /* bit pos of left half */
const int fpos = 0; /* bit pos of the fraction */
const int cpos = flen; /* bit pos of the characteristic */
const int b9pos = cpos - b9len; /* bit pos for Bit 9 */
const ulong a0mask = (1UL << alen) - 1UL; /* mask for Adress at bit 0*/
const ulong t0mask = (1UL << tlen) - 1UL; /* mask for Tag at bit 0*/
const ulong d0mask = (1UL << dlen) - 1UL; /* mask for Decrement at bit 0 */
const ulong p0mask = (1UL << plen) - 1UL; /* mask for Prefix at bit 0 */
const ulong wmask = (1UL << wlen) - 1UL; /* mask for Word */
const ulong m0mask = (1UL << mlen) - 1UL; /* mask for Magnitude at bit 0*/
const ulong s0mask = (1UL << slen) - 1UL; /* mask for Sign at bit 0 */
const ulong b10mask = (1UL << b1len) - 1UL; /* mask for Bit 1 at bit 0 */
const ulong l0mask = (1UL << llen) - 1UL; /* mask for left half at bit 0*/
const ulong f0mask = (1UL << flen) - 1UL; /* mask for fraction at bit 0 */
const ulong c0mask = (1UL << clen) - 1UL; /* mask for characteristic at bit 0 */
const ulong b90mask = (1UL << b9len) - 1UL; /* mask for Bit 9 at bit 0 */
const ulong awmask = a0mask << apos; /* mask for Adress in word */
const ulong twmask = t0mask << tpos; /* mask for Tag in word */
const ulong dwmask = d0mask << dpos; /* mask for Decrement in word */
const ulong pwmask = p0mask << ppos; /* mask for Prefix in word */
const ulong mwmask = m0mask << mpos; /* mask for Magnitude in word */
const ulong swmask = s0mask << spos; /* mask for Sign in word */
const ulong b1wmask = b10mask << b1pos; /* mask for Bit 1 in word */
const ulong lwmask = l0mask << lpos; /* mask for left half in word */
const ulong fwmask = f0mask << fpos; /* mask for fraction in word */
const ulong cwmask = c0mask << cpos; /* mask for characteristic int word */
const ulong b9wmask = b90mask << b9pos; /* mask for Bit 9 in word */
const ulong iawmask = (wmask ^ awmask); /* inv mask for Adress in word */
const ulong itwmask = (wmask ^ twmask); /* inv mask for Tag in word */
const ulong idwmask = (wmask ^ dwmask); /* inv mask for Decrement in word */
const ulong ipwmask = (wmask ^ pwmask); /* inv mask for Prefix in word */
const ulong imwmask = (wmask ^ mwmask); /* inv mask for Magnitude in word */
const ulong iswmask = (wmask ^ swmask); /* inv mask for Sign in word */
const ulong ib1wmask = (wmask ^ b1wmask); /* inv mask for Bit 1 in word */
const ulong ilwmask = (wmask ^ lwmask); /* inv mask for left half in word */
const ulong ifwmask = (wmask ^ fwmask); /* inv mask for fraction in word */
const ulong icwmask = (wmask ^ cwmask); /* inv mask for characteristic in word */
const ulong ib9wmask = (wmask ^ b9wmask); /* inv mask for Bit 1 in word */
ulong w; /* 36 bit Word stored in 64 bit ulong*/
public W36(ulong value)
{
w = value & wmask;
}
public static implicit operator ulong(W36 d)
{
return d.w;
}
public static explicit operator W36(ulong d)
{
return new W36(d);
}
public W15 A /* Adress 15 bit */
{
get
{
return (W15)w;
}
set
{
w = (w & iawmask) | value;
}
}
public W3 T /* Tag 3 bit */
{
get
{
return (W3)(w >> tpos);
}
set
{
w = (w & itwmask) | ((ulong)value << tpos);
}
}
public W15 D /* Decrement 15 bit */
{
get
{
return (W15)(w >> dpos);
}
set
{
w = (w & idwmask) | ((ulong)value << dpos);
}
}
public W3 P /* Prefix 3 bit */
{
get
{
return (W3)(w >> ppos);
}
set
{
w = (w & ipwmask) | ((ulong)value << ppos);
}
}
public W35 M /* Magnitude 35 bit */
{
get
{
return (W35)w;
}
set
{
w = (w & imwmask) | value;
}
}
public W1 S /* Sign 1 bit*/
{
get
{
return (W1)(w >> spos);
}
set
{
w = (w & iswmask) | ((ulong)value << spos);
}
}
public W1 B1 /* Bit1 1 Bit*/
{
get
{
return (W1)(w >> b1pos);
}
set
{
w = (w & ib1wmask) | ((ulong)value << b1pos);
}
}
public W18 L /* left half */
{
get
{
return (W18)(w >> lpos);
}
set
{
w = (w & ilwmask) | ((ulong)value << lpos);
}
}
public W27 F /* Fraction 27 bit */
{
get
{
return (W27)w;
}
set
{
w = (w & ifwmask) | value;
}
}
public W8 C /* Charcteristic 8 bit*/
{
get
{
return (W8)(w >> cpos);
}
set
{
w = (w & icwmask) | ((ulong)value << cpos);
}
}
public W1 B9 /* Bit9 1 Bit*/
{
get
{
return (W1)(w >> b9pos);
}
set
{
w = (w & ib9wmask) | ((ulong)value << b9pos);
}
}
public override string ToString()
{
return Convert.ToString((long)w, 8).PadLeft(12, '0');
}
public string MQtoString()
{
StringBuilder sb = new StringBuilder();
if (S != 0)
sb.Append('-');
else
sb.Append(' ');
sb.Append(M.ToString());
return sb.ToString();
}
}
struct W37
{
const int wlen = 37; /* Word length */
const ulong wmask = (1UL << wlen) - 1UL; /* mask for word */
ulong w; /* 37 bit Word stored in 64 bit ulong*/
public W37(ulong value)
{
w = value & wmask;
}
public static implicit operator ulong(W37 d)
{
return d.w;
}
public static explicit operator W37(ulong d)
{
return new W37(d);
}
public static explicit operator W37(W35 d)
{
return new W37(d);
}
public override string ToString()
{
return Convert.ToString((long)w, 8).PadLeft(13, '0');
}
}
struct W38
{
const int alen = 15; /* Adress length */
const int tlen = 3; /* Tag length */
const int dlen = 15; /* Decrement length */
const int prlen = 3; /* Prefix length */
const int slen = 1; /* Sign length */
const int m36len = alen + tlen + dlen + prlen; /* 36Bit Magnitude length */
const int m35len = m36len - slen; /* 35Bit Magnitude length */
const int pblen = 1; /* Lenght for P Bit */
const int qlen = 1; /* Length of Q BIT */
const int wlen = m35len + pblen + qlen + slen; /* Word length */
const int m37len = wlen - slen; /* 37Bit Magnitude length */
const int flen = 27; /* lenght of the fraction */
const int c10len = 10; /* lenght of the 10 bit characteristic */
const int b9len = 1; /* lenght of Bit 9 */
const int apos = 0; /* bit pos for Adress*/
const int tpos = apos + alen; /* bit pos for Tag*/
const int dpos = tpos + tlen; /* bit pos for Decrement*/
const int prpos = dpos + dlen; /* bit pos for Prefix*/
const int qpos = prpos + prlen; /* bit pos for Q Bit*/
const int spos = qpos + qlen; /* bit pos for Sign */
const int pbpos = qpos - pblen; /* bit pos for P Bit */
const int m35pos = 0; /* bit pos for 35 Bit Magniude */
const int m36pos = 0; /* bit pos for 36 Bit Magniude */
const int m37pos = 0; /* bit pos for 37 Bit Magniude */
const int fpos = 0; /* bit pos of the fraction */
const int c10pos = flen; /* bit pos of the 10 bit characteristic */
const int b9pos = c10pos - b9len; /* bit pos for Bit 9 */
const ulong a0mask = (1UL << alen) - 1UL; /* mask for Adress at bit 0*/
const ulong t0mask = (1UL << tlen) - 1UL; /* mask for Tag at bit 0*/
const ulong d0mask = (1UL << dlen) - 1UL; /* mask for Decrement at bit 0 */
const ulong pr0mask = (1UL << prlen) - 1UL; /* mask for Prefix at bit 0 */
const ulong wmask = (1UL << wlen) - 1UL; /* mask for Word */
const ulong m350mask = (1UL << m35len) - 1UL; /* mask for 35 Bit Magnitude at bit 0*/
const ulong m360mask = (1UL << m36len) - 1UL; /* mask for 36 Bit Magnitude at bit 0*/
const ulong m370mask = (1UL << m37len) - 1UL; /* mask for 37 Bit Magnitude at bit 0*/
const ulong s0mask = (1UL << slen) - 1UL; /* mask for Sign at bit 0 */
const ulong q0mask = (1UL << qlen) - 1UL;/* mask for Q Bit at bit 0 */
const ulong pb0mask = (1UL << pblen) - 1UL;/* mask for P Bit at bit 0 */
const ulong f0mask = (1UL << flen) - 1UL; /* mask for fraction at bit 0 */
const ulong c100mask = (1UL << c10len) - 1UL; /* mask for 10 bit characteristic at bit 0 */
const ulong b90mask = (1UL << b9len) - 1UL; /* mask for Bit 9 at bit 0 */
const ulong awmask = a0mask << apos; /* mask for Adress in word */
const ulong twmask = t0mask << tpos; /* mask for Tag in word */
const ulong dwmask = d0mask << dpos; /* mask for Decrement in word */
const ulong prwmask = pr0mask << prpos; /* mask for Prefix in word */
const ulong m35wmask = m350mask << m35pos; /* mask for 35 Bit Magnitude in word */
const ulong m36wmask = m360mask << m36pos; /* mask for 36 Bit Magnitude in word */
const ulong m37wmask = m370mask << m37pos; /* mask for 37 Bit Magnitude in word */
const ulong swmask = s0mask << spos; /* mask for Sign in word */
const ulong qwmask = q0mask << qpos; /* mask for Q Bit in word */
const ulong pbwmask = pb0mask << pbpos; /* mask for P Bit in word */
const ulong fwmask = f0mask << fpos; /* mask for fraction in word */
const ulong c10wmask = c100mask << c10pos; /* mask for 10 bit characteristic int word */
const ulong b9wmask = b90mask << b9pos; /* mask for Bit 9 in word */
const ulong iawmask = (wmask ^ awmask); /* inv mask for Adress in word */
const ulong itwmask = (wmask ^ twmask); /* inv mask for Tag in word */
const ulong idwmask = (wmask ^ dwmask); /* inv mask for Decrement in word */
const ulong iprwmask = (wmask ^ prwmask); /* inv mask for Prefix in word */
const ulong im35wmask = (wmask ^ m35wmask); /* inv mask for 35 Bit Magnitude in word */
const ulong im36wmask = (wmask ^ m36wmask); /* inv mask for 36 Bit Magnitude in word */
const ulong im37wmask = (wmask ^ m37wmask); /* inv mask for 37 Bit Magnitude in word */
const ulong iswmask = (wmask ^ swmask); /* inv mask for Sign in word */
const ulong iqwmask = (wmask ^ qwmask); /* inv mask for Q Bit in word */
const ulong ipbwmask = (wmask ^ pbwmask); /* inv mask for P Bit in word */
const ulong ifwmask = (wmask ^ fwmask); /* inv mask for fraction in word */
const ulong ic10wmask = (wmask ^ c10wmask); /* inv mask for 10 bit characteristic in word */
const ulong ib9wmask = (wmask ^ b9wmask); /* inv mask for Bit 1 in word */
ulong w; /* 38 bit Word stored in 64 bit ulong*/
public W38(ulong value)
{
w = value & wmask;
}
public static implicit operator ulong(W38 d)
{
return d.w;
}
public static explicit operator W38(ulong d)
{
return new W38(d);
}
public W15 A /* Adress 15 bit */
{
get
{
return (W15)w;
}
set
{
w = (w & iawmask) | value;
}
}
public W3 T /* Tag 3 bit */
{
get
{
return (W3)(w >> tpos);
}
set
{
w = (w & itwmask) | ((ulong)value << tpos);
}
}
public W15 D /* Decrement 15 bit */
{
get
{
return (W15)(w >> dpos);
}
set
{
w = (w & idwmask) | ((ulong)value << dpos);
}
}
public W3 PR /* Prefix 3 bit */
{
get
{
return (W3)(w >> prpos);
}
set
{
w = (w & iprwmask) | ((ulong)value << prpos);
}
}
public W1 PB /* P Bit 1 bit*/
{
get
{
return (W1)(w >> pbpos);
}
set
{
w = (w & ipbwmask) | ((ulong)value << pbpos);
}
}
public W1 Q /* Q Bit 1 bit*/
{
get
{
return (W1)(w >> qpos);
}
set
{
w = (w & iqwmask) | ((ulong)value << qpos);
}
}
public W35 M35 /* Magnitude 35 bit */
{
get
{
return (W35)w;
}
set
{
w = (w & im35wmask) | value;
}
}
public W36 M36 /* Magnitude 36 bit */
{
get
{
return (W36)w;
}
set
{
w = (w & im36wmask) | value;
}
}
public W37 M37 /* Magnitude 37 bit */
{
get
{
return (W37)w;
}
set
{
w = (w & im37wmask) | value;
}
}
public W1 S /* Sign 1 bit*/
{
get
{
return (W1)(w >> spos);
}
set
{
w = (w & iswmask) | ((ulong)value << spos);
}
}
public W27 F /* Fraction 27 bit */
{
get
{
return (W27)w;
}
set
{
w = (w & ifwmask) | value;
}
}
public W10 C10 /* Charcteristic 10 bit*/
{
get
{
return (W10)(w >> c10pos);
}
set
{
w = (w & ic10wmask) | ((ulong)value << c10pos);
}
}
public W1 B9 /* Bit9 1 Bit*/
{
get
{
return (W1)(w >> b9pos);
}
set
{
w = (w & ib9wmask) | ((ulong)value << b9pos);
}
}
public static explicit operator W38(W36 d)
{
return new W38(d);
}
public override string ToString()
{
return Convert.ToString((long)w, 8).PadLeft(13, '0');
}
public string ACToString()
{
StringBuilder sb = new StringBuilder();
uint l = S + Q + PB;
while (l < 3)
{
sb.Append(' ');
l++;
}
if (S != 0)
sb.Append('-');
if (Q != 0)
sb.Append('Q');
if (PB != 0)
sb.Append('P');
sb.Append(M35.ToString());
return sb.ToString();
}
}
struct WA
{
static uint wmask; /* mask for word */
public static int olen; /* octal lenght */
uint w; /* N bit Word stored in 32 bit uint*/
public static void SetMask(uint AddressMask)
{
olen = 0;
wmask = AddressMask;
while (AddressMask != 0)
{
AddressMask >>= 3;
olen++;
}
}
public WA(uint value)
{
w = value & wmask;
}
public static implicit operator uint(WA d)
{
return d.w;
}
public static explicit operator WA(uint d)
{
return new WA(d);
}
public override string ToString()
{
return Convert.ToString(w, 8).PadLeft(olen, '0');
}
}
static class CPU704
{
static bool trapping = false; /* Trapping mode active */
public static bool repeat; /* repeat last instruction */
public static WA ILC; /* Instruction Counter */
public static WA NIC; /* Next Instruction Counter for Transfer*/
public static W36 SR; /* Storage register */
static WA[] X = new WA[3]; /* Index Register */
public static bool halt = false;
static public void Clr()
{
ALU.Clear();
X[0] = (WA)0;
X[1] = (WA)0;
X[2] = (WA)0;
ILC = (WA)0;
halt = false;
trapping = false;
repeat = false;
CoreMemory.Clear();
}
static void SetX(WA A)
{
W3 T = SR.T;
if (0 != (T & 1))
X[0] = A;
if (0 != (T & 2))
X[1] = A;
if (0 != (T & 4))
X[2] = A;
}
static WA GetX()
{
uint A = 0;
W3 T = SR.T;
if (0 != (T & 1))
A |= X[0];
if (0 != (T & 2))
A |= X[1];
if (0 != (T & 4))
A |= X[2];
return (WA)A;
}
static WA GetY() /* Get Y*/
{
return (WA)(SR.A - GetX());
}
static W36 LoadCY() /* Load C(Y) */
{
return CoreMemory.C((WA)(SR.A - GetX()));
}
static void StoreCY(W36 V) /* Store V to C(Y) */
{
CoreMemory.C((WA)(SR.A - GetX()), V);
}
static WA GetYni() /* Get Y not indexed*/
{
return (WA)(uint)SR.A;
}
static W36 LoadCYni() /* Load C(Y) not indexed*/
{
return CoreMemory.C((WA)(uint)SR.A);
}
static void StoreCYni(W36 V) /* Store V to C(Y) not indexed*/
{
CoreMemory.C((WA)(uint)SR.A, V);
}
static void DebugHdr()
{
if (Io704.Config.LogCPU != null)
{
if (WA.olen == 4)
Io704.LogCPU.WriteLine("IC AC MQ XR1 XR2 XR4");
else
Io704.LogCPU.WriteLine("IC AC MQ XR1 XR2 XR4");
}
}
static void Debug(string OPC)
{
if (Io704.Config.LogCPU != null)
{
Io704.LogCPU.Write("{0} {1} {2} {3} {4} {5} {6} ", ILC, ALU.AC.ACToString(), ALU.MQ.MQtoString(), X[0], X[1], X[2], SR.MQtoString());
Io704.LogCPU.WriteLine(OPC);
}
if (halt)
{
Io704.Flush();
Console.Error.Write("{0} {1} {2} {3} {4} {5} {6} ", ILC, ALU.AC.ACToString(), ALU.MQ.MQtoString(), X[0], X[1], X[2], SR.MQtoString());
Console.Error.WriteLine(OPC);
}
}
static void DebugAT(string OPC)
{
if (Io704.Config.LogCPU != null)
{
Io704.LogCPU.Write("{0} {1} {2} {3} {4} {5} {6} ", ILC, ALU.AC.ACToString(), ALU.MQ.MQtoString(), X[0], X[1], X[2], SR.MQtoString());
Io704.LogCPU.Write(OPC);
if (SR.T != 0)
Io704.LogCPU.WriteLine(" {0},{1}", SR.A, SR.T);
else
Io704.LogCPU.WriteLine(" {0}", SR.A);
}
if (halt)
{
Io704.Flush();
Console.Error.Write("{0} {1} {2} {3} {4} {5} {6} ", ILC, ALU.AC.ACToString(), ALU.MQ.MQtoString(), X[0], X[1], X[2], SR.MQtoString());
Console.Error.Write(OPC);
if (SR.T != 0)
Console.Error.WriteLine(" {0},{1}", SR.A, SR.T);
else
Console.Error.WriteLine(" {0}", SR.A);
}
}
static void DebugAT0(string OPC)
{
if (Io704.Config.LogCPU != null)
{
Io704.LogCPU.Write("{0} {1} {2} {3} {4} {5} {6} ", ILC, ALU.AC.ACToString(), ALU.MQ.MQtoString(), X[0], X[1], X[2], SR.MQtoString());
Io704.LogCPU.Write(OPC);
Io704.LogCPU.WriteLine(" {0},{1}", SR.A, SR.T);
}
if (halt)
{
Io704.Flush();
Console.Error.Write("{0} {1} {2} {3} {4} {5} {6} ", ILC, ALU.AC.ACToString(), ALU.MQ.MQtoString(), X[0], X[1], X[2], SR.MQtoString());
Console.Error.Write(OPC);
Console.Error.WriteLine(" {0},{1}", SR.A, SR.T);
}
}
static void DebugATD(string OPC)
{
if (Io704.Config.LogCPU != null)
{
Io704.LogCPU.Write("{0} {1} {2} {3} {4} {5} {6} ", ILC, ALU.AC.ACToString(), ALU.MQ.MQtoString(), X[0], X[1], X[2], SR.MQtoString());
Io704.LogCPU.Write(OPC);
Io704.LogCPU.WriteLine(" {0},{1},{2}", SR.A, SR.T, SR.D);
}
if (halt)
{
Io704.Flush();
Console.Error.Write("{0} {1} {2} {3} {4} {5} {6} ", ILC, ALU.AC.ACToString(), ALU.MQ.MQtoString(), X[0], X[1], X[2], SR.MQtoString());
Console.Error.Write(OPC);
Console.Error.WriteLine(" {0},{1},{2}", SR.A, SR.T, SR.D);
}
}
static public bool Step()
{
/* Fetch instruction */
SR = CoreMemory.C(ILC);
bool transferInst = false;
bool doTransfer = false;
uint skip = 0;
/* check if Type A or Type B instruction */
bool splus = (SR.S == 0); /* Sign positive*/
uint P = (SR.P & 3); /* lower 2 bits of prefix */
bool inst = false;
switch (P)
{
case 1: /*1000*/
if (splus) /* +1 : TXI Transfer with index incremeted */
{
DebugATD("TXI");
SetX((WA)(GetX() + SR.D));
NIC = GetYni();
transferInst = true;
doTransfer = true;
inst = true;
}
break;
case 2:/*2000*/
if (splus) /* +2 : TIX Transfer on Index */
{
DebugATD("TIX");
WA X = GetX();
transferInst = true;
if (X > (WA)(uint)SR.D)
{
SetX((WA)(X - SR.D));
NIC = GetYni();
doTransfer = true;
}
inst = true;
}
else /* -2 : TNX Transfer on No Index */
{
DebugATD("TNX");
WA X = GetX();
transferInst = true;
if (X <= (WA)(uint)SR.D)
{
NIC = GetYni();
doTransfer = true;
}
else
SetX((WA)(X - SR.D));
inst = true;
}
break;
case 3: /*3000*/
if (splus) /* +3 : TXH */
{
DebugATD("TXH");
transferInst = true;
if (GetX() > (WA)(uint)SR.D)
{
NIC = GetYni();
doTransfer = true;
}
inst = true;
}
else /* -3 : TXL */
{
DebugATD("TXL");
transferInst = true;
if (GetX() <= (WA)(uint)SR.D)
{
NIC = GetYni();
doTransfer = true;
}
inst = true;
}
break;
case 0:
/* Type B instruction */
P = (uint)(SR.D >> 6); /* upper 9 bits of Decrement */
switch (P)
{
case 0: /*000*/
if (splus) /*+000 HTR Halt and Transfer*/
{
halt = true;
DebugAT("HTR");
transferInst = true;
doTransfer = true;
NIC = GetY();
inst = true;
}
break;
case 16: /*020*/
if (splus) /*+20 TRA Transfer*/
{
DebugAT("TRA");
transferInst = true;
doTransfer = true;
NIC = GetY();
inst = true;
}
break;
case 17: /*021*/
if (splus) /*+20 TTR Trap Transfer*/
{
DebugAT("TTR");
doTransfer = true;
NIC = GetY();
inst = true;
}
break;
case 32: /*040*/
if (splus) /*+40 TLQ Transfer on Low MQ*/
{
DebugAT("TLQ");
transferInst = true;
doTransfer = ALU.TLQ();
NIC = GetY();
inst = true;