-
Notifications
You must be signed in to change notification settings - Fork 2
/
axis.py
208 lines (173 loc) · 7.29 KB
/
axis.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
"""Classes for reading/writing AXI4-Stream interfaces."""
# 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 <andreas.oeldemann@tum.de>
#
# Description:
#
# Classes for reading/writing AXI4-Stream interfaces.
import cocotb
from cocotb.triggers import RisingEdge
from cocotb.result import ReturnValue
import random
class AXIS_Writer(object):
"""AXI4-Stream interface writer."""
def connect(self, dut, clk, bit_width, prefix=None):
"""Connect the DuT AXI-Stream interface to this writer.
When parameter 'prefix' is not set, DUT AXI-Stream signals are expected
to be named s_axis_tdata, s_axis_tvalid, ... If 'prefix' is set, DuT
AXI4-Stream signals are expected to be named s_axis_<prefix>_tdata, ...
"""
self.bit_width = bit_width
if prefix is None:
sig_prefix = "s_axis"
else:
sig_prefix = "s_axis_%s" % prefix
self.clk = clk
self.s_axis_tdata = getattr(dut, "%s_tdata" % sig_prefix)
self.s_axis_tvalid = getattr(dut, "%s_tvalid" % sig_prefix)
self.s_axis_tlast = getattr(dut, "%s_tlast" % sig_prefix)
self.s_axis_tkeep = getattr(dut, "%s_tkeep" % sig_prefix)
# flow control (tready) is optional
try:
self.s_axis_tready = getattr(dut, "%s_tready" % sig_prefix)
self.has_tready = True
except AttributeError:
self.has_tready = False
# tuser is optional
try:
self.s_axis_tuser = getattr(dut, "%s_tuser" % sig_prefix)
self.has_tuser = True
except AttributeError:
self.has_tuser = False
@cocotb.coroutine
def rst(self):
"""Reset signals."""
self.s_axis_tdata <= 0
self.s_axis_tvalid <= 0
self.s_axis_tlast <= 0
self.s_axis_tkeep <= 0
if self.has_tuser:
self.s_axis_tuser <= 0
yield RisingEdge(self.clk)
@cocotb.coroutine
def write(self, tdata, tkeep, tuser=None, insert_random_gaps=True):
"""Perform write on AXI4-Stream slave interface.
Writes a complete transfer on the AXI4-Stream slave interface. The
function expects a list of the TDATA words and the value of the TKEEP
signal for the last word transfer. Optionally, a list of TUSER values
can be specified. If the insert_random_gaps parameter is set to True
(which is the default), random idle gaps are inserted by setting
TVALID low.
"""
for i, word in enumerate(tdata):
self.s_axis_tdata <= word
self.s_axis_tvalid <= 1
if self.has_tuser:
if i < len(tuser):
self.s_axis_tuser <= tuser[i]
else:
self.s_axis_tuser <= 0
if i == len(tdata)-1:
self.s_axis_tlast <= 1
self.s_axis_tkeep <= tkeep
else:
self.s_axis_tkeep <= pow(2, self.bit_width/8)-1
while True:
yield RisingEdge(self.clk)
if not self.has_tready or int(self.s_axis_tready) == 1:
break
if insert_random_gaps:
# with a chance of 20%, insert a clock cycle in which no data
# is ready to be transmitted by setting tvalid low
if i != len(tdata)-1 and random.random() < 0.2:
self.s_axis_tvalid <= 0
yield RisingEdge(self.clk)
self.s_axis_tvalid <= 0
self.s_axis_tlast <= 0
class AXIS_Reader(object):
"""AXI4-Stream interface reader."""
def connect(self, dut, clk, bit_width, prefix=None):
"""Connect the DuT AXI-Stream interface to this reader.
When parameter 'prefix' is not set, DUT AXI-Stream signals are expected
to be named m_axis_tdata, m_axis_tvalid, ... If 'prefix' is set, DuT
AXI4-Stream signals are expected to be named m_axis_<prefix>_tdata, ...
"""
self.bit_width = bit_width
if prefix is None:
sig_prefix = "m_axis"
else:
sig_prefix = "m_axis_%s" % prefix
self.clk = clk
self.m_axis_tdata = getattr(dut, "%s_tdata" % sig_prefix)
self.m_axis_tvalid = getattr(dut, "%s_tvalid" % sig_prefix)
self.m_axis_tlast = getattr(dut, "%s_tlast" % sig_prefix)
self.m_axis_tkeep = getattr(dut, "%s_tkeep" % sig_prefix)
# flow control (tready) is optional
try:
self.m_axis_tready = getattr(dut, "%s_tready" % sig_prefix)
self.has_tready = True
except AttributeError:
self.has_tready = False
# tuser is optional
try:
self.m_axis_tuser = getattr(dut, "%s_tuser" % sig_prefix)
self.has_tuser = True
except AttributeError:
self.has_tuser = False
@cocotb.coroutine
def rst(self):
"""Reset signals."""
if self.has_tready:
self.m_axis_tready <= 0
yield RisingEdge(self.clk)
@cocotb.coroutine
def read(self):
"""Perform read on AXI4-Stream master interface.
Reads a complete transfer on the AXI4-Stream master interface. The
function returns a list of the TDATA words and the value of the TKEEP
signal for the last word transfer. If a side-band TUSER interface is
present, a list of the values is returned as well.
"""
# empty tdata list
tdata = []
if self.has_tuser:
# empty tuser list
tuser = []
else:
tuser = None
while True:
yield RisingEdge(self.clk)
if (not self.has_tready or int(self.m_axis_tready)) and \
int(self.m_axis_tvalid):
tdata.append(int(self.m_axis_tdata))
if self.has_tuser:
tuser.append(int(self.m_axis_tuser))
if int(self.m_axis_tlast):
tkeep = int(self.m_axis_tkeep)
break
elif int(self.m_axis_tkeep) != pow(2, self.bit_width/8)-1:
raise cocotb.result.TestFailure("invalid AXI4-Stream " +
"TKEEP signal value")
raise ReturnValue((tdata, tkeep, tuser))