-
Notifications
You must be signed in to change notification settings - Fork 9
/
pipelinec_app.c
264 lines (236 loc) · 7.88 KB
/
pipelinec_app.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
// Set the target FPGA part
#define LITEX_INTEGRATION
// CflexHDL compile setting
#define CCOMPILE
// Common PipelineC includes
#include "compiler.h"
#include "uintN_t.h"
#include "intN_t.h"
#include "float_e_m_t_helper.h" // Variable mantissa sizes
// Temp config work around since no preprocessor command line args
// https://github.com/JulianKemmerer/PipelineC/issues/56
#include "pipelinec_app_config.h"
#include "pipelinec_compat.h"
#include "fixed_type.h"
// Reset state func needs special marker to not be synthesized alone
// ~sorta PipelineC constexpr/const marker
#pragma FUNC_WIRES reset_state
#pragma FUNC_WIRES reset_state0
// Access to board buttons
#include "buttons/buttons.c"
// RGB color decomp requires a 3x pixel clock
#if COLOR_DECOMP == 3
#define PIXEL_OVER_CLK_RATIO 3 /*Default is 1 (no overclock)*/
#endif
// Litex provides its own external VGA timing
// and uses generic external VGA output
#ifdef LITEX_INTEGRATION
#include "vga/external_vga_timing.c"
#include "vga/external_vga_output.c"
#elif defined(POCKET_INTEGRATION)
// Pocket build generates timing internally
// But has non-pmod generic external vga output
#include "vga/external_vga_output.c"
#else
// Otherwise default to Arty video output w/ pmod
//#include "vga/vga_pmod.c"
#include "dvi/dvi_pmod.c"
#endif
// Include the code for ray tracer
#define get_scene() state.scene
#include "tr_pipelinec.gen.c"
// Define the user created frame clock
#define FRAME_CLK_MHZ 6e-5 // 60Hz
uint1_t frame_clock;
DECL_OUTPUT(uint1_t, frame_clock)
uint1_t frame_clock_rising_edge;
uint1_t frame_clock_falling_edge;
// Helper func to drive frame clock from isolated static frame_clock_reg
// Running on pixel clock, maybe include inside vga_timing?
void frame_clock_logic(uint16_t x, uint16_t y, bool active)
{
// Need to make ~50% duty cycle frame clock
static uint1_t frame_clock_reg;
// Drive clock from register
frame_clock = frame_clock_reg;
frame_clock_rising_edge = 0;
frame_clock_falling_edge = 0;
// Falling edge mid frame (does not update state)
if( active &
(x==(FRAME_WIDTH/2)) &
(y==(FRAME_HEIGHT/2))
){
frame_clock_reg = 0;
frame_clock_falling_edge = 1;
}
// Rising edge at end of frame (state updated during back porch)
else if(active &
(x==(FRAME_WIDTH-1)) &
(y==(FRAME_HEIGHT-1))
){
frame_clock_reg = 1;
frame_clock_rising_edge = 1;
}
}
// Pocket flow cannot use CLK_MHZ to declare user clocks
// Since Quartus not able to constrain user generated clocks see issues:
// https://github.com/JulianKemmerer/PipelineC/issues/138
// https://github.com/JulianKemmerer/PipelineC/issues/137
#ifndef POCKET_INTEGRATION
CLK_MHZ(frame_clock, FRAME_CLK_MHZ)
// Make pixel clock look like its user generated internally
// so that, regardless of frequency, the name is always the same
// This does not seem to work with
uint1_t pixel_clock;
CLK_MHZ(pixel_clock, PIXEL_CLK_MHZ)
// Connect constant name top level port to internal pixel_clock
MAIN_MHZ(pixel, PIXEL_CLK_MHZ)
#pragma FUNC_WIRES pixel
void pixel(uint1_t clock) // top level port is "pixel_clock"
{
pixel_clock = clock;
}
#endif //ifndef POCKET_INTEGRATION
// UART modules that augment buttons behavior
// Run uart at pixel clock to avoid multiple clocks
#define UART_CLK_MHZ PIXEL_CLK_MHZ
#include "uart/uart_mac.c"
// Receive bytes over uart
// continuosuly updating rx buffer
uint4_t uart_buttons;
MAIN_MHZ(uart_buttons_rx, UART_CLK_MHZ)
void uart_buttons_rx()
{
static uint4_t uart_buttons_reg;
uart_buttons = uart_buttons_reg;
// Always ready for incoming uart bytes
uart_rx_mac_out_ready = 1;
// Overwrite buffer as they arrive
if(uart_rx_mac_word_out.valid)
{
uart_buttons_reg = uart_rx_mac_word_out.data;
}
}
// Capture stable button value in time for frame clock rising edge
// Game logic uses OR of buttons or uart data
uint4_t buttons_or_uart;
#pragma ASYNC_WIRE buttons_or_uart // Read by slow frame clock domain
// Transmit out the button value used to update the state
MAIN_MHZ(uart_buttons_tx, UART_CLK_MHZ)
void uart_buttons_tx()
{
static uint4_t buttons_or_uart_reg;
buttons_or_uart = buttons_or_uart_reg;
// Upon rising edge of frame clock,
// transmit the buttons_or_uart state that was stable and used by full_update
uart_tx_mac_word_in.valid = frame_clock_rising_edge;
uart_tx_mac_word_in.data = buttons_or_uart_reg;
// TODO if !uart_tx_mac_in_ready then overflow/error
// Upon falling edge of frame clock sample the button state
// the game will use to update next state at the rising edge
if(frame_clock_falling_edge)
{
// OR of current buttons or current and uart state
// is what game logic samples for use
buttons_or_uart_reg = buttons | uart_buttons;
}
}
// User input
typedef struct user_input_t
{
uint1_t jump_pressed;
uint1_t reset_pressed;
}user_input_t;
inline user_input_t get_user_input()
{
user_input_t i;
// For now only exists in hardware
#ifdef __PIPELINEC__
// Select which buttons and switches do what?
i.jump_pressed = buttons_or_uart >> 0;
i.reset_pressed = buttons_or_uart >> 3;
#else
// TODO user IO for running as C code
i.jump_pressed = 1;
i.reset_pressed = 0;
#endif
return i;
}
// Per frame next state comb. logic runnning on frame clock
volatile full_state_t state; // The state wire, shared global wire
#pragma ASYNC_WIRE state // Async to pixel clock domain where read
MAIN_MHZ(frame_logic, FRAME_CLK_MHZ)
void frame_logic()
{
static uint1_t power_on_reset = 1;
static full_state_t state_reg; // The state register
state = state_reg; // Drives state wire directly
// Read user input
user_input_t ui = get_user_input();
// Normal next state update
state_reg = full_update(state_reg, ui.reset_pressed | power_on_reset, ui.jump_pressed);
power_on_reset = 0;
}
// Helper to isolate static pixel_t buffer register from pixel_logic stateless pipeline
typedef struct color_decomp_rgb_buffer_t{
vga_signals_t vga_signals;
pixel_t pixel;
}color_decomp_rgb_buffer_t;
color_decomp_rgb_buffer_t color_decomp_rgb_buffer(
uint8_t current_color_channel, uint8_t color_value,
vga_signals_t vga_signals
){
color_decomp_rgb_buffer_t o;
static pixel_t pixel_reg;
o.pixel = pixel_reg;
static vga_signals_t vga_signals_reg;
o.vga_signals = vga_signals_reg;
// Register VGA signals
vga_signals_reg = vga_signals;
// vga_signals.valid is true when channel=2
// Register one color of pixel at a time
if(current_color_channel==0){
pixel_reg.r = color_value;
}else if(current_color_channel==1){
pixel_reg.g = color_value;
}else{
pixel_reg.b = color_value;
}
return o;
}
// Logic running on pixel clock, mostly render_pixel pipeline
MAIN_MHZ(pixel_logic, PIXEL_CLK_MHZ)
void pixel_logic()
{
// VGA timing for fixed resolution (maybe overclocked)
vga_signals_t vga_signals = vga_timing();
// Use VGA timing to derive frame clock
frame_clock_logic(vga_signals.pos.x, vga_signals.pos.y, vga_signals.active & vga_signals.valid);
#ifndef COLOR_DECOMP
// Render the pixel at x,y pos
// Scene is wired in from frame logic domain
pixel_t color = render_pixel(vga_signals.pos.x, vga_signals.pos.y);
#else
#if COLOR_DECOMP == 1
pixel_t color;
color = render_pixel(vga_signals.pos.x, vga_signals.pos.y, 0);
color.g = color.r;
color.b = color.r;
#else
// R,G,B for this X,Y position serially into in pipeline
uint8_t current_color_channel = vga_signals.overclock_counter; // 0,1,2 repeating PIXEL_OVER_CLK_RATIO
pixel_t color = render_pixel(vga_signals.pos.x, vga_signals.pos.y, current_color_channel);
// Buffer serial R,G,B output from pipeline
color_decomp_rgb_buffer_t rgb_buf = color_decomp_rgb_buffer(
current_color_channel, color.r, // r channel has selected color value
vga_signals
);
vga_signals = rgb_buf.vga_signals;
color = rgb_buf.pixel;
#endif
#endif //COLOR_DECOMP
// Drive output signals/registers
pmod_register_outputs(vga_signals, color);
}
#include "opt_primitives.c"
#include "fp_overloads.c"