I am writing a program that acts as a cash deposit box. Everything occurs on the clock edge. If enable is true and reset is true, the value in the box resets to 0. If enable is true and add is 1, we add the amount to the box. If enable is true and add is 0 we subtract from the box.
Here is my code and test bench
module cashbox(output [15:0] value, input [15:0] amount, input add,enable, clock, reset);
reg value;
always @(posedge clock)
begin
if(enable) begin
if(reset)begin
value <= 0;
end
else begin
if(add)begin
value <= value + amount;
end
else begin
value <= value - amount;
end
end
end
end
endmodule
// cashbox testbench
module cashbox_tb;
reg [15:0] amt;
reg add, en, clk, rst;
wire [15:0] value;
cashbox cb(value, amt, add, en, clk, rst);
always
#50 clk = ~clk;
initial begin
$display("Amount in box %d", value);
clk = 1; en = 1; add = 1;
rst = 1; #10 rst = 0;
amt = 100; add = 1;
#170 en = 1; #100 en = 0; // 90
#20 amt = 50; add = 0;
#180 en = 1; #100 en = 0; // 290
#20 amt = 75; add = 1;
#180 en = 1; #100 en = 0; // 490
#20 amt = 35; add = 0;
#180 en = 1; #100 en = 0; // 690
#20 amt = 50; add = 0;
#180 en = 1; #100 en = 0;
#810 $finish;
end
endmodule
Everything compiles but when I run it, it displays Amount in box X. It doesnt display a value
I changed my test bench to this // cashbox testbench
module cashbox_tb;
reg [15:0] amt;
reg add, en, clk, rst;
wire [15:0] value;
cashbox cb(value, amt, add, en, clk, rst);
always
#50 clk = ~clk;
initial begin
$monitor("Amount in box %d",value);
clk = 0; en = 0; add = 0;
rst = 1; #10 rst = 0;
#10 amt = 100; add = 1;
#170 en = 1; #100 en = 0; // 90
#20 amt = 50; add = 0;
#180 en = 1; #100 en = 0; // 290
#20 amt = 75; add = 1;
#180 en = 1; #100 en = 0; // 490
#20 amt = 35; add = 0;
#180 en = 1; #100 en = 0; // 690
#20 amt = 50; add = 0;
#180 en = 1; #100 en = 0;
#810 $finish;
end
endmodule
And now my output is back to saying Amount in box X