-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaxilite.py
187 lines (150 loc) · 5.67 KB
/
axilite.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
"""Classes for reading/writing AXI4-Lite 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 <[email protected]>
#
# Description:
#
# Classes for reading/writing AXI4-Lite interfaces.
import cocotb
from cocotb.triggers import RisingEdge
from cocotb.result import ReturnValue
class AXI_Lite_Writer(object):
"""AXI4-Lite interface writer."""
def connect(self, dut, clk, bit_width, prefix=None):
"""Connect the DuT AXI4-Lite interface to this writer.
When parameter 'prefix' is not set, DuT AXI4-Lite signals are expected
to be named s_axi_awaddr, s_axi_awvalid, ... If 'prefix' is set, DuT
AXI4-Lite signals are expected to be named s_axi_<prefix>_awaddr, ...
"""
self.bit_width = bit_width
self.access_active = False
if prefix is None:
sig_prefix = "s_axi"
else:
sig_prefix = "s_axi_%s" % prefix
self.clk = clk
self.awaddr = getattr(dut, "%s_awaddr" % sig_prefix)
self.awvalid = getattr(dut, "%s_awvalid" % sig_prefix)
self.awready = getattr(dut, "%s_awready" % sig_prefix)
self.wdata = getattr(dut, "%s_wdata" % sig_prefix)
self.wstrb = getattr(dut, "%s_wstrb" % sig_prefix)
self.wvalid = getattr(dut, "%s_wvalid" % sig_prefix)
self.wready = getattr(dut, "%s_wready" % sig_prefix)
self.bvalid = getattr(dut, "%s_bvalid" % sig_prefix)
self.bready = getattr(dut, "%s_bready" % sig_prefix)
@cocotb.coroutine
def rst(self):
"""Reset signals."""
self.awvalid <= 0
self.wvalid <= 0
self.bready <= 0
self.wstrb <= 2**(self.bit_width/8)-1
yield RisingEdge(self.clk)
@cocotb.coroutine
def write(self, addr, data):
"""Write data to the AXI4-Lite interface."""
# serialize access
while True:
if not self.access_active:
break
yield RisingEdge(self.clk)
self.access_active = True
self.awaddr <= addr
self.awvalid <= 1
self.wdata <= data
self.wvalid <= 1
self.bready <= 1
while True:
yield RisingEdge(self.clk)
if int(self.awready) == 1:
break
while True:
if int(self.wready) == 1:
break
yield RisingEdge(self.clk)
self.awvalid <= 0
self.wvalid <= 0
while True:
yield RisingEdge(self.clk)
if int(self.bvalid) == 1:
break
self.bready <= 0
yield RisingEdge(self.clk)
# release access lock
self.access_active = False
class AXI_Lite_Reader(object):
"""AXI4-Lite interface reader."""
def connect(self, dut, clk, bit_width, prefix=None):
"""Connect the DuT AXI4-Lite interface to this reader.
When parameter 'prefix' is not set, DuT AXI4-Lite signals are expected
to be named s_axi_araddr, s_axi_arvalid, ... If 'prefix' is set, DuT
AXI4-Lite signals are expected to be named s_axi_<prefix>_araddr, ...
"""
self.bit_width = bit_width
self.access_active = False
if prefix is None:
sig_prefix = "s_axi"
else:
sig_prefix = "s_axi_%s" % prefix
self.clk = clk
self.araddr = getattr(dut, "%s_araddr" % sig_prefix)
self.arvalid = getattr(dut, "%s_arvalid" % sig_prefix)
self.arready = getattr(dut, "%s_arready" % sig_prefix)
self.rdata = getattr(dut, "%s_rdata" % sig_prefix)
self.rvalid = getattr(dut, "%s_rvalid" % sig_prefix)
self.rready = getattr(dut, "%s_rready" % sig_prefix)
@cocotb.coroutine
def rst(self):
"""Reset signals."""
self.arvalid <= 0
self.rready <= 0
yield RisingEdge(self.clk)
@cocotb.coroutine
def read(self, addr):
"""Read data from the AXI4-Lite interface."""
# serialize access
while True:
if not self.access_active:
break
yield RisingEdge(self.clk)
self.access_active = True
self.araddr <= addr
self.arvalid <= 1
self.rready <= 1
while True:
yield RisingEdge(self.clk)
if int(self.arready) == 1:
break
self.arvalid <= 0
while True:
yield RisingEdge(self.clk)
if int(self.rvalid) == 1:
break
self.rready <= 0
data = int(self.rdata)
yield RisingEdge(self.clk)
# release access lock
self.access_active = False
raise ReturnValue(data)