Question: is it easier if it start arithmode then press the button to switch the mode to bitwise? module calculator _ fsm ( input clk ,

is it easier if it start arithmode then press the button to switch the mode to bitwise?
module calculator_fsm(
input clk,
input btn_mode, // 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
);
// Parameters for modes
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
module calculator_fsm_tb;
// Inputs
reg clk;
reg btn_mode;
reg [1:0] btn_sel;
// Outputs
wire [2:0] opcode;
wire mode;
wire alu_enable;
// Instantiate the Unit Under Test (UUT)
calculator_fsm uut (
.clk(clk),
.btn_mode(btn_mode),
.btn_sel(btn_sel),
.opcode(opcode),
.mode(mode),
.alu_enable(alu_enable)
);
// Clock generation
initial begin
clk =0;
forever #5 clk = ~clk; //10ns clock period
end
// Test stimulus
initial begin
// Initialize inputs
btn_mode =0;
btn_sel =2'b00;
// Wait for global reset
#20;
// Test Mode Toggle and Operations
//1. Start in Arithmetic Mode, perform all operations
repeat(2) @(posedge clk); btn_sel =2'b00; #10; // Addition
repeat(2) @(posedge clk); btn_sel =2'b01; #10; // Subtraction
repeat(2) @(posedge clk); btn_sel =2'b10; #10; // Multiplication
repeat(2) @(posedge clk); btn_sel =2'b11; #10; // Division
//2. Toggle to Bitwise Mode
repeat(2) @(posedge clk); btn_mode =1; #10;
repeat(2) @(posedge clk); btn_mode =0; #50;
// Perform all operations in Bitwise Mode
repeat(2) @(posedge clk); btn_sel =2'b00; #10; // AND
repeat(2) @(posedge clk); btn_sel =2'b01; #10; // OR
repeat(2) @(posedge clk); btn_sel =2'b10; #10; // Shift Left
repeat(2) @(posedge clk); btn_sel =2'b11; #10; // Shift Right
// Toggle back to Arithmetic Mode
repeat(2) @(posedge clk); btn_mode =1; #10;
repeat(2) @(posedge clk); btn_mode =0; #50;
// Verify Arithmetic Mode operations again
repeat(2) @(posedge clk); btn_sel =2'b00; #10; // Addition
$stop; // End simulation
end
endmodule
is it easier if it start arithmode then press the

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!