From 23add2c55fa3b07557a4823f1b1685bccb9b7be8 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Tue, 17 Mar 2026 23:21:47 +0000 Subject: [PATCH] added simulation file for actually performing sim --- build.sh | 2 +- src/base.h | 3 +- src/main.c | 29 ------------------ src/simulation.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ src/simulation.h | 33 ++++++++++++++++++++ 5 files changed, 116 insertions(+), 31 deletions(-) create mode 100644 src/simulation.c create mode 100644 src/simulation.h diff --git a/build.sh b/build.sh index 59326c5..d2fd2b2 100644 --- a/build.sh +++ b/build.sh @@ -2,7 +2,7 @@ CFLAGS="-Wall -Wextra -Wpedantic -Wswitch-enum -Werror -std=c23 -ggdb" LDFLAGS="-lm -lraylib" -SRC="src/sv.c src/vec.c src/bf.c src/main.c" +SRC="src/sv.c src/vec.c src/bf.c src/simulation.c src/main.c" OUT="main.out" set -xe diff --git a/src/base.h b/src/base.h index 8466e36..16d74e4 100644 --- a/src/base.h +++ b/src/base.h @@ -32,8 +32,9 @@ typedef double f64; #define WIDTH 800 #define HEIGHT 600 -// All "programs" are 64 bytes. +// Simulation constants #define SIZEOF_PROGRAM 64 +#define NUM_PROGRAMS 64 #endif diff --git a/src/main.c b/src/main.c index aba94ec..3ab372b 100644 --- a/src/main.c +++ b/src/main.c @@ -16,35 +16,6 @@ #include "bf.h" -static char *VALID_OPS = "<>{}-+.,[]"; - -u8 get_op(const u8 cell) -{ - if (strchr(VALID_OPS, cell)) - return cell; - else - return '\0'; -} - -Color simulation_cell_color(const u8 cell) -{ - const Color possible_colors[] = { - ['<'] = ColorFromHSV(0.121, 0.467, 0.706), - ['>'] = ColorFromHSV(1.000, 0.498, 0.055), - ['{'] = ColorFromHSV(0.173, 0.627, 0.173), - ['}'] = ColorFromHSV(0.839, 0.153, 0.157), - ['-'] = ColorFromHSV(0.580, 0.404, 0.741), - ['+'] = ColorFromHSV(0.549, 0.337, 0.294), - ['.'] = ColorFromHSV(0.890, 0.467, 0.761), - [','] = ColorFromHSV(0.498, 0.498, 0.498), - ['['] = ColorFromHSV(0.737, 0.741, 0.133), - [']'] = ColorFromHSV(0.090, 0.745, 0.812), - ['\0'] = BLACK, - }; - - return possible_colors[get_op(cell)]; -} - int main(void) { srand(time(NULL)); diff --git a/src/simulation.c b/src/simulation.c new file mode 100644 index 0000000..8ef60e3 --- /dev/null +++ b/src/simulation.c @@ -0,0 +1,80 @@ +/* simulation.c: Simulation implementation + * Created: 2026-03-17 + * Author: Aryadev Chavali + * License: See end of file + */ + +#include +#include + +#include + +#include "simulation.h" + +static char *VALID_OPS = "<>{}-+.,[]"; + +static const Color possible_colors[] = { + {0xb4, 0x60, 0x5f, 0xff}, {0xe, 0x7, 0x7, 0xff}, + {0x2c, 0x10, 0x10, 0xff}, {0x28, 0x21, 0x21, 0xff}, + {0xbc, 0x71, 0x70, 0xff}, {0x4a, 0x31, 0x31, 0xff}, + {0xc2, 0x68, 0x67, 0xff}, {0x7e, 0x40, 0x3f, 0xff}, + {0x21, 0x9, 0x8, 0xff}, {0xcf, 0x35, 0x34, 0xff}, + {0x0, 0x0, 0x0, 0xff}, {0}, +}; + +void simulation_pick(u64 *a, u64 *b) +{ + // Pick two indices that don't intersect + while (*a == *b) + { + *a = rand() % NUM_PROGRAMS; + *b = rand() % NUM_PROGRAMS; + } +} + +void simulation_iterate(simulation_t *sim) +{ + u64 a = 0, b = 0; + simulation_pick(&a, &b); + + // Change of basis from program indices to bf_token indices. + a *= SIZEOF_PROGRAM; + b *= SIZEOF_PROGRAM; + + // Perform the catalytic reaction + struct ProgramConcat a_b_concat = {0}; + program_concat(&a_b_concat, sim->memory + a, sim->memory + b); + program_execute(&a_b_concat); + program_split(&a_b_concat); +} + +u8 get_op(const u8 cell) +{ + if (strchr(VALID_OPS, cell)) + return cell; + else + return '\0'; +} + +Color simulation_cell_color(const u8 cell) +{ + return possible_colors[get_op(cell)]; +} + +void simulation_draw(simulation_t *sim) +{ + assert(0 && "TODO: implement simulation_draw"); +} + +/* Copyright (C) 2026 Aryadev Chavali + + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for + * details. + + * You may distribute and modify this code under the terms of the GNU General + * Public License Version 2, which you should have received a copy of along with + * this program. If not, please go to . + + */ diff --git a/src/simulation.h b/src/simulation.h new file mode 100644 index 0000000..0cff818 --- /dev/null +++ b/src/simulation.h @@ -0,0 +1,33 @@ +/* simulation.h: Simulation + * Created: 2026-03-17 + * Author: Aryadev Chavali + * License: See end of file + */ + +#ifndef SIMULATION_H +#define SIMULATION_H + +#include "bf.h" + +typedef struct +{ + bf_token memory[NUM_PROGRAMS * SIZEOF_PROGRAM]; +} simulation_t; + +void simulation_iterate(simulation_t *sim); +void simulation_draw(simulation_t *sim); + +#endif + +/* Copyright (C) 2026 Aryadev Chavali + + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for + * details. + + * You may distribute and modify this code under the terms of the GNU General + * Public License Version 2, which you should have received a copy of along with + * this program. If not, please go to . + + */