|
| 1 | +{-| |
| 2 | + Copyright : (C) 2023, Google LLC |
| 3 | + License : BSD2 (see the file LICENSE) |
| 4 | + Maintainer : QBayLogic B.V. <[email protected]> |
| 5 | +-} |
| 6 | +{-# LANGUAGE LambdaCase #-} |
| 7 | +{-# LANGUAGE NumericUnderscores #-} |
| 8 | +{-# LANGUAGE OverloadedStrings #-} |
| 9 | +{-# LANGUAGE QuasiQuotes #-} |
| 10 | +{-# LANGUAGE ViewPatterns #-} |
| 11 | + |
| 12 | +module Clash.Cores.Xilinx.Xpm.Cdc.Gray.Internal where |
| 13 | + |
| 14 | +import Prelude |
| 15 | +import Clash.Explicit.Prelude |
| 16 | + ( type (<=), KnownNat, SNat, Unsigned, Clock, KnownDomain, errorX |
| 17 | + , unsafeSynchronizer ) |
| 18 | + |
| 19 | +import Clash.Annotations.Primitive (Primitive(..), HDL(..), hasBlackBox) |
| 20 | +import Clash.Backend (Backend) |
| 21 | +import Clash.Netlist.Types (TemplateFunction(..), BlackBoxContext) |
| 22 | +import Clash.Promoted.Nat (snatToNum) |
| 23 | +import Clash.Signal.Internal (Signal((:-))) |
| 24 | + |
| 25 | +import Control.Monad.State (State) |
| 26 | +import Data.List.Infinite (Infinite(..), (...)) |
| 27 | +import Data.String.Interpolate (__i) |
| 28 | +import Data.Text (Text) |
| 29 | +import Data.Text.Prettyprint.Doc.Extra (Doc) |
| 30 | +import GHC.Stack (HasCallStack) |
| 31 | +import Text.Show.Pretty (ppShow) |
| 32 | + |
| 33 | +import qualified Clash.Netlist.Id as Id |
| 34 | +import qualified Clash.Netlist.Types as N |
| 35 | +import qualified Clash.Primitives.DSL as DSL |
| 36 | + |
| 37 | +xpmCdcGrayTF :: TemplateFunction |
| 38 | +xpmCdcGrayTF = |
| 39 | + TemplateFunction |
| 40 | + [initBehavior, stages, clkSrc, clkDst, input] |
| 41 | + (const True) |
| 42 | + xpmCdcGrayTF# |
| 43 | + where |
| 44 | + _2LteN |
| 45 | + :< _nLte32 |
| 46 | + :< _2LteStages |
| 47 | + :< _stagesLte10 |
| 48 | + :< _knownNatN |
| 49 | + :< _knownDomainSrc |
| 50 | + :< _knownDomainDst |
| 51 | + :< _hasCallStack |
| 52 | + :< initBehavior |
| 53 | + :< stages |
| 54 | + :< clkSrc |
| 55 | + :< clkDst |
| 56 | + :< input |
| 57 | + :< _ = (0...) |
| 58 | + |
| 59 | +xpmCdcGrayTF# :: Backend backend => BlackBoxContext -> State backend Doc |
| 60 | +xpmCdcGrayTF# bbCtx |
| 61 | + | [ _2LteN |
| 62 | + , _nLte32 |
| 63 | + , _2LteStages |
| 64 | + , _stagesLte10 |
| 65 | + , _knownNatN |
| 66 | + , _knownDomainSrc |
| 67 | + , _knownDomainDst |
| 68 | + , _hasCallStack |
| 69 | + , DSL.getBool -> Just initValues |
| 70 | + , DSL.tExprToInteger -> Just stages |
| 71 | + , clkSrc |
| 72 | + , clkDst |
| 73 | + , input |
| 74 | + ] <- map fst (DSL.tInputs bbCtx) |
| 75 | + , [resultTy] <- map DSL.ety (DSL.tResults bbCtx) |
| 76 | + = do |
| 77 | + |
| 78 | + let |
| 79 | + compName :: Text |
| 80 | + compName = "xpm_cdc_gray" |
| 81 | + |
| 82 | + width :: Integral a => a |
| 83 | + width = DSL.tySize resultTy |
| 84 | + |
| 85 | + instName <- Id.make (compName <> "_inst") |
| 86 | + DSL.declarationReturn bbCtx (compName <> "_block") $ do |
| 87 | + inputBv <- DSL.toBV "src_in_bin_bv" input |
| 88 | + resultBv <- DSL.declare "dest_out_bin_bv" (N.BitVector width) |
| 89 | + result <- DSL.fromBV "dest_out_bin" resultTy resultBv |
| 90 | + |
| 91 | + let |
| 92 | + generics :: [(Text, DSL.LitHDL)] |
| 93 | + generics = |
| 94 | + [ ("DEST_SYNC_FF", DSL.I stages) |
| 95 | + , ("INIT_SYNC_FF", if initValues then 1 else 0) |
| 96 | + , ("REG_OUTPUT", 0) |
| 97 | + , ("SIM_ASSERT_CHK", 0) |
| 98 | + , ("SIM_LOSSLESS_GRAY_CHK", 0) |
| 99 | + , ("WIDTH", DSL.I width) |
| 100 | + ] |
| 101 | + |
| 102 | + inps :: [(Text, DSL.TExpr)] |
| 103 | + inps = |
| 104 | + [ ("src_clk", clkSrc) |
| 105 | + , ("dest_clk", clkDst) |
| 106 | + , ("src_in_bin", inputBv) |
| 107 | + ] |
| 108 | + |
| 109 | + outs :: [(Text, DSL.TExpr)] |
| 110 | + outs = |
| 111 | + [ ("dest_out_bin", resultBv) |
| 112 | + ] |
| 113 | + |
| 114 | + DSL.instDecl |
| 115 | + N.Empty |
| 116 | + (Id.unsafeMake compName) |
| 117 | + instName |
| 118 | + generics |
| 119 | + inps |
| 120 | + outs |
| 121 | + |
| 122 | + pure [result] |
| 123 | + |
| 124 | +xpmCdcGrayTF# bbCtx = error (ppShow bbCtx) |
| 125 | + |
| 126 | +{-# NOINLINE xpmCdcGray# #-} |
| 127 | +{-# ANN xpmCdcGray# hasBlackBox #-} |
| 128 | +{-# ANN xpmCdcGray# |
| 129 | + let |
| 130 | + primName = show 'xpmCdcGray# |
| 131 | + tfName = show 'xpmCdcGrayTF |
| 132 | + in InlineYamlPrimitive [VHDL] [__i| |
| 133 | + BlackBox: |
| 134 | + name: #{primName} |
| 135 | + kind: Declaration |
| 136 | + format: Haskell |
| 137 | + libraries: ["xpm"] |
| 138 | + imports: ["xpm.vcomponents.all"] |
| 139 | + templateFunction: #{tfName} |
| 140 | + |] #-} |
| 141 | +{-# ANN xpmCdcGray# |
| 142 | + let |
| 143 | + primName = show 'xpmCdcGray# |
| 144 | + tfName = show 'xpmCdcGrayTF |
| 145 | + in InlineYamlPrimitive [Verilog, SystemVerilog] [__i| |
| 146 | + BlackBox: |
| 147 | + name: #{primName} |
| 148 | + kind: Declaration |
| 149 | + format: Haskell |
| 150 | + templateFunction: #{tfName} |
| 151 | + |] #-} |
| 152 | +-- | Primitive used in 'Clash.Cores.Xilinx.Xpm.Cdc.Gray.xpmCdcGray' |
| 153 | +xpmCdcGray# :: |
| 154 | + forall stages n src dst. |
| 155 | + ( 2 <= n, n <= 32 |
| 156 | + , 2 <= stages, stages <= 10 |
| 157 | + , KnownNat n |
| 158 | + , KnownDomain src |
| 159 | + , KnownDomain dst |
| 160 | + , HasCallStack |
| 161 | + ) => |
| 162 | + -- | Initial values supported |
| 163 | + Bool -> |
| 164 | + SNat stages -> |
| 165 | + Clock src -> |
| 166 | + Clock dst -> |
| 167 | + Signal src (Unsigned n) -> |
| 168 | + Signal dst (Unsigned n) |
| 169 | +xpmCdcGray# initValuesSupported stages clkSrc clkDst input = |
| 170 | + go (snatToNum stages) (initVal :- input) |
| 171 | + where |
| 172 | + initVal |
| 173 | + | initValuesSupported = 0 |
| 174 | + | otherwise = errorX "xpmCdcGray: initial values undefined" |
| 175 | + |
| 176 | + go :: Word -> Signal src (Unsigned n) -> Signal dst (Unsigned n) |
| 177 | + go 0 src = unsafeSynchronizer clkSrc clkDst src |
| 178 | + go n src = initVal :- go (n - 1) src |
0 commit comments