Skip to content

Instantly share code, notes, and snippets.

@jeanthom
Last active June 10, 2020 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeanthom/f2ed41d67dd139e64665760d93eedda1 to your computer and use it in GitHub Desktop.
Save jeanthom/f2ed41d67dd139e64665760d93eedda1 to your computer and use it in GitHub Desktop.
Migrating a Migen codebase to nMigen

Migrating a Migen codebase to nMigen

In addition to nmigen.compat.

Signals

max=

Replace:

s = Signal(max=1234)

with:

s = Signal(range(1234))

Record

raw_bits()

Use lower() instead.

FSM

timeline

My personnal implementation of Migen's timeline as an Elaboratable:

from nmigen import *

__all__ = ["Timeline"]

class Timeline(Elaboratable):
    def __init__(self, events):
        self.trigger = Signal()
        self._events = events

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

        lastevent = max([e[0] for e in self._events])
        counter = Signal(range(lastevent+1))

        # Counter incrementation
        # (with overflow handling)
        if (lastevent & (lastevent + 1)) != 0:
            with m.If(counter == lastevent):
                m.d.sync += counter.eq(0)
            with m.Else():
                with m.If(counter != 0):
                    m.d.sync += counter.eq(counter+1)
                with m.Elif(self.trigger):
                    m.d.sync += counter.eq(1)
        else:
            with m.If(counter != 0):
                m.d.sync += counter.eq(counter+1)
            with m.Elif(self.trigger):
                m.d.sync += counter.eq(1)

        for e in self._events:
            if e[0] == 0:
                with m.If(self.trigger & (counter == 0)):
                    m.d.sync += e[1]
            else:
                with m.If(counter == e[0]):
                    m.d.sync += e[1]

        return m

Utils

log2_int

Replace:

from migen import log2_int

with:

from nmigen.utils import log2_int

Streams

No stream implementation is provided in nMigen or nMigen-SoC. LambdaConcept offers their own implementation in lambdaUSB.

Control signals equivalence

  • ack -> ready
  • stb -> valid
  • eop -> last
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment