Ignore:
Timestamp:
Sep 20, 2010, 10:35:53 AM (9 years ago)
Author:
mulligan
Message:

int_of_vect implemented.

File:
1 edited

Legend:

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

    r58 r88  
     1open Bit_vectors
     2
    13exception Byte7_conversion
    2 
    3 type bit = bool
    4 type nibble = bit * bit * bit * bit
    5 type byte = nibble * nibble
    6 type byte7 = bit * bit * bit * nibble
    7 type word = byte * byte
    8 type word11 = bit * bit * bit * byte
    94
    105module Byte7Map =
     
    1712    (false,b2,b3,b4),n -> (b2,b3,b4,n)
    1813  | _ -> raise Byte7_conversion
    19 
    20 let int_of_bit b = if b then 0 else 1
    21 
    22 let 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 
    25 let int_of_byte (n1,n2) = int_of_nibble n2 + int_of_nibble n1 * 16
    26 
    27 let int_of_byte7 (b1,b2,b3,n2) = int_of_nibble n2 + int_of_bit b3 * 16 +
    28                                  int_of_bit b2 * 32 + int_of_bit b1 * 64
    29 
    30 let int_of_word (b1,b2) = int_of_byte b2 + int_of_byte b1 * 256
    31 
    32 let bit_of_int n =
    33  if n >= 2 then assert false
    34  else
    35   if n = 0 then false
    36   else true
    37 ;;
    38 
    39 let nibble_of_int n =
    40  if n >= 16 then assert false
    41  else
    42   let b1 = n / 8 in
    43   let b2 = (n mod 8) / 4 in
    44   let b3 = (n mod 4) / 2 in
    45   let b4 = n mod 2 in
    46    bit_of_int b1, bit_of_int b2, bit_of_int b3, bit_of_int b4
    47 ;;
    48 
    49 let byte_of_int n =
    50  if n >= 256 then assert false
    51  else
    52   let b1 = n / 16 in
    53   let b2 = n mod 16 in
    54    nibble_of_int b1, nibble_of_int b2
    55 ;;
    56 
    57 let byte7_of_int n =
    58  if n >= 128 then assert false
    59  else
    60   let (_,b1,b2,b3),n = byte_of_int n in
    61    (b1,b2,b3,n)
    62 ;;
    63 
    64 let word_of_int n =
    65  if n >= 256 * 256 then assert false
    66  else
    67   let b1 = n / 256 in
    68   let b2 = n mod 256 in
    69    byte_of_int b1, byte_of_int b2
    70 ;;
    71 
    72 let complement ((b1,b2,b3,b4),(b5,b6,b7,b8)) =
    73   (not b1,not b2,not b3,not b4),(not b5,not b6,not b7,not b8)
    74 
    75 let (++) w n = word_of_int (int_of_word w + n);;
    7614
    7715let add8_with_c b1 b2 c =
     
    11957let byte_of_byte7 (b1,b2,b3,n) =
    12058        (false,b1,b2,b3),n
    121 
    122 let nth_bit pos ((b1,b2,b3,b4),(b5,b6,b7,b8)) =
    123   match pos with
    124     0 -> b1
    125   | 1 -> b2
    126   | 2 -> b3
    127   | 3 -> b4
    128   | 4 -> b5
    129   | 5 -> b6
    130   | 6 -> b7
    131   | 7 -> b8
    132 
    133 let set_nth_bit pos v ((b1,b2,b3,b4) as n1,((b5,b6,b7,b8) as n2)) =
    134   match pos with
    135     0 -> ((v,b2,b3,b4),n2)
    136   | 1 -> ((b1,v,b3,b4),n2)
    137   | 2 -> ((b1,b2,v,b4),n2)
    138   | 3 -> ((b1,b2,b3,v),n2)
    139   | 4 -> (n1,(v,b6,b7,b8))
    140   | 5 -> (n1,(b5,v,b7,b8))
    141   | 6 -> (n1,(b5,b6,v,b8))
    142   | 7 -> (n1,(b5,b6,b7,v))
     59;;
Note: See TracChangeset for help on using the changeset viewer.