Skip to content

Commit 4e993dc

Browse files
committed
Add Simulator
Simulator is a self contained model that executes code on data.
1 parent 0616530 commit 4e993dc

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

riscvmodel/sim.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from .model import Model, Memory
2+
3+
import struct
4+
5+
class Simulator:
6+
def __init__(self, model):
7+
self.model = model
8+
self.program = []
9+
10+
def load_program(self, program, *, address=0):
11+
self.program = [i for i in program]
12+
13+
def load_data(self, data = "", *, address=0):
14+
mem = self.model.state.memory.memory
15+
for a in range(int(len(data)/4)):
16+
mem[a] = struct.unpack("<L", data[a*4:(a+1)*4])[0]
17+
18+
def run(self, *, pc=0):
19+
self.model.reset(pc=pc)
20+
while True:
21+
try:
22+
self.model.issue(self.program[int(self.model.state.pc)>>2])
23+
except IndexError as e:
24+
return
25+
26+
def dump_data(self, *, address=0, size=None):
27+
data = b""
28+
mem = self.model.state.memory.memory
29+
for a in mem:
30+
if a >= address and (size is None or a < address + size):
31+
data += struct.pack("<L", mem[a])
32+
return data

0 commit comments

Comments
 (0)