aboutsummaryrefslogtreecommitdiffstats
path: root/src/Verismith/Verilog/Internal.hs
blob: 0ebc626f032b9d93b37947448a28709de5240296 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{-|
Module      : Verismith.Verilog.Internal
Description : Defaults and common functions.
Copyright   : (c) 2018-2019, Yann Herklotz
License     : GPL-3
Maintainer  : yann [at] yannherklotz [dot] com
Stability   : experimental
Portability : POSIX

Defaults and common functions.
-}

module Verismith.Verilog.Internal
    ( regDecl
    , wireDecl
    , emptyMod
    , setModName
    , addModPort
    , addModDecl
    , testBench
    , addTestBench
    , defaultPort
    , portToExpr
    , modName
    , yPort
    , wire
    , reg
    )
where

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

regDecl :: Identifier -> (ModItem ann)
regDecl i = Decl Nothing (Port Reg False (Range 1 0) i) Nothing

wireDecl :: Identifier -> (ModItem ann)
wireDecl i = Decl Nothing (Port Wire False (Range 1 0) i) Nothing

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

-- | Set a module name for a module declaration.
setModName :: Text -> (ModDecl ann) -> (ModDecl ann)
setModName str = modId .~ Identifier str

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

addModDecl :: (ModDecl ann) -> (Verilog ann) -> (Verilog ann)
addModDecl desc = _Wrapped %~ (:) desc

testBench :: (ModDecl ann)
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
        , BlockAssign . Assign (RegId "b") Nothing $ Number 1
        ]
    ]
    []

addTestBench :: (Verilog ann) -> (Verilog ann)
addTestBench = addModDecl testBench

defaultPort :: Identifier -> Port
defaultPort = Port Wire False (Range 1 0)

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

modName :: (ModDecl ann) -> Text
modName = getIdentifier . view modId

yPort :: Identifier -> Port
yPort = Port Wire False (Range 90 0)

wire :: Range -> Identifier -> Port
wire = Port Wire False

reg :: Range -> Identifier -> Port
reg = Port Reg False