-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrum.c
317 lines (251 loc) · 8.72 KB
/
spectrum.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* spectrum.c: Generic Spectrum routines
Copyright (c) 1999-2016 Philip Kendall, Darren Salt
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: [email protected]
*/
#include <config.h>
#include <libspectrum.h>
#include "compat.h"
#include "debugger/debugger.h"
#include "display.h"
#include "event.h"
#include "keyboard.h"
#include "infrastructure/startup_manager.h"
#include "loader.h"
#include "machine.h"
#include "memory_pages.h"
#include "module.h"
#include "peripherals/printer.h"
#include "peripherals/ula.h"
#include "phantom_typist.h"
#include "psg.h"
#include "profile.h"
#include "rzx.h"
#include "settings.h"
#include "sound.h"
#include "spectrum.h"
#include "tape.h"
#include "timer/timer.h"
#include "ui/ui.h"
#include "ui/uijoystick.h"
#include "z80/z80.h"
#ifdef FSEMU
#include "fsfuse/fsfuse.h"
#endif
/* 1040 KB of RAM */
libspectrum_byte RAM[ SPECTRUM_RAM_PAGES ][0x4000];
/* How many tstates have elapsed since the last interrupt? (or more
precisely, since the ULA last pulled the /INT line to the Z80 low) */
libspectrum_dword tstates;
/* Contention patterns */
static int contention_pattern_65432100[] = { 5, 4, 3, 2, 1, 0, 0, 6 };
static int contention_pattern_76543210[] = { 5, 4, 3, 2, 1, 0, 7, 6 };
/* Event */
int spectrum_frame_event;
/* Debugger variable prefix */
static const char * const debugger_type_string = "spectrum";
/* Debugger variable for frame count */
static const char * const frame_count_name = "frames";
/* Count of frames since last reset */
static libspectrum_dword frames_since_reset;
static void
spectrum_reset( int hard_reset )
{
frames_since_reset = 0;
}
static module_info_t module_info = {
/* .reset = */ spectrum_reset,
/* .romcs = */ NULL,
/* .snapshot_enabled = */ NULL,
/* .snapshot_from = */ NULL,
/* .snapshot_to = */ NULL
};
static void
spectrum_frame_event_fn( libspectrum_dword last_tstates, int type,
void *user_data )
{
if( rzx_playback ) event_force_events();
rzx_frame();
psg_frame();
spectrum_frame();
z80_interrupt();
ui_joystick_poll();
timer_estimate_speed();
debugger_add_time_events();
ui_event();
ui_error_frame();
}
static libspectrum_dword
get_frame_count( void )
{
return frames_since_reset;
}
static int
spectrum_init( void *context )
{
spectrum_frame_event = event_register( spectrum_frame_event_fn,
"End of frame" );
module_register( &module_info );
debugger_system_variable_register( debugger_type_string,
frame_count_name, get_frame_count, NULL );
return 0;
}
void
spectrum_register_startup( void )
{
startup_manager_module dependencies[] = {
STARTUP_MANAGER_MODULE_DEBUGGER,
STARTUP_MANAGER_MODULE_EVENT,
STARTUP_MANAGER_MODULE_SETUID,
};
startup_manager_register( STARTUP_MANAGER_MODULE_SPECTRUM, dependencies,
ARRAY_SIZE( dependencies ), spectrum_init, NULL,
NULL );
}
int
spectrum_frame( void )
{
#ifdef FSEMU
fsemu_frame_log_epoch("spectrum_frame\n");
#endif
libspectrum_dword frame_length;
/* Reduce the t-state count of both the processor and all the events
scheduled to occur. Done slightly differently if RZX playback is
occurring */
frame_length = rzx_playback ? tstates
: machine_current->timings.tstates_per_frame;
event_frame( frame_length );
debugger_breakpoint_reduce_tstates( frame_length );
tstates -= frame_length;
if( z80.interrupts_enabled_at >= 0 )
z80.interrupts_enabled_at -= frame_length;
#ifdef FSEMU
fsemu_frame_log_epoch("done emulating?\n");
#endif
if( sound_enabled ) sound_frame();
if( display_frame() ) return 1;
if( profile_active ) profile_frame( frame_length );
printer_frame();
/* Add an interrupt unless they're being generated by .rzx playback */
if( !rzx_playback )
event_add( machine_current->timings.tstates_per_frame,
spectrum_frame_event );
loader_frame( frame_length );
phantom_typist_frame();
frames_since_reset++;
#ifdef FSEMU
fsemu_frame_log_epoch("spectrum_frame end\n");
#endif
return 0;
}
libspectrum_byte
spectrum_contend_delay_none( libspectrum_dword time )
{
return 0;
}
static libspectrum_byte
contend_delay_common( libspectrum_dword time, int* timings, int offset )
{
int line, tstates_through_line;
line =
(libspectrum_signed_dword)( time - machine_current->line_times[ 0 ] ) /
machine_current->timings.tstates_per_line;
/* Work out where we are in this line, remembering that line_times[0] holds
the first pixel we display, not the start of where the Spectrum produced
the left border */
tstates_through_line = time - machine_current->line_times[ 0 ] +
( machine_current->timings.left_border - DISPLAY_BORDER_WIDTH_COLS * 4 );
tstates_through_line %= machine_current->timings.tstates_per_line;
/* No contention in the upper and lower borders */
if( line < DISPLAY_BORDER_HEIGHT ||
line >= DISPLAY_BORDER_HEIGHT + DISPLAY_HEIGHT ) return 0;
/* Or in the left border */
if( tstates_through_line < machine_current->timings.left_border - offset )
return 0;
/* Or the right border or retrace */
if( tstates_through_line >= machine_current->timings.left_border +
machine_current->timings.horizontal_screen -
offset )
return 0;
/* We now know the ULA is reading the screen, so put in the appropriate
delay */
return timings[ tstates_through_line % 8 ];
}
libspectrum_byte
spectrum_contend_delay_65432100( libspectrum_dword time )
{
return contend_delay_common( time, contention_pattern_65432100, 1 );
}
libspectrum_byte
spectrum_contend_delay_76543210( libspectrum_dword time )
{
return contend_delay_common( time, contention_pattern_76543210, 4 );
}
/* What happens if we read from an unattached port? */
libspectrum_byte
spectrum_unattached_port( void )
{
int line, tstates_through_line, column;
/* Return 0xff (idle bus) if we're in the top border */
if( tstates < machine_current->line_times[ DISPLAY_BORDER_HEIGHT ] )
return 0xff;
/* Work out which line we're on, relative to the top of the screen */
line = ( (libspectrum_signed_dword)tstates -
machine_current->line_times[ DISPLAY_BORDER_HEIGHT ] ) /
machine_current->timings.tstates_per_line;
/* Idle bus if we're in the lower border */
if( line >= DISPLAY_HEIGHT ) return 0xff;
/* Work out where we are in this line, remembering that line_times[] holds
the first pixel we display, not the start of where the Spectrum produced
the left border */
tstates_through_line = tstates -
machine_current->line_times[ DISPLAY_BORDER_HEIGHT + line ] +
( machine_current->timings.left_border - DISPLAY_BORDER_WIDTH_COLS * 4 );
/* Idle bus if we're in the left border */
if( tstates_through_line < machine_current->timings.left_border )
return 0xff;
/* Or the right border or retrace */
if( tstates_through_line >= machine_current->timings.left_border +
machine_current->timings.horizontal_screen )
return 0xff;
column = ( ( tstates_through_line -
machine_current->timings.left_border ) / 8 ) * 2;
switch( tstates_through_line % 8 ) {
/* The pattern of bytes returned here is the same as documented by
Ramsoft in their 'Floating bus technical guide' at
http://web.archive.org/web/20080509193736/http://www.ramsoft.bbk.org/floatingbus.html
However, the timings used are based on the first byte being
returned at 14338 (48K) and 14364 (128K) respectively, not
14347 and 14368 as used by Ramsoft.
In contrast to previous versions of this code, Arkanoid and
Sidewize now work. */
/* Attribute bytes */
case 5: column++;
case 3:
return RAM[ memory_current_screen ][ display_attr_start[line] + column ];
/* Screen data */
case 4: column++;
case 2:
return RAM[ memory_current_screen ][ display_line_start[line] + column ];
/* Idle bus */
case 0: case 1: case 6: case 7:
return 0xff;
}
return 0; /* Keep gcc happy */
}
libspectrum_byte
spectrum_unattached_port_none( void )
{
return 0xff;
}