-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
109 lines (93 loc) · 2.94 KB
/
main.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
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include "include/hex.h"
#include "include/memory.h"
#include "include/rv32i.h"
/**
* Print a usage message and abort the program.
*********************************************************************/
static void usage()
{
std::cerr << "Usage: rv32i [-m hex-mem-size] infile" << std::endl;
std::cerr << " -m specify memory size (default = 0x10000)" << std::endl;
exit(1);
}
/**
* Read a file of RV32I instructions and execute them.
********************************************************************/
int main(int argc, char **argv)
{
uint32_t memory_limit = 0x10000; // default memory size = 64k
uint64_t execution_limit = 0; // 0 = run forever
int opt;
bool show_disasm = false;
bool show_insn = false;
bool show_regs = false;
bool show_dump = false;
while ((opt = getopt(argc, argv, "dil::m::rz")) != -1)
{
switch (opt)
{
case 'd':
show_disasm = true;
// std::cout << " found d at \n";
break;
case 'i':
// std::cout << " found i at \n";
show_insn = true;
break;
case 'l':
// std::cout << " found l at \n";
execution_limit = (uint32_t)std::stoul(optarg, nullptr, 10);
// std::cout << " execution limit: " << execution_limit << "\n";
break;
case 'm':
// hex_mem_size given as argument is assigned to memory limit
memory_limit = (uint32_t)std::stoul(optarg, nullptr, 16);
// std::cout << " found m at \n";
// std::cout << " memory limit: " << memory_limit << "\n";
break;
case 'r':
// std::cout << " found r at \n";
show_regs = true;
break;
case 'z':
// std::cout << " found z at \n";
show_dump = true;
break;
default:
/* '?' */
usage();
}
}
// std::cout << " reading instructions from: " << argv[argc - 1] << std::endl;
// if (3 >= argc)
// usage(); // missing filename
memory mem(memory_limit);
// missing filename or file loading error
if (!mem.load_file(argv[argc - 1]))
usage();
rv32i sim(&mem);
// show disassembled instructions
// before the simulation begins
if (show_disasm)
sim.disasm();
// reset the simulator
// set program counter to zero
// set General Purpose registers to their default values
sim.reset();
// print instructions if option -i is given
sim.set_show_instructions(show_insn);
// print register hart dump if option -r is given
sim.set_show_registers(show_regs);
// run the simulated with fixed limit if -l flag has an argument
sim.run(execution_limit);
// show memory dump if -z option is given
if (show_dump)
{
sim.dump();
mem.dump();
}
return 0;
}