Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples.boilerplate: modify to use port groups #663

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 57 additions & 8 deletions examples/boilerplate.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,66 @@
import logging
import asyncio
from amaranth import *
from amaranth.lib import io, stream, wiring

from ... import *


class BoilerplateSubtarget(Elaboratable):
def __init__(self, pads, in_fifo, out_fifo):
self.pads = pads
class BoilerplateModule(wiring.Component):
data: In(8)
enable: Out(1)

in_stream: In(stream.Signature(signed(8)))
out_stream: Out(stream.Signature(signed(8)))
def elaborate(self, platform):
m = Module()

return m

class BoilerplateSubtarget(wiring.Component):
def __init__(self, ports, in_fifo, out_fifo):
self.ports = ports
self.in_fifo = in_fifo
self.out_fifo = out_fifo

def elaborate(self, platform):
m = Module()

# ┌───────┐ ┌─────────────────┐
# │in_fifo◄───in_stream───┤ │
# └───────┘ │ │
# │BoilerplateModule│
# ┌────────┐ │ │
# │out_fifo├───out_stream──► │
# └────────┘ └─────▲───────┬───┘
# │ │
# data enable
# │ │
# ┌───┴───────▼──┐
# │ Ports │
# └──────────────┘


m.submodules.boilerplate = boilerplate = BoilerplateModule()

## Instantiate IO buffers for pins/ports
m.submodules.data_buffer = data_buffer = io.Buffer("i", args.port_data)
m.submodules.enable_buffer = enable_buffer = io.Buffer("o", args.port_enable)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually these should live in the BoilerplateModule which I'd probably call BoilerplateCore. So the core would do the port manipulation and the subtarget would set up the transfers and stuff like that, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That separation of functions makes sense to me. A more verbose option would be to create a BoilerplateOutput module to handle the ports, and then leave BoilerplateCore empty as a placeholder for "core" logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems excessive for the sample applet. This all will change some time not too far away anyway.


## Connect IO buffers to corresponding ports of BoilerplateModule
wiring.connect(m, boilerplate.data, data_buffer.i)
wiring.connect(m, boilerplate.enable, enable_buffer.o)

## Connect BoilerplateModule.in_stream to BoilerplateSubtarget.out_fifo
boilerplate.in_stream.payload.eq(self.out_fifo.r_data),
boilerplate.in_stream.valid.eq(self.out_fifo.r_rdy),
self.out_fifo.r_en.eq(boilerplate.in_stream.ready),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should be able to do these via wiring.connect too, but I may not have upstreamed that patch yet...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my latest version, I used the "interface adaptation" pattern between the in/out fifo signals to a stream.Signature. But I see now that amaranth.lib.fifo implements streams, so out_fifo.r_stream and in_fifo.w_stream should just work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep!


## Connect BoilerplateSubtarget.in_fifo to BoilerplateModule.out_stream
self.in_fifo.w_data.eq(boilerplate.out_stream.payload),
self.in_fifo.w_en.eq(boilerplate.out_stream.valid),
boilerplate.out_stream.ready.eq(self.in_fifo.w_rdy),

return m


Expand All @@ -32,19 +80,20 @@ class BoilerplateApplet(GlasgowApplet):
nothing. Similarly, there is no requirement to use IN or OUT FIFOs, or any pins at all.
"""

__pins = ()

@classmethod
def add_build_arguments(cls, parser, access):
super().add_build_arguments(parser, access)

for pin in cls.__pins:
access.add_pin_argument(parser, pin, default=True)
access.add_pin_argument(parser, "enable", default=True)
access.add_pin_set_argument(parser, "data", width=8, default=True)

def build(self, target, args):
self.mux_interface = iface = target.multiplexer.claim_interface(self, args)
iface.add_subtarget(BoilerplateSubtarget(
pads=iface.get_deprecated_pads(args, pins=self.__pins),
ports=iface.get_port_group(
enable = args.pin_enable,
data = args.pin_set_data
),
in_fifo=iface.get_in_fifo(),
out_fifo=iface.get_out_fifo(),
))
Expand Down
Loading