2 from nmigen
.build
.dsl
import Resource
, Subsignal
, Pins
3 from nmigen
.build
.plat
import TemplatedPlatform
4 from nmigen
.build
.res
import ResourceManager
, ResourceError
5 from nmigen
.hdl
.rec
import Layout
6 from nmigen
import Elaboratable
, Signal
, Module
, Instance
7 from collections
import OrderedDict
8 from jtag
import JTAG
, resiotypes
9 from copy
import deepcopy
10 from nmigen
.cli
import rtlil
13 # extra dependencies for jtag testing (?)
14 from soc
.bus
.sram
import SRAM
16 from nmigen
import Memory
17 from nmigen
.sim
import Simulator
, Delay
, Settle
, Tick
19 from nmutil
.util
import wrap
21 from soc
.debug
.jtagutils
import (jtag_read_write_reg
,
22 jtag_srv
, jtag_set_reset
,
23 jtag_set_ir
, jtag_set_get_dr
)
25 from c4m
.nmigen
.jtag
.tap
import TAP
, IOType
26 from c4m
.nmigen
.jtag
.bus
import Interface
as JTAGInterface
27 from soc
.debug
.dmi
import DMIInterface
, DBGCore
28 from soc
.debug
.test
.dmi_sim
import dmi_sim
29 from soc
.debug
.test
.jtagremote
import JTAGServer
, JTAGClient
30 from nmigen
.build
.res
import ResourceError
32 # Was thinking of using these functions, but skipped for simplicity for now
33 # XXX nope. the output from JSON file.
34 #from pinfunctions import (i2s, lpc, emmc, sdmmc, mspi, mquadspi, spi,
35 # quadspi, i2c, mi2c, jtag, uart, uartfull, rgbttl, ulpi, rgmii, flexbus1,
36 # flexbus2, sdram1, sdram2, sdram3, vss, vdd, sys, eint, pwm, gpio)
38 # File for stage 1 pinmux tested proposed by Luke,
39 # https://bugs.libre-soc.org/show_bug.cgi?id=50#c10
43 # sigh this needs to come from pinmux.
46 gpios
.append("%d*" % i
)
47 return {'uart': ['tx+', 'rx-'],
49 #'jtag': ['tms-', 'tdi-', 'tdo+', 'tck+'],
50 'i2c': ['sda*', 'scl+']}
53 a function is needed which turns the results of dummy_pinset()
56 [UARTResource("uart", 0, tx=..., rx=..),
57 I2CResource("i2c", 0, scl=..., sda=...),
58 Resource("gpio", 0, Subsignal("i"...), Subsignal("o"...)
59 Resource("gpio", 1, Subsignal("i"...), Subsignal("o"...)
65 def create_resources(pinset
):
67 for periph
, pins
in pinset
.items():
70 #print("I2C required!")
71 resources
.append(I2CResource('i2c', 0, sda
='sda', scl
='scl'))
72 elif periph
== 'uart':
73 #print("UART required!")
74 resources
.append(UARTResource('uart', 0, tx
='tx', rx
='rx'))
75 elif periph
== 'gpio':
76 #print("GPIO required!")
77 print ("GPIO is defined as '*' type, meaning i, o and oe needed")
80 pname
= "gpio"+pin
[:-1] # strip "*" on end
81 # urrrr... tristsate and io assume a single pin which is
82 # of course exactly what we don't want in an ASIC: we want
83 # *all three* pins but the damn port is not outputted
84 # as a triplet, it's a single Record named "io". sigh.
85 # therefore the only way to get a triplet of i/o/oe
86 # is to *actually* create explicit triple pins
87 # XXX ARRRGH, doesn't work
88 #pad = Subsignal("io",
89 # Pins("%s_i %s_o %s_oe" % (pname, pname, pname),
90 # dir="io", assert_width=3))
91 #ios.append(Resource(pname, 0, pad))
93 pads
.append(Subsignal("i",
94 Pins(pname
+"_i", dir="i", assert_width
=1)))
95 pads
.append(Subsignal("o",
96 Pins(pname
+"_o", dir="o", assert_width
=1)))
97 pads
.append(Subsignal("oe",
98 Pins(pname
+"_oe", dir="o", assert_width
=1)))
99 ios
.append(Resource
.family(pname
, 0, default_name
=pname
,
101 resources
.append(Resource
.family(periph
, 0, default_name
="gpio",
104 # add clock and reset
105 clk
= Resource("clk", 0, Pins("sys_clk", dir="i"))
106 rst
= Resource("rst", 0, Pins("sys_rst", dir="i"))
107 resources
.append(clk
)
108 resources
.append(rst
)
112 def JTAGResource(*args
):
114 io
.append(Subsignal("tms", Pins("tms", dir="i", assert_width
=1)))
115 io
.append(Subsignal("tdi", Pins("tdi", dir="i", assert_width
=1)))
116 io
.append(Subsignal("tck", Pins("tck", dir="i", assert_width
=1)))
117 io
.append(Subsignal("tdo", Pins("tdo", dir="o", assert_width
=1)))
118 return Resource
.family(*args
, default_name
="jtag", ios
=io
)
120 def UARTResource(*args
, rx
, tx
):
122 io
.append(Subsignal("rx", Pins(rx
, dir="i", assert_width
=1)))
123 io
.append(Subsignal("tx", Pins(tx
, dir="o", assert_width
=1)))
124 return Resource
.family(*args
, default_name
="uart", ios
=io
)
127 def I2CResource(*args
, scl
, sda
):
130 pads
.append(Subsignal("i", Pins(sda
+"_i", dir="i", assert_width
=1)))
131 pads
.append(Subsignal("o", Pins(sda
+"_o", dir="o", assert_width
=1)))
132 pads
.append(Subsignal("oe", Pins(sda
+"_oe", dir="o", assert_width
=1)))
133 ios
.append(Resource
.family(sda
, 0, default_name
=sda
, ios
=pads
))
135 pads
.append(Subsignal("i", Pins(scl
+"_i", dir="i", assert_width
=1)))
136 pads
.append(Subsignal("o", Pins(scl
+"_o", dir="o", assert_width
=1)))
137 pads
.append(Subsignal("oe", Pins(scl
+"_oe", dir="o", assert_width
=1)))
138 ios
.append(Resource
.family(scl
, 0, default_name
=scl
, ios
=pads
))
139 return Resource
.family(*args
, default_name
="i2c", ios
=ios
)
142 # top-level demo module.
143 class Blinker(Elaboratable
):
144 def __init__(self
, pinset
, resources
):
145 self
.jtag
= JTAG({}, "sync", resources
=resources
)
146 memory
= Memory(width
=32, depth
=16)
147 self
.sram
= SRAM(memory
=memory
, bus
=self
.jtag
.wb
)
149 def elaborate(self
, platform
):
150 jtag_resources
= self
.jtag
.pad_mgr
.resources
152 m
.submodules
.jtag
= self
.jtag
153 m
.submodules
.sram
= self
.sram
156 m
.d
.sync
+= count
.eq(count
+1)
157 print ("resources", platform
, jtag_resources
.items())
158 gpio
= self
.jtag
.request('gpio')
159 print (gpio
, gpio
.layout
, gpio
.fields
)
160 # get the GPIO bank, mess about with some of the pins
161 m
.d
.comb
+= gpio
.gpio0
.o
.eq(1)
162 m
.d
.comb
+= gpio
.gpio1
.o
.eq(gpio
.gpio2
.i
)
163 m
.d
.comb
+= gpio
.gpio1
.oe
.eq(count
[4])
164 m
.d
.sync
+= count
[0].eq(gpio
.gpio1
.i
)
165 # get the UART resource, mess with the output tx
166 uart
= self
.jtag
.request('uart')
167 print (uart
, uart
.fields
)
168 intermediary
= Signal()
169 m
.d
.comb
+= uart
.tx
.eq(intermediary
)
170 m
.d
.comb
+= intermediary
.eq(uart
.rx
)
172 return self
.jtag
.boundary_elaborate(m
, platform
)
178 yield from self
.jtag
.iter_ports()
181 _trellis_command_templates = [
183 {{invoke_tool("yosys")}}
185 {{get_override("yosys_opts")|options}}
192 # sigh, have to create a dummy platform for now.
193 # TODO: investigate how the heck to get it to output ilang. or verilog.
194 # or, anything, really. but at least it doesn't barf
195 class ASICPlatform(TemplatedPlatform
):
197 resources
= OrderedDict()
199 command_templates
= ['/bin/true'] # no command needed: stops barfing
201 **TemplatedPlatform
.build_script_templates
,
206 "{{name}}.debug.v": r
"""
207 /* {{autogenerated}} */
208 {{emit_debug_verilog()}}
212 default_clk
= "clk" # should be picked up / overridden by platform sys.clk
213 default_rst
= "rst" # should be picked up / overridden by platform sys.rst
215 def __init__(self
, resources
, jtag
):
219 # create set of pin resources based on the pinset, this is for the core
220 #jtag_resources = self.jtag.pad_mgr.resources
221 self
.add_resources(resources
)
223 # add JTAG without scan
224 self
.add_resources([JTAGResource('jtag', 0)], no_boundary_scan
=True)
226 def add_resources(self
, resources
, no_boundary_scan
=False):
227 print ("ASICPlatform add_resources", resources
)
228 return super().add_resources(resources
)
230 #def iter_ports(self):
231 # yield from super().iter_ports()
232 # for io in self.jtag.ios.values():
233 # print ("iter ports", io.layout, io)
234 # for field in io.core.fields:
235 # yield getattr(io.core, field)
236 # for field in io.pad.fields:
237 # yield getattr(io.pad, field)
239 # XXX these aren't strictly necessary right now but the next
240 # phase is to add JTAG Boundary Scan so it maaay be worth adding?
241 # at least for the print statements
242 def get_input(self
, pin
, port
, attrs
, invert
):
243 self
._check
_feature
("single-ended input", pin
, attrs
,
244 valid_xdrs
=(0,), valid_attrs
=None)
247 print (" get_input", pin
, "port", port
, port
.layout
)
248 m
.d
.comb
+= pin
.i
.eq(self
._invert
_if
(invert
, port
))
251 def get_output(self
, pin
, port
, attrs
, invert
):
252 self
._check
_feature
("single-ended output", pin
, attrs
,
253 valid_xdrs
=(0,), valid_attrs
=None)
256 print (" get_output", pin
, "port", port
, port
.layout
)
257 m
.d
.comb
+= port
.eq(self
._invert
_if
(invert
, pin
.o
))
260 def get_tristate(self
, pin
, port
, attrs
, invert
):
261 self
._check
_feature
("single-ended tristate", pin
, attrs
,
262 valid_xdrs
=(0,), valid_attrs
=None)
264 print (" get_tristate", pin
, "port", port
, port
.layout
)
266 print (" pad", pin
, port
, attrs
)
267 print (" pin", pin
.layout
)
269 # m.submodules += Instance("$tribuf",
272 # i_A=self._invert_if(invert, pin.o),
275 m
.d
.comb
+= io
.core
.o
.eq(pin
.o
)
276 m
.d
.comb
+= io
.core
.oe
.eq(pin
.oe
)
277 m
.d
.comb
+= pin
.i
.eq(io
.core
.i
)
278 m
.d
.comb
+= io
.pad
.i
.eq(port
.i
)
279 m
.d
.comb
+= port
.o
.eq(io
.pad
.o
)
280 m
.d
.comb
+= port
.oe
.eq(io
.pad
.oe
)
283 def get_input_output(self
, pin
, port
, attrs
, invert
):
284 self
._check
_feature
("single-ended input/output", pin
, attrs
,
285 valid_xdrs
=(0,), valid_attrs
=None)
287 print (" get_input_output", pin
, "port", port
, port
.layout
)
289 print (" port layout", port
.layout
)
291 print (" layout", pin
.layout
)
292 #m.submodules += Instance("$tribuf",
295 # i_A=self._invert_if(invert, io.pad.o),
298 # Create aliases for the port sub-signals
303 m
.d
.comb
+= pin
.i
.eq(self
._invert
_if
(invert
, port_i
))
304 m
.d
.comb
+= port_o
.eq(self
._invert
_if
(invert
, pin
.o
))
305 m
.d
.comb
+= port_oe
.eq(pin
.oe
)
309 def toolchain_prepare(self
, fragment
, name
, **kwargs
):
310 """override toolchain_prepare in order to grab the fragment
312 self
.fragment
= fragment
313 return super().toolchain_prepare(fragment
, name
, **kwargs
)
316 and to create a Platform instance with that list, and build
320 p.resources=listofstuff
323 pinset
= dummy_pinset()
325 resources
= create_resources(pinset
)
326 top
= Blinker(pinset
, resources
)
328 vl
= rtlil
.convert(top
, ports
=top
.ports())
329 with
open("test_jtag_blinker.il", "w") as f
:
333 # XXX these modules are all being added *AFTER* the build process links
334 # everything together. the expectation that this would work is...
335 # unrealistic. ordering, clearly, is important.
337 # dut = JTAG(test_pinset(), wb_data_wid=64, domain="sync")
338 top
.jtag
.stop
= False
339 # rather than the client access the JTAG bus directly
340 # create an alternative that the client sets
343 cdut
.cbus
= JTAGInterface()
345 # set up client-server on port 44843-something
346 top
.jtag
.s
= JTAGServer()
347 cdut
.c
= JTAGClient()
348 top
.jtag
.s
.get_connection()
350 # print ("running server only as requested, use openocd remote to test")
352 # top.jtag.s.get_connection(None) # block waiting for connection
354 # take copy of ir_width and scan_len
355 cdut
._ir
_width
= top
.jtag
._ir
_width
356 cdut
.scan_len
= top
.jtag
.scan_len
358 p
= ASICPlatform (resources
, top
.jtag
)
360 # this is what needs to gets treated as "top", after "main module" top
361 # is augmented with IO pads with JTAG tacked on. the expectation that
362 # the get_input() etc functions will be called magically by some other
363 # function is unrealistic.
364 top_fragment
= p
.fragment
366 # XXX simulating top (the module that does not itself contain IO pads
367 # because that's covered by build) cannot possibly be expected to work
368 # particularly when modules have been added *after* the platform build()
369 # function has been called.
372 sim
.add_clock(1e-6, domain
="sync") # standard clock
374 sim
.add_sync_process(wrap(jtag_srv(top
))) #? jtag server
375 #if len(sys.argv) != 2 or sys.argv[1] != 'server':
376 sim
.add_sync_process(wrap(jtag_sim(cdut
, top
.jtag
))) # actual jtag tester
377 sim
.add_sync_process(wrap(dmi_sim(top
.jtag
))) # handles (pretends to be) DMI
379 with sim
.write_vcd("dmi2jtag_test_srv.vcd"):