aboutsummaryrefslogtreecommitdiff
path: root/src/salis.c
blob: 32a14b84b4c584ea74f3013f8928d5beafcf6e18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <assert.h>
#include <stdio.h>
#include "getter.h"
#include "salis.h"

static boolean g_is_init;
static uint32 g_cycle;
static uint32 g_epoch;

void sal_main_init(uint32 order)
{
	/* Initialize all Salis modules to their initial states. We pass along any
	arguments to their respective modules.
	*/
	assert(!g_is_init);
	_sal_mem_init(order);
	_sal_evo_init();
	_sal_proc_init();
	g_is_init = TRUE;
}

void sal_main_quit(void)
{
	/* Reset Salis and all of its modules back to zero. We may, thus, shutdown
	Salis and re-initialize it with different parameters without having to
	reload the library (useful, for example, when running data gathering
	scripts that must iterate through many save files).
	*/
	assert(g_is_init);
	_sal_proc_quit();
	_sal_evo_quit();
	_sal_mem_quit();
	g_is_init = FALSE;
	g_cycle = 0;
	g_epoch = 0;
}

void sal_main_load(string file_name)
{
	/* Load simulation state from file. This file must have been created by
	'sal_main_save()'.
	*/
	FILE *file;
	assert(!g_is_init);
	assert(file_name);
	file = fopen(file_name, "rb");
	assert(file);
	fread(&g_is_init, sizeof(boolean), 1, file);
	fread(&g_cycle, sizeof(uint32), 1, file);
	fread(&g_epoch, sizeof(uint32), 1, file);
	_sal_mem_load_from(file);
	_sal_evo_load_from(file);
	_sal_proc_load_from(file);
	fclose(file);
}

void sal_main_save(string file_name)
{
	/* Save simulation state to a file. This file may later be re-loaded with
	'sal_main_load()'. We save in binary format (to save space), which means
	save files might not be entirely portable.
	*/
	FILE *file;
	assert(g_is_init);
	assert(file_name);
	file = fopen(file_name, "wb");
	assert(file);
	fwrite(&g_is_init, sizeof(boolean), 1, file);
	fwrite(&g_cycle, sizeof(uint32), 1, file);
	fwrite(&g_epoch, sizeof(uint32), 1, file);
	_sal_mem_save_into(file);
	_sal_evo_save_into(file);
	_sal_proc_save_into(file);
	fclose(file);
}

boolean sal_main_is_init(void)
{
	/* Check if Salis is currently initialized/running.
	*/
	return g_is_init;
}

/* Getter methods for the Salis main module.
*/
UINT32_GETTER(main, cycle)
UINT32_GETTER(main, epoch)

void sal_main_cycle(void)
{
	/* Cycle the Salis simulator once. The combination of a cycle * epoch
	counter allows us to track simulations for an insane period of time
	(2^64 cycles).
	*/
	g_cycle++;

	if (!g_cycle) {
		g_epoch++;
	}

	/* Cycle all of the Salis modules.
	*/
	_sal_mem_cycle();
	_sal_evo_cycle();
	_sal_proc_cycle();
}