notable refactor with grok

This commit is contained in:
Yehowshua Immanuel 2025-04-04 15:27:25 -04:00
parent ca59e6eaec
commit 020bc5b646

View file

@ -20,111 +20,84 @@ interface (TagEngine :: # -> *) numTags =
-- on a bus for example. -- on a bus for example.
-- This implementation is FIFO based. -- This implementation is FIFO based.
mkTagEngine :: Module (TagEngine numTags) mkTagEngine :: Module (TagEngine numTags)
mkTagEngine = mkTagEngine = do
do -- Constants
let reifiedNumTags = fromInteger |> valueOf numTags let maxTagCount = fromInteger (valueOf numTags)
-- we really only use tagUsageTracker after emptying out our -- State
-- `initialTagDistributor` buffer tagUsage :: Vector numTags (Reg Bool) <- replicateM (mkReg False) -- Tracks which tags are in use
tagUsageTracker :: Vector numTags (Reg Bool) initialTagCounter <- mkReg (Just (maxTagCount - 1)) -- Distributes initial tags
tagUsageTracker <- replicateM |> mkReg False retireQueue <- mkBypassFIFO -- Queue for tags being retired
freeTagQueue <- mkSizedFIFOF maxTagCount -- Queue of available tags
-- Since Bluespec doesn't allow us to initialize FIFOs with values at -- Signals
-- reset, we can pretend as if the buffer within our tagFIFO is populated retireSignal <- mkRWire -- Signals a tag retirement
-- with sequentially incrementing values(starting from 0) on reset requestSignal <- mkRWire -- Signals a tag request
-- by having our tag engine effectively return the value of a decrementing
-- counter initialized to (numTags - 1) for the first n tag requests made
-- to TagEngine where `n := numTags`.
initialTagDistributor :: (Reg (Maybe UIntLog2N(numTags)))
initialTagDistributor <- mkReg |> Just |> reifiedNumTags - 1
validatedTagToRetire :: FIFO UIntLog2N(numTags) -- Debug
validatedTagToRetire <- mkBypassFIFO debugOnce <- mkReg True
updateUsageRetireTag :: RWire UIntLog2N(numTags) -- Rules
updateUsageRetireTag <- mkRWire addRules $
rules
"debug_initial_state": when debugOnce ==> do
$display "tagUsage: " (fshow (readVReg tagUsage))
debugOnce := False
updateUsageRequestTag :: RWire UIntLog2N(numTags) "retire_tag": when True ==> do
updateUsageRequestTag <- mkRWire let tag = retireQueue.first
$display "Retiring tag: " (fshow tag)
retireQueue.deq
freeTagQueue.enq tag
retireSignal.wset tag
tagFIFO :: FIFOF UIntLog2N(numTags) -- Combined update rules (simplified below)
tagFIFO <- mkSizedFIFOF reifiedNumTags "update_usage": when True ==> do
let mRetireTag = retireSignal.wget
debugOnce <- mkReg True mRequestTag = requestSignal.wget
case (mRetireTag, mRequestTag) of
addRules $ (Just retireTag, Just requestTag) -> do
rules let usage = readVReg tagUsage
"display": when (debugOnce == True) ==> usage' = update usage requestTag True
do usage'' = update usage' retireTag False
$display "tagUsageTracker : " (fshow |> readVReg tagUsageTracker) writeVReg tagUsage usage''
debugOnce := False $display $time " Updated usage (request + retire): " (fshow |> readVReg tagUsage)
(Just retireTag, Nothing) -> do
"update_usage_just_retire": when (select tagUsage retireTag) := False
Just tag <- updateUsageRetireTag.wget, $display $time " Updated usage (retire): " (fshow (readVReg tagUsage))
Nothing <- updateUsageRequestTag.wget ==> (Nothing, Just requestTag) -> do
do (select tagUsage requestTag) := True
$display $time " tagUsageTracker after update retire: " (fshow |> readVReg tagUsageTracker) $display $time " Updated usage (request): " (fshow (readVReg tagUsage))
(select tagUsageTracker tag) := False (Nothing, Nothing) -> action {}
"update_usage_just_request": when
Nothing <- updateUsageRetireTag.wget,
Just tag <- updateUsageRequestTag.wget ==>
do
$display $time " tagUsageTracker after update request: " (fshow |> readVReg tagUsageTracker)
(select tagUsageTracker tag) := True
"update_usage_request_and_retire": when
Just retireTag <- updateUsageRetireTag.wget,
Just requestTag <- updateUsageRequestTag.wget ==>
do
$display $time " tagUsageTracker after update request and retire: " (fshow |> readVReg tagUsageTracker)
let
tagUsageTrackerInner = readVReg tagUsageTracker
tagUsageTracker' = update tagUsageTrackerInner requestTag True
tagUsageTracker'' = update tagUsageTracker' retireTag False
writeVReg tagUsageTracker tagUsageTracker''
"retire_tags": when True ==>
do
let tagToRetire = validatedTagToRetire.first
$display "firing retire_tags" (fshow tagToRetire)
validatedTagToRetire.deq
tagFIFO.enq tagToRetire
updateUsageRetireTag.wset tagToRetire
return $
interface TagEngine
-- Interface
return $
interface TagEngine
requestTag :: ActionValue UIntLog2N(numTags) requestTag :: ActionValue UIntLog2N(numTags)
requestTag = requestTag = do
do case initialTagCounter of
case initialTagDistributor of Just 0 -> do
Just 0 -> do initialTagCounter := Nothing
initialTagDistributor := Nothing requestSignal.wset 0
updateUsageRequestTag.wset 0 return 0
return 0 Just tag -> do
Just tag -> do initialTagCounter := Just (tag - 1)
initialTagDistributor := Just |> tag - 1 requestSignal.wset tag
updateUsageRequestTag.wset tag return tag
return tag Nothing -> do
Nothing -> do let tag = freeTagQueue.first
let tag = tagFIFO.first freeTagQueue.deq
tagFIFO.deq requestSignal.wset tag
updateUsageRequestTag.wset tag return tag
return tag
-- `retireTag` isn't guarded on tag validity(this would break Bluespec's safety model)
-- so it is advisable that the caller of `retireTag` only attempt to retire valid tags.
-- Internally, the tagEngine will keep a correct and consistent state since TagEngine
-- validates tags before attempting to retire them.
retireTag :: UIntLog2N(numTags) -> Action retireTag :: UIntLog2N(numTags) -> Action
retireTag tag = retireTag tag =
do do
let let
tagValid = tag < reifiedNumTags tagValid = tag < maxTagCount
tagInUse = readReg (select tagUsageTracker tag) tagInUse = readReg (select tagUsage tag)
if (tagValid && tagInUse) if (tagValid && tagInUse)
then do then do
validatedTagToRetire.enq tag retireQueue.enq tag
else do else do
action {} action {}