-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstruction.cpp
More file actions
783 lines (745 loc) · 24.9 KB
/
instruction.cpp
File metadata and controls
783 lines (745 loc) · 24.9 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
#include <iostream>
#include <stdlib.h>
#include "instruction.hpp"
#include "simulator.hpp"
using namespace std;
//////// opecode start /////////
// RV32I
#define _BRANCH 0b1100011 // beq, bne, blt, bge, bltu, bgeu
#define _LOAD 0b0000011 // lb, lh, lw, lbu, lhu,
#define _STORE 0b0100011 // sb, sh, sw
#define _IMM 0b0010011 // addi, slti, sltiu, xori, ori, andi, slli, srli, srai
#define _OP 0b0110011 // add, sub, sll, slt, sltu, xor, srl, sra, or, and
#define _LUI 0b0110111 // lui
#define _AUIPC 0b0010111 // auipc
#define _JAL 0b1101111 // jal
#define _JALR 0b1100111 // jalr
#define _NOP 0b1111111 // nop
// RV32F
#define _FLW 0b0000111 // flw
#define _FSW 0b0100111 // fsw
#define _FMADDS 0b1000011 // fmadd.s
#define _FMSUBS 0b1000111 // fmsub.s
#define _FNMSUBS 0b1001011 // fnmsub.s
#define _FNMADDS 0b1001111 // fnmadd.s
#define _FOP 0b1010011 // fadd.s, fsub.s, fmul.s, fdiv.s, fsqrt.s, fsgnj.s, fsgnjn.s, fsgnnjx.s,
// fmin.s, fmax.s, fcvt.w.s, fcvt.wu.s, fmv.x.w, feq.s, flt.s, fle.s,
// fclass.s, fcvt.s.w, fcvt.s.wu, fmv.w.x
//////// opecode end /////////
int inst_branch(Emulator* emu, uint32_t instruction) {
// B-type
// beq, bne, blt, bge, bltu, bgeu
// imm1 rs2 rs1 funct3 imm2 opcode
// 7 5 5 3 5 7
uint32_t funct3 = (instruction & 0x00007000) >> 12;
uint32_t imm1 = (instruction & 0xFE000000) >> 20;
uint32_t imm2 = (instruction & 0x00000F80) >> 7;
int imm = imm1 + imm2;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t rs2 = (instruction & 0x01F00000) >> 20;
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
emu->stats.reg_used[rs1]++;
emu->stats.reg_used[rs2]++;
switch (funct3) {
case 0b000 :
BEQ(emu, rs1, rs2, imm);
if (emu->args.print_asm) {
cout << "beq " << reg_name[rs1] << " " << reg_name[rs2] << " " << imm << endl;
}
break;
case 0b100 : // in risc-v 0'b001
BNE(emu, rs1, rs2, imm);
if (emu->args.print_asm) {
cout << "bne " << reg_name[rs1] << " " << reg_name[rs2] << " " << imm << endl;
}
break;
case 0b001 : // in risc-v 0'b100
BLT(emu, rs1, rs2, imm);
if (emu->args.print_asm) {
cout << "blt " << reg_name[rs1] << " " << reg_name[rs2] << " " << imm << endl;
}
break;
case 0b101 :
BGE(emu, rs1, rs2, imm);
if (emu->args.print_asm) {
cout << "bge " << reg_name[rs1] << " " << reg_name[rs2] << " " << imm << endl;
}
break;
// case 0b110 :
// BLTU(emu, rs1, rs2, imm);
// if (DEBUG) cout << "BLTU" << endl;
// break;
// case 0b111 :
// BGEU(emu, rs1, rs2, imm);
// if (DEBUG) cout << "BGEU" << endl;
// break;
default :
cout << "no function matched" << endl;
return 1;
}
return 0;
}
int inst_load(Emulator* emu, uint32_t instruction) {
// I-type
// lb, lh, lw, lbu, lhu
// imm rs1 funct3 rd opcode
// 12 5 3 5 7
uint32_t funct3 = (instruction & 0x00007000) >> 12;
int imm = (instruction & 0xFFF00000) >> 20;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
emu->stats.reg_used[rs1]++;
emu->stats.reg_used[rd]++;
switch (funct3) {
// case 0b000 :
// LB(emu, rs1, rd, imm);
// if (DEBUG) cout << "LB" << endl;
// break;
// case 0b001 :
// LH(emu, rs1, rd, imm);
// if (DEBUG) cout << "LH" << endl;
// break;
case 0b010 :
if (emu->args.print_asm) {
cout << "lw " << reg_name[rd] << " " << reg_name[rs1] << " " << imm << endl;
}
LW(emu, rs1, rd, imm);
break;
// case 0b100 :
// LBU(emu, rs1, rd, imm);
// if (DEBUG) cout << "LBU" << endl;
// break;
// case 0b101 :
// LHU(emu, rs1, rd, imm);
// if (DEBUG) cout << "LHU" << endl;
// break;
default :
cout << "no function matched" << endl;
return 1;
}
return 0;
}
int inst_store(Emulator* emu, uint32_t instruction) {
// S-type
// sb, sh, sw
// imm1 rs2 rs1 funct3 imm2 opcode
// 7 5 5 3 5 7
uint32_t funct3 = (instruction & 0x00007000) >> 12;
uint32_t imm1 = (instruction & 0xFE000000) >> 20;
uint32_t imm2 = (instruction & 0x00000F80) >> 7;
int imm = imm1 + imm2;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t rs2 = (instruction & 0x01F00000) >> 20;
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
emu->stats.reg_used[rs1]++;
emu->stats.reg_used[rs2]++;
switch (funct3) {
// case 0b000 :
// SB(emu, rs1, rs2, imm);
// if (DEBUG) cout << "SB" << endl;
// break;
// case 0b001 :
// SH(emu, rs1, rs2, imm);
// if (DEBUG) cout << "SH" << endl;
// break;
case 0b010 :
if (emu->args.print_asm) {
cout << "sw " << reg_name[rs2] << " " << reg_name[rs1] << " " << imm << endl;
}
SW(emu, rs1, rs2, imm);
break;
default :
cout << "no function matched" << endl;
return 1;
}
return 0;
}
int inst_imm(Emulator* emu, uint32_t instruction) {
// I-type
// addi, slti, sltiu, xori, ori, andi, slli, srli, srai
// imm rs1 funct3 rd opcode
// 12 5 3 5 7
uint32_t funct3 = (instruction & 0x00007000) >> 12;
int imm = (instruction & 0xFFF00000) >> 20;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t shamt = (uint32_t)(imm & 0x0000001F);
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
emu->stats.reg_used[rs1]++;
emu->stats.reg_used[rd]++;
switch (funct3) {
case 0b000 :
ADDI(emu, rs1, rd, imm);
if (emu->args.print_asm) {
cout << "addi " << reg_name[rd] << " " << reg_name[rs1] << " " << imm << endl;
}
break;
// case 0b010 :
// SLTI(emu, rs1, rd, imm);
// if (DEBUG) cout << "SLTI" << endl;
// break;
// case 0b011 :
// SLTIU(emu, rs1, rd, imm);
// if (DEBUG) cout << "SLTIU" << endl;
// break;
// case 0b100 :
// XORI(emu, rs1, rd, imm);
// if (DEBUG) cout << "XORI" << endl;
// break;
// case 0b110 :
// ORI(emu, rs1, rd, imm);
// if (DEBUG) cout << "ORI" << endl;
// break;
// case 0b111 :
// ANDI(emu, rs1, rd, imm);
// if (DEBUG) cout << "ANDI" << endl;
// break;
case 0b001 :
SLLI(emu, rs1, rd, shamt);
if (emu->args.print_asm) {
cout << "slli " << reg_name[rd] << " " << reg_name[rs1] << " " << imm << endl;
}
break;
case 0b101 :
SRLI(emu, rs1, rd, shamt);
if (emu->args.print_asm) {
cout << "srli " << reg_name[rd] << " " << reg_name[rs1] << " " << imm << endl;
}
break;
default :
cout << "no function matched" << endl;
return 1;
}
return 0;
}
int inst_op(Emulator* emu, uint32_t instruction) {
// R-type
// add, sub, sll, slt, sltu, xor, srl, sra, or, and
// funct7 rs2 rs1 funct3 rd opcode
// 7 5 5 3 5 7
uint32_t funct3 = (instruction & 0x00007000) >> 12;
uint32_t funct7 = (instruction & 0xFE000000) >> 22;
uint32_t funct = funct7 + funct3;
uint32_t rs2 = (instruction & 0x01F00000) >> 20;
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
emu->stats.reg_used[rs1]++;
emu->stats.reg_used[rs2]++;
emu->stats.reg_used[rd]++;
switch (funct) {
// ²¼°Ì10bit(7bit->funct7, 3bit->funct3)
case 0b0000000000 :
ADD(emu, rs1, rs2, rd);
if (emu->args.print_asm) {
cout << "add " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2] << endl;
}
break;
case 0b0100000000 :
SUB(emu, rs1, rs2, rd);
if (emu->args.print_asm) {
cout << "sub " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2] << endl;
}
break;
case 0b0000000001 :
SLL(emu, rs1, rs2, rd);
if (emu->args.print_asm) {
cout << "sll " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2] << endl;
}
break;
// case 0b0000000010 :
// SLT(emu, rs1, rs2, rd);
// if (DEBUG) cout << "SLT" << endl;
// break;
// case 0b0000000011 :
// SLTU(emu, rs1, rs2, rd);
// if (DEBUG) cout << "SLTU" << endl;
// break;
// case 0b0000000100 :
// XOR(emu, rs1, rs2, rd);
// if (emu->args.print_asm) {
// cout << "xor " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2] << endl;
// }
// break;
// case 0b0000000101 :
// SRL(emu, rs1, rs2, rd);
// if (DEBUG) cout << "SRL" << endl;
// break;
// case 0b0100000101 :
// SRA(emu, rs1, rs2, rd);
// if (DEBUG) cout << "SRA" << endl;
// break;
// case 0b0000000110 :
// OR(emu, rs1, rs2, rd);
// if (DEBUG) cout << "OR" << endl;
// break;
// case 0b0000000111 :
// AND(emu, rs1, rs2, rd);
// if (DEBUG) cout << "AND" << endl;
// break;
default :
cout << "no function matched" << endl;
return 1;
}
return 0;
}
int inst_lui(Emulator* emu, uint32_t instruction) {
// U-type
// LUI
// imm rd opcode
// 20 5 7
int imm = (instruction & 0xFFFFF000) >> 12;
uint32_t rd = (instruction & 0x00000F80) >> 7;
emu->stats.reg_used[rd]++;
LUI(emu, rd, imm);
if (emu->args.print_asm) {
cout << "lui " << reg_name[rd] << " " << imm << endl;
}
return 0;
}
/*
int inst_auipc(Emulator* emu, uint32_t instruction) {
// U-type
// AUIPC
// imm rd opcode
// 20 5 7
int imm = instruction & 0xFFFFF000;
uint32_t rd = (instruction & 0x00000F80) >> 7;
if (DEBUG) {
cout << dec;
cout << "----------------" << endl;
cout << "imm : " << imm << endl
<< "rd : " << rd << " (-> " << reg_name[rd] << ")" << endl;
}
AUIPC(emu, rd, imm);
if (DEBUG) cout << "AUIPC" << endl;
return 0;
}
*/
int inst_jal(Emulator* emu, uint32_t instruction) {
// U-type
// AUIPC
// imm rd opcode
// 20 5 7
int imm = (instruction & 0xFFFFF000) >> 12;
if (imm & (1<<19)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFF00000);
}
uint32_t rd = (instruction & 0x00000F80) >> 7;
emu->stats.reg_used[rd]++;
JAL(emu, rd, imm);
if (emu->args.print_asm) {
cout << "jal " << reg_name[rd] << " " << imm << endl;
}
return 0;
}
int inst_jalr(Emulator* emu, uint32_t instruction) {
// I-type
// addi, slti, sltiu, xori, ori, andi, slli, srli, srai
// imm rs1 funct3 rd opcode
// 12 5 3 5 7
uint32_t funct3 = (instruction & 0x00007000) >> 12;
int imm = (instruction & 0xFFF00000) >> 20;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
emu->stats.reg_used[rs1]++;
emu->stats.reg_used[rd]++;
switch (funct3) {
case 0b000 :
JALR(emu, rs1, rd, imm);
if (emu->args.print_asm) {
cout << "jalr " << reg_name[rd] << " " << reg_name[rs1] << " " << imm << endl;
}
break;
default :
cout << "no function matched" << endl;
return 1;
}
return 0;
}
/*
int inst_flw(Emulator* emu, uint32_t instruction) {
// I-type
// flw
// imm rs1 funct3 rd opcode
// 12 5 3 5 7
uint32_t funct3 = (instruction & 0x00007000) >> 12;
int imm = (instruction & 0xFFF00000) >> 20;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
if (DEBUG) {
cout << dec;
cout << "imm : " << imm << endl
<< "rs1 : " << rs1 << " (-> " << reg_name[rs1] << ")" << endl
<< "rd : " << rd << " (-> " << reg_name[rd] << ")" << endl;
cout << "----------------" << endl;
}
switch (funct3) {
case 0b010 :
FLW(emu, rs1, rd, imm);
if (DEBUG) cout << "FLW" << endl;
break;
default :
cout << "no function matched" << endl;
return 1;
}
return 0;
}
*/
/*
int inst_fsw(Emulator* emu, uint32_t instruction) {
// S-type
// fsw
// imm1 rs2 rs1 funct3 imm2 opcode
// 7 5 5 3 5 7
uint32_t funct3 = (instruction & 0x00007000) >> 12;
uint32_t imm1 = (instruction & 0xFE000000) >> 20;
uint32_t imm2 = (instruction & 0x00000F80) >> 7;
int imm = imm1 + imm2;
if (imm & (1<<11)){
// if MSB = 1 then sign extend
imm = (imm | 0xFFFFF000);
}
uint32_t rs2 = (instruction & 0x01F00000) >> 20;
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
if (DEBUG) {
cout << dec;
cout << "----------------" << endl;
cout << "imm : " << imm << endl
<< "rs1 : " << rs1 << " (-> " << reg_name[rs1] << ")" << endl
<< "rs2 : " << rs2 << " (-> " << reg_name[rs2] << ")" << endl;
}
switch (funct3) {
case 0b010 :
FSW(emu, rs1, rs2, imm);
if (DEBUG) cout << "FSW" << endl;
break;
default :
cout << "no function matched" << endl;
return 1;
}
return 0;
}
*/
int inst_fop(Emulator* emu, uint32_t instruction) {
// R-type
// fadd.s, fsub.s, fmul.s, fdiv.s, fsqrt.s, fsgnj.s, fsgnjn.s, fsgnnjx.s,
// fmin.s, fmax.s, fcvt.w.s, fcvt.wu.s, fmv.x.w, feq.s, flt.s, fle.s,
// fclass.s, fcvt.s.w, fcvt.s.wu, fmv.w.x
// funct7 rs2 rs1 funct3 rd opcode
// 7 5 5 3 5 7
uint32_t rm = (instruction & 0x00007000) >> 12;
uint32_t funct7 = (instruction & 0xFE000000) >> 22;
uint32_t funct = funct7 | rm;
uint32_t rs2 = (instruction & 0x01F00000) >> 20;
uint32_t rs1 = (instruction & 0x000F8000) >> 15;
uint32_t rd = (instruction & 0x00000F80) >> 7;
emu->stats.reg_used[rs1]++;
emu->stats.reg_used[rs2]++;
emu->stats.reg_used[rd]++;
switch (funct) {
// ¾å°Ì7bit->funct7, ²¼°Ì3bit->funct3
case 0b0000000000 :
FADDS(emu, rs1, rs2, rd);
if (emu->args.print_asm) {
cout << "fadd " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2] << endl;
}
break;
case 0b0000100000 :
FSUBS(emu, rs1, rs2, rd);
if (emu->args.print_asm) {
cout << "fsub " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2] << endl;
}
break;
case 0b0001000000 :
FMULS(emu, rs1, rs2, rd);
if (emu->args.print_asm) {
cout << "fmul " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2] << endl;
}
break;
case 0b0001100000 :
FDIVS(emu, rs1, rs2, rd);
if (emu->args.print_asm) {
cout << "fdiv " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2] << endl;
}
break;
case 0b0001000001:
if (rs2 != 0){
cout << "no function matched" << endl;
return 1;
}
FHALF(emu, rs1, rd);
if (emu->args.print_asm) {
cout << "fhalf " << reg_name[rd] << " " << reg_name[rs1] << endl;
}
break;
case 0b0101100000:
if (rs2 != 0){
cout << "no function matched" << endl;
return 1;
}
FSQRT(emu, rs1, rd);
if (emu->args.print_asm) {
cout << "sqrt " << reg_name[rd] << " " << reg_name[rs1] << endl;
}
break;
case 0b0010000010:
if (rs2 != 0){
cout << "no function matched" << endl;
return 1;
}
FABS(emu, rs1, rd);
if (emu->args.print_asm) {
cout << "fabs " << reg_name[rd] << " " << reg_name[rs1] << endl;
}
break;
case 0b0010000001:
if (rs2 != 0){
cout << "no function matched" << endl;
return 1;
}
FNEG(emu, rs1, rd);
if (emu->args.print_asm) {
cout << "fneg " << reg_name[rd] << " " << reg_name[rs1] << endl;
}
break;
// case 0b0010000000 :
// FSGNJS(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FSGNJS" << endl;
// break;
// case 0b0010000001 :
// FSGNJNS(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FSGNJNS" << endl;
// break;
// case 0b0010000010 :
// FSGNJXS(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FSGNJXS" << endl;
// break;
// case 0b0010100000 :
// FMINS(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FMINS" << endl;
// break;
// case 0b0010100001 :
// FMAXS(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FMAXS" << endl;
// break;
// case 0b1100000000 :
// if (rs2 == 0b00000){
// FCVTWS(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FCVTWS" << endl;
// }
// else if (rs2 == 0b00001) {
// FCVTWUS(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FCVTWUS" << endl;
// }
// else {
// cout << "no function matched" << endl;
// return 1;
// }
// break;
// case 0b1110000000 :
// if (rs2 != 0b00000) {
// cout << "no function matched" << endl;
// return 1;
// }
// FMVXW(emu, rs1, rs2, rd);
// break;
case 0b1010000010 :
if (rs2 != 0b00000){
cout << "no function matched" << endl;
return 1;
}
FISZERO(emu, rs1, rd);
if (emu->args.print_asm) {
cout << "fiszero " << reg_name[rd] << " " << reg_name[rs1] << " " << endl;
}
break;
case 0b1010000101 :
if (rs2 != 0b00000){
cout << "no function matched" << endl;
return 1;
}
FISNEG(emu, rs1, rd);
if (emu->args.print_asm) {
cout << "fisneg " << reg_name[rd] << " " << reg_name[rs1] << " " << endl;
}
break;
case 0b1010000011 :
if (rs2 != 0b00000){
cout << "no function matched" << endl;
return 1;
}
FISPOS(emu, rs1, rd);
if (emu->args.print_asm) {
cout << "fispos " << reg_name[rd] << " " << reg_name[rs1] << " " << endl;
}
break;
case 0b1010000001 :
FLESS(emu, rs1, rs2, rd);
if (emu->args.print_asm) {
cout << "fless " << reg_name[rd] << " " << reg_name[rs1] << " " << reg_name[rs2] << endl;
}
break;
case 0b1100000001 :
if (rs2 != 0b00000){
cout << "no function matched" << endl;
return 1;
}
FLOOR(emu, rs1, rd);
if (emu->args.print_asm) {
cout << "floor " << reg_name[rd] << " " << reg_name[rs1] << " " << endl;
}
break;
case 0b1100000000 :
if (rs2 != 0b00000){
cout << "no function matched" << endl;
return 1;
}
FTOI(emu, rs1, rd);
if (emu->args.print_asm) {
cout << "ftoi " << reg_name[rd] << " " << reg_name[rs1] << " " << endl;
}
break;
case 0b1101000000 :
if (rs2 != 0b00000){
cout << "no function matched" << endl;
return 1;
}
ITOF(emu, rs1, rd);
if (emu->args.print_asm) {
cout << "itof " << reg_name[rd] << " " << reg_name[rs1] << " " << endl;
}
break;
// case 0b1010000000 :
// FLES(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FLES" << endl;
// break;
// case 0b1110000001 :
// if (rs2 == 0b00000) {
// FCLASSS(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FCLASSS" << endl;
// }
// else {
// cout << "no function matched" << endl;
// return 1;
// }
// break;
// case 0b1101000000 :
// if (rs2 == 0b00000) {
// FCVTSW(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FCVTSW" << endl;
// }
// else if (rs2 == 0b00001) {
// FCVTSWU(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FCVTSWU" << endl;
// }
// else {
// cout << "no function matched" << endl;
// return 1;
// }
// break;
// case 0b1111000000 :
// if (rs2 == 0b00001) {
// FMVWX(emu, rs1, rs2, rd);
// if (DEBUG) cout << "FMVWX" << endl;
// }
// else {
// cout << "no function matched" << endl;
// return 1;
// }
// break;
default :
cout << "no function matched" << endl;
return 1;
}
return 0;
}
int exec_one_instruction(Emulator* emu, uint32_t instruction){
uint32_t opcode = instruction & 0x007F;
switch (opcode) {
// RV32I
case _BRANCH :
inst_branch(emu, instruction);
break;
case _LOAD :
inst_load(emu, instruction);
break;
case _STORE :
inst_store(emu, instruction);
break;
case _IMM :
inst_imm(emu, instruction);
break;
case _OP :
inst_op(emu, instruction);
break;
case _LUI :
inst_lui(emu, instruction);
break;
// case _AUIPC :
// inst_auipc(emu, instruction);
// break;
case _JAL :
inst_jal(emu, instruction);
break;
case _JALR :
inst_jalr(emu, instruction);
break;
// RV32F
// case _FLW :
// inst_flw(emu, instruction);
// break;
// case _FSW :
// inst_fsw(emu, instruction);
// break;
// case _FMADDS :
// inst_fmadds(emu, instruction);
// break;
// case _FMSUBS :
// inst_fmsubs(emu, instruction);
// break;
// case _FNMSUBS :
// inst_fnmsubs(emu, instruction);
// break;
// case _FNMADDS :
// inst_fnmadds(emu, instruction);
// break;
case _FOP :
inst_fop(emu, instruction);
break;
case _NOP :
if (instruction & 0xFFFFFFFF) {
if (emu->args.print_asm) {
cout << "nop " << endl;
}
}
return 1;
default :
cout << "no opcode matched" << endl;
return 1;
}
emu->reg[0] = 0x00000000;
emu->reg[19] = 0x00000000;
return 0;
}