From 1cf06615f97bc27f732a880e89e9366140095bf1 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 03:37:41 +0300 Subject: publish --- src/memory.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/memory.c (limited to 'src/memory.c') 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 @@ +#include "memory.h" +#include +#include +#include "config.h" +#include "connections.h" + +MEMORY memory; + +extern Font font; + +void +init_memory (MEMORY memory, CPU any_cpu) +{ + memory->rec = (Rectangle) { + .x = -BLOCK_WIDTH/2, + .y = any_cpu->rec.y, + .width = BLOCK_WIDTH, + .height = 2*BLOCK_PADDING + MEMORY_SIZE*(ENTRY_HEIGHT + ENTRY_MARGIN) - ENTRY_MARGIN, + }; + memory->label = "MEMORY"; + memory->label_origin = Vector2Scale (MeasureTextEx (font, memory->label, FONT_SIZE, TEXT_SPACING), 0.5); + memory->label_position = (Vector2) { + .x = memory->rec.x - memory->label_origin.y/2 - TEXT_MARGIN, + .y = memory->rec.y + memory->rec.height/2, + }; + + float ys = memory->rec.y - LINE_THICKNESS; + float xd = memory->rec.width*1/4; + float xs = memory->rec.x + xd; + init_connection (con_memory_bus_addr, xs + 1*xd, ys, addr_bus->y + addr_bus->scale*BUS_THICKNESS/2, COLOR_ADDR, 1); + init_connection (con_memory_bus_data, xs + 2*xd, ys, data_bus->y + data_bus->scale*BUS_THICKNESS/2, COLOR_DATA, 1); + + memory->highlighted = MEMORY_SIZE; +} + +void +draw_memory (MEMORY memory) +{ + DrawRectangleRounded (memory->rec, ROUNDNESS, SEGMENTS, COLOR_BLOCK); + DrawTextPro (font, memory->label, memory->label_position, memory->label_origin, -90, FONT_SIZE, TEXT_SPACING, COLOR_TEXT); + + for (int i = 0; i < MEMORY_SIZE; ++i) { + if (i == memory->highlighted) { + Rectangle entry = { + .x = memory->rec.x, + .y = memory->rec.y + i*memory->rec.height/MEMORY_SIZE, + .width = BLOCK_WIDTH, + .height = memory->rec.height/MEMORY_SIZE, + }; + DrawRectangleRec (entry, GREEN); + } + + if (i != 0) { + const Vector2 divider_start_pos = { + .x = memory->rec.x, + .y = memory->rec.y + i*memory->rec.height/MEMORY_SIZE, + }; + const Vector2 divider_end_pos = { + .x = divider_start_pos.x + memory->rec.width, + .y = divider_start_pos.y, + }; + DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE); + } + + char address[3]; + sprintf (address, "#%X", i); + const Vector2 address_center = Vector2Scale (MeasureTextEx (font, address, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 address_entry_center = { + .x = memory->rec.x + memory->rec.width*1/3/2, + .y = memory->rec.y + (i + .5)*memory->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); + + char value[3]; + sprintf (value, "%02hhX", memory->entries[i]); + const Vector2 value_center = Vector2Scale (MeasureTextEx (font, value, FONT_SIZE, TEXT_SPACING), .5); + const Vector2 value_entry_center = { + .x = memory->rec.x + memory->rec.width*2/3, + .y = address_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 (memory->rec, ROUNDNESS, SEGMENTS, LINE_THICKNESS, COLOR_BLOCK_EDGE); + + const Vector2 divider_start_pos = { + .x = memory->rec.x + memory->rec.width*1/3, + .y = memory->rec.y, + }; + const Vector2 divider_end_pos = { + .x = divider_start_pos.x, + .y = divider_start_pos.y + memory->rec.height, + }; + DrawLineEx (divider_start_pos, divider_end_pos, 2, COLOR_BLOCK_EDGE); + + draw_connection (con_memory_bus_addr); + draw_connection (con_memory_bus_data); +} -- cgit v1.3.1