4 Bit Counter
Part of the Fundamentals section of Coddy's Verilog journey — lesson 84 of 90.
Challenge
Build a 4-bit counter that counts from 0 to 15 and wraps back to 0.
Module Interface
| Port | Direction | Width | Description |
|---|---|---|---|
clk | input | 1 bit | Clock signal |
reset | input | 1 bit | Reset counter to 0 |
count | output | 4 bits | Current counter value |
Truth Table
| Clock Cycle | count |
|---|---|
| After reset | 0 |
| 1 | 1 |
| 2 | 2 |
| ... | ... |
| 15 | 15 |
| 16 | 0 (wraps around) |
Your task is to complete the module below.
What to do:
- On
reset, setcountto 0 - On each rising clock edge, increment
countby 1 - When
countreaches 15, the next increment should wrap to 0
Try it yourself
module counter (
input clk,
input reset,
output reg [3:0] count
);
// TODO: Add always block with posedge clk and posedge reset
// On reset: count <= 0
// Otherwise: count <= count + 1
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