aboutsummaryrefslogtreecommitdiff
path: root/src/busses.c
blob: ff6a86df3b6dbdb8c30c5b899b4273f2a00fa1ad (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "busses.h"
#include <raymath.h>
#include <string.h>
#include "config.h"
#include "connections.h"

extern Font font;

int *cache_locking_busses;
int *bus_cycles;
BUS   data_bus;
BUS   addr_bus;
BUS shared_bus;


void
init_bus (BUS bus, CONNECTION cache_cons[CPU_COUNT], CONNECTION memory_con, float y, float scale, Color color, char *label)
{
  *bus = (struct bus) {
    .reserved_connection = CONNECTION_NONE,
    .linked = false,
    .memory_connection = memory_con,
    .y = y,
    .scale = scale,
    .color = color,
    .label = label,
    .label_size = MeasureTextEx (font, label, FONT_SIZE, TEXT_SPACING),
  };
  memcpy (bus->cache_connections, cache_cons, sizeof (bus->cache_connections));
}

void
draw_bus (BUS bus)
{
  const Vector2 pos_end = {
    .x = (CPU_COUNT + 1)/2.*(BLOCK_WIDTH + BLOCK_MARGINH) - BLOCK_MARGINH + addr_bus->label_size.x,
    .y = bus->y,
  };

  const Vector2 pos_start = {
    .x = -1*pos_end.x,
    .y =    pos_end.y,
  };

  const Color color = (bus->linked
                       ? bus->color
                       : COLOR_BUS_INACTIVE);

  const float thick = BUS_THICKNESS*bus->scale;
  const float head_side = 2*thick;
  const float head_height = head_side*sqrtf (3)/2;
  const Vector2 dir = Vector2Normalize (Vector2Subtract (pos_end, pos_start));
  const Vector2 line_offset = Vector2Scale (dir, head_height/bus->scale);
  const Vector2 line_begin_pos = Vector2Add      (pos_start, line_offset);
  const Vector2 line_end_pos   = Vector2Subtract (pos_end,   line_offset);
  DrawLineEx (line_begin_pos, line_end_pos, thick, color);
  DrawTriangle(Vector2Add (line_begin_pos, Vector2Scale (dir, -head_height)),
               Vector2Add (line_begin_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2), -PI/2)),
               Vector2Add (line_begin_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2),  PI/2)),
               color);
  DrawTriangle(Vector2Add (line_end_pos, Vector2Scale (dir, head_height)),
               Vector2Add (line_end_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2),  PI/2)),
               Vector2Add (line_end_pos, Vector2Rotate (Vector2Scale (dir, -head_side/2), -PI/2)),
               color);

  Vector2 label_pos = line_begin_pos;
  label_pos.x += TEXT_MARGIN;
  label_pos.y -= bus->label_size.y + TEXT_MARGIN;
  DrawTextEx (font, bus->label, label_pos, FONT_SIZE, TEXT_SPACING, bus->color);
}

void
bus_link_connection (BUS bus, CONNECTION con)
{
  bus->linked = true;

  for (int i = 0; i < CPU_COUNT; ++i) {
    if (!(bus->connection_targets & 1 << i))
      continue;

    CONNECTION_START_TRANSACTION_TO_FROM(bus->cache_connections[i], con->transaction_value);
  }

  if (bus->connection_targets & 1 << CPU_COUNT)
    CONNECTION_START_TRANSACTION_TO_FROM(bus->memory_connection, con->transaction_value);
}


void
bus_link_connections (BUS bus, CONNECTION cons[CPU_COUNT])
{
  bus->linked = true;

  int target;
  u8 data = 0;
  for (int i = 0; i < CPU_COUNT; ++i) {
    if (bus->connection_targets & 1 << i)
      target = i;
    else
      data |= cons[i]->transaction_value;
  }

  CONNECTION_START_TRANSACTION_TO_FROM(bus->cache_connections[target], data);
}


void
bus_unlink_connection (BUS bus)
{
  bus->linked = false;

  for (int i = 0; i < CPU_COUNT; ++i) {
    CONNECTION con = bus->cache_connections[i];
    connection_end_transaction (con);
  }

  CONNECTION con = bus->memory_connection;
  if (con) {
    connection_end_transaction (con);
  }
}