+module Octet
+where
+
+import Data.Maybe (fromJust)
+
+import Test.HUnit (assertEqual)
+import Test.Framework (Test, testGroup)
+import Test.Framework.Providers.HUnit (testCase)
+
+import Test.QuickCheck (Arbitrary(..), Gen)
+
+import Bit as B
+import Maskable
+import Maskbits
+
+-- | An Octet consists of eight bits. For our purposes, the most
+-- significant bit will come "first." That is, b1 is in the 2^7
+-- place while b8 is in the 2^0 place.
+data Octet =
+ Octet { b1 :: Bit,
+ b2 :: Bit,
+ b3 :: Bit,
+ b4 :: Bit,
+ b5 :: Bit,
+ b6 :: Bit,
+ b7 :: Bit,
+ b8 :: Bit }
+ deriving (Eq)
+
+
+instance Show Octet where
+ show oct = show (octet_to_int oct)
+
+
+instance Arbitrary Octet where
+ arbitrary = do
+ a1 <- arbitrary :: Gen Bit
+ a2 <- arbitrary :: Gen Bit
+ a3 <- arbitrary :: Gen Bit
+ a4 <- arbitrary :: Gen Bit
+ a5 <- arbitrary :: Gen Bit
+ a6 <- arbitrary :: Gen Bit
+ a7 <- arbitrary :: Gen Bit
+ a8 <- arbitrary :: Gen Bit
+ return (Octet a1 a2 a3 a4 a5 a6 a7 a8)
+
+
+instance Maskable Octet where
+ apply_mask oct Eight _ = oct
+
+ apply_mask oct Seven bit =
+ oct { b8 = bit }
+
+ apply_mask oct Six bit =
+ oct { b8 = bit, b7 = bit }
+
+ apply_mask oct Five bit =
+ oct { b8 = bit, b7 = bit, b6 = bit }
+
+ apply_mask oct Four bit =
+ oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit }
+
+ apply_mask oct Three bit =
+ oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit }
+
+ apply_mask oct Two bit =
+ oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit, b3 = bit }
+
+ apply_mask oct Maskbits.One bit =
+ oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit,
+ b4 = bit, b3 = bit, b2 = bit }
+
+ apply_mask oct Maskbits.Zero bit =
+ oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit,
+ b4 = bit, b3 = bit, b2 = bit, b1 = bit }
+
+ -- The Maskbits must be in [Eight..ThirtyTwo].
+ apply_mask oct _ _ = oct
+
+
+-- | Convert each bit to its integer value, and multiply by the
+-- appropriate power of two. Sum them up, and we should get an integer
+-- between 0 and 255.