-
Notifications
You must be signed in to change notification settings - Fork 46
/
shared_dual_frame_buffer.c
463 lines (438 loc) · 18.1 KB
/
shared_dual_frame_buffer.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
// Code for a single frame buffer
#include "frame_buffer.c"
// Dual frame buffers
#define N_FRAME_BUFFERS 2
// Frame buffer RAM application/user global wires for use once anywhere in code
frame_buffer_in_ports_t frame_buffers_in_ports[N_FRAME_BUFFERS][N_FRAME_BUF_PORTS];
frame_buffer_out_ports_t frame_buffers_out_ports[N_FRAME_BUFFERS][N_FRAME_BUF_PORTS];
// Frame buffer RAMs wired to global wires
MAIN_MHZ(frame_buffer_rams, DEV_CLK_MHZ)
void frame_buffer_rams()
{
uint32_t i;
for (i = 0; i < N_FRAME_BUFFERS; i+=1)
{
frame_buf_ram_module_t frame_buf_ram_o = frame_buf_ram_module(frame_buffers_in_ports[i]);
frame_buffers_out_ports[i] = frame_buf_ram_o.frame_buffer_out_ports;
}
}
//////////////// EXPOSE DUAL FRAME BUFFERS AS SHARED RESOURCES //////////////////////
// See docs: https://github.com/JulianKemmerer/PipelineC/wiki/Shared-Resource-Bus
#include "shared_resource_bus.h"
// Will be two instances of same type of shared resource
SHARED_BUS_TYPE_DEF(
frame_buf_bus_t,
uint32_t, n_pixels_t, uint1_t,
uint32_t, n_pixels_t
)
// Each device is a dual port RAM used by host threads
#define NUM_DEV_PORTS N_FRAME_BUF_PORTS
#define NUM_HOST_PORTS (NUM_TOTAL_THREADS+1) // User threads + one VGA display reader thread
// First frame buffer
SHARED_BUS_DECL(
frame_buf_bus_t,
uint32_t, n_pixels_t, uint1_t,
uint32_t, n_pixels_t,
frame_buf0_shared_bus,
NUM_HOST_PORTS,
NUM_DEV_PORTS
)
// Second frame buffer
SHARED_BUS_DECL(
frame_buf_bus_t,
uint32_t, n_pixels_t, uint1_t,
uint32_t, n_pixels_t,
frame_buf1_shared_bus,
NUM_HOST_PORTS,
NUM_DEV_PORTS
)
// TODO #include "shared_res_fifos/fifo0.h"
// When generates #include "clock_crossing/write_req_fifo0.h"
// #include "clock_crossing/write_data_fifo0.h" , etc
// COULD EVEN DO whole 'arrays'
// #include "shared_res_fifos/fifo[0:4].h"
// First frame buffer fifos
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 0)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 1)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 2)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 3)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 4)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 5)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 6)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 7)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 8)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 9)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 10)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 11)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 12)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 13)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 14)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 15)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf0_shared_bus, 16)
// Second frame buffer fifos
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 0)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 1)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 2)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 3)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 4)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 5)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 6)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 7)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 8)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 9)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 10)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 11)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 12)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 13)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 14)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 15)
SHARED_BUS_ASYNC_FIFO_DECL(frame_buf_bus_t, frame_buf1_shared_bus, 16)
// Wire ASYNC FIFOs to dev-host wires
MAIN_MHZ(host_side_fifo_wiring, HOST_CLK_MHZ)
void host_side_fifo_wiring()
{
// First frame buffer
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 0)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 1)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 2)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 3)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 4)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 5)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 6)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 7)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 8)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 9)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 10)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 11)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 12)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 13)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 14)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 15)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 16)
// Second frame buffer
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 0)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 1)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 2)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 3)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 4)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 5)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 6)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 7)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 8)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 9)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 10)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 11)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 12)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 13)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 14)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 15)
SHARED_BUS_ASYNC_FIFO_HOST_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 16)
}
MAIN_MHZ(dev_side_fifo_wiring, DEV_CLK_MHZ)
void dev_side_fifo_wiring()
{
// First frame buffer
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 0)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 1)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 2)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 3)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 4)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 5)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 6)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 7)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 8)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 9)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 10)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 11)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 12)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 13)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 14)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 15)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf0_shared_bus, 16)
// Second frame buffer
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 0)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 1)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 2)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 3)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 4)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 5)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 6)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 7)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 8)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 9)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 10)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 11)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 12)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 13)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 14)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 15)
SHARED_BUS_ASYNC_FIFO_DEV_WIRING(frame_buf_bus_t, frame_buf1_shared_bus, 16)
}
// User application uses 5 channel AXI-like shared bus wires for frame buffer control
// Need to wire up RAM with AXI style req,resp which uses valid ready
// resp/req/data channels are independent
// TODO what kind of helper funcs are good for generic support connecting to bus?
// How frame buffer port is 'converted' to shared bus connection
typedef enum frame_buf_ram_port_dev_ctrl_state_t
{
REQ,
DATA,
WR_RESP
}frame_buf_ram_port_dev_ctrl_state_t;
typedef struct frame_buf_ram_port_dev_ctrl_t{
frame_buffer_in_ports_t to_frame_buf;
frame_buf_bus_t_dev_to_host_t to_host;
}frame_buf_ram_port_dev_ctrl_t;
frame_buf_ram_port_dev_ctrl_t frame_buf_ram_port_dev_ctrl(
frame_buffer_out_ports_t from_frame_buf,
frame_buf_bus_t_host_to_dev_t from_host
)
{
// 5 channels between host and device
// Request to start
// Read req (addr)
// Write req (addr)
// Exchange of data
// Read data+resp (data+err)
// Write data (data bytes)
// Write resp (err code)
// Each channel has a valid+ready handshake buffer
static frame_buf_bus_t_buffer_t bus_buffer;
// Allow one req-data-resp in flight at a time:
// Wait for inputs to arrive in input handshake regs w/ things needed req/data
// Start operation
// Wait for outputs to arrive in output handshake regs, wait for handshake to complete
static frame_buf_ram_port_dev_ctrl_state_t state;
static uint1_t op_is_read;
static uint1_t read_has_priority; // Toggles for read-write sharing of bus
// Signal ready for inputs if buffers are empty
// Static since o.to_frame_buf written to over multiple cycles
static frame_buf_ram_port_dev_ctrl_t o;
o.to_host.write.req_ready = !bus_buffer.write_req.valid;
o.to_host.read.req_ready = !bus_buffer.read_req.valid;
o.to_host.write.data_ready = !bus_buffer.write_data.valid;
// Drive outputs from buffers
o.to_host.read.data = bus_buffer.read_data;
o.to_host.write.resp = bus_buffer.write_resp;
// Clear output buffers when ready
if(from_host.read.data_ready)
{
bus_buffer.read_data.valid = 0;
}
if(from_host.write.resp_ready)
{
bus_buffer.write_resp.valid = 0;
}
// State machine logic feeding signals into ram
o.to_frame_buf.wr_data = bus_buffer.write_data.burst.data_word.user;
o.to_frame_buf.wr_enable = 0;
o.to_frame_buf.valid = 0;
if(state==REQ)
{
// Wait for a request
// Choose a request to handle, read or write
uint1_t do_read;
uint1_t do_write;
if(read_has_priority)
{
// Read priority
if(bus_buffer.read_req.valid)
{
do_read = 1;
}
else if(bus_buffer.write_req.valid)
{
do_write = 1;
}
}
else
{
// Write priority
if(bus_buffer.write_req.valid)
{
do_write = 1;
}
else if(bus_buffer.read_req.valid)
{
do_read = 1;
}
}
if(do_read)
{
op_is_read = 1;
o.to_frame_buf.addr = bus_buffer.read_req.data.user;
bus_buffer.read_req.valid = 0; // Done w req now
read_has_priority = 0; // Writes next
o.to_frame_buf.valid = 1; // addr completes valid inputs, no input read data
o.to_frame_buf.id = bus_buffer.read_req.id;
// Waiting for output read data next
state = DATA;
}
else if(do_write)
{
op_is_read = 0;
o.to_frame_buf.addr = bus_buffer.write_req.data.user;
o.to_frame_buf.id = bus_buffer.write_req.id;
bus_buffer.write_req.valid = 0; // Done w req now
read_has_priority = 1; // Reads next
// Write stil needs data still before valid input to frame buf
state = DATA;
}
}
// TODO pass through write req and data
else if((state==DATA) & !op_is_read) // Write data into RAM
{
// Wait until valid write data
if(bus_buffer.write_data.valid)
{
o.to_frame_buf.wr_enable = 1;
o.to_frame_buf.valid = 1;
// AXI3-like interleaved writes only o.to_frame_buf.id = bus_buffer.write_data.id;
bus_buffer.write_data.valid = 0; // Done w data now
state = WR_RESP;
}
}
// RAM WAS HERE
// Outputs from RAM flow into output handshake buffers
if(from_frame_buf.valid)
{
// Have valid output, read or write?
if(from_frame_buf.wr_enable)
{
// Output write resp, err code unused
bus_buffer.write_resp.valid = 1;
bus_buffer.write_resp.id = from_frame_buf.id;
}
else
{
// Output read data
bus_buffer.read_data.valid = 1;
bus_buffer.read_data.id = from_frame_buf.id;
bus_buffer.read_data.burst.last = 1;
bus_buffer.read_data.burst.data_resp.user = from_frame_buf.rd_data;
}
}
// State machine logic dealing with ram output signals
if((state==DATA) & op_is_read) // Read data out of RAM
{
// Wait for last valid read data outgoing to host
if(o.to_host.read.data.valid & o.to_host.read.data.burst.last & from_host.read.data_ready)
{
state = REQ;
}
}
else if(state==WR_RESP)
{
// Wait for valid write resp outgoing to host
if(o.to_host.write.resp.valid & from_host.write.resp_ready)
{
state = REQ;
}
}
// Save data into input buffers if signalling ready
if(o.to_host.write.req_ready)
{
bus_buffer.write_req = from_host.write.req;
}
if(o.to_host.read.req_ready)
{
bus_buffer.read_req = from_host.read.req;
}
if(o.to_host.write.data_ready)
{
bus_buffer.write_data = from_host.write.data;
}
return o;
}
MAIN_MHZ(frame_buf_ram_dev_arb_connect, DEV_CLK_MHZ)
void frame_buf_ram_dev_arb_connect()
{
// Arbitrate M hosts to N devs
SHARED_BUS_ARB(frame_buf_bus_t, frame_buf0_shared_bus, NUM_DEV_PORTS)
SHARED_BUS_ARB(frame_buf_bus_t, frame_buf1_shared_bus, NUM_DEV_PORTS)
// Connect devs to frame buffer ports
uint32_t i;
for (i = 0; i < NUM_DEV_PORTS; i+=1)
{
// First frame buffer
frame_buf_ram_port_dev_ctrl_t port_ctrl
= frame_buf_ram_port_dev_ctrl(frame_buffers_out_ports[0][i], frame_buf0_shared_bus_from_host[i]);
frame_buffers_in_ports[0][i] = port_ctrl.to_frame_buf;
frame_buf0_shared_bus_to_host[i] = port_ctrl.to_host;
// Second frame buffer
frame_buf_ram_port_dev_ctrl_t port_ctrl
= frame_buf_ram_port_dev_ctrl(frame_buffers_out_ports[1][i], frame_buf1_shared_bus_from_host[i]);
frame_buffers_in_ports[1][i] = port_ctrl.to_frame_buf;
frame_buf1_shared_bus_to_host[i] = port_ctrl.to_host;
}
}
// frame_buf_read and write
// wrappers around shared_resource_bus.h read() and write() dealing with request types etc
// Dual frame buffer is writing to one buffer while other is for reading
uint1_t frame_buffer_read_port_sel;
n_pixels_t dual_frame_buf_read(uint16_t x_buffer_index, uint16_t y)
{
//__clk();
uint32_t addr = pos_to_addr(x_buffer_index, y);
n_pixels_t resp;
if(frame_buffer_read_port_sel){
resp = frame_buf1_shared_bus_read(addr);
}else{
resp = frame_buf0_shared_bus_read(addr);
}
return resp;
}
void dual_frame_buf_write(uint16_t x_buffer_index, uint16_t y, n_pixels_t wr_data)
{
//__clk();
uint32_t addr = pos_to_addr(x_buffer_index, y);
uint1_t resp; // dummy return resp val
if(!frame_buffer_read_port_sel){
resp = frame_buf1_shared_bus_write(addr, wr_data);
}else{
resp = frame_buf0_shared_bus_write(addr, wr_data);
}
}
// Always-reading logic to drive VGA signal into pmod_async_fifo_write
void host_vga_reader()
{
vga_pos_t vga_pos;
while(1)
{
// Read the pixels at x,y pos
uint16_t x_buffer_index = vga_pos.x >> RAM_PIXEL_BUFFER_SIZE_LOG2;
n_pixels_t pixels = dual_frame_buf_read(x_buffer_index, vga_pos.y);
// Default black=0 unless pixel is white=1
pixel_t colors[RAM_PIXEL_BUFFER_SIZE];
uint32_t i;
for(i = 0; i < RAM_PIXEL_BUFFER_SIZE; i+=1)
{
if(pixels.data[i])
{
colors[i].r = 255;
colors[i].g = 255;
colors[i].b = 255;
}
else
{
colors[i].r = 0;
colors[i].g = 0;
colors[i].b = 0;
}
}
// Write it into async fifo feeding vga pmod for display
pmod_async_fifo_write(colors);
// Execute a cycle of vga timing to get x,y and increment for next time
vga_pos = vga_frame_pos_increment(vga_pos, RAM_PIXEL_BUFFER_SIZE);
}
}
// Wrap up FSM as top level
#include "host_vga_reader_FSM.h"
MAIN_MHZ(host_vga_reader_wrapper, HOST_CLK_MHZ)
void host_vga_reader_wrapper()
{
host_vga_reader_INPUT_t i;
i.input_valid = 1;
i.output_ready = 1;
host_vga_reader_OUTPUT_t o = host_vga_reader_FSM(i);
}