fbdf8447a8f0fc107bfc39e49c39d2782084f41e
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
import Elaboratable
, Signal
, Module
, Instance
6 from collections
import OrderedDict
7 from jtag
import JTAG
, resiotypes
8 from copy
import deepcopy
10 # Was thinking of using these functions, but skipped for simplicity for now
11 # XXX nope. the output from JSON file.
12 #from pinfunctions import (i2s, lpc, emmc, sdmmc, mspi, mquadspi, spi,
13 # quadspi, i2c, mi2c, jtag, uart, uartfull, rgbttl, ulpi, rgmii, flexbus1,
14 # flexbus2, sdram1, sdram2, sdram3, vss, vdd, sys, eint, pwm, gpio)
16 # File for stage 1 pinmux tested proposed by Luke,
17 # https://bugs.libre-soc.org/show_bug.cgi?id=50#c10
21 # sigh this needs to come from pinmux.
24 gpios
.append("%d*" % i
)
25 return {'uart': ['tx+', 'rx-'],
27 'i2c': ['sda*', 'scl+']}
30 a function is needed which turns the results of dummy_pinset()
33 [UARTResource("uart", 0, tx=..., rx=..),
34 I2CResource("i2c", 0, scl=..., sda=...),
35 Resource("gpio", 0, Subsignal("i"...), Subsignal("o"...)
36 Resource("gpio", 1, Subsignal("i"...), Subsignal("o"...)
42 def create_resources(pinset
):
44 for periph
, pins
in pinset
.items():
47 #print("I2C required!")
48 resources
.append(I2CResource('i2c', 0, sda
='sda', scl
='scl'))
49 elif periph
== 'uart':
50 #print("UART required!")
51 resources
.append(UARTResource('uart', 0, tx
='tx', rx
='rx'))
52 elif periph
== 'gpio':
53 #print("GPIO required!")
54 print ("GPIO is defined as '*' type, meaning i, o and oe needed")
57 pname
= "gpio"+pin
[:-1] # strip "*" on end
58 # urrrr... tristsate and io assume a single pin which is
59 # of course exactly what we don't want in an ASIC: we want
60 # *all three* pins but the damn port is not outputted
61 # as a triplet, it's a single Record named "io". sigh.
62 # therefore the only way to get a triplet of i/o/oe
63 # is to *actually* create explicit triple pins
65 Pins("%s_i %s_o %s_oe" % (pname
, pname
, pname
),
66 dir="io", assert_width
=3))
67 ios
.append(Resource(pname
, 0, pad
))
68 resources
.append(Resource
.family(periph
, 0, default_name
="gpio",
72 clk
= Resource("clk", 0, Pins("sys_clk", dir="i"))
73 rst
= Resource("rst", 0, Pins("sys_rst", dir="i"))
79 def UARTResource(*args
, rx
, tx
):
81 io
.append(Subsignal("rx", Pins(rx
, dir="i", assert_width
=1)))
82 io
.append(Subsignal("tx", Pins(tx
, dir="o", assert_width
=1)))
83 return Resource
.family(*args
, default_name
="uart", ios
=io
)
86 def I2CResource(*args
, scl
, sda
):
88 io
.append(Subsignal("scl", Pins(scl
, dir="io", assert_width
=1)))
89 io
.append(Subsignal("sda", Pins(sda
, dir="io", assert_width
=1)))
90 return Resource
.family(*args
, default_name
="i2c", ios
=io
)
93 # ridiculously-simple top-level module. doesn't even have a sync domain
94 # and can't have one until a clock has been established by ASICPlatform.
95 class Blinker(Elaboratable
):
96 def __init__(self
, pinset
):
97 self
.jtag
= JTAG({}, "sync")
99 def elaborate(self
, platform
):
101 m
.submodules
.jtag
= self
.jtag
103 m
.d
.sync
+= count
.eq(5)
104 print ("resources", platform
.resources
.items())
105 gpio
= platform
.request('gpio')
106 print (gpio
, gpio
.layout
, gpio
.fields
)
107 # get the GPIO bank, mess about with some of the pins
108 m
.d
.comb
+= gpio
.gpio0
.io
.o
.eq(1)
109 m
.d
.comb
+= gpio
.gpio1
.io
.o
.eq(gpio
.gpio2
.io
.i
)
110 m
.d
.comb
+= gpio
.gpio1
.io
.oe
.eq(count
[4])
111 m
.d
.sync
+= count
[0].eq(gpio
.gpio1
.io
.i
)
112 # get the UART resource, mess with the output tx
113 uart
= platform
.request('uart')
114 print (uart
, uart
.fields
)
115 m
.d
.comb
+= uart
.tx
.eq(uart
.rx
)
120 _trellis_command_templates = [
122 {{invoke_tool("yosys")}}
124 {{get_override("yosys_opts")|options}}
131 # sigh, have to create a dummy platform for now.
132 # TODO: investigate how the heck to get it to output ilang. or verilog.
133 # or, anything, really. but at least it doesn't barf
134 class ASICPlatform(TemplatedPlatform
):
136 resources
= OrderedDict()
138 command_templates
= ['/bin/true']
140 **TemplatedPlatform
.build_script_templates
,
145 "{{name}}.debug.v": r
"""
146 /* {{autogenerated}} */
147 {{emit_debug_verilog()}}
151 default_clk
= "clk" # should be picked up / overridden by platform sys.clk
152 default_rst
= "rst" # should be picked up / overridden by platform sys.rst
154 def __init__(self
, resources
, jtag
):
155 self
.pad_mgr
= ResourceManager([], [])
158 # create set of pin resources based on the pinset, this is for the core
159 self
.add_resources(resources
)
160 # record resource lookup between core IO names and pads
163 def request(self
, name
, number
=0, *, dir=None, xdr
=None):
164 """request a Resource (e.g. name="uart", number=0) which will
165 return a data structure containing Records of all the pins.
167 this override will also - automatically - create a JTAG Boundary Scan
168 connection *without* any change to the actual Platform.request() API
170 # okaaaay, bit of shenanigens going on: the important data structure
171 # here is Resourcemanager._ports. requests add to _ports, which is
172 # what needs redirecting. therefore what has to happen is to
173 # capture the number of ports *before* the request. sigh.
174 start_ports
= len(self
._ports
)
175 value
= super().request(name
, number
, dir=dir, xdr
=xdr
)
176 end_ports
= len(self
._ports
)
178 # now make a corresponding (duplicate) request to the pad manager
179 # BUT, if it doesn't exist, don't sweat it: all it means is, the
180 # application did not request Boundary Scan for that resource.
181 pad_start_ports
= len(self
.pad_mgr
._ports
)
183 pvalue
= self
.pad_mgr
.request(name
, number
, dir=dir, xdr
=xdr
)
184 except AssertionError:
186 pad_end_ports
= len(self
.pad_mgr
._ports
)
188 # ok now we have the lengths: now create a lookup between the pad
189 # and the core, so that JTAG boundary scan can be inserted in between
190 core
= self
._ports
[start_ports
:end_ports
]
191 pads
= self
.pad_mgr
._ports
[pad_start_ports
:pad_end_ports
]
192 # oops if not the same numbers added. it's a duplicate. shouldn't happen
193 assert len(core
) == len(pads
), "argh, resource manager error"
197 # pad/core each return a list of tuples of (res, pin, port, attrs)
198 for pad
, core
in zip(pads
, core
):
199 # create a lookup on pin name to get at the hidden pad instance
200 # this pin name will be handed to get_input, get_output etc.
201 # and without the padlookup you can't find the (duplicate) pad.
202 # note that self.padlookup and self.jtag.ios use the *exact* same
206 if pin
is None: continue # skip when pin is None
207 assert corepin
is not None # if pad was None, core should be too
208 print ("iter", pad
, pin
.name
)
209 print ("existing pads", self
.padlookup
.keys())
210 assert pin
.name
not in self
.padlookup
# no overwrites allowed!
211 assert pin
.name
== corepin
.name
# has to be the same!
212 self
.padlookup
[pin
.name
] = pad
# store pad by pin name
214 # now add the IO Shift Register. first identify the type
215 # then request a JTAG IOConn. we can't wire it up (yet) because
216 # we don't have a Module() instance. doh. that comes in get_input
217 # and get_output etc. etc.
218 iotype
= resiotypes
[pin
.dir] # look up the C4M-JTAG IOType
219 io
= self
.jtag
.add_io(iotype
=iotype
, name
=pin
.name
) # create IOConn
220 self
.jtag
.ios
[pin
.name
] = io
# store IOConn Record by pin name
222 # finally return the value just like ResourceManager.request()
225 def add_resources(self
, resources
, no_boundary_scan
=False):
226 super().add_resources(resources
)
229 # make a *second* - identical - set of pin resources for the IO ring
230 padres
= deepcopy(resources
)
231 self
.pad_mgr
.add_resources(padres
)
233 #def iter_ports(self):
234 # yield from super().iter_ports()
235 # for io in self.jtag.ios.values():
236 # print ("iter ports", io.layout, io)
237 # for field in io.core.fields:
238 # yield getattr(io.core, field)
239 # for field in io.pad.fields:
240 # yield getattr(io.pad, field)
242 # XXX these aren't strictly necessary right now but the next
243 # phase is to add JTAG Boundary Scan so it maaay be worth adding?
244 # at least for the print statements
245 def get_input(self
, pin
, port
, attrs
, invert
):
246 self
._check
_feature
("single-ended input", pin
, attrs
,
247 valid_xdrs
=(0,), valid_attrs
=None)
250 print (" get_input", pin
, "port", port
, port
.layout
)
251 if pin
.name
in ['clk_0', 'rst_0']: # sigh
252 # simple pass-through from port to pin
253 print("No JTAG chain in-between")
254 m
.d
.comb
+= pin
.i
.eq(self
._invert
_if
(invert
, port
))
256 (padres
, padpin
, padport
, padattrs
) = self
.padlookup
[pin
.name
]
257 io
= self
.jtag
.ios
[pin
.name
]
258 print (" pad", padres
, padpin
, padport
, attrs
)
259 print (" padpin", padpin
.layout
)
260 print (" jtag", io
.core
.layout
, io
.pad
.layout
)
261 m
.d
.comb
+= padpin
.i
.eq(self
._invert
_if
(invert
, port
))
262 m
.d
.comb
+= padport
.io
.eq(io
.core
.i
)
263 m
.d
.comb
+= pin
.i
.eq(io
.pad
.i
)
266 def get_output(self
, pin
, port
, attrs
, invert
):
267 self
._check
_feature
("single-ended output", pin
, attrs
,
268 valid_xdrs
=(0,), valid_attrs
=None)
271 print (" get_output", pin
, "port", port
, port
.layout
)
272 if pin
.name
in ['clk_0', 'rst_0']: # sigh
273 # simple pass-through from pin to port
274 print("No JTAG chain in-between")
275 m
.d
.comb
+= port
.eq(self
._invert
_if
(invert
, pin
.o
))
277 (padres
, padpin
, padport
, padattrs
) = self
.padlookup
[pin
.name
]
278 io
= self
.jtag
.ios
[pin
.name
]
279 print (" pad", padres
, padpin
, padport
, padattrs
)
280 print (" pin", padpin
.layout
)
281 print (" jtag", io
.core
.layout
, io
.pad
.layout
)
282 m
.d
.comb
+= port
.eq(self
._invert
_if
(invert
, pin
.o
))
283 m
.d
.comb
+= padport
.io
.eq(io
.core
.o
)
284 m
.d
.comb
+= padpin
.o
.eq(io
.pad
.o
)
287 def get_tristate(self
, pin
, port
, attrs
, invert
):
288 self
._check
_feature
("single-ended tristate", pin
, attrs
,
289 valid_xdrs
=(0,), valid_attrs
=None)
291 print (" get_tristate", pin
, "port", port
, port
.layout
)
293 if pin
.name
in ['clk_0', 'rst_0']: # sigh
294 print("No JTAG chain in-between")
295 m
.submodules
+= Instance("$tribuf",
298 i_A
=self
._invert
_if
(invert
, pin
.o
),
302 (res
, pin
, port
, attrs
) = self
.padlookup
[pin
.name
]
303 io
= self
.jtag
.ios
[pin
.name
]
304 print (" pad", res
, pin
, port
, attrs
)
305 print (" pin", pin
.layout
)
306 print (" jtag", io
.core
.layout
, io
.pad
.layout
)
307 #m.submodules += Instance("$tribuf",
310 # i_A=self._invert_if(invert, io.pad.o),
313 m
.d
.comb
+= io
.core
.o
.eq(pin
.o
)
314 m
.d
.comb
+= io
.core
.oe
.eq(pin
.oe
)
315 m
.d
.comb
+= pin
.i
.eq(io
.core
.i
)
316 m
.d
.comb
+= io
.pad
.i
.eq(port
.i
)
317 m
.d
.comb
+= port
.o
.eq(io
.pad
.o
)
318 m
.d
.comb
+= port
.oe
.eq(io
.pad
.oe
)
321 def get_input_output(self
, pin
, port
, attrs
, invert
):
322 self
._check
_feature
("single-ended input/output", pin
, attrs
,
323 valid_xdrs
=(0,), valid_attrs
=None)
325 print (" get_input_output", pin
, "port", port
, port
.layout
)
327 if pin
.name
in ['clk_0', 'rst_0']: # sigh
328 print("No JTAG chain in-between")
329 m
.submodules
+= Instance("$tribuf",
332 i_A
=self
._invert
_if
(invert
, pin
.o
),
335 m
.d
.comb
+= pin
.i
.eq(self
._invert
_if
(invert
, port
))
337 (res
, pin
, port
, attrs
) = self
.padlookup
[pin
.name
]
338 io
= self
.jtag
.ios
[pin
.name
]
339 print (" pad", res
, pin
, port
, attrs
)
340 print (" pin", pin
.layout
)
341 print (" port layout", port
.layout
)
342 print (" jtag", io
.core
.layout
, io
.pad
.layout
)
343 #m.submodules += Instance("$tribuf",
346 # i_A=self._invert_if(invert, io.pad.o),
352 m
.d
.comb
+= io
.pad
.i
.eq(self
._invert
_if
(invert
, port_i
))
353 m
.d
.comb
+= port_o
.eq(self
._invert
_if
(invert
, io
.pad
.o
))
354 m
.d
.comb
+= port_oe
.eq(io
.pad
.o
)
355 m
.d
.comb
+= pin
.i
.eq(io
.core
.i
)
356 m
.d
.comb
+= io
.core
.o
.eq(pin
.o
)
357 m
.d
.comb
+= io
.core
.oe
.eq(pin
.oe
)
362 and to create a Platform instance with that list, and build
366 p.resources=listofstuff
369 pinset
= dummy_pinset()
370 top
= Blinker(pinset
)
372 resources
= create_resources(pinset
)
373 p
= ASICPlatform (resources
, top
.jtag
)