-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmod_functionality_test.c
189 lines (148 loc) · 6.3 KB
/
cmod_functionality_test.c
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
// Copyright lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
#include "sw/device/lib/base/mmio.h"
#include "sw/device/lib/dif/dif_cmod.h"
#include "sw/device/lib/runtime/ibex.h"
#include "sw/device/lib/testing/test_framework/check.h"
#include "sw/device/lib/testing/test_framework/ottf_main.h"
#include "cmod_regs.h" // Generated
#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
#define TIMEOUT (1000 * 1000)
#define WAIT_FOR_STATUS(cmod_, flag_, value_, timeout_usec_) \
IBEX_SPIN_FOR(cmod_get_status((cmod_), (flag_)) == (value_), (timeout_usec_))
static const uint32_t data[] = {
0x01234567, 0x89abcdef, 0xfedcab98, 0x76543210, 0x00000000, 0x11111111,
0x22222222, 0x33333333, 0x11111111, 0x00000000, 0x00000000, 0x11111111,
0x12121212, 0x21212121, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x23232323, 0x32323232, 0x01234567, 0x89abcdef, 0xfedcab98, 0x76543210,
0x00000000, 0x11111111, 0x22222222, 0x33333333, 0x11111111, 0x00000000,
0x00000000, 0x11111111,
};
OTTF_DEFINE_TEST_CONFIG();
inline bool cmod_get_status(dif_cmod_t *cmod, dif_cmod_status_t flag) {
bool status;
CHECK_DIF_OK(dif_cmod_get_status(cmod, flag, &status));
return status;
}
uint32_t get_status_register(dif_cmod_t *cmod) {
return mmio_region_read32(cmod->base_addr, CMOD_STATUS_REG_OFFSET);
}
bool test_main(void) {
// TODO: add comments about which edge case is tested
dif_cmod_t sender, receiver;
dif_cmod_data_t dataOut, dataIn;
// Initialize CMOD IP cores
CHECK_DIF_OK(dif_cmod_init(
mmio_region_from_addr(TOP_EARLGREY_CMOD0_BASE_ADDR), &sender));
CHECK_DIF_OK(dif_cmod_init(
mmio_region_from_addr(TOP_EARLGREY_CMOD1_BASE_ADDR), &receiver));
// Trigger the sending of a new message.
CHECK_DIF_OK(dif_cmod_txtrigger(&sender));
// Wait until CMOD IP core is ready.
WAIT_FOR_STATUS(&sender, kDifCmodStatusTx, true, TIMEOUT);
WAIT_FOR_STATUS(&sender, kDifCmodStatusTxinputReady, true, TIMEOUT);
// Check if bits of status register are correctly set.
CHECK(get_status_register(&sender) == 0b0000000011001);
// Send 128 bits
memcpy(dataOut.data, data, sizeof(dataOut.data));
CHECK_DIF_OK(dif_cmod_load_data(&sender, dataOut));
// Wait until data is received, then check status registers.
WAIT_FOR_STATUS(&receiver, kDifCmodStatusRxvalid, true, TIMEOUT);
CHECK(get_status_register(&sender) == 0b0000000011001);
CHECK(get_status_register(&receiver) == 0b0001000101000);
// Read data, check status register, and data afterward.
CHECK_DIF_OK(dif_cmod_read_data(&receiver, &dataIn));
CHECK(get_status_register(&receiver) == 0b0000000001000);
CHECK_ARRAYS_EQ(dataIn.data, data, 4);
for (int i = 0; i < 8; i++) {
// Send 128 bits
memcpy(dataOut.data, &data[i * 4], sizeof(dataOut.data));
CHECK_DIF_OK(dif_cmod_load_data(&sender, dataOut));
// Check status registers
if (i < 4) {
uint32_t value = 0b0000000101000;
// Set RXLVL field and RXFULL bit dynamically
value += (i + 1) << 9;
value += (i == 3) << 2;
CHECK(get_status_register(&receiver) == value);
} else {
uint32_t value = 0b0100000101100;
CHECK(get_status_register(&receiver) == value);
value = 0b1;
// Set TXLVL field and TXFULL bit dynamically
value += (i == 7) << 1;
value += (i != 7) << 4;
value += (i - 3) << 6;
CHECK(get_status_register(&sender) == value);
}
}
// Set TXEND and check status register
CHECK_DIF_OK(dif_cmod_txend(&sender));
CHECK(get_status_register(&sender) == 0b0000100000011);
for (int i = 0; i < 5; i++) {
// Read 128 bit and check the data.
CHECK_DIF_OK(dif_cmod_read_data(&receiver, &dataIn));
CHECK(dataIn.data[0] == data[i * 4 + 0] &&
dataIn.data[1] == data[i * 4 + 1] &&
dataIn.data[2] == data[i * 4 + 2] &&
dataIn.data[3] == data[i * 4 + 3]);
// Check status registers
if (i == 4) {
CHECK(get_status_register(&receiver) == 0b0011000101000);
CHECK(get_status_register(&sender) == 0b0000000001000);
} else {
CHECK(get_status_register(&receiver) == 0b0100000101100);
uint32_t value = 0b0;
value += (i != 3);
value += (i == 3) << 3;
value += (3 - i) << 6;
CHECK(get_status_register(&sender) == value);
}
}
// Trigger the sending of a new message.
CHECK_DIF_OK(dif_cmod_txtrigger(&sender));
// Wait until CMOD IP core is ready.
WAIT_FOR_STATUS(&sender, kDifCmodStatusTx, true, TIMEOUT);
WAIT_FOR_STATUS(&sender, kDifCmodStatusTxinputReady, true, TIMEOUT);
// Check if bits of status register are correctly set.
CHECK(get_status_register(&sender) == 0b0000000011001);
// Send 128 bits
memcpy(dataOut.data, data, sizeof(dataOut.data));
CHECK_DIF_OK(dif_cmod_load_data(&sender, dataOut));
// Check if bits of status register are correctly set.
CHECK(get_status_register(&sender) == 0b0000001010001);
CHECK(get_status_register(&receiver) == 0b0011000101000);
// Receive remaining data blocks of first message
for (int i = 0; i < 3; i++) {
// Read 128 bit and check the data.
CHECK_DIF_OK(dif_cmod_read_data(&receiver, &dataIn));
CHECK(dataIn.data[0] == data[i * 4 + 20] &&
dataIn.data[1] == data[i * 4 + 21] &&
dataIn.data[2] == data[i * 4 + 22] &&
dataIn.data[3] == data[i * 4 + 23]);
if (i == 2) {
CHECK(get_status_register(&receiver) == 0b1000000001000);
} else {
uint32_t value = 0b0000000101000;
value += (2 - i) << 9;
CHECK(get_status_register(&receiver) == value);
}
}
// Set RXCONFIRM and check register
CHECK_DIF_OK(dif_cmod_rxconfirm(&receiver));
CHECK(get_status_register(&receiver) == 0b0001000101000);
CHECK(get_status_register(&sender) == 0b0000000011001);
// Read data, check status register, and data afterward.
CHECK_DIF_OK(dif_cmod_read_data(&receiver, &dataIn));
CHECK(get_status_register(&receiver) == 0b0000000001000);
CHECK_ARRAYS_EQ(dataIn.data, data, 4);
// Set TXEND
CHECK_DIF_OK(dif_cmod_txend(&sender));
CHECK(get_status_register(&sender) == 0b0000000001000);
CHECK(get_status_register(&receiver) == 0b1000000001000);
// Set RXCONFIRM and check register
CHECK_DIF_OK(dif_cmod_rxconfirm(&receiver));
CHECK(get_status_register(&receiver) == 0b0000000001000);
return true;
}