diff options
| author | 2026-11-08 00:37:41 +0000 | |
|---|---|---|
| committer | 2026-11-08 00:37:41 +0000 | |
| commit | 1cf06615f97bc27f732a880e89e9366140095bf1 (patch) | |
| tree | a02c356c64b150c164fd72254da33d872f572957 /src/connections.c | |
| download | coursework-1cf06615f97bc27f732a880e89e9366140095bf1.tar.gz | |
Diffstat (limited to 'src/connections.c')
| -rw-r--r-- | src/connections.c | 183 |
1 files changed, 183 insertions, 0 deletions
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 @@ | |||
| 1 | #include "connections.h" | ||
| 2 | #include "config.h" | ||
| 3 | #include <raymath.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | |||
| 6 | bool transaction_proggressed; | ||
| 7 | |||
| 8 | #define TRANSACTION_STATE_DIRECTION(con) ((con)->transaction_state & CONNECTION_TRANSACTION_STATE_DIRECTION_MASK) | ||
| 9 | #define TRANSACTION_STATE_TYPE(con) ((con)->transaction_state & CONNECTION_TRANSACTION_STATE_TYPE_MASK) | ||
| 10 | |||
| 11 | static inline float | ||
| 12 | logistic (float x, float x0, float max, float rate) | ||
| 13 | { | ||
| 14 | return max/(1 + exp (-rate*(x - x0))); | ||
| 15 | } | ||
| 16 | |||
| 17 | CONNECTION cons_cpu_cache_addr [CPU_COUNT]; | ||
| 18 | CONNECTION cons_cpu_cache_data [CPU_COUNT]; | ||
| 19 | CONNECTION cons_cache_bus_shared [CPU_COUNT]; | ||
| 20 | CONNECTION cons_cache_bus_addr [CPU_COUNT]; | ||
| 21 | CONNECTION cons_cache_bus_data [CPU_COUNT]; | ||
| 22 | CONNECTION con_memory_bus_addr; | ||
| 23 | CONNECTION con_memory_bus_data; | ||
| 24 | |||
| 25 | void | ||
| 26 | init_connection (CONNECTION con, float x, float ys, float ye, Color c, float scale) | ||
| 27 | { | ||
| 28 | *con = (struct connection) { | ||
| 29 | .transaction_state = CONNECTION_TRANSACTION_STATE_NONE, | ||
| 30 | .x = x, | ||
| 31 | .ys = ys, | ||
| 32 | .ye = ye, | ||
| 33 | .color = c, | ||
| 34 | .scale = scale, | ||
| 35 | }; | ||
| 36 | } | ||
| 37 | |||
| 38 | void | ||
| 39 | draw_connection (CONNECTION con) | ||
| 40 | { | ||
| 41 | const float x = con->x; | ||
| 42 | float ys; | ||
| 43 | float ye; | ||
| 44 | int dir; | ||
| 45 | |||
| 46 | switch (TRANSACTION_STATE_DIRECTION(con)) { | ||
| 47 | case CONNECTION_TRANSACTION_DIRECTION_FROM_TO: | ||
| 48 | ys = con->ys; | ||
| 49 | ye = con->ye; | ||
| 50 | dir = +1; | ||
| 51 | break; | ||
| 52 | case CONNECTION_TRANSACTION_DIRECTION_TO_FROM: | ||
| 53 | ys = con->ye; | ||
| 54 | ye = con->ys; | ||
| 55 | dir = -1; | ||
| 56 | break; | ||
| 57 | } | ||
| 58 | |||
| 59 | const Vector2 pos_start = { x, ys }; | ||
| 60 | const float thick = BUS_THICKNESS*con->scale; | ||
| 61 | const float head_side = 2*thick; | ||
| 62 | const float head_height = head_side*sqrt (3)/2; | ||
| 63 | const Vector2 head_offset_peak = { 0, dir*head_height }; | ||
| 64 | const Vector2 head_offset_left = { +dir*head_side/2, 0 }; | ||
| 65 | const Vector2 head_offset_right = { -dir*head_side/2, 0 }; | ||
| 66 | const Vector2 line_begin_pos = { x, ys - dir*head_height }; | ||
| 67 | const Vector2 line_end_pos = { x, ye + dir*head_height }; | ||
| 68 | |||
| 69 | float fraction = (con->transaction_progress < 1 | ||
| 70 | ? logistic (con->transaction_progress, 0.5, 1, 10) : 1); | ||
| 71 | Vector2 lerped_line_pos = Vector2Lerp (pos_start, line_end_pos, fraction); | ||
| 72 | |||
| 73 | switch (TRANSACTION_STATE_TYPE(con)) { | ||
| 74 | case CONNECTION_TRANSACTION_TYPE_NONE: | ||
| 75 | DrawLineEx (line_begin_pos, line_end_pos, thick, COLOR_BUS_INACTIVE); | ||
| 76 | DrawTriangle(Vector2Add (line_begin_pos, head_offset_peak), | ||
| 77 | Vector2Add (line_begin_pos, head_offset_left), | ||
| 78 | Vector2Add (line_begin_pos, head_offset_right), | ||
| 79 | COLOR_BUS_INACTIVE); | ||
| 80 | DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak), | ||
| 81 | Vector2Subtract (line_end_pos, head_offset_left), | ||
| 82 | Vector2Subtract (line_end_pos, head_offset_right), | ||
| 83 | COLOR_BUS_INACTIVE); | ||
| 84 | break; | ||
| 85 | case CONNECTION_TRANSACTION_TYPE_FORWARD: | ||
| 86 | DrawLineEx (pos_start, line_end_pos, thick, COLOR_BUS_INACTIVE); | ||
| 87 | DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak), | ||
| 88 | Vector2Subtract (line_end_pos, head_offset_left), | ||
| 89 | Vector2Subtract (line_end_pos, head_offset_right), | ||
| 90 | COLOR_BUS_INACTIVE); | ||
| 91 | |||
| 92 | DrawLineEx (pos_start, lerped_line_pos, thick, con->color); | ||
| 93 | DrawTriangle(Vector2Subtract (lerped_line_pos, head_offset_peak), | ||
| 94 | Vector2Subtract (lerped_line_pos, head_offset_left), | ||
| 95 | Vector2Subtract (lerped_line_pos, head_offset_right), | ||
| 96 | con->color); | ||
| 97 | break; | ||
| 98 | case CONNECTION_TRANSACTION_TYPE_TRAILING: | ||
| 99 | DrawTriangle(Vector2Add (line_begin_pos, head_offset_peak), | ||
| 100 | Vector2Add (line_begin_pos, head_offset_left), | ||
| 101 | Vector2Add (line_begin_pos, head_offset_right), | ||
| 102 | ColorAlpha (COLOR_BUS_INACTIVE, fraction)); | ||
| 103 | |||
| 104 | DrawLineEx (Vector2Add (line_begin_pos, Vector2Scale (head_offset_peak, .5)), | ||
| 105 | line_end_pos, thick, COLOR_BUS_INACTIVE); | ||
| 106 | DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak), | ||
| 107 | Vector2Subtract (line_end_pos, head_offset_left), | ||
| 108 | Vector2Subtract (line_end_pos, head_offset_right), | ||
| 109 | COLOR_BUS_INACTIVE); | ||
| 110 | |||
| 111 | DrawLineEx (lerped_line_pos, line_end_pos, thick, con->color); | ||
| 112 | DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak), | ||
| 113 | Vector2Subtract (line_end_pos, head_offset_left), | ||
| 114 | Vector2Subtract (line_end_pos, head_offset_right), | ||
| 115 | con->color); | ||
| 116 | break; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | void | ||
| 121 | connection_start_transaction (CONNECTION con, enum connection_transaction_direction dir, u8 value) | ||
| 122 | { | ||
| 123 | con->transaction_value = value; | ||
| 124 | con->transaction_progress = 0; | ||
| 125 | con->transaction_state = dir | CONNECTION_TRANSACTION_TYPE_FORWARD; | ||
| 126 | } | ||
| 127 | |||
| 128 | void | ||
| 129 | connection_end_transaction (CONNECTION con) | ||
| 130 | { | ||
| 131 | if (TRANSACTION_STATE_TYPE(con) != CONNECTION_TRANSACTION_TYPE_FORWARD) | ||
| 132 | return; | ||
| 133 | |||
| 134 | con->transaction_state = TRANSACTION_STATE_DIRECTION(con) | | ||
| 135 | CONNECTION_TRANSACTION_TYPE_TRAILING; | ||
| 136 | con->transaction_progress = 0; | ||
| 137 | } | ||
| 138 | |||
| 139 | bool | ||
| 140 | connection_progress_transaction (CONNECTION con) | ||
| 141 | { | ||
| 142 | if (!connection_is_transaction_ongoing (con) || | ||
| 143 | con->transaction_progress == 1) { | ||
| 144 | return con->transaction_just_finished = false; | ||
| 145 | } | ||
| 146 | |||
| 147 | transaction_proggressed = true; | ||
| 148 | con->transaction_progress = fmin (con->transaction_progress + | ||
| 149 | GetFrameTime ()*TRANSACTIONS_PER_SECOND, 1); | ||
| 150 | if (con->transaction_progress == 1) { | ||
| 151 | con->transaction_just_finished_state = con->transaction_state; | ||
| 152 | return con->transaction_just_finished = true; | ||
| 153 | } | ||
| 154 | |||
| 155 | return con->transaction_just_finished = false; | ||
| 156 | } | ||
| 157 | |||
| 158 | bool | ||
| 159 | connection_is_transaction_ongoing (CONNECTION con) | ||
| 160 | { | ||
| 161 | return (TRANSACTION_STATE_TYPE(con) != CONNECTION_TRANSACTION_TYPE_NONE); | ||
| 162 | } | ||
| 163 | |||
| 164 | bool | ||
| 165 | connection_is_transaction_complete (CONNECTION con) | ||
| 166 | { | ||
| 167 | return (con->transaction_progress == 1 && | ||
| 168 | connection_is_transaction_ongoing(con)); | ||
| 169 | } | ||
| 170 | |||
| 171 | bool | ||
| 172 | connection_is_transaction_direction_from_to (CONNECTION con) | ||
| 173 | { | ||
| 174 | return (con->transaction_state != CONNECTION_TRANSACTION_STATE_NONE && | ||
| 175 | TRANSACTION_STATE_DIRECTION(con) == CONNECTION_TRANSACTION_DIRECTION_FROM_TO); | ||
| 176 | } | ||
| 177 | |||
| 178 | bool | ||
| 179 | connection_is_transaction_direction_to_from (CONNECTION con) | ||
| 180 | { | ||
| 181 | return (con->transaction_state != CONNECTION_TRANSACTION_STATE_NONE && | ||
| 182 | TRANSACTION_STATE_DIRECTION(con) == CONNECTION_TRANSACTION_DIRECTION_TO_FROM); | ||
| 183 | } | ||
