{-|
  Copyright   :  (C) 2018, Google Inc.
  License     :  BSD2 (see the file LICENSE)
  Maintainer  :  Christiaan Baaij <christiaan.baaij@gmail.com>

  Blackbox template functions for Clash.Intel.ClockGen.{alteraPll,altpll}
-}

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE ViewPatterns      #-}

module Clash.Primitives.Intel.ClockGen where

import Clash.Backend
import Clash.Netlist.BlackBox.Util
import Clash.Netlist.Id
import Clash.Netlist.Types
import Clash.Netlist.Util hiding (mkUniqueIdentifier)

import Control.Monad.State

import Data.Semigroup.Monad
import qualified Data.String.Interpolate.IsString as I
import Data.Text.Prettyprint.Doc.Extra

import qualified Data.Text as TextS

altpllTF :: TemplateFunction
altpllTF :: TemplateFunction
altpllTF = [Int]
-> (BlackBoxContext -> Bool)
-> (forall s. Backend s => BlackBoxContext -> State s Doc)
-> TemplateFunction
TemplateFunction [Int]
used BlackBoxContext -> Bool
valid forall s. Backend s => BlackBoxContext -> State s Doc
altpllTemplate
 where
  used :: [Int]
used         = [Int
0..Int
4]
  valid :: BlackBoxContext -> Bool
valid BlackBoxContext
bbCtx
    | [(Expr, HWType, Bool)
_,(Expr, HWType, Bool)
_,(Expr
nm,HWType
_,Bool
_),(Expr, HWType, Bool)
_,(Expr, HWType, Bool)
_] <- BlackBoxContext -> [(Expr, HWType, Bool)]
bbInputs BlackBoxContext
bbCtx
    , Just String
_ <- Expr -> Maybe String
exprToString Expr
nm
    , (Identifier Identifier
_ Maybe Modifier
Nothing,Product {}) <- BlackBoxContext -> (Expr, HWType)
bbResult BlackBoxContext
bbCtx
    = Bool
True
  valid BlackBoxContext
_ = Bool
False

altpllQsysTF :: TemplateFunction
altpllQsysTF :: TemplateFunction
altpllQsysTF = [Int]
-> (BlackBoxContext -> Bool)
-> (forall s. Backend s => BlackBoxContext -> State s Doc)
-> TemplateFunction
TemplateFunction [Int]
used BlackBoxContext -> Bool
valid forall s. Backend s => BlackBoxContext -> State s Doc
altpllQsysTemplate
 where
  used :: [Int]
used = [Int
0..Int
4]
  valid :: BlackBoxContext -> Bool
valid BlackBoxContext
bbCtx
    | [(Expr, HWType, Bool)
_,(Expr, HWType, Bool)
_,(Expr
nm,HWType
_,Bool
_),(Expr, HWType, Bool)
_,(Expr, HWType, Bool)
_] <- BlackBoxContext -> [(Expr, HWType, Bool)]
bbInputs BlackBoxContext
bbCtx
    , Just String
_ <- Expr -> Maybe String
exprToString Expr
nm
    , (Identifier Identifier
_ Maybe Modifier
Nothing,Product {}) <- BlackBoxContext -> (Expr, HWType)
bbResult BlackBoxContext
bbCtx
    = Bool
True
  valid BlackBoxContext
_ = Bool
False

alteraPllTF :: TemplateFunction
alteraPllTF :: TemplateFunction
alteraPllTF = [Int]
-> (BlackBoxContext -> Bool)
-> (forall s. Backend s => BlackBoxContext -> State s Doc)
-> TemplateFunction
TemplateFunction [Int]
used BlackBoxContext -> Bool
valid forall s. Backend s => BlackBoxContext -> State s Doc
alteraPllTemplate
 where
  used :: [Int]
used         = [Int
1..Int
20]
  valid :: BlackBoxContext -> Bool
valid BlackBoxContext
bbCtx
    | ((Expr
nm,HWType
_,Bool
_):[(Expr, HWType, Bool)]
_) <- Int -> [(Expr, HWType, Bool)] -> [(Expr, HWType, Bool)]
forall a. Int -> [a] -> [a]
drop Int
3 (BlackBoxContext -> [(Expr, HWType, Bool)]
bbInputs BlackBoxContext
bbCtx)
    , Just String
_ <- Expr -> Maybe String
exprToString Expr
nm
    = Bool
True
  valid BlackBoxContext
_ = Bool
False

alteraPllQsysTF :: TemplateFunction
alteraPllQsysTF :: TemplateFunction
alteraPllQsysTF = [Int]
-> (BlackBoxContext -> Bool)
-> (forall s. Backend s => BlackBoxContext -> State s Doc)
-> TemplateFunction
TemplateFunction [Int]
used BlackBoxContext -> Bool
valid forall s. Backend s => BlackBoxContext -> State s Doc
alteraPllQsysTemplate
 where
  used :: [Int]
used  = [Int
1..Int
20]
  valid :: BlackBoxContext -> Bool
valid BlackBoxContext
bbCtx
    | ((Expr
nm,HWType
_,Bool
_):[(Expr, HWType, Bool)]
_) <- Int -> [(Expr, HWType, Bool)] -> [(Expr, HWType, Bool)]
forall a. Int -> [a] -> [a]
drop Int
3 (BlackBoxContext -> [(Expr, HWType, Bool)]
bbInputs BlackBoxContext
bbCtx)
    , Just String
_ <- Expr -> Maybe String
exprToString Expr
nm
    = Bool
True
  valid BlackBoxContext
_ = Bool
False

alteraPllTemplate
  :: Backend s
  => BlackBoxContext
  -> State s Doc
alteraPllTemplate :: BlackBoxContext -> State s Doc
alteraPllTemplate BlackBoxContext
bbCtx = do
 let mkId :: Identifier -> State s Identifier
mkId = IdType -> Identifier -> State s Identifier
forall s. Backend s => IdType -> Identifier -> State s Identifier
mkUniqueIdentifier IdType
Basic
 Identifier
locked <- Identifier -> State s Identifier
mkId Identifier
"locked"
 Identifier
pllLock <- Identifier -> State s Identifier
mkId Identifier
"pllLock"
 Identifier
alteraPll <- Identifier -> State s Identifier
mkId Identifier
"altera_pll_block"
 Identifier
alteraPll_inst <- Identifier -> State s Identifier
mkId Identifier
instname0

 [Identifier]
clocks <- (Identifier -> State s Identifier)
-> [Identifier] -> StateT s Identity [Identifier]
forall (t :: Type -> Type) (f :: Type -> Type) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (IdType -> Identifier -> State s Identifier
forall s. Backend s => IdType -> Identifier -> State s Identifier
mkUniqueIdentifier IdType
Extended)
                    [String -> Identifier
TextS.pack (String
"pllOut" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n) | Int
n <- [Int
0..[HWType] -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [HWType]
tys Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]]
 Mon (State s) Doc -> State s Doc
forall (f :: Type -> Type) m. Mon f m -> f m
getMon (Mon (State s) Doc -> State s Doc)
-> Mon (State s) Doc -> State s Doc
forall a b. (a -> b) -> a -> b
$ Identifier -> [Declaration] -> Mon (State s) Doc
forall state.
Backend state =>
Identifier -> [Declaration] -> Mon (State state) Doc
blockDecl Identifier
alteraPll ([Declaration] -> Mon (State s) Doc)
-> [Declaration] -> Mon (State s) Doc
forall a b. (a -> b) -> a -> b
$ [[Declaration]] -> [Declaration]
forall (t :: Type -> Type) a. Foldable t => t [a] -> [a]
concat
  [[ Maybe Identifier -> Identifier -> HWType -> Declaration
NetDecl Maybe Identifier
forall a. Maybe a
Nothing Identifier
locked  HWType
rstTy
   , Maybe Identifier
-> WireOrReg
-> Identifier
-> Either Identifier HWType
-> Maybe Expr
-> Declaration
NetDecl' Maybe Identifier
forall a. Maybe a
Nothing WireOrReg
Reg Identifier
pllLock (HWType -> Either Identifier HWType
forall a b. b -> Either a b
Right HWType
Bool) Maybe Expr
forall a. Maybe a
Nothing]
  ,[ Maybe Identifier -> Identifier -> HWType -> Declaration
NetDecl Maybe Identifier
forall a. Maybe a
Nothing Identifier
clkNm HWType
ty | (Identifier
clkNm,HWType
ty) <- [Identifier] -> [HWType] -> [(Identifier, HWType)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Identifier]
clocks [HWType]
tys]
  ,[ EntityOrComponent
-> Maybe Identifier
-> Identifier
-> Identifier
-> [(Expr, HWType, Expr)]
-> [(Expr, PortDirection, HWType, Expr)]
-> Declaration
InstDecl EntityOrComponent
Comp Maybe Identifier
forall a. Maybe a
Nothing Identifier
compName Identifier
alteraPll_inst [] ([(Expr, PortDirection, HWType, Expr)] -> Declaration)
-> [(Expr, PortDirection, HWType, Expr)] -> Declaration
forall a b. (a -> b) -> a -> b
$ [[(Expr, PortDirection, HWType, Expr)]]
-> [(Expr, PortDirection, HWType, Expr)]
forall (t :: Type -> Type) a. Foldable t => t [a] -> [a]
concat
      [[(Identifier -> Maybe Modifier -> Expr
Identifier Identifier
"refclk" Maybe Modifier
forall a. Maybe a
Nothing,PortDirection
In,HWType
clkTy,Expr
clk)
       ,(Identifier -> Maybe Modifier -> Expr
Identifier Identifier
"rst" Maybe Modifier
forall a. Maybe a
Nothing,PortDirection
In,HWType
rstTy,Expr
rst)]
      ,[(Identifier -> Maybe Modifier -> Expr
Identifier (String -> Identifier
TextS.pack (String
"outclk_" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n)) Maybe Modifier
forall a. Maybe a
Nothing,PortDirection
Out,HWType
ty,Identifier -> Maybe Modifier -> Expr
Identifier Identifier
k Maybe Modifier
forall a. Maybe a
Nothing)
       |(Identifier
k,HWType
ty,Int
n) <- [Identifier] -> [HWType] -> [Int] -> [(Identifier, HWType, Int)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Identifier]
clocks [HWType]
tys [(Int
0 :: Int)..]  ]
      ,[(Identifier -> Maybe Modifier -> Expr
Identifier Identifier
"locked" Maybe Modifier
forall a. Maybe a
Nothing,PortDirection
Out,HWType
rstTy,Identifier -> Maybe Modifier -> Expr
Identifier Identifier
locked Maybe Modifier
forall a. Maybe a
Nothing)]]
   , Identifier
-> HWType
-> Expr
-> HWType
-> [(Maybe Literal, Expr)]
-> Declaration
CondAssignment Identifier
pllLock HWType
Bool (Identifier -> Maybe Modifier -> Expr
Identifier Identifier
locked Maybe Modifier
forall a. Maybe a
Nothing) HWType
rstTy
      [(Literal -> Maybe Literal
forall a. a -> Maybe a
Just (Bit -> Literal
BitLit Bit
H),Maybe (HWType, Int) -> Literal -> Expr
Literal Maybe (HWType, Int)
forall a. Maybe a
Nothing (Bool -> Literal
BoolLit Bool
True))
      ,(Maybe Literal
forall a. Maybe a
Nothing        ,Maybe (HWType, Int) -> Literal -> Expr
Literal Maybe (HWType, Int)
forall a. Maybe a
Nothing (Bool -> Literal
BoolLit Bool
False))]
   , Identifier -> Expr -> Declaration
Assignment Identifier
result (HWType -> Modifier -> [Expr] -> Expr
DataCon HWType
resTy ((HWType, Int) -> Modifier
DC (HWType
resTy,Int
0)) ([Expr] -> Expr) -> [Expr] -> Expr
forall a b. (a -> b) -> a -> b
$ [[Expr]] -> [Expr]
forall (t :: Type -> Type) a. Foldable t => t [a] -> [a]
concat
                          [[Identifier -> Maybe Modifier -> Expr
Identifier Identifier
k Maybe Modifier
forall a. Maybe a
Nothing | Identifier
k <- [Identifier]
clocks]
                          ,[Identifier -> Maybe Modifier -> Expr
Identifier Identifier
pllLock Maybe Modifier
forall a. Maybe a
Nothing]])

   ]
  ]
 where
  (Identifier Identifier
result Maybe Modifier
Nothing,resTy :: HWType
resTy@(Product Identifier
_ Maybe [Identifier]
_ ([HWType] -> [HWType]
forall a. [a] -> [a]
init -> [HWType]
tys))) = BlackBoxContext -> (Expr, HWType)
bbResult BlackBoxContext
bbCtx
  [(Expr
nm,HWType
_,Bool
_),(Expr
clk,HWType
clkTy,Bool
_),(Expr
rst,HWType
rstTy,Bool
_)] = Int -> [(Expr, HWType, Bool)] -> [(Expr, HWType, Bool)]
forall a. Int -> [a] -> [a]
drop Int
3 (BlackBoxContext -> [(Expr, HWType, Bool)]
bbInputs BlackBoxContext
bbCtx)
  Just String
nm' = Expr -> Maybe String
exprToString Expr
nm
  instname0 :: Identifier
instname0 = String -> Identifier
TextS.pack String
nm'
  compName :: Identifier
compName = [Identifier] -> Identifier
forall a. [a] -> a
head (BlackBoxContext -> [Identifier]
bbQsysIncName BlackBoxContext
bbCtx)

altpllTemplate
  :: Backend s
  => BlackBoxContext
  -> State s Doc
altpllTemplate :: BlackBoxContext -> State s Doc
altpllTemplate BlackBoxContext
bbCtx = do
 let mkId :: Identifier -> State s Identifier
mkId = IdType -> Identifier -> State s Identifier
forall s. Backend s => IdType -> Identifier -> State s Identifier
mkUniqueIdentifier IdType
Basic
 Identifier
pllOut <- Identifier -> State s Identifier
mkId Identifier
"pllOut"
 Identifier
locked <- Identifier -> State s Identifier
mkId Identifier
"locked"
 Identifier
pllLock <- Identifier -> State s Identifier
mkId Identifier
"pllLock"
 Identifier
alteraPll <- Identifier -> State s Identifier
mkId Identifier
"altpll_block"
 Identifier
alteraPll_inst <- Identifier -> State s Identifier
mkId Identifier
instname0
 Mon (State s) Doc -> State s Doc
forall (f :: Type -> Type) m. Mon f m -> f m
getMon (Mon (State s) Doc -> State s Doc)
-> Mon (State s) Doc -> State s Doc
forall a b. (a -> b) -> a -> b
$ Identifier -> [Declaration] -> Mon (State s) Doc
forall state.
Backend state =>
Identifier -> [Declaration] -> Mon (State state) Doc
blockDecl Identifier
alteraPll
  [ Maybe Identifier -> Identifier -> HWType -> Declaration
NetDecl Maybe Identifier
forall a. Maybe a
Nothing Identifier
locked  HWType
Bit
  , Maybe Identifier
-> WireOrReg
-> Identifier
-> Either Identifier HWType
-> Maybe Expr
-> Declaration
NetDecl' Maybe Identifier
forall a. Maybe a
Nothing WireOrReg
Reg Identifier
pllLock (HWType -> Either Identifier HWType
forall a b. b -> Either a b
Right HWType
Bool) Maybe Expr
forall a. Maybe a
Nothing
  , Maybe Identifier -> Identifier -> HWType -> Declaration
NetDecl Maybe Identifier
forall a. Maybe a
Nothing Identifier
pllOut HWType
clkOutTy
  , EntityOrComponent
-> Maybe Identifier
-> Identifier
-> Identifier
-> [(Expr, HWType, Expr)]
-> [(Expr, PortDirection, HWType, Expr)]
-> Declaration
InstDecl EntityOrComponent
Comp Maybe Identifier
forall a. Maybe a
Nothing Identifier
compName Identifier
alteraPll_inst []
      [(Identifier -> Maybe Modifier -> Expr
Identifier Identifier
"clk" Maybe Modifier
forall a. Maybe a
Nothing,PortDirection
In,HWType
clkTy,Expr
clk)
      ,(Identifier -> Maybe Modifier -> Expr
Identifier Identifier
"areset" Maybe Modifier
forall a. Maybe a
Nothing,PortDirection
In,HWType
rstTy,Expr
rst)
      ,(Identifier -> Maybe Modifier -> Expr
Identifier Identifier
"c0" Maybe Modifier
forall a. Maybe a
Nothing,PortDirection
Out,HWType
clkOutTy,Identifier -> Maybe Modifier -> Expr
Identifier Identifier
pllOut Maybe Modifier
forall a. Maybe a
Nothing)
      ,(Identifier -> Maybe Modifier -> Expr
Identifier Identifier
"locked" Maybe Modifier
forall a. Maybe a
Nothing,PortDirection
Out,HWType
Bit,Identifier -> Maybe Modifier -> Expr
Identifier Identifier
locked Maybe Modifier
forall a. Maybe a
Nothing)]
  , Identifier
-> HWType
-> Expr
-> HWType
-> [(Maybe Literal, Expr)]
-> Declaration
CondAssignment Identifier
pllLock HWType
Bool (Identifier -> Maybe Modifier -> Expr
Identifier Identifier
locked Maybe Modifier
forall a. Maybe a
Nothing) HWType
rstTy
      [(Literal -> Maybe Literal
forall a. a -> Maybe a
Just (Bit -> Literal
BitLit Bit
H),Maybe (HWType, Int) -> Literal -> Expr
Literal Maybe (HWType, Int)
forall a. Maybe a
Nothing (Bool -> Literal
BoolLit Bool
True))
      ,(Maybe Literal
forall a. Maybe a
Nothing        ,Maybe (HWType, Int) -> Literal -> Expr
Literal Maybe (HWType, Int)
forall a. Maybe a
Nothing (Bool -> Literal
BoolLit Bool
False))]
  , Identifier -> Expr -> Declaration
Assignment Identifier
result (HWType -> Modifier -> [Expr] -> Expr
DataCon HWType
resTy ((HWType, Int) -> Modifier
DC (HWType
resTy,Int
0))
                        [Identifier -> Maybe Modifier -> Expr
Identifier Identifier
pllOut Maybe Modifier
forall a. Maybe a
Nothing
                        ,Identifier -> Maybe Modifier -> Expr
Identifier Identifier
pllLock Maybe Modifier
forall a. Maybe a
Nothing])

  ]
 where
  [(Expr, HWType, Bool)
_,(Expr, HWType, Bool)
_,(Expr
nm,HWType
_,Bool
_),(Expr
clk,HWType
clkTy,Bool
_),(Expr
rst,HWType
rstTy,Bool
_)] = BlackBoxContext -> [(Expr, HWType, Bool)]
bbInputs BlackBoxContext
bbCtx
  (Identifier Identifier
result Maybe Modifier
Nothing,resTy :: HWType
resTy@(Product Identifier
_ Maybe [Identifier]
_ [HWType
clkOutTy,HWType
_])) = BlackBoxContext -> (Expr, HWType)
bbResult BlackBoxContext
bbCtx
  Just String
nm' = Expr -> Maybe String
exprToString Expr
nm
  instname0 :: Identifier
instname0 = String -> Identifier
TextS.pack String
nm'
  compName :: Identifier
compName = [Identifier] -> Identifier
forall a. [a] -> a
head (BlackBoxContext -> [Identifier]
bbQsysIncName BlackBoxContext
bbCtx)


altpllQsysTemplate
  :: Backend s
  => BlackBoxContext
  -> State s Doc
altpllQsysTemplate :: BlackBoxContext -> State s Doc
altpllQsysTemplate BlackBoxContext
bbCtx = Doc -> State s Doc
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Doc
bbText
 where
  ((Expr
_,HWType -> HWType
stripVoid -> HWType
kdIn,Bool
_):(Expr
_,HWType -> HWType
stripVoid -> HWType
kdOut,Bool
_):[(Expr, HWType, Bool)]
_) = BlackBoxContext -> [(Expr, HWType, Bool)]
bbInputs BlackBoxContext
bbCtx
  KnownDomain Identifier
_ Integer
clkInPeriod ActiveEdge
_ ResetKind
_ InitBehavior
_ ResetPolarity
_ = HWType
kdIn
  KnownDomain Identifier
_ Integer
clkOutPeriod ActiveEdge
_ ResetKind
_ InitBehavior
_ ResetPolarity
_ = HWType
kdOut
  clkOutFreq :: Double
  clkOutFreq :: Double
clkOutFreq = (Double
1.0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Integer -> Double
forall a. Num a => Integer -> a
fromInteger Integer
clkOutPeriod Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1.0e-12)) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
1e6
  clklcm :: Integer
clklcm = Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
lcm Integer
clkInPeriod Integer
clkOutPeriod
  clkmult :: Integer
clkmult = Integer
clklcm Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`quot` Integer
clkOutPeriod
  clkdiv :: Integer
clkdiv = Integer
clklcm Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`quot` Integer
clkInPeriod
  -- Note [QSys file templates]
  -- This QSys file template was derived from a "full" QSys system with a single
  -- "altpll" IP. Module parameters were then stripped on a trial-and-error
  -- basis to get a template that has the minimal number of parameters, but
  -- still has the desired, working, configuration.
  bbText :: Doc
bbText = [I.i|<?xml version="1.0" encoding="UTF-8"?>
<system name="$${FILENAME}">
  <module
    name="altpll0"
    kind="altpll"
    enabled="1"
    autoexport="1">
  <parameter name="AVALON_USE_SEPARATE_SYSCLK" value="NO" />
  <parameter name="BANDWIDTH" value="" />
  <parameter name="BANDWIDTH_TYPE" value="AUTO" />
  <parameter name="CLK0_DIVIDE_BY" value="#{clkdiv}" />
  <parameter name="CLK0_DUTY_CYCLE" value="50" />
  <parameter name="CLK0_MULTIPLY_BY" value="#{clkmult}" />
  <parameter name="CLK0_PHASE_SHIFT" value="0" />
  <parameter name="COMPENSATE_CLOCK" value="CLK0" />
  <parameter name="INCLK0_INPUT_FREQUENCY" value="#{clkInPeriod}" />
  <parameter name="OPERATION_MODE" value="NORMAL" />
  <parameter name="PLL_TYPE" value="AUTO" />
  <parameter name="PORT_ARESET" value="PORT_USED" />
  <parameter name="PORT_INCLK0" value="PORT_USED" />
  <parameter name="PORT_LOCKED" value="PORT_USED" />
  <parameter name="PORT_clk0" value="PORT_USED" />
  <parameter name="HIDDEN_IS_FIRST_EDIT" value="0" />
  <parameter name="HIDDEN_CONSTANTS">
    CT#PORT_clk0 PORT_USED
    CT#CLK0_MULTIPLY_BY #{clkmult}
    CT#WIDTH_CLOCK 5
    CT#LPM_TYPE altpll
    CT#PLL_TYPE AUTO
    CT#CLK0_PHASE_SHIFT 0
    CT#OPERATION_MODE NORMAL
    CT#COMPENSATE_CLOCK CLK0
    CT#INCLK0_INPUT_FREQUENCY #{clkInPeriod}
    CT#PORT_INCLK0 PORT_USED
    CT#PORT_ARESET PORT_USED
    CT#BANDWIDTH_TYPE AUTO
    CT#CLK0_DUTY_CYCLE 50
    CT#CLK0_DIVIDE_BY #{clkdiv}
    CT#PORT_LOCKED PORT_USED</parameter>
  <parameter name="HIDDEN_IF_PORTS">
    IF#phasecounterselect {input 4}
    IF#locked {output 0}
    IF#reset {input 0}
    IF#clk {input 0}
    IF#phaseupdown {input 0}
    IF#scandone {output 0}
    IF#readdata {output 32}
    IF#write {input 0}
    IF#scanclk {input 0}
    IF#phasedone {output 0}
    IF#address {input 2}
    IF#c0 {output 0}
    IF#writedata {input 32}
    IF#read {input 0}
    IF#areset {input 0}
    IF#scanclkena {input 0}
    IF#scandataout {output 0}
    IF#configupdate {input 0}
    IF#phasestep {input 0}
    IF#scandata {input 0}</parameter>
  <parameter name="HIDDEN_MF_PORTS">
    MF#areset 1
    MF#clk 1
    MF#locked 1
    MF#inclk 1</parameter>
  <parameter name="HIDDEN_PRIVATES">
    PT#PHASE_SHIFT0 0.00000000
    PT#DIV_FACTOR0 #{clkdiv}
    PT#EFF_OUTPUT_FREQ_VALUE0 #{clkOutFreq}
    PT#MULT_FACTOR0 #{clkmult}
    PT#DUTY_CYCLE0 50.00000000</parameter>
  </module>
</system>|]

alteraPllQsysTemplate
  :: Backend s
  => BlackBoxContext
  -> State s Doc
alteraPllQsysTemplate :: BlackBoxContext -> State s Doc
alteraPllQsysTemplate BlackBoxContext
bbCtx = Doc -> State s Doc
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Doc
bbText
 where
  ((Expr, HWType, Bool)
_:(Expr
_,HWType -> HWType
stripVoid -> HWType
kdIn,Bool
_):(Expr
_,HWType -> HWType
stripVoid -> HWType
kdOutsProd,Bool
_):[(Expr, HWType, Bool)]
_) = BlackBoxContext -> [(Expr, HWType, Bool)]
bbInputs BlackBoxContext
bbCtx
  kdOuts :: [HWType]
kdOuts = case HWType
kdOutsProd of
    Product Identifier
_ Maybe [Identifier]
_ [HWType]
ps -> [HWType]
ps
    KnownDomain {} -> [HWType
kdOutsProd]
    HWType
_ -> String -> [HWType]
forall a. HasCallStack => String -> a
error String
"internal error: not a Product or KnownDomain"

  cklFreq :: HWType -> Double
cklFreq (KnownDomain Identifier
_ Integer
p ActiveEdge
_ ResetKind
_ InitBehavior
_ ResetPolarity
_)
    = (Double
1.0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Integer -> Double
forall a. Num a => Integer -> a
fromInteger Integer
p Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1.0e-12 :: Double)) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
1e6
  cklFreq HWType
_ = String -> Double
forall a. HasCallStack => String -> a
error String
"internal error: not a KnownDomain"

  clkOuts :: Identifier
clkOuts = [Identifier] -> Identifier
TextS.unlines
    [[I.i|<parameter name="gui_output_clock_frequency#{n}" value="#{f}"/>|]
    | (Word
n,Double
f) <- [Word] -> [Double] -> [(Word, Double)]
forall a b. [a] -> [b] -> [(a, b)]
zip [(Word
0 :: Word)..] ((HWType -> Double) -> [HWType] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map HWType -> Double
cklFreq [HWType]
kdOuts)
    ]

  -- See Note [QSys file templates] on how this qsys template was derived.
  bbText :: Doc
bbText = [I.i|<?xml version="1.0" encoding="UTF-8"?>
<system name="$${FILENAME}">
 <module
    name="pll_0"
    kind="altera_pll"
    enabled="1"
    autoexport="1">
  <parameter name="gui_feedback_clock" value="Global Clock" />
  <parameter name="gui_number_of_clocks" value="#{length kdOuts}" />
  <parameter name="gui_operation_mode" value="direct" />
  #{clkOuts}
  <parameter name="gui_pll_mode" value="Integer-N PLL" />
  <parameter name="gui_reference_clock_frequency" value="#{cklFreq kdIn}" />
  <parameter name="gui_use_locked" value="true" />
 </module>
</system>|]