comparing t_data and rx_data for errors and giving the error value by total_error "verilog" - verilog

I have made a bit error rate (ber) file in verilog. In ber module, I have made total_error which compares the t_data and rx_data and totals up the number of errors it gets from comparing.
Here is the code
`timescale 1ns / 1ps
module ber
(
clk,
rstn,
T_Data,
RX_Data,
total_error,
enable
);
//inputs
input clk;
input rstn;
input [15 : 0] T_Data;
input [15 : 0] RX_Data;
input enable;
//outputs
output [15:0] total_error;
reg [4:0] i;
reg [15:0] subtotal, next_subtotal;
assign total_error = subtotal;
always #(posedge clk) begin : comb
next_subtotal = 0;
for (i = 0; i < 16; i = i + 1)
begin
if (T_Data[i] != RX_Data[i] )
begin
next_subtotal = next_subtotal + 1;
end
end
end
always #(posedge clk) begin : dff
if (rstn==1'b0)
begin
subtotal <= 7'b0000000;
end else
begin
subtotal <= next_subtotal;
end
end
endmodule
After making the BER file, I made another file known as BER_STATE_MACHINE where I made a state machine for TRANFERRING AND RECEIVING SIGNALS, I instantiated the ber file with this ber_state_machine file.
Here is the code
// --------------------------------------------------------------------
`timescale 1ns/1ps
module ber_state_machine
(
clk,
resetn,
T_Data [15:0],
T_Valid,
T_Ready,
RX_Data [15:0],
RX_Active,
RX_Valid,
total_error
);
//-----------------------------------
input resetn, clk;
// DECLARING INPUTS AND OUTPUTS FOR TRASMIT SIGNALS
input [15 : 0] T_Data;
input T_Valid;
output T_Ready;
// DECLARING INPUTS AND OUTPUTS FOR RECEIVING SIGNALS
input [15 : 0] RX_Data;
input RX_Active;
input RX_Valid;
//-----------------------------------
output [15 : 0] total_error;
//-----------------------------------
reg [6:0] sel;
reg execute_in;
reg T_Ready;
//------------------------------------------
ber uut
(
.clk(clk),
.rstn(resetn),
.T_Data(T_Data),
.RX_Data(RX_Data),
.total_error(total_error),
.enable(execute_in)
);
//------------------------------------------
// MAKING STATE MACHINE HERE //INPUTS
always # (posedge clk or negedge resetn) // state machine for changing states
begin
if (resetn == 1'b1) // idle state
begin
sel <= 7'b000; // state 0
end
else if (T_Valid == 7'b1)
begin
sel <= 7'b001; // state 1
end
else if (sel == 7'b001)
begin
sel <= 7'b010; // state 2
end
else if (RX_Active == 7'b1)
begin
sel <= 7'b011; // state 3
end
else if (T_Valid == 7'b1 && RX_Valid == 7'b1)
begin
sel <= 7'b100; // state 4
end
else if (sel == 7'b100)
begin
sel <= 7'b101; // state 5
end
else if (T_Valid == 2'b0 && RX_Valid == 2'b0)
begin
sel <= 7'b100; // going back to state 4
end
end
// STATE MACHINE //OUTPUTS
always # (posedge clk) // outputs for every state in state diagram
begin
case(sel)
7'b000 :
execute_in = 2'b0; // state 0
7'b001 :
T_Ready = 2'b1; // state 1
7'b010 :
T_Ready = 2'b0; // state 2
7'b011 :
execute_in = 2'b1; // state 3
7'b100 :
T_Ready = 2'b1; // state 4
7'b101 :
T_Ready = 2'b0; // state 5
endcase
end
endmodule
After this, I made a test bech to see the behavioral simulation.
There are few problems that I am getting which I am not able to fix due to less verilog experience.
the t_data and rx_data are undefined in the simulation, after the last bit, I can see the value each contains but from 0 - 15, they are undefined. I really dont know whats the problem.
I dont see any value in total_error, even though I put errors in t_data and rx_data but I dont see the number of errors in total_error. But I can observe in the simulation that there are errors in t_data and rx_data in their final values.
Here is the code for test bench
`timescale 1ns / 1ps
module test_bench();
//inputs
reg execute_in;
reg clk;
reg resetn;
//inputs for transferring signals
reg [15:0] T_Data;
reg T_Valid;
//inputs for receiving signals
reg [15:0] RX_Data;
reg RX_Active;
reg RX_Valid;
//outputs
wire [15:0] total_error;
wire T_Ready;
//instantiate the unit under test (UUT)
ber_state_machine uut_ber
(
.clk(clk),
.resetn(resetn),
.T_Data(T_Data),
.T_Valid(T_Valid),
.T_Ready(T_Ready),
.RX_Data(RX_Data),
.RX_Active(RX_Active),
.RX_Valid(RX_Valid),
.total_error(total_error)
);
initial begin
clk = 1'b0;
resetn = 1'b1;
repeat(4) #10 clk = ~clk;
resetn = 1'b0;
forever #10 clk = ~clk;
end
initial begin
#100
execute_in = 0;
#100
execute_in = 1;
#100
T_Valid = 1'b0;
RX_Active = 1'b0;
#100
RX_Valid = 1'b0;
//************//
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[0] = 1'b0; // data 0 // make it 0 for error
RX_Valid = 1'b1;
RX_Data[0] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
//************//
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[1] = 1'b1; // data 1
RX_Valid = 1'b1;
RX_Data[1] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[2] = 1'b1; // data 2
RX_Valid = 1'b1;
RX_Data[2] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[3] = 1'b0; // data 3
RX_Valid = 1'b1;
RX_Data[3] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[4] = 1'b1; // data 4
RX_Valid = 1'b1;
RX_Data[4] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[5] = 1'b1; // data 5
RX_Valid = 1'b1;
RX_Data[5] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[6] = 1'b1; // data 6
RX_Valid = 1'b1;
RX_Data[6] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[7] = 1'b1; // data 7
RX_Valid = 1'b1;
RX_Data[7] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[8] = 1'b1; // data 8
RX_Valid = 1'b1;
RX_Data[8] = 1'b0; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[9] = 1'b1; // data 9
RX_Valid = 1'b1;
RX_Data[9] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[10] = 1'b1; // data 10
RX_Valid = 1'b1;
RX_Data[10] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[11] = 1'b1; // data 11
RX_Valid = 1'b1;
RX_Data[11] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[12] = 1'b1; // data 12
RX_Valid = 1'b1;
RX_Data[12] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[13] = 1'b1; // data 13
RX_Valid = 1'b1;
RX_Data[13] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[14] = 1'b1; // data 14
RX_Valid = 1'b1;
RX_Data[14] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[15] = 1'b1; // data 15
RX_Valid = 1'b1;
RX_Data[15] = 1'b1; // make it 0 for error
#50
T_Valid = 1'b0;
end
endmodule
Please help me out in this

Your logic uses an active-low reset. However, your testbench starts with the reset signal de-asserted (resetn=1), then asserts it (resetn=0) after 40ns. I think you need to invert the polarity in the testbench:
initial begin
clk = 1'b0;
resetn = 0; // Assert active-low reset
repeat(4) #10 clk = ~clk;
resetn = ~resetn; // De-assert reset
forever #10 clk = ~clk;
end

Related

Verilog Testbench signal value not updating

`timescale 1ns / 1ps
module test_module_t(
input clk_net,
/* Network interfaces */
input [63:0] rx_data_net,
input rx_sof_net,
input rx_eof_net,
input rx_vld_net,
output reg [31:0] port_match=0
);
always #(posedge clk_net) begin
if(rx_vld_net && rx_sof_net)
port_match <= 0;
else if(rx_vld_net)
port_match <= port_match+1;
if (rx_vld_net && rx_eof_net)
port_match <= 0;
end
endmodule
module test_module_tb;
reg clk_net_tb = 0;
reg [63:0] rx_data_net_tb;
reg rx_sof_net_tb;
reg rx_eof_net_tb;
reg rx_vld_net_tb;
wire [31:0] port_match_tb = 0;
integer i;
always #0.5 clk_net_tb = ~clk_net_tb;
initial begin : full_packet
rx_eof_net_tb = 1'b0;
rx_sof_net_tb = 1'b0;
rx_vld_net_tb = 1'b0;
rx_data_net_tb = 64'h0000000000000000;
#10
rx_eof_net_tb = 1'b0;
rx_sof_net_tb = 1'b1;
rx_vld_net_tb = 1'b0;
rx_data_net_tb = 64'h0000a94d00000000;
#1;
rx_vld_net_tb = 1'b1;
#1;
rx_sof_net_tb = 1'b0;
rx_vld_net_tb = 1'b0;
rx_eof_net_tb = 1'b0;
rx_data_net_tb = 64'h00000000002d0000;
#1
rx_vld_net_tb = 1'b1;
#1
rx_sof_net_tb = 1'b0;
rx_vld_net_tb = 1'b0;
rx_data_net_tb = 64'hdeadbeef;
#1
rx_vld_net_tb = 1'b1;
#1
rx_eof_net_tb = 1'b1;
rx_vld_net_tb = 1'b0;
rx_data_net_tb = 64'h000decaf;
#1
rx_vld_net_tb = 1'b1;
#1
rx_eof_net_tb = 1'b0;
rx_vld_net_tb = 1'b0;
rx_data_net_tb = 64'h0000000000000000;
$stop;
end
test_module_t test_module_i
(
.clk_net(clk_net_tb),
.rx_sof_net(rx_sof_net_tb),
.rx_vld_net(rx_vld_net_tb),
.rx_data_net(rx_data_net_tb),
.rx_eof_net(rx_eof_net_tb),
.port_match(port_match_tb)
);
endmodule
This is the code I have been working on. The signal in test_module: port_match updates correctly, but the same signal value is not reflected in the testbench. port_match_tb value goes x when testing.
Attaching a screenshot of the simulation of the above code. If anybody could help me out with where this is going wrong.
The port_match_tb wire has multiple drivers: the wire declaration where you continuously drive it as 0, and the test_module_t output. Change:
wire [31:0] port_match_tb = 0;
to:
wire [31:0] port_match_tb;
This eliminates the x and allows port_match_tb to match port_match.

Delay counter not incrementing? FSM

I was writing a code for DAC register and there is delay required. However, in the delay state, the DAC_counter_2 is not incrementing, and delay is not being achieved. I have copied the code to different project and tried to check. The problem still exists. Any help would be great.
module dac_card
( output reg DAC_SCLK,
output reg SYNC,
output reg SDIN,
input MHZ_50_CLK,
input RST,
output reg [7:0] DAC_counter_1,
output reg [7:0] DAC_counter_2
);
reg [7:0] pst_state, nxt_state;
reg [23:0] DAC_reg;
always #(posedge MHZ_50_CLK)
begin
if (RST)
begin
DAC_reg <= 24'hEEEEEE;
SYNC <= 1'b1;
DAC_SCLK <= 1'b0;
SDIN <=1'b0;
DAC_counter_1 <= 8'd0;
DAC_counter_2 <= 8'd0;
pst_state <= 8'd0;
end
else
begin
pst_state <= nxt_state;
DAC_counter_1 <= DAC_counter_1 + 1'b1;
end
end
always #(pst_state or DAC_counter_2)
begin
case (pst_state)
8'd0 : begin
if (DAC_counter_2 == 8'd24)
begin
DAC_counter_2 = 8'd0;
SYNC = 1'b1;
SDIN = 1'b0;
nxt_state = 8'd2;
end
else
begin
SYNC = 1'b0;
DAC_SCLK = 1'b1;
DAC_counter_2 = DAC_counter_2 + 1'b1;
SDIN = DAC_reg [23]; //Writing DAC register
DAC_reg = DAC_reg << 1;
nxt_state = 8'd1;
end
end
8'd1 : begin
DAC_SCLK = 1'b0;
nxt_state = 8'd0;
end
8'd2 : begin
if (DAC_counter_2 == 8'd10) //Minimum delay for SYNC to be low for write mode
begin
SYNC = 1'b1;
DAC_counter_2 = 8'd0;
nxt_state = 8'd3;
end
else
begin
SYNC = 1'b0;
//Not incrementing
DAC_counter_2 = DAC_counter_2 + 1'b1;
end
end
8'd3 : begin
nxt_state = 8'd0;
end
default : begin
nxt_state = 8'd0;
end
endcase
end
endmodule
Here is the test bench
module test_bench
();
//Analog Card DAC wires and registers
reg MHZ_50_CLK;
reg RST;
wire DAC_SCLK;
wire SYNC;
wire SDIN;
wire [7:0] DAC_counter_1;
wire [7:0] DAC_counter_2;
//Instatntiate DAC
dac_card dc (.DAC_SCLK(DAC_SCLK),
.SYNC(SYNC),
.SDIN(SDIN),
.MHZ_50_CLK(MHZ_50_CLK),
.RST(RST),
.DAC_counter_1(DAC_counter_1),
.DAC_counter_2(DAC_counter_2)
);
initial
begin
MHZ_50_CLK = 1'b0;
#10 RST = 1'b1;
#20 RST = 1'b0;
end
always
begin
#10 MHZ_50_CLK <= ~MHZ_50_CLK;
end
endmodule
Here is the waveform. After 24 counts of DAC_counter_2, delay for 8'd10 is not achieved.
You need to make an assignment to nxt_state in all branches of your case statement. This also avoids inferring unintended latches. For example, refer to the line // MISSING nxt_state = below:
8'd2 : begin
if (DAC_counter_2 == 8'd10) //Minimum delay for SYNC to be low for write mode
begin
SYNC = 1'b1;
DAC_counter_2 = 8'd0;
nxt_state = 8'd3;
end
else
begin
SYNC = 1'b0;
//Not incrementing
DAC_counter_2 = DAC_counter_2 + 1'b1;
// MISSING nxt_state =
end
end
As your waves show, once you enter state 2, you remain in state 2. Since DAC_counter_2 is not 10 in state 2, you always execute the else clause, which does not change nxt_state.
There are a couple other issues which may also be causing problems.
Good coding practices recommend making assignments to a reg from a single always block. DAC_counter_2 is assigned in 2 different blocks.
Also, the same signal should not appear on both the LHS and RHS of an assignment in a combinational always block since it creates a feedback loop. For example, DAC_counter_2 = DAC_counter_2 + 1 should probably be in a sequential always block like DAC_counter_1.

How to make led active low on vivado

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

Undefined Data Packets (verilog) seen in behavioral simulation

I have been working on this code from more than week now and there is a problem which I have not been able to solve. Basically I am making 2 data patterns, one is T_DATA and the other is RX_Data. I am comparing both patterns and get a result after comparing in the form of total_error.
Here is the code
`timescale 1ns / 1ps
module BER
(
clk,
rstn,
T_Data,
RX_Data,
enable,
total_error
);
input clk;
input rstn;
input [15:0] T_Data;
input [15:0] RX_Data;
input enable;
output [15:0] total_error;
reg [4:0] i;
reg [15:0] subtotal, next_subtotal;
assign total_error = subtotal;
always # (posedge clk) begin: comb
next_subtotal = 0;
for (i = 0; i < 16; i = i +1)
begin
if (T_Data[i] != RX_Data[i])
begin
next_subtotal = next_subtotal + 1;
end
end
end
always # (posedge clk) begin: dff
if (rstn == 1'b0) begin
subtotal <= 16'b0000000000000000;
end else
begin
subtotal <= next_subtotal;
end
end
endmodule
Above is the code of module ber, in the next module, I made state machine that basically controls the transfer and receiving signals. The above module BER has been instantiated with StateMachine_BER module. Please see the code below
`timescale 1ns / 1ps
module StateMachine_BER
(
clk,
resetn, //negetive edge reset
T_Data [15:0],
T_Valid,
T_Ready,
RX_Data [15:0],
RX_Active,
RX_Valid,
total_error
);
//-----------------------//
input clk;
input resetn;
//-----------------------//
//declaring input and output for transferring signals
input [15:0] T_Data;
input T_Valid;
output T_Ready;
//declaring inputs and outputs for receiving signals
input [15:0] RX_Data;
input RX_Active;
input RX_Valid;
//-----------------------//
output [15:0] total_error;
//-----------------------//
//declaring registers to use them for procedural assignments
//-----------------------//
reg [6:0] sel;
reg execute_in;
reg T_Ready;
//-----------------------//
//instantiating ber module here
BER uut
(
.clk(clk),
.rstn(resetn),
.T_Data(T_Data),
.RX_Data(RX_Data),
.enable(execute_in),
.total_error(total_error)
);
//-----------------------//
//making state machine here
always # (posedge clk or negedge resetn)
begin
if (resetn == 1'b0) //idle state
begin
sel <= 7'b0000000; //state 0
end
else if (T_Valid == 7'b0000001)
begin
sel <= 7'b0000001; //state 1
end
else if (sel == 7'b0000001)
begin
sel <= 7'b0000010; //state 2
end
else if (RX_Active == 7'b0000001)
begin
sel <= 7'b0000011; //state 3
end
else if (T_Valid == 7'b0000001 && RX_Valid == 7'b0000001)
begin
sel <= 7'b0000100; //state 4
end
else if (sel == 7'b0000100)
begin
sel <= 7'b0000101; //state 5
end
else if (T_Valid == 7'b0000000 && RX_Valid == 7'b0000000)
begin
sel <= 7'b0000100; //goes back to state 4
end
end
//making outputs for state machine
always # (posedge clk)
begin
case(sel)
7'b0000000 :
execute_in = 1'b0; //state 0
7'b0000001 :
T_Ready = 1'b1; //state 1
7'b0000010 :
T_Ready = 1'b0; //state 2
7'b0000011 :
execute_in = 1'b1; //state 3
7'b0000100 :
T_Ready = 1'b1; //state 4
7'b0000101 :
T_Ready = 1'b0; //state 5
endcase
end
endmodule
Here is the test bench for above modules.
`timescale 1ns / 1ps
module TB_BER();
//inputs
reg clk;
reg resetn;
reg execute_in;
//-----------------------//
//inputs for trasnferring signals
reg [15:0] T_Data;
reg T_Valid;
//-----------------------//
//inputs for receiving signals
reg [15:0] RX_Data;
reg RX_Active;
reg RX_Valid;
//-----------------------//
//outputs
wire [15:0] total_error;
wire T_Ready;
//-----------------------//
//instantiate the unit under test (UUT)
StateMachine_BER uut_BER
(
.clk(clk),
.resetn(resetn), //negetive edge reset
.T_Data(T_Data),
.T_Valid(T_Valid),
.T_Ready(T_Ready),
.RX_Data(RX_Data),
.RX_Active(RX_Active),
.RX_Valid(RX_Valid),
.total_error(total_error)
);
//-----------------------//
initial begin
clk = 1'b0;
resetn = 1'b0;
#50
resetn = 1'b1;
forever #10 clk = ~clk;
end
//-----------------------//
initial begin
#100
execute_in = 0;
#100
execute_in = 1;
//-----------------------//
#100
T_Valid = 1'b0;
RX_Active = 1'b0;
#100
RX_Valid = 1'b0;
//-----------------------//
// for T_Data: 0 and RX_Data: 0
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[0] = 1'b1; //data 0 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[0] = 1'b1; //data 0 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 1 and RX_Data: 1
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[1] = 1'b1; //data 1 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[1] = 1'b1; //data 1 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 2 and RX_Data: 2
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[2] = 1'b1; //data 2 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[2] = 1'b1; //data 2 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 3 and RX_Data: 3
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[3] = 1'b1; //data 3 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[3] = 1'b1; //data 3 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 4 and RX_Data: 4
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[4] = 1'b1; //data 4 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[4] = 1'b1; //data 4 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 5 and RX_Data: 5
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[5] = 1'b1; //data 5 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[5] = 1'b1; //data 5 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 6 and RX_Data: 6
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[6] = 1'b1; //data 6 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[6] = 1'b1; //data 6 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 7 and RX_Data: 7
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[7] = 1'b1; //data 7 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[7] = 1'b1; //data 7 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 8 and RX_Data: 8
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[8] = 1'b0; //data 8 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[8] = 1'b1; //data 8 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 9 and RX_Data: 9
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[9] = 1'b1; //data 9 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[9] = 1'b0; //data 9 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 10 and RX_Data: 10
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[10] = 1'b0; //data 10 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[10] = 1'b1; //data 10 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 11 and RX_Data: 11
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[11] = 1'b1; //data 11 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[11] = 1'b0; //data 11 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 12 and RX_Data: 12
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[12] = 1'b1; //data 12 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[12] = 1'b0; //data 12 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 13 and RX_Data: 13
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[13] = 1'b0; //data 13 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[13] = 1'b1; //data 13 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 14 and RX_Data: 14
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[14] = 1'b0; //data 14 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[14] = 1'b1; //data 14 //make it 0 for getting an error
#50
T_Valid = 1'b0;
//-----------------------//
// for T_Data: 15 and RX_Data: 15
#100
RX_Active = 1'b1;
T_Valid = 1'b1;
T_Data[15] = 1'b1; //data 15 //make it 0 for getting an error
RX_Valid = 1'b1;
RX_Data[15] = 1'b0; //data 15 //make it 0 for getting an error
#50
T_Valid = 1'b0;
end
endmodule
I am using VIVADO 2014.3 software.
After running bhavioral simulation, it starts from 0 ns, I keep it running for 50 ns. During this, the data transmission takes place, T_Data and RX_Data are 16 bit datas. So the transmisison takes place from 0 till 15. I am expecting every data bit to have a value, but that I dont see. I only see the final values of T_Data and RX_Data after the 16 bit data trasnmission is complete. Throughout the trasnmission, both the datas are undefined in red having value X. But after 16 bits, when the process is complete, I can see the final data values.
Strange thing is during data trasnmission, even though they are undefined but I can see the value at total_error changing, which means that there is some value in those undefined datas. I really dont understand how to fix this issue. Need help.
Please help me with this, I am new to verilog, I have not been able to figure it out on my own yet.
The reason you are seeing the red X's during simulation is that you never initialize the value of T_Data or RX_Data at the start of simulation. As these are declared reg [15:0], they will begin with the value of 16'bXXXX (ie, all bits 1'bx for dont care). Many data types in Verilog being in that state to illustrate the unknown state of the system at the beginning of time. You'll need to have all your inputs set to some default value (probably 0) at the start of your simulation, so you can add them to the start of your stimulus initial block:
//-----------------------//
initial begin
// Set all inputs to 0
T_data = 16'b0;
RX_data = 16'b0;
T_Valid = 1'b0;
RX_Valid = 1'b0;
RX_Active = 1'b0;
#100
execute_in = 0;
...
That should help clear up your error. However, as you are new to Verilog, there are a few other mistakes you want want to fix:
Your combinational logic are triggered at the clock, when they should be triggered any time any of the "input" signals change, so any always block in your code using blocking assignment = should be always #(*) instead of always #(posedge clk) such as the blocks determining T_Ready and next_subtotal. The subtotal and sel blocks use always #(posedge clk) correctly.
For your combinational logic determining T_ready, the value of T_ready is not determined for all values of sel. For example, what should T_ready be is sel is 7'd3? You need to be sure any values determined by combinational logic are defined for all values of the inputs.
As not all your flip-flops are reset asynchronously, you need to be sure to run the clock while reset is begin asserted. Your initial block for initial reset and clocking does not do this.

hdl verilog Compiler Errors

When I first tried to compile my code, I only had syntax errors and was able to fix them. Now I have errors that I cannot figure out at all. I don't know how to fix.
Here is my current code:
module p_5 (output y_out, input x_in, clk, reset_b);
parameter s_a = 2'd0;
parameter s_b = 2'd1;
parameter s_c = 2'd2;
reg Set_flag;
reg Clr_flag;
reg [1:0] state, next_state;
assign y_out = (state == s_b) || (state == s_c) ;
always # (posedge clk)
if (reset_b == 1'b0) state <= s_a;
else state <= next_state;
always # (state, x_in, flag) begin
next_state = s_a;
Set_flag = 0;
Clr_flag = 0;
case (state)
s_a: if ((x_in == 1'b1) && (flag == 1'b0))
begin next_state = s_a; Set_flag = 1; end
else if ((x_in == 1'b1) && (flag == 1'b1))
begin next_state = s_b; Set_flag = 0; end
else if (x_in == 1'b0) next_state = s_a;
s_b: if (x_in == 1'b0) next_state = s_b;
else begin next_state = s_c; Clr_flag = 1; end
s_c: if (x_in == 1'b0) next_state = s_c;
else next_state = s_a;
default: begin next_state = s_a; Clr_flag = 1'b0; Set_flag = 1'b0; end
endcase
end
always # (posedge clk)
if (reset_b == 1'b0) flag <= 0;
else if (Set_flag) flag <= 1'b1;
else if (Clr_flag) flag <= 1'b0;
endmodule
This is the test bench:
module test_5 ();
wire y_out;
reg x_in, clk, flag, reset_b;
p_5 M0 (y_out, x_in, clk, reset_b);
initial #500 $finish;
initial begin clk = 0; forever #5 clk = !clk; end
initial fork
reset_b = 1'b0;
#20 reset_b = 1;
#20 x_in = 1'b0;
#40 x_in = 1'b1;
#50 x_in = 1'b0;
#80 x_in = 1'b1;
#100 x_in = 0;
#150 x_in = 1'b1;
#160 x_in = 1'b0;
#200 x_in = 1'b1;
#230 reset_b = 1'b0;
#250 reset_b = 1'b1;
#300 x_in = 1'b0;
#300 flag = 1'b0;
join
endmodule
Errors:
p5.v:22: error: Unable to bind wire/reg/memory `flag' in `t_ques_5_50.M0'
p5.v:22: error: Unable to elaborate condition expression.
p5.v:17: error: Unable to bind wire/reg/memory `flag' in `t_ques_5_50.M0'
flag
p5.v:36: error: Could not find variable ``flag'' in ``t_ques_5_50.M0''
p5.v:37: error: Could not find variable ``flag'' in ``t_ques_5_50.M0''
p5.v:38: error: Could not find variable ``flag'' in ``t_ques_5_50.M0''
7 error(s) during elaboration.
You refer to the value flag repeatedly inside p5.v, yet it is not declared anywhere as an input, reg, or wire.
Add the appropriate declaration and it should be resolved.

Resources