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
| Signal | Width | Role |
|---|---|---|
scenario | 8 | input of each check |
Full specification
| Rule | What it says |
|---|---|
| 1 | op is ADD (0), SUB (1), AND (2) or XOR (3). Codes 4 to 7 are reserved and never drawn. |
| 2 | In a SUB, a is never smaller than b. (An exercise rule: this ALU's users never subtract a bigger number.) |
| 3 | After start(s), the draws depend only on s: the same seed gives the same sequence, every time. |
| 4 | Every 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