+#include <string.h>
+#include <SDL/SDL.h>
+
+#define WIN_X 800
+#define WIN_Y 600
+
+#define RINGSZ 528088
+
+char getone()
+{
+ unsigned char c;
+ static char ring[RINGSZ];
+ static int prod = 0, cons = 0;
+ int oldcons;
+
+ if (prod == cons)
+ {
+ int r;
+
+ /* Try to read some. */
+ while ((r = read(0, &(ring[prod]), RINGSZ-prod)) == 0)
+ usleep(250000);
+ prod = (prod + r) % RINGSZ;
+ }
+ oldcons = cons;
+ cons = (cons + 1) % RINGSZ;
+ return ring[oldcons];
+}
+
+char *readline()
+{
+ static char s[2048];
+ int i = 0;
+
+ while (i < 2048)
+ {
+ s[i] = getone();
+ if (s[i] == '\r' || s[i] == '\n')
+ {
+ s[i] = '\0';
+ return s;
+ }
+ i++;
+ }
+ s[2047] = 0;
+ return s;
+}
+
+struct vars {
+ char *vname, *var, *sub;
+ int val;
+};
+
+int *findvar(char *var, char *sub, struct vars *vars, int n)
+{
+ int i;
+ for (i=0; i<96*96; i++)
+ if (vars[i].var && !strcmp(vars[i].var, var) && ((!sub && !vars[i].sub) || !strcmp(vars[i].sub, sub)))
+ return &vars[i].val;
+ printf("Unknown var: %s %s\n", var, sub);
+ abort();
+}
+
+int numname(char *s)
+{
+ int i = 0;
+ while(*s)
+ {
+ i *= 96;
+ i += *(s++) - '!' + 1;
+ }
+ return i;
+}
+
+void main()
+{
+ char *s;
+ struct vars vars[96*96];
+ int nvars = 0;
+ int readingdata = 0;
+ SDL_Surface *screen;
+
+ if (SDL_Init(SDL_INIT_VIDEO) < 0)
+ {
+ printf("SDL init failed: %s\n", SDL_GetError());
+ return 1;
+ }
+ atexit(SDL_Quit);
+ screen = SDL_SetVideoMode(WIN_X, WIN_Y, 24, SDL_SWSURFACE);
+ if (!screen)
+ {
+ printf("Video init failed: %s\n", SDL_GetError());
+ }
+
+ memset(vars, 0, sizeof(vars));
+
+ while ((s = readline()))
+ {
+ if (s[0] == '$')
+ {
+ if (!strcmp(s, "$dumpvars"))
+ {
+ readingdata = 1;
+ printf("Var dump begin on %d vars\n", nvars);
+ } else if (!strncmp(s, "$var", 4)) {
+ char *t;
+ char *vname, *var, *sub;\
+ t = strtok(s, " ");
+ t = strtok(NULL, " ");
+ t = strtok(NULL, " ");
+ t = strtok(NULL, " ");
+ vname = t;
+ t = strtok(NULL, " ");
+ var = t;
+ t = strtok(NULL, " ");
+ if (strcmp(t, "$end"))
+ sub = t;
+ else
+ sub = NULL;
+ printf("Found: %s (%d) -> %s %s\n", vname, numname(vname), var, sub);
+
+ vars[numname(vname)].vname = strdup(vname);
+ vars[numname(vname)].var = strdup(var);
+ vars[numname(vname)].sub = sub ? strdup(sub) : NULL;
+ vars[numname(vname)].val = 0;
+ }
+ } else if (s[0] == '#' || !s[0])
+ ;
+ else if (readingdata)
+ {
+ int val = (s[0] == '1');
+ int i;
+ static int *tclk;
+ int oldclk = tclk ? *tclk : 0;
+ static int pixn = 0;
+
+ if (!tclk) tclk = findvar("vgaclk", NULL, vars, nvars);
+
+ vars[numname(s+1)].val = val;
+
+ if (*tclk != oldclk && ((pixn++) % 2)) {
+ static int x = 0, y = 0;
+ static int lasths;
+ unsigned char *pixp;
+ static int *r2 = NULL, *r1 = NULL, *r0 = NULL, *g2 = NULL, *g1 = NULL, *g0 = NULL, *b1 = NULL, *b0 = NULL, *vs = NULL, *hs = NULL;
+ if (!r2) r2 = findvar("r", "[2]", vars, nvars);
+ if (!r1) r1 = findvar("r", "[1]", vars, nvars);
+ if (!r0) r0 = findvar("r", "[0]", vars, nvars);
+ if (!g2) g2 = findvar("g", "[2]", vars, nvars);
+ if (!g1) g1 = findvar("g", "[1]", vars, nvars);
+ if (!g0) g0 = findvar("g", "[0]", vars, nvars);
+ if (!b1) b1 = findvar("b", "[1]", vars, nvars);
+ if (!b0) b0 = findvar("b", "[0]", vars, nvars);
+ if (!vs) vs = findvar("vs", NULL, vars, nvars);
+ if (!hs) hs = findvar("hs", NULL, vars, nvars);
+
+
+ int r = *r2 << 7 | *r1 << 6 | *r0 << 5;
+ int g = *g2 << 7 | *g1 << 6 | *g0 << 5;
+ int b = *b1 << 7 | *b0 << 6;
+ if (*vs)
+ y = 0;
+ else {
+ if (*hs)
+ {
+ x = 0;
+ if (!lasths)
+ y++;
+ } else
+ x++;
+ }
+ SDL_LockSurface(screen);
+ pixp = screen->pixels + ((WIN_X * y + x) * 3);
+ pixp[0] = b; pixp[1] = g; pixp[2] = r;
+ SDL_UnlockSurface(screen);
+ if (x == 0 && !lasths && y != 0)
+ {
+ int i;
+ SDL_LockSurface(screen);
+ for (i = 1; i < WIN_X; i++)
+ pixp[0+i*3] = pixp[1+i*3] = pixp[2+i*3] = 0xFF;
+ SDL_UnlockSurface(screen);
+ SDL_UpdateRect(screen, 0, y-1, WIN_X, 2);
+ if (!(y % 3))
+ SDL_Flip(screen);
+ }
+ lasths = *hs;
+ }
+ }
+ }
+}