aboutsummaryrefslogtreecommitdiff
path: root/src/states.c
blob: 3bf80caadb3aca11229fffbf06f638a07959b90e (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
#include "states.h"
#include <string.h>

extern bool is_paused;

struct state states[MAX_TICKS];
size_t current_tick = 0;
size_t last_tick = 0;

void
init_state (void)
{
  *cache_locking_busses = CPU_COUNT;
  *bus_cycles = 0;
  init_bus (data_bus,   cons_cache_bus_data,   con_memory_bus_data, 0*BUS_SPACING + BUS_THICKNESS, 1.00, COLOR_DATA,   "DATA BUS");
  init_bus (addr_bus,   cons_cache_bus_addr,   con_memory_bus_addr, 1*BUS_SPACING + BUS_THICKNESS, 1.00, COLOR_ADDR,   "ADDRESS BUS");
  init_bus (shared_bus, cons_cache_bus_shared,                NULL, 2*BUS_SPACING + BUS_THICKNESS, 0.75, COLOR_SHARED, "SHARED BUS");
  for (int i = 0; i < CPU_COUNT; ++i) {
    init_cache (caches[i], shared_bus);
    init_cpu (cpus[i], caches[i]);
  }
  init_memory (memory, cpus[0]);
}

void
reset_state (void)
{
  set_state (0);
  init_state ();
  is_paused = false;
}

void
set_state (size_t tick)
{
  cache_locking_busses = &states[tick].cache_locking_busses;
  bus_cycles = &states[tick].bus_cycles;
    data_bus = &states[tick].data_bus;
    addr_bus = &states[tick].addr_bus;
  shared_bus = &states[tick].shared_bus;
  memory     = &states[tick].memory;
  for (int i = 0; i < CPU_COUNT; ++i) {
    caches                [i] = &states[tick].caches                [i];
    cpus                  [i] = &states[tick].cpus                  [i];
    cons_cpu_cache_addr   [i] = &states[tick].cons_cpu_cache_addr   [i];
    cons_cpu_cache_data   [i] = &states[tick].cons_cpu_cache_data   [i];
    cons_cache_bus_shared [i] = &states[tick].cons_cache_bus_shared [i];
    cons_cache_bus_addr   [i] = &states[tick].cons_cache_bus_addr   [i];
    cons_cache_bus_data   [i] = &states[tick].cons_cache_bus_data   [i];
  }
  con_memory_bus_addr = &states[tick].con_memory_bus_addr;
  con_memory_bus_data = &states[tick].con_memory_bus_data;

  current_tick = tick;
}

void
save_state (void)
{
  memcpy (&states[current_tick + 1],
          &states[current_tick + 0],
          sizeof (struct state));
  set_state (current_tick + 1);
  last_tick = current_tick;

  // re-link bus connections
  for (int i = 0; i < CPU_COUNT; ++i) {
    states[current_tick] .   data_bus.cache_connections [i] = &states[current_tick].cons_cache_bus_data   [i];
    states[current_tick] .   addr_bus.cache_connections [i] = &states[current_tick].cons_cache_bus_addr   [i];
    states[current_tick] . shared_bus.cache_connections [i] = &states[current_tick].cons_cache_bus_shared [i];
  }
  states[current_tick] .   data_bus.memory_connection = &states[current_tick].con_memory_bus_data;
  states[current_tick] .   addr_bus.memory_connection = &states[current_tick].con_memory_bus_addr;
  states[current_tick] . shared_bus.memory_connection = NULL;
}