-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.cc
More file actions
145 lines (116 loc) · 4.27 KB
/
sim.cc
File metadata and controls
145 lines (116 loc) · 4.27 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
#include <asmjit/asmjit.h>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <ostream>
#include <vector>
#include "sim/cpu_state.hh"
#include "sim/decoder.hh"
#include "sim/hart.hh"
#include "sim/isa.hh"
#include "sim/memory.hh"
namespace sim {
class JitAssembly : public Hart {
public:
using FuncTy = void (*)();
private:
void execute(isa::Instruction insn) override {
asmjit::CodeHolder code;
code.init(runtime.environment());
asmjit::x86::Assembler assembler{&code};
switch (insn.opc) {
case isa::Opcode::kAdd: {
assembler.mov(asmjit::x86::eax, cpu.regs[insn.src1]);
assembler.add(asmjit::x86::eax, cpu.regs[insn.src2]);
//
assembler.mov(
asmjit::x86::dword_ptr((size_t)(&(cpu.regs[insn.dst]))),
asmjit::x86::eax);
assembler.mov(asmjit::x86::eax, cpu.pc);
assembler.add(asmjit::x86::eax, 1);
assembler.mov(asmjit::x86::dword_ptr((size_t)(&cpu.pc)),
asmjit::x86::eax);
assembler.ret();
break;
}
case isa::Opcode::kHalt: {
cpu.finished = true;
assembler.ret();
break;
}
case isa::Opcode::kJump: {
assembler.mov(asmjit::x86::eax, cpu.getReg(insn.dst));
assembler.mov(asmjit::x86::dword_ptr((size_t)(&cpu.pc)),
asmjit::x86::eax);
assembler.ret();
break;
}
case isa::Opcode::kLoad: {
assembler.mov(asmjit::x86::eax,
cpu.memory->load(cpu.regs[insn.src1]));
assembler.mov(
asmjit::x86::dword_ptr((size_t)(&(cpu.regs[insn.dst]))),
asmjit::x86::eax);
assembler.mov(asmjit::x86::eax, cpu.pc);
assembler.add(asmjit::x86::eax, 1);
assembler.mov(asmjit::x86::dword_ptr((size_t)(&cpu.pc)),
asmjit::x86::eax);
assembler.ret();
break;
}
case isa::Opcode::kStore: {
assembler.mov(
asmjit::x86::dword_ptr((size_t)(&cpu.memory->get_data().at(
cpu.regs[insn.src1]))),
cpu.regs[insn.dst]);
assembler.mov(asmjit::x86::eax, cpu.pc);
assembler.add(asmjit::x86::eax, 1);
assembler.mov(asmjit::x86::dword_ptr((size_t)(&cpu.pc)),
asmjit::x86::eax);
assembler.ret();
break;
}
case isa::Opcode::kBeq: {
asmjit::Label beq_beg = assembler.newLabel(),
beq_end = assembler.newLabel();
assembler.mov(asmjit::x86::eax, cpu.regs[insn.src1]);
assembler.cmp(asmjit::x86::eax, cpu.regs[insn.src2]);
assembler.je(beq_beg);
assembler.mov(asmjit::x86::eax, cpu.pc);
assembler.add(asmjit::x86::eax, 1);
assembler.jmp(beq_end);
assembler.bind(beq_beg);
assembler.mov(asmjit::x86::eax, cpu.pc);
assembler.mov(asmjit::x86::eax, cpu.regs[insn.dst]);
assembler.bind(beq_end);
assembler.mov(asmjit::x86::dword_ptr((size_t)(&cpu.pc)),
asmjit::x86::eax);
assembler.ret();
break;
}
default:
assert(false && "Unknown instruction");
}
FuncTy exec_func{};
asmjit::Error err = runtime.add(&exec_func, &code);
//
if (err) {
std::cout << "AsmJit failed: "
<< asmjit::DebugUtils::errorAsString(err) << std::endl;
return;
}
exec_func();
runtime.release(exec_func);
}
asmjit::JitRuntime runtime;
};
} // namespace sim
int main() {
// Define program
std::vector<uint32_t> program = {
#include "code.hpp"
};
sim::JitAssembly model{};
sim::do_sim(model, program);
model.dump(std::cout);
}