Code: Select all
zcc +zx81 -create-app -ogol -O3 gol.c
Code: Select all
/* Conway's Game of Life for the ZX81 */
#include <stdio.h>
#include <stdlib.h>
#define WIDTH 32
#define HEIGHT 21
#define for_y for (y = 0; y < HEIGHT; y++)
#define for_x for (x = 0; x < WIDTH; x++)
struct {
unsigned char x;
unsigned char y;
} param;
void myputch(void) {
__asm
ld a, (_param)
jp 0x07f1
__endasm;
}
void myplot(void) {
__asm
ld bc,(_param)
call 0x08f5
ld a, 0x80
jp 0x07f1
__endasm;
}
void myunplot(void) {
__asm
ld bc,(_param)
call 0x08f5
ld a, 0
jp 0x07f1
__endasm;
}
char univ[HEIGHT][WIDTH];
char new[HEIGHT][WIDTH];
char* q;
char* r;
char* g;
char* m;
char* p;
int seed, hasChange, a, b, c, d, e, f, x, y, newval;
int main(int c, char **v) {
printf("seed?\n");
scanf("%d", &seed);
srand(seed);
do {
/* void init(void) */ {
r = (void*)univ;
__asm
ld bc,0
call 0x08f5
__endasm;
for_y {
for_x {
param.x = ((*r++) = (rand() < RAND_MAX / 10)) ? 0x80 : 0;
myputch();
}
//param.chars.x = 0x76;
//myputch();
}
}
do {
if(getk()) {
while(getk()) {}
break;
}
/* void evolve(void) */ {
q = (void*)new;
r = (void*)univ;
for_y {
for_x {
e = (y + HEIGHT);
a = (((e - 1) % HEIGHT) * WIDTH);
f = (((e + 1) % HEIGHT) * WIDTH);
c = (x + WIDTH);
b = ((c - 1) % WIDTH);
d = ((c + 1) % WIDTH);
e = ((e % HEIGHT) * WIDTH);
c %= WIDTH;
g = r + a;
m = r + e;
p = r + f;
switch(g[b] + g[c] + g[d] + m[b] + m[d] + p[b] + p[c] + p[d]) {
case 2: {
if(m[x]) {
case 3:
q[x] = 1;
break;
}
} /* fall thru */
default: {
q[x] = 0;
} break;
}
}
q += WIDTH;
}
q = (void*)new;
hasChange = 0;
for_y {
for_x {
newval = *q++;
if(newval != (*r)) {
hasChange = 1;
param.x = x;
param.y = y;
((*r++) = newval) ? myplot() : myunplot();
}
else {
++r;
}
}
}
}
} while(hasChange);
++seed;
} while (1);
}