1 | exception Byte7_conversion |
---|
2 | |
---|
3 | module Byte7Map = |
---|
4 | Map.Make (struct type t = byte7 let compare = Pervasives.compare end) |
---|
5 | module WordMap = |
---|
6 | Map.Make (struct type t = word let compare = Pervasives.compare end) |
---|
7 | |
---|
8 | let int_of_bit = |
---|
9 | function |
---|
10 | false -> 0 |
---|
11 | | true -> 1 |
---|
12 | |
---|
13 | let byte7_of_byte b = |
---|
14 | let n1,n2 = from_byte b in |
---|
15 | match from_nibble n1 with |
---|
16 | (false,b1,b2,b3) -> mk_byte7 b1 b2 b3 n2 |
---|
17 | | _ -> raise Byte7_conversion |
---|
18 | |
---|
19 | let add8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) = |
---|
20 | let n1 = int_of_vect b1 in |
---|
21 | let n2 = int_of_vect b2 in |
---|
22 | let c = int_of_bit c in |
---|
23 | let res = n1 + n2 + c in |
---|
24 | let ac = n1 mod 16 + n2 mod 16 + c >= 16 in |
---|
25 | let c6 = n1 mod 128 + n2 mod 128 + c >= 128 in |
---|
26 | let res,c = res mod 256, res >= 256 in |
---|
27 | let ov = c <> c6 in |
---|
28 | vect_of_int res `Eight,c,ac,ov |
---|
29 | ;; |
---|
30 | |
---|
31 | let subb8_with_c (b1 : [`Eight] vect) (b2 : [`Eight] vect) (c : bit) = |
---|
32 | let n1 = int_of_vect b1 in |
---|
33 | let n2 = int_of_vect b2 in |
---|
34 | let c = int_of_bit c in |
---|
35 | let res = n1 - n2 - c in |
---|
36 | let ac = n1 mod 16 - n2 mod 16 - c < 0 in |
---|
37 | let c6 = n1 mod 128 - n2 mod 128 - c < 0 in |
---|
38 | let res,c = |
---|
39 | if res >= 0 then res,false |
---|
40 | else n1 + 256 - n2 - c, true in |
---|
41 | let ov = c <> c6 in |
---|
42 | (vect_of_int res `Eight,c,ac,ov) |
---|
43 | ;; |
---|
44 | |
---|
45 | let dec b = |
---|
46 | let res = int_of_vect b - 1 in |
---|
47 | if res < 0 then vect_of_int 255 `Eight |
---|
48 | else vect_of_int res `Eight |
---|
49 | ;; |
---|
50 | |
---|
51 | let inc b = |
---|
52 | let res = int_of_vect b + 1 in |
---|
53 | if res > 255 then (vect_of_int 0 `Eight : byte) |
---|
54 | else (vect_of_int res `Eight : byte) |
---|
55 | ;; |
---|
56 | |
---|
57 | let byte7_of_bit b = |
---|
58 | [false;false;false;false;false;false;b] |
---|
59 | ;; |
---|
60 | |
---|
61 | let byte_of_byte7 = |
---|
62 | function |
---|
63 | ([b1;b2;b3]::n) -> [false;b1;b2;b3]::n |
---|
64 | | _ -> assert false |
---|
65 | ;; |
---|