aboutsummaryrefslogtreecommitdiffstats
path: root/src/VeriFuzz/Verilog/Eval.hs
diff options
context:
space:
mode:
authorYann Herklotz <git@ymhg.org>2019-04-26 13:48:32 +0100
committerYann Herklotz <git@ymhg.org>2019-04-26 13:48:56 +0100
commit1f92f329dabfaf5077bed677a273a196667229e1 (patch)
treea19c9ed6ec91db71d51684911420fd12a80a59bc /src/VeriFuzz/Verilog/Eval.hs
parent1486a2afa481de46938c1bc122c469975978593f (diff)
downloadverismith-1f92f329dabfaf5077bed677a273a196667229e1.tar.gz
verismith-1f92f329dabfaf5077bed677a273a196667229e1.zip
Add random bit selection for wires
This has not been tested fully yet
Diffstat (limited to 'src/VeriFuzz/Verilog/Eval.hs')
-rw-r--r--src/VeriFuzz/Verilog/Eval.hs25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/VeriFuzz/Verilog/Eval.hs b/src/VeriFuzz/Verilog/Eval.hs
index 7a3126e..5dbee8c 100644
--- a/src/VeriFuzz/Verilog/Eval.hs
+++ b/src/VeriFuzz/Verilog/Eval.hs
@@ -12,13 +12,14 @@ Evaluation of Verilog expressions and statements.
module VeriFuzz.Verilog.Eval
( evaluateConst
+ , resize
)
where
import Data.Bits
import Data.Foldable (fold)
-import Data.Functor.Foldable (cata)
-import Data.Maybe (fromMaybe, listToMaybe)
+import Data.Functor.Foldable hiding (fold)
+import Data.Maybe (listToMaybe)
import VeriFuzz.Verilog.AST
import VeriFuzz.Verilog.BitVec
@@ -92,8 +93,7 @@ evaluateConst :: Bindings -> ConstExprF BitVec -> BitVec
evaluateConst _ (ConstNumF b) = b
evaluateConst p (ParamIdF i) =
cata (evaluateConst p)
- . fromMaybe 0
- . fmap paramValue_
+ . maybe 0 paramValue_
. listToMaybe
$ filter ((== i) . paramIdent_) p
evaluateConst _ (ConstConcatF c ) = fold c
@@ -101,3 +101,20 @@ evaluateConst _ (ConstUnOpF unop c ) = applyUnary unop c
evaluateConst _ (ConstBinOpF a binop b) = applyBinary binop a b
evaluateConst _ (ConstCondF a b c) = if a > 0 then b else c
evaluateConst _ (ConstStrF _ ) = 0
+
+-- | Apply a function to all the bitvectors. Would be fixed by having a
+-- 'Functor' instance for a polymorphic 'ConstExpr'.
+applyBitVec :: (BitVec -> BitVec) -> ConstExpr -> ConstExpr
+applyBitVec f (ConstNum b) = ConstNum $ f b
+applyBitVec f (ConstConcat c) = ConstConcat $ fmap (applyBitVec f) c
+applyBitVec f (ConstUnOp unop c) = ConstUnOp unop $ applyBitVec f c
+applyBitVec f (ConstBinOp a binop b) = ConstBinOp (applyBitVec f a) binop (applyBitVec f b)
+applyBitVec f (ConstCond a b c) = ConstCond (abv a) (abv b) (abv c) where abv = applyBitVec f
+applyBitVec _ a = a
+
+-- | This probably could be implemented using some recursion scheme in the
+-- future. It would also be fixed by having a polymorphic expression type.
+resize :: Int -> ConstExpr -> ConstExpr
+resize n = applyBitVec (resize' n)
+ where
+ resize' n' (BitVec _ a) = BitVec n' a