Skip to content

Commit 2cdde17

Browse files
authored
[PyCDE] Binding for FIFOs (#7806)
Adds a `seq` module which contains a FIFO class which produces a `seq.fifo` op. Does not include anything related to almost full/empty.
1 parent 9351303 commit 2cdde17

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

frontends/PyCDE/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ declare_mlir_python_sources(PyCDESources
3232
pycde/system.py
3333
pycde/devicedb.py
3434
pycde/instance.py
35+
pycde/seq.py
3536
pycde/signals.py
3637
pycde/ndarray.py
3738
pycde/esi.py

frontends/PyCDE/src/pycde/seq.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
from .circt.dialects import seq as raw_seq
6+
from .constructs import Wire
7+
8+
from .types import Bits, Type
9+
from .signals import _FromCirctValue, BitsSignal, ClockSignal, Signal
10+
11+
12+
class FIFO:
13+
"""Creates a FIFO operation with the specified type, depth, clock, and reset
14+
signal. Adds push and pop methods to wire up the FIFO."""
15+
16+
def __init__(self,
17+
type: Type,
18+
depth: int,
19+
clk: ClockSignal,
20+
rst: BitsSignal,
21+
rd_latency: int = 0):
22+
self.type = type
23+
self.input = Wire(type)
24+
self.wr_en = Wire(Bits(1))
25+
self.rd_en = Wire(Bits(1))
26+
i1 = Bits(1)._type
27+
self.fifo = raw_seq.FIFOOp(self.input.type._type,
28+
i1,
29+
i1,
30+
i1,
31+
i1,
32+
self.input.value,
33+
self.rd_en.value,
34+
self.wr_en.value,
35+
clk.value,
36+
rst.value,
37+
depth,
38+
rdLatency=rd_latency)
39+
self._output = _FromCirctValue(self.fifo.output)
40+
41+
def push(self, data: Signal, en: BitsSignal):
42+
"""Connect 'data' to the FIFO input and 'en' to write enable."""
43+
self.input.assign(data)
44+
self.wr_en.assign(en)
45+
46+
def pop(self, en: BitsSignal):
47+
"""Wire up 'en' to read enable and returns the FIFO output."""
48+
self.rd_en.assign(en)
49+
return self._output
50+
51+
@property
52+
def output(self):
53+
return self._output
54+
55+
@property
56+
def full(self):
57+
return _FromCirctValue(self.fifo.full)
58+
59+
@property
60+
def empty(self):
61+
return _FromCirctValue(self.fifo.empty)

frontends/PyCDE/test/test_seq.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# RUN: %PYTHON% %s | FileCheck %s
2+
3+
from pycde import Module, Clock, Reset, Input, Output
4+
from pycde.seq import FIFO
5+
from pycde.testing import unittestmodule
6+
from pycde.types import Bits, UInt
7+
8+
from pycde.module import generator
9+
10+
# CHECK-LABEL: hw.module @SimpleFIFOTest(in %clk : !seq.clock, in %rst : i1)
11+
# CHECK-NEXT: %false = hw.constant false
12+
# CHECK-NEXT: [[R0:%.+]] = hwarith.constant 0 : ui32
13+
# CHECK-NEXT: %out, %full, %empty, %almostFull, %almostEmpty = seq.fifo depth 16 in [[R0]] rdEn %false wrEn %false clk %clk rst %rst : ui32
14+
15+
16+
@unittestmodule(run_passes=False)
17+
class SimpleFIFOTest(Module):
18+
clk = Clock()
19+
rst = Reset()
20+
21+
@generator
22+
def construct(ports):
23+
c0 = Bits(1)(0)
24+
ui32 = UInt(32)(0)
25+
26+
fifo = FIFO(type=UInt(32), depth=16, clk=ports.clk, rst=ports.rst)
27+
fifo.push(ui32, c0)
28+
fifo.pop(c0)
29+
30+
31+
# CHECK-LABEL: hw.module @SimpleFIFOTestRd1(in %clk : !seq.clock, in %rst : i1)
32+
# CHECK-NEXT: %false = hw.constant false
33+
# CHECK-NEXT: [[R0:%.+]] = hwarith.constant 0 : ui32
34+
# CHECK-NEXT: %out, %full, %empty, %almostFull, %almostEmpty = seq.fifo depth 16 rd_latency 1 in [[R0]] rdEn %false wrEn %false clk %clk rst %rst : ui32
35+
36+
37+
@unittestmodule(run_passes=False)
38+
class SimpleFIFOTestRd1(Module):
39+
clk = Clock()
40+
rst = Reset()
41+
42+
@generator
43+
def construct(ports):
44+
c0 = Bits(1)(0)
45+
ui32 = UInt(32)(0)
46+
47+
fifo = FIFO(type=UInt(32),
48+
depth=16,
49+
clk=ports.clk,
50+
rst=ports.rst,
51+
rd_latency=1)
52+
fifo.push(ui32, c0)
53+
fifo.pop(c0)

0 commit comments

Comments
 (0)