-
Notifications
You must be signed in to change notification settings - Fork 16
/
cpu_driver.c
212 lines (186 loc) · 6.05 KB
/
cpu_driver.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "cpu_driver.h"
#include "defs_6502.h"
#include "interp.h"
#include "inturbo.h"
#include "jit.h"
#include "log.h"
#include "util.h"
#include <assert.h>
#include <stddef.h>
static void
cpu_driver_memory_range_invalidate_dummy(struct cpu_driver* p_cpu_driver,
uint16_t addr,
uint32_t len) {
(void) p_cpu_driver;
(void) addr;
(void) len;
}
static char*
cpu_driver_get_address_info_dummy(struct cpu_driver* p_cpu_driver,
uint16_t addr) {
(void) p_cpu_driver;
(void) addr;
return " ";
}
static void
cpu_driver_get_custom_counters_dummy(struct cpu_driver* p_cpu_driver,
uint64_t* p_c1,
uint64_t* p_c2) {
(void) p_cpu_driver;
*p_c1 = 0;
*p_c2 = 0;
}
static void
cpu_driver_housekeeping_tick_dummy(struct cpu_driver* p_cpu_driver) {
(void) p_cpu_driver;
}
static void
cpu_driver_set_reset_callback_default(
struct cpu_driver* p_cpu_driver,
void (*do_reset_callback)(void* p, uint32_t flags),
void* p_do_reset_callback_object) {
p_cpu_driver->do_reset_callback = do_reset_callback;
p_cpu_driver->p_do_reset_callback_object = p_do_reset_callback_object;
}
static void
cpu_driver_set_memory_written_callback_default(
struct cpu_driver* p_cpu_driver,
void (*memory_written_callback)(void* p),
void* p_memory_written_callback_object) {
(void) p_cpu_driver;
(void) memory_written_callback;
(void) p_memory_written_callback_object;
}
static void
cpu_driver_apply_flags_default(struct cpu_driver* p_cpu_driver,
uint32_t flags_set,
uint32_t flags_clear) {
p_cpu_driver->flags |= flags_set;
p_cpu_driver->flags &= ~flags_clear;
}
static uint32_t
cpu_driver_get_flags_default(struct cpu_driver* p_cpu_driver) {
return p_cpu_driver->flags;
}
static uint32_t
cpu_driver_get_exit_value_default(struct cpu_driver* p_cpu_driver) {
return p_cpu_driver->exit_value;
}
static void
cpu_driver_set_exit_value_default(struct cpu_driver* p_cpu_driver,
uint32_t exit_value) {
p_cpu_driver->exit_value = exit_value;
}
static void
cpu_driver_get_6502_opcode_maps(struct cpu_driver* p_cpu_driver,
uint8_t** p_out_optypes,
uint8_t** p_out_opmodes,
uint8_t** p_out_opmem,
uint8_t** p_out_opcycles) {
(void) p_cpu_driver;
if (p_out_optypes != NULL) {
*p_out_optypes = defs_6502_get_6502_optype_map();
}
if (p_out_opmodes != NULL) {
*p_out_opmodes = defs_6502_get_6502_opmode_map();
}
if (p_out_opmem != NULL) {
*p_out_opmem = defs_6502_get_6502_opmem_map();
}
if (p_out_opcycles != NULL) {
*p_out_opcycles = defs_6502_get_6502_opcycles_map();
}
}
static void
cpu_driver_get_65c12_opcode_maps(struct cpu_driver* p_cpu_driver,
uint8_t** p_out_optypes,
uint8_t** p_out_opmodes,
uint8_t** p_out_opmem,
uint8_t** p_out_opcycles) {
(void) p_cpu_driver;
if (p_out_optypes != NULL) {
*p_out_optypes = defs_6502_get_65c12_optype_map();
}
if (p_out_opmodes != NULL) {
*p_out_opmodes = defs_6502_get_65c12_opmode_map();
}
if (p_out_opmem != NULL) {
*p_out_opmem = defs_6502_get_65c12_opmem_map();
}
if (p_out_opcycles != NULL) {
*p_out_opcycles = defs_6502_get_65c12_opcycles_map();
}
}
struct cpu_driver*
cpu_driver_alloc(int mode,
int is_65c12,
struct state_6502* p_state_6502,
struct memory_access* p_memory_access,
struct timing_struct* p_timing,
struct bbc_options* p_options) {
struct cpu_driver* p_cpu_driver = NULL;
struct cpu_driver_funcs* p_funcs =
util_mallocz(sizeof(struct cpu_driver_funcs));
struct cpu_driver_extra* p_extra =
util_mallocz(sizeof(struct cpu_driver_extra));
switch (mode) {
case k_cpu_mode_interp:
p_cpu_driver = interp_create(p_funcs, is_65c12);
if (p_cpu_driver == NULL) {
util_bail("interp_create() failed");
}
break;
case k_cpu_mode_inturbo:
if (!is_65c12) {
p_cpu_driver = inturbo_create(p_funcs);
}
break;
case k_cpu_mode_jit:
if (!is_65c12) {
p_cpu_driver = jit_create(p_funcs);
}
break;
default:
assert(0);
break;
}
if (p_cpu_driver == NULL) {
log_do_log(k_log_misc,
k_log_info,
"cannot allocate 6502 driver type; falling back to interp");
p_cpu_driver = interp_create(p_funcs, is_65c12);
if (p_cpu_driver == NULL) {
util_bail("interp_create() failed");
}
mode = k_cpu_mode_interp;
}
asm_abi_init(&p_cpu_driver->abi, p_memory_access, p_options, p_state_6502);
defs_6502_init();
p_cpu_driver->p_extra = p_extra;
p_extra->type = mode;
p_extra->p_memory_access = p_memory_access;
p_extra->p_timing = p_timing;
p_extra->p_options = p_options;
p_cpu_driver->p_funcs = p_funcs;
p_funcs->set_reset_callback = cpu_driver_set_reset_callback_default;
p_funcs->set_memory_written_callback =
cpu_driver_set_memory_written_callback_default;
p_funcs->apply_flags = cpu_driver_apply_flags_default;
p_funcs->get_flags = cpu_driver_get_flags_default;
p_funcs->get_exit_value = cpu_driver_get_exit_value_default;
p_funcs->set_exit_value = cpu_driver_set_exit_value_default;
p_funcs->memory_range_invalidate = cpu_driver_memory_range_invalidate_dummy;
p_funcs->get_address_info = cpu_driver_get_address_info_dummy;
p_funcs->get_custom_counters = cpu_driver_get_custom_counters_dummy;
if (is_65c12) {
p_funcs->get_opcode_maps = cpu_driver_get_65c12_opcode_maps;
} else {
p_funcs->get_opcode_maps = cpu_driver_get_6502_opcode_maps;
}
p_funcs->housekeeping_tick = cpu_driver_housekeeping_tick_dummy;
return p_cpu_driver;
}
void
cpu_driver_init(struct cpu_driver* p_driver) {
p_driver->p_funcs->init(p_driver);
}