Changeset 66


Ignore:
Timestamp:
Sep 13, 2010, 3:15:54 PM (9 years ago)
Author:
mulligan
Message:

Bytes mostly finished.

Location:
Deliverables/D4.1
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D4.1/8051Series.ml

    r65 r66  
    1 use "Bit.ml";;
    2 use "Nibble.ml";;
    3 use "Byte.ml";;
    4 use "Byte7.ml";;
    5 use "Word.ml";;
     1open "Bit.ml";;
     2open "Nibble.ml";;
     3open "Byte.ml";;
     4open "Byte7.ml";;
     5open "Word.ml";;
    66
    77module type 8051_SERIES =
  • Deliverables/D4.1/Byte.ml

    r64 r66  
    1515  type nibble
    1616  type byte
    17 (*
     17
    1818  val from_bits: bit -> bit -> bit -> bit -> bit -> bit -> bit -> bit -> byte
    1919  val from_bit: bit -> byte
    2020  val from_nibbles: nibble -> nibble -> byte
    2121  val from_nibble: nibble -> byte
     22(*
    2223  val from_int: int -> byte
    23 
     24*)
    2425  val to_bit: byte -> bit option
    2526  val to_nibble: byte -> nibble option
    2627  val to_int: byte -> int
    2728
    28   val get_nibble_at: int -> byte -> nibble
     29  val get_nibble_at: int -> byte -> nibble option
    2930  val set_nibble_at: int -> nibble -> byte -> byte option
    30   val get_bit_at: int -> byte -> bit
     31  val get_bit_at: int -> byte -> bit option
    3132  val set_bit_at: int -> bit -> byte -> byte option
    32 *)
    3333
    3434  val (-&-): byte -> byte -> byte
    3535  val (-|-): byte -> byte -> byte
    3636  val (-^-): byte -> byte -> byte
    37 (*
    3837  val rotate_right: byte -> byte
    3938  val rotate_left: byte -> byte
    4039  val shift_right: byte -> byte
    4140  val shift_left: byte -> byte
    42 *)
    4341  val not: byte -> byte
    4442
    45 (*
    4643  val map_nibble: (nibble -> nibble) -> byte -> byte
    4744  val map_bit: (bit -> bit) -> byte -> byte
     
    5148  val zip_bit: (bit -> bit -> bit) -> byte -> byte -> byte
    5249  val to_string: byte -> string
    53 *)
    5450end
    5551
    56 module Byte(Bit: BIT) (Nibble: NIBBLE): BYTE
    57   with type Nibble.bit = Bit.bit
     52module ByteFunctor(Bit: BIT) (Nibble: NIBBLE with type bit = Bit.bit): BYTE
     53  with type bit = Bit.bit
    5854  and  type nibble = Nibble.nibble =
    5955struct
     
    6258  type byte = nibble * nibble
    6359
     60  let from_bits b1 b2 b3 b4 b5 b6 b7 b8 =
     61    let n1 = Nibble.from_bits b1 b2 b3 b4 in
     62    let n2 = Nibble.from_bits b5 b6 b7 b8 in
     63      (n1, n2)
     64  let from_bit b =
     65    let n1 = Nibble.from_bits (Bit.from_bool false) (Bit.from_bool false)
     66                              (Bit.from_bool false) (Bit.from_bool false) in
     67    let n2 = Nibble.from_bits (Bit.from_bool false) (Bit.from_bool false)
     68                              (Bit.from_bool false) b in
     69      (n1, n2)
     70  let from_nibbles n1 n2 = (n1, n2)
     71  let from_nibble n2 =
     72    let n1 = Nibble.from_bits (Bit.from_bool false) (Bit.from_bool false)
     73                              (Bit.from_bool false) (Bit.from_bool false) in
     74      (n1, n2)
     75
     76  let get_nibble_at index (n1, n2) =
     77    match index with
     78      0 -> Some n2
     79    | 1 -> Some n1
     80    | _ -> None
     81  let get_bit_at index (n1, n2) =
     82    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
     83    let (b5, b6, b7, b8) = Nibble.to_bits n2 in
     84      match index with
     85        0 -> Some b8
     86      | 1 -> Some b7
     87      | 2 -> Some b6
     88      | 3 -> Some b5
     89      | 4 -> Some b4
     90      | 3 -> Some b3
     91      | 2 -> Some b2
     92      | 1 -> Some b1
     93      | _ -> None
     94
     95  let set_nibble_at index new_val (n1, n2) =
     96    match index with
     97      0 -> Some (n1, new_val)
     98    | 1 -> Some (new_val, n2)
     99    | _ -> None
     100  let set_bit_at index new_val (n1, n2) =
     101    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
     102    let (b5, b6, b7, b8) = Nibble.to_bits n2 in
     103      match index with
     104        0 -> Some (from_bits b1 b2 b3 b4 b5 b6 b7 new_val)
     105      | 1 -> Some (from_bits b1 b2 b3 b4 b5 b6 new_val b8)
     106      | 2 -> Some (from_bits b1 b2 b3 b4 b5 new_val b7 b8)
     107      | 3 -> Some (from_bits b1 b2 b3 b4 new_val b6 b7 b8)
     108      | 4 -> Some (from_bits b1 b2 b3 new_val b5 b6 b7 b8)
     109      | 3 -> Some (from_bits b1 b2 new_val b4 b5 b6 b7 b8)
     110      | 2 -> Some (from_bits b1 new_val b3 b4 b5 b6 b7 b8)
     111      | 1 -> Some (from_bits new_val b2 b3 b4 b5 b6 b7 b8)
     112      | _ -> None
     113
     114  let to_bit (n1, n2) =
     115    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
     116    let (b5, b6, b7, b8) = Nibble.to_bits n2 in
     117      if (Bit.to_bool b1, Bit.to_bool b2,
     118          Bit.to_bool b3, Bit.to_bool b4) = (false, false, false, false) then
     119        if Bit.to_bool b5 = false && Bit.to_bool b6 = false && Bit.to_bool b7 = false then
     120          Some b8
     121        else
     122          None
     123      else
     124        None
     125  let to_nibble (n1, n2) =
     126    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
     127      if (Bit.to_bool b1, Bit.to_bool b2,
     128          Bit.to_bool b3, Bit.to_bool b4) = (false, false, false, false) then
     129        Some n2
     130      else None
     131  let to_int (n1, n2) =
     132    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
     133    let (b5, b6, b7, b8) = Nibble.to_bits n2 in
     134      (128 * Bit.to_int b8) + (64 * Bit.to_int b8) +
     135      (32 * Bit.to_int b8) + (16 * Bit.to_int b8) +
     136      (8 * Bit.to_int b8) + (4 * Bit.to_int b6) +
     137      (2 * Bit.to_int b7) + Bit.to_int b8
     138
    64139  let (-&-) (l1, l2) (r1, r2) = (Nibble.(-&-) l1 r1, Nibble.(-&-) l2 r2)
    65140  let (-|-) (l1, l2) (r1, r2) = (Nibble.(-|-) l1 r1, Nibble.(-|-) l2 r2)
    66141  let (-^-) (l1, l2) (r1, r2) = (Nibble.(-^-) l1 r1, Nibble.(-^-) l2 r2)
    67 (*
     142
    68143  let shift_right (n1, n2) =
    69144    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
     
    90165    let new_n2 = Nibble.from_bits b5 b6 b7 b1 in
    91166      (new_n1, new_n2)
    92 *)
    93167  let not (n1, n2) = (Nibble.not n1, Nibble.not n2)
    94 end
     168
     169  let map_nibble f (n1, n2) = (f n1, f n2)
     170  let map_bit f (n1, n2) =
     171    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
     172    let (b5, b6, b7, b8) = Nibble.to_bits n2 in
     173    let new_n1 = Nibble.from_bits (f b1) (f b2) (f b3) (f b4) in
     174    let new_n2 = Nibble.from_bits (f b5) (f b6) (f b7) (f b8) in
     175      (new_n1, new_n2)
     176  let iter_nibble f (n1, n2) =
     177    let str_n1 = f n1 in
     178    let str_n2 = f n2 in
     179      str_n1 ^ str_n2
     180  let iter_bit f (n1, n2) =
     181    let (b1, b2, b3, b4) = Nibble.to_bits n1 in
     182    let (b5, b6, b7, b8) = Nibble.to_bits n2 in
     183    let new_n1 = (f b1) ^ (f b2) ^ (f b3) ^ (f b4) in
     184    let new_n2 = (f b5) ^ (f b6) ^ (f b7) ^ (f b8) in
     185      new_n1 ^ new_n2
     186  let zip_nibble f (l1, l2) (r1, r2) = (f l1 r1, f l2 r2)
     187  let zip_bit f (l1, l2) (r1, r2) =
     188    let (lb1, lb2, lb3, lb4) = Nibble.to_bits l1 in
     189    let (lb5, lb6, lb7, lb8) = Nibble.to_bits l2 in
     190    let (rb1, rb2, rb3, rb4) = Nibble.to_bits r1 in
     191    let (rb5, rb6, rb7, rb8) = Nibble.to_bits r2 in
     192    let (b1, b2, b3, b4) = (f lb1 rb1), (f lb2 rb2), (f lb3 rb3), (f lb4 rb4) in
     193    let (b5, b6, b7, b8) = (f lb5 rb5), (f lb6 rb6), (f lb7 rb7), (f lb8 rb8) in
     194    let new_n1 = Nibble.from_bits b1 b2 b3 b4 in
     195    let new_n2 = Nibble.from_bits b5 b6 b7 b8 in
     196      (new_n1, new_n2)
     197  let to_string = iter_nibble Nibble.to_string
     198end;;
     199
     200module Byte = ByteFunctor(Bit) (Nibble);;
  • Deliverables/D4.1/Nibble.ml

    r64 r66  
    4141end
    4242
    43 module NibbleFunctor(Bit: BIT) : NIBBLE with type bit = Bit.bit =
     43module NibbleFunctor(Bit: BIT) : NIBBLE
     44  with type bit = Bit.bit =
    4445struct
    4546  type bit = Bit.bit
  • Deliverables/D4.1/Word.ml

    r63 r66  
    77(*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*)
    88
    9 use "Bit.ml";;
    10 use "Nibble.ml";;
    11 use "Byte.ml";;
     9open "Bit.ml";;
     10open "Nibble.ml";;
     11open "Byte.ml";;
    1212
    1313module type WORD =
Note: See TracChangeset for help on using the changeset viewer.