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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.