OXYGENHUB
# 9120e87480a6 enhanced-x16-emulator / tests / support / fake_devices.c
5.8 KiB · C · last changed chore(): initial commit 26 days ago source blame history raw
1
#include "fake_devices.h"
2
3
#include <stddef.h>
4
#include <string.h>
5
6
#include "glue.h"
7
#include "cartridge.h"
8
#include "wav_recorder.h"
9
#include "memory.h"
10
11
extern uint32_t clockticks6502;
12
13
fake_dev_log_t fake_dev[FAKE_DEV_COUNT];
14
15
const char *
16
fake_dev_name(fake_dev_t dev)
17
{
18
	switch (dev) {
19
		case FAKE_VIA1: return "VIA1";
20
		case FAKE_VIA2: return "VIA2";
21
		case FAKE_VERA: return "VERA";
22
		case FAKE_YM:   return "YM";
23
		case FAKE_MIDI: return "MIDI";
24
		case FAKE_CART: return "cartridge";
25
		default:        return "?";
26
	}
27
}
28
29
static uint8_t
30
note_read(fake_dev_t dev, uint8_t reg, bool debug)
31
{
32
	fake_dev_log_t *d = &fake_dev[dev];
33
	d->reads++;
34
	d->last_reg = reg;
35
	if (debug) {
36
		d->debug_reads++;
37
	} else {
38
		// What a debug read is not allowed to cause.
39
		d->state++;
40
	}
41
	return (uint8_t)(0xD0 | (reg & 0x0F));
42
}
43
44
static void
45
note_write(fake_dev_t dev, uint8_t reg, uint8_t value)
46
{
47
	fake_dev_log_t *d = &fake_dev[dev];
48
	d->writes++;
49
	d->last_reg = reg;
50
	d->last_value = value;
51
	d->state++;
52
}
53
54
void
55
fake_devices_reset(void)
56
{
57
	// memory.c allocates RAM and BRAM lazily, so the first reset has to bring
58
	// the memory system up before anything reads through it.
59
	static bool memory_ready = false;
60
	if (!memory_ready) {
61
		memory_init();
62
		memory_ready = true;
63
	}
64
65
	memset(fake_dev, 0, sizeof fake_dev);
66
	clockticks6502 = 0;
67
}
68
69
uint32_t
70
fake_devices_state_sum(void)
71
{
72
	uint32_t sum = 0;
73
	for (int i = 0; i < FAKE_DEV_COUNT; i++) {
74
		sum += fake_dev[i].state;
75
	}
76
	return sum;
77
}
78
79
int
80
fake_devices_total_reads(void)
81
{
82
	int n = 0;
83
	for (int i = 0; i < FAKE_DEV_COUNT; i++) {
84
		n += fake_dev[i].reads;
85
	}
86
	return n;
87
}
88
89
int
90
fake_devices_total_writes(void)
91
{
92
	int n = 0;
93
	for (int i = 0; i < FAKE_DEV_COUNT; i++) {
94
		n += fake_dev[i].writes;
95
	}
96
	return n;
97
}
98
99
// ---- The devices memory.c routes to --------------------------------------
100
101
uint8_t via1_read(uint8_t reg, bool debug)  { return note_read(FAKE_VIA1, reg, debug); }
102
uint8_t via2_read(uint8_t reg, bool debug)  { return note_read(FAKE_VIA2, reg, debug); }
103
uint8_t video_read(uint8_t reg, bool debug) { return note_read(FAKE_VERA, reg, debug); }
104
uint8_t midi_serial_read(uint8_t reg, bool debug) { return note_read(FAKE_MIDI, reg, debug); }
105
106
void via1_write(uint8_t reg, uint8_t value)  { note_write(FAKE_VIA1, reg, value); }
107
void via2_write(uint8_t reg, uint8_t value)  { note_write(FAKE_VIA2, reg, value); }
108
void video_write(uint8_t reg, uint8_t value) { note_write(FAKE_VERA, reg, value); }
109
void midi_serial_write(uint8_t reg, uint8_t value) { note_write(FAKE_MIDI, reg, value); }
110
void YM_write_reg(uint8_t reg, uint8_t value) { note_write(FAKE_YM, reg, value); }
111
112
// memory.c brackets its VERA writes with this so the bandwidth accounting can
113
// tell a debugger's poke from a store the guest made. There is no accounting
114
// here to tell, and the flag is not part of what these tests assert about, so
115
// it is swallowed rather than recorded -- noting it would put an extra event in
116
// every trace the write tests compare against.
117
void video_set_debug_write(bool debug) { (void)debug; }
118
119
// YM_read_status() has no debug parameter, so this cannot behave differently
120
// for a debug read. Recorded as a real read, which is the honest reading of a
121
// signature that carries no way to say otherwise.
122
uint8_t
123
YM_read_status(void)
124
{
125
	return note_read(FAKE_YM, 0, false);
126
}
127
128
uint8_t
129
cartridge_read(uint16_t address, uint8_t bank)
130
{
131
	(void)bank;
132
	return note_read(FAKE_CART, (uint8_t)(address & 0x0F), false);
133
}
134
135
void
136
cartridge_write(uint16_t address, uint8_t bank, uint8_t value)
137
{
138
	(void)bank;
139
	note_write(FAKE_CART, (uint8_t)(address & 0x0F), value);
140
}
141
142
// ---- Everything else memory.c reaches for --------------------------------
143
// Inert on purpose: none of it is what these tests are about, and a stub that
144
// does nothing cannot quietly become part of the result.
145
146
void audio_render(void) {}
147
void print_iso8859_15_char(char c) { (void)c; }
148
void dbg_load_note_debugger(bool on) { (void)on; }
149
uint8_t wav_recorder_get_state(void) { return 0; }
150
void wav_recorder_set(wav_recorder_command_t command) { (void)command; }
151
152
// Counted rather than ignored: whether a watchpoint reported a break is the one
153
// thing a watchpoint is allowed to change.
154
int fake_break_count = 0;
155
void DEBUGBreakOnWatchpoint(void) { fake_break_count++; }
156
157
// The CPU core signals a halt (STP) through this. memory.c already provides
158
// vp6502(), the vector-pull callback.
159
void stop6502(uint16_t address, uint8_t bank) { (void)address; (void)bank; }
160
161
// SDL is reachable from memory.c only through memory_save() and the usage-count
162
// dump, neither of which these tests call. Defining them here keeps the test
163
// free of an SDL runtime.
164
size_t SDL_RWwrite(SDL_RWops *c, const void *p, size_t s, size_t n)
165
{
166
	(void)c; (void)p; (void)s;
167
	return n;
168
}
169
SDL_RWops *SDL_RWFromFile(const char *f, const char *m) { (void)f; (void)m; return NULL; }
170
int SDL_RWclose(SDL_RWops *c) { (void)c; return 0; }
171
172
// ---- Machine configuration -----------------------------------------------
173
// A Gen1 machine with both VIAs and no MIDI card, which is what the X16 is.
174
//
175
// Every one of these is initialised deliberately. An uninitialised global is a
176
// tentative definition, which some linkers quietly merge with a real one
177
// elsewhere and others reject; initialising them means a clash with a linked
178
// module is a hard error everywhere rather than only on GCC.
179
180
uint8_t *CART = NULL;
181
uint8_t  keymap = 0;
182
bool     is_gen2 = false;
183
bool     has_via2 = true;
184
bool     has_midi_card = false;
185
uint16_t midi_card_addr = 0x9f60;
186
uint16_t num_banks = 1;
187
uint16_t num_ram_banks = 64;
188
// opcode_addr belongs to the CPU core, which is linked here for real.
189
190
bool debugger_enabled = false;
191
bool imgui_debugger_enabled = false;
192
bool disable_emu_cmd_keys = false;
193
bool log_keyboard = false;
194
bool log_video = false;
195
bool save_on_exit = false;
196
197
echo_mode_t echo_mode = ECHO_MODE_NONE;
198
gif_recorder_state_t record_gif = RECORD_GIF_PAUSE;