-- ===================================================================================
shMem :: (SharedMem, RequestFifo)
         -> IndRequests
         -> ((SharedMem, RequestFifo), (SprID,Reply))

shMem (sharedMem,requestFifo) chRequests = ((sharedMem',requestFifo'), (i,reply))
        where
          (i,req)  | not $ null requestFifo   = head requestFifo
                   | otherwise                = (0, NoRequest)

          (reply, sharedMem')   = case req of
                NoRequest                          -> ( Nothing            , sharedMem )
                ReadReq a                          -> ( Just (sharedMem!a) , sharedMem )
                WriteReq v a                       -> ( Nothing            , sharedMem <~ (a,v))
                TestReq a     | sharedMem!a == 0   -> ( Just 1             , sharedMem <~ (a,1))
                              | otherwise          -> ( Just 0             , sharedMem )

          requestFifo'  = drop 1 requestFifo ++ filter ((/=NoRequest).snd) chRequests

-- ===================================================================================
shMem :: (SharedMem, RequestFifo)
         -> IndRequests
         -> ((SharedMem, RequestFifo), (SprID,Reply))
shMem (sharedMem,requestFifo) chRequests = ((sharedMem',requestFifo'), (i,reply))
        where
          (i,req)  | not $ null requestFifo   = head requestFifo
                   | otherwise                = (0, NoRequest)

          (reply, sharedMem')   = case req of
                NoRequest                          -> ( Nothing            , sharedMem )
                ReadReq a                          -> ( Just (sharedMem!a) , sharedMem )
                WriteReq v a                       -> ( Nothing            , sharedMem <~ (a,v))
                TestReq a     | sharedMem!a == 0   -> ( Just 1             , sharedMem <~ (a,1))
                              | otherwise          -> ( Just 0             , sharedMem )

          requestFifo'  = drop 1 requestFifo ++ filter ((/=NoRequest).snd) chRequests

-- ===================================================================================
shMem (sharedMem,requestFifo) chRequests = ((sharedMem',requestFifo'), (i,reply))
        where
          (i,req)  | not $ null requestFifo   = head requestFifo
                   | otherwise                = (0, NoRequest)

          (reply, sharedMem')   = case req of
                NoRequest                          -> ( Nothing            , sharedMem )
                ReadReq a                          -> ( Just (sharedMem!a) , sharedMem )
                WriteReq v a                       -> ( Nothing            , sharedMem <~ (a,v))
                TestReq a     | sharedMem!a == 0   -> ( Just 1             , sharedMem <~ (a,1))
                              | otherwise          -> ( Just 0             , sharedMem )

          requestFifo'  = drop 1 requestFifo ++ filter ((/=NoRequest).snd) chRequests

-- ===================================================================================