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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
#include <fstream>
#include "bench_window.hpp"
#include "main.hpp"
bench_window::bench_window()
: runner_stop_now(false),
runner_stop_on_death(true),
runner_stop_on_win(true),
runner_hyperspeed(false),
core_fading(true),
control_box(Gtk::Orientation::VERTICAL),
new_round_button("new round"),
single_step_button("single step"),
start_button("start"),
stop_button("stop"),
core_fading_toggle("core fading"),
hyperspeed_toggle("hyperspeed"),
pause_on_death_toggle("pause on death"),
pause_on_win_toggle("pause on win"),
target_core_rate_label(),
target_core_rate_scale(),
add_warrior_button("add warrior"),
remove_warrior_button("remove warrior"),
runner({}),
runner_active(false) {
warrior_list_store = Gtk::TreeStore::create(warrior_list_columns);
instructions_store = Gtk::TreeStore::create(instructions_columns);
warrior_list_view.set_model(warrior_list_store);
instructions_view.set_model(instructions_store);
instructions_view.get_selection()->set_mode(Gtk::SelectionMode::NONE);
warrior_list_view.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &bench_window::update_buttons));
warrior_list_view.append_column("name", warrior_list_columns.warrior_name);
warrior_list_view.append_column("#p", warrior_list_columns.processes);
warrior_list_view.append_column("pc", warrior_list_columns.next_pc);
instructions_view.append_column("", instructions_columns.address);
instructions_view.append_column("", instructions_columns.instruction);
for (lib94::number_t i = 0; i < LIB94_CORE_SIZE; ++i) {
auto row = instructions_store->append();
(*row)[instructions_columns.address] = i;
}
update_ui();
core_fading_toggle.activate();
pause_on_death_toggle.activate();
pause_on_win_toggle.activate();
new_round_button.signal_clicked().connect(sigc::mem_fun(*this, &bench_window::on_click_new_round));
single_step_button.signal_clicked().connect(sigc::mem_fun(*this, &bench_window::on_click_single_step));
start_button.signal_clicked().connect(sigc::mem_fun(*this, &bench_window::on_click_start));
stop_button.signal_clicked().connect(sigc::mem_fun(*this, &bench_window::on_click_stop));
add_warrior_button.signal_clicked().connect(sigc::mem_fun(*this, &bench_window::on_click_add_warrior));
remove_warrior_button.signal_clicked().connect(sigc::mem_fun(*this, &bench_window::on_click_remove_warrior));
core_fading_toggle.signal_toggled().connect(sigc::mem_fun(*this, &bench_window::on_toggle_core_fading));
hyperspeed_toggle.signal_toggled().connect(sigc::mem_fun(*this, &bench_window::on_toggle_hyperspeed));
pause_on_death_toggle.signal_toggled().connect(sigc::mem_fun(*this, &bench_window::on_toggle_pause_on_death));
pause_on_win_toggle.signal_toggled().connect(sigc::mem_fun(*this, &bench_window::on_toggle_pause_on_win));
target_core_rate_scale.signal_value_changed().connect(sigc::mem_fun(*this, &bench_window::on_target_core_rate_changed));
target_core_rate_scale.set_range(0.0, 4.0);
target_core_rate_scale.set_value(std::log10(20.0));
control_box.append(new_round_button);
control_box.append(single_step_button);
control_box.append(start_button);
control_box.append(stop_button);
control_box.append(core_fading_toggle);
control_box.append(hyperspeed_toggle);
control_box.append(pause_on_death_toggle);
control_box.append(pause_on_win_toggle);
control_box.append(target_core_rate_label);
control_box.append(target_core_rate_scale);
control_box.append(add_warrior_button);
control_box.append(remove_warrior_button);
warrior_list_scroll.set_child(warrior_list_view);
warrior_list_scroll.set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::AUTOMATIC);
warrior_list_scroll.set_vexpand();
control_box.append(warrior_list_scroll);
control_box.append(core_rate_label);
control_box.append(core_render_label);
instructions_scroll.set_child(instructions_view);
instructions_scroll.set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::AUTOMATIC);
control_box.set_size_request(200, -1);
core.set_size_request(200, 200);
core.set_expand();
instructions_scroll.set_size_request(200, -1);
main_box.append(control_box);
main_box.append(core);
main_box.append(instructions_scroll);
set_child(main_box);
set_title("lib94 bench");
runner_update_ui_dispatcher.connect(sigc::mem_fun(*this, &bench_window::update_ui));
runner_stopping_dispatcher.connect(sigc::mem_fun(*this, &bench_window::on_runner_stopping));
}
void bench_window::add_modified_for_instruction_view(std::set<lib94::number_t> the_set) {
for (lib94::number_t n : the_set)
modified_addresses_for_instructions_view.insert(n);
}
const lib94::warrior *bench_window::do_step() {
auto time = std::chrono::system_clock::now();
last_core_step_distance = time - last_core_step_start;
last_core_step_start = time;
const lib94::warrior *result = lib94::single_step<true>();
core.mut.lock();
if (core_fading)
core.age_all();
core.add_new_writes(lib94::get_written_addresses());
core.add_new_reads(lib94::get_read_addresses());
core.add_new_executions(lib94::get_executed_addresses());
core.mut.unlock();
add_modified_for_instruction_view(lib94::get_written_addresses());
add_modified_for_instruction_view(lib94::get_read_addresses());
add_modified_for_instruction_view(lib94::get_executed_addresses());
lib94::clear_address_sets();
return result;
}
static constexpr std::chrono::nanoseconds min_hyperspeed_ui_update_distance(1000000);
void bench_window::runner_main() {
std::chrono::system_clock::time_point last_step = std::chrono::system_clock::now();
core_mutex.lock();
while (!runner_stop_now) {
bool death = do_step() != 0;
if (runner_stop_on_death && death)
break;
if (runner_stop_on_win && death && lib94::alive_warrior_count() == 1)
break;
core_mutex.unlock();
if (runner_hyperspeed) {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
if (now - last_hyperspeed_ui_update >= min_hyperspeed_ui_update_distance) {
last_hyperspeed_ui_update = now;
runner_update_ui_dispatcher.emit();
}
}
else {
runner_update_ui_dispatcher.emit();
std::this_thread::sleep_until(last_step + time_between_steps);
last_step = std::chrono::system_clock::now();
}
core_mutex.lock();
}
runner_stopping_dispatcher.emit();
core_mutex.unlock();
}
void bench_window::on_click_single_step() {
do_step();
update_ui();
}
void bench_window::on_click_new_round() {
lib94::clear_core({
.op = lib94::DAT,
.mod = lib94::F,
.amode = lib94::DIRECT,
.bmode = lib94::DIRECT,
.anumber = 0,
.bnumber = 0
});
lib94::init_round(warriors.data(), warriors.size());
core.mut.lock();
core.age_scale = std::pow(2.0 / 3.0, 1.0 / (float)warriors.size());
core.clear_all();
core.mut.unlock();
modified_addresses_for_instructions_view.clear();
for (lib94::number_t i = 0; i < LIB94_CORE_SIZE; ++i)
instructions_store->children()[i][instructions_columns.instruction]
= lib94::instruction_to_string(lib94::get_instruction(i));
update_ui();
}
void bench_window::on_click_start() {
runner_active = true;
update_ui();
runner_stop_now = false;
runner = std::thread(sigc::mem_fun(*this, &bench_window::runner_main));
}
void bench_window::on_click_stop() {
runner_stop_now = true;
}
void bench_window::on_toggle_core_fading() {
core_fading = core_fading_toggle.get_active();
}
void bench_window::on_toggle_hyperspeed() {
runner_hyperspeed = hyperspeed_toggle.get_active();
}
void bench_window::on_toggle_pause_on_death() {
runner_stop_on_death = pause_on_death_toggle.get_active();
}
void bench_window::on_toggle_pause_on_win() {
runner_stop_on_win = pause_on_win_toggle.get_active();
}
void bench_window::on_target_core_rate_changed() {
double target_rate = std::pow(10.0, target_core_rate_scale.get_value());
target_core_rate_label.set_text("target core rate (" + hz_to_string(target_rate) + ")");
time_between_steps = std::chrono::nanoseconds((int)(1000000000.0 / target_rate));
}
void bench_window::on_add_warrior_dialog_response(int response_id, Gtk::FileChooserDialog *dialog) {
if (response_id == Gtk::ResponseType::OK) {
Glib::RefPtr<Gio::File> file = dialog->get_file();
char *contents;
gsize length;
file->load_contents(contents, length);
try {
warriors.push_back(lib94::compile_warrior(std::string(contents, length)));
on_click_new_round();
}
catch (const lib94::compiler_exception &ex) {
Gtk::MessageDialog *md = new Gtk::MessageDialog(std::string("failed to compile: ") + ex.message + " on line " + std::to_string(ex.line_number));
md->set_transient_for(*this);
md->set_modal();
md->signal_response().connect([md](int) {delete md;});
md->show();
}
delete contents;
}
delete dialog;
}
void bench_window::on_click_add_warrior() {
Gtk::FileChooserDialog *dialog = new Gtk::FileChooserDialog("select a warrior");
dialog->set_transient_for(*this);
dialog->set_modal();
dialog->signal_response().connect(sigc::bind(sigc::mem_fun(*this, &bench_window::on_add_warrior_dialog_response), dialog));
dialog->add_button("add", Gtk::ResponseType::OK);
dialog->add_button("cancel", Gtk::ResponseType::CANCEL);
Glib::RefPtr<Gtk::FileFilter> text_filter = Gtk::FileFilter::create();
text_filter->add_mime_type("text/plain");
text_filter->set_name("text files");
dialog->add_filter(text_filter);
Glib::RefPtr<Gtk::FileFilter> any_filter = Gtk::FileFilter::create();
any_filter->add_pattern("*");
any_filter->set_name("all files");
dialog->add_filter(any_filter);
dialog->show();
}
void bench_window::on_click_remove_warrior() {
auto iter = warrior_list_view.get_selection()->get_selected();
if (iter) {
while (!(*iter)[warrior_list_columns.warrior])
iter = iter->parent();
const lib94::warrior *w = (*iter)[warrior_list_columns.warrior];
for (auto i = warriors.begin(); i != warriors.end(); ++i)
if (*i == w) {
warriors.erase(i);
break;
}
on_click_new_round();
delete w;
}
}
void bench_window::update_buttons() {
new_round_button.set_sensitive(!runner_active && warriors.size() > 0);
single_step_button.set_sensitive(!runner_active && lib94::alive_warrior_count() > 0);
start_button.set_sensitive(!runner_active && lib94::alive_warrior_count() > 0);
stop_button.set_sensitive(runner_active);
add_warrior_button.set_sensitive(!runner_active);
remove_warrior_button.set_sensitive(!runner_active && warrior_list_view.get_selection()->get_selected());
warrior_list_view.get_selection()->set_mode(runner_active ? Gtk::SelectionMode::NONE : Gtk::SelectionMode::SINGLE);
core_render_label.set_text("core render: " + ns_to_string(core.last_draw_time));
core_rate_label.set_text("core rate: " + hz_to_string(1000000000.0 / (double)last_core_step_distance.count()));
}
void bench_window::update_ui() {
if (runner_active && runner_hyperspeed) {
update_buttons();
return;
}
if (runner_active)
core_mutex.lock();
update_buttons();
core.queue_draw();
warrior_list_store->clear();
for (const lib94::warrior *w : warriors) {
auto w_row = warrior_list_store->append();
(*w_row)[warrior_list_columns.warrior] = w;
(*w_row)[warrior_list_columns.warrior_name] = w->name;
auto procs = lib94::get_processes(w);
(*w_row)[warrior_list_columns.processes] = procs.size();
(*w_row)[warrior_list_columns.next_pc] = procs.size() ? std::to_string(procs[0]) : "";
}
for (lib94::number_t i : modified_addresses_for_instructions_view)
instructions_store->children()[i][instructions_columns.instruction]
= lib94::instruction_to_string(lib94::get_instruction(i));
modified_addresses_for_instructions_view.clear();
if (runner_active)
core_mutex.unlock();
}
void bench_window::on_runner_stopping() {
runner.join();
runner_active = false;
update_ui();
}
|