simulation split out from main
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
*.out
|
*.out
|
||||||
.cache/
|
.cache/
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
|
/TAGS
|
||||||
|
/.gdb_history
|
||||||
|
|||||||
2
build.sh
2
build.sh
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
CFLAGS="-Wall -Wextra -Wpedantic -Wswitch-enum -Werror -std=c23 -ggdb"
|
CFLAGS="-Wall -Wextra -Wpedantic -Wswitch-enum -Werror -std=c23 -ggdb"
|
||||||
LDFLAGS="-lm -lraylib"
|
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"
|
OUT="main.out"
|
||||||
|
|
||||||
set -xe
|
set -xe
|
||||||
|
|||||||
47
src/main.c
47
src/main.c
@@ -15,55 +15,11 @@
|
|||||||
#include "vec.h"
|
#include "vec.h"
|
||||||
|
|
||||||
#include "program_iter.h"
|
#include "program_iter.h"
|
||||||
|
#include "simulation.h"
|
||||||
struct ProgramConcat
|
|
||||||
{
|
|
||||||
sv_t A, B;
|
|
||||||
u8 tape[SIZEOF_PROGRAM * 2];
|
|
||||||
};
|
|
||||||
|
|
||||||
#define WIDTH 800
|
#define WIDTH 800
|
||||||
#define HEIGHT 600
|
#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)
|
Color simulation_cell_color(const u8 *program)
|
||||||
{
|
{
|
||||||
// How do we compute a "colour" for a program? I say we count all the valid
|
// 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);
|
SetTargetFPS(60);
|
||||||
for (size_t ticks = 0; !WindowShouldClose(); ++ticks)
|
for (size_t ticks = 0; !WindowShouldClose(); ++ticks)
|
||||||
{
|
{
|
||||||
simulation_pick(&sim);
|
|
||||||
simulation_update(&sim);
|
simulation_update(&sim);
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
|
|||||||
53
src/simulation.c
Normal file
53
src/simulation.c
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/* simulation.c: Simulation implementation
|
||||||
|
* Created: 2026-03-10
|
||||||
|
* Author: Aryadev Chavali
|
||||||
|
* License: See end of file
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
41
src/simulation.h
Normal file
41
src/simulation.h
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user