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