This program represents a finite state machine with a 7 segment led that counts to 5. I need to have it active low instead of active high but I am just unsure how to do this. I also have the testbench included. I know it would be better to use an always statement for the clock but I can handle that later.
`timescale 1ns / 1ps
//inputs, outputs
module Counter(
input u,
input clrn,
input clk,
output reg a,
output reg b,
output reg c,
output reg d,
output reg e,
output reg f,
output reg g);
reg [2:0] ns; //next state
reg [2:0] q; //present state
//declaration of the states
parameter [2:0] S0 = 3'b000, S1 = 3'b001, S2 = 3'b010, S3 = 3'b011, S4 = 3'b100, S5 = 3'b101;
always @ (posedge clk or negedge clrn)
begin
if(~clrn) //if reset present state q goes to 0
q = S0;
else
begin
case(q) //tests present state
S0:
if (u==1) begin
ns = S1;
a = 1'b0;
b = 1'b1;
c = 1'b1;
d = 1'b0;
e = 1'b0;
f = 1'b0;
g = 1'b0;
end
else begin
ns = S5;
a = 1'b1;
b = 1'b0;
c = 1'b1;
d = 1'b1;
e = 1'b0;
f = 1'b1;
g = 1'b1;
end
S1:
if (u==1) begin
ns = S2;
a = 1'b1;
b = 1'b1;
c = 1'b0;
d = 1'b1;
e = 1'b1;
f = 1'b0;
g = 1'b1;
end
else begin
ns = S0;
a = 1'b1;
b = 1'b1;
c = 1'b1;
d = 1'b1;
e = 1'b1;
f = 1'b1;
g = 1'b0;
end
S2:
if (u==1) begin
ns = S3;
a = 1'b1;
b = 1'b1;
c = 1'b1;
d = 1'b1;
e = 1'b0;
f = 1'b0;
g = 1'b1;
end
else begin
ns = S1;
a = 1'b0;
b = 1'b1;
c = 1'b1;
d = 1'b0;
e = 1'b0;
f = 1'b0;
g = 1'b0;
end
S3:
if (u==1) begin
ns = S4;
a = 1'b0;
b = 1'b1;
c = 1'b1;
d = 1'b0;
e = 1'b0;
f = 1'b1;
g = 1'b1;
end
else begin
ns = S2;
a = 1'b1;
b = 1'b0;
c = 1'b1;
d = 1'b1;
e = 1'b0;
f = 1'b1;
g = 1'b1;
end
S4:
if (u==1) begin
ns = S5;
a = 1'b1;
b = 1'b0;
c = 1'b1;
d = 1'b1;
e = 1'b0;
f = 1'b1;
g = 1'b1;
end
else begin
ns = S3;
a = 1'b1;
b = 1'b1;
c = 1'b1;
d = 1'b1;
e = 1'b0;
f = 1'b0;
g = 1'b1;
end
S5:
if (u==1) begin
ns = S0;
a = 1'b1;
b = 1'b1;
c = 1'b1;
d = 1'b1;
e = 1'b1;
f = 1'b1;
g = 1'b0;
end
else begin
ns = S4;
a = 1'b0;
b = 1'b1;
c = 1'b1;
d = 1'b0;
e = 1'b0;
f = 1'b1;
g = 1'b1;
end
endcase
q = ns;
end
end
endmodule
TESTBENCH:
`timescale 1ns / 1ps
module testbench;
reg U, CLK, CLRN;
wire A, B, C, D, E, F, G;
Counter inst(
.clk (CLK),
.u (U),
.clrn (CLRN),
.a (A),
.b (B),
.c (C),
.d (D),
.e (E),
.f (F),
.g (G));
initial
begin //CLRN starts low, CLK starts high, U starts high
CLRN = 1'b0;
CLK = 1'b1;
U = 1'b1;
//CLK will change every ns
#1 CLRN = 1'b1;
CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0; //On the ns 17 u will change to low
U = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
#1 CLK = 1'b0;
#1 CLK = 1'b1;
end
endmodule
Thank you very much!
The quickest way is to simply change the assignments for a thru g for each state.
In the first case you had this
a = 1'b0;
b = 1'b1;
c = 1'b1;
d = 1'b0;
e = 1'b0;
f = 1'b0;
g = 1'b0;
Change it to this and you will reverse the polarity of the output.
a = 1'b1; // old setting 1'b0;
b = 1'b0; // old setting 1'b1;
c = 1'b0; // old setting 1'b1;
d = 1'b1; // old setting 1'b0;
e = 1'b1; // old setting 1'b0;
f = 1'b1; // old setting 1'b0;
g = 1'b1; // old setting 1'b0;
Alternatively, you can make new registers
reg a_n, b_n, c_n, d_n, e_n, f_n, g_n;
Then go through all the register assignments and change them to these new register names
a_n = 1'b0;
b_n = 1'b1;
c_n = 1'b1;
d_n = 1'b0;
e_n = 1'b0;
f_n = 1'b0;
g_n = 1'b0;
then make a new always block
// Invert the register outputs
always @ (a_n or b_n or c_n or d_n or e_n or f_n or g_n)
begin
a <= ~a_n;
b <= ~b_n;
c <= ~c_n;
d <= ~d_n;
e <= ~e_n;
f <= ~f_n;
g <= ~g_n;
end