~ruther/vhdl-i2c

ref: cb7143c0b0bf4ac603f4e0ddf705e88474afc496 vhdl-i2c/tb/i2c/model/i2c_bus_pkg.vhd -rw-r--r-- 10.2 KiB
cb7143c0 — Rutherther tests: i2c bus model 1 year, 3 months ago
                                                                                
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
library ieee;
use ieee.std_logic_1164.all;

library vunit_lib;
context vunit_lib.vunit_context;
context vunit_lib.com_context;

-- except for wait_until_idle,
-- the procedures take 0 simulation time.
-- wait_until_idle will take time until all
-- operations requested on the i2c were
-- performed.

package i2c_bus_pkg is
  constant free_bus_msg : msg_type_t := new_msg_type("free bus");
  constant set_scl_freq_msg : msg_type_t := new_msg_type("scl freq");
  constant gen_start_cond_msg : msg_type_t := new_msg_type("gen start cond");
  constant gen_stop_cond_msg : msg_type_t := new_msg_type("gen stop cond");
  constant gen_clocks_msg : msg_type_t := new_msg_type("gen clocks");
  constant send_data_msg : msg_type_t := new_msg_type("send data");
  constant send_data_clocks_msg : msg_type_t := new_msg_type("send data and clocks");

  constant auto_ack_msg : msg_type_t := new_msg_type("auto acknowledge");

  constant wait_start_cond_msg : msg_type_t := new_msg_type("wait start cond");
  constant wait_stop_cond_msg : msg_type_t := new_msg_type("wait stop cond");
  constant wait_clocks_msg : msg_type_t := new_msg_type("wait clocks");

  constant check_data_msg : msg_type_t := new_msg_type("check data");
  constant check_data_clocks_msg : msg_type_t := new_msg_type("check data and gen clocks");

  constant wait_until_idle_msg : msg_type_t := new_msg_type("wait until idle");

  impure function get_actor (
    constant inst_name : string)
    return actor_t;

  procedure free_bus (
    signal net : inout network_t;
    constant actor : in actor_t);

  procedure set_scl_frequency (
    signal net : inout network_t;
    constant frequency : in real;
    constant actor : in actor_t);

  procedure gen_start_cond (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure gen_stop_cond (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure gen_clocks (
    signal net : inout network_t;
    constant times : in natural;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure send_data (
    signal net : inout network_t;
    constant data : in std_logic_vector;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure send_ack (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure send_ack_and_clock (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure send_data_and_clock (
    signal net : inout network_t;
    constant data : in std_logic_vector;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure wait_for_start_cond (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure wait_for_stop_cond (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure wait_for_clocks (
    signal net : inout network_t;
    constant times : in natural;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure check_data (
    signal net : inout network_t;
    constant exp_data : in std_logic_vector;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure check_data_gen_clock (
    signal net : inout network_t;
    constant exp_data : in std_logic_vector;
    constant timeout : in time;
    constant actor : in actor_t);

  procedure check_ack_gen_clock (
    signal net : inout network_t;
    constant ack : in std_logic := '1';
    constant timeout : in time;
    constant actor : in actor_t);

  procedure check_ack (
    signal net : inout network_t;
    constant ack : in std_logic := '1';
    constant timeout : in time;
    constant actor : in actor_t);

  procedure set_auto_ack (
    signal net : inout network_t;
    constant auto_ack : in boolean;
    constant address : in std_logic_vector(6 downto 0);
    constant bytes_count : in natural;
    constant actor : in actor_t);

  procedure wait_until_idle (
    signal net : inout network_t;
    constant actor : in actor_t);

end package i2c_bus_pkg;

package body i2c_bus_pkg is

  impure function get_actor (
    constant inst_name : string)
    return actor_t is
  begin
    return find(inst_name);
  end function get_actor;

  procedure free_bus (
    signal net : inout network_t;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(free_bus_msg);
  begin
    send(net, actor, msg);
  end procedure free_bus;

  procedure set_scl_frequency (
    signal net : inout network_t;
    constant frequency : in real;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(set_scl_freq_msg);
  begin
    push(msg, frequency);
    send(net, actor, msg);
  end procedure set_scl_frequency;

  procedure gen_start_cond (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(gen_start_cond_msg);
  begin
    push(msg, timeout);
    send(net, actor, msg);
  end procedure gen_start_cond;

  procedure gen_stop_cond (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(gen_stop_cond_msg);
  begin
    push(msg, timeout);
    send(net, actor, msg);
  end procedure gen_stop_cond;

  procedure gen_clocks (
    signal net : inout network_t;
    constant times : in natural;
    constant timeout : in time;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(gen_clocks_msg);
  begin
    push(msg, times);
    push(msg, timeout);
    send(net, actor, msg);
  end procedure gen_clocks;

  procedure send_data (
    signal net : inout network_t;
    constant data : in std_logic_vector;
    constant timeout : in time;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(send_data_msg);
    variable msg_data : std_logic_vector(1023 downto 0);
  begin
    msg_data(data'length - 1 downto 0) := data(data'range);
    push(msg, data'length);
    push(msg, msg_data);
    push(msg, timeout);
    send(net, actor, msg);
  end procedure send_data;

  procedure send_ack (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t) is
  begin
    send_data(net, "0", timeout, actor);
  end procedure send_ack;

  procedure send_ack_and_clock (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t) is
  begin
    send_data_and_clock(net, "0", timeout, actor);
  end procedure send_ack_and_clock;

  procedure send_data_and_clock (
    signal net : inout network_t;
    constant data : in std_logic_vector;
    constant timeout : in time;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(send_data_clocks_msg);
    variable msg_data : std_logic_vector(1023 downto 0);
  begin
    msg_data(data'length - 1 downto 0) := data(data'range);
    push(msg, data'length);
    push(msg, msg_data);
    push(msg, timeout);
    send(net, actor, msg);
  end procedure send_data_and_clock;

  procedure wait_for_start_cond (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(wait_start_cond_msg);
  begin
    push(msg, timeout);
    send(net, actor, msg);
  end procedure wait_for_start_cond;

  procedure wait_for_stop_cond (
    signal net : inout network_t;
    constant timeout : in time;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(wait_stop_cond_msg);
  begin
    push(msg, timeout);
    send(net, actor, msg);
  end procedure wait_for_stop_cond;

  procedure wait_for_clocks (
    signal net : inout network_t;
    constant times : in natural;
    constant timeout : in time;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(wait_clocks_msg);
  begin
    push(msg, times);
    push(msg, timeout);
    send(net, actor, msg);
  end procedure wait_for_clocks;

  procedure check_data (
    signal net : inout network_t;
    constant exp_data : in std_logic_vector;
    constant timeout : in time;
    constant actor : in actor_t) is
    variable msg_data : std_logic_vector(1023 downto 0);
    variable msg : msg_t := new_msg(check_data_msg);
  begin
    msg_data(exp_data'length - 1 downto 0) := exp_data(exp_data'range);
    push(msg, exp_data'length);
    push(msg, msg_data);
    push(msg, timeout);
    send(net, actor, msg);
  end procedure check_data;

  procedure check_data_gen_clock (
    signal net : inout network_t;
    constant exp_data : in std_logic_vector;
    constant timeout : in time;
    constant actor : in actor_t) is
    variable msg_data : std_logic_vector(1023 downto 0);
    variable msg : msg_t := new_msg(check_data_clocks_msg);
  begin
    msg_data(exp_data'length - 1 downto 0) := exp_data(exp_data'range);
    push(msg, exp_data'length);
    push(msg, msg_data);
    push(msg, timeout);
    send(net, actor, msg);
  end procedure check_data_gen_clock;

  procedure check_ack_gen_clock (
    signal net : inout network_t;
    constant ack : in std_logic := '1';
    constant timeout : in time;
    constant actor : in actor_t) is
    variable v_vector : std_logic_vector(0 downto 0) := (0 => not ack);
  begin
    check_data_gen_clock(net, v_vector, timeout, actor);
  end procedure check_ack_gen_clock;

  procedure check_ack (
    signal net : inout network_t;
    constant ack : in std_logic := '1';
    constant timeout : in time;
    constant actor : in actor_t) is
    variable v_vector : std_logic_vector(0 downto 0) := (0 => not ack);
  begin
    check_data(net, v_vector, timeout, actor);
  end procedure check_ack;

  procedure set_auto_ack (
    signal net : inout network_t;
    constant auto_ack : in boolean;
    constant address : in std_logic_vector(6 downto 0);
    constant bytes_count : in natural;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(auto_ack_msg);
  begin
    push(msg, auto_ack);
    push(msg, address);
    push(msg, bytes_count);
    send(net, actor, msg);
  end procedure set_auto_ack;

  procedure wait_until_idle (
    signal net : inout network_t;
    constant actor : in actor_t) is
    variable msg : msg_t := new_msg(wait_until_idle_msg);
    variable ack : boolean;
  begin
    request(net, actor, msg, ack);
  end procedure wait_until_idle;

end package body i2c_bus_pkg;
Do not follow this link