From 1cf06615f97bc27f732a880e89e9366140095bf1 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 03:37:41 +0300 Subject: publish --- src/busses.c | 121 ++++++++ src/busses.h | 36 +++ src/caches.c | 312 ++++++++++++++++++++ src/caches.h | 60 ++++ src/config.h | 74 +++++ src/connections.c | 183 ++++++++++++ src/connections.h | 82 ++++++ src/cpus.c | 179 ++++++++++++ src/cpus.h | 24 ++ src/export-font-as-code.c | 17 ++ src/memory.c | 99 +++++++ src/memory.h | 22 ++ src/mesif-lru-simulation.c | 705 +++++++++++++++++++++++++++++++++++++++++++++ src/states.c | 75 +++++ src/states.h | 39 +++ src/utils.h | 10 + 16 files changed, 2038 insertions(+) create mode 100644 src/busses.c create mode 100644 src/busses.h create mode 100644 src/caches.c create mode 100644 src/caches.h create mode 100644 src/config.h create mode 100644 src/connections.c create mode 100644 src/connections.h create mode 100644 src/cpus.c create mode 100644 src/cpus.h create mode 100644 src/export-font-as-code.c create mode 100644 src/memory.c create mode 100644 src/memory.h create mode 100644 src/mesif-lru-simulation.c create mode 100644 src/states.c create mode 100644 src/states.h create mode 100644 src/utils.h (limited to 'src') diff --git a/src/busses.c b/src/busses.c new file mode 100644 index 0000000..ff6a86d --- /dev/null +++ b/src/busses.c @@ -0,0 +1,121 @@ +#include "busses.h" +#include +#include +#include "config.h" +#include "connections.h" + +extern Font font; + +int *cache_locking_busses; +int *bus_cycles; +BUS data_bus; +BUS addr_bus; +BUS shared_bus; + + +void +init_bus (BUS bus, CONNECTION cache_cons[CPU_COUNT], CONNECTION memory_con, float y, float scale, Color color, char *label) +{ + *bus = (struct bus) { + .reserved_connection = CONNECTION_NONE, + .linked = false, + .memory_connection = memory_con, + .y = y, + .scale = scale, + .color = color, + .label = label, + .label_size = MeasureTextEx (font, label, FONT_SIZE, TEXT_SPACING), + }; + memcpy (bus->cache_connections, cache_cons, sizeof (bus->cache_connections)); +} + +void +draw_bus (BUS bus) +{ + const Vector2 pos_end = { + .x = (CPU_COUNT + 1)/2.*(BLOCK_WIDTH + BLOCK_MARGINH) - BLOCK_MARGINH + addr_bus->label_size.x, + .y = bus->y, + }; + + const Vector2 pos_start = { + .x = -1*pos_end.x, + .y = pos_end.y, + }; + + const Color color = (bus->linked + ? bus->color + : COLOR_BUS_INACTIVE); + + const float thick = BUS_THICKNESS*bus->scale; + const float head_side = 2*thick; + const float head_height = head_side*sqrtf (3)/2; + const Vector2 dir = Vector2Normalize (Vector2Subtract (pos_end, pos_start)); + const Vector2 line_offset = Vector2Scale (dir, head_height/bus->scale); + const Vector2 line_begin_pos = Vector2Add (pos_start, line_offset); + const Vector2 line_end_pos = Vector2Subtract (pos_end, line_offset); + DrawLineEx (line_begin_pos, line_end_pos, thick, color); + DrawTriangle(Vector2Add (line_begin_pos, Vector2Scale (dir, -head_height)), + Vector2Add (line_begin_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2), -PI/2)), + Vector2Add (line_begin_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2), PI/2)), + color); + DrawTriangle(Vector2Add (line_end_pos, Vector2Scale (dir, head_height)), + Vector2Add (line_end_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2), PI/2)), + Vector2Add (line_end_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2), -PI/2)), + color); + + Vector2 label_pos = line_begin_pos; + label_pos.x += TEXT_MARGIN; + label_pos.y -= bus->label_size.y + TEXT_MARGIN; + DrawTextEx (font, bus->label, label_pos, FONT_SIZE, TEXT_SPACING, bus->color); +} + +void +bus_link_connection (BUS bus, CONNECTION con) +{ + bus->linked = true; + + for (int i = 0; i < CPU_COUNT; ++i) { + if (!(bus->connection_targets & 1 << i)) + continue; + + CONNECTION_START_TRANSACTION_TO_FROM(bus->cache_connections[i], con->transaction_value); + } + + if (bus->connection_targets & 1 << CPU_COUNT) + CONNECTION_START_TRANSACTION_TO_FROM(bus->memory_connection, con->transaction_value); +} + + +void +bus_link_connections (BUS bus, CONNECTION cons[CPU_COUNT]) +{ + bus->linked = true; + + int target; + u8 data = 0; + for (int i = 0; i < CPU_COUNT; ++i) { + if (bus->connection_targets & 1 << i) + target = i; + else + data |= cons[i]->transaction_value; + } + + CONNECTION_START_TRANSACTION_TO_FROM(bus->cache_connections[target], data); +} + + +void +bus_unlink_connection (BUS bus) +{ + bus->linked = false; + + for (int i = 0; i < CPU_COUNT; ++i) { + CONNECTION con = bus->cache_connections[i]; + connection_end_transaction (con); + } + + CONNECTION con = bus->memory_connection; + if (con) { + connection_end_transaction (con); + } +} diff --git a/src/busses.h b/src/busses.h new file mode 100644 index 0000000..9c08f25 --- /dev/null +++ b/src/busses.h @@ -0,0 +1,36 @@ +#ifndef INCLUDED_BUSSES_H +#define INCLUDED_BUSSES_H + +#include +#include +#include "connections.h" + +#define IS_BUS_FREE(bus) ((bus)->reserved_connection == CONNECTION_NONE) +#define FREE_BUS(bus) ((bus)->reserved_connection = CONNECTION_NONE) + +typedef struct bus { + int reserved_connection; + int connection_targets; + bool linked; + CONNECTION cache_connections[CPU_COUNT]; + CONNECTION memory_connection; + float y; + float scale; + Color color; + char *label; + Vector2 label_size; +} *BUS; + +extern int *cache_locking_busses; +extern int *bus_cycles; +extern BUS data_bus; +extern BUS addr_bus; +extern BUS shared_bus; + +void init_bus (BUS, CONNECTION caches[CPU_COUNT], CONNECTION memory, float y, float scale, Color, char *label); +void draw_bus (BUS); +void bus_link_connection (BUS, CONNECTION); +void bus_unlink_connection (BUS); +void bus_link_connections (BUS, CONNECTION cons[CPU_COUNT]); + +#endif diff --git a/src/caches.c b/src/caches.c new file mode 100644 index 0000000..4d710e6 --- /dev/null +++ b/src/caches.c @@ -0,0 +1,312 @@ +#include "caches.h" +#include +#include +#include +#include "config.h" +#include "connections.h" + +extern Font font; + +CACHE caches[CPU_COUNT]; + +static char *cache_line_state_labels[] = { + [CACHE_LINE_STATE_MODIFIED ] = "M", + [CACHE_LINE_STATE_EXCLUSIVE] = "E", + [CACHE_LINE_STATE_SHARED ] = "S", + [CACHE_LINE_STATE_INVALID ] = "I", + [CACHE_LINE_STATE_FORWARD ] = "F", +}; + +void +init_cache (CACHE cache, BUS lower_bus) +{ + int i = cache - caches[0]; + int block_i = i < CACHE_COUNT/2 ? i : i + 1; + cache->rec = (Rectangle) { + .x = Remap (block_i, 0, CACHE_COUNT, + - CACHE_COUNT/2*(BLOCK_WIDTH + BLOCK_MARGINH) - BLOCK_WIDTH/2, + + CACHE_COUNT/2*(BLOCK_WIDTH + BLOCK_MARGINH) - BLOCK_WIDTH/2), + .y = lower_bus->y + lower_bus->scale*BUS_THICKNESS/2 + BLOCK_MARGINV, + .width = BLOCK_WIDTH, + .height = 2*BLOCK_PADDING + CACHE_LINE_COUNT*(ENTRY_HEIGHT + ENTRY_MARGIN) - ENTRY_MARGIN, + }; + + strcpy (cache->label, "CACHE 0"); + cache->label[strlen (cache->label) - 1] += i; + cache->label_origin = Vector2Scale (MeasureTextEx (font, cache->label, FONT_SIZE, TEXT_SPACING), 0.5); + cache->label_position = (Vector2) { + .x = cache->rec.x - cache->label_origin.y/2 - TEXT_MARGIN, + .y = cache->rec.y + cache->rec.height/2, + }; + + float ys = cache->rec.y - LINE_THICKNESS; + float xd = cache->rec.width*1/4; + float xs = cache->rec.x + xd; + init_connection (cons_cache_bus_shared [i], xs + 0*xd, ys, shared_bus->y + shared_bus->scale*BUS_THICKNESS/2, COLOR_SHARED, shared_bus->scale); + init_connection (cons_cache_bus_addr [i], xs + 1*xd, ys, addr_bus->y + addr_bus->scale*BUS_THICKNESS/2, COLOR_ADDR, addr_bus->scale); + init_connection (cons_cache_bus_data [i], xs + 2*xd, ys, data_bus->y + data_bus->scale*BUS_THICKNESS/2, COLOR_DATA, data_bus->scale); + + cache->highlighted = CACHE_LINE_COUNT; + cache->highlighted_pending = CACHE_LINE_COUNT; + + for (int i = 0; i < CACHE_LINE_COUNT; ++i) { + cache->lines[i] = (struct cache_line) { + .age = 0, + .state = CACHE_LINE_STATE_INVALID, + .addr = 0, + .data = 0, + }; + } +} + +void +draw_cache (CACHE cache) +{ + int i = cache - caches[0]; + + DrawRectangleRounded (cache->rec, ROUNDNESS, SEGMENTS, COLOR_BLOCK); + DrawTextPro (font, cache->label, cache->label_position, cache->label_origin, -90, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + for (int j = 0; j < CACHE_ASSOCIATIVITY; ++j) { + char set[2]; + sprintf (set, "%d", j); + const Vector2 set_center = Vector2Scale (MeasureTextEx (font, set, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 set_entry_center = { + .x = cache->rec.x + cache->rec.height/CACHE_LINE_COUNT/2, + .y = cache->rec.y + 2*(j + .5)*cache->rec.height/CACHE_LINE_COUNT, + }; + const Vector2 set_pos = Vector2Subtract (set_entry_center, set_center); + DrawTextEx (font, set, set_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + if (j != 0) { + const Vector2 divider_start_pos = { + .x = cache->rec.x, + .y = cache->rec.y + 2*j*cache->rec.height/CACHE_LINE_COUNT, + }; + const Vector2 divider_end_pos = { + .x = divider_start_pos.x + cache->rec.height/CACHE_LINE_COUNT, + .y = divider_start_pos.y, + }; + DrawLineEx(divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE); + } + } + + for (int j = 0; j < CACHE_LINE_COUNT; ++j) { + Rectangle entry = { + .x = cache->rec.x + cache->rec.height/CACHE_LINE_COUNT, + .y = cache->rec.y + j*cache->rec.height/CACHE_LINE_COUNT, + .width = BLOCK_WIDTH - cache->rec.height/CACHE_LINE_COUNT, + .height = cache->rec.height/CACHE_LINE_COUNT, + }; + if (cache->highlighted == j) + DrawRectangleRec (entry, GREEN); + else if (cache->highlighted_pending == j) + DrawRectangleRec (entry, DARKGREEN); + + if (j != 0) { + const Vector2 divider_start_pos = { + .x = cache->rec.x + cache->rec.height/CACHE_LINE_COUNT, + .y = cache->rec.y + j*cache->rec.height/CACHE_LINE_COUNT, + }; + const Vector2 divider_end_pos = { + .x = divider_start_pos.x + cache->rec.width - cache->rec.height/CACHE_LINE_COUNT, + .y = divider_start_pos.y, + }; + DrawLineEx(divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE); + } + + char age[2]; + age[0] = cache->lines[j].age < 10 ? '0' + cache->lines[j].age : '+'; + age[1] = '\0'; + const Vector2 age_center = Vector2Scale (MeasureTextEx (font, age, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 age_entry_center = { + .x = cache->rec.x + 3*cache->rec.height/CACHE_LINE_COUNT/2, + .y = cache->rec.y + (j + .5)*cache->rec.height/CACHE_LINE_COUNT, + }; + const Vector2 age_pos = Vector2Subtract (age_entry_center, age_center); + DrawTextEx (font, age, age_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + char *state = cache_line_state_labels[cache->lines[j].state]; + const Vector2 state_center = Vector2Scale (MeasureTextEx (font, state, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 state_entry_center = { + .x = cache->rec.x + 5*cache->rec.height/CACHE_LINE_COUNT/2, + .y = cache->rec.y + (j + .5)*cache->rec.height/CACHE_LINE_COUNT, + }; + const Vector2 state_pos = Vector2Subtract (state_entry_center, state_center); + DrawTextEx (font, state, state_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + if (cache->lines[j].state != CACHE_LINE_STATE_INVALID || true) { + char tag[4]; + sprintf (tag, "%d%d%d", + cache->lines[j].addr >> 3 & 1, + cache->lines[j].addr >> 2 & 1, + cache->lines[j].addr >> 1 & 1); + const Vector2 tag_center = Vector2Scale (MeasureTextEx (font, tag, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 tag_entry_center = { + .x = cache->rec.x + 3*cache->rec.height/CACHE_LINE_COUNT + + (cache->rec.width - 3*cache->rec.height/CACHE_LINE_COUNT)*3/10, + .y = state_entry_center.y, + }; + const Vector2 tag_pos = Vector2Subtract (tag_entry_center, tag_center); + DrawTextEx (font, tag, tag_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + char value[3]; + sprintf (value, "%02hhX", cache->lines[j].data); + const Vector2 value_center = Vector2Scale (MeasureTextEx (font, value, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 value_entry_center = { + .x = cache->rec.x + 3*cache->rec.height/CACHE_LINE_COUNT + + (cache->rec.width - 3*cache->rec.height/CACHE_LINE_COUNT)*4/5, + .y = state_entry_center.y, + }; + const Vector2 value_pos = Vector2Subtract (value_entry_center, value_center); + DrawTextEx (font, value, value_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + } + } + + DrawRectangleRoundedLinesEx (cache->rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, COLOR_BLOCK_EDGE); + + const Vector2 set_divider_start_pos = { + .x = cache->rec.x + cache->rec.height/CACHE_LINE_COUNT, + .y = cache->rec.y, + }; + const Vector2 set_divider_end_pos = { + .x = set_divider_start_pos.x, + .y = set_divider_start_pos.y + cache->rec.height, + }; + DrawLineEx (set_divider_start_pos, set_divider_end_pos, 2, COLOR_BLOCK_EDGE); + + const Vector2 age_divider_start_pos = { + .x = set_divider_start_pos.x + cache->rec.height/CACHE_LINE_COUNT, + .y = cache->rec.y, + }; + const Vector2 age_divider_end_pos = { + .x = age_divider_start_pos.x, + .y = age_divider_start_pos.y + cache->rec.height, + }; + DrawLineEx (age_divider_start_pos, age_divider_end_pos, 2, COLOR_BLOCK_EDGE); + + const Vector2 state_divider_start_pos = { + .x = set_divider_start_pos.x + 2*cache->rec.height/CACHE_LINE_COUNT, + .y = cache->rec.y, + }; + const Vector2 state_divider_end_pos = { + .x = state_divider_start_pos.x, + .y = state_divider_start_pos.y + cache->rec.height, + }; + DrawLineEx (state_divider_start_pos, state_divider_end_pos, 2, COLOR_BLOCK_EDGE); + + const Vector2 data_divider_start_pos = { + .x = state_divider_start_pos.x + (cache->rec.width - 3*cache->rec.height/CACHE_LINE_COUNT)*3/5, + .y = cache->rec.y, + }; + const Vector2 data_divider_end_pos = { + .x = data_divider_start_pos.x, + .y = data_divider_start_pos.y + cache->rec.height, + }; + DrawLineEx (data_divider_start_pos, data_divider_end_pos, 2, COLOR_BLOCK_EDGE); + + draw_connection (cons_cache_bus_shared [i]); + draw_connection (cons_cache_bus_addr [i]); + draw_connection (cons_cache_bus_data [i]); +} + +CACHE_LINE +cache_get (CACHE cache, u8 addr, bool age) +{ + u8 offset = (addr % CACHE_ASSOCIATIVITY)*CACHE_LINES_PER_SET; + CACHE_LINE lines_begin = &cache->lines[offset]; + CACHE_LINE lines_end = lines_begin + CACHE_LINES_PER_SET; + CACHE_LINE line = NULL; + + for (CACHE_LINE iter_line = lines_begin; iter_line < lines_end; ++iter_line) { + if (iter_line->addr == addr && + iter_line->state != CACHE_LINE_STATE_INVALID) { + line = iter_line; + line->age = 0; + cache->highlighted_pending = cache->highlighted; + cache->highlighted = line - cache->lines; + // printf ("%d %d\n", cache->highlighted, cache->highlighted_pending); + } else if (iter_line->state != CACHE_LINE_STATE_INVALID) + iter_line->age += age; + } + + return line; +} + +void +cache_set (CACHE cache, CACHE_LINE line, u8 addr, u8 data, enum cache_line_state state, bool age) +{ + if (line) + goto set; + + u8 offset = (addr % CACHE_ASSOCIATIVITY)*CACHE_LINES_PER_SET; + CACHE_LINE lines_begin = &cache->lines[offset]; + CACHE_LINE lines_end = lines_begin + CACHE_LINES_PER_SET; + + // Age cache lines + for (line = lines_begin; line < lines_end; ++line) { + if (line->state != CACHE_LINE_STATE_INVALID) + line->age += age; + } + + // Find cache line with matching address + for (line = lines_begin; line < lines_end; ++line) { + if (line->addr == addr && + line->state != CACHE_LINE_STATE_INVALID) + goto select; + } + + // Find new spot for cache line + line = lines_begin; + if (line->state == CACHE_LINE_STATE_INVALID) + goto select; + for (CACHE_LINE iter_line = lines_begin + 1; iter_line < lines_end; ++iter_line) { + if (iter_line->state == CACHE_LINE_STATE_INVALID) { + line = iter_line; + break; + } + if (iter_line->age > line->age) + line = iter_line; + } + + select: + // printf ("%d %d\n", cache->highlighted, cache->highlighted_pending); + cache->highlighted_pending = cache->highlighted; + cache->highlighted = line - cache->lines; + line->age = 0; + set: + line->state = state; + line->addr = addr; + line->data = data; +} + +CACHE_LINE +cache_to_replace (CACHE cache, u8 addr) +{ + u8 offset = (addr % CACHE_ASSOCIATIVITY)*CACHE_LINES_PER_SET; + CACHE_LINE lines_begin = &cache->lines[offset]; + CACHE_LINE lines_end = lines_begin + CACHE_LINES_PER_SET; + CACHE_LINE line = NULL; + + // Find cache line with matching address + for (line = lines_begin; line < lines_end; ++line) { + if (line->addr == addr && + line->state != CACHE_LINE_STATE_INVALID) + return line; + } + + // Find new spot for cache line + line = lines_begin; + if (line->state == CACHE_LINE_STATE_INVALID) + return line; + for (CACHE_LINE iter_line = lines_begin + 1; iter_line < lines_end; ++iter_line) { + if (iter_line->state == CACHE_LINE_STATE_INVALID) { + line = iter_line; + break; + } + if (iter_line->age > line->age) + line = iter_line; + } + + return line; +} diff --git a/src/caches.h b/src/caches.h new file mode 100644 index 0000000..b6af6ae --- /dev/null +++ b/src/caches.h @@ -0,0 +1,60 @@ +#ifndef INCLUDED_CACHES_H +#define INCLUDED_CACHES_H + +#include +#include +#include "config.h" +#include "busses.h" + +enum PACKED cache_line_state { + CACHE_LINE_STATE_MODIFIED = 2, + CACHE_LINE_STATE_EXCLUSIVE = 3, + CACHE_LINE_STATE_SHARED = 1, + CACHE_LINE_STATE_INVALID = 0, + CACHE_LINE_STATE_FORWARD = 4, + CACHE_LINE_STATE_COUNT, +}; + +typedef struct cache_line { + u8 age; + enum cache_line_state state; + u8 addr; + u8 data; +} *CACHE_LINE; + +enum PACKED cache_state { + CACHE_STATE_WAIT, + CACHE_STATE_START_MEMORY_READ, + CACHE_STATE_START_MEMORY_WRITE, + CACHE_STATE_DATA_BUS_WAIT, +}; + +typedef struct cache { + struct cache_line lines[CACHE_LINE_COUNT]; + enum cache_state state; + u8 addr_targets; + u8 data_targets; + u8 addr_value; + u8 data_value; + bool invalidate_others; + bool next_forward; + bool copy_back; + bool continue_addr; + bool continue_data; + Rectangle rec; + char label[8]; + Vector2 label_origin; + Vector2 label_position; + int highlighted; + int highlighted_pending; +} *CACHE; + +extern CACHE caches[CPU_COUNT]; + +void init_cache (CACHE, BUS lower); +void draw_cache (CACHE); +CACHE_LINE cache_get (CACHE, u8 addr, bool age); +void cache_set (CACHE, CACHE_LINE, u8 addr, u8 data, enum cache_line_state, bool age); +CACHE_LINE cache_to_replace (CACHE cache, u8 addr); + +#endif diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..5bc9557 --- /dev/null +++ b/src/config.h @@ -0,0 +1,74 @@ +#ifndef INCLUDED_CONFIG_H +#define INCLUDED_CONFIG_H + +#include + +#define INITIAL_SCREEN_WIDTH 500 +#define INITIAL_SCREEN_HEIGHT 500 +#define WINDOW_NAME "MERSI/MESIF LRU vizualization" + +#define CPU_COUNT 4 +#define CACHE_COUNT CPU_COUNT +#define MEMORY_SIZE 16 +#define CACHE_LINE_SIZE 1 +#define CACHE_LINE_COUNT 4 +#define CACHE_ASSOCIATIVITY 2 +#define CACHE_LINES_PER_SET (CACHE_LINE_COUNT/CACHE_ASSOCIATIVITY) + +#define SCREEN_PADDING 25 + +#define BUS_THICKNESS 10 +#define BUS_MARGIN 40 +#define BUS_SPACING 40 +#define BUS_ARROW_SIDE (2*BUS_THICKNESS) +#define BUS_ARROW_HEIGHT (BUS_ARROW_SIDE*sqrt (3)/2) + +#define BLOCK_WIDTH 170 +#define BLOCK_MARGINH 50 +#define BLOCK_MARGINV 50 +#define BLOCK_PADDING 4 + +#define TEXT_MARGIN 4 +#define TEXT_SPACING 4 +#define FONT_SIZE 24 + +#define ENTRY_HEIGHT 20 +#define ENTRY_MARGIN 6 + +#define ROUNDNESS 0.05 +#define SEGMENTS 16 +#define LINE_THICKNESS 2 + +#define TRANSACTIONS_PER_SECOND 2 +#define TICKS_PER_TRANSACTION 20 +#define TICKS_PER_SECOND (TICKS_PER_TRANSACTION*TRANSACTIONS_PER_SECOND) +#define TICK_SECONDS (1./TICKS_PER_SECOND) +#define MAX_TIME (60*60) +#define MAX_TICKS (MAX_TIME*TICKS_PER_SECOND) + +#define DARKDARKGRAY (Color) { 20, 20, 20, 255 } +#define COLOR_DATA RED +#define COLOR_ADDR BLUE +#define COLOR_SHARED MAGENTA +#define THEME_LIGHT 0 +#define THEME_DARK 1 +#define THEME THEME_LIGHT +#if THEME == THEME_DARK +#define COLOR_BACKGROUND BLACK +#define COLOR_BUS_INACTIVE LIGHTGRAY +#define COLOR_BLOCK BLACK +#define COLOR_BLOCK_EDGE WHITE +#define COLOR_BUTTON DARKDARKGRAY +#define COLOR_BUTTON_HOVER COLOR_BACKGROUND +#define COLOR_TEXT WHITE +#else +#define COLOR_BACKGROUND WHITE +#define COLOR_BUS_INACTIVE DARKGRAY +#define COLOR_BLOCK WHITE +#define COLOR_BLOCK_EDGE BLACK +#define COLOR_BUTTON LIGHTGRAY +#define COLOR_BUTTON_HOVER COLOR_BACKGROUND +#define COLOR_TEXT BLACK +#endif + +#endif diff --git a/src/connections.c b/src/connections.c new file mode 100644 index 0000000..438eb29 --- /dev/null +++ b/src/connections.c @@ -0,0 +1,183 @@ +#include "connections.h" +#include "config.h" +#include +#include + +bool transaction_proggressed; + +#define TRANSACTION_STATE_DIRECTION(con) ((con)->transaction_state & CONNECTION_TRANSACTION_STATE_DIRECTION_MASK) +#define TRANSACTION_STATE_TYPE(con) ((con)->transaction_state & CONNECTION_TRANSACTION_STATE_TYPE_MASK) + +static inline float +logistic (float x, float x0, float max, float rate) +{ + return max/(1 + exp (-rate*(x - x0))); +} + +CONNECTION cons_cpu_cache_addr [CPU_COUNT]; +CONNECTION cons_cpu_cache_data [CPU_COUNT]; +CONNECTION cons_cache_bus_shared [CPU_COUNT]; +CONNECTION cons_cache_bus_addr [CPU_COUNT]; +CONNECTION cons_cache_bus_data [CPU_COUNT]; +CONNECTION con_memory_bus_addr; +CONNECTION con_memory_bus_data; + +void +init_connection (CONNECTION con, float x, float ys, float ye, Color c, float scale) +{ + *con = (struct connection) { + .transaction_state = CONNECTION_TRANSACTION_STATE_NONE, + .x = x, + .ys = ys, + .ye = ye, + .color = c, + .scale = scale, + }; +} + +void +draw_connection (CONNECTION con) +{ + const float x = con->x; + float ys; + float ye; + int dir; + + switch (TRANSACTION_STATE_DIRECTION(con)) { + case CONNECTION_TRANSACTION_DIRECTION_FROM_TO: + ys = con->ys; + ye = con->ye; + dir = +1; + break; + case CONNECTION_TRANSACTION_DIRECTION_TO_FROM: + ys = con->ye; + ye = con->ys; + dir = -1; + break; + } + + const Vector2 pos_start = { x, ys }; + const float thick = BUS_THICKNESS*con->scale; + const float head_side = 2*thick; + const float head_height = head_side*sqrt (3)/2; + const Vector2 head_offset_peak = { 0, dir*head_height }; + const Vector2 head_offset_left = { +dir*head_side/2, 0 }; + const Vector2 head_offset_right = { -dir*head_side/2, 0 }; + const Vector2 line_begin_pos = { x, ys - dir*head_height }; + const Vector2 line_end_pos = { x, ye + dir*head_height }; + + float fraction = (con->transaction_progress < 1 + ? logistic (con->transaction_progress, 0.5, 1, 10) : 1); + Vector2 lerped_line_pos = Vector2Lerp (pos_start, line_end_pos, fraction); + + switch (TRANSACTION_STATE_TYPE(con)) { + case CONNECTION_TRANSACTION_TYPE_NONE: + DrawLineEx (line_begin_pos, line_end_pos, thick, COLOR_BUS_INACTIVE); + DrawTriangle(Vector2Add (line_begin_pos, head_offset_peak), + Vector2Add (line_begin_pos, head_offset_left), + Vector2Add (line_begin_pos, head_offset_right), + COLOR_BUS_INACTIVE); + DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak), + Vector2Subtract (line_end_pos, head_offset_left), + Vector2Subtract (line_end_pos, head_offset_right), + COLOR_BUS_INACTIVE); + break; + case CONNECTION_TRANSACTION_TYPE_FORWARD: + DrawLineEx (pos_start, line_end_pos, thick, COLOR_BUS_INACTIVE); + DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak), + Vector2Subtract (line_end_pos, head_offset_left), + Vector2Subtract (line_end_pos, head_offset_right), + COLOR_BUS_INACTIVE); + + DrawLineEx (pos_start, lerped_line_pos, thick, con->color); + DrawTriangle(Vector2Subtract (lerped_line_pos, head_offset_peak), + Vector2Subtract (lerped_line_pos, head_offset_left), + Vector2Subtract (lerped_line_pos, head_offset_right), + con->color); + break; + case CONNECTION_TRANSACTION_TYPE_TRAILING: + DrawTriangle(Vector2Add (line_begin_pos, head_offset_peak), + Vector2Add (line_begin_pos, head_offset_left), + Vector2Add (line_begin_pos, head_offset_right), + ColorAlpha (COLOR_BUS_INACTIVE, fraction)); + + DrawLineEx (Vector2Add (line_begin_pos, Vector2Scale (head_offset_peak, .5)), + line_end_pos, thick, COLOR_BUS_INACTIVE); + DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak), + Vector2Subtract (line_end_pos, head_offset_left), + Vector2Subtract (line_end_pos, head_offset_right), + COLOR_BUS_INACTIVE); + + DrawLineEx (lerped_line_pos, line_end_pos, thick, con->color); + DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak), + Vector2Subtract (line_end_pos, head_offset_left), + Vector2Subtract (line_end_pos, head_offset_right), + con->color); + break; + } +} + +void +connection_start_transaction (CONNECTION con, enum connection_transaction_direction dir, u8 value) +{ + con->transaction_value = value; + con->transaction_progress = 0; + con->transaction_state = dir | CONNECTION_TRANSACTION_TYPE_FORWARD; +} + +void +connection_end_transaction (CONNECTION con) +{ + if (TRANSACTION_STATE_TYPE(con) != CONNECTION_TRANSACTION_TYPE_FORWARD) + return; + + con->transaction_state = TRANSACTION_STATE_DIRECTION(con) | + CONNECTION_TRANSACTION_TYPE_TRAILING; + con->transaction_progress = 0; +} + +bool +connection_progress_transaction (CONNECTION con) +{ + if (!connection_is_transaction_ongoing (con) || + con->transaction_progress == 1) { + return con->transaction_just_finished = false; + } + + transaction_proggressed = true; + con->transaction_progress = fmin (con->transaction_progress + + GetFrameTime ()*TRANSACTIONS_PER_SECOND, 1); + if (con->transaction_progress == 1) { + con->transaction_just_finished_state = con->transaction_state; + return con->transaction_just_finished = true; + } + + return con->transaction_just_finished = false; +} + +bool +connection_is_transaction_ongoing (CONNECTION con) +{ + return (TRANSACTION_STATE_TYPE(con) != CONNECTION_TRANSACTION_TYPE_NONE); +} + +bool +connection_is_transaction_complete (CONNECTION con) +{ + return (con->transaction_progress == 1 && + connection_is_transaction_ongoing(con)); +} + +bool +connection_is_transaction_direction_from_to (CONNECTION con) +{ + return (con->transaction_state != CONNECTION_TRANSACTION_STATE_NONE && + TRANSACTION_STATE_DIRECTION(con) == CONNECTION_TRANSACTION_DIRECTION_FROM_TO); +} + +bool +connection_is_transaction_direction_to_from (CONNECTION con) +{ + return (con->transaction_state != CONNECTION_TRANSACTION_STATE_NONE && + TRANSACTION_STATE_DIRECTION(con) == CONNECTION_TRANSACTION_DIRECTION_TO_FROM); +} diff --git a/src/connections.h b/src/connections.h new file mode 100644 index 0000000..ed7245b --- /dev/null +++ b/src/connections.h @@ -0,0 +1,82 @@ +#ifndef INCLUDED_CONNECTIONS_H +#define INCLUDED_CONNECTIONS_H + +#include +#include +#include "config.h" +#include "utils.h" + +#define CONNECTION_NONE -1 +#define CONNECTION_MEMORY CPU_COUNT +#define CONNECTION_CACHES ((1 << CPU_COUNT) - 1) + +#define CONNECTION_TRANSACTION_STATE_DIRECTION(con) ((con)->transaction_state & CONNECTION_TRANSACTION_STATE_DIRECTION_MASK) +#define CONNECTION_TRANSACTION_STATE_TYPE(con) ((con)->transaction_state & CONNECTION_TRANSACTION_STATE_TYPE_MASK) + +#define CONNECTION_START_TRANSACTION_FROM_TO(con, val) connection_start_transaction ((con), CONNECTION_TRANSACTION_DIRECTION_FROM_TO, (val)) +#define CONNECTION_START_TRANSACTION_TO_FROM(con, val) connection_start_transaction ((con), CONNECTION_TRANSACTION_DIRECTION_TO_FROM, (val)) +#define CONNECTION_CLEAR_TRANSACTION(con) (con->transaction_state = CONNECTION_TRANSACTION_STATE_NONE) + +enum PACKED connection_transaction_direction { + CONNECTION_TRANSACTION_DIRECTION_FROM_TO, + CONNECTION_TRANSACTION_DIRECTION_TO_FROM, + CONNECTION_TRANSACTION_DIRECTION_LAST, +}; + +#define CONNECTION_TRANSACTION_DIRECTION_BITS (CONNECTION_TRANSACTION_DIRECTION_LAST - 1) + +enum PACKED connection_transaction_type { + CONNECTION_TRANSACTION_TYPE_NONE, + CONNECTION_TRANSACTION_TYPE_FORWARD = 0b01 << CONNECTION_TRANSACTION_DIRECTION_BITS, + CONNECTION_TRANSACTION_TYPE_TRAILING = 0b10 << CONNECTION_TRANSACTION_DIRECTION_BITS, + CONNECTION_TRANSACTION_TYPE_LAST, +}; + +#define CONNECTION_TRANSACTION_TYPE_BITS (CONNECTION_TRANSACTION_TYPE_LAST >> 1) + +#define CONNECTION_TRANSACTION_STATE_DIRECTION_MASK ((1 << CONNECTION_TRANSACTION_DIRECTION_BITS) - 1) +#define CONNECTION_TRANSACTION_STATE_TYPE_MASK (((1 << CONNECTION_TRANSACTION_TYPE_BITS ) - 1) << CONNECTION_TRANSACTION_DIRECTION_BITS) + +enum PACKED connection_transaction_state { + CONNECTION_TRANSACTION_STATE_NONE, + CONNECTION_TRANSACTION_STATE_FROM_TO_FORWARD = CONNECTION_TRANSACTION_DIRECTION_FROM_TO | CONNECTION_TRANSACTION_TYPE_FORWARD , + CONNECTION_TRANSACTION_STATE_FROM_TO_TRAILING = CONNECTION_TRANSACTION_DIRECTION_FROM_TO | CONNECTION_TRANSACTION_TYPE_TRAILING, + CONNECTION_TRANSACTION_STATE_TO_FROM_FORWARD = CONNECTION_TRANSACTION_DIRECTION_TO_FROM | CONNECTION_TRANSACTION_TYPE_FORWARD , + CONNECTION_TRANSACTION_STATE_TO_FROM_TRAILING = CONNECTION_TRANSACTION_DIRECTION_TO_FROM | CONNECTION_TRANSACTION_TYPE_TRAILING, +}; + +typedef struct connection { + enum connection_transaction_state transaction_state; + u8 transaction_value; + float transaction_progress; + bool transaction_just_finished; + enum connection_transaction_state transaction_just_finished_state; + bool transaction_pending; + float x; + float ys; + float ye; + Color color; + float scale; +} *CONNECTION; + +extern CONNECTION cons_cpu_cache_addr [CPU_COUNT]; +extern CONNECTION cons_cpu_cache_data [CPU_COUNT]; +extern CONNECTION cons_cache_bus_shared [CPU_COUNT]; +extern CONNECTION cons_cache_bus_addr [CPU_COUNT]; +extern CONNECTION cons_cache_bus_data [CPU_COUNT]; +extern CONNECTION con_memory_bus_addr; +extern CONNECTION con_memory_bus_data; +extern bool transaction_proggressed; + +void init_connection (CONNECTION, float x, float ys, float ye, Color, float scale); +void draw_connection (CONNECTION); +void connection_start_transaction (CONNECTION, enum connection_transaction_direction, u8 value); +void connection_end_transaction (CONNECTION); +void connection_reverse_transaction (CONNECTION); +bool connection_progress_transaction (CONNECTION); +bool connection_is_transaction_ongoing (CONNECTION); +bool connection_is_transaction_complete (CONNECTION); +bool connection_is_transaction_direction_from_to (CONNECTION); +bool connection_is_transaction_direction_to_from (CONNECTION); + +#endif diff --git a/src/cpus.c b/src/cpus.c new file mode 100644 index 0000000..f2effc0 --- /dev/null +++ b/src/cpus.c @@ -0,0 +1,179 @@ +#include "cpus.h" +#include +#include +#include +#include "caches.h" +#include "connections.h" +#include "config.h" + +extern Font font; +extern Camera2D camera; + +CPU cpus[CPU_COUNT]; +int cpu_button_hover = CPU_COUNT*MEMORY_SIZE*3; + +void +init_cpu (CPU cpu, CACHE cache) +{ + int i = cpu - cpus[0]; + cpu->rec = (Rectangle) { + .x = cache->rec.x, + .y = cache->rec.y + cache->rec.height + BLOCK_MARGINV, + .width = BLOCK_WIDTH, + .height = 2*BLOCK_PADDING + MEMORY_SIZE*(ENTRY_HEIGHT + ENTRY_MARGIN) - ENTRY_MARGIN, + }; + + strcpy (cpu->label, "CPU 0"); + cpu->label[strlen (cpu->label) - 1] += i; + cpu->label_origin = Vector2Scale (MeasureTextEx (font, cpu->label, FONT_SIZE, TEXT_SPACING), 0.5); + cpu->label_position = (Vector2) { + .x = cpu->rec.x - cpu->label_origin.y/2 - TEXT_MARGIN, + .y = cpu->rec.y + cpu->rec.height/2, + }; + + float ys = cpu->rec.y - LINE_THICKNESS; + float ye = cache->rec.y + cache->rec.height + LINE_THICKNESS; + float xd = cpu->rec.width*1/4; + float xs = cpu->rec.x + xd; + init_connection (cons_cpu_cache_addr[i], xs + 1*xd, ys, ye, COLOR_ADDR, 1.); + init_connection (cons_cpu_cache_data[i], xs + 2*xd, ys, ye, COLOR_DATA, 1.); + + cpu->highlighted = 3*MEMORY_SIZE; +} + +bool +handle_cpu_buttons (CPU cpu) +{ + int i = cpu - cpus[0]; + Vector2 mouse_pos = GetScreenToWorld2D (GetMousePosition (), camera); + if (!CheckCollisionPointRec (mouse_pos, cpu->rec)) + return false; + + for (int j = 0; j < MEMORY_SIZE; ++j) { + Rectangle read_rec = { + .x = cpu->rec.x + cpu->rec.width*1/4 + BLOCK_PADDING, + .y = cpu->rec.y + Remap (j, 0, MEMORY_SIZE - 1, + BLOCK_PADDING, + cpu->rec.height - BLOCK_PADDING - ENTRY_HEIGHT), + .width = cpu->rec.width*1/4. - 2*BLOCK_PADDING/3. - 2*LINE_THICKNESS/3. - ENTRY_MARGIN/2., + .height = ENTRY_HEIGHT, + }; + if (CheckCollisionPointRec (mouse_pos, read_rec)) { + cpu_button_hover = i*MEMORY_SIZE*3 + j*3 + 0; + return true; + } + + Rectangle increment_rec = read_rec; + increment_rec.x += read_rec.width + ENTRY_MARGIN; + if (CheckCollisionPointRec (mouse_pos, increment_rec)) { + cpu_button_hover = i*MEMORY_SIZE*3 + j*3 + 1; + return true; + } + + Rectangle write_rec = increment_rec; + write_rec.x += increment_rec.width + ENTRY_MARGIN; + if (CheckCollisionPointRec (mouse_pos, write_rec)) { + cpu_button_hover = i*MEMORY_SIZE*3 + j*3 + 2; + return true; + } + } + + return false; +} + +void +draw_cpu (CPU cpu) +{ + int i = cpu - cpus[0]; + const Color edge_color = cpu->highlighted < 2*MEMORY_SIZE ? GREEN : COLOR_BLOCK_EDGE; + DrawRectangleRounded (cpu->rec, ROUNDNESS, SEGMENTS, COLOR_BLOCK); + DrawRectangleRoundedLinesEx (cpu->rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, edge_color); + DrawTextPro (font, cpu->label, cpu->label_position, cpu->label_origin, -90, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + const Vector2 divider_start_pos = { + .x = cpu->rec.x + cpu->rec.width*1/4, + .y = cpu->rec.y, + }; + const Vector2 divider_end_pos = { + .x = divider_start_pos.x, + .y = divider_start_pos.y + cpu->rec.height, + }; + DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE); + for (int j = 0; j < MEMORY_SIZE; ++j) { + if (j != 0) { + const Vector2 divider_start_pos = { + .x = cpu->rec.x, + .y = cpu->rec.y + j*cpu->rec.height/MEMORY_SIZE, + }; + const Vector2 divider_end_pos = { + .x = divider_start_pos.x + cpu->rec.width/4, + .y = divider_start_pos.y, + }; + DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE); + } + + char address[3]; + sprintf (address, "#%hhX", j); + const Vector2 address_center = Vector2Scale (MeasureTextEx (font, address, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 address_entry_center = { + .x = cpu->rec.x + cpu->rec.width*1/4/2., + .y = cpu->rec.y + (j + .5)*cpu->rec.height/MEMORY_SIZE, + }; + const Vector2 address_pos = Vector2Subtract (address_entry_center, address_center); + DrawTextEx (font, address, address_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + Rectangle read_rec = { + .x = divider_start_pos.x + BLOCK_PADDING + LINE_THICKNESS/3., + .y = cpu->rec.y + Remap (j, 0, MEMORY_SIZE - 1, + BLOCK_PADDING, + cpu->rec.height - BLOCK_PADDING - ENTRY_HEIGHT), + .width = cpu->rec.width*1/4. - 2*BLOCK_PADDING/3. - 2*LINE_THICKNESS/3. - ENTRY_MARGIN/2., + .height = ENTRY_HEIGHT, + }; + const Color read_color = cpu_button_hover == i*MEMORY_SIZE*3 + j*3 + 0 + ? COLOR_BUTTON_HOVER + : COLOR_BUTTON; + const Color read_edge_color = 3*j == cpu->highlighted ? RED : COLOR_BLOCK_EDGE; + DrawRectangleRounded (read_rec, ROUNDNESS, SEGMENTS, read_color); + DrawRectangleRoundedLinesEx (read_rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, read_edge_color); + const Vector2 read_text_center = MeasureTextEx (font, "R", FONT_SIZE, TEXT_SPACING); + const Vector2 read_text_pos = { + .x = read_rec.x + read_rec.width /2 - read_text_center.x/2, + .y = read_rec.y + read_rec.height/2 - read_text_center.y/2, + }; + DrawTextEx (font, "R", read_text_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + Rectangle increment_rec = read_rec; + increment_rec.x += read_rec.width + ENTRY_MARGIN; + Color increment_color = cpu_button_hover == i*MEMORY_SIZE*3 + j*3 + 1 + ? COLOR_BUTTON_HOVER + : COLOR_BUTTON; + const Color increment_edge_color = 3*j + 1 == cpu->highlighted ? RED : COLOR_BLOCK_EDGE; + DrawRectangleRounded (increment_rec, ROUNDNESS, SEGMENTS, increment_color); + DrawRectangleRoundedLinesEx (increment_rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, increment_edge_color); + const Vector2 increment_text_center = MeasureTextEx (font, "I", FONT_SIZE, TEXT_SPACING); + const Vector2 increment_text_pos = { + .x = increment_rec.x + increment_rec.width /2 - increment_text_center.x/2, + .y = increment_rec.y + increment_rec.height/2 - increment_text_center.y/2, + }; + DrawTextEx (font, "I", increment_text_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + Rectangle write_rec = increment_rec; + write_rec.x += increment_rec.width + ENTRY_MARGIN; + Color write_color = cpu_button_hover == i*MEMORY_SIZE*3 + j*3 + 2 + ? COLOR_BUTTON_HOVER + : COLOR_BUTTON; + const Color write_edge_color = 3*j + 2 == cpu->highlighted ? RED : COLOR_BLOCK_EDGE; + DrawRectangleRounded (write_rec, ROUNDNESS, SEGMENTS, write_color); + DrawRectangleRoundedLinesEx (write_rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, write_edge_color); + const Vector2 write_text_center = MeasureTextEx (font, "W", FONT_SIZE, TEXT_SPACING); + const Vector2 write_text_pos = { + .x = write_rec.x + write_rec.width /2 - write_text_center.x/2, + .y = write_rec.y + write_rec.height/2 - write_text_center.y/2, + }; + DrawTextEx (font, "W", write_text_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + } + + draw_connection (cons_cpu_cache_addr[i]); + draw_connection (cons_cpu_cache_data[i]); +} diff --git a/src/cpus.h b/src/cpus.h new file mode 100644 index 0000000..71fde22 --- /dev/null +++ b/src/cpus.h @@ -0,0 +1,24 @@ +#ifndef INCLUDED_CPUS_H +#define INCLUDED_CPUS_H + +#include +#include +#include "config.h" +#include "caches.h" + +typedef struct cpu { + Rectangle rec; + char label[6]; + Vector2 label_origin; + Vector2 label_position; + int highlighted; +} *CPU; + +extern CPU cpus[CPU_COUNT]; +extern int cpu_button_hover; + +void init_cpu (CPU, CACHE); +bool handle_cpu_buttons (CPU); +void draw_cpu (CPU); + +#endif diff --git a/src/export-font-as-code.c b/src/export-font-as-code.c new file mode 100644 index 0000000..1fedc3a --- /dev/null +++ b/src/export-font-as-code.c @@ -0,0 +1,17 @@ +#include +#include +#include "config.h" + +int +main (int argc, char *argv[]) +{ + InitWindow (INITIAL_SCREEN_WIDTH, + INITIAL_SCREEN_HEIGHT, + WINDOW_NAME); + + // Font font = LoadFontEx ("res/KodeMono-VariableFont_wght.ttf", 4*FONT_SIZE, NULL, 0); + Font font = LoadFontEx ("res/KodeMono-VariableFont_wght.ttf", FONT_SIZE + 4, NULL, 0); + ExportFontAsCode (font, "res/font_data.h"); + + return EXIT_SUCCESS; +} diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..1124f32 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,99 @@ +#include "memory.h" +#include +#include +#include "config.h" +#include "connections.h" + +MEMORY memory; + +extern Font font; + +void +init_memory (MEMORY memory, CPU any_cpu) +{ + memory->rec = (Rectangle) { + .x = -BLOCK_WIDTH/2, + .y = any_cpu->rec.y, + .width = BLOCK_WIDTH, + .height = 2*BLOCK_PADDING + MEMORY_SIZE*(ENTRY_HEIGHT + ENTRY_MARGIN) - ENTRY_MARGIN, + }; + memory->label = "MEMORY"; + memory->label_origin = Vector2Scale (MeasureTextEx (font, memory->label, FONT_SIZE, TEXT_SPACING), 0.5); + memory->label_position = (Vector2) { + .x = memory->rec.x - memory->label_origin.y/2 - TEXT_MARGIN, + .y = memory->rec.y + memory->rec.height/2, + }; + + float ys = memory->rec.y - LINE_THICKNESS; + float xd = memory->rec.width*1/4; + float xs = memory->rec.x + xd; + init_connection (con_memory_bus_addr, xs + 1*xd, ys, addr_bus->y + addr_bus->scale*BUS_THICKNESS/2, COLOR_ADDR, 1); + init_connection (con_memory_bus_data, xs + 2*xd, ys, data_bus->y + data_bus->scale*BUS_THICKNESS/2, COLOR_DATA, 1); + + memory->highlighted = MEMORY_SIZE; +} + +void +draw_memory (MEMORY memory) +{ + DrawRectangleRounded (memory->rec, ROUNDNESS, SEGMENTS, COLOR_BLOCK); + DrawTextPro (font, memory->label, memory->label_position, memory->label_origin, -90, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + for (int i = 0; i < MEMORY_SIZE; ++i) { + if (i == memory->highlighted) { + Rectangle entry = { + .x = memory->rec.x, + .y = memory->rec.y + i*memory->rec.height/MEMORY_SIZE, + .width = BLOCK_WIDTH, + .height = memory->rec.height/MEMORY_SIZE, + }; + DrawRectangleRec (entry, GREEN); + } + + if (i != 0) { + const Vector2 divider_start_pos = { + .x = memory->rec.x, + .y = memory->rec.y + i*memory->rec.height/MEMORY_SIZE, + }; + const Vector2 divider_end_pos = { + .x = divider_start_pos.x + memory->rec.width, + .y = divider_start_pos.y, + }; + DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE); + } + + char address[3]; + sprintf (address, "#%X", i); + const Vector2 address_center = Vector2Scale (MeasureTextEx (font, address, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 address_entry_center = { + .x = memory->rec.x + memory->rec.width*1/3/2, + .y = memory->rec.y + (i + .5)*memory->rec.height/MEMORY_SIZE, + }; + const Vector2 address_pos = Vector2Subtract (address_entry_center, address_center); + DrawTextEx (font, address, address_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + char value[3]; + sprintf (value, "%02hhX", memory->entries[i]); + const Vector2 value_center = Vector2Scale (MeasureTextEx (font, value, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 value_entry_center = { + .x = memory->rec.x + memory->rec.width*2/3, + .y = address_entry_center.y, + }; + const Vector2 value_pos = Vector2Subtract (value_entry_center, value_center); + DrawTextEx (font, value, value_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + } + DrawRectangleRoundedLinesEx (memory->rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, COLOR_BLOCK_EDGE); + + const Vector2 divider_start_pos = { + .x = memory->rec.x + memory->rec.width*1/3, + .y = memory->rec.y, + }; + const Vector2 divider_end_pos = { + .x = divider_start_pos.x, + .y = divider_start_pos.y + memory->rec.height, + }; + DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE); + + draw_connection (con_memory_bus_addr); + draw_connection (con_memory_bus_data); +} diff --git a/src/memory.h b/src/memory.h new file mode 100644 index 0000000..9c348cf --- /dev/null +++ b/src/memory.h @@ -0,0 +1,22 @@ +#ifndef INCLUDED_MEMORY_H +#define INCLUDED_MEMORY_H + +#include +#include +#include "cpus.h" + +typedef struct memory { + u8 entries[MEMORY_SIZE]; + Rectangle rec; + const char *label; + Vector2 label_origin; + Vector2 label_position; + u8 highlighted; +} *MEMORY; + +extern MEMORY memory; + +void init_memory (MEMORY, CPU any); +void draw_memory (MEMORY); + +#endif 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; +} diff --git a/src/states.c b/src/states.c new file mode 100644 index 0000000..3bf80ca --- /dev/null +++ b/src/states.c @@ -0,0 +1,75 @@ +#include "states.h" +#include + +extern bool is_paused; + +struct state states[MAX_TICKS]; +size_t current_tick = 0; +size_t last_tick = 0; + +void +init_state (void) +{ + *cache_locking_busses = CPU_COUNT; + *bus_cycles = 0; + init_bus (data_bus, cons_cache_bus_data, con_memory_bus_data, 0*BUS_SPACING + BUS_THICKNESS, 1.00, COLOR_DATA, "DATA BUS"); + init_bus (addr_bus, cons_cache_bus_addr, con_memory_bus_addr, 1*BUS_SPACING + BUS_THICKNESS, 1.00, COLOR_ADDR, "ADDRESS BUS"); + init_bus (shared_bus, cons_cache_bus_shared, NULL, 2*BUS_SPACING + BUS_THICKNESS, 0.75, COLOR_SHARED, "SHARED BUS"); + for (int i = 0; i < CPU_COUNT; ++i) { + init_cache (caches[i], shared_bus); + init_cpu (cpus[i], caches[i]); + } + init_memory (memory, cpus[0]); +} + +void +reset_state (void) +{ + set_state (0); + init_state (); + is_paused = false; +} + +void +set_state (size_t tick) +{ + cache_locking_busses = &states[tick].cache_locking_busses; + bus_cycles = &states[tick].bus_cycles; + data_bus = &states[tick].data_bus; + addr_bus = &states[tick].addr_bus; + shared_bus = &states[tick].shared_bus; + memory = &states[tick].memory; + for (int i = 0; i < CPU_COUNT; ++i) { + caches [i] = &states[tick].caches [i]; + cpus [i] = &states[tick].cpus [i]; + cons_cpu_cache_addr [i] = &states[tick].cons_cpu_cache_addr [i]; + cons_cpu_cache_data [i] = &states[tick].cons_cpu_cache_data [i]; + cons_cache_bus_shared [i] = &states[tick].cons_cache_bus_shared [i]; + cons_cache_bus_addr [i] = &states[tick].cons_cache_bus_addr [i]; + cons_cache_bus_data [i] = &states[tick].cons_cache_bus_data [i]; + } + con_memory_bus_addr = &states[tick].con_memory_bus_addr; + con_memory_bus_data = &states[tick].con_memory_bus_data; + + current_tick = tick; +} + +void +save_state (void) +{ + memcpy (&states[current_tick + 1], + &states[current_tick + 0], + sizeof (struct state)); + set_state (current_tick + 1); + last_tick = current_tick; + + // re-link bus connections + for (int i = 0; i < CPU_COUNT; ++i) { + states[current_tick] . data_bus.cache_connections [i] = &states[current_tick].cons_cache_bus_data [i]; + states[current_tick] . addr_bus.cache_connections [i] = &states[current_tick].cons_cache_bus_addr [i]; + states[current_tick] . shared_bus.cache_connections [i] = &states[current_tick].cons_cache_bus_shared [i]; + } + states[current_tick] . data_bus.memory_connection = &states[current_tick].con_memory_bus_data; + states[current_tick] . addr_bus.memory_connection = &states[current_tick].con_memory_bus_addr; + states[current_tick] . shared_bus.memory_connection = NULL; +} diff --git a/src/states.h b/src/states.h new file mode 100644 index 0000000..4baf72c --- /dev/null +++ b/src/states.h @@ -0,0 +1,39 @@ +#ifndef INCLUDED_STATES_H +#define INCLUDED_STATES_H + +#include +#include "config.h" +#include "busses.h" +#include "caches.h" +#include "cpus.h" +#include "memory.h" +#include "connections.h" + +struct state { + int cache_locking_busses; + int bus_cycles; + struct bus data_bus; + struct bus addr_bus; + struct bus shared_bus; + struct cache caches [CPU_COUNT]; + struct cpu cpus [CPU_COUNT]; + struct connection cons_cpu_cache_addr [CPU_COUNT]; + struct connection cons_cpu_cache_data [CPU_COUNT]; + struct connection cons_cache_bus_shared [CPU_COUNT]; + struct connection cons_cache_bus_addr [CPU_COUNT]; + struct connection cons_cache_bus_data [CPU_COUNT]; + struct connection con_memory_bus_addr; + struct connection con_memory_bus_data; + struct memory memory; +}; + +extern struct state states[MAX_TICKS]; +extern size_t current_tick; +extern size_t last_tick; + +void set_state (size_t tick); +void reset_state (void); +void init_state (void); +void save_state (void); + +#endif diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..56c33fb --- /dev/null +++ b/src/utils.h @@ -0,0 +1,10 @@ +#ifndef INCLUDED_UTILS_H +#define INCLUDED_UTILS_H + +#include + +#define PACKED __attribute__((__packed__)) + +typedef uint8_t u8; + +#endif -- cgit v1.3.1