Skip to content

Commit

Permalink
Use a CReg for Counter
Browse files Browse the repository at this point in the history
  • Loading branch information
krame505 authored and quark17 committed Feb 5, 2024
1 parent 4a2927a commit be5e071
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions Libraries/GenC/GenCMsg/GenCMsg.bs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ instance (FIFOs fifos rxBytes rxCount txBytes txCount,
rxMsgs.deq
let enq :: Integer -> ByteList -> Action
enq txTag txBody = do
mapM_ (\ cr -> cr.dec cr.value) rxCredits
mapM_ (.zero) rxCredits
let packedCredits :: Vector rxCount (Bit 8)
packedCredits = map (\ cr -> Prelude.pack cr.value) rxCredits
let packedTag :: Vector tagBytes (Bit 8)
Expand All @@ -120,7 +120,7 @@ instance (FIFOs fifos rxBytes rxCount txBytes txCount,

let handleCreditsOnly =
rules
"handle_tx_credits_only": when any (\ cr -> cr.isGreaterThan 0) rxCredits ==> enq 0 List.nil
"handle_tx_credits_only": when any (\ cr -> cr.value > 0) rxCredits ==> enq 0 List.nil
"handle_rx_credits_only": when rxTagEq 0 ==> deq
addRules $ rs `rJoinDescendingUrgency` handleCreditsOnly

Expand Down Expand Up @@ -186,27 +186,19 @@ interface Counter =
value :: UInt 8
inc :: UInt 8 -> Action
dec :: UInt 8 -> Action

isLessThan :: Integer -> Bool
isGreaterThan :: Integer -> Bool
zero :: Action

mkCounter :: Integer -> Module Counter
mkCounter init = module
value :: Reg (UInt 8) <- mkReg $ fromInteger init
inc :: Wire (UInt 8) <- mkDWire 0
dec :: Wire (UInt 8) <- mkDWire 0

rules
{-# ASSERT fire when enabled #-}
{-# ASSERT no implicit conditions #-}
"update": when True ==> value := value + inc - dec
value :: Vector 2 (Reg (UInt 8)) <- fmap arrayToVector $ mkCReg 2 $ fromInteger init
let a :: Reg (UInt 8) = value !! 0
b :: Reg (UInt 8) = value !! 1

interface
value = value
inc = inc._write
dec x = _when_ (x <= value) $ dec := x
isLessThan n = value < fromInteger n
isGreaterThan n = value > fromInteger n
value = b
inc x = a := a + x
dec x = _when_ (x <= b) $ b := b - x
zero = b := 0

class FIFOs fifos rxBytes rxCount txBytes txCount | fifos -> rxBytes rxCount txBytes txCount where
mkRxCredits :: fifos -> Module (Vector rxCount Counter)
Expand Down

0 comments on commit be5e071

Please sign in to comment.