Question: why is the btn _ sel 1 1 staying while the mode switch and how can you get rid of that so it go to

why is the btn_sel 11 staying while the mode switch and how can you get rid of that so it go to the second mode and string to 00.
module calculator_fsm (
input clk,
input btn_mode1,
input btn_mode2,
input [1:0] btn_sel,
output reg [2:0] opcode,
output reg mode,
output reg alu_enable
);
localparam MODE_1=1'b0; // Arithmetic operations
localparam MODE_2=1'b1; // Bitwise operations
reg current_mode;
reg btn_mode1_last, btn_mode2_last;
reg [1:0] btn_sel_internal;
// Detect mode switch and reset btn_sel
always @(posedge clk) begin
if (btn_mode1 && !btn_mode1_last) begin
current_mode = MODE_1;
btn_sel_internal =2'b00; // Reset to default operation
end else if (btn_mode2 && !btn_mode2_last) begin
current_mode = MODE_2;
btn_sel_internal =2'b00; // Reset to default operation
end else begin
btn_sel_internal = btn_sel; // Retain btn_sel
end
btn_mode1_last = btn_mode1;
btn_mode2_last = btn_mode2;
end
always @(*) begin
alu_enable =0; // Default disable ALU
case (current_mode)
MODE_1: begin
mode =0; // Arithmetic Mode
case (btn_sel)
2'b00: opcode =3'b000; // Addition
2'b01: opcode =3'b001; // Subtraction
2'b10: opcode =3'b010; // Multiplication
2'b11: opcode =3'b011; // Division
default: opcode =3'b000; // Default to Addition
endcase
alu_enable =1;
end
MODE_2: begin
mode =1; // Bitwise Mode
case (btn_sel)
2'b00: opcode =3'b100; // AND
2'b01: opcode =3'b101; // OR
2'b10: opcode =3'b110; // Shift Left
2'b11: opcode =3'b111; // Shift Right
default: opcode =3'b100; // Default to AND
endcase
alu_enable =1;
end
default: begin
mode =0;
opcode =3'b000;
alu_enable =0;
end
endcase
end
endmodule
`timescale 1ns /1ps
module calculator_fsm_tb();
reg clk, btn_mode1, btn_mode2;
reg [1:0] btn_sel;
wire [2:0] opcode;
wire mode;
wire alu_enable;
calculator_fsm uut (
.clk(clk),
.btn_mode1(btn_mode1),
.btn_mode2(btn_mode2),
.btn_sel(btn_sel),
.opcode(opcode),
.mode(mode),
.alu_enable(alu_enable)
);
// Clock generation
initial begin
clk =0;
forever #5 clk = ~clk; //10 ns clock period
end
// Test sequence
initial begin
// Initialize inputs
btn_mode1=0;
btn_mode2=0;
btn_sel =2'b00;
// Wait for some time before starting
#10;
// Step through all operations in Arithmetic mode
btn_mode1=1; #10 btn_mode1=0; // Select Arithmetic mode
#10 btn_sel =2'b00; // Addition
#10 btn_sel =2'b01; // Subtraction
#10 btn_sel =2'b10; // Multiplication
#10 btn_sel =2'b11; // Division
#10;
// Step through all operations in Bitwise mode
btn_mode2=1; #10 btn_mode2=0; // Select Bitwise mode
#10 btn_sel =2'b00; // AND
#10 btn_sel =2'b01; // OR
#10 btn_sel =2'b10; // Shift Left
#10 btn_sel =2'b11; // Shift Right
// End simulation
#50 $finish;
end
endmodule
why is the btn _ sel 1 1 staying while the mode

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!