From 1cf06615f97bc27f732a880e89e9366140095bf1 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 03:37:41 +0300 Subject: publish --- src/cpus.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 src/cpus.c (limited to 'src/cpus.c') diff --git a/src/cpus.c b/src/cpus.c new file mode 100644 index 0000000..f2effc0 --- /dev/null +++ b/src/cpus.c @@ -0,0 +1,179 @@ +#include "cpus.h" +#include +#include +#include +#include "caches.h" +#include "connections.h" +#include "config.h" + +extern Font font; +extern Camera2D camera; + +CPU cpus[CPU_COUNT]; +int cpu_button_hover = CPU_COUNT*MEMORY_SIZE*3; + +void +init_cpu (CPU cpu, CACHE cache) +{ + int i = cpu - cpus[0]; + cpu->rec = (Rectangle) { + .x = cache->rec.x, + .y = cache->rec.y + cache->rec.height + BLOCK_MARGINV, + .width = BLOCK_WIDTH, + .height = 2*BLOCK_PADDING + MEMORY_SIZE*(ENTRY_HEIGHT + ENTRY_MARGIN) - ENTRY_MARGIN, + }; + + strcpy (cpu->label, "CPU 0"); + cpu->label[strlen (cpu->label) - 1] += i; + cpu->label_origin = Vector2Scale (MeasureTextEx (font, cpu->label, FONT_SIZE, TEXT_SPACING), 0.5); + cpu->label_position = (Vector2) { + .x = cpu->rec.x - cpu->label_origin.y/2 - TEXT_MARGIN, + .y = cpu->rec.y + cpu->rec.height/2, + }; + + float ys = cpu->rec.y - LINE_THICKNESS; + float ye = cache->rec.y + cache->rec.height + LINE_THICKNESS; + float xd = cpu->rec.width*1/4; + float xs = cpu->rec.x + xd; + init_connection (cons_cpu_cache_addr[i], xs + 1*xd, ys, ye, COLOR_ADDR, 1.); + init_connection (cons_cpu_cache_data[i], xs + 2*xd, ys, ye, COLOR_DATA, 1.); + + cpu->highlighted = 3*MEMORY_SIZE; +} + +bool +handle_cpu_buttons (CPU cpu) +{ + int i = cpu - cpus[0]; + Vector2 mouse_pos = GetScreenToWorld2D (GetMousePosition (), camera); + if (!CheckCollisionPointRec (mouse_pos, cpu->rec)) + return false; + + for (int j = 0; j < MEMORY_SIZE; ++j) { + Rectangle read_rec = { + .x = cpu->rec.x + cpu->rec.width*1/4 + BLOCK_PADDING, + .y = cpu->rec.y + Remap (j, 0, MEMORY_SIZE - 1, + BLOCK_PADDING, + cpu->rec.height - BLOCK_PADDING - ENTRY_HEIGHT), + .width = cpu->rec.width*1/4. - 2*BLOCK_PADDING/3. - 2*LINE_THICKNESS/3. - ENTRY_MARGIN/2., + .height = ENTRY_HEIGHT, + }; + if (CheckCollisionPointRec (mouse_pos, read_rec)) { + cpu_button_hover = i*MEMORY_SIZE*3 + j*3 + 0; + return true; + } + + Rectangle increment_rec = read_rec; + increment_rec.x += read_rec.width + ENTRY_MARGIN; + if (CheckCollisionPointRec (mouse_pos, increment_rec)) { + cpu_button_hover = i*MEMORY_SIZE*3 + j*3 + 1; + return true; + } + + Rectangle write_rec = increment_rec; + write_rec.x += increment_rec.width + ENTRY_MARGIN; + if (CheckCollisionPointRec (mouse_pos, write_rec)) { + cpu_button_hover = i*MEMORY_SIZE*3 + j*3 + 2; + return true; + } + } + + return false; +} + +void +draw_cpu (CPU cpu) +{ + int i = cpu - cpus[0]; + const Color edge_color = cpu->highlighted < 2*MEMORY_SIZE ? GREEN : COLOR_BLOCK_EDGE; + DrawRectangleRounded (cpu->rec, ROUNDNESS, SEGMENTS, COLOR_BLOCK); + DrawRectangleRoundedLinesEx (cpu->rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, edge_color); + DrawTextPro (font, cpu->label, cpu->label_position, cpu->label_origin, -90, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + const Vector2 divider_start_pos = { + .x = cpu->rec.x + cpu->rec.width*1/4, + .y = cpu->rec.y, + }; + const Vector2 divider_end_pos = { + .x = divider_start_pos.x, + .y = divider_start_pos.y + cpu->rec.height, + }; + DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE); + for (int j = 0; j < MEMORY_SIZE; ++j) { + if (j != 0) { + const Vector2 divider_start_pos = { + .x = cpu->rec.x, + .y = cpu->rec.y + j*cpu->rec.height/MEMORY_SIZE, + }; + const Vector2 divider_end_pos = { + .x = divider_start_pos.x + cpu->rec.width/4, + .y = divider_start_pos.y, + }; + DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE); + } + + char address[3]; + sprintf (address, "#%hhX", j); + const Vector2 address_center = Vector2Scale (MeasureTextEx (font, address, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 address_entry_center = { + .x = cpu->rec.x + cpu->rec.width*1/4/2., + .y = cpu->rec.y + (j + .5)*cpu->rec.height/MEMORY_SIZE, + }; + const Vector2 address_pos = Vector2Subtract (address_entry_center, address_center); + DrawTextEx (font, address, address_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + Rectangle read_rec = { + .x = divider_start_pos.x + BLOCK_PADDING + LINE_THICKNESS/3., + .y = cpu->rec.y + Remap (j, 0, MEMORY_SIZE - 1, + BLOCK_PADDING, + cpu->rec.height - BLOCK_PADDING - ENTRY_HEIGHT), + .width = cpu->rec.width*1/4. - 2*BLOCK_PADDING/3. - 2*LINE_THICKNESS/3. - ENTRY_MARGIN/2., + .height = ENTRY_HEIGHT, + }; + const Color read_color = cpu_button_hover == i*MEMORY_SIZE*3 + j*3 + 0 + ? COLOR_BUTTON_HOVER + : COLOR_BUTTON; + const Color read_edge_color = 3*j == cpu->highlighted ? RED : COLOR_BLOCK_EDGE; + DrawRectangleRounded (read_rec, ROUNDNESS, SEGMENTS, read_color); + DrawRectangleRoundedLinesEx (read_rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, read_edge_color); + const Vector2 read_text_center = MeasureTextEx (font, "R", FONT_SIZE, TEXT_SPACING); + const Vector2 read_text_pos = { + .x = read_rec.x + read_rec.width /2 - read_text_center.x/2, + .y = read_rec.y + read_rec.height/2 - read_text_center.y/2, + }; + DrawTextEx (font, "R", read_text_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + Rectangle increment_rec = read_rec; + increment_rec.x += read_rec.width + ENTRY_MARGIN; + Color increment_color = cpu_button_hover == i*MEMORY_SIZE*3 + j*3 + 1 + ? COLOR_BUTTON_HOVER + : COLOR_BUTTON; + const Color increment_edge_color = 3*j + 1 == cpu->highlighted ? RED : COLOR_BLOCK_EDGE; + DrawRectangleRounded (increment_rec, ROUNDNESS, SEGMENTS, increment_color); + DrawRectangleRoundedLinesEx (increment_rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, increment_edge_color); + const Vector2 increment_text_center = MeasureTextEx (font, "I", FONT_SIZE, TEXT_SPACING); + const Vector2 increment_text_pos = { + .x = increment_rec.x + increment_rec.width /2 - increment_text_center.x/2, + .y = increment_rec.y + increment_rec.height/2 - increment_text_center.y/2, + }; + DrawTextEx (font, "I", increment_text_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + Rectangle write_rec = increment_rec; + write_rec.x += increment_rec.width + ENTRY_MARGIN; + Color write_color = cpu_button_hover == i*MEMORY_SIZE*3 + j*3 + 2 + ? COLOR_BUTTON_HOVER + : COLOR_BUTTON; + const Color write_edge_color = 3*j + 2 == cpu->highlighted ? RED : COLOR_BLOCK_EDGE; + DrawRectangleRounded (write_rec, ROUNDNESS, SEGMENTS, write_color); + DrawRectangleRoundedLinesEx (write_rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, write_edge_color); + const Vector2 write_text_center = MeasureTextEx (font, "W", FONT_SIZE, TEXT_SPACING); + const Vector2 write_text_pos = { + .x = write_rec.x + write_rec.width /2 - write_text_center.x/2, + .y = write_rec.y + write_rec.height/2 - write_text_center.y/2, + }; + DrawTextEx (font, "W", write_text_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + } + + draw_connection (cons_cpu_cache_addr[i]); + draw_connection (cons_cpu_cache_data[i]); +} -- cgit v1.3.1