-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1422 lines (1364 loc) · 61 KB
/
main.cpp
File metadata and controls
1422 lines (1364 loc) · 61 KB
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
#include <assert.h>
#include <iostream>
#include <regex>
#include <fstream>
#include <iomanip>
#include <map>
#include <set>
#include <sstream>
#include <vector>
#include <bitset>
#include <iostream>
#include <filesystem>
#include <array>
#define ARGS_COUNT_CHECK(N,DESC, LINE_SRC) if (args.size() != N) { \
error = true; \
std::cout << "\033[91mERROR: " << LINE_SRC << " Invalid number of arguments for " << DESC << "\033[0m" << std::endl; \
return i; \
}
class Memory;
static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__, "Host byte order must be little endian");
// Big Endian MIPS Simulator
std::vector<std::string> split(std::string str,std::string delim) {
std::vector<std::string> tokens;
size_t prev = 0, pos = 0;
do {
pos = str.find(delim, prev);
if (pos == std::string::npos) pos = str.length();
std::string token = str.substr(prev, pos-prev);
if (!token.empty()) tokens.push_back(token);
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());
return tokens;
}
std::string parse_string(std::string str) {
// \ is escape character followed by octal number (3 digits) or any character
std::string out;
for (size_t i = 0; i < str.size(); i++) {
if (str[i] == '\\') {
std::string escape = str.substr(i, 4);
// Check if excape is octal
if(escape.size() == 4 && escape.find_first_not_of("01234567", 1) == std::string::npos) {
out += (char)std::stoi(escape.substr(1), nullptr, 8);
i += 3;
} else {
out += str[i + 1];
i++;
}
} else {
out += str[i];
}
}
return out;
}
struct SourceGraphNode {
std::string file;
int start;
int end;
bool present = false;
std::vector<std::shared_ptr<SourceGraphNode>> children;
std::weak_ptr<SourceGraphNode> parent;
};
std::string getInclusionPath(SourceGraphNode& src_graph) {
std::string path = src_graph.file;
if (src_graph.parent.expired()) {
return path;
}
return getInclusionPath(*src_graph.parent.lock()) + " -> " + path;
}
std::string getDescriptionForLine(SourceGraphNode& src_graph, unsigned line_num, std::string line) {
// determine which file it is in
std::string path = src_graph.file;
unsigned lower = src_graph.start;
int child_lines = 0;
for (auto child : src_graph.children) {
if (line_num >= child->start && line_num < child->end) {
return getDescriptionForLine(*child, line_num, line);
}
if(line_num > child->start) {
child_lines += child->end - child->start - 1; // 1 to include for %include line
}
}
return path + ":" + std::to_string(line_num - lower - child_lines + 1) + " : " + line;
}
std::map<std::string, unsigned> reg_name = {
{"$zero", 0},
{"$at", 1},
{"$v0", 2},
{"$v1", 3},
{"$a0", 4},
{"$a1", 5},
{"$a2", 6},
{"$a3", 7},
{"$t0", 8},
{"$t1", 9},
{"$t2", 10},
{"$t3", 11},
{"$t4", 12},
{"$t5", 13},
{"$t6", 14},
{"$t7", 15},
{"$s0", 16},
{"$s1", 17},
{"$s2", 18},
{"$s3", 19},
{"$s4", 20},
{"$s5", 21},
{"$s6", 22},
{"$s7", 23},
{"$t8", 24},
{"$t9", 25},
{"$k0", 26},
{"$k1", 27},
{"$gp", 28},
{"$sp", 29},
{"$fp", 30},
{"$ra", 31},
{"$0", 0},
{"$1", 1},
{"$2", 2},
{"$3", 3},
{"$4", 4},
{"$5", 5},
{"$6", 6},
{"$7", 7},
{"$8", 8},
{"$9", 9},
{"$10", 10},
{"$11", 11},
{"$12", 12},
{"$13", 13},
{"$14", 14},
{"$15", 15},
{"$16", 16},
{"$17", 17},
{"$18", 18},
{"$19", 19},
{"$20", 20},
{"$21", 21},
{"$22", 22},
{"$23", 23},
{"$24", 24},
{"$25", 25},
{"$26", 26},
{"$27", 27},
{"$28", 28},
{"$29", 29},
{"$30", 30},
{"$31", 31}
};
unsigned symbol_to_imm(std::string symbol, std::map<std::string, unsigned>& labels,bool& error, std::string line_src) {
// Check for %hi(symbol) or %lo(symbol)
if (symbol.find("%hi(") == 0) {
symbol = symbol.substr(4, symbol.size() - 5);
if (labels.find(symbol) == labels.end()) {
error = true;
std::cout << "\033[91mERROR: " << line_src << " Unknown symbol in %hi(..)" << symbol << "\033[0m" << std::endl;
}
// if bit 15 is set, add 1 to the upper 16 bits to account for sign extension of %lo
if (labels[symbol] & 0x8000) {
return (labels[symbol] >> 16) + 1;
}
return labels[symbol] >> 16;
}
if (symbol.find("%lo(") == 0) {
symbol = symbol.substr(4, symbol.size() - 5);
if (labels.find(symbol) == labels.end()) {
error = true;
std::cout << "\033[91mERROR: " << line_src << " Unknown symbol in %lo(..)" << symbol << "\033[0m" << std::endl;
}
return labels[symbol] & 0xffff;
}
if(symbol.substr(0,2) == "0x") {
return std::stoi(symbol, nullptr, 16);
}
if (symbol.find_first_not_of("0123456789-") == std::string::npos) {
return std::stoi(symbol);
}
if (labels.find(symbol) == labels.end()) {
error = true;
std::cout << "\033[91mERROR: " << line_src << " Unknown symbol " << symbol << "\033[0m" << std::endl;
}
return labels[symbol];
}
unsigned reg_from_str(std::string reg, bool& error, std::string line_src) {
if (reg_name.find(reg) == reg_name.end()) {
std::cout << "\033[91mERROR: " << line_src << " Invalid register " << reg << "\033[0m" << std::endl;
error = true;
return 0;
}
return reg_name[reg];
}
std::string reg_to_str(unsigned reg) {
for (auto it = reg_name.rbegin(); it != reg_name.rend(); it++) {
if (it->second == reg) {
return it->first;
}
}
return "<unknown Reg: " + std::to_string(reg) + ">";
}
std::string imm_to_string(unsigned imm, std::map<std::string, unsigned>& labels) {
if (labels.find(std::to_string(imm)) != labels.end()) {
return std::to_string(labels[std::to_string(imm)]);
}
std::stringstream stream;
stream << std::hex << imm;
return "0x" + stream.str();
}
template <unsigned offset,unsigned length>
class AW32 {
uint32_t* ptr;
public:
AW32(uint32_t* ptr) : ptr(ptr) {}
inline void operator=(uint32_t value) {
uint32_t clr_mask = ((1 << length) - 1) << offset;
uint32_t set_mask = (value & ((1 << length) - 1)) << offset;
*ptr = (*ptr & ~clr_mask) | set_mask;
}
inline operator uint32_t() const {
return (*ptr >> offset) & ((1 << length) - 1);
}
};
/* It really sucks that the C++ standard doesn't guarantee that bitfields are in a specific order
C++20 at least guarantees us that signed integers are twos complement, which is assumed throughout this code
*/
typedef uint32_t Instr;
/*
struct Instr {
union {
uint32_t val;
struct __attribute__ ((packed)) {
unsigned op: 6;
unsigned rs: 5;
unsigned rt: 5;
unsigned rd: 5;
unsigned shamt: 5;
unsigned funct: 6;
} R;
struct __attribute__ ((packed)) {
unsigned op: 6;
unsigned rs: 5;
unsigned rt: 5;
unsigned imm: 16;
} I;
struct __attribute__ ((packed)) {
unsigned op: 6;
unsigned add: 26;
} J;
} __attribute__ ((packed));
Instr(uint32_t i) {
R.op = i >> 26;
R.rs = (i >> 21) & 0x1f;
R.rt = (i >> 16) & 0x1f;
R.rd = (i >> 11) & 0x1f;
R.shamt = (i >> 6) & 0x1f;
R.funct = i & 0x3f;
}
uint32_t to_32() const {
return (R.op << 26) | (R.rs << 21) | (R.rt << 16) | (R.rd << 11) | (R.shamt << 6) | R.funct;
}
} __attribute__ ((packed));*/
#define INSTR_OP(X) (uint32_t)((uint32_t)(X) >> 26)
#define INSTR_RS(X) (uint32_t)(((uint32_t)(X) >> 21) & 0x1f)
#define INSTR_RT(X) (uint32_t)(((uint32_t)(X) >> 16) & 0x1f)
#define INSTR_RD(X) (uint32_t)(((uint32_t)(X) >> 11) & 0x1f)
#define INSTR_SHAMT(X) (uint32_t)(((uint32_t)(X) >> 6) & 0x1f)
#define INSTR_FUNCT(X) (uint32_t)((X) & 0x3f)
#define INSTR_IMM(X) (uint32_t)((X) & 0xffff)
#define INSTR_ADD(X) (uint32_t)((X) & 0x3ffffff)
#define INSTR_W_OP(X, V) (X) = ((X) & 0x3ffffff) | ((V) << 26)
#define INSTR_W_RS(X, V) (X) = ((X) & 0xff07ffff) | ((V) << 21)
#define INSTR_W_RT(X, V) (X) = ((X) & 0xffff07ff) | ((V) << 16)
#define INSTR_W_RD(X, V) (X) = ((X) & 0xfffff07f) | ((V) << 11)
#define INSTR_W_SHAMT(X, V) (X) = ((X) & 0xffffffc1) | ((V) << 6)
#define INSTR_W_FUNCT(X, V) (X) = ((X) & 0xffffffc0) | (V)
#define INSTR_W_IMM(X, V) (X) = ((X) & 0xffff0000) | ((V) & 0xffff)
#define INSTR_W_ADD(X, V) (X) = ((X) & 0xfc000000) | ((V) & 0x3ffffff)
static_assert(sizeof(Instr) == 4, "Invalid size for Instr");
class Memory {
public:
unsigned start_addr = 0;
unsigned size_bytes = 0;
Memory(unsigned start, unsigned size) {
start_addr = start;
size_bytes = size;
}
unsigned size() {
return size_bytes;
}
virtual void w_32(uint32_t addr, uint32_t value) {
assert(addr + 3 < size());
// Big Endian
w_8(addr,(value >> 24) & 0xff);
w_8(addr + 1,(value >> 16) & 0xff);
w_8(addr + 2, (value >> 8) & 0xff);
w_8(addr + 3, value & 0xff);
}
virtual void w_16(uint32_t addr, uint16_t value) {
assert(addr + 1 < size());
w_8(addr, (value >> 8) & 0xff);
w_8(addr + 1, value & 0xff);
}
virtual uint32_t r_32(uint32_t addr) {
assert(addr + 3 < size());
return (r_8(addr) << 24) | (r_8(addr + 1) << 16) | (r_8(addr + 2) << 8) | r_8(addr + 3);
}
virtual uint16_t r_16(uint32_t addr) {
assert(addr + 1 < size());
return (r_8(addr) << 8) | r_8(addr + 1);
}
virtual uint32_t r_8(uint32_t addr) = 0;
virtual void w_8(uint32_t addr, uint8_t value) = 0;
};
class RamMemory : public Memory {
std::vector<uint8_t> mem;
public:
RamMemory(unsigned start, unsigned size) : Memory(start, size) {
mem.resize(size);
std::fill(mem.begin(), mem.end(), 0);
}
virtual uint32_t r_8(uint32_t addr) {
assert(addr < size());
return mem[addr];
}
virtual void w_8(uint32_t addr, uint8_t value) {
assert(addr < size());
mem[addr] = value & 0xff;
}
};
namespace Assembler {
std::string disassemble(Instr i, std::map<std::string, unsigned>& labels) {
std::string mnemonic;
switch (INSTR_OP(i)) {
case 0x0: // R-type
switch (INSTR_FUNCT(i)) {
case 0x20: // add
return "add " + reg_to_str(INSTR_RD(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + reg_to_str(INSTR_RT(i));
case 0x21: // addu
return "addu " + reg_to_str(INSTR_RD(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + reg_to_str(INSTR_RT(i));
case 0x24: // and
return "and " + reg_to_str(INSTR_RD(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + reg_to_str(INSTR_RT(i));
case 0x08: // jr
return "jr " + reg_to_str(INSTR_RS(i));
case 0x27: // nor
return "nor " + reg_to_str(INSTR_RD(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + reg_to_str(INSTR_RT(i));
case 0x25: // or
return "or " + reg_to_str(INSTR_RD(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + reg_to_str(INSTR_RT(i));
case 0x2a: // slt
return "slt " + reg_to_str(INSTR_RD(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + reg_to_str(INSTR_RT(i));
case 0x00: // sll
return "sll " + reg_to_str(INSTR_RD(i)) + ", " + reg_to_str(INSTR_RT(i)) + ", " + std::to_string(INSTR_SHAMT(i));
case 0x02: // srl
return "srl " + reg_to_str(INSTR_RD(i)) + ", " + reg_to_str(INSTR_RT(i)) + ", " + std::to_string(INSTR_SHAMT(i));
case 0x22: // sub
return "sub " + reg_to_str(INSTR_RD(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + reg_to_str(INSTR_RT(i));
case 0x23: // subu
return "subu " + reg_to_str(INSTR_RD(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + reg_to_str(INSTR_RT(i));
default: return "<unknown funct> : " + std::to_string(INSTR_FUNCT(i));
}
break;
case 0x08: // addi
return "addi " + reg_to_str(INSTR_RT(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + imm_to_string(INSTR_IMM(i), labels);
case 0x09: // addiu
return "addiu " + reg_to_str(INSTR_RT(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + imm_to_string(INSTR_IMM(i), labels);
case 0x0c: // andi
return "andi " + reg_to_str(INSTR_RT(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + imm_to_string(INSTR_IMM(i), labels);
case 0x04: // beq
return "beq " + reg_to_str(INSTR_RS(i)) + ", " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels);
case 0x05: // bne
return "bne " + reg_to_str(INSTR_RS(i)) + ", " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels);
case 0x02: // j
return "j " + imm_to_string(INSTR_ADD(i) << 2, labels);
case 0x03: // jal
return "jal " + imm_to_string(INSTR_ADD(i) << 2, labels);
case 0x20: // lb
return "lb " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels) + "(" + reg_to_str(INSTR_RS(i)) + ")";
case 0x24: // lbu
return "lbu " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels) + "(" + reg_to_str(INSTR_RS(i)) + ")";
case 0x25: // lhu
return "lhu " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels) + "(" + reg_to_str(INSTR_RS(i)) + ")";
case 0x30: // ll
return "ll " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels) + "(" + reg_to_str(INSTR_RS(i)) + ")";
case 0x0f: // lui
return "lui " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels);
case 0x23: // lw
return "lw " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels) + "(" + reg_to_str(INSTR_RS(i)) + ")";
case 0x0d: // ori
return "ori " + reg_to_str(INSTR_RT(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + imm_to_string(INSTR_IMM(i), labels);
case 0x0a: // slti
return "slti " + reg_to_str(INSTR_RT(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + imm_to_string(INSTR_IMM(i), labels);
case 0x0b: // sltiu
return "sltiu " + reg_to_str(INSTR_RT(i)) + ", " + reg_to_str(INSTR_RS(i)) + ", " + imm_to_string(INSTR_IMM(i), labels);
case 0x28: // sb
return "sb " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels) + "(" + reg_to_str(INSTR_RS(i)) + ")";
case 0x38: // sc
return "sc " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels) + "(" + reg_to_str(INSTR_RS(i)) + ")";
case 0x29: // sh
return "sh " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels) + "(" + reg_to_str(INSTR_RS(i)) + ")";
case 0x2b: // sw
return "sw " + reg_to_str(INSTR_RT(i)) + ", " + imm_to_string(INSTR_IMM(i), labels) + "(" + reg_to_str(INSTR_RS(i)) + ")";
default: return "<unknown opcode> : " + std::to_string(INSTR_OP(i));
}
return mnemonic;
}
Instr assembleInstr(unsigned addr,std::string mnemonic, std::vector<std::string> args, bool& error, std::map<std::string, unsigned>& labels, std::string line_src) {
Instr i = (Instr){0};
if (mnemonic == "li") {
ARGS_COUNT_CHECK(2, "li",line_src);
INSTR_W_OP(i,0x08);
INSTR_W_RS(i,0);
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[1],labels, error,line_src));
//assert(INSTR_IMM(i) < 0b0111111111111111); // the immediate value can not be 1 at start (sign extension)
std::cout << "\033[33mWARNING: " << line_src << " Using li as an alias for addi " << reg_to_str(INSTR_RT(i)) << ", $0, " << imm_to_string(INSTR_IMM(i), labels) << "\033[0m"<< std::endl;
} else if (mnemonic == "nop") {
ARGS_COUNT_CHECK(0, "nop",line_src);
std::cout << "\033[33mWARNING: " << line_src << " Using nop as an alias for sll $zero, $zero, 0\033[0m" << std::endl;
} else if (mnemonic == "move") {
std::cout << "\033[33mWARNING: " << line_src << " Using move as an alias for add $1, $2, $0\033[0m" << std::endl;
ARGS_COUNT_CHECK(2, "move",line_src);
INSTR_W_FUNCT(i,0x20);
INSTR_W_RD(i,reg_from_str(args[0],error,line_src));
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
}
else if (mnemonic == "add") {
ARGS_COUNT_CHECK(3, "add",line_src);
INSTR_W_OP(i,0x0);
INSTR_W_FUNCT(i,0x20);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[2],error,line_src));
INSTR_W_RD(i,reg_from_str(args[0],error,line_src));
} else if (mnemonic == "addi") {
ARGS_COUNT_CHECK(3, "addi",line_src);
INSTR_W_OP(i,0x08);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "addiu") {
ARGS_COUNT_CHECK(3, "addiu",line_src);
INSTR_W_OP(i,0x09);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "addu") {
ARGS_COUNT_CHECK(3, "addu",line_src);
INSTR_W_OP(i,0x0);
INSTR_W_FUNCT(i,0x21);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[2],error,line_src));
INSTR_W_RD(i,reg_from_str(args[0],error,line_src));
}
else if (mnemonic == "andi") {
ARGS_COUNT_CHECK(3, "andi",line_src);
INSTR_W_OP(i,0x0c);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "beq") {
ARGS_COUNT_CHECK(3, "beq",line_src);
INSTR_W_OP(i,0x04);
INSTR_W_RS(i,reg_from_str(args[0],error,line_src));
INSTR_W_RT(i,reg_from_str(args[1],error,line_src));
unsigned target = symbol_to_imm(args[2],labels, error,line_src);
INSTR_W_IMM(i,(target - addr - 4) >> 2);
} else if (mnemonic == "bne") {
ARGS_COUNT_CHECK(3, "bne",line_src);
INSTR_W_OP(i,0x05);
INSTR_W_RS(i,reg_from_str(args[0],error,line_src));
INSTR_W_RT(i,reg_from_str(args[1],error,line_src));
unsigned target = symbol_to_imm(args[2],labels, error,line_src);
INSTR_W_IMM(i,(target - addr - 4) >> 2);
} else if (mnemonic == "j" || mnemonic == "b") {
ARGS_COUNT_CHECK(1, "j",line_src);
if(mnemonic == "b") {
std::cout << "\033[33mWARNING: Using b as an alias for j\033[0m" << std::endl;
}
INSTR_W_OP(i,0x02);
unsigned add = symbol_to_imm(args[0],labels, error,line_src);
if(add % 4) {
std::cout << "\033[91mERROR: " << line_src << " Jump address is not word aligned\033[0m" << std::endl;
error = true;
}
INSTR_W_ADD(i,add >> 2);
} else if (mnemonic == "jr") {
ARGS_COUNT_CHECK(1, "jr",line_src);
INSTR_W_OP(i,0x0);
INSTR_W_FUNCT(i,0x08);
INSTR_W_RS(i,reg_from_str(args[0],error,line_src));
} else if (mnemonic == "jal") {
ARGS_COUNT_CHECK(1, "jal",line_src);
INSTR_W_OP(i,0x03);
auto addr = symbol_to_imm(args[0],labels, error,line_src);
if(addr & 0x3) {
std::cout << "\033[33mWARNING: Jump address is not word aligned\033[0m" << std::endl;
}
INSTR_W_ADD(i,symbol_to_imm(args[0],labels, error,line_src) >> 2);
} else if (mnemonic == "lbu") {
ARGS_COUNT_CHECK(3, "lbu",line_src);
INSTR_W_OP(i,0x24);
INSTR_W_RS(i,reg_from_str(args[2],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[1],labels, error,line_src));
} else if (mnemonic == "lb") {
ARGS_COUNT_CHECK(3, "lb",line_src);
INSTR_W_OP(i,0x20);
INSTR_W_RS(i,reg_from_str(args[2],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[1],labels, error,line_src));
std::cout << "\033[33mWARNING: " << line_src << " Using lb even though it is not a core instruction\033[0m" << std::endl;
} else if (mnemonic == "lhu") {
ARGS_COUNT_CHECK(3, "lhu",line_src);
INSTR_W_OP(i,0x25);
INSTR_W_RS(i,reg_from_str(args[2],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[1],labels, error,line_src));
} else if (mnemonic == "ll") {
ARGS_COUNT_CHECK(3, "ll",line_src);
INSTR_W_OP(i,0x30);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "lui") {
ARGS_COUNT_CHECK(2, "lui",line_src);
INSTR_W_OP(i,0x0f);
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[1],labels, error,line_src));
} else if (mnemonic == "lw") {
ARGS_COUNT_CHECK(3, "lw",line_src);
INSTR_W_OP(i,0x23);
INSTR_W_RS(i,reg_from_str(args[2],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[1],labels, error,line_src));
} else if (mnemonic == "ori") {
ARGS_COUNT_CHECK(3, "ori",line_src);
INSTR_W_OP(i,0x0d);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "slti") {
ARGS_COUNT_CHECK(3, "slti",line_src);
INSTR_W_OP(i,0x0a);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "sltiu") {
ARGS_COUNT_CHECK(3, "sltiu",line_src);
INSTR_W_OP(i,0x0b);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "sb") {
ARGS_COUNT_CHECK(3, "sb",line_src);
INSTR_W_OP(i,0x28);
INSTR_W_RS(i,reg_from_str(args[2],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[1],labels, error,line_src));
} else if (mnemonic == "sc") {
ARGS_COUNT_CHECK(3, "sc",line_src);
INSTR_W_OP(i,0x38);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "sh") {
ARGS_COUNT_CHECK(3, "sh",line_src);
INSTR_W_OP(i,0x29);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "sw") {
ARGS_COUNT_CHECK(3, "sw",line_src);
INSTR_W_OP(i,0x2b);
INSTR_W_RS(i,reg_from_str(args[2],error,line_src));
INSTR_W_RT(i,reg_from_str(args[0],error,line_src));
INSTR_W_IMM(i,symbol_to_imm(args[1],labels, error,line_src));
} else if (mnemonic == "sub") {
ARGS_COUNT_CHECK(3, "sub",line_src);
INSTR_W_OP(i,0x0);
INSTR_W_FUNCT(i,0x22);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[2],error,line_src));
INSTR_W_RD(i,reg_from_str(args[0],error,line_src));
} else if (mnemonic == "subu") {
ARGS_COUNT_CHECK(3, "subu",line_src);
INSTR_W_OP(i,0x0);
INSTR_W_FUNCT(i,0x23);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[2],error,line_src));
INSTR_W_RD(i,reg_from_str(args[0],error,line_src));
} else if (mnemonic == "nor") {
ARGS_COUNT_CHECK(3, "nor",line_src);
INSTR_W_OP(i,0x0);
INSTR_W_FUNCT(i,0x27);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[2],error,line_src));
INSTR_W_RD(i,reg_from_str(args[0],error,line_src));
} else if (mnemonic == "or") {
ARGS_COUNT_CHECK(3, "or",line_src);
INSTR_W_OP(i,0x0);
INSTR_W_FUNCT(i,0x25);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[2],error,line_src));
INSTR_W_RD(i,reg_from_str(args[0],error,line_src));
} else if (mnemonic == "sll") {
ARGS_COUNT_CHECK(3, "sll",line_src);
INSTR_W_OP(i,0x0);
INSTR_W_FUNCT(i,0x00);
INSTR_W_RT(i,reg_from_str(args[1],error,line_src));
INSTR_W_RD(i,reg_from_str(args[0],error,line_src));
INSTR_W_SHAMT(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "srl") {
ARGS_COUNT_CHECK(3, "srl",line_src);
INSTR_W_OP(i,0x0);
INSTR_W_FUNCT(i,0x02);
INSTR_W_RT(i,reg_from_str(args[1],error,line_src));
INSTR_W_RD(i,reg_from_str(args[0],error,line_src));
INSTR_W_SHAMT(i,symbol_to_imm(args[2],labels, error,line_src));
} else if (mnemonic == "slt") {
ARGS_COUNT_CHECK(3, "slt",line_src);
INSTR_W_OP(i,0x0);
INSTR_W_FUNCT(i,0x2a);
INSTR_W_RS(i,reg_from_str(args[1],error,line_src));
INSTR_W_RT(i,reg_from_str(args[2],error,line_src));
INSTR_W_RD(i,reg_from_str(args[0],error,line_src));
} else {
error = true;
std::cout << "\033[91mERROR: " << line_src << " Invalid mnemonic " << mnemonic << "\033[0m" << std::endl;
}
//std::cout << "Assembled " << mnemonic << " to " << std::bitset<6>(i.R.op) << " " << std::bitset<5>(INSTR_RS(i)) << " " << std::bitset<5>(INSTR_RT(i)) << " " << std::bitset<5>(INSTR_RD(i)) << " " << std::bitset<5>(INSTR_SHAMT(i)) << " " << std::bitset<6>(INSTR_FUNCT(i))
//<< " Bin:" << std::bitset<32>(i.to_32()) << std::endl;
return i;
}
std::shared_ptr<Memory> assemble(std::vector<std::string> lines, uint32_t code_start ,std::map<std::string, uint32_t>& labels,bool& error, SourceGraphNode& source) {
auto prog = std::make_shared<RamMemory>(code_start, 2048);
std::regex r_label("^[\\s]*([\\$a-zA-Z0-9_]+):[\\s]*$");
std::regex r_define("^[\\s]*%define[\\s]+([a-zA-Z0-9_]+)[\\s]+([a-zA-Z0-9_]+)[\\s]*$");
std::regex r_dot("^[\\s]*\\.(word|ascii)[\\s]+(.*)$");
std::regex r_mnm(R"([\s]*([a-zA-Z]+)[\s]*$)");
std::regex r_mnm_reg_reg(R"([\s]*([a-zA-Z]+)[\s]*(\$[a-zA-Z0-9]+)[\s]*,[\s]*(\$[a-zA-Z0-9]+)[\s]*$)");
std::regex r_mnm_reg_num(R"([\s]*([a-zA-Z]+)[\s]+(\$[a-zA-Z0-9]+)[\s]*,[\s]*(\%lo\([a-zA-Z0-9_]+\)|(\%hi\([a-zA-Z0-9_]+\)|[a-zA-Z0-9_])+))");
std::regex r_mnm_reg_imm_reg(R"([\s]*([a-zA-Z]+)[\s]+(\$[a-zA-Z0-9]+)[\s]*,[\s]*(\%(?:lo|hi)\([a-zA-Z_]+\)|[a-zA-Z0-9_]+)\(([$a-zA-Z0-9]+)\))");
std::regex r_mnm_reg_reg_reg(R"([\s]*([a-zA-Z]+)[\s]+(\$[a-zA-Z0-9]+)[\s]*,[\s]*(\$[a-zA-Z0-9]+)[\s]*,[\s]*(\$[a-zA-Z0-9]+))");
std::regex r_mnm_reg_reg_num(R"([\s]*([a-zA-Z]+)[\s]+(\$[a-zA-Z0-9]+)[\s]*,[\s]*(\$[a-zA-Z0-9]+)[\s]*,[\s]*([\-a-zA-Z0-9_]+))");
std::regex r_mnm_num(R"([\s]*([a-zA-Z]+)[\s]+([\$a-zA-Z0-9_]+))");
int line_num = 0;
// 1. Pass: Find labels
int pos = code_start;
for (auto line: lines) {
std::string line_src = getDescriptionForLine(source, line_num, line);
line = line.substr(0, line.find("#"));
line = line.substr(0, line.find_last_not_of(" \t") + 1);
if (line.empty()) {
line_num++;
continue;
}
std::smatch m;
if (std::regex_match(line, m, r_label)) {
labels[m[1]] = code_start + pos;
} else if (std::regex_match(line, m, r_define)) {
labels[m[1]] = symbol_to_imm(m[2], labels, error,line_src);
} else if (std::regex_match(line, m, r_dot)) {
if(m[1] == "word") {
pos += 4;
} else if(m[1] == "ascii") {
std::string s = parse_string(m[2]);
if(s.length() < 2 || s[0] != '"' || s[s.length() - 1] != '"') {
std::cout << "\033[91mERROR(First Pass): " << line_src << " Invalid string format\033[0m" << std::endl;
error = true;
}
s = s.substr(1, s.length() - 2);
pos += parse_string(s).size();
} else {
std::cout << "\033[91mERROR(First Pass): " << line_src << " Invalid directive\033[0m" << std::endl;
error = true;
}
} else if (std::regex_match(line, m, r_mnm_reg_num)) {
pos += 4;
} else if (std::regex_match(line, m, r_mnm_reg_imm_reg)) {
pos += 4;
} else if (std::regex_match(line, m, r_mnm_reg_reg_reg)) {
pos += 4;
} else if (std::regex_match(line, m, r_mnm_num)) {
pos += 4;
} else if (std::regex_match(line, m, r_mnm_reg_reg_num)) {
pos += 4;
} else if (std::regex_match(line, m, r_mnm)) {
pos += 4;
}else if (std::regex_match(line, m, r_mnm_reg_reg)) {
pos += 4;
} else{
std::cout << "\033[91mERROR(First Pass): " << line_src << " Invalid instruction format\033[0m" << std::endl;
error = true;
}
line_num++;
}
// 2. Pass: Assemble instructions
line_num = 0;
unsigned address = code_start;
for (auto line: lines) {
std::string line_src = getDescriptionForLine(source, line_num, line);
line = line.substr(0, line.find("#"));
line = line.substr(0, line.find_last_not_of(" \t") + 1);
if (line.empty()) {
line_num++;
continue;
}
std::smatch m;
if (std::regex_match(line, m, r_label) || std::regex_match(line, m, r_define)) {
// Skip
} else if(std::regex_match(line, m, r_mnm)) {
prog->w_32(address,assembleInstr(address,m[1], {}, error, labels,line_src));
address += 4;
} else if (std::regex_match(line, m, r_dot)) {
assert(m.size() == 3);
if(m[1] == "word") {
prog->w_32(address,symbol_to_imm(m[2], labels, error,line_src));
address += 4;
} else if(m[1] == "ascii") {
std::string s = parse_string(m[2]);
if(s.length() < 2 || s[0] != '"' || s[s.length() - 1] != '"') {
std::cout << "\033[91mERROR(First Pass): " << line_src << " Invalid string format\033[0m" << std::endl;
error = true;
}
s = s.substr(1, s.length() - 2);
for (char c: s) {
prog->w_8(address, c);
address++;
}
} else {
std::cout << "\033[91mERROR: " << line_src << " Invalid directive\033[0m" << std::endl;
error = true;
}
} else if (std::regex_match(line, m, r_mnm_reg_num)) {
prog->w_32(address,assembleInstr(address,m[1], {m[2],m[3]}, error, labels,line_src));
address += 4;
} else if (std::regex_match(line, m, r_mnm_reg_imm_reg)) {
prog->w_32(address,assembleInstr(address,m[1], {m[2],m[3],m[4]}, error, labels,line_src));
address += 4;
} else if (std::regex_match(line, m, r_mnm_reg_reg_reg)) {
prog->w_32(address,assembleInstr(address,m[1], {m[2],m[3],m[4]}, error, labels,line_src));
address += 4;
} else if (std::regex_match(line, m, r_mnm_num)) {
prog->w_32(address,assembleInstr(address,m[1], {m[2]}, error, labels,line_src));
address += 4;
} else if (std::regex_match(line, m, r_mnm_reg_reg_num)) {
prog->w_32(address,assembleInstr(address,m[1], {m[2],m[3],m[4]}, error, labels,line_src));
address += 4;
}else if (std::regex_match(line, m, r_mnm_reg_reg)) {
prog->w_32(address,assembleInstr(address,m[1], {m[2],m[3]}, error, labels,line_src));
address += 4;
} else {
std::cout << "\033[91mERROR: " << line_src << " Invalid instruction format\033[0m" << std::endl;
error = true;
}
line_num++;
}
if (error) {
std::cout << "\033[91mError assembling code\033[0m" << std::endl;
} else {
std::cout << "\033[92mSUCCESS: Assembled code to " << address - code_start << " bytes \033[0m" << std::endl;
}
return prog;
}
};
class MMU {
public:
std::vector<std::shared_ptr<Memory>> memories;
std::set<std::pair<uint32_t,uint32_t>> breakpoints;
bool hit_bp = false;
bool get_bp_and_clear() {
if (hit_bp) {
hit_bp = false;
return true;
}
return false;
}
void add_breakpoint(uint32_t addr, uint32_t len) {
breakpoints.insert({addr, addr + len});
}
bool register_potential_bp_hit(uint32_t base, uint32_t len) {
hit_bp |= isBP(base, len);
return hit_bp;
}
bool isBP(uint32_t base, uint32_t len) {
for (auto& bp: breakpoints) {
if (bp.first < base + len && bp.second > base) {
return true;
}
}
return false;
}
std::shared_ptr<Memory> at(uint32_t addr) {
for (auto& mem: memories) {
if (addr >= mem->start_addr && addr < mem->start_addr + mem->size()) {
return mem;
}
}
return nullptr;
}
public:
void map(std::shared_ptr<Memory> mem) {
memories.push_back(mem);
}
void w_32(uint32_t addr, uint32_t value, bool no_bp = false) {
if(!no_bp) register_potential_bp_hit(addr, 4);
auto mem = at(addr);
if (mem) {
mem->w_32(addr - mem->start_addr, value);
}
}
void w_16(uint32_t addr, uint16_t value, bool no_bp = false) {
if(!no_bp) register_potential_bp_hit(addr, 2);
auto mem = at(addr);
if (mem) {
mem->w_16(addr - mem->start_addr, value);
}
}
void w_8(uint32_t addr, uint8_t value, bool no_bp = false) {
if(!no_bp) register_potential_bp_hit(addr, 1);
auto mem = at(addr);
if(mem) {
mem->w_8(addr - mem->start_addr, value);
}
}
uint32_t r_32(uint32_t addr, bool no_bp = false) {
if(!no_bp) register_potential_bp_hit(addr, 4);
auto mem = at(addr);
if (!mem) {
return 0;
}
return mem->r_32(addr - mem->start_addr);
}
uint32_t r_16(uint32_t addr, bool no_bp = false) {
if(!no_bp) register_potential_bp_hit(addr, 2);
auto mem = at(addr);
if (!mem) {
return 0;
}
return mem->r_16(addr - mem->start_addr);
}
uint32_t r_8(uint32_t addr, bool no_bp = false) {
if(!no_bp) register_potential_bp_hit(addr, 1);
auto mem = at(addr);
if (!mem) {
return 0;
}
return mem->r_8(addr - mem->start_addr);
}
};
class CPU {
public:
std::ostream& dbg = std::cout;
std::array<uint32_t, 32> reg;
MMU mmu;
uint32_t pc = 0;
uint32_t ir = 0;
uint32_t hi;
uint32_t lo;
enum CPU_FEATURES : uint32_t {
CPU_FEATURE_BRANCH_DELAY_SLOT = 0x1
};
uint32_t cpu_features_reg = 0;
CPU() {
std::fill(reg.begin(), reg.end(), 0);
}
static int32_t signExtend16(uint16_t imm) {
if (imm & 0b1000000000000000) {
return imm | 0xffff0000;
}
return imm;
}
static int32_t signExtend8(uint8_t imm) {
if (imm & 0b10000000) {
return imm | 0xffffff00;
}
return imm;
}
inline bool isBranchOrJump(uint32_t instr) {
return INSTR_OP(instr) == 0x02 || INSTR_OP(instr) == 0x03 || INSTR_OP(instr) == 0x04 || INSTR_OP(instr) == 0x05
|| (INSTR_OP(instr) == 0 && (INSTR_FUNCT(instr) == 0x08));
}
void step() {
reg[0] = 0;
Instr instr(mmu.r_32(pc));
uint32_t bdelay_slot_pc = pc + 4;
switch (INSTR_OP(instr)) {
case 0x0: // R-type
switch (INSTR_FUNCT(instr)) {
case 0x20: // add
reg[INSTR_RD(instr)] = reg[INSTR_RS(instr)] + reg[INSTR_RT(instr)];
break;
case 0x21: // addu
reg[INSTR_RT(instr)] = reg[INSTR_RS(instr)] + signExtend16(INSTR_IMM(instr));
break;
case 0x24: // and
reg[INSTR_RD(instr)] = reg[INSTR_RS(instr)] & reg[INSTR_RT(instr)];
break;
case 0x08: // jr
pc = reg[INSTR_RS(instr)] - 4; // -4 because it will be incremented by 4 again
break;
case 0x27: // nor
reg[INSTR_RD(instr)] = ~(reg[INSTR_RS(instr)] | reg[INSTR_RT(instr)]);
break;
case 0x25: // or
reg[INSTR_RD(instr)] = reg[INSTR_RS(instr)] | reg[INSTR_RT(instr)];
break;
case 0x2a: // slt
reg[INSTR_RD(instr)] = reg[INSTR_RS(instr)] < reg[INSTR_RT(instr)];
break;
case 0x00: // sll
reg[INSTR_RD(instr)] = reg[INSTR_RT(instr)] << INSTR_SHAMT(instr);
break;
case 0x02: // srl
reg[INSTR_RD(instr)] = reg[INSTR_RT(instr)] >> INSTR_SHAMT(instr);
break;
case 0x22: // sub
reg[INSTR_RD(instr)] = reg[INSTR_RS(instr)] - reg[INSTR_RT(instr)];
break;
case 0x23: // subu
reg[INSTR_RD(instr)] = reg[INSTR_RS(instr)] - reg[INSTR_RT(instr)];
break;
default:
std::cout << "Invalid funct" << std::endl;
}
break;
case 0x08: // addi
reg[INSTR_RT(instr)] = reg[INSTR_RS(instr)] + signExtend16(INSTR_IMM(instr));
//pc = ir; // Generate overflow exception
break;
case 0x09: // addiu
reg[INSTR_RT(instr)] = reg[INSTR_RS(instr)] + signExtend16(INSTR_IMM(instr));
break;
case 0x0c: // andi
reg[INSTR_RT(instr)] = reg[INSTR_RS(instr)] & INSTR_IMM(instr);
break;
case 0x04: // beq
if (reg[INSTR_RS(instr)] == reg[INSTR_RT(instr)]) {
pc += signExtend16(INSTR_IMM(instr)) << 2;
}
break;
case 0x05: // bne
if (reg[INSTR_RS(instr)] != reg[INSTR_RT(instr)]) {
pc += signExtend16(INSTR_IMM(instr)) << 2;
}
break;
case 0x02: // j
pc = ((pc + 4) & 0xf0000000) | (INSTR_ADD(instr) << 2);
return;
case 0x03: // jal
reg[31] = pc + ((cpu_features_reg & CPU_FEATURE_BRANCH_DELAY_SLOT) ? 8 : 4);
pc = (pc & 0xf0000000) | (INSTR_ADD(instr) << 2);
return;
case 0x24: // lbu
reg[INSTR_RT(instr)] = mmu.r_8(reg[INSTR_RS(instr)] + signExtend16(INSTR_IMM(instr)));
reg[INSTR_RT(instr)] &= 0xff;
break;
case 0x20: // lb
reg[INSTR_RT(instr)] = signExtend8(mmu.r_8(reg[INSTR_RS(instr)] + signExtend16(INSTR_IMM(instr))));
break;
case 0x25: // lhu
reg[INSTR_RT(instr)] = mmu.r_16(reg[INSTR_RS(instr)] + signExtend16(INSTR_IMM(instr) & 0xffff));
reg[INSTR_RT(instr)] &= 0xffff;