Consider the following Verilog module modeling a custom register update w…
Consider the following Verilog module modeling a custom register update with a function: module test; reg [3:0] q; reg clk; function [3:0] calc; input [3:0] din; begin din = din + 4'd2; calc = din * 2; end endfunction initial begin clk = 0; q = 4'd3; #5 clk = 1; end always @(posedge clk) begin q <= calc(q); end endmodule What is the value of q immediately after the first clock edge at t = 5 ns ?
- 4'd3
- 4'd8
- 4'd10
- 4'd7
Correct answer
C. 4'd10
Explanation
1. At $t = 5$, the rising edge of clk occurs. 2. The right-hand side of the non-blocking assignment evaluates calc(q) with the current value of q = 4'd3 . 3. Inside function calc : * din receives 4'd3 . * din = din + 4'd2 updates local variable din to $3 + 2 = 5$ ( 4'd5 ). * calc = din * 2 computes $5 \times 2 = 10$ ( 4'd10 or 4'b1010 ). 4. In the NBA update phase of the time step, q takes the scheduled value 4'd10 .