-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_idx_tree_tb.cpp
More file actions
217 lines (176 loc) · 7.29 KB
/
Copy pathmax_idx_tree_tb.cpp
File metadata and controls
217 lines (176 loc) · 7.29 KB
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
#include "Vmax_idx_tree.h"
#include "verilated.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <random>
#include <string>
#include <cassert>
class MaxTestbench {
private:
Vmax_idx_tree* top;
uint64_t sim_time;
static const int TOTAL_WIDTH = 60;
static const int CHUNK_WIDTH = 6;
static const int NUM_CHUNKS = TOTAL_WIDTH / CHUNK_WIDTH;
static const int IDX_WIDTH = 4;
public:
MaxTestbench() {
top = new Vmax_idx_tree;
sim_time = 0;
top->clk = 0;
top->rst_n = 0;
top->ce_i = 1;
top->valid_i = 0;
std::cout << "Testing max_idx_tree module with " << NUM_CHUNKS
<< " chunks of " << CHUNK_WIDTH << " bits each\n";
std::cout << "Total input width: " << TOTAL_WIDTH << " bits\n";
std::cout << "RTL uses LSB-first chunking: chunk[0] = din[5:0]\n";
}
~MaxTestbench() {
delete top;
}
void tick() {
top->clk = 0;
top->eval();
sim_time++;
top->clk = 1;
top->eval();
sim_time++;
}
void reset() {
top->rst_n = 0;
tick();
tick();
top->rst_n = 1;
tick();
}
bool run_test(uint64_t din_value, const std::string& test_name) {
reset();
top->din = din_value;
top->valid_i = 1;
top->eval();
int dout = top->idx_o;
bool output_valid = top->valid_o;
top->valid_i = 0;
tick();
// Calculate result using LSB-first chunking
std::vector<int> chunks(NUM_CHUNKS);
for (int i = 0; i < NUM_CHUNKS; i++) {
// Extract chunk i using LSB-first: chunk[i] = din[(i+1)*CHUNK_WIDTH-1:i*CHUNK_WIDTH]
int bit_start = i * CHUNK_WIDTH;
uint64_t mask = (1ULL << CHUNK_WIDTH) - 1;
chunks[i] = (din_value >> bit_start) & mask;
}
int max_val = *std::max_element(chunks.begin(), chunks.end());
int expected_idx = std::distance(chunks.begin(),
std::find(chunks.begin(), chunks.end(), max_val));
bool success = (dout == expected_idx) && output_valid;
std::cout << "\n" << test_name << "\n";
std::cout << "Input: 0x" << std::hex << std::setfill('0')
<< std::setw(15) << din_value << std::dec << "\n";
std::cout << "Chunks: [";
for (int i = 0; i < NUM_CHUNKS; i++) {
std::cout << chunks[i];
if (i < NUM_CHUNKS - 1) std::cout << ", ";
}
std::cout << "] (LSB-first)\n";
std::cout << "Max value: " << max_val << " at index " << expected_idx << "\n";
std::cout << "Output: " << dout << " (valid=" << output_valid << ")\n";
std::cout << "Result: " << (success ? "✅ PASS" : "❌ FAIL") << "\n";
return success;
}
void test_max_module() {
std::cout << "\n" << std::string(50, '=') << "\n";
std::cout << "Starting max_idx_tree module tests...\n";
int pass_count = 0;
int total_tests = 0;
// Test Case 1: All zeros
if (run_test(0x0ULL, "Test 1: All zeros")) pass_count++;
total_tests++;
// Test Case 2: Maximum value in first position (LSB chunk)
uint64_t test_val = (1ULL << CHUNK_WIDTH) - 1;
if (run_test(test_val, "Test 2: Max in first chunk (LSB)")) pass_count++;
total_tests++;
// Test Case 3: Maximum value in last position (MSB chunk)
test_val = ((1ULL << CHUNK_WIDTH) - 1) << (TOTAL_WIDTH - CHUNK_WIDTH);
if (run_test(test_val, "Test 3: Max in last chunk (MSB)")) pass_count++;
total_tests++;
// Test Case 4: Maximum value in middle (chunk 5)
test_val = ((1ULL << CHUNK_WIDTH) - 1) << (5 * CHUNK_WIDTH);
if (run_test(test_val, "Test 4: Max in middle chunk (index 5)")) pass_count++;
total_tests++;
// Test Case 5: Multiple equal maximum values (should return first occurrence)
test_val = 0;
test_val |= 63ULL;
test_val |= 63ULL << (2 * CHUNK_WIDTH);
if (run_test(test_val, "Test 5: Multiple equal max values")) pass_count++;
total_tests++;
// Test Case 6: Random values
std::mt19937 gen(42);
std::uniform_int_distribution<uint64_t> dis(0, (1ULL << TOTAL_WIDTH) - 1);
for (int i = 0; i < 5; i++) {
test_val = dis(gen);
std::string test_name = "Test " + std::to_string(6 + i) + ": Random test " + std::to_string(i + 1);
if (run_test(test_val, test_name)) pass_count++;
total_tests++;
}
// Test Case 7: Sequential increasing values
test_val = 0;
for (int i = 0; i < NUM_CHUNKS; i++) {
int chunk_val = i % ((1 << CHUNK_WIDTH) - 1);
test_val |= (uint64_t)chunk_val << (i * CHUNK_WIDTH);
}
if (run_test(test_val, "Test 11: Sequential increasing")) pass_count++;
total_tests++;
// Test Case 8: Sequential decreasing values
test_val = 0;
for (int i = 0; i < NUM_CHUNKS; i++) {
int chunk_val = (NUM_CHUNKS - 1 - i) % ((1 << CHUNK_WIDTH) - 1);
test_val |= (uint64_t)chunk_val << (i * CHUNK_WIDTH);
}
if (run_test(test_val, "Test 12: Sequential decreasing")) pass_count++;
total_tests++;
// Test Case 9: Specific pattern test
test_val = 0;
for (int i = 0; i < NUM_CHUNKS; i++) {
int chunk_val = (i == 7) ? 50 : 30;
test_val |= (uint64_t)chunk_val << (i * CHUNK_WIDTH);
}
if (run_test(test_val, "Test 13: Specific pattern (max at index 7)")) pass_count++;
total_tests++;
std::cout << "\n" << std::string(50, '=') << "\n";
std::cout << "Max module test completed!\n";
std::cout << "Results: " << pass_count << "/" << total_tests
<< " tests passed (" << (100.0 * pass_count / total_tests) << "%)\n";
}
void test_edge_cases() {
std::cout << "\n" << std::string(50, '=') << "\n";
std::cout << "Testing edge cases...\n";
std::cout << "\nTesting boundary conditions:\n";
// Test maximum possible value
uint64_t max_val = (1ULL << TOTAL_WIDTH) - 1;
run_test(max_val, "Edge case: Maximum possible input");
// Test with only one bit set in each chunk
uint64_t test_val = 0;
for (int i = 0; i < NUM_CHUNKS; i++) {
test_val |= 1ULL << (i * CHUNK_WIDTH);
}
run_test(test_val, "Edge case: One bit set per chunk");
// Test with alternating pattern
test_val = 0;
for (int i = 0; i < NUM_CHUNKS; i++) {
int chunk_val = (i % 2) ? ((1 << CHUNK_WIDTH) - 1) : 0;
test_val |= (uint64_t)chunk_val << (i * CHUNK_WIDTH);
}
run_test(test_val, "Edge case: Alternating max/min pattern");
}
};
int main(int argc, char** argv) {
Verilated::commandArgs(argc, argv);
MaxTestbench tb;
tb.test_max_module();
tb.test_edge_cases();
std::cout << "\nSimulation completed successfully!\n";
return 0;
}