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