2020#ifdef __linux__
2121#include <stdlib.h>
2222#include <unistd.h>
23+ #else
24+ #include "io.h"
2325#endif
2426
25-
27+ // when compiling for SPI flash, uncomment to fit some routines in fast BRAM
28+ // (but it does not change much, the bottleneck is ANSI RGB encoding and uart.
2629//#define RV32_FASTCODE __attribute((section(".fastcode")))
2730#define RV32_FASTCODE
2831
32+ // when compiling for SPI flash, uncomment to enable wireframe mode (but it is ugly
33+ // and it will not fit in BRAM !)
34+ // #define WITH_WIREFRAME
35+
36+ #ifdef WITH_WIREFRAME
2937int wireframe = 0 ;
38+ #endif
3039
3140#define MIN (x ,y ) ((x) < (y) ? (x) : (y))
3241#define MAX (x ,y ) ((x) > (y) ? (x) : (y))
3342
43+
3444/**********************************************************************************/
3545/* Graphics routines */
3646/**********************************************************************************/
@@ -46,13 +56,11 @@ static inline uint8_t map_y(uint8_t y) {
4656 return y >> 2 ;
4757}
4858
49-
5059void GL_clear () {
5160 printf ("\033[48;5;16m" // set background color black
5261 "\033[2J" ); // clear screen
5362}
5463
55-
5664/*
5765 * Set background color using 6x6x6 colorcube codes
5866 * see https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
@@ -69,6 +77,7 @@ static inline void GL_setpixel(int x, int y) {
6977 printf ("\033[%d;%dH " ,y ,x ); // Goto_XY(x1,y) and print space
7078}
7179
80+ #ifdef WITH_WIREFRAME
7281void GL_line (int x1 , int y1 , int x2 , int y2 ) RV32_FASTCODE ;
7382void GL_line (int x1 , int y1 , int x2 , int y2 ) {
7483 int x ,y ,dx ,dy ,sy ,tmp ;
@@ -83,7 +92,7 @@ void GL_line(int x1, int y1, int x2, int y2) {
8392 y1 = tmp ;
8493 }
8594
86- /* Bresenham line drawing. */
95+ // Bresenham line drawing.
8796 dy = y2 - y1 ;
8897 sy = 1 ;
8998 if (dy < 0 ) {
@@ -127,6 +136,7 @@ void GL_line(int x1, int y1, int x2, int y2) {
127136 }
128137 }
129138}
139+ #endif
130140
131141void GL_fillpoly (int nb_pts , int * points ) RV32_FASTCODE ;
132142void GL_fillpoly (int nb_pts , int * points ) {
@@ -163,12 +173,14 @@ void GL_fillpoly(int nb_pts, int* points) {
163173 int x2 = points [2 * i2 ];
164174 int y2 = points [2 * i2 + 1 ];
165175
176+ #ifdef WITH_WIREFRAME
166177 if (wireframe ) {
167178 if ((clockwise > 0 ) ^ (y2 > y1 )) {
168179 GL_line (x1 ,y1 ,x2 ,y2 );
169180 }
170181 continue ;
171182 }
183+ #endif
172184
173185 char * x_buffer = ((clockwise > 0 ) ^ (y2 > y1 )) ? x_left : x_right ;
174186 int dx = x2 - x1 ;
@@ -208,7 +220,10 @@ void GL_fillpoly(int nb_pts, int* points) {
208220 }
209221 }
210222
211- if (!wireframe ) {
223+ #ifdef WITH_WIREFRAME
224+ if (!wireframe )
225+ #endif
226+ {
212227 for (int y = miny ; y <= maxy ; ++ y ) {
213228 int x1 = x_left [y ];
214229 int x2 = x_right [y ];
@@ -431,18 +446,35 @@ int read_frame() {
431446
432447int main () {
433448 // printf("\x1B[?25l"); // hide cursor
434- wireframe = 0 ;
449+
450+ #ifndef __linux__
451+ IO_OUT (IO_LEDS ,15 );
452+ #endif
453+ printf ("starting\n" );
454+
455+ #ifdef WITH_WIREFRAME
456+ wireframe = 0 ;
457+ #endif
458+ int frame = 0 ;
435459 GL_clear ();
436460 for (;;) {
437461 spi_reset ();
462+ frame = 0 ;
438463 while (read_frame ()) {
464+ #ifdef WITH_WIREFRAME
439465 if (wireframe ) {
440466 GL_clear ();
441467 }
468+ #endif
442469#ifdef __linux__
443470 usleep (20000 );
444- #endif
471+ #else
472+ IO_OUT (IO_LEDS ,frame );
473+ #endif
474+ ++ frame ;
445475 }
476+ #ifdef WITH_WIREFRAME
446477 wireframe = !wireframe ;
478+ #endif
447479 }
448480}
0 commit comments