06 - Basic Verilog Coding &sequential Logic
06 - Basic Verilog Coding &sequential Logic
Sequential Logic
謝東佑
國立中山大學電機系
Office: F7038
07-5252000 Ext. 4114
[email protected]
PDSD Unit6-1 NSYSUEE-TYHSIEH
Objective, Outline &
Application
Objective
Understand how to design a sequential
circuit using Verilog
Outline
Basics of sequential circuits
Models for designing sequential circuits
design
Applications
Homework assignment
Project
specify specify
$setup(data, posedge clock, 3); $hold(posedge clock, data, 5);
endspecify endspecify
PDSD Unit6-6 NSYSUEE-TYHSIEH
Verilog RTL Coding for
Sequential Logic
Non-blocking assignments are usually
used.
Allow scheduling of assignments without
blocking execution of the statements that
follow in a sequential block
Can model several concurrent data transfers
that take place after a common event
always @(posedge clock)
begin
reg1 <= #1 in1;
reg2 <= @(negedge clock) in2 ^ in3;
reg3 <= #1 reg1; //The old value of reg1
end
PDSD Unit6-7 NSYSUEE-TYHSIEH
Concurrent Data Transfer
always @(posedge clock)
always @(posedge clock) a<=b;
a=b; always @(posedge clock)
always @(posedge clock) b<=a;
b=a;
always @(posedge clock)
begin
The race condition occur. The temp_a = a;
result depends on the temp_b = b;
simulation process. a = temp_b;
b = temp_a;
end
SET
D Q
SET
D Q LCLR Q
LCLR Q
SET
D Q
LCLR Q
module module2;
module module1 (c, a, b); …
… module1 #(8, 2.14) m1 (c, a, b);
parameter length=15, /* length=8
real_const=3.14, real_const=2.14
x_word=16’bx; x_word=16’bx */
… module1 #(.real_const (2.14), .x_word
wire [length:0] w1; (8’bx)) m2 (c, a, b);
… …
endmodule endmodule
PDSD Unit6-23 NSYSUEE-TYHSIEH
Initialize A Memory
Assigning values to each word of the
memory array.
Call $readmemb or $readmemh system
task
$readmemb("<file_name>",
<memory_name>);
$readmemb("<file_name>", <memory_name>,
<start_addr>);
$readmemb("<file_name>", <memory_name>,
<start_addr>,<finish_addr>);
Identical syntax for $readmemh
Content of init.dat
0000_0000
0110_0001 0011_0010
//addresses 3-255 are not defined
@100
1111_1100
//addresses 257-1022 are not defined
@3FF
1110_0010
Farmroad
HL FL
Highway
Highway
HL
FL
Farmroad
States Description
S0 HG Highway green (farmroad red)
S1 HY Highway yellow (farmroad red)
S2 FG Farmroad green (highway red)
S3 FY Farmroad yellow (highway red)
parameter S0=2’b00,S1=2’b01,S2=2’b10,S3=2’b11;
assign HG=(state==S0);
assign HY=(state==S1);
assign HR=((state==S2)||(state==S3));
assign FG=(state==S2);
assign FY=(state==S3);
assign FR=((state==S0)||(state==S1));
// flip-flops
always @(posedge clk or posedge reset)
if(reset) // an asynchronous reset
begin
state<=S0;
ST_out<=0;
end
else begin
state<=next_state;
ST_out<=ST;
end
PDSD Unit6-31 NSYSUEE-TYHSIEH
Verilog Description (cont.)
// Combinational circuit
always @(state or c or tl or ts)
S0:if(TL&C) begin
next_state=S1;
ST=1;
end
else begin
next_state=S0;
ST=0;
end
S1:if(TS) begin
next_state=S2;
ST=1;
end
else begin
next_state=S1;
ST=0;
PDSD end Unit6-32 NSYSUEE-TYHSIEH
Verilog Description (cont.)
S2:if(TL|!C) begin
next_state=S3;
ST=1;
end
else begin
next_state=S2;
ST=0;
end
S3:if(TS) begin
next_state=S0;
ST=1;
end
else begin
next_state=S3;
ST=0;
end
endcase
endmodule