Commit b22cd210 by Zachary Snow

improved portability of logic conversion

- indirect converted reg continuous assignments through wires
- fix typeof for implicitly typed ports
- fix typeof for sized implicitly typed params
parent 5f0dc6be
...@@ -106,21 +106,17 @@ traverseModuleItem ports scopes = ...@@ -106,21 +106,17 @@ traverseModuleItem ports scopes =
fixModuleItem :: ModuleItem -> ModuleItem fixModuleItem :: ModuleItem -> ModuleItem
-- rewrite bad continuous assignments to use procedural assignments -- rewrite bad continuous assignments to use procedural assignments
fixModuleItem (Assign AssignOptionNone lhs expr) = fixModuleItem (Assign AssignOptionNone lhs expr) =
if not (isReg lhs) then if not (isReg lhs)
Assign AssignOptionNone lhs expr then Assign AssignOptionNone lhs expr
else if isConstant expr then
Initial $ Asgn AsgnOpEq Nothing lhs expr
else else
AlwaysC AlwaysComb $ Asgn AsgnOpEq Nothing lhs expr Generate $ map GenModuleItem
[ MIPackageItem (Decl (Variable Local t x [] Nil))
, Assign AssignOptionNone (LHSIdent x) expr
, AlwaysC AlwaysComb $ Asgn AsgnOpEq Nothing lhs (Ident x)
]
where where
-- only handles expressions which are trivially constant for now t = TypeOf expr
isConstant :: Expr -> Bool x = "sv2v_tmp_" ++ shortHash (lhs, expr)
isConstant Number{} = True
isConstant (Repeat _ es) = all isConstant es
isConstant (Concat es) = all isConstant es
isConstant (BinOp _ e1 e2) = isConstant e1 && isConstant e2
isConstant (UniOp _ e) = isConstant e
isConstant _ = False
-- rewrite port bindings to use temporary nets where necessary -- rewrite port bindings to use temporary nets where necessary
fixModuleItem (Instance moduleName params instanceName rs bindings) = fixModuleItem (Instance moduleName params instanceName rs bindings) =
if null newItems if null newItems
......
...@@ -37,11 +37,11 @@ traverseDeclM decl = do ...@@ -37,11 +37,11 @@ traverseDeclM decl = do
item <- traverseModuleItemM (MIPackageItem $ Decl decl) item <- traverseModuleItemM (MIPackageItem $ Decl decl)
let MIPackageItem (Decl decl') = item let MIPackageItem (Decl decl') = item
case decl' of case decl' of
Variable Local (Implicit sg rs) ident [] Nil -> do Variable _ (Implicit sg rs) ident a _ ->
-- implicit types, which are commonly found in function return -- implicit types, which are commonly found in function return
-- types, are recast as logics to avoid outputting bare ranges -- types, are recast as logics to avoid outputting bare ranges
insertElem ident $ IntegerVector TLogic sg rs insertElem ident t' >> return decl'
return decl' where t' = injectRanges (IntegerVector TLogic sg rs) a
Variable d t ident a e -> do Variable d t ident a e -> do
let t' = injectRanges t a let t' = injectRanges t a
insertElem ident t' insertElem ident t'
...@@ -52,6 +52,9 @@ traverseDeclM decl = do ...@@ -52,6 +52,9 @@ traverseDeclM decl = do
insertElem ident UnknownType >> return decl' insertElem ident UnknownType >> return decl'
Param _ UnknownType ident e -> Param _ UnknownType ident e ->
typeof e >>= insertElem ident >> return decl' typeof e >>= insertElem ident >> return decl'
Param _ (Implicit sg rs) ident _ ->
insertElem ident t' >> return decl'
where t' = IntegerVector TLogic sg rs
Param _ t ident _ -> Param _ t ident _ ->
insertElem ident t >> return decl' insertElem ident t >> return decl'
ParamType{} -> return decl' ParamType{} -> return decl'
......
module Example(inp, out); module Example(inp, out);
parameter ENABLED = 1; parameter ENABLED = 1;
localparam [0:0] DEFAULT = 1'b0;
input logic inp; input logic inp;
output logic out; output logic out;
if (ENABLED) if (ENABLED)
always_comb out = inp; always_comb out = inp;
else else
assign out = '0; assign out = DEFAULT;
endmodule endmodule
module Example(inp, out); module Example(inp, out);
parameter ENABLED = 1; parameter ENABLED = 1;
localparam [0:0] DEFAULT = 1'b0;
input wire inp; input wire inp;
output reg out; output reg out;
generate generate
if (ENABLED) if (ENABLED)
always @* out = inp; always @* out = inp;
else else
initial out = 0; initial out = DEFAULT;
endgenerate endgenerate
endmodule endmodule
...@@ -77,12 +77,16 @@ module top; ...@@ -77,12 +77,16 @@ module top;
localparam X = 5'b10110; localparam X = 5'b10110;
localparam Y = X + 6'b00001; localparam Y = X + 6'b00001;
localparam [7:0] Z = 234;
initial begin initial begin
type(X) tX = X; type(X) tX = X;
type(Y) tY = Y; type(Y) tY = Y;
type(Z) tZ = Z;
$display("%b %d %d %d", X, X, $left(X), $right(X)); $display("%b %d %d %d", X, X, $left(X), $right(X));
$display("%b %d %d %d", Y, Y, $left(Y), $right(Y)); $display("%b %d %d %d", Y, Y, $left(Y), $right(Y));
$display("%b %d %d %d", Z, Z, $left(Z), $right(Z));
$display("%b %d %d %d", tX, tX, $left(tX), $right(tX)); $display("%b %d %d %d", tX, tX, $left(tX), $right(tX));
$display("%b %d %d %d", tY, tY, $left(tY), $right(tY)); $display("%b %d %d %d", tY, tY, $left(tY), $right(tY));
$display("%b %d %d %d", tZ, tZ, $left(tZ), $right(tZ));
end end
endmodule endmodule
...@@ -94,14 +94,19 @@ module top; ...@@ -94,14 +94,19 @@ module top;
localparam X = 5'b10110; localparam X = 5'b10110;
localparam Y = X + 6'b00001; localparam Y = X + 6'b00001;
localparam [7:0] Z = 234;
initial begin : block5 initial begin : block5
reg [4:0] tX; reg [4:0] tX;
reg [5:0] tY; reg [5:0] tY;
reg [7:0] tZ;
tX = X; tX = X;
tY = Y; tY = Y;
tZ = Z;
$display("%b %d %d %d", X, X, 4, 0); $display("%b %d %d %d", X, X, 4, 0);
$display("%b %d %d %d", Y, Y, 5, 0); $display("%b %d %d %d", Y, Y, 5, 0);
$display("%b %d %d %d", Z, Z, 7, 0);
$display("%b %d %d %d", tX, tX, 4, 0); $display("%b %d %d %d", tX, tX, 4, 0);
$display("%b %d %d %d", tY, tY, 5, 0); $display("%b %d %d %d", tY, tY, 5, 0);
$display("%b %d %d %d", tZ, tZ, 7, 0);
end end
endmodule endmodule
module Example(inp, out);
input inp;
output out;
type(inp) data;
assign data = ~inp;
assign out = data;
endmodule
module Example(inp, out);
input inp;
output out;
wire data;
assign data = ~inp;
assign out = data;
endmodule
module top;
reg inp;
wire out;
Example e(inp, out);
initial begin
$monitor("%0d %b %b", $time, inp, out);
end
endmodule
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment