{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.CodeGeneration.CRC_USB5 where
import Data.SBV
import Data.SBV.Tools.CodeGen
import Data.SBV.Tools.Polynomial
usb5 :: SWord16
usb5 :: SWord16
usb5 = [Int] -> SWord16
forall a. Polynomial a => [Int] -> a
polynomial [Int
5, Int
2, Int
0]
crcUSB :: SWord16 -> SWord16
crcUSB :: SWord16 -> SWord16
crcUSB SWord16
i = [SBool] -> SWord16
forall a. SFiniteBits a => [SBool] -> SBV a
fromBitsBE ([SBool]
ib [SBool] -> [SBool] -> [SBool]
forall a. [a] -> [a] -> [a]
++ [SBool]
cb)
where ib :: [SBool]
ib = Int -> [SBool] -> [SBool]
forall a. Int -> [a] -> [a]
drop Int
5 (SWord16 -> [SBool]
forall a. SFiniteBits a => SBV a -> [SBool]
blastBE SWord16
i)
pb :: [SBool]
pb = Int -> [SBool] -> [SBool]
forall a. Int -> [a] -> [a]
drop Int
11 (SWord16 -> [SBool]
forall a. SFiniteBits a => SBV a -> [SBool]
blastBE SWord16
usb5)
cb :: [SBool]
cb = Int -> [SBool] -> [SBool] -> [SBool]
crcBV Int
5 [SBool]
ib [SBool]
pb
crcUSB' :: SWord16 -> SWord16
crcUSB' :: SWord16 -> SWord16
crcUSB' SWord16
i' = SWord16
i SWord16 -> SWord16 -> SWord16
forall a. Bits a => a -> a -> a
.|. SWord16 -> SWord16 -> SWord16
forall a. Polynomial a => a -> a -> a
pMod SWord16
i SWord16
usb5
where i :: SWord16
i = SWord16
i' SWord16 -> Int -> SWord16
forall a. Bits a => a -> Int -> a
`shiftL` Int
5
crcGood :: IO ThmResult
crcGood :: IO ThmResult
crcGood = (SWord16 -> SBool) -> IO ThmResult
forall a. Provable a => a -> IO ThmResult
prove ((SWord16 -> SBool) -> IO ThmResult)
-> (SWord16 -> SBool) -> IO ThmResult
forall a b. (a -> b) -> a -> b
$ \SWord16
i -> SWord16 -> SWord16
crcUSB SWord16
i SWord16 -> SWord16 -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== SWord16 -> SWord16
crcUSB' SWord16
i
cg1 :: IO ()
cg1 :: IO ()
cg1 = Maybe FilePath -> FilePath -> SBVCodeGen () -> IO ()
forall a. Maybe FilePath -> FilePath -> SBVCodeGen a -> IO a
compileToC (FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
"crcUSB1") FilePath
"crcUSB1" (SBVCodeGen () -> IO ()) -> SBVCodeGen () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
SWord16
msg <- FilePath -> SBVCodeGen SWord16
forall a. SymVal a => FilePath -> SBVCodeGen (SBV a)
cgInput FilePath
"msg"
FilePath -> SWord16 -> SBVCodeGen ()
forall a. FilePath -> SBV a -> SBVCodeGen ()
cgOutput FilePath
"crc" (SWord16 -> SWord16
crcUSB SWord16
msg)
cg2 :: IO ()
cg2 :: IO ()
cg2 = Maybe FilePath -> FilePath -> SBVCodeGen () -> IO ()
forall a. Maybe FilePath -> FilePath -> SBVCodeGen a -> IO a
compileToC (FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
"crcUSB2") FilePath
"crcUSB2" (SBVCodeGen () -> IO ()) -> SBVCodeGen () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
SWord16
msg <- FilePath -> SBVCodeGen SWord16
forall a. SymVal a => FilePath -> SBVCodeGen (SBV a)
cgInput FilePath
"msg"
FilePath -> SWord16 -> SBVCodeGen ()
forall a. FilePath -> SBV a -> SBVCodeGen ()
cgOutput FilePath
"crc" (SWord16 -> SWord16
crcUSB' SWord16
msg)