I’m working through the Retroprogramming with Clash book and am currently stuck on the blinking light n times problem. Not sure what’s wrong with my current solution as it’s not blinking at all. Any hints or tips would be greatly appreciated.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wno-simplifiable-class-constraints #-}
import Clash.Class.Num
import Clash.Prelude
import RetroClash.Clock
import RetroClash.Utils
data OnOff on off = On (Index on) | Off (Index off)
deriving (Generic, NFDataX)
{-# ANN
topEntity
( Synthesize
{ t_name = "topEntity",
t_output = PortName "led_0",
t_inputs =
[PortName "clk"]
}
)
#-}
{-# OPAQUE topEntity #-}
topEntity :: Clock System -> Signal System Bit
topEntity clk = withClockResetEnable clk resetGen enableGen (blinkN d9)
blinkN ::
forall n.
(HiddenClockResetEnable System, KnownNat n, KnownNat (n + 1)) =>
SNat n ->
Signal System Bit
blinkN n = mux done (pure low) (boolToBit . isOn <$> r)
where
done :: Signal System Bool
done = (>= snatToNum n) <$> cnt
cnt :: Signal System (Index (n + 1))
cnt = register 0 $ satSucc SatBound <$> cnt
r :: Signal System (OnOff (ClockDivider System (Seconds 1)) (ClockDivider System (Seconds 1)))
r = register (Off 0) $ countOnOff <$> r
isOn :: OnOff on off -> Bool
isOn On {} = True
isOn Off {} = False
countOnOff :: (KnownNat on, KnownNat off) => OnOff on off -> OnOff on off
countOnOff (On x) = maybe (Off 0) On $ succIdx x
countOnOff (Off y) = maybe (On 0) Off $ succIdx y