Skip to content

Commit

Permalink
tie up case
Browse files Browse the repository at this point in the history
Co-authored-by: Martijn Bastiaan <[email protected]>

Co-authored-by: Martijn Bastiaan <[email protected]>

Co-authored-by: Martijn Bastiaan <[email protected]>

Co-authored-by: Martijn Bastiaan <[email protected]>

Co-authored-by: Martijn Bastiaan <[email protected]>

Maybe in place of enable, stylistic changes
  • Loading branch information
vmchale committed May 6, 2022
1 parent cee9680 commit e04eb51
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
1 change: 1 addition & 0 deletions clash-cores/clash-cores.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ test-suite unittests

build-depends:
clash-cores,
clash-lib,
tasty >= 1.2 && < 1.3,
tasty-hunit,
tasty-quickcheck,
Expand Down
47 changes: 23 additions & 24 deletions clash-cores/src/Clash/Cores/Xilinx/DcFifo/Explicit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ dcFifo# ::
dcFifo# DcConfig{..} wClk rClk rst writeData wEnable rEnable =
let
(wRstBusy, wFull, wCnt, rRstBusy, rEmpty, rCnt, rData) =
go rstSignalR rstSignalW start wEnable rEnable writeData
go initState rstSignalR rEnable rstSignalW wEnable writeData
in
( wRstBusy
, wFull
Expand All @@ -180,13 +180,12 @@ dcFifo# DcConfig{..} wClk rClk rst writeData wEnable rEnable =
-- https://github.com/clash-lang/clash-compiler/blob/ea114d8edd6a110f72d148203b9db2454cae8f37/clash-prelude/src/Clash/Explicit/BlockRam.hs#L1276-L1280

go ::
Signal read Bool ->
Signal write Bool ->

FifoState n ->
Signal write Bool ->
Signal read Bool ->
Signal write (BitVector n) ->
Signal read Bool -> -- reset
Signal read Bool -> -- read enabled
Signal write Bool -> -- reset
Signal write Bool -> -- write enable
Signal write (BitVector n) -> -- write data
( Signal write ResetBusy
, Signal write Full
, Signal write (DataCount depth)
Expand All @@ -196,56 +195,56 @@ dcFifo# DcConfig{..} wClk rClk rst writeData wEnable rEnable =
, Signal read (DataCount depth)
, Signal read (BitVector n)
)
go rstR rstW st@(FifoState _ rt) wEna rEna =
go st@(FifoState _ rt) rstR rEna rstW =
if rt < tWr
then goRead rstR rstW st wEna rEna
else goWrite rstR rstW st wEna rEna
then goRead st rstR rEna rstW
else goWrite st rstR rEna rstW
-- TODO: goBoth case?

goWrite rstR (True :- rstWNext) (FifoState _ rt) (_ :- wEna) rEna (_ :- wData) =
goWrite (FifoState _ rt) rstR rEna (True :- rstWNext) (_ :- wEna) (_ :- wData) =
(1 :- wRstBusy, 0 :- preFull, 0 :- preWCnt, rRstBusy, fifoEmpty, rCnt, rData)
where
(wRstBusy, preFull, preWCnt, rRstBusy, fifoEmpty, rCnt, rData) =
go rstR rstWNext (FifoState mempty (rt-tWr)) wEna rEna wData
go (FifoState mempty (rt-tWr)) rstR rEna rstWNext wEna wData

goWrite rstR (_ :- rstW) (FifoState q rt) wEna rEna wData =
goWrite (FifoState q rt) rstR rEna (_ :- rstW) wEnas0 wDats0 =
(0 :- wRstBusy, full, wCnt, rRstBusy, fifoEmpty, rCnt, rData)
where
(wRstBusy, preFull, preWCnt, rRstBusy, fifoEmpty, rCnt, rData) =
go rstR rstW (FifoState q' (rt-tWr)) wEna' rEna wData'
go (FifoState q' (rt-tWr)) rstR rEna rstW wEnas1 wDats1

wCnt = sDepth q :- preWCnt
full = (if Seq.length q == rD then high else low) :- preFull
(en :- wEna') = wEna
(wDatum :- wData') = wData
(wDat :- wDats1) = wDats0
(wEna :- wEnas1) = wEnas0
q' =
if Seq.length q + 1 <= rD && en
then wDatum Seq.<| q
if Seq.length q + 1 <= rD && wEna
then wDat Seq.<| q
else q

sDepth = fromIntegral . Seq.length

goRead (rstR :- rstRNext) rstW (FifoState q rt) wEna rEna wData =
goRead (FifoState q rt) (rstR :- rstRNext) rEnas0 rstW wEna wData =
(wRstBusy, full, wCnt, (if rstR then 1 else 0) :- rRstBusy, fifoEmpty, rCnt, rData)
where
rCnt = sDepth q :- preRCnt
fifoEmpty = (if Seq.length q == 0 then high else low) :- preEmpty
rData = nextData :- preRData

(wRstBusy, full, wCnt, rRstBusy, preEmpty, preRCnt, preRData) =
go rstRNext rstW (FifoState q' (rt+tR)) wEna rEna' wData
go (FifoState q' (rt+tR)) rstRNext rEnas1 rstW wEna wData

(en :- rEna') = rEna
(rEna :- rEnas1) = rEnas0
(q', nextData) =
if en && not rstR
if rEna && not rstR
then
case Seq.viewr q of
Seq.EmptyR -> (q, deepErrorX "FIFO empty")
qData Seq.:> qDatum -> (qData, qDatum)
else (q, deepErrorX "Enable off or resetting")

start :: FifoState n
start = FifoState Seq.empty 0
initState :: FifoState n
initState = FifoState Seq.empty 0

tWr = snatToNum @Int (clockPeriod @write)
tR = snatToNum @Int (clockPeriod @read)
Expand Down
15 changes: 9 additions & 6 deletions clash-cores/test/Test/Cores/Xilinx/DcFifo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Control.Monad (replicateM)

import Clash.Explicit.Prelude
import Clash.Cores.Xilinx.DcFifo.Explicit
import Clash.Netlist.Util (orNothing)


import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
Expand All @@ -18,7 +20,7 @@ import Test.Tasty (TestTree)

tests :: TestTree
tests = testPropertyNamed
"FIFO doesn't lose any data"
"FIFO doesn't lose any data with small stalls"
"prop_fifo"
prop_fifo

Expand All @@ -34,12 +36,13 @@ intersperseStalls [] _ = []
prop_fifo :: Property
prop_fifo = property $ do
xs <- fmap Just <$> forAll (replicateM 10 genData)
stallRead <- forAll (Gen.maybe (Gen.int (Range.linear 0 10)))
stallWrite <- forAll (Gen.maybe (Gen.int (Range.linear 0 10)))
let iWrites = case stallRead of
-- TODO: stall one by 9, get overflow!
stallRead <- forAll (Gen.maybe (Gen.int (Range.linear 0 8)))
stallWrite <- forAll (Gen.maybe (Gen.int (Range.linear 0 8)))
let iWrites = case stallWrite of
Nothing -> []
Just i -> P.replicate i Nothing
let readStalls = case stallWrite of
let readStalls = case stallRead of
Nothing -> [False]
Just i -> False : L.replicate i True
throughFifo (intersperseStalls xs iWrites) (cycle readStalls) === catMaybes xs
Expand All @@ -56,7 +59,7 @@ takeState (_, _:stalls) (1, _, _, _) = ((False, stalls), (Nothing, False))
takeState (readLastCycle, True:stalls) (_, _, _, d) =
((False, stalls), (nextData, False))
where
nextData = if readLastCycle then Just d else Nothing
nextData = readLastCycle `orNothing` d
takeState (readLastCycle, _:stalls) (_, fifoEmpty, _, d) =
((readThisCycle, stalls), (nextData, readThisCycle))
where
Expand Down

0 comments on commit e04eb51

Please sign in to comment.