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 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/busses.c (limited to 'src/busses.c') 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); + } +} -- cgit v1.3.1