{-|
Copyright  :  (C) 2013-2016, University of Twente,
                  2017-2019, Myrtle Software Ltd
                  2017,      Google Inc.
License    :  BSD2 (see the file LICENSE)
Maintainer :  Christiaan Baaij <christiaan.baaij@gmail.com>
-}

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}

#if __GLASGOW_HASKELL__ < 806
{-# LANGUAGE TypeInType #-}
#endif

{-# LANGUAGE Unsafe #-}

{-# OPTIONS_GHC -fplugin=GHC.TypeLits.Extra.Solver #-}
{-# OPTIONS_GHC -fplugin=GHC.TypeLits.Normalise #-}
{-# OPTIONS_GHC -fplugin=GHC.TypeLits.KnownNat.Solver #-}

-- See: https://github.com/clash-lang/clash-compiler/commit/721fcfa9198925661cd836668705f817bddaae3c
-- as to why we need this.
{-# OPTIONS_GHC -fno-cpr-anal #-}

{-# OPTIONS_HADDOCK show-extensions not-home #-}

module Clash.Signal.Internal
  ( -- * Datatypes
    Signal(..)
  , head#
  , tail#
    -- * Domains
  , Domain
  , KnownDomain(..)
  , KnownConfiguration
  , knownDomainByName
  , ActiveEdge(..)
  , SActiveEdge(..)
  , InitBehavior(..)
  , SInitBehavior(..)
  , ResetKind(..)
  , SResetKind(..)
  , ResetPolarity(..)
  , SResetPolarity(..)
  , DomainConfiguration(..)
  , SDomainConfiguration(..)
  -- ** Configuration type families
  , DomainPeriod
  , DomainActiveEdge
  , DomainResetKind
  , DomainInitBehavior
  , DomainResetPolarity
    -- ** Default domains
  , System
  , XilinxSystem
  , IntelSystem
  , vSystem
  , vIntelSystem
  , vXilinxSystem
    -- ** Domain utilities
  , VDomainConfiguration(..)
  , vDomain
  , createDomain
    -- * Clocks
  , Clock (..)
  , clockTag
  , hzToPeriod
  , periodToHz
    -- ** Enabling
  , Enable(..)
  , toEnable
  , fromEnable
  , enableGen
    -- * Resets
  , Reset(..)
  , unsafeToReset
  , unsafeFromReset
  , unsafeToHighPolarity
  , unsafeToLowPolarity
  , unsafeFromHighPolarity
  , unsafeFromLowPolarity
  , invertReset
    -- * Basic circuits
  , delay#
  , register#
  , asyncRegister#
  , syncRegister#
  , registerPowerup#
  , mux
    -- * Simulation and testbench functions
  , clockGen
  , resetGen
  , resetGenN
    -- * Boolean connectives
  , (.&&.), (.||.)
    -- * Simulation functions (not synthesizable)
  , simulate
    -- ** lazy version
  , simulate_lazy
    -- * List \<-\> Signal conversion (not synthesizable)
  , sample
  , sampleN
  , fromList
    -- ** lazy versions
  , sample_lazy
  , sampleN_lazy
  , fromList_lazy
    -- * QuickCheck combinators
  , testFor
    -- * Type classes
    -- ** 'Eq'-like
  , (.==.), (./=.)
    -- ** 'Ord'-like
  , (.<.), (.<=.), (.>=.), (.>.)
    -- ** 'Functor'
  , mapSignal#
    -- ** 'Applicative'
  , signal#
  , appSignal#
    -- ** 'Foldable'
  , foldr#
    -- ** 'Traversable'
  , traverse#
  -- * EXTREMELY EXPERIMENTAL
  , joinSignal#
  )
where

import Type.Reflection            (Typeable)
import Control.Applicative        (liftA2, liftA3)
import Control.DeepSeq            (NFData)
import Clash.Annotations.Primitive (hasBlackBox, dontTranslate)
import Data.Binary                (Binary)
import Data.Char                  (isAsciiUpper, isAlphaNum, isAscii)
import Data.Coerce                (coerce)
import Data.Data                  (Data)
import Data.Default.Class         (Default (..))
import Data.Hashable              (Hashable)
import Data.Proxy                 (Proxy(..))
import GHC.Generics               (Generic)
import GHC.Stack                  (HasCallStack)
import GHC.TypeLits               (KnownSymbol, Nat, Symbol, type (<=))
import Language.Haskell.TH.Syntax -- (Lift (..), Q, Dec)
import Language.Haskell.TH.Compat
import Numeric.Natural            (Natural)
import Test.QuickCheck            (Arbitrary (..), CoArbitrary(..), Property,
                                   property)

import Clash.CPP                  (fStrictMapSignal)
import Clash.Promoted.Nat         (SNat (..), snatToNum, snatToNatural)
import Clash.Promoted.Symbol      (SSymbol (..), ssymbolToString)
import Clash.XException
  (NFDataX(..), errorX, isX, deepseqX, defaultSeqX, seqX)

{- $setup
>>> :set -XDataKinds
>>> :set -XMagicHash
>>> :set -XTypeApplications
>>> :m -Clash.Prelude
>>> :m -Clash.Signal
>>> import Clash.Signal.Internal
>>> import Clash.Promoted.Nat
>>> import Clash.XException
>>> type System = "System"
>>> let systemClockGen = clockGen @System
>>> let systemResetGen = resetGen @System
>>> import Clash.Explicit.Signal (register)
>>> let registerS = register
>>> let registerA = register
-}


-- * Signal

-- | Determines clock edge memory elements are sensitive to. Not yet
-- implemented.
data ActiveEdge
  -- TODO: Implement in blackboxes:
  = Rising
  -- ^ Elements are sensitive to the rising edge (low-to-high) of the clock.
  | Falling
  -- ^ Elements are sensitive to the falling edge (high-to-low) of the clock.
  deriving (Int -> ActiveEdge -> ShowS
[ActiveEdge] -> ShowS
ActiveEdge -> String
(Int -> ActiveEdge -> ShowS)
-> (ActiveEdge -> String)
-> ([ActiveEdge] -> ShowS)
-> Show ActiveEdge
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ActiveEdge] -> ShowS
$cshowList :: [ActiveEdge] -> ShowS
show :: ActiveEdge -> String
$cshow :: ActiveEdge -> String
showsPrec :: Int -> ActiveEdge -> ShowS
$cshowsPrec :: Int -> ActiveEdge -> ShowS
Show, ActiveEdge -> ActiveEdge -> Bool
(ActiveEdge -> ActiveEdge -> Bool)
-> (ActiveEdge -> ActiveEdge -> Bool) -> Eq ActiveEdge
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ActiveEdge -> ActiveEdge -> Bool
$c/= :: ActiveEdge -> ActiveEdge -> Bool
== :: ActiveEdge -> ActiveEdge -> Bool
$c== :: ActiveEdge -> ActiveEdge -> Bool
Eq, Eq ActiveEdge
Eq ActiveEdge
-> (ActiveEdge -> ActiveEdge -> Ordering)
-> (ActiveEdge -> ActiveEdge -> Bool)
-> (ActiveEdge -> ActiveEdge -> Bool)
-> (ActiveEdge -> ActiveEdge -> Bool)
-> (ActiveEdge -> ActiveEdge -> Bool)
-> (ActiveEdge -> ActiveEdge -> ActiveEdge)
-> (ActiveEdge -> ActiveEdge -> ActiveEdge)
-> Ord ActiveEdge
ActiveEdge -> ActiveEdge -> Bool
ActiveEdge -> ActiveEdge -> Ordering
ActiveEdge -> ActiveEdge -> ActiveEdge
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ActiveEdge -> ActiveEdge -> ActiveEdge
$cmin :: ActiveEdge -> ActiveEdge -> ActiveEdge
max :: ActiveEdge -> ActiveEdge -> ActiveEdge
$cmax :: ActiveEdge -> ActiveEdge -> ActiveEdge
>= :: ActiveEdge -> ActiveEdge -> Bool
$c>= :: ActiveEdge -> ActiveEdge -> Bool
> :: ActiveEdge -> ActiveEdge -> Bool
$c> :: ActiveEdge -> ActiveEdge -> Bool
<= :: ActiveEdge -> ActiveEdge -> Bool
$c<= :: ActiveEdge -> ActiveEdge -> Bool
< :: ActiveEdge -> ActiveEdge -> Bool
$c< :: ActiveEdge -> ActiveEdge -> Bool
compare :: ActiveEdge -> ActiveEdge -> Ordering
$ccompare :: ActiveEdge -> ActiveEdge -> Ordering
$cp1Ord :: Eq ActiveEdge
Ord, (forall x. ActiveEdge -> Rep ActiveEdge x)
-> (forall x. Rep ActiveEdge x -> ActiveEdge) -> Generic ActiveEdge
forall x. Rep ActiveEdge x -> ActiveEdge
forall x. ActiveEdge -> Rep ActiveEdge x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ActiveEdge x -> ActiveEdge
$cfrom :: forall x. ActiveEdge -> Rep ActiveEdge x
Generic, ActiveEdge -> ()
(ActiveEdge -> ()) -> NFData ActiveEdge
forall a. (a -> ()) -> NFData a
rnf :: ActiveEdge -> ()
$crnf :: ActiveEdge -> ()
NFData, Typeable ActiveEdge
DataType
Constr
Typeable ActiveEdge
-> (forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ActiveEdge -> c ActiveEdge)
-> (forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c ActiveEdge)
-> (ActiveEdge -> Constr)
-> (ActiveEdge -> DataType)
-> (forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c ActiveEdge))
-> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c ActiveEdge))
-> ((forall b. Data b => b -> b) -> ActiveEdge -> ActiveEdge)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ActiveEdge -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ActiveEdge -> r)
-> (forall u. (forall d. Data d => d -> u) -> ActiveEdge -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> ActiveEdge -> u)
-> (forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge)
-> Data ActiveEdge
ActiveEdge -> DataType
ActiveEdge -> Constr
(forall b. Data b => b -> b) -> ActiveEdge -> ActiveEdge
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ActiveEdge -> c ActiveEdge
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ActiveEdge
forall a.
Typeable a
-> (forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> ActiveEdge -> u
forall u. (forall d. Data d => d -> u) -> ActiveEdge -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ActiveEdge -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ActiveEdge -> r
forall (m :: Type -> Type).
Monad m =>
(forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge
forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge
forall (c :: Type -> Type).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ActiveEdge
forall (c :: Type -> Type).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ActiveEdge -> c ActiveEdge
forall (t :: Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ActiveEdge)
forall (t :: Type -> Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ActiveEdge)
$cFalling :: Constr
$cRising :: Constr
$tActiveEdge :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge
$cgmapMo :: forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge
gmapMp :: (forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge
$cgmapMp :: forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge
gmapM :: (forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge
$cgmapM :: forall (m :: Type -> Type).
Monad m =>
(forall d. Data d => d -> m d) -> ActiveEdge -> m ActiveEdge
gmapQi :: Int -> (forall d. Data d => d -> u) -> ActiveEdge -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ActiveEdge -> u
gmapQ :: (forall d. Data d => d -> u) -> ActiveEdge -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> ActiveEdge -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ActiveEdge -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ActiveEdge -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ActiveEdge -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ActiveEdge -> r
gmapT :: (forall b. Data b => b -> b) -> ActiveEdge -> ActiveEdge
$cgmapT :: (forall b. Data b => b -> b) -> ActiveEdge -> ActiveEdge
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ActiveEdge)
$cdataCast2 :: forall (t :: Type -> Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ActiveEdge)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c ActiveEdge)
$cdataCast1 :: forall (t :: Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ActiveEdge)
dataTypeOf :: ActiveEdge -> DataType
$cdataTypeOf :: ActiveEdge -> DataType
toConstr :: ActiveEdge -> Constr
$ctoConstr :: ActiveEdge -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ActiveEdge
$cgunfold :: forall (c :: Type -> Type).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ActiveEdge
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ActiveEdge -> c ActiveEdge
$cgfoldl :: forall (c :: Type -> Type).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ActiveEdge -> c ActiveEdge
$cp1Data :: Typeable ActiveEdge
Data, Int -> ActiveEdge -> Int
ActiveEdge -> Int
(Int -> ActiveEdge -> Int)
-> (ActiveEdge -> Int) -> Hashable ActiveEdge
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: ActiveEdge -> Int
$chash :: ActiveEdge -> Int
hashWithSalt :: Int -> ActiveEdge -> Int
$chashWithSalt :: Int -> ActiveEdge -> Int
Hashable, Get ActiveEdge
[ActiveEdge] -> Put
ActiveEdge -> Put
(ActiveEdge -> Put)
-> Get ActiveEdge -> ([ActiveEdge] -> Put) -> Binary ActiveEdge
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [ActiveEdge] -> Put
$cputList :: [ActiveEdge] -> Put
get :: Get ActiveEdge
$cget :: Get ActiveEdge
put :: ActiveEdge -> Put
$cput :: ActiveEdge -> Put
Binary)

-- | Singleton version of 'ActiveEdge'
data SActiveEdge (edge :: ActiveEdge) where
  SRising  :: SActiveEdge 'Rising
  SFalling :: SActiveEdge 'Falling

instance Show (SActiveEdge edge) where
  show :: SActiveEdge edge -> String
show SActiveEdge edge
SRising = String
"SRising"
  show SActiveEdge edge
SFalling = String
"SFalling"

data ResetKind
  = Asynchronous
  -- ^ Elements respond /asynchronously/ to changes in their reset input. This
  -- means that they do /not/ wait for the next active clock edge, but respond
  -- immediately instead. Common on Intel FPGA platforms.
  | Synchronous
  -- ^ Elements respond /synchronously/ to changes in their reset input. This
  -- means that changes in their reset input won't take effect until the next
  -- active clock edge. Common on Xilinx FPGA platforms.
  deriving (Int -> ResetKind -> ShowS
[ResetKind] -> ShowS
ResetKind -> String
(Int -> ResetKind -> ShowS)
-> (ResetKind -> String)
-> ([ResetKind] -> ShowS)
-> Show ResetKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ResetKind] -> ShowS
$cshowList :: [ResetKind] -> ShowS
show :: ResetKind -> String
$cshow :: ResetKind -> String
showsPrec :: Int -> ResetKind -> ShowS
$cshowsPrec :: Int -> ResetKind -> ShowS
Show, ResetKind -> ResetKind -> Bool
(ResetKind -> ResetKind -> Bool)
-> (ResetKind -> ResetKind -> Bool) -> Eq ResetKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ResetKind -> ResetKind -> Bool
$c/= :: ResetKind -> ResetKind -> Bool
== :: ResetKind -> ResetKind -> Bool
$c== :: ResetKind -> ResetKind -> Bool
Eq, Eq ResetKind
Eq ResetKind
-> (ResetKind -> ResetKind -> Ordering)
-> (ResetKind -> ResetKind -> Bool)
-> (ResetKind -> ResetKind -> Bool)
-> (ResetKind -> ResetKind -> Bool)
-> (ResetKind -> ResetKind -> Bool)
-> (ResetKind -> ResetKind -> ResetKind)
-> (ResetKind -> ResetKind -> ResetKind)
-> Ord ResetKind
ResetKind -> ResetKind -> Bool
ResetKind -> ResetKind -> Ordering
ResetKind -> ResetKind -> ResetKind
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ResetKind -> ResetKind -> ResetKind
$cmin :: ResetKind -> ResetKind -> ResetKind
max :: ResetKind -> ResetKind -> ResetKind
$cmax :: ResetKind -> ResetKind -> ResetKind
>= :: ResetKind -> ResetKind -> Bool
$c>= :: ResetKind -> ResetKind -> Bool
> :: ResetKind -> ResetKind -> Bool
$c> :: ResetKind -> ResetKind -> Bool
<= :: ResetKind -> ResetKind -> Bool
$c<= :: ResetKind -> ResetKind -> Bool
< :: ResetKind -> ResetKind -> Bool
$c< :: ResetKind -> ResetKind -> Bool
compare :: ResetKind -> ResetKind -> Ordering
$ccompare :: ResetKind -> ResetKind -> Ordering
$cp1Ord :: Eq ResetKind
Ord, (forall x. ResetKind -> Rep ResetKind x)
-> (forall x. Rep ResetKind x -> ResetKind) -> Generic ResetKind
forall x. Rep ResetKind x -> ResetKind
forall x. ResetKind -> Rep ResetKind x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ResetKind x -> ResetKind
$cfrom :: forall x. ResetKind -> Rep ResetKind x
Generic, ResetKind -> ()
(ResetKind -> ()) -> NFData ResetKind
forall a. (a -> ()) -> NFData a
rnf :: ResetKind -> ()
$crnf :: ResetKind -> ()
NFData, Typeable ResetKind
DataType
Constr
Typeable ResetKind
-> (forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ResetKind -> c ResetKind)
-> (forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c ResetKind)
-> (ResetKind -> Constr)
-> (ResetKind -> DataType)
-> (forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c ResetKind))
-> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ResetKind))
-> ((forall b. Data b => b -> b) -> ResetKind -> ResetKind)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ResetKind -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ResetKind -> r)
-> (forall u. (forall d. Data d => d -> u) -> ResetKind -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> ResetKind -> u)
-> (forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> ResetKind -> m ResetKind)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ResetKind -> m ResetKind)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ResetKind -> m ResetKind)
-> Data ResetKind
ResetKind -> DataType
ResetKind -> Constr
(forall b. Data b => b -> b) -> ResetKind -> ResetKind
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ResetKind -> c ResetKind
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ResetKind
forall a.
Typeable a
-> (forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> ResetKind -> u
forall u. (forall d. Data d => d -> u) -> ResetKind -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ResetKind -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ResetKind -> r
forall (m :: Type -> Type).
Monad m =>
(forall d. Data d => d -> m d) -> ResetKind -> m ResetKind
forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ResetKind -> m ResetKind
forall (c :: Type -> Type).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ResetKind
forall (c :: Type -> Type).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ResetKind -> c ResetKind
forall (t :: Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ResetKind)
forall (t :: Type -> Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ResetKind)
$cSynchronous :: Constr
$cAsynchronous :: Constr
$tResetKind :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> ResetKind -> m ResetKind
$cgmapMo :: forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ResetKind -> m ResetKind
gmapMp :: (forall d. Data d => d -> m d) -> ResetKind -> m ResetKind
$cgmapMp :: forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ResetKind -> m ResetKind
gmapM :: (forall d. Data d => d -> m d) -> ResetKind -> m ResetKind
$cgmapM :: forall (m :: Type -> Type).
Monad m =>
(forall d. Data d => d -> m d) -> ResetKind -> m ResetKind
gmapQi :: Int -> (forall d. Data d => d -> u) -> ResetKind -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ResetKind -> u
gmapQ :: (forall d. Data d => d -> u) -> ResetKind -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> ResetKind -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ResetKind -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ResetKind -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ResetKind -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ResetKind -> r
gmapT :: (forall b. Data b => b -> b) -> ResetKind -> ResetKind
$cgmapT :: (forall b. Data b => b -> b) -> ResetKind -> ResetKind
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ResetKind)
$cdataCast2 :: forall (t :: Type -> Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ResetKind)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c ResetKind)
$cdataCast1 :: forall (t :: Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ResetKind)
dataTypeOf :: ResetKind -> DataType
$cdataTypeOf :: ResetKind -> DataType
toConstr :: ResetKind -> Constr
$ctoConstr :: ResetKind -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ResetKind
$cgunfold :: forall (c :: Type -> Type).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ResetKind
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ResetKind -> c ResetKind
$cgfoldl :: forall (c :: Type -> Type).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ResetKind -> c ResetKind
$cp1Data :: Typeable ResetKind
Data, Int -> ResetKind -> Int
ResetKind -> Int
(Int -> ResetKind -> Int)
-> (ResetKind -> Int) -> Hashable ResetKind
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: ResetKind -> Int
$chash :: ResetKind -> Int
hashWithSalt :: Int -> ResetKind -> Int
$chashWithSalt :: Int -> ResetKind -> Int
Hashable)

-- | Singleton version of 'ResetKind'
data SResetKind (resetKind :: ResetKind) where
  SAsynchronous :: SResetKind 'Asynchronous
  -- See 'Asynchronous' ^

  SSynchronous  :: SResetKind 'Synchronous
  -- See 'Synchronous' ^

instance Show (SResetKind reset) where
  show :: SResetKind reset -> String
show SResetKind reset
SAsynchronous = String
"SAsynchronous"
  show SResetKind reset
SSynchronous = String
"SSynchronous"

-- | Determines the value for which a reset line is considered "active"
data ResetPolarity
  = ActiveHigh
  -- ^ Reset is considered active if underlying signal is 'True'.
  | ActiveLow
  -- ^ Reset is considered active if underlying signal is 'False'.
  deriving (ResetPolarity -> ResetPolarity -> Bool
(ResetPolarity -> ResetPolarity -> Bool)
-> (ResetPolarity -> ResetPolarity -> Bool) -> Eq ResetPolarity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ResetPolarity -> ResetPolarity -> Bool
$c/= :: ResetPolarity -> ResetPolarity -> Bool
== :: ResetPolarity -> ResetPolarity -> Bool
$c== :: ResetPolarity -> ResetPolarity -> Bool
Eq, Eq ResetPolarity
Eq ResetPolarity
-> (ResetPolarity -> ResetPolarity -> Ordering)
-> (ResetPolarity -> ResetPolarity -> Bool)
-> (ResetPolarity -> ResetPolarity -> Bool)
-> (ResetPolarity -> ResetPolarity -> Bool)
-> (ResetPolarity -> ResetPolarity -> Bool)
-> (ResetPolarity -> ResetPolarity -> ResetPolarity)
-> (ResetPolarity -> ResetPolarity -> ResetPolarity)
-> Ord ResetPolarity
ResetPolarity -> ResetPolarity -> Bool
ResetPolarity -> ResetPolarity -> Ordering
ResetPolarity -> ResetPolarity -> ResetPolarity
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ResetPolarity -> ResetPolarity -> ResetPolarity
$cmin :: ResetPolarity -> ResetPolarity -> ResetPolarity
max :: ResetPolarity -> ResetPolarity -> ResetPolarity
$cmax :: ResetPolarity -> ResetPolarity -> ResetPolarity
>= :: ResetPolarity -> ResetPolarity -> Bool
$c>= :: ResetPolarity -> ResetPolarity -> Bool
> :: ResetPolarity -> ResetPolarity -> Bool
$c> :: ResetPolarity -> ResetPolarity -> Bool
<= :: ResetPolarity -> ResetPolarity -> Bool
$c<= :: ResetPolarity -> ResetPolarity -> Bool
< :: ResetPolarity -> ResetPolarity -> Bool
$c< :: ResetPolarity -> ResetPolarity -> Bool
compare :: ResetPolarity -> ResetPolarity -> Ordering
$ccompare :: ResetPolarity -> ResetPolarity -> Ordering
$cp1Ord :: Eq ResetPolarity
Ord, Int -> ResetPolarity -> ShowS
[ResetPolarity] -> ShowS
ResetPolarity -> String
(Int -> ResetPolarity -> ShowS)
-> (ResetPolarity -> String)
-> ([ResetPolarity] -> ShowS)
-> Show ResetPolarity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ResetPolarity] -> ShowS
$cshowList :: [ResetPolarity] -> ShowS
show :: ResetPolarity -> String
$cshow :: ResetPolarity -> String
showsPrec :: Int -> ResetPolarity -> ShowS
$cshowsPrec :: Int -> ResetPolarity -> ShowS
Show, (forall x. ResetPolarity -> Rep ResetPolarity x)
-> (forall x. Rep ResetPolarity x -> ResetPolarity)
-> Generic ResetPolarity
forall x. Rep ResetPolarity x -> ResetPolarity
forall x. ResetPolarity -> Rep ResetPolarity x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ResetPolarity x -> ResetPolarity
$cfrom :: forall x. ResetPolarity -> Rep ResetPolarity x
Generic, ResetPolarity -> ()
(ResetPolarity -> ()) -> NFData ResetPolarity
forall a. (a -> ()) -> NFData a
rnf :: ResetPolarity -> ()
$crnf :: ResetPolarity -> ()
NFData, Typeable ResetPolarity
DataType
Constr
Typeable ResetPolarity
-> (forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> ResetPolarity -> c ResetPolarity)
-> (forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c ResetPolarity)
-> (ResetPolarity -> Constr)
-> (ResetPolarity -> DataType)
-> (forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c ResetPolarity))
-> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c ResetPolarity))
-> ((forall b. Data b => b -> b) -> ResetPolarity -> ResetPolarity)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> ResetPolarity -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> ResetPolarity -> r)
-> (forall u. (forall d. Data d => d -> u) -> ResetPolarity -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> ResetPolarity -> u)
-> (forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity)
-> Data ResetPolarity
ResetPolarity -> DataType
ResetPolarity -> Constr
(forall b. Data b => b -> b) -> ResetPolarity -> ResetPolarity
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ResetPolarity -> c ResetPolarity
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ResetPolarity
forall a.
Typeable a
-> (forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> ResetPolarity -> u
forall u. (forall d. Data d => d -> u) -> ResetPolarity -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ResetPolarity -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ResetPolarity -> r
forall (m :: Type -> Type).
Monad m =>
(forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity
forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity
forall (c :: Type -> Type).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ResetPolarity
forall (c :: Type -> Type).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ResetPolarity -> c ResetPolarity
forall (t :: Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ResetPolarity)
forall (t :: Type -> Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ResetPolarity)
$cActiveLow :: Constr
$cActiveHigh :: Constr
$tResetPolarity :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity
$cgmapMo :: forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity
gmapMp :: (forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity
$cgmapMp :: forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity
gmapM :: (forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity
$cgmapM :: forall (m :: Type -> Type).
Monad m =>
(forall d. Data d => d -> m d) -> ResetPolarity -> m ResetPolarity
gmapQi :: Int -> (forall d. Data d => d -> u) -> ResetPolarity -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ResetPolarity -> u
gmapQ :: (forall d. Data d => d -> u) -> ResetPolarity -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> ResetPolarity -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ResetPolarity -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ResetPolarity -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ResetPolarity -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ResetPolarity -> r
gmapT :: (forall b. Data b => b -> b) -> ResetPolarity -> ResetPolarity
$cgmapT :: (forall b. Data b => b -> b) -> ResetPolarity -> ResetPolarity
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ResetPolarity)
$cdataCast2 :: forall (t :: Type -> Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ResetPolarity)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c ResetPolarity)
$cdataCast1 :: forall (t :: Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ResetPolarity)
dataTypeOf :: ResetPolarity -> DataType
$cdataTypeOf :: ResetPolarity -> DataType
toConstr :: ResetPolarity -> Constr
$ctoConstr :: ResetPolarity -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ResetPolarity
$cgunfold :: forall (c :: Type -> Type).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ResetPolarity
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ResetPolarity -> c ResetPolarity
$cgfoldl :: forall (c :: Type -> Type).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ResetPolarity -> c ResetPolarity
$cp1Data :: Typeable ResetPolarity
Data, Int -> ResetPolarity -> Int
ResetPolarity -> Int
(Int -> ResetPolarity -> Int)
-> (ResetPolarity -> Int) -> Hashable ResetPolarity
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: ResetPolarity -> Int
$chash :: ResetPolarity -> Int
hashWithSalt :: Int -> ResetPolarity -> Int
$chashWithSalt :: Int -> ResetPolarity -> Int
Hashable)

-- | Singleton version of 'ResetPolarity'
data SResetPolarity (polarity :: ResetPolarity) where
  SActiveHigh :: SResetPolarity 'ActiveHigh
  -- See: 'ActiveHigh' ^

  SActiveLow :: SResetPolarity 'ActiveLow
  -- See: 'ActiveLow' ^

instance Show (SResetPolarity polarity) where
  show :: SResetPolarity polarity -> String
show SResetPolarity polarity
SActiveHigh = String
"SActiveHigh"
  show SResetPolarity polarity
SActiveLow = String
"SActiveLow"

data InitBehavior
  = Unknown
  -- ^ Power up value of memory elements is /unknown/.
  | Defined
  -- ^ If applicable, power up value of a memory element is defined. Applies to
  -- 'register's for example, but not to 'blockRam'.
  deriving (Int -> InitBehavior -> ShowS
[InitBehavior] -> ShowS
InitBehavior -> String
(Int -> InitBehavior -> ShowS)
-> (InitBehavior -> String)
-> ([InitBehavior] -> ShowS)
-> Show InitBehavior
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [InitBehavior] -> ShowS
$cshowList :: [InitBehavior] -> ShowS
show :: InitBehavior -> String
$cshow :: InitBehavior -> String
showsPrec :: Int -> InitBehavior -> ShowS
$cshowsPrec :: Int -> InitBehavior -> ShowS
Show, InitBehavior -> InitBehavior -> Bool
(InitBehavior -> InitBehavior -> Bool)
-> (InitBehavior -> InitBehavior -> Bool) -> Eq InitBehavior
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: InitBehavior -> InitBehavior -> Bool
$c/= :: InitBehavior -> InitBehavior -> Bool
== :: InitBehavior -> InitBehavior -> Bool
$c== :: InitBehavior -> InitBehavior -> Bool
Eq, Eq InitBehavior
Eq InitBehavior
-> (InitBehavior -> InitBehavior -> Ordering)
-> (InitBehavior -> InitBehavior -> Bool)
-> (InitBehavior -> InitBehavior -> Bool)
-> (InitBehavior -> InitBehavior -> Bool)
-> (InitBehavior -> InitBehavior -> Bool)
-> (InitBehavior -> InitBehavior -> InitBehavior)
-> (InitBehavior -> InitBehavior -> InitBehavior)
-> Ord InitBehavior
InitBehavior -> InitBehavior -> Bool
InitBehavior -> InitBehavior -> Ordering
InitBehavior -> InitBehavior -> InitBehavior
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: InitBehavior -> InitBehavior -> InitBehavior
$cmin :: InitBehavior -> InitBehavior -> InitBehavior
max :: InitBehavior -> InitBehavior -> InitBehavior
$cmax :: InitBehavior -> InitBehavior -> InitBehavior
>= :: InitBehavior -> InitBehavior -> Bool
$c>= :: InitBehavior -> InitBehavior -> Bool
> :: InitBehavior -> InitBehavior -> Bool
$c> :: InitBehavior -> InitBehavior -> Bool
<= :: InitBehavior -> InitBehavior -> Bool
$c<= :: InitBehavior -> InitBehavior -> Bool
< :: InitBehavior -> InitBehavior -> Bool
$c< :: InitBehavior -> InitBehavior -> Bool
compare :: InitBehavior -> InitBehavior -> Ordering
$ccompare :: InitBehavior -> InitBehavior -> Ordering
$cp1Ord :: Eq InitBehavior
Ord, (forall x. InitBehavior -> Rep InitBehavior x)
-> (forall x. Rep InitBehavior x -> InitBehavior)
-> Generic InitBehavior
forall x. Rep InitBehavior x -> InitBehavior
forall x. InitBehavior -> Rep InitBehavior x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep InitBehavior x -> InitBehavior
$cfrom :: forall x. InitBehavior -> Rep InitBehavior x
Generic, InitBehavior -> ()
(InitBehavior -> ()) -> NFData InitBehavior
forall a. (a -> ()) -> NFData a
rnf :: InitBehavior -> ()
$crnf :: InitBehavior -> ()
NFData, Typeable InitBehavior
DataType
Constr
Typeable InitBehavior
-> (forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> InitBehavior -> c InitBehavior)
-> (forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c InitBehavior)
-> (InitBehavior -> Constr)
-> (InitBehavior -> DataType)
-> (forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c InitBehavior))
-> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c InitBehavior))
-> ((forall b. Data b => b -> b) -> InitBehavior -> InitBehavior)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> InitBehavior -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> InitBehavior -> r)
-> (forall u. (forall d. Data d => d -> u) -> InitBehavior -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> InitBehavior -> u)
-> (forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior)
-> Data InitBehavior
InitBehavior -> DataType
InitBehavior -> Constr
(forall b. Data b => b -> b) -> InitBehavior -> InitBehavior
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> InitBehavior -> c InitBehavior
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c InitBehavior
forall a.
Typeable a
-> (forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> InitBehavior -> u
forall u. (forall d. Data d => d -> u) -> InitBehavior -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> InitBehavior -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> InitBehavior -> r
forall (m :: Type -> Type).
Monad m =>
(forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior
forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior
forall (c :: Type -> Type).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c InitBehavior
forall (c :: Type -> Type).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> InitBehavior -> c InitBehavior
forall (t :: Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c InitBehavior)
forall (t :: Type -> Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c InitBehavior)
$cDefined :: Constr
$cUnknown :: Constr
$tInitBehavior :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior
$cgmapMo :: forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior
gmapMp :: (forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior
$cgmapMp :: forall (m :: Type -> Type).
MonadPlus m =>
(forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior
gmapM :: (forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior
$cgmapM :: forall (m :: Type -> Type).
Monad m =>
(forall d. Data d => d -> m d) -> InitBehavior -> m InitBehavior
gmapQi :: Int -> (forall d. Data d => d -> u) -> InitBehavior -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> InitBehavior -> u
gmapQ :: (forall d. Data d => d -> u) -> InitBehavior -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> InitBehavior -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> InitBehavior -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> InitBehavior -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> InitBehavior -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> InitBehavior -> r
gmapT :: (forall b. Data b => b -> b) -> InitBehavior -> InitBehavior
$cgmapT :: (forall b. Data b => b -> b) -> InitBehavior -> InitBehavior
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c InitBehavior)
$cdataCast2 :: forall (t :: Type -> Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c InitBehavior)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c InitBehavior)
$cdataCast1 :: forall (t :: Type -> Type) (c :: Type -> Type).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c InitBehavior)
dataTypeOf :: InitBehavior -> DataType
$cdataTypeOf :: InitBehavior -> DataType
toConstr :: InitBehavior -> Constr
$ctoConstr :: InitBehavior -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c InitBehavior
$cgunfold :: forall (c :: Type -> Type).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c InitBehavior
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> InitBehavior -> c InitBehavior
$cgfoldl :: forall (c :: Type -> Type).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> InitBehavior -> c InitBehavior
$cp1Data :: Typeable InitBehavior
Data, Int -> InitBehavior -> Int
InitBehavior -> Int
(Int -> InitBehavior -> Int)
-> (InitBehavior -> Int) -> Hashable InitBehavior
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: InitBehavior -> Int
$chash :: InitBehavior -> Int
hashWithSalt :: Int -> InitBehavior -> Int
$chashWithSalt :: Int -> InitBehavior -> Int
Hashable)

data SInitBehavior (init :: InitBehavior) where
  SUnknown :: SInitBehavior 'Unknown
  -- See: 'Unknown' ^

  SDefined :: SInitBehavior 'Defined
  -- See: 'Defined' ^

instance Show (SInitBehavior init) where
  show :: SInitBehavior init -> String
show SInitBehavior init
SUnknown = String
"SUnknown"
  show SInitBehavior init
SDefined = String
"SDefined"

-- | A domain with a name (@Domain@). Configures the behavior of various aspects
-- of a circuits. See the documentation of this record's field types for more
-- information on the options.
--
-- See module documentation of "Clash.Explicit.Signal" for more information on
-- how to create custom synthesis domains.
data DomainConfiguration
  = DomainConfiguration
  { DomainConfiguration -> Domain
_name :: Domain
  -- ^ Domain name
  , DomainConfiguration -> Nat
_period :: Nat
  -- ^ Period of clock in /ps/
  , DomainConfiguration -> ActiveEdge
_activeEdge :: ActiveEdge
  -- ^ Active edge of the clock
  , DomainConfiguration -> ResetKind
_resetKind :: ResetKind
  -- ^ Whether resets are synchronous (edge-sensitive) or asynchronous (level-sensitive)
  , DomainConfiguration -> InitBehavior
_initBehavior :: InitBehavior
  -- ^ Whether the initial (or "power up") value of memory elements is
  -- unknown/undefined, or configurable to a specific value
  , DomainConfiguration -> ResetPolarity
_resetPolarity :: ResetPolarity
  -- ^ Whether resets are active high or active low
  }
  deriving (Typeable)

-- | Helper type family for 'DomainPeriod'
type family DomainConfigurationPeriod (config :: DomainConfiguration) :: Nat where
  DomainConfigurationPeriod ('DomainConfiguration name period edge reset init polarity) = period

-- | Helper type family for 'DomainActiveEdge'
type family DomainConfigurationActiveEdge (config :: DomainConfiguration) :: ActiveEdge where
  DomainConfigurationActiveEdge ('DomainConfiguration name period edge reset init polarity) = edge

-- | Helper type family for 'DomainResetKind'
type family DomainConfigurationResetKind (config :: DomainConfiguration) :: ResetKind where
  DomainConfigurationResetKind ('DomainConfiguration name period edge reset init polarity) = reset

-- | Helper type family for 'DomainInitBehavior'
type family DomainConfigurationInitBehavior (config :: DomainConfiguration) :: InitBehavior where
  DomainConfigurationInitBehavior ('DomainConfiguration name period edge reset init polarity) = init

-- | Helper type family for 'DomainResetPolarity'
type family DomainConfigurationResetPolarity (config :: DomainConfiguration) :: ResetPolarity where
  DomainConfigurationResetPolarity ('DomainConfiguration name period edge reset init polarity) = polarity

-- | Convenience type to help to extract a period from a domain. Example usage:
--
-- @
-- myFunc :: (KnownDomain dom, DomainPeriod dom ~ 6000) => ...
-- @
type DomainPeriod (dom :: Domain) =
  DomainConfigurationPeriod (KnownConf dom)

-- | Convenience type to help to extract the active edge from a domain. Example
-- usage:
--
-- @
-- myFunc :: (KnownDomain dom, DomainActiveEdge dom ~ 'Rising) => ...
-- @
type DomainActiveEdge (dom :: Domain) =
  DomainConfigurationActiveEdge (KnownConf dom)

-- | Convenience type to help to extract the reset synchronicity from a
-- domain. Example usage:
--
-- @
-- myFunc :: (KnownDomain dom, DomainResetKind dom ~ 'Synchronous) => ...
-- @
type DomainResetKind (dom :: Domain) =
  DomainConfigurationResetKind (KnownConf dom)

-- | Convenience type to help to extract the initial value behavior from a
-- domain. Example usage:
--
-- @
-- myFunc :: (KnownDomain dom, DomainInitBehavior dom ~ 'Defined) => ...
-- @
type DomainInitBehavior (dom :: Domain) =
  DomainConfigurationInitBehavior (KnownConf dom)

-- | Convenience type to help to extract the reset polarity from a domain.
-- Example usage:
--
-- @
-- myFunc :: (KnownDomain dom, DomainResetPolarity dom ~ 'ActiveHigh) => ...
-- @
type DomainResetPolarity (dom :: Domain) =
  DomainConfigurationResetPolarity (KnownConf dom)

-- | Singleton version of 'DomainConfiguration'
data SDomainConfiguration (dom :: Domain) (conf :: DomainConfiguration) where
  SDomainConfiguration
    :: SSymbol dom
    -- Domain name ^
    -> SNat period
    -- Period of clock in /ps/ ^
    -> SActiveEdge edge
    -- Active edge of the clock (not yet
    -- implemented) ^
    -> SResetKind reset
    -- Whether resets are synchronous (edge-sensitive) or asynchronous (level-sensitive) ^
    -> SInitBehavior init
    -- Whether the initial (or "power up") value of memory elements is
    -- unknown/undefined, or configurable to a specific value ^
    -> SResetPolarity polarity
    -- Whether resets are active high or active low ^
    -> SDomainConfiguration dom ('DomainConfiguration dom period edge reset init polarity)

deriving instance Show (SDomainConfiguration dom conf)

type KnownConfiguration dom conf = (KnownDomain dom, KnownConf dom ~ conf)

-- | A 'KnownDomain' constraint indicates that a circuit's behavior depends on
-- some properties of a domain. See 'DomainConfiguration' for more information.
class KnownSymbol dom => KnownDomain (dom :: Domain) where
  type KnownConf dom :: DomainConfiguration
  -- | Returns 'SDomainConfiguration' corresponding to an instance's 'DomainConfiguration'.
  --
  -- Example usage:
  -- > knownDomain @System
  --
  knownDomain :: SDomainConfiguration dom (KnownConf dom)

-- | Version of 'knownDomain' that takes a 'SSymbol'. For example:
--
-- >>> knownDomainByName (SSymbol @"System")
-- SDomainConfiguration (SSymbol @"System") (SNat @10000) SRising SAsynchronous SDefined SActiveHigh
knownDomainByName
  :: forall dom
   . KnownDomain dom
  => SSymbol dom
  -> SDomainConfiguration dom (KnownConf dom)
knownDomainByName :: SSymbol dom -> SDomainConfiguration dom (KnownConf dom)
knownDomainByName =
  SDomainConfiguration dom (KnownConf dom)
-> SSymbol dom -> SDomainConfiguration dom (KnownConf dom)
forall a b. a -> b -> a
const SDomainConfiguration dom (KnownConf dom)
forall (dom :: Domain).
KnownDomain dom =>
SDomainConfiguration dom (KnownConf dom)
knownDomain
{-# INLINE knownDomainByName #-}

-- | A /clock/ (and /reset/) dom with clocks running at 100 MHz
instance KnownDomain System where
  type KnownConf System = 'DomainConfiguration System 10000 'Rising 'Asynchronous 'Defined 'ActiveHigh
  knownDomain :: SDomainConfiguration System (KnownConf System)
knownDomain = SSymbol System
-> SNat 10000
-> SActiveEdge 'Rising
-> SResetKind 'Asynchronous
-> SInitBehavior 'Defined
-> SResetPolarity 'ActiveHigh
-> SDomainConfiguration
     System
     ('DomainConfiguration
        System 10000 'Rising 'Asynchronous 'Defined 'ActiveHigh)
forall (dom :: Domain) (period :: Nat) (edge :: ActiveEdge)
       (reset :: ResetKind) (init :: InitBehavior)
       (polarity :: ResetPolarity).
SSymbol dom
-> SNat period
-> SActiveEdge edge
-> SResetKind reset
-> SInitBehavior init
-> SResetPolarity polarity
-> SDomainConfiguration
     dom ('DomainConfiguration dom period edge reset init polarity)
SDomainConfiguration SSymbol System
forall (s :: Domain). KnownSymbol s => SSymbol s
SSymbol SNat 10000
forall (n :: Nat). KnownNat n => SNat n
SNat SActiveEdge 'Rising
SRising SResetKind 'Asynchronous
SAsynchronous SInitBehavior 'Defined
SDefined SResetPolarity 'ActiveHigh
SActiveHigh

-- | System instance with defaults set for Xilinx FPGAs
instance KnownDomain XilinxSystem where
  type KnownConf XilinxSystem = 'DomainConfiguration XilinxSystem 10000 'Rising 'Synchronous 'Defined 'ActiveHigh
  knownDomain :: SDomainConfiguration XilinxSystem (KnownConf XilinxSystem)
knownDomain = SSymbol XilinxSystem
-> SNat 10000
-> SActiveEdge 'Rising
-> SResetKind 'Synchronous
-> SInitBehavior 'Defined
-> SResetPolarity 'ActiveHigh
-> SDomainConfiguration
     XilinxSystem
     ('DomainConfiguration
        XilinxSystem 10000 'Rising 'Synchronous 'Defined 'ActiveHigh)
forall (dom :: Domain) (period :: Nat) (edge :: ActiveEdge)
       (reset :: ResetKind) (init :: InitBehavior)
       (polarity :: ResetPolarity).
SSymbol dom
-> SNat period
-> SActiveEdge edge
-> SResetKind reset
-> SInitBehavior init
-> SResetPolarity polarity
-> SDomainConfiguration
     dom ('DomainConfiguration dom period edge reset init polarity)
SDomainConfiguration SSymbol XilinxSystem
forall (s :: Domain). KnownSymbol s => SSymbol s
SSymbol SNat 10000
forall (n :: Nat). KnownNat n => SNat n
SNat SActiveEdge 'Rising
SRising SResetKind 'Synchronous
SSynchronous SInitBehavior 'Defined
SDefined SResetPolarity 'ActiveHigh
SActiveHigh

-- | System instance with defaults set for Intel FPGAs
instance KnownDomain IntelSystem where
  type KnownConf IntelSystem = 'DomainConfiguration IntelSystem 10000 'Rising 'Asynchronous 'Defined 'ActiveHigh
  knownDomain :: SDomainConfiguration IntelSystem (KnownConf IntelSystem)
knownDomain = SSymbol IntelSystem
-> SNat 10000
-> SActiveEdge 'Rising
-> SResetKind 'Asynchronous
-> SInitBehavior 'Defined
-> SResetPolarity 'ActiveHigh
-> SDomainConfiguration
     IntelSystem
     ('DomainConfiguration
        IntelSystem 10000 'Rising 'Asynchronous 'Defined 'ActiveHigh)
forall (dom :: Domain) (period :: Nat) (edge :: ActiveEdge)
       (reset :: ResetKind) (init :: InitBehavior)
       (polarity :: ResetPolarity).
SSymbol dom
-> SNat period
-> SActiveEdge edge
-> SResetKind reset
-> SInitBehavior init
-> SResetPolarity polarity
-> SDomainConfiguration
     dom ('DomainConfiguration dom period edge reset init polarity)
SDomainConfiguration SSymbol IntelSystem
forall (s :: Domain). KnownSymbol s => SSymbol s
SSymbol SNat 10000
forall (n :: Nat). KnownNat n => SNat n
SNat SActiveEdge 'Rising
SRising SResetKind 'Asynchronous
SAsynchronous SInitBehavior 'Defined
SDefined SResetPolarity 'ActiveHigh
SActiveHigh

-- | Convenience value to allow easy "subclassing" of System domain. Should
-- be used in combination with 'createDomain'. For example, if you just want to
-- change the period but leave all other settings in tact use:
--
-- > createDomain vSystem{vName="System10", vPeriod=10}
--
vSystem :: VDomainConfiguration
vSystem :: VDomainConfiguration
vSystem = SDomainConfiguration
  System
  ('DomainConfiguration
     System 10000 'Rising 'Asynchronous 'Defined 'ActiveHigh)
-> VDomainConfiguration
forall (dom :: Domain) (conf :: DomainConfiguration).
SDomainConfiguration dom conf -> VDomainConfiguration
vDomain (KnownDomain System =>
SDomainConfiguration System (KnownConf System)
forall (dom :: Domain).
KnownDomain dom =>
SDomainConfiguration dom (KnownConf dom)
knownDomain @System)

-- | A clock (and reset) dom with clocks running at 100 MHz. Memory elements
-- respond to the rising edge of the clock, and asynchronously to changes in
-- reset signals. It has defined initial values, and active-high resets.
--
-- See module documentation of "Clash.Explicit.Signal" for more information on
-- how to create custom synthesis domains.
type System = ("System" :: Domain)


-- | Convenience value to allow easy "subclassing" of IntelSystem domain. Should
-- be used in combination with 'createDomain'. For example, if you just want to
-- change the period but leave all other settings in tact use:
--
-- > createDomain vIntelSystem{vName="Intel10", vPeriod=10}
--
vIntelSystem :: VDomainConfiguration
vIntelSystem :: VDomainConfiguration
vIntelSystem = SDomainConfiguration
  IntelSystem
  ('DomainConfiguration
     IntelSystem 10000 'Rising 'Asynchronous 'Defined 'ActiveHigh)
-> VDomainConfiguration
forall (dom :: Domain) (conf :: DomainConfiguration).
SDomainConfiguration dom conf -> VDomainConfiguration
vDomain (KnownDomain IntelSystem =>
SDomainConfiguration IntelSystem (KnownConf IntelSystem)
forall (dom :: Domain).
KnownDomain dom =>
SDomainConfiguration dom (KnownConf dom)
knownDomain @IntelSystem)

-- | A clock (and reset) dom with clocks running at 100 MHz. Memory elements
-- respond to the rising edge of the clock, and asynchronously to changes in
-- reset signals. It has defined initial values, and active-high resets.
--
-- See module documentation of "Clash.Explicit.Signal" for more information on
-- how to create custom synthesis domains.
type IntelSystem = ("IntelSystem" :: Domain)

-- | Convenience value to allow easy "subclassing" of XilinxSystem domain. Should
-- be used in combination with 'createDomain'. For example, if you just want to
-- change the period but leave all other settings in tact use:
--
-- > createDomain vXilinxSystem{vName="Xilinx10", vPeriod=10}
--
vXilinxSystem :: VDomainConfiguration
vXilinxSystem :: VDomainConfiguration
vXilinxSystem = SDomainConfiguration
  XilinxSystem
  ('DomainConfiguration
     XilinxSystem 10000 'Rising 'Synchronous 'Defined 'ActiveHigh)
-> VDomainConfiguration
forall (dom :: Domain) (conf :: DomainConfiguration).
SDomainConfiguration dom conf -> VDomainConfiguration
vDomain (KnownDomain XilinxSystem =>
SDomainConfiguration XilinxSystem (KnownConf XilinxSystem)
forall (dom :: Domain).
KnownDomain dom =>
SDomainConfiguration dom (KnownConf dom)
knownDomain @XilinxSystem)

-- | A clock (and reset) dom with clocks running at 100 MHz. Memory elements
-- respond to the rising edge of the clock, and synchronously to changes in
-- reset signals. It has defined initial values, and active-high resets.
--
-- See module documentation of "Clash.Explicit.Signal" for more information on
-- how to create custom synthesis domains.
type XilinxSystem = ("XilinxSystem" :: Domain)

-- | Same as SDomainConfiguration but allows for easy updates through record update syntax.
-- Should be used in combination with 'vDomain' and 'createDomain'. Example:
--
-- > createDomain (knownVDomain @System){vName="System10", vPeriod=10}
--
-- This duplicates the settings in the "System" domain, replaces the name and
-- period, and creates an instance for it. As most users often want to update
-- the system domain, a shortcut is available in the form:
--
-- > createDomain vSystem{vName="System10", vPeriod=10}
--
data VDomainConfiguration
  = VDomainConfiguration
  { VDomainConfiguration -> String
vName :: String
  -- ^ Corresponds to '_name' on 'DomainConfiguration'
  , VDomainConfiguration -> Natural
vPeriod :: Natural
  -- ^ Corresponds to '_period' on 'DomainConfiguration'
  , VDomainConfiguration -> ActiveEdge
vActiveEdge :: ActiveEdge
  -- ^ Corresponds to '_edge' on 'DomainConfiguration'
  , VDomainConfiguration -> ResetKind
vResetKind :: ResetKind
  -- ^ Corresponds to '_reset' on 'DomainConfiguration'
  , VDomainConfiguration -> InitBehavior
vInitBehavior :: InitBehavior
  -- ^ Corresponds to '_init' on 'DomainConfiguration'
  , VDomainConfiguration -> ResetPolarity
vResetPolarity :: ResetPolarity
  -- ^ Corresponds to '_polarity' on 'DomainConfiguration'
  }

-- | Convert 'SDomainConfiguration' to 'VDomainConfiguration'. Should be used in combination with
-- 'createDomain' only.
vDomain :: SDomainConfiguration dom conf -> VDomainConfiguration
vDomain :: SDomainConfiguration dom conf -> VDomainConfiguration
vDomain (SDomainConfiguration SSymbol dom
dom SNat period
period SActiveEdge edge
edge SResetKind reset
reset SInitBehavior init
init_ SResetPolarity polarity
polarity) =
  String
-> Natural
-> ActiveEdge
-> ResetKind
-> InitBehavior
-> ResetPolarity
-> VDomainConfiguration
VDomainConfiguration
    (SSymbol dom -> String
forall (s :: Domain). SSymbol s -> String
ssymbolToString SSymbol dom
dom)
    (SNat period -> Natural
forall (n :: Nat). SNat n -> Natural
snatToNatural SNat period
period)
    (case SActiveEdge edge
edge of {SActiveEdge edge
SRising -> ActiveEdge
Rising; SActiveEdge edge
SFalling -> ActiveEdge
Falling})
    (case SResetKind reset
reset of {SResetKind reset
SAsynchronous -> ResetKind
Asynchronous; SResetKind reset
SSynchronous -> ResetKind
Synchronous})
    (case SInitBehavior init
init_ of {SInitBehavior init
SDefined -> InitBehavior
Defined; SInitBehavior init
SUnknown -> InitBehavior
Unknown})
    (case SResetPolarity polarity
polarity of {SResetPolarity polarity
SActiveHigh -> ResetPolarity
ActiveHigh; SResetPolarity polarity
SActiveLow -> ResetPolarity
ActiveLow})

-- TODO: Function might reject valid type names. Figure out what's allowed.
isValidDomainName :: String -> Bool
isValidDomainName :: String -> Bool
isValidDomainName (Char
x:String
xs) = Char -> Bool
isAsciiUpper Char
x Bool -> Bool -> Bool
&& (Char -> Bool) -> String -> Bool
forall (t :: Type -> Type) a.
Foldable t =>
(a -> Bool) -> t a -> Bool
all Char -> Bool
isAscii String
xs Bool -> Bool -> Bool
&& (Char -> Bool) -> String -> Bool
forall (t :: Type -> Type) a.
Foldable t =>
(a -> Bool) -> t a -> Bool
all Char -> Bool
isAlphaNum String
xs
isValidDomainName String
_ = Bool
False

-- | Convenience method to express new domains in terms of others.
--
-- > createDomain (knownVDomain @System){vName="System10", vPeriod=10}
--
-- This duplicates the settings in the "System" domain, replaces the name and
-- period, and creates an instance for it. As most users often want to update
-- the system domain, a shortcut is available in the form:
--
-- > createDomain vSystem{vName="System10", vPeriod=10}
--
-- The function will create two extra identifiers. The first:
--
-- > type System10 = ..
--
-- You can use that as the dom to Clocks\/Resets\/Enables\/Signals. For example:
-- @Signal System10 Int@. Additionally, it will create a 'VDomainConfiguration' that you can
-- use in later calls to 'createDomain':
--
-- > vSystem10 = knownVDomain @System10
--
createDomain :: VDomainConfiguration -> Q [Dec]
createDomain :: VDomainConfiguration -> Q [Dec]
createDomain (VDomainConfiguration String
name Natural
period ActiveEdge
edge ResetKind
reset InitBehavior
init_ ResetPolarity
polarity) =
  if String -> Bool
isValidDomainName String
name then do
    Type
kdType <- [t| KnownDomain $nameT |]
    Type
kcType <- [t| ('DomainConfiguration $nameT $periodT $edgeT $resetKindT $initT $polarityT) |]
    Exp
sDom <- [| SDomainConfiguration SSymbol SNat $edgeE $resetKindE $initE $polarityE |]

    let vNameImpl :: Exp
vNameImpl = Exp -> Exp -> Exp
AppE (Name -> Exp
VarE 'vDomain) (Exp -> Type -> Exp
AppTypeE (Name -> Exp
VarE 'knownDomain) (TyLit -> Type
LitT (String -> TyLit
StrTyLit String
name)))
        kdImpl :: Dec
kdImpl = Name -> [Clause] -> Dec
FunD 'knownDomain [[Pat] -> Body -> [Dec] -> Clause
Clause [] (Exp -> Body
NormalB Exp
sDom) []]
        kcImpl :: Dec
kcImpl = Name -> [Type] -> Type -> Dec
mkTySynInstD ''KnownConf [TyLit -> Type
LitT (String -> TyLit
StrTyLit String
name)] Type
kcType
        vName' :: Name
vName' = String -> Name
mkName (Char
'v'Char -> ShowS
forall a. a -> [a] -> [a]
:String
name)

    [Dec] -> Q [Dec]
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure  [ -- KnownDomain instance (ex: instance KnownDomain "System" where ...)
            Maybe Overlap -> [Type] -> Type -> [Dec] -> Dec
InstanceD Maybe Overlap
forall a. Maybe a
Nothing [] Type
kdType [Dec
kcImpl, Dec
kdImpl]

            -- Type synonym (ex: type System = "System")
          , Name -> [TyVarBndr] -> Type -> Dec
TySynD (String -> Name
mkName String
name) [] (TyLit -> Type
LitT (String -> TyLit
StrTyLit String
name)  Type -> Type -> Type
`SigT`  Name -> Type
ConT ''Domain)

            -- vDomain helper (ex: vSystem = vDomain (knownDomain @System))
          , Name -> Type -> Dec
SigD Name
vName' (Name -> Type
ConT ''VDomainConfiguration)
          , Name -> [Clause] -> Dec
FunD Name
vName' [[Pat] -> Body -> [Dec] -> Clause
Clause [] (Exp -> Body
NormalB Exp
vNameImpl) []]
          ]
  else
    String -> Q [Dec]
forall a. HasCallStack => String -> a
error (String
"Domain names should be a valid Haskell type name, not: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
name)
 where

  edgeE :: Q Exp
edgeE =
    Exp -> Q Exp
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$
    case ActiveEdge
edge of
      ActiveEdge
Rising -> Name -> Exp
ConE 'SRising
      ActiveEdge
Falling -> Name -> Exp
ConE 'SFalling

  resetKindE :: Q Exp
resetKindE =
    Exp -> Q Exp
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$
    case ResetKind
reset of
      ResetKind
Asynchronous -> Name -> Exp
ConE 'SAsynchronous
      ResetKind
Synchronous -> Name -> Exp
ConE 'SSynchronous

  initE :: Q Exp
initE =
    Exp -> Q Exp
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$
    case InitBehavior
init_ of
      InitBehavior
Unknown -> Name -> Exp
ConE 'SUnknown
      InitBehavior
Defined -> Name -> Exp
ConE 'SDefined

  polarityE :: Q Exp
polarityE =
    Exp -> Q Exp
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$
    case ResetPolarity
polarity of
      ResetPolarity
ActiveHigh -> Name -> Exp
ConE 'SActiveHigh
      ResetPolarity
ActiveLow -> Name -> Exp
ConE 'SActiveLow

  nameT :: Q Type
nameT   = Type -> Q Type
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (TyLit -> Type
LitT (String -> TyLit
StrTyLit String
name))
  periodT :: Q Type
periodT = Type -> Q Type
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (TyLit -> Type
LitT (Integer -> TyLit
NumTyLit (Natural -> Integer
forall a. Integral a => a -> Integer
toInteger Natural
period)))

  edgeT :: Q Type
edgeT =
    Type -> Q Type
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Type -> Q Type) -> Type -> Q Type
forall a b. (a -> b) -> a -> b
$
    case ActiveEdge
edge of
      ActiveEdge
Rising -> Name -> Type
PromotedT 'Rising
      ActiveEdge
Falling -> Name -> Type
PromotedT 'Falling

  resetKindT :: Q Type
resetKindT =
    Type -> Q Type
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Type -> Q Type) -> Type -> Q Type
forall a b. (a -> b) -> a -> b
$
    case ResetKind
reset of
      ResetKind
Asynchronous -> Name -> Type
PromotedT 'Asynchronous
      ResetKind
Synchronous -> Name -> Type
PromotedT 'Synchronous

  initT :: Q Type
initT =
    Type -> Q Type
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Type -> Q Type) -> Type -> Q Type
forall a b. (a -> b) -> a -> b
$
    case InitBehavior
init_ of
      InitBehavior
Unknown -> Name -> Type
PromotedT 'Unknown
      InitBehavior
Defined -> Name -> Type
PromotedT 'Defined

  polarityT :: Q Type
polarityT =
    Type -> Q Type
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Type -> Q Type) -> Type -> Q Type
forall a b. (a -> b) -> a -> b
$
    case ResetPolarity
polarity of
      ResetPolarity
ActiveHigh -> Name -> Type
PromotedT 'ActiveHigh
      ResetPolarity
ActiveLow -> Name -> Type
PromotedT 'ActiveLow


type Domain = Symbol

infixr 5 :-
{- | Clash has synchronous 'Signal's in the form of:

@
'Signal' (dom :: 'Domain') a
@

Where /a/ is the type of the value of the 'Signal', for example /Int/ or /Bool/,
and /dom/ is the /clock-/ (and /reset-/) domain to which the memory elements
manipulating these 'Signal's belong.

The type-parameter, /dom/, is of the kind 'Domain' - a simple string. That
string refers to a single /synthesis domain/. A synthesis domain describes the
behavior of certain aspects of memory elements in it.

* __NB__: \"Bad things\"™  happen when you actually use a clock period of @0@,
so do __not__ do that!
* __NB__: You should be judicious using a clock with period of @1@ as you can
never create a clock that goes any faster!
* __NB__: For the best compatibility make sure your period is divisible by 2,
because some VHDL simulators don't support fractions of picoseconds.
* __NB__: Whether 'System' has good defaults depends on your target platform.
Check out 'IntelSystem' and 'XilinxSystem' too!

See the module documentation of "Clash.Signal" for more information about
domains.
-}
data Signal (dom :: Domain) a
  -- | The constructor, @(':-')@, is __not__ synthesizable.
  = a :- Signal dom a

head# :: Signal dom a -> a
head# :: Signal dom a -> a
head# (a
x' :- Signal dom a
_ )  = a
x'

tail# :: Signal dom a -> Signal dom a
tail# :: Signal dom a -> Signal dom a
tail# (a
_  :- Signal dom a
xs') = Signal dom a
xs'

instance Show a => Show (Signal dom a) where
  show :: Signal dom a -> String
show (a
x :- Signal dom a
xs) = a -> String
forall a. Show a => a -> String
show a
x String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Signal dom a -> String
forall a. Show a => a -> String
show Signal dom a
xs

instance Lift a => Lift (Signal dom a) where
  lift :: Signal dom a -> Q Exp
lift ~(a
x :- Signal dom a
_) = [| signal# x |]
#if MIN_VERSION_template_haskell(2,16,0)
  liftTyped :: Signal dom a -> Q (TExp (Signal dom a))
liftTyped = Signal dom a -> Q (TExp (Signal dom a))
forall a. Lift a => a -> Q (TExp a)
liftTypedFromUntyped
#endif

instance Default a => Default (Signal dom a) where
  def :: Signal dom a
def = a -> Signal dom a
forall a (dom :: Domain). a -> Signal dom a
signal# a
forall a. Default a => a
def

instance Functor (Signal dom) where
  fmap :: (a -> b) -> Signal dom a -> Signal dom b
fmap = (a -> b) -> Signal dom a -> Signal dom b
forall a b (dom :: Domain).
(a -> b) -> Signal dom a -> Signal dom b
mapSignal#

mapSignal# :: forall a b dom. (a -> b) -> Signal dom a -> Signal dom b
mapSignal# :: (a -> b) -> Signal dom a -> Signal dom b
mapSignal# a -> b
f = Signal dom a -> Signal dom b
go
 where
  -- See -fstrict-mapSignal documentation in clash-prelude.cabal
  theSeq :: a -> b -> b
theSeq = if Bool
fStrictMapSignal then a -> b -> b
forall a b. a -> b -> b
seqX else (b -> a -> b) -> a -> b -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip b -> a -> b
forall a b. a -> b -> a
const
  go :: Signal dom a -> Signal dom b
go ~(xs :: Signal dom a
xs@(a
a :- Signal dom a
as)) = a -> b
f a
a b -> Signal dom b -> Signal dom b
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- (a
a a -> Signal dom b -> Signal dom b
forall a b. a -> b -> b
`theSeq` (Signal dom a
xs Signal dom a -> Signal dom b -> Signal dom b
`seq` Signal dom a -> Signal dom b
go Signal dom a
as))
{-# NOINLINE mapSignal# #-}
{-# ANN mapSignal# hasBlackBox #-}

instance Applicative (Signal dom) where
  pure :: a -> Signal dom a
pure  = a -> Signal dom a
forall a (dom :: Domain). a -> Signal dom a
signal#
  <*> :: Signal dom (a -> b) -> Signal dom a -> Signal dom b
(<*>) = Signal dom (a -> b) -> Signal dom a -> Signal dom b
forall (dom :: Domain) a b.
Signal dom (a -> b) -> Signal dom a -> Signal dom b
appSignal#

signal# :: a -> Signal dom a
signal# :: a -> Signal dom a
signal# a
a = let s :: Signal dom a
s = a
a a -> Signal dom a -> Signal dom a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- Signal dom a
s in Signal dom a
s
{-# NOINLINE signal# #-}
{-# ANN signal# hasBlackBox #-}

appSignal# :: Signal dom (a -> b) -> Signal dom a -> Signal dom b
appSignal# :: Signal dom (a -> b) -> Signal dom a -> Signal dom b
appSignal# (a -> b
f :- Signal dom (a -> b)
fs) xs :: Signal dom a
xs@(~(a
a :- Signal dom a
as)) = a -> b
f a
a b -> Signal dom b -> Signal dom b
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- (Signal dom a
xs Signal dom a -> Signal dom b -> Signal dom b
`seq` Signal dom (a -> b) -> Signal dom a -> Signal dom b
forall (dom :: Domain) a b.
Signal dom (a -> b) -> Signal dom a -> Signal dom b
appSignal# Signal dom (a -> b)
fs Signal dom a
as) -- See [NOTE: Lazy ap]
{-# NOINLINE appSignal# #-}
{-# ANN appSignal# hasBlackBox #-}

instance NFDataX a => NFDataX (Signal domain a) where
  deepErrorX :: String -> Signal domain a
deepErrorX = a -> Signal domain a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (a -> Signal domain a)
-> (String -> a) -> String -> Signal domain a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX
  ensureSpine :: Signal domain a -> Signal domain a
ensureSpine Signal domain a
s = case Signal domain a -> Either String (Signal domain a)
forall a. a -> Either String a
isX Signal domain a
s of
    Left String
e -> String -> Signal domain a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX String
e
    Right (a
a :- Signal domain a
s') -> a -> a
forall a. NFDataX a => a -> a
ensureSpine a
a a -> Signal domain a -> Signal domain a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- Signal domain a -> Signal domain a
forall a. NFDataX a => a -> a
ensureSpine Signal domain a
s'
  hasUndefined :: Signal domain a -> Bool
hasUndefined = String -> Signal domain a -> Bool
forall a. HasCallStack => String -> a
error String
"hasUndefined on (Signal domain a): No sensible implementation exists"
  rnfX :: Signal domain a -> ()
rnfX = String -> Signal domain a -> ()
forall a. HasCallStack => String -> a
error String
"rnfX on (Signal domain a): No sensible implementation exists"

{- NOTE: Lazy ap
Signal's ap, i.e (Applicative.<*>), must be lazy in it's second argument:

> appSignal :: Signal clk (a -> b) -> Signal clk a -> Signal clk b
> appSignal (f :- fs) ~(a :- as) = f a :- appSignal fs as

because some feedback loops, such as the loop described in 'system' in the
example at https://hackage.haskell.org/package/clash-prelude-1.0.0/docs/Clash-Prelude-BlockRam.html,
will lead to "Exception <<loop>>".

However, this "naive" lazy version is _too_ lazy and induces spaceleaks.
The current version:

> appSignal# :: Signal clk (a -> b) -> Signal clk a -> Signal clk b
> appSignal# (f :- fs) xs@(~(a :- as)) = f a :- (xs `seq` appSignal# fs as)

Is lazy enough to handle the earlier mentioned feedback loops, but doesn't leak
(as much) memory like the "naive" lazy version, because the Signal constructor
of the second argument is evaluated as soon as the tail of the result is evaluated.
-}


-- | __WARNING: EXTREMELY EXPERIMENTAL__
--
-- The circuit semantics of this operation are unclear and/or non-existent.
-- There is a good reason there is no 'Monad' instance for 'Signal''.
--
-- Is currently treated as 'id' by the Clash compiler.
joinSignal# :: Signal dom (Signal dom a) -> Signal dom a
joinSignal# :: Signal dom (Signal dom a) -> Signal dom a
joinSignal# ~(Signal dom a
xs :- Signal dom (Signal dom a)
xss) = Signal dom a -> a
forall (dom :: Domain) a. Signal dom a -> a
head# Signal dom a
xs a -> Signal dom a -> Signal dom a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- Signal dom (Signal dom a) -> Signal dom a
forall (dom :: Domain) a. Signal dom (Signal dom a) -> Signal dom a
joinSignal# ((Signal dom a -> Signal dom a)
-> Signal dom (Signal dom a) -> Signal dom (Signal dom a)
forall a b (dom :: Domain).
(a -> b) -> Signal dom a -> Signal dom b
mapSignal# Signal dom a -> Signal dom a
forall (dom :: Domain) a. Signal dom a -> Signal dom a
tail# Signal dom (Signal dom a)
xss)
{-# NOINLINE joinSignal# #-}
{-# ANN joinSignal# hasBlackBox #-}

instance Num a => Num (Signal dom a) where
  + :: Signal dom a -> Signal dom a -> Signal dom a
(+)         = (a -> a -> a) -> Signal dom a -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(+)
  (-)         = (a -> a -> a) -> Signal dom a -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (-)
  * :: Signal dom a -> Signal dom a -> Signal dom a
(*)         = (a -> a -> a) -> Signal dom a -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(*)
  negate :: Signal dom a -> Signal dom a
negate      = (a -> a) -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
negate
  abs :: Signal dom a -> Signal dom a
abs         = (a -> a) -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
abs
  signum :: Signal dom a -> Signal dom a
signum      = (a -> a) -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
signum
  fromInteger :: Integer -> Signal dom a
fromInteger = a -> Signal dom a
forall a (dom :: Domain). a -> Signal dom a
signal# (a -> Signal dom a) -> (Integer -> a) -> Integer -> Signal dom a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Num a => Integer -> a
fromInteger

-- | __NB__: Not synthesizable
--
-- __NB__: In \"@'foldr' f z s@\":
--
-- * The function @f@ should be /lazy/ in its second argument.
-- * The @z@ element will never be used.
instance Foldable (Signal dom) where
  foldr :: (a -> b -> b) -> b -> Signal dom a -> b
foldr = (a -> b -> b) -> b -> Signal dom a -> b
forall a b (dom :: Domain). (a -> b -> b) -> b -> Signal dom a -> b
foldr#

-- | __NB__: Not synthesizable
--
-- __NB__: In \"@'foldr#' f z s@\":
--
-- * The function @f@ should be /lazy/ in its second argument.
-- * The @z@ element will never be used.
foldr# :: (a -> b -> b) -> b -> Signal dom a -> b
foldr# :: (a -> b -> b) -> b -> Signal dom a -> b
foldr# a -> b -> b
f b
z (a
a :- Signal dom a
s) = a
a a -> b -> b
`f` ((a -> b -> b) -> b -> Signal dom a -> b
forall a b (dom :: Domain). (a -> b -> b) -> b -> Signal dom a -> b
foldr# a -> b -> b
f b
z Signal dom a
s)
{-# NOINLINE foldr# #-}
{-# ANN foldr# hasBlackBox #-}

instance Traversable (Signal dom) where
  traverse :: (a -> f b) -> Signal dom a -> f (Signal dom b)
traverse = (a -> f b) -> Signal dom a -> f (Signal dom b)
forall (f :: Type -> Type) a b (dom :: Domain).
Applicative f =>
(a -> f b) -> Signal dom a -> f (Signal dom b)
traverse#

traverse# :: Applicative f => (a -> f b) -> Signal dom a -> f (Signal dom b)
traverse# :: (a -> f b) -> Signal dom a -> f (Signal dom b)
traverse# a -> f b
f (a
a :- Signal dom a
s) = b -> Signal dom b -> Signal dom b
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
(:-) (b -> Signal dom b -> Signal dom b)
-> f b -> f (Signal dom b -> Signal dom b)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a f (Signal dom b -> Signal dom b)
-> f (Signal dom b) -> f (Signal dom b)
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> (a -> f b) -> Signal dom a -> f (Signal dom b)
forall (f :: Type -> Type) a b (dom :: Domain).
Applicative f =>
(a -> f b) -> Signal dom a -> f (Signal dom b)
traverse# a -> f b
f Signal dom a
s
{-# NOINLINE traverse# #-}
{-# ANN traverse# hasBlackBox #-}

-- * Clocks, resets, and enables

-- | A signal of booleans, indicating whether a component is enabled. No special
-- meaning is implied, it's up to the component itself to decide how to respond
-- to its enable line. It is used throughout Clash as a global enable signal.
newtype Enable dom = Enable (Signal dom Bool)

-- | Convert 'Enable' construct to its underlying representation: a signal of
-- bools.
fromEnable :: Enable dom -> Signal dom Bool
fromEnable :: Enable dom -> Signal dom Bool
fromEnable = Enable dom -> Signal dom Bool
coerce
{-# INLINE fromEnable #-}

-- | Convert a signal of bools to an 'Enable' construct
toEnable :: Signal dom Bool -> Enable dom
toEnable :: Signal dom Bool -> Enable dom
toEnable = Signal dom Bool -> Enable dom
coerce
{-# INLINE toEnable #-}

-- | Enable generator for some domain. Is simply always True.
enableGen :: Enable dom
enableGen :: Enable dom
enableGen = Signal dom Bool -> Enable dom
forall (dom :: Domain). Signal dom Bool -> Enable dom
toEnable (Bool -> Signal dom Bool
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Bool
True)

-- | A clock signal belonging to a domain named /dom/.
data Clock (dom :: Domain) = Clock (SSymbol dom)

instance Show (Clock dom) where
  show :: Clock dom -> String
show (Clock SSymbol dom
dom) = String
"<Clock: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SSymbol dom -> String
forall (s :: Domain). SSymbol s -> String
ssymbolToString SSymbol dom
dom String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
">"

-- | Extract dom symbol from Clock
clockTag
  :: Clock dom
  -> SSymbol dom
clockTag :: Clock dom -> SSymbol dom
clockTag (Clock SSymbol dom
dom) = SSymbol dom
dom

-- | Clock generator for simulations. Do __not__ use this clock generator for
-- for the /testBench/ function, use 'tbClockGen' instead.
--
-- To be used like:
--
-- @
-- clkSystem = clockGen @System
-- @
--
-- See 'DomainConfiguration' for more information on how to use synthesis domains.
clockGen
  :: KnownDomain dom
  => Clock dom
clockGen :: Clock dom
clockGen = SSymbol dom -> Clock dom
forall (dom :: Domain). SSymbol dom -> Clock dom
Clock SSymbol dom
forall (s :: Domain). KnownSymbol s => SSymbol s
SSymbol
{-# NOINLINE clockGen #-}
{-# ANN clockGen hasBlackBox #-}



-- | Reset generator
--
-- To be used like:
--
-- @
-- rstSystem = resetGen @System
-- @
--
-- See 'tbClockGen' for example usage.
--
resetGen
  :: forall dom
   . KnownDomain dom
  => Reset dom
resetGen :: Reset dom
resetGen = SNat 1 -> Reset dom
forall (dom :: Domain) (n :: Nat).
(KnownDomain dom, 1 <= n) =>
SNat n -> Reset dom
resetGenN (KnownNat 1 => SNat 1
forall (n :: Nat). KnownNat n => SNat n
SNat @1)
{-# INLINE resetGen #-}

-- | Generate reset that's asserted for the first /n/ cycles.
--
-- To be used like:
--
-- @
-- rstSystem5 = resetGen @System (SNat @5)
-- @
--
-- Example usage:
--
-- >>> sampleN 7 (unsafeToHighPolarity (resetGenN @System (SNat @3)))
-- [True,True,True,False,False,False,False]
--
resetGenN
  :: forall dom n
   . (KnownDomain dom, 1 <= n)
  => SNat n
  -- ^ Number of initial cycles to hold reset high
  -> Reset dom
resetGenN :: SNat n -> Reset dom
resetGenN SNat n
n =
  let asserted :: [Bool]
asserted = Int -> Bool -> [Bool]
forall a. Int -> a -> [a]
replicate (SNat n -> Int
forall a (n :: Nat). Num a => SNat n -> a
snatToNum SNat n
n) Bool
True in
  Signal dom Bool -> Reset dom
forall (dom :: Domain).
KnownDomain dom =>
Signal dom Bool -> Reset dom
unsafeFromHighPolarity ([Bool] -> Signal dom Bool
forall a (dom :: Domain). NFDataX a => [a] -> Signal dom a
fromList ([Bool]
asserted [Bool] -> [Bool] -> [Bool]
forall a. [a] -> [a] -> [a]
++ Bool -> [Bool]
forall a. a -> [a]
repeat Bool
False))
{-# ANN resetGenN hasBlackBox #-}
{-# NOINLINE resetGenN #-}


-- | A reset signal belonging to a domain called /dom/.
--
-- The underlying representation of resets is 'Bool'.
data Reset (dom :: Domain) = Reset (Signal dom Bool)

-- | Non-ambiguous version of 'Clash.Signal.Internal.Ambiguous.resetPolarity'
resetPolarityProxy
  :: forall dom proxy polarity
   . (KnownDomain dom, DomainResetPolarity dom ~ polarity)
  => proxy dom
  -> SResetPolarity polarity
resetPolarityProxy :: proxy dom -> SResetPolarity polarity
resetPolarityProxy proxy dom
_proxy =
  case KnownDomain dom => SDomainConfiguration dom (KnownConf dom)
forall (dom :: Domain).
KnownDomain dom =>
SDomainConfiguration dom (KnownConf dom)
knownDomain @dom of
    SDomainConfiguration SSymbol dom
_dom SNat period
_period SActiveEdge edge
_edge SResetKind reset
_sync SInitBehavior init
_init SResetPolarity polarity
polarity ->
      SResetPolarity polarity
SResetPolarity polarity
polarity

-- | Convert a reset to an active high reset. Has no effect if reset is already
-- an active high reset. Is unsafe because it can introduce:
--
-- * <Clash-Explicit-Signal.html#metastability meta-stability>
--
-- For asynchronous resets it is unsafe because it can cause combinatorial
-- loops. In case of synchronous resets it can lead to
-- <Clash-Explicit-Signal.html#metastability meta-stability> in the presence of
-- asynchronous resets.
unsafeToHighPolarity
  :: forall dom
   . KnownDomain dom
  => Reset dom
  -> Signal dom Bool
unsafeToHighPolarity :: Reset dom -> Signal dom Bool
unsafeToHighPolarity (Reset dom -> Signal dom Bool
forall (dom :: Domain). Reset dom -> Signal dom Bool
unsafeFromReset -> Signal dom Bool
r) =
  case Proxy dom
-> SResetPolarity
     (DomainConfigurationResetPolarity (KnownConf dom))
forall (dom :: Domain) (proxy :: Domain -> Type)
       (polarity :: ResetPolarity).
(KnownDomain dom, DomainResetPolarity dom ~ polarity) =>
proxy dom -> SResetPolarity polarity
resetPolarityProxy (Proxy dom
forall k (t :: k). Proxy t
Proxy @dom) of
    SResetPolarity (DomainConfigurationResetPolarity (KnownConf dom))
SActiveHigh -> Signal dom Bool
r
    SResetPolarity (DomainConfigurationResetPolarity (KnownConf dom))
SActiveLow -> Bool -> Bool
not (Bool -> Bool) -> Signal dom Bool -> Signal dom Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Signal dom Bool
r
{-# INLINE unsafeToHighPolarity #-}

-- | Convert a reset to an active low reset. Has no effect if reset is already
-- an active low reset. It is unsafe because it can introduce:
--
-- * <Clash-Explicit-Signal.html#metastability meta-stability>
--
-- For asynchronous resets it is unsafe because it can cause combinatorial
-- loops. In case of synchronous resets it can lead to
-- <Clash-Explicit-Signal.html#metastability meta-stability> in the presence of
-- asynchronous resets.
unsafeToLowPolarity
  :: forall dom
   . KnownDomain dom
  => Reset dom
  -> Signal dom Bool
unsafeToLowPolarity :: Reset dom -> Signal dom Bool
unsafeToLowPolarity (Reset dom -> Signal dom Bool
forall (dom :: Domain). Reset dom -> Signal dom Bool
unsafeFromReset -> Signal dom Bool
r) =
  case Proxy dom
-> SResetPolarity
     (DomainConfigurationResetPolarity (KnownConf dom))
forall (dom :: Domain) (proxy :: Domain -> Type)
       (polarity :: ResetPolarity).
(KnownDomain dom, DomainResetPolarity dom ~ polarity) =>
proxy dom -> SResetPolarity polarity
resetPolarityProxy (Proxy dom
forall k (t :: k). Proxy t
Proxy @dom) of
    SResetPolarity (DomainConfigurationResetPolarity (KnownConf dom))
SActiveHigh -> Bool -> Bool
not (Bool -> Bool) -> Signal dom Bool -> Signal dom Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Signal dom Bool
r
    SResetPolarity (DomainConfigurationResetPolarity (KnownConf dom))
SActiveLow -> Signal dom Bool
r
{-# INLINE unsafeToLowPolarity #-}

-- | 'unsafeFromReset' is unsafe because it can introduce:
--
-- * <Clash-Explicit-Signal.html#metastability meta-stability>
--
-- For asynchronous resets it is unsafe because it can cause combinatorial
-- loops. In case of synchronous resets it can lead to
-- <Clash-Explicit-Signal.html#metastability meta-stability> in the presence of
-- asynchronous resets.
--
-- __NB__: You probably want to use 'unsafeToLowPolarity' or
-- 'unsafeToHighPolarity'.
unsafeFromReset
  :: Reset dom
  -> Signal dom Bool
unsafeFromReset :: Reset dom -> Signal dom Bool
unsafeFromReset (Reset Signal dom Bool
r) = Signal dom Bool
r
{-# NOINLINE unsafeFromReset #-}
{-# ANN unsafeFromReset hasBlackBox #-}

-- | 'unsafeToReset' is unsafe. For asynchronous resets it is unsafe
-- because it can introduce combinatorial loops. In case of synchronous resets
-- it can lead to <Clash-Explicit-Signal.html#metastability meta-stability>
-- issues in the presence of asynchronous resets.
--
-- __NB__: You probably want to use 'unsafeFromLowPolarity' or
-- 'unsafeFromHighPolarity'.
unsafeToReset
  :: Signal dom Bool
  -> Reset dom
unsafeToReset :: Signal dom Bool -> Reset dom
unsafeToReset Signal dom Bool
r = Signal dom Bool -> Reset dom
forall (dom :: Domain). Signal dom Bool -> Reset dom
Reset Signal dom Bool
r
{-# NOINLINE unsafeToReset #-}
{-# ANN unsafeToReset hasBlackBox #-}

-- | Interpret a signal of bools as an active high reset and convert it to
-- a reset signal corresponding to the domain's setting.
--
-- For asynchronous resets it is unsafe because it can cause combinatorial
-- loops. In case of synchronous resets it can lead to
-- <Clash-Explicit-Signal.html#metastability meta-stability> in the presence of
-- asynchronous resets.
unsafeFromHighPolarity
  :: forall dom
   . KnownDomain dom
  => Signal dom Bool
  -- ^ Reset signal that's 'True' when active, and 'False' when inactive.
  -> Reset dom
unsafeFromHighPolarity :: Signal dom Bool -> Reset dom
unsafeFromHighPolarity Signal dom Bool
r =
  Signal dom Bool -> Reset dom
forall (dom :: Domain). Signal dom Bool -> Reset dom
unsafeToReset (Signal dom Bool -> Reset dom) -> Signal dom Bool -> Reset dom
forall a b. (a -> b) -> a -> b
$
    case Proxy dom
-> SResetPolarity
     (DomainConfigurationResetPolarity (KnownConf dom))
forall (dom :: Domain) (proxy :: Domain -> Type)
       (polarity :: ResetPolarity).
(KnownDomain dom, DomainResetPolarity dom ~ polarity) =>
proxy dom -> SResetPolarity polarity
resetPolarityProxy (Proxy dom
forall k (t :: k). Proxy t
Proxy @dom) of
      SResetPolarity (DomainConfigurationResetPolarity (KnownConf dom))
SActiveHigh -> Signal dom Bool
r
      SResetPolarity (DomainConfigurationResetPolarity (KnownConf dom))
SActiveLow -> Bool -> Bool
not (Bool -> Bool) -> Signal dom Bool -> Signal dom Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Signal dom Bool
r

-- | Interpret a signal of bools as an active low reset and convert it to
-- a reset signal corresponding to the domain's setting.
--
-- For asynchronous resets it is unsafe because it can cause combinatorial
-- loops. In case of synchronous resets it can lead to
-- <Clash-Explicit-Signal.html#metastability meta-stability> in the presence of
-- asynchronous resets.
unsafeFromLowPolarity
  :: forall dom
   . KnownDomain dom
  => Signal dom Bool
  -- ^ Reset signal that's 'False' when active, and 'True' when inactive.
  -> Reset dom
unsafeFromLowPolarity :: Signal dom Bool -> Reset dom
unsafeFromLowPolarity Signal dom Bool
r =
  Signal dom Bool -> Reset dom
forall (dom :: Domain). Signal dom Bool -> Reset dom
unsafeToReset (Signal dom Bool -> Reset dom) -> Signal dom Bool -> Reset dom
forall a b. (a -> b) -> a -> b
$
    case Proxy dom
-> SResetPolarity
     (DomainConfigurationResetPolarity (KnownConf dom))
forall (dom :: Domain) (proxy :: Domain -> Type)
       (polarity :: ResetPolarity).
(KnownDomain dom, DomainResetPolarity dom ~ polarity) =>
proxy dom -> SResetPolarity polarity
resetPolarityProxy (Proxy dom
forall k (t :: k). Proxy t
Proxy @dom) of
      SResetPolarity (DomainConfigurationResetPolarity (KnownConf dom))
SActiveHigh -> Bool -> Bool
not (Bool -> Bool) -> Signal dom Bool -> Signal dom Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Signal dom Bool
r
      SResetPolarity (DomainConfigurationResetPolarity (KnownConf dom))
SActiveLow -> Signal dom Bool
r

-- | Invert reset signal
invertReset :: Reset dom -> Reset dom
invertReset :: Reset dom -> Reset dom
invertReset = Signal dom Bool -> Reset dom
forall (dom :: Domain). Signal dom Bool -> Reset dom
unsafeToReset (Signal dom Bool -> Reset dom)
-> (Reset dom -> Signal dom Bool) -> Reset dom -> Reset dom
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> Bool) -> Signal dom Bool -> Signal dom Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> Bool
not (Signal dom Bool -> Signal dom Bool)
-> (Reset dom -> Signal dom Bool) -> Reset dom -> Signal dom Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Reset dom -> Signal dom Bool
forall (dom :: Domain). Reset dom -> Signal dom Bool
unsafeFromReset

infixr 2 .||.
-- | The above type is a generalization for:
--
-- @
-- __(.||.)__ :: 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' 'Bool'
-- @
--
-- It is a version of ('||') that returns a 'Clash.Signal.Signal' of 'Bool'
(.||.) :: Applicative f => f Bool -> f Bool -> f Bool
.||. :: f Bool -> f Bool -> f Bool
(.||.) = (Bool -> Bool -> Bool) -> f Bool -> f Bool -> f Bool
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Bool -> Bool -> Bool
(||)

infixr 3 .&&.
-- | The above type is a generalization for:
--
-- @
-- __(.&&.)__ :: 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' 'Bool'
-- @
--
-- It is a version of ('&&') that returns a 'Clash.Signal.Signal' of 'Bool'
(.&&.) :: Applicative f => f Bool -> f Bool -> f Bool
.&&. :: f Bool -> f Bool -> f Bool
(.&&.) = (Bool -> Bool -> Bool) -> f Bool -> f Bool -> f Bool
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Bool -> Bool -> Bool
(&&)

-- [Note: register strictness annotations]
--
-- In order to produce the first (current) value of the register's output
-- signal, 'o', we don't need to know the shape of either input (enable or
-- value-in).  This is important, because both values might be produced from
-- the output in a feedback loop, so we can't know their shape (pattern
-- match) them until we have produced output.
--
-- Thus, we use lazy pattern matching to delay inspecting the shape of
-- either argument until output has been produced.
--
-- However, both arguments need to be evaluated to WHNF as soon as possible
-- to avoid a space-leak.  Below, we explicitly reduce the value-in signal
-- using 'seq' as the tail of our output signal is produced.  On the other
-- hand, because the value of the tail depends on the value of the enable
-- signal 'e', it will be forced by the 'if'/'then' statement and we don't
-- need to 'seq' it explicitly.

delay#
  :: forall dom a
   . ( KnownDomain dom
     , NFDataX a )
  => Clock dom
  -> Enable dom
  -> a
  -> Signal dom a
  -> Signal dom a
delay# :: Clock dom -> Enable dom -> a -> Signal dom a -> Signal dom a
delay# (Clock SSymbol dom
dom) (Enable dom -> Signal dom Bool
forall (dom :: Domain). Enable dom -> Signal dom Bool
fromEnable -> Signal dom Bool
en) a
powerUpVal0 =
    a -> Signal dom Bool -> Signal dom a -> Signal dom a
forall t (dom :: Domain) (dom :: Domain) (dom :: Domain).
NFDataX t =>
t -> Signal dom Bool -> Signal dom t -> Signal dom t
go a
powerUpVal1 Signal dom Bool
en
  where
    powerUpVal1 :: a
    powerUpVal1 :: a
powerUpVal1 =
      case SSymbol dom -> SDomainConfiguration dom (KnownConf dom)
forall (dom :: Domain).
KnownDomain dom =>
SSymbol dom -> SDomainConfiguration dom (KnownConf dom)
knownDomainByName SSymbol dom
dom of
        SDomainConfiguration SSymbol dom
_dom SNat period
_period SActiveEdge edge
_edge SResetKind reset
_sync SInitBehavior init
SDefined SResetPolarity polarity
_polarity ->
          a
powerUpVal0
        SDomainConfiguration SSymbol dom
_dom SNat period
_period SActiveEdge edge
_edge SResetKind reset
_sync SInitBehavior init
SUnknown SResetPolarity polarity
_polarity ->
          String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX (String
"First value of `delay` unknown on domain " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SSymbol dom -> String
forall a. Show a => a -> String
show SSymbol dom
dom)

    go :: t -> Signal dom Bool -> Signal dom t -> Signal dom t
go t
o (Bool
e :- Signal dom Bool
es) as :: Signal dom t
as@(~(t
x :- Signal dom t
xs)) =
      let o' :: t
o' = if Bool
e then t
x else t
o
      -- See [Note: register strictness annotations]
      in  t
o t -> Signal dom t -> Signal dom t
forall a b. NFDataX a => a -> b -> b
`defaultSeqX` t
o t -> Signal dom t -> Signal dom t
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- (Signal dom t
as Signal dom t -> Signal dom t -> Signal dom t
`seq` t -> Signal dom Bool -> Signal dom t -> Signal dom t
go t
o' Signal dom Bool
es Signal dom t
xs)
{-# NOINLINE delay# #-}
{-# ANN delay# hasBlackBox #-}

-- | A register with a power up and reset value. Power up values are not
-- supported on all platforms, please consult the manual of your target platform
-- and check the notes below.
--
-- Xilinx: power up values and reset values MUST be the same. If they are not,
-- the Xilinx tooling __will ignore the reset value__ and use the power up value
-- instead. Source: MIA
--
-- Intel: power up values and reset values MUST be the same. If they are not,
-- the Intel tooling __will ignore the power up value__ and use the reset value
-- instead. Source: https://www.intel.com/content/www/us/en/programmable/support/support-resources/knowledge-base/solutions/rd01072011_91.html
register#
  :: forall dom  a
   . ( KnownDomain dom
     , NFDataX a )
  => Clock dom
  -> Reset dom
  -> Enable dom
  -> a
  -- ^ Power up value
  -> a
  -- ^ Reset value
  -> Signal dom a
  -> Signal dom a
register# :: Clock dom
-> Reset dom
-> Enable dom
-> a
-> a
-> Signal dom a
-> Signal dom a
register# clk :: Clock dom
clk@(Clock SSymbol dom
dom) Reset dom
rst Enable dom
ena a
powerUpVal a
resetVal =
  case SSymbol dom -> SDomainConfiguration dom (KnownConf dom)
forall (dom :: Domain).
KnownDomain dom =>
SSymbol dom -> SDomainConfiguration dom (KnownConf dom)
knownDomainByName SSymbol dom
dom of
    SDomainConfiguration SSymbol dom
_name SNat period
_period SActiveEdge edge
_edge SResetKind reset
SSynchronous SInitBehavior init
_init SResetPolarity polarity
_polarity ->
      Clock dom
-> Reset dom
-> Enable dom
-> a
-> a
-> Signal dom a
-> Signal dom a
forall (dom :: Domain) a.
(KnownDomain dom, NFDataX a) =>
Clock dom
-> Reset dom
-> Enable dom
-> a
-> a
-> Signal dom a
-> Signal dom a
syncRegister# Clock dom
clk Reset dom
rst Enable dom
ena a
powerUpVal a
resetVal
    SDomainConfiguration SSymbol dom
_name SNat period
_period SActiveEdge edge
_edge SResetKind reset
SAsynchronous SInitBehavior init
_init SResetPolarity polarity
_polarity ->
      Clock dom
-> Reset dom
-> Enable dom
-> a
-> a
-> Signal dom a
-> Signal dom a
forall (dom :: Domain) a.
(KnownDomain dom, NFDataX a) =>
Clock dom
-> Reset dom
-> Enable dom
-> a
-> a
-> Signal dom a
-> Signal dom a
asyncRegister# Clock dom
clk Reset dom
rst Enable dom
ena a
powerUpVal a
resetVal
{-# NOINLINE register# #-}
{-# ANN register# hasBlackBox #-}

-- | Acts like 'id' if given domain allows powerup values, but returns a
-- value constructed with 'deepErrorX' otherwise.
registerPowerup#
  :: forall dom a
   . ( KnownDomain dom
     , NFDataX a
     , HasCallStack )
  => Clock dom
  -> a
  -> a
registerPowerup# :: Clock dom -> a -> a
registerPowerup# (Clock SSymbol dom
dom) a
a =
  case SSymbol dom -> SDomainConfiguration dom (KnownConf dom)
forall (dom :: Domain).
KnownDomain dom =>
SSymbol dom -> SDomainConfiguration dom (KnownConf dom)
knownDomainByName SSymbol dom
dom of
    SDomainConfiguration SSymbol dom
_dom SNat period
_period SActiveEdge edge
_edge SResetKind reset
_sync SInitBehavior init
SDefined SResetPolarity polarity
_polarity -> a
a
    SDomainConfiguration SSymbol dom
_dom SNat period
_period SActiveEdge edge
_edge SResetKind reset
_sync SInitBehavior init
SUnknown SResetPolarity polarity
_polarity ->
      String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX (String
"First value of register undefined on domain " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SSymbol dom -> String
forall a. Show a => a -> String
show SSymbol dom
dom)

-- | Version of 'register#' that simulates a register on an asynchronous
-- domain. Is synthesizable.
asyncRegister#
  :: forall dom  a
   . ( KnownDomain dom
     , NFDataX a )
  => Clock dom
  -- ^ Clock signal
  -> Reset dom
  -- ^ Reset signal
  -> Enable dom
  -- ^ Enable signal
  -> a
  -- ^ Power up value
  -> a
  -- ^ Reset value
  -> Signal dom a
  -> Signal dom a
asyncRegister# :: Clock dom
-> Reset dom
-> Enable dom
-> a
-> a
-> Signal dom a
-> Signal dom a
asyncRegister# Clock dom
clk (Reset dom -> Signal dom Bool
forall (dom :: Domain).
KnownDomain dom =>
Reset dom -> Signal dom Bool
unsafeToHighPolarity -> Signal dom Bool
rst) (Enable dom -> Signal dom Bool
forall (dom :: Domain). Enable dom -> Signal dom Bool
fromEnable -> Signal dom Bool
ena) a
initVal a
resetVal =
  a
-> Signal dom Bool
-> Signal dom Bool
-> Signal dom a
-> Signal dom a
go (Clock dom -> a -> a
forall (dom :: Domain) a.
(KnownDomain dom, NFDataX a, HasCallStack) =>
Clock dom -> a -> a
registerPowerup# Clock dom
clk a
initVal) Signal dom Bool
rst Signal dom Bool
ena
 where
  go :: a
-> Signal dom Bool
-> Signal dom Bool
-> Signal dom a
-> Signal dom a
go a
o (Bool
r :- Signal dom Bool
rs) enas :: Signal dom Bool
enas@(~(Bool
e :- Signal dom Bool
es)) as :: Signal dom a
as@(~(a
x :- Signal dom a
xs)) =
    let oR :: a
oR = if Bool
r then a
resetVal else a
o
        oE :: a
oE = if Bool
r then a
resetVal else (if Bool
e then a
x else a
o)
        -- [Note: register strictness annotations]
    in  a
oR a -> Signal dom a -> Signal dom a
forall a b. NFDataX a => a -> b -> b
`defaultSeqX` a
oR a -> Signal dom a -> Signal dom a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- (Signal dom a
as Signal dom a -> Signal dom a -> Signal dom a
`seq` Signal dom Bool
enas Signal dom Bool -> Signal dom a -> Signal dom a
`seq` a
-> Signal dom Bool
-> Signal dom Bool
-> Signal dom a
-> Signal dom a
go a
oE Signal dom Bool
rs Signal dom Bool
es Signal dom a
xs)
{-# NOINLINE asyncRegister# #-}
{-# ANN asyncRegister# hasBlackBox #-}

-- | Version of 'register#' that simulates a register on a synchronous
-- domain. Not synthesizable.
syncRegister#
  :: forall dom  a
   . ( KnownDomain dom
     , NFDataX a )
  => Clock dom
  -- ^ Clock signal
  -> Reset dom
  -- ^ Reset signal
  -> Enable dom
  -- ^ Enable signal
  -> a
  -- ^ Power up value
  -> a
  -- ^ Reset value
  -> Signal dom a
  -> Signal dom a
syncRegister# :: Clock dom
-> Reset dom
-> Enable dom
-> a
-> a
-> Signal dom a
-> Signal dom a
syncRegister# Clock dom
clk (Reset dom -> Signal dom Bool
forall (dom :: Domain).
KnownDomain dom =>
Reset dom -> Signal dom Bool
unsafeToHighPolarity -> Signal dom Bool
rst) (Enable dom -> Signal dom Bool
forall (dom :: Domain). Enable dom -> Signal dom Bool
fromEnable -> Signal dom Bool
ena) a
initVal a
resetVal =
  a
-> Signal dom Bool
-> Signal dom Bool
-> Signal dom a
-> Signal dom a
go (Clock dom -> a -> a
forall (dom :: Domain) a.
(KnownDomain dom, NFDataX a, HasCallStack) =>
Clock dom -> a -> a
registerPowerup# Clock dom
clk a
initVal) Signal dom Bool
rst Signal dom Bool
ena
 where
  go :: a
-> Signal dom Bool
-> Signal dom Bool
-> Signal dom a
-> Signal dom a
go a
o rt :: Signal dom Bool
rt@(~(Bool
r :- Signal dom Bool
rs)) enas :: Signal dom Bool
enas@(~(Bool
e :- Signal dom Bool
es)) as :: Signal dom a
as@(~(a
x :- Signal dom a
xs)) =
    let oE :: a
oE = if Bool
e then a
x else a
o
        oR :: a
oR = if Bool
r then a
resetVal else a
oE
        -- [Note: register strictness annotations]
    in  a
o a -> Signal dom a -> Signal dom a
forall a b. NFDataX a => a -> b -> b
`defaultSeqX` a
o a -> Signal dom a -> Signal dom a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- (Signal dom Bool
rt Signal dom Bool -> Signal dom a -> Signal dom a
`seq` Signal dom Bool
enas Signal dom Bool -> Signal dom a -> Signal dom a
`seq` Signal dom a
as Signal dom a -> Signal dom a -> Signal dom a
`seq` a
-> Signal dom Bool
-> Signal dom Bool
-> Signal dom a
-> Signal dom a
go a
oR Signal dom Bool
rs Signal dom Bool
es Signal dom a
xs)
{-# NOINLINE syncRegister# #-}
{-# ANN syncRegister# dontTranslate #-}

-- | The above type is a generalization for:
--
-- @
-- __mux__ :: 'Clash.Signal.Signal' 'Bool' -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' a
-- @
--
-- A multiplexer. Given "@'mux' b t f@", output @t@ when @b@ is 'True', and @f@
-- when @b@ is 'False'.
mux :: Applicative f => f Bool -> f a -> f a -> f a
mux :: f Bool -> f a -> f a -> f a
mux = (Bool -> a -> a -> a) -> f Bool -> f a -> f a -> f a
forall (f :: Type -> Type) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 (\Bool
b a
t a
f -> if Bool
b then a
t else a
f)
{-# INLINE mux #-}

infix 4 .==.
-- | The above type is a generalization for:
--
-- @
-- __(.==.)__ :: 'Eq' a => 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
-- @
--
-- It is a version of ('==') that returns a 'Clash.Signal.Signal' of 'Bool'
(.==.) :: (Eq a, Applicative f) => f a -> f a -> f Bool
.==. :: f a -> f a -> f Bool
(.==.) = (a -> a -> Bool) -> f a -> f a -> f Bool
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)

infix 4 ./=.
-- | The above type is a generalization for:
--
-- @
-- __(./=.)__ :: 'Eq' a => 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
-- @
--
-- It is a version of ('/=') that returns a 'Clash.Signal.Signal' of 'Bool'
(./=.) :: (Eq a, Applicative f) => f a -> f a -> f Bool
./=. :: f a -> f a -> f Bool
(./=.) = (a -> a -> Bool) -> f a -> f a -> f Bool
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(/=)

infix 4 .<.
-- | The above type is a generalization for:
--
-- @
-- __(.<.)__ :: 'Ord' a => 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
-- @
--
-- It is a version of ('<') that returns a 'Clash.Signal.Signal' of 'Bool'
(.<.) :: (Ord a, Applicative f) => f a -> f a -> f Bool
.<. :: f a -> f a -> f Bool
(.<.) = (a -> a -> Bool) -> f a -> f a -> f Bool
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
(<)

infix 4 .<=.
-- | The above type is a generalization for:
--
-- @
-- __(.<=.)__ :: 'Ord' a => 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
-- @
--
-- It is a version of ('<=') that returns a 'Clash.Signal.Signal' of 'Bool'
(.<=.) :: (Ord a, Applicative f) => f a -> f a -> f Bool
.<=. :: f a -> f a -> f Bool
(.<=.) = (a -> a -> Bool) -> f a -> f a -> f Bool
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
(<=)

infix 4 .>.
-- | The above type is a generalization for:
--
-- @
-- __(.>.)__ :: 'Ord' a => 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
-- @
--
-- It is a version of ('>') that returns a 'Clash.Signal.Signal' of 'Bool'
(.>.) :: (Ord a, Applicative f) => f a -> f a -> f Bool
.>. :: f a -> f a -> f Bool
(.>.) = (a -> a -> Bool) -> f a -> f a -> f Bool
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
(>)

infix 4 .>=.
-- | The above type is a generalization for:
--
-- @
-- __(.>=.)__ :: 'Ord' a => 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' 'Bool'
-- @
--
--  It is a version of ('>=') that returns a 'Clash.Signal.Signal' of 'Bool'
(.>=.) :: (Ord a, Applicative f) => f a -> f a -> f Bool
.>=. :: f a -> f a -> f Bool
(.>=.) = (a -> a -> Bool) -> f a -> f a -> f Bool
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
(>=)

instance Fractional a => Fractional (Signal dom a) where
  / :: Signal dom a -> Signal dom a -> Signal dom a
(/)          = (a -> a -> a) -> Signal dom a -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Fractional a => a -> a -> a
(/)
  recip :: Signal dom a -> Signal dom a
recip        = (a -> a) -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Fractional a => a -> a
recip
  fromRational :: Rational -> Signal dom a
fromRational = a -> Signal dom a
forall a (dom :: Domain). a -> Signal dom a
signal# (a -> Signal dom a) -> (Rational -> a) -> Rational -> Signal dom a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> a
forall a. Fractional a => Rational -> a
fromRational

instance Arbitrary a => Arbitrary (Signal dom a) where
  arbitrary :: Gen (Signal dom a)
arbitrary = (a -> Signal dom a -> Signal dom a)
-> Gen a -> Gen (Signal dom a) -> Gen (Signal dom a)
forall (f :: Type -> Type) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> Signal dom a -> Signal dom a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
(:-) Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen (Signal dom a)
forall a. Arbitrary a => Gen a
arbitrary

instance CoArbitrary a => CoArbitrary (Signal dom a) where
  coarbitrary :: Signal dom a -> Gen b -> Gen b
coarbitrary Signal dom a
xs Gen b
gen = do
    Int
n <- Gen Int
forall a. Arbitrary a => Gen a
arbitrary
    [a] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take (Int -> Int
forall a. Num a => a -> a
abs Int
n) (Signal dom a -> [a]
forall (f :: Type -> Type) a. Foldable f => f a -> [a]
sample_lazy Signal dom a
xs)) Gen b
gen

-- | The above type is a generalization for:
--
-- @
-- __testFor__ :: 'Int' -> 'Clash.Signal.Signal' Bool -> 'Property'
-- @
--
-- @testFor n s@ tests the signal @s@ for @n@ cycles.
--
-- __NB__: This function is not synthesizable
testFor :: Foldable f => Int -> f Bool -> Property
testFor :: Int -> f Bool -> Property
testFor Int
n = Bool -> Property
forall prop. Testable prop => prop -> Property
property (Bool -> Property) -> (f Bool -> Bool) -> f Bool -> Property
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Bool] -> Bool
forall (t :: Type -> Type). Foldable t => t Bool -> Bool
and ([Bool] -> Bool) -> (f Bool -> [Bool]) -> f Bool -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Bool] -> [Bool]
forall a. Int -> [a] -> [a]
take Int
n ([Bool] -> [Bool]) -> (f Bool -> [Bool]) -> f Bool -> [Bool]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f Bool -> [Bool]
forall (f :: Type -> Type) a. (Foldable f, NFDataX a) => f a -> [a]
sample

-- * List \<-\> Signal conversion (not synthesizable)

-- | The above type is a generalization for:
--
-- @
-- __sample__ :: 'Clash.Signal.Signal' a -> [a]
-- @
--
-- Get an infinite list of samples from a 'Clash.Signal.Signal'
--
-- The elements in the list correspond to the values of the 'Clash.Signal.Signal'
-- at consecutive clock cycles
--
-- > sample s == [s0, s1, s2, s3, ...
--
-- __NB__: This function is not synthesizable
sample :: (Foldable f, NFDataX a) => f a -> [a]
sample :: f a -> [a]
sample = (a -> [a] -> [a]) -> [a] -> f a -> [a]
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\a
a [a]
b -> a -> [a] -> [a]
forall a b. NFDataX a => a -> b -> b
deepseqX a
a (a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
b)) []

-- | The above type is a generalization for:
--
-- @
-- __sampleN__ :: Int -> 'Clash.Signal.Signal' a -> [a]
-- @
--
-- Get a list of @n@ samples from a 'Clash.Signal.Signal'
--
-- The elements in the list correspond to the values of the 'Clash.Signal.Signal'
-- at consecutive clock cycles
--
-- > sampleN 3 s == [s0, s1, s2]
--
-- __NB__: This function is not synthesizable
sampleN :: (Foldable f, NFDataX a) => Int -> f a -> [a]
sampleN :: Int -> f a -> [a]
sampleN Int
n = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n ([a] -> [a]) -> (f a -> [a]) -> f a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall (f :: Type -> Type) a. (Foldable f, NFDataX a) => f a -> [a]
sample

-- | Create a 'Clash.Signal.Signal' from a list
--
-- Every element in the list will correspond to a value of the signal for one
-- clock cycle.
--
-- >>> sampleN 2 (fromList [1,2,3,4,5])
-- [1,2]
--
-- __NB__: This function is not synthesizable
fromList :: NFDataX a => [a] -> Signal dom a
fromList :: [a] -> Signal dom a
fromList = (a -> Signal dom a -> Signal dom a)
-> Signal dom a -> [a] -> Signal dom a
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr (\a
a Signal dom a
b -> a -> Signal dom a -> Signal dom a
forall a b. NFDataX a => a -> b -> b
deepseqX a
a (a
a a -> Signal dom a -> Signal dom a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- Signal dom a
b)) (String -> Signal dom a
forall a. HasCallStack => String -> a
errorX String
"finite list")

-- * Simulation functions (not synthesizable)

-- | Simulate a (@'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' b@) function
-- given a list of samples of type @a@
--
-- >>> simulate (register systemClockGen resetGen enableGen 8) [1, 1, 2, 3]
-- [8,8,1,2,3...
-- ...
--
-- __NB__: This function is not synthesizable
simulate :: (NFDataX a, NFDataX b) => (Signal dom1 a -> Signal dom2 b) -> [a] -> [b]
simulate :: (Signal dom1 a -> Signal dom2 b) -> [a] -> [b]
simulate Signal dom1 a -> Signal dom2 b
f = Signal dom2 b -> [b]
forall (f :: Type -> Type) a. (Foldable f, NFDataX a) => f a -> [a]
sample (Signal dom2 b -> [b]) -> ([a] -> Signal dom2 b) -> [a] -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signal dom1 a -> Signal dom2 b
f (Signal dom1 a -> Signal dom2 b)
-> ([a] -> Signal dom1 a) -> [a] -> Signal dom2 b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Signal dom1 a
forall a (dom :: Domain). NFDataX a => [a] -> Signal dom a
fromList

-- | The above type is a generalization for:
--
-- @
-- __sample__ :: 'Clash.Signal.Signal' a -> [a]
-- @
--
-- Get an infinite list of samples from a 'Clash.Signal.Signal'
--
-- The elements in the list correspond to the values of the 'Clash.Signal.Signal'
-- at consecutive clock cycles
--
-- > sample s == [s0, s1, s2, s3, ...
--
-- __NB__: This function is not synthesizable
sample_lazy :: Foldable f => f a -> [a]
sample_lazy :: f a -> [a]
sample_lazy = (a -> [a] -> [a]) -> [a] -> f a -> [a]
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (:) []

-- | The above type is a generalization for:
--
-- @
-- __sampleN__ :: Int -> 'Clash.Signal.Signal' a -> [a]
-- @
--
-- Get a list of @n@ samples from a 'Clash.Signal.Signal'
--
-- The elements in the list correspond to the values of the 'Clash.Signal.Signal'
-- at consecutive clock cycles
--
-- > sampleN 3 s == [s0, s1, s2]
--
-- __NB__: This function is not synthesizable
sampleN_lazy :: Foldable f => Int -> f a -> [a]
sampleN_lazy :: Int -> f a -> [a]
sampleN_lazy Int
n = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n ([a] -> [a]) -> (f a -> [a]) -> f a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall (f :: Type -> Type) a. Foldable f => f a -> [a]
sample_lazy

-- | Create a 'Clash.Signal.Signal' from a list
--
-- Every element in the list will correspond to a value of the signal for one
-- clock cycle.
--
-- >>> sampleN 2 (fromList [1,2,3,4,5] :: Signal System Int)
-- [1,2]
--
-- __NB__: This function is not synthesizable
fromList_lazy :: [a] -> Signal dom a
fromList_lazy :: [a] -> Signal dom a
fromList_lazy = (a -> Signal dom a -> Signal dom a)
-> Signal dom a -> [a] -> Signal dom a
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr a -> Signal dom a -> Signal dom a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
(:-) (String -> Signal dom a
forall a. HasCallStack => String -> a
error String
"finite list")

-- * Simulation functions (not synthesizable)

-- | Simulate a (@'Clash.Signal.Signal' a -> 'Clash.Signal.Signal' b@) function
-- given a list of samples of type @a@
--
-- >>> simulate (register systemClockGen resetGen enableGen 8) [1, 1, 2, 3]
-- [8,8,1,2,3...
-- ...
--
-- __NB__: This function is not synthesizable
simulate_lazy :: (Signal dom1 a -> Signal dom2 b) -> [a] -> [b]
simulate_lazy :: (Signal dom1 a -> Signal dom2 b) -> [a] -> [b]
simulate_lazy Signal dom1 a -> Signal dom2 b
f = Signal dom2 b -> [b]
forall (f :: Type -> Type) a. Foldable f => f a -> [a]
sample_lazy (Signal dom2 b -> [b]) -> ([a] -> Signal dom2 b) -> [a] -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signal dom1 a -> Signal dom2 b
f (Signal dom1 a -> Signal dom2 b)
-> ([a] -> Signal dom1 a) -> [a] -> Signal dom2 b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Signal dom1 a
forall a (dom :: Domain). [a] -> Signal dom a
fromList_lazy

-- | Calculate the period, in __ps__, given a frequency in __Hz__
--
-- i.e. to calculate the clock period for a circuit to run at 240 MHz we get
--
-- >>> hzToPeriod 240e6
-- 4167
--
-- __NB__: This function is /not/ synthesizable
-- __NB__: This function is lossy. I.e., hzToPeriod . periodToHz /= id.
hzToPeriod :: HasCallStack => Double -> Natural
hzToPeriod :: Double -> Natural
hzToPeriod Double
freq | Double
freq Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0.0 = String -> Natural
forall a. HasCallStack => String -> a
error String
"Frequency must be strictly positive"
                | Bool
otherwise   = Double -> Natural
forall a b. (RealFrac a, Integral b) => a -> b
ceiling ((Double
1.0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
freq) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
1.0e-12)

-- | Calculate the frequence in __Hz__, given the period in __ps__
--
-- i.e. to calculate the clock frequency of a clock with a period of 5000 ps:
--
-- >>> periodToHz 5000
-- 2.0e8
--
-- __NB__: This function is /not/ synthesizable
-- __NB__: This function is lossy. I.e., hzToPeriod . periodToHz /= id.
periodToHz :: Natural -> Double
periodToHz :: Natural -> Double
periodToHz Natural
period = Double
1.0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
1.0e-12 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Natural -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Natural
period)