Changeset 138 for Deliverables/D4.1
- Timestamp:
- Sep 29, 2010, 12:25:28 PM (10 years ago)
- Location:
- Deliverables/D4.1
- Files:
-
- 2 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
Deliverables/D4.1/ASMInterpret.ml
r130 r138 13 13 type line = [`P0 | `P1 ];; (* ??? *) 14 14 type continuation = 15 time -> 16 [`In of line * byte * continuation 17 |`Out of (line -> byte -> continuation) ] 15 unit (* 16 [`In of time * line * byte * continuation] option * 17 [`Out of (time -> line -> byte -> continuation) ] 18 *) 18 19 19 20 (* no differentiation between internal and external code memory *) … … 90 91 timer2 = zero `Sixteen; 91 92 92 io = ( fun _ -> assert false)93 io = () 93 94 } 94 95 … … 114 115 let fetch pmem pc = 115 116 let next pc = 116 let (carry, res)= half_add pc (vect_of_int 1 `Sixteen) in117 let _carry, res = half_add pc (vect_of_int 1 `Sixteen) in 117 118 res, WordMap.find pc pmem 118 119 in 119 let instr = WordMap.find pc pmem in 120 let cy, pc = half_add pc (vect_of_int 1 `Sixteen) in 121 let (un, ln) = from_byte instr in 120 let pc,instr = next pc in 121 let un, ln = from_byte instr in 122 122 let bits = (from_nibble un, from_nibble ln) in 123 123 match bits with … … 667 667 let load_code_memory = fold_lefti (fun i mem v -> WordMap.add (vect_of_int i `Sixteen) v mem) WordMap.empty 668 668 669 let load l status = { status with code_memory = load_code_memory l } 669 let load_mem mem status = { status with code_memory = mem } 670 let load l = load_mem (load_code_memory l) 670 671 671 672 module StringMap = Map.Make(String);; 673 module IntMap = Map.Make(struct type t = int let compare = compare end);; 672 674 673 675 let assembly l = … … 677 679 match i with 678 680 `Label s -> pc, StringMap.add s pc labels, costs 679 | `Cost s -> pc, labels, StringMap.add s pccosts680 | `Jmp s681 | `Call s-> pc + 3, labels, costs (*CSC: very stupid: always expand to worst opcode *)681 | `Cost s -> pc, labels, IntMap.add pc s costs 682 | `Jmp _ 683 | `Call _ -> pc + 3, labels, costs (*CSC: very stupid: always expand to worst opcode *) 682 684 | #instruction as i -> 683 685 let i',pc',_ = fetch (load_code_memory (assembly1 i)) (vect_of_int 0 `Sixteen) in 684 686 assert (i = i'); 685 687 (pc + int_of_vect pc',labels, costs) 686 ) (0,StringMap.empty, StringMap.empty) l688 ) (0,StringMap.empty,IntMap.empty) l 687 689 in 688 690 if pc >= 65536 then … … 691 693 List.flatten (List.map 692 694 (function 693 `Label s -> []694 | `Cost s-> []695 `Label _ 696 | `Cost _ -> [] 695 697 | `Jmp s -> 696 698 let pc_offset = StringMap.find s labels in … … 699 701 let pc_offset = StringMap.find s labels in 700 702 assembly1 (`LCALL (`ADDR16 (vect_of_int pc_offset `Sixteen))) 701 | #instruction as i -> assembly1 i) l) 703 | #instruction as i -> assembly1 i) l), costs 702 704 ;; 703 705 704 706 let get_address_of_register status (b1,b2,b3) = 705 let bu, bl = from_byte status.psw in707 let bu,_bl = from_byte status.psw in 706 708 let (_,_,rs1,rs0) = from_nibble bu in 707 709 let base = … … 763 765 ;; 764 766 765 let get_arg_16 status = 766 function 767 `DATA16 w -> w 767 let get_arg_16 _status = function `DATA16 w -> w 768 768 769 769 let get_arg_1 status = … … 820 820 (*CSC: SFR access, TO BE IMPLEMENTED *) 821 821 (* assert false for now. Try to understand what DEC really does *) 822 assert false) 822 prerr_endline ("!!! SFR USED !!!"); 823 status (*assert false*)) 823 824 | `INDIRECT b -> 824 825 let (b1, b2) = from_byte (get_register status (false,false,b)) in … … 956 957 | `CLR `A -> set_arg_8 status (zero `Eight) `A 957 958 | `CLR `C -> set_arg_1 status false `C 958 | `CLR ((`BIT b) as a) -> set_arg_1 status false a959 | `CLR ((`BIT _) as a) -> set_arg_1 status false a 959 960 | `CPL `A -> { status with acc = complement status.acc } 960 961 | `CPL `C -> set_arg_1 status (not $ get_arg_1 status `C) `C … … 1090 1091 let status = { status with low_internal_ram = lower_mem } in 1091 1092 let n1, n2 = from_byte pc_upper_byte in 1092 let (b1,b2,b3, b) = from_word11 a in1093 let (b1,b2,b3,_) = from_word11 a in 1093 1094 let (p1,p2,p3,p4),(p5,_,_,_) = from_nibble n1, from_nibble n2 in 1094 1095 let addr = mk_word (mk_byte (mk_nibble p1 p2 p3 p4) (mk_nibble p5 b1 b2 b3)) pc_lower_byte in … … 1169 1170 1170 1171 let rec execute f s = 1171 let s = execute1 s in1172 1172 let cont = 1173 1173 try f s; true 1174 1174 with Halt -> false 1175 1175 in 1176 if cont then execute f s1176 if cont then execute f (execute1 s) 1177 1177 else s 1178 1178 ;; -
Deliverables/D4.1/ASMInterpret.mli
r101 r138 1 open BitVectors;; 2 open Physical;; 3 1 4 exception CodeTooLarge 2 5 3 type status 6 type time = int;; 7 type line = [`P0 | `P1 ];; (* ??? *) 8 type continuation = 9 unit (* 10 [`In of time * line * byte * continuation] option * 11 [`Out of (time -> line -> byte -> continuation) ] 12 *) 4 13 5 val assembly: ASM.labelled_instruction list -> BitVectors.byte list 14 type status = private 15 { code_memory: WordMap.map; (* can be reduced *) 16 low_internal_ram: Byte7Map.map; 17 high_internal_ram: Byte7Map.map; 18 external_ram: WordMap.map; 19 20 pc: word; 21 22 (* sfr *) 23 p0: byte; 24 sp: byte; 25 dpl: byte; 26 dph: byte; 27 pcon: byte; 28 tcon: byte; 29 tmod: byte; 30 tl0: byte; 31 tl1: byte; 32 th0: byte; 33 th1: byte; 34 p1: byte; 35 scon: byte; 36 sbuf: byte; 37 p2: byte; 38 ie: byte; 39 p3: byte; 40 ip: byte; 41 psw: byte; 42 acc: byte; 43 b: byte; 44 45 clock: time; 46 timer0: word; 47 timer1: word; 48 timer2: word; (* can be missing *) 49 io: continuation 50 } 51 52 module IntMap: Map.S with type key = int 53 54 val assembly: 55 ASM.labelled_instruction list -> BitVectors.byte list (*ASM.instruction list * symbol_table *) * string IntMap.t 56 57 (* 58 val link: 59 (ASM.instruction list * symbol_table * cost_map) list -> BitVectors.byte list 60 *) 6 61 7 62 val initialize: status 8 63 64 val load_mem: Physical.WordMap.map -> status -> status 9 65 val load: BitVectors.byte list -> status -> status 10 66 … … 15 71 the processor never halts. *) 16 72 val execute: (status -> unit) -> status -> status 73 74 val fetch: Physical.WordMap.map -> word -> ASM.instruction * word * int -
Deliverables/D4.1/BitVectors.ml
r137 r138 101 101 aux 1 (List.rev v) 102 102 103 let string_of_vect v =104 String.concat "" (List.map (function false -> "0" | _ -> "1") v)105 106 103 let size_lookup = 107 104 function … … 132 129 true :: aux d 133 130 131 let rec pad i l = if i = 0 then l else false :: (pad (i - 1) l) 132 134 133 let vect_of_int i size = 135 134 let big_list = List.rev (aux i) in 136 if List.length big_list > (size_lookup size)then135 if List.length big_list > size_lookup size then 137 136 raise (Invalid_argument "Size not big enough") 138 137 else 139 let diff = (size_lookup size) - (List.length big_list)in140 pad falsediff big_list138 let diff = size_lookup size - List.length big_list in 139 pad diff big_list 141 140 142 let zero size = pad false(size_lookup size) []141 let zero size = pad (size_lookup size) [] -
Deliverables/D4.1/IntelHex.ml
r137 r138 3 3 open Util;; 4 4 open Parser;; 5 6 exception WrongFormat of string 5 7 6 8 type intel_hex_entry_type = … … 13 15 type intel_hex_entry = 14 16 { 15 record_length: nibble * nibble;16 record_addr: nibble * nibble * nibble * nibble;17 record_length: byte; 18 record_addr: word; 17 19 record_type: intel_hex_entry_type; 18 data_field: nibble list;19 data_checksum: nibble * nibble20 data_field: byte list; 21 data_checksum: byte 20 22 } 21 23 ;; … … 32 34 | 'F' -> 15 | _ -> assert false 33 35 36 (* CSC: tipare piu' strettamente: prendere la taglia del vettore in input 37 come taglia della stringa *) 34 38 let vect_of_hex_string s size = 35 39 let int_of_hex_string h = … … 46 50 ;; 47 51 48 let hex_string_of_vect v = 49 let hex_string_of_int i = 50 let digit_lookup = 51 function 52 0 -> "0" | 1 -> "1" | 2 -> "2" 53 | 3 -> "3" | 4 -> "4" | 5 -> "5" 54 | 6 -> "6" | 7 -> "7" | 8 -> "8" 55 | 9 -> "9" | 10 -> "A" | 11 -> "B" 56 | 12 -> "C" | 13 -> "D" | 14 -> "E" 57 | 15 -> "F" | _ -> assert false in 58 59 let rec aux i = 60 if i < 16 then 61 digit_lookup i 62 else 63 let div = i / 16 in 64 let rem = i mod 16 in 65 aux div ^ digit_lookup rem 66 in 67 aux i 68 in 69 let vect_int = int_of_vect v in 70 hex_string_of_int vect_int 71 ;; 52 let hex_string_of_vect v = Printf.sprintf "%0 2X" (int_of_vect v);; 72 53 73 54 let intel_hex_entry_type_of_int = … … 80 61 ;; 81 62 82 let int_of_intel_hex_ type =83 63 let int_of_intel_hex_entry_type = 64 function 84 65 Data -> 0 85 66 | End -> 1 … … 88 69 ;; 89 70 90 let prs_ length=91 prs_hex_digit >>= 71 let prs_byte = 72 prs_hex_digit >>= 92 73 fun a -> prs_hex_digit >>= 93 fun b -> return (vect_of_hex_string (String.make 1 a) `Four, 94 vect_of_hex_string (String.make 1 b) `Four) 95 ;; 96 97 let prs_addr = 74 fun b -> return $ vect_of_hex_string (String.make 1 a ^ String.make 1 b) `Eight 75 ;; 76 77 let prs_word = 98 78 prs_hex_digit >>= 99 79 fun a -> prs_hex_digit >>= 100 80 fun b -> prs_hex_digit >>= 101 81 fun c -> prs_hex_digit >>= 102 fun d -> return $ (vect_of_hex_string (String.make 1 a) `Four, 103 vect_of_hex_string (String.make 1 b) `Four, 104 vect_of_hex_string (String.make 1 c) `Four, 105 vect_of_hex_string (String.make 1 d) `Four) 106 ;; 82 fun d -> return $ vect_of_hex_string (String.make 1 a ^ String.make 1 b ^ String.make 1 c ^ String.make 1 d) `Sixteen 83 ;; 84 85 let prs_length = prs_byte;; 86 let prs_data len = prs_exact len prs_byte 87 let prs_checksum = prs_byte;; 88 let prs_addr = prs_word;; 107 89 108 90 let prs_type = … … 115 97 return $ intel_hex_entry_type_of_int total 116 98 117 let prs_data len = 118 prs_exact len $ prs_hex_digit >>= 119 fun a -> 120 let a_as_strs = List.map (String.make 1) a in 121 let byte_data = List.map (fun x -> vect_of_hex_string x `Four) a_as_strs in 122 return $ byte_data 123 ;; 124 125 let prs_checksum = 126 prs_hex_digit >>= 127 fun a -> prs_hex_digit >>= 128 fun b -> return (vect_of_hex_string (String.make 1 a) `Four, 129 vect_of_hex_string (String.make 1 b) `Four) 130 ;; 99 let add_bytes v = 100 let r = List.rev v in 101 let rec aux (cry, bs) = 102 function 103 [] -> (cry, bs) 104 | hd::tl -> 105 aux (half_add hd bs) tl 106 in 107 aux (false, (vect_of_int 0 `Eight)) r 108 109 let checksum_valid hex_entry = 110 let ty = (flip vect_of_int $ `Eight) $ int_of_intel_hex_entry_type hex_entry.record_type in 111 let addr1,addr2 = from_word hex_entry.record_addr in 112 let _, total = add_bytes (hex_entry.record_length :: addr1 :: addr2 :: ty :: hex_entry.data_field) in 113 let _,total = half_add (vect_of_int 1 `Eight) $ complement total in 114 hex_entry.data_checksum = total 115 131 116 132 117 let prs_intel_hex_record = 133 118 prs_char ':' >>= 134 fun a-> prs_length >>=119 fun _ -> prs_length >>= 135 120 fun b -> prs_addr >>= 136 121 fun c -> prs_type >>= 137 122 fun d -> 138 let (l_u_b, l_l_b) = b in 139 let len = int_of_vect (mk_byte l_u_b l_l_b) in 140 prs_data (2 * len) >>= 123 let len = int_of_vect b in 124 prs_data len >>= 141 125 fun e -> prs_checksum >>= 142 fun f -> 143 return $ { 144 record_length = b; 126 fun f -> prs_eof >>= 127 fun _ -> 128 let entry = 129 { record_length = b; 145 130 record_addr = c; 146 131 record_type = d; 147 132 data_field = e; 148 data_checksum = f 149 } 133 data_checksum = f } 134 in 135 if checksum_valid entry then 136 return entry 137 else 138 prs_zero 150 139 ;; 151 140 … … 164 153 long, and all addr strings should be four. *) 165 154 let string_of_intel_hex_entry entry = 166 let record_length_l, record_length_r = entry.record_length in 167 let record_addr_1, record_addr_2, record_addr_3, record_addr_4 = entry.record_addr in 168 let data_checksum_l, data_checksum_r = entry.data_checksum in 169 let length_string = hex_string_of_vect $ mk_byte record_length_l record_length_r in 170 let addr_string = hex_string_of_vect record_addr_1 ^ 171 hex_string_of_vect record_addr_2 ^ 172 hex_string_of_vect record_addr_3 ^ 173 hex_string_of_vect record_addr_4 in 174 let checksum_string = hex_string_of_vect data_checksum_l ^ 175 hex_string_of_vect data_checksum_r in 155 let length_string = hex_string_of_vect entry.record_length in 156 let addr_string = hex_string_of_vect entry.record_addr in 157 let checksum_string = hex_string_of_vect entry.data_checksum in 176 158 let type_string = 177 159 match entry.record_type with … … 194 176 aux strs 195 177 196 let add_bytes v = 197 let r = List.rev v in 198 let rec aux (cry, bs) = 199 function 200 [] -> (cry, bs) 201 | hd::tl -> 202 aux (half_add hd bs) tl 203 in 204 aux (false, (vect_of_int 0 `Eight)) r 205 206 (* DPM: Non exhaustive pattern as we always check list length is even! *) 207 let rec lift_to_bytes = 178 let intel_hex_of_file path = 179 let fd = open_in path in 180 let rec aux () = 181 match try Some (input_line fd) with End_of_file -> None with 182 None -> [] 183 | Some txt -> 184 let read = prs_intel_hex_record (Parser.chars_of_string txt) in 185 let read = 186 match read with 187 [x,[]] -> x 188 | _ -> raise (WrongFormat txt) 189 in 190 read::aux () 191 in 192 aux () 193 ;; 194 195 let rec load_from mem addr = 196 function 197 [] -> mem 198 | he::tl -> 199 load_from (Physical.WordMap.add addr he mem) (snd (BitVectors.half_add addr (BitVectors.vect_of_int 1 `Sixteen))) tl 200 ;; 201 202 let process_intel_hex = 203 let rec aux mem = 208 204 function 209 [] -> [] 210 | hd::hd'::tl -> 211 (mk_byte hd hd')::(lift_to_bytes tl) 212 213 let checksum_valid hex_entry = 214 if List.length hex_entry.data_field mod 2 <> 0 then 215 false 216 else 217 let chk_1, chk_2 = hex_entry.data_checksum in 218 let checksum = mk_byte chk_1 chk_2 in 219 let len_1, len_2 = hex_entry.record_length in 220 let ln_total = mk_byte len_1 len_2 in 221 let ty_total = (flip vect_of_int $ `Eight) $ int_of_intel_hex_type hex_entry.record_type in 222 let adr_1, adr_2, adr_3, adr_4 = hex_entry.record_addr in 223 let ad_total1 = mk_byte adr_1 adr_2 in 224 let ad_total2 = mk_byte adr_3 adr_4 in 225 let _, dt_total = add_bytes <*> lift_to_bytes $ hex_entry.data_field in 226 let _, total = add_bytes [ln_total; ad_total1; ad_total2; ty_total; dt_total] in 227 let _,total = half_add (vect_of_int 1 `Eight) $ complement total in 228 checksum = total 229 230 (* DPM: Debug 231 let Some entry = intel_hex_format_of_string ":10002F00EFF88DF0A4FFEDC5F0CEA42EFEEC88F016";; 232 checksum_valid $ List.hd entry;; 233 *) 205 [] -> assert false 206 | he::tl -> 207 match he.record_type with 208 End -> assert (tl = []); mem 209 | Data -> aux (load_from mem he.record_addr he.data_field) tl 210 | _ -> assert false 211 in 212 aux Physical.WordMap.empty 213 ;; -
Deliverables/D4.1/IntelHex.mli
r133 r138 12 12 type intel_hex_entry = 13 13 { 14 record_length: nibble * nibble;15 record_addr: nibble * nibble * nibble * nibble;14 record_length: byte; 15 record_addr: word; 16 16 record_type: intel_hex_entry_type; 17 data_field: nibble list; 18 data_checksum: nibble * nibble 19 };; 17 data_field: byte list; 18 data_checksum: byte 19 } 20 ;; 20 21 21 22 type intel_hex_format = intel_hex_entry list;; … … 27 28 val hex_string_of_vect: 'a vect -> string;; 28 29 30 exception WrongFormat of string 31 32 val intel_hex_of_file: string -> intel_hex_format 33 val process_intel_hex: intel_hex_format -> Physical.WordMap.map 34 29 35 val checksum_valid: intel_hex_entry -> bool;; -
Deliverables/D4.1/Makefile
r28 r138 1 1 all: 2 ocamlbuild ASMInterpret.native2 ocamlbuild -cflags "-w Ae" test.native 3 3 4 4 .PHONY: all -
Deliverables/D4.1/Parser.ml
r130 r138 2 2 open BitVectors;; 3 3 open ASM;; 4 5 let chars_of_string s = 6 let len = String.length s in 7 let rec aux n = 8 if n < len then 9 s.[n] :: aux (n + 1) 10 else 11 [] 12 in 13 aux 0 14 ;; 4 15 5 16 type 'a parser = char list -> ('a * char list) list … … 13 24 List.concat $ List.map (fun (a, x') -> (g a) x') frst 14 25 15 let prs_zero = fun x-> []26 let prs_zero = fun _ -> [] 16 27 ;; 28 29 let prs_eof = function [] -> [(),[]] | _ -> [];; 17 30 18 31 let prs_predicate p = … … 35 48 match (f ++ g) x with 36 49 [] -> [] 37 | hd:: tl-> [hd]50 | hd::_ -> [hd] 38 51 ;; 39 52 -
Deliverables/D4.1/Parser.mli
r130 r138 1 val chars_of_string: string -> char list 2 1 3 type 'a parser = char list -> ('a * char list) list 2 4 … … 4 6 val (>>=): 'a parser -> ('a -> 'b parser) -> 'b parser 5 7 val prs_zero: 'a parser 8 val prs_eof: unit parser 6 9 val prs_predicate: (char -> bool) -> char parser;; 7 10 val prs_many1: 'a parser -> ('a list) parser;; -
Deliverables/D4.1/Pretty.ml
r122 r138 224 224 | `XRL(`U2(`DIRECT b1, `DATA b2)) -> 225 225 "XRL direct #data (" ^ string_of_vect b2 ^ ")" 226 | _ -> "BUG: Unimplemented!" -
Deliverables/D4.1/Pretty.mli
r122 r138 1 val pp_instruction: ASM.labelled_instruction-> string1 val pp_instruction: [< ASM.labelled_instruction] -> string
Note: See TracChangeset
for help on using the changeset viewer.