Lecture07 Full and Parallel
Lecture07 Full and Parallel
Lecture07 Full and Parallel
Macro Statistics
# Registers : 2
1-bit register : 2
Macro Statistics
# Registers : 2
Flip-Flops : 2
=========================================================================
* Low Level Synthesis *
=========================================================================
(Cummings 1999)
Notes on Case Statements
Order of case_items sets priority (first match wins), therefore
the order encodes meaning/logic
●
Be very attentive to the order of the cases in a case statement.
Example: Assume you are standing outside your door
considering an afternoon in the park or a return inside
Decision Algorithm: If see lightning go inside
Else if see rain walk to park with umbrella
Else run to park
Taking in isolation the latter part of the second line, we could
conclude an intent to
“walk to the park if we see rain”
The algorithm does not intend this. It does tells us to
“walk to the park if we see rain AND we do not see lighting”
●
Conditions presented as case items in case statements must be
interpreted in the same manor, with attention to the order and
precedence
Critical Design Considerations for always blocks
●
Consider every output individually for every traversal path of a
the decision tree.
Stated otherwise: Consider every output for every input-
and-state combination.
●
Note if the signal is being used externally to the block it is
assigned. This can determine how the signal is interpreted.
●
Our coding rule is that if we are coding an clocked always
block, then any signal assigned using a blocking assign should
not be used outside the block so that the registered version of
the signal is trimmed and only the combinatorial version
remains. We enforce this by using named blocks and local
variables for the output of combinatorial circuits
●
What happens when there are conditions within states in which
outputs or variables are not assigned?...
Complete Assignment for
Combinatorial Functions
The definition of a combinatorial function must have an assignment to
every variable for all possible cases. Does your code cover all the cases?
This must be assessed for each and every output variable.
z = 1; Draw a decision tree, label conditions for
if (a > b) begin branching carefully including branches for
x = 1; default decisions.
Decision Tree entry
z = 4; Mark where
end else begin variables are z
x=0; assigned. If a
if (a < (b-100)) begin Inferred
tree can be by priority
x = 2; y=1; traversed to a x,z x
else if (a>=(b-3)) begin leaf without
case(a-b) assigning
1: begin x = 3; y=1; end every output x,y
2: begin x = 3; y=2; end variable then
3: begin x = 3; y=3; end it is not
endcase complete.
end x,y x,y x,y
end Tip: flattening your decision hierarchy can avoid mistakes
Unintentional and Intentional Latches
●
In a combinatorial block any input condition in which any
output is not assigned may result in an inferred latch.
Latches Combinatorial
always @(x,en) always @(x,en)
if (en==1) y = x; if (en==1) y = x;
else y = 0;
always @(a) always @(a)
if (a>1) y=a; if (a>1) y=a;
else y=0;
●
Avoid the use of latches without consulting the user good for
recommended practices, coding style, and precautions
●
Assess any report of an inferred latch and investigate if it was
truly intentional
Register Enables
●
Inside a sequential-trigger block, not specifying an output may
generate an enable on that output register unless the finite
state machine extractor and synthesizer figures out how to
avoid it by repeating logic for you in certain states.
Functionally, not specifying an output in a condition in a state
means the previous value should be held.
Examples which potentially
generate a register w/ Enable No Enable
always @(posedge clk)
if (en==1) always @(posedge clk)
y<=x; if (a<10)
y<=a;
always @(posedge clk) else if (a>20)
if (a<10) y<=20;
y<=a; else
else if (a>20) y<=10;
y<=20;
Don't Care using 'x'
Potentially Generates
●
What if output not No Enable
a Register with Enable
provided for every
always always
input condition? @(posedge clk) @(posedge clk)
●
If Edge Triggered, an if (a<10) if (a<10)
enable may be y<=a; y<=a;
added to registers else if (a>20) else if (a>20)
●
If Combinatorially y<=20; y<=20;
triggered, Latches else
created y<=8'bx;
●
Assign outputs to be
'x' to avoid extra Latches Combinatorial Blocks
enables or latches always @(x,en) always @(x,en)
respectively, this if (en==1) y = x; if (en==1) y = x;
encodes a don't care else y = 8'bx;
condition for an always @(a) always @(a)
output. if (a>1) y=a; if (a>1) y=a;
else y=8'bx;
Registered Logic
●
Implementing combinatorial logic signals in a edge triggered block
may create “registered logic” if the signal is used outside the block (if
not used outside the block, Xilinx will report “trimming” of the register)
•Combining combinatorial logic in an edge-triggered block limits you to
only implement registered logic outputs. You can't have a combinatorial
output from an edge-triggered block, though you can have internal
combinatorial logic
•In the example below, if c is used outside the block a register will be
created for it, though it's combinatorial version survives internally to
immediately propagated through the remaining calculations. If c is not
used elsewhere, the register is trimmed. Some do not recommend this
practice.
•We'll discuss this more later, but for now our
always @(posedge clk)
coding guidelines dictate that we not create
c=a+b;
registers using blocking statements. If it is
d=c+3; trimmed, we are still following this rule.
q<=d; So, signals assigned in sequentially triggered
block using a blocking statement should not be
used outside the block they are assigned
Simple case statement
wire lighting; //see lightning
wire rain; // feel rain
wire sun; // sun shining
reg go; // flag decision goto park
reg run; //flag decision run
reg umbrella; //flag decision to use umbrella
.
.
.
case ({lighting,rain,sun})
{3'b110} : go =0; run = 0; umbrella=0;
{3'b010} : go =1; run = 0; umbrella=1;
{3'b001} : go =1; run = 1; umbrella=0;
endcase
●
Many “physical” cases of {lighting,rain,sun} are not covered
above and thus a physical latch may be inferred for synthesis.
●
Even if all physical cases are covered, in simulation lighting,
rain and sun can take on values x and z
Meaning a latch is inferred for simulation
i.e. if lighting, rain, or sun is equal to x or z
do nothing (hold the previous value)
Adding Default Don't Care Case
Setting values to x is treated as Don't Care for synthesis
●
Maybe better to default to 0 when you miss a case?
●
Sometimes a coding style include a safe catchall is
recommended.
●
Note, if you do this, you'll miss out on having go=x in simulation
when the input is a neglected case or x or z.
●
An x result allows you to clearly identify neglected cases.
●
One option is to add a print statement to the case,
(which is of course not synthesized)
case ({lighting,rain,sun})
{3'b110} : go =1'b0; run = 1'b0; umbrella=1'b0;
{3'b010} : go =1'b1; run = 1'b0; umbrella=1'b1;
{3'b001} : go =1'b1; run = 1'b1; umbrella=1'b0;
default : go = 1'b0; run = 1'bx; umbrella = 1'bx;
endcase
casez (sel)
3'b1??: int2 = 1'b1;
3'b?1?: int1 = 1'b1;
3'b??1: int0 = 1'b1;
case_item
Casex shouldn't be used for synth. only sim.
Full Case and Parallel Case
Full case: Does your logic cover all the cases for each and
variable achieving complete assignment.
Avoids inferred latches.
Parallel Case: Are all the cases mutually exclusive?
Overlapping cases can mean complex logic is involved
to determine correct action.
●
This is a non-parallel case, ●
Based on order, this
order matters here implements a priority encoder
●
Priority is given to the first case and so on ●
Encoding logic in “the order”
●
First match blocks others is not best style
Non-parallel case
casez ({lighting,rain,sun})
3'b1?? : go =1'b0; run = 1'b0; bring_umbrella=1'b0;
3'b?1? : go =1'b1; run = 1'b0; bring_umbrella=1'b1;
3'b??1 : go =1'b1; run = 1'b1; bring_umbrella=1'b0;
default : go = 1'b0; run = 1'bx; bring_umbrella = 1'bx;
endcase
the zero in the leftmost position makes it clear that this case
does not apply when there is lighting
It also creates a case_item with matches that are mutually
exclusive to the first.
•When all case_items and any included default have
mutually exclusive match sets, we have a parallel cases
•If any input to the case_expression can match more than
one case_item or a case_item and default, the we do not
have a parallel cases
Parallel and Non-Parallel Examples
As an academic exercise, consider effect of moving first case.
Non-Parallel Case, order does matter
casez ({lighting,rain,sun})
3'b?1? : go =1'b1; run = 1'b0; bring_umbrella=1'b1;
3'b??1 : go =1'b1; run = 1'b1; bring_umbrella=1'b0;
3'b1?? : go =1'b0; run = 1'b0; bring_umbrella=1'b0;
3'b000 : go =1'b0; run = 1'bx; bring_umbrella=1'bx;
default: go =1'bx; run = 1'bx; bring_umbrella = 1'bx;
endcase Note: Walking in park with umbrella during a lighting
Storm can be unhealthy
Parallel Case, order doesn't matter
casez ({lighting,rain,sun})
3'b01? : go =1'b1; run = 1'b0; bring_umbrella=1'b1;
3'b001 : go =1'b1; run = 1'b1; bring_umbrella=1'b0;
3'b1?? : go =1'b0; run = 1'b0; bring_umbrella=1'b0;
3'b000 : go =1'b0; run = 1'bx; bring_umbrella=1'bx;
default: go =1'bx; run = 1'bx; bring_umbrella = 1'bx;
endcase
casex,casez
In literal numbers ? is an alternative for z
It is not a shorthand for 0,1,x,z as with UDPs
(user defined primitives)
casez (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
(Cummings 1999)
Implementing Don't care inputs
casex treats x and z as don't care
casez treats z as don't care
(Cummings 1999)
casez (sel)
3'b1??: int2 = 1'b1;
3'b?1?: int1 = 1'b1;
3'b??1: int0 = 1'b1;
case_item
casex shouldn't be used for synthesis,
only simulation if needed
Which Case?
sel = 2'bx1
case (sel)
2'b00 : case_item_statement1;
2'b10 : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casez (sel)
2'b00 : case_item_statement1;
2'b10 : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casex (sel)
2'b00 : case_item_statement1;
2'b10 : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
sel = 2'bz1
case (sel)
2'b00 : case_item_statement1;
2'b10 : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casez (sel)
2'b00 : case_item_statement1;
2'b10 : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casex (sel)
2'b00 : case_item_statement1;
2'b10 : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
sel = 2'b11
case (sel)
2'b00 : case_item_statement1;
2'b1x : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casez (sel)
2'b00 : case_item_statement1;
2'b1x : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casex (sel)
2'b00 : case_item_statement1;
2'b1x : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
sel = 2'b1x
case (sel)
2'b00 : case_item_statement1;
2'b1x : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casez (sel)
2'b00 : case_item_statement1;
2'b1x : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
Not useful for synth
casex (sel)
2'b00 : case_item_statement1;
2'b1x : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
sel = 2'b1z
case (sel)
2'b00 : case_item_statement1;
2'b1x : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casez (sel)
2'b00 : case_item_statement1;
2'b1x : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casex (sel)
2'b00 : case_item_statement1;
2'b1x : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
sel = 2'b1x
case (sel)
2'b00 : case_item_statement1;
2'b1z : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casez (sel)
2'b00 : case_item_statement1;
2'b1z : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
casex (sel)
2'b00 : case_item_statement1;
2'b1z : case_item_statement2;
2'b01 : case_item_statement3;
default : case_item_statement4;
endcase
Adding Default Don't Care Case
module mux3c (y, a, b, c, sel);
output y;
input [1:0] sel;
input a, b, c;
reg y;
always @(a , b , c , sel)
y=1'bx;
case (sel) •Two options
2'b00: y = a; •The second one
2'b01: y = b; is preferred
2'b10: y = c; for readability
default: y = 1'bx;
endcase
endmodule (Cummings 1999)
Allowed Use of x,z,? for Synthesis
casex/casez (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 (Cummings 1999)
•casex allows x in the
literal case_expression While this may be
and in case-item literal expression dependent on the
•Synthesis only considers x and z/? in synthesis tool,
case-item expression a reasonable practice is
•casez allows z in the to not use x or z/? in the
case _expression case_expression
and in the literal case-item expression for code intended for
•synthesis only considers z/? in synthesis
case-item expression
module my_parallel_case (sel, a, b, c);
input [2:0] sel;
output a, b, c;
reg a, b, c;
always @(sel) begin
{a, b, c} = 3'b0;
casez (sel)
3'b1??: a = 1'b1;
3'b?1?: b = 1'b1;
3'b??1: c = 1'b1;
endcase
end
endmodule
Same as
3'b1??: a = 1'b1;
3'b01?: b = 1'b1;
3'b001: c = 1'b1;
module my_parallel_case (sel, a, b, c);
input [2:0] sel;
output a, b, c;
reg a, b, c;
always @(sel) begin
{a, b, c} = 3'b0;
case (sel)
3'b1??: a = 1'b1;
3'b?1?: b = 1'b1;
3'b??1: c = 1'b1;
endcase
end
endmodule