forked from 2ndbleu/ca-2022-2-pa4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.py
executable file
·217 lines (170 loc) · 6.45 KB
/
components.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#==========================================================================
#
# The PyRISC Project
#
# SNURISC5: A 5-stage Pipelined RISC-V ISA Simulator
#
# Classes for hardware components: RegisterFile, Memory, Adder, etc.
#
# Jin-Soo Kim
# Systems Software and Architecture Laboratory
# Seoul National University
# http://csl.snu.ac.kr
#
#==========================================================================
from consts import *
from isa import *
#--------------------------------------------------------------------------
# Constants
#--------------------------------------------------------------------------
# Symbolic register names
rname = [
'zero', 'ra', 'sp', 'gp', 'tp', 't0', 't1', 't2',
's0', 's1', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5',
'a6', 'a7', 's2', 's3', 's4', 's5', 's6', 's7',
's8', 's9', 's10', 's11', 't3', 't4', 't5', 't6'
]
#--------------------------------------------------------------------------
# RegisterFile: models 32-bit RISC-V register file
#--------------------------------------------------------------------------
class RegisterFile(object):
def __init__(self):
self.reg = WORD([0] * NUM_REGS)
# Register file with two read ports
def read(self, rs1, rs2):
if rs1 < 0 or rs1 >= NUM_REGS or rs2 < 0 or rs2 >= NUM_REGS:
raise ValueError
rs1_data = self.reg[rs1] if rs1 != 0 else 0
rs2_data = self.reg[rs2] if rs2 != 0 else 0
return rs1_data, rs2_data
# Register file with two write ports
def write(self, rd, wbdata, rd2=0, wbdata2=0):
if rd < 0 or rd >= NUM_REGS or rd2 < 0 or rd2 >= NUM_REGS:
raise ValueError
if rd != 0:
self.reg[rd] = wbdata
if rd2 != 0:
self.reg[rd2] = wbdata2
def dump(self, columns = 4):
print("Registers")
print("=" * 9)
for c in range (0, NUM_REGS, columns):
str = ""
for r in range (c, min(NUM_REGS, c + columns)):
name = rname[r]
val = self.reg[r]
str += "%-11s0x%08x " % ("%s ($%d):" % (name, r), val)
print(str)
#--------------------------------------------------------------------------
# Register: models a single 32-bit register
#--------------------------------------------------------------------------
class Register(object):
def __init__(self, initval = 0):
self.r = WORD(initval)
def read(self):
return self.r
def write(self, val):
self.r = WORD(val)
#--------------------------------------------------------------------------
# Memory: models a memory
#--------------------------------------------------------------------------
class Memory(object):
def __init__(self, mem_start, mem_size, word_size):
self.word_size = word_size
self.mem_words = mem_size // word_size
self.mem_start = mem_start
self.mem_end = mem_start + mem_size
self.mem = WORD([0] * self.mem_words)
def access(self, valid, addr, data, fcn):
if (not valid):
res = ( WORD(0), True )
elif (addr < self.mem_start) or (addr >= self.mem_end) or \
addr % self.word_size != 0:
res = ( WORD(0) , False )
elif fcn == M_XRD:
val = self.mem[(addr - self.mem_start) // self.word_size]
res = ( val, True )
elif fcn == M_XWR:
self.mem[(addr - self.mem_start) // self.word_size] = WORD(data)
res = ( WORD(0), True )
else:
res = ( WORD(0), False )
return res
def dump(self, skipzero = False):
print("Memory 0x%08x - 0x%08x" % (self.mem_start, self.mem_end - 1))
print("=" * 30)
for a in range(self.mem_start, self.mem_end, self.word_size):
val, status = self.access(True, a, 0, M_XRD)
if not status:
continue
if (not skipzero) or (val != 0):
print("0x%08x: " % a, ' '.join("%02x" % ((val >> i) & 0xff) for i in [0, 8, 16, 24]), " (0x%08x)" % val)
#--------------------------------------------------------------------------
# ALU: models an ALU
#--------------------------------------------------------------------------
class ALU(object):
def __init__(self):
pass
def op(self, alufun, alu1, alu2):
np.seterr(all='ignore')
if alufun == ALU_ADD:
output = WORD(alu1 + alu2)
elif alufun == ALU_SUB:
output = WORD(alu1 - alu2)
elif alufun == ALU_AND:
output = WORD(alu1 & alu2)
elif alufun == ALU_OR:
output = WORD(alu1 | alu2)
elif alufun == ALU_XOR:
output = WORD(alu1 ^ alu2)
elif alufun == ALU_SLT:
output = WORD(1) if SWORD(alu1) < SWORD(alu2) else WORD(0)
elif alufun == ALU_SLTU:
output = WORD(1) if alu1 < alu2 else WORD(0)
elif alufun == ALU_SLL:
output = WORD(alu1 << (alu2 & 0x1f))
elif alufun == ALU_SRA:
output = WORD(SWORD(alu1) >> (alu2 & 0x1f))
elif alufun == ALU_SRL:
output = alu1 >> (alu2 & 0x1f)
elif alufun == ALU_COPY1:
output = alu1
elif alufun == ALU_COPY2:
output = alu2
elif alufun == ALU_SEQ:
output = WORD(1) if (alu1 == alu2) else WORD(0)
else:
output = WORD(0)
return output
#--------------------------------------------------------------------------
# Adder: models a simple 32-bit adder
#--------------------------------------------------------------------------
class Adder(object):
def __init__(self):
pass
def op(self, operand1, operand2 = 4):
np.seterr(all='ignore')
return WORD(operand1 + operand2)
#--------------------------------------------------------------------------
# BTB: For Project #4
#--------------------------------------------------------------------------
#class BTB(object):
#
# def __init__(self, k):
# self.k = k
# print('BTB initalized k = %d' % k)
# # initialize your BTB here
#
# # Lookup the entry corresponding to the pc
# # It will return the target address if there is a matching entry
# def lookup(self, pc):
# return 0
#
# # Add an entry
# def add(self, pc, target):
# return
#
# # Remove an entry
# def remove(self, pc):
# return
#