Create "raw" (string-based) cache model types for user input.

This commit is contained in:
Danila Fedorin 2019-05-28 19:01:41 -07:00
parent 5023b6c341
commit ddfd65b099
1 changed files with 49 additions and 0 deletions

49
src/CacheSim/Raw.elm Normal file
View File

@ -0,0 +1,49 @@
module CacheSim.Raw exposing (..)
import CacheSim.Cache exposing (..)
import CacheSim.Hierarchy exposing (..)
type alias RawCacheModel =
{ blockSize : String
, setCount : String
, setSize : String
}
type alias RawCacheModelHierarchy = List RawCacheModel
translateRawCacheModel : RawCacheModel -> Result String CacheModel
translateRawCacheModel rcm =
let
blockSizeInt = String.toInt rcm.blockSize
setCountInt = String.toInt rcm.setCount
setSizeInt = String.toInt rcm.setSize
in
case (blockSizeInt, setCountInt, setSizeInt) of
(Just bs, Just sc, Just ss) ->
Ok { blockSize = bs, setCount = sc, setSize = ss }
(Nothing, _, _) ->
Err <| "Unable to parse the block size: " ++ rcm.blockSize
(_, Nothing, _) ->
Err <| "Unable to parse the set count: " ++ rcm.setCount
(_, _, Nothing) ->
Err <| "Unable to parse the set size: " ++ rcm.setSize
translateRawCacheModelHierarchy : RawCacheModelHierarchy -> Result String CacheModelHierarchy
translateRawCacheModelHierarchy rcmh =
case rcmh of
[] -> Ok []
cm::cms ->
case translateRawCacheModel cm of
Ok ncm -> Result.map ((::) ncm) <| translateRawCacheModelHierarchy cms
Err e -> Err e
validateCacheModelHierarchy : CacheModelHierarchy -> Result String CacheModelHierarchy
validateCacheModelHierarchy cmh =
case cmh of
[] -> Ok []
x::[] -> Ok [x]
x::y::xs ->
if modBy y.blockSize x.blockSize == 0
then validateCacheModelHierarchy (y::xs)
else Err <| "Block cache size " ++ String.fromInt x.blockSize ++
" is not a multiple of the next level cache's" ++
" block size (" ++ String.fromInt y.blockSize ++ ")"