source: Deliverables/D4.1/Physical.ml @ 142

Last change on this file since 142 was 128, checked in by mulligan, 10 years ago

Some utility functions useful across modules moved into new file Util.
Started working on parser for Intel hex file format. Moved
physical.ml*' to Physical.ml*' for naming consistency across modules.

File size: 2.0 KB
Line 
1open BitVectors;;
2
3exception Byte7_conversion
4
5module type Map =
6 sig
7  type key
8  type map
9   val empty : map
10   val find : key -> map -> byte
11   val add : key -> byte -> map -> map
12 end
13;;
14
15module Byte7Map : Map with type key = byte7 =
16struct
17  include Map.Make (struct type t = byte7 let compare = Pervasives.compare end)
18  type map = byte t
19  let find k m =
20    try
21      find k m
22    with Not_found -> zero `Eight
23end;;
24
25module WordMap : Map with type key = word =
26struct
27  include Map.Make (struct type t = word let compare = Pervasives.compare end)
28  type map = byte t
29  let find k m =
30    try
31      find k m
32    with Not_found -> zero `Eight
33end;;
34
35let int_of_bit =
36  function
37    false -> 0
38  | true -> 1
39
40let byte7_of_byte b =
41  let n1,n2 = from_byte b in
42    match from_nibble n1 with
43      (false,b1,b2,b3) -> mk_byte7 b1 b2 b3 n2
44    | _ -> raise Byte7_conversion
45
46let add8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) =
47 let n1 = int_of_vect b1 in
48 let n2 = int_of_vect b2 in
49 let c = int_of_bit c in
50 let res = n1 + n2 + c in
51 let ac = n1 mod 16 + n2 mod 16 + c >= 16 in
52 let c6 = n1 mod 128 + n2 mod 128 + c >= 128 in
53 let res,c = res mod 256, res >= 256 in
54 let ov = c <> c6 in
55   vect_of_int res `Eight,c,ac,ov
56;;
57
58let subb8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) =
59 let n1 = int_of_vect b1 in
60 let n2 = int_of_vect b2 in
61 let c = int_of_bit c in
62 let res = n1 - n2 - c in
63 let ac = n1 mod 16 - n2 mod 16 - c < 0 in
64 let c6 = n1 mod 128 - n2 mod 128 - c < 0 in
65 let res,c =
66  if res >= 0 then res,false
67  else n1 + 256 - n2 - c, true in
68 let ov = c <> c6 in
69  (vect_of_int res `Eight,c,ac,ov)
70;;
71
72let dec b =
73 let res = int_of_vect b - 1 in
74  if res < 0 then vect_of_int 255 `Eight
75  else vect_of_int res `Eight
76;;
77
78let inc b =
79 let res = int_of_vect b + 1 in
80  if res > 255 then (vect_of_int 0 `Eight : byte)
81  else (vect_of_int res `Eight : byte)
82;;
83
84let byte7_of_bit b =
85  [false;false;false;false;false;false;b]
86;;
87
88let byte_of_byte7 =
89 function
90    ([b1;b2;b3]::n) -> [false;b1;b2;b3]::n
91  | _ -> assert false
92;;
Note: See TracBrowser for help on using the repository browser.