Using Case Statement
Part of the Fundamentals section of Coddy's Verilog journey — lesson 66 of 90.
Challenge
Build a 4-to-1 multiplexer using a case statement instead of if-else.
Truth Table
| sel | out |
|---|---|
| 00 | out = in0 |
| 01 | out = in1 |
| 10 | out = in2 |
| 11 | out = in3 |
What to do:
- Create a module named
mux4to1_case - Add inputs
in0,in1,in2,in3(1 bit each) - Add input
sel(2 bits) - Add output
out(1 bit, typereg) - Add an
always @(*)block - Inside, add a
case (sel)statement - Add cases for
2'b00,2'b01,2'b10,2'b11 - Add a
defaultcase - Close with
endcaseandendmodule
Try it yourself
// TODO: Create module named mux4to1_case
// TODO: Add inputs: in0, in1, in2, in3 (1 bit each)
// TODO: Add input sel (2 bits)
// TODO: Add output out (reg type)
// TODO: Add always @(*) block
// TODO: Add case (sel)
// TODO: Add case 2'b00: out = in0;
// TODO: Add case 2'b01: out = in1;
// TODO: Add case 2'b10: out = in2;
// TODO: Add case 2'b11: out = in3;
// TODO: Add default: out = in0;
// TODO: Add endcase
// TODO: Add endmoduleAll lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic