Mealy vs. Moore Machine: Verilog Code Examples
Advertisement
This page provides Verilog code examples for both Mealy and Moore Machines.
Mealy Machine Verilog Code
Here’s a look at the Mealy Machine, along with its corresponding Verilog code. In a Mealy machine, the output is determined by both the current state and the input.

module mealy_verilog_code(out, in, rst, clk);
output out;
input in;
input clk, rst;
reg out;
reg[1:0] state;
parameter s0 = 2'd0,
s1 = 2'd1,
s2 = 2'd2,
s3 = 2'd3;
always @(posedge clk or negedge rst)
if(rst == 0) begin
state = s0;
out = 0;
end
else begin
case (state)
s0: if(in == 0) begin
out = 0;
state = s1;
end
else begin
out = 0;
state = s0;
end
s1: if(in == 0) begin
out = 0;
state = s1;
end
else begin
out = 0;
state = s2;
end
s2: if(in == 0) begin
out = 0;
state = s3;
end
else begin
out = 0;
state = s0;
end
s3: if(in == 0) begin
out = 0;
state = s1;
end
else begin
out = 1;
state = s2;
end
default: state = s0;
endcase
end
endmodule
Moore Machine Verilog Code
Now, let’s examine the Moore Machine and its Verilog implementation. In a Moore machine, the output is solely determined by the current state.

module moore_verilog_code(out, in, rst, clk);
output out;
input in;
input clk, rst;
reg out;
reg[1:0] state;
parameter s0 = 2'd0,
s1 = 2'd1,
s2 = 2'd2,
s3 = 2'd3;
always @(posedge clk or negedge rst)
if(rst == 0) begin
state = s0;
out = 0;
end
else begin
case (state)
s0: begin
out = 0;
if(in == 0) state = s1;
else state = s0;
end
s1: begin
out = 0;
if(in == 0) state = s1;
else state = s2;
end
s2: begin
out = 0;
if(in == 0) state = s3;
else state = s0;
end
s3: begin
out = 1;
if(in == 0) state = s1;
else state = s2;
end
default: state = s0;
endcase
end
endmodule
Explore Verilog Logic Gates, Mux & Encoders
- Verilog HDL Code for All Logic Gates
- 2-to-4 Decoder (Verilog HDL Code)
- 4-to-1 Multiplexer and 1-to-4 Demultiplexer (Verilog Code)
- Verilog Code: 1-to-4 Demultiplexer
- 8-to-1 Multiplexer (Verilog HDL Code)
- 8-to-3 Encoder Without Priority (Verilog)
- 8-to-3 Priority Encoder (Verilog)
- 8-to-3 Priority Encoder (Verilog Code)
- 1-Bit and 4-Bit Comparator Design (Verilog)
Explore Verilog Flip-Flops, Counters & Registers
- T, D, SR, JK Flip Flop (Verilog HDL Code)
- D Flip Flop Synchronous Reset (Verilog)
- D Flip Flop Without Reset (Verilog)
- Verilog Code for Binary Up-Down Counter
- Verilog HDL: BCD & Gray Counters
- 4-bit Down Counter (Verilog Code Test Bench)
- 4-bit Binary Asynchronous Reset Counter (Verilog)
- 4-bit Binary Synchronous Reset Counter (Verilog)
- 4-bit BCD Asynchronous Reset Counter (Verilog)
- 4-bit BCD Synchronous Reset Counter (Verilog Code)
- 4-bit Mod-13 Counter (Verilog Code Test Bench)
- Shift Left / Shift Right Register (Verilog)
- Parallel Load Shift Left Register (Verilog Code)
- PRBS Generator (Verilog Code Test Bench)
Explore Verilog Math, Processing & Memory
- Full Adder (Verilog HDL Code)
- Verilog Code: Half Adder, Half Subtractor, Full Subtractor
- 32-Bit ALU (Verilog Code)
- RAM / ROM (Verilog Code)
- Asynchronous FIFO (Verilog Code Test Bench)
- Asynchronous FIFO Design (Verilog Code and Explanation)
- Mealy / Moore Machine (Verilog)
- Low Pass FIR Filter (Verilog Code)
- 4-bit Binary to Gray Counter Converter in Verilog
