forked from raspberrypi/pico-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hscroll_dma_tiles.c
605 lines (545 loc) · 19.7 KB
/
hscroll_dma_tiles.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/sync.h"
#include "pico/stdlib.h"
#include "pico/scanvideo.h"
#include "pico/scanvideo/composable_scanline.h"
#include "pico/multicore.h"
#include "spans.h"
#include "data.h"
CU_REGISTER_DEBUG_PINS(frame_gen)
//CU_SELECT_DEBUG_PINS(frame_gen)
//#define DEBUG_HALF_PIXEL
typedef bool (*render_scanline_func)(struct scanvideo_scanline_buffer *dest, int core);
bool render_scanline_test_pattern(struct scanvideo_scanline_buffer *dest, int core);
bool render_scanline_bg(struct scanvideo_scanline_buffer *dest, int core);
////#define vga_mode vga_mode_640x480_60
#define vga_mode vga_mode_320x240_60
//#define vga_mode vga_mode_213x160_60
//#define vga_mode vga_mode_160x120_60
////#define vga_mode vga_mode_tft_800x480_50
//#define vga_mode vga_mode_tft_400x240_50
//#define DISABLE_HPIXELS
//#define vga_mode vga_mode_tft_320x240_60
#define COUNT ((vga_mode.width/8)-1)
// for now we want to see second counter on native and don't need both cores
#if !PICO_NO_HARDWARE
// todo there is a bug in multithreaded rendering atm
#define RENDER_ON_CORE0
#endif
#define RENDER_ON_CORE1
//#define IRQS_ON_CORE1
render_scanline_func render_scanline = render_scanline_bg;
#define COORD_SHIFT 3
int vspeed = 1 * 1;
int hspeed = 1 << COORD_SHIFT;
int hpos;
int vpos;
#ifndef DISABLE_HPIXELS
bool enable_half_pixels = true;
#else
#define enable_half_pixels false
#endif
bool enable_wave;
int wave_amplitude = 50;
static const int input_pin0 = 22;
// to make sure only one core updates the state when the frame number changes
// todo note we should actually make sure here that the other core isn't still rendering (i.e. all must arrive before either can proceed - a la barrier)
static struct mutex frame_logic_mutex;
static int left = 0;
static int top = 0;
static int x_sprites = 1;
void go_core1(void (*execute)());
void init_render_state(int core);
void print_status() {
printf("hspeed %d/8 half-pixel=%d wave=%d (amp=%d) (sprites %d) \r", hspeed, enable_half_pixels, enable_wave,
wave_amplitude, x_sprites);
}
// ok this is going to be the beginning of retained mode
//
// data is in the wrong color format
void convert_spans(const struct tile_data16 *td) {
#if PICO_ON_DEVICE
uint16_t *p = (uint16_t *) td->blob.bytes;
for (int i = 0; i < td->blob.size / 2; i++) {
uint r = p[i] & 0x1f;
uint g = (p[i] >> 5) & 0x1f;
uint b = (p[i] >> 10) & 0x1f;
uint alpha = p[i] >> 15;
p[i] = PICO_SCANVIDEO_PIXEL_FROM_RGB5(r, g, b) | (alpha ? PICO_SCANVIDEO_ALPHA_MASK : 0);
}
#endif
}
void render_loop() {
static uint8_t last_input = 0;
static uint32_t last_frame_num = 0;
int core_num = get_core_num();
assert(core_num >= 0 && core_num < 2);
printf("Rendering on core %d\r\n", core_num);
if (DEBUG_PINS_ENABLED(frame_gen)) {
gpio_init(PICO_DEBUG_PIN_BASE + 1);
gpio_set_dir_out_masked(2 << PICO_DEBUG_PIN_BASE); // steal debug pin 2 for this core
}
while (true) {
struct scanvideo_scanline_buffer *scanvideo_scanline_buffer = scanvideo_begin_scanline_generation(true);
// if (scanvideo_scanline_buffer->data_used) {
// // validate the previous scanline to make sure no one corrupted it
// validate_scanline(scanvideo_scanline_buffer->data, scanvideo_scanline_buffer->data_used, vga_mode.width, vga_mode.width);
// }
// do any frame related logic
bool ps = false;
// todo probably a race condition here ... thread dealing with last line of a frame may end
// todo up waiting on the next frame...
mutex_enter_blocking(&frame_logic_mutex);
uint32_t frame_num = scanvideo_frame_number(scanvideo_scanline_buffer->scanline_id);
// note that with multiple cores we may have got here not for the first scanline, however one of the cores will do this logic first before either does the actual generation
if (frame_num != last_frame_num) {
if (frame_num == 1) {
ps = true;
}
// this could should be during vblank as we try to create the next line
// todo should we ignore if we aren't attempting the next line
last_frame_num = frame_num;
#if PICO_ON_DEVICE
if (uart_is_readable(uart_default)) {
int c = uart_getc(uart_default);
switch (c) {
case '+':
case '=':
hspeed++;
break;
case '_':
case '-':
hspeed--;
break;
case '9':
if (x_sprites > 0) x_sprites--;
break;
case '0':
if (x_sprites < 17) x_sprites++;
break;
case '[':
wave_amplitude--;
break;
case ']':
wave_amplitude++;
break;
#ifndef DISABLE_HPIXELS
case 'h':
enable_half_pixels ^= true;
break;
#endif
case 'w':
enable_wave ^= true;
}
ps = true;
}
#endif
hpos += hspeed;
if (hpos < 0) {
hpos = 0;
hspeed = -hspeed;
} else if (hpos >= (level0_map_width * 8 - vga_mode.width) << COORD_SHIFT) {
hpos = (level0_map_width * 8 - vga_mode.width) << COORD_SHIFT;
hspeed = -hspeed;
}
uint8_t new_input = gpio_get(input_pin0);
if (last_input && !new_input) {
hpos++;
}
last_input = new_input;
}
mutex_exit(&frame_logic_mutex);
DEBUG_PINS_SET(frame_gen, core_num ? 2 : 4);
render_scanline(scanvideo_scanline_buffer, core_num);
DEBUG_PINS_CLR(frame_gen, core_num ? 2 : 4);
#if PICO_SCANVIDEO_PLANE_COUNT > 2
assert(false);
#endif
// release the scanline into the wild
scanvideo_end_scanline_generation(scanvideo_scanline_buffer);
// do this outside mutex and scanline generation
if (ps) {
print_status();
}
}
}
struct semaphore video_setup_complete;
void setup_video() {
scanvideo_setup(&vga_mode);
scanvideo_timing_enable(true);
sem_release(&video_setup_complete);
}
void core1_func() {
#ifdef IRQS_ON_CORE1
setup_video();
#endif
#ifdef RENDER_ON_CORE1
render_loop();
#endif
}
#define TEST_WAIT_FOR_SCANLINE
#ifdef TEST_WAIT_FOR_SCANLINE
volatile uint32_t scanline_color = 0;
#endif
int video_main(void) {
#ifndef DISABLE_HPIXELS
assert(vga_mode.xscale >= 2); // too slow anyway, but we would need to turn half pixel off
#endif
// gpio_debug_pins_init();
mutex_init(&frame_logic_mutex);
// get the bottom
vpos = level0_map_height * 8 - vga_mode.height;
assert(vpos >= 0);
convert_spans(&tiles_tile_data);
convert_spans(&galaga_tile_data);
sem_init(&video_setup_complete, 0, 1);
#ifndef IRQS_ON_CORE1
setup_video();
#endif
puts("KEYS:");
puts(" +/- adjust horizonatal speed");
puts(" h toggle half pixel mode");
puts(" 9/0 up/down horizontal sprite count");
puts(" w toggle wave mode");
puts(" [/] adjust wave amplitude");
init_render_state(0);
#ifdef RENDER_ON_CORE1
init_render_state(1);
#endif
#if defined(RENDER_ON_CORE1) || defined(IRQS_ON_CORE1)
go_core1(core1_func);
#endif
#ifdef RENDER_ON_CORE0
render_loop();
#else
sem_acquire_blocking(&video_setup_complete);
while (true) {
#ifndef TEST_WAIT_FOR_SCANLINE
// Just use vblank to print out a value every second
static int i=0, s=0;
video_wait_for_vblank();
if (++i == 60) {
printf("%d\n", s++);
i = 0;
}
#else
static uint32_t sl = 0;
sl = scanvideo_wait_for_scanline_complete(sl);
scanline_color = (scanline_color + 0x10u) & 0xffu;
#endif
}
#endif
__builtin_unreachable();
}
//struct palette16 *opaque_pi_palette = NULL;
// must not be called concurrently
void init_render_state(int core) {
// todo we should of course have a wide solid color span that overlaps
// todo we can of course also reuse these
// init_solid_color_span(&before_span[core], left, opaque_pi_palette->entries[15], NULL);
// init_vogon_4bit_span(&pi_span[core], pi400_image_data.width, NULL, 0, opaque_pi_palette, &before_span[core]);
// init_solid_color_span(&after_span[core], vga_mode.width - left - pi400_image_data.width, opaque_pi_palette->entries[15],
// &pi_span[core]);
}
static const int8_t hacky_cos_table[] = {127, 126, 124, 121, 117, 112, 105, 98, 89, 80, 70, 59, 48, 36, 24, 12, 0, -12,
-24, -36, -48, -59, -70, -80, -89, -98, -105, -112, -117, -121, -124, -126,
-127, -126, -124, -121, -117, -112, -105, -98, -89, -80, -70, -59, -48, -36,
-24, -12, 0, 12, 24, 36, 48, 59, 70, 80, 89, 98, 105, 112, 117, 121, 124, 126};
bool render_scanline_bg(struct scanvideo_scanline_buffer *dest, int core) {
// 1 + line_num red, then white
uint32_t *buf = dest->data;
size_t buf_length = dest->data_max;
int y = scanvideo_scanline_number(dest->scanline_id) + vpos;
int x = hpos;
if (enable_wave) {
x += (wave_amplitude *
hacky_cos_table[(y + scanvideo_frame_number(dest->scanline_id)) % count_of(hacky_cos_table)]) >> 8;
if (x < 0) x = 0;
if (x > ((level0_map_width * 8 - vga_mode.width) << COORD_SHIFT))
x = (level0_map_width * 8 - vga_mode.width) << COORD_SHIFT;
}
//y = (y + frame_number(dest->scanline_id)) % (level0_map_height * 8);
const uint16_t *map = level0_map + level0_map_width * (y / 8);
map += (x >> (COORD_SHIFT + 3));
const uint16_t *map0 = map;
const uint16_t *span_offsets = tiles_tile_data.span_offsets + (y & 7u);
#if !PICO_SCANVIDEO_PLANE1_VARIABLE_FRAGMENT_DMA
uint16_t *output = (uint16_t*)buf;
// raw run has an inline first pixel, so we need to handle that here
// todo shift the video mode over by a pixel to account for this
*output++ = COMPOSABLE_RAW_RUN;
*output++ = 0;
*output++ = 1 + COUNT * 8 - 3;
for(int i=0;i<COUNT;i++) {
uint32_t off = span_offsets[8 * *map++];
uint16_t *data = (uint16_t *)(tiles_tile_data.blob.bytes + off);
for(int j=0;j<8;j++) *output++ = *data++;
}
// todo fix so we don't need whole scanline
*output++ = COMPOSABLE_COLOR_RUN;
*output++ = 0;
*output++ = vga_mode.width - COUNT * 8 - 1 - 3;
// end of line stuff
*output++ = COMPOSABLE_RAW_1P;
*output++ = 0;
if (2 & (intptr_t)output) {
// we are unaligned
*output++ = COMPOSABLE_EOL_ALIGN;
} else {
*output++ = COMPOSABLE_EOL_SKIP_ALIGN;
*output++ = 0xffff; // eye catcher
}
assert(0 == (3u & (intptr_t)output));
assert((uint32_t*)output <= (buf + dest->data_max));
dest->data_used = (uint16_t)(((uint32_t*)output) - buf);
#else
#ifdef DEBUG_HALF_PIXEL
int debug_half_pixel_count = 0;
#endif
// we handle both ends separately
// static const uint32_t end_of_line[] = {
// COMPOSABLE_RAW_1P | (0u<<16),
// COMPOSABLE_EOL_SKIP_ALIGN | (0xffff << 16) // eye catcher ffff
// };
uint32_t *output32 = buf + 2; // skip one chain segment - we fill in below
map++; // skip first element
uint32_t off = span_offsets[8 * map0[0]];
int i = (x >> COORD_SHIFT) & 7;
int eol_pixels = i;
uint16_t *data = (uint16_t *) (tiles_tile_data.blob.bytes + off);
bool half_pixel = enable_half_pixels && (x & (1 << (COORD_SHIFT - 1)));
data += i;
if (half_pixel) i++; // we deal with the second pixel specially
int j;
if (i >= 7) {
j = 1;
// skip second element in the offset 7 7/12 case
map++;
} else {
j = 0;
}
// render full size tiles
for (; j < COUNT; j++) {
uint32_t off = span_offsets[8 * *map++];
uint16_t *data = (uint16_t *) (tiles_tile_data.blob.bytes + off);
*output32++ = 4;
assert(!(3u & (intptr_t) data));
*output32++ = host_safe_hw_ptr(data);
}
// end of scanline // to be filled in below
*output32++ = 0;
*output32++ = 0;
// end of dma chain (correct 0 values)
*output32++ = 0;
*output32++ = 0;
uint16_t *output = (uint16_t *) output32;
// draw a possibly fractional first tile
// 0: RUN T00 | LEN T01 | T02 T03 | T04 T05 | T06 T07 |
// 1: R2P T01 | T02 RUN | T03 LEN | T04 T05 | T06 T07 |
// 2: RUN T02 | LEN T03 | T04 T05 | T06 T07 |
// 3: R2P T03 | T04 RUN | T05 LEN | T06 T07 |
// 4: RUN T04 | LEN T05 | T06 T07 |
// 5: R2P T05 | T06 RUN | T05 LEN | T06 T07 |
// 6: RUN T06 | LEN T07 |
// 7: R2P T07 | T10 RUN | T11 LEN | T12 T13 | T14 T15 | T16 T17 |
// with half pixel (i.e we set i to i+1 above, and always prefix)
// 0: H1P T00 | R2P T01 | T02 RUN | T03 LEN | T04 T05 | T06 T07 |
// 1: H1P T01 | RUN T02 | LEN T03 | T04 T05 | T06 T07 |
// 2: H1P T02 | R2P T03 | T04 RUN | T05 LEN | T06 T07 |
// 3: H1P T03 | RUN T04 | LEN T05 | T06 T07 |
// 4: H1P T04 | R2P T05 | T06 RUN | T05 LEN | T06 T07 |
// 5: H1P T05 | RUN T06 | LEN T07 |
// 6: H1P T06 | R2P T07 | T10 RUN | T11 LEN | T12 T13 | T14 T15 | T16 T17 |
// 7: H1P T07 | RUN T10 | LEN T11 | T12 T13 | T14 T15 | T16 T17 |
int run_length = (8 - (i & 7u)) + COUNT * 8 - 3;
#ifndef DISABLE_HPIXELS
if (half_pixel) {
// prefix with the half pixel
*output++ = COMPOSABLE_RAW_1P_2CYCLE;
*output++ = *data++;
#ifdef DEBUG_HALF_PIXEL
debug_half_pixel_count++;
#endif
if (i == 8) {
// cope with the case where we've stepped onto the new tile after the half-pixel
off = span_offsets[8 * map0[1]];
data = (uint16_t *) (tiles_tile_data.blob.bytes + off);
i = 0;
run_length -= 8;
}
}
#endif
if (i & 1) {
*output++ = COMPOSABLE_RAW_2P;
*output++ = *data++;
if (i == 7) {
// cope with the case where we've stepped onto the new tile
off = span_offsets[8 * map0[1]];
data = (uint16_t *) (tiles_tile_data.blob.bytes + off);
i = 1;
} else {
i += 2;
}
run_length -= 2;
#ifdef DEBUG_HALF_PIXEL
debug_half_pixel_count+=4;
#endif
*output++ = *data++;
*output++ = COMPOSABLE_RAW_RUN;
} else {
*output++ = COMPOSABLE_RAW_RUN;
}
*output++ = *data++;
*output++ = run_length;
for (; i < 7; i++) {
*output++ = *data++;
}
assert(0 == (3u & (intptr_t) output));
// setup our first chain segment (to point into our buffer here)
buf[0] = ((uint32_t *) output) - output32;
buf[1] = host_safe_hw_ptr(output32);
// end of line
uint32_t *eol_base = (uint32_t *) output;
if (eol_pixels || half_pixel) {
off = span_offsets[8 * *map++];
data = (uint16_t *) (tiles_tile_data.blob.bytes + off);
#ifdef DEBUG_HALF_PIXEL
debug_half_pixel_count+=eol_pixels * 2;
#endif
switch (eol_pixels) {
// we could be slightly more optimal in the non half pixel case, but don't reall care
case 0:
break;
case 1:
*output++ = COMPOSABLE_RAW_1P;
*output++ = *data++;
break;
case 2:
*output++ = COMPOSABLE_RAW_2P;
*output++ = *data++;
*output++ = *data++;
break;
default:
*output++ = COMPOSABLE_RAW_RUN;
*output++ = *data++;
*output++ = eol_pixels - 3;
for (int i = 1; i < eol_pixels; i++) {
*output++ = *data++;
}
break;
}
#ifndef DISABLE_HPIXELS
if (half_pixel) {
*output++ = COMPOSABLE_RAW_1P_2CYCLE;
*output++ = *data++;
#ifdef DEBUG_HALF_PIXEL
debug_half_pixel_count++;
#endif
}
#endif
}
*output++ = COMPOSABLE_RAW_1P;
*output++ = 0;
if (2u & (intptr_t) output) {
*output++ = COMPOSABLE_EOL_ALIGN;
} else {
*output++ = COMPOSABLE_EOL_SKIP_ALIGN;
*output++ = 0xffff; // eye catcher
}
// setup our last chain segment (to point into our buffer here)
output32[-4] = ((uint32_t *) output) - eol_base;; //len
output32[-3] = host_safe_hw_ptr(eol_base);
#ifdef DEBUG_HALF_PIXEL
debug_half_pixel_count += (run_length+3)*2;
if (y==vpos) printf("%d %d %d %d\n", (x>>COORD_SHIFT)&7, half_pixel, debug_half_pixel_count, eol_pixels);
#endif
assert(0 == (3u & (intptr_t) output));
assert((uint32_t *) output <= (buf + dest->data_max));
dest->data_used = (uint16_t) (output32 -
buf); // todo we don't want to include the off the end data in the "size" for the dma
#endif
// why was this here, it is buf anyway!
// dest->data = buf;
#if PICO_SCANVIDEO_PLANE_COUNT > 1
#if !PICO_SCANVIDEO_PLANE2_VARIABLE_FRAGMENT_DMA
assert(false);
#endif
buf = dest->data2;
output32 = buf;
uint32_t *inline_data = output32 + PICO_SCANVIDEO_MAX_SCANLINE_BUFFER2_WORDS / 2;
output = (uint16_t *) inline_data;
uint32_t *base = (uint32_t *) output;
#define MAKE_SEGMENT \
assert(0 == (3u & (intptr_t)output)); \
*output32++ = (uint32_t*)output - base; \
*output32++ = host_safe_hw_ptr(base); \
base = (uint32_t *)output;
int wibble = (scanvideo_frame_number(dest->scanline_id) >> 2) % 7;
for (int q = 0; q < x_sprites; q++) {
// nice if we can do two black pixel before
*output++ = COMPOSABLE_RAW_RUN;
*output++ = 0;
*output++ = galaga_tile_data.width + 2 - 3;
*output++ = 0;
MAKE_SEGMENT;
span_offsets = galaga_tile_data.span_offsets + (q + wibble) * galaga_tile_data.height +
(y - vpos);//(y%galaga_tile_data.count 7u);
off = span_offsets[0];
data = (uint16_t *) (galaga_tile_data.blob.bytes + off);
*output32++ = galaga_tile_data.width / 2;
*output32++ = host_safe_hw_ptr(data);
}
*output++ = COMPOSABLE_RAW_1P;
*output++ = 0;
*output++ = COMPOSABLE_EOL_SKIP_ALIGN;
*output++ = 0xffff;
MAKE_SEGMENT;
// end of dma chain
*output32++ = 0;
*output32++ = 0;
assert(output32 < inline_data);
assert((uint32_t *) output <= (buf + dest->data2_max));
dest->data2_used = (uint16_t) (output32 -
buf); // todo we don't want to include the inline data in the "size" for the dma
#endif
dest->status = SCANLINE_OK;
return true;
}
void go_core1(void (*execute)()) {
multicore_launch_core1(execute);
}
int main(void) {
#if PICO_SCANVIDEO_48MHZ
set_sys_clock_48mhz();
#endif
setup_default_uart();
#if PICO_NO_HARDWARE
//#include <math.h>
// for(int i = 0; i<64;i++) {
// printf("%d, ", (int)(0x7f*cos(i*M_PI/32)));
// }
// printf("\n");
#endif
//#include "level0.h"
// uint8_t *p = level0_dat;
// int w = 1<<p[0];
// int h = 1<<p[1];
// printf("const int level0_map_width = %d;\n", w);
// printf("const int level0_map_height = %d;\n", h);
// printf("const uint16_t level0_map = {\n");
// for (int i = 0; i < w*h; i += 32) {
// printf("\t\t");
// for (int j = i; j < w*h && j < (i + 32); j++) {
// uint8_t *q = p + 2 + j*2;
// printf("0x%04x, ", q[0]*256+q[1]);
// }
// printf("\n");
// }
// printf("};\n");
return video_main();
}