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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
|
#include <stdbool.h>
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <raylib.h>
#include <raymath.h>
#include "config.h"
#include "font_data.h"
#include "states.h"
#include "busses.h"
#include "connections.h"
#include "caches.h"
#include "cpus.h"
#include "memory.h"
Font font;
Camera2D camera;
bool is_paused;
float seconds_since_last_tick;
static inline int
get_display_width (void)
{
return IsWindowFullscreen ()
? GetMonitorWidth (GetCurrentMonitor ())
: GetScreenWidth ();
}
static inline int
get_display_height (void)
{
return IsWindowFullscreen ()
? GetMonitorHeight (GetCurrentMonitor ())
: GetScreenHeight ();
}
inline static void
init_camera (void)
{
camera = (Camera2D) {
.target = { 0, 0 },
.offset = { get_display_width ()/2., SCREEN_PADDING },
.rotation = 0.,
.zoom = 1.,
};
}
static inline void
startup (void)
{
SetConfigFlags (FLAG_MSAA_4X_HINT |
FLAG_WINDOW_RESIZABLE |
FLAG_VSYNC_HINT);
InitWindow (INITIAL_SCREEN_WIDTH,
INITIAL_SCREEN_HEIGHT,
WINDOW_NAME);
SetTargetFPS (60);
srand (time (NULL));
// font = LoadFontEx ("res/KodeMono-VariableFont_wght.ttf", 4*FONT_SIZE, NULL, 0);
font = LoadFont_FontData ();
SetTextureFilter (font.texture, TEXTURE_FILTER_TRILINEAR);
// SetTextureFilter (font.texture, TEXTURE_FILTER_ANISOTROPIC_16X);
init_camera ();
reset_state ();
}
static inline void
shutdown (void)
{
CloseWindow ();
}
static inline void
toggle_fullscreen_window (void)
{
static int window_width;
static int window_height;
if (!IsWindowFullscreen ()) {
window_width = GetScreenWidth ();
window_height = GetScreenHeight ();
int monitor = GetCurrentMonitor ();
SetWindowSize (GetMonitorWidth (monitor), GetMonitorHeight (monitor));
ToggleFullscreen ();
} else {
ToggleFullscreen ();
SetWindowSize (window_width, window_height);
}
}
static inline void
handle_input (void)
{
if (IsKeyPressed (KEY_SPACE)) is_paused = is_paused ? false : true;
if (IsKeyPressed (KEY_R)) reset_state ();
if (is_paused) {
float wheel = GetMouseWheelMove();
if (wheel > 0 && current_tick < last_tick)
set_state (current_tick + 1);
if (wheel < 0 && current_tick)
set_state (current_tick - 1);
} else
camera.zoom += .1*GetMouseWheelMove();
if (IsKeyPressed (KEY_F11)) toggle_fullscreen_window ();
if (IsKeyDown (KEY_RIGHT)) camera.target.x += 2;
if (IsKeyDown (KEY_LEFT )) camera.target.x -= 2;
if (IsKeyDown (KEY_DOWN )) camera.target.y += 2;
if (IsKeyDown (KEY_UP )) camera.target.y -= 2;
if (IsMouseButtonDown (MOUSE_MIDDLE_BUTTON)) camera.target = Vector2Add (camera.target, Vector2Scale (GetMouseDelta (), -1));
if (IsKeyDown (KEY_MINUS)) camera.zoom -= .01;
if (IsKeyDown (KEY_EQUAL)) camera.zoom += .01;
if (IsKeyDown (KEY_H)) {
camera.target = (Vector2) { 0 };
camera.zoom = 1.;
}
cpu_button_hover = CPU_COUNT*MEMORY_SIZE*3;
for (int i = 0; i < CPU_COUNT; ++i) {
if (handle_cpu_buttons (cpus[i]))
break;
}
}
static inline void
update (void)
{
camera.offset.x = get_display_width ()/2.;
// check for cpu button press
if (cpu_button_hover != CPU_COUNT*MEMORY_SIZE*3 &&
IsMouseButtonReleased (MOUSE_LEFT_BUTTON)) {
int i = cpu_button_hover/MEMORY_SIZE/3;
CPU cpu = cpus [i];
if (cpu->highlighted == MEMORY_SIZE*3) {
cpu->highlighted = cpu_button_hover - i*MEMORY_SIZE*3;
CONNECTION_START_TRANSACTION_FROM_TO(cons_cpu_cache_addr[i],
cpu->highlighted/3);
if (cpu->highlighted % 3 == 2) {
CONNECTION_START_TRANSACTION_FROM_TO(cons_cpu_cache_data[i], rand ()%(1 << 8));
}
}
}
// progress all transactions
if (!is_paused) {
transaction_proggressed = false;
for (int i = 0; i < CPU_COUNT; ++i){
connection_progress_transaction (cons_cpu_cache_addr [i]);
connection_progress_transaction (cons_cpu_cache_data [i]);
connection_progress_transaction (cons_cache_bus_shared [i]);
connection_progress_transaction (cons_cache_bus_addr [i]);
connection_progress_transaction (cons_cache_bus_data [i]);
}
connection_progress_transaction (con_memory_bus_addr);
connection_progress_transaction (con_memory_bus_data);
}
CONNECTION con;
// TODO(cmmm): extract state advancers into separate functions
for (int i = 0; i < CPU_COUNT; ++i) {
const CPU cpu = cpus [i];
const CACHE cache = caches [i];
const CONNECTION con_cpu_cache_addr = cons_cpu_cache_addr [i];
const CONNECTION con_cpu_cache_data = cons_cpu_cache_data [i];
const CONNECTION con_cache_bus_shared = cons_cache_bus_shared [i];
const CONNECTION con_cache_bus_addr = cons_cache_bus_addr [i];
const CONNECTION con_cache_bus_data = cons_cache_bus_data [i];
bool is_addr = false;
bool is_data = false;
con = con_cache_bus_shared;
if (con->transaction_just_finished) {
switch (con->transaction_just_finished_state) {
case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
switch (con_cache_bus_addr->transaction_state & CONNECTION_TRANSACTION_STATE_DIRECTION_MASK) {
case CONNECTION_TRANSACTION_DIRECTION_FROM_TO: {
bus_link_connection (shared_bus, con);
} break;
case CONNECTION_TRANSACTION_DIRECTION_TO_FROM: {
cons_cache_bus_shared[addr_bus->reserved_connection]->transaction_pending = true;
connection_end_transaction (con_cache_bus_shared);
} break;
}
} break;
case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
if ((con_cache_bus_addr->transaction_state &
CONNECTION_TRANSACTION_STATE_DIRECTION_MASK) ==
CONNECTION_TRANSACTION_DIRECTION_FROM_TO) {
bus_unlink_connection (shared_bus);
}
CONNECTION_CLEAR_TRANSACTION(con);
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
switch (CONNECTION_TRANSACTION_STATE_DIRECTION(con_cache_bus_addr)) {
case CONNECTION_TRANSACTION_DIRECTION_FROM_TO: {
bus_unlink_connection (shared_bus);
if (!connection_is_transaction_ongoing (con_cache_bus_data)) {
cache->addr_targets = 1 << CONNECTION_MEMORY;
cache->addr_value = con_cpu_cache_addr->transaction_value;
con_cache_bus_addr->transaction_pending = true;
if (con_cache_bus_shared->transaction_value)
cache->next_forward = true;
}
} break;
case CONNECTION_TRANSACTION_DIRECTION_TO_FROM: {
u8 addr = con_cache_bus_addr->transaction_value;
CACHE_LINE line = cache_get (cache, addr, true);
if (line) line->state = con_cache_bus_shared->transaction_value;
} break;
}
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
CONNECTION_CLEAR_TRANSACTION(con);
if (!connection_is_transaction_ongoing (con_cache_bus_data))
connection_end_transaction (con_cache_bus_addr);
} break;
default: break;
}
}
con = con_cache_bus_addr;
if (con->transaction_just_finished) {
switch (con->transaction_just_finished_state) {
case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
bus_link_connection (addr_bus, con);
} break;
case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
bus_unlink_connection (addr_bus);
FREE_BUS(addr_bus);
CONNECTION_CLEAR_TRANSACTION(con);
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
if (con_cache_bus_shared->transaction_just_finished)
break;
CACHE_LINE line = cache_get (cache, con_cache_bus_addr->transaction_value, true);
enum cache_line_state state = line ? line->state : CACHE_LINE_STATE_INVALID;
if (!line) {
cache->highlighted_pending = cache->highlighted;
cache->highlighted = CPU_COUNT;
}
if (line &&
connection_is_transaction_ongoing (cons_cpu_cache_data[*cache_locking_busses])) {
switch (line->state) {
case CACHE_LINE_STATE_EXCLUSIVE:
case CACHE_LINE_STATE_FORWARD:
case CACHE_LINE_STATE_MODIFIED:
data_bus->reserved_connection = i;
data_bus->connection_targets = 1 << addr_bus->reserved_connection;
CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data, line->data);
/* fallthrough */
case CACHE_LINE_STATE_SHARED:
line->state = CACHE_LINE_STATE_INVALID;
break;
default: break;
}
} else {
switch (state) {
case CACHE_LINE_STATE_MODIFIED: {
data_bus->reserved_connection = i;
data_bus->connection_targets = 1 << addr_bus->reserved_connection |
1 << CONNECTION_MEMORY;
CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data, line->data);
cache_set (cache, line, line->addr, line->data, CACHE_LINE_STATE_SHARED, false);
} break;
case CACHE_LINE_STATE_EXCLUSIVE: {
data_bus->reserved_connection = i;
data_bus->connection_targets = 1 << addr_bus->reserved_connection;
CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data, line->data);
cache_set (cache, line, line->addr, line->data, CACHE_LINE_STATE_SHARED, false);
} break;
case CACHE_LINE_STATE_SHARED: {
} break;
case CACHE_LINE_STATE_FORWARD: {
data_bus->reserved_connection = i;
data_bus->connection_targets = 1 << addr_bus->reserved_connection;
CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data, line->data);
cache_set (cache, line, line->addr, line->data, CACHE_LINE_STATE_SHARED, false);
} break;
default: break;
}
}
con_cache_bus_shared->transaction_pending = true;
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
CONNECTION_CLEAR_TRANSACTION(con);
connection_end_transaction (con_cache_bus_data);
cache->highlighted = cache->highlighted_pending;
cache->highlighted_pending = CACHE_LINE_COUNT;
} break;
default: break;
}
}
if (con_cache_bus_shared->transaction_pending &&
addr_bus->reserved_connection != i &&
*cache_locking_busses != i) {
// shared_bus.reserved_connection = i;
shared_bus->connection_targets = 1 << addr_bus->reserved_connection;
bool has = cache->highlighted != CACHE_LINE_COUNT &&
cache->lines[cache->highlighted].state != CACHE_LINE_STATE_INVALID;
CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_shared, has << i);
con_cache_bus_shared->transaction_pending = false;
}
con = con_cache_bus_data;
if (con->transaction_just_finished) {
switch (con->transaction_just_finished_state) {
case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
if (cache->lines[cache->highlighted].state == CACHE_LINE_STATE_MODIFIED)
CONNECTION_START_TRANSACTION_TO_FROM(con_memory_bus_addr, cache->lines[cache->highlighted].addr);
bus_link_connection (data_bus, con);
} break;
case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
bus_unlink_connection (data_bus);
CONNECTION_CLEAR_TRANSACTION(con);
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
if (connection_is_transaction_ongoing (con_cpu_cache_data)) { // RWITM finished
cache_set (cache, NULL,
con_cpu_cache_addr->transaction_value,
con_cpu_cache_data->transaction_value,
CACHE_LINE_STATE_MODIFIED, true);
con_cache_bus_addr->transaction_pending = false;
connection_end_transaction (con_cpu_cache_addr);
connection_end_transaction (con_cpu_cache_data);
} else {
enum cache_line_state next_state = data_bus->reserved_connection == CONNECTION_MEMORY && !cache->next_forward
? CACHE_LINE_STATE_EXCLUSIVE
: CACHE_LINE_STATE_FORWARD;
cache->next_forward = false;
cache_set (cache, NULL,
con_cache_bus_addr->transaction_value,
con_cache_bus_data->transaction_value,
next_state, true);
CONNECTION_START_TRANSACTION_TO_FROM(con_cpu_cache_data,
con->transaction_value);
}
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
if (!connection_is_transaction_complete (con_cpu_cache_data)) {
cache->highlighted = cache->highlighted_pending;
cache->highlighted_pending = CACHE_LINE_COUNT;
}
FREE_BUS(data_bus);
CONNECTION_CLEAR_TRANSACTION(con);
} break;
default: break;
}
}
con = con_cpu_cache_addr;
if (con->transaction_just_finished) {
switch (con->transaction_just_finished_state) {
case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
is_addr = true;
} break;
case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
cache->highlighted = cache->highlighted_pending;
cache->highlighted_pending = CACHE_LINE_COUNT;
if (CONNECTION_TRANSACTION_STATE_DIRECTION(con_cache_bus_addr) ==
CONNECTION_TRANSACTION_DIRECTION_FROM_TO) {
connection_end_transaction (con_cache_bus_addr);
}
connection_end_transaction (con_cpu_cache_data);
CONNECTION_CLEAR_TRANSACTION(con);
} break;
default: break;
}
}
con = con_cpu_cache_data;
if (con->transaction_just_finished) {
switch (con->transaction_just_finished_state) {
case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
is_data = true;
} break;
case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
connection_end_transaction (con_cache_bus_addr);
connection_end_transaction (con_cache_bus_shared);
cpu->highlighted = 3*MEMORY_SIZE;
*cache_locking_busses = CPU_COUNT;
CONNECTION_CLEAR_TRANSACTION(con);
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
connection_end_transaction (con_cpu_cache_addr);
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
cache->highlighted = cache->highlighted_pending;
cache->highlighted_pending = CACHE_LINE_COUNT;
CONNECTION_CLEAR_TRANSACTION(con);
if (cpu->highlighted % 3 == 1) {
CONNECTION_START_TRANSACTION_FROM_TO(con_cpu_cache_addr, con_cpu_cache_addr->transaction_value);
CONNECTION_START_TRANSACTION_FROM_TO(con_cpu_cache_data, con_cpu_cache_data->transaction_value + 1);
} else {
cpu->highlighted = 3*MEMORY_SIZE;
}
*cache_locking_busses = CPU_COUNT;
} break;
default: break;
}
}
// Process READ/WRITE request if any
if (is_addr || cache->continue_addr) {
cache->continue_addr = false;
u8 addr = con_cpu_cache_addr->transaction_value;
CACHE_LINE line = cache_get (cache, addr, true);
CACHE_LINE to_replace = cache_to_replace (cache, addr);
if (!line && !cache->copy_back && to_replace && to_replace->state == CACHE_LINE_STATE_MODIFIED) {
cache->invalidate_others = true;
cache->addr_targets = ~(1 << i) & CONNECTION_CACHES | 1 << CONNECTION_MEMORY;
cache->addr_value = to_replace->addr;
con_cache_bus_addr->transaction_pending = true;
cache->data_targets = 1 << CONNECTION_MEMORY;
cache->data_value = to_replace->data;
con_cache_bus_data->transaction_pending = true;
cache->copy_back = true;
} else {
cache->copy_back = false;
enum cache_line_state state = line ? line->state : CACHE_LINE_STATE_INVALID;
if (!is_data || cache->continue_data) { // Process CPU READ
cache->continue_data = false;
switch (state) {
case CACHE_LINE_STATE_MODIFIED:
case CACHE_LINE_STATE_EXCLUSIVE:
case CACHE_LINE_STATE_SHARED:
case CACHE_LINE_STATE_FORWARD: {
cache->data_targets = 1 << *cache_locking_busses;
CONNECTION_START_TRANSACTION_TO_FROM(con_cpu_cache_data, line->data);
} break;
case CACHE_LINE_STATE_INVALID: {
cache->addr_targets = ~(1 << i) & CONNECTION_CACHES;
cache->addr_value = con_cpu_cache_addr->transaction_value;
con_cache_bus_addr->transaction_pending = true;
} break;
default: break;
}
} else { // Process CPU WRITE
u8 data = con_cpu_cache_data->transaction_value;
switch (state) {
case CACHE_LINE_STATE_EXCLUSIVE: {
cache_set (cache, line, addr, data, CACHE_LINE_STATE_MODIFIED, false);
connection_end_transaction (con_cpu_cache_addr);
connection_end_transaction (con_cpu_cache_data);
} break;
case CACHE_LINE_STATE_MODIFIED: {
cache_set (cache, line, addr, data, CACHE_LINE_STATE_MODIFIED, false);
connection_end_transaction (con_cpu_cache_addr);
connection_end_transaction (con_cpu_cache_data);
} break;
case CACHE_LINE_STATE_FORWARD:
case CACHE_LINE_STATE_SHARED: {
cache_set (cache, line, addr, data, CACHE_LINE_STATE_MODIFIED, false);
cache->invalidate_others = true;
cache->addr_targets = ~(1 << i) & CONNECTION_CACHES;
cache->addr_value = con_cpu_cache_addr->transaction_value;
con_cache_bus_addr->transaction_pending = true;
} break;
case CACHE_LINE_STATE_INVALID: {
cache->addr_targets = ~(1 << i) & CONNECTION_CACHES;
cache->addr_value = con_cpu_cache_addr->transaction_value;
con_cache_bus_addr->transaction_pending = true;
} break;
default:
break;
}
}
}
}
CACHE_LINE line = NULL;
for (int i = 0; i < CACHE_LINE_COUNT; ++i) {
if (cache->lines[i].state != CACHE_LINE_STATE_INVALID &&
cache->lines[i].addr == cache->addr_value) {
line = &cache->lines[i];
}
}
if (con_cache_bus_addr->transaction_pending &&
con_cache_bus_data->transaction_pending &&
IS_BUS_FREE(addr_bus) &&
IS_BUS_FREE(data_bus) &&
!line) {
// cache_set (cache, line, cache->addr_value, cache->data_value, CACHE_LINE_STATE_MODIFIED);
*cache_locking_busses = i;
*bus_cycles += 1;
addr_bus->connection_targets = cache->addr_targets;
addr_bus->reserved_connection = i;
CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_addr,
cache->addr_value);
con_cache_bus_addr->transaction_pending = false;
if (cache->invalidate_others) {
shared_bus->reserved_connection = i;
shared_bus->connection_targets = ~(1 << i) & CONNECTION_CACHES;
CONNECTION_START_TRANSACTION_FROM_TO (con_cache_bus_shared,
CACHE_LINE_STATE_INVALID);
con_cache_bus_shared->transaction_pending = false;
}
} else if (con_cache_bus_addr->transaction_pending &&
IS_BUS_FREE(addr_bus) &&
IS_BUS_FREE(data_bus) &&
!connection_is_transaction_ongoing (con_cache_bus_addr) &&
!connection_is_transaction_ongoing (con_cache_bus_data) &&
(*cache_locking_busses == CPU_COUNT ||
*cache_locking_busses == i)) {
*cache_locking_busses = i;
*bus_cycles += 1;
addr_bus->connection_targets = cache->addr_targets;
addr_bus->reserved_connection = i;
CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_addr,
cache->addr_value);
con_cache_bus_addr->transaction_pending = false;
if (cache->invalidate_others) {
shared_bus->reserved_connection = i;
shared_bus->connection_targets = ~(1 << i) & CONNECTION_CACHES;
CONNECTION_START_TRANSACTION_FROM_TO (con_cache_bus_shared,
CACHE_LINE_STATE_INVALID);
cache->invalidate_others = false;
/* connection_end_transaction (con_cpu_cache_addr); */
/* connection_end_transaction (con_cpu_cache_data); */
}
if (con_cache_bus_data->transaction_pending &&
IS_BUS_FREE(data_bus)) {
data_bus->connection_targets = cache->data_targets;
data_bus->reserved_connection = i;
CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data,
cache->data_value);
con_cache_bus_data->transaction_pending = false;
shared_bus->reserved_connection = i;
shared_bus->connection_targets = ~(1 << i) & CONNECTION_CACHES;
CONNECTION_START_TRANSACTION_FROM_TO (con_cache_bus_shared,
CACHE_LINE_STATE_INVALID);
con_cache_bus_shared->transaction_pending = false;
if (cache->invalidate_others && line)
cache_set (cache, line, cache->addr_value, cache->data_value, CACHE_LINE_STATE_EXCLUSIVE, false);
cache->invalidate_others = false;
}
} else if (con_cache_bus_data->transaction_pending &&
IS_BUS_FREE(data_bus) &&
(*cache_locking_busses == CPU_COUNT ||
*cache_locking_busses == i)) {
*cache_locking_busses = i;
*bus_cycles += 1;
data_bus->connection_targets = cache->data_targets;
data_bus->reserved_connection = i;
CONNECTION_START_TRANSACTION_FROM_TO(con_cache_bus_data,
cache->data_value);
con_cache_bus_data->transaction_pending = false;
}
}
for (int i = 0; i < CPU_COUNT; ++i) {
if (cons_cache_bus_shared[i]->transaction_pending &&
addr_bus->reserved_connection == i) {
bus_link_connections (shared_bus, cons_cache_bus_shared); // BUG(cmmm): started twice on double READ
cons_cache_bus_shared[i]->transaction_pending = false;
}
}
con = con_memory_bus_addr;
if (con->transaction_just_finished) {
switch (con->transaction_just_finished_state) {
case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
int addr = con->transaction_value;
memory->highlighted = addr;
if (!connection_is_transaction_ongoing (con_memory_bus_data)) {
data_bus->reserved_connection = CONNECTION_MEMORY;
CONNECTION_START_TRANSACTION_FROM_TO(con_memory_bus_data, memory->entries[addr]);
} else if (connection_is_transaction_direction_from_to (cons_cache_bus_data[addr_bus->reserved_connection])) {
if (caches[addr_bus->reserved_connection]->copy_back) {
caches[addr_bus->reserved_connection]->continue_addr = connection_is_transaction_ongoing (cons_cpu_cache_addr[addr_bus->reserved_connection]);
caches[addr_bus->reserved_connection]->continue_data = connection_is_transaction_ongoing (cons_cpu_cache_data[addr_bus->reserved_connection]);
}
}
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
FREE_BUS(addr_bus);
memory->highlighted = MEMORY_SIZE;
connection_end_transaction (con_memory_bus_data);
CONNECTION_CLEAR_TRANSACTION(con);
} break;
default: break;
}
}
con = con_memory_bus_data;
if (con->transaction_just_finished) {
switch (con->transaction_just_finished_state) {
case CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD: {
data_bus->connection_targets = 1 << addr_bus->reserved_connection;
bus_link_connection (data_bus, con_memory_bus_data);
} break;
case CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING: {
bus_unlink_connection (data_bus);
CONNECTION_CLEAR_TRANSACTION(con);
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD: {
memory->entries[con_memory_bus_addr->transaction_value] = con->transaction_value;
if (connection_is_transaction_ongoing (cons_cpu_cache_data[data_bus->reserved_connection]) &&
connection_is_transaction_direction_to_from (cons_cpu_cache_data[data_bus->reserved_connection])) {
connection_end_transaction (cons_cpu_cache_addr[addr_bus->reserved_connection]);
connection_end_transaction (cons_cpu_cache_data[data_bus->reserved_connection]);
} else {
connection_end_transaction (cons_cache_bus_shared[data_bus->reserved_connection]);
connection_end_transaction (cons_cache_bus_addr[data_bus->reserved_connection]);
connection_end_transaction (cons_cache_bus_data[data_bus->reserved_connection]);
}
} break;
case CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING: {
FREE_BUS(data_bus);
CONNECTION_CLEAR_TRANSACTION(con);
} break;
default: break;
}
}
// Save state
if (!is_paused && transaction_proggressed) {
if (seconds_since_last_tick >= TICK_SECONDS) {
seconds_since_last_tick = 0;
save_state ();
} else
seconds_since_last_tick += GetFrameTime ();
}
}
static inline void
draw (void)
{
BeginDrawing ();
ClearBackground (COLOR_BACKGROUND);
BeginMode2D (camera);
draw_bus (data_bus);
draw_bus (addr_bus);
draw_bus (shared_bus);
for (int i = 0; i < CPU_COUNT; ++i) {
draw_cache (caches[i]);
draw_cpu (cpus[i]);
}
draw_memory (memory);
EndMode2D();
if (is_paused) {
DrawRectangle ( 5, 5, 10, 40, GRAY);
DrawRectangle (25, 5, 10, 40, GRAY);
// draw tick
char *tick_text;
asprintf (&tick_text, "%lu", current_tick);
DrawTextEx (font, tick_text, (Vector2) { 5, 43 }, FONT_SIZE, TEXT_SPACING, GRAY);
free (tick_text);
}
char *bus_cycles_text;
asprintf (&bus_cycles_text, "BUS CYCLES: %d", *bus_cycles);
float bus_cycles_text_width = MeasureTextEx (font, bus_cycles_text, FONT_SIZE, TEXT_SPACING).x;
Color bus_cycles_text_color = IS_BUS_FREE(data_bus) && IS_BUS_FREE(addr_bus) ? COLOR_TEXT : COLOR_DATA;
DrawTextEx (font, bus_cycles_text, (Vector2) { GetScreenWidth ()/2 - bus_cycles_text_width/2, 0 }, FONT_SIZE, TEXT_SPACING, bus_cycles_text_color);
free (bus_cycles_text);
const char *info_text = "Press R to reset...";
float info_text_width = MeasureTextEx (font, info_text, FONT_SIZE, TEXT_SPACING).x;
DrawTextEx (font, info_text, (Vector2) { GetScreenWidth () - info_text_width, 0 }, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
// DrawFPS (2, 0);
EndDrawing ();
}
int
main (void)
{
startup ();
while (!WindowShouldClose ()) {
handle_input ();
update ();
draw ();
} shutdown ();
return EXIT_SUCCESS;
}
|