diff --git a/.gitignore b/.gitignore index 5c4e686..f34e038 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.out .cache/ -compile_commands.json \ No newline at end of file +compile_commands.json +/TAGS +/.gdb_history diff --git a/build.sh b/build.sh index b249881..f5635b1 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/program_iter.c src/main.c" +SRC="src/sv.c src/vec.c src/program_iter.c src/simulation.c src/main.c" OUT="main.out" set -xe diff --git a/src/main.c b/src/main.c index b25f5a5..bfc9cae 100644 --- a/src/main.c +++ b/src/main.c @@ -15,55 +15,11 @@ #include "vec.h" #include "program_iter.h" - -struct ProgramConcat -{ - sv_t A, B; - u8 tape[SIZEOF_PROGRAM * 2]; -}; +#include "simulation.h" #define WIDTH 800 #define HEIGHT 600 -#define NUM_PROGRAMS_POW_2 10 -#define NUM_PROGRAMS (1LU << NUM_PROGRAMS_POW_2) -#define SIMULATION_SIZE (SIZEOF_PROGRAM * NUM_PROGRAMS) -struct Simulation -{ - char buffer[SIMULATION_SIZE]; - u64 p1, p2; -}; - -void simulation_init(struct Simulation *sim) -{ - for (u64 i = 0; i < SIMULATION_SIZE / sizeof(u16); ++i) - { - ((u16 *)(sim->buffer))[i] = rand() % UINT16_MAX; - } -} - -void simulation_pick(struct Simulation *sim) -{ - sim->p1 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM); - sim->p2 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM); - while (sim->p1 * 8 <= ((sim->p2 * 8) + SIZEOF_PROGRAM) && - sim->p2 * 8 <= ((sim->p1 * 8) + SIZEOF_PROGRAM)) - { - sim->p2 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM); - } -} - -void simulation_update(struct Simulation *sim) -{ - sv_t a = SV(sim->buffer + (sim->p1 * SIZEOF_PROGRAM), 64); - sv_t b = SV(sim->buffer + (sim->p2 * SIZEOF_PROGRAM), 64); - - struct ProgramConcat prog_concat = {0}; - program_concat(&prog_concat, a, b); - program_execute(&prog_concat); - program_split(&prog_concat); -} - Color simulation_cell_color(const u8 *program) { // How do we compute a "colour" for a program? I say we count all the valid @@ -160,7 +116,6 @@ int main(void) SetTargetFPS(60); for (size_t ticks = 0; !WindowShouldClose(); ++ticks) { - simulation_pick(&sim); simulation_update(&sim); BeginDrawing(); ClearBackground(BLACK); diff --git a/src/simulation.c b/src/simulation.c new file mode 100644 index 0000000..5343b8e --- /dev/null +++ b/src/simulation.c @@ -0,0 +1,53 @@ +/* simulation.c: Simulation implementation + * Created: 2026-03-10 + * Author: Aryadev Chavali + * License: See end of file + */ + +#include + +#include "simulation.h" + +void simulation_init(struct Simulation *sim) +{ + for (u64 i = 0; i < SIMULATION_SIZE / sizeof(u16); ++i) + { + ((u16 *)(sim->buffer))[i] = rand() % UINT16_MAX; + } +} + +void simulation_pick(struct Simulation *sim) +{ + sim->p1 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM); + sim->p2 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM); + while (sim->p1 * 8 <= ((sim->p2 * 8) + SIZEOF_PROGRAM) && + sim->p2 * 8 <= ((sim->p1 * 8) + SIZEOF_PROGRAM)) + { + sim->p2 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM); + } +} + +void simulation_update(struct Simulation *sim) +{ + simulation_pick(sim); + sv_t a = SV(sim->buffer + (sim->p1 * SIZEOF_PROGRAM), 64); + sv_t b = SV(sim->buffer + (sim->p2 * SIZEOF_PROGRAM), 64); + + struct ProgramConcat prog_concat = {0}; + program_concat(&prog_concat, a, b); + program_execute(&prog_concat); + program_split(&prog_concat); +} + +/* 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..89d7da7 --- /dev/null +++ b/src/simulation.h @@ -0,0 +1,41 @@ +/* simulation.h: Simulation handlers and helpers + * Created: 2026-03-10 + * Author: Aryadev Chavali + * License: See end of file + * Commentary: + */ + +#ifndef SIMULATION_H +#define SIMULATION_H + +#include "program_iter.h" + +#define NUM_PROGRAMS_POW_2 10 +#define NUM_PROGRAMS (1LU << NUM_PROGRAMS_POW_2) +#define SIMULATION_SIZE (SIZEOF_PROGRAM * NUM_PROGRAMS) + +// Simulation is simply a massive tape. We pick two 64 byte portions of this +// buffer for our update procedure, which is based on program_iter. +struct Simulation +{ + char buffer[SIMULATION_SIZE]; + u64 p1, p2; +}; + +void simulation_init(struct Simulation *sim); +void simulation_update(struct Simulation *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 . + + */