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 calculatorfsm
input clk
input btnmode, Button for toggling modes
input : btnsel, Operation selection input
output reg : opcode,
output reg mode, Arithmetic, Bitwise
output reg aluenable
;
Parameters for modes
localparam MODEb; Arithmetic operations
localparam MODEb; Bitwise operations
Internal signals
reg btnmodelast; For edge detection
reg : debouncecnt; Debouncing counter
Debouncing logic for btnmode
always @posedge clk begin
if btnmode btnmodelast begin
debouncecnt ; Reset the debounce counter on button change
end else if debouncecnt d begin
debouncecnt debouncecnt ; Increment the counter
end
if debouncecnt d begin
btnmodelast btnmode; Update the last button state after debounce
end
end
Toggle between modes on button press
always @posedge clk begin
if btnmodelast && btnmode begin
mode ~mode; Toggle mode on a clean edge
end
end
Determine opcode based on mode and button inputs
always @ begin
aluenable ; Default disable ALU
case mode
MODE: begin Arithmetic Mode
case btnsel
b: opcode b; Addition
b: opcode b; Subtraction
b: opcode b; Multiplication
b: opcode b; Division
default: opcode b; Default to Addition
endcase
aluenable ;
end
MODE: begin Bitwise Mode
case btnsel
b: opcode b; AND
b: opcode b; OR
b: opcode b; Shift Left
b: opcode b; Shift Right
default: opcode b; Default to AND
endcase
aluenable ;
end
default: begin
opcode b;
aluenable ;
end
endcase
end
endmodule
module calculatorfsmtb;
Inputs
reg clk;
reg btnmode;
reg : btnsel;
Outputs
wire : opcode;
wire mode;
wire aluenable;
Instantiate the Unit Under Test UUT
calculatorfsm uut
clkclk
btnmodebtnmode
btnselbtnsel
opcodeopcode
modemode
aluenablealuenable
;
Clock generation
initial begin
clk ;
forever # clk ~clk; ns clock period
end
Test stimulus
initial begin
Initialize inputs
btnmode ;
btnsel b;
Wait for global reset
#;
Test Mode Toggle and Operations
Start in Arithmetic Mode, perform all operations
repeat @posedge clk; btnsel b; #; Addition
repeat @posedge clk; btnsel b; #; Subtraction
repeat @posedge clk; btnsel b; #; Multiplication
repeat @posedge clk; btnsel b; #; Division
Toggle to Bitwise Mode
repeat @posedge clk; btnmode ; #;
repeat @posedge clk; btnmode ; #;
Perform all operations in Bitwise Mode
repeat @posedge clk; btnsel b; #; AND
repeat @posedge clk; btnsel b; #; OR
repeat @posedge clk; btnsel b; #; Shift Left
repeat @posedge clk; btnsel b; #; Shift Right
Toggle back to Arithmetic Mode
repeat @posedge clk; btnmode ; #;
repeat @posedge clk; btnmode ; #;
Verify Arithmetic Mode operations again
repeat @posedge clk; btnsel b; #; Addition
$stop; End simulation
end
endmodule
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
