Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulator throws an exception instead of returning X values on undefined RAM signal #15

Open
gergoerdi opened this issue Feb 10, 2015 · 4 comments

Comments

@gergoerdi
Copy link
Member

It seems in certain circumstances, undefined values in RAM spoil the whole thing, not even into a fully-undefined signal, but one that throws an exception.

Here's a repro case on 0.2.4. Consider the following module:

import Language.KansasLava hiding (D)
import Language.KansasLava.Signal
import Data.Sized.Unsigned
import qualified Language.KansasLava.Stream as Stream

type A = U4
type D = U8

boolS :: Signal CLK Bool
boolS = iterateS bitNot True

toRV :: (Rep a, Clock clk) => Signal clk a -> RepValue
toRV = toRep . (!!0) . Stream.toList . shallowS

rv1 :: RepValue
rv1 = toRV (pack (undefinedS, boolS) :: Signal CLK (A -> D, Bool))

rv1' :: RepValue
rv1' = toRV (pack (undefinedS, undefinedS) :: Signal CLK (A -> D, Bool))

If we look at rv1 and rv1', we can see that rv1' takes on undefined (X) values as expected:

λ» rv1'
0bXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

However, rv1 crashes:

λ» rv1
0b1*** Exception: ("X",16[8U],"15")
@gergoerdi gergoerdi reopened this Feb 10, 2015
@gergoerdi
Copy link
Member Author

Note that this bug doesn't affect all "compound" signal types:

rv2 :: RepValue
rv2 = toRV (pack (undefinedS, boolS) :: Signal CLK (Matrix A D, Bool))

rv2' :: RepValue
rv2' = toRV (pack (undefinedS, undefinedS) :: Signal CLK (Matrix A D, Bool))

this results in both rv2 and rv2' being correct:

λ» rv2
0bXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
λ» rv2'
0bXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

@gergoerdi
Copy link
Member Author

(apologies for the accidental close-and-reopen)

@gergoerdi
Copy link
Member Author

The above suggests a workaround: if we have

funS :: Signal CLK (A -> D)
funS = undefinedS

s :: Signal CLK (A -> D, Bool)
s = pack (funS, boolS)

s' :: Signal CLK (Matrix A D, Bool)
s' = pack (bitwise funS, boolS)

then toRV s' works:

λ» toRV s
0b1*** Exception: ("X",16[8U],"15")
λ» toRV s'
0bXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

@gergoerdi
Copy link
Member Author

The exception is coming from the Rep instance of ix -> a:

instance (Size ix, Rep a, Rep ix) => Rep (ix -> a) where
    data X (ix -> a) = XFunction (ix -> X a)

    unX (XFunction f) = return (\ a -> 
        let fromJust' (Just x) = x
            fromJust' _ = error $ show ("X",repType (Witness :: Witness (ix -> a)), showRep (optX (Just a) :: X ix))
        in (fromJust' . unX . f) a)

There's no easy way to fix this since unX has to return a Maybe (ix -> a) instead of ix -> Maybe a which would work better here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant