-
Notifications
You must be signed in to change notification settings - Fork 5
/
cvideo.c
379 lines (297 loc) · 14.1 KB
/
cvideo.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
/*-----------------------------------------------
* Pico-Composite8 Composite Video, 8-bit output
*
* 2021-06-04 [email protected]
*-----------------------------------------------
*/
/* Based on:
*-----------------------------------------------
* Title: Pico-mposite Video Output
* Author: Dean Belfield
* Created: 26/01/2021
* Last Updated: 15/02/2021
*-----------------------------------------------
*/
/*
Copyright (C) 2021 Bill Neisius <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define TESTPATTERN
#include <stdlib.h>
//#include <stdio.h>
#include "memory.h"
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "hardware/pio.h"
#include "hardware/dma.h"
#include "hardware/irq.h"
void cvideo_configure_pio_dma(PIO pio, uint sm, uint dma_channel, size_t buffer_size_words);
void cvideo_dma_handler(void);
#include "cvideo.pio.h" // The assembled PIO code
#define width 512 // Bitmap width in pixels
#define height 384 // Bitmap height in pixels
/*-------------------------------------------------------------------*/
/*------------------Video Standard-----------------------------------*/
/*-------------------------------------------------------------------*/
const int VIDEO_frame_lines = 525;
const int VIDEO_frame_lines_visible = 480;
const float VIDEO_aspect_ratio = 4.0/3.0;
const float VIDEO_horizontal_freq = 15750.0;
const float VIDEO_h_FP_usec = 1.5; // front porch
const float VIDEO_h_SYNC_usec = 4.7; // sync
const float VIDEO_h_BP_usec = 4.7; // back porch
const float VIDEO_h_EP_usec = 2.3; // equalizing pulse
/*-------------------------------------------------------------------*/
/*------------------Horizontal Derived-------------------------------*/
/*-------------------------------------------------------------------*/
const int HORIZ_visible_dots = VIDEO_frame_lines_visible * VIDEO_aspect_ratio; // full frame width
const float HORIZ_usec = 1000000.0 / VIDEO_horizontal_freq;
const float HORIZ_usec_dot = (HORIZ_usec - VIDEO_h_FP_usec - VIDEO_h_SYNC_usec - VIDEO_h_BP_usec) / HORIZ_visible_dots;
const int HORIZ_dots = HORIZ_usec / HORIZ_usec_dot;
const int HORIZ_FP_dots = VIDEO_h_FP_usec / HORIZ_usec_dot;
const int HORIZ_SYNC_dots = VIDEO_h_SYNC_usec / HORIZ_usec_dot;
const int HORIZ_BP_dots = VIDEO_h_BP_usec / HORIZ_usec_dot;
const int HORIZ_EP_dots = VIDEO_h_EP_usec / HORIZ_usec_dot; // equalizing pulse during vertical sync
const int HORIZ_pixel_start = (HORIZ_visible_dots - width) / 2 + HORIZ_SYNC_dots + HORIZ_BP_dots;
/*-------------------------------------------------------------------*/
/*------------------Vertical Derived---------------------------------*/
/*-------------------------------------------------------------------*/
const int VERT_scanlines = VIDEO_frame_lines / 2; // one field
const int VERT_vblank = (VIDEO_frame_lines - VIDEO_frame_lines_visible) / 2; // vertical blanking, one field
const int VERT_border = (VERT_scanlines - VERT_vblank - height/2) / 2;
const int VERT_bitmap = height/2;
/*-------------------------------------------------------------------*/
/*------------------PIO----------------------------------------------*/
/*-------------------------------------------------------------------*/
const float PIO_clkdot = 1.0; // PIO instructions per dot
const float PIO_sysclk = 125000000.0; // default Pico system clock
const float PIO_clkdiv = PIO_sysclk / VIDEO_horizontal_freq / PIO_clkdot / HORIZ_dots;
/*-------------------------------------------------------------------*/
/*------------------Gray Scale---------------------------------------*/
/*-------------------------------------------------------------------*/
// NTSC in IRE units+40: SYNC = 0; BLANK = 40; BLACK = 47.5; WHITE = 140
const int WHITE = 255;
const int BLACK = 255.0 / 140.0 * 47.5;
const int BLANK = 255.0 / 140.0 * 40.0;
const int SYNC = 0;
#define border_colour BLACK
#define state_machine 0 // The PIO state machine to use
uint dma_channel; // DMA channel for transferring hsync data to PIO
uint vline = 9999; // Current video line being processed
uint bline = 0; // Line in the bitmap to fetch
uint field = 0; // field, even/odd
int bmIndex = 0;
int bmCount = 0;
// bitmap buffer
#ifdef TESTPATTERN
#include "indian.h"
//#include "gradient4.h"
//#include "demo.h"
unsigned char * bitmap = (unsigned char *)indian;
#else
unsigned char bitmap[width*height]={[0 ... width*height-1] = WHITE};
#endif
unsigned char * vsync_ll; // buffer for a vsync line with a long/long pulse
unsigned char * vsync_ss; // Buffer for an equalizing line with a short/short pulse
unsigned char * vsync_bb; // Buffer for a vsync blanking
unsigned char * vsync_ssb; // Buffer and a half for equalizing/blank line
unsigned char * border; // Buffer for a vsync line for the top and bottom borders
unsigned char * pixel_buffer[2]; // Double-buffer for the pixel data scanlines
volatile bool changeBitmap = false;
/*-------------------------------------------------------------------*/
void second_core() {
unsigned char * dataCount = (unsigned char *)0x10050000;
unsigned char * dataStart = (unsigned char *)0x10050001;
//int bmMax = *dataCount / 4; // using the low-res bitmaps for testing
int bmMax = *dataCount;
bmIndex = 0;
while (true) {
#ifdef TESTPATTERN
#else
while (vline >= VERT_vblank ) {
busy_wait_us(HORIZ_usec);
}
changeBitmap = true; // DMA interrupt now starts sending BLACK instead of bitmap
bmIndex++;
if ( bmIndex >= bmMax ) {
bmIndex = 0;
}
memcpy(bitmap, dataStart + (width * height * bmIndex), width*height);
// settling time
busy_wait_us(HORIZ_usec*VERT_scanlines);
while (vline >= VERT_vblank ) {
busy_wait_us(HORIZ_usec);
}
changeBitmap = false; // ...switch back to displaying bitmap
//sleep_ms(1000); // sleep_ms() causes a visible glitch
busy_wait_us(1000000);
#endif
}
}
/*-------------------------------------------------------------------*/
int main() {
// stdio_init_all();
// sleep_ms(2000);
// printf("Start of program\n");
multicore_launch_core1(second_core);
vsync_ll = (unsigned char *)malloc(HORIZ_dots);
memset(vsync_ll, SYNC, HORIZ_dots); // vertical sync/serrations
memset(vsync_ll + (HORIZ_dots>>1) - HORIZ_SYNC_dots, BLANK, HORIZ_SYNC_dots);
memset(vsync_ll + HORIZ_dots - HORIZ_SYNC_dots, BLANK, HORIZ_SYNC_dots);
vsync_ss = (unsigned char *)malloc(HORIZ_dots);
memset(vsync_ss, BLANK, HORIZ_dots); // vertical equalizing
memset(vsync_ss, SYNC, HORIZ_EP_dots);
memset(vsync_ss + (HORIZ_dots>>1), SYNC, HORIZ_EP_dots);
vsync_bb = (unsigned char *)malloc(HORIZ_dots);
memset(vsync_bb, BLANK, HORIZ_dots); // vertical blanking
memset(vsync_bb, SYNC, HORIZ_SYNC_dots);
vsync_ssb = (unsigned char *)malloc(HORIZ_dots+(HORIZ_dots>>1));
memset(vsync_ssb, BLANK, HORIZ_dots + (HORIZ_dots>>1)); // vertical equalizing/blanking
memset(vsync_ssb, SYNC, HORIZ_EP_dots);
memset(vsync_ssb + (HORIZ_dots>>1), SYNC, HORIZ_EP_dots);
// This bit pre-builds the border scanline and pixel buffers
border = (unsigned char *)malloc(HORIZ_dots);
memset(border, border_colour, HORIZ_dots); // Fill the border with the border colour
memset(border, SYNC, HORIZ_SYNC_dots); // Add the hsync pulse
memset(border + HORIZ_SYNC_dots, BLANK, HORIZ_BP_dots);
memset(border + HORIZ_dots - HORIZ_FP_dots, BLANK, HORIZ_FP_dots); // front porch
pixel_buffer[0] = (unsigned char *)malloc(HORIZ_dots);
memcpy(pixel_buffer[0], border, HORIZ_dots); // pixel buffer
pixel_buffer[1] = (unsigned char *)malloc(HORIZ_dots);
memcpy(pixel_buffer[1], border, HORIZ_dots); // pixel buffer
// Initialise the PIO
PIO pio = pio0;
uint offset = pio_add_program(pio, &cvideo_program); // Load up the PIO program
pio_sm_set_enabled(pio, state_machine, false); // Disable the PIO state machine
pio_sm_clear_fifos(pio, state_machine); // Clear the PIO FIFO buffers
cvideo_initialise_pio(pio, state_machine, offset, 0, 8, PIO_clkdiv); // Initialise the PIO (function in cvideo.pio)
dma_channel = dma_claim_unused_channel(true); // Claim a DMA channel for the hsync transfer
cvideo_configure_pio_dma(pio, state_machine, dma_channel, HORIZ_dots); // Hook up the DMA channel to the state machine
// And kick everything off
pio_sm_set_enabled(pio, state_machine, true); // Enable the PIO state machine
while (true) { // And then just loop doing nothing
tight_loop_contents();
}
}
/*-------------------------------------------------------------------*/
// The DMA interrupt handler
// This is triggered by DMA_IRQ_0
void cvideo_dma_handler(void) {
if ( ++vline <= VERT_scanlines ) {
} else {
vline = 0;
bline = 0;
field = ++field & 0x01;
}
while (true) {
if ( vline <= VERT_vblank ) {
switch(vline) {
case 0:
// for some reason interlace fails unless there's a 30usec delay here:
busy_wait_us(HORIZ_usec/2);
if ( field ) {
// odd field - blank, full line
dma_channel_set_read_addr(dma_channel, vsync_bb, true);
} else {
// even field - blank, half line
dma_channel_set_trans_count(dma_channel, HORIZ_dots/2, false);
dma_channel_set_read_addr(dma_channel, vsync_bb, true);
}
break;
case 1:
dma_channel_set_trans_count(dma_channel, HORIZ_dots, false); // reset transfer size
case 2 ... 3:
// send 3 vsync_ss - 'equalizing pulses'
dma_channel_set_read_addr(dma_channel, vsync_ss, true);
break;
case 4 ... 6:
// send 3 vsync_ll - 'vertical sync/serrations'
dma_channel_set_read_addr(dma_channel, vsync_ll, true);
break;
case 7 ... 8:
// send 3 vsync_ss - 'equalizing pulses'
dma_channel_set_read_addr(dma_channel, vsync_ss, true);
break;
case 9:
if ( field ) {
// odd field - equalizing pulse, full line
dma_channel_set_read_addr(dma_channel, vsync_ss, true);
} else {
//even field - equalizing pulse, line and a half
dma_channel_set_trans_count(dma_channel, HORIZ_dots + HORIZ_dots/2, false);
dma_channel_set_read_addr(dma_channel, vsync_ssb, true);
}
break;
case 10:
// everything back to normal
dma_channel_set_trans_count(dma_channel, HORIZ_dots, false); // reset transfer size
default:
// send BLANK till end of vertical blanking
dma_channel_set_read_addr(dma_channel, vsync_bb, true);
break;
}
break;
}
if ( vline <= VERT_vblank + VERT_border ) {
if (changeBitmap) {
dma_channel_set_read_addr(dma_channel, vsync_bb, true);
} else {
dma_channel_set_read_addr(dma_channel, border, true);
if ( vline == VERT_vblank + VERT_border ) {
memcpy(pixel_buffer[bline & 1] + HORIZ_pixel_start, bitmap+(bline*2+field)*width, width);
}
}
break;
}
if ( vline <= VERT_vblank + VERT_border + VERT_bitmap ) {
if (changeBitmap) {
dma_channel_set_read_addr(dma_channel, vsync_bb, true);
} else {
dma_channel_set_read_addr(dma_channel, pixel_buffer[bline++ & 1], true); // Set the DMA to read from one of the pixel_buffers
memcpy(pixel_buffer[bline & 1] + HORIZ_pixel_start, bitmap+(bline*2+field)*width, width); // And memcpy the next scanline
}
break;
}
// otherwise, just output border until end of scanlines
if (changeBitmap) {
dma_channel_set_read_addr(dma_channel, vsync_bb, true);
} else {
dma_channel_set_read_addr(dma_channel, border, true);
}
break;
}
// Finally, clear the interrupt request ready for the next horizontal sync interrupt
dma_hw->ints0 = 1u << dma_channel;
}
/*-------------------------------------------------------------------*/
// Configure the PIO DMA
// Parameters:
// - pio: The PIO to attach this to
// - sm: The state machine number
// - dma_channel: The DMA channel
// - buffer_size_words: Number of bytes to transfer
//
void cvideo_configure_pio_dma(PIO pio, uint sm, uint dma_channel, size_t buffer_size_words) {
pio_sm_clear_fifos(pio, sm);
dma_channel_config c = dma_channel_get_default_config(dma_channel);
channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
channel_config_set_read_increment(&c, true);
channel_config_set_dreq(&c, pio_get_dreq(pio, sm, true));
dma_channel_configure(dma_channel, &c,
&pio->txf[sm], // Destination - PIO queue
vsync_bb, // Source - Equalizing Pulses
buffer_size_words, // Number of transfers
true // Start - queue the Source to the Destination
);
dma_channel_set_irq0_enabled(dma_channel, true);
irq_set_exclusive_handler(DMA_IRQ_0, cvideo_dma_handler);
irq_set_enabled(DMA_IRQ_0, true);
}