目錄
前言
行為級描述
Verilog HDL設計代碼為:
ISE綜合
RTL Schematic
Technology Schematic
狀态機描述狀态轉移圖
Verilog HDL代碼
測試檔案
仿真波形
修正為同步複位
Verilog HDL語言描述
仿真波形圖
結構性描述
簡單的比較
前言
采用Verilog HDL對時序邏輯電路進行描述的方法有:狀态轉移圖描述、結構性描述以及抽象性的行為描述。
下面以5進制同步加法計數器的Verilog HDL描述為例,對比不同的描述方式綜合出來的RTL Schematic,看看有什麼不同。
行為級描述
首先采用電路的行為級描述方式。
module counter5(clk, rst, cnt, co);
input clk;
input rst;
output co; //輸出為進位
reg co;
output [2:0] cnt;
reg[2:0] cnt;
always@(posedge clk)
begin
if(rst)
begin
cnt <= 3'b000;
co <= 1'b0;
end
else if(cnt == 3'b100)
begin
cnt <= 3'b000;
co <= 1'b1;
end
else
begin
cnt <= cnt + 1'b1;
co <= 1'b0;
end
end
endmodule
展開後
`timescale 1ns / 1ps
//
// Company:
// Engineer:
//
// Create Date: 22:12:23 08/03/2018
// Design Name:
// Module Name: counter5
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//
module counter5(clk,rst, cnt, co);
input clk;
input rst;
output[2:0] cnt;
output co;
reg co;
reg[2:0] pre_state, next_state;
parameter s0 = 3'b000, s1 = 3'b001, s2 = 3'b010, s3 = 3'b011, s4 = 3'b100;
always@(posedge clk or posedge rst)
begin
if(rst)
begin
pre_state <= s0;
// co <= 1'b0;
end
else
begin
pre_state <= next_state;
end
end
always@(pre_state)
begin
case(pre_state)
s0:
begin
next_state = s1;
co = 1'b0;
end
s1:
begin
next_state = s2;
co = 1'b0;
end
s2:
begin
next_state = s3;
co = 1'b0;
end
s3:
begin
next_state = s4;
co = 1'b0;
end
s4:
begin
next_state = s0;
co = 1'b1;
end
default:
begin
next_state = s0;
co = 1'b0;
end
endcase
end
assign cnt = pre_state;
endmodule
`timescale 1ns/1ps
module counter5_tb;
reg clk,rst;
wire[2:0] cnt;
wire co;
always
begin
#10 clk = ~clk;
end
initial
begin
clk = 1'b0;
rst = 1'b1;
#20 rst = 1'b0;
end
counter5 u1(.clk(clk), .rst(rst), .cnt(cnt), .co(co));
endmodule
`timescale 1ns / 1ps
//
// Company:
// Engineer:
//
// Create Date: 22:12:23 08/03/2018
// Design Name:
// Module Name: counter5
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//
module counter5(clk,rst, cnt, co);
input clk;
input rst;
output[2:0] cnt;
output co;
reg co;
reg[2:0] pre_state, next_state;
parameter s0 = 3'b000, s1 = 3'b001, s2 = 3'b010, s3 = 3'b011, s4 = 3'b100;
always@(posedge clk )
begin
if(rst)
begin
pre_state <= s0;
// co <= 1'b0;
end
else
begin
pre_state <= next_state;
end
end
always@(pre_state)
begin
case(pre_state)
s0:
begin
next_state = s1;
co = 1'b0;
end
s1:
begin
next_state = s2;
co = 1'b0;
end
s2:
begin
next_state = s3;
co = 1'b0;
end
s3:
begin
next_state = s4;
co = 1'b0;
end
s4:
begin
next_state = s0;
co = 1'b1;
end
default:
begin
next_state = s0;
co = 1'b0;
end
endcase
end
assign cnt = pre_state;
endmodule
測試代碼不變
省略
注:用有限狀态機這種描述方法中,出現了一系列的錯誤,這也是一個發現錯誤并解決的過程,解決問題的代價是很大的(時間的花費,心理的奔潰),但解決問題後的成就感是微妙的,問題見博文:Modelsim下進行功能仿真沒問題,可是在ISE綜合報錯,如何解決?
結構性描述
其實還有一種描述方式,是結構性描述,但這種描述方式實在過于繁瑣,例如本例如果采用結構性描述,就不得不畫卡諾圖,化簡卡諾圖,等等,真的很讓人X疼,這不如讓我用原理圖來設計電路算了。
這裡就不采用這種方式了,但這也不意味着結構性的描述方式一無是處,在有的場合還是需要的;
這三種描述方式有的時候可以混合使用,例如模60計數器的實作就可以采用模10計數器和模6計數器的級聯來描述,見部落格:
級聯模60計數器(Verilog HDL語言描述)(仿真與綜合)
這裡的級聯方式不就是一種結構性的描述嗎?
而小的子產品的描述則采用的是比價抽象的行為級描述方式。
簡單的比較
從RTL Schematic中可以看出,行為級描述方式和有限狀态機描述方式綜合出來的電路還是有很大不同的:
行為級描述的RTL Schematic
有限狀态機描述
直覺上看的确如此,可是是不是感覺到很迷惑,這些RTL級元器件到底是什麼?有什麼功能?似乎看不懂綜合出來的電路是什麼意思,為了探讨這個問題,下篇部落格再議。