detect (but don't correct two-bit errors in a Word16

This commit is contained in:
Jonathan Lamothe 2024-07-06 20:55:31 -04:00
parent ed405e366e
commit 74b26de743
2 changed files with 21 additions and 4 deletions

View File

@ -65,6 +65,7 @@ instance Hamming Word16 where
correctErrors c correctErrors c
| isValid c = Just c | isValid c = Just c
| not (oddParity c) = Nothing
| otherwise = let | otherwise = let
errorFlags = map (oddParity . (c .&.)) errorFlags = map (oddParity . (c .&.))
[0xaaaa, 0xcccc, 0xf0f0, 0xff00] [0xaaaa, 0xcccc, 0xf0f0, 0xff00]

View File

@ -21,7 +21,7 @@ License along with this program. If not, see
module Hamming.Word16Spec (spec) where module Hamming.Word16Spec (spec) where
import Data.Bits (shiftL, xor) import Data.Bits (shiftL, xor, (.|.))
import Data.Word (Word16) import Data.Word (Word16)
import Hamming import Hamming
@ -68,7 +68,7 @@ correctErrorsSpec = describe "correctErrors" $ mapM_
in it ("should be " ++ show expected) $ in it ("should be " ++ show expected) $
actual `shouldBe` expected actual `shouldBe` expected
) $ ("valid", withChkBits, Just withChkBits) : ) $ ("valid", withChkBits, Just withChkBits) :
singleBitErrors singleBitErrors ++ doubleBitErrors
singleBitErrors :: [(String, Word16, Maybe Word16)] singleBitErrors :: [(String, Word16, Maybe Word16)]
singleBitErrors = map singleBitErrors = map
@ -78,6 +78,22 @@ singleBitErrors = map
in ("bad bit " ++ show bit, code, Just withChkBits) in ("bad bit " ++ show bit, code, Just withChkBits)
) [0..15] ) [0..15]
doubleBitErrors :: [(String, Word16, Maybe Word16)]
doubleBitErrors =
[ ( \(bitA, bitB) -> let
mask = (1 `shiftL` bitA) .|. (1 `shiftL` bitB)
code = withChkBits `xor` mask
in
( "bad bits " ++ show bitA ++ " & " ++ show bitB
, code
, Nothing
)
) (a, b)
| a <- [0..14]
, b <- [1..15]
, a < b
]
noChkBits :: Word16 noChkBits :: Word16
noChkBits = 0x34c0 noChkBits = 0x34c0