#include "caches.h" #include #include #include #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; }