From 672d321616adffe2f643a447bcd83a191061fa0b Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 28 May 2019 21:35:46 -0700 Subject: [PATCH] Add a "cache view" type to step through a cache access. --- src/CacheSim/AccessView.elm | 54 +++++++++++++++++++++++++++++++++++++ src/CacheSim/Model.elm | 3 +++ src/CacheSim/Update.elm | 10 +++++++ src/Main.elm | 2 ++ 4 files changed, 69 insertions(+) create mode 100644 src/CacheSim/AccessView.elm diff --git a/src/CacheSim/AccessView.elm b/src/CacheSim/AccessView.elm new file mode 100644 index 0000000..c52232d --- /dev/null +++ b/src/CacheSim/AccessView.elm @@ -0,0 +1,54 @@ +module CacheSim.AccessView exposing (..) +import CacheSim.Cache exposing (..) +import CacheSim.Hierarchy exposing (..) + +type AccessPosition = Down Int | Up Int | Done +type alias AccessView = (List (AccessEffect Cache), AccessPosition) + +accessPositionForward : Int -> AccessPosition -> AccessPosition +accessPositionForward depth av = + case av of + Down n -> if n == depth - 1 then Up n else Down (n + 1) + Up n -> if n == 0 then Done else Up (n - 1) + Done -> Done + +accessPositionBack : Int -> AccessPosition -> AccessPosition +accessPositionBack depth av = + case av of + Down 0 -> Down 0 + Down n -> Down (n-1) + Up n -> if n == (depth - 1) then Down n else Up (n + 1) + Done -> Up 0 + +accessPositionDone : AccessPosition -> Bool +accessPositionDone av = + case av of + Done -> True + _ -> False + +accessViewForward : AccessView -> AccessView +accessViewForward (l, ap) = (l, accessPositionForward (List.length l) ap) + +accessViewBack : AccessView -> AccessView +accessViewBack (l, ap) = (l, accessPositionBack (List.length l) ap) + +accessViewDone : AccessView -> Bool +accessViewDone (_, ap) = accessPositionDone ap + +effectiveCacheHierarchy : CacheHierarchy -> AccessView -> CacheHierarchy +effectiveCacheHierarchy c (l, ap) = + let + finalContents = List.map .output l + unaccessed = List.drop (List.length l) c + notDone = + case ap of + Done -> [] + Down _ -> List.take (List.length l) c + Up n -> List.take n c + done = + case ap of + Done -> finalContents + Down _ -> [] + Up n -> List.drop n finalContents + in + notDone ++ done ++ unaccessed diff --git a/src/CacheSim/Model.elm b/src/CacheSim/Model.elm index f188915..3bc3a1d 100644 --- a/src/CacheSim/Model.elm +++ b/src/CacheSim/Model.elm @@ -1,10 +1,12 @@ module CacheSim.Model exposing (..) import CacheSim.Raw exposing (..) import CacheSim.Hierarchy exposing (..) +import CacheSim.AccessView exposing (..) type alias Model = { rawHierarchy : RawCacheModelHierarchy , hierarchy : Maybe CacheHierarchy + , accessView : Maybe AccessView } type alias Flags = () type Msg @@ -12,3 +14,4 @@ type Msg | CreateRawModel | DeleteRawModel Int | UseHierarchy (Maybe CacheModelHierarchy) + | Access Int diff --git a/src/CacheSim/Update.elm b/src/CacheSim/Update.elm index c69d6c0..110736a 100644 --- a/src/CacheSim/Update.elm +++ b/src/CacheSim/Update.elm @@ -3,6 +3,7 @@ import CacheSim.Model exposing (..) import CacheSim.Hierarchy exposing (..) import CacheSim.Raw exposing (..) import CacheSim.IntMap exposing (..) +import CacheSim.AccessView exposing (..) updateChangeRawModel : Int -> (RawCacheModel -> RawCacheModel) -> Model -> (Model, Cmd Msg) updateChangeRawModel l f m = @@ -36,3 +37,12 @@ updateUseHierarchy cmh m = cmd = Cmd.none in (newModel, cmd) + +updateAccess : Int -> Model -> (Model, Cmd Msg) +updateAccess i m = + let + accessResult = Maybe.map (accessCacheHierarchy i) m.hierarchy + newModel = m + cmd = Cmd.none + in + (newModel, cmd) diff --git a/src/Main.elm b/src/Main.elm index 447ba9f..0f83b95 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -18,6 +18,7 @@ init f = initialModel = { rawHierarchy = testCacheModelHierarchy , hierarchy = Nothing + , accessView = Nothing } in (initialModel, Cmd.none) @@ -35,6 +36,7 @@ update msg m = CreateRawModel -> updateCreateRawModel m DeleteRawModel i -> updateDeleteRawModel i m UseHierarchy cmh -> updateUseHierarchy cmh m + Access i -> updateAccess i m subscriptions : Model -> Sub Msg subscriptions m = Sub.none