-
Notifications
You must be signed in to change notification settings - Fork 0
/
bup_header.h
386 lines (338 loc) · 10.6 KB
/
bup_header.h
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* bup_header.h - structure definition for the .BUP file header used
* by various Sega Saturn tools.
*
* Format developed by Cafe-Alpha (https://segaxtreme.net/threads/save-game-metadata-questions.24625/post-178534)
*
* Date conversion functions taken from Cafe-Alpha's Pseudo Saturn Kai
*
*/
/** .BUP extension is required to work wih PS Kai */
#define BUP_EXTENSION ".BUP"
/** The BUP header structure (sizeof(vmem_bup_header_t)) is exactly 64 bytes */
#define BUP_HEADER_SIZE 64
/** BUP header magic */
#define VMEM_MAGIC_STRING_LEN 4
#define VMEM_MAGIC_STRING "Vmem"
/** Max backup filename length */
#define JO_BACKUP_MAX_FILENAME_LENGTH (12)
/** Max backup file comment length */
#define JO_BACKUP_MAX_COMMENT_LENGTH (10)
/*
* Language of backup save in jo_backup_file
* taken from Jo Engine backup.c
*/
enum jo_backup_language
{
backup_japanese = 0,
backup_english = 1,
backup_french = 2,
backup_deutsch = 3,
backup_espanol = 4,
backup_italiano = 5,
};
/**
* Backup file metadata information. Taken from Jo Engine.
**/
typedef struct _jo_backup_file
{
unsigned char filename[JO_BACKUP_MAX_FILENAME_LENGTH];
unsigned char comment[JO_BACKUP_MAX_COMMENT_LENGTH + 1];
unsigned char language; // jo_backup_language
unsigned int date;
unsigned int datasize;
unsigned short blocksize;
unsigned short padding;
} jo_backup_file;
typedef struct _jo_backup_date {
unsigned char year; // year - 1980
unsigned char month;
unsigned char day;
unsigned char time;
unsigned char min;
unsigned char week;
} jo_backup_date;
#if defined( __GNUC__ )
#pragma pack(1)
#else
#pragma pack(push,1)
#endif
/**
* Vmem usage statistics structure.
* Statistics are reset on each vmem session, ie when Saturn is reset,
* or when game calls BUP_Init function.
**/
typedef struct _vmem_bup_stats_t
{
/* Number of times BUP_Dir function is called. */
unsigned char dir_cnt;
/* Number of times BUP_Read function is called. */
unsigned char read_cnt;
/* Number of times BUP_Write function is called. */
unsigned char write_cnt;
/* Number of times BUP_Verify function is called. */
unsigned char verify_cnt;
} vmem_bup_stats_t;
/**
* Backup data header. Multibyte values are stored as big-endian.
**/
typedef struct _vmem_bup_header_t
{
/* Magic string.
* Used in order to verify that file is in vmem format.
*/
char magic[VMEM_MAGIC_STRING_LEN];
/* Save ID.
* "Unique" ID for each save data file, the higher, the most recent.
*/
unsigned int save_id;
/* Vmem usage statistics. */
vmem_bup_stats_t stats;
/* Unused, kept for future use. */
char unused1[8 - sizeof(vmem_bup_stats_t)];
/* Backup Data Informations Record (34 bytes + 2 padding bytes). */
jo_backup_file dir;
/* Date stamp, in Saturn's BUP library format.
* Used in order to verify which save data is the most
* recent one when rebuilding index data.
* Note #1 : this information is already present in BupDir structure,
* but games are setting it, so it may be incorrect (typically, set
* to zero).
* Note #2 : this date information is the date when Pseudo Saturn Kai
* last started, not the time the save was saved, so if information in
* dir structure is available, it is more accurate.
*/
unsigned int date;
/* Unused, kept for future use. */
char unused2[8];
} vmem_bup_header_t;
#if defined( __GNUC__ )
#pragma pack()
#else
#pragma pack(pop)
#endif
//
// Functions for converting dates
//
void bup_getdate(unsigned int date, jo_backup_date *tb);
unsigned int bup_setdate(jo_backup_date *tb);
/*
* Format void BUP_GetDate(unsigned int date, jo_backup_date *date)
* Input pdate : data and time data of directory information
* date : date and time table
* Output none
* Function value none
* Function Expands the date and time data in the directory information table.
*/
void bup_getdate(unsigned int date, jo_backup_date *tb)
{
unsigned long div;
/* Set minute count. */
tb->min = (unsigned char)(date % 60);
/* Set hour. */
tb->time = (unsigned char)((date % (60*24)) / 60);
/* Compute days count. */
div = date / (60*24);
/* Set of week. */
if (div > 0xAB71)
{
tb->week = (unsigned char)((div + 1) % 7);
}
else
{
tb->week = (unsigned char)((div + 2) % 7);
}
/* To process leap year, simply consider a pack of 4 years whose first one
* is a multiple of 4, and loop up to 48 months until getting remaining
* days count fitting into one month.
*
* It's a bit kind of naive (not to say dumb) implementation, but it works,
* and as it's not the kind of routine that require heavy optimization
* there's no plan to make this algorithm smarter.
*/
unsigned long year_base = div / ((365*4) + 1);
year_base = year_base * 4;
unsigned long days_remain = div % ((365*4) + 1);
const char days_count[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
unsigned char month = 0;
int i;
for(i=0; i<(4*12); i++)
{
unsigned char days_per_month = days_count[i % 12];
if(i == 1)
{
days_per_month++;
}
if(days_remain < days_per_month)
{
break;
}
days_remain -= days_per_month;
month++;
if((i % 12) == 11)
{
month = 0;
year_base++;
}
}
tb->month = month + 1;
tb->day = days_remain + 1;
tb->year = year_base;
/* Code below is here to reproduce the same bug as in Saturn BIOS,
* so please disable it if you prefer a valid date rather than a
* completely equivalent re-implementation.
*
* Description : 1st January following a leap year is incorrectly
* returned as December 32nd of this leap year.
*
* Examples :
* >Error2 # 1, BIO:1980/12/32 00:42 4, B:00080AEA
* > , VMM:1981/01/01 00:42 4
* > , ORG:1981/01/01 00:42
* >Error2 # 2, BIO:1980/12/32 07:42 4, B:00080C8E
* > , VMM:1981/01/01 07:42 4
* > , ORG:1981/01/01 07:42
* >Error2 # 5, BIO:1984/12/32 00:42 2, B:0028250A
* > , VMM:1985/01/01 00:42 2
* > , ORG:1985/01/01 00:42
*
* Note : it is preferable that the implementation here (vmem) should
* return a valid date rather than pushing mimic of BIOS behavior
* including its bugs.
*/
#if 0
if((tb->month == 1)
&& (tb->day == 1)
&& ((tb->year % 4) == 1))
{
tb->month = 12;
tb->day = 32;
tb->year--;
}
#endif
}
/*
* Format Uint32 BUP_SetDate(BupDate *date)
* Input date : date and time table
* Output none
* Function value Data in date and time data form in the directory information table.
* Function Compresses the date and time data in the directory information table.
*/
unsigned int bup_setdate(jo_backup_date *tb)
{
/* Table of elapsed days per month. */
const unsigned short monthtbl[11] =
{
31,
31+28,
31+28+31,
31+28+31+30,
31+28+31+30+31,
31+28+31+30+31+30,
31+28+31+30+31+30+31,
31+28+31+30+31+30+31+31,
31+28+31+30+31+30+31+31+30,
31+28+31+30+31+30+31+31+30+31,
31+28+31+30+31+30+31+31+30+31+30
};
unsigned long date;
unsigned char data;
unsigned long remainder;
/* Easter Egg when specified time is set to invalid null values
*
* Technical details on SegaXtreme forums :
* https://segaxtreme.net/threads/backup-memory-structure.16803/
*
* antime : If you take the 1980/0/0 value and go backwards from
* 1980/1/1 you get 1963/10/8. I wonder if that's an
* easter egg - the programmer's birthday or something similar.
* TascoDLX : The value in question, 0x008246A0, corresponds to
* 1996/3/26 0:00, which pretty much coincides with the
* passing of Comet Hyakutake.
* As far as easter eggs go, that'd be my guess.
* antime : The comet wasn't discovered until January 1996.
* That'd be hell of an easter egg...
*
* Note : in this re-implementation, the value before adding this
* easter egg was : t:0x026CECE0 -> 2057/05/15 00:00
* Probably something caused by random value read outside range,
* or indicating the passing of another new comet ?!
*/
if((tb->year == 0)
&& (tb->month == 0)
&& (tb->day == 0)
&& (tb->time == 0)
&& (tb->min == 0))
{
return 0x008246A0;
}
/* Add year to result. */
data = tb->year;
date = (data / 4) * 0x5B5; // 0x5B5 = 365.25 * 4
remainder = data % 4;
if(remainder)
{
date += (remainder * 0x16D) + 1; // 0x16D = 365
}
/* Leap year fix.
* Code from Yabause HLE BIOS seems broken regarding leap years support.
*
* Rather than making a "clean" fix, and also because I'm not smart enough
* to understand the "365.25 * 4" code above, date is adjusted in cases
* causing problems.
*
* Additionally, I don't want to spend days in optimizing that : writing
* accurate date manipulation routines is some kind of art that may chew
* a lot of free time that would be better being dedicated in more
* interesting projects.
*
* Fix is verified under test bench implemented in Save Data Manager's
* extra menu which compares result between BIOS and this custom BUP_SetDate
* functions, so that it should behave fine in any possible case until 2199.
*/
unsigned short year = 1980 + tb->year;
int leap_year;
if((year % 4) != 0)
{
leap_year = 0;
}
else if((year % 100) != 0)
{
leap_year = 1;
}
else if((year % 400) != 0)
{
leap_year = 0;
}
else
{
leap_year = 1;
}
date--;
if((leap_year) && (tb->month == 2))
{
date--;
}
if((year > 2000) && ((year % 100) == 0) && (tb->month == 2))
{
date--;
}
/* Add month to result. */
data = tb->month;
if(data != 1 && data < 13)
{
date += monthtbl[data - 2];
if(date > 2 && remainder == 0)
{
date++;
}
}
/* Add day to result. */
date += tb->day;
date *= 0x5A0;
/* Add hour to result. */
date += (tb->time * 0x3C);
/* Add minute to result. */
date += tb->min;
return date;
}