462fc4928e9b21f0c3cee552be9d76a5f054df3a
[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 yield jtag.reset()
55
56 jtag_svf = SVF_Executor(jtag)
57 with open(svf_filename, "r") as f:
58 svf_deck = f.read()
59 yield jtag_svf.run(svf_deck, p=dut._log.info)
60
61 #
62 # IDCODE using JTAG_master
63 #
64
65 def idcode(dut, *, jtag):
66 yield jtag.idcode()
67 result1 = jtag.result
68 dut._log.info("IDCODE1: {}".format(result1))
69 assert(result1 == BinaryValue("00000000000000000001100011111111"))
70
71 yield jtag.idcode()
72 result2 = jtag.result
73 dut._log.info("IDCODE2: {}".format(result2))
74
75 assert(result1 == result2)
76
77 @cocotb.test()
78 def idcode_reset(dut):
79 dut._log.info("Running IDCODE test; cpu in reset...")
80
81 clk_period = 100 # 10MHz
82 tck_period = 300 # 3MHz
83
84 yield from setup_sim(dut, clk_period=clk_period, run=False)
85 jtag = yield from setup_jtag(dut, tck_period = tck_period)
86
87 yield from idcode(dut, jtag=jtag)
88
89 dut._log.info("IDCODE test completed")
90
91 @cocotb.test()
92 def idcode_run(dut):
93 dut._log.info("Running IDCODE test; cpu running...")
94
95 clk_period = 100 # 10MHz
96 tck_period = 300 # 3MHz
97
98 yield from setup_sim(dut, clk_period=clk_period, run=True)
99 jtag = yield from setup_jtag(dut, tck_period = tck_period)
100
101 yield from idcode(dut, jtag=jtag)
102
103 dut._log.info("IDCODE test completed")
104
105 #
106 # Read IDCODE from SVF file
107 #
108
109 @cocotb.test()
110 def idcodesvf_reset(dut):
111 dut._log.info("Running IDCODE through SVF test; cpu in reset...")
112
113 clk_period = 100 # 10MHz
114 tck_period = 300 # 3MHz
115
116 yield from setup_sim(dut, clk_period=clk_period, run=False)
117 jtag = yield from setup_jtag(dut, tck_period = tck_period)
118
119 yield from execute_svf(dut, jtag=jtag, svf_filename="idcode.svf")
120
121 dut._log.info("IDCODE test completed")
122
123 @cocotb.test()
124 def idcodesvf_run(dut):
125 dut._log.info("Running IDCODE through SVF test; cpu running...")
126
127 clk_period = 100 # 10MHz
128 tck_period = 300 # 3MHz
129
130 yield from setup_sim(dut, clk_period=clk_period, run=True)
131 jtag = yield from setup_jtag(dut, tck_period = tck_period)
132
133 yield from execute_svf(dut, jtag=jtag, svf_filename="idcode.svf")
134
135 dut._log.info("IDCODE test completed")
136
137
138 # demo / debug how to get boundary scan names. run "python3 test.py"
139 if __name__ == '__main__':
140 pinouts = get_jtag_boundary()
141 for pin in pinouts:
142 # example: ('eint', '2', <IOType.In: 1>, 'eint_2', 125)
143 print (pin)