diff options
Diffstat (limited to 'camlsim')
-rw-r--r-- | camlsim/_tags | 3 | ||||
-rw-r--r-- | camlsim/machine.ml | 174 | ||||
-rw-r--r-- | camlsim/netlist.ml | 17 | ||||
-rw-r--r-- | camlsim/netlist_ast.ml | 42 | ||||
-rw-r--r-- | camlsim/netlist_lexer.mll | 37 | ||||
-rw-r--r-- | camlsim/netlist_parser.mly | 70 | ||||
-rw-r--r-- | camlsim/simulator.ml | 67 |
7 files changed, 0 insertions, 410 deletions
diff --git a/camlsim/_tags b/camlsim/_tags deleted file mode 100644 index 503ce62..0000000 --- a/camlsim/_tags +++ /dev/null @@ -1,3 +0,0 @@ -true: use_menhir -<*.ml>: debug -<*.byte>: use_unix, debug
\ No newline at end of file diff --git a/camlsim/machine.ml b/camlsim/machine.ml deleted file mode 100644 index 232cb08..0000000 --- a/camlsim/machine.ml +++ /dev/null @@ -1,174 +0,0 @@ -(* - Système Digital, cours de J.Vuillemin, 2013-2013 - Alex AUVOLAT, ENS INFO 2013 - - Circuit Simulator, machine simulator - - ASSUMPTIONS: - - Only one ROM chip -*) - -open Netlist_ast - -exception Error of string -exception Is_not_modified - -let load_rom filename = - () (* TODO *) - - -type ram_prop = int * int (* addr size ; word size *) -type machine = { - p : program; - mutable vars : value Env.t; - mutable regs : value Env.t; - ram_chips : ram_prop array; - ram : value array array; -} - -let rec two_to_the = function - | 0 -> 1 - | n -> let k = (two_to_the (n/2)) in - if n mod 2 = 1 then 2 * k else k - -let create p = - let ram_chips = Array.of_list - (List.fold_left - (fun x (_, v) -> match v with - | Eram (a, w, _, _, _, _) -> - (a, w)::x - | _ -> x) - [] - p.p_eqs) in - let vars = Env.fold - (fun k v x -> - Env.add k - (match v with - | TBit -> VBit(false) - | TBitArray(n) -> VBitArray(Array.make n false)) - x) - p.p_vars - Env.empty in - { - p = p; - vars = vars; - regs = Env.empty; (* no use putting anything at the moment *) - ram_chips = ram_chips; - ram = Array.map - (fun (a, w) -> Array.make (two_to_the a) (VBitArray (Array.make w false))) - ram_chips; - } - -let read_inputs m f = - List.iter - (fun n -> - m.vars <- Env.add n (f (n, Env.find n m.p.p_vars)) m.vars) - m.p.p_inputs - -let step m = - let get = function - | Avar(x) -> - begin match Env.find x m.vars with - | VBit(x) -> VBit(x) - | VBitArray(u) as s -> - if Array.length u = 1 then VBit(u.(0)) else s - end - | Aconst(x) -> x - in - (* Load register values into dest variables *) - Env.iter - (fun k v -> - m.vars <- Env.add k v m.vars) - m.regs; - (* Do all the logic *) - List.iter - (fun (varname, exp) -> - let evaluate = function - | Earg(k) -> get k - | Ereg(_) -> raise Is_not_modified (* do nothing, registers are handled somewhere else *) - | Enot(k) -> - begin match get k with - | VBit(u) -> VBit (not u) - | VBitArray(u) -> VBitArray(Array.map (fun x -> not x) u) - end - | Ebinop(op, k1, k2) -> - let f = begin match op with - | Or -> (fun x y -> x || y) - | Xor -> (fun x y -> (x || y) && (not (x && y))) - | And -> (fun x y -> x && y) - | Nand -> (fun x y -> not (x && y)) - end in - begin match get k1, get k2 with - | VBit(u), VBit(v) -> VBit(f u v) - | VBitArray(u), VBitArray(v) when Array.length u = Array.length v -> - let r = Array.make (Array.length u) false in - for i = 0 to Array.length u - 1 do - r.(i) <- (f u.(i) v.(i)) - done; VBitArray(r) - | VBit(u), VBitArray(v) -> VBit(f u v.(0)) - | VBitArray(u), VBit(v) -> VBit(f u.(0) v) - | _ -> raise (Error "Incompatible data types.") - end - | Emux (control, in0, in1) -> - begin match get control, get in0, get in1 with - | VBit(u), x, y -> - if u then y else x - | VBitArray(u), VBitArray(a), VBitArray(b) - when Array.length a = Array.length b - && Array.length u = Array.length a -> - let r = Array.make (Array.length u) false in - for i = 0 to Array.length u - 1 do - r.(i) <- (if u.(i) then b.(i) else a.(i)) - done; - VBitArray(r) - | _ -> raise (Error "Invalid data size in mux") - end - | Erom(_, w, _) -> VBitArray(Array.make w false) (* TODO *) - | Eram(_, _, _,_, _, _) -> raise Is_not_modified (* TODO *) - | Econcat(k1, k2) -> - let a1 = match get k1 with - | VBit(u) -> [|u|] - | VBitArray(u) -> u - in - let a2 = match get k2 with - | VBit(u) -> [|u|] - | VBitArray(u) -> u - in - VBitArray(Array.append a1 a2) - | Eslice (first, last, k) -> - begin match get k with - | VBit(u) when first = 0 && last = 0 -> VBit(u) - | VBitArray(u) -> - let r = Array.make (last - first) false in - for i = first to last - 1 do - r.(i - first) <- u.(i) - done; - VBitArray(u) - | _ -> raise (Error "Invalid slicing parameters") - end - | Eselect(id, k) -> - begin match get k with - | VBit(u) when id = 0 -> VBit(u) - | VBitArray(u) -> VBit(u.(id)) - | _ -> raise (Error "Invalid select parameters") - end - in - try - m.vars <- Env.add varname (evaluate exp) m.vars - with Is_not_modified -> ()) - m.p.p_eqs; - (* Saves register values *) - m.regs <- List.fold_left - (fun x (k, v) -> - match v with - | Ereg(n) -> Env.add k (Env.find n m.vars) x - | _ -> x) - Env.empty - m.p.p_eqs - -let print_outputs m f = - List.iter - (fun n -> - f (n, Env.find n m.vars)) - m.p.p_outputs - diff --git a/camlsim/netlist.ml b/camlsim/netlist.ml deleted file mode 100644 index b1d7932..0000000 --- a/camlsim/netlist.ml +++ /dev/null @@ -1,17 +0,0 @@ -exception Parse_error of string - -let find_file filename = - try - open_in filename - with - | _ -> raise (Parse_error "No such file '%s'") - -let read_file filename = - let ic = find_file filename in - let lexbuf = Lexing.from_channel ic in - lexbuf.Lexing.lex_curr_p <- { lexbuf.Lexing.lex_curr_p with Lexing.pos_fname = filename }; - try - Netlist_parser.program Netlist_lexer.token lexbuf - with - | e -> raise (Parse_error ("Syntax error (exception: "^(Printexc.to_string e)^")")) - diff --git a/camlsim/netlist_ast.ml b/camlsim/netlist_ast.ml deleted file mode 100644 index ae16888..0000000 --- a/camlsim/netlist_ast.ml +++ /dev/null @@ -1,42 +0,0 @@ -type ident = string - -module Env = struct - include Map.Make(struct - type t = ident - let compare = compare - end) - - let of_list l = - List.fold_left (fun env (x, ty) -> add x ty env) empty l -end - -type ty = TBit | TBitArray of int -type value = VBit of bool | VBitArray of bool array - -type binop = Or | Xor | And | Nand - -type arg = - | Avar of ident - | Aconst of value - -type exp = - | Earg of arg - | Ereg of ident - | Enot of arg - | Ebinop of binop * arg * arg - | Emux of arg * arg * arg - | Erom of int (*addr size*) * int (*word size*) * arg (*read_addr*) - | Eram of int (*addr size*) * int (*word size*) - * arg (*read_addr*) * arg (*write_enable*) - * arg (*write_addr*) * arg (*data*) - | Econcat of arg * arg - | Eslice of int * int * arg - | Eselect of int * arg - -type equation = ident * exp - -type program = - { p_eqs : equation list; - p_inputs : ident list; - p_outputs : ident list; - p_vars : ty Env.t; } diff --git a/camlsim/netlist_lexer.mll b/camlsim/netlist_lexer.mll deleted file mode 100644 index 78b0410..0000000 --- a/camlsim/netlist_lexer.mll +++ /dev/null @@ -1,37 +0,0 @@ -{ -open Netlist_parser -exception Eof - -let keyword_list = -[ - "AND", AND; - "CONCAT", CONCAT; - "IN", IN; - "INPUT", INPUT; - "MUX", MUX; - "NAND", NAND; - "NOT", NOT; - "OR", OR; - "OUTPUT", OUTPUT; - "RAM", RAM; - "REG", REG; - "ROM", ROM; - "SELECT", SELECT; - "SLICE", SLICE; - "VAR", VAR; - "XOR", XOR; -] - -} - -rule token = parse - [' ' '\t' '\n'] { token lexbuf } (* skip blanks *) - | "=" { EQUAL } - | ":" { COLON } - | "," { COMMA } - | ['0'-'9']+ as lxm { INT(int_of_string lxm) } - | ('_' ? ['A'-'Z' 'a'-'z']('_' ? ['A'-'Z' 'a'-'z' ''' '0'-'9']) * as id) - { let s = Lexing.lexeme lexbuf in - try List.assoc s keyword_list - with Not_found -> NAME id } - | eof { EOF } diff --git a/camlsim/netlist_parser.mly b/camlsim/netlist_parser.mly deleted file mode 100644 index 0908703..0000000 --- a/camlsim/netlist_parser.mly +++ /dev/null @@ -1,70 +0,0 @@ -%{ - open Netlist_ast - - let value_of_int n = - let rec aux n = - let b = - match n mod 10 with - | 0 -> false - | 1 -> true - | i -> Format.eprintf "Unexpected: %d@." i; raise Parsing.Parse_error - in - if n < 10 then - [b] - else - b::(aux (n / 10)) - in - match aux n with - | [] -> Format.eprintf "Empty list@."; raise Parsing.Parse_error - | [b] -> VBit b - | bl -> VBitArray (Array.of_list (List.rev bl)) -%} - -%token <int> INT -%token <string> NAME -%token AND MUX NAND OR RAM ROM XOR REG NOT -%token CONCAT SELECT SLICE -%token COLON EQUAL COMMA VAR IN INPUT OUTPUT -%token EOF - -%start program /* the entry point */ -%type <Netlist_ast.program> program - -%% -program: - INPUT inp=separated_list(COMMA, NAME) - OUTPUT out=separated_list(COMMA, NAME) - VAR vars=separated_list(COMMA, var) IN eqs=list(equ) EOF - { { p_eqs = eqs; p_vars = Env.of_list vars; p_inputs = inp; p_outputs = out; } } - -equ: - x=NAME EQUAL e=exp { (x, e) } - -exp: - | a=arg { Earg a } - | NOT x=arg { Enot x } - | REG x=NAME { Ereg x } - | AND x=arg y=arg { Ebinop(And, x, y) } - | OR x=arg y=arg { Ebinop(Or, x, y) } - | NAND x=arg y=arg { Ebinop(Nand, x, y) } - | XOR x=arg y=arg { Ebinop(Xor, x, y) } - | MUX x=arg y=arg z=arg { Emux(x, y, z) } - | ROM addr=INT word=INT ra=arg - { Erom(addr, word, ra) } - | RAM addr=INT word=INT ra=arg we=arg wa=arg data=arg - { Eram(addr, word, ra, we, wa, data) } - | CONCAT x=arg y=arg - { Econcat(x, y) } - | SELECT idx=INT x=arg - { Eselect (idx, x) } - | SLICE min=INT max=INT x=arg - { Eslice (min, max, x) } - -arg: - | n=INT { Aconst (value_of_int n) } - | id=NAME { Avar id } - -var: x=NAME ty=ty_exp { (x, ty) } -ty_exp: - | /*empty*/ { TBit } - | COLON n=INT { TBitArray n } diff --git a/camlsim/simulator.ml b/camlsim/simulator.ml deleted file mode 100644 index fbb831a..0000000 --- a/camlsim/simulator.ml +++ /dev/null @@ -1,67 +0,0 @@ -(* - 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 - |