81338834f36da2c1ce8efbe55b18a550a4f8c7e0
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(1)
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 # XXX these aren't strictly necessary right now but the next
234 # phase is to add JTAG Boundary Scan so it maaay be worth adding?
235 # at least for the print statements
236 def get_input(self
, pin
, port
, attrs
, invert
):
237 self
._check
_feature
("single-ended input", pin
, attrs
,
238 valid_xdrs
=(0,), valid_attrs
=None)
241 print (" get_input", pin
, "port", port
, port
.layout
)
242 if pin
.name
in ['clk_0', 'rst_0']: # sigh
243 # simple pass-through from port to pin
244 print("No JTAG chain in-between")
245 m
.d
.comb
+= pin
.i
.eq(self
._invert
_if
(invert
, port
))
247 (res
, pin
, port
, attrs
) = self
.padlookup
[pin
.name
]
248 io
= self
.jtag
.ios
[pin
.name
]
249 print (" pad", res
, pin
, port
, attrs
)
250 print (" pin", pin
.layout
)
251 print (" jtag", io
.core
.layout
, io
.pad
.layout
)
252 m
.d
.comb
+= io
.pad
.i
.eq(self
._invert
_if
(invert
, port
))
253 m
.d
.comb
+= pin
.i
.eq(io
.core
.i
)
256 def get_output(self
, pin
, port
, attrs
, invert
):
257 self
._check
_feature
("single-ended output", pin
, attrs
,
258 valid_xdrs
=(0,), valid_attrs
=None)
261 print (" get_output", pin
, "port", port
, port
.layout
)
262 if pin
.name
in ['clk_0', 'rst_0']: # sigh
263 # simple pass-through from pin to port
264 print("No JTAG chain in-between")
265 m
.d
.comb
+= port
.eq(self
._invert
_if
(invert
, pin
.o
))
267 (res
, pin
, port
, attrs
) = self
.padlookup
[pin
.name
]
268 io
= self
.jtag
.ios
[pin
.name
]
269 print (" pad", res
, pin
, port
, attrs
)
270 print (" pin", pin
.layout
)
271 print (" jtag", io
.core
.layout
, io
.pad
.layout
)
272 m
.d
.comb
+= port
.eq(self
._invert
_if
(invert
, io
.pad
.o
))
273 m
.d
.comb
+= pin
.o
.eq(io
.core
.o
)
276 def get_tristate(self
, pin
, port
, attrs
, invert
):
277 self
._check
_feature
("single-ended tristate", pin
, attrs
,
278 valid_xdrs
=(0,), valid_attrs
=None)
280 print (" get_tristate", pin
, "port", port
, port
.layout
)
282 if pin
.name
in ['clk_0', 'rst_0']: # sigh
283 print("No JTAG chain in-between")
284 # Can port's i/o/oe be accessed like this?
285 m
.d
.comb
+= port
.o
.eq(pin
.o
)
286 m
.d
.comb
+= port
.oe
.eq(pin
.oe
)
287 m
.d
.comb
+= pin
.i
.eq(port
.i
)
289 (res
, pin
, port
, attrs
) = self
.padlookup
[pin
.name
]
290 io
= self
.jtag
.ios
[pin
.name
]
291 print (" pad", res
, pin
, port
, attrs
)
292 print (" pin", pin
.layout
)
293 print (" jtag", io
.core
.layout
, io
.pad
.layout
)
294 m
.d
.comb
+= io
.core
.o
.eq(pin
.o
)
295 m
.d
.comb
+= io
.core
.oe
.eq(pin
.oe
)
296 m
.d
.comb
+= pin
.i
.eq(io
.core
.i
)
297 m
.d
.comb
+= io
.pad
.i
.eq(port
.i
)
298 m
.d
.comb
+= port
.o
.eq(io
.pad
.o
)
299 m
.d
.comb
+= port
.oe
.eq(io
.pad
.oe
)
302 def get_input_output(self
, pin
, port
, attrs
, invert
):
303 self
._check
_feature
("single-ended input/output", pin
, attrs
,
304 valid_xdrs
=(0,), valid_attrs
=None)
306 print (" get_input_output", pin
, "port", port
, port
.layout
)
308 if pin
.name
in ['clk_0', 'rst_0']: # sigh
309 print("No JTAG chain in-between")
310 m
.submodules
+= Instance("$tribuf",
313 i_A
=self
._invert
_if
(invert
, pin
.o
),
316 m
.d
.comb
+= pin
.i
.eq(self
._invert
_if
(invert
, port
))
318 (res
, pin
, port
, attrs
) = self
.padlookup
[pin
.name
]
319 io
= self
.jtag
.ios
[pin
.name
]
320 print (" pad", res
, pin
, port
, attrs
)
321 print (" pin", pin
.layout
)
322 print (" jtag", io
.core
.layout
, io
.pad
.layout
)
323 m
.submodules
+= Instance("$tribuf",
326 i_A
=self
._invert
_if
(invert
, io
.pad
.o
),
329 m
.d
.comb
+= io
.pad
.i
.eq(self
._invert
_if
(invert
, port
))
330 m
.d
.comb
+= pin
.i
.eq(io
.core
.i
)
331 m
.d
.comb
+= io
.core
.o
.eq(pin
.o
)
332 m
.d
.comb
+= io
.core
.oe
.eq(pin
.oe
)
337 and to create a Platform instance with that list, and build
341 p.resources=listofstuff
344 pinset
= dummy_pinset()
345 top
= Blinker(pinset
)
347 resources
= create_resources(pinset
)
348 p
= ASICPlatform (resources
, top
.jtag
)