aboutsummaryrefslogtreecommitdiff
path: root/src/mesif-lru-simulation.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mesif-lru-simulation.c')
-rw-r--r--src/mesif-lru-simulation.c705
1 files changed, 705 insertions, 0 deletions
diff --git a/src/mesif-lru-simulation.c b/src/mesif-lru-simulation.c
new file mode 100644
index 0000000..45f99c4
--- /dev/null
+++ b/src/mesif-lru-simulation.c
@@ -0,0 +1,705 @@
1#include <stdbool.h>
2#define _GNU_SOURCE
3#include <stdlib.h>
4#include <string.h>
5#include <stdio.h>
6#include <assert.h>
7#include <time.h>
8#include <raylib.h>
9#include <raymath.h>
10
11#include "config.h"
12#include "font_data.h"
13#include "states.h"
14#include "busses.h"
15#include "connections.h"
16#include "caches.h"
17#include "cpus.h"
18#include "memory.h"
19
20Font font;
21Camera2D camera;
22
23bool is_paused;
24float seconds_since_last_tick;
25
26static inline int
27get_display_width (void)
28{
29 return IsWindowFullscreen ()
30 ? GetMonitorWidth (GetCurrentMonitor ())
31 : GetScreenWidth ();
32}
33
34static inline int
35get_display_height (void)
36{
37 return IsWindowFullscreen ()
38 ? GetMonitorHeight (GetCurrentMonitor ())
39 : GetScreenHeight ();
40}
41
42inline static void
43init_camera (void)
44{
45 camera = (Camera2D) {
46 .target = { 0, 0 },
47 .offset = { get_display_width ()/2., SCREEN_PADDING },
48 .rotation = 0.,
49 .zoom = 1.,
50 };
51}
52
53static inline void
54startup (void)
55{
56 SetConfigFlags (FLAG_MSAA_4X_HINT |
57 FLAG_WINDOW_RESIZABLE |
58 FLAG_VSYNC_HINT);
59 InitWindow (INITIAL_SCREEN_WIDTH,
60 INITIAL_SCREEN_HEIGHT,
61 WINDOW_NAME);
62 SetTargetFPS (60);
63
64 srand (time (NULL));
65
66 // font = LoadFontEx ("res/KodeMono-VariableFont_wght.ttf", 4*FONT_SIZE, NULL, 0);
67 font = LoadFont_FontData ();
68 SetTextureFilter (font.texture, TEXTURE_FILTER_TRILINEAR);
69 // SetTextureFilter (font.texture, TEXTURE_FILTER_ANISOTROPIC_16X);
70
71 init_camera ();
72 reset_state ();
73}
74
75static inline void
76shutdown (void)
77{
78 CloseWindow ();
79}
80
81static inline void
82toggle_fullscreen_window (void)
83{
84 static int window_width;
85 static int window_height;
86
87 if (!IsWindowFullscreen ()) {
88 window_width = GetScreenWidth ();
89 window_height = GetScreenHeight ();
90 int monitor = GetCurrentMonitor ();
91 SetWindowSize (GetMonitorWidth (monitor), GetMonitorHeight (monitor));
92 ToggleFullscreen ();
93 } else {
94 ToggleFullscreen ();
95 SetWindowSize (window_width, window_height);
96 }
97}
98
99static inline void
100handle_input (void)
101{
102 if (IsKeyPressed (KEY_SPACE)) is_paused = is_paused ? false : true;
103 if (IsKeyPressed (KEY_R)) reset_state ();
104
105 if (is_paused) {
106 float wheel = GetMouseWheelMove();
107 if (wheel > 0 && current_tick < last_tick)
108 set_state (current_tick + 1);
109 if (wheel < 0 && current_tick)
110 set_state (current_tick - 1);
111 } else
112 camera.zoom += .1*GetMouseWheelMove();
113
114 if (IsKeyPressed (KEY_F11)) toggle_fullscreen_window ();
115 if (IsKeyDown (KEY_RIGHT)) camera.target.x += 2;
116 if (IsKeyDown (KEY_LEFT )) camera.target.x -= 2;
117 if (IsKeyDown (KEY_DOWN )) camera.target.y += 2;
118 if (IsKeyDown (KEY_UP )) camera.target.y -= 2;
119 if (IsMouseButtonDown (MOUSE_MIDDLE_BUTTON)) camera.target = Vector2Add (camera.target, Vector2Scale (GetMouseDelta (), -1));
120 if (IsKeyDown (KEY_MINUS)) camera.zoom -= .01;
121 if (IsKeyDown (KEY_EQUAL)) camera.zoom += .01;
122 if (IsKeyDown (KEY_H)) {
123 camera.target = (Vector2) { 0 };
124 camera.zoom = 1.;
125 }
126
127 cpu_button_hover = CPU_COUNT*MEMORY_SIZE*3;
128 for (int i = 0; i < CPU_COUNT; ++i) {
129 if (handle_cpu_buttons (cpus[i]))
130 break;
131 }
132}
133
134static inline void
135update (void)
136{
137 camera.offset.x = get_display_width ()/2.;
138
139 // check for cpu button press
140 if (cpu_button_hover != CPU_COUNT*MEMORY_SIZE*3 &&
141 IsMouseButtonReleased (MOUSE_LEFT_BUTTON)) {
142 int i = cpu_button_hover/MEMORY_SIZE/3;
143 CPU cpu = cpus [i];
144 if (cpu->highlighted == MEMORY_SIZE*3) {
145 cpu->highlighted = cpu_button_hover - i*MEMORY_SIZE*3;
146 CONNECTION_START_TRANSACTION_FROM_TO(cons_cpu_cache_addr[i],
147 cpu->highlighted/3);
148 if (cpu->highlighted % 3 == 2) {
149 CONNECTION_START_TRANSACTION_FROM_TO(cons_cpu_cache_data[i], rand ()%(1 << 8));
150 }
151 }
152 }
153
154 // progress all transactions
155 if (!is_paused) {
156 transaction_proggressed = false;
157 for (int i = 0; i < CPU_COUNT; ++i){
158 connection_progress_transaction (cons_cpu_cache_addr [i]);
159 connection_progress_transaction (cons_cpu_cache_data [i]);
160 connection_progress_transaction (cons_cache_bus_shared [i]);
161 connection_progress_transaction (cons_cache_bus_addr [i]);
162 connection_progress_transaction (cons_cache_bus_data [i]);
163 }
164 connection_progress_transaction (con_memory_bus_addr);
165 connection_progress_transaction (con_memory_bus_data);
166 }
167
168 CONNECTION con;
169
170 // TODO(cmmm): extract state advancers into separate functions
171 for (int i = 0; i < CPU_COUNT; ++i) {
172 const CPU cpu = cpus [i];
173 const CACHE cache = caches [i];
174 const CONNECTION con_cpu_cache_addr = cons_cpu_cache_addr [i];
175 const CONNECTION con_cpu_cache_data = cons_cpu_cache_data [i];
176 const CONNECTION con_cache_bus_shared = cons_cache_bus_shared [i];
177 const CONNECTION con_cache_bus_addr = cons_cache_bus_addr [i];
178 const CONNECTION con_cache_bus_data = cons_cache_bus_data [i];
179
180 bool is_addr = false;
181 bool is_data = false;
182
183 con = con_cache_bus_shared;
184 if (con->transaction_just_finished) {
185 switch (con->transaction_just_finished_state) {
186 case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
187 switch (con_cache_bus_addr->transaction_state & CONNECTION_TRANSACTION_STATE_DIRECTION_MASK) {
188 case CONNECTION_TRANSACTION_DIRECTION_FROM_TO: {
189 bus_link_connection (shared_bus, con);
190 } break;
191 case CONNECTION_TRANSACTION_DIRECTION_TO_FROM: {
192 cons_cache_bus_shared[addr_bus->reserved_connection]->transaction_pending = true;
193 connection_end_transaction (con_cache_bus_shared);
194 } break;
195 }
196 } break;
197 case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
198 if ((con_cache_bus_addr->transaction_state &
199 CONNECTION_TRANSACTION_STATE_DIRECTION_MASK) ==
200 CONNECTION_TRANSACTION_DIRECTION_FROM_TO) {
201 bus_unlink_connection (shared_bus);
202 }
203 CONNECTION_CLEAR_TRANSACTION(con);
204 } break;
205 case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
206 switch (CONNECTION_TRANSACTION_STATE_DIRECTION(con_cache_bus_addr)) {
207 case CONNECTION_TRANSACTION_DIRECTION_FROM_TO: {
208 bus_unlink_connection (shared_bus);
209 if (!connection_is_transaction_ongoing (con_cache_bus_data)) {
210 cache->addr_targets = 1 << CONNECTION_MEMORY;
211 cache->addr_value = con_cpu_cache_addr->transaction_value;
212 con_cache_bus_addr->transaction_pending = true;
213 if (con_cache_bus_shared->transaction_value)
214 cache->next_forward = true;
215 }
216 } break;
217 case CONNECTION_TRANSACTION_DIRECTION_TO_FROM: {
218 u8 addr = con_cache_bus_addr->transaction_value;
219 CACHE_LINE line = cache_get (cache, addr, true);
220 if (line) line->state = con_cache_bus_shared->transaction_value;
221 } break;
222 }
223 } break;
224 case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
225 CONNECTION_CLEAR_TRANSACTION(con);
226 if (!connection_is_transaction_ongoing (con_cache_bus_data))
227 connection_end_transaction (con_cache_bus_addr);
228 } break;
229 default: break;
230 }
231 }
232
233 con = con_cache_bus_addr;
234 if (con->transaction_just_finished) {
235 switch (con->transaction_just_finished_state) {
236 case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
237 bus_link_connection (addr_bus, con);
238 } break;
239 case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
240 bus_unlink_connection (addr_bus);
241 FREE_BUS(addr_bus);
242 CONNECTION_CLEAR_TRANSACTION(con);
243 } break;
244 case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
245 if (con_cache_bus_shared->transaction_just_finished)
246 break;
247
248 CACHE_LINE line = cache_get (cache, con_cache_bus_addr->transaction_value, true);
249 enum cache_line_state state = line ? line->state : CACHE_LINE_STATE_INVALID;
250 if (!line) {
251 cache->highlighted_pending = cache->highlighted;
252 cache->highlighted = CPU_COUNT;
253 }
254 if (line &&
255 connection_is_transaction_ongoing (cons_cpu_cache_data[*cache_locking_busses])) {
256 switch (line->state) {
257 case CACHE_LINE_STATE_EXCLUSIVE:
258 case CACHE_LINE_STATE_FORWARD:
259 case CACHE_LINE_STATE_MODIFIED:
260 data_bus->reserved_connection = i;
261 data_bus->connection_targets = 1 << addr_bus->reserved_connection;
262 CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data, line->data);
263 /* fallthrough */
264 case CACHE_LINE_STATE_SHARED:
265 line->state = CACHE_LINE_STATE_INVALID;
266 break;
267 default: break;
268 }
269 } else {
270 switch (state) {
271 case CACHE_LINE_STATE_MODIFIED: {
272 data_bus->reserved_connection = i;
273 data_bus->connection_targets = 1 << addr_bus->reserved_connection |
274 1 << CONNECTION_MEMORY;
275 CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data, line->data);
276 cache_set (cache, line, line->addr, line->data, CACHE_LINE_STATE_SHARED, false);
277 } break;
278 case CACHE_LINE_STATE_EXCLUSIVE: {
279 data_bus->reserved_connection = i;
280 data_bus->connection_targets = 1 << addr_bus->reserved_connection;
281 CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data, line->data);
282 cache_set (cache, line, line->addr, line->data, CACHE_LINE_STATE_SHARED, false);
283 } break;
284 case CACHE_LINE_STATE_SHARED: {
285 } break;
286 case CACHE_LINE_STATE_FORWARD: {
287 data_bus->reserved_connection = i;
288 data_bus->connection_targets = 1 << addr_bus->reserved_connection;
289 CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data, line->data);
290 cache_set (cache, line, line->addr, line->data, CACHE_LINE_STATE_SHARED, false);
291 } break;
292 default: break;
293 }
294 }
295 con_cache_bus_shared->transaction_pending = true;
296 } break;
297 case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
298 CONNECTION_CLEAR_TRANSACTION(con);
299 connection_end_transaction (con_cache_bus_data);
300 cache->highlighted = cache->highlighted_pending;
301 cache->highlighted_pending = CACHE_LINE_COUNT;
302 } break;
303 default: break;
304 }
305 }
306
307 if (con_cache_bus_shared->transaction_pending &&
308 addr_bus->reserved_connection != i &&
309 *cache_locking_busses != i) {
310 // shared_bus.reserved_connection = i;
311 shared_bus->connection_targets = 1 << addr_bus->reserved_connection;
312 bool has = cache->highlighted != CACHE_LINE_COUNT &&
313 cache->lines[cache->highlighted].state != CACHE_LINE_STATE_INVALID;
314 CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_shared, has << i);
315 con_cache_bus_shared->transaction_pending = false;
316 }
317
318 con = con_cache_bus_data;
319 if (con->transaction_just_finished) {
320 switch (con->transaction_just_finished_state) {
321 case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
322 if (cache->lines[cache->highlighted].state == CACHE_LINE_STATE_MODIFIED)
323 CONNECTION_START_TRANSACTION_TO_FROM(con_memory_bus_addr, cache->lines[cache->highlighted].addr);
324 bus_link_connection (data_bus, con);
325 } break;
326 case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
327 bus_unlink_connection (data_bus);
328 CONNECTION_CLEAR_TRANSACTION(con);
329 } break;
330 case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
331 if (connection_is_transaction_ongoing (con_cpu_cache_data)) { // RWITM finished
332 cache_set (cache, NULL,
333 con_cpu_cache_addr->transaction_value,
334 con_cpu_cache_data->transaction_value,
335 CACHE_LINE_STATE_MODIFIED, true);
336 con_cache_bus_addr->transaction_pending = false;
337 connection_end_transaction (con_cpu_cache_addr);
338 connection_end_transaction (con_cpu_cache_data);
339 } else {
340 enum cache_line_state next_state = data_bus->reserved_connection == CONNECTION_MEMORY && !cache->next_forward
341 ? CACHE_LINE_STATE_EXCLUSIVE
342 : CACHE_LINE_STATE_FORWARD;
343 cache->next_forward = false;
344 cache_set (cache, NULL,
345 con_cache_bus_addr->transaction_value,
346 con_cache_bus_data->transaction_value,
347 next_state, true);
348 CONNECTION_START_TRANSACTION_TO_FROM(con_cpu_cache_data,
349 con->transaction_value);
350 }
351 } break;
352 case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
353 if (!connection_is_transaction_complete (con_cpu_cache_data)) {
354 cache->highlighted = cache->highlighted_pending;
355 cache->highlighted_pending = CACHE_LINE_COUNT;
356 }
357 FREE_BUS(data_bus);
358 CONNECTION_CLEAR_TRANSACTION(con);
359 } break;
360 default: break;
361 }
362 }
363
364 con = con_cpu_cache_addr;
365 if (con->transaction_just_finished) {
366 switch (con->transaction_just_finished_state) {
367 case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
368 is_addr = true;
369 } break;
370 case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
371 cache->highlighted = cache->highlighted_pending;
372 cache->highlighted_pending = CACHE_LINE_COUNT;
373 if (CONNECTION_TRANSACTION_STATE_DIRECTION(con_cache_bus_addr) ==
374 CONNECTION_TRANSACTION_DIRECTION_FROM_TO) {
375 connection_end_transaction (con_cache_bus_addr);
376 }
377 connection_end_transaction (con_cpu_cache_data);
378 CONNECTION_CLEAR_TRANSACTION(con);
379 } break;
380 default: break;
381 }
382 }
383
384 con = con_cpu_cache_data;
385 if (con->transaction_just_finished) {
386 switch (con->transaction_just_finished_state) {
387 case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
388 is_data = true;
389 } break;
390 case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
391 connection_end_transaction (con_cache_bus_addr);
392 connection_end_transaction (con_cache_bus_shared);
393 cpu->highlighted = 3*MEMORY_SIZE;
394 *cache_locking_busses = CPU_COUNT;
395 CONNECTION_CLEAR_TRANSACTION(con);
396 } break;
397 case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
398 connection_end_transaction (con_cpu_cache_addr);
399 } break;
400 case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
401 cache->highlighted = cache->highlighted_pending;
402 cache->highlighted_pending = CACHE_LINE_COUNT;
403 CONNECTION_CLEAR_TRANSACTION(con);
404 if (cpu->highlighted % 3 == 1) {
405 CONNECTION_START_TRANSACTION_FROM_TO(con_cpu_cache_addr, con_cpu_cache_addr->transaction_value);
406 CONNECTION_START_TRANSACTION_FROM_TO(con_cpu_cache_data, con_cpu_cache_data->transaction_value + 1);
407 } else {
408 cpu->highlighted = 3*MEMORY_SIZE;
409 }
410 *cache_locking_busses = CPU_COUNT;
411 } break;
412 default: break;
413 }
414 }
415
416 // Process READ/WRITE request if any
417 if (is_addr || cache->continue_addr) {
418 cache->continue_addr = false;
419 u8 addr = con_cpu_cache_addr->transaction_value;
420 CACHE_LINE line = cache_get (cache, addr, true);
421 CACHE_LINE to_replace = cache_to_replace (cache, addr);
422 if (!line && !cache->copy_back && to_replace && to_replace->state == CACHE_LINE_STATE_MODIFIED) {
423 cache->invalidate_others = true;
424
425 cache->addr_targets = ~(1 << i) & CONNECTION_CACHES | 1 << CONNECTION_MEMORY;
426 cache->addr_value = to_replace->addr;
427 con_cache_bus_addr->transaction_pending = true;
428
429 cache->data_targets = 1 << CONNECTION_MEMORY;
430 cache->data_value = to_replace->data;
431 con_cache_bus_data->transaction_pending = true;
432
433 cache->copy_back = true;
434 } else {
435 cache->copy_back = false;
436 enum cache_line_state state = line ? line->state : CACHE_LINE_STATE_INVALID;
437 if (!is_data || cache->continue_data) { // Process CPU READ
438 cache->continue_data = false;
439 switch (state) {
440 case CACHE_LINE_STATE_MODIFIED:
441 case CACHE_LINE_STATE_EXCLUSIVE:
442 case CACHE_LINE_STATE_SHARED:
443 case CACHE_LINE_STATE_FORWARD: {
444 cache->data_targets = 1 << *cache_locking_busses;
445 CONNECTION_START_TRANSACTION_TO_FROM(con_cpu_cache_data, line->data);
446 } break;
447 case CACHE_LINE_STATE_INVALID: {
448 cache->addr_targets = ~(1 << i) & CONNECTION_CACHES;
449 cache->addr_value = con_cpu_cache_addr->transaction_value;
450 con_cache_bus_addr->transaction_pending = true;
451 } break;
452 default: break;
453 }
454 } else { // Process CPU WRITE
455 u8 data = con_cpu_cache_data->transaction_value;
456 switch (state) {
457 case CACHE_LINE_STATE_EXCLUSIVE: {
458 cache_set (cache, line, addr, data, CACHE_LINE_STATE_MODIFIED, false);
459 connection_end_transaction (con_cpu_cache_addr);
460 connection_end_transaction (con_cpu_cache_data);
461 } break;
462 case CACHE_LINE_STATE_MODIFIED: {
463 cache_set (cache, line, addr, data, CACHE_LINE_STATE_MODIFIED, false);
464 connection_end_transaction (con_cpu_cache_addr);
465 connection_end_transaction (con_cpu_cache_data);
466 } break;
467 case CACHE_LINE_STATE_FORWARD:
468 case CACHE_LINE_STATE_SHARED: {
469 cache_set (cache, line, addr, data, CACHE_LINE_STATE_MODIFIED, false);
470 cache->invalidate_others = true;
471
472 cache->addr_targets = ~(1 << i) & CONNECTION_CACHES;
473 cache->addr_value = con_cpu_cache_addr->transaction_value;
474 con_cache_bus_addr->transaction_pending = true;
475 } break;
476 case CACHE_LINE_STATE_INVALID: {
477 cache->addr_targets = ~(1 << i) & CONNECTION_CACHES;
478 cache->addr_value = con_cpu_cache_addr->transaction_value;
479 con_cache_bus_addr->transaction_pending = true;
480 } break;
481 default:
482 break;
483 }
484 }
485 }
486 }
487
488 CACHE_LINE line = NULL;
489 for (int i = 0; i < CACHE_LINE_COUNT; ++i) {
490 if (cache->lines[i].state != CACHE_LINE_STATE_INVALID &&
491 cache->lines[i].addr == cache->addr_value) {
492 line = &cache->lines[i];
493 }
494 }
495 if (con_cache_bus_addr->transaction_pending &&
496 con_cache_bus_data->transaction_pending &&
497 IS_BUS_FREE(addr_bus) &&
498 IS_BUS_FREE(data_bus) &&
499 !line) {
500 // cache_set (cache, line, cache->addr_value, cache->data_value, CACHE_LINE_STATE_MODIFIED);
501
502 *cache_locking_busses = i;
503 *bus_cycles += 1;
504 addr_bus->connection_targets = cache->addr_targets;
505 addr_bus->reserved_connection = i;
506 CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_addr,
507 cache->addr_value);
508 con_cache_bus_addr->transaction_pending = false;
509
510 if (cache->invalidate_others) {
511 shared_bus->reserved_connection = i;
512 shared_bus->connection_targets = ~(1 << i) & CONNECTION_CACHES;
513 CONNECTION_START_TRANSACTION_FROM_TO (con_cache_bus_shared,
514 CACHE_LINE_STATE_INVALID);
515 con_cache_bus_shared->transaction_pending = false;
516 }
517 } else if (con_cache_bus_addr->transaction_pending &&
518 IS_BUS_FREE(addr_bus) &&
519 IS_BUS_FREE(data_bus) &&
520 !connection_is_transaction_ongoing (con_cache_bus_addr) &&
521 !connection_is_transaction_ongoing (con_cache_bus_data) &&
522 (*cache_locking_busses == CPU_COUNT ||
523 *cache_locking_busses == i)) {
524 *cache_locking_busses = i;
525 *bus_cycles += 1;
526 addr_bus->connection_targets = cache->addr_targets;
527 addr_bus->reserved_connection = i;
528 CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_addr,
529 cache->addr_value);
530 con_cache_bus_addr->transaction_pending = false;
531
532 if (cache->invalidate_others) {
533 shared_bus->reserved_connection = i;
534 shared_bus->connection_targets = ~(1 << i) & CONNECTION_CACHES;
535 CONNECTION_START_TRANSACTION_FROM_TO (con_cache_bus_shared,
536 CACHE_LINE_STATE_INVALID);
537 cache->invalidate_others = false;
538 /* connection_end_transaction (con_cpu_cache_addr); */
539 /* connection_end_transaction (con_cpu_cache_data); */
540 }
541
542 if (con_cache_bus_data->transaction_pending &&
543 IS_BUS_FREE(data_bus)) {
544 data_bus->connection_targets = cache->data_targets;
545 data_bus->reserved_connection = i;
546 CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data,
547 cache->data_value);
548 con_cache_bus_data->transaction_pending = false;
549
550 shared_bus->reserved_connection = i;
551 shared_bus->connection_targets = ~(1 << i) & CONNECTION_CACHES;
552 CONNECTION_START_TRANSACTION_FROM_TO (con_cache_bus_shared,
553 CACHE_LINE_STATE_INVALID);
554 con_cache_bus_shared->transaction_pending = false;
555
556 if (cache->invalidate_others && line)
557 cache_set (cache, line, cache->addr_value, cache->data_value, CACHE_LINE_STATE_EXCLUSIVE, false);
558
559 cache->invalidate_others = false;
560 }
561 } else if (con_cache_bus_data->transaction_pending &&
562 IS_BUS_FREE(data_bus) &&
563 (*cache_locking_busses == CPU_COUNT ||
564 *cache_locking_busses == i)) {
565 *cache_locking_busses = i;
566 *bus_cycles += 1;
567 data_bus->connection_targets = cache->data_targets;
568 data_bus->reserved_connection = i;
569 CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data,
570 cache->data_value);
571 con_cache_bus_data->transaction_pending = false;
572 }
573 }
574
575 for (int i = 0; i < CPU_COUNT; ++i) {
576 if (cons_cache_bus_shared[i]->transaction_pending &&
577 addr_bus->reserved_connection == i) {
578 bus_link_connections (shared_bus, cons_cache_bus_shared); // BUG(cmmm): started twice on double READ
579 cons_cache_bus_shared[i]->transaction_pending = false;
580 }
581 }
582
583 con = con_memory_bus_addr;
584 if (con->transaction_just_finished) {
585 switch (con->transaction_just_finished_state) {
586 case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
587 int addr = con->transaction_value;
588 memory->highlighted = addr;
589 if (!connection_is_transaction_ongoing (con_memory_bus_data)) {
590 data_bus->reserved_connection = CONNECTION_MEMORY;
591 CONNECTION_START_TRANSACTION_FROM_TO(con_memory_bus_data, memory->entries[addr]);
592 } else if (connection_is_transaction_direction_from_to (cons_cache_bus_data[addr_bus->reserved_connection])) {
593 if (caches[addr_bus->reserved_connection]->copy_back) {
594 caches[addr_bus->reserved_connection]->continue_addr = connection_is_transaction_ongoing (cons_cpu_cache_addr[addr_bus->reserved_connection]);
595 caches[addr_bus->reserved_connection]->continue_data = connection_is_transaction_ongoing (cons_cpu_cache_data[addr_bus->reserved_connection]);
596 }
597 }
598 } break;
599 case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
600 FREE_BUS(addr_bus);
601 memory->highlighted = MEMORY_SIZE;
602 connection_end_transaction (con_memory_bus_data);
603 CONNECTION_CLEAR_TRANSACTION(con);
604 } break;
605 default: break;
606 }
607 }
608
609 con = con_memory_bus_data;
610 if (con->transaction_just_finished) {
611 switch (con->transaction_just_finished_state) {
612 case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
613 data_bus->connection_targets = 1 << addr_bus->reserved_connection;
614 bus_link_connection (data_bus, con_memory_bus_data);
615 } break;
616 case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
617 bus_unlink_connection (data_bus);
618 CONNECTION_CLEAR_TRANSACTION(con);
619 } break;
620 case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
621 memory->entries[con_memory_bus_addr->transaction_value] = con->transaction_value;
622 if (connection_is_transaction_ongoing (cons_cpu_cache_data[data_bus->reserved_connection]) &&
623 connection_is_transaction_direction_to_from (cons_cpu_cache_data[data_bus->reserved_connection])) {
624 connection_end_transaction (cons_cpu_cache_addr[addr_bus->reserved_connection]);
625 connection_end_transaction (cons_cpu_cache_data[data_bus->reserved_connection]);
626 } else {
627 connection_end_transaction (cons_cache_bus_shared[data_bus->reserved_connection]);
628 connection_end_transaction (cons_cache_bus_addr[data_bus->reserved_connection]);
629 connection_end_transaction (cons_cache_bus_data[data_bus->reserved_connection]);
630 }
631 } break;
632 case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
633 FREE_BUS(data_bus);
634 CONNECTION_CLEAR_TRANSACTION(con);
635 } break;
636 default: break;
637 }
638 }
639
640 // Save state
641 if (!is_paused && transaction_proggressed) {
642 if (seconds_since_last_tick >= TICK_SECONDS) {
643 seconds_since_last_tick = 0;
644 save_state ();
645 } else
646 seconds_since_last_tick += GetFrameTime ();
647 }
648}
649
650static inline void
651draw (void)
652{
653 BeginDrawing ();
654 ClearBackground (COLOR_BACKGROUND);
655 BeginMode2D (camera);
656
657 draw_bus (data_bus);
658 draw_bus (addr_bus);
659 draw_bus (shared_bus);
660 for (int i = 0; i < CPU_COUNT; ++i) {
661 draw_cache (caches[i]);
662 draw_cpu (cpus[i]);
663 }
664 draw_memory (memory);
665
666 EndMode2D();
667
668 if (is_paused) {
669 DrawRectangle ( 5, 5, 10, 40, GRAY);
670 DrawRectangle (25, 5, 10, 40, GRAY);
671
672 // draw tick
673 char *tick_text;
674 asprintf (&tick_text, "%lu", current_tick);
675 DrawTextEx (font, tick_text, (Vector2) { 5, 43 }, FONT_SIZE, TEXT_SPACING, GRAY);
676 free (tick_text);
677 }
678
679 char *bus_cycles_text;
680 asprintf (&bus_cycles_text, "BUS CYCLES: %d", *bus_cycles);
681 float bus_cycles_text_width = MeasureTextEx (font, bus_cycles_text, FONT_SIZE, TEXT_SPACING).x;
682 Color bus_cycles_text_color = IS_BUS_FREE(data_bus) && IS_BUS_FREE(addr_bus) ? COLOR_TEXT : COLOR_DATA;
683 DrawTextEx (font, bus_cycles_text, (Vector2) { GetScreenWidth ()/2 - bus_cycles_text_width/2, 0 }, FONT_SIZE, TEXT_SPACING, bus_cycles_text_color);
684 free (bus_cycles_text);
685
686 const char *info_text = "Press R to reset...";
687 float info_text_width = MeasureTextEx (font, info_text, FONT_SIZE, TEXT_SPACING).x;
688 DrawTextEx (font, info_text, (Vector2) { GetScreenWidth () - info_text_width, 0 }, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
689
690 // DrawFPS (2, 0);
691 EndDrawing ();
692}
693
694int
695main (void)
696{
697 startup ();
698 while (!WindowShouldClose ()) {
699 handle_input ();
700 update ();
701 draw ();
702 } shutdown ();
703
704 return EXIT_SUCCESS;
705}