Combo Logic Case

download Combo Logic Case

of 17

Transcript of Combo Logic Case

  • 8/13/2019 Combo Logic Case

    1/17

    case Statement

    Nesting if past two levels is error prone and possibly inefficient.

    case excels when many tests are performed on the same expression.

    case works well for muxes, decoders, and next state logic.

    CASE Statement Structure

    case (case_expression)

    case_item1 : case_item_statement1;

    case_item2 : case_item_statement2;

    case_item3 : case_item_statement3;

    case_item4 : case_item_statement4;default : case_item_statement5;

    endcase

    case is shorthand to describe a complicated if/then/else structure.

    http://find/http://goback/
  • 8/13/2019 Combo Logic Case

    2/17

    case Statement

    //"full case" case statement

    module case1 (

    input [7:0] a_in, b_in, c_in, d_in,

    input [1:0] sel,

    output reg [7:0] d_out);

    always_comb

    case (sel)

    2b00 : d_out = a_in;

    2b01 : d_out = b_in;

    2b10 : d_out = c_in;

    2b11 : d_out = d_in;endcase

    endmodule

    U34

    U32

    U30

    U28

    U26

    U24

    U22

    U36

    U29

    U27

    U25

    U23

    U39U41

    U38

    U40

    U42

    U37

    U35

    U33

    U31

    http://find/
  • 8/13/2019 Combo Logic Case

    3/17

    case Statement

    Is the simulation correct?

    //"full case" case statementmodule case1 (

    input [7:0] a_in, b_in, c_in, d_in,

    input [1:0] sel,

    output reg [7:0] d_out);

    always_comb

    case (sel)2b00 : d_out = a_in;

    2b01 : d_out = b_in;

    2b10 : d_out = c_in;

    2b11 : d_out = d_in;

    endcase

    endmodule

    10

    20

    30

    40

    0 1 2 3 X

    10 20 30 40

    /case1/a_in

    10

    /case1/b_in 20

    /case1/c_in 30

    /case1/d_in 40

    /case1/sel 0 1 2 3 X

    /case1/d_out 10 20 30 40

    http://find/
  • 8/13/2019 Combo Logic Case

    4/17

    case Statement

    Use default case to propagate x

    //"full case" case statementmodule case1 (

    input [7:0] a_in, b_in, c_in, d_in,input [1:0] sel,output reg [7:0] d_out);

    always_combcase (sel)

    2b00 : d_out = a_in;

    2b01 : d_out = b_in;2b10 : d_out = c_in;2b11 : d_out = d_in;default : d_out = 8bx;

    endcaseendmodule

    10

    20

    30

    40

    0 1 2 3 X

    10 20 30 40 xx

    /case1/a_in 10

    /case1/b_in 20

    /case1/c_in 30

    /case1/d_in 40

    /case1/sel 0 1 2 3 X

    /case1/d_out 10 20 30 40 xx

    http://find/
  • 8/13/2019 Combo Logic Case

    5/17

    case Statement

    //incomplete case statementmodule case2 (

    input [7:0] a_in, b_in, c_in,

    input [1:0] sel,

    output reg [7:0] d_out);

    always_comb

    case (sel)2b00 : d_out = a_in;

    2b01 : d_out = b_in;

    2b10 : d_out = c_in;

    endcase

    endmodule

    U19

    U20

    U21

    U17

    U22

    U15

    U18

    U23

    ...g[5]

    ...g[0]

    U25

    ...g[4]

    U27

    ...g[3]

    U16

    U24

    ...g[7]

    ...g[2]

    U26

    ...g[6]

    ...g[1]

    Inferred memory devices in process in routine case2 line 6 in file case2.sv.===========================================================================| Register Name | Type | Width | Bus | MB | AR | AS | SR | SS | ST |===========================================================================| d_out_reg | Latch | 8 | Y | N | N | N | - | - | - |===========================================================================Warning: Netlist for always_comb block contains a latch. (ELAB-974)

    Presto compilation completed successfully.

    http://find/http://goback/
  • 8/13/2019 Combo Logic Case

    6/17

    case Statement

    //incomplete case statement

    //with default case_item

    module case2 (

    input [7:0] a_in, b_in, c_in,

    input [1:0] sel,

    output reg [7:0] d_out);

    always_comb

    case (sel)

    2b00 : d_out = a_in;

    2b01 : d_out = b_in;

    2b10 : d_out = c_in;

    default : d_out = 8bx;

    endcase

    endmodule

    U15

    U11

    U16

    U12

    U13

    U14

    U17

    U10

    U18

    a_in[2]

    c_in[6]

    a_in[1]

    a_in[0]

    c_in[7]

    n3

    b_in[3]

    b_in[4]

    b_in[2]

    b_in[5]

    b_in[1]

    b_in[6]

    sel[0]

    b_in[0]

    b_in[7]

    sel[1]

    a_in[7]

    c_in[0]

    c_in[1]

    a_in[6]

    c_in[2]

    a_in[5]

    c_in[3]

    a_in[4]

    c_in[4]

    a_in[3]

    c_in[5]

    c_in[7:0]

    d_out[7:0]

    b_in[7:0]

    sel[1:0]

    a_in[7:0]

    c_in[7:0]

    sel[1:0]

    a_in[7:0]

    b_in[7:0] d_out[7:0]

    Does RTL and gate simulation differ when sel = 2b11?

    http://find/
  • 8/13/2019 Combo Logic Case

    7/17

    case Statement

    System Verilog priority Modifier

    SystemVerilog introduced two case and if statement modifiers priority unique

    Both give information to synthesis to aid optimization.

    Both are assertions (simulation error reporting mechanisms)

    Both imply that there is a case item for all the possible legal valuesthat case expression might assume.

    Priority

    Priority tells the simulator that if all is well, you will find a match If a match is not found at run-time, emit an error or warning. Since all legal combinations are listed, synthesis is free to optimize any

    other unlisted case items since they are effectively dont-cares. Priority does not guarantee removal of unintended latches.

    http://find/
  • 8/13/2019 Combo Logic Case

    8/17

    case Statement

    //incomplete case statement

    //with systemverilog priority modifier

    module case2 (

    input [7:0] a_in, b_in, c_in,

    input [1:0] sel,

    output reg [7:0] d_out);

    always_comb

    priority case (sel)

    2b00 : d_out = a_in;

    2b01 : d_out = b_in;

    2b10 : d_out = c_in;

    endcase

    endmodule

    U17

    U11

    U12

    U14

    U13

    U15

    U16

    U10

    U18

    c_in[2]

    b_in[1]

    c_in[3]

    b_in[0]

    c_in[4]

    c_in[5]

    a_in[7]

    c_in[6]

    a_in[6]

    c_in[7]

    a_in[5]

    a_in[4]

    a_in[3]

    a_in[2]

    a_in[1]

    a_in[0]

    b_in[4]

    b_in[5]

    b_in[6]

    sel[0]

    b_in[7]

    n3

    sel[1]

    c_in[0]

    b_in[3]

    c_in[1]

    b_in[2]

    a_in[7:0]

    c_in[7:0]

    d_out[7:0]

    b_in[7:0]

    sel[

    1:0]

    b_in[7:0]

    c_in[7:0]

    sel[1:0]

    a_in[7:0]

    d_out[7:0]

    Warning: case2.sv:7: Case statement marked priority does not cover all possible conditions. (VER-504)

    http://find/
  • 8/13/2019 Combo Logic Case

    9/17

    case Statement

    System Verilog priority Modifier

    OK, so what happens in simulation with 2b11 ?

    No x propagation in RTL simulation, but warning is emitted Go back, fix the error that caused the unallowed case to occur.

    After fixing, the RTL and gate simulation should match.

    http://find/
  • 8/13/2019 Combo Logic Case

    10/17

    case Statement

    //incomplete case statement//with systemverilog priority modifier

    module case2 (input [7:0] a_in, b_in, c_in,

    input [1:0] sel,output reg [7:0] d_out);

    always_combpriority case (sel)

    2b00 : d_out = a_in;2b01 : d_out = b_in;2b10 : d_out = c_in;

    endcase

    endmodule

    ** Warning: (vsim-8315) case2.sv(7): No condition is true in the unique/priority if/case statement.

    ** Warning: (vsim-8315) case2.sv(7): No condition is true in the unique/priority if/case statement.

    http://find/
  • 8/13/2019 Combo Logic Case

    11/17

    case Statement

    System Verilog priority Modifier Bottom line...

    If case is full, use default expression. If case is not full, use priority modifier.

    http://find/
  • 8/13/2019 Combo Logic Case

    12/17

    casez Statement

    casez is a special version of the case expression

    Allows dont care bits in the comparison

    Uses ? or Z values as dont care instead of as a logic value (weird!)

    Recommendation: use ? as dont care

    Use caution when using this statement

    casez Example

    module interrupt_ctl1 (

    output reg int2, int1, int0,

    input [2:0] irq );

    always_comb begin

    {int2, int1, int0} = 3b0; //defaultcasez (irq)

    3b1?? : int2 = 1b1;

    3b?1? : int1 = 1b1;

    3b??1 : int0 = 1b1;

    endcase

    end

    endmodule

    http://find/
  • 8/13/2019 Combo Logic Case

    13/17

    casez Statement

    casez Example

    module interrupt_ctl1 (

    output reg int2, int1, int0,

    input [2:0] irq );

    always_comb begin

    {int2, int1, int0} = 3b0; //default

    casez (irq)3b1?? : int2 = 1b1;

    3b?1? : int1 = 1b1;

    3b??1 : int0 = 1b1;

    endcase

    end

    U7

    U8

    U6

    U5

    n4

    irq[2]

    irq[1]

    irq[0]

    int1

    int0

    n3

    irq[2:0]

    irq[2:0]

    int0

    int2

    int1

    http://find/
  • 8/13/2019 Combo Logic Case

    14/17

    casez Statement

    Parallel or Unique encoding

    If the irq bus has any of the values: 3b011, 3b101, 3b110 or3b111, more than one case item could match the irq value.

    This is known as a non-parallel case statement.

    Synthesis will produce a priority encoder structure.

    If code could be rewritten to have parallel or unique case items, nopriority structure would be needed. Thus, faster/smallerimplementation.

    We indicate this situation with the modifier unique.

    http://find/
  • 8/13/2019 Combo Logic Case

    15/17

    casez Statement

    Parallel or Unique Encoding

    module interrupt_ctl2a (output reg int2, int1, int0,

    input [2:0] irq );

    always_comb begin

    {int2, int1, int0} = 3b0; //default

    unique casez (irq)

    3b1?? : int2 = 1b1;

    3b01? : int1 = 1b1;3b001 : int0 = 1b1;

    endcase

    end

    U6

    U7

    U8 int0

    n2

    int1

    int2

    irq[1]

    int2,irq[1]

    irq[2:1]

    irq[0]

    int1

    int0

    int2

    SYNTHESIS RESULTS ARE INCORRECT!

    S

    http://find/
  • 8/13/2019 Combo Logic Case

    16/17

    casez Statement

    Now what?

    Further work has shown unique to be of at most little benefit.

    I only got it to work properly once. Synthesis does not stick strictly to priority encoding, its smarter.

    Using unique, I get about 10% decrease in delay and area at best.

    At this point, until I can bet a better handle on it, avoid unique.

    S

    http://find/
  • 8/13/2019 Combo Logic Case

    17/17

    casex Statement

    One more case expression exists: casex.

    Can be useful for testbenches. Current recommendations are to not use it in synthesizible code.

    http://find/