2019-04-04 02:24:09 +02:00
|
|
|
{- sv2v
|
|
|
|
|
- Author: Zachary Snow <zach@zachjs.com>
|
|
|
|
|
- Initial Verilog AST Author: Tom Hawkins <tomahawkins@gmail.com>
|
|
|
|
|
-
|
|
|
|
|
- SystemVerilog `module` items
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
module Language.SystemVerilog.AST.ModuleItem
|
|
|
|
|
( ModuleItem (..)
|
|
|
|
|
, PortBinding
|
2019-09-09 07:38:14 +02:00
|
|
|
, ParamBinding
|
2019-04-04 02:24:09 +02:00
|
|
|
, ModportDecl
|
|
|
|
|
, AlwaysKW (..)
|
|
|
|
|
, NInputGateKW (..)
|
|
|
|
|
, NOutputGateKW (..)
|
2020-03-21 02:13:57 +01:00
|
|
|
, AssignOption (..)
|
2019-04-04 02:24:09 +02:00
|
|
|
) where
|
|
|
|
|
|
|
|
|
|
import Data.List (intercalate)
|
|
|
|
|
import Data.Maybe (maybe, fromJust, isJust)
|
2019-09-09 07:38:14 +02:00
|
|
|
import Data.Either (either)
|
2019-04-04 02:24:09 +02:00
|
|
|
import Text.Printf (printf)
|
|
|
|
|
|
|
|
|
|
import Language.SystemVerilog.AST.ShowHelp
|
|
|
|
|
|
|
|
|
|
import Language.SystemVerilog.AST.Attr (Attr)
|
2019-04-23 23:12:56 +02:00
|
|
|
import Language.SystemVerilog.AST.Decl (Direction)
|
2019-04-04 02:24:09 +02:00
|
|
|
import Language.SystemVerilog.AST.Description (PackageItem)
|
2019-09-09 07:38:14 +02:00
|
|
|
import Language.SystemVerilog.AST.Expr (Expr(Ident, Nil), Range, TypeOrExpr, showRanges)
|
2019-04-04 02:24:09 +02:00
|
|
|
import Language.SystemVerilog.AST.GenItem (GenItem)
|
|
|
|
|
import Language.SystemVerilog.AST.LHS (LHS)
|
2020-02-01 02:24:37 +01:00
|
|
|
import Language.SystemVerilog.AST.Stmt (Stmt, AssertionItem, Timing(Delay))
|
2020-03-21 02:13:57 +01:00
|
|
|
import Language.SystemVerilog.AST.Type (Identifier, DriveStrength)
|
2019-04-04 02:24:09 +02:00
|
|
|
|
|
|
|
|
data ModuleItem
|
|
|
|
|
= MIAttr Attr ModuleItem
|
|
|
|
|
| AlwaysC AlwaysKW Stmt
|
2020-03-21 02:13:57 +01:00
|
|
|
| Assign AssignOption LHS Expr
|
2019-04-04 02:24:09 +02:00
|
|
|
| Defparam LHS Expr
|
2019-09-09 07:38:14 +02:00
|
|
|
| Instance Identifier [ParamBinding] Identifier (Maybe Range) [PortBinding]
|
2019-04-04 02:24:09 +02:00
|
|
|
| Genvar Identifier
|
|
|
|
|
| Generate [GenItem]
|
|
|
|
|
| Modport Identifier [ModportDecl]
|
|
|
|
|
| Initial Stmt
|
2019-11-01 01:39:11 +01:00
|
|
|
| Final Stmt
|
2019-04-04 02:24:09 +02:00
|
|
|
| MIPackageItem PackageItem
|
2020-02-01 02:24:37 +01:00
|
|
|
| NInputGate NInputGateKW (Maybe Expr) Identifier LHS [Expr]
|
|
|
|
|
| NOutputGate NOutputGateKW (Maybe Expr) Identifier [LHS] Expr
|
2019-04-04 02:24:09 +02:00
|
|
|
| AssertionItem AssertionItem
|
|
|
|
|
deriving Eq
|
|
|
|
|
|
|
|
|
|
instance Show ModuleItem where
|
|
|
|
|
show (MIPackageItem i) = show i
|
|
|
|
|
show (MIAttr attr mi ) = printf "%s %s" (show attr) (show mi)
|
|
|
|
|
show (AlwaysC k b) = printf "%s %s" (show k) (show b)
|
2020-03-21 02:13:57 +01:00
|
|
|
show (Assign o a b) = printf "assign %s%s = %s;" (showPad o) (show a) (show b)
|
2019-04-04 02:24:09 +02:00
|
|
|
show (Defparam a b) = printf "defparam %s = %s;" (show a) (show b)
|
|
|
|
|
show (Genvar x ) = printf "genvar %s;" x
|
|
|
|
|
show (Generate b ) = printf "generate\n%s\nendgenerate" (indent $ unlines' $ map show b)
|
|
|
|
|
show (Modport x l) = printf "modport %s(\n%s\n);" x (indent $ intercalate ",\n" $ map showModportDecl l)
|
|
|
|
|
show (Initial s ) = printf "initial %s" (show s)
|
2019-11-01 01:39:11 +01:00
|
|
|
show (Final s ) = printf "final %s" (show s)
|
2020-02-01 02:24:37 +01:00
|
|
|
show (NInputGate kw d x lhs exprs) =
|
|
|
|
|
showGate kw d x $ show lhs : map show exprs
|
|
|
|
|
show (NOutputGate kw d x lhss expr) =
|
|
|
|
|
showGate kw d x $ (map show lhss) ++ [show expr]
|
2019-04-04 02:24:09 +02:00
|
|
|
show (AssertionItem (mx, a)) =
|
|
|
|
|
if mx == Nothing
|
|
|
|
|
then show a
|
|
|
|
|
else printf "%s : %s" (fromJust mx) (show a)
|
|
|
|
|
show (Instance m params i r ports) =
|
|
|
|
|
if null params
|
2019-09-09 07:38:14 +02:00
|
|
|
then printf "%s %s%s%s;" m i rStr (showPorts ports)
|
|
|
|
|
else printf "%s #%s %s%s%s;" m (showParams params) i rStr (showPorts ports)
|
2019-04-04 02:24:09 +02:00
|
|
|
where rStr = maybe "" (\a -> showRanges [a] ++ " ") r
|
|
|
|
|
|
|
|
|
|
showPorts :: [PortBinding] -> String
|
|
|
|
|
showPorts ports = indentedParenList $ map showPort ports
|
|
|
|
|
|
|
|
|
|
showPort :: PortBinding -> String
|
|
|
|
|
showPort ("*", Nothing) = ".*"
|
|
|
|
|
showPort (i, arg) =
|
|
|
|
|
if i == ""
|
|
|
|
|
then show (fromJust arg)
|
|
|
|
|
else printf ".%s(%s)" i (if isJust arg then show $ fromJust arg else "")
|
|
|
|
|
|
2020-02-01 02:24:37 +01:00
|
|
|
showGate :: Show k => k -> Maybe Expr -> Identifier -> [String] -> String
|
|
|
|
|
showGate kw d x args =
|
|
|
|
|
printf "%s %s%s(%s);" (show kw) delayStr nameStr (commas args)
|
|
|
|
|
where
|
|
|
|
|
delayStr = maybe "" (showPad . Delay) d
|
|
|
|
|
nameStr = showPad $ Ident x
|
|
|
|
|
|
2019-09-09 07:38:14 +02:00
|
|
|
showParams :: [ParamBinding] -> String
|
|
|
|
|
showParams params = indentedParenList $ map showParam params
|
|
|
|
|
|
|
|
|
|
showParam :: ParamBinding -> String
|
|
|
|
|
showParam ("*", Right Nil) = ".*"
|
|
|
|
|
showParam (i, arg) =
|
|
|
|
|
printf fmt i (either show show arg)
|
|
|
|
|
where fmt = if i == "" then "%s%s" else ".%s(%s)"
|
|
|
|
|
|
2019-04-04 02:24:09 +02:00
|
|
|
showModportDecl :: ModportDecl -> String
|
|
|
|
|
showModportDecl (dir, ident, me) =
|
|
|
|
|
if me == Just (Ident ident)
|
|
|
|
|
then printf "%s %s" (show dir) ident
|
|
|
|
|
else printf "%s .%s(%s)" (show dir) ident (maybe "" show me)
|
|
|
|
|
|
|
|
|
|
type PortBinding = (Identifier, Maybe Expr)
|
|
|
|
|
|
2019-09-09 07:38:14 +02:00
|
|
|
type ParamBinding = (Identifier, TypeOrExpr)
|
|
|
|
|
|
2019-04-04 02:24:09 +02:00
|
|
|
type ModportDecl = (Direction, Identifier, Maybe Expr)
|
|
|
|
|
|
|
|
|
|
data AlwaysKW
|
|
|
|
|
= Always
|
|
|
|
|
| AlwaysComb
|
|
|
|
|
| AlwaysFF
|
|
|
|
|
| AlwaysLatch
|
|
|
|
|
deriving Eq
|
|
|
|
|
|
|
|
|
|
instance Show AlwaysKW where
|
|
|
|
|
show Always = "always"
|
|
|
|
|
show AlwaysComb = "always_comb"
|
|
|
|
|
show AlwaysFF = "always_ff"
|
|
|
|
|
show AlwaysLatch = "always_latch"
|
|
|
|
|
|
|
|
|
|
data NInputGateKW
|
|
|
|
|
= GateAnd
|
|
|
|
|
| GateNand
|
|
|
|
|
| GateOr
|
|
|
|
|
| GateNor
|
|
|
|
|
| GateXor
|
|
|
|
|
| GateXnor
|
|
|
|
|
deriving Eq
|
|
|
|
|
|
|
|
|
|
instance Show NInputGateKW where
|
|
|
|
|
show GateAnd = "and"
|
|
|
|
|
show GateNand = "nand"
|
|
|
|
|
show GateOr = "or"
|
|
|
|
|
show GateNor = "nor"
|
|
|
|
|
show GateXor = "xor"
|
|
|
|
|
show GateXnor = "xnor"
|
|
|
|
|
|
|
|
|
|
data NOutputGateKW
|
|
|
|
|
= GateBuf
|
|
|
|
|
| GateNot
|
|
|
|
|
deriving Eq
|
|
|
|
|
|
|
|
|
|
instance Show NOutputGateKW where
|
|
|
|
|
show GateBuf = "buf"
|
|
|
|
|
show GateNot = "not"
|
2020-03-21 02:13:57 +01:00
|
|
|
|
|
|
|
|
data AssignOption
|
|
|
|
|
= AssignOptionNone
|
|
|
|
|
| AssignOptionDelay Expr
|
|
|
|
|
| AssignOptionDrive DriveStrength
|
|
|
|
|
deriving Eq
|
|
|
|
|
|
|
|
|
|
instance Show AssignOption where
|
|
|
|
|
show AssignOptionNone = ""
|
|
|
|
|
show (AssignOptionDelay de) = printf "#(%s)" (show de)
|
|
|
|
|
show (AssignOptionDrive ds) = show ds
|