aboutsummaryrefslogtreecommitdiff
path: root/src/connections.c
blob: 438eb299edcd5c1cf3d6b9f7620efcaaba6c92be (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
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
180
181
182
183
#include "connections.h"
#include "config.h"
#include <raymath.h>
#include <stdio.h>

bool transaction_proggressed;

#define TRANSACTION_STATE_DIRECTION(con) ((con)->transaction_state & CONNECTION_TRANSACTION_STATE_DIRECTION_MASK)
#define TRANSACTION_STATE_TYPE(con)      ((con)->transaction_state & CONNECTION_TRANSACTION_STATE_TYPE_MASK)

static inline float
logistic (float x, float x0, float max, float rate)
{
  return max/(1 + exp (-rate*(x - x0)));
}

CONNECTION cons_cpu_cache_addr   [CPU_COUNT];
CONNECTION cons_cpu_cache_data   [CPU_COUNT];
CONNECTION cons_cache_bus_shared [CPU_COUNT];
CONNECTION cons_cache_bus_addr   [CPU_COUNT];
CONNECTION cons_cache_bus_data   [CPU_COUNT];
CONNECTION con_memory_bus_addr;
CONNECTION con_memory_bus_data;

void
init_connection (CONNECTION con, float x, float ys, float ye, Color c, float scale)
{
  *con = (struct connection) {
    .transaction_state = CONNECTION_TRANSACTION_STATE_NONE,
    .x     = x,
    .ys    = ys,
    .ye    = ye,
    .color = c,
    .scale = scale,
  };
}

void
draw_connection (CONNECTION con)
{
  const float x = con->x;
  float ys;
  float ye;
  int dir;

  switch (TRANSACTION_STATE_DIRECTION(con)) {
  case CONNECTION_TRANSACTION_DIRECTION_FROM_TO:
    ys = con->ys;
    ye = con->ye;
    dir = +1;
    break;
  case CONNECTION_TRANSACTION_DIRECTION_TO_FROM:
    ys = con->ye;
    ye = con->ys;
    dir = -1;
    break;
  }

  const Vector2 pos_start = { x, ys };
  const float thick = BUS_THICKNESS*con->scale;
  const float head_side = 2*thick;
  const float head_height = head_side*sqrt (3)/2;
  const Vector2 head_offset_peak  = { 0, dir*head_height };
  const Vector2 head_offset_left  = { +dir*head_side/2, 0 };
  const Vector2 head_offset_right = { -dir*head_side/2, 0 };
  const Vector2 line_begin_pos = { x, ys - dir*head_height };
  const Vector2 line_end_pos   = { x, ye + dir*head_height };

  float fraction = (con->transaction_progress < 1
                    ? logistic (con->transaction_progress, 0.5, 1, 10) : 1);
  Vector2 lerped_line_pos = Vector2Lerp (pos_start, line_end_pos, fraction);

  switch (TRANSACTION_STATE_TYPE(con)) {
  case CONNECTION_TRANSACTION_TYPE_NONE:
    DrawLineEx (line_begin_pos, line_end_pos, thick, COLOR_BUS_INACTIVE);
    DrawTriangle(Vector2Add (line_begin_pos, head_offset_peak),
                 Vector2Add (line_begin_pos, head_offset_left),
                 Vector2Add (line_begin_pos, head_offset_right),
                 COLOR_BUS_INACTIVE);
    DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak),
                 Vector2Subtract (line_end_pos, head_offset_left),
                 Vector2Subtract (line_end_pos, head_offset_right),
                 COLOR_BUS_INACTIVE);
    break;
  case CONNECTION_TRANSACTION_TYPE_FORWARD:
    DrawLineEx (pos_start, line_end_pos, thick, COLOR_BUS_INACTIVE);
    DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak),
                 Vector2Subtract (line_end_pos, head_offset_left),
                 Vector2Subtract (line_end_pos, head_offset_right),
                 COLOR_BUS_INACTIVE);

    DrawLineEx (pos_start, lerped_line_pos, thick, con->color);
    DrawTriangle(Vector2Subtract (lerped_line_pos, head_offset_peak),
                 Vector2Subtract (lerped_line_pos, head_offset_left),
                 Vector2Subtract (lerped_line_pos, head_offset_right),
                 con->color);
    break;
  case CONNECTION_TRANSACTION_TYPE_TRAILING:
    DrawTriangle(Vector2Add (line_begin_pos, head_offset_peak),
                 Vector2Add (line_begin_pos, head_offset_left),
                 Vector2Add (line_begin_pos, head_offset_right),
                 ColorAlpha (COLOR_BUS_INACTIVE, fraction));

    DrawLineEx (Vector2Add (line_begin_pos, Vector2Scale (head_offset_peak, .5)),
                line_end_pos, thick, COLOR_BUS_INACTIVE);
    DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak),
                 Vector2Subtract (line_end_pos, head_offset_left),
                 Vector2Subtract (line_end_pos, head_offset_right),
                 COLOR_BUS_INACTIVE);

    DrawLineEx (lerped_line_pos, line_end_pos, thick, con->color);
    DrawTriangle(Vector2Subtract (line_end_pos, head_offset_peak),
                 Vector2Subtract (line_end_pos, head_offset_left),
                 Vector2Subtract (line_end_pos, head_offset_right),
                 con->color);
    break;
  }
}

void
connection_start_transaction (CONNECTION con, enum connection_transaction_direction dir, u8 value)
{
  con->transaction_value = value;
  con->transaction_progress = 0;
  con->transaction_state = dir | CONNECTION_TRANSACTION_TYPE_FORWARD;
}

void
connection_end_transaction (CONNECTION con)
{
  if (TRANSACTION_STATE_TYPE(con) != CONNECTION_TRANSACTION_TYPE_FORWARD)
    return;

  con->transaction_state = TRANSACTION_STATE_DIRECTION(con) |
                           CONNECTION_TRANSACTION_TYPE_TRAILING;
  con->transaction_progress = 0;
}

bool
connection_progress_transaction (CONNECTION con)
{
  if (!connection_is_transaction_ongoing (con) ||
      con->transaction_progress == 1) {
    return con->transaction_just_finished = false;
  }

  transaction_proggressed = true;
  con->transaction_progress = fmin (con->transaction_progress +
                                    GetFrameTime ()*TRANSACTIONS_PER_SECOND, 1);
  if (con->transaction_progress == 1) {
    con->transaction_just_finished_state = con->transaction_state;
    return con->transaction_just_finished = true;
  }

  return con->transaction_just_finished = false;
}

bool
connection_is_transaction_ongoing (CONNECTION con)
{
  return (TRANSACTION_STATE_TYPE(con) != CONNECTION_TRANSACTION_TYPE_NONE);
}

bool
connection_is_transaction_complete (CONNECTION con)
{
  return (con->transaction_progress == 1 &&
          connection_is_transaction_ongoing(con));
}

bool
connection_is_transaction_direction_from_to (CONNECTION con)
{
  return (con->transaction_state != CONNECTION_TRANSACTION_STATE_NONE &&
          TRANSACTION_STATE_DIRECTION(con) == CONNECTION_TRANSACTION_DIRECTION_FROM_TO);
}

bool
connection_is_transaction_direction_to_from (CONNECTION con)
{
  return (con->transaction_state != CONNECTION_TRANSACTION_STATE_NONE &&
          TRANSACTION_STATE_DIRECTION(con) == CONNECTION_TRANSACTION_DIRECTION_TO_FROM);
}