-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProSystem.cpp
341 lines (298 loc) · 9.75 KB
/
ProSystem.cpp
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// ----------------------------------------------------------------------------
// ___ ___ ___ ___ ___ ____ ___ _ _
// /__/ /__/ / / /__ /__/ /__ / /_ / |/ /
// / / \ /__/ ___/ ___/ ___/ / /__ / / emulator
//
// ----------------------------------------------------------------------------
// Copyright 2003, 2004 Greg Stanton
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
// ----------------------------------------------------------------------------
// ProSystem.cpp
// ----------------------------------------------------------------------------
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include "ProSystem.h"
#include "Sound.h"
#define PRO_SYSTEM_SOURCE "ProSystem.cpp"
#define PRO_SYSTEM_STATE_HEADER "PRO-SYSTEM STATE"
bool prosystem_active = false;
bool prosystem_paused = false;
word prosystem_frequency = 60;
byte prosystem_frame = 0;
word prosystem_scanlines = 262;
uint prosystem_cycles = 0;
// ----------------------------------------------------------------------------
// Reset
// ----------------------------------------------------------------------------
void prosystem_Reset( ) {
if(cartridge_IsLoaded( )) {
prosystem_paused = false;
prosystem_frame = 0;
region_Reset( );
tia_Clear( );
tia_Reset( );
pokey_Clear( );
pokey_Reset( );
memory_Reset( );
maria_Clear( );
maria_Reset( );
riot_Reset ( );
if(bios_enabled) {
bios_Store( );
}
else {
cartridge_Store( );
}
prosystem_cycles = sally_ExecuteRES( );
prosystem_active = true;
}
}
// ----------------------------------------------------------------------------
// ExecuteFrame
// ----------------------------------------------------------------------------
void prosystem_ExecuteFrame(const byte* input)
{
riot_SetInput(input);
for(maria_scanline = 1; maria_scanline <= prosystem_scanlines; maria_scanline++) {
if(maria_scanline == maria_displayArea.top) {
memory_ram[MSTAT] = 0;
}
if(maria_scanline == maria_displayArea.bottom) {
memory_ram[MSTAT] = 128;
}
uint cycles;
prosystem_cycles %= 456;
while(prosystem_cycles < 28) {
cycles = sally_ExecuteInstruction( );
prosystem_cycles += (cycles << 2);
if(riot_timing) {
riot_UpdateTimer(cycles);
}
if(memory_ram[WSYNC] && !(cartridge_flags & CARTRIDGE_WSYNC_MASK)) {
prosystem_cycles = 456;
memory_ram[WSYNC] = false;
break;
}
}
cycles = maria_RenderScanline( );
if(cartridge_flags & CARTRIDGE_CYCLE_STEALING_MASK) {
prosystem_cycles += cycles;
}
while(prosystem_cycles < 456) {
cycles = sally_ExecuteInstruction( );
prosystem_cycles += (cycles << 2);
if(riot_timing) {
riot_UpdateTimer(cycles);
}
if(memory_ram[WSYNC] && !(cartridge_flags & CARTRIDGE_WSYNC_MASK)) {
prosystem_cycles = 456;
memory_ram[WSYNC] = false;
break;
}
}
tia_Process(2);
if(cartridge_pokey) {
pokey_Process(2);
}
}
prosystem_frame++;
if(prosystem_frame >= prosystem_frequency) {
prosystem_frame = 0;
}
}
byte *loc_buffer = 0;
// ----------------------------------------------------------------------------
// Save
// ----------------------------------------------------------------------------
bool prosystem_Save(std::string filename, bool compress) {
if(filename.empty( ) || filename.length( ) == 0) {
logger_LogError("Filename is invalid.", PRO_SYSTEM_SOURCE);
return false;
}
if (! loc_buffer) loc_buffer = (byte *)malloc(33000 * sizeof(byte));
logger_LogInfo("Saving game state to file " + filename + ".");
uint size = 0;
uint index;
for(index = 0; index < 16; index++) {
loc_buffer[size + index] = PRO_SYSTEM_STATE_HEADER[index];
}
size += 16;
loc_buffer[size++] = 1;
for(index = 0; index < 4; index++) {
loc_buffer[size + index] = 0;
}
size += 4;
for(index = 0; index < 32; index++) {
loc_buffer[size + index] = cartridge_digest[index];
}
size += 32;
loc_buffer[size++] = sally_a;
loc_buffer[size++] = sally_x;
loc_buffer[size++] = sally_y;
loc_buffer[size++] = sally_p;
loc_buffer[size++] = sally_s;
loc_buffer[size++] = sally_pc.b.l;
loc_buffer[size++] = sally_pc.b.h;
loc_buffer[size++] = cartridge_bank;
for(index = 0; index < 16384; index++) {
loc_buffer[size + index] = memory_ram[index];
}
size += 16384;
if(cartridge_type == CARTRIDGE_TYPE_SUPERCART_RAM) {
for(index = 0; index < 16384; index++) {
loc_buffer[size + index] = memory_ram[16384 + index];
}
size += 16384;
}
if(!compress) {
FILE* file = fopen(filename.c_str( ), "wb");
if(file == NULL) {
logger_LogError("Failed to open the file " + filename + " for writing.", PRO_SYSTEM_SOURCE);
return false;
}
if(fwrite(loc_buffer, 1, size, file) != size) {
fclose(file);
logger_LogError("Failed to write the save state data to the file " + filename + ".", PRO_SYSTEM_SOURCE);
return false;
}
fclose(file);
}
else {
if(!archive_Compress(filename.c_str( ), "Save.sav", loc_buffer, size)) {
logger_LogError("Failed to compress the save state data to the file " + filename + ".", PRO_SYSTEM_SOURCE);
return false;
}
}
return true;
}
// ----------------------------------------------------------------------------
// Load
// ----------------------------------------------------------------------------
bool prosystem_Load(const std::string filename) {
if(filename.empty( ) || filename.length( ) == 0) {
logger_LogError("Filename is invalid.", PRO_SYSTEM_SOURCE);
return false;
}
if (! loc_buffer) loc_buffer = (byte *)malloc(33000 * sizeof(byte));
logger_LogInfo("Loading game state from file " + filename + ".");
uint size = archive_GetUncompressedFileSize(filename);
if(size == 0) {
FILE* file = fopen(filename.c_str( ), "rb");
if(file == NULL) {
logger_LogError("Failed to open the file " + filename + " for reading.", PRO_SYSTEM_SOURCE);
return false;
}
if(fseek(file, 0, SEEK_END)) {
fclose(file);
logger_LogError("Failed to find the end of the file.", PRO_SYSTEM_SOURCE);
return false;
}
size = ftell(file);
if(fseek(file, 0, SEEK_SET)) {
fclose(file);
logger_LogError("Failed to find the size of the file.", PRO_SYSTEM_SOURCE);
return false;
}
if(size != 16445 && size != 32829) {
fclose(file);
logger_LogError("Save state file has an invalid size.", PRO_SYSTEM_SOURCE);
return false;
}
if(fread(loc_buffer, 1, size, file) != size && ferror(file)) {
fclose(file);
logger_LogError("Failed to read the file data.", PRO_SYSTEM_SOURCE);
return false;
}
fclose(file);
}
else if(size == 16445 || size == 32829) {
archive_Uncompress(filename, loc_buffer, size);
}
else {
logger_LogError("Save state file has an invalid size.", PRO_SYSTEM_SOURCE);
return false;
}
uint offset = 0;
uint index;
for(index = 0; index < 16; index++) {
if(loc_buffer[offset + index] != PRO_SYSTEM_STATE_HEADER[index]) {
logger_LogError("File is not a valid ProSystem save state.", PRO_SYSTEM_SOURCE);
return false;
}
}
offset += 16;
byte version = loc_buffer[offset++];
uint date = 0;
for(index = 0; index < 4; index++) {
}
offset += 4;
prosystem_Reset( );
char digest[33] = {0};
for(index = 0; index < 32; index++) {
digest[index] = loc_buffer[offset + index];
}
offset += 32;
if(cartridge_digest != std::string(digest)) {
logger_LogError("Load state digest [" + std::string(digest) + "] does not match loaded cartridge digest [" + cartridge_digest + "].", PRO_SYSTEM_SOURCE);
return false;
}
sally_a = loc_buffer[offset++];
sally_x = loc_buffer[offset++];
sally_y = loc_buffer[offset++];
sally_p = loc_buffer[offset++];
sally_s = loc_buffer[offset++];
sally_pc.b.l = loc_buffer[offset++];
sally_pc.b.h = loc_buffer[offset++];
cartridge_StoreBank(loc_buffer[offset++]);
for(index = 0; index < 16384; index++) {
memory_ram[index] = loc_buffer[offset + index];
}
offset += 16384;
if(cartridge_type == CARTRIDGE_TYPE_SUPERCART_RAM) {
if(size != 32829) {
logger_LogError("Save state file has an invalid size.", PRO_SYSTEM_SOURCE);
return false;
}
for(index = 0; index < 16384; index++) {
memory_ram[16384 + index] = loc_buffer[offset + index];
}
offset += 16384;
}
return true;
}
// ----------------------------------------------------------------------------
// Pause
// ----------------------------------------------------------------------------
void prosystem_Pause(bool pause) {
if(prosystem_active) {
prosystem_paused = pause;
}
}
// ----------------------------------------------------------------------------
// Close
// ----------------------------------------------------------------------------
void prosystem_Close( ) {
prosystem_active = false;
prosystem_paused = false;
cartridge_Release( );
maria_Reset( );
maria_Clear( );
memory_Reset( );
tia_Reset( );
tia_Clear( );
}