跳转至

Shift registers

Shift 4

Verilog
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
module top_module(
    input clk,
    input areset,  // async active-high reset to zero
    input load,
    input ena,
    input [3:0] data,
    output reg [3:0] q); 
    always @(posedge clk or posedge areset) begin
        if (areset) begin
            q <= 4'b0000;  // Reset q to 0
        end else if (load) begin
            q <= data;  // Load the data into q
        end else if (ena) begin
            q <= q >> 1;  // Shift q right by 1 when enabled
        end
    end
endmodule
How dose it work when both ena and load are 1? In the Verilog code you provided, the condition where both load and ena are high is handled by the sequential priority of the if-else statements. Here's how it works:

  1. if (areset): This takes the highest priority because it's the first condition checked. If areset is high, the output q is reset to 4'b0000, regardless of the states of load or ena.

  2. else if (load): This is checked next. If areset is not active and load is high, q will be loaded with the value of data. This happens even if ena is also high. The load operation takes priority over the enable (ena) because it comes before it in the else if chain.

  3. else if (ena): This block is only executed if both areset is low and load is low. In this case, if ena is high, the value of q will be right-shifted. However, this will not happen if load is also high, as the load condition would already have been executed, skipping the ena block.

Summary of load and ena behavior:

  • If both load and ena are high at the same time, load takes priority. q will be loaded with data, and the ena block (right shift) will not execute.
  • If only ena is high, the right shift operation will take place.

This ensures that when load is asserted, it overrides the enable behavior, preventing simultaneous conflicting operations.