B3.2.1 A Constrained-Random Generator with a Seed

MediumImplement25 minIcarus · browser samples / server checksReview the topic

Write the generator of a random ALU bench. It draws legal transactions only, can reach every value, and repeats exactly from a seed.

Hardware context: The same ALU driven by a seeded random generator. The generator of a random bench.

A random bench is only as good as its generator. If it draws illegal inputs, the bench reports failures that are not bugs. If it ignores its seed, a failure can never be replayed. If it never draws some legal input, that input is never tested.

Write the module alu_gen with two tasks. start(s) starts the generator from the seed s. next(op, a, b) returns one random transaction that keeps both rules below. Use $urandom, $urandom_range or both.

The rows check what any correct generator must do. They never compare your numbers with a fixed list, so any legal way of drawing is accepted.

Signals in the Timeline

SignalWidthRole
scenario8input of each check
Full specification
RuleWhat it says
1op is ADD (0), SUB (1), AND (2) or XOR (3). Codes 4 to 7 are reserved and never drawn.
2In a SUB, a is never smaller than b. (An exercise rule: this ALU's users never subtract a bigger number.)
3After start(s), the draws depend only on s: the same seed gives the same sequence, every time.
4Every legal value is possible: each operation, and every value of a and of b. Each operation is at least 15 percent of the draws.

In Icarus, the seed argument of $urandom must be a variable: keep the seed in one, as in seed = s; r = $urandom(seed);. Later calls of $urandom with no argument then continue that sequence.

Examples

Example 1

Seed 1: 2,000 Draws, All Legal
Input
start(1), then 2,000 calls of next
Output
illegal_draws = 0

Every draw is checked against rules 1 and 2. A reserved code, a SUB of a bigger number or an unknown bit counts as illegal.

Example 2

The Same Seed Twice Gives the Same 100 Draws
Input
start(7), 100 draws; start(7) again, 100 draws
Output
same_sequence = yes

Rule 3: start(7) twice must give the same 100 transactions. A generator that is never started from the seed fails here.

Example 3

Seeds 1 and 2 Give Different Draws
Input
The first 20 draws of each
Output
sequences_differ = yes

Rule 3 the other way: two seeds must not give the same sequence. A generator that always starts from one fixed seed fails here.

Constraints and Contract

  • Keep the module name alu_gen and the two tasks with these arguments.
  • Do not read plusargs or the time; the draws must depend only on the seed.

Hints

Hint 1

$urandom_range(3, 0) draws a number from 0 to 3.

Hint 2

For rule 2, swapping a and b keeps every value possible. Redrawing until a >= b works too.

Hint 3

start must do more than store s: it must start the generator from it.

What the grader checks: The rows call start and next and check rules 1 to 4 over thousands of draws. They check legality, repeatability, and that two seeds differ. Each operation must be at least 15 percent of the draws, and a and b must reach 250 of their 256 values. The exact numbers drawn are never compared.

7 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 2 problems on this path

Report a problem with this page

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

Seed 1: 2,000 Draws, All Legal

Input
start(1), then 2,000 calls of next
Expected output
illegal_draws = 0

Every draw is checked against rules 1 and 2. A reserved code, a SUB of a bigger number or an unknown bit counts as illegal.

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

Icarus · browser samples / server checks