first commit
This commit is contained in:
commit
ef58d5b07e
34 changed files with 2210 additions and 0 deletions
93
hs/Peripherals/Ram.hs
Normal file
93
hs/Peripherals/Ram.hs
Normal file
|
@ -0,0 +1,93 @@
|
|||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
{-# LANGUAGE CPP #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
|
||||
module Peripherals.Ram() where
|
||||
|
||||
import Clash.Prelude
|
||||
import qualified Prelude as P
|
||||
import qualified Data.ByteString.Lazy as BL
|
||||
import Data.Binary.Get
|
||||
import Data.Int (Int32)
|
||||
import qualified Clash.Sized.Vector as Vec
|
||||
|
||||
-- vector depth has to be known statically at compile time
|
||||
#ifndef _RAM_DEPTH
|
||||
#define _RAM_DEPTH 1024
|
||||
#endif
|
||||
|
||||
-- TODO : replace Unsigned 32 with BusVal types later...
|
||||
type Ram = Vec _RAM_DEPTH (Unsigned 32)
|
||||
|
||||
initRamFromFile :: FilePath -> IO (Maybe Ram)
|
||||
initRamFromFile filePath =
|
||||
let
|
||||
initRam = Vec.replicate (SNat :: SNat _RAM_DEPTH) 0
|
||||
in
|
||||
do
|
||||
bs <- readFileIntoByteString filePath
|
||||
let ints = getInts bs
|
||||
pure $ populateVectorFromInt32 ints initRam
|
||||
|
||||
readFileIntoByteString :: FilePath -> IO BL.ByteString
|
||||
readFileIntoByteString filePath = BL.readFile filePath
|
||||
|
||||
-- Define a function to read a ByteString and convert to [Int32]
|
||||
getInts :: BL.ByteString -> [Int32]
|
||||
getInts bs = runGet listOfInts bs
|
||||
where
|
||||
listOfInts = do
|
||||
empty <- isEmpty
|
||||
if empty
|
||||
then pure []
|
||||
else do
|
||||
i <- getInt32le -- Parse a single Int32 from the stream
|
||||
rest <- listOfInts -- Recursively parse the rest
|
||||
pure (i : rest)
|
||||
|
||||
-- Adjusts the length of a list of integers by either truncating or padding with zeros
|
||||
populateVectorFromInt32 ::
|
||||
KnownNat n =>
|
||||
[Int32] ->
|
||||
Vec n (Unsigned 32) ->
|
||||
Maybe (Vec n (Unsigned 32))
|
||||
populateVectorFromInt32 ls v = Vec.fromList adjustedLs
|
||||
where
|
||||
vecLen = length v
|
||||
adjustedLs = fromIntegral <$> adjustLength vecLen ls
|
||||
adjustLength :: Int -> [Int32] -> [Int32]
|
||||
adjustLength n xs = P.take n (xs P.++ P.repeat 0)
|
||||
|
||||
|
||||
|
||||
-- Function to increment each element of a Clash vector
|
||||
-- prepareVector :: KnownNat n => [Int32] -> Vec n (Unsigned 32)
|
||||
-- prepareVector xs = let
|
||||
-- unsigneds = map (fromIntegral :: Int32 -> Unsigned 32) xs -- Step 1: Convert Int32 to Unsigned 32
|
||||
-- len = length unsigneds
|
||||
-- in case compare len (snatToNum (SNat @n)) of -- Step 2: Adjust the length of the list
|
||||
-- LT -> takeI unsigneds ++ repeat 0 -- Pad with zeros if the list is shorter
|
||||
-- GT -> takeI unsigneds -- Truncate if the list is longer
|
||||
-- EQ -> takeI unsigneds -- No padding or truncation needed
|
||||
|
||||
-- Function to load firmware
|
||||
-- loadFirmware :: KnownNat n => [Int32] -> Vec n (Unsigned 32)
|
||||
-- loadFirmware (x:xs) = vecHead ++ vecTail
|
||||
-- where
|
||||
-- vecHead = singleton (fromIntegral x)
|
||||
-- vecTail = loadFirmware xs
|
||||
-- loadFirmware [] = takeI $ repeat 0
|
||||
|
||||
-- loadFirmware xs = v
|
||||
-- where
|
||||
-- mapped :: [Unsigned 32] = Clash.Prelude.fromIntegral <$> xs
|
||||
-- c = takeI (mapped ++ repeat 0)
|
||||
-- v = takeI $ (mapped ++ repeat 0)
|
||||
|
||||
-- -- Example usage
|
||||
-- someList :: [Int32]
|
||||
-- someList = [1, 2, 3, 4, 5]
|
||||
|
||||
-- mem :: Vec 16 (Unsigned 32)
|
||||
-- mem = loadFirmware someList
|
8
hs/Peripherals/Setup.hs
Normal file
8
hs/Peripherals/Setup.hs
Normal file
|
@ -0,0 +1,8 @@
|
|||
module Peripherals.Setup (setupPeripherals) where
|
||||
|
||||
import Prelude
|
||||
import Peripherals.UartCFFI(initTerminal)
|
||||
|
||||
setupPeripherals :: IO ()
|
||||
setupPeripherals = do
|
||||
initTerminal
|
8
hs/Peripherals/Teardown.hs
Normal file
8
hs/Peripherals/Teardown.hs
Normal file
|
@ -0,0 +1,8 @@
|
|||
module Peripherals.Teardown(teardownPeripherals) where
|
||||
|
||||
import Prelude
|
||||
import Peripherals.UartCFFI(restoreTerminal)
|
||||
|
||||
teardownPeripherals :: IO ()
|
||||
teardownPeripherals = do
|
||||
restoreTerminal
|
52
hs/Peripherals/UartCFFI.hs
Normal file
52
hs/Peripherals/UartCFFI.hs
Normal file
|
@ -0,0 +1,52 @@
|
|||
{-# LANGUAGE ForeignFunctionInterface #-}
|
||||
|
||||
module Peripherals.UartCFFI (
|
||||
initTerminal,
|
||||
restoreTerminal,
|
||||
getCharFromTerminal,
|
||||
writeCharToTerminal,
|
||||
isCharAvailable,
|
||||
setupSigintHandler,
|
||||
wasCtrlCReceived
|
||||
) where
|
||||
|
||||
import Prelude
|
||||
import Foreign.C.Types
|
||||
import Foreign.C.String
|
||||
import Foreign.Ptr
|
||||
import Data.Char (chr, ord)
|
||||
|
||||
-- Foreign imports directly corresponding to the C functions
|
||||
foreign import ccall "init_terminal" c_initTerminal :: IO ()
|
||||
foreign import ccall "restore_terminal" c_restoreTerminal :: IO ()
|
||||
foreign import ccall "get_char_from_terminal" c_getCharFromTerminal :: IO CChar
|
||||
foreign import ccall "write_char_to_terminal" c_writeCharToTerminal :: CChar -> IO ()
|
||||
foreign import ccall "is_char_available" c_isCharAvailable :: IO CInt
|
||||
foreign import ccall "setup_sigint_handler" c_setupSigintHandler :: IO ()
|
||||
foreign import ccall "was_ctrl_c_received" c_wasCtrlCReceived :: IO CInt
|
||||
|
||||
-- Haskell friendly wrappers
|
||||
initTerminal :: IO ()
|
||||
initTerminal = c_initTerminal
|
||||
|
||||
restoreTerminal :: IO ()
|
||||
restoreTerminal = c_restoreTerminal
|
||||
|
||||
getCharFromTerminal :: IO Char
|
||||
getCharFromTerminal = fmap (chr . fromEnum) c_getCharFromTerminal
|
||||
|
||||
writeCharToTerminal :: Char -> IO ()
|
||||
writeCharToTerminal char = c_writeCharToTerminal (toEnum $ ord char)
|
||||
|
||||
isCharAvailable :: IO Int
|
||||
isCharAvailable = fmap fromEnum c_isCharAvailable
|
||||
|
||||
setupSigintHandler :: IO ()
|
||||
setupSigintHandler = c_setupSigintHandler
|
||||
|
||||
wasCtrlCReceived :: IO Int
|
||||
wasCtrlCReceived = fmap fromEnum c_wasCtrlCReceived
|
||||
|
||||
-- Improved version of the ctrlCReceived to use the new wasCtrlCReceived signature
|
||||
ctrlCReceived :: IO Bool
|
||||
ctrlCReceived = fmap (/= 0) wasCtrlCReceived
|
Reference in a new issue