Skip to content

Commit

Permalink
Added handling of Floating Point.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paebbels authored and tgingold committed Jun 18, 2021
1 parent 05755b5 commit 823ee7d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
16 changes: 12 additions & 4 deletions pyGHDL/dom/InterfaceItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later
# ============================================================================
from pyGHDL.libghdl.vhdl.nodes import Null_Iir

from pyGHDL.libghdl.vhdl import nodes
from pydecor import export

from pyVHDLModel.VHDLModel import (
Expand All @@ -41,7 +44,7 @@
)

from pyGHDL.dom._Utils import NodeToName, GetModeOfNode
from pyGHDL.dom._Translate import GetSubtypeIndicationFromNode
from pyGHDL.dom._Translate import GetSubtypeIndicationFromNode, GetExpressionFromNode
from pyGHDL.dom.Common import GHDLMixin

__all__ = []
Expand All @@ -54,14 +57,16 @@ def parse(cls, generic):
name = NodeToName(generic)
mode = GetModeOfNode(generic)
subTypeIndication = GetSubtypeIndicationFromNode(generic, "generic", name)
value = GetExpressionFromNode(nodes.Get_Default_Value(generic))

generic = cls(name, mode, subTypeIndication)
generic = cls(name, mode, subTypeIndication, value)

return generic

def __init__(self, name: str, mode: Mode, subType: SubTypeOrSymbol):
def __init__(self, name: str, mode: Mode, subType: SubTypeOrSymbol, defaultExpression: Expression):
super().__init__(name=name, mode=mode)
self._subType = subType
self._defaultExpression = defaultExpression


@export
Expand All @@ -72,7 +77,10 @@ def parse(cls, port):
mode = GetModeOfNode(port)
subTypeIndication = GetSubtypeIndicationFromNode(port, "port", name)

port = cls(name, mode, subTypeIndication)
defaultValue = nodes.Get_Default_Value(port)
value = GetExpressionFromNode(defaultValue) if defaultValue != Null_Iir else None

port = cls(name, mode, subTypeIndication, value)

return port

Expand Down
2 changes: 1 addition & 1 deletion pyGHDL/dom/Literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class FloatingPointLiteral(VHDLModel_FloatingPointLiteral):
@classmethod
def parse(cls, node):
value = nodes.Get_Fp_Value(node)
return cls(value)
return cls(float(value))


@export
Expand Down
17 changes: 13 additions & 4 deletions pyGHDL/dom/formatting/prettyprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
PortInterfaceItem,
BinaryExpression,
IdentityExpression,
UnaryExpression,
UnaryExpression, WithDefaultExpression,
)

from pyGHDL import GHDLBaseException
Expand Down Expand Up @@ -220,16 +220,17 @@ def formatGenericConstant(
subType = generic.SubType
if isinstance(subType, SimpleSubTypeSymbol):
buffer.append(
"{prefix} - {name} : {mode} {type}".format(
"{prefix} - {name} : {mode} {type}{initialValue}".format(
prefix=prefix,
name=generic.Name,
mode=ModeTranslation[generic.Mode],
type=subType.SymbolName,
initialValue=self.formatInitialValue(generic),
)
)
elif isinstance(subType, ConstrainedSubTypeSymbol):
buffer.append(
"{prefix} - {name} : {mode} {type}({constraints})".format(
"{prefix} - {name} : {mode} {type}({constraints}){initialValue}".format(
prefix=prefix,
name=generic.Name,
mode=ModeTranslation[generic.Mode],
Expand All @@ -246,6 +247,7 @@ def formatGenericConstant(
for constraint in subType.Constraints
]
),
initialValue=self.formatInitialValue(generic),
)
)
else:
Expand All @@ -264,13 +266,14 @@ def formatPortSignal(
prefix = " " * level

buffer.append(
"{prefix} - {name} : {mode} {subtypeindication}".format(
"{prefix} - {name} : {mode} {subtypeindication}{initialValue}".format(
prefix=prefix,
name=port.Name,
mode=ModeTranslation[port.Mode],
subtypeindication=self.formatSubtypeIndication(
port.SubType, "port", port.Name
),
initialValue=self.formatInitialValue(port),
)
)

Expand Down Expand Up @@ -336,6 +339,12 @@ def formatSubtypeIndication(self, subTypeIndication, entity: str, name: str) ->
)
)

def formatInitialValue(self, item: WithDefaultExpression) -> str:
if item.DefaultExpression is None:
return ""

return " := {expr}".format(expr=self.formatExpression(item.DefaultExpression))

def formatExpression(self, expression: Expression) -> str:
if isinstance(expression, SimpleObjectSymbol):
return "{name}".format(name=expression.SymbolName)
Expand Down
3 changes: 2 additions & 1 deletion testsuite/pyunit/SimpleEntity.vhdl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use ieee.numeric_std.all;

entity entity_1 is
generic (
FREQ : real := 100.0;
BITS : positive := 8
);
port (
Clock: in std_logic;
Reset: in std_logic;
Reset: in std_logic := '0';
Q: out std_logic_vector(BITS - 1 downto 0)
);
end entity entity_1;
Expand Down

0 comments on commit 823ee7d

Please sign in to comment.