aboutsummaryrefslogtreecommitdiff
path: root/src/cpus.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/cpus.c
downloadcoursework-1cf06615f97bc27f732a880e89e9366140095bf1.tar.gz
publishHEADmaster
Diffstat (limited to 'src/cpus.c')
-rw-r--r--src/cpus.c179
1 files changed, 179 insertions, 0 deletions
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 @@
1#include "cpus.h"
2#include <raymath.h>
3#include <string.h>
4#include <stdio.h>
5#include "caches.h"
6#include "connections.h"
7#include "config.h"
8
9extern Font font;
10extern Camera2D camera;
11
12CPU cpus[CPU_COUNT];
13int cpu_button_hover = CPU_COUNT*MEMORY_SIZE*3;
14
15void
16init_cpu (CPU cpu, CACHE cache)
17{
18 int i = cpu - cpus[0];
19 cpu->rec = (Rectangle) {
20 .x = cache->rec.x,
21 .y = cache->rec.y + cache->rec.height + BLOCK_MARGINV,
22 .width = BLOCK_WIDTH,
23 .height = 2*BLOCK_PADDING + MEMORY_SIZE*(ENTRY_HEIGHT + ENTRY_MARGIN) - ENTRY_MARGIN,
24 };
25
26 strcpy (cpu->label, "CPU 0");
27 cpu->label[strlen (cpu->label) - 1] += i;
28 cpu->label_origin = Vector2Scale (MeasureTextEx (font, cpu->label, FONT_SIZE, TEXT_SPACING), 0.5);
29 cpu->label_position = (Vector2) {
30 .x = cpu->rec.x - cpu->label_origin.y/2 - TEXT_MARGIN,
31 .y = cpu->rec.y + cpu->rec.height/2,
32 };
33
34 float ys = cpu->rec.y - LINE_THICKNESS;
35 float ye = cache->rec.y + cache->rec.height + LINE_THICKNESS;
36 float xd = cpu->rec.width*1/4;
37 float xs = cpu->rec.x + xd;
38 init_connection (cons_cpu_cache_addr[i], xs + 1*xd, ys, ye, COLOR_ADDR, 1.);
39 init_connection (cons_cpu_cache_data[i], xs + 2*xd, ys, ye, COLOR_DATA, 1.);
40
41 cpu->highlighted = 3*MEMORY_SIZE;
42}
43
44bool
45handle_cpu_buttons (CPU cpu)
46{
47 int i = cpu - cpus[0];
48 Vector2 mouse_pos = GetScreenToWorld2D (GetMousePosition (), camera);
49 if (!CheckCollisionPointRec (mouse_pos, cpu->rec))
50 return false;
51
52 for (int j = 0; j < MEMORY_SIZE; ++j) {
53 Rectangle read_rec = {
54 .x = cpu->rec.x + cpu->rec.width*1/4 + BLOCK_PADDING,
55 .y = cpu->rec.y + Remap (j, 0, MEMORY_SIZE - 1,
56 BLOCK_PADDING,
57 cpu->rec.height - BLOCK_PADDING - ENTRY_HEIGHT),
58 .width = cpu->rec.width*1/4. - 2*BLOCK_PADDING/3. - 2*LINE_THICKNESS/3. - ENTRY_MARGIN/2.,
59 .height = ENTRY_HEIGHT,
60 };
61 if (CheckCollisionPointRec (mouse_pos, read_rec)) {
62 cpu_button_hover = i*MEMORY_SIZE*3 + j*3 + 0;
63 return true;
64 }
65
66 Rectangle increment_rec = read_rec;
67 increment_rec.x += read_rec.width + ENTRY_MARGIN;
68 if (CheckCollisionPointRec (mouse_pos, increment_rec)) {
69 cpu_button_hover = i*MEMORY_SIZE*3 + j*3 + 1;
70 return true;
71 }
72
73 Rectangle write_rec = increment_rec;
74 write_rec.x += increment_rec.width + ENTRY_MARGIN;
75 if (CheckCollisionPointRec (mouse_pos, write_rec)) {
76 cpu_button_hover = i*MEMORY_SIZE*3 + j*3 + 2;
77 return true;
78 }
79 }
80
81 return false;
82}
83
84void
85draw_cpu (CPU cpu)
86{
87 int i = cpu - cpus[0];
88 const Color edge_color = cpu->highlighted < 2*MEMORY_SIZE ? GREEN : COLOR_BLOCK_EDGE;
89 DrawRectangleRounded (cpu->rec, ROUNDNESS, SEGMENTS, COLOR_BLOCK);
90 DrawRectangleRoundedLinesEx (cpu->rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, edge_color);
91 DrawTextPro (font, cpu->label, cpu->label_position, cpu->label_origin, -90, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
92
93 const Vector2 divider_start_pos = {
94 .x = cpu->rec.x + cpu->rec.width*1/4,
95 .y = cpu->rec.y,
96 };
97 const Vector2 divider_end_pos = {
98 .x = divider_start_pos.x,
99 .y = divider_start_pos.y + cpu->rec.height,
100 };
101 DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE);
102 for (int j = 0; j < MEMORY_SIZE; ++j) {
103 if (j != 0) {
104 const Vector2 divider_start_pos = {
105 .x = cpu->rec.x,
106 .y = cpu->rec.y + j*cpu->rec.height/MEMORY_SIZE,
107 };
108 const Vector2 divider_end_pos = {
109 .x = divider_start_pos.x + cpu->rec.width/4,
110 .y = divider_start_pos.y,
111 };
112 DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE);
113 }
114
115 char address[3];
116 sprintf (address, "#%hhX", j);
117 const Vector2 address_center = Vector2Scale (MeasureTextEx (font, address, FONT_SIZE, TEXT_SPACING), .5);
118 const Vector2 address_entry_center = {
119 .x = cpu->rec.x + cpu->rec.width*1/4/2.,
120 .y = cpu->rec.y + (j + .5)*cpu->rec.height/MEMORY_SIZE,
121 };
122 const Vector2 address_pos = Vector2Subtract (address_entry_center, address_center);
123 DrawTextEx (font, address, address_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
124
125 Rectangle read_rec = {
126 .x = divider_start_pos.x + BLOCK_PADDING + LINE_THICKNESS/3.,
127 .y = cpu->rec.y + Remap (j, 0, MEMORY_SIZE - 1,
128 BLOCK_PADDING,
129 cpu->rec.height - BLOCK_PADDING - ENTRY_HEIGHT),
130 .width = cpu->rec.width*1/4. - 2*BLOCK_PADDING/3. - 2*LINE_THICKNESS/3. - ENTRY_MARGIN/2.,
131 .height = ENTRY_HEIGHT,
132 };
133 const Color read_color = cpu_button_hover == i*MEMORY_SIZE*3 + j*3 + 0
134 ? COLOR_BUTTON_HOVER
135 : COLOR_BUTTON;
136 const Color read_edge_color = 3*j == cpu->highlighted ? RED : COLOR_BLOCK_EDGE;
137 DrawRectangleRounded (read_rec, ROUNDNESS, SEGMENTS, read_color);
138 DrawRectangleRoundedLinesEx (read_rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, read_edge_color);
139 const Vector2 read_text_center = MeasureTextEx (font, "R", FONT_SIZE, TEXT_SPACING);
140 const Vector2 read_text_pos = {
141 .x = read_rec.x + read_rec.width /2 - read_text_center.x/2,
142 .y = read_rec.y + read_rec.height/2 - read_text_center.y/2,
143 };
144 DrawTextEx (font, "R", read_text_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
145
146 Rectangle increment_rec = read_rec;
147 increment_rec.x += read_rec.width + ENTRY_MARGIN;
148 Color increment_color = cpu_button_hover == i*MEMORY_SIZE*3 + j*3 + 1
149 ? COLOR_BUTTON_HOVER
150 : COLOR_BUTTON;
151 const Color increment_edge_color = 3*j + 1 == cpu->highlighted ? RED : COLOR_BLOCK_EDGE;
152 DrawRectangleRounded (increment_rec, ROUNDNESS, SEGMENTS, increment_color);
153 DrawRectangleRoundedLinesEx (increment_rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, increment_edge_color);
154 const Vector2 increment_text_center = MeasureTextEx (font, "I", FONT_SIZE, TEXT_SPACING);
155 const Vector2 increment_text_pos = {
156 .x = increment_rec.x + increment_rec.width /2 - increment_text_center.x/2,
157 .y = increment_rec.y + increment_rec.height/2 - increment_text_center.y/2,
158 };
159 DrawTextEx (font, "I", increment_text_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
160
161 Rectangle write_rec = increment_rec;
162 write_rec.x += increment_rec.width + ENTRY_MARGIN;
163 Color write_color = cpu_button_hover == i*MEMORY_SIZE*3 + j*3 + 2
164 ? COLOR_BUTTON_HOVER
165 : COLOR_BUTTON;
166 const Color write_edge_color = 3*j + 2 == cpu->highlighted ? RED : COLOR_BLOCK_EDGE;
167 DrawRectangleRounded (write_rec, ROUNDNESS, SEGMENTS, write_color);
168 DrawRectangleRoundedLinesEx (write_rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, write_edge_color);
169 const Vector2 write_text_center = MeasureTextEx (font, "W", FONT_SIZE, TEXT_SPACING);
170 const Vector2 write_text_pos = {
171 .x = write_rec.x + write_rec.width /2 - write_text_center.x/2,
172 .y = write_rec.y + write_rec.height/2 - write_text_center.y/2,
173 };
174 DrawTextEx (font, "W", write_text_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
175 }
176
177 draw_connection (cons_cpu_cache_addr[i]);
178 draw_connection (cons_cpu_cache_data[i]);
179}