Skip to content

Commit 77e9f91

Browse files
Add ToConst and ToConstBwd protocols (#182)
1 parent 534f859 commit 77e9f91

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

clash-protocols-base/src/Protocols/Plugin.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module Protocols.Plugin (
88
-- * Circuit types
99
Circuit (..),
1010
Protocol (..),
11+
ToConst,
12+
ToConstBwd,
1113

1214
-- * clash-prelude related types
1315
CSignal,
@@ -19,6 +21,8 @@ module Protocols.Plugin (
1921
) where
2022

2123
-- base
24+
25+
import Data.Kind (Type)
2226
import Prelude
2327

2428
-- clash-prelude
@@ -57,6 +61,24 @@ instance Protocol (a, b) where
5761
-- Generate n-tuple instances, where n > 2
5862
protocolTupleInstances 3 maxTupleSize
5963

64+
{- | A protocol that carries a constant value in the forward direction and no
65+
information in the backward direction.
66+
-}
67+
data ToConst (a :: Type)
68+
69+
instance Protocol (ToConst a) where
70+
type Fwd (ToConst a) = a
71+
type Bwd (ToConst a) = ()
72+
73+
{- | A protocol that carries no information in the forward direction and a
74+
constant value in the backward direction.
75+
-}
76+
data ToConstBwd (a :: Type)
77+
78+
instance Protocol (ToConstBwd a) where
79+
type Fwd (ToConstBwd a) = ()
80+
type Bwd (ToConstBwd a) = a
81+
6082
instance (C.KnownNat n) => Protocol (C.Vec n a) where
6183
type Fwd (C.Vec n a) = C.Vec n (Fwd a)
6284
type Bwd (C.Vec n a) = C.Vec n (Bwd a)

clash-protocols/clash-protocols.cabal

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,24 @@ library
125125
Protocols.Axi4.WriteAddress
126126
Protocols.Axi4.WriteData
127127
Protocols.Axi4.WriteResponse
128+
Protocols.Df
129+
Protocols.DfConv
130+
Protocols.Hedgehog
131+
Protocols.Hedgehog.Internal
132+
Protocols.Idle
133+
Protocols.Internal
134+
Protocols.Internal.TH
128135
Protocols.PacketStream
129-
Protocols.PacketStream.Base
130136
Protocols.PacketStream.AsyncFifo
137+
Protocols.PacketStream.Base
131138
Protocols.PacketStream.Converters
132139
Protocols.PacketStream.Depacketizers
133140
Protocols.PacketStream.Hedgehog
134141
Protocols.PacketStream.PacketFifo
135142
Protocols.PacketStream.Packetizers
136143
Protocols.PacketStream.Padding
137144
Protocols.PacketStream.Routing
138-
Protocols.Df
139-
Protocols.DfConv
140-
Protocols.Hedgehog
141-
Protocols.Hedgehog.Internal
142-
Protocols.Idle
143-
Protocols.Internal
144-
Protocols.Internal.TH
145+
Protocols.ToConst
145146
Protocols.Vec
146147
Protocols.Wishbone
147148
Protocols.Wishbone.Standard

clash-protocols/src/Protocols.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module Protocols (
2626
-- * Protocol types
2727
CSignal,
2828
Df,
29+
ToConst,
30+
ToConstBwd,
2931

3032
-- * Basic circuits
3133
idC,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{- | Definitions for the 'ToConst' protocol. Usable to pass constant values in
2+
circuits.
3+
-}
4+
module Protocols.ToConst (
5+
ToConst,
6+
ToConstBwd,
7+
to,
8+
from,
9+
toBwd,
10+
fromBwd,
11+
) where
12+
13+
import Protocols.Plugin (Circuit (..), ToConst, ToConstBwd)
14+
15+
-- | Convert a value to a 'Circuit' that produces a constant value.
16+
to :: a -> Circuit () (ToConst a)
17+
to a = Circuit (\_ -> ((), a))
18+
19+
-- | Extract the constant value
20+
from :: Circuit () (ToConst a) -> a
21+
from (Circuit f) = snd (f ((), ()))
22+
23+
{- | Convert a value to a 'Circuit' that produces a constant value in the
24+
backward direction.
25+
-}
26+
toBwd :: a -> Circuit (ToConstBwd a) ()
27+
toBwd a = Circuit (\_ -> (a, ()))
28+
29+
-- | Extract the constant value from the backward direction.
30+
fromBwd :: Circuit (ToConstBwd a) () -> a
31+
fromBwd (Circuit f) = fst (f ((), ()))

0 commit comments

Comments
 (0)