forked from anyaevostinar/SymbulationEmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstructions.h
More file actions
246 lines (208 loc) · 7.64 KB
/
Copy pathInstructions.h
File metadata and controls
246 lines (208 loc) · 7.64 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#ifndef INSTRUCTIONS_H
#define INSTRUCTIONS_H
#include "CPUState.h"
// #include "SGPWorld.h"
// #include "Tasks.h"
#include "sgpl/hardware/Cpu.hpp"
#include "sgpl/operations/flow_global/Anchor.hpp"
#include "sgpl/program/Program.hpp"
#include "sgpl/utility/ThreadLocalRandom.hpp"
#include <functional>
#include <mutex>
namespace sgpmode::inst {
// TODO - Implement an instruction library to help manage instruction set?
// NOTE - discuss register value typing (float vs unsigned integer)
/**
* Macro to easily create an instruction:
* `INST(MyInstruction, { *a = *b + 2;})`. In the code block, operand registers
* are visible as `a`, `b`, and `c`, all of type `uint32_t *`. Instructions may
* also access the `Core &core`, `Instruction &inst`, `Program &program`, and
* `CPUState &state`.
*/
#define INST(InstName, InstCode) \
struct InstName { \
template <typename HW_SPEC_T> \
static void run( \
sgpl::Core<HW_SPEC_T>& core, \
const sgpl::Instruction<HW_SPEC_T>& inst, \
const sgpl::Program<HW_SPEC_T>& program, \
CPUState<typename HW_SPEC_T::world_t>& state \
) { \
uint32_t& a = *reinterpret_cast<uint32_t*>(&core.registers[inst.args[0]]); \
uint32_t& b = *reinterpret_cast<uint32_t*>(&core.registers[inst.args[1]]); \
uint32_t& c = *reinterpret_cast<uint32_t*>(&core.registers[inst.args[2]]); \
/* avoid "unused variable" warnings */ \
a = a, b = b, c = c; \
InstCode \
} \
static size_t prevalence() { return 1; } \
static std::string name() { return #InstName; } \
};
INST(Increment, {
// core.registers[inst.args[0]] += 1;
a += 1;
});
INST(Decrement, {
// core.registers[inst.args[0]] -= 1;
a -= 1;
});
// Unary shift (>>1 or <<1)
INST(ShiftLeft, { a <<= 1; });
INST(ShiftRight, { a >>= 1; });
INST(Add, { a = b + c; });
INST(Subtract, { a = b - c; });
INST(Nand, {
a = ~(b & c);
// a_uint = ~(b_uint & c_uint);
// const size_t arg0 = inst.args[0];
// const size_t arg1 = inst.args[1];
// const size_t arg2 = inst.args[2];
// // Work with raw bit representation of floats
// std::transform(
// reinterpret_cast<std::byte*>( &core.registers[arg1] ),
// reinterpret_cast<std::byte*>( &core.registers[arg1] ) + sizeof( core.registers[b] ),
// reinterpret_cast<std::byte*>( &core.registers[arg2] ),
// reinterpret_cast<std::byte*>( &core.registers[arg0] ),
// [](const std::byte b, const std::byte c){ return ~(b & c); }
// );
});
INST(Push, {
// Push value in register a to active stack.
state.GetStacks().Push(a);
});
INST(Pop, {
if (auto val = state.GetStacks().Pop()) {
a = val.value();
} else {
a = 0;
}
});
INST(SwapStack, {
state.GetStacks().ChangeActive();
});
INST(Swap, { std::swap(a, b); });
INST(Reproduce, {
const emp::WorldPosition& org_loc = state.GetLocation();
// Check whether this attempt at reproduction is allowed.
auto& world_config = state.GetWorld().GetConfig();
const bool too_soon = (state.IsHost()) ?
state.GetCPUCyclesSinceRepro() < world_config.HOST_MIN_CYCLES_BEFORE_REPRO() :
state.GetCPUCyclesSinceRepro() < world_config.SYM_MIN_CYCLES_BEFORE_REPRO();
const bool invalid_attempt = state.ReproInProgress() || !org_loc.IsValid()
|| state.ReproAttempt() || too_soon;
if (invalid_attempt) {
return;
}
state.MarkReproAttempt();
});
INST(IO, {
// (1) Add output to output buffer
state.GetOutputBuffer().emplace_back(a);
// (2) Read next value from input buffer (advancing buffer read ptr)
a = state.GetInputBuffer().read();
});
// INST(Input, {
// a = state.GetInputBuffer().read();
// });
// INST(Output, {
// state.GetOutputBuffer().emplace_back(a);
// });
// NOTE - Discuss whether we want to be using custom jump table vs. using signalgp's
// module infrastructure.
INST(JumpIfNEq, {
if (a != b) {
core.JumpToIndex(state.GetJumpDest(core.GetProgramCounter()));
}
});
INST(JumpIfLess, {
if (a < b) {
core.JumpToIndex(state.GetJumpDest(core.GetProgramCounter()));
}
});
INST(JumpIfEq, {
if (a == b) {
core.JumpToIndex(state.GetJumpDest(core.GetProgramCounter()));
}
});
// INST(Jump, {
// core.JumpToIndex(state.GetJumpDest(core.GetProgramCounter()));
// });
// BOOKMARK
// TODO - Donate / Steal instructions
INST(Donate, {
// This instruction does nothing if executed by a host or if this is a symbiont
// without a host.
if (state.IsHost() || !state.HasHost()) {
return;
}
// If we're here, we know that we have a symbiont with a host.
state.GetWorld().SymDonateToHost(state.GetOrg(), state.GetHost());
});
INST(Steal, {
// This instruction does nothing if executed by a
if (state.IsHost() || !state.HasHost()) {
return;
}
state.GetWorld().SymStealFromHost(state.GetOrg(), state.GetHost());
});
// Only active if free living sym mode turned on
INST(Infect, {
// Check that this is neither a host or a hosted symbiont
if (state.IsHost() || state.HasHost()) {
return;
}
state.GetWorld().FreeLivingSymDoInfect(state.GetOrg());
});
// only active if ENABLE_TEMP_CHANGING_ENVIRONMENT turned on and static turned off
INST(SenseTask, {
const size_t env_task_id = state.GetTaskEnvID();
auto& task_env = state.GetWorld().GetTaskEnv();
const auto& task_io = task_env.GetIOBank().GetIO(env_task_id);
// Check loaded value
if (task_io.IsValidOutput(a)) {
// Yes, this output is correct.
// Get all task ids associated with this output value
const emp::vector<size_t>& task_ids = task_io.GetTaskIDs(a);
// Give credit for completed tasks
for (size_t task_id : task_ids) {
// Is this a host task?
if (!task_env.IsHostTask(task_id)) continue;
// Not first task
const bool not_first_task = state.GetWorld().GetConfig().HOST_ONLY_FIRST_TASK_CREDIT() && state.GetFirstTaskPerformed().Any() && !state.GetFirstTaskPerformed().Get(task_id);
if (not_first_task) {
continue;
}
// Has this organism already gotten credit with this output on this task?
if (state.OutputCredited(task_id, a)) continue;
// Check task requirements
auto& task_req_info = task_env.GetHostTaskReq(task_id);
if (!state.GetWorld().CanPerformTask(state, task_req_info)) {
continue;
}
// check task reward or punishment
b = task_req_info.task_value > 0;
return;
}
}
});
// NOTE - Discuss following old instructions that were unused (and whether we still want them)
/*
INST(Reuptake, {
uint32_t next;
AddOrganismPoints(state, *a);
// Only get resources if the organism has values in their internal environment
if (state.internal_environment->size() > 0) {
// Take a resource from back of internal environment vector
next = state.internal_environment->back();
// Clear out the selected resource from Internal Environment
state.internal_environment->pop_back();
*a = next;
state.input_buf.push(next);
} else {
// Otherwise, reset the register to 0
*a = 0;
}
});
*/
} // namespace inst
#endif