From 724dfbe1f7f1019e7cbed65400013a06fe603ba0 Mon Sep 17 00:00:00 2001 From: Gergo ERDI Date: Mon, 25 Nov 2024 20:41:04 +0800 Subject: [PATCH] Move block RAM reset value into the `ClearOnReset` constructor of `ResetStrategy`, to avoid arguments that are only going to be ignored on `NoClearOnReset` --- .../2024-11-25T20_38_27+08_00_reset_strategy | 2 + clash-prelude/src/Clash/Explicit/BlockRam.hs | 38 +++++++++---------- clash-prelude/src/Clash/Prelude/BlockRam.hs | 10 ++--- 3 files changed, 24 insertions(+), 26 deletions(-) create mode 100644 changelog/2024-11-25T20_38_27+08_00_reset_strategy diff --git a/changelog/2024-11-25T20_38_27+08_00_reset_strategy b/changelog/2024-11-25T20_38_27+08_00_reset_strategy new file mode 100644 index 0000000000..563801fe99 --- /dev/null +++ b/changelog/2024-11-25T20_38_27+08_00_reset_strategy @@ -0,0 +1,2 @@ +CHANGED: `ResetStrategy` now contains the reset function/value, to avoid +dummy arguments on `NoClearOnReset` \ No newline at end of file diff --git a/clash-prelude/src/Clash/Explicit/BlockRam.hs b/clash-prelude/src/Clash/Explicit/BlockRam.hs index a52084fbe9..4591442b30 100644 --- a/clash-prelude/src/Clash/Explicit/BlockRam.hs +++ b/clash-prelude/src/Clash/Explicit/BlockRam.hs @@ -854,9 +854,9 @@ blockRamPow2 = \clk en cnt rd wrM -> withFrozenCallStack (blockRam clk en cnt rd wrM) {-# INLINE blockRamPow2 #-} -data ResetStrategy (r :: Bool) where - ClearOnReset :: ResetStrategy 'True - NoClearOnReset :: ResetStrategy 'False +data ResetStrategy (r :: Bool) a where + ClearOnReset :: a -> ResetStrategy 'True a + NoClearOnReset :: ResetStrategy 'False a -- | A version of 'blockRam' that has no default values set. May be cleared to -- an arbitrary state using a reset function. @@ -875,25 +875,28 @@ blockRamU -- for the BRAM to be reset to its initial state. -> Enable dom -- ^ 'Enable' line - -> ResetStrategy r + -> ResetStrategy r (Index n -> a) -- ^ Whether to clear BRAM on asserted reset ('ClearOnReset') or -- not ('NoClearOnReset'). The reset needs to be asserted for at least /n/ -- cycles to clear the BRAM. -> SNat n -- ^ Number of elements in BRAM - -> (Index n -> a) - -- ^ If applicable (see 'ResetStrategy' argument), reset BRAM using this function -> Signal dom addr -- ^ Read address @r@ -> Signal dom (Maybe (addr, a)) -- ^ (write address @w@, value to write) -> Signal dom a -- ^ Value of the BRAM at address @r@ from the previous clock cycle -blockRamU clk rst0 en rstStrategy n@SNat initF rd0 mw0 = +blockRamU clk rst0 en rstStrategy n@SNat rd0 mw0 = case rstStrategy of - ClearOnReset -> + ClearOnReset initF -> -- Use reset infrastructure blockRamU# clk en n rd1 we1 wa1 w1 + where + rd1 = mux rstBool 0 (fromEnum <$> rd0) + we1 = mux rstBool (pure True) we0 + wa1 = mux rstBool (fromInteger . toInteger <$> waCounter) (fromEnum <$> wa0) + w1 = mux rstBool (initF <$> waCounter) w0 NoClearOnReset -> -- Ignore reset infrastructure, pass values unchanged blockRamU# clk en n @@ -912,11 +915,6 @@ blockRamU clk rst0 en rstStrategy n@SNat initF rd0 mw0 = w0 = snd . fromJustX <$> mw0 we0 = isJust <$> mw0 - rd1 = mux rstBool 0 (fromEnum <$> rd0) - we1 = mux rstBool (pure True) we0 - wa1 = mux rstBool (fromInteger . toInteger <$> waCounter) (fromEnum <$> wa0) - w1 = mux rstBool (initF <$> waCounter) w0 - -- | blockRAMU primitive blockRamU# :: forall n dom a @@ -968,7 +966,7 @@ blockRam1 -- for the BRAM to be reset to its initial state. -> Enable dom -- ^ 'Enable' line - -> ResetStrategy r + -> ResetStrategy r a -- ^ Whether to clear BRAM on asserted reset ('ClearOnReset') or -- not ('NoClearOnReset'). The reset needs to be asserted for at least /n/ -- cycles to clear the BRAM. @@ -984,9 +982,14 @@ blockRam1 -- ^ Value of the BRAM at address @r@ from the previous clock cycle blockRam1 clk rst0 en rstStrategy n@SNat a rd0 mw0 = case rstStrategy of - ClearOnReset -> + ClearOnReset a' -> -- Use reset infrastructure blockRam1# clk en n a rd1 we1 wa1 w1 + where + rd1 = mux rstBool 0 (fromEnum <$> rd0) + we1 = mux rstBool (pure True) we0 + wa1 = mux rstBool (fromInteger . toInteger <$> waCounter) (fromEnum <$> wa0) + w1 = mux rstBool (pure a') w0 NoClearOnReset -> -- Ignore reset infrastructure, pass values unchanged blockRam1# clk en n a @@ -1005,11 +1008,6 @@ blockRam1 clk rst0 en rstStrategy n@SNat a rd0 mw0 = w0 = snd . fromJustX <$> mw0 we0 = isJust <$> mw0 - rd1 = mux rstBool 0 (fromEnum <$> rd0) - we1 = mux rstBool (pure True) we0 - wa1 = mux rstBool (fromInteger . toInteger <$> waCounter) (fromEnum <$> wa0) - w1 = mux rstBool (pure a) w0 - -- | blockRAM1 primitive blockRam1# :: forall n dom a diff --git a/clash-prelude/src/Clash/Prelude/BlockRam.hs b/clash-prelude/src/Clash/Prelude/BlockRam.hs index db58c6d3c7..d8b68637c1 100644 --- a/clash-prelude/src/Clash/Prelude/BlockRam.hs +++ b/clash-prelude/src/Clash/Prelude/BlockRam.hs @@ -741,14 +741,12 @@ blockRamU , Enum addr , NFDataX addr , 1 <= n ) - => E.ResetStrategy r + => E.ResetStrategy r (Index n -> a) -- ^ Whether to clear BRAM on asserted reset ('Clash.Explicit.BlockRam.ClearOnReset') -- or not ('Clash.Explicit.BlockRam.NoClearOnReset'). The reset needs to be -- asserted for at least /n/ cycles to clear the BRAM. -> SNat n -- ^ Number of elements in BRAM - -> (Index n -> a) - -- ^ If applicable (see first argument), reset BRAM using this function -> Signal dom addr -- ^ Read address @r@ -> Signal dom (Maybe (addr, a)) @@ -756,8 +754,8 @@ blockRamU -> Signal dom a -- ^ Value of the BRAM at address @r@ from the previous clock cycle blockRamU = - \rstStrategy cnt initF rd wrM -> withFrozenCallStack - (hideClockResetEnable E.blockRamU) rstStrategy cnt initF rd wrM + \rstStrategy cnt rd wrM -> withFrozenCallStack + (hideClockResetEnable E.blockRamU) rstStrategy cnt rd wrM {-# INLINE blockRamU #-} -- | A version of 'blockRam' that is initialized with the same value on all @@ -770,7 +768,7 @@ blockRam1 , Enum addr , NFDataX addr , 1 <= n ) - => E.ResetStrategy r + => E.ResetStrategy r a -- ^ Whether to clear BRAM on asserted reset ('Clash.Explicit.BlockRam.ClearOnReset') -- or not ('Clash.Explicit.BlockRam.NoClearOnReset'). The reset needs to be -- asserted for at least /n/ cycles to clear the BRAM.