playground/haskell/clash-unsorted/LastReg.hs

71 lines
1.8 KiB
Haskell

module LastReg where
import Clash.Prelude
lastval
:: Maybe b -- ^ trigger (reset) value
-> Maybe a -- ^ value saved in register
-> Maybe a -- ^ output value of last
lastval (Just _) reg = reg
lastval Nothing _ = Nothing
lastregnextval
:: Maybe a -- ^ v signal value
-> Maybe a -- ^ value saved in register
-> Maybe a -- ^ next value for register
lastregnextval Nothing Nothing = Nothing
lastregnextval Nothing (Just reg) = Just reg
lastregnextval (Just v) _ = Just v
lastS
:: (HiddenClockResetEnable dom,
NFDataX a)
=> Signal dom (Maybe a) -- ^ v signal
-> Signal dom (Maybe b) -- ^ r (reset) signal
-> Signal dom (Maybe a) -- ^ output signal
lastS v rst = lastval <$> rst <*> reg
where
reg = register Nothing (lastregnextval <$> v <*> reg)
topEntity
:: Clock System
-> Reset System
-> Enable System
-> Signal System (Maybe Int)
-> Signal System (Maybe Int)
-> Signal System (Maybe Int)
topEntity = exposeClockResetEnable lastS
-- import qualified Data.List as L
-- *LastReg L> L.take 6 $ simulate @System (lastS (fromList [Just 1, Just 2, Nothing, Just 3, Just 4, Nothing])) [Nothing, Just 1, Just 1, Nothing, Nothing, Just 1]
-- [Nothing,Just 2,Just 2,Nothing,Nothing,Just 4]
-- -- | Calculate output, next state
-- -- output Nothing means no change in state
-- last
-- ::
-- -> Maybe a -- ^ current state
-- -> Maybe a -- ^ v
-- -> Maybe b -- ^ r
-- -> Maybe a -- ^ new state
-- -> Maybe a -- ^ output
-- last Nothing Nothing _ = (Nothing, Nothing)
-- last Nothing (Just x) _ = (Just x, Nothing)
-- last (Just s) Nothing Nothing = (Just s, Nothing)
-- last (Just s) Nothing (Just _) = (Just s, Just s)
-- last (Just s) (Just x) Nothing = (Just x, Nothing)
-- last (Just s) (Just x) (Just _) = (Just x, Just s)