TWI enabled in boundary scan
[soc-cocotb-sim.git] / ls180 / pre_pnr / test.py
1 import cocotb
2 from cocotb.clock import Clock
3 from cocotb.triggers import Timer
4 from cocotb.utils import get_sim_steps
5 from cocotb.binary import BinaryValue
6
7 from c4m.cocotb.jtag.c4m_jtag import JTAG_Master
8 from c4m.cocotb.jtag.c4m_jtag_svfcocotb import SVF_Executor
9
10 from soc.config.pinouts import get_pinspecs
11 from soc.debug.jtag import Pins
12
13 #
14 # Helper functions
15 #
16
17 def get_jtag_boundary():
18 """gets the list of information for jtag boundary scan
19 """
20 # currently only a subset of pins is enabled. nuisance
21 subset = ['uart',
22 'mtwi',
23 'eint', 'gpio', 'mspi0',
24 # 'mspi1', - disabled for now
25 # 'pwm', 'sd0', - disabled for now
26 'sdr']
27 pins = Pins(get_pinspecs(subset=subset))
28 return pins
29
30
31 def setup_sim(dut, *, clk_period, run):
32 """Initialize CPU and setup clock"""
33
34 clk_steps = get_sim_steps(clk_period, "ns")
35 cocotb.fork(Clock(dut.sys_clk, clk_steps).start())
36
37 dut.sys_rst <= 1
38 dut.sys_clk <= 0
39 if run:
40 yield Timer(int(10.5*clk_steps))
41 dut.sys_rst <= 0
42 yield Timer(int(5*clk_steps))
43
44 def setup_jtag(dut, *, tck_period):
45 # Make this a generator
46 if False:
47 yield Timer(0)
48 return JTAG_Master(dut.jtag_tck, dut.jtag_tms,
49 dut.jtag_tdi, dut.jtag_tdo,
50 clk_period=tck_period,
51 ir_width=4)
52
53 def execute_svf(dut, *, jtag, svf_filename):
54 jtag_svf = SVF_Executor(jtag)
55 with open(svf_filename, "r") as f:
56 svf_deck = f.read()
57 yield jtag_svf.run(svf_deck, p=dut._log.info)
58
59 #
60 # IDCODE using JTAG_master
61 #
62
63 def idcode(dut, *, jtag):
64 yield jtag.idcode()
65 result1 = jtag.result
66 dut._log.info("IDCODE1: {}".format(result1))
67 assert(result1 == BinaryValue("00000000000000000001100011111111"))
68
69 yield jtag.idcode()
70 result2 = jtag.result
71 dut._log.info("IDCODE2: {}".format(result2))
72
73 assert(result1 == result2)
74
75 @cocotb.test()
76 def idcode_reset(dut):
77 dut._log.info("Running IDCODE test; cpu in reset...")
78
79 clk_period = 100 # 10MHz
80 tck_period = 300 # 3MHz
81
82 yield from setup_sim(dut, clk_period=clk_period, run=False)
83 jtag = yield from setup_jtag(dut, tck_period = tck_period)
84
85 yield from idcode(dut, jtag=jtag)
86
87 dut._log.info("IDCODE test completed")
88
89 @cocotb.test()
90 def idcode_run(dut):
91 dut._log.info("Running IDCODE test; cpu running...")
92
93 clk_period = 100 # 10MHz
94 tck_period = 300 # 3MHz
95
96 yield from setup_sim(dut, clk_period=clk_period, run=True)
97 jtag = yield from setup_jtag(dut, tck_period = tck_period)
98
99 yield from idcode(dut, jtag=jtag)
100
101 dut._log.info("IDCODE test completed")
102
103 #
104 # Read IDCODE from SVF file
105 #
106
107 @cocotb.test()
108 def idcodesvf_reset(dut):
109 dut._log.info("Running IDCODE through SVF test; cpu in reset...")
110
111 clk_period = 100 # 10MHz
112 tck_period = 300 # 3MHz
113
114 yield from setup_sim(dut, clk_period=clk_period, run=False)
115 jtag = yield from setup_jtag(dut, tck_period = tck_period)
116
117 yield from execute_svf(dut, jtag=jtag, svf_filename="idcode.svf")
118
119 dut._log.info("IDCODE test completed")
120
121 @cocotb.test()
122 def idcode_run(dut):
123 dut._log.info("Running IDCODE through test; cpu running...")
124
125 clk_period = 100 # 10MHz
126 tck_period = 300 # 3MHz
127
128 yield from setup_sim(dut, clk_period=clk_period, run=True)
129 jtag = yield from setup_jtag(dut, tck_period = tck_period)
130
131 yield from execute_svf(dut, jtag=jtag, svf_filename="idcode.svf")
132
133 dut._log.info("IDCODE test completed")
134
135
136 # demo / debug how to get boundary scan names. run "python3 test.py"
137 if __name__ == '__main__':
138 pinouts = get_jtag_boundary()
139 for pin in pinouts:
140 # example: ('eint', '2', <IOType.In: 1>, 'eint_2', 125)
141 print (pin)