diff --git a/app/Main.hs b/app/Main.hs index 862a37efc..488fc190d 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -40,7 +40,6 @@ import NITTA.Synthesis (TargetSynthesis (..), mlScoreKeyPrefix, noSynthesis, sta import NITTA.Synthesis.MlBackend.ServerInstance import NITTA.UIBackend import NITTA.UIBackend.Types (BackendCtx, mlBackendGetter, nodeScores, outputPath, receivedValues, root) -import NITTA.Utils import Paths_nitta import System.Console.CmdArgs hiding (def) import System.Exit @@ -199,8 +198,6 @@ nittaArgs = getNittaArgs :: IO Nitta getNittaArgs = cmdArgs nittaArgs -fromConf toml s = getFromTomlSection s =<< toml - main = do ( Nitta filename @@ -230,9 +227,7 @@ main = do -- it's critical for successful parsing of NITTA's stdout in python scripts hSetBuffering stdout LineBuffering - toml <- case uarch of - Nothing -> return Nothing - Just path -> Just . getToml <$> T.readFile path + conf <- parseConfig $ fromJust uarch let exactFrontendType = identifyFrontendType filename frontend_language @@ -241,17 +236,17 @@ main = do let frontendResult@FrontendResult{frDataFlow, frTrace, frPrettyLog} = translate exactFrontendType src received = [("u#0", map (\i -> read $ show $ sin ((2 :: Double) * 3.14 * 50 * 0.001 * i)) [0 .. toEnum n])] - ioSync = fromJust $ io_sync <|> fromConf toml "ioSync" <|> Just Sync - confMa = toml >>= Just . mkMicroarchitecture ioSync + ioSync_ = fromJust $ io_sync <|> Just (ioSync' conf) <|> Just Sync + confMa = Just (mkMicroarchitecture conf) ma :: BusNetwork T.Text T.Text (Attr (FX m b)) Int ma | auto_uarch && isJust confMa = error $ "auto_uarch flag means that an empty uarch with default prototypes will be used. " <> "Remove uarch flag or specify prototypes list in config file and remove auto_uarch." - | auto_uarch = microarchWithProtos ioSync + | auto_uarch = microarchWithProtos ioSync_ | isJust confMa = fromJust confMa - | otherwise = defMicroarch ioSync + | otherwise = defMicroarch ioSync_ infoM "NITTA" $ "will trace: " <> S.join ", " (map (show . tvVar) frTrace) @@ -302,7 +297,7 @@ main = do exitSuccess ) $ parseFX . fromJust - $ type_ <|> fromConf toml "type" <|> Just "fx32.32" + $ type_ <|> Just (T.unpack $ type' conf) <|> Just "fx32.32" parseFX input = let typePattern = mkRegex "fx([0-9]+).([0-9]+)" diff --git a/examples/microarch.toml b/examples/microarch.toml deleted file mode 100644 index 8ed5cfcf5..000000000 --- a/examples/microarch.toml +++ /dev/null @@ -1,46 +0,0 @@ -type = "fx32.32" -ioSync = "Sync" - -[[networks]] -name = "net1" - -# Array of PUs - -[[networks.pus]] -type = "SPI" -name = "spi" -mosi = "mosi" -miso = "miso" -sclk = "sclk" -cs = "cs" -isSlave = true -bufferSize = 6 -bounceFilter = 0 - -# Array of PU prototypes - -[[networks.protos]] -type = "Fram" -name = "fram{x}" # If you want a PU can be allocated only once, remove {x} from the PU name. -size = 32 - -[[networks.protos]] -type = "Shift" -name = "shift{x}" -sRight = true - -[[networks.protos]] -type = "Multiplier" -name = "mul{x}" -mock = true - -[[networks.protos]] -type = "Accum" -name = "accum{x}" -isInt = true - -[[networks.protos]] -type = "Divider" -name = "div{x}" -mock = true -pipeline = 4 diff --git a/examples/microarch.yml b/examples/microarch.yml new file mode 100644 index 000000000..84c269ce6 --- /dev/null +++ b/examples/microarch.yml @@ -0,0 +1,32 @@ +type: fx32.32 +ioSync: Sync +networks: + - name: net1 + pus: + - type: SPI + name: spi + mosi: mosi + miso: miso + sclk: sclk + cs: cs + isSlave: true + bufferSize: 6 + bounceFilter: 0 + protos: + - type: Fram + name: fram{x} # If you want a PU can be allocated only once, remove {x} from the PU name. + size: 32 + - type: Shift + name: shift{x} + sRight: true + - type: Multiplier + name: mul{x} + mock: true + - type: Accum + name: accum{x} + isInt: true + - type: Divider + name: div{x} + mock: true + pipeline: 4 + diff --git a/nitta.cabal b/nitta.cabal index 7c1e3ba5a..47b73944b 100644 --- a/nitta.cabal +++ b/nitta.cabal @@ -148,7 +148,6 @@ library , hashable , heap , hslogger - , htoml , http-api-data , http-conduit , hxt @@ -178,6 +177,7 @@ library , wai-app-static , wai-cors , warp + , yaml default-language: Haskell2010 executable nitta @@ -211,7 +211,6 @@ executable nitta , ginger , heap , hslogger - , htoml , intervals , mtl , nitta @@ -223,6 +222,7 @@ executable nitta , tostring , unordered-containers , wai-app-static + , yaml default-language: Haskell2010 executable nitta-api-gen @@ -259,7 +259,6 @@ executable nitta-api-gen , ginger , heap , hslogger - , htoml , intervals , mtl , nitta @@ -271,6 +270,7 @@ executable nitta-api-gen , tostring , unordered-containers , wai-app-static + , yaml default-language: Haskell2010 test-suite nitta-test @@ -337,7 +337,6 @@ test-suite nitta-test , genvalidity-property , ginger , heap - , htoml , hxt , intervals , language-lua @@ -359,4 +358,5 @@ test-suite nitta-test , tostring , unordered-containers , wai-app-static + , yaml default-language: Haskell2010 diff --git a/package.yaml b/package.yaml index 689b5f03d..1278107f5 100644 --- a/package.yaml +++ b/package.yaml @@ -61,7 +61,7 @@ dependencies: - data-default - filepath - ginger - - htoml + - yaml - intervals - mtl - prettyprinter diff --git a/src/NITTA/Model/Microarchitecture/Config.hs b/src/NITTA/Model/Microarchitecture/Config.hs index 60f32e85b..5df6eb139 100644 --- a/src/NITTA/Model/Microarchitecture/Config.hs +++ b/src/NITTA/Model/Microarchitecture/Config.hs @@ -3,20 +3,26 @@ {-# LANGUAGE PartialTypeSignatures #-} module NITTA.Model.Microarchitecture.Config ( + MicroarchitectureConf (type', ioSync'), + parseConfig, mkMicroarchitecture, ) where import Data.Aeson ( - FromJSON (parseJSON), Options (sumEncoding), SumEncoding (TaggedObject, contentsFieldName, tagFieldName), - ToJSON (toJSON), defaultOptions, genericParseJSON, genericToJSON, ) import Data.Default (Default (def)) -import Data.HashMap.Internal.Strict (HashMap) +import Data.Yaml ( + decodeFileThrow, + (.:), + Value (Object), + FromJSON (parseJSON), + ToJSON (toJSON), + ) import Data.Text qualified as T import GHC.Generics (Generic) import NITTA.Intermediate.Value (Val) @@ -30,7 +36,6 @@ import NITTA.Model.Networks.Bus ( ) import NITTA.Model.Networks.Types (IOSynchronization) import NITTA.Model.ProcessorUnits qualified as PU -import NITTA.Utils (getFromToml) data PUConf = Accum @@ -86,16 +91,28 @@ data NetworkConf = NetworkConf instance FromJSON NetworkConf instance ToJSON NetworkConf -newtype MicroarchitectureConf = MicroarchitectureConf - { networks :: [NetworkConf] +data MicroarchitectureConf = MicroarchitectureConf + { type' :: T.Text + , ioSync' :: IOSynchronization + , networks :: [NetworkConf] } deriving (Generic, Show) -instance FromJSON MicroarchitectureConf +instance FromJSON MicroarchitectureConf where + parseJSON (Object v) = do + type' <- v .: "type" + ioSync' <- v .: "ioSync" + networks <- v .: "networks" + return MicroarchitectureConf { type' = type', ioSync' = ioSync', networks = networks } + parseJSON v = fail $ show v instance ToJSON MicroarchitectureConf -mkMicroarchitecture :: (Val v, Var x, ToJSON a, ToJSON x) => IOSynchronization -> HashMap T.Text a -> BusNetwork T.Text x v Int -mkMicroarchitecture ioSync toml = +parseConfig :: FilePath -> IO MicroarchitectureConf +parseConfig path = do + decodeFileThrow path :: IO MicroarchitectureConf + +mkMicroarchitecture :: (Val v, Var x, ToJSON x) => MicroarchitectureConf -> BusNetwork T.Text x v Int +mkMicroarchitecture conf = let addPU proto | proto = addCustomPrototype | otherwise = addCustom @@ -125,8 +142,8 @@ mkMicroarchitecture ioSync toml = , master_sclk = PU.OutputPortTag sclk , master_cs = PU.OutputPortTag cs } - nets = networks (getFromToml toml :: MicroarchitectureConf) - mkNetwork net@NetworkConf{name} = modifyNetwork (busNetwork name ioSync) (build net) + nets = networks conf + mkNetwork net@NetworkConf{name} = modifyNetwork (busNetwork name $ ioSync' conf) (build net) in case nets of [n] -> mkNetwork n _ -> error "multi-networks are not currently supported" diff --git a/src/NITTA/Project/Template.hs b/src/NITTA/Project/Template.hs index f7311341c..5d362b332 100644 --- a/src/NITTA/Project/Template.hs +++ b/src/NITTA/Project/Template.hs @@ -31,6 +31,7 @@ import Data.Maybe import Data.String.Interpolate import Data.Text qualified as T import Data.Text.IO qualified as T +import Data.Yaml import GHC.Generics hiding (moduleName) import NITTA.Project.Context import NITTA.Project.Types @@ -39,13 +40,15 @@ import System.FilePath import System.Log.Logger import System.Path.WildMatch import Text.Ginger -import Text.Toml data Conf = Conf { template :: TemplateConf , signals :: M.HashMap T.Text T.Text } - deriving (Show) + deriving (Generic, Show) + +instance FromJSON Conf +instance ToJSON Conf data TemplateConf = TemplateConf { nittaPath :: Maybe FilePath @@ -54,7 +57,7 @@ data TemplateConf = TemplateConf deriving (Generic, Show) defNittaPath = "." -templateConfFileName = "template.toml" +templateConfFileName = "template.yml" instance Default TemplateConf where def = @@ -86,26 +89,11 @@ collectNittaPath templates = do where getNittaPath = fromMaybe (error "internal error") . nittaPath . template +readTemplateConfDef :: FilePath -> IO Conf readTemplateConfDef fn = do - text <- - doesFileExist fn >>= \case - True -> T.readFile fn - False -> return "" - let conf = either (error . show) id $ parseTomlDoc (fn <> ": parse error: ") text - return - Conf - { template = confLookup fn "template" conf - , signals = confLookup fn "signals" conf - } - -confLookup fn sec conf = - maybe - def - (unwrap (fn <> " in section [" <> T.unpack sec <> "]: ") . fromJSON . toJSON) - $ M.lookup sec conf - where - unwrap _prefix (Success a) = a - unwrap prefix (Error msg) = error $ prefix <> msg + doesFileExist fn >>= \case + True -> decodeFileThrow fn :: IO Conf + False -> return Conf{template = def, signals = def} applyCustomSignal signals diff --git a/src/NITTA/Utils.hs b/src/NITTA/Utils.hs index 440a2ed2a..951960d1b 100644 --- a/src/NITTA/Utils.hs +++ b/src/NITTA/Utils.hs @@ -33,16 +33,9 @@ module NITTA.Utils ( getIntermediates, isInstruction, module NITTA.Utils.Base, - - -- * Toml - getToml, - getFromToml, - getFromTomlSection, ) where -import Data.Aeson import Data.Bits (setBit, testBit) -import Data.HashMap.Strict qualified as HM import Data.List (sortOn) import Data.Maybe import Data.String.Utils qualified as S @@ -56,7 +49,6 @@ import Numeric.Interval.NonEmpty (inf, sup, (...)) import Numeric.Interval.NonEmpty qualified as I import Prettyprinter import Prettyprinter.Render.Text -import Text.Toml (parseTomlDoc) type Verilog = Doc () doc2text :: Verilog -> T.Text @@ -129,16 +121,3 @@ stepsInterval ss = in a ... b stepStart Step{pInterval} = I.inf pInterval - -getToml text = either (error . show) id $ parseTomlDoc "parse error: " text - -getFromToml toml = getFromTomlSection T.empty toml - -getFromTomlSection section toml - | section == T.empty = unwrap $ fromJSON $ toJSON toml - | otherwise = case HM.lookup section toml of - Just s -> unwrap $ fromJSON $ toJSON s - Nothing -> error $ "section not found - " <> T.unpack section - where - unwrap (Success conf) = conf - unwrap (Error msg) = error msg diff --git a/templates/Icarus/template.toml b/templates/Icarus/template.toml deleted file mode 100644 index 5a8c6403e..000000000 --- a/templates/Icarus/template.toml +++ /dev/null @@ -1,11 +0,0 @@ -# section for general parameters of template. -[template] -# subpath of nitta implementation (default: '.') -nittaPath = "." -# list of template files, which should be ignored (default: ['template.toml']) -ignore = ['template.toml'] - -# external signal names used in nitta instance -[signals] -rst = "rst" -clk = "clk" diff --git a/templates/Icarus/template.yml b/templates/Icarus/template.yml new file mode 100644 index 000000000..15b685341 --- /dev/null +++ b/templates/Icarus/template.yml @@ -0,0 +1,7 @@ +template: + nittaPath: "." + ignore: + - 'template.yml' +signals: + rst: rst + clk: clk \ No newline at end of file