Variables: Wire Reg
Variables: Wire Reg
F wire
Ì Connects components together
F reg
Ì Saves a value
Á Part of a behavioral description
Ì Does NOT necessarily become a register when you synthesize
Á May become a wire
F The rule
Ì Declare a variable as reg if it is a target of an assignment statement
Á Continuous assign doesn’t count
arithmetic operator
always @(sel or A or B or C or D)
if (sel == 2’b00) Y = A;
else if (sel == 2’b01) Y = B;
else if (sel == 2’b10) Y = C;
else if (sel == 2’b11) Y = D;
endmodule
CSE467, Sequential Verilog 10
if: Another way
always @(sel or A or B or C or D)
if (sel[0] == 0)
if (sel[1] == 0) Y = A;
else Y = B;
else
if (sel[1] == 0) Y = C;
else Y = D;
endmodule
always @(sel or A or B or C or D)
case (sel)
2’b00: Y = A;
2’b01: Y = B;
2’b10: Y = C;
2’b11: Y = D;
endcase
endmodule
always @(A)
case (A)
8’b00000001: Y = 0; If you omit the default, the compiler will create
8’b00000010: Y = 1; a latch for Y
8’b00000100: Y = 2; fi Or you can list all 256 cases
8’b00001000: Y = 3; Better way: Use a function
8’b00010000: Y = 4; fi Compiler will warn you of missing cases
8’b00100000: Y = 5;
8’b01000000: Y = 6;
8’b10000000: Y = 7;
default: Y = 3’bx; // Don’t care when input isnt 1-hot
endcase
endmodule
always @(A)
case (1’b1)
A[0]: Y = 0;
A[1]: Y = 1; Case statements execute sequentially
A[2]: Y = 2;
A[3]: Y = 3;
fi Take the first alternative that matches
A[4]: Y = 4;
A[5]: Y = 5;
A[6]: Y = 6;
A[7]: Y = 7;
default: Y = 3’bx; // Don’t care when input is all 0’s
endcase
endmodule
always @(A)
casez (A)
8’bzzzz0001: Y = 0;
8’bzzzz0010: Y = 1;
8’bzzzz0100: Y = 2;
8’bzzzz1000: Y = 3;
default: Y = 2’bx; // Don’t care when input isnt 1-hot
endcase
endmodule
endmodule // reg8
endmodule // shiftReg
F Example: Swap
always @(posedge CLK) always @(posedge CLK)
begin begin
temp = B; A <= B;
B = A; B <= A;
A = temp; end
end
endmodule