diff options
author | Alex AUVOLAT <alex.auvolat@ens.fr> | 2013-10-31 18:15:01 +0100 |
---|---|---|
committer | Alex AUVOLAT <alex.auvolat@ens.fr> | 2013-10-31 18:15:01 +0100 |
commit | f253f98136def21b5e50c5922246e2ddfe315442 (patch) | |
tree | 5ba21cf7a697bccaf58bf8072cb4c283be85a84e /camlsim/simulator.ml | |
parent | 0b269f32dd9b8d349f94793dad44e728473e9f0a (diff) | |
download | SystDigit-Projet-f253f98136def21b5e50c5922246e2ddfe315442.tar.gz SystDigit-Projet-f253f98136def21b5e50c5922246e2ddfe315442.zip |
Simulator started.
Diffstat (limited to 'camlsim/simulator.ml')
-rw-r--r-- | camlsim/simulator.ml | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/camlsim/simulator.ml b/camlsim/simulator.ml new file mode 100644 index 0000000..fbb831a --- /dev/null +++ b/camlsim/simulator.ml @@ -0,0 +1,67 @@ +(* + Système Digital, cours de J.Vuillemin, 2013-2013 + Alex AUVOLAT, ENS INFO 2013 + + Circuit Simulator, main file +*) + +open Netlist_ast + +let num_steps = ref (-1) +let step = ref 0 + +let read_input = function + | (s, TBit) -> Format.printf "%s (1 bit) : @?" s; + let k = ref (read_line()) in + while String.length !k < 1 do + Format.printf "Too short. Retry : "; + k := read_line(); + done; + VBit ((!k).[0] = '1') + | (s, TBitArray(n)) -> Format.printf "%s (%d bits) : @?" s n; + let k = ref (read_line()) in + while String.length !k < 1 do + Format.printf "Too short. Retry : "; + k := read_line(); + done; + let r = Array.make n false in + for i = 0 to n-1 do + r.(i) <- ((!k).[i] = '1') + done; + VBitArray(r) + +let print_output = function + | (n, VBit (b)) -> + Format.printf "%s\t: %d@." n (if b then 1 else 0) + | (n, VBitArray (a)) -> + Format.printf "%s\t: " n; + for i = 0 to Array.length a - 1 do + Format.printf "%d" (if a.(i) then 1 else 0) + done; + Format.printf "@." + +let loadrun filename = + let p = Netlist.read_file filename in + + let machine = Machine.create p in + + while !num_steps > !step || !num_steps == -1 do + step := !step + 1; + Format.printf "Step #%d@." !step; + + Machine.read_inputs machine read_input; + Machine.step machine; + Machine.print_outputs machine print_output + done + +let () = + try + Arg.parse + ["-rom", Arg.String(Machine.load_rom), "Load one ROM file into the simulator (will be used by all ROM chips)."; + "-n", Arg.Set_int num_steps, "Number of steps to simulate"] + loadrun + "" + with + | Netlist.Parse_error s -> Format.eprintf "An error occurred: %s@." s; exit 2 + | Machine.Error s -> Format.eprintf "An error occurred: %s@." s; exit 2 + |