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" data DispatchFromClient inFlightTransactions numServers = DispatchRequest (TaggedBusRequest inFlightTransactions) (MkServerIdx numServers) | BypassResponse (TaggedBusResponse inFlightTransactions) deriving (Bits, Eq, FShow) -- we need a way to make serverMap safer... mkBus :: (Addr -> Maybe (MkServerIdx numServers)) -> 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) -- After we inspect the head/oldest request in the `clientRequestQueues` and perform an -- arbiter request to the destination arbiter, we need to inspect for grant in another -- rule as I don't believe grant and request can be called simultaneously. -- The following vector allows us to move dispatchByClient :: Vector numClients (Wire (DispatchFromClient inFlightTransactions numServers)) dispatchByClient <- replicateM mkWire -- 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 server. 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)) -- The following two vectors of FIFOs make it easier to push/pull data to/from internal -- server methods: consumeRequestQueues :: Vector numServers (FIFOF (TaggedBusResponse inFlightTransactions)) consumeRequestQueues <- replicateM mkBypassFIFOF submitResponseQueues :: Vector numServers (FIFOF (TaggedBusResponse inFlightTransactions)) submitResponseQueues <- replicateM mkBypassFIFOF let clientRules :: Vector numClients (Rules) clientRules = genWith $ \clientIdx -> let selectedClientRequestQueue :: FIFOF (TaggedBusRequest inFlightTransactions) selectedClientRequestQueue = (select clientRequestQueues clientIdx) in rules (sprintf "dispatch client request %d" clientIdx): when True ==> do let clientRequest :: TaggedBusRequest inFlightTransactions clientRequest = selectedClientRequestQueue.first targetAddr :: Addr = busRequestToAddr |> clientRequest.busRequest targetServerIdx :: (Maybe (MkServerIdx numServers)) = serverMap targetAddr dispatchByClientWire :: Wire (DispatchFromClient inFlightTransactions numServers) dispatchByClientWire = (select dispatchByClient clientIdx) 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 dispatchByClientWire := DispatchRequest clientRequest serverIdx Nothing -> do let targetClientResponseArbiter :: Arbiter.Arbiter_IFC (TAdd numServers 1) targetClientResponseArbiter = (select responseArbiterByClient clientIdx) clientResponseArbiterSlot :: Arbiter.ArbiterClient_IFC -- arbiters 0 to n-1 where `n:=numServer` are reserved -- for servers to make requests to. Arbiter n is reserved for -- when this rule needs to skip making a request to a server -- and should instead forward the `BusError UnMapped` response -- back to the client. Vector.last selects arbiter `n` clientResponseArbiterSlot = Vector.last targetClientResponseArbiter.clients responseUnMapped = case clientRequest.busRequest of BusReadRequest _ -> BusReadResponse (Left UnMapped) BusWriteRequest _ -> BusWriteResponse (Left UnMapped) response :: TaggedBusResponse inFlightTransactions response = TaggedBusResponse { tag = clientRequest.tag; busResponse = responseUnMapped } clientResponseArbiterSlot.request dispatchByClientWire := BypassResponse response 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 selectedTagEngine.retireTag busResponse.tag selectedClientResponseQueue.deq return busResponse -- Server interface vector let servers :: Vector numServers (BusServer inFlightTransactions numClients) servers = genWith $ \serverIdx -> interface BusServer consumeRequest :: ActionValue (TaggedBusRequest inFlightTransactions) consumeRequest = do dummyVar := (not dummyVar) let dummyBusRequest = BusReadRequest (ReadRequest 0 SizeByte) return (TaggedBusRequest {tag = 0; busRequest = dummyBusRequest}) submitResponse :: ( MkClientTagType numClients, TaggedBusResponse inFlightTransactions ) -> Action submitResponse (clientTag, taggedBusResponse) = do dummyVar := (not dummyVar) return $ interface Bus clients = clients servers = servers