A register stores multiple bits of data at once. It remembers the data until you change it.
Register (multi-bit flip-flop) in Verilog
module register #(parameter WIDTH = 8) ( input wire clk, input wire reset, input wire [WIDTH-1:0] d, output reg [WIDTH-1:0] q ); always @(posedge clk or posedge reset) begin if (reset) q <= {WIDTH{1'b0}}; else q <= d; end endmodule
WIDTH sets how many bits the register holds.
The always block updates the register on the clock's rising edge or resets it.
module reg4bit ( input wire clk, input wire reset, input wire [3:0] d, output reg [3:0] q ); always @(posedge clk or posedge reset) begin if (reset) q <= 4'b0000; else q <= d; end endmodule
module reg16bit ( input wire clk, input wire reset, input wire [15:0] d, output reg [15:0] q ); always @(posedge clk or posedge reset) begin if (reset) q <= 16'h0000; else q <= d; end endmodule
This testbench creates an 8-bit register. It resets the register, then changes input data every 10 time units. The output shows how the register stores the data on each clock pulse.
module test_register; reg clk = 0; reg reset; reg [7:0] d; wire [7:0] q; // Instantiate the 8-bit register register #(8) myreg ( .clk(clk), .reset(reset), .d(d), .q(q) ); // Clock generation always #5 clk = ~clk; initial begin $monitor($time, " clk=%b reset=%b d=%b q=%b", clk, reset, d, q); reset = 1; d = 8'b00000000; #10 reset = 0; d = 8'b10101010; #10 d = 8'b11110000; #10 d = 8'b00001111; #10 $finish; end endmodule
Use non-blocking assignment (<=) inside always blocks for registers.
Resetting the register clears stored data to a known state.
Registers update only on clock edges, so timing is important.
A register holds multiple bits of data and updates on clock edges.
Use parameters to set the register size for flexibility.
Reset input helps start the register with a known value.
Practice
What is the main purpose of a multi-bit register in Verilog?
Solution
Step 1: Understand the function of a register
A register is used to hold data bits temporarily in digital circuits.Step 2: Recognize clock edge behavior
Registers update their stored data only on clock edges, making them synchronous storage elements.Final Answer:
To store multiple bits of data and update them on clock edges -> Option AQuick Check:
Register = multi-bit storage updated on clock edges [OK]
- Confusing registers with combinational logic
- Thinking registers generate clocks
- Assuming registers perform arithmetic
Which of the following is the correct way to declare an 8-bit register in Verilog?
?Solution
Step 1: Recall Verilog register declaration syntax
Registers are declared with 'reg' keyword followed by bit range in square brackets before the name.Step 2: Check each option
reg [7:0] my_reg; uses correct syntax: 'reg [7:0] my_reg;'. Others have syntax errors or wrong keywords.Final Answer:
reg [7:0] my_reg; -> Option AQuick Check:
Correct reg declaration = reg [7:0] name; [OK]
- Placing bit range after variable name
- Using 'register' keyword instead of 'reg'
- Using colon instead of square brackets
Given the following Verilog code, what will be the value of reg_out after the positive edge of clk if reset is 0 and data_in is 8'b10101010?
module reg_example(input clk, input reset, input [7:0] data_in, output reg [7:0] reg_out);
always @(posedge clk or posedge reset) begin
if (reset)
reg_out <= 8'b00000000;
else
reg_out <= data_in;
end
endmoduleSolution
Step 1: Analyze the always block sensitivity
The block triggers on positive edge of clk or reset. If reset is high, reg_out resets to zero.Step 2: Check conditions given
Reset is 0, so reg_out will be assigned data_in value on clk rising edge, which is 8'b10101010.Final Answer:
8'b10101010 -> Option BQuick Check:
Reset=0, reg_out = data_in = 10101010 [OK]
- Assuming reset clears reg_out when reset=0
- Ignoring clock edge sensitivity
- Confusing blocking and non-blocking assignments
Identify the error in this Verilog register code snippet:
always @(posedge clk) begin
if (reset == 1)
reg_data = 8'b0;
else
reg_data <= input_data;
endSolution
Step 1: Review assignment types in sequential blocks
Inside always @(posedge clk), non-blocking assignments (<=) should be used for registers to avoid race conditions.Step 2: Identify the error in reset assignment
The reset assignment uses blocking '=' which is incorrect; it should be '<=' to match the else branch.Final Answer:
Using blocking assignment (=) for reset instead of non-blocking (<=) -> Option CQuick Check:
Use <= for all register assignments in clocked always blocks [OK]
- Mixing blocking and non-blocking assignments
- Not including reset in sensitivity list (not error here but common)
- Using '==' in if condition is correct syntax
You want to create a parameterized multi-bit register module with asynchronous reset that can hold WIDTH bits. Which of the following code snippets correctly implements this?
module param_reg #(parameter WIDTH = 8) (
input clk,
input reset,
input [WIDTH-1:0] data_in,
output reg [WIDTH-1:0] data_out
);
always @(posedge clk or posedge reset) begin
if (reset)
data_out <= {WIDTH{1'b0}};
else
data_out <= data_in;
end
endmoduleSolution
Step 1: Check parameter usage
The module declares parameter WIDTH and uses it to size inputs and outputs correctly.Step 2: Verify asynchronous reset and assignments
The always block triggers on posedge clk or reset, and reset asynchronously clears data_out using replication operator.Step 3: Confirm data_out declared as reg
data_out is declared as reg, which is required for assignment inside always block.Final Answer:
Correct parameterized register with asynchronous reset -> Option DQuick Check:
Parameter + async reset + reg output = correct [OK]
- Forgetting parameter declaration
- Using synchronous reset when async needed
- Declaring output as wire instead of reg
Ready to prove it?
Pass the test and this pattern moves unknown
