Browse Source

add conway example

master
mntmn 1 month ago
parent
commit
9632232a84
3 changed files with 137 additions and 0 deletions
  1. 19
    0
      conway/build-conway.sh
  2. BIN
      conway/conway.bin
  3. 118
    0
      conway/conway.c

+ 19
- 0
conway/build-conway.sh View File

@@ -0,0 +1,19 @@
COMPILE="arm-none-eabi-gcc -std=gnu99 -nostdlib -O5 -c -mcpu=cortex-a9 -mfpu=neon -mfloat-abi=hard -I../lib"
LINK="arm-none-eabi-gcc -T ../link.ld -std=gnu99 -nostdlib -O5 -mcpu=cortex-a9 -mfpu=neon -mfloat-abi=hard"
NAME=conway

mkdir -p build

$COMPILE -I. -o build/conway.o conway.c
$COMPILE -o build/idiv.o ../lib/div/idiv.S
$COMPILE -o build/idivmod.o ../lib/div/idivmod.S
$COMPILE -o build/ldivmod.o ../lib/div/ldivmod.S
$COMPILE -o build/memcpy.o ../lib/memory/memcpy.c
$COMPILE -o build/printf.o ../lib/printf/printf.c

$LINK -o $NAME build/conway.o build/memcpy.o build/printf.o build/*div*.o -L.
arm-none-eabi-objcopy -O binary $NAME $NAME.bin

rm $NAME.lha
lha a0 $NAME.lha $NAME.bin


BIN
conway/conway.bin View File


+ 118
- 0
conway/conway.c View File

@@ -0,0 +1,118 @@
#include <stdlib.h>
#include <unistd.h>
#include "printf/printf.h"

// matrix width and height
#define CONW_W 640
#define CONW_H (480-16)

unsigned char univ[CONW_H][CONW_W];
unsigned char new[CONW_H][CONW_W];

void __aeabi_idiv0(int r) {
while (1) {
printf("__aeabi_idiv0()!\n");
}
}
void __aeabi_ldiv0(int r) {
while (1) {
printf("__aeabi_idiv0()!\n");
}
}

unsigned int lfsr113 (void)
{
static unsigned int z1 = 12345, z2 = 12345, z3 = 12345, z4 = 12345;
unsigned int b;
b = ((z1 << 6) ^ z1) >> 13;
z1 = ((z1 & 4294967294U) << 18) ^ b;
b = ((z2 << 2) ^ z2) >> 27;
z2 = ((z2 & 4294967288U) << 2) ^ b;
b = ((z3 << 13) ^ z3) >> 21;
z3 = ((z3 & 4294967280U) << 7) ^ b;
b = ((z4 << 3) ^ z4) >> 12;
z4 = ((z4 & 4294967168U) << 13) ^ b;
return (z1 ^ z2 ^ z3 ^ z4);
}

// Lifted from https://rosettacode.org/wiki/Conway%27s_Game_of_Life#C

void show(uint32_t* fb, int fbw, int w, int h)
{
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
fb[y*fbw+x] = (univ[y][x] ? 0xffffff : 0x000000);
}
}
}

void evolve(int w, int h)
{
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int n = 0;
for (int y1 = y - 1; y1 <= y + 1; y1++) {
for (int x1 = x - 1; x1 <= x + 1; x1++) {
if (univ[(y1 + h) % h][(x1 + w) % w])
n++;
}
}
if (univ[y][x]) n--;
new[y][x] = (n == 3 || (n == 2 && univ[y][x]));
}
}
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
univ[y][x] = new[y][x];
}
}
}

void init(int w, int h) {
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
univ[y][x] = lfsr113() < RAND_MAX / 10 ? 1 : 0;
}
}
}

struct ZZ9K_ENV {
uint32_t api_version;
uint32_t argv[8];
uint32_t argc;
int (*fn_putchar)(char);
};

struct ZZ9K_ENV* _zz9k_env;

void _putchar(char c) {
_zz9k_env->fn_putchar(c);
};

int did_init = 0;

void __attribute__ ((section (".binstart"))) main(struct ZZ9K_ENV* env) {
_zz9k_env = env;

if (!env) {
return;
}

if (env->argc<3) {
return;
}

// arg0: init the matrix?
if (env->argv[0] || !did_init) {
init(CONW_W, CONW_H);
did_init = 1;
}

while (1) {
evolve(CONW_W, CONW_H);
// arg1: framebuffer pointer (32bpp)
// arg2: framebuffer width in pixels
show((uint32_t*)env->argv[1], (uint32_t)env->argv[2], CONW_W, CONW_H);
}
}

Loading…
Cancel
Save