aboutsummaryrefslogtreecommitdiff
path: root/src/memory.c
blob: 1124f3245cd8c2bdb1809f39a442e6001ec72e17 (plain)
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
#include "memory.h"
#include <raymath.h>
#include <stdio.h>
#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);
}