From 1cf06615f97bc27f732a880e89e9366140095bf1 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 03:37:41 +0300 Subject: publish --- src/mesif-lru-simulation.c | 705 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 705 insertions(+) create mode 100644 src/mesif-lru-simulation.c (limited to 'src/mesif-lru-simulation.c') 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 @@ +#include +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#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; +} -- cgit v1.3.1