forked from jeremyrayner/beebem-rg350
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uefstate.cc
293 lines (236 loc) · 8.42 KB
/
uefstate.cc
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
// UEF Game state code for BeebEm V1.38
// Note - This version will be the last of the V1.3 series.
// After that, BeebEm the V1.4 Series will commence.
// (C) June 2001 Richard Gellman
//==#if HAVE_CONFIG_H
//==# include <config.h>
//==#endif
#include <stdio.h>
#include <iostream>
#include "6502core.h"
#include "beebmem.h"
#include "video.h"
#include "via.h"
#include "beebwin.h"
#include "main.h"
#include "beebsound.h"
#include "disc8271.h"
//==#include "disc1770.h"
//==#include "tube.h"
//==#include "serial.h"
#include "beebconfig.h"
#include "messagebox.h"
#include "progressdialog.h"
#include "messages.h"
#include "sysvia.h"
using namespace std;
FILE *UEFState;
void fput32(unsigned int word32,FILE *fileptr) {
fputc(word32&255,fileptr);
fputc((word32>>8)&255,fileptr);
fputc((word32>>16)&255,fileptr);
fputc((word32>>24)&255,fileptr);
}
void fput16(unsigned int word16,FILE *fileptr) {
fputc(word16&255,fileptr);
fputc((word16>>8)&255,fileptr);
}
unsigned int fget32(FILE *fileptr) {
unsigned int tmpvar;
tmpvar =fgetc(fileptr);
tmpvar|=fgetc(fileptr)<<8;
tmpvar|=fgetc(fileptr)<<16;
tmpvar|=fgetc(fileptr)<<24;
return(tmpvar);
}
unsigned int fget16(FILE *fileptr) {
unsigned int tmpvar;
tmpvar =fgetc(fileptr);
tmpvar|=fgetc(fileptr)<<8;
return(tmpvar);
}
void SaveEmuUEF(FILE *SUEF) {
char EmuName[16]="...............";
fput16(0x046C,SUEF);
fput32(16,SUEF);
// BeebEm Title Block
strcpy(EmuName,"BeebEm-GP2X");
EmuName[14]=0;
EmuName[15]=5; // Version, 1.4
fwrite(EmuName,16,1,SUEF);
//
fput16(0x046a,SUEF);
fput32(16,SUEF);
// Emulator Specifics
// Note about this block: It should only be handled by beebem from uefstate.cpp if
// the UEF has been determined to be from BeebEm (Block 046C)
//== fputc(MachineType,SUEF);
fputc(0,SUEF);
//== fputc((NativeFDC)?0:1,SUEF);
fputc(0, SUEF);
//== fputc(TubeEnabled,SUEF);
fputc(0, SUEF);
fputc(0,SUEF); // Monitor type, reserved
fputc(0,SUEF); // Speed Setting, reserved
fput32(0,SUEF);
fput32(0,SUEF);
fput16(0,SUEF);
fputc(0,SUEF);
}
void LoadEmuUEF(FILE *SUEF, int Version) {
int MachineType, NativeFDC, TubeEnabled;
MachineType=fgetc(SUEF);
//== if (Version <= 8 && MachineType == 1)
//== MachineType = 3;
NativeFDC=(fgetc(SUEF)==0)?TRUE:FALSE;
TubeEnabled=fgetc(SUEF);
//== mainWin->ResetBeebSystem(MachineType,TubeEnabled,1);
//== mainWin->UpdateModelType();
// [TODO] Shouldn't there really be a function to reset...
Init6502core();
Disc8271_reset();
}
#define SAVE_STEP_INC (1.0 / 7.0)
#define SAVE_UPDATE_PD() {ProgressDialog_SetPosition(pd_p, step+=SAVE_STEP_INC); SDL_Delay(100);}
extern SDL_Surface *frame_buffer_p;
void SaveUEFState(char *StateName) {
ProgressDialog *pd_p;
float step = 0;
/* If file exists prompt for over-write
*/
UEFState=fopen(StateName, "rb");
if (UEFState != NULL) {
fclose(UEFState);
char errstr[200];
sprintf(errstr, UEFSTATE_OVERWRITESTATEIMAGE_M, StateName);
if (EG_MessageBox(frame_buffer_p, EG_MESSAGEBOX_QUESTION, UEFSTATE_OVERWRITESTATEIMAGE_T, errstr, "Yes", "No", NULL, NULL, 2) == 2) {
EG_MessageBox(frame_buffer_p, EG_MESSAGEBOX_INFORMATION, UEFSTATE_CANCELLEDOVERWRITE_T
, UEFSTATE_CANCELLEDOVERWRITE_M, "Continue", NULL, NULL, NULL, 0);
return;
}
}
UEFState=fopen(StateName,"wb");
if (UEFState != NULL)
{
pd_p=ProgressDialog_Create(frame_buffer_p, "Saving current emulator state", 0);
ProgressDialog_Show(pd_p);
fprintf(UEFState,"UEF File!");
fputc(0,UEFState); // UEF Header
fputc(10,UEFState); fputc(0,UEFState); // Version
SaveEmuUEF(UEFState); SAVE_UPDATE_PD();
Save6502UEF(UEFState); SAVE_UPDATE_PD();
SaveMemUEF(UEFState); SAVE_UPDATE_PD();
SaveVideoUEF(UEFState); SAVE_UPDATE_PD();
SaveVIAUEF(UEFState); SAVE_UPDATE_PD();
SaveSoundUEF(UEFState); SAVE_UPDATE_PD();
//== if (MachineType!=3 && NativeFDC)
Save8271UEF(UEFState); SAVE_UPDATE_PD();
//== else
//== Save1770UEF(UEFState);
//== if (EnableTube) {
//== SaveTubeUEF(UEFState);
//== Save65C02UEF(UEFState);
//== Save65C02MemUEF(UEFState);
//== }
//== SaveSerialUEF(UEFState);
fclose(UEFState);
sync();
SDL_Delay(500);
ProgressDialog_Free(pd_p);
EG_MessageBox(frame_buffer_p, EG_MESSAGEBOX_INFORMATION, UEFSTATE_SAVESTATESUCCESSFUL_T, UEFSTATE_SAVESTATESUCCESSFUL_M, "Continue", NULL, NULL, NULL, 0);
}
else
{
char errstr[256];
sprintf(errstr, UEFSTATE_SAVESTATEFAILED_M, StateName);
//== MessageBox(GETHWND,errstr,"BeebEm",MB_ICONERROR|MB_OK);
cerr << "Failed to write state file: "; // << StateName << "\n";
EG_MessageBox(frame_buffer_p, EG_MESSAGEBOX_STOP, UEFSTATE_SAVESTATEFAILED_T, errstr, "Continue", NULL, NULL, NULL, 0);
}
}
void LoadUEFState(char *StateName) {
char errmsg[256];
char UEFId[10];
//-- int CompletionBits=0; // These bits should be filled in
//## Unused.
long RPos=0,FLength,CPos;
unsigned int Block,Length;
int Version;
// ProgressDialog *pd_p;
strcpy(UEFId,"BlankFile");
if (mainWin!=NULL) mainWin->Reset();
printf("Loading UEF State...\n");
sync();
UEFState=fopen(StateName,"rb");
if (UEFState != NULL)
{
//-> fseek(UEFState,NULL,SEEK_END);
//++
fseek(UEFState,0,SEEK_END);
//<-
FLength=ftell(UEFState);
fseek(UEFState,0,SEEK_SET); // Get File length for eof comparison.
fread(UEFId,10,1,UEFState);
if (strcmp(UEFId,"UEF File!")!=0) {
//== MessageBox(GETHWND,"The file selected is not a UEF File.","BeebEm",MB_ICONERROR|MB_OK);
EG_MessageBox(frame_buffer_p, EG_MESSAGEBOX_STOP, UEFSTATE_FILEFORMATINCORRECT_T, UEFSTATE_FILEFORMATINCORRECT_M, "Continue", NULL, NULL, NULL, 0);
cerr << "The file selected is not a UEF File.\n";
fclose(UEFState);
return;
}
Version=fget16(UEFState);
//== sprintf(errmsg,"UEF Version %x",Version);
//== //MessageBox(GETHWND,errmsg,"BeebEm",MB_OK);
printf("UEF Version %d\n", Version);
// SDL_Delay(5000);
RPos=ftell(UEFState);
// pd_p=ProgressDialog_Create(frame_buffer_p, "Loading previously saved emulator state", -1);
// ProgressDialog_Show(pd_p);
while (ftell(UEFState)<FLength) {
Block=fget16(UEFState);
Length=fget32(UEFState);
CPos=ftell(UEFState);
sprintf(errmsg,"Block %04X - Length %d (%04X)",Block,Length,Length);
//MessageBox(GETHWND,errmsg,"BeebEm",MB_ICONERROR|MB_OK);
if (Block==0x046A) { printf("A\n"); LoadEmuUEF(UEFState,Version); }
if (Block==0x0460) { printf("B\n"); Load6502UEF(UEFState); }
if (Block==0x0461) { printf("C\n"); LoadRomRegsUEF(UEFState); }
if (Block==0x0462) { printf("D\n"); LoadMainMemUEF(UEFState); }
//if (Block==0x0463) LoadShadMemUEF(UEFState);
//if (Block==0x0464) LoadPrivMemUEF(UEFState);
//if (Block==0x0465) LoadFileMemUEF(UEFState);
if (Block==0x0466) { printf("E\n"); LoadSWRMMemUEF(UEFState); }
if (Block==0x0467) { printf("F\n"); LoadViaUEF(UEFState); }
if (Block==0x0468) { printf("G\n"); LoadVideoUEF(UEFState); }
if (Block==0x046B) { printf("H\n"); LoadSoundUEF(UEFState); }
//== if (Block==0x046D) LoadIntegraBHiddenMemUEF(UEFState);
if (Block==0x046E) { printf("I\n"); Load8271UEF(UEFState); }
//== if (Block==0x046F) Load1770UEF(UEFState,Version);
//== if (Block==0x0470) LoadTubeUEF(UEFState);
//== if (Block==0x0471) Load65C02UEF(UEFState);
//== if (Block==0x0472) Load65C02MemUEF(UEFState);
//== if (Block==0x0473) LoadSerialUEF(UEFState);
fseek(UEFState,CPos+Length,SEEK_SET); // Skip unrecognised blocks (and over any gaps)
}
fclose(UEFState);
// SDL_Delay(1500);
// ProgressDialog_Free(pd_p);
//== mainWin->SetRomMenu();
//== mainWin->SetDiscWriteProtects();
}
else
{
char errstr[256];
sprintf(errstr, UEFSTATE_CANNOTOPENFILE_M, StateName);
//== MessageBox(GETHWND,errstr,"BeebEm",MB_ICONERROR|MB_OK);
cerr << "Cannot open state file: " << StateName << "\n";
EG_MessageBox(frame_buffer_p, EG_MESSAGEBOX_STOP, UEFSTATE_CANNOTOPENFILE_T, errstr, "Continue", NULL, NULL, NULL, 0);
}
/* Remap the keys:
*/
Config_LoadDiscConfig( GetDiscImagePath(0) );
/* Make sure shift-booting is off:
*/
mainWin->ShiftBooted = false;
BeebKeyUp(0, 0);
}