diff --git a/src/CacheSim/Raw.elm b/src/CacheSim/Raw.elm index 6dfc6eb..8084f7f 100644 --- a/src/CacheSim/Raw.elm +++ b/src/CacheSim/Raw.elm @@ -21,11 +21,11 @@ translateRawCacheModel rcm = (Just bs, Just sc, Just ss) -> Ok { blockSize = bs, setCount = sc, setSize = ss } (Nothing, _, _) -> - Err <| "Unable to parse the block size: " ++ rcm.blockSize + Err <| "Unable to parse the block size: \"" ++ rcm.blockSize ++ "\"" (_, Nothing, _) -> - Err <| "Unable to parse the set count: " ++ rcm.setCount + Err <| "Unable to parse the set count: \"" ++ rcm.setCount ++ "\"" (_, _, Nothing) -> - Err <| "Unable to parse the set size: " ++ rcm.setSize + Err <| "Unable to parse the set size: \"" ++ rcm.setSize ++ "\"" translateRawCacheModelHierarchy : RawCacheModelHierarchy -> Result String CacheModelHierarchy translateRawCacheModelHierarchy rcmh = @@ -42,8 +42,8 @@ validateCacheModelHierarchy cmh = [] -> Ok [] x::[] -> Ok [x] x::y::xs -> - if modBy y.blockSize x.blockSize == 0 + if modBy x.blockSize y.blockSize == 0 then validateCacheModelHierarchy (y::xs) - else Err <| "Block cache size " ++ String.fromInt x.blockSize ++ + else Err <| "Block cache size " ++ String.fromInt y.blockSize ++ " is not a multiple of the next level cache's" ++ - " block size (" ++ String.fromInt y.blockSize ++ ")" + " block size (" ++ String.fromInt x.blockSize ++ ")" diff --git a/src/CacheSim/View.elm b/src/CacheSim/View.elm index 11b3a7f..f574b3a 100644 --- a/src/CacheSim/View.elm +++ b/src/CacheSim/View.elm @@ -2,9 +2,16 @@ module CacheSim.View exposing (..) import CacheSim.Raw exposing (..) import CacheSim.Model exposing (..) import Html exposing (Html, input, text, div, label, span, h2, h3) -import Html.Attributes exposing (type_, class, value, for) +import Html.Attributes exposing (type_, class, value, for, classList, disabled) import Html.Events exposing (onInput, onClick) +optionalButton : Bool -> String -> Msg -> Html Msg +optionalButton e s m = + let + events = if e then [ onClick m ] else [ disabled (not e) ] + in + input ([ type_ "button", value s ] ++ events) [] + button : String -> Msg -> Html Msg button s m = input [ type_ "button", onClick m, value s] [] @@ -45,11 +52,30 @@ viewRawCacheModelHierarchy rcmh = let models = div [ class "cache-model-levels" ] <|List.indexedMap viewRawCacheModel rcmh + translationResult = Result.andThen validateCacheModelHierarchy + <| translateRawCacheModelHierarchy rcmh + isValid = + case translationResult of + Ok _ -> True + Err _ -> False + errorHtml = + case translationResult of + Ok _ -> viewError True "" + Err e -> viewError False e newButton = button "Add level" CreateRawModel + useButton = optionalButton isValid "Use hierarchy" CreateRawModel in div [ class "cache-model-hierarchy" ] [ h2 [] [ text "Cache hierarchy" ] - , buttonWrapper [ newButton ] + , errorHtml + , buttonWrapper [ newButton, useButton ] , models ] + +viewError : Bool -> String -> Html Msg +viewError hide e = span [ classList [ ("hidden", hide) ] ] [ text e ] + +viewBase : Model -> Html Msg +viewBase m = + div [] [ viewRawCacheModelHierarchy m.rawHierarchy ] diff --git a/src/Main.elm b/src/Main.elm index 9e8b1cf..3a5ce63 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -24,7 +24,7 @@ init f = view : Model -> Document Msg view m = { title = "Cache Simulator" - , body = [ viewRawCacheModelHierarchy m.rawHierarchy ] + , body = [ viewBase m ] } update : Msg -> Model -> (Model, Cmd Msg)