Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions should avoid non-local references #66

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions VerilogCodingStyle.md
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,52 @@ function automatic logic [2:0] foo(logic [2:0] a, logic [2:0] b);
endfunction
```

Functions should not reference any non-local signals or variables outside their
scope. Avoiding non-local references improves readability and helps reduce
simulation/synthesis mismatches. Accessing non-local parameters and constants
is allowed.

👎
```systemverilog {.bad}
// - Incorrect because `mem` is not local to get_mem()
// - Incorrect because `in_i` is not local to get_mem()
module mymod (
input logic [7:0] in_i,
output logic [7:0] out_o
);
logic [7:0] mem[256];
function automatic logic [7:0] get_mem();
return mem[in_i];
endfunction
assign out_o = get_mem();
endmodule
```

👍
```systemverilog {.good}
// - Correct because `MagicValue` is a parameter
// - Correct because `my_pkg::OtherMagicValue` is a parameter
// - Correct because `in_i` passed as an argument
module mymod (
input logic [7:0] in_i,
output logic [7:0] out_o
);
localparam [7:0] MagicValue = 1;
function automatic logic is_magic(logic [7:0] v);
return (v == MagicValue) || (v == my_pkg::OtherMagicValue);
endfunction
assign out_o = is_magic(in_i);
endmodule
```

### Problematic Language Features and Constructs

These language features are considered problematic and their use is discouraged
Expand Down