组合逻辑电路在逻辑功能上特点是任意时刻的输出仅仅取决于当前时刻的输入,与电路原来的状态无关。而时序逻辑在逻辑功能上的特点是任意时刻的输出不仅仅取决于当前的输入信号, 而且还取决于电路原来的状态。下面以典型的时序逻辑分析。
1、D触发器
D 触发器在时钟的上升沿或下降沿存储数据,输出与时钟跳变之前输入信号的状态相同。
代码如下:
module top(d, clk, q) ;
input d ;
input clk ;
output reg q ;
always @(posedge clk)
begin
q <= d ;
end
endmodule
激励文件如下:
`timescale 1 ns/1 ns
module top_tb() ;
reg d ;
reg clk ;
wire q ;
initial
begin
d = 0 ;
clk = 0 ;
forever
begin
#({$random}%100)
d = ~d ;
end
end
always #10 clk = ~clk ;
top t0(.d(d),.clk(clk),.q(q)) ;
endmodule
RTL 图表示如下
仿真结果如下,可以看到在 t0 时刻时,d 的值为 0,则 q 的值也为 0;在 t1 时刻 d 发生了变 化,值为 1,那么 q 相应也发生了变化,值变为 1。可以看到在 t0-t1 之间的一个时钟周期内,无 论输入信号 d 的值如何变化,q 的值是保持不变的,也就是有存储的功能,保存的值为在时钟的 跳变沿时 d 的值。
2、两级D触发器
软件是按照两级 D 触发器的模型进行时序分析的,具体可以分析在同一时刻两个 D 触发器输 出的数据有何不同,其 RTL 图如下:
代码如下:
module top(d, clk, q, q1) ;
input d ;
input clk ;
output reg q ;
output reg q1 ;
always @(posedge clk)
begin
q <= d ;
end
always @(posedge clk)
begin
q1 <= q ;
end
endmodule
激励文件如下:
`timescale 1 ns/1 ns
module top_tb() ;
reg d ;
reg clk ;
wire q ;
wire q1 ;
initial
begin
d = 0 ;
clk = 0 ;
forever
begin
#({$random}%100)
d = ~d ;
end
end
always #10 clk = ~clk ;
top
t0(.d(d),.clk(clk),.q(q),.q1(q1)) ;
endmodule
仿真结果如下,可以看到 t0 时刻,d 为 0,q 输出为 0,t1 时刻,q 随着 d 的数据变化而变化, 而此时钟跳变之前 q 的值仍为 0,那么 q1 的值仍为 0,t2 时刻,时钟跳变前 q 的值为 1,则 q1 的 值相应为 1,q1 相对于 q 落后一个周期。
3、带异步复位的 D 触发器
异步复位是指独立于时钟,一旦异步复位信号有效,就触发复位操作。这个功能在写代码时 会经常用到,用于给信号复位,初始化。其 RTL 图如下:
代码如下,注意要把异步复位信号放在敏感列表里,如果是低电平复位,即为 negedge,如 果是高电平复位,则是 posedge
module top(d, rst, clk, q) ;
input d ;
input rst ;
input clk ;
output reg q ;
always @(posedge clk or negedge rst)
begin
if (rst == 1'b0)
q <= 0 ;
else
q <= d ;
end
endmodule
激励文件:
`timescale 1 ns/1 ns
module top_tb() ;
reg d ;
reg rst ;
reg clk ;
wire q ;
initial
begin
d = 0 ;
clk = 0 ;
forever
begin
#({$random}%100)
d = ~d ;
end
end
initial
begin
rst = 0 ;
#200 rst = 1 ;
end
always #10 clk = ~clk ;
top
t0(.d(d),.rst(rst),.clk(clk),.q(q)) ;
endmodule
仿真结果如下,可以看到在复位信号之前,虽然输入信号 d 数据有变化,但由于正处于复位 状态,输入信号 q 始终为 0,在复位之后 q 的值就正常了。
4、带异步复位同步清零的 D 触发器
前面讲到异步复位独立于时钟操作,而同步清零则是同步于时钟信号下操作的,当然也不仅 限于同步清零,也可以是其他的同步操作,其 RTL 图如下:
代码如下,不同于异步复位,同步操作不能把信号放到敏感列表里
module top(d, rst, clr, clk, q) ;
input d ;
input rst ;
input clr ;
input clk ;
output reg q ;
always @(posedge clk or negedge rst)
begin
if (rst == 1'b0)
q <= 0 ;
else if (clr == 1'b1)
q <= 0 ;
else
q <= d ;
end
endmodule
激励文件:
`timescale 1 ns/1 ns
module top_tb() ;
reg d ;
reg rst ;
reg clr ;
reg clk ;
wire q ;
initial
begin
d = 0 ;
clk = 0 ;
forever
begin
#({$random}%100)
d = ~d ;
end
end
initial
begin
rst = 0 ;
clr = 0 ;
#200 rst = 1 ;
#200 clr = 1 ;
#100 clr = 0 ;
end
always #10 clk = ~clk ;
top
t0(.d(d),.rst(rst),.clr(clr),.clk(clk),
.q(q)) ;
endmodule
仿真结果如下,可以看到 clr 信号拉高后,q 没有立即清零,而是在下个 clk 上升沿之后执行 清零操作,也就是 clr 同步于 clk。
5、移位寄存器
移位寄存器是指在每个时钟脉冲来时,向左或向右移动一位,由于 D 触发器的特性,数据输 出同步于时钟边沿,其结构如下,每个时钟来临,每个 D 触发器的输出 q 等于前一个 D 触发器输 出的值,从而实现移位的功能。
代码实现:
module top(d, rst, clk, q) ;
input d ;
input rst ;
input clk ;
output reg [7:0] q ;
always @(posedge clk or negedge rst)
begin
if (rst == 1'b0)
q <= 0 ;
else
q <= {q[6:0], d} ; //向左移位
//q <= {d, q[7:1]} ; //向右移位
end
endmodule
激励文件:
`timescale 1 ns/1 ns
module top_tb() ;
reg d ;
reg rst ;
reg clk ;
wire [7:0] q ;
initial
begin
d = 0 ;
clk = 0 ;
forever
begin
#({$random}%100)
d = ~d ;
end
end
initial
begin
rst = 0 ;
#200 rst = 1 ;
end
always #10 clk = ~clk ;
top
t0(.d(d),.rst(rst),.clk(clk),.q(q)) ;
endmodule
仿真结果如下,可以看到复位之后,每个 clk 上升沿左移一位
6、单口RAM
单口 RAM 的写地址与读地址共用一个地址,代码如下,其中 reg [7:0] ram [63:0]意思是定义 了 64 个 8 位宽度的数据。其中定义了 addr_reg,可以保持住读地址,延迟一周期之后将数据送出。
代码:
module top
(
input [7:0] data,
input [5:0] addr,
input wr,
input clk,
output [7:0] q
);
reg [7:0] ram[63:0]; //declare ram
reg [5:0] addr_reg; //addr register
always @ (posedge clk)
begin
if (wr) //write
ram[addr] <= data;
addr_reg <= addr;
end
assign q = ram[addr_reg]; //read data
endmodule
激励:
`timescale 1 ns/1 ns
module top_tb() ;
reg [7:0] data ;
reg [5:0] addr ;
reg wr ;
reg clk ;
wire [7:0] q ;
initial
begin
data = 0 ;
addr = 0 ;
wr = 1 ;
clk = 0 ;
end
always #10 clk = ~clk ;
always @(posedge clk)
begin
data <= data + 1'b1 ;
addr <= addr + 1'b1 ;
end
top t0(.data(data),
.addr(addr),
.clk(clk),
.wr(wr),
.q(q)) ;
endmodule
仿真结果如下,可以看到 q 的输出与写入的数据一致
7、伪双口 RAM
伪双口 RAM 的读写地址是独立的,可以随机选择写或读地址,同时进行读写操作。代码如下, 在激励文件中定义了 en 信号,在其有效时发送读地址。
module top
(
input [7:0] data,
input [5:0] write_addr,
input [5:0] read_addr,
input wr,
input rd,
input clk,
output reg [7:0] q
);
reg [7:0] ram[63:0]; //declare ram
reg [5:0] addr_reg; //addr register
always @ (posedge clk)
begin
if (wr) //write
ram[write_addr] <= data;
if (rd) //read
q <= ram[read_addr];
end
endmodule
激励:
`timescale 1 ns/1 ns
module top_tb() ;
reg [7:0] data ;
reg [5:0] write_addr ;
reg [5:0] read_addr ;
reg wr ;
reg clk ;
reg rd ;
wire [7:0] q ;
initial
begin
data = 0 ;
write_addr = 0 ;
read_addr = 0 ;
wr = 0 ;
rd = 0 ;
clk = 0 ;
#100 wr = 1 ;
#20 rd = 1 ;
end
always #10 clk = ~clk ;
always @(posedge clk)
begin
if (wr)
begin
data <= data + 1'b1 ;
write_addr <= write_addr + 1'b1 ;
if (rd)
read_addr <= read_addr + 1'b1 ;
end
end
top t0(.data(data),
.write_addr(write_addr),
.read_addr(read_addr),
.clk(clk),
.wr(wr),
.rd(rd),
.q(q)) ;
endmodule
仿真结果如下,可以看到在 rd 有效时,对读地址进行操作,读出数据
8、真双口 RAM
真双口 RAM 有两套控制线,数据线,允许两个系统对其进行读写操作,代码如下:
module top
(
input [7:0] data_a, data_b,
input [5:0] addr_a, addr_b,
input wr_a, wr_b,
input rd_a, rd_b,
input clk,
output reg [7:0] q_a, q_b
);
reg [7:0] ram[63:0]; //declare ram
//Port A
always @ (posedge clk)
begin
if (wr_a) //write
begin
ram[addr_a] <= data_a;
q_a <= data_a ;
end
if (rd_a)
//read
q_a <= ram[addr_a];
end
//Port B
always @ (posedge clk)
begin
if (wr_b) //write
begin
ram[addr_b] <= data_b;
q_b <= data_b ;
end
if (rd_b)
//read
q_b <= ram[addr_b];
end
endmodule
激励:
`timescale 1 ns/1 ns
module top_tb() ;
reg [7:0] data_a, data_b ;
reg [5:0] addr_a, addr_b ;
reg wr_a, wr_b ;
reg rd_a, rd_b ;
reg clk ;
wire [7:0] q_a, q_b ;
initial
begin
data_a = 0 ;
data_b = 0 ;
addr_a = 0 ;
addr_b = 0 ;
wr_a = 0 ;
wr_b = 0 ;
rd_a = 0 ;
rd_b = 0 ;
clk = 0 ;
#100 wr_a = 1 ;
#100 rd_b = 1 ;
end
always #10 clk = ~clk ;
always @(posedge clk)
begin
if (wr_a)
begin
data_a <= data_a + 1'b1 ;
addr_a <= addr_a + 1'b1 ;
end
else
begin
data_a <= 0 ;
addr_a <= 0 ;
end
end
always @(posedge clk)
begin
if (rd_b)
begin
addr_b <= addr_b + 1'b1 ;
end
else addr_b <= 0 ;
end
top
t0(.data_a(data_a), .data_b(data_b),
.addr_a(addr_a), .addr_b(addr_b
),
.wr_a(wr_a), .wr_b(wr_b),
.rd_a(rd_a), .rd_b(rd_b),
.clk(clk),
.q_a(q_a), .q_b(q_b)) ;
endmodule
仿真结果如下:
9、单口 ROM
ROM 是用来存储数据的,可以按照下列代码形式初始化 ROM,但这种方法处理大容量的 ROM 就比较麻烦,建议用 FPGA 自带的 ROM IP 核实现,并添加初始化文件。
代码:
module top
(
input [3:0] addr,
input clk,
output reg [7:0] q
);
reg [7:0] rom [15:0] ; //declare rom
always @(addr)
begin
case(addr)
4'd0 : rom[addr] = 8'd15 ;
4'd1 : rom[addr] = 8'd24 ;
4'd2 : rom[addr] = 8'd100 ;
4'd3 : rom[addr] = 8'd78 ;
4'd4 : rom[addr] = 8'd98 ;
4'd5 : rom[addr] = 8'd105 ;
4'd6 : rom[addr] = 8'd86 ;
4'd7 : rom[addr] = 8'd254 ;
4'd8 : rom[addr] = 8'd76 ;
4'd9 : rom[addr] = 8'd35 ;
4'd10 : rom[addr] = 8'd120 ;
4'd11 : rom[addr] = 8'd85 ;
4'd12 : rom[addr] = 8'd37 ;
4'd13 : rom[addr] = 8'd19 ;
4'd14 : rom[addr] = 8'd22 ;
4'd15 : rom[addr] = 8'd67 ;
endcase
end
always @(posedge clk)
begin
q <= rom[addr] ;
end
endmodule
激励:
`timescale 1 ns/1 ns
module top_tb() ;
reg [3:0] addr ;
reg clk ;
wire [7:0] q ;
initial
begin
addr = 0 ;
clk = 0 ;
end
always #10 clk = ~clk ;
always @(posedge clk)
begin
addr <= addr + 1'b1 ;
end
top t0(.addr(addr),
.clk(clk),
.q(q)) ;
endmodule
仿真结果如下
10、有限状态机
在 verilog 里经常会用到有限状态机,处理相对复杂的逻辑,设定好不同的状态,根据触发条 件跳转到对应的状态,在不同的状态下做相应的处理。有限状态机主要用到 always 及 case 语句。 下面以一个四状态的有限状态机举例说明。
在程序中设计了 8 位的移位寄存器,在 Idle 状态下,判断 shift_start 信号是否为高,如果为 高,进入 Start 状态,在 Start 状态延迟 100 个周期,进入 Run 状态,进行移位处理,如果 shift_stop 信号有效了,进入 Stop 状态,在 Stop 状态,清零 q 的值,再跳转到 Idle 状态。
Mealy 有限状态机,输出不仅与当前状态有关,也与输入信号有关,在 RTL 中会与输入信号 有连接。
代码
module top
(
input shift_start,
input shift_stop,
input rst,
input clk,
input d,
output reg [7:0] q
);
parameter Idle = 2'd0 ; //Idle state
parameter Start = 2'd1 ; //Start state
parameter Run = 2'd2 ; //Run state
parameter Stop = 2'd3 ; //Stop state
reg [1:0] state ; //statement
reg [4:0] delay_cnt ; //delay counter
always @(posedge clk or negedge rst)
begin
if (!rst)
begin
state <= Idle ;
delay_cnt <= 0 ;
q <= 0 ;
end
else
case(state)
Idle : begin
if (shift_start)
state <= Start ;
end
Start : begin
if (delay_cnt == 5'd99)
begin
delay_cnt <= 0 ;
state <= Run ;
end
else
delay_cnt <= delay_cnt + 1'b1 ;
end
Run : begin
if (shift_stop)
state <= Stop ;
else
q <= {q[6:0], d} ;
end
Stop : begin
q <= 0 ;
state <= Idle ;
end
default: state <= Idle ;
endcase
end
endmodule
Moore 有限状态机,输出只与当前状态有关,与输入信号无关,输入信号只影响状态的改变, 不影响输出,比如对 delay_cnt 和 q 的处理,只与 state 状态有关。
module top
(
input shift_start,
input shift_stop,
input rst,
input clk,
input d,
output reg [7:0] q
);
parameter Idle = 2'd0 ; //Idle state
parameter Start = 2'd1 ; //Start state
parameter Run = 2'd2 ; //Run state
parameter Stop = 2'd3 ; //Stop state
reg [1:0] current_state ; //statement
reg [1:0] next_state ;
reg [4:0] delay_cnt ; //delay counter
//First part: statement transition
always @(posedge clk or negedge rst)
begin
if (!rst)
current_state <= Idle ;
else
current_state <= next_state ;
end
//Second part: combination logic, judge statement transition condition
always @(*)
begin
case(current_state)
Idle : begin
if (shift_start)
next_state <= Start ;
else
next_state <= Idle ;
end
Start : begin
if (delay_cnt == 5'd99)
next_state <= Run ;
else
next_state <= Start ;
end
Run : begin
if (shift_stop)
next_state <= Stop ;
else
next_state <= Run ;
end
Stop : next_state <= Idle ;
default: next_state <= Idle ;
endcase
end
//Last part: output data
always @(posedge clk or negedge rst)
begin
if (!rst)
delay_cnt <= 0 ;
else if (current_state == Start)
delay_cnt <= delay_cnt + 1'b1 ;
else
delay_cnt <= 0 ;
end
always @(posedge clk or negedge rst)
begin
if (!rst)
q <= 0 ;
else if (current_state == Run)
q <= {q[6:0], d} ;
else
q <= 0 ;
end
endmodule
在上面两个程序中用到了两种方式的写法,第一种的 Mealy 状态机,采用了一段式的写法, 只用了一个 always 语句,所有的状态转移,判断状态转移条件,数据输出都在一个 always 语句里, 缺点是如果状态太多,会使整段程序显的冗长。第二个 Moore 状态机,采用了三段式的写法,状态转移用了一个 always 语句,判断状态转移条件是组合逻辑,采用了一个 always 语句,数据输出 也是单独的 always 语句,这样写起来比较直观清晰,状态很多时也不会显得繁琐。
激励文件如下:
`timescale 1 ns/1 ns
module top_tb() ;
reg shift_start ;
reg shift_stop ;
reg rst ;
reg clk ;
reg d ;
wire [7:0] q ;
initial
begin
rst = 0 ;
clk = 0 ;
d = 0 ;
#200 rst = 1 ;
forever
begin
#({$random}%100)
d = ~d ;
end
end
initial
begin
shift_start = 0 ;
shift_stop = 0 ;
#300 shift_start = 1 ;
#1000 shift_start = 0 ;
shift_stop = 1 ;
#50 shift_stop = 0 ;
end
always #10 clk = ~clk ;
top t0
(
.shift_start(shift_start),
.shift_stop(shift_stop),
.rst(rst),
.clk(clk),
.d(d),
.q(q)
);
endmodule
仿真结果如下: