-
Notifications
You must be signed in to change notification settings - Fork 9
/
ice40.c
570 lines (510 loc) · 18.1 KB
/
ice40.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "ice40.h"
#define MAX(x, y) (x) > (y) ? (x) : (y)
#define ARRAY_SIZE(x) ((sizeof(x) / sizeof(*x)))
#define DEBUG_PRINT(...)
// #define DEBUG_PRINT(...) printf(__VA_ARGS__)
// #define SCAN_DEBUG
// Make this a macro so line numbers work correctly
#define assert_words_equal(check_word, old_word) \
if (check_word != old_word) { \
int j; \
printf("mismatch in source stream!\n"); \
printf("old_word: %04x check_word: %04x\n", old_word, check_word); \
printf("rand:"); \
for (j = (offset + mapping) - 16; j < (offset + mapping) + 16; j++) { \
if (j == (offset + mapping)) \
printf(" [%04x]", ora16[j]); \
else \
printf(" %04x", ora16[j]); \
}\
printf("\n"); \
\
printf(" rom:");\
for (j = i - 16; j < (int)i + 16; j++) {\
printf(" %04x", oro16[j]);\
}\
printf("\n");\
printf("if possible, please email the rom and bitstream to [email protected]\n"); \
} \
assert(check_word == old_word)
#ifdef SCAN_DEBUG
#define SCAN_DEBUG_PRINT(...) printf(__VA_ARGS__)
#else
#define SCAN_DEBUG_PRINT(...)
#endif
struct Ice40Bitstream
{
uint32_t offset;
uint32_t current_bank;
uint32_t current_width;
uint32_t current_height;
uint32_t current_offset;
uint32_t cram_width;
uint32_t cram_height;
uint32_t bram_width;
uint32_t bram_height;
uint16_t crc_value;
uint8_t warmboot;
uint8_t nosleep;
uint8_t frequency_range;
};
static void update_crc16(uint16_t *crc, uint8_t byte)
{
// CRC-16-CCITT, Initialize to 0xFFFF, No zero padding
for (int i = 7; i >= 0; i--)
{
uint16_t xor_value = ((*crc >> 15) ^ ((byte >> i) & 1)) ? 0x1021 : 0;
*crc = (*crc << 1) ^ xor_value;
}
}
static uint32_t get_bit_offset(int x, int total_bits) {
// return (8192 * (x & 7)) + (x >> 3);
int bitshift = ffs(total_bits)-1;
return ((x * 8192) % total_bits) + ((x*8192) >> bitshift);
}
uint32_t xorshift32(uint32_t x)
{
/* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
x = x ^ (x << 13);
x = x ^ (x >> 17);
x = x ^ (x << 5);
return x;
}
uint32_t get_rand(uint32_t x) {
uint32_t out = 0;
int i;
for (i = 0; i < 32; i++) {
x = xorshift32(x);
if ((x & 1) == 1)
out = out | (1 << i);
}
return out;
}
static uint32_t fill_rand(uint32_t *bfr, int count) {
int i;
uint32_t last = 1;
for (i = 0; i < count / 4; i++) {
last = get_rand(last);
bfr[i] = last;
}
return i;
}
uint32_t swap_u32(uint32_t word) {
return (((word >> 24) & 0x000000ff)
| ((word >> 8) & 0x0000ff00)
| ((word << 8) & 0x00ff0000)
| ((word << 24) & 0xff000000));
}
struct irw_file *irw_open(const char *filename, const char *mode)
{
FILE *tmpfile = fopen(filename, mode);
if (!tmpfile)
return NULL;
struct irw_file *f = malloc(sizeof(*f));
memset(f, 0, sizeof(*f));
f->f = tmpfile;
return f;
}
struct irw_file *irw_open_fake(void *hook_data,
int (*read_hook)(void *data),
int (*write_hook)(void *data, uint8_t b)) {
struct irw_file *f = malloc(sizeof(*f));
memset(f, 0, sizeof(*f));
f->read_hook = read_hook;
f->write_hook = write_hook;
f->hook_data = hook_data;
return f;
}
int irw_readb(struct irw_file *f)
{
int val;
if (f->read_hook)
val = f->read_hook(f->hook_data);
else
val = fgetc(f->f);
if (val == EOF)
return EOF;
update_crc16(&f->crc, val);
return val;
}
int irw_writeb(struct irw_file *f, int c) {
update_crc16(&f->crc, c);
if (f->write_hook)
return f->write_hook(f->hook_data, c);
else
return fputc(c, f->f);
}
void irw_close(struct irw_file **f) {
if (!f)
return;
if (!*f)
return;
if (!(*f)->f)
return;
fclose((*f)->f);
*f = NULL;
}
uint8_t get_bit(uint32_t *field, uint32_t offset)
{
// printf("offset&31: %d\n", offset & 31);
// printf("offset/sizeof(*field): %d\n", offset >> 5);
assert(offset < 65536);
return !!(field[offset >> 5] & (1 << (offset & 31)));
}
void set_bit(uint32_t *field, uint32_t offset)
{
assert(offset < 65536);
field[offset >> 5] |= (1 << (offset & 31));
}
void clear_bit(uint32_t *field, uint32_t offset)
{
assert(offset < 65536);
field[offset >> 5] &= ~(1 << (offset & 31));
}
int ice40_patch(struct irw_file *f, struct irw_file *rom,
struct irw_file *o, uint32_t byte_count)
{
uint32_t preamble = 0;
uint8_t wakeup = 0;
struct Ice40Bitstream bs;
uint32_t input_rom[byte_count / sizeof(uint32_t)];
uint32_t input_rand[byte_count / sizeof(uint32_t)];
uint32_t output_rand[byte_count / sizeof(uint32_t)];
uint32_t output_rom[byte_count / sizeof(uint32_t)];
uint8_t *i8 = (uint8_t *)input_rom;
uint16_t *ora16 = (uint16_t *)output_rand;
uint16_t *oro16 = (uint16_t *)output_rom;
unsigned int ora_ptr = 0;
unsigned int input_ptr;
int b;
int errors = 0;
memset(&bs, 0, sizeof(bs));
// Read the ROM into a source buffer
memset(input_rom, 0, sizeof(input_rom));
input_ptr = 0;
while (((b = irw_readb(rom)) != EOF) && input_ptr <= byte_count)
i8[input_ptr++] = b;
if (input_ptr > byte_count) {
fprintf(stderr, "input file is larger than %d bytes\n", byte_count);
return -1;
}
DEBUG_PRINT("read %d bytes from rom\n", input_ptr);
// Generate our reference pattern
memset(input_rand, 0, sizeof(input_rand));
fill_rand(input_rand, sizeof(input_rand));
// Swap either the input or output data, as necessary
for (input_ptr = 0; input_ptr < sizeof(input_rom)/4; input_ptr++) {
// input_rand[input_ptr] = swap_u32(input_rand[input_ptr]);
// input_rom[input_ptr] = swap_u32(input_rom[input_ptr]);
}
// Spray the reference pattern and ROM like they would exist in the FPGA
for (input_ptr = 0; input_ptr < sizeof(input_rom) * 8; input_ptr++) {
int bit;
bit = get_bit(input_rand, get_bit_offset(input_ptr, sizeof(input_rand)*8));
if (bit)
set_bit(output_rand, input_ptr);
else
clear_bit(output_rand, input_ptr);
bit = get_bit(input_rom, get_bit_offset(input_ptr, sizeof(input_rom)*8));
if (bit)
set_bit(output_rom, input_ptr);
else
clear_bit(output_rom, input_ptr);
}
while (1)
{
b = irw_readb(f);
if (b == EOF)
break;
irw_writeb(o, b);
preamble = (preamble << 8) | b;
if (preamble == 0x7eaa997e)
{
// DEBUG_PRINT("found preamble at %d\n", bs.offset);
break;
}
}
while (!wakeup)
{
int b = irw_readb(f);
if (b == EOF)
{
// DEBUG_PRINT("reached end of file\n");
break;
}
irw_writeb(o, b);
uint8_t cmd = b >> 4;
uint8_t payload_len = b & 0xf;
uint32_t payload = 0;
uint8_t last0, last1;
unsigned int i;
for (i = 0; i < payload_len; i++)
{
b = irw_readb(f);
payload = (payload << 8) | (b & 0xff);
// Don't write the CRC16 out, since we'll do that later on.
if (cmd != 2)
irw_writeb(o, b);
}
switch (cmd)
{
case 0:
switch (payload)
{
case 1:
DEBUG_PRINT("CRAM data (bank %d): %d x %d @ 0x%08x; %d bits = %d bytes\n",
bs.current_bank,
bs.current_width,
bs.current_height,
bs.current_offset,
bs.current_width * bs.current_height,
(bs.current_width * bs.current_height) / 8);
bs.cram_width = MAX(bs.cram_width, bs.current_width);
bs.cram_height = MAX(bs.cram_height, bs.current_height);
for (i = 0; i < ((bs.current_width * bs.current_height) / 8); i++)
{
irw_writeb(o, irw_readb(f));
}
last0 = irw_readb(f);
last1 = irw_readb(f);
if (last0 || last1)
{
printf("expected 0x0000 after CRAM data, got %02x %02x\n", last0, last1);
}
irw_writeb(o, last0);
irw_writeb(o, last1);
break;
case 3:
DEBUG_PRINT("BRAM data (bank %d): %d x %d @ 0x%08x; %d bits = %d bytes\n",
bs.current_bank,
bs.current_width,
bs.current_height,
bs.current_offset,
bs.current_width * bs.current_height,
(bs.current_width * bs.current_height) / 8);
bs.bram_width = MAX(bs.bram_width, bs.current_width);
bs.bram_height = MAX(bs.bram_height, bs.current_height);
ora_ptr = 16 * bs.current_offset;
// Step 1: Find a mapping by scanning through the first 128 words looking for patterns.
uint16_t scan_buffer[128];
assert(((bs.current_width * bs.current_height)/8)*2 > 128);
for (i = 0; i < ARRAY_SIZE(scan_buffer); i++) {
scan_buffer[i] = ((irw_readb(f) << 8) & 0xff00) | ((irw_readb(f) << 0) & 0x00ff);
}
#ifdef SCAN_DEBUG
SCAN_DEBUG_PRINT("scan:");
for (i = 0; i < ARRAY_SIZE(scan_buffer); i++) {
SCAN_DEBUG_PRINT(" %04x", scan_buffer[i]);
}
SCAN_DEBUG_PRINT("\n");
SCAN_DEBUG_PRINT("rand:");
for (i = 0; i < ARRAY_SIZE(scan_buffer); i++) {
SCAN_DEBUG_PRINT(" %04x", ora16[ora_ptr + i]);
}
SCAN_DEBUG_PRINT("\n");
SCAN_DEBUG_PRINT(" rom:");
for (i = 0; i < ARRAY_SIZE(scan_buffer); i++) {
SCAN_DEBUG_PRINT(" %04x", oro16[ora_ptr + i]);
}
SCAN_DEBUG_PRINT("\n");
#endif /* SCAN_DEBUG */
int outer_word = 0;
static struct {
int bitstream;
int random;
int stride;
} word_mappings[16];
int word_stride = -1;
for (outer_word = 0; outer_word < 16; outer_word++) {
int inner_word;
word_mappings[outer_word].bitstream = -1;
word_mappings[outer_word].random = -1;
word_mappings[outer_word].stride = -1;
for (inner_word = 0; inner_word < 16; inner_word++) {
// We have a candidate offset. Figure out what its stride is,
// and validate that we have multiple matches.
if (scan_buffer[outer_word] == ora16[ora_ptr + inner_word]) {
SCAN_DEBUG_PRINT("Candidate %04x @ %d/%d\n", scan_buffer[outer_word], outer_word, inner_word);
int scan_offset = 0;
for (scan_offset = 0; scan_offset < 30; scan_offset++) {
if ((scan_buffer[outer_word + scan_offset] == ora16[ora_ptr + inner_word + 16])
&& (scan_buffer[outer_word + (scan_offset*2)] == ora16[ora_ptr + inner_word + 32])) {
SCAN_DEBUG_PRINT("Scan offset: %d\n", scan_offset);
word_mappings[outer_word].bitstream = outer_word;
word_mappings[outer_word].random = inner_word;
word_mappings[outer_word].stride = scan_offset;
}
}
}
}
}
for (i = 0; i < ARRAY_SIZE(word_mappings); i++) {
if (word_mappings[i].stride != -1) {
if (word_stride != -1) {
if (word_mappings[i].stride != word_stride) {
printf("This stride is different (%d vs expected %d)\n", word_mappings[i].stride, word_stride);
}
}
word_stride = word_mappings[i].stride;
}
}
#ifdef SCAN_DEBUG
for (outer_word = 0; outer_word < ARRAY_SIZE(word_mappings); outer_word++) {
SCAN_DEBUG_PRINT("word_mappings[%2d]: bitstream: %2d random: %2d stride: %2d\n",
outer_word, word_mappings[outer_word].bitstream,
word_mappings[outer_word].random, word_mappings[outer_word].stride);
}
#endif /* SCAN_DEBUG */
for (i = 0; i < ARRAY_SIZE(scan_buffer); i++) {
int offset = -1;
int mapping = -1;
if (word_stride != -1) {
offset = (i / word_stride) * 16;
mapping = word_mappings[i % word_stride].random;
}
uint16_t old_word = scan_buffer[i];
uint16_t check_word;
uint16_t new_word;
if (mapping == -1) {
new_word = check_word = old_word;
}
else {
check_word = ora16[ora_ptr + offset + mapping];
new_word = oro16[ora_ptr + offset + mapping];
}
irw_writeb(o, new_word >> 8);
irw_writeb(o, new_word);
SCAN_DEBUG_PRINT("%4d %4d %2d %2d %04x <-> %04x -> %04x\n", i, sizeof(scan_buffer), offset, mapping, old_word, check_word, new_word);
assert_words_equal(check_word, old_word);
}
SCAN_DEBUG_PRINT("---\n");
// Finish reading the page
for (i = ARRAY_SIZE(scan_buffer); i < ((bs.current_width * bs.current_height) / 8)/2; i++) {
int offset = -1;
int mapping = -1;
if (word_stride != -1) {
offset = (i / word_stride) * 16;
mapping = word_mappings[i % word_stride].random;
}
uint16_t old_word =
((irw_readb(f) << 8) & 0xff00)
|
((irw_readb(f) << 0) & 0x00ff)
;
uint16_t check_word;
uint16_t new_word;
if (mapping == -1) {
new_word = check_word = old_word;
}
else {
check_word = ora16[ora_ptr + offset + mapping];
new_word = oro16[ora_ptr + offset + mapping];
}
irw_writeb(o, new_word >> 8);
irw_writeb(o, new_word);
SCAN_DEBUG_PRINT("%4d %4d %2d %2d %04x <-> %04x -> %04x\n", i, sizeof(scan_buffer), offset, mapping, old_word, check_word, new_word);
assert_words_equal(check_word, old_word);
}
last0 = irw_readb(f);
last1 = irw_readb(f);
if (last0 || last1)
{
printf("expected 0x0000 after BRAM data, got %02x %02x\n", last0, last1);
}
irw_writeb(o, last0);
irw_writeb(o, last1);
break;
// Reset CRC
case 5:
f->crc = 0xffff;
o->crc = 0xffff;
break;
// Wakeup
case 6:
wakeup = 1;
break;
default:
printf("unrecognized command 0x%02x 0x%02x\n", cmd, payload);
break;
}
break;
// Set current bank
case 1:
bs.current_bank = payload;
ora_ptr = 0;
// printf("setting bank number to %d\n", bs.current_bank);
break;
// Validate CRC16
case 2:
DEBUG_PRINT("crc check (%04x == %04x)\n", f->crc, 0);
uint16_t crc16 = o->crc;
irw_writeb(o, crc16 >> 8);
irw_writeb(o, crc16);
break;
// Set frequency range
case 5:
switch (payload)
{
case 0:
bs.frequency_range = 0;
break;
case 1:
bs.frequency_range = 1;
break;
case 2:
bs.frequency_range = 2;
break;
default:
printf("unknown frequency range payload: %02x\n", payload);
break;
}
break;
// Set current width
case 6:
bs.current_width = payload + 1;
break;
// Set current height
case 7:
bs.current_height = payload;
break;
// Set current ofset
case 8:
bs.current_offset = payload;
break;
// Set flags
case 9:
switch (payload)
{
case 0:
bs.warmboot = 0;
bs.nosleep = 0;
break;
case 1:
bs.warmboot = 0;
bs.nosleep = 1;
break;
case 32:
bs.warmboot = 1;
bs.nosleep = 0;
break;
case 33:
bs.warmboot = 1;
bs.nosleep = 1;
break;
default:
printf("unrecognized feature flags: %02x\n", payload);
break;
}
break;
default:
printf("unrecognized command: %02x\n", cmd);
break;
}
}
// Padding
irw_writeb(o, 0);
return errors;
}