RiscV-Formal/hs/Peripherals/Setup.hs

30 lines
986 B
Haskell
Raw Permalink Normal View History

2025-02-19 14:06:40 +00:00
module Peripherals.Setup (
setupPeripherals, InitializedPeripherals(..)
) where
2025-02-13 04:54:15 +00:00
import Prelude
import Peripherals.UartCFFI(initTerminal)
2025-02-19 14:06:40 +00:00
import Peripherals.Ram (initRamFromFile, Ram)
import Control.Exception (try)
import System.IO.Error (ioeGetErrorString)
2025-02-13 04:54:15 +00:00
2025-02-19 14:06:40 +00:00
type FirmwareFilePath = FilePath
data InitializedPeripherals
= InitializedPeripherals Ram
| InitializationError String
deriving (Show)
setupPeripherals :: FirmwareFilePath -> IO InitializedPeripherals
setupPeripherals firmwareFilePath = do
initTerminal
result <- try (initRamFromFile firmwareFilePath)
return $ case result of
Right (Just ram) -> InitializedPeripherals ram
Right Nothing -> InitializationError $ firmwareFilePath ++ failure ++ suggestion
Left e -> InitializationError $ firmwareFilePath ++ failure ++ suggestion ++ " Error: " ++ ioeGetErrorString e
where
failure = ": Failed to initialize RAM from file!"
suggestion = " Is the file 4-byte aligned?"