CacheSim/src/CacheSim/View.elm

234 lines
8.2 KiB
Elm
Raw Normal View History

module CacheSim.View exposing (..)
import CacheSim.Raw exposing (..)
import CacheSim.Model exposing (..)
2019-05-28 21:01:35 -07:00
import CacheSim.Cache exposing (..)
import CacheSim.AccessView exposing (..)
2019-05-28 21:01:35 -07:00
import CacheSim.Hierarchy exposing (..)
import Html exposing (Html, Attribute, input, text, div, label, span, h2, h3, table, tr, td, th)
import Html.Attributes exposing (type_, class, value, for, classList, disabled, colspan, hidden)
import Html.Events exposing (onInput, onClick)
-- Button components, powered by Bootstrap
basicButton : List (Attribute Msg) -> String -> Html Msg
basicButton attrs s = input ([ type_ "button", value s, class "btn", class "btn-info" ] ++ attrs) []
disabledButton : String -> Html Msg
disabledButton = basicButton [ disabled True ]
advancedButton : List (Attribute Msg) -> String -> Msg -> Html Msg
advancedButton attrs s m = basicButton (attrs ++ [ onClick m ]) s
2019-05-28 20:08:04 -07:00
button : String -> Msg -> Html Msg
button s m = basicButton [ onClick m ] s
dangerButton : String -> Msg -> Html Msg
dangerButton = advancedButton [ class "btn-danger" ]
primaryButton : String -> Msg -> Html Msg
primaryButton = advancedButton [ class "btn-primary" ]
secondaryButton : String -> Msg -> Html Msg
secondaryButton = advancedButton [ class "btn-secondary" ]
maybeButton : Maybe a -> String -> (a -> Msg) -> Html Msg
maybeButton m s f =
case m of
Just v -> button s (f v)
_ -> disabledButton s
resultButton : Result e a -> String -> (a -> Msg) -> Html Msg
resultButton = maybeButton << Result.toMaybe
-- Button wrapper (button group)
buttonWrapper : List (Html Msg) -> Html Msg
buttonWrapper = div [ classList [("btn-group", True), ("mb-3", True) ] ]
-- Input with a label
labeledInput : String -> String -> (String -> Msg) -> Html Msg
labeledInput s val f =
div [ class "input-group mb-3" ]
[ div [ class "input-group-prepend" ]
[ span [ class "input-group-text" ] [ text s ]
]
, input [ value val, type_ "text", class "form-control", onInput f ] []
]
-- Error view
viewError : Bool -> String -> Html Msg
viewError hide e = div
[ classList
[ ("alert", True)
, ("alert-danger", True)
]
, hidden hide
] [ text e ]
panel : List (Html Msg) -> Html Msg
panel = div [ classList [("card", True), ("p-3", True), ("mb-3", True) ] ]
viewRawCacheModel : Int -> RawCacheModel -> Html Msg
viewRawCacheModel level rcm =
let
updateBlockSize s cm = { cm | blockSize = s}
updateSetCount s cm = { cm | setCount = s}
updateSetSize s cm = { cm | setSize = s}
2019-05-28 19:37:22 -07:00
wrapUpdate f s = ChangeRawModel level (f s)
deleteButton = dangerButton "Delete" (DeleteRawModel level)
params = div []
[ labeledInput "Block size" rcm.blockSize (wrapUpdate updateBlockSize)
, labeledInput "Set count" rcm.setCount (wrapUpdate updateSetCount)
, labeledInput "Set size" rcm.setSize (wrapUpdate updateSetSize)
]
in
panel
[ h3 [] [ text <| "L" ++ String.fromInt (level + 1) ++ " Cache" ]
, buttonWrapper [ deleteButton ]
, params
]
viewRawCacheModelHierarchy : RawCacheModelHierarchy -> Html Msg
viewRawCacheModelHierarchy rcmh =
let
models = div [ class "cache-model-levels" ]
2019-05-28 21:01:35 -07:00
<| List.indexedMap viewRawCacheModel rcmh
2019-05-28 20:08:04 -07:00
translationResult = Result.andThen validateCacheModelHierarchy
<| translateRawCacheModelHierarchy rcmh
errorHtml =
case translationResult of
Ok _ -> viewError True ""
Err e -> viewError False e
newButton = button "Add level" CreateRawModel
useButton = resultButton translationResult "Use hierarchy" (UseHierarchy << Just)
in
div []
[ h2 [] [ text "Cache hierarchy" ]
2019-05-28 20:08:04 -07:00
, errorHtml
, buttonWrapper [ newButton, useButton ]
, models
]
2019-05-28 20:08:04 -07:00
2019-05-28 21:01:35 -07:00
viewCache : Int -> Cache -> Html Msg
viewCache level (cm, cs) =
let
slotLabels =
List.indexedMap (\i _ -> td [] [ text <| String.fromInt i ])
<| List.repeat cm.setSize ()
slotLabel = th [ colspan cm.setSize ] [ text "Slot" ]
2019-05-28 21:01:35 -07:00
allSlotLabels = List.concat <| List.repeat cm.setCount slotLabels
allSlotsLabel = List.repeat cm.setCount slotLabel
setLabels =
List.indexedMap (\i _ -> td [ colspan cm.setSize ] [ text <| String.fromInt i ])
<| List.repeat cm.setCount ()
setLabel = [ th [ colspan <| cm.setSize * cm.setCount ] [ text "Set" ] ]
2019-05-28 21:07:41 -07:00
setRow set =
let
slotHtml s =
case s of
Empty -> td [] [ text "" ]
Used l a -> td [] [ text <| String.fromInt a ]
in
List.map slotHtml set
cacheRow = List.concat <| List.map setRow cs
2019-05-28 21:01:35 -07:00
cacheTable =
table [ class "table" ]
[ tr [ hidden (cm.setCount == 1), class "table-info" ] setLabel
, tr [ hidden (cm.setCount == 1) ] setLabels
, tr [ hidden (cm.setSize == 1), class "table-info" ] allSlotsLabel
, tr [ hidden (cm.setSize == 1) ] allSlotLabels
2019-05-28 21:07:41 -07:00
, tr [] cacheRow
2019-05-28 21:01:35 -07:00
]
2019-05-28 21:07:41 -07:00
2019-05-28 21:01:35 -07:00
in
panel
[ h3 [] [ text <| "L" ++ String.fromInt (level + 1) ++ " Cache" ]
2019-05-28 21:01:35 -07:00
, cacheTable
]
viewCacheHierarchy : CacheHierarchy -> Html Msg
viewCacheHierarchy ch =
let
levels = div [ class "cache-levels" ]
<| List.indexedMap viewCache ch
in
div [] <|
2019-05-28 23:44:53 -07:00
[ h2 [] [ text <| "Cache State" ]
2019-05-28 21:01:35 -07:00
, levels
]
viewAccessView : Model -> AccessView -> Html Msg
viewAccessView m av =
let
currentCache = Maybe.withDefault [] m.hierarchy
in
div []
2019-05-28 22:51:02 -07:00
[ h2 [] [ text "Access Simulation" ]
, buttonWrapper
[ primaryButton "Back" AccessViewBack
, primaryButton "Forward" AccessViewForward
]
2019-05-28 22:51:02 -07:00
, viewAccessLog av
, viewCacheHierarchy <| effectiveCacheHierarchy currentCache av
]
2019-05-28 22:51:02 -07:00
viewAccessLog : AccessView -> Html Msg
viewAccessLog (aes, ap) =
let
resultSpan r =
span [ classList [ ("badge", True), ("badge-danger", True), ("badge-success", r == Hit) ] ]
[ text <| if r == Hit then "Hit" else "Miss" ]
2019-05-28 22:51:02 -07:00
downEvent n ae = div [ class "event" ]
[ text <| "L" ++ String.fromInt (n + 1) ++ " "
2019-05-28 22:51:02 -07:00
, resultSpan ae.result
]
upEvent n ae = div [ class "event" ]
2019-05-28 23:44:53 -07:00
[ text <| "Updated L" ++ String.fromInt (List.length aes - n)
2019-05-28 22:51:02 -07:00
]
events =
case ap of
Done -> []
Down n -> List.indexedMap downEvent <| List.take (n + 1) aes
Up n -> List.indexedMap downEvent aes ++
(List.indexedMap upEvent <| List.drop n aes)
in
div [ ]
2019-05-28 22:51:02 -07:00
[ h3 [] [ text "Simulation events" ]
, div [] events
2019-05-28 22:51:02 -07:00
]
2019-05-28 22:12:36 -07:00
viewAccessInput : Model -> Html Msg
viewAccessInput m =
let
accessButton = maybeButton (String.toInt m.accessInput) "Access address" Access
2019-05-28 22:12:36 -07:00
in
div []
2019-05-28 23:44:53 -07:00
[ h2 [] [ text "Run access simulation" ]
, div [ classList [("alert", True), ("alert-primary", True)] ]
2019-05-28 23:44:53 -07:00
[ text "Please make sure to click \"Use Hierarchy\" to load a hierarchy to simulate."
]
, labeledInput "Access address" m.accessInput ChangeAccessInput
2019-05-28 22:12:36 -07:00
, accessButton
]
2019-05-28 21:01:35 -07:00
2019-05-28 20:08:04 -07:00
viewBase : Model -> Html Msg
viewBase m =
2019-05-28 21:01:35 -07:00
let
rawView =
case m.accessView of
Nothing -> [ viewRawCacheModelHierarchy m.rawHierarchy ]
Just _ -> []
2019-05-28 23:44:53 -07:00
cacheView =
case m.accessView of
Nothing ->
Maybe.withDefault []
<| Maybe.map (List.singleton << viewCacheHierarchy) <| m.hierarchy
Just _ -> []
accessView = Maybe.withDefault [] <| Maybe.map (List.singleton << viewAccessView m) <| m.accessView
accessInputView = [ viewAccessInput m ]
2019-05-28 21:01:35 -07:00
in
div [ class "container" ]
<| rawView ++ cacheView ++ accessView ++ accessInputView