{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Safe #-}
{-# OPTIONS_HADDOCK show-extensions #-}
module Clash.Explicit.Prelude.Safe
(
mealy
, mealyB
, moore
, mooreB
, registerB
, dualFlipFlopSynchronizer
, asyncFIFOSynchronizer
, asyncRom
, asyncRomPow2
, rom
, romPow2
, asyncRam
, asyncRamPow2
, blockRam
, blockRamPow2
, readNew
, isRising
, isFalling
, riseEvery
, oscillate
, module Clash.Explicit.Signal
, module Clash.Explicit.Signal.Delayed
, module Clash.Prelude.DataFlow
, module Clash.Sized.BitVector
, module Clash.Prelude.BitIndex
, module Clash.Prelude.BitReduction
, module Clash.Sized.Signed
, module Clash.Sized.Unsigned
, module Clash.Sized.Index
, module Clash.Sized.Fixed
, module Clash.Sized.Vector
, module Clash.Sized.RTree
, module Clash.Annotations.TopEntity
, module GHC.TypeLits
, module GHC.TypeLits.Extra
, module Clash.Promoted.Nat
, module Clash.Promoted.Nat.Literals
, module Clash.Promoted.Nat.TH
, module Clash.Promoted.Symbol
, module Clash.Class.BitPack
, module Clash.Class.Num
, module Clash.Class.Resize
, module Control.Applicative
, module Data.Bits
, module Clash.XException
, undefined
, module Clash.NamedTypes
, module Prelude
)
where
import Control.Applicative
import Data.Bits
import GHC.Stack
import GHC.TypeLits
import GHC.TypeLits.Extra
import Prelude hiding
((++), (!!), concat, drop, foldl, foldl1, foldr, foldr1, head, init, iterate,
last, length, map, repeat, replicate, reverse, scanl, scanr, splitAt, tail,
take, unzip, unzip3, zip, zip3, zipWith, zipWith3, undefined)
import Clash.Annotations.TopEntity
import Clash.Class.BitPack
import Clash.Class.Num
import Clash.Class.Resize
import Clash.NamedTypes
import Clash.Explicit.BlockRam
import Clash.Explicit.Mealy
import Clash.Explicit.Moore
import Clash.Explicit.RAM
import Clash.Explicit.ROM
import Clash.Explicit.Signal
import Clash.Explicit.Signal.Delayed
import Clash.Explicit.Synchronizer
(dualFlipFlopSynchronizer, asyncFIFOSynchronizer)
import Clash.Prelude.BitIndex
import Clash.Prelude.BitReduction
import Clash.Prelude.DataFlow
import Clash.Prelude.ROM (asyncRom, asyncRomPow2)
import Clash.Promoted.Nat
import Clash.Promoted.Nat.TH
import Clash.Promoted.Nat.Literals
import Clash.Promoted.Symbol
import Clash.Sized.BitVector
import Clash.Sized.Fixed
import Clash.Sized.Index
import Clash.Sized.RTree
import Clash.Sized.Signed
import Clash.Sized.Unsigned
import Clash.Sized.Vector
import Clash.XException
registerB
:: Bundle a
=> Clock domain gated
-> Reset domain synchronous
-> a
-> Unbundled domain a
-> Unbundled domain a
registerB clk rst i = unbundle Prelude.. register clk rst i Prelude.. bundle
{-# INLINE registerB #-}
isRising
:: (Bounded a, Eq a)
=> Clock domain gated
-> Reset domain synchronous
-> a
-> Signal domain a
-> Signal domain Bool
isRising clk rst is s = liftA2 edgeDetect prev s
where
prev = register clk rst is s
edgeDetect old new = old == minBound && new == maxBound
{-# INLINABLE isRising #-}
isFalling
:: (Bounded a, Eq a)
=> Clock domain gated
-> Reset domain synchronous
-> a
-> Signal domain a
-> Signal domain Bool
isFalling clk rst is s = liftA2 edgeDetect prev s
where
prev = register clk rst is s
edgeDetect old new = old == maxBound && new == minBound
{-# INLINABLE isFalling #-}
riseEvery
:: forall domain gated synchronous n
. Clock domain gated
-> Reset domain synchronous
-> SNat n
-> Signal domain Bool
riseEvery clk rst SNat = moore clk rst transfer output 0 (pure ())
where
output :: Index n -> Bool
output = (== maxBound)
transfer :: Index n -> () -> Index n
transfer s _ = if (s == maxBound) then 0 else s+1
{-# INLINEABLE riseEvery #-}
oscillate
:: forall domain gated synchronous n
. Clock domain gated
-> Reset domain synchronous
-> Bool
-> SNat n
-> Signal domain Bool
oscillate clk rst begin SNat = moore clk rst transfer snd (0, begin) (pure ())
where
transfer :: (Index n, Bool) -> () -> (Index n, Bool)
transfer (s, i) _ =
if s == maxBound
then (0, not i)
else (s+1, i)
{-# INLINEABLE oscillate #-}
undefined :: HasCallStack => a
undefined = errorX "undefined"