Changeset 63


Ignore:
Timestamp:
Sep 13, 2010, 11:04:55 AM (9 years ago)
Author:
mulligan
Message:

More work on bits, bytes, nibbles, and added modules for byte7s and
words.

Location:
Deliverables/D4.1
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D4.1/Bit.ml

    r62 r63  
    11(*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*)
    2 (* FILENAME: Bit.ml                                                          *)
     2(* FILENAME:    Bit.ml                                                       *)
    33(* DESCRIPTION: An ADT implementing bits, and common operations on them.     *)
    4 (* CREATED: 10/09/2010, Dominic Mulligan                                     *)
     4(* CREATED:     10/09/2010, Dominic Mulligan                                 *)
    55(* BUGS:                                                                     *)
    66(*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*)
     
    1212  val from_bool: bool -> bit
    1313  val from_int: int -> bit
     14  val from_string: string -> bit option
     15
     16  val to_bool: bit -> bool
     17  val to_int: bit -> int
     18  val to_string: bit -> string
    1419
    1520  val (-&-): bit -> bit -> bit
     
    1823  val not: bit -> bit
    1924
    20   val pretty: bit -> string
    2125end;;
    2226
     
    3135    else
    3236      false
     37  let from_string =
     38    function
     39      "0" -> Some false
     40    | "1" -> Some true
     41    | _ -> None
     42
     43  let to_int =
     44    function
     45      false -> 0
     46    | true -> 1
     47  let to_string l =
     48    match l with
     49      true -> "1"
     50    | false -> "0"
     51
     52  let to_bool b = b
    3353
    3454  let (-&-) l r =
     
    4868      true -> false
    4969    | false -> true
    50 
    51   let pretty l =
    52     match l with
    53       true -> "1b"
    54     | false -> "0b"
    5570end;;
  • Deliverables/D4.1/Byte.ml

    r62 r63  
    11(*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*)
    2 (* FILENAME: Byte.ml                                                         *)
    3 (* DESCRIPTION: An ADT implementing bytes, and common operations on them.    *)
    4 (* CREATED: 10/09/2010, Dominic Mulligan                                     *)
     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                                 *)
    56(* BUGS:                                                                     *)
    67(*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*)
     
    1617(*
    1718  val from_bits: bit -> bit -> bit -> bit -> bit -> bit -> bit -> bit -> byte
     19  val from_bit: bit -> byte
    1820  val from_nibbles: nibble -> nibble -> byte
     21  val from_nibble: nibble -> byte
    1922  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
    2027
    2128  val get_nibble_at: int -> byte -> nibble
     
    4552  type nibble = Nibble.nibble
    4653  type byte = nibble * nibble
     54
     55  let from_bits b1 b2 b3 b4 b5 b6 b7 b8 =
     56    let nibble
    4757end
  • Deliverables/D4.1/Nibble.ml

    r62 r63  
    11(*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*)
    2 (* FILENAME: Nibble.ml                                                       *)
    3 (* DESCRIPTION: An ADT implementing nibbles, and common operations on them.  *)
    4 (* CREATED: 10/09/2010, Dominic Mulligan                                     *)
     2(* FILENAME:    Nibble.ml                                                    *)
     3(* DESCRIPTION: An ADT implementing 4 bit nibbles, and common operations on  *)
     4(*              them.                                                        *)
     5(* CREATED:     10/09/2010, Dominic Mulligan                                 *)
    56(* BUGS:                                                                     *)
    67(*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*)
     
    1314  type nibble
    1415
    15   val from_bits: bit -> bit -> nibble
     16  val from_bits: bit -> bit -> bit -> bit -> nibble
     17  val from_bit: bit -> nibble
    1618  val from_int: int -> nibble option
     19  val from_string: string -> nibble option
     20
     21  val to_bits: nibble -> (bit * bit * bit * bit)
     22  val to_bit: nibble -> bit option
     23  val to_int: nibble -> int
     24  val to_string: nibble -> string
    1725
    1826  val get_bit_at: int -> nibble -> bit option
     
    2937  val iter_bit: (bit -> string) -> nibble -> string
    3038  val zip_bit: (bit -> bit -> bit) -> nibble -> nibble -> nibble
    31   val pretty: nibble -> string
    3239end
    3340
     
    3542struct
    3643  type bit = Bit.bit
    37   type nibble = bit * bit
     44  type nibble = bit * bit * bit * bit
    3845
    39   let from_bits l h = (l, h)
     46  let from_bits b1 b2 b3 b4 = (b1, b2, b3, b4)
     47  let from_bit h = (Bit.from_bool false, Bit.from_bool false, Bit.from_bool false, h)
    4048  let from_int int_val =
    4149    match int_val with
    42       0 -> Some (Bit.from_int 0, Bit.from_int 0)
    43     | 1 -> Some (Bit.from_int 0, Bit.from_int 1)
    44     | 2 -> Some (Bit.from_int 1, Bit.from_int 0)
    45     | 3 -> Some (Bit.from_int 1, Bit.from_int 1)
     50      0 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 0, Bit.from_int 0)
     51    | 1 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 0, Bit.from_int 1)
     52    | 2 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 1, Bit.from_int 0)
     53    | 3 -> Some (Bit.from_int 0, Bit.from_int 0, Bit.from_int 1, Bit.from_int 1)
     54    | 4 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 0, Bit.from_int 0)
     55    | 5 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 0, Bit.from_int 1)
     56    | 6 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 1, Bit.from_int 0)
     57    | 7 -> Some (Bit.from_int 0, Bit.from_int 1, Bit.from_int 1, Bit.from_int 1)
     58    | 8 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 0, Bit.from_int 0)
     59    | 9 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 0, Bit.from_int 1)
     60    | 10 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 1, Bit.from_int 0)
     61    | 11 -> Some (Bit.from_int 1, Bit.from_int 0, Bit.from_int 1, Bit.from_int 1)
     62    | 12 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 0, Bit.from_int 0)
     63    | 13 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 0, Bit.from_int 1)
     64    | 14 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 1, Bit.from_int 0)
     65    | 15 -> Some (Bit.from_int 1, Bit.from_int 1, Bit.from_int 1, Bit.from_int 1)
    4666    | _ -> None
     67  let from_string
     68
     69  let to_bit (l, h) =
     70    let (bl, bh) = Bit.to_bool l, Bit.to_bool h in
     71    match (bl, bh) with
     72      (false, b) -> Some (Bit.from_bool b)
     73    | (true, b) -> None
     74  let to_int (l, h) = (2 * Bit.to_int l) + Bit.to_int h
     75  let to_string = iter_bit Bit.to_string
    4776
    4877  let get_bit_at index (l, h) =
     
    79108      str_l ^ str_h
    80109  let zip_bit f (l1, h1) (l2, h2) = (f l1 l2, f l2 h2)
    81   let pretty = iter_bit Bit.pretty
    82110end
    83111
Note: See TracChangeset for help on using the changeset viewer.