aboutsummaryrefslogtreecommitdiff
path: root/src/caches.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 00:37:41 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 00:37:41 +0000
commit1cf06615f97bc27f732a880e89e9366140095bf1 (patch)
treea02c356c64b150c164fd72254da33d872f572957 /src/caches.c
downloadcoursework-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/caches.c')
-rw-r--r--src/caches.c312
1 files changed, 312 insertions, 0 deletions
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 @@
1#include "caches.h"
2#include <raymath.h>
3#include <string.h>
4#include <stdio.h>
5#include "config.h"
6#include "connections.h"
7
8extern Font font;
9
10CACHE caches[CPU_COUNT];
11
12static char *cache_line_state_labels[] = {
13 [CACHE_LINE_STATE_MODIFIED ] = "M",
14 [CACHE_LINE_STATE_EXCLUSIVE] = "E",
15 [CACHE_LINE_STATE_SHARED ] = "S",
16 [CACHE_LINE_STATE_INVALID ] = "I",
17 [CACHE_LINE_STATE_FORWARD ] = "F",
18};
19
20void
21init_cache (CACHE cache, BUS lower_bus)
22{
23 int i = cache - caches[0];
24 int block_i = i < CACHE_COUNT/2 ? i : i + 1;
25 cache->rec = (Rectangle) {
26 .x = Remap (block_i, 0, CACHE_COUNT,
27 - CACHE_COUNT/2*(BLOCK_WIDTH + BLOCK_MARGINH) - BLOCK_WIDTH/2,
28 + CACHE_COUNT/2*(BLOCK_WIDTH + BLOCK_MARGINH) - BLOCK_WIDTH/2),
29 .y = lower_bus->y + lower_bus->scale*BUS_THICKNESS/2 + BLOCK_MARGINV,
30 .width = BLOCK_WIDTH,
31 .height = 2*BLOCK_PADDING + CACHE_LINE_COUNT*(ENTRY_HEIGHT + ENTRY_MARGIN) - ENTRY_MARGIN,
32 };
33
34 strcpy (cache->label, "CACHE 0");
35 cache->label[strlen (cache->label) - 1] += i;
36 cache->label_origin = Vector2Scale (MeasureTextEx (font, cache->label, FONT_SIZE, TEXT_SPACING), 0.5);
37 cache->label_position = (Vector2) {
38 .x = cache->rec.x - cache->label_origin.y/2 - TEXT_MARGIN,
39 .y = cache->rec.y + cache->rec.height/2,
40 };
41
42 float ys = cache->rec.y - LINE_THICKNESS;
43 float xd = cache->rec.width*1/4;
44 float xs = cache->rec.x + xd;
45 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);
46 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);
47 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);
48
49 cache->highlighted = CACHE_LINE_COUNT;
50 cache->highlighted_pending = CACHE_LINE_COUNT;
51
52 for (int i = 0; i < CACHE_LINE_COUNT; ++i) {
53 cache->lines[i] = (struct cache_line) {
54 .age = 0,
55 .state = CACHE_LINE_STATE_INVALID,
56 .addr = 0,
57 .data = 0,
58 };
59 }
60}
61
62void
63draw_cache (CACHE cache)
64{
65 int i = cache - caches[0];
66
67 DrawRectangleRounded (cache->rec, ROUNDNESS, SEGMENTS, COLOR_BLOCK);
68 DrawTextPro (font, cache->label, cache->label_position, cache->label_origin, -90, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
69
70 for (int j = 0; j < CACHE_ASSOCIATIVITY; ++j) {
71 char set[2];
72 sprintf (set, "%d", j);
73 const Vector2 set_center = Vector2Scale (MeasureTextEx (font, set, FONT_SIZE, TEXT_SPACING), .5);
74 const Vector2 set_entry_center = {
75 .x = cache->rec.x + cache->rec.height/CACHE_LINE_COUNT/2,
76 .y = cache->rec.y + 2*(j + .5)*cache->rec.height/CACHE_LINE_COUNT,
77 };
78 const Vector2 set_pos = Vector2Subtract (set_entry_center, set_center);
79 DrawTextEx (font, set, set_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
80
81 if (j != 0) {
82 const Vector2 divider_start_pos = {
83 .x = cache->rec.x,
84 .y = cache->rec.y + 2*j*cache->rec.height/CACHE_LINE_COUNT,
85 };
86 const Vector2 divider_end_pos = {
87 .x = divider_start_pos.x + cache->rec.height/CACHE_LINE_COUNT,
88 .y = divider_start_pos.y,
89 };
90 DrawLineEx(divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE);
91 }
92 }
93
94 for (int j = 0; j < CACHE_LINE_COUNT; ++j) {
95 Rectangle entry = {
96 .x = cache->rec.x + cache->rec.height/CACHE_LINE_COUNT,
97 .y = cache->rec.y + j*cache->rec.height/CACHE_LINE_COUNT,
98 .width = BLOCK_WIDTH - cache->rec.height/CACHE_LINE_COUNT,
99 .height = cache->rec.height/CACHE_LINE_COUNT,
100 };
101 if (cache->highlighted == j)
102 DrawRectangleRec (entry, GREEN);
103 else if (cache->highlighted_pending == j)
104 DrawRectangleRec (entry, DARKGREEN);
105
106 if (j != 0) {
107 const Vector2 divider_start_pos = {
108 .x = cache->rec.x + cache->rec.height/CACHE_LINE_COUNT,
109 .y = cache->rec.y + j*cache->rec.height/CACHE_LINE_COUNT,
110 };
111 const Vector2 divider_end_pos = {
112 .x = divider_start_pos.x + cache->rec.width - cache->rec.height/CACHE_LINE_COUNT,
113 .y = divider_start_pos.y,
114 };
115 DrawLineEx(divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE);
116 }
117
118 char age[2];
119 age[0] = cache->lines[j].age < 10 ? '0' + cache->lines[j].age : '+';
120 age[1] = '\0';
121 const Vector2 age_center = Vector2Scale (MeasureTextEx (font, age, FONT_SIZE, TEXT_SPACING), .5);
122 const Vector2 age_entry_center = {
123 .x = cache->rec.x + 3*cache->rec.height/CACHE_LINE_COUNT/2,
124 .y = cache->rec.y + (j + .5)*cache->rec.height/CACHE_LINE_COUNT,
125 };
126 const Vector2 age_pos = Vector2Subtract (age_entry_center, age_center);
127 DrawTextEx (font, age, age_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
128
129 char *state = cache_line_state_labels[cache->lines[j].state];
130 const Vector2 state_center = Vector2Scale (MeasureTextEx (font, state, FONT_SIZE, TEXT_SPACING), .5);
131 const Vector2 state_entry_center = {
132 .x = cache->rec.x + 5*cache->rec.height/CACHE_LINE_COUNT/2,
133 .y = cache->rec.y + (j + .5)*cache->rec.height/CACHE_LINE_COUNT,
134 };
135 const Vector2 state_pos = Vector2Subtract (state_entry_center, state_center);
136 DrawTextEx (font, state, state_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
137
138 if (cache->lines[j].state != CACHE_LINE_STATE_INVALID || true) {
139 char tag[4];
140 sprintf (tag, "%d%d%d",
141 cache->lines[j].addr >> 3 & 1,
142 cache->lines[j].addr >> 2 & 1,
143 cache->lines[j].addr >> 1 & 1);
144 const Vector2 tag_center = Vector2Scale (MeasureTextEx (font, tag, FONT_SIZE, TEXT_SPACING), .5);
145 const Vector2 tag_entry_center = {
146 .x = cache->rec.x + 3*cache->rec.height/CACHE_LINE_COUNT +
147 (cache->rec.width - 3*cache->rec.height/CACHE_LINE_COUNT)*3/10,
148 .y = state_entry_center.y,
149 };
150 const Vector2 tag_pos = Vector2Subtract (tag_entry_center, tag_center);
151 DrawTextEx (font, tag, tag_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
152
153 char value[3];
154 sprintf (value, "%02hhX", cache->lines[j].data);
155 const Vector2 value_center = Vector2Scale (MeasureTextEx (font, value, FONT_SIZE, TEXT_SPACING), .5);
156 const Vector2 value_entry_center = {
157 .x = cache->rec.x + 3*cache->rec.height/CACHE_LINE_COUNT +
158 (cache->rec.width - 3*cache->rec.height/CACHE_LINE_COUNT)*4/5,
159 .y = state_entry_center.y,
160 };
161 const Vector2 value_pos = Vector2Subtract (value_entry_center, value_center);
162 DrawTextEx (font, value, value_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
163 }
164 }
165
166 DrawRectangleRoundedLinesEx (cache->rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, COLOR_BLOCK_EDGE);
167
168 const Vector2 set_divider_start_pos = {
169 .x = cache->rec.x + cache->rec.height/CACHE_LINE_COUNT,
170 .y = cache->rec.y,
171 };
172 const Vector2 set_divider_end_pos = {
173 .x = set_divider_start_pos.x,
174 .y = set_divider_start_pos.y + cache->rec.height,
175 };
176 DrawLineEx (set_divider_start_pos, set_divider_end_pos, 2, COLOR_BLOCK_EDGE);
177
178 const Vector2 age_divider_start_pos = {
179 .x = set_divider_start_pos.x + cache->rec.height/CACHE_LINE_COUNT,
180 .y = cache->rec.y,
181 };
182 const Vector2 age_divider_end_pos = {
183 .x = age_divider_start_pos.x,
184 .y = age_divider_start_pos.y + cache->rec.height,
185 };
186 DrawLineEx (age_divider_start_pos, age_divider_end_pos, 2, COLOR_BLOCK_EDGE);
187
188 const Vector2 state_divider_start_pos = {
189 .x = set_divider_start_pos.x + 2*cache->rec.height/CACHE_LINE_COUNT,
190 .y = cache->rec.y,
191 };
192 const Vector2 state_divider_end_pos = {
193 .x = state_divider_start_pos.x,
194 .y = state_divider_start_pos.y + cache->rec.height,
195 };
196 DrawLineEx (state_divider_start_pos, state_divider_end_pos, 2, COLOR_BLOCK_EDGE);
197
198 const Vector2 data_divider_start_pos = {
199 .x = state_divider_start_pos.x + (cache->rec.width - 3*cache->rec.height/CACHE_LINE_COUNT)*3/5,
200 .y = cache->rec.y,
201 };
202 const Vector2 data_divider_end_pos = {
203 .x = data_divider_start_pos.x,
204 .y = data_divider_start_pos.y + cache->rec.height,
205 };
206 DrawLineEx (data_divider_start_pos, data_divider_end_pos, 2, COLOR_BLOCK_EDGE);
207
208 draw_connection (cons_cache_bus_shared [i]);
209 draw_connection (cons_cache_bus_addr [i]);
210 draw_connection (cons_cache_bus_data [i]);
211}
212
213CACHE_LINE
214cache_get (CACHE cache, u8 addr, bool age)
215{
216 u8 offset = (addr % CACHE_ASSOCIATIVITY)*CACHE_LINES_PER_SET;
217 CACHE_LINE lines_begin = &cache->lines[offset];
218 CACHE_LINE lines_end = lines_begin + CACHE_LINES_PER_SET;
219 CACHE_LINE line = NULL;
220
221 for (CACHE_LINE iter_line = lines_begin; iter_line < lines_end; ++iter_line) {
222 if (iter_line->addr == addr &&
223 iter_line->state != CACHE_LINE_STATE_INVALID) {
224 line = iter_line;
225 line->age = 0;
226 cache->highlighted_pending = cache->highlighted;
227 cache->highlighted = line - cache->lines;
228 // printf ("%d %d\n", cache->highlighted, cache->highlighted_pending);
229 } else if (iter_line->state != CACHE_LINE_STATE_INVALID)
230 iter_line->age += age;
231 }
232
233 return line;
234}
235
236void
237cache_set (CACHE cache, CACHE_LINE line, u8 addr, u8 data, enum cache_line_state state, bool age)
238{
239 if (line)
240 goto set;
241
242 u8 offset = (addr % CACHE_ASSOCIATIVITY)*CACHE_LINES_PER_SET;
243 CACHE_LINE lines_begin = &cache->lines[offset];
244 CACHE_LINE lines_end = lines_begin + CACHE_LINES_PER_SET;
245
246 // Age cache lines
247 for (line = lines_begin; line < lines_end; ++line) {
248 if (line->state != CACHE_LINE_STATE_INVALID)
249 line->age += age;
250 }
251
252 // Find cache line with matching address
253 for (line = lines_begin; line < lines_end; ++line) {
254 if (line->addr == addr &&
255 line->state != CACHE_LINE_STATE_INVALID)
256 goto select;
257 }
258
259 // Find new spot for cache line
260 line = lines_begin;
261 if (line->state == CACHE_LINE_STATE_INVALID)
262 goto select;
263 for (CACHE_LINE iter_line = lines_begin + 1; iter_line < lines_end; ++iter_line) {
264 if (iter_line->state == CACHE_LINE_STATE_INVALID) {
265 line = iter_line;
266 break;
267 }
268 if (iter_line->age > line->age)
269 line = iter_line;
270 }
271
272 select:
273 // printf ("%d %d\n", cache->highlighted, cache->highlighted_pending);
274 cache->highlighted_pending = cache->highlighted;
275 cache->highlighted = line - cache->lines;
276 line->age = 0;
277 set:
278 line->state = state;
279 line->addr = addr;
280 line->data = data;
281}
282
283CACHE_LINE
284cache_to_replace (CACHE cache, u8 addr)
285{
286 u8 offset = (addr % CACHE_ASSOCIATIVITY)*CACHE_LINES_PER_SET;
287 CACHE_LINE lines_begin = &cache->lines[offset];
288 CACHE_LINE lines_end = lines_begin + CACHE_LINES_PER_SET;
289 CACHE_LINE line = NULL;
290
291 // Find cache line with matching address
292 for (line = lines_begin; line < lines_end; ++line) {
293 if (line->addr == addr &&
294 line->state != CACHE_LINE_STATE_INVALID)
295 return line;
296 }
297
298 // Find new spot for cache line
299 line = lines_begin;
300 if (line->state == CACHE_LINE_STATE_INVALID)
301 return line;
302 for (CACHE_LINE iter_line = lines_begin + 1; iter_line < lines_end; ++iter_line) {
303 if (iter_line->state == CACHE_LINE_STATE_INVALID) {
304 line = iter_line;
305 break;
306 }
307 if (iter_line->age > line->age)
308 line = iter_line;
309 }
310
311 return line;
312}