-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemu.c
More file actions
61 lines (49 loc) · 1.71 KB
/
emu.c
File metadata and controls
61 lines (49 loc) · 1.71 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
#include "emu.h"
#include <stdint.h>
machine_status_t ms = {
.PC = 0, .REG = {0}, .REG_discard = 0, .MEM = {0},
.isRunning = 0
};
extern int inst_decode(inst_t inst);
extern inst_exe_t inst_exe_arr[INST_NUM];
void inst_cycle(){
const inst_t icur = *(inst_t*)&ms.MEM[ms.PC]; // current instruction
//const uint32_t opcode = icur->R.opcode;
//uint32_t * const rd = (icur->R.rd) ? (ms.REG + icur->R.rd) : &ms.REG_discard; // Use *rd to write
//const uint32_t rs1 = ms.REG[icur->R.rs1], rs2 = ms.REG[icur->R.rs2]; // Previous value of REG[rs1/2]
uint32_t cur_inst; // like addi
if((cur_inst = inst_decode(icur)) != -1){
inst_exe_arr[cur_inst](&ms, icur);
if(cur_inst == i_jalr){
return;
}
}
ms.PC += 4;
return;
}
int main(int argc, char *argv[]){
Assert(argc >= 2, "Lack arguments: Program needed");
FILE *fp = fopen(argv[1], "r");
Assert(fp != NULL, "Fail to open %s", argv[1]);
int fseek_ret = fseek(fp, 0, SEEK_END);
Assert(fseek_ret != -1, "Fail to seek to the EOF with SEEK_END");
long fsize = ftell(fp);
Assert(fsize != 0, "Fail to return the indicator's position");
Assert(fsize < MSIZE, "Program size exceeds %d bytes (fsize = %ld)", MSIZE, fsize);
rewind(fp);
size_t fread_ret = fread(ms.MEM, 1, fsize, fp);
Assert(fread_ret == fsize, "Fail to load the whole file");
fclose(fp);
// Special Judge
if(strstr(argv[1], "mem.bin")){
*(uint32_t*)&ms.MEM[0x1218] = 0x00100073;
}
if(strstr(argv[1], "sum.bin")){
*(uint32_t*)&ms.MEM[0x0224] = 0x00100073;
}
ms.isRunning = 1;
for(int i = 0; i < MCYCLE && ms.isRunning; i++){
inst_cycle();
}
return 0;
}