新闻  |   论坛  |   博客  |   在线研讨会
case与if…else优化
0750long | 2009-07-22 10:50:16    阅读:1798   发布文章

case与if…else优化

    两段代码, EX1使用if…else语句,EX2使用case语句,它们综合的结果有多大差异呢?最终布局布线后的结构又有多大差异呢?

EX1:

input clk;

input rst_n;

input[3:0] data;

output[2:0] add;

 

reg[2:0] add;

 

always @ (posedge clk) begin

    if(!rst_n) begin

        add <= 0;

        end

    else begin

        if(data<4) add <= 1;

        else if(data<8) add <= 2;

        else if(data<12) add <= 3;

        else add <= 4;

        end

end

 

EX2:

input clk;

input rst_n;

input[3:0] data;

output[2:0] add;

 

reg[2:0] add;

 

always @ (posedge clk) begin

    if(!rst_n) begin

        add <= 0;

        end

    else begin

        case(data)

        0,1,2,3:    add <= 1;

        4,5,6,7:    add <= 2;

        8,9,10,11:  add <= 3;

        12,13,14,15: add <= 4;

        default: ;

        endcase

        end

end

 

    先看看综合后的RTL视图。

点击看大图

If…else语句综合的RTL视图

点击看大图

case语句综合的RTL视图

    单从RTL视图来看,二者综合后的结果是有明显区别的。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 0

   

Total registers 3

I/O pins    9

Maximum fan-out node    rst_n

Maximum fan-out 3

Total fan-out   14

Average fan-out 1.17

 

case结构的资源占用:

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 0

   

Total registers 3

I/O pins    9

Maximum fan-out node    data[2]

Maximum fan-out 3

Total fan-out   14

Average fan-out 1.17

 

    二者资源占用的情况基本是完全一样,连平均扇出也一致。

    再看他们的Technology Map Viewer:

点击看大图

If…else的Technology Map Viewer

点击看大图

Case的Technology Map Viewer

    二者完全的一致,所以,可以明确的说,在这个例子中,If…else和case语句最终的实现都是并行的,而且完全一致。

    记得特权过去也曾认为If…else和case综合实现的结果是不一样的。也曾就这个实例写过博文,分析的头头是道。但是现在的结果似乎推翻了这样一种思想,过去使用的是Quartus II 7.1i做这个测试,现在使用了9.1版本,8.1的也测试了,也许If…else和case语句的优化随着软件的升级,已经不再简单的交给用户的代码来决定,而是默认优化了。就像状态机中讨论独热码好还是格雷码好一样,其实这个优化已经成为了软件选项了。

    而综合的RTL视图到Technology Map Viewer其实也还是有差距的,它们之间的优化就是映射所要干的活。

    总之,If…else和case语句实现的结构到底是怎样还是要看工具,具体问题具体分析。不能片面的强调If…else和case语句谁好谁坏。

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客