If there’s no match, the default statement is executed.
When there is only one statement within a branch, we can remove the “begin” and “end” keywords.
The following example shows the use of a nested structure to describe a priority encoder.
Use the Verilog “if” statement to describe a 4-to-2 priority encoder with the truth table below: The following code is a Verilog description for this priority encoder: 1 module Prio_4_to_2( 2 input wire [3:0] x, 3 output reg [1:0] y, 4 output reg v 5 ); 6 always @* 7 if (x == 1'b1) 8 y = 2'b11; 9 else if (x == 1'b1) 10 y = 2'b10; 11 else if (x == 1'b1) 12 y = 2'b01; 13 else 14 y = 2'b00; 15 always @* 16 if (x | x | x | x) 17 v = 1'b1; 18 else 19 v = 1'b0; 20 endmodule Line 6 introduces a useful Verilog notation.
In a previous article describing combinational circuits in Verilog, we discussed that the Verilog conditional operator can be used to check a condition when making an assignment with the “assign” keyword.
Inside an “always” block, we can use the Verilog “if” statement to implement a similar functionality.Lines 15 to 19 use an “if” statement to describe the “v” output as given in the truth table.The condition checked within this “if” statement is defined using the Verilog bitwise OR operator.If it’s not true, the expression within the first “else” which is another “if” statement is executed. If it’s true, the output is set to 10 otherwise the expression within the next “else” statement is evaluated.As you can see, there’s another “if” statement within the “else” branch of Line 11.When there is only one expression within a branch, the “begin” and “end” keywords can be removed.A more complex functionality can be described by using nested “if” statements.The input with the highest priority (x) is checked first.If it’s logic high, the condition is evaluated as true and the output is set to 11.The following example clarifies this point: When “addr” is 001 or 011, “out” should be 00. What branch will be selected by the “casex” statement? 1 match x11 so the default branch should be chosen and "out" should be 00.However, as mentioned above, the bit locations that contain z or x values will be masked no matter they are in the branch expression or in the expression within the parentheses after the “casex” statement.