Question: You were telling me to make it so one button if I press once in mode 1 and if I press twice in mode 2

You were telling me to make it so one button if I press once in mode 1 and if I press twice in mode 2? The thing you are mentioning is what we discussed about how to encode your operation once you are within that mode. The way you had it in your code was to use switches, however you'd then have to figure out how to then still utilize all 16 bits for your inputs.
The other method would be to encode your selection with more button presses. So, one press you are in mode 1, then say press again for add, press again for subtract, and then once you have a selection a separate button press goes to execute stage.
#set_property PACKAGE_PIN U18[get_ports btnC]
#set_property IOSTANDARD LVCMOS33[get_ports btnC]
#set_property PACKAGE_PIN T18[get_ports btnU]
#set_property IOSTANDARD LVCMOS33[get_ports btnU]
#set_property PACKAGE_PIN W19[get_ports btnL]
#set_property IOSTANDARD LVCMOS33[get_ports btnL]
#set_property PACKAGE_PIN T17[get_ports btnR]
#set_property IOSTANDARD LVCMOS33[get_ports btnR]
#set_property PACKAGE_PIN U17[get_ports btnD]
#set_property IOSTANDARD LVCMOS33[get_ports btnD]
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
reg mode = MODE_1; // Initialize mode
reg btn_mode_sync; // Synchronized button signal
reg btn_mode_debounced; // Debounced button signal
// Synchronize and debounce btn_mode
always @(posedge clk) begin
btn_mode_sync <= btn_mode; // Synchronize input to clock domain
if (btn_mode_sync != btn_mode_debounced) begin
debounce_cnt <=0; // Reset counter
end else if (debounce_cnt <4'd15) begin
debounce_cnt <= debounce_cnt +1; // Increment counter
end
if (debounce_cnt ==4'd15) begin
btn_mode_debounced <= btn_mode_sync; // Update debounced signal
end
end
// Toggle mode on falling edge of debounced button
always @(posedge clk) begin
if (btn_mode_debounced && !btn_mode_sync) begin
mode <= ~mode; // Toggle mode
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

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 Electrical Engineering Questions!