-
Notifications
You must be signed in to change notification settings - Fork 1
/
MillULX.cpp
1635 lines (1564 loc) · 60.6 KB
/
MillULX.cpp
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
/*
Copyright (c) 2019, Thomas DiModica
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the names of the copyright holders nor the names of other
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Intended to logically exercise the following patents:
Computer processor employing explicit operations that support execution of software pipelined loops
and a compiler that utilizes such operations for scheduling software pipelined loops
Patent number: 9815669
Computer processor employing instructions with elided nop operations
Patent number: 9785441
Computer processor employing temporal addressing for storage of transient operands
Patent number: 9513921
Computer processor employing split-stream encoding
Patent number: 9513920
These are property of Mill Computing, Inc.
*/
#include <vector>
#include <pthread.h>
#include <cstdio>
#include <cstring>
#include <unistd.h>
typedef long long BELT_T;
typedef unsigned int MEM_T;
static const size_t BELT_SIZE = 32U; // If you change this, BAD! things will happen.
static const size_t ALUNITS = 2U;
static const size_t ALU_RETIRE_SIZE = 2U;
static const size_t FLOW_UNITS = 1U;
static const size_t FLOW_RETIRE_SIZE = BELT_SIZE;
// A not-taken call with a full belt of returns requires this.
static const BELT_T TRANSIENT = 0x200000000LL;
static const BELT_T INVALID = 0x400000000LL;
static const BELT_T OVERFLOW = 0x800000000LL;
static const BELT_T ZERO = 0x1000000000LL;
static const BELT_T EMPTY = 0x2000000000LL;
static const BELT_T CARRY = 0x100000000LL;
static const BELT_T NEGATIVE = 0x80000000LL;
static const char* endian()
{
const short var = 0x454C;
return (0 == std::strncmp("LE", static_cast<const char*>(static_cast<const void*>(&var)), 2U)) ? "LE" : "BE";
}
enum FlowBeltUse
{
NOT_IN_USE, // Because SOMEONE is probably using UNUSED
CANON,
SLOW_CANON,
SIGNAL_CALL,
SIGNAL_RETURN
};
class ALURetire
{
public:
BELT_T fast [ALU_RETIRE_SIZE]; // Reinitialize to EMPTY after each op.
BELT_T slow [ALU_RETIRE_SIZE];
size_t nops; // Flow NOPs queued up by the ALUnits
void flush()
{
for (size_t i = 0; i < ALU_RETIRE_SIZE; ++i) fast[i] = EMPTY;
for (size_t i = 0; i < ALU_RETIRE_SIZE; ++i) slow[i] = EMPTY;
nops = 0U;
}
void write(std::FILE * file)
{
std::fwrite(static_cast<void*>(fast), sizeof(BELT_T), ALU_RETIRE_SIZE, file);
std::fwrite(static_cast<void*>(slow), sizeof(BELT_T), ALU_RETIRE_SIZE, file);
std::fwrite(static_cast<void*>(&nops), sizeof(size_t), 1U, file);
}
void read(std::FILE * file)
{
std::fread(static_cast<void*>(fast), sizeof(BELT_T), ALU_RETIRE_SIZE, file);
std::fread(static_cast<void*>(slow), sizeof(BELT_T), ALU_RETIRE_SIZE, file);
std::fread(static_cast<void*>(&nops), sizeof(size_t), 1U, file);
}
};
class FlowRetire
{
public:
BELT_T fast [FLOW_RETIRE_SIZE];
BELT_T slow;
size_t nops; // ALU NOPs queued up by the flow instructions
BELT_T belt [BELT_SIZE]; // Don't need it's filled size: just count not EMPTY
FlowBeltUse use; // Is the belt used and how
size_t next; // Size of the extra data to this flow instruction
size_t jump; // The destination of a branch or call instruction (0 IS invalid)
FlowRetire()
{
for (size_t i = 0; i < FLOW_RETIRE_SIZE; ++i) fast[i] = EMPTY;
slow = EMPTY;
nops = 0U;
for (size_t i = 0; i < BELT_SIZE; ++i) belt[i] = EMPTY;
use = NOT_IN_USE;
next = 0U;
jump = 0U;
}
void flush()
{
fast[0] = EMPTY;
if (EMPTY != fast[1])
{
for (size_t i = 1; i < FLOW_RETIRE_SIZE; ++i) fast[i] = EMPTY;
}
slow = EMPTY;
nops = 0U;
if ((NOT_IN_USE != use) || (EMPTY != belt[0]))
{
for (size_t i = 0; i < BELT_SIZE; ++i) belt[i] = EMPTY;
use = NOT_IN_USE;
}
next = 0U;
jump = 0U;
}
void write(std::FILE * file)
{
std::fwrite(static_cast<void*>(fast), sizeof(BELT_T), FLOW_RETIRE_SIZE, file);
std::fwrite(static_cast<void*>(&slow), sizeof(BELT_T), 1U, file);
std::fwrite(static_cast<void*>(&nops), sizeof(size_t), 1U, file);
std::fwrite(static_cast<void*>(belt), sizeof(BELT_T), BELT_SIZE, file);
std::fwrite(static_cast<void*>(&use), sizeof(FlowBeltUse), 1U, file);
std::fwrite(static_cast<void*>(&next), sizeof(size_t), 1U, file);
std::fwrite(static_cast<void*>(&jump), sizeof(size_t), 1U, file);
}
void read(std::FILE * file)
{
std::fread(static_cast<void*>(fast), sizeof(BELT_T), FLOW_RETIRE_SIZE, file);
std::fread(static_cast<void*>(&slow), sizeof(BELT_T), 1U, file);
std::fread(static_cast<void*>(&nops), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(belt), sizeof(BELT_T), BELT_SIZE, file);
std::fread(static_cast<void*>(&use), sizeof(FlowBeltUse), 1U, file);
std::fread(static_cast<void*>(&next), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&jump), sizeof(size_t), 1U, file);
}
};
class Frame
{
public:
// ALU/FLOW read-only
BELT_T fast [BELT_SIZE];
BELT_T slow [BELT_SIZE];
size_t ffront, fsize; // The front and size of the fast belt
size_t sfront, ssize; // The front and size of the slow belt
size_t alunop;
size_t flownop;
size_t alupc;
size_t flowpc;
size_t entryPoint; // What alupc and flowpc were set to at the creation of this Frame.
size_t nextpc; // The winning branch instruction.
size_t index; // For a call, the flow unit that initiated it.
// ALU/FLOW write-only
ALURetire alu_retire [ALUNITS];
FlowRetire flow_retire [FLOW_UNITS];
void init (void)
{
for (size_t i = 0U; i < BELT_SIZE; ++i) fast[i] = INVALID;
for (size_t i = 0U; i < BELT_SIZE; ++i) slow[i] = INVALID;
ffront = 0U;
fsize = 0U;
sfront = 0U;
ssize = 0U;
alunop = 0U;
flownop = 0U;
alupc = 0U;
flowpc = 0U;
entryPoint = 0U;
nextpc = 0U;
fast[(ffront + 30) & 0x1F] = ZERO;
fast[(ffront + 31) & 0x1F] = 1;
slow[(sfront + 30) & 0x1F] = INVALID;
slow[(sfront + 31) & 0x1F] = TRANSIENT;
}
void write(std::FILE * file)
{
std::fwrite(static_cast<void*>(fast), sizeof(BELT_T), BELT_SIZE, file);
std::fwrite(static_cast<void*>(slow), sizeof(BELT_T), BELT_SIZE, file);
std::fwrite(static_cast<void*>(&ffront), sizeof(size_t), 1U, file); //std::printf("ffront %lu\n", ffront);
std::fwrite(static_cast<void*>(&fsize), sizeof(size_t), 1U, file); //std::printf("fsize %lu\n", fsize);
std::fwrite(static_cast<void*>(&sfront), sizeof(size_t), 1U, file);
std::fwrite(static_cast<void*>(&ssize), sizeof(size_t), 1U, file);
std::fwrite(static_cast<void*>(&alunop), sizeof(size_t), 1U, file); //std::printf("alunop %lu\n", alunop);
std::fwrite(static_cast<void*>(&flownop), sizeof(size_t), 1U, file); //std::printf("flownop %lu\n", flownop);
std::fwrite(static_cast<void*>(&alupc), sizeof(size_t), 1U, file); //std::printf("alupc %lu\n", alupc);
std::fwrite(static_cast<void*>(&flowpc), sizeof(size_t), 1U, file); //std::printf("flowpc %lu\n", flowpc);
std::fwrite(static_cast<void*>(&entryPoint), sizeof(size_t), 1U, file); //std::printf("entryPoint %lu\n", entryPoint);
std::fwrite(static_cast<void*>(&nextpc), sizeof(size_t), 1U, file); //std::printf("nextpc %lu\n", nextpc);
std::fwrite(static_cast<void*>(&index), sizeof(size_t), 1U, file);
for (size_t i = 0U; i < ALUNITS; ++i) alu_retire[i].write(file);
for (size_t i = 0U; i < FLOW_UNITS; ++i) flow_retire[i].write(file);
}
void read(std::FILE * file)
{
std::fread(static_cast<void*>(fast), sizeof(BELT_T), BELT_SIZE, file);
std::fread(static_cast<void*>(slow), sizeof(BELT_T), BELT_SIZE, file);
std::fread(static_cast<void*>(&ffront), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&fsize), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&sfront), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&ssize), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&alunop), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&flownop), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&alupc), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&flowpc), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&entryPoint), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&nextpc), sizeof(size_t), 1U, file);
std::fread(static_cast<void*>(&index), sizeof(size_t), 1U, file);
for (size_t i = 0U; i < ALUNITS; ++i) alu_retire[i].read(file);
for (size_t i = 0U; i < FLOW_UNITS; ++i) flow_retire[i].read(file);
}
};
class Machine
{
public:
std::vector<Frame> frames;
MEM_T * memory;
size_t memsize;
bool terminate;
bool invalidOp;
bool stop;
Machine() : memory(NULL), memsize(0U), terminate(false), invalidOp(false), stop(false)
{
frames.push_back(Frame());
}
void write(std::FILE * file)
{
std::fwrite(static_cast<void*>(&memsize), sizeof(size_t), 1U, file);
std::fwrite(static_cast<void*>(memory), sizeof(MEM_T), memsize, file);
size_t framesSize = frames.size();
std::fwrite(static_cast<void*>(&framesSize), sizeof(size_t), 1U, file);
for (size_t i = 0U; i < framesSize; ++i)
{
frames[i].write(file);
}
}
void read(std::FILE * file)
{
std::fread(static_cast<void*>(&memsize), sizeof(size_t), 1U, file);
memory = new MEM_T [memsize];
std::fread(static_cast<void*>(memory), sizeof(MEM_T), memsize, file);
size_t framesSize;
std::fread(static_cast<void*>(&framesSize), sizeof(size_t), 1U, file);
frames.clear();
for (size_t i = 0U; i < framesSize; ++i)
{
frames.push_back(Frame());
frames.back().read(file);
}
}
};
class FunctionalUnit
{
public:
Machine* machine;
pthread_barrier_t* synchronizer;
size_t slot;
pthread_t thread;
virtual void doStuff() = 0;
BELT_T getMemory(size_t location)
{
if (location >= machine->memsize)
{
return INVALID;
}
return machine->memory[location];
}
BELT_T setMemory(size_t location, MEM_T value)
{
if (location >= machine->memsize)
{
return INVALID;
}
machine->memory[location] = value;
return 0U;
}
static BELT_T getBeltContent(Frame& frame, size_t beltLocation)
{
if (0U == (beltLocation & 0x20))
{
if ((beltLocation > frame.fsize) && (beltLocation < 30))
{
return INVALID;
}
return frame.fast[(frame.ffront + beltLocation) & 0x1F];
}
else
{
if (((beltLocation & 0x1F) > frame.ssize) && (beltLocation < 62))
{
return INVALID;
}
return frame.slow[(frame.sfront + beltLocation) & 0x1F];
}
}
static bool extraNumerical(BELT_T op, BELT_T& res)
{
if (0L != (op & (TRANSIENT | INVALID)))
{
res = op;
return true;
}
return false;
}
static bool extraNumerical(BELT_T op1, BELT_T op2, BELT_T& res)
{
if (0L != ((op1 & op2) & TRANSIENT))
{
res = op1 > op2 ? op1 : op2; // Assume that the flow address of highest value is
return true; // the chronologically earlier instruction
}
if (0L != (op1 & TRANSIENT)) // TRANSIENT takes precedence over INVALID
{
res = op1;
return true;
}
if (0L != (op2 & TRANSIENT))
{
res = op2;
return true;
}
if (0L != ((op1 & op2) & INVALID))
{
res = op1 > op2 ? op1 : op2;
return true;
}
if (0L != (op1 & INVALID)) // Save the metadata for INVALID
{
res = op1;
return true;
}
if (0L != (op2 & INVALID))
{
res = op2;
return true;
}
return false;
}
static bool extraNumerical(BELT_T op1, BELT_T op2, BELT_T op3, BELT_T& res)
{
BELT_T temp = TRANSIENT;
bool result = false;
if (0L != (op1 & TRANSIENT))
{
temp = temp > op1 ? temp : op1;
result = true;
}
if (0L != (op2 & TRANSIENT))
{
temp = temp > op2 ? temp : op2;
result = true;
}
if (0L != (op3 & TRANSIENT))
{
temp = temp > op3 ? temp : op3;
result = true;
}
if (true == result)
{
res = temp;
return true;
}
temp = INVALID;
if (0L != (op1 & INVALID))
{
temp = temp > op1 ? temp : op1;
result = true;
}
if (0L != (op2 & INVALID))
{
temp = temp > op2 ? temp : op2;
result = true;
}
if (0L != (op3 & INVALID))
{
temp = temp > op3 ? temp : op3;
result = true;
}
if (true == result)
{
res = temp;
}
return result;
}
static BELT_T getZero(BELT_T input)
{
if (0U == (input & 0xFFFFFFFF))
{
return ZERO;
}
return 0;
}
static BELT_T getAdd(BELT_T op1, BELT_T op2, BELT_T op3)
{
BELT_T cb = (op3 & CARRY) ? 1 : 0;
BELT_T result = (op1 + op2 + cb) & 0x1FFFFFFFFLL; // CARRY/BORROW is free
if (0U != ((result ^ op1) & (result ^ op2) & 0x80000000))
{
result |= OVERFLOW;
}
return result;
}
bool conditionTrue(BELT_T cond, BELT_T flags)
{
if (0U != (cond & ~0xFLL))
{
std::printf("Arrived in conditionTrue with invalid condition code.\nThis is a bug.\n");
machine->invalidOp = true;
return false;
}
static const BELT_T conds [] = { 0U, CARRY, OVERFLOW, NEGATIVE, ZERO, ZERO | NEGATIVE, INVALID, TRANSIENT };
if (0 == cond)
{
return true;
}
else if (1 == cond)
{
return (0U == (flags & (INVALID | TRANSIENT)));
}
else
{
if (0U == (cond & 1))
{
return (0U != (flags & conds[cond >> 1]));
}
else
{
return (0U == (flags & conds[cond >> 1]));
}
}
}
};
class ALUnit : public FunctionalUnit
{
public:
virtual void doStuff()
{
for (;;)
{
//Wait for the start of an instruction cycle
pthread_barrier_wait(synchronizer);
// Do we need to die?
if (true == machine->terminate)
{
// std::printf("Terminating ALU slot: %lu\n", slot);
break;
}
// Interpret and execute the next operation.
Frame& frame = machine->frames.back();
ALURetire& retire = frame.alu_retire[slot];
retire.flush(); // Make retire station is clean.
if (0U == frame.alunop)
{
// std::printf("Executing ALU slot: %lu %lu\n", slot, frame.alupc);
BELT_T curOp = getMemory(frame.alupc + slot);
if (0U != (curOp & INVALID))
{
std::printf("Terminate initiated due to invalid operation in ALU slot: %d %d\n", static_cast<int>(slot), static_cast<int>(frame.alupc + slot));
machine->invalidOp = true;
}
if ((curOp & 0xF) > 5)
{
BELT_T cond, src, op1, op2, temp;
BELT_T* dest;
if (0 == (curOp & 0x10))
{
cond = (curOp >> 6) & 0xF;
src = getBeltContent(frame, (curOp >> 10) & 0x3F);
op1 = getBeltContent(frame, (curOp >> 16) & 0x3F);
op2 = getBeltContent(frame, (curOp >> 22) & 0x3F);
retire.nops = (curOp >> 28) & 0x7;
}
else
{
cond = 0; // Unconditional
src = 0;
op1 = getBeltContent(frame, (curOp >> 6) & 0x3F);
op2 = (curOp >> 12) & 0x1FFFF;
if (op2 & 0x10000)
{
op2 |= 0xFFFE0000;
}
retire.nops = (curOp >> 29) & 0x7;
}
dest = retire.fast;
if (curOp & 0x20)
{
dest = retire.slow;
}
if (false == conditionTrue(cond, src))
{
dest[0] = TRANSIENT | frame.alupc;
if ((9 == (curOp & 0xF)) || (10 == (curOp & 0xF)))
{
dest[1] = TRANSIENT | frame.alupc;
}
}
else if (true == extraNumerical(op1, op2, temp))
{
dest[0] = temp;
if ((9 == (curOp & 0xF)) || (10 == (curOp & 0xF)))
{
dest[1] = temp;
}
}
else
{
switch (curOp & 0xF)
{
case 6: // ADD
temp = getAdd(op1 & 0xFFFFFFFFLL, op2 & 0xFFFFFFFFLL, 0U);
break;
case 7: // SUB
temp = getAdd(op1 & 0xFFFFFFFFLL, (op2 & 0xFFFFFFFFLL) ^ 0xFFFFFFFFLL, CARRY) ^ CARRY;
break;
case 8: // MUL
temp = (op1 & 0xFFFFFFFFLL) * (op2 & 0xFFFFFFFFLL);
if (0U != ((op1 ^ op2 ^ temp) & 0x80000000LL))
{
temp = (temp & 0xFFFFFFFFLL) | OVERFLOW;
}
else
{
temp &= 0xFFFFFFFFLL;
}
break;
case 9: // DIV
if (0U == (op2 & 0xFFFFFFFFLL))
{
temp = INVALID | frame.alupc;
dest[1] = temp;
}
else
{
if (0U != (op1 & NEGATIVE))
{
op1 |= 0xFFFFFFFF00000000LL;
}
else
{
op1 &= 0xFFFFFFFFLL;
}
if (0U != (op2 & NEGATIVE))
{
op2 |= 0xFFFFFFFF00000000LL;
}
else
{
op2 &= 0xFFFFFFFFLL;
}
temp = (op1 / op2) & 0xFFFFFFFFLL;
BELT_T temp2 = (op1 % op2) & 0xFFFFFFFFLL;
temp2 |= getZero(temp2);
dest[1] = temp2;
}
break;
case 10: // UDIV
if (0U == (op2 & 0xFFFFFFFFLL))
{
temp = INVALID | frame.alupc;
dest[1] = temp;
}
else
{
temp = ((op1 & 0xFFFFFFFFLL) / (op2 & 0xFFFFFFFFLL)) & 0xFFFFFFFFLL;
BELT_T temp2 = ((op1 & 0xFFFFFFFFLL) % (op2 & 0xFFFFFFFFLL)) & 0xFFFFFFFFLL;
temp2 |= getZero(temp2);
dest[1] = temp2;
}
break;
case 11: // SHR
if (0U == (op2 & NEGATIVE)) // op2 is positive, so shift is right
{
if (0 != (op2 & 0x7FFFFFFFLL))
{
if (33U <= (op2 & 0x7FFFFFFFLL))
{
temp = 0U;
}
else
{
temp = ((op1 & 0xFFFFFFFFLL) >> ((op2 & 0xFFFFFFFFLL) - 1)) & 0xFFFFFFFFLL;
BELT_T out = temp & 1;
temp >>= 1;
if (1 == out)
{
temp |= CARRY;
}
}
}
else
{
temp = op1 & 0xFFFFFFFFLL;
}
}
else
{
if (33U <= (-op2 & 0x7FFFFFFFLL))
{
temp = 0U;
}
else
{
temp = ((op1 & 0xFFFFFFFFLL) << (-op2 & 0xFFFFFFFFLL)) & 0x1FFFFFFFFLL;
}
}
break;
case 12: // ASHR
if (0U == (op2 & NEGATIVE)) // op2 is positive, so shift is right
{
if (32U <= (op2 & 0x7FFFFFFFLL))
{
if (0U == (op1 & NEGATIVE))
{
temp = 0U;
}
else
{
temp = 0xFFFFFFFFLL;
}
}
else
{
if (0U != (op1 & NEGATIVE))
{
op1 |= 0xFFFFFFFF00000000LL;
}
else
{
op1 &= 0xFFFFFFFFLL;
}
temp = (op1 >> (op2 & 0xFFFFFFFFLL)) & 0xFFFFFFFFLL;
}
}
else // Standard shift left.
{
if (32U <= (-op2 & 0x7FFFFFFFLL))
{
temp = 0U;
}
else
{
temp = ((op1 & 0xFFFFFFFFLL) << (-op2 & 0xFFFFFFFFLL)) & 0xFFFFFFFFLL;
}
}
break;
case 13: // AND
temp = (op1 & op2) & 0xFFFFFFFFLL;
break;
case 14: // OR
temp = (op1 | op2) & 0xFFFFFFFFLL;
break;
case 15: // XOR
temp = (op1 ^ op2) & 0xFFFFFFFFLL;
break;
}
temp |= getZero(temp);
dest[0] = temp;
}
}
else
{
BELT_T* dest = retire.fast;
if (curOp & 0x20)
{
dest = retire.slow;
}
BELT_T op1 = getBeltContent(frame, (curOp >> 10) & 0x3F);
BELT_T op2 = getBeltContent(frame, (curOp >> 16) & 0x3F);
BELT_T op3 = getBeltContent(frame, (curOp >> 22) & 0x3F);
BELT_T temp;
retire.nops = (curOp >> 28) & 0x7;
switch (curOp & 0x1F)
{
case 0: // NOP
break;
case 1: // ADDC
if (false == extraNumerical(op1, op2, temp))
{
temp = getAdd(op1 & 0xFFFFFFFFLL, op2 & 0xFFFFFFFFLL, op3);
temp |= getZero(temp);
}
dest[0] = temp;
break;
case 2: // SUBB
if (false == extraNumerical(op1, op2, temp))
{
temp = getAdd(op1 & 0xFFFFFFFFLL, (op2 & 0xFFFFFFFFLL) ^ 0xFFFFFFFFLL, op3 ^ CARRY) ^ CARRY;
temp |= getZero(temp);
}
dest[0] = temp;
break;
case 3: // MULL
if (true == extraNumerical(op1, op2, temp))
{
dest[0] = temp;
dest[1] = temp;
}
else
{
temp = (op1 & 0xFFFFFFFFLL) * (op2 & 0xFFFFFFFFLL);
BELT_T temp1 = temp & 0xFFFFFFFFLL;
BELT_T temp2 = (temp >> 32) & 0xFFFFFFFFLL;
temp1 |= getZero(temp1);
temp2 |= getZero(temp2);
dest[0] = temp1;
dest[1] = temp2;
}
break;
case 4: // DIVL
if (true == extraNumerical(op1, op2, op3, temp))
{
dest[0] = temp;
dest[1] = temp;
}
else
{
if (0U == (op3 & 0xFFFFFFFFLL))
{
temp = INVALID | frame.alupc;
dest[0] = temp;
dest[1] = temp;
}
else
{
temp = static_cast<unsigned long long>((op1 << 32) | (op2 & 0xFFFFFFFFLL)) / (op3 & 0xFFFFFFFFLL);
BELT_T temp2 = static_cast<unsigned long long>((op1 << 32) | (op2 & 0xFFFFFFFFLL)) % (op3 & 0xFFFFFFFFLL);
if (temp > 0xFFFFFFFFLL)
{
temp = (temp & 0xFFFFFFFFLL) | OVERFLOW;
}
temp |= getZero(temp);
temp2 |= getZero(temp2);
dest[0] = temp;
dest[1] = temp2;
}
}
break;
case 5: // PICK?
if (conditionTrue((curOp >> 6) & 0xF, op1))
{
dest[0] = op2;
}
else
{
dest[0] = op3;
}
break;
case 16: // RAISE INVALID OPERATION
case 17: // RAISE INVALID OPERATION
case 18: // RAISE INVALID OPERATION
case 19: // RAISE INVALID OPERATION
case 20: // RAISE INVALID OPERATION
case 21: // RAISE INVALID OPERATION
std::printf("Terminate initiated due to invalid operation in ALU slot: %d %d\n", static_cast<int>(slot), static_cast<int>(frame.alupc + slot));
machine->invalidOp = true;
break;
}
}
}
// Signal that we have ended this cycle.
pthread_barrier_wait(synchronizer);
}
}
};
class FlowUnit : public FunctionalUnit
{
public:
void fillBelt(Frame& frame, int num)
{
int memOff = -1; // Start at the current instruction
BELT_T cur = 0U; // If cur is used uninitialized, that is a bug in the compiler.
FlowRetire& retire = frame.flow_retire[slot];
for (int i = 0; i < num; ++i)
{
if (0 == (i % 4)) // memOff is intentionally initialized for this to occur at zero
{
--memOff;
cur = getMemory(frame.flowpc - slot + memOff);
if (0U != (cur & INVALID))
{
machine->invalidOp = true;
}
if (0x10 != (cur & 0x1F)) // Make sure this is an ARGS NOP
{
machine->invalidOp = true;
}
}
retire.belt[i] = getBeltContent(frame, (cur >> (5 + 6 * (i % 4))) & 0x3F);
}
}
static void serviceInterrupt(Machine& machine, int /*serviceCode*/, const BELT_T* args, BELT_T* rets)
{
switch (args[0] & 0xFFFFFFFFLL)
{
case 1: // request put character, TODO deprecate
putchar(args[1]);
break;
case 2: // request get character, TODO deprecate
rets[0] = getchar() & 0xFFFFFFFFLL;
rets[0] |= getZero(rets[0]);
break;
case 3: // request stop
machine.stop = true;
break;
case 4: // gestalt : currently return zero
rets[0] = ZERO;
break;
default: // INVALID OPERATION
std::printf("Terminate initiated due to invalid interrupt: %lld\n", args[0]);
machine.invalidOp = true;
break;
}
}
virtual void doStuff()
{
for (;;)
{
// Wait for the start of the instruction cycle
pthread_barrier_wait(synchronizer);
// Do we need to die?
if (true == machine->terminate)
{
// std::printf("Terminating Flow slot: %lu\n", slot);
break;
}
// Interpret and execute the next operation.
Frame& frame = machine->frames.back();
FlowRetire& retire = frame.flow_retire[slot];
retire.flush(); // Make retire station is clean.
if (0U == frame.flownop)
{
// std::printf("Executing Flow slot: %lu %lu\n", slot, frame.flowpc);
BELT_T curOp = getMemory(frame.flowpc - slot - 1U);
if (0U != (curOp & INVALID))
{
std::printf("Terminate initiated due to invalid operation in Flow slot: %d %d\n", static_cast<int>(slot), static_cast<int>(frame.flowpc - slot - 1U));
machine->invalidOp = true;
}
BELT_T cond, src, num, op1, op2, temp;
BELT_T* dest;
cond = (curOp >> 5) & 0xF;
src = getBeltContent(frame, (curOp >> 9) & 0x3F);
num = (curOp >> 15) & 0x3F;
op1 = getBeltContent(frame, num);
op2 = getBeltContent(frame, (curOp >> 21) & 0x3F);
dest = retire.fast;
if (curOp & 0x10)
{
dest = &retire.slow;
}
switch (curOp & 0xF)
{
case 0: // NOP
retire.nops = (curOp >> 29) & 0x7;
break;
case 1: // JMP
if ((0U != (op1 & TRANSIENT)) && conditionTrue(cond, src))
{
if (0U == (op1 & INVALID))
{
retire.jump = ((op1 & 0xFFFFFFFFLL) + frame.entryPoint) & 0xFFFFFFFFLL;
if (0U == retire.jump)
{
std::printf("Terminate initiated due to branch to zero in Flow slot: %d %d\n", static_cast<int>(slot), static_cast<int>(frame.flowpc - slot - 1U));
machine->invalidOp = true;
}
}
else
{
std::printf("Terminate initiated due to branch to invalid in Flow slot: %d %d\n", static_cast<int>(slot), static_cast<int>(frame.flowpc - slot - 1U));
machine->invalidOp = true;
}
}
retire.nops = (curOp >> 27) & 0x7;
break;
case 2: // LD
if (conditionTrue(cond, src))
{
if (false == extraNumerical(op1, temp))
{
temp = getMemory(op1 & 0xFFFFFFFFLL);
if (0U == (temp & INVALID))
{
temp |= getZero(temp);
}
else
{
temp |= frame.flowpc;
}
}
dest[0] = temp;
}
else
{
dest[0] = TRANSIENT | frame.flowpc;
}
retire.nops = (curOp >> 27) & 0x7;
break;
case 3: // LDH
if (conditionTrue(cond, src))
{
if (false == extraNumerical(op1, temp))
{
temp = getMemory((op1 & 0xFFFFFFFFLL) >> 1);
if (0U == (temp & INVALID))
{
temp >>= 16 * (op1 & 1);
if (0U != (temp & 0x8000))
{
temp |= 0xFFFF0000LL;
}
else
{
temp &= 0xFFFF;
}
temp |= getZero(temp);
}
else
{
temp |= frame.flowpc;
}
}
dest[0] = temp;
}
else
{
dest[0] = TRANSIENT | frame.flowpc;
}
retire.nops = (curOp >> 27) & 0x7;
break;
case 4: // LDB
if (conditionTrue(cond, src))
{
if (false == extraNumerical(op1, temp))
{