I need to use a case statement with the control signal being 4 bits. I have multiple cases of those 4 bits doing the same operation, how do I make the code more concise?
For ex:
casez (fbe) //fbe is defined as logic [3:0] fbe;
4'b0000: begin
// operation a
end
4'b???1 : begin
// operation b
end
Operation a and operation b are exactly identical. How do I collapse these 2 into a single case? Something like (fbe == 4'b0000) | (fbe == 4'b???1) into a single case?
You can use commas to separate all case expressions that will perform the operations. The default condition cannot be in this list (because it would be redundant).
Example:
casez(fbe)
4'b0000, 4'b???1 : begin /* do same stuff */ end
4'b??10 : begin /* do other stuff */ end
default : begin ... end
endcase
This is documented in IEEE Verilog and SystemVerilog LRMs with examples. Such as IEEE1364-1995 § 9.5 Case statement and IEEE1800-2012 § 12.5 Case statement.
In SystemVerilog, you should use the case (expression) inside statement. (§12.5.4 Set membership case statement). This lets you use the same kind of lists of expressions that the inside operator uses, like a range of values. It also has asymmetrical wildcard matching. This means only Z's in the case item become wildcards, not Z's in the case expression. For example
case (fbe) inside
4'b0000, 4'b0??1: begin end // 0, and 1,3,7
[9:11]: begin end // 9,10,11
default: begin end //2,4,6,8,12-15
endcase
If fbe was 4'bz000 for some reason, the default would be taken. casez would have matched 4'b0000.
Related
I have an inferred latch problem after synthesis when I designed a simple dual port RAM block. Due to large code size, I have just embedded this always block code as follows:
integer i;
always_latch
begin
for (i=0;i<NUM_RAMS;i=i+1) begin
if (ena_t == 1) begin
w_addra_t[i] = w_addra[i];
end
else begin
w_addra_t[bank_addra[i]] = w_addra[i];
end
end
end
My RAM block includes NUM_RAMS numbers of banks. The addresses of respective input data are stored in w_addra.
Data with given w_addra addresses are scrambled into w_addra_t depend on the values of respective bank_addra (depend on access pattern) when ena_t = 0.
I tried to replace for loop with if...else, switch...case, generate but the problem is same. With different always block in my code that the left-side is with only w_addra_t[i] in both if.else of ena_t, there is no error.
I would like to get your suggestion if you have any idea. I did look for similar issue but getting no results.
Thanks very much :)
My guess is the entries for bank_addra are not guaranteed to be unique. If two or more entries hold the same values then an index hole is created for w_addra_t; which will infer a latch.
Here are three possible solution:
Functionally guaranteed that bank_addra entries will have unique values, then the synthesizer should not infer a latch. This can be challenging.
Move the address variation from the LHS to the RHS so that each index of w_addra_t is guaranteed to be assigned a value. Ex change w_addra_t[bank_addra[i]] = w_addra[i]; to w_addra_t[i] = w_addra[bank_addra_lookup[i]];.
Assign all entries of w_addra_t to a known value (constant, flip-flop, or deterministic value) before other logic. You can put this default assignment at the top of your always block (option 1 example) or above the logic where the latches were about to be inferred (option 2 example). This is the simplest solution to implement assuming it still satisfies relational requirements with your other code.
// NOTE: SystemVerilog supports full array assignments (Verilog requires a for-loop)
always_comb
begin
w_addra_t = '{default:'0}; // <-- default assignment : option 1
if (ena_t == 1) begin
w_addra_t = w_addra;
end
else begin
w_addra_t = w_addra_t_ff; // <-- default assignment : option 2
for (i=0;i<NUM_RAMS;i=i+1) begin
w_addra_t[bank_addra[i]] = w_addra[i];
end
end
end
always_ff #(posedge clk) begin
w_addra_t_ff <= w_addra_t; // assuming w_addra_t should hold it current values
end
TL;DR
always_latch is a SystemVerilog keyword to identify explicit latches. Some tools will auto-waive the warning when the keyword is used, but will throw an error/warning if the keyword is used and a latch is not detected.
If you know it should be combinational logic, then use the always_comb SystemVerilog keyword. With always_comb, if the synthesis detects a latch then it should report an error.
Read related question:
What is inferred latch and how it is created when it is missing else statement in if condition. Can anybody explain briefly?
I don't know if it will solve your problem by changing to
int i
always_comb
instead. Perhaps the tool gets sad when you use a 4-state variable like integer?
I have logic to compare a variable with multiple values.
For example:
logic [3:0] a;
always_comb begin
flag = (a == 'd13) || (a == 'd2) || (a=='d1); //can this be simplified?
end
Is there a easy way to write this statement?
This is more concise using the inside operator:
always_comb begin
flag = (a inside {1, 2, 13});
end
This is more scalable as well, allowing you to easily add or remove values from the set.
The syntax also supports ranges of values:
flag = (a inside {[1:2], 13});
Refer to IEEE Std 1800-2017, section 11.4.13 Set membership operator.
Since the values in the set are all constants, it should be synthesizable (but YMMV).
1) When this procedural block will be executed?
output a;
reg a;
always#(a)
begin
// Do something...
end
In C this would be executed when "a" has a value non-zero.
2) When this if statement would be true?
if(!a)
begin
// Do something...
end
I come from C and I'm actually confused about Verilog.
verilog is 4-state simulator, meaning that any bit in a variable could have one of the 4 values: 0, 1, x and z. 'x' expresse an unknown value and all variables in verilog are initialized to this value at the beginning.
now if a is initizlized to x, the following statements will always fail:
if (a) -> because x cannot be true (in verilog)
if (!a) -> false, because !x is also unknown and is x;
You should check verilog 4-state simulation and arithmetic rules.
Always block, on the other hand, will wait till value of 'a' changes, for exemple, from 'x' to '0'.
always #(a) ...
So, untill you assign a value of '1', the 'if' statement will fail and always block will not execute till you change the value of 'a' to something different from 'x'. Initilzation of those variables is usually done in test bench code, say in an initial block. For example
initial #10 a = 1; // initialize 'a' to '1' after 10 time units.
Since you coming from 'c', there is a big difference in the language concept. Verilog is a parallel programming language, it means that all procedural blocks are executed preemptively in parallel. inital blocks and always blocks are procedural blocks. Statements in side a block are executed sequentially, but two different blocks are executed in parallel. Events and delays cause preemption.
Is these two codes behave in the same way ?
always #(a == 1'b1) // I guess for this one, it's like a combinational if (AND gate logic : a AND 1).
// code
and
always #(a) begin // I guess for this one, if a= 1 from the time 0,
// it won't go inside since there wouldn't be any change.
if (a == 1) begin
// code
end
end
Is it true ?
It's a basic question but I'd like to know if I miss something, thank you!
Nope, both are not same.
Note that variables in the sensitivity list are considered as an event
and hence they are not executed, rather just change in the variable is
considered as a trigger to that event.
So the 1st always block
always # (a==1'b1)
is same as :
always # (a)
So 1st always block will execute with both a = 1 & 0, whereas in 2nd case, because of "if" condition, it will be executed only with a = 1
Say I have a scenario in which I need to compare only a few bits of a register and I don't care about other bits. eq, I need to check the first and last bits of a 3 bit register (A[2:0]) and I don't care about the middle bit, say compare vector is 3'b1X0 (Parameter).
Simplest way to do this is choose all the bits I care about, AND them and I have generated a control signal:
if ((A[2]==1) & ((A[0]==0)) Here the condition inside if statement is my control signal.
Another way is to use a casex statement: casex(A) begin 3'b1?0: ... , ... endcase.
Is there anything like ifx-elsex statement or something that can be used to do this kind of operation without using the 1st and 2nd method?
Thanks!
if (A[2:0] inside {3'b1?0} )
SystemVerilog keyword inside. It has been supported since at least Accellera's SystemVerilog 3.1 (before SystemVerilog was a part of IEEE). IEEE Std 1800-2012 11.4.13 has examples of use. inside is synthesizable.
There is also if ( A[2:0] ==? 3'b1?0 ) (IEEE Std 1800-2012 11.4.6). The only reference I have on hand (a book published 2004) says it is not supported for synthesis yet. You are welcome to try it.
(A[2]==1) is a logical expression the & is a bitwise operator, although either works it would be better semantics to use the && logical and operator. This is slightly different to most other languages where the && is a short-circuit operator.
Logically what you want is if ((A[2]==1) && ((A[0]==0)) but it could be reduced to a bitwise expression :
if ( ~A[0] & A[2] )
NB: Try to avoid using casex, the unknown parts will match x's in simulation. Try to use casez instead, ? can still be used to match don't cares.
Update comparing inside to casez
Case statements a clean control structure used in most languages to avoid large if elsif else chains. the inside operation will match x's to the do not care '?' values. this makes it usage similar to the casex which is considered to be bad practise to use as it can hide simulation fails.
casez(sel)
4'b1??? a= 3'd4;
4'b01?? a= 3'd3;
4'b001? a= 3'd2;
4'b0001 a= 3'd1;
4'b0000 a= 3'd0;
endcase
vs
if (sel inside {4'b1???})
a= 3'd4;
else if (sel inside {4'b01??})
a= 3'd3;
else if (sel inside {4'b001?})
a= 3'd2;
...
The above is actually equal to the casex (but more verbose) I believe that instead of casex you could also use :
case(sel) inside
4'b1??? a= 3'd4;
4'b01?? a= 3'd3;
4'b001? a= 3'd2;
4'b0001 a= 3'd1;
4'b0000 a= 3'd0;
endcase
but then I would never use a casex.
There's no operator I'm aware of that allows you to use '?' or 'x' inside an equality comparison to have them ignored.
Another alternative that you didn't mention would be to use a bitmask to select the bits you only care about. If you have a lot of bits this can be more compact than testing each bit individually.
If you only care about A == 3'b1?0, then it can be written as such:
if((A & 3'b101) == 3'b100)