Skip to content

Commit

Permalink
Generalize periodToHz
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddemoll committed Mar 3, 2023
1 parent 807ea3c commit b55d12c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/2023-03-01T10_02_55+01_00_generalized_periodToHz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed: Generalized `periodToHz`. Use type applications (`periodToHz @Natural @(Ratio Natural)`) to get the old behaviour back, in case type errors arise.
16 changes: 13 additions & 3 deletions clash-prelude/src/Clash/Signal/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Copyright : (C) 2013-2016, University of Twente,
2017-2019, Myrtle Software Ltd
2017-2022, Google Inc.,
2021-2022, QBayLogic B.V.
2021-2023, QBayLogic B.V.
License : BSD2 (see the file LICENSE)
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
-}
Expand Down Expand Up @@ -195,6 +195,8 @@ import Clash.XException
>>> import Clash.Signal.Internal
>>> import Clash.Promoted.Nat
>>> import Clash.XException
>>> import Data.Ratio (Ratio)
>>> import Numeric.Natural (Natural)
>>> type System = "System"
>>> let systemClockGen = clockGen @System
>>> let systemResetGen = resetGen @System
Expand Down Expand Up @@ -1617,11 +1619,19 @@ hzToFs freq = Femtoseconds (floor ((1.0 / freq) / 1.0e-15))
-- i.e. to calculate the clock frequency of a clock with a period of 5000 ps:
--
-- >>> periodToHz 5000
-- 2.0e8
--
-- To get the old behaviour of this function, use type applications:
--
-- >>> periodToHz @Natural @(Ratio Natural) 5000
-- 200000000 % 1
--
-- __NB__: This function is /not/ synthesizable
periodToHz :: Natural -> Ratio Natural
periodToHz period = 1.0 / (1.0e-12 * fromIntegral period)
periodToHz :: (Integral a, Fractional b) => a -> b
periodToHz = fromRational . toRational . periodToHzx
where
periodToHzx :: Integral a => a -> Ratio Natural
periodToHzx period = 1.0 / (1.0e-12 * fromIntegral period)

-- | Calculate the frequence in __Hz__, given the period in __fs__
--
Expand Down

0 comments on commit b55d12c

Please sign in to comment.