-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.cc
75 lines (65 loc) · 1.97 KB
/
sim.cc
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
#include <cassert>
#include <cstdint>
#include <iostream>
#include <ostream>
#include <string>
#include <unordered_map>
#include <vector>
#include "capsule_asm.hh"
#include "sim/cpu_state.hh"
#include "sim/decoder.hh"
#include "sim/hart.hh"
#include "sim/isa.hh"
#include "sim/memory.hh"
namespace sim {
class InlineAssemly : public Hart {
void execute(isa::Instruction insn) override {
switch (insn.opc) {
case isa::Opcode::kAdd: {
ARITHM_CAPSULE("add", cpu.regs[insn.dst], cpu.regs[insn.src1],
cpu.regs[insn.src2])
ADVANCE_PC(cpu.pc)
break;
}
case isa::Opcode::kHalt: {
cpu.finished = true;
break;
}
case isa::Opcode::kJump: {
asm volatile("mov %[rd], %[pc]\n\t"
: [pc] "+r"(cpu.pc)
: [rd] "r"(cpu.regs[insn.dst]));
break;
}
case isa::Opcode::kLoad: {
LOAD_CAPSULE(cpu.regs[insn.dst],
cpu.memory->get_data().at(cpu.regs[insn.src1]))
ADVANCE_PC(cpu.pc)
break;
}
case isa::Opcode::kStore: {
STORE_CAPSULE(cpu.regs[insn.dst],
cpu.memory->get_data().at(cpu.regs[insn.src1]))
ADVANCE_PC(cpu.pc)
break;
}
case isa::Opcode::kBeq: {
B_COND_CAPSULE("je", cpu.pc, cpu.regs[insn.dst],
cpu.regs[insn.src1], cpu.regs[insn.src2])
break;
}
default:
assert(false && "Unknown instruction");
}
}
};
} // namespace sim
int main() {
// Define program
std::vector<uint32_t> program = {
#include "code.hpp"
};
sim::InlineAssemly model{};
sim::do_sim(model, program);
model.dump(std::cout);
}