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}), " \
59 "pad.(o={pad_o}, oe={pad_oe})")
60 elif self
.type_
== IOType
.InTriOut
:
61 core_i
= getattr(wrap
.ti
, f
"{self.name}__core__i").value
62 core_o
= getattr(wrap
.ti
, f
"{self.name}__core__o").value
63 core_oe
= getattr(wrap
.ti
, f
"{self.name}__core__oe").value
64 pad_i
= getattr(wrap
.ti
, f
"{self.name}__pad__i").value
65 pad_o
= getattr(wrap
.ti
, f
"{self.name}__pad__o").value
66 pad_oe
= getattr(wrap
.ti
, f
"{self.name}__pad__oe").value
67 wrap
.info(f
"{self.name}: core.(i={core_i}, o={core_o}, " \
68 "oe={core_oe}), pad.(i={core_i}, o={pad_o}, " \
71 raise ValueError(f
"Unsupported pin type {self.type_}")
73 def data(self
, *, i
=None, o
=None, oe
=None):
74 if self
.type_
== IOType
.In
:
77 elif self
.type_
== IOType
.Out
:
80 elif self
.type_
== IOType
.TriOut
:
81 assert (o
is not None) and (oe
is not None)
83 elif self
.type_
== IOType
.InTriOut
:
84 assert (i
is not None) and(o
is not None) and (oe
is not None)
87 raise ValueError(f
"Unsupported pin type {self.type_}")
89 def check(self
, *, wrap
, i
=None, o
=None, oe
=None):
90 if self
.type_
in (IOType
.In
, IOType
.InTriOut
):
91 sig
= f
"{self.name}__core__i"
92 val
= getattr(wrap
.ti
, sig
).value
94 raise ValueError(f
"'{sig}' should be {i}, not {val}")
95 if self
.type_
in (IOType
.Out
, IOType
.TriOut
, IOType
.InTriOut
):
96 sig
= f
"{self.name}__pad__o"
97 val
= getattr(wrap
.ti
, sig
).value
99 raise ValueError(f
"'{sig}' should be {o}, not {val}")
100 if self
.type_
in (IOType
.TriOut
, IOType
.InTriOut
):
101 sig
= f
"{self.name}__pad__oe"
102 val
= getattr(wrap
.ti
, sig
).value
104 raise ValueError(f
"'{sig}' should be {oe}, not {val}")
107 def log_pins(wrap
, pins
):
112 def get_jtag_boundary():
113 """gets the list of information for jtag boundary scan
115 # currently only a subset of pins is enabled. nuisance
118 'eint', 'gpio', 'mspi0',
119 # 'mspi1', - disabled for now
120 # 'pwm', 'sd0', - disabled for now
122 pins
= tuple(JTAGPin(pin
) for pin
in Pins(get_pinspecs(subset
=subset
)))
126 def setup_sim(dut
, *, info
, clk_period
, run
):
127 """Initialize CPU and setup clock"""
129 wrap
= DUTWrapper(dut
)
132 clk_steps
= get_sim_steps(clk_period
, "ns")
133 cocotb
.fork(Clock(wrap
.clk
, clk_steps
).start())
138 yield Timer(int(10.5*clk_steps
))
140 yield Timer(int(5*clk_steps
))
144 def setup_jtag(wrap
, *, tck_period
):
145 # Make this a generator
148 return JTAG_Master(wrap
.tck
, wrap
.tms
, wrap
.tdi
, wrap
.tdo
,
149 clk_period
=tck_period
,
152 def execute_svf(wrap
, *, jtag
, svf_filename
):
155 jtag_svf
= SVF_Executor(jtag
)
156 with
open(svf_filename
, "r") as f
:
158 yield jtag_svf
.run(svf_deck
, p
=wrap
.info
)
161 # IDCODE using JTAG_master
164 def idcode(wrap
, *, jtag
):
166 result1
= jtag
.result
167 wrap
.info("IDCODE1: {}".format(result1
))
168 assert(result1
== BinaryValue("00000000000000000001100011111111"))
171 result2
= jtag
.result
172 wrap
.info("IDCODE2: {}".format(result2
))
174 assert(result1
== result2
)
177 def idcode_reset(dut
):
178 clk_period
= 100 # 10MHz
179 tck_period
= 300 # 3MHz
181 info
= "Running IDCODE test; cpu in reset..."
182 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
184 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
186 yield from idcode(wrap
, jtag
=jtag
)
188 wrap
.info("IDCODE test completed")
192 clk_period
= 100 # 10MHz
193 tck_period
= 300 # 3MHz
195 info
= "Running IDCODE test; cpu running..."
196 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
198 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
200 yield from idcode(wrap
, jtag
=jtag
)
202 wrap
.info("IDCODE test completed")
205 # Read IDCODE from SVF file
209 def idcodesvf_reset(dut
):
210 clk_period
= 100 # 10MHz
211 tck_period
= 300 # 3MHz
213 info
= "Running IDCODE through SVF test; cpu in reset..."
214 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
216 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
218 yield from execute_svf(wrap
, jtag
=jtag
, svf_filename
="idcode.svf")
220 wrap
.info("IDCODE test completed")
223 def idcodesvf_run(dut
):
224 clk_period
= 100 # 10MHz
225 tck_period
= 300 # 3MHz
227 info
= "Running IDCODE through SVF test; cpu running..."
228 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
230 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
232 yield from execute_svf(wrap
, jtag
=jtag
, svf_filename
="idcode.svf")
234 wrap
.info("IDCODE test completed")
240 def boundary_scan(wrap
, *, jtag
):
241 pins
= get_jtag_boundary()
246 wrap
.info("Before scan")
249 yield jtag
.load_ir([0, 0, 0, 0])
250 pinsdata
= tuple(pin
.data(i
=i
%2, o
=((i
%3)%2), oe
=((i
%5)%2))
251 for i
, pin
in enumerate(pins
))
252 yield jtag
.shift_data(chain(*pinsdata
))
255 wrap
.info("After scan")
257 for i
, pin
in enumerate(pins
):
258 pin
.check(wrap
=wrap
, i
=i
%2, o
=((i
%3)%2), oe
=((i
%5)%2))
263 wrap
.info("After reset")
268 def boundary_scan_reset(dut
):
269 clk_period
= 100 # 10MHz
270 tck_period
= 300 # 3MHz
272 info
= "Running boundary scan test; cpu in reset..."
273 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
275 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
277 yield from boundary_scan(wrap
, jtag
=jtag
)
279 wrap
.info("IDCODE test completed")
282 def boundary_scan_run(dut
):
283 clk_period
= 100 # 10MHz
284 tck_period
= 300 # 3MHz
286 info
= "Running boundary scan test; cpu running..."
287 wrap
= yield from setup_sim(dut
, info
=info
, clk_period
=clk_period
,
289 jtag
= yield from setup_jtag(wrap
, tck_period
= tck_period
)
291 yield from boundary_scan(wrap
, jtag
=jtag
)
293 wrap
.info("IDCODE test completed")
296 # demo / debug how to get boundary scan names. run "python3 test.py"
297 if __name__
== '__main__':
298 pinouts
= get_jtag_boundary()
300 # example: ('eint', '2', <IOType.In: 1>, 'eint_2', 125)