Verilog 15 :Memory Modeling and Finite State Machines (FSMs)
1. Introduction to Sequential Logic
Before we dive into memories and FSMs, we need to understand sequential logic.
-
Combinational logic: output depends only on current inputs. Examples: adders, multiplexers, logic gates.
-
Sequential logic: output depends on current inputs and previous state. Examples: flip-flops, registers, counters.
Key Building Blocks:
-
Flip-flops: Store a single bit of data. Types include D, T, JK flip-flops.
-
Registers: Group of flip-flops storing multiple bits.
-
Clock signal: Controls when sequential elements update their state.
Why Sequential Logic Matters:
Memories and FSMs are sequential circuits. Without understanding how data flows over time (clock cycles), modeling these components becomes impossible.
2. Memory Modeling in Verilog
Memory in digital systems is simply a collection of registers or arrays of storage elements.
2.1. Memory Basics
-
Memory Address: Unique location index (like 0, 1, 2, …).
-
Memory Word: Data stored at an address (8-bit, 16-bit, etc.).
-
Memory Depth: Total number of storage locations.
-
Data Width: Number of bits per word.
Example Analogy:
Think of a spreadsheet: rows = addresses, columns = bits in each word.
2.2. Declaring Memories in Verilog
-
[7:0]→ 8 bits per word -
[0:15]→ 16 addresses (0 to 15) -
Access like
mem[3]→ reads or writes the 4th word
2.3. Writing to Memory
-
write_enablecontrols whether the memory is updated. -
Non-blocking assignment (
<=) ensures correct sequential behavior.
2.4. Reading from Memory
Synchronous Read (clocked):
Asynchronous Read (immediate):
Tip: Use synchronous read for sequential circuits, asynchronous for lookup tables or ROMs.
2.5. Accessing Bits in Memory
2.6. Initializing Memory
Option 1: Hard-coded Initialization
Option 2: Using a Memory File
-
Each line of
memory.listcontains one word in binary. -
$readmemhworks for hexadecimal format.
2.7. Simple Memory Example
Explanation:
-
Synchronous memory write and read.
-
16 words × 8-bit each.
-
data_outreflects content at selected address.
3. Introduction to Finite State Machines (FSMs)
FSMs are sequential circuits used to control operations based on current state and inputs.
3.1. FSM Components
-
States: Represent the status of the system (IDLE, RUNNING, ERROR).
-
Inputs: External signals that trigger transitions.
-
Outputs: System responses.
-
Transitions: Rules that move from one state to another.
Real-life Analogy:
Traffic light controller: states = Red, Yellow, Green; transitions = timer or sensor; outputs = light signals.
3.2. Types of FSM
| Type | Output depends on | Characteristics |
|---|---|---|
| Moore | Current state only | Stable outputs, fewer glitches |
| Mealy | Current state + inputs | Potential glitches, fewer states needed |
3.3. Moore vs Mealy Example
Moore FSM – output = 1 if in RUN state.
Mealy FSM – output = 1 if in RUN state AND input start is high.
4. Modeling FSMs in Verilog
FSMs are modeled in two main sections:
-
Combinational logic – calculates next_state
-
Sequential logic – updates current_state at clock edge
4.1. Step-by-Step FSM Design
Step 1: Define all states
Step 2: Sequential section (state register)
Step 3: Combinational section (next state logic)
Step 4: Output logic (Moore or Mealy)
4.2. FSM Encoding Techniques
-
Binary Encoding – minimal flip-flops. Efficient for ASICs.
-
One-Hot Encoding – one flip-flop per state. Fast for FPGAs.
-
Gray Encoding – only one bit changes between states, reduces glitches.
Example – One-Hot:
4.3. Complete Arbiter FSM Example
5. Best Practices
-
Use non-blocking assignments (
<=) in sequential logic. -
Initialize FSM states and memory elements to avoid X (unknown) values.
-
Prefer separate combinational and sequential blocks for readability.
-
Choose encoding style based on hardware: one-hot for FPGAs, binary for ASICs.
-
Test FSMs with simulation and waveform inspection before synthesis.
6. Summary
-
Memory Modeling: Create arrays of registers, read/write synchronously or asynchronously, initialize using code or files.
-
FSMs: Use sequential logic for states, combinational logic for next state, Moore vs Mealy output logic.
-
State Encodings: Binary, one-hot, gray – choose depending on design requirements.
-
Proper understanding of clocked sequential logic is key to robust memory and FSM design.

Comments
Post a Comment