-
Notifications
You must be signed in to change notification settings - Fork 2
/
mem.py
304 lines (242 loc) · 10.3 KB
/
mem.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""AXI4 memory slave module."""
# The MIT License
#
# Copyright (c) 2017-2018 by the author(s)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Author(s):
# - Andreas Oeldemann <[email protected]>
#
# Description:
#
# Memory module. Acts as a simplified AXI4 slave and allows attached DuTs to
# read and write data from/to a specific memory location.
import cocotb
from cocotb.triggers import RisingEdge
from tb import wait_n_cycles
import random
class Mem(object):
"""Memory module.
Acts as a simplified AXI slave that allows attached DUTs to read and write
data from/to a specific memory location.
"""
def __init__(self, size, offset=0):
"""Initialize an empty memory with the specified byte size."""
# initialize empty memory
self._data = ['\x00'] * size
self._offset = offset
def write(self, addr, data, size):
"""Write data to the memory."""
assert addr >= self._offset
addr -= self._offset
assert (addr + size) <= self.size()
# convert to hex string
data = "{0:0{1}x}".format(data, 2*size)
self._data[addr:addr+size] = data.decode('hex')
def write_reverse_byte_order(self, addr, data, size):
"""Write data to the memory (reverse byte order)."""
assert addr >= self._offset
addr -= self._offset
assert (addr + size) <= self.size()
# convert to hex string
data = "{0:0{1}x}".format(data, 2*size)
# reverse byte order
data = "".join(reversed([data[i:i+2] for i in range(0, len(data), 2)]))
self._data[addr:addr+size] = data.decode('hex')
def read(self, addr, size):
"""Read data from the memory."""
assert addr >= self._offset
addr -= self._offset
assert (addr + size) <= self.size()
return int("".join(self._data[addr:addr+size]).encode('hex'), 16)
def read_reverse_byte_order(self, addr, size):
"""Read data from the memory (reverse byte order)."""
# read data
data = self.read(addr, size)
# convert to hex string
data = "{0:0{1}x}".format(data, 2*size)
# reverse byte order
data = "".join(reversed([data[i:i+2] for i in range(0, len(data), 2)]))
return int(data, 16)
def set_size(self, size):
"""Update the memory size."""
self._data = ['\x00'] * size
def set_offset(self, offset):
"""Update the memory offset address."""
self._offset = offset
def size(self):
"""Return the memory size."""
return len(self._data)
def clear(self):
"""Clear the memory content."""
for i in range(len(self._data)):
self._data[i] = '\x00'
def connect(self, dut, prefix=None):
"""Connect DuT to the AXI4 slave interface of the memory module."""
if prefix is None:
sig_prefix = "m_axi"
else:
sig_prefix = "m_axi_%s" % prefix
self._CLK = dut.clk
# read interface
self._ARADDR = getattr(dut, "%s_araddr" % sig_prefix)
self._ARLEN = getattr(dut, "%s_arlen" % sig_prefix)
self._ARSIZE = getattr(dut, "%s_arsize" % sig_prefix)
self._ARVALID = getattr(dut, "%s_arvalid" % sig_prefix)
self._ARREADY = getattr(dut, "%s_arready" % sig_prefix)
self._RREADY = getattr(dut, "%s_rready" % sig_prefix)
self._RDATA = getattr(dut, "%s_rdata" % sig_prefix)
self._RLAST = getattr(dut, "%s_rlast" % sig_prefix)
self._RVALID = getattr(dut, "%s_rvalid" % sig_prefix)
# write interface
self._AWADDR = getattr(dut, "%s_awaddr" % sig_prefix)
self._AWLEN = getattr(dut, "%s_awlen" % sig_prefix)
self._AWSIZE = getattr(dut, "%s_awsize" % sig_prefix)
self._AWVALID = getattr(dut, "%s_awvalid" % sig_prefix)
self._AWREADY = getattr(dut, "%s_awready" % sig_prefix)
self._WREADY = getattr(dut, "%s_wready" % sig_prefix)
self._WDATA = getattr(dut, "%s_wdata" % sig_prefix)
self._WLAST = getattr(dut, "%s_wlast" % sig_prefix)
self._WVALID = getattr(dut, "%s_wvalid" % sig_prefix)
self._BRESP = getattr(dut, "%s_bresp" % sig_prefix)
self._BVALID = getattr(dut, "%s_bvalid" % sig_prefix)
self._BREADY = getattr(dut, "%s_bready" % sig_prefix)
@cocotb.coroutine
def main(self):
"""Handle AXI4 slave read/write interface.
Allows attached DUT to read/write memory content via an AXI interface.
"""
# initially read/write address ready signals are low
self._ARREADY <= 0
self._AWREADY <= 0
# initially the read data is invalid
self._RVALID <= 0
# initially no write data is accepted
self._WREADY <= 0
# initially BVALID is low
self._BVALID <= 0
while True: # infinite loop
read = False
write = False
# wait for read/write request
while True:
yield RisingEdge(self._CLK)
# read requests are served before write requests
if int(self._ARVALID):
read = True
break
if int(self._AWVALID):
write = True
break
# wait a random number of cycles
yield wait_n_cycles(self._CLK, random.randint(0, 10))
if read:
# acknowledge read request
self._ARREADY <= 1
# ARVALID should still be high, but let's explicitly wait and
# check anyways
while True:
yield RisingEdge(self._CLK)
if int(self._ARVALID):
break
# save address and burst information
araddr = int(self._ARADDR)
arlen = int(self._ARLEN)
arsize = int(self._ARSIZE)
# deassert ARREADY
self._ARREADY <= 0
# wait a random number of cycles
yield wait_n_cycles(self._CLK, random.randint(0, 10))
# start answering read request
for i in range(arlen+1):
# read data for current burst
self._RDATA <= \
self.read_reverse_byte_order(
araddr+i*pow(2, arsize), pow(2, arsize))
# data is valid
self._RVALID <= 1
# set rlast signal high for last beat of the burst
if i == arlen:
self._RLAST <= 1
else:
self._RLAST <= 0
# wait for requestor to get ready to accept data
while True:
yield RisingEdge(self._CLK)
if int(self._RREADY) == 1:
break
# set data to be not valid anymore
self._RVALID <= 0
# insert some random gaps between beats of the same burst
if i != arlen:
yield wait_n_cycles(self._CLK, random.randint(0, 5))
elif write:
# acknowledge write request
self._AWREADY <= 1
# AWVALID should still be high, but let's explicitly wait and
# check anyways
while True:
yield RisingEdge(self._CLK)
if int(self._AWVALID):
break
# save address and burst information
awaddr = int(self._AWADDR)
awlen = int(self._AWLEN)
awsize = int(self._AWSIZE)
# deassert AWREADY
self._AWREADY <= 0
# wait a random number of cycles
yield wait_n_cycles(self._CLK, random.randint(0, 10))
# start write
for i in range(awlen+1):
# accept data
self._WREADY <= 1
# wait for WVALID to become high
while True:
yield RisingEdge(self._CLK)
if int(self._WVALID):
break
# get data
data = int(self._WDATA)
# write data
self.write_reverse_byte_order(
awaddr+i*pow(2, awsize), data, pow(2, awsize))
# check wlast signal
if i == awlen:
assert int(self._WLAST) == 1
else:
assert int(self._WLAST) == 0
# set WREADY back low
self._WREADY <= 0
# randomly keep WREADY low sometimes for a bit
if i != awlen:
if random.random() < 0.1:
yield wait_n_cycles(self._CLK,
random.randint(1, 5))
# set BRESP and BVALID
self._BRESP <= 0
self._BVALID <= 1
# wait for BREADY to become high
while True:
yield RisingEdge(self._CLK)
if int(self._BREADY):
break
# set BVALID back low
self._BVALID <= 0