A system has a 3-bit input D_IN which is read in at every positive going edge of a clock input CLK. If the current D_IN is greater than the previous D_IN by at least 2, a 3-bit output COUNT is incremented. If D_IN is 0 for 3 consecutive CLK cycles, the COUNT is reset. When COUNT reaches 6, the system will assert an output ALARM and the COUNT will not increase further, till it is reset by giving 0s at D_IN for 3 consecutive cycles. Write a Verilog program that implements such a system. Compile and verify the functionality of the program with appropriate test cases in your Verilog Test Fixture.
I have attempted the question above but as of now, I am still trying to debug the process, to where the current value is 110, it should not execute 110 + 1, instead, it should assert ALARM = 1 and count should remain the same, i.e. don't care.
Here is my code below.
May I know what is wrong with my code? Please be patient as this is my first time doing Verilog.
Thank you.
Moderators note : Please use code tags for pieces of code
I have attempted the question above but as of now, I am still trying to debug the process, to where the current value is 110, it should not execute 110 + 1, instead, it should assert ALARM = 1 and count should remain the same, i.e. don't care.
Here is my code below.
Code:
`timescale 1ns / 1ps
module D2Q2_mod(
input [2:0] D_IN, //new input
input CLK,
output reg [2:0] ALARM = 3'b000,
output reg [2:0] COUNT = 3'b000
);
reg [2:0] D_IN_OLD; //previous input
reg [2:0] D_IN_1 = 3'b000;
/*reg [2:0] D_IN_2 = 3'b000;
reg [2:0] D_IN_3 = 3'b000;*/
always @ (posedge CLK)
begin
if (D_IN == 0 && D_IN_OLD == 0 && D_IN_1 == 0) begin
COUNT = 3'b000;
ALARM = 3'b000;
end
else if ((D_IN > D_IN_OLD) && (D_IN >= D_IN_OLD + 3'b010) && (D_IN <= 3'b110)) begin //Dr Chua said "D_IN < 6". Is it becos you don't want 6+2=8(OVERFLOW).
COUNT = COUNT + 1;
end
else if ((D_IN < D_IN_OLD) && (D_IN_OLD >= D_IN + 3'b010) && (D_IN <= 3'b110)) begin
COUNT = COUNT + 1;
end
else if ((D_IN_OLD < D_IN + 3'b010) && (D_IN <= 3'b110)) begin
COUNT = COUNT;
end
else if (COUNT == 3'b110) begin
ALARM = 3'b001;
COUNT = COUNT;
end
/*else if (COUNT == 3'b110 && (D_IN_OLD == 3'b111 && D_IN == 000)) begin
COUNT = COUNT;
ALARM = 3'b001;
end*/
if (ALARM == 3'b001 && (D_IN == 0 && D_IN_OLD == 0 && D_IN_1 == 0)) begin
COUNT = 3'b000;
ALARM = 3'b000;
end
D_IN_1 = D_IN_OLD;
D_IN_OLD = D_IN;
end
endmodule
Thank you.
Moderators note : Please use code tags for pieces of code
Last edited by a moderator: