[28] | 1 | exception FOO2 |
---|
| 2 | |
---|
| 3 | type bit = bool |
---|
| 4 | type nibble = bit * bit * bit * bit |
---|
| 5 | type byte = nibble * nibble |
---|
| 6 | type byte7 = bit * bit * bit * nibble |
---|
| 7 | type word = byte * byte |
---|
| 8 | type word11 = bit * bit * bit * byte |
---|
| 9 | |
---|
| 10 | module Byte7Map = |
---|
| 11 | Map.Make (struct type t = byte7 let compare = Pervasives.compare end) |
---|
| 12 | module WordMap = |
---|
| 13 | Map.Make (struct type t = word let compare = Pervasives.compare end) |
---|
| 14 | |
---|
| 15 | let byte7_of_byte = |
---|
| 16 | function |
---|
| 17 | (false,b2,b3,b4),n -> (b2,b3,b4,n) |
---|
| 18 | | _ -> raise FOO2 |
---|
| 19 | |
---|
| 20 | let int_of_bit b = if b then 0 else 1 |
---|
| 21 | |
---|
| 22 | let int_of_nibble (b1,b2,b3,b4) = |
---|
| 23 | int_of_bit b4 + int_of_bit b3 * 2 + int_of_bit b2 * 4 + int_of_bit b1 * 8 |
---|
| 24 | |
---|
| 25 | let int_of_byte (n1,n2) = int_of_nibble n2 + int_of_nibble n1 * 16 |
---|
| 26 | |
---|
| 27 | let int_of_word (b1,b2) = int_of_byte b2 + int_of_byte b1 * 256 |
---|
| 28 | |
---|
| 29 | let bit_of_int n = |
---|
| 30 | if n >= 2 then assert false |
---|
| 31 | else |
---|
| 32 | if n = 0 then false |
---|
| 33 | else true |
---|
| 34 | ;; |
---|
| 35 | |
---|
| 36 | let nibble_of_int n = |
---|
| 37 | if n >= 16 then assert false |
---|
| 38 | else |
---|
| 39 | let b1 = n / 8 in |
---|
| 40 | let b2 = (n mod 8) / 4 in |
---|
| 41 | let b3 = (n mod 4) / 2 in |
---|
| 42 | let b4 = n mod 2 in |
---|
| 43 | bit_of_int b1, bit_of_int b2, bit_of_int b3, bit_of_int b4 |
---|
| 44 | ;; |
---|
| 45 | |
---|
| 46 | let byte_of_int n = |
---|
| 47 | if n >= 256 then assert false |
---|
| 48 | else |
---|
| 49 | let b1 = n / 16 in |
---|
| 50 | let b2 = n mod 16 in |
---|
| 51 | nibble_of_int b1, nibble_of_int b2 |
---|
| 52 | ;; |
---|
| 53 | |
---|
| 54 | let byte7_of_int n = |
---|
| 55 | if n >= 128 then assert false |
---|
| 56 | else |
---|
| 57 | let (_,b1,b2,b3),n = byte_of_int n in |
---|
| 58 | (b1,b2,b3,n) |
---|
| 59 | ;; |
---|
| 60 | |
---|
| 61 | let word_of_int n = |
---|
| 62 | if n >= 256 * 256 then assert false |
---|
| 63 | else |
---|
| 64 | let b1 = n / 256 in |
---|
| 65 | let b2 = n mod 256 in |
---|
| 66 | byte_of_int b1, byte_of_int b2 |
---|
| 67 | ;; |
---|
| 68 | |
---|
| 69 | let (++) w n = word_of_int (int_of_word w + n);; |
---|
| 70 | |
---|
| 71 | let add8_with_c b1 b2 c = |
---|
| 72 | let n1 = int_of_byte b1 in |
---|
| 73 | let n2 = int_of_byte b2 in |
---|
| 74 | let c = int_of_bit c in |
---|
| 75 | let res = n1 + n2 + c in |
---|
| 76 | let ac = n1 mod 16 + n2 mod 16 + c >= 16 in |
---|
| 77 | let c6 = n1 mod 128 + n2 mod 128 + c >= 128 in |
---|
| 78 | let res,c = res mod 256, res >= 256 in |
---|
| 79 | let ov = c <> c6 in |
---|
| 80 | byte_of_int res,c,ac,ov |
---|
| 81 | ;; |
---|
| 82 | |
---|
| 83 | let subb8_with_c b1 b2 c = |
---|
| 84 | let n1 = int_of_byte b1 in |
---|
| 85 | let n2 = int_of_byte b2 in |
---|
| 86 | let c = int_of_bit c in |
---|
| 87 | let res = n1 - n2 - c in |
---|
| 88 | let ac = n1 mod 16 - n2 mod 16 - c < 0 in |
---|
| 89 | let c6 = n1 mod 128 - n2 mod 128 - c < 0 in |
---|
| 90 | let res,c = |
---|
| 91 | if res >= 0 then res,false |
---|
| 92 | else n1 + 256 - n2 - c, true in |
---|
| 93 | let ov = c <> c6 in |
---|
| 94 | byte_of_int res,c,ac,ov |
---|
| 95 | ;; |
---|
| 96 | |
---|
| 97 | let dec b = |
---|
| 98 | let res = int_of_byte b - 1 in |
---|
| 99 | if res < 0 then byte_of_int 255 |
---|
| 100 | else byte_of_int res |
---|
| 101 | ;; |
---|
| 102 | |
---|
| 103 | let inc b = |
---|
| 104 | let res = int_of_byte b + 1 in |
---|
| 105 | if res > 255 then byte_of_int 0 |
---|
| 106 | else byte_of_int res |
---|
| 107 | ;; |
---|
[41] | 108 | |
---|
[42] | 109 | let byte7_of_bit b = |
---|
| 110 | false,false,false,(false,false,false,b) |
---|
[41] | 111 | ;; |
---|
[42] | 112 | |
---|
| 113 | let byte_of_byte7 (b1,b2,b3,n) = |
---|
| 114 | (false,b1,b2,b3),n |
---|