Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue #42

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bayeux.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ library
, Bayeux.Ice40.Spi
, Bayeux.Ice40.Spram
, Bayeux.Lp
, Bayeux.Queue
, Bayeux.Rtl
, Bayeux.Signal
, Bayeux.Tableaux
Expand Down
95 changes: 95 additions & 0 deletions lib/Bayeux/Queue.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Bayeux.Queue
( MonadQueue(..)
) where

import Bayeux.Encode
import Bayeux.Rtl (Rtl)
import Bayeux.Signal
import Bayeux.Width
import Data.Array
import Data.Finite
import Data.Proxy
import GHC.TypeNats

class MonadQueue m where
queue :: Width a
=> Natural -- ^ size
-> Sig (Maybe a) -- ^ enqueue
-> Sig Bool -- ^ dequeue
-> m (Sig (Maybe a))

instance MonadQueue Rtl where
queue sz = withNat sz queue'


withNat :: forall r. Natural -> (forall n. KnownNat n => SNat n -> r) -> r
withNat n f = withSomeSNat n go
where
go :: forall m. SNat m -> r
go sm = withKnownNat sm $ f sm

data Queue n a = Queue
{ rPtr :: Finite n
, rWrap :: Bool
, wPtr :: Finite n
, wWrap :: Bool
, dArray :: Array (Finite n) a
}
deriving (Eq, Read, Show)

instance (KnownNat n, Width a) => Width (Queue n a) where
width _ = 2 + sum
[ 2 * width (undefined :: Finite n)
, width (undefined :: Array (Finite n) a)
]

instance (KnownNat n, Encode a) => Encode (Queue n a) where
encode q = mconcat
[ encode $ rPtr q
, encode $ rWrap q
, encode $ wPtr q
, encode $ wWrap q
, encode $ dArray q
]

sliceRPtr :: Sig (Queue n a) -> Sig (Finite n)
sliceRPtr = slice (width (undefined :: Finite n) + base - 1) base
where
base = width (undefined :: Finite n) - 1 + dArrayWidth + 3

Check failure on line 61 in lib/Bayeux/Queue.hs

View workflow job for this annotation

GitHub Actions / ubuntu-20.04 GHC 9.8

• Couldn't match expected type ‘Integer’

sliceRWrap :: Sig (Queue n a) -> Sig Bool
sliceRWrap = slice
(width (undefined :: Finite n) - 1 + dArrayWidth + 2)

Check failure on line 65 in lib/Bayeux/Queue.hs

View workflow job for this annotation

GitHub Actions / ubuntu-20.04 GHC 9.8

• Couldn't match expected type ‘Integer’
(width (undefined :: Finite n) - 1 + dArrayWidth + 2)

Check failure on line 66 in lib/Bayeux/Queue.hs

View workflow job for this annotation

GitHub Actions / ubuntu-20.04 GHC 9.8

• Couldn't match expected type ‘Integer’

rWrapBase :: forall n. KnownNat n => Proxy n -> Integer
rWrapBase _ = width (undefined :: Finite n) - 1 + dArrayWidth (Proxy :: Proxy n) + 2

sliceWPtr :: forall n a. KnownNat n => Sig (Queue n a) -> Sig (Finite n)
sliceWPtr = slice
(width (undefined :: Finite n) - 1 + dArrayWidth (Proxy :: Proxy n) + 1)
(dArrayWidth (Proxy :: Proxy n) + 1)

sliceWWrap :: forall n a. KnownNat n => Sig (Queue n a) -> Sig Bool
sliceWWrap = slice (dArrayWidth (Proxy :: Proxy n)) (dArrayWidth (Proxy :: Proxy n))

sliceDArray :: forall n a. KnownNat n => Sig (Queue n a) -> Sig (Array (Finite n) a)
sliceDArray = slice (dArrayWidth (Proxy :: Proxy n) - 1) 0

dArrayWidth :: forall n. KnownNat n => Proxy n -> Integer
dArrayWidth _ = width (undefined :: Array (Finite n) a)

queue'
:: forall n a
. KnownNat n
=> Width a
=> SNat n
-> Sig (Maybe a)
-> Sig Bool
-> Rtl (Sig (Maybe a))
queue' _ enqM deq = do
s <- process $ \(s :: Sig (Queue n a)) -> undefined
return $ Sig undefined
Loading