8.2.2 Get off $unit

EasyFixIcarus · browser samples / server checksReview the topic

Fix two checkers that compile and run, but take their limit, their counter and their parity function from old file-scope declarations.

Hardware context: UART line watcher, link checkers and a bus lock that share declarations through packages. Move two link checkers from old file-scope helpers to their package.

This is a Fix problem: the code in the editor has one realistic defect. Find it and fix it.

Last week the link helpers moved into link_pkg: the frame limit MAX_LEN, the errors counter and the parity function. The two checkers were never touched, and they still compile. A 20-byte frame now passes the length check, and the error count of link_pkg stays at 0.

An older file is compiled in the same unit. It declares MAX_LEN, errors and parity at file scope, with a limit of 255 and odd parity. A plain name with no declaration in its module is searched outward, so both checkers quietly found these.

Make both checkers use link_pkg. Every failed check must add one to link_pkg::errors, and none to the file-scope counter.

The tests run on Icarus 12, which cannot assign through a package prefix: link_pkg::errors++ is a syntax error there. An import works.

Signals in the Timeline

SignalWidthRole
value16input of each check
Full specification
Compiled before your file, in the same unit (you cannot change it)
// old_defs.sv: file-scope helpers of an older checker, compiled in the same unit.
parameter int MAX_LEN = 255;
int errors = 0;
function automatic bit parity(input logic [7:0] b);
  return ~^b;                      // odd parity
endfunction

CallCorrect result
u_len.ok(len)1 when len is 1 to 16, the limit in link_pkg. Otherwise 0, and link_pkg::errors goes up by one.
u_par.ok(data, p)1 when p is the even parity of data, as in link_pkg. Otherwise 0, and link_pkg::errors goes up by one.

Each case makes one call and reports its result and both counters: the one in link_pkg and the one at file scope. The counters carry on from case to case.

Examples

Example 1

A Frame Longer Than link_pkg Allows
Input
check_len(20)
Output
ok = no, link_pkg_errors = 1, file_scope_errors = 0

The limit in link_pkg is 16, so the frame is refused and link_pkg::errors becomes 1. With the old file-scope limit of 255 it would pass.

Example 2

Even Parity of a Byte with One Bit Set
Input
check_parity(8'h01, 1)
Output
ok = yes, link_pkg_errors = 1, file_scope_errors = 0

One bit is set, so the even-parity bit is 1 and the check passes. The old file-scope function uses odd parity and would refuse it.

Example 3

The Longest Legal Frame
Input
check_len(16)
Output
ok = yes, link_pkg_errors = 1, file_scope_errors = 0

Sixteen bytes is legal, and the counters do not move.

Constraints and Contract

  • Keep the package, the module names and both ok functions.
  • Do not declare anything new at file scope.
  • The file-scope counter must stay at 0.

Hints

Hint 1

Neither module declares MAX_LEN, errors or parity. Where does the search for a name go after the module?

Hint 2

A package offers its names only where it is imported, or where a name is written with the package in front. On Icarus 12, only an import lets you change errors.

Hint 3

Check all three names in both modules. A module that imports one name by hand can still take the others from file scope.

What the grader checks: Both checkers are called with lengths on each side of both limits and with parity bits of both kinds. Each call reports the package counter and the file-scope counter. Passing shows all three names in both modules now mean link_pkg. The runs use Icarus 12.

22 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 3 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

A Frame Longer Than link_pkg Allows

Input
check_len(20)
Expected output
ok = no, link_pkg_errors = 1, file_scope_errors = 0

The limit in link_pkg is 16, so the frame is refused and link_pkg::errors becomes 1. With the old file-scope limit of 255 it would pass.

Run samples executes all 3 public checks. Check all tests runs 22 checks on the server.

Icarus · browser samples / server checks