riscv-bluespec-classic/bs/Bus.bs
2025-04-11 14:26:40 -04:00

142 lines
6.3 KiB
Haskell

package Bus(mkBus, Bus(..)) where
import Types
import BusTypes
import TagEngine
import Vector
import Util
import Arbiter
import FIFO
import FIFOF
import SpecialFIFOs
import Printf
clientRequest :: Arbiter.ArbiterClient_IFC -> Action
clientRequest ifc = ifc.request
busRequestToAddr :: BusRequest -> Addr
busRequestToAddr req = case req of
BusReadRequest (ReadRequest addr _) -> addr
BusWriteRequest (WriteRequest addr _) -> addr
dummyRule :: Rules
dummyRule =
rules
"test rule": when True ==> do
$display "test rule"
-- we need a way to make serverMap safer...
mkBus :: (Addr -> Maybe ServerIdx)
-> Module (Bus inFlightTransactions numClients numServers)
mkBus serverMap = do
-- Tag engines for each client to manage transaction tags
tagEngineByClientVec :: Vector numClients (TagEngine inFlightTransactions)
tagEngineByClientVec <- replicateM mkTagEngine
-- There are `numClients` clients, each of which needs its own arbiter as
-- there are up to `numServer` servers that may wish to submit a response
-- to a given client. Furthermore the rule that routes client requests to
-- servers makes for another potential requestor as it may determine that
-- a request is unmappable and instead opt to form and submit a
-- `BusError UnMapped` response directly to a client response arbiter. Thus
-- we must arbit between a total of `numServers + 1` requestors.
responseArbiterByClient :: Vector numClients (Arbiter.Arbiter_IFC (TAdd numServers 1))
responseArbiterByClient <- replicateM (mkArbiter False)
-- There are `numServer` servers, each of which needs its own arbiter as
-- there are up to `numClient` clients that may wish to submit a response
-- to a given client.
requestArbiterByServer :: Vector numServers (Arbiter.Arbiter_IFC numClients)
requestArbiterByServer <- replicateM (mkArbiter False)
dummyVar :: Reg(Bool)
dummyVar <- mkReg False
-- Queues to hold requests from clients
clientRequestQueues :: Vector numClients (FIFOF (TaggedBusRequest inFlightTransactions))
clientRequestQueues <- replicateM (mkSizedBypassFIFOF (valueOf inFlightTransactions))
-- Queues to hold responses to clients
clientResponseQueues :: Vector numClients (FIFOF (TaggedBusResponse inFlightTransactions))
clientResponseQueues <- replicateM (mkSizedBypassFIFOF (valueOf inFlightTransactions))
let clientRules :: Vector numClients (Rules)
clientRules = genWith $ \clientIdx ->
let
selectedClientRequestQueue :: FIFOF (TaggedBusRequest inFlightTransactions)
selectedClientRequestQueue = (select clientRequestQueues clientIdx)
in
rules
(sprintf "request server from client %d" clientIdx): when True ==> do
let
clientRequest :: TaggedBusRequest inFlightTransactions
clientRequest = selectedClientRequestQueue.first
targetAddr :: Addr = busRequestToAddr |> clientRequest.busRequest
targetServerIdx :: (Maybe ServerIdx) = serverMap targetAddr
-- $display "clientRequest" (fshow clientRequest)
case targetServerIdx of
Just serverIdx -> do
let
targetServerArbiter :: Arbiter.Arbiter_IFC numClients
targetServerArbiter = (select requestArbiterByServer serverIdx)
arbiterClientSlot :: Arbiter.ArbiterClient_IFC
arbiterClientSlot = (select targetServerArbiter.clients clientIdx)
arbiterClientSlot.request
Nothing -> do action {}
addRules |> foldr (<+>) (rules {}) clientRules
-- Client interface vector
let clients :: Vector numClients (BusClient inFlightTransactions)
clients = genWith $ \clientIdx ->
let
selectedClientRequestQueue :: FIFOF (TaggedBusRequest inFlightTransactions)
selectedClientRequestQueue = (select clientRequestQueues clientIdx)
selectedClientResponseQueue :: FIFOF (TaggedBusResponse inFlightTransactions)
selectedClientResponseQueue = (select clientResponseQueues clientIdx)
selectedTagEngine :: TagEngine inFlightTransactions
selectedTagEngine = (select tagEngineByClientVec clientIdx)
in
interface BusClient
submitRequest :: BusRequest
-> ActionValue (MkTagType inFlightTransactions)
submitRequest busRequest = do
tag <- selectedTagEngine.requestTag
let taggedReuqest = TaggedBusRequest {tag = tag; busRequest = busRequest}
selectedClientRequestQueue.enq taggedReuqest
return tag
consumeResponse :: ActionValue (TaggedBusResponse inFlightTransactions)
consumeResponse = do
let
busResponse :: (TaggedBusResponse inFlightTransactions)
busResponse = selectedClientResponseQueue.first
selectedClientResponseQueue.deq
return busResponse
-- Server interface vector
let servers :: Vector numServers (BusServer inFlightTransactions numClients)
servers = genWith $ \serverIdx ->
interface BusServer
consumeRequest :: ActionValue (MkTagType inFlightTransactions, BusRequest)
consumeRequest = do
dummyVar := (not dummyVar)
let dummyBusRequest = BusReadRequest (ReadRequest 0 SizeByte)
return (0, dummyBusRequest)
submitResponse :: ( MkClientTagType numClients,
TaggedBusResponse inFlightTransactions
) -> Action
submitResponse (clientTag, taggedBusResponse) = do
dummyVar := (not dummyVar)
return $
interface Bus
clients = clients
servers = servers