]> Joshua Wise's Git repositories - mandelfpga.git/blob - readout.c
Prod/cons-ify readout so it can do realtime
[mandelfpga.git] / readout.c
1 #include <string.h>
2 #include <SDL/SDL.h>
3
4 #define WIN_X 800
5 #define WIN_Y 600
6
7 char getone()
8 {
9         unsigned char c;
10         static char ring[2048];
11         static int prod = 0, cons = 0;
12         int oldcons;
13         
14         if (prod == cons)
15         {
16                 int r;
17                 /* Try to read some. */
18                 while ((r = read(0, &(ring[prod]), 2048-prod)) == 0)
19                         usleep(250000);
20                 prod = (prod + r) % 2048;
21         }
22         oldcons = cons;
23         cons = (cons + 1) % 2048;
24         return ring[oldcons];
25 }
26
27 char *readline()
28 {
29         static char s[2048];
30         int i = 0;
31         
32         while (i < 2048)
33         {
34                 s[i] = getone();
35                 if (s[i] == '\r' || s[i] == '\n')
36                 {
37                         s[i] = '\0';
38                         return s;
39                 }
40                 i++;
41         }
42         s[2047] = 0;
43         return s;
44 }
45
46 struct vars {
47         char *vname, *var, *sub;
48         int val;
49 };
50
51 int *findvar(char *var, char *sub, struct vars *vars, int n)
52 {
53         int i;
54         for (i=0; i<n; i++)
55                 if (!strcmp(vars[i].var, var) && ((!sub && !vars[i].sub) || !strcmp(vars[i].sub, sub)))
56                         return &vars[i].val;
57         printf("Unknown var: %s %s\n", var, sub);
58         abort();
59 }
60
61 void main()
62 {
63         char *s;
64         struct vars vars[512];
65         int nvars = 0;
66         int readingdata = 0;
67         SDL_Surface *screen;
68         
69         if (SDL_Init(SDL_INIT_VIDEO) < 0)
70         {
71                 printf("SDL init failed: %s\n", SDL_GetError());
72                 return 1;
73         }
74         atexit(SDL_Quit);
75         screen = SDL_SetVideoMode(WIN_X, WIN_Y, 24, SDL_SWSURFACE);
76         if (!screen)
77         {
78                 printf("Video init failed: %s\n", SDL_GetError());
79         }
80         
81         while ((s = readline()))
82         {
83                 if (s[0] == '$')
84                 {
85                         if (!strcmp(s, "$dumpvars"))
86                         {
87                                 readingdata = 1;
88                                 printf("Var dump begin on %d vars\n", nvars);
89                         } else if (!strncmp(s, "$var", 4)) {
90                                 char *t;
91                                 char *vname, *var, *sub;\
92                                 t = strtok(s, " ");
93                                 t = strtok(NULL, " ");
94                                 t = strtok(NULL, " ");
95                                 t = strtok(NULL, " ");
96                                 vname = t;
97                                 t = strtok(NULL, " ");
98                                 var = t;
99                                 t = strtok(NULL, " ");
100                                 if (strcmp(t, "$end"))
101                                         sub = t;
102                                 else
103                                         sub = NULL;
104                                 printf("Found: %s -> %s %s\n", vname, var, sub);
105                                 
106                                 vars[nvars].vname = strdup(vname);
107                                 vars[nvars].var = strdup(var);
108                                 vars[nvars].sub = sub ? strdup(sub) : NULL;
109                                 vars[nvars].val = 0;
110                                 nvars++;
111                         }
112                 } else if (s[0] == '#' || !s[0])
113                         ;
114                 else if (readingdata)
115                 {
116                         int val = (s[0] == '1');
117                         int i;
118                         int oldclk = *findvar("gclk", NULL, vars, nvars);
119                         static int pixn = 0;
120                         for (i=0; i<nvars; i++)
121                         {
122                                 if (!strcmp(vars[i].vname, s+1))
123                                 {
124                                         vars[i].val = val;
125                                         break;
126                                 }
127                         }
128                         if (i == nvars)
129                                 printf("Unknown: %s\n", s);
130                         if (*findvar("gclk", NULL, vars, nvars) != oldclk && oldclk == 1 && ((pixn++) % 2))
131                         {
132                                 static int x = 0, y = 0;
133                                 static int lasths;
134                                 unsigned char *pixp;
135                                 static int *r2 = NULL, *r1 = NULL, *r0 = NULL, *g2 = NULL, *g1 = NULL, *g0 = NULL, *b1 = NULL, *b0 = NULL, *vs = NULL, *hs = NULL;
136                                 r2 = r2 ? r2 : findvar("red", "[2]", vars, nvars);
137                                 r1 = r1 ? r1 : findvar("red", "[1]", vars, nvars);
138                                 r0 = r0 ? r0 : findvar("red", "[0]", vars, nvars);
139                                 g2 = g2 ? g2 : findvar("green", "[2]", vars, nvars);
140                                 g1 = g1 ? g1 : findvar("green", "[1]", vars, nvars);
141                                 g0 = g0 ? g0 : findvar("green", "[0]", vars, nvars);
142                                 b1 = b1 ? b1 : findvar("blue", "[1]", vars, nvars);
143                                 b0 = b0 ? b0 : findvar("blue", "[0]", vars, nvars);
144                                 vs = vs ? vs : findvar("vs", NULL, vars, nvars);
145                                 hs = hs ? hs : findvar("hs", NULL, vars, nvars);
146                                 
147                                 
148                                 int r = *r2 << 7 | *r1 << 6 | *r0 << 5;
149                                 int g = *g2 << 7 | *g1 << 6 | *g0 << 5;
150                                 int b = *b1 << 7 | *b0 << 6;
151                                 if (*vs)
152                                         y = 0;
153                                 else {
154                                         if (*hs)
155                                         {
156                                                 x = 0;
157                                                 if (!lasths)
158                                                 {
159                                                         y++;
160                                                         SDL_Flip(screen);
161                                                 }
162                                         } else
163                                                 x++;
164                                 }
165                                 lasths = *hs;
166                                 SDL_LockSurface(screen);
167                                 pixp = screen->pixels + ((WIN_X * y + x) * 3);
168                                 pixp[0] = b; pixp[1] = g; pixp[2] = r;
169                                 SDL_UnlockSurface(screen);
170                                 if ((x % 5) == 4)
171                                         SDL_UpdateRect(screen, x/5*5, y, 5, 1);
172                         }
173                 }
174         }
175 }
This page took 0.033924 seconds and 4 git commands to generate.