What is the m How to translate the state and output equations of a FSM into Verilog code?

I am developing an FSM of a university elevator problem in Verilog through the Boolean equations of states and outputs that I obtained from the truth table. But when I compile the code to see the expected output waveforms, it doesn't work as expected. This is the state diagram of the FSM along with its encoded inputs and outputs:
Later, I did a truth table based on the corresponding transition for a mealy fsm machine. Since the inputs are encoded in two bits, I placed them in the table as R1 and R2. And since there are three bits for the output, I divided it into Y2, Y1, and Y0. Then I selected for the next state and output those values that are true (i.e., equal to 1), and then simplified the Boolean equations using Karnaugh maps, resulting in the following equations:
Y2 = ¬S1 · R1 · ¬R0 + ¬S0 · R1 · R0
Y1 = S1 · ¬S0 · R0 + ¬S1 · S0 · R1 + ¬S1 · R1 · ¬R0 + S0 · ¬R1 · R0
Y0 = S1 · ¬S0 · R0 + ¬S1 · S0 · R1 + ¬S0 · R1 · ¬R0 + S1 · S0 · ¬R1 · R0 S1 ′ = S1 · ¬S0 · R1 · ¬R0 + ¬S1 · S0 · R1 · R0 + S1 · ¬S0 · R1 · R0 S0 ′ = ¬S1 · R1 · ¬R0 + S1 · ¬S0 · R1 · R0
And next state equations:
S1 ′ = S1 · ¬S0 · R1 · ¬R0 + ¬S1 · S0 · R1 · R0 + S1 · ¬S0 · R1 · R0
S0 ′ = ¬S1 · R1 · ¬R0 + S1 · ¬S0 · R1 · R0
Trying to take the FSM into Verilog code, I based it on the examples on this page , but I adapted them to the equations that I derived from the truth table, and this is what I did:
module mealy_fsm(
input clk,
input reset,
input R1,
input R0,
output reg [2:0] Y,
output reg [1:0] S
);
wire S1 = S[1], S0 = S[0];
wire [1:0] S_next;
wire [2:0] Y_comb;
assign S_next[1] = ( S1 & ~S0 & R1 & ~R0)
| (~S1 & S0 & R1 & R0)
| ( S1 & ~S0 & R1 & R0);
assign S_next[0] = (~S1 & R1 & ~R0)
| ( S1 & ~S0 & R1 & R0);
assign Y_comb[2] = (~S1 & R1 & ~R0)
| (~S0 & R1 & R0);
assign Y_comb[1] = ( S1 & ~S0 & R0)
| (~S1 & S0 & R1)
| (~S1 & R1 & ~R0)
| ( S0 & ~R1 & R0);
assign Y_comb[0] = ( S1 & ~S0 & R0)
| (~S1 & S0 & R1)
| (~S0 & R1 & ~R0)
| ( S1 & S0 & ~R1 & R0);
always @(posedge clk or posedge reset) begin
if (reset) begin
S <= 2'b00;
Y <= 3'b000;
end else begin
S <= S_next;
Y <= Y_comb;
end
end
endmodule
And the Test bench:
`timescale 1ns/1ps
module tb_mealy_fsm;
reg clk;
reg reset;
reg R1;
reg R0;
wire [2:0] Y;
wire [1:0] S;
mealy_fsm uut (
.clk (clk),
.reset (reset),
.R1 (R1),
.R0 (R0),
.Y (Y),
.S (S)
);
initial begin
clk = 0;
forever #5 clk = ~clk;
end
initial begin
reset = 1;
R1 = 0;
R0 = 0;
#12 reset = 0;
#10 {R1,R0} = 2'b00;
#10 {R1,R0} = 2'b01;
#10 {R1,R0} = 2'b10;
#10 {R1,R0} = 2'b11;
#10 {R1,R0} = 2'b01;
#10 {R1,R0} = 2'b10;
#10 {R1,R0} = 2'b00;
#10 {R1,R0} = 2'b11;
#10 $finish;
end
initial begin
$display("time clk reset R1 R0 | S1 S0 | Y2 Y1 Y0");
$monitor("%4t %b %b %b %b | %b %b | %b %b %b",
$time, clk, reset, R1, R0, S[1], S[0], Y[2], Y[1], Y[0]);
end
endmodule
But when I compile it in EDA Playground, I couldn't see the output waveforms for the inputs (Clk,reset, X1, X2), states, and outputs (Y0, Y1, Y2). So my question is as follows:
Should I have implemented the FSM based on the logic of the diagram or on the Boolean equations derived from the truth table?
Are there errors in the FSM implementation code and its testbench? How should it be?
Answer
**📁 my-app/
📄 index.js**
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);
📄 App.js
import React from "react";
import Navbar from "./components/Navbar";
import Hero from "./components/Hero";
import Events from "./components/Events";
import RegisterForm from "./components/RegisterForm";
import LoginForm from "./components/LoginForm";
import ForgetPassword from "./components/ForgetPassword";
import Footer from "./components/Footer";
function App() {
return (
\<div\>
\<Navbar /\>
\<Hero /\>
\<Events /\>
\<RegisterForm /\>
\<LoginForm /\>
\<ForgetPassword /\>
\<Footer /\>
\</div\>
);
}
export default App;
📁 components/
📄 Navbar.js
import React from "react";
function Navbar() {
return (
\<nav\>
\<h2\>TechEvent\</h2\>
\<ul\>
\<li\>Home\</li\>
\<li\>Events\</li\>
\<li\>Register\</li\>
\<li\>Contact\</li\>
\</ul\>
\</nav\>
);
}
export default Navbar;
📄 Hero.js
import React from "react";
function Hero() {
return (
\<section\>
\<h1\>Welcome to TechEvent 2025\</h1\>
\<button\>Register Now\</button\>
\</section\>
);
}
export default Hero;
📄 Events.js
import React from "react";
const sessions = [
{ id: 1, title: "React Basics", speaker: "John Doe", time: "10:00 AM" },
{ id: 2, title: "Advanced JS", speaker: "Jane Smith", time: "11:30 AM" },
];
function Events() {
return (
\<div\>
\<h2\>Sessions\</h2\>
{sessions.map((session) =\> (
\<div key={session.id}\>
\<h3\>{session.title}\</h3\>
\<p\>{session.speaker} - {session.time}\</p\>
\<button\>Register\</button\>
\</div\>
))}
\</div\>
);
}
export default Events;
📄 RegisterForm.js
import React, { useState, useEffect } from "react";
function RegisterForm() {
const [formData, setFormData] = useState({
firstName: "",
lastName: "",
email: "",
mobile: "",
session: "",
});
useEffect(() => {
const saved = localStorage.getItem("registration");
if (saved) setFormData(JSON.parse(saved));
}, []);
useEffect(() => {
localStorage.setItem("registration", JSON.stringify(formData));
}, [formData]);
const handleChange = (e) => {
const { name, value } = e.target;
setFormData({ ...formData, \[name\]: value });
};
return (
\<form\>
\<input name="firstName" value={formData.firstName} onChange={handleChange} placeholder="First Name" /\>
\<input name="lastName" value={formData.lastName} onChange={handleChange} placeholder="Last Name" /\>
\<input name="email" value={formData.email} onChange={handleChange} placeholder="Email" /\>
\<input name="mobile" value={formData.mobile} onChange={handleChange} placeholder="Mobile No" /\>
\<input name="session" value={formData.session} onChange={handleChange} placeholder="Session Title" /\>
\<button type="submit"\>Register\</button\>
\</form\>
);
}
export default RegisterForm;
📄 LoginForm.js
import React, { useState } from "react";
function LoginForm() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
const emailRegex = /^\[^\\s@\]+@\[^\\s@\]+\\.\[^\\s@\]+$/;
if (!emailRegex.test(email)) setError("Invalid email");
else if (password.length \< 6) setError("Password too short");
else setError("");
};
return (
\<form onSubmit={handleSubmit}\>
\<input type="email" value={email} onChange={(e) =\> setEmail(e.target.value)} placeholder="Email" /\>
\<input type="password" value={password} onChange={(e) =\> setPassword(e.target.value)} placeholder="Password" /\>
{error && \<p\>{error}\</p\>}
\<button type="submit"\>Login\</button\>
\</form\>
);
}
export default LoginForm;
📄 ForgetPassword.js
import React, { useState } from "react";
function ForgetPassword() {
const [email, setEmail] = useState("");
const [message, setMessage] = useState("");
const handleSend = () => {
const emailRegex = /^\[^\\s@\]+@\[^\\s@\]+\\.\[^\\s@\]+$/;
if (emailRegex.test(email)) setMessage("Recovery email sent!");
else setMessage("Invalid email");
};
return (
\<div\>
\<input value={email} onChange={(e) =\> setEmail(e.target.value)} placeholder="Enter email" /\>
\<button onClick={handleSend}\>Send Recovery Email\</button\>
{message && \<p\>{message}\</p\>}
\</div\>
);
}
export default ForgetPassword;
📄 Footer.js
import React from "react";
function Footer() {
return (
\<footer\>
\<p\>Contact:
);
}
export default Footer;
📄 index.css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
nav, footer {
background-color: #333;
color: white;
padding: 1rem;
}
ul {
list-style: none;
display: flex;
gap: 1rem;
}
form, section, div {
padding: 1rem;
margin: 1rem;
}
input, button {
display: block;
margin-top: 0.5rem;
}
Let me know if you'd like me to split each into downloadable files or add routing and responsiveness (e.g., using Tailwind or Bootstrap).