<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
上回提到了if…else和case在某些情况下实现的结构完全一致,但是这次特权同学同样举一个例子。只不过这一次if…else和case所实现的结构却不一样。
//if…else实例
input clk;
input rst_n;
input close,wr,rd;
output[2:0] db;
reg[2:0] dbr;
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
dbr <= 3'd0;
end
else begin
if(close) dbr <= 3'b111;
else if(rd) dbr <= 3'b101;
else if(wr) dbr <= 3'b011;
else dbr <= 3'd0;
end
end
assign db = dbr;
//case实例
input clk;
input rst_n;
input close,wr,rd;
output[2:0] db;
reg[2:0] dbr;
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
dbr <= 3'd0;
end
else begin
case({close,rd,wr})
3'b100: dbr <= 3'b111;
3'b010: dbr <= 3'b101;
3'b001: dbr <= 3'b011;
default: dbr <= 3'd0;
endcase
end
end
assign db = dbr;
对于上面两段代码,单从代码上分析,if…else是带优先级的,case是平行结构。下面看看结果是否这样?
首先看资源消耗的情况。
//if…else
Resource Usage
Total logic elements 3
-- Combinational with no register 0
-- Register only 0
-- Combinational with a register 3
Logic element usage by number of LUT inputs
-- 4 input functions 0
-- 3 input functions 2
-- 2 input functions 1
-- 1 input functions 0
-- 0 input functions 0
Logic elements by mode
-- normal mode 3
-- arithmetic mode 0
-- qfbk mode 0
-- register cascade mode 0
-- synchronous clear/load mode 0
-- asynchronous clear/load mode 3
Total registers 3
I/O pins 8
Maximum fan-out node close
Maximum fan-out 3
Total fan-out 17
Average fan-out 1.55
Resource Usage
Total logic elements 3
-- Combinational with no register 0
-- Register only 0
-- Combinational with a register 3
Logic element usage by number of LUT inputs
-- 4 input functions 0
-- 3 input functions 3
-- 2 input functions 0
-- 1 input functions 0
-- 0 input functions 0
Logic elements by mode
-- normal mode 3
-- arithmetic mode 0
-- qfbk mode 0
-- register cascade mode 0
-- synchronous clear/load mode 0
-- asynchronous clear/load mode 3
Total registers 3
I/O pins 8
Maximum fan-out node wr
Maximum fan-out 3
Total fan-out 18
Average fan-out 1.64
二者的资源消耗是存在差异的,那么二者的最终实现也一定是不一样的。下面再看看它们综合后的RTL视图。
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />
图1 if…else综合后RTL视图
图2 case综合后RTL视图
从RTL视图看,二者实现的确确实实也是如我们早先所预期的,一个带优先级,一个并行处理。再看看最后布局布线后的结构吧。
图3 if…else布局布线后Technology Map Viewer
图4 case布局布线后Technology Map Viewer
从最终的结构看,和RTL视图很接近。这两个实例代码所使用的if…else和case最终实现的结构是有差异的。从之前的实例分析看这并不稀奇,意外的是使用if…else实现的结构资源消耗居然比case要来得少(只是相对而言)。这样的结果似乎能够很好的反驳不少人提出的所谓“多用case语句,少用if…else语句,因为实现带优先级的结构比并行结构更耗费资源”的论断。特权同学提出这一点,并不是认为实现带优先级的结构更节省资源,而是想强调一点,任何代码的实现都不是绝对的,所谓好的代码风格也不是一层不变的,是需要设计者在实践中更多的具体问题具体分析。
另外,补充一点,该例子中使用多个if…if…语句实现的结果会和case语句的结果一致。实例代码如下:
//if…if…实例
input clk;
input rst_n;
input close,wr,rd;
output[2:0] db;
reg[2:0] dbr;
always @ (posedge clk or negedge rst_n) begin
if(!rst_n) begin
dbr <= 3'd0;
end
else begin
dbr <= 3'd0;
if({close,rd,wr} == 3'b100) dbr <= 3'b111;
if({close,rd,wr} == 3'b010) dbr <= 3'b101;
if({close,rd,wr} == 3'b001) dbr <= 3'b011;
end
end
assign db = dbr;
ilove314_323192455 2013-3-11 14:56
guojianjun5_823636633 2013-3-11 11:07
用户430586 2013-3-7 16:02
用户593939 2013-3-6 22:38
用户1395232 2009-8-1 17:58
ash_riple_768180695 2009-7-24 13:14