Verilog keyword multi-branch statement example detail
Key words: case, selector
The case statement is a form of multiple conditional branches, which can solve the problem of inconvenient use when there are multiple conditional options in the if statement.
case statement
The format of the case statement is as follows:
case(case_expr) condition1 : true_statement1 ; condition2 : true_statement2 ; ... default : default_statement ; endcase
condition1 is executed if Condition1 is true. true_statement1 is executed. If condition1 is false and condition2 is true, then true_statement2 is executed; And so on. If none of the conditions is true, the default_statement statement is executed.
The default statement is optional, and there cannot be more than one default statement in a case statement.
There can be multiple conditional options, not limited to condition1, condition2, etc., and these conditional options are not required to be mutually exclusive. Although these conditional options are compared concurrently, the execution effect is who comes first and who is executed if the condition is true.
Execution statements such as ture_statement1 can be a single statement or multiple statements. If multiple statements are executed, use the begin and end keywords.
case statements support nested use.
The following uses the case statement instead of the if statement to implement a 4-way selector function. The simulation results are in complete agreement with the testbench Reference Conditional Statements chapter.
module mux4to1( input [1:0] sel , input [1:0] p0 , input [1:0] p1 , input [1:0] p2 , input [1:0] p3 , output [1:0] sout); reg [1:0] sout_t ; always @(*) case(sel) 2'b00: begin sout_t = p0 ; end 2'b01: sout_t = p1 ; 2'b10: sout_t = p2 ; default: sout_t = p3 ; endcase assign sout = sout_t ; endmodule
Conditional option forms in a case statement need not all be constants, but can also be x or z values.
When the same statement needs to be executed under multiple conditional options, multiple conditional options can be separated by commas and placed in candidates of the same statement block.
However, the comparison logic of x or z in a case statement is not comprehensive, so it is generally not recommended to use x or z as comparison values in a case statement.
For example, extend the case statement for 4-way selectors as follows:
case(sel) 2'b00: sout_t = p0 ; 2'b01: sout_t = p1 ; 2'b10: sout_t = p2 ; 2'b11: sout_t = p3 ; 2'bx0, 2'bx1, 2'bxz, 2'bxx, 2'b0x, 2'b1x, 2'bzx : sout_t = 2'bxx ; 2'bz0, 2'bz1, 2'bzz, 2'b0z, 2'b1z : sout_t = 2'bzz ; default: $display("Unexpected input control!!!" ); endcase
casex/casez statement
casex and casez statements are variations of case statements to represent irrelevant items in conditional options.
casex uses "x" for irrelevant values, casez uses question marks "?" To represent irrelevant values.
The functions of the two implementations are completely consistent, and the syntax is also completely consistent with the case statement.
However, casex and casez are generally not integrated and are mostly used for simulation.
For example, the casez statement is used to implement a 4-way selector on a 4-bit control terminal.
module mux4to1( input [3:0] sel , input [1:0] p0 , input [1:0] p1 , input [1:0] p2 , input [1:0] p3 , output [1:0] sout); reg [1:0] sout_t ; always @(*) casez(sel) 4'b??? 1: sout_t = p0 ; 4'b?? 1? : sout_t = p1 ; 4'b? 1?? : sout_t = p2 ; 4'b1??? : sout_t = p3 ; default: sout_t = 2'b0 ; endcase assign sout = sout_t ; endmodule
The above is the Verilog keyword multi-branch statement example detailed content, more information about Verilog keyword multi-branch statement please pay attention to script home other related articles!
- Verilog language data type basics tutorial
- Verilog language expression basic use
- Verilog 8 kinds of compilation instructions use details
- Verilog keyword conditional statement example detail
- Verilog language keyword module example explanation
- Verilog language loop statement example detail
- Verilog design method and process detailed explanation
Related article
-
Verilog language expression basic use
This article mainly introduces the basic use of Verilog language expressions for everyone. Friends in need can use it for reference. I hope it can be helpful2023-04-04 -
Introduction to Macromedia Flex markup language
Macromedia Flex Markup Language Introduction...2007-01-01 -
Script error How to solve the current page script error solution summary
We often visit the web page, the bottom right corner often appear script error prompt, remember a foreign bank because of this reason caused a great impact on the business, if put on the website for users are also easy to produce some illusions, the website has problems, unsafe and so on2011-12-12 -
Verilog language loop statement example detail
This article mainly introduces the detailed explanation of Verilog language circular statement examples. If you need it, you can use it for reference. I hope it can be helpful2023-04-04 -
git eclipse plug-in installation
Usually we use ecliipse more in development projects. So in this eclipse Git plug-in installation and use, the need for friends can refer to2016-05-05 -
Analysis of common ebook formats and decompilation ideas
This article is only from a technical point of view to discuss the decompilation of e-books, do not use it for illegal purposes such as copyright infringement, or harm the interests of others. If you are disappointed by this, do not read on.2008-07-07 -
Verilog keyword conditional statement example detail
This article mainly introduces the detailed explanation of Verilog keyword conditional sentences. Friends in need can use it for reference. I hope it can be helpful2023-04-04 -
Reference WINSOCK's example code in InstallShield
Example code for referencing WINSOCK in InstallShield...2007-03-03 -
linux Shell Learning notes Day 5
Recently, I feel a little dizzy when I look at Shell, and I will be busy with the squid problem of the company in another day. Now the two-wire machine room is about to become a single-wire machine room.2010-12-12 -
Scripting and batch processing in one
We can now use JScript or VBScript to do more complex work, with WMI, can be said to do anything2012-05-05
Latest comments