source: Deliverables/D4.1/Byte.ml @ 64

Last change on this file since 64 was 64, checked in by mulligan, 11 years ago

More work on bits, bytes and nibbles.

File size: 3.2 KB
Line 
1(*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*)
2(* FILENAME:    Byte.ml                                                      *)
3(* DESCRIPTION: An ADT implementing standard 8 bit bytes, and common         *)
4(*              operations on them.                                          *)
5(* CREATED:     10/09/2010, Dominic Mulligan                                 *)
6(* BUGS:                                                                     *)
7(*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*)
8
9open Bit;;
10open Nibble;;
11
12module type BYTE =
13sig
14  type bit
15  type nibble
16  type byte
17(*
18  val from_bits: bit -> bit -> bit -> bit -> bit -> bit -> bit -> bit -> byte
19  val from_bit: bit -> byte
20  val from_nibbles: nibble -> nibble -> byte
21  val from_nibble: nibble -> byte
22  val from_int: int -> byte
23
24  val to_bit: byte -> bit option
25  val to_nibble: byte -> nibble option
26  val to_int: byte -> int
27
28  val get_nibble_at: int -> byte -> nibble
29  val set_nibble_at: int -> nibble -> byte -> byte option
30  val get_bit_at: int -> byte -> bit
31  val set_bit_at: int -> bit -> byte -> byte option
32*)
33
34  val (-&-): byte -> byte -> byte
35  val (-|-): byte -> byte -> byte
36  val (-^-): byte -> byte -> byte
37(*
38  val rotate_right: byte -> byte
39  val rotate_left: byte -> byte
40  val shift_right: byte -> byte
41  val shift_left: byte -> byte
42*)
43  val not: byte -> byte
44
45(*
46  val map_nibble: (nibble -> nibble) -> byte -> byte
47  val map_bit: (bit -> bit) -> byte -> byte
48  val iter_nibble: (nibble -> string) -> byte -> string
49  val iter_bit: (bit -> string) -> byte -> string
50  val zip_nibble: (nibble -> nibble -> nibble) -> byte -> byte -> byte
51  val zip_bit: (bit -> bit -> bit) -> byte -> byte -> byte
52  val to_string: byte -> string
53*)
54end
55
56module Byte(Bit: BIT) (Nibble: NIBBLE): BYTE
57  with type Nibble.bit = Bit.bit
58  and  type nibble = Nibble.nibble =
59struct
60  type bit = Bit.bit
61  type nibble = Nibble.nibble
62  type byte = nibble * nibble
63
64  let (-&-) (l1, l2) (r1, r2) = (Nibble.(-&-) l1 r1, Nibble.(-&-) l2 r2)
65  let (-|-) (l1, l2) (r1, r2) = (Nibble.(-|-) l1 r1, Nibble.(-|-) l2 r2)
66  let (-^-) (l1, l2) (r1, r2) = (Nibble.(-^-) l1 r1, Nibble.(-^-) l2 r2)
67(*
68  let shift_right (n1, n2) =
69    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
70    let (b5, b6, b7, b8) = Nibble.to_bits n2 in
71    let new_n1 = Nibble.from_bits (Bit.from_bool false) b1 b2 b3 in
72    let new_n2 = Nibble.from_bits b4 b5 b6 b7 in
73      (new_n1, new_n2)
74  let shift_left (n1, n2) =
75    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
76    let (b5, b6, b7, b8) = Nibble.to_bits n2 in
77    let new_n1 = Nibble.from_bits b1 b2 b3 b4 in
78    let new_n2 = Nibble.from_bits b5 b6 b7 (Bit.from_bool false) in
79      (new_n1, new_n2)
80  let rotate_right (n1, n2) =
81    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
82    let (b5, b6, b7, b8) = Nibble.to_bits n2 in
83    let new_n1 = Nibble.from_bits b8 b1 b2 b3 in
84    let new_n2 = Nibble.from_bits b4 b5 b6 b7 in
85      (new_n1, new_n2)
86  let rotate_left (n1, n2) =
87    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
88    let (b5, b6, b7, b8) = Nibble.to_bits n2 in
89    let new_n1 = Nibble.from_bits b2 b3 b4 b5 in
90    let new_n2 = Nibble.from_bits b5 b6 b7 b1 in
91      (new_n1, new_n2)
92*)
93  let not (n1, n2) = (Nibble.not n1, Nibble.not n2)
94end
Note: See TracBrowser for help on using the repository browser.