CIRCT 24.0.0git
Loading...
Searching...
No Matches
backpressure.py
Go to the documentation of this file.
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# Hardware for the cosim backpressure integration tests.
6#
7# * GatedLoopback ("loopback"): loops `in` back to `out`, but only while the
8# last message on `gate` was non-zero. With the gate closed, `in` is never
9# consumed, so host writes back up (host -> simulation backpressure).
10#
11# * Producer ("producer"): a `produce` command with N makes it emit 0..N-1 on
12# `stream` as fast as the host side accepts them. A message on `query`
13# returns, on `sent`, how many it has emitted so far. That lets the host
14# observe that the producer was stalled by an unread `stream` (simulation ->
15# host backpressure) rather than having buffered everything host-side.
16#
17# * VoidProducer ("void_producer"): the same as Producer, but `stream` carries
18# void (zero-width) messages.
19#
20# Takes the BSP name as an optional second argument (default: native cosim).
21
22import sys
23
24from pycde import AppID, Clock, Module, Reset, System, esi, generator
25from pycde.constructs import Counter, Wire
26from pycde.types import Bits, Channel, UInt
27
28from esiaccel.bsp import get_bsp
29
30
32 clk = Clock()
33 rst = Reset()
34
35 @generator
36 def build(ports):
37 gate = esi.ChannelService.from_host(AppID("gate"), UInt(8))
38 gate_data, gate_valid = gate.unwrap(Bits(1)(1))
39 enabled = gate_data.as_bits()[0].reg(ports.clk,
40 ports.rst,
41 ce=gate_valid,
42 name="enabled")
43
44 inp = esi.ChannelService.from_host(AppID("in"), UInt(32))
45 in_ready = Wire(Bits(1))
46 data, valid = inp.unwrap(in_ready)
47 out, out_ready = Channel(UInt(32)).wrap(data, valid & enabled)
48 in_ready.assign(out_ready & enabled)
49 esi.ChannelService.to_host(AppID("out"), out)
50
51
53 clk = Clock()
54 rst = Reset()
55
56 @generator
57 def build(ports):
58 produce = esi.ChannelService.from_host(AppID("produce"), UInt(32))
59 count, count_valid = produce.unwrap(Bits(1)(1))
60 target = count.reg(ports.clk, ports.rst, ce=count_valid, name="target")
61
62 xact = Wire(Bits(1))
63 sent = Counter(32)(clk=ports.clk,
64 rst=ports.rst,
65 clear=Bits(1)(0),
66 increment=xact)
67 valid = sent.out < target
68 stream, stream_ready = Channel(UInt(32)).wrap(sent.out, valid)
69 xact.assign(valid & stream_ready)
70 esi.ChannelService.to_host(AppID("stream"), stream)
71
72 query = esi.ChannelService.from_host(AppID("query"), UInt(8))
73 query_ready = Wire(Bits(1))
74 _, query_valid = query.unwrap(query_ready)
75 resp, resp_ready = Channel(UInt(32)).wrap(sent.out, query_valid)
76 query_ready.assign(resp_ready)
77 esi.ChannelService.to_host(AppID("sent"), resp)
78
79
81 """Like `Producer`, but `stream` carries void (zero-width) messages."""
82 clk = Clock()
83 rst = Reset()
84
85 @generator
86 def build(ports):
87 produce = esi.ChannelService.from_host(AppID("produce"), UInt(32))
88 count, count_valid = produce.unwrap(Bits(1)(1))
89 target = count.reg(ports.clk, ports.rst, ce=count_valid, name="target")
90
91 xact = Wire(Bits(1))
92 sent = Counter(32)(clk=ports.clk,
93 rst=ports.rst,
94 clear=Bits(1)(0),
95 increment=xact)
96 valid = sent.out < target
97 stream, stream_ready = Channel(Bits(0)).wrap(Bits(0)(0), valid)
98 xact.assign(valid & stream_ready)
99 esi.ChannelService.to_host(AppID("stream"), stream)
100
101 query = esi.ChannelService.from_host(AppID("query"), UInt(8))
102 query_ready = Wire(Bits(1))
103 _, query_valid = query.unwrap(query_ready)
104 resp, resp_ready = Channel(UInt(32)).wrap(sent.out, query_valid)
105 query_ready.assign(resp_ready)
106 esi.ChannelService.to_host(AppID("sent"), resp)
107
108
109class Top(Module):
110 clk = Clock()
111 rst = Reset()
112
113 @generator
114 def build(ports):
115 GatedLoopback(clk=ports.clk, rst=ports.rst, appid=AppID("loopback"))
116 Producer(clk=ports.clk, rst=ports.rst, appid=AppID("producer"))
117 VoidProducer(clk=ports.clk, rst=ports.rst, appid=AppID("void_producer"))
118
119
120if __name__ == "__main__":
121 bsp = get_bsp(sys.argv[2] if len(sys.argv) > 2 else None)
122 s = System(bsp(Top), name="BackpressureTest", output_directory=sys.argv[1])
123 s.compile()
124 s.package()
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))