-
Notifications
You must be signed in to change notification settings - Fork 4
/
sketch.ino.template
295 lines (261 loc) · 8.04 KB
/
sketch.ino.template
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
// Copyright (c) maehw, 2022
// wokwi-lookup-table-generator is licensed under the GNU General Public License v3.0
// Copyright and license notices must be preserved. Contributors provide an express grant of patent rights.
/* Let's start with design specific configuration */
#define DESIGN_NUM_USED_INPUTS ({DESIGN_NUM_USED_INPUTS_PH})
#define DESIGN_NUM_USED_OUTPUTS ({DESIGN_NUM_USED_OUTPUTS_PH})
/* Define whether the verification shall halt on the detection of the
* first error (true) or keep on running to the end (false).
*/
#define VERIFICATION_STOP_ON_ERROR ({VERIFICATION_STOP_ON_ERROR})
/* This table shall be generated from the truth table!
* Obviously, also design specific.
*/
uint16_t expected_out_val[2 << (DESIGN_NUM_USED_INPUTS-1)] = {{VERIFICATION_EXPECTED_OUT_VALS_PH}
};
/* Option to pretty print the input value,
* dependent on your design.
*/
#undef VERIFICATION_PRETTY_PRINT_INPUT_VAL
#undef VERIFICATION_PRETTY_PRINT_EXPECTED_OUT_VAL
#undef VERIFICATION_PRETTY_PRINT_REAL_OUT_VAL
// ------------------------------------------------------------------------------
/* Some parameters to tune verification, but actually
* no real need to touch any code below this line
*/
/* As this is run in simulation, baud rate is not that important */
#define SERIAL_BAUDRATE ({SERIAL_BAUDRATE_PH})
/* These params can be used to control simulation speed;
* getting a correct reading the 7-segment display on my device takes quite some time.
*/
#define VERIFICATION_SETUP_TIME_MS ({VERIFICATION_SETUP_TIME_MS_PH})
#define VERIFICATION_HOLD_TIME_MS ({VERIFICATION_HOLD_TIME_MS_PH})
// ------------------------------------------------------------------------------
/* No real need to touch any code below this line */
/* The mapping of 10 input and 10 output pins each could be static;
* however, this limits support to designs with <= 10 input and <= 10 output pins,
* instead of limiting the support to designs with an overall pin count of 20!
*/
#define DESIGN_IN_0 ( 2)
#define DESIGN_IN_1 ( 3)
#define DESIGN_IN_2 ( 4)
#define DESIGN_IN_3 ( 5)
#define DESIGN_IN_4 ( 6)
#define DESIGN_IN_5 ( 7)
#define DESIGN_IN_6 ( 8)
#define DESIGN_IN_7 ( 9)
#define DESIGN_IN_8 (10)
#define DESIGN_IN_9 (11)
#define DESIGN_OUT_0 (12)
#define DESIGN_OUT_1 (13)
#define DESIGN_OUT_2 (14)
#define DESIGN_OUT_3 (15)
#define DESIGN_OUT_4 (16)
#define DESIGN_OUT_5 (17)
#define DESIGN_OUT_6 (18)
#define DESIGN_OUT_7 (19)
#define DESIGN_OUT_8 (20)
#define DESIGN_OUT_9 (21)
void setup()
{
const bool stop_verification_on_error = VERIFICATION_STOP_ON_ERROR;
bool tests_passed = true; /* asume the best for the start */
Serial.begin(SERIAL_BAUDRATE);
pinMode(DESIGN_IN_0, OUTPUT);
pinMode(DESIGN_IN_1, OUTPUT);
pinMode(DESIGN_IN_2, OUTPUT);
pinMode(DESIGN_IN_3, OUTPUT);
pinMode(DESIGN_IN_4, OUTPUT);
pinMode(DESIGN_IN_5, OUTPUT);
pinMode(DESIGN_IN_6, OUTPUT);
pinMode(DESIGN_IN_7, OUTPUT);
pinMode(DESIGN_IN_8, OUTPUT);
pinMode(DESIGN_IN_9, OUTPUT);
pinMode(DESIGN_OUT_0, INPUT);
pinMode(DESIGN_OUT_1, INPUT);
pinMode(DESIGN_OUT_2, INPUT);
pinMode(DESIGN_OUT_3, INPUT);
pinMode(DESIGN_OUT_4, INPUT);
pinMode(DESIGN_OUT_5, INPUT);
pinMode(DESIGN_OUT_6, INPUT);
pinMode(DESIGN_OUT_7, INPUT);
pinMode(DESIGN_OUT_8, INPUT);
pinMode(DESIGN_OUT_9, INPUT);
Serial.print("Design has ");
Serial.print(DESIGN_NUM_USED_INPUTS, DEC);
Serial.println(" inputs.");
Serial.print("Design has ");
Serial.print(DESIGN_NUM_USED_OUTPUTS, DEC);
Serial.println(" outputs.");
Serial.print("Testing all 2^");
Serial.print(DESIGN_NUM_USED_INPUTS, DEC);
Serial.println(" input combinations.");
Serial.print("Stop verification on error? ");
Serial.println(stop_verification_on_error ? "Yes" : "No");
for(uint16_t in_val = 0; ( in_val < (2 << (DESIGN_NUM_USED_INPUTS-1)) ) &&
( !stop_verification_on_error ||
(stop_verification_on_error && tests_passed) ); in_val++ )
{
set_design_input_val(in_val);
// wait some time before checking outputs
delay(VERIFICATION_SETUP_TIME_MS);
tests_passed &= verify_design_output_val(in_val);
// wait some time before setting next inputs
delay(VERIFICATION_HOLD_TIME_MS);
}
Serial.println();
if(tests_passed)
{
Serial.println("[PASSED]");
}
else
{
Serial.println("[FAILED]");
}
}
void set_design_input_val(uint16_t val)
{
/* Set logic design inputs at Arduino's output pins;
* output the bits via serial later, so that we don't add large delay between the different pins!
*/
if( (DESIGN_NUM_USED_INPUTS-1) >= 0 )
{
digitalWrite(DESIGN_IN_0, val & (1 << DESIGN_NUM_USED_INPUTS-1));
}
if( (DESIGN_NUM_USED_INPUTS-2) >= 0 )
{
digitalWrite(DESIGN_IN_1, val & (1 << DESIGN_NUM_USED_INPUTS-2));
}
if( (DESIGN_NUM_USED_INPUTS-3) >= 0 )
{
digitalWrite(DESIGN_IN_2, val & (1 << DESIGN_NUM_USED_INPUTS-3));
}
if( (DESIGN_NUM_USED_INPUTS-4) >= 0 )
{
digitalWrite(DESIGN_IN_3, val & (1 << DESIGN_NUM_USED_INPUTS-4));
}
if( (DESIGN_NUM_USED_INPUTS-5) >= 0 )
{
digitalWrite(DESIGN_IN_4, val & (1 << DESIGN_NUM_USED_INPUTS-5));
}
if( (DESIGN_NUM_USED_INPUTS-6) >= 0 )
{
digitalWrite(DESIGN_IN_5, val & (1 << DESIGN_NUM_USED_INPUTS-6));
}
if( (DESIGN_NUM_USED_INPUTS-7) >= 0 )
{
digitalWrite(DESIGN_IN_6, val & (1 << DESIGN_NUM_USED_INPUTS-7));
}
if( (DESIGN_NUM_USED_INPUTS-8) >= 0 )
{
digitalWrite(DESIGN_IN_7, val & (1 << DESIGN_NUM_USED_INPUTS-8));
}
if( (DESIGN_NUM_USED_INPUTS-9) >= 0 )
{
digitalWrite(DESIGN_IN_8, val & (1 << DESIGN_NUM_USED_INPUTS-9));
}
if( (DESIGN_NUM_USED_INPUTS-10) >= 0 )
{
digitalWrite(DESIGN_IN_9, val & (1 << DESIGN_NUM_USED_INPUTS-10));
}
Serial.print("\nWrote input: 0b");
for(int8_t bit_index = DESIGN_NUM_USED_INPUTS-1; bit_index >= 0; bit_index--)
{
Serial.print((val >> bit_index) & 1, BIN);
}
#ifdef VERIFICATION_PRETTY_PRINT_INPUT_VAL
VERIFICATION_PRETTY_PRINT_INPUT_VAL(val);
#endif
Serial.println();
}
bool verify_design_output_val(uint16_t in_val)
{
// read value from logic design outputs at Arduino's input pins
uint16_t val = 0;
if( DESIGN_NUM_USED_OUTPUTS >= 1 )
{
val |= digitalRead(DESIGN_OUT_0);
val <<= 1;
}
if( DESIGN_NUM_USED_OUTPUTS >= 2 )
{
val |= digitalRead(DESIGN_OUT_1);
val <<= 1;
}
if( DESIGN_NUM_USED_OUTPUTS >= 3 )
{
val |= digitalRead(DESIGN_OUT_2);
val <<= 1;
}
if( DESIGN_NUM_USED_OUTPUTS >= 4 )
{
val |= digitalRead(DESIGN_OUT_3);
val <<= 1;
}
if( DESIGN_NUM_USED_OUTPUTS >= 5 )
{
val |= digitalRead(DESIGN_OUT_4);
val <<= 1;
}
if( DESIGN_NUM_USED_OUTPUTS >= 6 )
{
val |= digitalRead(DESIGN_OUT_5);
val <<= 1;
}
if( DESIGN_NUM_USED_OUTPUTS >= 7 )
{
val |= digitalRead(DESIGN_OUT_6);
val <<= 1;
}
if( DESIGN_NUM_USED_OUTPUTS >= 8 )
{
val |= digitalRead(DESIGN_OUT_7);
val <<= 1;
}
if( DESIGN_NUM_USED_OUTPUTS >= 9 )
{
val |= digitalRead(DESIGN_OUT_8);
val <<= 1;
}
if( DESIGN_NUM_USED_OUTPUTS >= 10 )
{
val |= digitalRead(DESIGN_OUT_9);
val <<= 1;
}
/* the last shift is always one too many */
val >>= 1;
Serial.print(" Expected output: 0b");
for(int8_t bit_index = DESIGN_NUM_USED_OUTPUTS-1; bit_index >= 0; bit_index--)
{
Serial.print((expected_out_val[in_val] >> bit_index) & 1, BIN);
}
#ifdef VERIFICATION_PRETTY_PRINT_EXPECTED_OUT_VAL
VERIFICATION_PRETTY_PRINT_EXPECTED_OUT_VAL(expected_out_val[in_val]);
#endif
Serial.println();
Serial.print(" Read back output: 0b");
for(int8_t bit_index = DESIGN_NUM_USED_OUTPUTS-1; bit_index >= 0; bit_index--)
{
Serial.print((val >> bit_index) & 1, BIN);
}
#ifdef VERIFICATION_PRETTY_PRINT_REAL_OUT_VAL
VERIFICATION_PRETTY_PRINT_REAL_OUT_VAL();
#endif
Serial.println();
if(expected_out_val[in_val] == val)
{
Serial.println(" [PASS]");
return true;
}
else
{
Serial.println(" [FAIL]");
return false;
}
}
void loop()
{
/* no need to loop as everything is only required once and therefore
* already done in setup()
*/
}