```{-|
Copyright  :  (C) 2013-2016, University of Twente,
2016-2017, Myrtle Software Ltd,
2021-2022, QBayLogic B.V.
Maintainer :  QBayLogic B.V. <devops@qbaylogic.com>

BlockRAM primitives

= Using RAMs #usingrams#

We will show a rather elaborate example on how you can, and why you might want
to use 'blockRam's. We will build a \"small\" CPU+Memory+Program ROM where we
will slowly evolve to using blockRams. Note that the code is /not/ meant as a

We start with the definition of the Instructions, Register names and machine
codes:

@
{\-\# LANGUAGE RecordWildCards, TupleSections, DeriveAnyClass \#-\}

module CPU where

import Clash.Explicit.Prelude

type Value     = Signed 8

data Instruction
= Compute Operator Reg Reg Reg
| Branch Reg Value
| Jump Value
| Nop
deriving (Eq,Show)

data Reg
= Zero
| PC
| RegA
| RegB
| RegC
| RegD
| RegE
deriving (Eq,Show,Enum)

data Operator = Add | Sub | Incr | Imm | CmpGt
deriving (Eq,Show)

data MachCode
= MachCode
{ inputX  :: Reg
, inputY  :: Reg
, result  :: Reg
, aluCode :: Operator
, ldReg   :: Reg
, jmpM    :: Maybe Value
}

nullCode = MachCode { inputX = Zero, inputY = Zero, result = Zero, aluCode = Imm
, jmpM = Nothing
}
@

Next we define the CPU and its ALU:

@
cpu
:: Vec 7 Value
-- ^ Register bank
-> (Value,Instruction)
-- ^ (Memory output, Current instruction)
-> ( Vec 7 Value
)
where
-- Current instruction pointer
ipntr = regbank 'Clash.Sized.Vector.!!' PC

-- Decoder
(MachCode {..}) = case instr of
Compute op rx ry res -> nullCode {inputX=rx,inputY=ry,result=res,aluCode=op}
Branch cr a          -> nullCode {inputX=cr,jmpM=Just a}
Jump a               -> nullCode {aluCode=Incr,jmpM=Just a}
Store r a            -> nullCode {inputX=r,wrAddrM=Just a}
Nop                  -> nullCode

-- ALU
regX   = regbank 'Clash.Sized.Vector.!!' inputX
regY   = regbank 'Clash.Sized.Vector.!!' inputY
aluOut = alu aluCode regX regY

-- next instruction
nextPC = case jmpM of
Just a | aluOut /= 0 -> ipntr + a
_                    -> ipntr + 1

-- update registers
regbank' = 'Clash.Sized.Vector.replace' Zero   0
\$ 'Clash.Sized.Vector.replace' PC     nextPC
\$ 'Clash.Sized.Vector.replace' result aluOut
\$ 'Clash.Sized.Vector.replace' ldReg  memOut
\$ regbank

alu Add   x y = x + y
alu Sub   x y = x - y
alu Incr  x _ = x + 1
alu Imm   x _ = x
alu CmpGt x y = if x > y then 1 else 0
@

We initially create a memory out of simple registers:

@
dataMem
:: KnownDomain dom
=> Clock dom
-> Reset dom
-> Enable dom
-- ^ (write address, data in)
-> Signal dom Value
-- ^ data out
dataMem clk rst en rd wrM = 'Clash.Explicit.Mealy.mealy' clk rst en dataMemT ('Clash.Sized.Vector.replicate' d32 0) (bundle (rd,wrM))
where
dataMemT mem (rd,wrM) = (mem',dout)
where
dout = mem 'Clash.Sized.Vector.!!' rd
mem' = case wrM of
Just (wr,din) -> 'Clash.Sized.Vector.replace' wr din mem
_ -> mem
@

And then connect everything:

@
system
:: ( KnownDomain dom
, KnownNat n )
=> Vec n Instruction
-> Clock dom
-> Reset dom
-> Enable dom
-> Signal dom Value
system instrs clk rst en = memOut
where
memOut = dataMem clk rst en rdAddr dout
(rdAddr,dout,ipntr) = 'Clash.Explicit.Mealy.mealyB' clk rst en cpu ('Clash.Sized.Vector.replicate' d7 0) (memOut,instr)
instr  = 'Clash.Explicit.Prelude.asyncRom' instrs '<\$>' ipntr
@

Create a simple program that calculates the GCD of 4 and 6:

@
-- Compute GCD of 4 and 6
prog = -- 0 := 4
Compute Incr Zero RegA RegA :>
replicate d3 (Compute Incr RegA Zero RegA) ++
Store RegA 0 :>
-- 1 := 6
Compute Incr Zero RegA RegA :>
replicate d5 (Compute Incr RegA Zero RegA) ++
Store RegA 1 :>
-- A := 4
-- B := 6
-- start
Compute CmpGt RegA RegB RegC :>
Branch RegC 4 :>
Compute CmpGt RegB RegA RegC :>
Branch RegC 4 :>
Jump 5 :>
-- (a > b)
Compute Sub RegA RegB RegA :>
Jump (-6) :>
-- (b > a)
Compute Sub RegB RegA RegB :>
Jump (-8) :>
-- end
Store RegA 2 :>
Nil
@

And test our system:

@
>>> sampleN 32 \$ system prog systemClockGen resetGen enableGen
[0,0,0,0,0,0,4,4,4,4,4,4,4,4,6,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2]

@

to see that our system indeed calculates that the GCD of 6 and 4 is 2.

=== Improvement 1: using @asyncRam@

As you can see, it's fairly straightforward to build a memory using registers
and read ('Clash.Sized.Vector.!!') and write ('Clash.Sized.Vector.replace')
logic. This might however not result in the most efficient hardware structure,
especially when building an ASIC.

Instead it is preferable to use the 'Clash.Prelude.RAM.asyncRam' function which
has the potential to be translated to a more efficient structure:

@
system2
:: ( KnownDomain dom
, KnownNat n )
=> Vec n Instruction
-> Clock dom
-> Reset dom
-> Enable dom
-> Signal dom Value
system2 instrs clk rst en = memOut
where
memOut = 'Clash.Explicit.RAM.asyncRam' clk clk en d32 rdAddr dout
(rdAddr,dout,ipntr) = 'Clash.Explicit.Prelude.mealyB' clk rst en cpu ('Clash.Sized.Vector.replicate' d7 0) (memOut,instr)
instr  = 'Clash.Prelude.ROM.asyncRom' instrs '<\$>' ipntr
@

Again, we can simulate our system and see that it works. This time however,
we need to disregard the first few output samples, because the initial content of an
'Clash.Prelude.RAM.asyncRam' is /undefined/, and consequently, the first few
output samples are also /undefined/. We use the utility function
'Clash.XException.printX' to conveniently filter out the undefinedness and
replace it with the string @\"undefined\"@ in the first few leading outputs.

@
>>> printX \$ sampleN 32 \$ system2 prog systemClockGen resetGen enableGen
[undefined,undefined,undefined,undefined,undefined,undefined,4,4,4,4,4,4,4,4,6,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2]

@

=== Improvement 2: using @blockRam@

Finally we get to using 'blockRam'. On FPGAs, 'Clash.Prelude.RAM.asyncRam' will
be implemented in terms of LUTs, and therefore take up logic resources. FPGAs
also have large(r) memory structures called /Block RAMs/, which are preferred,
especially as the memories we need for our application get bigger. The
'blockRam' function will be translated to such a /Block RAM/.

One important aspect of Block RAMs have a /synchronous/ read port, meaning that,
at time @t@, the value @v@ in the RAM at address @r@ is only available at time
@t+1@.

For us that means we need to change the design of our CPU. Right now, upon a
that read address is immediately available to be put in the register bank.
Because we will be using a BlockRAM, the value is delayed until the next cycle.
We hence need to also delay the register address to which the memory address

@
cpu2
:: (Vec 7 Value,Reg)
-> (Value,Instruction)
-- ^ (Memory output, Current instruction)
-> ( (Vec 7 Value,Reg)
)
where
-- Current instruction pointer
ipntr = regbank 'Clash.Sized.Vector.!!' PC

-- Decoder
(MachCode {..}) = case instr of
Compute op rx ry res -> nullCode {inputX=rx,inputY=ry,result=res,aluCode=op}
Branch cr a          -> nullCode {inputX=cr,jmpM=Just a}
Jump a               -> nullCode {aluCode=Incr,jmpM=Just a}
Store r a            -> nullCode {inputX=r,wrAddrM=Just a}
Nop                  -> nullCode

-- ALU
regX   = regbank 'Clash.Sized.Vector.!!' inputX
regY   = regbank 'Clash.Sized.Vector.!!' inputY
aluOut = alu aluCode regX regY

-- next instruction
nextPC = case jmpM of
Just a | aluOut /= 0 -> ipntr + a
_                    -> ipntr + 1

-- update registers
ldRegD'  = ldReg -- Delay the ldReg by 1 cycle
regbank' = 'Clash.Sized.Vector.replace' Zero   0
\$ 'Clash.Sized.Vector.replace' PC     nextPC
\$ 'Clash.Sized.Vector.replace' result aluOut
\$ 'Clash.Sized.Vector.replace' ldRegD memOut
\$ regbank
@

We can now finally instantiate our system with a 'blockRam':

@
system3
:: ( KnownDomain dom
, KnownNat n )
=> Vec n Instruction
-> Clock dom
-> Reset dom
-> Enable dom
-> Signal dom Value
system3 instrs clk rst en = memOut
where
memOut = 'blockRam' clk en (replicate d32 0) rdAddr dout
(rdAddr,dout,ipntr) = 'Clash.Explicit.Prelude.mealyB' clk rst en cpu2 (('Clash.Sized.Vector.replicate' d7 0),Zero) (memOut,instr)
instr  = 'Clash.Explicit.Prelude.asyncRom' instrs '<\$>' ipntr
@

We are, however, not done. We will also need to update our program. The reason
being that values that we try to load in our registers won't be loaded into the
register until the next cycle. This is a problem when the next instruction
immediately depended on this memory value. In our case, this was only the case
when the loaded the value @6@, which was stored at address @1@, into @RegB@.
Our updated program is thus:

@
prog2 = -- 0 := 4
Compute Incr Zero RegA RegA :>
replicate d3 (Compute Incr RegA Zero RegA) ++
Store RegA 0 :>
-- 1 := 6
Compute Incr Zero RegA RegA :>
replicate d5 (Compute Incr RegA Zero RegA) ++
Store RegA 1 :>
-- A := 4
-- B := 6
Nop :> -- Extra NOP
-- start
Compute CmpGt RegA RegB RegC :>
Branch RegC 4 :>
Compute CmpGt RegB RegA RegC :>
Branch RegC 4 :>
Jump 5 :>
-- (a > b)
Compute Sub RegA RegB RegA :>
Jump (-6) :>
-- (b > a)
Compute Sub RegB RegA RegB :>
Jump (-8) :>
-- end
Store RegA 2 :>
Nil
@

When we simulate our system we see that it works. This time again,
we need to disregard the first sample, because the initial output of a
'blockRam' is /undefined/. We use the utility function 'Clash.XException.printX'
to conveniently filter out the undefinedness and replace it with the string @\"undefined\"@.

@
>>> printX \$ sampleN 34 \$ system3 prog2 systemClockGen resetGen enableGen
[undefined,0,0,0,0,0,0,4,4,4,4,4,4,4,4,6,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2]

@

This concludes the short introduction to using 'blockRam'.

-}

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE NoImplicitPrelude #-}

{-# LANGUAGE Trustworthy #-}

{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}

-- See [Note: eta port names for trueDualPortBlockRam]
{-# OPTIONS_GHC -fno-do-lambda-eta-expansion #-}

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

module Clash.Explicit.BlockRam
( -- * BlockRAM synchronized to the system clock
blockRam
, blockRamPow2
, blockRamU
, blockRam1
, ResetStrategy(..)
-- * True dual-port block RAM
-- \$tdpbram
, trueDualPortBlockRam
, RamOp(..)
-- * Internal
, blockRam#
, trueDualPortBlockRam#
)
where

import           Control.Exception      (catch, throw)
import           Data.Array.MArray      (newListArray)
import qualified Data.List              as L
import           Data.Either            (isLeft)
import           Data.Maybe             (isJust, fromMaybe)
import           GHC.Arr
import qualified Data.Sequence          as Seq
import           Data.Sequence          (Seq)
import           Data.Tuple             (swap)
import           GHC.Generics           (Generic)
import           GHC.Stack              (HasCallStack, withFrozenCallStack)
import           GHC.TypeLits           (KnownNat, type (^), type (<=))
import           Unsafe.Coerce          (unsafeCoerce)

import           Clash.Annotations.Primitive
(hasBlackBox)
import           Clash.Class.Num        (SaturationMode(SatBound), satSucc)
import           Clash.Explicit.Signal  (KnownDomain, Enable, register, fromEnable)
import           Clash.Signal.Internal
(Clock(..), Reset, Signal (..), invertReset, (.&&.), mux)
import           Clash.Promoted.Nat     (SNat(..), snatToNum, natToNum)
import           Clash.Signal.Bundle    (unbundle, bundle)
import           Clash.Signal.Internal.Ambiguous (clockPeriod)
import           Clash.Sized.Unsigned   (Unsigned)
import           Clash.Sized.Index      (Index)
import           Clash.Sized.Vector     (Vec, replicate, iterateI)
import qualified Clash.Sized.Vector     as CV
import           Clash.XException
(maybeIsX, NFDataX(deepErrorX), defaultSeqX, fromJustX, undefined,
XException (..), seqX, isX, errorX)

{- \$tdpbram
A true dual-port block RAM has two fully independent, fully functional access
ports: port A and port B. Either port can do both RAM reads and writes. These
two ports can even be on distinct clock domains, but the memory itself is shared
between the ports. This also makes a true dual-port block RAM suitable as a
component in a domain crossing circuit (but it needs additional logic for it to
be safe, see e.g. 'Clash.Explicit.Synchronizer.asyncFIFOSynchronizer').

A version with implicit clocks can be found in "Clash.Prelude.BlockRam".
-}

-- start benchmark only
-- import GHC.Arr (listArray, unsafeThawSTArray)
-- end benchmark only

{- \$setup
>>> import Clash.Explicit.Prelude as C
>>> import qualified Data.List as L
>>> :set -XDataKinds -XRecordWildCards -XTupleSections -XDeriveAnyClass -XDeriveGeneric
>>> type InstrAddr = Unsigned 8
>>> type MemAddr = Unsigned 5
>>> type Value = Signed 8
>>> :{
data Reg
= Zero
| PC
| RegA
| RegB
| RegC
| RegD
| RegE
deriving (Eq,Show,Enum,C.Generic,NFDataX)
:}

>>> :{
data Operator = Add | Sub | Incr | Imm | CmpGt
deriving (Eq,Show)
:}

>>> :{
data Instruction
= Compute Operator Reg Reg Reg
| Branch Reg Value
| Jump Value
| Nop
deriving (Eq,Show)
:}

>>> :{
data MachCode
= MachCode
{ inputX  :: Reg
, inputY  :: Reg
, result  :: Reg
, aluCode :: Operator
, ldReg   :: Reg
, jmpM    :: Maybe Value
}
:}

>>> :{
nullCode = MachCode { inputX = Zero, inputY = Zero, result = Zero, aluCode = Imm
, jmpM = Nothing
}
:}

>>> :{
alu Add   x y = x + y
alu Sub   x y = x - y
alu Incr  x _ = x + 1
alu Imm   x _ = x
alu CmpGt x y = if x > y then 1 else 0
:}

>>> :{
let cpu :: Vec 7 Value          -- ^ Register bank
-> (Value,Instruction)  -- ^ (Memory output, Current instruction)
-> ( Vec 7 Value
)
where
-- Current instruction pointer
ipntr = regbank C.!! PC
-- Decoder
(MachCode {..}) = case instr of
Compute op rx ry res -> nullCode {inputX=rx,inputY=ry,result=res,aluCode=op}
Branch cr a          -> nullCode {inputX=cr,jmpM=Just a}
Jump a               -> nullCode {aluCode=Incr,jmpM=Just a}
Store r a            -> nullCode {inputX=r,wrAddrM=Just a}
Nop                  -> nullCode
-- ALU
regX   = regbank C.!! inputX
regY   = regbank C.!! inputY
aluOut = alu aluCode regX regY
-- next instruction
nextPC = case jmpM of
Just a | aluOut /= 0 -> ipntr + a
_                    -> ipntr + 1
-- update registers
regbank' = replace Zero   0
\$ replace PC     nextPC
\$ replace result aluOut
\$ replace ldReg  memOut
\$ regbank
:}

>>> :{
let dataMem
:: KnownDomain dom
=> Clock  dom
-> Reset  dom
-> Enable dom
-> Signal dom Value
dataMem clk rst en rd wrM = mealy clk rst en dataMemT (C.replicate d32 0) (bundle (rd,wrM))
where
dataMemT mem (rd,wrM) = (mem',dout)
where
dout = mem C.!! rd
mem' = case wrM of
Just (wr,din) -> replace wr din mem
Nothing       -> mem
:}

>>> :{
let system
:: ( KnownDomain dom
, KnownNat n )
=> Vec n Instruction
-> Clock dom
-> Reset dom
-> Enable dom
-> Signal dom Value
system instrs clk rst en = memOut
where
memOut = dataMem clk rst en rdAddr dout
(rdAddr,dout,ipntr) = mealyB clk rst en cpu (C.replicate d7 0) (memOut,instr)
instr  = asyncRom instrs <\$> ipntr
:}

>>> :{
-- Compute GCD of 4 and 6
prog = -- 0 := 4
Compute Incr Zero RegA RegA :>
C.replicate d3 (Compute Incr RegA Zero RegA) C.++
Store RegA 0 :>
-- 1 := 6
Compute Incr Zero RegA RegA :>
C.replicate d5 (Compute Incr RegA Zero RegA) C.++
Store RegA 1 :>
-- A := 4
-- B := 6
-- start
Compute CmpGt RegA RegB RegC :>
Branch RegC 4 :>
Compute CmpGt RegB RegA RegC :>
Branch RegC 4 :>
Jump 5 :>
-- (a > b)
Compute Sub RegA RegB RegA :>
Jump (-6) :>
-- (b > a)
Compute Sub RegB RegA RegB :>
Jump (-8) :>
-- end
Store RegA 2 :>
Nil
:}

>>> :{
let system2
:: ( KnownDomain dom
, KnownNat n )
=> Vec n Instruction
-> Clock dom
-> Reset dom
-> Enable dom
-> Signal dom Value
system2 instrs clk rst en = memOut
where
memOut = asyncRam clk clk en d32 rdAddr dout
(rdAddr,dout,ipntr) = mealyB clk rst en cpu (C.replicate d7 0) (memOut,instr)
instr  = asyncRom instrs <\$> ipntr
:}

>>> :{
let cpu2 :: (Vec 7 Value,Reg)    -- ^ (Register bank, Load reg addr)
-> (Value,Instruction)  -- ^ (Memory output, Current instruction)
-> ( (Vec 7 Value,Reg)
)
where
-- Current instruction pointer
ipntr = regbank C.!! PC
-- Decoder
(MachCode {..}) = case instr of
Compute op rx ry res -> nullCode {inputX=rx,inputY=ry,result=res,aluCode=op}
Branch cr a          -> nullCode {inputX=cr,jmpM=Just a}
Jump a               -> nullCode {aluCode=Incr,jmpM=Just a}
Store r a            -> nullCode {inputX=r,wrAddrM=Just a}
Nop                  -> nullCode
-- ALU
regX   = regbank C.!! inputX
regY   = regbank C.!! inputY
aluOut = alu aluCode regX regY
-- next instruction
nextPC = case jmpM of
Just a | aluOut /= 0 -> ipntr + a
_                    -> ipntr + 1
-- update registers
ldRegD'  = ldReg -- Delay the ldReg by 1 cycle
regbank' = replace Zero   0
\$ replace PC     nextPC
\$ replace result aluOut
\$ replace ldRegD memOut
\$ regbank
:}

>>> :{
let system3
:: ( KnownDomain dom
, KnownNat n )
=> Vec n Instruction
-> Clock dom
-> Reset dom
-> Enable dom
-> Signal dom Value
system3 instrs clk rst en = memOut
where
memOut = blockRam clk en (C.replicate d32 0) rdAddr dout
(rdAddr,dout,ipntr) = mealyB clk rst en cpu2 ((C.replicate d7 0),Zero) (memOut,instr)
instr  = asyncRom instrs <\$> ipntr
:}

>>> :{
prog2 = -- 0 := 4
Compute Incr Zero RegA RegA :>
C.replicate d3 (Compute Incr RegA Zero RegA) C.++
Store RegA 0 :>
-- 1 := 6
Compute Incr Zero RegA RegA :>
C.replicate d5 (Compute Incr RegA Zero RegA) C.++
Store RegA 1 :>
-- A := 4
-- B := 6
Nop :> -- Extra NOP
-- start
Compute CmpGt RegA RegB RegC :>
Branch RegC 4 :>
Compute CmpGt RegB RegA RegC :>
Branch RegC 4 :>
Jump 5 :>
-- (a > b)
Compute Sub RegA RegB RegA :>
Jump (-6) :>
-- (b > a)
Compute Sub RegB RegA RegB :>
Jump (-8) :>
-- end
Store RegA 2 :>
Nil
:}

-}

-- | Create a blockRAM with space for @n@ elements
--
-- * __NB__: Read value is delayed by 1 cycle
-- * __NB__: Initial output value is /undefined/, reading it will throw an
-- 'XException'
--
-- @
-- bram40
--   :: 'Clock'  dom
--   -> 'Enable'  dom
--   -> 'Signal' dom ('Unsigned' 6)
--   -> 'Signal' dom (Maybe ('Unsigned' 6, 'Clash.Sized.BitVector.Bit'))
--   -> 'Signal' dom 'Clash.Sized.BitVector.Bit'
-- bram40 clk en = 'blockRam' clk en ('Clash.Sized.Vector.replicate' d40 1)
-- @
--
--
-- * See "Clash.Explicit.BlockRam#usingrams" for more information on how to use a
-- Block RAM.
-- * A large 'Vec' for the initial content might be too inefficient, depending
-- on how it is constructed. See 'Clash.Explicit.BlockRam.File.blockRamFile' and
-- 'Clash.Explicit.BlockRam.Blob.blockRamBlob' for different approaches that
-- scale well.
blockRam
:: ( KnownDomain dom
, HasCallStack
, NFDataX a
=> Clock dom
-- ^ 'Clock' to synchronize to
-> Enable dom
-- ^ Global enable
-> Vec n a
-- ^ Initial content of the BRAM, also determines the size, @n@, of the BRAM.
--
-- __NB__: __MUST__ be a constant.
-> Signal dom (Maybe (addr, a))
-- ^ (write address @w@, value to write)
-> Signal dom a
-- ^ Value of the @blockRAM@ at address @r@ from the previous clock cycle
blockRam :: Clock dom
-> Enable dom
-> Vec n a
-> Signal dom (Maybe (addr, a))
-> Signal dom a
blockRam = \Clock dom
clk Enable dom
gen Vec n a
rd Signal dom (Maybe (addr, a))
wrM ->
let en :: Signal dom Bool
en       = Maybe (addr, a) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (addr, a) -> Bool)
-> Signal dom (Maybe (addr, a)) -> Signal dom Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Maybe (addr, a))
wrM
wr,Signal dom a
forall a (dom :: Domain).
Bundle a =>
Signal dom a -> Unbundled dom a
forall a. HasCallStack => Maybe a -> a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Maybe (addr, a))
wrM)
in  (HasCallStack => Signal dom a) -> Signal dom a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack
(Clock dom
-> Enable dom
-> Vec n a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
forall (dom :: Domain) a (n :: Nat).
(KnownDomain dom, HasCallStack, NFDataX a) =>
Clock dom
-> Enable dom
-> Vec n a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
blockRam# Clock dom
clk Enable dom
gen Vec n a
forall a. Enum a => a -> Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
rd) Signal dom Bool
forall a. Enum a => a -> Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
wr) Signal dom a
din)
{-# INLINE blockRam #-}

-- | Create a blockRAM with space for 2^@n@ elements
--
-- * __NB__: Read value is delayed by 1 cycle
-- * __NB__: Initial output value is /undefined/, reading it will throw an
-- 'XException'
--
-- @
-- bram32
--   :: 'Clock' dom
--   -> 'Enable' dom
--   -> 'Signal' dom ('Unsigned' 5)
--   -> 'Signal' dom (Maybe ('Unsigned' 5, 'Clash.Sized.BitVector.Bit'))
--   -> 'Signal' dom 'Clash.Sized.BitVector.Bit'
-- bram32 clk en = 'blockRamPow2' clk en ('Clash.Sized.Vector.replicate' d32 1)
-- @
--
--
-- * See "Clash.Prelude.BlockRam#usingrams" for more information on how to use a
-- Block RAM.
-- * A large 'Vec' for the initial content might be too inefficient, depending
-- on how it is constructed. See 'Clash.Explicit.BlockRam.File.blockRamFilePow2'
-- and 'Clash.Explicit.BlockRam.Blob.blockRamBlobPow2' for different approaches
-- that scale well.
blockRamPow2
:: ( KnownDomain dom
, HasCallStack
, NFDataX a
, KnownNat n )
=> Clock dom
-- ^ 'Clock' to synchronize to
-> Enable dom
-- ^ Global enable
-> Vec (2^n) a
-- ^ Initial content of the BRAM
--
-- __NB__: __MUST__ be a constant.
-> Signal dom (Unsigned n)
-> Signal dom (Maybe (Unsigned n, a))
-- ^ (Write address @w@, value to write)
-> Signal dom a
-- ^ Value of the @blockRAM@ at address @r@ from the previous
-- clock cycle
blockRamPow2 :: Clock dom
-> Enable dom
-> Vec (2 ^ n) a
-> Signal dom (Unsigned n)
-> Signal dom (Maybe (Unsigned n, a))
-> Signal dom a
blockRamPow2 = \Clock dom
clk Enable dom
en Vec (2 ^ n) a
cnt Signal dom (Unsigned n)
rd Signal dom (Maybe (Unsigned n, a))
wrM -> (HasCallStack => Signal dom a) -> Signal dom a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack
(Clock dom
-> Enable dom
-> Vec (2 ^ n) a
-> Signal dom (Unsigned n)
-> Signal dom (Maybe (Unsigned n, a))
-> Signal dom a
forall (dom :: Domain) a addr (n :: Nat).
(KnownDomain dom, HasCallStack, NFDataX a, Enum addr) =>
Clock dom
-> Enable dom
-> Vec n a
-> Signal dom (Maybe (addr, a))
-> Signal dom a
blockRam Clock dom
clk Enable dom
en Vec (2 ^ n) a
cnt Signal dom (Unsigned n)
rd Signal dom (Maybe (Unsigned n, a))
wrM)
{-# INLINE blockRamPow2 #-}

data ResetStrategy (r :: Bool) where
ClearOnReset :: ResetStrategy 'True
NoClearOnReset :: ResetStrategy 'False

-- | Version of blockram that has no default values set. May be cleared to an
-- arbitrary state using a reset function.
blockRamU
:: forall n dom a r addr
. ( KnownDomain dom
, HasCallStack
, NFDataX a
, 1 <= n )
=> Clock dom
-- ^ 'Clock' to synchronize to
-> Reset dom
-- ^ 'Reset' line to listen to. Needs to be held at least /n/ cycles in order
-- for the BRAM to be reset to its initial state.
-> Enable dom
-- ^ Global enable
-> ResetStrategy r
-- ^ Whether to clear BRAM on asserted reset ('ClearOnReset') or
-- not ('NoClearOnReset'). Reset needs to be asserted at least /n/ cycles to
-- clear the BRAM.
-> SNat n
-- ^ Number of elements in BRAM
-> (Index n -> a)
-- ^ If applicable (see 'ResetStrategy' argument), reset BRAM using this function.
-> Signal dom (Maybe (addr, a))
-- ^ (write address @w@, value to write)
-> Signal dom a
-- ^ Value of the @blockRAM@ at address @r@ from the previous clock cycle
blockRamU :: Clock dom
-> Reset dom
-> Enable dom
-> ResetStrategy r
-> SNat n
-> (Index n -> a)
-> Signal dom (Maybe (addr, a))
-> Signal dom a
blockRamU Clock dom
clk Reset dom
rst0 Enable dom
en ResetStrategy r
rstStrategy n :: SNat n
n@SNat n
SNat Index n -> a
rd0 Signal dom (Maybe (addr, a))
mw0 =
case ResetStrategy r
rstStrategy of
ResetStrategy r
ClearOnReset ->
-- Use reset infrastructure
Clock dom
-> Enable dom
-> SNat n
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
forall (n :: Nat) (dom :: Domain) a.
(KnownDomain dom, HasCallStack, NFDataX a) =>
Clock dom
-> Enable dom
-> SNat n
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
blockRamU# Clock dom
clk Enable dom
en SNat n
n Signal dom Int
rd1 Signal dom Bool
we1 Signal dom Int
wa1 Signal dom a
w1
ResetStrategy r
NoClearOnReset ->
-- Ignore reset infrastructure, pass values unchanged
Clock dom
-> Enable dom
-> SNat n
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
forall (n :: Nat) (dom :: Domain) a.
(KnownDomain dom, HasCallStack, NFDataX a) =>
Clock dom
-> Enable dom
-> SNat n
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
blockRamU# Clock dom
clk Enable dom
en SNat n
n
forall a. Enum a => a -> Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
rd0)
Signal dom Bool
we0
forall a. Enum a => a -> Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
wa0)
Signal dom a
w0
where
rstBool :: Signal dom Bool
rstBool = Clock dom
-> Reset dom
-> Enable dom
-> Bool
-> Signal dom Bool
-> Signal dom Bool
forall (dom :: Domain) a.
(KnownDomain dom, NFDataX a) =>
Clock dom
-> Reset dom -> Enable dom -> a -> Signal dom a -> Signal dom a
register Clock dom
clk Reset dom
rst0 Enable dom
en Bool
True (Bool -> Signal dom Bool
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Bool
False)
rstInv :: Reset dom
rstInv = Reset dom -> Reset dom
forall (dom :: Domain). Reset dom -> Reset dom
invertReset Reset dom
rst0

waCounter :: Signal dom (Index n)
waCounter :: Signal dom (Index n)
waCounter = Clock dom
-> Reset dom
-> Enable dom
-> Index n
-> Signal dom (Index n)
-> Signal dom (Index n)
forall (dom :: Domain) a.
(KnownDomain dom, NFDataX a) =>
Clock dom
-> Reset dom -> Enable dom -> a -> Signal dom a -> Signal dom a
register Clock dom
clk Reset dom
rstInv Enable dom
en Index n
0 (SaturationMode -> Index n -> Index n
forall a. SaturatingNum a => SaturationMode -> a -> a
satSucc SaturationMode
SatBound (Index n -> Index n)
-> Signal dom (Index n) -> Signal dom (Index n)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Index n)
waCounter)

forall a b. (a, b) -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall a. HasCallStack => Maybe a -> a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Maybe (addr, a))
mw0
w0 :: Signal dom a
w0  = (addr, a) -> a
forall a b. (a, b) -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall a. HasCallStack => Maybe a -> a
fromJustX (Maybe (addr, a) -> a)
-> Signal dom (Maybe (addr, a)) -> Signal dom a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Maybe (addr, a))
mw0
we0 :: Signal dom Bool
we0 = Maybe (addr, a) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (addr, a) -> Bool)
-> Signal dom (Maybe (addr, a)) -> Signal dom Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Maybe (addr, a))
mw0

rd1 :: Signal dom Int
rd1 = Signal dom Bool
-> Signal dom Int -> Signal dom Int -> Signal dom Int
forall (f :: Type -> Type) a.
Applicative f =>
f Bool -> f a -> f a -> f a
mux Signal dom Bool
rstBool Signal dom Int
forall a. Enum a => a -> Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
rd0)
we1 :: Signal dom Bool
we1 = Signal dom Bool
-> Signal dom Bool -> Signal dom Bool -> Signal dom Bool
forall (f :: Type -> Type) a.
Applicative f =>
f Bool -> f a -> f a -> f a
mux Signal dom Bool
rstBool (Bool -> Signal dom Bool
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Bool
True) Signal dom Bool
we0
wa1 :: Signal dom Int
wa1 = Signal dom Bool
-> Signal dom Int -> Signal dom Int -> Signal dom Int
forall (f :: Type -> Type) a.
Applicative f =>
f Bool -> f a -> f a -> f a
mux Signal dom Bool
rstBool (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> (Index n -> Integer) -> Index n -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index n -> Integer
forall a. Integral a => a -> Integer
toInteger (Index n -> Int) -> Signal dom (Index n) -> Signal dom Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Index n)
forall a. Enum a => a -> Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
wa0)
w1 :: Signal dom a
w1  = Signal dom Bool -> Signal dom a -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a.
Applicative f =>
f Bool -> f a -> f a -> f a
mux Signal dom Bool
rstBool (Index n -> a
initF (Index n -> a) -> Signal dom (Index n) -> Signal dom a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Index n)
waCounter) Signal dom a
w0

-- | blockRAMU primitive
blockRamU#
:: forall n dom a
. ( KnownDomain dom
, HasCallStack
, NFDataX a )
=> Clock dom
-- ^ 'Clock' to synchronize to
-> Enable dom
-- ^ Global Enable
-> SNat n
-- ^ Number of elements in BRAM
-> Signal dom Int
-> Signal dom Bool
-- ^ Write enable
-> Signal dom Int
-> Signal dom a
-- ^ Value to write (at address @w@)
-> Signal dom a
-- ^ Value of the @blockRAM@ at address @r@ from the previous clock cycle
blockRamU# :: Clock dom
-> Enable dom
-> SNat n
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
blockRamU# Clock dom
clk Enable dom
en SNat n
SNat =
-- TODO: Generalize to single BRAM primitive taking an initialization function
Clock dom
-> Enable dom
-> Vec n a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
forall (dom :: Domain) a (n :: Nat).
(KnownDomain dom, HasCallStack, NFDataX a) =>
Clock dom
-> Enable dom
-> Vec n a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
blockRam#
Clock dom
clk
Enable dom
en
((Int -> a) -> Vec n Int -> Vec n a
forall a b (n :: Nat). (a -> b) -> Vec n a -> Vec n b
CV.map
(\Int
i -> String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
\$ String
"Initial value at index " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
i String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" undefined.")
((Int -> Int) -> Int -> Vec n Int
forall (n :: Nat) a. KnownNat n => (a -> a) -> a -> Vec n a
iterateI @n Int -> Int
forall a. Enum a => a -> a
succ (Int
0 :: Int)))
{-# NOINLINE blockRamU# #-}
{-# ANN blockRamU# hasBlackBox #-}

-- | Version of blockram that is initialized with the same value on all
-- memory positions.
blockRam1
:: forall n dom a r addr
. ( KnownDomain dom
, HasCallStack
, NFDataX a
, 1 <= n )
=> Clock dom
-- ^ 'Clock' to synchronize to
-> Reset dom
-- ^ 'Reset' line to listen to. Needs to be held at least /n/ cycles in order
-- for the BRAM to be reset to its initial state.
-> Enable dom
-- ^ Global enable
-> ResetStrategy r
-- ^ Whether to clear BRAM on asserted reset ('ClearOnReset') or
-- not ('NoClearOnReset'). Reset needs to be asserted at least /n/ cycles to
-- clear the BRAM.
-> SNat n
-- ^ Number of elements in BRAM
-> a
-- ^ Initial content of the BRAM (replicated /n/ times)
-> Signal dom (Maybe (addr, a))
-- ^ (write address @w@, value to write)
-> Signal dom a
-- ^ Value of the @blockRAM@ at address @r@ from the previous clock cycle
blockRam1 :: Clock dom
-> Reset dom
-> Enable dom
-> ResetStrategy r
-> SNat n
-> a
-> Signal dom (Maybe (addr, a))
-> Signal dom a
blockRam1 Clock dom
clk Reset dom
rst0 Enable dom
en ResetStrategy r
rstStrategy n :: SNat n
n@SNat n
SNat a
rd0 Signal dom (Maybe (addr, a))
mw0 =
case ResetStrategy r
rstStrategy of
ResetStrategy r
ClearOnReset ->
-- Use reset infrastructure
Clock dom
-> Enable dom
-> SNat n
-> a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
forall (n :: Nat) (dom :: Domain) a.
(KnownDomain dom, HasCallStack, NFDataX a) =>
Clock dom
-> Enable dom
-> SNat n
-> a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
blockRam1# Clock dom
clk Enable dom
en SNat n
n a
a Signal dom Int
rd1 Signal dom Bool
we1 Signal dom Int
wa1 Signal dom a
w1
ResetStrategy r
NoClearOnReset ->
-- Ignore reset infrastructure, pass values unchanged
Clock dom
-> Enable dom
-> SNat n
-> a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
forall (n :: Nat) (dom :: Domain) a.
(KnownDomain dom, HasCallStack, NFDataX a) =>
Clock dom
-> Enable dom
-> SNat n
-> a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
blockRam1# Clock dom
clk Enable dom
en SNat n
n a
a
forall a. Enum a => a -> Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
rd0)
Signal dom Bool
we0
forall a. Enum a => a -> Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
wa0)
Signal dom a
w0
where
rstBool :: Signal dom Bool
rstBool = Clock dom
-> Reset dom
-> Enable dom
-> Bool
-> Signal dom Bool
-> Signal dom Bool
forall (dom :: Domain) a.
(KnownDomain dom, NFDataX a) =>
Clock dom
-> Reset dom -> Enable dom -> a -> Signal dom a -> Signal dom a
register Clock dom
clk Reset dom
rst0 Enable dom
en Bool
True (Bool -> Signal dom Bool
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Bool
False)
rstInv :: Reset dom
rstInv = Reset dom -> Reset dom
forall (dom :: Domain). Reset dom -> Reset dom
invertReset Reset dom
rst0

waCounter :: Signal dom (Index n)
waCounter :: Signal dom (Index n)
waCounter = Clock dom
-> Reset dom
-> Enable dom
-> Index n
-> Signal dom (Index n)
-> Signal dom (Index n)
forall (dom :: Domain) a.
(KnownDomain dom, NFDataX a) =>
Clock dom
-> Reset dom -> Enable dom -> a -> Signal dom a -> Signal dom a
register Clock dom
clk Reset dom
rstInv Enable dom
en Index n
0 (SaturationMode -> Index n -> Index n
forall a. SaturatingNum a => SaturationMode -> a -> a
satSucc SaturationMode
SatBound (Index n -> Index n)
-> Signal dom (Index n) -> Signal dom (Index n)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Index n)
waCounter)

forall a b. (a, b) -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall a. HasCallStack => Maybe a -> a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Maybe (addr, a))
mw0
w0 :: Signal dom a
w0  = (addr, a) -> a
forall a b. (a, b) -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall a. HasCallStack => Maybe a -> a
fromJustX (Maybe (addr, a) -> a)
-> Signal dom (Maybe (addr, a)) -> Signal dom a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Maybe (addr, a))
mw0
we0 :: Signal dom Bool
we0 = Maybe (addr, a) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (addr, a) -> Bool)
-> Signal dom (Maybe (addr, a)) -> Signal dom Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Maybe (addr, a))
mw0

rd1 :: Signal dom Int
rd1 = Signal dom Bool
-> Signal dom Int -> Signal dom Int -> Signal dom Int
forall (f :: Type -> Type) a.
Applicative f =>
f Bool -> f a -> f a -> f a
mux Signal dom Bool
rstBool Signal dom Int
forall a. Enum a => a -> Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
rd0)
we1 :: Signal dom Bool
we1 = Signal dom Bool
-> Signal dom Bool -> Signal dom Bool -> Signal dom Bool
forall (f :: Type -> Type) a.
Applicative f =>
f Bool -> f a -> f a -> f a
mux Signal dom Bool
rstBool (Bool -> Signal dom Bool
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Bool
True) Signal dom Bool
we0
wa1 :: Signal dom Int
wa1 = Signal dom Bool
-> Signal dom Int -> Signal dom Int -> Signal dom Int
forall (f :: Type -> Type) a.
Applicative f =>
f Bool -> f a -> f a -> f a
mux Signal dom Bool
rstBool (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> (Index n -> Integer) -> Index n -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index n -> Integer
forall a. Integral a => a -> Integer
toInteger (Index n -> Int) -> Signal dom (Index n) -> Signal dom Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal dom (Index n)
forall a. Enum a => a -> Int
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
wa0)
w1 :: Signal dom a
w1  = Signal dom Bool -> Signal dom a -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a.
Applicative f =>
f Bool -> f a -> f a -> f a
mux Signal dom Bool
rstBool (a -> Signal dom a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure a
a) Signal dom a
w0

-- | blockRAM1 primitive
blockRam1#
:: forall n dom a
. ( KnownDomain dom
, HasCallStack
, NFDataX a )
=> Clock dom
-- ^ 'Clock' to synchronize to
-> Enable dom
-- ^ Global Enable
-> SNat n
-- ^ Number of elements in BRAM
-> a
-- ^ Initial content of the BRAM (replicated /n/ times)
-> Signal dom Int
-> Signal dom Bool
-- ^ Write enable
-> Signal dom Int
-> Signal dom a
-- ^ Value to write (at address @w@)
-> Signal dom a
-- ^ Value of the @blockRAM@ at address @r@ from the previous clock cycle
blockRam1# :: Clock dom
-> Enable dom
-> SNat n
-> a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
blockRam1# Clock dom
clk Enable dom
en SNat n
n a
a =
-- TODO: Generalize to single BRAM primitive taking an initialization function
Clock dom
-> Enable dom
-> Vec n a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
forall (dom :: Domain) a (n :: Nat).
(KnownDomain dom, HasCallStack, NFDataX a) =>
Clock dom
-> Enable dom
-> Vec n a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
blockRam# Clock dom
clk Enable dom
en (SNat n -> a -> Vec n a
forall (n :: Nat) a. SNat n -> a -> Vec n a
replicate SNat n
n a
a)
{-# NOINLINE blockRam1# #-}
{-# ANN blockRam1# hasBlackBox #-}

-- | blockRAM primitive
blockRam#
:: forall dom a n
. ( KnownDomain dom
, HasCallStack
, NFDataX a )
=> Clock dom
-- ^ 'Clock' to synchronize to
-> Enable dom
-- ^ Global enable
-> Vec n a
-- ^ Initial content of the BRAM, also
-- determines the size, @n@, of the BRAM.
--
-- __NB__: __MUST__ be a constant.
-> Signal dom Int
-> Signal dom Bool
-- ^ Write enable
-> Signal dom Int
-> Signal dom a
-- ^ Value to write (at address @w@)
-> Signal dom a
-- ^ Value of the @blockRAM@ at address @r@ from the previous clock cycle
blockRam# :: Clock dom
-> Enable dom
-> Vec n a
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> Signal dom a
blockRam# (Clock SSymbol dom
_) Enable dom
gen Vec n a
content = \Signal dom Int
rd Signal dom Bool
wen Signal dom Int
waS Signal dom a
wd -> (forall s. ST s (Signal dom a)) -> Signal dom a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Signal dom a)) -> Signal dom a)
-> (forall s. ST s (Signal dom a)) -> Signal dom a
forall a b. (a -> b) -> a -> b
\$ do
STArray s Int a
ramStart <- (Int, Int) -> [a] -> ST s (STArray s Int a)
forall (a :: Type -> Type -> Type) e (m :: Type -> Type) i.
(MArray a e m, Ix i) =>
(i, i) -> [e] -> m (a i e)
newListArray (Int
0,Int
szIInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) [a]
contentL
-- start benchmark only
-- ramStart <- unsafeThawSTArray ramArr
-- end benchmark only
STArray s Int a
-> a
-> Signal dom Bool
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> ST s (Signal dom a)
forall s.
STArray s Int a
-> a
-> Signal dom Bool
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> ST s (Signal dom a)
go
STArray s Int a
ramStart
((HasCallStack => a) -> a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack (String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX String
"blockRam: intial value undefined"))
(Enable dom -> Signal dom Bool
forall (dom :: Domain). Enable dom -> Signal dom Bool
fromEnable Enable dom
gen)
Signal dom Int
rd
(Enable dom -> Signal dom Bool
forall (dom :: Domain). Enable dom -> Signal dom Bool
fromEnable Enable dom
gen Signal dom Bool -> Signal dom Bool -> Signal dom Bool
forall (f :: Type -> Type).
Applicative f =>
f Bool -> f Bool -> f Bool
.&&. Signal dom Bool
wen)
Signal dom Int
waS
Signal dom a
wd
where
contentL :: [a]
contentL = Vec n a -> [a]
forall a b. a -> b
unsafeCoerce Vec n a
content :: [a]
szI :: Int
szI = [a] -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
L.length [a]
contentL
-- start benchmark only
-- ramArr = listArray (0,szI-1) contentL
-- end benchmark only

go :: STArray s Int a -> a -> Signal dom Bool -> Signal dom Int
-> Signal dom Bool -> Signal dom Int -> Signal dom a
-> ST s (Signal dom a)
go :: STArray s Int a
-> a
-> Signal dom Bool
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> ST s (Signal dom a)
go !STArray s Int a
ram a
o ret :: Signal dom Bool
ret@(~(Bool
re :- Signal dom Bool
res)) rt :: Signal dom Int
rt@(~(Int
r :- Signal dom Int
rs)) et :: Signal dom Bool
et@(~(Bool
e :- Signal dom Bool
en)) wt :: Signal dom Int
wt@(~(Int
w :- Signal dom Int
wr)) dt :: Signal dom a
dt@(~(a
d :- Signal dom a
din)) = do
a
o a -> ST s (Signal dom a) -> ST s (Signal dom a)
forall a b. a -> b -> b
`seqX` (a
o a -> Signal dom a -> Signal dom a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:-) (Signal dom a -> Signal dom a)
-> ST s (Signal dom a) -> ST s (Signal dom a)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> (Signal dom Bool
ret Signal dom Bool -> ST s (Signal dom a) -> ST s (Signal dom a)
`seq` Signal dom Int
rt Signal dom Int -> ST s (Signal dom a) -> ST s (Signal dom a)
`seq` Signal dom Bool
et Signal dom Bool -> ST s (Signal dom a) -> ST s (Signal dom a)
`seq` Signal dom Int
wt Signal dom Int -> ST s (Signal dom a) -> ST s (Signal dom a)
`seq` Signal dom a
dt Signal dom a -> ST s (Signal dom a) -> ST s (Signal dom a)
`seq`
ST s (Signal dom a) -> ST s (Signal dom a)
forall s a. ST s a -> ST s a
unsafeInterleaveST
(do a
o' <- IO a -> ST s a
forall a s. IO a -> ST s a
unsafeIOToST
(IO a -> (XException -> IO a) -> IO a
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch (if Bool
re then ST s a -> IO a
forall s a. ST s a -> IO a
unsafeSTToIO (STArray s Int a
ram STArray s Int a -> Int -> ST s a
forall s. HasCallStack => STArray s Int a -> Int -> ST s a
`safeAt` Int
r) else a -> IO a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure a
o)
(\err :: XException
err@XException {} -> a -> IO a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (XException -> a
forall a e. Exception e => e -> a
throw XException
err)))
a
d a -> ST s () -> ST s ()
forall a b. NFDataX a => a -> b -> b
`defaultSeqX` STArray s Int a -> Bool -> Int -> a -> ST s ()
forall s. STArray s Int a -> Bool -> Int -> a -> ST s ()
upd STArray s Int a
ram Bool
e (Int -> Int
forall a. Enum a => a -> Int
w) a
d
STArray s Int a
-> a
-> Signal dom Bool
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> ST s (Signal dom a)
forall s.
STArray s Int a
-> a
-> Signal dom Bool
-> Signal dom Int
-> Signal dom Bool
-> Signal dom Int
-> Signal dom a
-> ST s (Signal dom a)
go STArray s Int a
ram a
o' Signal dom Bool
res Signal dom Int
rs Signal dom Bool
en Signal dom Int
wr Signal dom a
din))

upd :: STArray s Int a -> Bool -> Int -> a -> ST s ()
upd :: STArray s Int a -> Bool -> Int -> a -> ST s ()
upd STArray s Int a
ram Bool
we Int
d = case Bool -> Maybe Bool
forall a. a -> Maybe a
maybeIsX Bool
we of
Maybe Bool
Nothing -> case Int -> Maybe Int
forall a. a -> Maybe a
maybeIsX Int
Maybe Int
Nothing -> -- Put the XException from `waddr` as the value in all
-- locations of `ram`.
[Int] -> (Int -> ST s ()) -> ST s ()
forall (t :: Type -> Type) (m :: Type -> Type) a b.
t a -> (a -> m b) -> m ()
forM_ [Int
0..(Int
szIInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)] (\Int
i -> STArray s Int a -> Int -> a -> ST s ()
forall s i e. STArray s i e -> Int -> e -> ST s ()
unsafeWriteSTArray STArray s Int a
ram Int
i (Int -> a -> a
seq Int
d))
Just Int
wa -> -- Put the XException from `we` as the value at address
Int -> a -> STArray s Int a -> ST s ()
forall s. HasCallStack => Int -> a -> STArray s Int a -> ST s ()
safeUpdate Int
wa (Bool -> a -> a
seq Bool
we a
d) STArray s Int a
ram
Just Bool
True -> case Int -> Maybe Int
forall a. a -> Maybe a
maybeIsX Int
Maybe Int
Nothing -> -- Put the XException from `waddr` as the value in all
-- locations of `ram`.
[Int] -> (Int -> ST s ()) -> ST s ()
forall (t :: Type -> Type) (m :: Type -> Type) a b.
t a -> (a -> m b) -> m ()
forM_ [Int
0..(Int
szIInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)] (\Int
i -> STArray s Int a -> Int -> a -> ST s ()
forall s i e. STArray s i e -> Int -> e -> ST s ()
unsafeWriteSTArray STArray s Int a
ram Int
i (Int -> a -> a
seq Int
d))
Just Int
wa -> Int -> a -> STArray s Int a -> ST s ()
forall s. HasCallStack => Int -> a -> STArray s Int a -> ST s ()
safeUpdate Int
wa a
d STArray s Int a
ram
Maybe Bool
_ -> () -> ST s ()
forall (m :: Type -> Type) a. Monad m => a -> m a
return ()

safeAt :: HasCallStack => STArray s Int a -> Int -> ST s a
safeAt :: STArray s Int a -> Int -> ST s a
safeAt STArray s Int a
s Int
i =
if (Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
i) Bool -> Bool -> Bool
&& (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
szI) then
STArray s Int a -> Int -> ST s a
forall s i e. STArray s i e -> Int -> ST s e
s Int
i
else a -> ST s a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (a -> ST s a) -> a -> ST s a
forall a b. (a -> b) -> a -> b
\$
(HasCallStack => a) -> a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack
(String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX (String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
i String -> String -> String
forall a. Semigroup a => a -> a -> a
<>
String
" not in range [0.." String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
szI String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
")"))
{-# INLINE safeAt #-}

safeUpdate :: HasCallStack => Int -> a -> STArray s Int a -> ST s ()
safeUpdate :: Int -> a -> STArray s Int a -> ST s ()
safeUpdate Int
i a
a STArray s Int a
s =
if (Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
i) Bool -> Bool -> Bool
&& (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
szI) then
STArray s Int a -> Int -> a -> ST s ()
forall s i e. STArray s i e -> Int -> e -> ST s ()
unsafeWriteSTArray STArray s Int a
s Int
i a
a
else
let d :: a
d = (HasCallStack => a) -> a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack
(String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX (String
"blockRam: write address " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
i String -> String -> String
forall a. Semigroup a => a -> a -> a
<>
String
" not in range [0.." String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
szI String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
")"))
in [Int] -> (Int -> ST s ()) -> ST s ()
forall (t :: Type -> Type) (m :: Type -> Type) a b.
t a -> (a -> m b) -> m ()
forM_ [Int
0..(Int
szIInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)] (\Int
j -> STArray s Int a -> Int -> a -> ST s ()
forall s i e. STArray s i e -> Int -> e -> ST s ()
unsafeWriteSTArray STArray s Int a
s Int
j a
d)
{-# INLINE safeUpdate #-}
{-# ANN blockRam# hasBlackBox #-}
{-# NOINLINE blockRam# #-}

:: ( KnownDomain dom
, NFDataX a
=> Clock dom
-> Reset dom
-> Enable dom
-> (Signal dom addr -> Signal dom (Maybe (addr, a)) -> Signal dom a)
-- ^ The @ram@ component
-> Signal dom (Maybe (addr, a))
-- ^ (Write address @w@, value to write)
-> Signal dom a
-- ^ Value of the @ram@ at address @r@ from the previous clock cycle
-> Reset dom
-> Enable dom
-> Signal dom (Maybe (addr, a)) -> Signal dom a)
-> Signal dom (Maybe (addr, a))
-> Signal dom a
clk Reset dom
rst Enable dom
en Signal dom addr -> Signal dom (Maybe (addr, a)) -> Signal dom a
wrM = Signal dom Bool -> Signal dom a -> Signal dom a -> Signal dom a
forall (f :: Type -> Type) a.
Applicative f =>
f Bool -> f a -> f a -> f a
mux Signal dom Bool
wasSame Signal dom a
wasWritten (Signal dom a -> Signal dom a) -> Signal dom a -> Signal dom a
forall a b. (a -> b) -> a -> b
\$ Signal dom addr -> Signal dom (Maybe (addr, a)) -> Signal dom a
wrM
where readNewT :: a -> Maybe (a, b) -> (Bool, b)
rd (Just (a
wr, b
wrdata)) = (a
wr a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
rd, b
wrdata)
_  Maybe (a, b)
Nothing             = (Bool
False   , b
forall a. HasCallStack => a
undefined)

(Signal dom Bool
wasSame,Signal dom a
wasWritten) =
Signal dom (Bool, a) -> Unbundled dom (Bool, a)
forall a (dom :: Domain).
Bundle a =>
Signal dom a -> Unbundled dom a
unbundle (Clock dom
-> Reset dom
-> Enable dom
-> (Bool, a)
-> Signal dom (Bool, a)
-> Signal dom (Bool, a)
forall (dom :: Domain) a.
(KnownDomain dom, NFDataX a) =>
Clock dom
-> Reset dom -> Enable dom -> a -> Signal dom a -> Signal dom a
register Clock dom
clk Reset dom
rst Enable dom
en (Bool
False, a
forall a. HasCallStack => a
undefined)
forall a b. Eq a => a -> Maybe (a, b) -> (Bool, b)
-> Signal dom addr -> Signal dom (Maybe (addr, a) -> (Bool, a))
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
-> Signal dom (Maybe (addr, a)) -> Signal dom (Bool, a)
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> Signal dom (Maybe (addr, a))
wrM))

-- | Port operation
data RamOp n a
| RamWrite (Index n) a
-- ^ Write data to address
| RamNoOp
-- ^ No operation
deriving ((forall x. RamOp n a -> Rep (RamOp n a) x)
-> (forall x. Rep (RamOp n a) x -> RamOp n a)
-> Generic (RamOp n a)
forall x. Rep (RamOp n a) x -> RamOp n a
forall x. RamOp n a -> Rep (RamOp n a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (n :: Nat) a x. Rep (RamOp n a) x -> RamOp n a
forall (n :: Nat) a x. RamOp n a -> Rep (RamOp n a) x
\$cto :: forall (n :: Nat) a x. Rep (RamOp n a) x -> RamOp n a
\$cfrom :: forall (n :: Nat) a x. RamOp n a -> Rep (RamOp n a) x
Generic, HasCallStack => String -> RamOp n a
RamOp n a -> Bool
RamOp n a -> ()
RamOp n a -> RamOp n a
(HasCallStack => String -> RamOp n a)
-> (RamOp n a -> Bool)
-> (RamOp n a -> RamOp n a)
-> (RamOp n a -> ())
-> NFDataX (RamOp n a)
forall a.
(HasCallStack => String -> a)
-> (a -> Bool) -> (a -> a) -> (a -> ()) -> NFDataX a
forall (n :: Nat) a.
(NFDataX a, HasCallStack) =>
String -> RamOp n a
forall (n :: Nat) a. NFDataX a => RamOp n a -> Bool
forall (n :: Nat) a. NFDataX a => RamOp n a -> ()
forall (n :: Nat) a. NFDataX a => RamOp n a -> RamOp n a
rnfX :: RamOp n a -> ()
\$crnfX :: forall (n :: Nat) a. NFDataX a => RamOp n a -> ()
ensureSpine :: RamOp n a -> RamOp n a
\$censureSpine :: forall (n :: Nat) a. NFDataX a => RamOp n a -> RamOp n a
hasUndefined :: RamOp n a -> Bool
\$chasUndefined :: forall (n :: Nat) a. NFDataX a => RamOp n a -> Bool
deepErrorX :: String -> RamOp n a
\$cdeepErrorX :: forall (n :: Nat) a.
(NFDataX a, HasCallStack) =>
String -> RamOp n a
NFDataX, Int -> RamOp n a -> String -> String
[RamOp n a] -> String -> String
RamOp n a -> String
(Int -> RamOp n a -> String -> String)
-> (RamOp n a -> String)
-> ([RamOp n a] -> String -> String)
-> Show (RamOp n a)
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
forall (n :: Nat) a. Show a => Int -> RamOp n a -> String -> String
forall (n :: Nat) a. Show a => [RamOp n a] -> String -> String
forall (n :: Nat) a. Show a => RamOp n a -> String
showList :: [RamOp n a] -> String -> String
\$cshowList :: forall (n :: Nat) a. Show a => [RamOp n a] -> String -> String
show :: RamOp n a -> String
\$cshow :: forall (n :: Nat) a. Show a => RamOp n a -> String
showsPrec :: Int -> RamOp n a -> String -> String
\$cshowsPrec :: forall (n :: Nat) a. Show a => Int -> RamOp n a -> String -> String
Show)

ramOpAddr :: RamOp n a -> Index n
ramOpAddr :: RamOp n a -> Index n
_) = Index n
RamNoOp           = String -> Index n
forall a. HasCallStack => String -> a
errorX String

isRamWrite :: RamOp n a -> Bool
isRamWrite :: RamOp n a -> Bool
isRamWrite (RamWrite {}) = Bool
True
isRamWrite RamOp n a
_             = Bool
False

ramOpWriteVal :: RamOp n a -> Maybe a
ramOpWriteVal :: RamOp n a -> Maybe a
ramOpWriteVal (RamWrite Index n
_ a
val) = a -> Maybe a
forall a. a -> Maybe a
Just a
val
ramOpWriteVal RamOp n a
_                = Maybe a
forall a. Maybe a
Nothing

isOp :: RamOp n a -> Bool
isOp :: RamOp n a -> Bool
isOp RamOp n a
RamNoOp = Bool
False
isOp RamOp n a
_       = Bool
True

-- | Produces vendor-agnostic HDL that will be inferred as a true dual-port
-- block RAM
--
-- Any value that is being written on a particular port is also the
-- value that will be read on that port, i.e. the same-port read/write behavior
-- is: WriteFirst. For mixed-port read/write, when port A writes to the address
-- port B reads from, the output of port B is undefined, and vice versa.
trueDualPortBlockRam ::
forall nAddrs domA domB a .
( HasCallStack
, KnownDomain domA
, KnownDomain domB
, NFDataX a
)
=> Clock domA
-- ^ Clock for port A
-> Clock domB
-- ^ Clock for port B
-> Signal domA (RamOp nAddrs a)
-- ^ RAM operation for port A
-> Signal domB (RamOp nAddrs a)
-- ^ RAM operation for port B
-> (Signal domA a, Signal domB a)
-- ^ Outputs data on /next/ cycle. When writing, the data written

{-# INLINE trueDualPortBlockRam #-}
trueDualPortBlockRam :: Clock domA
-> Clock domB
-> Signal domA (RamOp nAddrs a)
-> Signal domB (RamOp nAddrs a)
-> (Signal domA a, Signal domB a)
trueDualPortBlockRam = \Clock domA
clkA Clock domB
clkB Signal domA (RamOp nAddrs a)
opA Signal domB (RamOp nAddrs a)
opB ->
Clock domA
-> Signal domA Bool
-> Signal domA Bool
-> Signal domA a
-> Clock domB
-> Signal domB Bool
-> Signal domB Bool
-> Signal domB a
-> (Signal domA a, Signal domB a)
forall (nAddrs :: Nat) (domA :: Domain) (domB :: Domain) a.
(HasCallStack, KnownNat nAddrs, KnownDomain domA, KnownDomain domB,
NFDataX a) =>
Clock domA
-> Signal domA Bool
-> Signal domA Bool
-> Signal domA a
-> Clock domB
-> Signal domB Bool
-> Signal domB Bool
-> Signal domB a
-> (Signal domA a, Signal domB a)
trueDualPortBlockRamWrapper
Clock domA
clkA (RamOp nAddrs a -> Bool
forall (n :: Nat) a. RamOp n a -> Bool
isOp (RamOp nAddrs a -> Bool)
-> Signal domA (RamOp nAddrs a) -> Signal domA Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal domA (RamOp nAddrs a)
opA) (RamOp nAddrs a -> Bool
forall (n :: Nat) a. RamOp n a -> Bool
isRamWrite (RamOp nAddrs a -> Bool)
-> Signal domA (RamOp nAddrs a) -> Signal domA Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal domA (RamOp nAddrs a)
forall (n :: Nat) a. RamOp n a -> Index n
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal domA (RamOp nAddrs a)
opA) (Maybe a -> a
forall a. HasCallStack => Maybe a -> a
fromJustX (Maybe a -> a)
-> (RamOp nAddrs a -> Maybe a) -> RamOp nAddrs a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RamOp nAddrs a -> Maybe a
forall (n :: Nat) a. RamOp n a -> Maybe a
ramOpWriteVal (RamOp nAddrs a -> a)
-> Signal domA (RamOp nAddrs a) -> Signal domA a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal domA (RamOp nAddrs a)
opA)
Clock domB
clkB (RamOp nAddrs a -> Bool
forall (n :: Nat) a. RamOp n a -> Bool
isOp (RamOp nAddrs a -> Bool)
-> Signal domB (RamOp nAddrs a) -> Signal domB Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal domB (RamOp nAddrs a)
opB) (RamOp nAddrs a -> Bool
forall (n :: Nat) a. RamOp n a -> Bool
isRamWrite (RamOp nAddrs a -> Bool)
-> Signal domB (RamOp nAddrs a) -> Signal domB Bool
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal domB (RamOp nAddrs a)
forall (n :: Nat) a. RamOp n a -> Index n
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal domB (RamOp nAddrs a)
opB) (Maybe a -> a
forall a. HasCallStack => Maybe a -> a
fromJustX (Maybe a -> a)
-> (RamOp nAddrs a -> Maybe a) -> RamOp nAddrs a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RamOp nAddrs a -> Maybe a
forall (n :: Nat) a. RamOp n a -> Maybe a
ramOpWriteVal (RamOp nAddrs a -> a)
-> Signal domB (RamOp nAddrs a) -> Signal domB a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<\$> Signal domB (RamOp nAddrs a)
opB)

toMaybeX :: a -> MaybeX a
toMaybeX :: a -> MaybeX a
toMaybeX a
a =
case a -> Either String a
forall a. a -> Either String a
isX a
a of
Left String
_ -> MaybeX a
forall a. MaybeX a
IsX
Right a
_ -> a -> MaybeX a
forall a. a -> MaybeX a
IsDefined a
a

data MaybeX a = IsX | IsDefined !a

data Conflict = Conflict
{ Conflict -> MaybeX Bool
cfWrite :: !(MaybeX Bool)
, Conflict -> MaybeX Int

instance Semigroup Conflict where
<> :: Conflict -> Conflict -> Conflict
(<>) = Conflict -> Conflict -> Conflict
mergeConflicts

-- | "Stronger" conflict wins:
--
--   * Undefineds over anything
--
mergeConflicts :: Conflict -> Conflict -> Conflict
mergeConflicts :: Conflict -> Conflict -> Conflict
mergeConflicts Conflict
conflict1 Conflict
conflict2 = Conflict :: MaybeX Bool -> MaybeX Int -> Conflict
Conflict
{ cfWrite :: MaybeX Bool
cfWrite = MaybeX Bool -> MaybeX Bool -> MaybeX Bool
mergeWrite (Conflict -> MaybeX Bool
cfWrite Conflict
conflict1) (Conflict -> MaybeX Bool
cfWrite Conflict
conflict2)
cfAddress = MaybeX Int -> MaybeX Int -> MaybeX Int
forall a b. MaybeX a -> MaybeX b -> MaybeX a
conflict1) (Conflict -> MaybeX Int
conflict2) }
where
mergeX :: (t -> t -> a) -> MaybeX t -> MaybeX t -> MaybeX a
mergeX t -> t -> a
_ MaybeX t
IsX MaybeX t
_ = MaybeX a
forall a. MaybeX a
IsX
mergeX t -> t -> a
_ MaybeX t
_ MaybeX t
IsX = MaybeX a
forall a. MaybeX a
IsX
mergeX t -> t -> a
f (IsDefined t
a) (IsDefined t
b) = a -> MaybeX a
forall a. a -> MaybeX a
IsDefined (t -> t -> a
f t
a t
b)

mergeWrite :: MaybeX Bool -> MaybeX Bool -> MaybeX Bool
mergeWrite MaybeX Bool
a MaybeX Bool
b = (Bool -> Bool -> Bool) -> MaybeX Bool -> MaybeX Bool -> MaybeX Bool
forall t t a. (t -> t -> a) -> MaybeX t -> MaybeX t -> MaybeX a
mergeX Bool -> Bool -> Bool
(||) MaybeX Bool
a MaybeX Bool
b
mergeAddress :: MaybeX a -> MaybeX b -> MaybeX a
a MaybeX b
b = (a -> b -> a) -> MaybeX a -> MaybeX b -> MaybeX a
forall t t a. (t -> t -> a) -> MaybeX t -> MaybeX t -> MaybeX a
mergeX a -> b -> a
forall a b. a -> b -> a
const MaybeX a
a MaybeX b
b

-- [Note: eta port names for trueDualPortBlockRam]
--
-- By naming all the arguments and setting the -fno-do-lambda-eta-expansion GHC
-- option for this module, the generated HDL also contains names based on the
-- argument names used here. This greatly improves readability of the HDL.

-- [Note: true dual-port blockRAM separate architecture]
--
-- A multi-clock true dual-port block RAM is only inferred from the generated HDL
-- when it lives in its own Verilog module / VHDL architecture. Add any other
-- logic to the module / architecture, and synthesis will no longer infer a
-- multi-clock true dual-port block RAM. This wrapper pushes the primitive out
-- into its own module / architecture.
trueDualPortBlockRamWrapper :: Clock domA
-> Signal domA Bool
-> Signal domA Bool
-> Signal domA a
-> Clock domB
-> Signal domB Bool
-> Signal domB Bool
-> Signal domB a
-> (Signal domA a, Signal domB a)
trueDualPortBlockRamWrapper Clock domA
clkA Signal domA Bool
enA Signal domA Bool
datA Clock domB
clkB Signal domB Bool
enB Signal domB Bool
datB =
Clock domA
-> Signal domA Bool
-> Signal domA Bool
-> Signal domA a
-> Clock domB
-> Signal domB Bool
-> Signal domB Bool
-> Signal domB a
-> (Signal domA a, Signal domB a)
forall (nAddrs :: Nat) (domA :: Domain) (domB :: Domain) a.
(HasCallStack, KnownNat nAddrs, KnownDomain domA, KnownDomain domB,
NFDataX a) =>
Clock domA
-> Signal domA Bool
-> Signal domA Bool
-> Signal domA a
-> Clock domB
-> Signal domB Bool
-> Signal domB Bool
-> Signal domB a
-> (Signal domA a, Signal domB a)
trueDualPortBlockRam# Clock domA
clkA Signal domA Bool
enA Signal domA Bool
datA Clock domB
clkB Signal domB Bool
enB Signal domB Bool
datB
{-# NOINLINE trueDualPortBlockRamWrapper #-}

-- | Primitive of 'trueDualPortBlockRam'.
trueDualPortBlockRam#, trueDualPortBlockRamWrapper ::
forall nAddrs domA domB a .
( HasCallStack
, KnownDomain domA
, KnownDomain domB
, NFDataX a
)
=> Clock domA
-- ^ Clock for port A
-> Signal domA Bool
-- ^ Enable for port A
-> Signal domA Bool
-- ^ Write enable for port A
-- ^ Address to read from or write to on port A
-> Signal domA a
-- ^ Data in for port A; ignored when /write enable/ is @False@

-> Clock domB
-- ^ Clock for port B
-> Signal domB Bool
-- ^ Enable for port B
-> Signal domB Bool
-- ^ Write enable for port B
-- ^ Address to read from or write to on port B
-> Signal domB a
-- ^ Data in for port B; ignored when /write enable/ is @False@

-> (Signal domA a, Signal domB a)
-- ^ Outputs data on /next/ cycle. If write enable is @True@, the data written
-- will be echoed. If write enable is @False@, the read data is returned. If
-- port enable is @False@, it is /undefined/.
trueDualPortBlockRam# :: Clock domA
-> Signal domA Bool
-> Signal domA Bool
-> Signal domA a
-> Clock domB
-> Signal domB Bool
-> Signal domB Bool
-> Signal domB a
-> (Signal domA a, Signal domB a)
trueDualPortBlockRam# Clock domA
clkA Signal domA Bool
enA Signal domA Bool
datA Clock domB
clkB Signal domB Bool
enB Signal domB Bool
datB
| SNat (DomainConfigurationPeriod (KnownConf domA)) -> Int
forall a (n :: Nat). Num a => SNat n -> a
snatToNum @Int (forall (period :: Nat).
(KnownDomain domA,
DomainConfigurationPeriod (KnownConf domA) ~ period) =>
SNat period
forall (dom :: Domain) (period :: Nat).
(KnownDomain dom, DomainPeriod dom ~ period) =>
SNat period
clockPeriod @domA) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< SNat (DomainConfigurationPeriod (KnownConf domB)) -> Int
forall a (n :: Nat). Num a => SNat n -> a
snatToNum @Int (forall (period :: Nat).
(KnownDomain domB,
DomainConfigurationPeriod (KnownConf domB) ~ period) =>
SNat period
forall (dom :: Domain) (period :: Nat).
(KnownDomain dom, DomainPeriod dom ~ period) =>
SNat period
clockPeriod @domB)
= (Signal domB a, Signal domA a) -> (Signal domA a, Signal domB a)
forall a b. (a, b) -> (b, a)
swap (Clock domB
-> Signal domB Bool
-> Signal domB Bool
-> Signal domB a
-> Clock domA
-> Signal domA Bool
-> Signal domA Bool
-> Signal domA a
-> (Signal domB a, Signal domA a)
forall (nAddrs :: Nat) (domFast :: Domain) (domSlow :: Domain) a.
KnownDomain domFast, NFDataX a) =>
Clock domSlow
-> Signal domSlow Bool
-> Signal domSlow Bool
-> Signal domSlow a
-> Clock domFast
-> Signal domFast Bool
-> Signal domFast Bool
-> Signal domFast a
-> (Signal domSlow a, Signal domFast a)
trueDualPortBlockRamModel Clock domB
clkB Signal domB Bool
enB Signal domB Bool
datB Clock domA
clkA Signal domA Bool
enA Signal domA Bool
datA)
| Bool
otherwise
=       Clock domA
-> Signal domA Bool
-> Signal domA Bool
-> Signal domA a
-> Clock domB
-> Signal domB Bool
-> Signal domB Bool
-> Signal domB a
-> (Signal domA a, Signal domB a)
forall (nAddrs :: Nat) (domFast :: Domain) (domSlow :: Domain) a.
KnownDomain domFast, NFDataX a) =>
Clock domSlow
-> Signal domSlow Bool
-> Signal domSlow Bool
-> Signal domSlow a
-> Clock domFast
-> Signal domFast Bool
-> Signal domFast Bool
-> Signal domFast a
-> (Signal domSlow a, Signal domFast a)
trueDualPortBlockRamModel Clock domA
clkA Signal domA Bool
enA Signal domA Bool
datA Clock domB
clkB Signal domB Bool
enB Signal domB Bool
datB
{-# NOINLINE trueDualPortBlockRam# #-}
{-# ANN trueDualPortBlockRam# hasBlackBox #-}

-- | Haskell model for the primitive 'trueDualPortBlockRam#'.
--
-- Warning: this model only works if @domFast@'s clock is faster (or equal to)
-- @domSlow@'s clock.
trueDualPortBlockRamModel ::
forall nAddrs domFast domSlow a .
( HasCallStack
, KnownDomain domSlow
, KnownDomain domFast
, NFDataX a
) =>

Clock domSlow ->
Signal domSlow Bool ->
Signal domSlow Bool ->
Signal domSlow a ->

Clock domFast ->
Signal domFast Bool ->
Signal domFast Bool ->
Signal domFast a ->

(Signal domSlow a, Signal domFast a)
trueDualPortBlockRamModel :: Clock domSlow
-> Signal domSlow Bool
-> Signal domSlow Bool
-> Signal domSlow a
-> Clock domFast
-> Signal domFast Bool
-> Signal domFast Bool
-> Signal domFast a
-> (Signal domSlow a, Signal domFast a)
trueDualPortBlockRamModel !Clock domSlow
_clkA Signal domSlow Bool
enA Signal domSlow Bool
datA !Clock domFast
_clkB Signal domFast Bool
enB Signal domFast Bool
datB =
( String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX String
"trueDualPortBlockRam: Port A: First value undefined" a -> Signal domSlow a -> Signal domSlow a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- Signal domSlow a
outA
, String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX String
"trueDualPortBlockRam: Port B: First value undefined" a -> Signal domFast a -> Signal domFast a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- Signal domFast a
outB )
where
(Signal domSlow a
outA, Signal domFast a
outB) =
Maybe Conflict
-> Seq a
-> Int
-> Signal domSlow (Bool, Bool, Int, a)
-> Signal domFast (Bool, Bool, Int, a)
-> (Signal domSlow a, Signal domFast a)
go
Maybe Conflict
forall a. Maybe a
Nothing
(Int -> (Int -> a) -> Seq a
forall a. Int -> (Int -> a) -> Seq a
Seq.fromFunction (forall a. (Num a, KnownNat nAddrs) => a
forall (n :: Nat) a. (Num a, KnownNat n) => a
initElement)
Int
tA -- ensure 'go' hits fast clock first
(Unbundled domSlow (Bool, Bool, Int, a)
-> Signal domSlow (Bool, Bool, Int, a)
forall a (dom :: Domain).
Bundle a =>
Unbundled dom a -> Signal dom a
bundle (Signal domSlow Bool
enA, Signal domSlow Bool
forall a b. (Integral a, Num b) => a -> b
-> Signal domSlow (Index nAddrs) -> Signal domSlow Integer
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
datA))
(Unbundled domFast (Bool, Bool, Int, a)
-> Signal domFast (Bool, Bool, Int, a)
forall a (dom :: Domain).
Bundle a =>
Unbundled dom a -> Signal dom a
bundle (Signal domFast Bool
enB, Signal domFast Bool
forall a b. (Integral a, Num b) => a -> b
-> Signal domFast (Index nAddrs) -> Signal domFast Integer
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
datB))

tA :: Int
tA = SNat (DomainConfigurationPeriod (KnownConf domSlow)) -> Int
forall a (n :: Nat). Num a => SNat n -> a
snatToNum @Int (forall (period :: Nat).
(KnownDomain domSlow,
DomainConfigurationPeriod (KnownConf domSlow) ~ period) =>
SNat period
forall (dom :: Domain) (period :: Nat).
(KnownDomain dom, DomainPeriod dom ~ period) =>
SNat period
clockPeriod @domSlow)
tB :: Int
tB = SNat (DomainConfigurationPeriod (KnownConf domFast)) -> Int
forall a (n :: Nat). Num a => SNat n -> a
snatToNum @Int (forall (period :: Nat).
(KnownDomain domFast,
DomainConfigurationPeriod (KnownConf domFast) ~ period) =>
SNat period
forall (dom :: Domain) (period :: Nat).
(KnownDomain dom, DomainPeriod dom ~ period) =>
SNat period
clockPeriod @domFast)

initElement :: Int -> a
initElement :: Int -> a
initElement Int
n =
String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX (String
"Unknown initial element; position " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
n)

n =
String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX (String
"Write enable and data unknown; position " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
n)

n =
String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX (String
"Write enabled, but address unknown; position " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
n)

getConflict :: Bool -> Int -> Bool -> Int -> Maybe Conflict
getConflict :: Bool -> Int -> Bool -> Int -> Maybe Conflict
getConflict Bool
enableA Int
enableB Int
-- If port A or port B is writing on (potentially!) the same address,
-- there's a conflict
if Bool
maybeConflict else Maybe Conflict
forall a. Maybe a
Nothing
where
conflict :: Conflict
conflict = Conflict :: MaybeX Bool -> MaybeX Int -> Conflict
Conflict
{ cfWrite :: MaybeX Bool
cfWrite = Bool -> MaybeX Bool
forall a. a -> MaybeX a
toMaybeX Bool
enableA
cfAddress = Int -> MaybeX Int
forall a. a -> MaybeX a
toMaybeX Int

maybeConflict :: Maybe Conflict
maybeConflict =
case (Bool -> Either String Bool
forall a. a -> Either String a
isX Bool
enableA, Bool -> Either String Bool
forall a. a -> Either String a
isX Bool
enableB) of
(Left String
_, Either String Bool
_)     -> Conflict -> Maybe Conflict
forall a. a -> Maybe a
Just Conflict
conflict
(Right Bool
True, Either String Bool
_) -> Conflict -> Maybe Conflict
forall a. a -> Maybe a
Just Conflict
conflict
(Either String Bool
_, Left String
_)     -> Conflict -> Maybe Conflict
forall a. a -> Maybe a
Just Conflict
conflict
(Either String Bool
_, Right Bool
True) -> Conflict -> Maybe Conflict
forall a. a -> Maybe a
Just Conflict
conflict
(Either String Bool, Either String Bool)
_               -> Maybe Conflict
forall a. Maybe a
Nothing

case (Int -> Either String Int
forall a. a -> Either String a
isX Int
addrA_, Int -> Either String Int
forall a. a -> Either String a
isX Int
(Left String
_, Either String Int
_) -> Bool
True
(Either String Int
_, Left String
_) -> Bool
True
(Either String Int, Either String Int)
_           -> Int
addrA_ Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int

writeRam :: Bool -> Int -> a -> Seq a -> (Maybe a, Seq a)
writeRam :: Bool -> Int -> a -> Seq a -> (Maybe a, Seq a)
writeRam Bool
enable Int
dat Seq a
mem
| Bool
enableUndefined Bool -> Bool -> Bool
&& Bool
= ( a -> Maybe a
forall a. a -> Maybe a
Just (String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX String
, Int -> (Int -> a) -> Seq a
forall a. Int -> (Int -> a) -> Seq a
Seq.fromFunction (forall a. (Num a, KnownNat nAddrs) => a
forall (n :: Nat) a. (Num a, KnownNat n) => a
| Bool
= ( a -> Maybe a
forall a. a -> Maybe a
Just (String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX String
, Int -> (Int -> a) -> Seq a
forall a. Int -> (Int -> a) -> Seq a
Seq.fromFunction (forall a. (Num a, KnownNat nAddrs) => a
forall (n :: Nat) a. (Num a, KnownNat n) => a
| Bool
enableUndefined
= Bool -> Int -> a -> Seq a -> (Maybe a, Seq a)
writeRam Bool
True Int
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX (String
"Write unknown; position" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
mem
| Bool
enable
= (a -> Maybe a
forall a. a -> Maybe a
Just a
dat, Int -> a -> Seq a -> Seq a
forall a. Int -> a -> Seq a -> Seq a
Seq.update Int
dat Seq a
mem)
| Bool
otherwise
= (Maybe a
forall a. Maybe a
Nothing, Seq a
mem)
where
enableUndefined :: Bool
enableUndefined = Either String Bool -> Bool
forall a b. Either a b -> Bool
isLeft (Bool -> Either String Bool
forall a. a -> Either String a
isX Bool
enable)
addrUndefined = Either String Int -> Bool
forall a b. Either a b -> Bool
isLeft (Int -> Either String Int
forall a. a -> Either String a
isX Int

go ::
Maybe Conflict ->
Seq a ->
Int ->
Signal domSlow (Bool, Bool, Int, a) ->
Signal domFast (Bool, Bool, Int, a) ->
(Signal domSlow a, Signal domFast a)
go :: Maybe Conflict
-> Seq a
-> Int
-> Signal domSlow (Bool, Bool, Int, a)
-> Signal domFast (Bool, Bool, Int, a)
-> (Signal domSlow a, Signal domFast a)
go Maybe Conflict
conflict0 Seq a
ram0 Int
relativeTime Signal domSlow (Bool, Bool, Int, a)
as0 Signal domFast (Bool, Bool, Int, a)
bs0 =
if Int
relativeTime Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 then (Signal domSlow a, Signal domFast a)
goSlow else (Signal domSlow a, Signal domFast a)
goFast
where
(Bool
enA_, Bool
weA_, Int
datA_) :- Signal domSlow (Bool, Bool, Int, a)
as1 = Signal domSlow (Bool, Bool, Int, a)
as0
(Bool
enB_, Bool
weB_, Int
datB_) :- Signal domFast (Bool, Bool, Int, a)
bs1 = Signal domFast (Bool, Bool, Int, a)
bs0

-- 1 iteration here, as this is the slow clock.
goSlow :: (Signal domSlow a, Signal domFast a)
goSlow = a
out1 a
-> (Signal domSlow a, Signal domFast a)
-> (Signal domSlow a, Signal domFast a)
forall a b. a -> b -> b
`seqX` (a
out1 a -> Signal domSlow a -> Signal domSlow a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- Signal domSlow a
as2, Signal domFast a
bs2)
where
(Maybe a
wrote, !Seq a
ram1) = Bool -> Int -> a -> Seq a -> (Maybe a, Seq a)
writeRam Bool
weA_ Int
datA_ Seq a
ram0
out0 :: a
out0 = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe (Seq a
ram1 Seq a -> Int -> a
forall a. Seq a -> Int -> a
`Seq.index` Int
wrote
(Signal domSlow a
as2, Signal domFast a
bs2) = Maybe Conflict
-> Seq a
-> Int
-> Signal domSlow (Bool, Bool, Int, a)
-> Signal domFast (Bool, Bool, Int, a)
-> (Signal domSlow a, Signal domFast a)
go Maybe Conflict
forall a. Maybe a
Nothing Seq a
ram1 (Int
relativeTime Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
tA) Signal domSlow (Bool, Bool, Int, a)
as1 Signal domFast (Bool, Bool, Int, a)
bs0
out1 :: a
out1 =
case Maybe Conflict
conflict0 of
Just Conflict{cfWrite :: Conflict -> MaybeX Bool
cfWrite=IsDefined Bool
True} ->
String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX String
Just Conflict{cfWrite :: Conflict -> MaybeX Bool
cfWrite=MaybeX Bool
IsX} ->
String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX String
Maybe Conflict
_ -> a
out0

-- 1 or more iterations here, as this is the fast clock. First iteration
-- happens here.
goFast :: (Signal domSlow a, Signal domFast a)
goFast = a
out1 a
-> (Signal domSlow a, Signal domFast a)
-> (Signal domSlow a, Signal domFast a)
forall a b. a -> b -> b
`seqX` (Signal domSlow a
as2, a
out1 a -> Signal domFast a -> Signal domFast a
forall (dom :: Domain) a. a -> Signal dom a -> Signal dom a
:- Signal domFast a
bs2)
where
conflict1 :: Maybe Conflict
conflict1 | Bool
enA_ Bool -> Bool -> Bool
&& Bool
enB_ = Bool -> Int -> Bool -> Int -> Maybe Conflict
getConflict Bool
weB_ Int
weA_ Int
| Bool
otherwise    = Maybe Conflict
forall a. Maybe a
Nothing
(Maybe a
wrote, !Seq a
ram1) = Bool -> Int -> a -> Seq a -> (Maybe a, Seq a)
writeRam Bool
weB_ Int
datB_ Seq a
ram0
out0 :: a
out0 = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe (Seq a
ram1 Seq a -> Int -> a
forall a. Seq a -> Int -> a
`Seq.index` Int
wrote
conflict2 :: Maybe Conflict
conflict2 = Maybe Conflict
conflict0 Maybe Conflict -> Maybe Conflict -> Maybe Conflict
forall a. Semigroup a => a -> a -> a
<> Maybe Conflict
conflict1
(Signal domSlow a
as2, Signal domFast a
bs2) = Maybe Conflict
-> Seq a
-> Int
-> Signal domSlow (Bool, Bool, Int, a)
-> Signal domFast (Bool, Bool, Int, a)
-> (Signal domSlow a, Signal domFast a)
go Maybe Conflict
conflict2 Seq a
ram1 (Int
relativeTime Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
tB) Signal domSlow (Bool, Bool, Int, a)
as0 Signal domFast (Bool, Bool, Int, a)
bs1
out1 :: a
out1 =
case Maybe Conflict
conflict1 of
Just Conflict{cfWrite :: Conflict -> MaybeX Bool
cfWrite=IsDefined Bool
False} ->
String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX String
Just Conflict{cfWrite :: Conflict -> MaybeX Bool
cfWrite=MaybeX Bool
IsX} ->
String -> a
forall a. (NFDataX a, HasCallStack) => String -> a
deepErrorX String
Maybe Conflict
_ ->
a
out0
```