aboutsummaryrefslogtreecommitdiffstats
path: root/src/VeriFuzz/Verilog/Helpers.hs
blob: 47713291246cd8a0cd7d8f9ffb3401c9682902c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{-|
Module      : VeriFuzz.Verilog.Helpers
Description : Defaults and common functions.
Copyright   : (c) 2018-2019, Yann Herklotz Grave
License     : BSD-3
Maintainer  : ymherklotz [at] gmail [dot] com
Stability   : experimental
Portability : POSIX

Defaults and common functions.
-}

module VeriFuzz.Verilog.Helpers where

import           Control.Lens
import           Data.Text            (Text)
import           VeriFuzz.Verilog.AST

regDecl :: Identifier -> ModItem
regDecl = Decl Nothing . Port (Reg False) 1

wireDecl :: Identifier -> ModItem
wireDecl = Decl Nothing . Port Wire 1

-- | Create an empty module.
emptyMod :: ModDecl
emptyMod = ModDecl "" [] [] []

-- | Set a module name for a module declaration.
setModName :: Text -> ModDecl -> ModDecl
setModName str = moduleId .~ Identifier str

-- | Add a input port to the module declaration.
addModPort :: Port -> ModDecl -> ModDecl
addModPort port = modInPorts %~ (:) port

addDescription :: Description -> VerilogSrc -> VerilogSrc
addDescription desc = getVerilogSrc %~ (:) desc

testBench :: ModDecl
testBench = ModDecl
  "main"
  []
  []
  [ regDecl "a"
  , regDecl "b"
  , wireDecl "c"
  , ModInst "and" "and_gate" [ModConn $ Id "c", ModConn $ Id "a", ModConn $ Id "b"]
  , Initial $ SeqBlock
    [ BlockAssign . Assign (RegId "a") Nothing $ Number 1 1
    , BlockAssign . Assign (RegId "b") Nothing $ Number 1 1
    -- , TimeCtrl (Delay 1) . Just . SysTaskEnable $ Task "display"
    --   [ Str "%d & %d = %d"
    --   , PrimExpr $ PrimId "a"
    --   , PrimExpr $ PrimId "b"
    --   , PrimExpr $ PrimId "c"
    --   ]
    -- , SysTaskEnable $ Task "finish" []
    ]
  ]

addTestBench :: VerilogSrc -> VerilogSrc
addTestBench = addDescription $ Description testBench

defaultPort :: Identifier -> Port
defaultPort = Port Wire 1

portToExpr :: Port -> Expr
portToExpr (Port _ _ i) = Id i