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