82dc5e87dece718809485cf03c704ef885f3fbce
1 from itertools
import chain
4 from cocotb
.clock
import Clock
5 from cocotb
.triggers
import Timer
6 from cocotb
.utils
import get_sim_steps
7 from cocotb
.binary
import BinaryValue
9 from c4m
.nmigen
.jtag
.tap
import IOType
10 from c4m
.cocotb
.jtag
.c4m_jtag
import JTAG_Master
11 from c4m
.cocotb
.jtag
.c4m_jtag_svfcocotb
import SVF_Executor
13 from soc
.config
.pinouts
import get_pinspecs
14 from soc
.debug
.jtag
import Pins
21 def __init__(self
, dut
):
25 except AttributeError:
31 self
.tck
= ti
.TAP_bus__tck
32 self
.tms
= ti
.TAP_bus__tms
33 self
.tdi
= ti
.TAP_bus__tdi
34 self
.tdo
= ti
.TAP_bus__tdo
36 def info(self
, *args
, **kwargs
):
37 return self
.dut
._log
.info(*args
, **kwargs
)
40 def __init__(self
, pin
):
45 if self
.type_
== IOType
.In
:
46 core_i
= getattr(wrap
.ti
, f
"{self.name}__core__i").value
47 pad_i
= getattr(wrap
.ti
, f
"{self.name}__pad__i").value
48 wrap
.info(f
"{self.name}: core.i={core_i}, pad.i={pad_i}")
49 elif self
.type_
== IOType
.Out
:
50 core_o
= getattr(wrap
.ti
, f
"{self.name}__core__o").value
51 pad_o
= getattr(wrap
.ti
, f
"{self.name}__pad__o").value
52 wrap
.info(f
"{self.name}: core.o={core_o}, pad.o={pad_o}")
53 elif self
.type_
== IOType
.TriOut
:
54 core_o
= getattr(wrap
.ti
, f
"{self.name}__core__o").value
55 core_oe
= getattr(wrap
.ti
, f
"{self.name}__core__oe").value
56 pad_o
= getattr(wrap
.ti
, f
"{self.name}__pad__o").value
57 pad_oe
= getattr(wrap
.ti
, f
"{self.name}__pad__oe").value
58 wrap
.info(f
"{self.name}: core.(o={core_o}, oe={core_oe}), pad.(o={pad_o}, oe={pad_oe})")
59 elif self
.type_
== IOType
.InTriOut
:
60 core_i
= getattr(wrap
.ti
, f
"{self.name}__core__i").value
61 core_o
= getattr(wrap
.ti
, f
"{self.name}__core__o").value
62 core_oe
= getattr(wrap
.ti
, f
"{self.name}__core__oe").value
63 pad_i
= getattr(wrap
.ti
, f
"{self.name}__pad__i").value
64 pad_o
= getattr(wrap
.ti
, f
"{self.name}__pad__o").value
65 pad_oe
= getattr(wrap
.ti
, f
"{self.name}__pad__oe").value
66 wrap
.info(f
"{self.name}: core.(i={core_i}, o={core_o}, oe={core_oe}), pad.(i={core_i}, o={pad_o}, oe={pad_oe})")
68 raise ValueError(f
"Unsupported pin type {self.type_}")
70 def data(self
, *, i
=None, o
=None, oe
=None):
71 if self
.type_
== IOType
.In
:
74 elif self
.type_
== IOType
.Out
:
77 elif self
.type_
== IOType
.TriOut
:
78 assert (o
is not None) and (oe
is not None)
80 elif self
.type_
== IOType
.InTriOut
:
81 assert (i
is not None) and(o
is not None) and (oe
is not None)
84 raise ValueError(f
"Unsupported pin type {self.type_}")
86 def check(self
, *, wrap
, i
=None, o
=None, oe
=None):
87 if self
.type_
in (IOType
.In
, IOType
.InTriOut
):
88 sig
= f
"{self.name}__core__i"
89 val
= getattr(wrap
.ti
, sig
).value
91 raise ValueError(f
"'{sig}' should be {i}, not {val}")
92 if self
.type_
in (IOType
.Out
, IOType
.TriOut
, IOType
.InTriOut
):
93 sig
= f
"{self.name}__pad__o"
94 val
= getattr(wrap
.ti
, sig
).value
96 raise ValueError(f
"'{sig}' should be {o}, not {val}")
97 if self
.type_
in (IOType
.TriOut
, IOType
.InTriOut
):
98 sig
= f
"{self.name}__pad__oe"
99 val
= getattr(wrap
.ti
, sig
).value
101 raise ValueError(f
"'{sig}' should be {oe}, not {val}")
104 def log_pins(wrap
, pins
):
109 def get_jtag_boundary():
110 """gets the list of information for jtag boundary scan
112 # currently only a subset of pins is enabled. nuisance
115 'eint', 'gpio', 'mspi0',
116 # 'mspi1', - disabled for now
117 # 'pwm', 'sd0', - disabled for now
119 pins
= tuple(JTAGPin(pin
) for pin
in Pins(get_pinspecs(subset
=subset
)))
123 def setup_sim(dut
, *, info
, clk_period
, run
):
124 """Initialize CPU and setup clock"""
126 wrap
= DUTWrapper(dut
)
129 clk_steps
= get_sim_steps(clk_period
, "ns")
130 cocotb
.fork(Clock(wrap
.clk
, clk_steps
).start())
135 yield Timer(int(10.5*clk_steps
))
137 yield Timer(int(5*clk_steps
))
141 def setup_jtag(wrap
, *, tck_period
):
142 # Make this a generator
145 return JTAG_Master(wrap
.tck
, wrap
.tms
, wrap
.tdi
, wrap
.tdo
,
146 clk_period
=tck_period
,
149 def execute_svf(wrap
, *, jtag
, svf_filename
):
152 jtag_svf
= SVF_Executor(jtag
)
153 with
open(svf_filename
, "r") as f
:
155 yield jtag_svf
.run(svf_deck
, p
=wrap
.info
)
158 # IDCODE using JTAG_master
161 def idcode(wrap
, *, jtag
):
163 result1
= jtag
.result
164 wrap
.info("IDCODE1: {}".format(result1
))
165 assert(result1
== BinaryValue("00000000000000000001100011111111"))
168 result2
= jtag
.result
169 wrap
.info("IDCODE2: {}".format(result2
))
171 assert(result1
== result2
)
174 def idcode_reset(dut
):
175 clk_period
= 100 # 10MHz
176 tck_period
= 300 # 3MHz
178 info
= "Running IDCODE test; cpu in reset..."
179 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
181 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
183 yield from idcode(wrap
, jtag
=jtag
)
185 wrap
.info("IDCODE test completed")
189 clk_period
= 100 # 10MHz
190 tck_period
= 300 # 3MHz
192 info
= "Running IDCODE test; cpu running..."
193 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
195 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
197 yield from idcode(wrap
, jtag
=jtag
)
199 wrap
.info("IDCODE test completed")
202 # Read IDCODE from SVF file
206 def idcodesvf_reset(dut
):
207 clk_period
= 100 # 10MHz
208 tck_period
= 300 # 3MHz
210 info
= "Running IDCODE through SVF test; cpu in reset..."
211 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
213 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
215 yield from execute_svf(wrap
, jtag
=jtag
, svf_filename
="idcode.svf")
217 wrap
.info("IDCODE test completed")
220 def idcodesvf_run(dut
):
221 clk_period
= 100 # 10MHz
222 tck_period
= 300 # 3MHz
224 info
= "Running IDCODE through SVF test; cpu running..."
225 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
227 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
229 yield from execute_svf(wrap
, jtag
=jtag
, svf_filename
="idcode.svf")
231 wrap
.info("IDCODE test completed")
237 def boundary_scan(wrap
, *, jtag
):
238 pins
= get_jtag_boundary()
243 wrap
.info("Before scan")
246 yield jtag
.load_ir([0, 0, 0, 0])
247 pinsdata
= tuple(pin
.data(i
=i
%2, o
=((i
%3)%2), oe
=((i
%5)%2))
248 for i
, pin
in enumerate(pins
))
249 yield jtag
.shift_data(chain(*pinsdata
))
252 wrap
.info("After scan")
254 for i
, pin
in enumerate(pins
):
255 pin
.check(wrap
=wrap
, i
=i
%2, o
=((i
%3)%2), oe
=((i
%5)%2))
260 wrap
.info("After reset")
265 def boundary_scan_reset(dut
):
266 clk_period
= 100 # 10MHz
267 tck_period
= 300 # 3MHz
269 info
= "Running boundary scan test; cpu in reset..."
270 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
272 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
274 yield from boundary_scan(wrap
, jtag
=jtag
)
276 wrap
.info("IDCODE test completed")
279 def boundary_scan_run(dut
):
280 clk_period
= 100 # 10MHz
281 tck_period
= 300 # 3MHz
283 info
= "Running boundary scan test; cpu running..."
284 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
286 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
288 yield from boundary_scan(wrap
, jtag
=jtag
)
290 wrap
.info("IDCODE test completed")
293 # demo / debug how to get boundary scan names. run "python3 test.py"
294 if __name__
== '__main__':
295 pinouts
= get_jtag_boundary()
297 # example: ('eint', '2', <IOType.In: 1>, 'eint_2', 125)