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
|
#include "cpus.h"
#include <raymath.h>
#include <string.h>
#include <stdio.h>
#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]);
}
|