90a7158c58cb7bcdcda1152827489558a9899aee
1 """Simple GPIO peripheral on wishbone
3 This is an extremely simple GPIO peripheral intended for use in XICS
4 testing, however it could also be used as an actual GPIO peripheral
6 Modified for use with pinmux, will probably change the class name later.
8 from random
import randint
, shuffle
9 #from math import ceil, floor
10 from nmigen
import Elaboratable
, Module
, Signal
, Record
, Array
, Cat
11 from nmigen
.hdl
.rec
import Layout
12 from nmigen
.utils
import log2_int
13 from nmigen
.cli
import rtlil
14 #from soc.minerva.wishbone import make_wb_layout
15 from nmutil
.util
import wrap
16 #from soc.bus.test.wb_rw import wb_read, wb_write
18 from nmutil
.gtkw
import write_gtkw
22 from nmigen
.sim
.cxxsim
import Simulator
, Settle
, Delay
24 from nmigen
.sim
import Simulator
, Settle
, Delay
26 io_layout
= (("i", 1),
31 # This block produces an N-to-1 mux with N 3-bit periph ports and one pad port.
32 # The peripheral ports are intended to be wired to peripheral functions,
33 # while the pad port will connect to the I/O pad.
34 # Peripheral and output ports have o/oe/i signals, and the port signal is used
35 # to select between the peripheral ports.
36 class IOMuxBlockSingle(Elaboratable
):
38 def __init__(self
, n_ports
=4):
39 print("1-bit IO Mux Block")
40 self
.n_ports
= n_ports
41 self
.port
= Signal(log2_int(self
.n_ports
))
44 for i
in range(self
.n_ports
):
46 temp
.append(Record(name
=name
, layout
=io_layout
))
47 self
.periph_ports
= Array(temp
)
49 self
.out_port
= Record(name
="IO", layout
=io_layout
)
51 def elaborate(self
, platform
):
53 comb
, sync
= m
.d
.comb
, m
.d
.sync
56 periph_ports
= self
.periph_ports
57 out_port
= self
.out_port
59 # Connect IO Pad output port to one of the peripheral IOs
60 # Connect peripheral inputs to the IO pad input
61 comb
+= self
.out_port
.o
.eq(self
.periph_ports
[port
].o
)
62 comb
+= self
.out_port
.oe
.eq(self
.periph_ports
[port
].oe
)
64 comb
+= self
.periph_ports
[port
].i
.eq(self
.out_port
.i
)
68 def connect_port_to_io(self
, domain
, port_arg
):
69 domain
+= self
.out_port
.o
.eq(self
.periph_ports
[port_arg
].o
)
70 domain
+= self
.out_port
.oe
.eq(self
.periph_ports
[port_arg
].oe
)
71 domain
+= self
.periph_ports
[port_arg
].i
.eq(self
.out_port
.i
)
74 """ Get member signals for Verilog form. """
75 for field
in self
.out_port
.fields
.values():
77 for port
in range(self
.n_ports
):
78 for field
in self
.periph_ports
[port
].fields
.values():
85 # Method to test a particular peripheral port
86 # when rand_order is True, previous and consecutive ports are
87 # random (but NOT equal to given port)
88 def test_single_port(dut
, port
, rand_order
=True, delay
=1e-6):
90 print("Randomising the prev and next ports")
92 while(prev_port
== port
):
93 prev_port
= randint(0, dut
.n_ports
-1)
95 while(next_port
== port
):
96 next_port
= randint(0, dut
.n_ports
-1)
98 # Set the prev and next ports as consecutive ports
100 prev_port
= dut
.n_ports
- 1
104 if port
== dut
.n_ports
:
109 print("Prev=%d, Given=%d, Next=%d" % (prev_port
, port
, next_port
))
111 # Clear o/oe, delay, set port i
112 # Set to previous port, delay
114 # Set to desired port
118 # Set to next port, delay
120 yield dut
.periph_ports
[port
].o
.eq(0)
122 yield dut
.periph_ports
[port
].oe
.eq(0)
124 yield dut
.out_port
.i
.eq(1)
127 yield dut
.port
.eq(prev_port
)
130 test_i
= yield dut
.periph_ports
[port
].i
133 yield dut
.port
.eq(port
)
136 test_o
= yield dut
.out_port
.o
137 test_oe
= yield dut
.out_port
.oe
138 test_i
= yield dut
.periph_ports
[port
].i
143 yield dut
.periph_ports
[port
].o
.eq(1)
145 yield dut
.periph_ports
[port
].oe
.eq(1)
148 test_o
= yield dut
.out_port
.o
149 test_oe
= yield dut
.out_port
.oe
153 yield dut
.port
.eq(next_port
)
156 test_i
= yield dut
.periph_ports
[port
].i
159 def test_iomux(dut
, rand_order
=True):
160 print("------START----------------------")
161 #print(dir(dut.periph_ports[0]))
162 #print(dut.periph_ports[0].fields)
164 # Produce a test list of port values
165 test_port_vec
= list(range(0, dut
.n_ports
))
166 #print(test_port_vec)
167 # Randomise for wider testing
169 shuffle(test_port_vec
)
170 #print(test_port_vec)
171 for i
in range(dut
.n_ports
):
172 yield from test_single_port(dut
, test_port_vec
[i
], rand_order
)
174 print("Finished the 1-bit IO mux block test!")
176 def gen_gtkw_doc(module_name
, n_ports
, filename
):
177 # GTKWave doc generation
180 'in': {'color': 'orange'},
181 'out': {'color': 'yellow'},
182 'debug': {'module': 'top', 'color': 'red'}
185 # Create a trace list, each block expected to be a tuple()
187 for port
in range(0, n_ports
):
188 temp_traces
= ('Bank%d' % port
, [
189 ('port%d__i' % port
, 'in'),
190 ('port%d__o' % port
, 'out'),
191 ('port%d__oe' % port
, 'out')
193 traces
.append(temp_traces
)
195 temp_traces
= ('Misc', [
196 ('port[%d:0]' % ((n_ports
-1).bit_length()-1), 'in')
198 traces
.append(temp_traces
)
199 temp_traces
= ('IO port to pad', [
204 traces
.append(temp_traces
)
207 write_gtkw(filename
+".gtkw", filename
+".vcd", traces
, style
,
210 def sim_iomux(rand_order
=True):
211 filename
= "test_iomux" # Doesn't include extension
213 dut
= IOMuxBlockSingle(n_ports
)
214 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
215 with
open(filename
+".il", "w") as f
:
219 m
.submodules
.pinmux
= dut
223 sim
.add_process(wrap(test_iomux(dut
, rand_order
)))
224 sim_writer
= sim
.write_vcd(filename
+".vcd")
228 gen_gtkw_doc("top.pinmux", dut
.n_ports
, filename
)
232 if __name__
== '__main__':
233 sim_iomux(rand_order
=True)