-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2C_tb.v
174 lines (138 loc) · 3.57 KB
/
I2C_tb.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
`timescale 1ns/1ns
`define CLK_PERIOD 20
//仿真模型选择
`define TEST_M24LC64 //24LC64
//`define TEST_M24LC04 //24LC04
module I2C_tb;
reg Clk; //系统时钟
reg Rst_n; //系统复位信号
reg [15:0] Word_addr; //I2C器件寄存器地址
reg Wr; //I2C器件写使能
reg [7:0] Wr_data; //I2C器件写数据
wire Wr_data_vaild;//I2C器件写数据有效标志位
reg Rd; //I2C器件读使能
wire[7:0] Rd_data; //I2C器件读数据
wire Rd_data_vaild;//I2C器件读数据有效标志位
wire Scl; //I2C时钟线
wire Sda; //I2C数据线
wire Done; //对I2C器件读写完成标识位
localparam NUM = 6'd4; //单次读写数据字节数
`ifdef TEST_M24LC64
localparam DevAddr = 3'b000; //I2C器件的器件地址
localparam WdAr_NUM= 2'd2; //I2C器件的存储器地址字节数
`elsif TEST_M24LC04
localparam DevAddr = 3'b001; //I2C器件的器件地址
localparam WdAr_NUM= 2'd1; //I2C器件的存储器地址字节数
`endif
iic_control iic_control_inst_0
(
.sys_clk (Clk ),
.Rst_n (Rst_n),
.Device_addr (DevAddr ),
.Word_addr (Word_addr),
.wr_en (Wr ),
.rd_en (Rd ),
.Wd_addr_long (WdAr_NUM ),
.wr_data (Wr_data ),
.wr_data_long (NUM ),
.rd_data_long (NUM ),
.wr_data_done (Wr_data_vaild),
.r_rd_data_done (Rd_data_vaild),
.iic_busy_done (Done ),
.iic_scl (Scl ),
.rd_data_out (Rd_data),
.iic_sda (Sda )
);
`ifdef TEST_M24LC64
M24LC64 M24LC64(
.A0(1'b0),
.A1(1'b0),
.A2(1'b0),
.WP(1'b0),
.SDA(Sda),
.SCL(Scl),
.RESET(!Rst_n)
);
`elsif TEST_M24LC04
M24LC04B M24LC04(
.A0(1'b1),
.A1(1'b0),
.A2(1'b0),
.WP(1'b0),
.SDA(Sda),
.SCL(Scl),
.RESET(!Rst_n)
);
`endif
//系统时钟产生
initial Clk = 1'b1;
always #(`CLK_PERIOD/2)Clk = ~Clk;
initial
begin
Rst_n = 0;
Word_addr = 0;
Wr = 0;
Wr_data = 0;
Rd = 0;
#(`CLK_PERIOD*200 + 1)
Rst_n = 1;
#200;
`ifdef TEST_M24LC64 //仿真验证24LC64模型
//写入20组数据
Word_addr = 0;
Wr_data = 0;
repeat(20)begin
Wr = 1'b1;
#(`CLK_PERIOD);
Wr = 1'b0;
repeat(NUM)begin //在写数据有效前给待写入数据
@(posedge Wr_data_vaild)
Wr_data = Wr_data + 1;
end
@(posedge Done);
#2000;
Word_addr = Word_addr + NUM;
end
#2000;
//读出刚写入的20组数据
Word_addr = 0;
repeat(20)begin
Rd = 1'b1;
#(`CLK_PERIOD);
Rd = 1'b0;
@(posedge Done);
#2000;
Word_addr = Word_addr + NUM;
end
`elsif TEST_M24LC04 //仿真验证24LC64模型
//写入20组数据
Word_addr = 100;
Wr_data = 100;
repeat(20)begin
Wr = 1'b1;
#(`CLK_PERIOD);
Wr = 1'b0;
repeat(NUM)begin //在写数据有效前给待写入数据
@(posedge Wr_data_vaild)
Wr_data = Wr_data + 1;
end
@(posedge Done);
#2000;
Word_addr = Word_addr + NUM;
end
#2000;
//读出刚写入的20组数据
Word_addr = 100;
repeat(20)begin
Rd = 1'b1;
#(`CLK_PERIOD);
Rd = 1'b0;
@(posedge Done);
#2000;
Word_addr = Word_addr + NUM;
end
`endif
#5000;
$stop;
end
endmodule