Recap - ALU Design
Part of the Fundamentals section of Coddy's Verilog journey — lesson 57 of 90.
Challenge
An ALU (Arithmetic Logic Unit) is a digital circuit that takes two inputs, performs an operation on them, and produces one output. It performs arithmetic and logic operations on two inputs based on a select signal. The select signal chooses one operation to execute.
How it works
| select | Operation | Output |
|---|---|---|
2'b00 | Addition | a + b |
2'b01 | Subtraction | a - b |
2'b10 | Bitwise AND | a & b |
2'b11 | Bitwise OR | a | b |
Build a simple ALU using a case statement.
What to do:
- Create a module named
alu - Add inputs:
a(4-bit),b(4-bit),select(2-bit) - Add output:
result(4-bit, reg type) - Add an
always @(*)block - Inside, add a
case (select)statement - Add four cases for
2'b00,2'b01,2'b10,2'b11 - Add a
defaultcase to setresult = 0 - Close with
endcaseandendmodule
Try it yourself
// TODO: Create module alu
// TODO: Add inputs (a, b, select)
// TODO: Add output (result)
// TODO: Add always @(*) block
// TODO: Add case (select)
// TODO: Add case 2'b00: result = a + b;
// TODO: Add case 2'b01: result = a - b;
// TODO: Add case 2'b10: result = a & b;
// TODO: Add case 2'b11: result = a | b;
// TODO: Add default: result = 0;
// 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