Skip to content

Commit

Permalink
access: reimplement get_deprecated_pad{,s}() via get_port().
Browse files Browse the repository at this point in the history
There is no loss of functionality and this change allows us to only
consider `get_port()` as a consumer of pin argument values.
  • Loading branch information
whitequark committed Jul 23, 2024
1 parent bf02911 commit 3454c82
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 121 deletions.
78 changes: 41 additions & 37 deletions software/glasgow/access/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import argparse

from ..gateware.ports import PortGroup
from ..gateware.pads import Pads


__all__ = ["AccessArguments"]
__all__ += ["AccessMultiplexer", "AccessMultiplexerInterface"]
__all__ += ["AccessDemultiplexer", "AccessDemultiplexerInterface"]


class _DeprecatedPads:
"""Deprecated in favor of :class:`glasgow.gateware.ports.PortGroup`."""


class AccessArguments(metaclass=ABCMeta):
def _arg_error(self, message):
raise argparse.ArgumentTypeError(f"applet {self._applet_name!r}: " + message)
Expand Down Expand Up @@ -48,7 +51,8 @@ def __init__(self, applet, analyzer):
self.applet = applet
self.logger = applet.logger
self.analyzer = analyzer
self.pads = None

self._deprecated_buffers = []

@abstractmethod
def get_out_fifo(self, **kwargs):
Expand All @@ -63,56 +67,56 @@ def get_pin_name(self, pin):
pass

@abstractmethod
def get_port(self, pin, *, name):
def get_port_impl(self, pin, *, name):
pass

def get_port(self, pin_or_pins, *, name):
if isinstance(pin_or_pins, list):
if pin_or_pins == []:
self.logger.debug("not assigning applet ports '%s[]' to any device pins", name)
return None
port = None
for index, subpin in enumerate(pin_or_pins):
subport = self.get_port(subpin, name=f"{name}[{index}]")
if port is None:
port = subport
else:
port += subport
assert port is not None
return port
else:
if pin_or_pins is None:
self.logger.debug("not assigning applet port '%s' to any device pin", name)
return None
return self.get_port_impl(pin_or_pins, name=name)

def get_port_group(self, **kwargs):
return PortGroup(**{
name: self.get_port(pin_or_pins, name=name) for name, pin_or_pins in kwargs.items()
})

@abstractmethod
def _build_pad_tristate(self, pin, oe, o, i):
pass

def get_deprecated_pad(self, pins, name=None):
if type(pins) is int:
pins = [pins]

triple = io.Buffer.Signature("io", len(pins)).create()
for n, pin in enumerate(pins):
self._build_pad_tristate(pin, triple.oe, triple.o[n], triple.i[n])

if name is None:
name = "-".join([self.get_pin_name(pins) for pins in pins])
port = self.get_port(pins, name=name)
self._deprecated_buffers.append(buffer := io.Buffer("io", port))
if self.analyzer:
self.analyzer.add_pin_event(self.applet, name, triple)

return triple
if name is None:
name = ",".join(self.get_pin_name(pins) for pins in pins)
self.analyzer.add_pin_event(self.applet, name, buffer)
return buffer

def get_deprecated_pads(self, args, pins=[], pin_sets=[]):
pad_args = {}

pads = _DeprecatedPads()
for pin in pins:
pin_num = getattr(args, f"pin_{pin}")
if pin_num is None:
self.logger.debug("not assigning pin %r to any device pin", pin)
else:
self.logger.debug("assigning pin %r to device pin %s",
pin, self.get_pin_name(pin_num))
pad_args[pin] = self.get_deprecated_pad(pin_num, name=pin)

if pin_num is not None:
setattr(pads, f"{pin}_t", self.get_deprecated_pad(pin_num, name=pin))
for pin_set in pin_sets:
pin_nums = getattr(args, f"pin_set_{pin_set}")
if pin_nums is None:
self.logger.debug("not assigning pin set %r to any device pins", pin_set)
else:
self.logger.debug("assigning pin set %r to device pins %s",
pin_set, ", ".join([self.get_pin_name(pin_num) for pin_num in pin_nums]))
pad_args[pin_set] = self.get_deprecated_pad(pin_nums, name=pin_set)

self.pads = Pads(**pad_args)
return self.pads
if pin_nums is not None:
setattr(pads, f"{pin_set}_t", self.get_deprecated_pad(pin_nums, name=pin_set))
# Horrifically dirty, but the `uart` applet currently depends on this :(
self.pads = pads
return pads


class AccessDemultiplexer(metaclass=ABCMeta):
Expand Down
52 changes: 10 additions & 42 deletions software/glasgow/access/direct/multiplexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def __init__(self, applet, analyzer, registers, fx2_crossbar, pipe_num, pins, th
self._throttle = throttle
self._subtargets = []
self._fifos = []
self._pad_tristates = []

self.reset, self._addr_reset = self._registers.add_rw(1, init=1)
self.logger.debug("adding reset register at address %#04x", self._addr_reset)
Expand All @@ -186,6 +185,8 @@ def elaborate(self, platform):

m.submodules += self._subtargets

m.submodules += self._deprecated_buffers

for fifo in self._fifos:
if self._throttle == "full":
m.d.comb += fifo._ctrl_en.eq(~self.analyzer.throttle)
Expand All @@ -194,54 +195,21 @@ def elaborate(self, platform):

m.submodules += fifo

for pin_parts, oe, o, i in self._pad_tristates:
m.submodules += (io_buffer := io.Buffer("io", pin_parts.io))
m.d.comb += [
io_buffer.oe.eq(oe),
io_buffer.o.eq(o),
i.eq(io_buffer.i),
]
if hasattr(pin_parts, "oe"):
m.submodules += (oe_buffer := io.Buffer("o", pin_parts.oe))
m.d.comb += oe_buffer.o.eq(oe)

return m

def get_pin_name(self, pin):
port, bit, request = self._pins[pin]
return f"{port}{bit}"

def get_port(self, pin_or_pins, *, name):
if isinstance(pin_or_pins, list):
if pin_or_pins == []:
self.logger.debug("not assigning applet ports '%s[]' to any device pins", name)
return None
port = None
for index, subpin in enumerate(pin_or_pins):
subport = self.get_port(subpin, name=f"{name}[{index}]")
if port is None:
port = subport
else:
port += subport
assert port is not None
return port
def get_port_impl(self, pin, *, name):
port, bit, request = self._pins[pin]
self.logger.debug("assigning applet port '%s' to device pin %s",
name, self.get_pin_name(pin))
pin_components = request(bit)
if hasattr(pin_components, "oe"):
return GlasgowPlatformPort(io=pin_components.io, oe=pin_components.oe)
else:
if pin_or_pins is None:
self.logger.debug("not assigning applet port '%s' to any device pin", name)
return None
port, bit, request = self._pins[pin_or_pins]
self.logger.debug("assigning applet port '%s' to device pin %s",
name, self.get_pin_name(pin_or_pins))
pin_subports = request(bit)
if hasattr(pin_subports, "oe"):
return GlasgowPlatformPort(io=pin_subports.io, oe=pin_subports.oe)
else:
return GlasgowPlatformPort(io=pin_subports.io)

def _build_pad_tristate(self, pin_num, oe, o, i):
port, bit, request = self._pins[pin_num]
pin_subports = request(bit)
self._pad_tristates.append((pin_subports, oe, o, i))
return GlasgowPlatformPort(io=pin_components.io)

def get_in_fifo(self, **kwargs):
fifo = self._fx2_crossbar.get_in_fifo(self._pipe_num, **kwargs, reset=self.reset)
Expand Down
11 changes: 6 additions & 5 deletions software/glasgow/access/simulation/multiplexer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from amaranth import *
from amaranth.lib.io import SimulationPort
from amaranth.lib.fifo import FIFOInterface, AsyncFIFO, SyncFIFOBuffered

from .. import AccessMultiplexer, AccessMultiplexerInterface
Expand Down Expand Up @@ -65,6 +66,9 @@ def elaborate(self, platform):
m = Module()

m.submodules += self._subtargets

m.submodules += self._deprecated_buffers

if self.in_fifo is not None:
m.submodules.in_fifo = self.in_fifo
if self.out_fifo is not None:
Expand All @@ -75,11 +79,8 @@ def elaborate(self, platform):
def get_pin_name(self, pin):
return str(pin)

def get_port(self, pin, *, name):
raise NotImplementedError

def _build_pad_tristate(self, pin, oe, o, i):
pass
def get_port_impl(self, pin, *, name):
return SimulationPort("io", 1, name=name)

def _make_fifo(self, crossbar_side, logic_side, cd_logic, depth, wrapper=lambda x: x):
if cd_logic is None:
Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/audio/yamaha_opx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@
from amaranth.lib.cdc import FFSynchronizer
from urllib.parse import urlparse

from ....gateware.pads import *
from ....gateware.clockgen import *
from ....protocol.vgm import *
from ... import *
Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/interface/analyzer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from amaranth import *
from amaranth.lib.cdc import FFSynchronizer

from ....gateware.pads import *
from ....gateware.analyzer import *
from ... import *

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import math
from amaranth import *

from ....gateware.pads import *
from ....gateware.i2c import I2CInitiator
from ... import *

Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/interface/i2c_target/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from abc import ABCMeta, abstractmethod
from amaranth import *

from ....gateware.pads import *
from ....gateware.i2c import I2CTarget
from ... import *

Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/interface/jtag_openocd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from ....support.bits import *
from ....support.logging import *
from ....support.endpoint import *
from ....gateware.pads import *
from ... import *


Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/interface/jtag_pinout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from amaranth import *
from amaranth.lib.cdc import FFSynchronizer

from ....gateware.pads import *
from ... import *


Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/interface/jtag_probe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from ....support.bits import *
from ....support.logging import *
from ....support.arepl import *
from ....gateware.pads import *
from ....database.jedec import *
from ....arch.jtag import *
from ... import *
Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/interface/sbw_probe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from amaranth import *
from amaranth.lib.cdc import FFSynchronizer

from ....gateware.pads import *
from ... import *
from ..jtag_probe import JTAGProbeDriver, JTAGProbeInterface

Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/interface/swd_openocd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from ....support.bits import *
from ....support.logging import *
from ....support.endpoint import *
from ....gateware.pads import *
from ... import *


Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/interface/uart/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from amaranth import *

from ....support.endpoint import *
from ....gateware.pads import *
from ....gateware.uart import *
from ... import *

Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/program/m16c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from amaranth import *

from ....support.logging import *
from ....gateware.pads import *
from ....gateware.uart import *
from ... import *

Expand Down
6 changes: 3 additions & 3 deletions software/glasgow/applet/video/hub75_output/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ class VideoHub75OutputApplet(GlasgowApplet):
def add_build_arguments(cls, parser, access):
super().add_build_arguments(parser, access)

access.add_pin_set_argument(parser, "rgb1", width=3, default=(0,1,2))
access.add_pin_set_argument(parser, "rgb2", width=3, default=(3,4,5))
access.add_pin_set_argument(parser, "addr", width=range(1,6), default=(8,9,10,11,12))
access.add_pin_set_argument(parser, "rgb1", width=3, default=[0,1,2])
access.add_pin_set_argument(parser, "rgb2", width=3, default=[3,4,5])
access.add_pin_set_argument(parser, "addr", width=range(1,6), default=[8,9,10,11,12])
access.add_pin_argument(parser, "clk", default=13)
access.add_pin_argument(parser, "lat", default=14)
access.add_pin_argument(parser, "oe", default=15)
Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/video/vga_output/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from amaranth import *
from amaranth.lib import data

from ....gateware.pads import *
from ....gateware.pll import *
from ... import *

Expand Down
1 change: 0 additions & 1 deletion software/glasgow/applet/video/ws2812_output/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from amaranth import *

from ....support.endpoint import *
from ....gateware.pads import *
from ....gateware.pll import *
from ... import *

Expand Down
21 changes: 0 additions & 21 deletions software/glasgow/gateware/pads.py

This file was deleted.

0 comments on commit 3454c82

Please sign in to comment.