Commit 95cbeb04 authored by Quentin Aristote's avatar Quentin Aristote
Browse files

added timing

parent 899bc3a3
......@@ -185,17 +185,65 @@ let rec read_value id t =
| Invalid_argument s -> Format.printf "Wrong input %s.@." s;
read_value id t
(* Prints the variables to output in stdout. *)
let output env vars =
List.iter
(fun id -> try
let value = (eval_addr env (Aconst (Env.find id env))) in
Format.printf "=> %s = %d = 0x%x@\n"
id
value
value
with
| Not_found -> Format.eprintf "Outbound output variable %s.@." id;
exit 2)
vars ;
Format.printf "@."
let load_memory ?(ram_size = 0) in_channel word_size =
let bits = ref [] in
let size = ref 0 in
begin try
while true do
let n = input_byte in_channel in
for j = 7 downto 0 do
bits := bool_of_bit ((n / (pow2 j)) mod 2) :: !bits ;
size := !size + 1
done ;
done ;
with
| End_of_file -> () end ;
close_in in_channel ;
bits := List.rev !bits ;
let amount_filled = !size / word_size in
Array.append
(Array.init
amount_filled
(fun i -> Array.init
word_size
(fun i ->
try
let result = List.hd !bits in
bits := List.tl !bits ;
result
with
| Failure _ -> false)))
(Array.init
(abs (ram_size - amount_filled))
(fun i -> Array.make word_size false))
(* Variables set when the program is called through the command line *)
let print_only = ref false
let number_steps = ref (-1)
let rom_directory = ref "./"
let cycle_duration = ref 0.
let memory_directory = ref "./"
let frequence = ref 0
let default = [|0; 0; 0; 1; 1; 0; 0; 0|]
(* The main part of the program *)
let compile filename =
(* We first check for any combinational cycle and order the netlist. *)
Format.printf "scheduling ...@." ;
let p = try
Scheduler.schedule (Netlist.read_file filename)
with
......@@ -244,12 +292,11 @@ let compile filename =
(* Loads the ROM block associated
to each variable whose value is
read through a ROM statement. *)
Format.printf "loading rom ...@." ;
let file = (* Tries to open <id>.rom *)
try
open_in_bin (!rom_directory ^ id ^ ".rom")
open_in_bin (!memory_directory ^ id ^ ".rom")
with
| Sys_error s when (s = !rom_directory ^
| Sys_error s when (s = !memory_directory ^
id ^
".rom: No such file or directory"
|| s = "Is a directory") ->
......@@ -257,49 +304,43 @@ let compile filename =
"%s.rom either does not exist or is a directory."
id;
exit 2 in
let bits = ref [] in
let size = ref 0 in
begin try
while true do
let n = (* Tries to read from <id>.rom *)
input_byte file in
for j = 7 downto 0 do
bits := bool_of_bit ((n / (pow2 j)) mod 2) :: !bits ;
size := !size + 1
done ;
done ;
with
| End_of_file -> () end ;
close_in file ;
bits := List.rev !bits ;
let r = Array.init
(!size / word_size)
(fun i -> Array.init
word_size
(fun i ->
try
let result = List.hd !bits in
bits := List.tl !bits ;
result
with
| Failure _ -> false)) in
let r = load_memory file word_size in
roms := Env.add id r !roms
| Eram (addr_size, word_size, _, _, _, _) ->
(* Creates an empty RAM block for each variable
whose value is read through a RAM statement *)
rams := Env.add
id
(Array.make
(pow2 addr_size)
(Array.make word_size false))
!rams
(* Loads the RAM block associated
to each variable whose value is
read through a RAM statement. *)
let ram_size = pow2 addr_size in
let r = try
let file = (* Tries to open <id>.ram *)
open_in_bin (!memory_directory ^ id ^ ".ram") in
load_memory file word_size ~ram_size: ram_size
with
| Sys_error s when (s = !memory_directory ^
id ^
".ram: No such file or directory"
|| s = "Is a directory") ->
Array.init
ram_size
(fun i -> Array.make word_size false) in
(* only for the clock, useless elsewhere *)
if List.mem id ["t0" ; "t1" ; "t2" ; "t3" ;
"t4" ; "t5" ; "t6" ; "t7" ;
"_l_329_1629" ; "_l_334_1625"] then (
for i = 8 to 15 do
for j = 0 to word_size - 1 do
r.(i).(j) <- bool_of_bit ((default.(i - 8) /
(pow2 (word_size - j - 1)))
mod 2)
done ;
done) ;
rams := Env.add id r !rams
| _ -> ())
p.p_eqs;
let i = ref 0 in
let start_time = ref (Unix.time ()) in
while !i <> !number_steps do
......@@ -384,45 +425,42 @@ let compile filename =
exit 2
end;
(* --- Output ---
For each variable in p.p_outputs,
we print it in stdout *)
List.iter
(fun id -> try
let value = Env.find id !env in
Format.printf "=> %s = %d@\n"
id
(eval_addr !env (Aconst value))
with
| Not_found -> Format.eprintf "Outbound output variable %s.@." id;
exit 2)
p.p_outputs ;
Format.printf "@." ;
(* --- Update --- *)
let end_time = Unix.time () in
Unix.sleepf (!cycle_duration -. end_time +. !start_time);
start_time := end_time ;
i := 1 + !i;
if !frequence > 0 then (
if (!i - 1) mod !frequence = 0 then (
let end_time = Unix.time () in
Unix.sleepf (1. -. end_time +. !start_time);
start_time := end_time ;
(* --- Output ---
For each variable in p.p_outputs, we print it in stdout.
We do so iff !frequence cycles have been done since the last output. *)
output !env p.p_outputs)
) else
output !env p.p_outputs ;
done)
let main () =
Arg.parse
["-print", Arg.Set print_only, "Only print the result of scheduling";
"-n", Arg.Set_int number_steps, "Number of steps to simulate";
"-rom", Arg.Set_string rom_directory, "Location of the ROM files. By defau"
^ "lt, set to './' . The path of the"
^ "directory must contain a / at its"
^ "end. \n There must be one <"
^ "var>.rom file for each variable <"
^ "var> that accesses the ROM.\n "
^ "Each file should be a binary file"
^ ", that is a text file where chara"
^ "cter n is the ASCII character cor"
^ "responding encoded by the byte RO"
^ "M[n]ROM[n+1]...ROM[n+7].";
"-time", Arg.Int (fun t -> cycle_duration := float_of_int t /. 1000.),
"Minimum time in millisecondes each cycle has to take, set to 0 by default."]
["-p", Arg.Set print_only, " " ^ "See --print." ;
"--print", Arg.Set print_only, " " ^ "Only print the result of scheduling" ;
"-n", Arg.Set_int number_steps, " " ^ "Number of steps to simulate." ;
"-m", Arg.Set_string memory_directory, " " ^ "See --rom." ;
"--memory", Arg.Set_string memory_directory,
"Location of the memory files. By default, set to './'.\n The pa"
^ "th of the directory must contain '/' at its end. \n There mus"
^ "t be one <var>.rom file for each variable <var> that accesses the ROM."
^ "\n <var>.ram files are optional.\n Each file shoul"
^ "d be a binary file, that is a text file where character n is the ASCII"
^ "character encoded by the byte ROM[8*n]...ROM[8*n+7]." ;
"-f", Arg.Set_int frequence, " " ^ "See --frequence." ;
"--freq", Arg.Set_int frequence, " " ^ "Number of cycles per second." ;
"-t", Arg.Tuple (List.init
8
(fun i ->
Arg.Int (fun n ->
default.(i) <- n))), ""]
compile
""
;;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment