aboutsummaryrefslogtreecommitdiff
path: root/src/memory.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/memory.c
downloadcoursework-1cf06615f97bc27f732a880e89e9366140095bf1.tar.gz
publishHEADmaster
Diffstat (limited to 'src/memory.c')
-rw-r--r--src/memory.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/memory.c b/src/memory.c
new file mode 100644
index 0000000..1124f32
--- /dev/null
+++ b/src/memory.c
@@ -0,0 +1,99 @@
1#include "memory.h"
2#include <raymath.h>
3#include <stdio.h>
4#include "config.h"
5#include "connections.h"
6
7MEMORY memory;
8
9extern Font font;
10
11void
12init_memory (MEMORY memory, CPU any_cpu)
13{
14 memory->rec = (Rectangle) {
15 .x = -BLOCK_WIDTH/2,
16 .y = any_cpu->rec.y,
17 .width = BLOCK_WIDTH,
18 .height = 2*BLOCK_PADDING + MEMORY_SIZE*(ENTRY_HEIGHT + ENTRY_MARGIN) - ENTRY_MARGIN,
19 };
20 memory->label = "MEMORY";
21 memory->label_origin = Vector2Scale (MeasureTextEx (font, memory->label, FONT_SIZE, TEXT_SPACING), 0.5);
22 memory->label_position = (Vector2) {
23 .x = memory->rec.x - memory->label_origin.y/2 - TEXT_MARGIN,
24 .y = memory->rec.y + memory->rec.height/2,
25 };
26
27 float ys = memory->rec.y - LINE_THICKNESS;
28 float xd = memory->rec.width*1/4;
29 float xs = memory->rec.x + xd;
30 init_connection (con_memory_bus_addr, xs + 1*xd, ys, addr_bus->y + addr_bus->scale*BUS_THICKNESS/2, COLOR_ADDR, 1);
31 init_connection (con_memory_bus_data, xs + 2*xd, ys, data_bus->y + data_bus->scale*BUS_THICKNESS/2, COLOR_DATA, 1);
32
33 memory->highlighted = MEMORY_SIZE;
34}
35
36void
37draw_memory (MEMORY memory)
38{
39 DrawRectangleRounded (memory->rec, ROUNDNESS, SEGMENTS, COLOR_BLOCK);
40 DrawTextPro (font, memory->label, memory->label_position, memory->label_origin, -90, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
41
42 for (int i = 0; i < MEMORY_SIZE; ++i) {
43 if (i == memory->highlighted) {
44 Rectangle entry = {
45 .x = memory->rec.x,
46 .y = memory->rec.y + i*memory->rec.height/MEMORY_SIZE,
47 .width = BLOCK_WIDTH,
48 .height = memory->rec.height/MEMORY_SIZE,
49 };
50 DrawRectangleRec (entry, GREEN);
51 }
52
53 if (i != 0) {
54 const Vector2 divider_start_pos = {
55 .x = memory->rec.x,
56 .y = memory->rec.y + i*memory->rec.height/MEMORY_SIZE,
57 };
58 const Vector2 divider_end_pos = {
59 .x = divider_start_pos.x + memory->rec.width,
60 .y = divider_start_pos.y,
61 };
62 DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE);
63 }
64
65 char address[3];
66 sprintf (address, "#%X", i);
67 const Vector2 address_center = Vector2Scale (MeasureTextEx (font, address, FONT_SIZE, TEXT_SPACING), .5);
68 const Vector2 address_entry_center = {
69 .x = memory->rec.x + memory->rec.width*1/3/2,
70 .y = memory->rec.y + (i + .5)*memory->rec.height/MEMORY_SIZE,
71 };
72 const Vector2 address_pos = Vector2Subtract (address_entry_center, address_center);
73 DrawTextEx (font, address, address_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
74
75 char value[3];
76 sprintf (value, "%02hhX", memory->entries[i]);
77 const Vector2 value_center = Vector2Scale (MeasureTextEx (font, value, FONT_SIZE, TEXT_SPACING), .5);
78 const Vector2 value_entry_center = {
79 .x = memory->rec.x + memory->rec.width*2/3,
80 .y = address_entry_center.y,
81 };
82 const Vector2 value_pos = Vector2Subtract (value_entry_center, value_center);
83 DrawTextEx (font, value, value_pos, FONT_SIZE, TEXT_SPACING, COLOR_TEXT);
84 }
85 DrawRectangleRoundedLinesEx (memory->rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, COLOR_BLOCK_EDGE);
86
87 const Vector2 divider_start_pos = {
88 .x = memory->rec.x + memory->rec.width*1/3,
89 .y = memory->rec.y,
90 };
91 const Vector2 divider_end_pos = {
92 .x = divider_start_pos.x,
93 .y = divider_start_pos.y + memory->rec.height,
94 };
95 DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE);
96
97 draw_connection (con_memory_bus_addr);
98 draw_connection (con_memory_bus_data);
99}