Skip to content

Commit 2d49740

Browse files
Assert that roundrobinCollect Parallel is left-biased
Fixes #169
1 parent 2a2af0c commit 2d49740

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

clash-protocols/src/Protocols/Df.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ data CollectMode
740740
| -- | Collect in a /round-robin/ fashion. If a source does not produce
741741
-- data, skip it and check the next source on the next cycle.
742742
Skip
743-
| -- | Check all sources in parallel. Biased towards the /last/ source.
743+
| -- | Check all sources in parallel. Biased towards the /first/ source.
744744
-- If the number of sources is high, this is more expensive than other
745745
-- modes.
746746
Parallel

clash-protocols/src/Protocols/PacketStream/Routing.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ packetArbiterC mode =
5454
(Df.NoSkip, _) -> satSucc SatWrap i
5555
(Df.Skip, _) -> satSucc SatWrap i
5656
(Df.Parallel, _) ->
57-
-- Index of last sink with data
57+
-- Index of first sink with data
5858
fromMaybe maxBound
5959
$ fold @(sources - 1) (<|>) (zipWith (<$) indicesI fwds)
6060

clash-protocols/tests/Tests/Protocols/Df.hs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import Prelude
1414

1515
-- clash-prelude
1616

17-
import Clash.Prelude (type (<=))
17+
import Clash.Explicit.Prelude qualified as E
18+
import Clash.Explicit.Reset (noReset)
19+
import Clash.Prelude (Vec (Nil, (:>)), type (<=))
1820
import Clash.Prelude qualified as C
1921

2022
-- containers
@@ -37,6 +39,7 @@ import Hedgehog.Range qualified as Range
3739

3840
-- tasty
3941
import Test.Tasty
42+
import Test.Tasty.HUnit (Assertion, testCase, (@?=))
4043
import Test.Tasty.Hedgehog (HedgehogTestLimit (HedgehogTestLimit))
4144
import Test.Tasty.Hedgehog.Extra (testProperty)
4245
import Test.Tasty.TH (testGroupGenerator)
@@ -47,6 +50,8 @@ import Protocols.Df qualified as Df
4750
import Protocols.Hedgehog
4851

4952
-- tests
53+
54+
import Data.Bifunctor (Bifunctor (first))
5055
import Util
5156

5257
newtype PlusInt = PlusInt Int
@@ -319,6 +324,36 @@ prop_roundrobinCollectParallel =
319324
prop :: [Int] -> [Int] -> PropertyT IO ()
320325
prop expected actual = HashSet.fromList expected === HashSet.fromList actual
321326

327+
{- | Asserts that roundrobinCollect with Parallel mode behaves in a left-biased
328+
fashion.
329+
-}
330+
case_roundrobinCollectParallel :: Assertion
331+
case_roundrobinCollectParallel = do
332+
expected @?= actual
333+
where
334+
actual =
335+
E.sampleN 5
336+
. C.bundle
337+
. first C.bundle
338+
$ dut (input0 :> input1 :> input2 :> Nil, pure $ Ack True)
339+
340+
expected =
341+
[ (Ack True :> Ack False :> Ack False :> Nil, Just (1 :: Int))
342+
, (Ack True :> Ack False :> Ack False :> Nil, Just 2)
343+
, (Ack False :> Ack True :> Ack False :> Nil, Just 10)
344+
, (Ack False :> Ack True :> Ack False :> Nil, Just 40)
345+
, (Ack False :> Ack False :> Ack True :> Nil, Just 100)
346+
]
347+
348+
input0 = C.fromList @_ @C.System [Just 1, Just 2, Nothing, Nothing, Nothing]
349+
input1 = C.fromList @_ @C.System [Just 10, Just 10, Just 10, Just 40, Nothing]
350+
input2 = C.fromList @_ @C.System [Just 100, Just 100, Just 100, Just 100, Just 100]
351+
352+
dut =
353+
toSignals $
354+
C.withClockResetEnable @C.System C.clockGen noReset C.enableGen $
355+
Df.roundrobinCollect @3 Df.Parallel
356+
322357
prop_unbundleVec :: Property
323358
prop_unbundleVec =
324359
idWithModelSingleDomain

0 commit comments

Comments
 (0)