1 """Simple example of a FSM-based ALU
3 This demonstrates a design that follows the valid/ready protocol of the
4 ALU, but with a FSM implementation, instead of a pipeline. It is also
5 intended to comply with both the CompALU API and the nmutil Pipeline API
6 (Liskov Substitution Principle)
10 1) p.ready_o is asserted on the initial ("Idle") state, otherwise it keeps low.
11 2) n.valid_o is asserted on the final ("Done") state, otherwise it keeps low.
12 3) The FSM stays in the Idle state while p.valid_i is low, otherwise
13 it accepts the input data and moves on.
14 4) The FSM stays in the Done state while n.ready_i is low, otherwise
15 it releases the output data and goes back to the Idle state.
19 from nmigen
import Elaboratable
, Signal
, Module
, Cat
22 from nmigen
.sim
.cxxsim
import Simulator
, Settle
24 from nmigen
.back
.pysim
import Simulator
, Settle
25 from nmigen
.cli
import rtlil
27 from nmutil
.iocontrol
import PrevControl
, NextControl
29 from soc
.fu
.base_input_record
import CompOpSubsetBase
30 from soc
.decoder
.power_enums
import (MicrOp
, Function
)
32 from vcd
.gtkw
import GTKWSave
, GTKWColor
35 class CompFSMOpSubset(CompOpSubsetBase
):
36 def __init__(self
, name
=None):
37 layout
= (('sdir', 1),
40 super().__init
__(layout
, name
=name
)
48 class Shifter(Elaboratable
):
49 """Simple sequential shifter
52 * p.data_i.data: value to be shifted
53 * p.data_i.shift: shift amount
54 * When zero, no shift occurs.
55 * On POWER, range is 0 to 63 for 32-bit,
56 * and 0 to 127 for 64-bit.
57 * Other values wrap around.
60 * op.sdir: shift direction (0 = left, 1 = right)
63 * n.data_o.data: shifted value
66 def __init__(self
, width
):
67 self
.data
= Signal(width
, name
="p_data_i")
68 self
.shift
= Signal(width
, name
="p_shift_i")
69 self
.ctx
= Dummy() # comply with CompALU API
72 return [self
.data
, self
.shift
]
75 def __init__(self
, width
):
76 self
.data
= Signal(width
, name
="n_data_o")
81 def __init__(self
, width
):
83 self
.p
= PrevControl()
84 self
.n
= NextControl()
85 self
.p
.data_i
= Shifter
.PrevData(width
)
86 self
.n
.data_o
= Shifter
.NextData(width
)
88 # more pieces to make this example class comply with the CompALU API
89 self
.op
= CompFSMOpSubset(name
="op")
90 self
.p
.data_i
.ctx
.op
= self
.op
91 self
.i
= self
.p
.data_i
._get
_data
()
92 self
.out
= self
.n
.data_o
._get
_data
()
94 def elaborate(self
, platform
):
97 m
.submodules
.p
= self
.p
98 m
.submodules
.n
= self
.n
101 # It is good practice to design a sequential circuit as
102 # a data path and a control path.
106 # The idea is to have a register that can be
107 # loaded or shifted (left and right).
109 # the control signals
114 shift_in
= Signal(self
.width
)
115 shift_left_by_1
= Signal(self
.width
)
116 shift_right_by_1
= Signal(self
.width
)
117 next_shift
= Signal(self
.width
)
119 shift_reg
= Signal(self
.width
, reset_less
=True)
120 # build the data flow
122 # connect input and output
123 shift_in
.eq(self
.p
.data_i
.data
),
124 self
.n
.data_o
.data
.eq(shift_reg
),
125 # generate shifted views of the register
126 shift_left_by_1
.eq(Cat(0, shift_reg
[:-1])),
127 shift_right_by_1
.eq(Cat(shift_reg
[1:], 0)),
129 # choose the next value of the register according to the
131 # default is no change
132 m
.d
.comb
+= next_shift
.eq(shift_reg
)
134 m
.d
.comb
+= next_shift
.eq(shift_in
)
136 with m
.If(direction
):
137 m
.d
.comb
+= next_shift
.eq(shift_right_by_1
)
139 m
.d
.comb
+= next_shift
.eq(shift_left_by_1
)
141 # register the next value
142 m
.d
.sync
+= shift_reg
.eq(next_shift
)
146 # The idea is to have a SHIFT state where the shift register
147 # is shifted every cycle, while a counter decrements.
148 # This counter is loaded with shift amount in the initial state.
149 # The SHIFT state is left when the counter goes to zero.
152 shift_width
= int(log2(self
.width
)) + 1
153 next_count
= Signal(shift_width
)
154 count
= Signal(shift_width
, reset_less
=True)
155 m
.d
.sync
+= count
.eq(next_count
)
158 with m
.State("IDLE"):
160 # keep p.ready_o active on IDLE
161 self
.p
.ready_o
.eq(1),
162 # keep loading the shift register and shift count
164 next_count
.eq(self
.p
.data_i
.shift
),
166 # capture the direction bit as well
167 m
.d
.sync
+= direction
.eq(self
.op
.sdir
)
168 with m
.If(self
.p
.valid_i
):
169 # Leave IDLE when data arrives
170 with m
.If(next_count
== 0):
171 # short-circuit for zero shift
175 with m
.State("SHIFT"):
177 # keep shifting, while counter is not zero
179 # decrement the shift counter
180 next_count
.eq(count
- 1),
182 with m
.If(next_count
== 0):
183 # exit when shift counter goes to zero
185 with m
.State("DONE"):
186 # keep n.valid_o active while the data is not accepted
187 m
.d
.comb
+= self
.n
.valid_o
.eq(1)
188 with m
.If(self
.n
.ready_i
):
189 # go back to IDLE when the data is accepted
196 yield self
.p
.data_i
.data
197 yield self
.p
.data_i
.shift
202 yield self
.n
.data_o
.data
208 # Write a formatted GTKWave "save" file
209 def write_gtkw_v1(base_name
, top_dut_name
, loc
):
210 # hierarchy path, to prepend to signal names
211 dut
= top_dut_name
+ "."
213 style_input
= GTKWColor
.orange
214 style_output
= GTKWColor
.yellow
215 style_debug
= GTKWColor
.red
216 with
open(base_name
+ ".gtkw", "wt") as gtkw_file
:
217 gtkw
= GTKWSave(gtkw_file
)
218 gtkw
.comment("Auto-generated by " + loc
)
219 gtkw
.dumpfile(base_name
+ ".vcd")
220 # set a reasonable zoom level
221 # also, move the marker to an interesting place
222 gtkw
.zoom_markers(-22.9, 10500000)
223 gtkw
.trace(dut
+ "clk")
224 # place a comment in the signal names panel
225 gtkw
.blank("Shifter Demonstration")
226 with gtkw
.group("prev port"):
227 gtkw
.trace(dut
+ "op__sdir", color
=style_input
)
228 # demonstrates using decimal base (default is hex)
229 gtkw
.trace(dut
+ "p_data_i[7:0]", color
=style_input
,
231 gtkw
.trace(dut
+ "p_shift_i[7:0]", color
=style_input
,
233 gtkw
.trace(dut
+ "p_valid_i", color
=style_input
)
234 gtkw
.trace(dut
+ "p_ready_o", color
=style_output
)
235 with gtkw
.group("debug"):
236 gtkw
.blank("Some debug statements")
237 # change the displayed name in the panel
238 gtkw
.trace("top.zero", alias
='zero delay shift',
240 gtkw
.trace("top.interesting", color
=style_debug
)
241 gtkw
.trace("top.test_case", alias
="test case", color
=style_debug
)
242 gtkw
.trace("top.msg", color
=style_debug
)
243 with gtkw
.group("internal"):
244 gtkw
.trace(dut
+ "fsm_state")
245 gtkw
.trace(dut
+ "count[3:0]")
246 gtkw
.trace(dut
+ "shift_reg[7:0]", datafmt
='dec')
247 with gtkw
.group("next port"):
248 gtkw
.trace(dut
+ "n_data_o[7:0]", color
=style_output
,
250 gtkw
.trace(dut
+ "n_valid_o", color
=style_output
)
251 gtkw
.trace(dut
+ "n_ready_i", color
=style_input
)
254 def write_gtkw(gtkw_name
, vcd_name
, gtkw_style
, gtkw_dom
,
255 loc
=None, zoom
=-22.9, marker
=-1):
256 """ Write a GTKWave document according to the supplied style and DOM.
258 :param gtkw_name: name of the generated GTKWave document
259 :param vcd_name: name of the waveform file
260 :param gtkw_style: style for signals, classes and groups
261 :param gtkw_dom: DOM style description for the trace pane
262 :param loc: source code location to include as a comment
263 :param zoom: initial zoom level, in GTKWave format
264 :param marker: initial location of a marker
266 **gtkw_style format**
268 Syntax: ``{selector: {attribute: value, ...}, ...}``
270 "selector" can be a signal, class or group
272 Signal groups propagate most attributes to their children
276 * module: instance path, for prepending to the signal name
278 * base: numerical base for value display
279 * display: alternate text to display in the signal pane
280 * comment: comment to display in the signal pane
284 Syntax: ``[signal, (signal, class), (group, [children]), comment, ...]``
286 The DOM is a list of nodes.
288 Nodes are signals, signal groups or comments.
290 * signals are strings, or tuples: ``(signal name, class, class, ...)``
291 * signal groups are tuples: ``(group name, class, class, ..., [nodes])``
292 * comments are: ``{'comment': 'comment string'}``
294 In place of a class name, an inline class description can be used.
295 ``(signal, {attribute: value, ...}, ...)``
298 'blue': GTKWColor
.blue
,
299 'cycle': GTKWColor
.cycle
,
300 'green': GTKWColor
.green
,
301 'indigo': GTKWColor
.indigo
,
302 'normal': GTKWColor
.normal
,
303 'orange': GTKWColor
.orange
,
304 'red': GTKWColor
.red
,
305 'violet': GTKWColor
.violet
,
306 'yellow': GTKWColor
.yellow
,
309 with
open(gtkw_name
, "wt") as gtkw_file
:
310 gtkw
= GTKWSave(gtkw_file
)
312 gtkw
.comment("Auto-generated by " + loc
)
313 gtkw
.dumpfile(vcd_name
)
314 # set a reasonable zoom level
315 # also, move the marker to an interesting place
316 gtkw
.zoom_markers(zoom
, marker
)
319 root_style
= gtkw_style
['']
323 # recursively walk the DOM
324 def walk(dom
, style
):
328 # copy the style from the parent
329 node_style
= style
.copy()
330 # node is a signal name string
331 if isinstance(node
, str):
333 # apply style from node name, if specified
334 if node_name
in gtkw_style
:
335 node_style
.update(gtkw_style
[node_name
])
337 # could be a signal or a group
338 elif isinstance(node
, tuple):
340 # collect styles from the selectors
341 # order goes from the most specific to most generic
342 # which means earlier selectors override later ones
343 for selector
in reversed(node
):
344 # update the node style from the selector
345 if isinstance(selector
, str):
346 if selector
in gtkw_style
:
347 node_style
.update(gtkw_style
[selector
])
348 # apply an inline style description
349 elif isinstance(selector
, dict):
350 node_style
.update(selector
)
351 # node is a group if it has a child list
352 if isinstance(node
[-1], list):
355 elif isinstance(node
, dict):
356 if 'comment' in node
:
357 gtkw
.blank(node
['comment'])
358 # emit the group delimiters and walk over the child list
359 if children
is not None:
360 gtkw
.begin_group(node_name
)
361 # pass on the group style to its children
362 walk(children
, node_style
)
363 gtkw
.end_group(node_name
)
364 # emit a trace, if the node is a signal
365 elif node_name
is not None:
366 signal_name
= node_name
367 # prepend module name to signal
368 if 'module' in node_style
:
369 signal_name
= node_style
['module'] + '.' + signal_name
370 color
= colors
.get(node_style
.get('color'))
371 base
= node_style
.get('base')
372 display
= node_style
.get('display')
373 gtkw
.trace(signal_name
, color
=color
, datafmt
=base
,
376 walk(gtkw_dom
, root_style
)
381 m
.submodules
.shf
= dut
= Shifter(8)
382 print("Shifter port names:")
384 print("-", port
.name
)
386 # try "proc; show" in yosys to check the data path
387 il
= rtlil
.convert(dut
, ports
=dut
.ports())
388 with
open("test_shifter.il", "w") as f
:
391 # Write the GTKWave project file
392 write_gtkw_v1("test_shifter", "top.shf", __file__
)
394 # Describe a GTKWave document
396 # Style for signals, classes and groups
398 # Root selector. Gives default attributes for every signal.
399 '': {'module': 'top.shf', 'base': 'dec'},
400 # color the traces, according to class
401 # class names are not hardcoded, they are just strings
402 'in': {'color': 'orange'},
403 'out': {'color': 'yellow'},
404 # signals in the debug group have a common color and module path
405 'debug': {'module': 'top', 'color': 'red'},
406 # display a different string replacing the signal name
407 'test_case': {'display': 'test case'},
410 # DOM style description for the trace pane
412 # simple signal, without a class
413 # even so, it inherits the top-level root attributes
416 {'comment': 'Shifter Demonstration'},
417 # collapsible signal group
419 # attach a class style for each signal
421 ('p_data_i[7:0]', 'in'),
422 ('p_shift_i[7:0]', 'in'),
424 ('p_ready_o', 'out'),
426 # Signals in a signal group inherit the group attributes.
427 # In this case, a different module path and color.
429 {'comment': 'Some debug statements'},
430 # inline attributes, instead of a class name
431 ('zero', {'display': 'zero delay shift'}),
442 ('n_data_o[7:0]', 'out'),
443 ('n_valid_o', 'out'),
448 write_gtkw("test_shifter_v2.gtkw", "test_shifter.vcd",
449 gtkwave_style
, gtkwave_desc
,
450 loc
=__file__
, marker
=10500000)
455 # demonstrates adding extra debug signal traces
456 # they end up in the top module
458 zero
= Signal() # mark an interesting place
460 # demonstrates string traces
462 # display a message when the signal is high
463 # the low level is just an horizontal line
464 interesting
= Signal(decoder
=lambda v
: 'interesting!' if v
else '')
465 # choose between alternate strings based on numerical value
466 test_cases
= ['', '13>>2', '3<<4', '21<<0']
467 test_case
= Signal(8, decoder
=lambda v
: test_cases
[v
])
468 # hack to display arbitrary strings, like debug statements
469 msg
= Signal(decoder
=lambda _
: msg
.str)
472 def send(data
, shift
, direction
):
473 # present input data and assert valid_i
474 yield dut
.p
.data_i
.data
.eq(data
)
475 yield dut
.p
.data_i
.shift
.eq(shift
)
476 yield dut
.op
.sdir
.eq(direction
)
477 yield dut
.p
.valid_i
.eq(1)
479 # wait for p.ready_o to be asserted
480 while not (yield dut
.p
.ready_o
):
482 # show current operation operation
484 msg
.str = f
'{data}>>{shift}'
486 msg
.str = f
'{data}<<{shift}'
487 # force dump of the above message by toggling the
491 # clear input data and negate p.valid_i
492 yield dut
.p
.valid_i
.eq(0)
493 yield dut
.p
.data_i
.data
.eq(0)
494 yield dut
.p
.data_i
.shift
.eq(0)
495 yield dut
.op
.sdir
.eq(0)
497 def receive(expected
):
498 # signal readiness to receive data
499 yield dut
.n
.ready_i
.eq(1)
501 # wait for n.valid_o to be asserted
502 while not (yield dut
.n
.valid_o
):
505 result
= yield dut
.n
.data_o
.data
507 yield dut
.n
.ready_i
.eq(0)
509 assert result
== expected
510 # finish displaying the current operation
517 yield from send(13, 2, 1)
519 yield from send(3, 4, 0)
521 # use a debug signal to mark an interesting operation
522 # in this case, it is a shift by zero
523 yield interesting
.eq(1)
524 yield from send(21, 0, 0)
525 yield interesting
.eq(0)
528 # the consumer is not in step with the producer, but the
529 # order of the results are preserved
531 yield test_case
.eq(1)
532 yield from receive(3)
534 yield test_case
.eq(2)
535 yield from receive(48)
537 yield test_case
.eq(3)
538 # you can look for the rising edge of this signal to quickly
539 # locate this point in the traces
541 yield from receive(21)
543 yield test_case
.eq(0)
545 sim
.add_sync_process(producer
)
546 sim
.add_sync_process(consumer
)
547 sim_writer
= sim
.write_vcd(
549 # include additional signals in the trace dump
550 traces
=[zero
, interesting
, test_case
, msg
],
556 if __name__
== "__main__":