Commit 06411d70 by Zachary Snow

support converting interfaces with parameters

parent 5843ef38
......@@ -86,9 +86,8 @@ Other:
## Supported Features
sv2v supports most synthesizable SystemVerilog features. Current notable
exceptions include `export`, interfaces _with parameter bindings_, and complex
(non-identifier) `modport` expressions. Assertions are also supported, but are
simply dropped during conversion.
exceptions include `export` and complex (non-identifier) `modport` expressions.
Assertions are also supported, but are simply dropped during conversion.
If you find a bug or have a feature request, please create an issue. Preference
will be given to issues which include examples or test cases.
......
......@@ -85,13 +85,8 @@ convertDescription interfaces modules (Part attrs extern Module lifetime name po
mapInterface (Instance part params ident Nothing instancePorts) =
case Map.lookup part interfaces of
Just interface ->
-- TODO: Add support for interfaces with parameter bindings.
if not $ null params
then error $ "interface instantiations with parameter "
++ "bindings are not yet supported: "
++ show (part, params, ident)
else Generate $ map GenModuleItem $
inlineInterface interface (ident, expandedPorts)
Generate $ map GenModuleItem $
inlineInterface interface (ident, params, expandedPorts)
Nothing -> Instance part params ident Nothing expandedPorts
where expandedPorts = concatMap (expandPortBinding part) instancePorts
mapInterface (orig @ (MIPackageItem (Function _ _ _ decls _))) =
......@@ -234,8 +229,8 @@ lookupType _ expr =
++ " are not identifiers: " ++ show expr
-- convert an interface instantiation into a series of equivalent module items
inlineInterface :: Interface -> (Identifier, [PortBinding]) -> [ModuleItem]
inlineInterface (ports, items) (instanceName, instancePorts) =
inlineInterface :: Interface -> (Identifier, [ParamBinding], [PortBinding]) -> [ModuleItem]
inlineInterface (ports, items) (instanceName, instanceParams, instancePorts) =
(:) (MIPackageItem $ Comment $ "expanded instance: " ++ instanceName) $
flip (++) portBindings $
map (traverseNestedModuleItems removeModport) $
......@@ -243,7 +238,10 @@ inlineInterface (ports, items) (instanceName, instancePorts) =
itemsPrefixed
where
prefix = instanceName ++ "_"
itemsPrefixed = map (prefixModuleItems prefix) $ items
itemsPrefixed =
map (prefixModuleItems prefix) $
map (traverseDecls overrideParam) $
items
origInstancePortNames = map fst instancePorts
instancePortExprs = map snd instancePorts
instancePortNames =
......@@ -264,6 +262,24 @@ inlineInterface (ports, items) (instanceName, instancePorts) =
MIPackageItem $ Comment $ "removed modport " ++ x
removeModport other = other
instanceParamMap = Map.fromList instanceParams
overrideParam :: Decl -> Decl
overrideParam (Param Parameter t x e) =
case Map.lookup x instanceParamMap of
Nothing -> Param Parameter t x e
Just (Right e') -> Param Parameter t x e'
Just (Left t') ->
error $ "interface param expected expression, found type: "
++ show t'
overrideParam (ParamType Parameter x mt) =
case Map.lookup x instanceParamMap of
Nothing -> ParamType Parameter x mt
Just (Left t') -> ParamType Parameter x (Just t')
Just (Right e') ->
error $ "interface param expected type, found expression: "
++ show e'
overrideParam other = other
portBindingItem :: PortBinding -> Maybe ModuleItem
portBindingItem (ident, Just expr) =
Just $ if declDirs Map.! ident == Input
......
interface Foo;
function bar;
parameter MULTIPLIER = 7;
function integer bar;
input integer x;
return x * x;
return x * x * MULTIPLIER;
endfunction
endinterface
module top;
Foo foo();
initial $display(foo.bar(3));
Foo foo_1();
Foo #(.MULTIPLIER(8)) foo_2();
initial begin
$display(foo_1.bar(3));
$display(foo_2.bar(3));
end
endmodule
module top;
function bar;
localparam MULTIPLIER_1 = 7;
localparam MULTIPLIER_2 = 8;
function integer bar_1;
input integer x;
bar = x * x;
bar_1 = x * x * MULTIPLIER_1;
endfunction
initial $display(bar(3));
function integer bar_2;
input integer x;
bar_2 = x * x * MULTIPLIER_2;
endfunction
initial begin
$display(bar_1(3));
$display(bar_2(3));
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