Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ryukzak committed Dec 25, 2023
1 parent a15a998 commit 5365b5b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
30 changes: 14 additions & 16 deletions ocaml/code.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Json = Yojson.Safe
module JsonU = Yojson.Safe.Util
module JsonUtil = Yojson.Safe.Util

(************************************************************)
(* Code parsing from json file. *)
Expand All @@ -17,17 +17,14 @@ let opcode_of_string = function
| "jmp" -> Jmp
| "jz" -> Jz
| "halt" -> Halt
| text -> failwith ("isa_from_string: invalid string: " ^ text)
| text -> failwith ("opcode_of_string: invalid string: " ^ text)

type term = { line : int; pos : int; symbol : string } [@@deriving show]

let term_fo_json_list = function
let term_of_json_list = function
| `List [ line; pos; symbol ] ->
{
line = JsonU.to_int line;
pos = JsonU.to_int pos;
symbol = JsonU.to_string symbol;
}
JsonUtil.
{ line = to_int line; pos = to_int pos; symbol = to_string symbol }
| _ -> failwith "term_from_json_list: invalid json list"

type instruction = {
Expand All @@ -38,14 +35,15 @@ type instruction = {
}
[@@deriving show]

let instruction_from_json json =
let index = JsonU.member "index" json |> JsonU.to_int
and opcode = JsonU.member "opcode" json |> JsonU.to_string |> opcode_of_string
and arg = JsonU.member "arg" json |> JsonU.to_int_option
and term = JsonU.member "term" json |> JsonU.to_option term_fo_json_list in
{ index; opcode; arg; term }
let instruction_of_json json =
JsonUtil.(
let index = member "index" json |> to_int
and opcode = member "opcode" json |> to_string |> opcode_of_string
and arg = member "arg" json |> to_int_option
and term = member "term" json |> to_option term_of_json_list in
{ index; opcode; arg; term })

let program_from_file filename =
Json.from_file filename |> JsonU.to_list
|> List.map instruction_from_json
Json.from_file filename |> JsonUtil.to_list
|> List.map instruction_of_json
|> Array.of_list
16 changes: 4 additions & 12 deletions ocaml/datapath.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,14 @@ let data_memory_sel : opcode -> data_memory_sel = function

let signal_data_memory_wr sel data_path =
match sel with
| Inc ->
| Inc | Dec ->
{
data_path with
data_memory =
(let mem = Array.copy data_path.data_memory in
(let mem = Array.copy data_path.data_memory
and change = if sel == Inc then 1 else -1 in
mem.(data_path.data_address) <-
data_path.data_memory.(data_path.data_address) + 1;
mem);
}
| Dec ->
{
data_path with
data_memory =
(let mem = Array.copy data_path.data_memory in
mem.(data_path.data_address) <-
data_path.data_memory.(data_path.data_address) - 1;
data_path.data_memory.(data_path.data_address) + change;
mem);
}
| Input -> (
Expand Down
2 changes: 1 addition & 1 deletion ocaml/microcoded.ml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ let mpc_of_opcode : opcode -> int = function
signals with arguments (selectors).
If signal is not presented in the list -- that means zero signals. *)
let mprogram =
let mprogram: signal list array =
[|
(* Instructin Fetch *)
(* 0 *) [ LatchMpc Opcode ];
Expand Down

0 comments on commit 5365b5b

Please sign in to comment.