aboutsummaryrefslogtreecommitdiff
path: root/src/busses.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/busses.c')
-rw-r--r--src/busses.c121
1 files changed, 121 insertions, 0 deletions
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 @@
1#include "busses.h"
2#include <raymath.h>
3#include <string.h>
4#include "config.h"
5#include "connections.h"
6
7extern Font font;
8
9int *cache_locking_busses;
10int *bus_cycles;
11BUS data_bus;
12BUS addr_bus;
13BUS shared_bus;
14
15
16void
17init_bus (BUS bus, CONNECTION cache_cons[CPU_COUNT], CONNECTION memory_con, float y, float scale, Color color, char *label)
18{
19 *bus = (struct bus) {
20 .reserved_connection = CONNECTION_NONE,
21 .linked = false,
22 .memory_connection = memory_con,
23 .y = y,
24 .scale = scale,
25 .color = color,
26 .label = label,
27 .label_size = MeasureTextEx (font, label, FONT_SIZE, TEXT_SPACING),
28 };
29 memcpy (bus->cache_connections, cache_cons, sizeof (bus->cache_connections));
30}
31
32void
33draw_bus (BUS bus)
34{
35 const Vector2 pos_end = {
36 .x = (CPU_COUNT + 1)/2.*(BLOCK_WIDTH + BLOCK_MARGINH) - BLOCK_MARGINH + addr_bus->label_size.x,
37 .y = bus->y,
38 };
39
40 const Vector2 pos_start = {
41 .x = -1*pos_end.x,
42 .y = pos_end.y,
43 };
44
45 const Color color = (bus->linked
46 ? bus->color
47 : COLOR_BUS_INACTIVE);
48
49 const float thick = BUS_THICKNESS*bus->scale;
50 const float head_side = 2*thick;
51 const float head_height = head_side*sqrtf (3)/2;
52 const Vector2 dir = Vector2Normalize (Vector2Subtract (pos_end, pos_start));
53 const Vector2 line_offset = Vector2Scale (dir, head_height/bus->scale);
54 const Vector2 line_begin_pos = Vector2Add (pos_start, line_offset);
55 const Vector2 line_end_pos = Vector2Subtract (pos_end, line_offset);
56 DrawLineEx (line_begin_pos, line_end_pos, thick, color);
57 DrawTriangle(Vector2Add (line_begin_pos, Vector2Scale (dir, -head_height)),
58 Vector2Add (line_begin_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2), -PI/2)),
59 Vector2Add (line_begin_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2), PI/2)),
60 color);
61 DrawTriangle(Vector2Add (line_end_pos, Vector2Scale (dir, head_height)),
62 Vector2Add (line_end_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2), PI/2)),
63 Vector2Add (line_end_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2), -PI/2)),
64 color);
65
66 Vector2 label_pos = line_begin_pos;
67 label_pos.x += TEXT_MARGIN;
68 label_pos.y -= bus->label_size.y + TEXT_MARGIN;
69 DrawTextEx (font, bus->label, label_pos, FONT_SIZE, TEXT_SPACING, bus->color);
70}
71
72void
73bus_link_connection (BUS bus, CONNECTION con)
74{
75 bus->linked = true;
76
77 for (int i = 0; i < CPU_COUNT; ++i) {
78 if (!(bus->connection_targets & 1 << i))
79 continue;
80
81 CONNECTION_START_TRANSACTION_TO_FROM(bus->cache_connections[i], con->transaction_value);
82 }
83
84 if (bus->connection_targets & 1 << CPU_COUNT)
85 CONNECTION_START_TRANSACTION_TO_FROM(bus->memory_connection, con->transaction_value);
86}
87
88
89void
90bus_link_connections (BUS bus, CONNECTION cons[CPU_COUNT])
91{
92 bus->linked = true;
93
94 int target;
95 u8 data = 0;
96 for (int i = 0; i < CPU_COUNT; ++i) {
97 if (bus->connection_targets & 1 << i)
98 target = i;
99 else
100 data |= cons[i]->transaction_value;
101 }
102
103 CONNECTION_START_TRANSACTION_TO_FROM(bus->cache_connections[target], data);
104}
105
106
107void
108bus_unlink_connection (BUS bus)
109{
110 bus->linked = false;
111
112 for (int i = 0; i < CPU_COUNT; ++i) {
113 CONNECTION con = bus->cache_connections[i];
114 connection_end_transaction (con);
115 }
116
117 CONNECTION con = bus->memory_connection;
118 if (con) {
119 connection_end_transaction (con);
120 }
121}