Question: module calculator _ fsm ( input clk , input btn _ mode, / / Single button for toggling modes input [ 1 : 0 ]

module calculator_fsm (
input clk,
input btn_mode, // Single button for toggling modes
input [1:0] btn_sel, // Operation selection input
output reg [2:0] opcode,
output reg mode, //0= Arithmetic, 1= Bitwise
output reg alu_enable
);
localparam MODE_1=1'b0; // Arithmetic operations
localparam MODE_2=1'b1; // Bitwise operations
// Internal signals
reg btn_mode_last; // For edge detection
reg [3:0] debounce_cnt; // Debouncing counter
// Debouncing logic for btn_mode
always @(posedge clk) begin
if (btn_mode != btn_mode_last) begin
debounce_cnt =0; // Reset the debounce counter on button change
end else if (debounce_cnt 4'd15) begin
debounce_cnt = debounce_cnt +1; // Increment the counter
end
if (debounce_cnt ==4'd15) begin
btn_mode_last = btn_mode; // Update the last button state after debounce
end
end
// Toggle between modes on button press
always @(posedge clk) begin
if (btn_mode_last && !btn_mode) begin
mode = ~mode; // Toggle mode on a clean edge
end
end
// Determine opcode based on mode and button inputs
always @(*) begin
alu_enable =0; // Default disable ALU
case (mode)
MODE_1: begin
// 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
// 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
opcode =3'b000;
alu_enable =0;
end
endcase
end
endmodule
I have 5 buttons and i need the four to choose the opcode and the last one to use the button to change mode.
module calculator _ fsm ( input clk , input btn _

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!