Verilog HDL: Module
Verilog HDL: Module
Verilog HDL: Module
Modeling Levels
Switch-Level, Gate-Level, Dataflow, Behavioral
Assignments
Blocking assignment: “=”– executed in order they appear in a block
Nonblocking assignment: “<=”– allow scheduling of assignments without blocking execution of
statements that follow in a sequential block
Continuous assignment “assign A = B;” – connects nets permanently.
Example 1:
#10 A = 1’b1;
counter = 0; //executed at 10 (after A assignment)
Example 2:
A <= #10 1’b1;
counter <= 0; // executed at time 0 (before A assignment)
Module definition:
Gate-Level Modeling
endmodule
module stimulus;
// declare variables
reg[3:0] A, B;
reg C_IN;
wire [3:0] SUM;
wire C_OUT;
initial
begin
$monitor($time, “A=%b B=%b Cin=%b, -> Sum = %b Cout=%b\n”, A, B, C_IN, SUM,
C_OUT);
end
// stimulate inputs
initial
begin // sequential block begins
A = 4’d0; B = 4’d0, C_IN = 1’b0; // 0 + 0 + 0
#10 A = 4’d2; B=4’d2; // 2 + 2 + 0
#10 A = 4’d5; B=4’d8; // 5 + 8 + 0
#10 C_IN = 1’b1 // 5 + 8 + 1
end
endmodule
Dataflow Modeling
endmodule
Behavioral Modeling
module ClkGen;
reg clk;
initial
clk = 1’b0;
always
#10 clk = ~clk;
initial
#1000 $finish; //or $stop to end simulation
endmodule
endmodule
Example 8: D-Type Latch
module Latch(D, C, Q)
input D, C;
output Q;
reg Q; // output must preserve values
initial
Q = 1’b0;
always @(C or D)
begin
if(C == 1'b1)
#10 Q = D;
end
endmodule
initial
begin
Q = 1’b0;
QN = 1’b1;
end
endmodule
wire D, QN;
case (DATA)
4’b0000: if(CLK == 1’b0)
SEGMENTS = ‘DSP0;
else
SEGMENTS = ‘DSP0 ^ 7’b1111111;
4’b0001: if(CLK == 1’b0)
SEGMENTS = ‘DSP1;
else
SEGMENTS = ‘DSP1 ^ 7’b1111111;
4’b0010: if(CLK == 1’b0)
SEGMENTS = ‘DSP2;
else
SEGMENTS = ‘DSP2 ^ 7’b1111111;
4’b0011: if(CLK == 1’b0)
SEGMENTS = ‘DSP3;
else
SEGMENTS = ‘DSP3 ^ 7’b1111111;
4’b0100: if(CLK == 1’b0)
SEGMENTS = ‘DSP4;
else
SEGMENTS = ‘DSP4 ^ 7’b1111111;
4’b0101: if(CLK == 1’b0)
SEGMENTS = ‘DSP5;
else
SEGMENTS = ‘DSP5 ^ 7’b1111111;
4’b0110: if(CLK == 1’b0)
SEGMENTS = ‘DSP6;
else
SEGMENTS = ‘DSP6 ^ 7’b1111111;
4’b0111: if(CLK == 1’b0)
SEGMENTS = ‘DSP7;
else
SEGMENTS = ‘DSP7 ^ 7’b1111111;
4’b1000: if(CLK == 1’b0)
SEGMENTS = ‘DSP8;
else
SEGMENTS = ‘DSP8 ^ 7’b1111111;
4’b1001: if(CLK == 1’b0)
SEGMENTS = ‘DSP9;
else
SEGMENTS = ‘DSP9 ^ 7’b1111111;
default: if(CLK == 1’b0)
SEGMENTS = ‘BLANK;
else
SEGMENTS = ‘BLANK ^ 7’b1111111;
endcase
end
endmodule
Two roads intersect: the highway and the country road. On the highway the green light is always on
unless the sensor on the country road detects a car. The green light on the country road stays on until all
cars leave that road. Model the traffic lights there. Set 3 clock cycle delay for yellow to red signal
change and 2 for red to green for both directions.
initial
begin
state = ‘S0;
nextstate = ‘S0;
highway_signal = ‘GREEN;
country_signal = ‘RED;
end
always @(state)
begin
case (state)
‘S0: begin
highway_signal = ‘GREEN;
country_signal = ‘RED;
end
‘S1: begin
highway_signal = ‘YELLOW;
country_signal = ‘RED;
end
‘S2: begin
highway_signal = ‘RED;
country_signal = ‘RED;
end
‘S3: begin
highway_signal = ‘RED;
country_signal = ‘GREEN;
end
‘S4: begin
highway_signal = ‘RED;
country_signal = ‘YELLOW;
end
endcase
end
always @(state or sensor)
begin
case(state)
‘S0: if(sensor)
nextstate = ‘S1;
else
nextstate = ‘S0;
‘S1: begin
repeat(Y2RDELAY) @(posedge clock)
nextstate = ‘S2;
end
‘S2: begin
repeat(R2GDELAY) @(posedge clock)
nextstate = ‘S3;
end
‘S3: if(sensor)
nextstate = ‘S3;
else
nextstate = ‘S4;
‘S4: begin
repeat(Y2RDELAY) @(posedge clock)
nextstate = ‘S0;
end
default: nextstate = ‘S0;
endcase
end
endmodule
always @(addr)
begin
left_addr = shift(addr, ‘LEFT_SHIFT);
right_addr = shift(addr, ‘RIGHT_SHIFT);
end
begin
shift = (control == ‘LEFT_SHIFT) ? (address << 1) : (address >> 1);
end
endfunction
endmodule
Example 14: Bitwise Operator Using Verilog Tasks
module operation;
taks bitwise_oper;
output [15:0] ab_and, ab_or, ab_xor;
input [15:0] a, b
begin
#delay ab_and = a & b;
ab_or = a | b;
ab_xor = a ^ b;
end
endtask
endmodule
Switch-Level Modeling
endmodule