source: Deliverables/D4.1/physical.ml @ 45

Last change on this file since 45 was 44, checked in by mulligan, 10 years ago

Added pretty printing functions for bits, bytes, words etc.

File size: 2.5 KB
Line 
1exception Byte7_conversion
2
3type bit = bool
4type nibble = bit * bit * bit * bit
5type byte = nibble * nibble
6type byte7 = bit * bit * bit * nibble
7type word = byte * byte
8type word11 = bit * bit * bit * byte
9
10module Byte7Map =
11  Map.Make (struct type t = byte7 let compare = Pervasives.compare end)
12module WordMap =
13  Map.Make (struct type t = word let compare = Pervasives.compare end)
14
15let byte7_of_byte =
16 function
17    (false,b2,b3,b4),n -> (b2,b3,b4,n)
18  | _ -> raise Byte7_conversion
19
20let int_of_bit b = if b then 0 else 1
21
22let 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
25let int_of_byte (n1,n2) = int_of_nibble n2 + int_of_nibble n1 * 16
26
27let int_of_word (b1,b2) = int_of_byte b2 + int_of_byte b1 * 256
28
29let bit_of_int n =
30 if n >= 2 then assert false
31 else
32  if n = 0 then false
33  else true
34;;
35
36let 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
46let 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
54let 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
61let 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
69let (++) w n = word_of_int (int_of_word w + n);;
70
71let 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
83let 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
97let 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
103let 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;;
108
109let byte7_of_bit b =
110  false,false,false,(false,false,false,b)
111;;
112
113let byte_of_byte7 (b1,b2,b3,n) =
114        (false,b1,b2,b3),n
Note: See TracBrowser for help on using the repository browser.