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

Last change on this file since 144 was 144, checked in by sacerdot, 9 years ago

The sp can also point to upper internal ram. Fixed everywhere.

File size: 1.9 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 add8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) =
41 let n1 = int_of_vect b1 in
42 let n2 = int_of_vect b2 in
43 let c = int_of_bit c in
44 let res = n1 + n2 + c in
45 let ac = n1 mod 16 + n2 mod 16 + c >= 16 in
46 let c6 = n1 mod 128 + n2 mod 128 + c >= 128 in
47 let res,c = res mod 256, res >= 256 in
48 let ov = c <> c6 in
49   vect_of_int res `Eight,c,ac,ov
50;;
51
52let subb8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) =
53 let n1 = int_of_vect b1 in
54 let n2 = int_of_vect b2 in
55 let c = int_of_bit c in
56 let res = n1 - n2 - c in
57 let ac = n1 mod 16 - n2 mod 16 - c < 0 in
58 let c6 = n1 mod 128 - n2 mod 128 - c < 0 in
59 let res,c =
60  if res >= 0 then res,false
61  else n1 + 256 - n2 - c, true in
62 let ov = c <> c6 in
63  (vect_of_int res `Eight,c,ac,ov)
64;;
65
66let dec b =
67 let res = int_of_vect b - 1 in
68  if res < 0 then vect_of_int 255 `Eight
69  else vect_of_int res `Eight
70;;
71
72let inc b =
73 let res = int_of_vect b + 1 in
74  if res > 255 then (vect_of_int 0 `Eight : byte)
75  else (vect_of_int res `Eight : byte)
76;;
77
78let byte7_of_bit b =
79  [false;false;false;false;false;false;b]
80;;
81
82let byte_of_byte7 =
83 function
84    ([b1;b2;b3]::n) -> [false;b1;b2;b3]::n
85  | _ -> assert false
86;;
Note: See TracBrowser for help on using the repository browser.