-
Notifications
You must be signed in to change notification settings - Fork 7
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
bgamari
wants to merge
4
commits into
clash-lang:main
Choose a base branch
from
bgamari:wip/bidf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ module Protocols ( | |
-- * Basic circuits | ||
idC, | ||
repeatC, | ||
repeatWithIndexC, | ||
prod2C, | ||
|
||
-- * Simulation | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
{-# 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. The response may be produced in the same cycle the | ||
-- request is acknowledged (but see the law about a combinational path | ||
-- below). | ||
-- | ||
-- - 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. | ||
-- | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
If so, why would that not be allowed?