-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_i2sclock.v
185 lines (158 loc) · 5.33 KB
/
test_i2sclock.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/************************************************************************************************************
TEST BENCH FOR I2C to GPIO Port expander. Date: December 23, 2006.
*********************************************************************************************************** */
`timescale 1us/10ns
module test_i2sclock;
// registers and wires
reg mclk;
wire bck;
wire lrck;
wire oscillator_22;
wire oscillator_24;
reg sclk;
reg start_t;
reg sda_in;
reg ack_check;
reg GPIO_input_check;
reg read_oper_check;
reg write_oper_check;
reg slave_add_check;
wand sda;
wire [3:0] count;
// Linking module under test
i2sclock test ( mclk, bck, lrck, oscillator_22, oscillator_24, sda, sclk);
reg[7:0] GPIO_input_store; //Stores the data from GPIO input line which master wants to read
reg[7:0] GPIO_output_send; //Stores the data which master wants to write at GPIO output line
reg[6:0] slave_add_compare; //For random slave address
integer i = 0;
integer r_seed; // Seed ensures that the same random sequence is generated during every simulation
parameter tdelay = 3.5 ;
parameter testcycle = 100.0;
assign sda = sda_in;
initial
begin
$dumpfile("test_I2C_to_GPIO.dmp");
$dumpvars;
end
initial // Generates serial clock of time period 10
begin
sclk = 0;
forever #5 sclk = !sclk;
end
initial
begin
mclk = 0;
forever #1 mclk = !mclk;
end
initial
r_seed =2; // Arbitrarily define the seed as 2
always @(posedge sclk) // Test that slave acknowledges only the correct address
begin
if (slave_add_check & (slave_add_compare != 0000000) & ~sda) begin
$display("PASS: I2C address check successful ", $time);
end else if (slave_add_check) begin
$display(" Slave acknowledging wrong address at time %d", $time);
end
if (sda & ack_check) begin
$display(" Acknowledge Fail at time %d", $time);
end else if (ack_check) begin
$display("Acknowledge recieved correctly %d", $time);
end
/*
if (write_oper_check & (GPIO_output == GPIO_output_send)) begin
$display ("PASS: I2C Write successful ", $time);
end else if (write_oper_check) begin
$display ("Write Failed !" , $time);
end
if (read_oper_check & (GPIO_input == GPIO_input_store)) begin
$display ("PASS: I2C Read successful ", $time);
end else if (read_oper_check) begin
$display ("I2C Read Failed !" , $time);
end
*/
end
initial
begin
@(negedge sclk)
$display("Testing for randoms values");
#500 ;
#tdelay sda_in = 1;
#tdelay sda_in = 0; // For start
// Generate random slave address
slave_add_compare = $random(r_seed);
//slave_add_compare = $random(r_seed);
slave_add_compare = 7'h41;
#tdelay sda_in <= slave_add_compare[6];
#10 sda_in <= slave_add_compare[5];
#10 sda_in <= slave_add_compare[4];
#10 sda_in <= slave_add_compare[3];
#10 sda_in <= slave_add_compare[2];
#10 sda_in <= slave_add_compare[1];
#10 sda_in <= slave_add_compare[0];
#10 sda_in <=1; //For read operation
#10 slave_add_check <= 1;
#10 slave_add_check <= 0;
# testcycle
@ (negedge sclk) //For repeat start
$display ("Starting a write operation", $time);
#tdelay sda_in = 1;
#tdelay sda_in = 0;
#tdelay sda_in <= slave_add_compare[6];
#10 sda_in <= slave_add_compare[5];
#10 sda_in <= slave_add_compare[4];
#10 sda_in <= slave_add_compare[3];
#10 sda_in <= slave_add_compare[2];
#10 sda_in <= slave_add_compare[1];
#10 sda_in <= slave_add_compare[0];
#10 sda_in = 0; // For write operation
#10 sda_in = 1;
ack_check <=1;
//For storing a random data which master writes on GPIO output
// GPIO_output_send <= $random(r_seed);
GPIO_output_send <= 8'b00000011;
#10 ack_check <= 0;
sda_in <= GPIO_output_send[7];
#10 sda_in <= GPIO_output_send[6];
#10 sda_in <= GPIO_output_send[5];
#10 sda_in <= GPIO_output_send[4];
#10 sda_in <= GPIO_output_send[3];
#10 sda_in <= GPIO_output_send[2];
#10 sda_in <= GPIO_output_send[1];
#10 sda_in <= GPIO_output_send[0];
# 10 sda_in <= 1;
write_oper_check <= 1;
# 10 write_oper_check <=0 ;
# testcycle
$display("starting a read operation", $time);
@ (negedge sclk) //For repeat start
#tdelay sda_in= 1;
#tdelay sda_in= 0; //Sending slave address=0000000
#tdelay sda_in <= slave_add_compare[6];
#10 sda_in <= slave_add_compare[5];
#10 sda_in <= slave_add_compare[4];
#10 sda_in <= slave_add_compare[3];
#10 sda_in <= slave_add_compare[2];
#10 sda_in <= slave_add_compare[1];
#10 sda_in <= slave_add_compare[0];
#10 sda_in <=1; //For read operation
#5 ack_check <= 1;
#10 ack_check <= 0;
//For Storing the data from sda line which master is reading
#10 GPIO_input_store[7] <= sda;
#10 GPIO_input_store[6] <= sda;
#10 GPIO_input_store[5] <= sda;
#10 GPIO_input_store[4] <= sda;
#10 GPIO_input_store[3] <= sda;
#10 GPIO_input_store[2] <= sda;
#10 GPIO_input_store[1] <= sda;
#10 GPIO_input_store[0] <= sda;
#10 read_oper_check <= 1;
#10 read_oper_check <= 0;
# testcycle
@ (negedge sclk) //For stop
#tdelay sda_in = 0;
#tdelay sda_in = 1;
$display (" End of testcycle, for another random check, run again ");
#500 $stop;
end
endmodule