c01bad9e29aed74e7c8243e81e8aa520c51e2ff7
[soc-cocotb-sim.git] / ls180 / cocotb / 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 #
11 # Helper functions
12 #
13
14 def setup_sim(dut, *, clk_period, run):
15 """Initialize CPU and setup clock"""
16
17 clk_steps = get_sim_steps(clk_period, "ns")
18 cocotb.fork(Clock(dut.clk_from_pad, clk_steps).start())
19
20 dut.rst_from_pad <= 1
21 dut.clk_from_pad <= 0
22 if run:
23 yield Timer(int(10.5*clk_steps))
24 dut.rst_from_pad <= 0
25 yield Timer(int(5*clk_steps))
26
27 def setup_jtag(dut, *, tck_period):
28 # Make this a generator
29 if False:
30 yield Timer(0)
31 return JTAG_Master(dut.tck_from_pad, dut.tms_from_pad,
32 dut.tdi_from_pad, dut.tdo_to_pad,
33 clk_period=tck_period)
34
35 def execute_svf(dut, *, jtag, svf_filename):
36 jtag_svf = SVF_Executor(jtag)
37 with open(svf_filename, "r") as f:
38 svf_deck = f.read()
39 yield jtag_svf.run(svf_deck, p=dut._log.info)
40
41 #
42 # IDCODE using JTAG_master
43 #
44
45 def idcode(dut, *, jtag):
46 jtag.IDCODE = [0, 0, 0, 1]
47 yield jtag.idcode()
48 result1 = jtag.result
49 dut._log.info("IDCODE1: {}".format(result1))
50 assert(result1 == BinaryValue("00000000000000000001100011111111"))
51
52 yield jtag.idcode()
53 result2 = jtag.result
54 dut._log.info("IDCODE2: {}".format(result2))
55
56 assert(result1 == result2)
57
58 @cocotb.test()
59 def idcode_reset(dut):
60 dut._log.info("Running IDCODE test; cpu in reset...")
61
62 clk_period = 100 # 10MHz
63 tck_period = 300 # 3MHz
64
65 yield from setup_sim(dut, clk_period=clk_period, run=False)
66 jtag = yield from setup_jtag(dut, tck_period = tck_period)
67
68 yield from idcode(dut, jtag=jtag)
69
70 dut._log.info("IDCODE test completed")
71
72 @cocotb.test()
73 def idcode_run(dut):
74 dut._log.info("Running IDCODE test; cpu running...")
75
76 clk_period = 100 # 10MHz
77 tck_period = 300 # 3MHz
78
79 yield from setup_sim(dut, clk_period=clk_period, run=True)
80 jtag = yield from setup_jtag(dut, tck_period = tck_period)
81
82 yield from idcode(dut, jtag=jtag)
83
84 dut._log.info("IDCODE test completed")
85
86 #
87 # Read IDCODE from SVF file
88 #
89
90 @cocotb.test()
91 def idcodesvf_reset(dut):
92 dut._log.info("Running IDCODE through SVF test; cpu in reset...")
93
94 clk_period = 100 # 10MHz
95 tck_period = 300 # 3MHz
96
97 yield from setup_sim(dut, clk_period=clk_period, run=False)
98 jtag = yield from setup_jtag(dut, tck_period = tck_period)
99
100 yield from execute_svf(dut, jtag=jtag, svf_filename="idcode.svf")
101
102 dut._log.info("IDCODE test completed")
103
104 @cocotb.test()
105 def idcode_run(dut):
106 dut._log.info("Running IDCODE through test; cpu running...")
107
108 clk_period = 100 # 10MHz
109 tck_period = 300 # 3MHz
110
111 yield from setup_sim(dut, clk_period=clk_period, run=True)
112 jtag = yield from setup_jtag(dut, tck_period = tck_period)
113
114 yield from execute_svf(dut, jtag=jtag, svf_filename="idcode.svf")
115
116 dut._log.info("IDCODE test completed")
117