Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce BiDf protocol #109

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions clash-protocols-base/src/Protocols/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ repeatC ::
repeatC (Circuit f) =
Circuit (C.unzip . C.map f . uncurry C.zip)

{- | Copy a circuit /n/ times, providing access to the index of each replica.
If looking for a circuit that turns a single channel into multiple, check out
'Protocols.Df.fanout'.
-}
repeatWithIndexC
:: forall n a b. (C.KnownNat n) =>
(C.Index n -> Circuit a b) ->
Circuit (C.Vec n a) (C.Vec n b)
repeatWithIndexC f =
Circuit (C.unzip . C.zipWith g C.indicesI . uncurry C.zip)
where
g i = case f i of Circuit f' -> f'

{- | Combine two separate circuits into one. If you are looking to combine
multiple streams into a single stream, checkout 'Protocols.Df.fanin'.
-}
Expand Down
2 changes: 2 additions & 0 deletions clash-protocols/clash-protocols.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ library
Protocols.Axi4.WriteAddress
Protocols.Axi4.WriteData
Protocols.Axi4.WriteResponse
Protocols.BiDf
Protocols.Df
Protocols.DfConv
Protocols.Hedgehog
Expand Down Expand Up @@ -175,6 +176,7 @@ test-suite unittests
main-is: unittests.hs
other-modules:
Tests.Protocols
Tests.Protocols.BiDf
Tests.Protocols.Df
Tests.Protocols.DfConv
Tests.Protocols.Avalon
Expand Down
1 change: 1 addition & 0 deletions clash-protocols/src/Protocols.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Protocols (
-- * Basic circuits
idC,
repeatC,
repeatWithIndexC,
prod2C,

-- * Simulation
Expand Down
139 changes: 139 additions & 0 deletions clash-protocols/src/Protocols/BiDf.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{-# OPTIONS_GHC -fplugin Protocols.Plugin #-}

-- | Bi-directional request/response-style 'Df' channels.
module Protocols.BiDf (
BiDf,
-- * Conversion
fromDfs,
toDfs,
fromBiDf,
toBiDf,
-- * Trivial combinators
void,
loopback,
-- * Mapping
dimap,
-- * Fan-in
fanin
) where

import Prelude ()

import Clash.Prelude

import Protocols
import qualified Protocols.Df as Df

-- | A 'Protocol' allowing requests to be passed downstream, with corresponding
-- responses being passed back upstream. Responses are provided in the order that
-- their corresponding requests were submitted.
--
-- *Correctness conditions*
--
-- - The response channel must not produce a value before the request channel
-- has produced a value.
bgamari marked this conversation as resolved.
Show resolved Hide resolved
--
-- - Each request must be paired with exactly one response.
--
-- - Responses must be issued in the order that their corresponding requests arrived.
--
-- - Both the request and response channels must obey usual 'Df' correctness
-- conditions.
--
-- - There must not be a combinational path from the request channel to the
-- response channel.
Comment on lines +45 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I reading correctly that this disallows stateless circuits such as:

idBiDf :: Circuit (BiDf dom a a) ()
idBiDf = Circuit go
 where
  go ((fwd, bwd), _) = ((bwd, fwd), ())

If so, why would that not be allowed?

--
type BiDf dom req resp =
(Df dom req, Reverse (Df dom resp))

-- | Convert a circuit of 'Df's to a 'BiDf' circuit.
toBiDf
:: Circuit (Df dom req) (Df dom resp)
-> Circuit (BiDf dom req resp) ()
toBiDf c = circuit $ \bidf -> do
resp <- c -< req
req <- toDfs -< (bidf, resp)
idC -< ()

-- | Convert a 'BiDf' circuit to a circuit of 'Df's.
fromBiDf
:: Circuit (BiDf dom req resp) ()
-> Circuit (Df dom req) (Df dom resp)
fromBiDf c = circuit $ \req -> do
(biDf, resp) <- fromDfs -< req
c -< biDf
idC -< resp

-- | Convert a pair of a request and response 'Df`s into a 'BiDf'.
toDfs :: Circuit (BiDf dom req resp, Df dom resp) (Df dom req)
toDfs = fromSignals $ \(~((reqData, respAck), respData), reqAck) ->
(((reqAck, respData), respAck), reqData)

-- | Convert a 'BiDf' into a pair of request and response 'Df`s.
fromDfs :: Circuit (Df dom req) (BiDf dom req resp, Df dom resp)
fromDfs = fromSignals $ \(reqData, ~((reqAck, respData), respAck)) ->
(reqAck, ((reqData, respAck), respData))

-- | Ignore all requests, never providing responses.
void :: (HiddenClockResetEnable dom) => Circuit (BiDf dom req resp') ()
void = circuit $ \biDf -> do
req <- toDfs -< (biDf, resp)
resp <- Df.empty -< ()
Df.void -< req

-- | Return mapped requests as responses.
loopback
:: (HiddenClockResetEnable dom, NFDataX req)
=> (req -> resp)
-> Circuit (BiDf dom req resp) ()
loopback f = circuit $ \biDf -> do
req <- toDfs -< (biDf, resp)
resp <- Df.map f <| Df.registerFwd -< req
idC -< ()

-- | Map both requests and responses.
dimap
:: (req -> req')
-> (resp -> resp')
-> Circuit (BiDf dom req resp') (BiDf dom req' resp)
dimap f g = circuit $ \biDf -> do
req <- toDfs -< (biDf, resp')
req' <- Df.map f -< req
resp' <- Df.map g -< resp
(biDf', resp) <- fromDfs -< req'
idC -< biDf'

-- | Merge a number of 'BiDf's, preferring requests from the last channel.
fanin
:: forall n dom req resp.
( KnownNat n
, 1 <= n
, NFDataX req
, NFDataX resp
, HiddenClockResetEnable dom
)
=> Circuit (Vec n (BiDf dom req resp)) (BiDf dom req resp)
fanin = fromSignals $ \(upFwds, (reqAck, respData)) ->
let reqDatas :: Vec n (Signal dom (Df.Data req))
reqDatas = map fst upFwds
respAcks :: Vec n (Signal dom Ack)
respAcks = map snd upFwds

((reqAcks, respAck), (respDatas, reqData)) =
toSignals fanin' ((reqDatas, respData), (respAcks, reqAck))
in (zip reqAcks respDatas, (reqData, respAck))
where
fanin'
:: Circuit (Vec n (Df dom req), Df dom resp)
(Vec n (Df dom resp), Df dom req)
fanin' = circuit $ \(reqs, resp) -> do
[fwd0, fwd1]
<- Df.fanout
<| Df.roundrobinCollect @n Df.Parallel
<| repeatWithIndexC (\i -> Df.map (\x -> (i,x)))
-< reqs

activeN <- Df.map fst -< fwd1
resps <- Df.route <| Df.zip -< (activeN, resp)
req <- Df.map snd -< fwd0
idC -< (resps, req)
4 changes: 3 additions & 1 deletion clash-protocols/tests/Tests/Protocols.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Tests.Protocols (tests, main) where
import Test.Tasty
import qualified Tests.Protocols.Avalon
import qualified Tests.Protocols.Axi4
import qualified Tests.Protocols.BiDf
import qualified Tests.Protocols.Df
import qualified Tests.Protocols.DfConv
import qualified Tests.Protocols.Wishbone
Expand All @@ -11,7 +12,8 @@ tests :: TestTree
tests =
testGroup
"Protocols"
[ Tests.Protocols.Df.tests
[ Tests.Protocols.BiDf.tests
, Tests.Protocols.Df.tests
, Tests.Protocols.DfConv.tests
, Tests.Protocols.Avalon.tests
, Tests.Protocols.Axi4.tests
Expand Down
104 changes: 104 additions & 0 deletions clash-protocols/tests/Tests/Protocols/BiDf.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{-# OPTIONS_GHC -fplugin Protocols.Plugin #-}
{-# LANGUAGE TemplateHaskell #-}

module Tests.Protocols.BiDf (tests) where

-- clash-prelude
import Clash.Prelude as C
import qualified Clash.Sized.Vector as Vector
import Clash.Hedgehog.Sized.Vector

-- clash-protocols
import Protocols
import Protocols.Hedgehog
import Protocols.BiDf as BiDf

-- hedgehog
import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range

-- tasty
import Test.Tasty
import Test.Tasty.Hedgehog.Extra (testProperty)
import Test.Tasty.TH (testGroupGenerator)

-- | Ensure that 'BiDf.toDfs' composed with 'BiDf.fromDfs' behaves as an
-- identity.
prop_toDfs_fromDfs_id :: Property
prop_toDfs_fromDfs_id =
idWithModelSingleDomain @System defExpectOptions gen (\_ _ _ -> id) (exposeClockResetEnable impl)
where
gen :: Gen [Int]
gen = Gen.list (Range.linear 0 10) (Gen.integral (Range.linear 0 100))

impl :: forall dom a. (HiddenClockResetEnable dom, NFDataX a)
=> Circuit (Df dom a) (Df dom a)
impl = BiDf.toDfs <| BiDf.fromDfs

-- | Ensure that 'BiDf.loopback' behaves as an identity.
prop_loopback_id :: Property
prop_loopback_id =
idWithModelSingleDomain @System defExpectOptions gen (\_ _ _ -> id) (exposeClockResetEnable impl)
where
gen :: Gen [Int]
gen = Gen.list (Range.linear 0 10) (Gen.integral (Range.linear 0 100))

impl :: forall dom a. (HiddenClockResetEnable dom, NFDataX a)
=> Circuit (Df dom a) (Df dom a)
impl = circuit $ \req -> do
(biDf, resp) <- BiDf.fromDfs -< req
BiDf.loopback id -< biDf
idC -< resp

-- | Test that 'BiDf.fanin' on a single 'BiDf' channel behaves as an identity.
prop_fanin_id :: Property
prop_fanin_id =
idWithModelSingleDomain @System defExpectOptions gen (\_ _ _ -> id) (exposeClockResetEnable impl)
where
gen :: Gen [Int]
gen = Gen.list (Range.linear 0 10) (Gen.integral (Range.linear 0 100))

impl
:: forall dom a. (HiddenClockResetEnable dom, NFDataX a)
=> Circuit (Df dom a) (Df dom a)
impl = circuit $ \req -> do
(biDf, resp) <- BiDf.fromDfs -< req
BiDf.loopback id <| BiDf.fanin @1 -< [biDf]
idC -< resp

-- | Test that 'BiDf.fanin' on a number of 'BiDf' channels behaves as an
-- identity on each channel.
prop_fanin :: Property
prop_fanin =
idWithModelSingleDomain @System expectOpts
(gen @3)
(\_ _ _ -> id)
(exposeClockResetEnable impl)
where
expectOpts = defExpectOptions

gen :: forall n. KnownNat n => Gen (Vec n [(Index n, Int)])
gen = do
xs <- genVec @Gen @n $ Gen.list (Range.linear 0 10) (Gen.integral (Range.linear 0 100))
return $ C.zipWith (\i -> fmap (\x -> (i,x))) indicesI xs

impl
:: forall n dom a.
(HiddenClockResetEnable dom, KnownNat n, 1 <= n, NFDataX a)
=> Circuit (Vec n (Df dom a)) (Vec n (Df dom a))
impl = circuit $ \reqs -> do
(biDfs, resps) <- unbundleC <| repeatC BiDf.fromDfs -< reqs
BiDf.loopback id <| BiDf.fanin @n -< biDfs
idC -< resps

unbundleC :: forall n a b. Circuit (Vec n (a, b)) (Vec n a, Vec n b)
unbundleC = fromSignals $ \(fwd, (bwdA, bwdB)) ->
let fwdA :: Vec n (Fwd a)
fwdB :: Vec n (Fwd b)
(fwdA, fwdB) = Vector.unzip fwd
in (Vector.zip bwdA bwdB, (fwdA, fwdB))

tests :: TestTree
tests =
$(testGroupGenerator)
Loading