RiscV-Formal/bin/Main.hs

52 lines
1.7 KiB
Haskell
Raw Permalink Normal View History

2025-02-13 04:54:15 +00:00
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ConstraintKinds #-}
module Main where
import Prelude
import System.Environment (getArgs, getProgName)
import System.Exit (exitFailure)
import Data.Maybe (listToMaybe)
import Data.List (isPrefixOf)
import Text.Show.Pretty (ppShow)
2025-02-19 14:06:40 +00:00
import Simulation (simulation, Args(..), Simulation(..))
2025-02-13 04:54:15 +00:00
main :: IO ()
main = do
rawArgs <- getArgs
args <- parseArgs rawArgs
2025-02-19 14:06:40 +00:00
simResult <- simulation args
2025-02-13 04:54:15 +00:00
putStrLn "Simulating Machine"
2025-02-19 14:06:40 +00:00
case simResult of
Success states -> do
-- mapM_ (putStrLn . ppShow) states -- Uncomment to print each state, if needed.
putStrLn $ "Last state: " ++ show (last states)
putStrLn $ "Executed for " ++ show (length states) ++ " cycles"
putStrLn "Simulation complete"
Failure err -> do
putStrLn $ "Simulation failed: " ++ err
exitFailure
2025-02-13 04:54:15 +00:00
-- Function to parse command line arguments into the Args data type
parseArgs :: [String] -> IO Args
2025-02-19 14:06:40 +00:00
parseArgs argv =
2025-02-13 04:54:15 +00:00
case extractKey "firmware" argv of
Just firmwarePath -> return Args { firmware = firmwarePath }
Nothing -> do
progName <- getProgName
putStrLn "Error: No firmware file found."
putStrLn $ "Usage: " ++ progName ++ " --firmware=FILE"
exitFailure
filterByKey :: String -> [String] -> [String]
filterByKey key argv = filter (switch `isPrefixOf`) argv
where
switch = "--" ++ key ++ "="
extractKey :: String -> [String] -> Maybe String
extractKey key argv = listToMaybe $ map removePrefix $ filterByKey key argv
where removePrefix = drop $ length ("--" ++ key ++ "=")