Added Mobile Layout and comments

This commit is contained in:
2020-12-28 17:05:59 +01:00
parent 1b7a502cd4
commit 9b789a1378
6 changed files with 506 additions and 250 deletions

View File

@ -16,7 +16,7 @@ import Json.Decode as JD
import Html.Events exposing (targetValue)
import Http
modelVersion = 1
modelVersion = 2
examplesListUrl = "examples-list.json"
-- Note that general Stuff is at the end of the document
@ -33,7 +33,7 @@ port localStorageRecieve : (String -> msg) -> Sub msg
type alias PC =
{ ram : List Int
{ ram : List (Int, String)
, dataBus : Int
, addressBus : Int
, instructionReg : Int
@ -138,19 +138,19 @@ uCodeMaps =
]
initialRam : List Int
initialRam : List (Int, String)
initialRam =
[ 100005 -- 000 -- LoadA #005
, 300000 -- 001 -- IncA
, 200005 -- 002 -- StoreA #005
, 400000 -- 003 -- JMP #000
, 0 -- 004
, 5 -- 005
, 0 -- 006
, 0 -- 007
, 0 -- 008
, 0 -- 009
, 0 -- 010
[ (100005, "LoadA #005") -- 000 -- LoadA #005
, (300000, "IncA") -- 001 -- IncA
, (200005, "StoreA #005") -- 002 -- StoreA #005
, (400000, "JMP #000") -- 003 -- JMP #000
, (0,"") -- 004
, (5,"val") -- 005
, (0, "") -- 006
, (0, "") -- 007
, (0, "") -- 008
, (0, "") -- 009
, (0, "") -- 010
]
@ -238,6 +238,7 @@ type Msg
| MsgRamEditAddress Int String
| MsgRamEditInstr Int String
| MsgRamAddBelow
| MsgRamEditComment Int String
| MsgCuEditAction Int String
| MsgCuAddBelow
| MsgCuInstrRegEditAddr String
@ -291,10 +292,10 @@ update msg model =
case String.toInt may_int of
Just int ->
let
(inst,_) = seperateInstructionsEntry ( valueAtInt addr model.pc.ram )
(inst,_) = seperateInstructionsEntry ( Tuple.first <| valueAtRam addr model.pc.ram )
new_val = inst * 100000 + int
old_pc = model.pc
new_pc = { old_pc | ram = (changeAtInt addr new_val old_pc.ram) }
new_pc = { old_pc | ram = (changeAtRam addr new_val old_pc.ram) }
in
updateModel { model | pc = new_pc }
_ -> (model, Cmd.none)
@ -303,10 +304,10 @@ update msg model =
case String.toInt may_int of
Just int ->
let
(_,address) = seperateInstructionsEntry ( valueAtInt addr model.pc.ram )
(_,address) = seperateInstructionsEntry ( Tuple.first <| valueAtRam addr model.pc.ram )
new_val = int * 100000 + address
old_pc = model.pc
new_pc = { old_pc | ram = (changeAtInt addr new_val old_pc.ram) }
new_pc = { old_pc | ram = (changeAtRam addr new_val old_pc.ram) }
in
updateModel { model | pc = new_pc }
_ -> ( model, Cmd.none )
@ -314,10 +315,18 @@ update msg model =
MsgRamAddBelow ->
let
old_pc = model.pc
new_pc = {old_pc | ram = old_pc.ram ++ [0]}
new_pc = {old_pc | ram = old_pc.ram ++ [(0, "")]}
in
updateModel {model | pc = new_pc}
MsgRamEditComment addr str ->
let
(val, _) = valueAtRam addr model.pc.ram
old_pc = model.pc
new_pc = { old_pc | ram = changeAt addr (val, str) (0,"") old_pc.ram }
in
updateModel { model | pc = new_pc }
MsgCuEditAction addr may_action ->
case string2uAction may_action of
Just action ->
@ -328,7 +337,7 @@ update msg model =
_ -> ( model, Cmd.none )
MsgCuAddBelow ->
updateModel {model | uCode = model.uCode ++ [ ActNothing ]}
updateModel {model | uCode = model.uCode ++ List.repeat 10 ActNothing}
MsgCuInstrRegEditAddr text ->
case String.toInt text of
@ -522,6 +531,11 @@ executeInstruction model =
encodeModel : Model -> String
encodeModel model =
let
tuple2Encoder : (a -> JE.Value) -> (b -> JE.Value) -> (a, b) -> JE.Value
tuple2Encoder enc1 enc2 (val1, val2) =
JE.list identity [ enc1 val1, enc2 val2 ]
in
JE.object
[ ( "model-version", JE.int modelVersion )
, ( "pc"
@ -532,7 +546,7 @@ encodeModel model =
, ("programmCounter", JE.int model.pc.programmCounter )
, ("uCounter", JE.int model.pc.uCounter )
, ("accumulator", JE.int model.pc.accumulator )
, ("ram", JE.list JE.int model.pc.ram)
, ("ram", JE.list (tuple2Encoder JE.int JE.string) model.pc.ram )
]
)
, ( "uCode", JE.list JE.string (List.map uAction2String model.uCode) )
@ -564,13 +578,20 @@ exampleListDecoder =
( JD.field "enabled" (JD.map (\s -> s == 1) JD.int) )
modelDecoder : JD.Decoder Model
modelDecoder =
modelDecoder =
let
tuple2Decoder : JD.Decoder a -> JD.Decoder b -> JD.Decoder (a,b)
tuple2Decoder enc1 enc2 =
JD.map2 Tuple.pair
(JD.index 0 enc1)
(JD.index 1 enc2)
in
JD.map3
(\a b c -> Model a b c [] False Waiting Waiting)
( JD.field "pc"
( JD.map7
PC
(JD.field "ram" (JD.list JD.int))
(JD.field "ram" <| JD.list <| tuple2Decoder JD.int JD.string)
(JD.field "dataBus" JD.int)
(JD.field "addressBus" JD.int)
(JD.field "instructionReg" JD.int)
@ -657,11 +678,11 @@ viewPC model =
]
, button [ onClick MsgToggleLoadExample ] [ text "Load Example" ]
]
, div [ class "grid-fullwidth" ] [ lazy viewAddressBus model ]
, div [ class "grid-fullwidth", class "grid-addressbus" ] [ lazy viewAddressBus model ]
, lazy viewRam model
, lazy viewCu model
, lazy viewAlu model
, div [ class "grid-fullwidth" ] [ lazy viewDataBus model ]
, div [ class "grid-fullwidth", class "grid-databus" ] [ lazy viewDataBus model ]
, lazy viewExamples model
, lazy viewExamplesLoaderError model
]
@ -678,6 +699,7 @@ viewRam model =
[ Html.tr []
[ Html.th [ class "address" ] [ text "Addr" ]
, Html.th [] [ text "Value" ]
, Html.th [] [ text "Comment" ]
]
]
, lazy viewRamContent model
@ -702,13 +724,13 @@ viewRamContent model =
indexedRam =
List.indexedMap Tuple.pair model.pc.ram
ram2table : ( Int, Int ) -> Html Msg
ram2table : ( Int, (Int, String) ) -> Html Msg
ram2table entry =
let
id =
Tuple.first entry
val =
(val,comment) =
Tuple.second entry
(instruction, address) = seperateInstructionsEntry val
@ -719,21 +741,28 @@ viewRamContent model =
, Html.td [ class "num " ]
[ Html.input
[ HAttr.type_ "number"
, HAttr.value (addLeadingZero instruction 3)
, onInput (MsgRamEditInstr id)
, HAttr.value <| addLeadingZero instruction 3
, onInput <| MsgRamEditInstr id
, class "ram-entry"
, class "instruction"
]
[]
, Html.input
[ HAttr.type_ "number"
, HAttr.value (addLeadingZero address 5)
, onInput (MsgRamEditAddress id)
, HAttr.value <| addLeadingZero address 5
, onInput <| MsgRamEditAddress id
, class "ram-entry"
, class "address"
]
[]
]
, Html.td[ class "comment" ]
[ Html.input
[ HAttr.type_ "text"
, HAttr.value comment
, onInput <| MsgRamEditComment id ]
[]
]
]
in
Html.tbody []
@ -746,6 +775,7 @@ viewRamContent model =
[ onClick MsgRamAddBelow ]
[ text "Add Entry"]
]
, Html.td [][]
]
]
)
@ -1014,7 +1044,7 @@ actRam2DataBus pc =
pc.addressBus
db =
valueAtInt ab pc.ram
Tuple.first <| valueAtRam ab pc.ram
in
{ pc | dataBus = db }
@ -1098,7 +1128,7 @@ actDataBus2Ram : PC -> PC
actDataBus2Ram pc =
let
newRam =
changeAtInt pc.addressBus pc.dataBus pc.ram
changeAtRam pc.addressBus pc.dataBus pc.ram
in
{ pc | ram = newRam }
@ -1108,14 +1138,14 @@ actDataBus2Ram pc =
-- HELPERS
valueAtInt : Int -> List Int -> Int
valueAtInt n l =
valueAtRam : Int -> List (Int, String) -> (Int, String)
valueAtRam n l =
case valueAt n l of
Just a ->
a
Nothing ->
0
(0, "")
valueAt : Int -> List a -> Maybe a
@ -1123,9 +1153,12 @@ valueAt n l =
List.head (List.drop n l)
changeAtInt : Int -> Int -> List Int -> List Int
changeAtInt pos newVal list =
changeAt pos newVal 0 list
changeAtRam : Int -> Int -> List (Int, String) -> List (Int,String)
changeAtRam pos newVal list =
let
(_,comment) = valueAtRam pos list
in
changeAt pos (newVal, comment) (0, "") list
changeAtUCode : Int -> UAction -> List UAction -> List UAction