-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35cf9e9
commit c3233c7
Showing
5 changed files
with
180 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,36 @@ Maintainer : QBayLogic B.V. <[email protected]> | |
Generic clock related utilities. | ||
-} | ||
|
||
{-# LANGUAGE ConstraintKinds #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
|
||
{-# OPTIONS_GHC "-Wno-orphans" #-} | ||
|
||
module Clash.Clocks (Clocks(..)) where | ||
module Clash.Clocks | ||
( Clocks(..) | ||
, ClocksSync(..) | ||
, ClocksSyncCxt | ||
, NumOutClocksSync | ||
) where | ||
|
||
import Clash.Clocks.Internal (Clocks(..), deriveClocksInstances) | ||
import Clash.Clocks.Internal | ||
(Clocks(..), ClocksSync(..), deriveClocksInstances, deriveClocksSyncInstances) | ||
import Clash.Signal.Internal (Domain, KnownDomain) | ||
|
||
deriveClocksInstances | ||
|
||
type ClocksSyncCxt t (domIn :: Domain) = | ||
( KnownDomain domIn | ||
, ClocksSync t | ||
, ClocksResetSynchronizerCxt t | ||
, Clocks (ClocksSyncClocksInst t domIn) | ||
, ClocksCxt (ClocksSyncClocksInst t domIn) | ||
) | ||
|
||
type NumOutClocksSync t (domIn :: Domain) = | ||
NumOutClocks (ClocksSyncClocksInst t domIn) | ||
|
||
deriveClocksSyncInstances |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,18 +11,26 @@ Maintainer : QBayLogic B.V. <[email protected]> | |
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
|
||
module Clash.Clocks.Internal (Clocks(..), deriveClocksInstances) where | ||
module Clash.Clocks.Internal | ||
( Clocks(..) | ||
, deriveClocksInstances | ||
, ClocksSync(..) | ||
, deriveClocksSyncInstances | ||
) where | ||
|
||
import Control.Monad.Extra (concatMapM) | ||
import Data.Kind (Constraint) | ||
import Data.Kind (Constraint, Type) | ||
import GHC.TypeLits (Nat) | ||
import Language.Haskell.TH | ||
import Language.Haskell.TH hiding (Type) | ||
|
||
import Clash.CPP (haddockOnly) | ||
import Clash.Explicit.Reset (resetSynchronizer) | ||
import Clash.Explicit.Signal (unsafeSynchronizer) | ||
import Clash.Magic (setName) | ||
import Clash.Promoted.Symbol (SSymbol(..)) | ||
import Clash.Signal.Internal | ||
(clockGen, Clock(..), KnownDomain, Reset, Signal, unsafeToActiveLow) | ||
(clockGen, Clock(..), Domain, KnownDomain, Reset, Signal, unsafeFromActiveLow, | ||
unsafeToActiveLow) | ||
|
||
-- | __NB__: The documentation only shows instances up to /3/ output clocks. By | ||
-- default, instances up to and including /18/ clocks will exist. | ||
|
@@ -75,3 +83,77 @@ deriveClocksInstances = concatMapM deriveClocksInstance [1..n] | |
where | ||
n | haddockOnly = 3 | ||
| otherwise = 18 | ||
|
||
-- | __NB__: The documentation only shows instances up to /3/ output clocks. By | ||
-- default, instances up to and including /18/ clocks will exist. | ||
class ClocksSync t where | ||
type ClocksSyncClocksInst t (domIn :: Domain) :: Type | ||
type ClocksResetSynchronizerCxt t :: Constraint | ||
|
||
clocksResetSynchronizer :: | ||
( KnownDomain domIn | ||
, ClocksResetSynchronizerCxt t | ||
) => | ||
ClocksSyncClocksInst t domIn -> | ||
Clock domIn -> | ||
t | ||
|
||
-- Derive instance for /n/ clocks | ||
deriveClocksSyncInstance :: Int -> DecsQ | ||
deriveClocksSyncInstance n = | ||
[d| | ||
instance ClocksSync $instType where | ||
type ClocksSyncClocksInst $instType $domInTyVar = $clocksInstType | ||
type ClocksResetSynchronizerCxt $instType = $cxtType | ||
|
||
clocksResetSynchronizer pllOut $(varP clkIn) = | ||
let $pllPat = pllOut | ||
in $funcImpl | ||
|] | ||
where | ||
clkVarName m = mkName $ "c" <> show m | ||
clkTyVar :: Int -> TypeQ | ||
clkTyVar = varT . clkVarName | ||
clkAndRstTy m = [ [t| Clock $(clkTyVar m) |] | ||
, [t| Reset $(clkTyVar m) |] | ||
] | ||
-- (Clock c1, Reset c1, Clock c2, Reset c2, ...) | ||
instType = foldl appT (tupleT $ n * 2) $ concatMap clkAndRstTy [1..n] | ||
domInTyVar = varT $ mkName "domIn" | ||
clkTypes = map (\m -> [t| Clock $(clkTyVar m) |]) [1..n] | ||
-- (Clock c1, Clock c2, ..., Signal domIn Bool) | ||
clocksInstType = foldl appT (tupleT $ n + 1) $ | ||
clkTypes <> [ [t| Signal $domInTyVar Bool |] ] | ||
-- (KnownDomain c1, KnownDomain c2, ...) | ||
cxtType | ||
| n == 1 | ||
= [t| KnownDomain $(clkTyVar 1) |] | ||
| otherwise | ||
= foldl appT (tupleT n) $ | ||
map (\m -> [t| KnownDomain $(clkTyVar m) |]) [1..n] | ||
|
||
-- 'clocksResetSynchronizer' function | ||
clkIn = mkName "clkIn" | ||
pllLock = mkName "pllLock" | ||
-- (c1, c2, ..., pllLock) | ||
pllPat = tupP $ map (varP . clkVarName) [1..n] <> [varP pllLock] | ||
syncImpl m = | ||
[| | ||
setName @"resetSynchronizer" (resetSynchronizer $(varE $ clkVarName m) | ||
(unsafeFromActiveLow | ||
(unsafeSynchronizer $(varE clkIn) $(varE $ clkVarName m) | ||
$(varE pllLock)))) | ||
|] | ||
clkAndRstExp m = [ varE $ clkVarName m | ||
, syncImpl m | ||
] | ||
-- (c1, r1, c2, r2, ...) where rN is the synchronized reset for clock N | ||
funcImpl = tupE $ concatMap clkAndRstExp [1..n] | ||
|
||
-- Derive instances for up to and including 18 clocks, except when we are | ||
-- generating Haddock | ||
deriveClocksSyncInstances :: DecsQ | ||
deriveClocksSyncInstances = concatMapM deriveClocksSyncInstance [1..n] | ||
where | ||
n | haddockOnly = 3 | ||
| otherwise = 18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters