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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "types.h"
#include "instset.h"
#include "memory.h"
#include "evolver.h"
sbool g_isInit;
sword g_lastAddress;
sbyte g_lastInst;
sword g_state[4];
void
se_init(void)
{
assert(!g_isInit);
srand((unsigned)time(NULL));
g_state[0] = rand();
g_state[1] = rand();
g_state[2] = rand();
g_state[3] = rand();
g_isInit = STRUE;
}
void
se_quit(void)
{
assert(g_isInit);
g_isInit = SFALSE;
g_lastAddress = 0;
g_lastInst = 0;
g_state[0] = 0;
g_state[1] = 0;
g_state[2] = 0;
g_state[3] = 0;
}
void
se_load(FILE *file)
{
assert(!g_isInit);
assert(file);
fread(&g_isInit, sizeof(sbool), 1, file);
fread(&g_lastAddress, sizeof(sword), 1, file);
fread(&g_lastInst, sizeof(sbyte), 1, file);
fread(g_state, sizeof(sword), 4, file);
}
void
se_save(FILE *file)
{
assert(g_isInit);
assert(file);
fwrite(&g_isInit, sizeof(sbool), 1, file);
fwrite(&g_lastAddress, sizeof(sword), 1, file);
fwrite(&g_lastInst, sizeof(sbyte), 1, file);
fwrite(g_state, sizeof(sword), 4, file);
}
sbool
se_isInit(void)
{
return g_isInit;
}
sword
se_getLastAddress(void)
{
return g_lastAddress;
}
sbyte
se_getLastInst(void)
{
assert(si_isInst(g_lastInst));
return g_lastInst;
}
sword
se_getState(sword eidx)
{
assert(eidx < 4);
return g_state[eidx];
}
void
se_setState(sword eidx, sword state)
{
assert(g_isInit);
assert(eidx < 4);
g_state[eidx] = state;
}
static sword
generateRandomNumber(void)
{
sword s;
sword t;
assert(g_isInit);
t = g_state[3];
t ^= t << 11;
t ^= t >> 8;
g_state[3] = g_state[2];
g_state[2] = g_state[1];
g_state[1] = g_state[0];
s = g_state[0];
t ^= s;
t ^= s >> 19;
g_state[0] = t;
return t;
}
void
se_cycle(void)
{
assert(g_isInit);
g_lastAddress = generateRandomNumber();
g_lastInst = (sbyte)(generateRandomNumber() % SINST_COUNT);
if (sm_isValidAt(g_lastAddress)) {
sm_setInstAt(g_lastAddress, (sbyte)g_lastInst);
}
}
|