Skip to content

Commit

Permalink
Big endian fixes - part 2 (#405)
Browse files Browse the repository at this point in the history
* Big endian fix for S3LoadSample and Win32SetPaletteEntries

* Add the missing BR_ENDIAN_BIG guard to S3LoadSample

Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>

* DRPixelmapDoubledCopy big endian fix

* big endian fix for the KEYBOARD.COK loading

* CalcLSChecksum big endian fix

* PrintScreenFile big endian fix

---------

Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>
  • Loading branch information
BSzili and madebr authored Oct 14, 2024
1 parent 78ef3c2 commit 2236974
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/DETHRACE/common/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -3147,8 +3147,13 @@ void DRPixelmapDoubledCopy(br_pixelmap* pDestn, br_pixelmap* pSource, int pSourc
for (j = 0; j < width_over_2; j++) {
--sptr;
pixels = *sptr;
#if BR_ENDIAN_BIG
pixel_1 = pixels >> 0;
pixel_2 = pixels >> 8;
#else
pixel_1 = pixels >> 8;
pixel_2 = pixels >> 0;
#endif
dptr[-1] = pixel_1;
dptr2[-1] = pixel_1;
dptr[-2] = pixel_1;
Expand Down
36 changes: 36 additions & 0 deletions src/DETHRACE/common/loadsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ int gSave_allowed;
(V) = BrHtoNL(V); \
} while (0)

#define SWAP32_LE(V) \
do { \
(V) = BrHtoLL(V); \
} while (0)

// IDA: void __usercall CorrectLoadByteOrdering(int pIndex@<EAX>)
void CorrectLoadByteOrdering(int pIndex) {
int i;
Expand All @@ -56,6 +61,29 @@ void CorrectLoadByteOrdering(int pIndex) {
}
}

#if BR_ENDIAN_BIG
static void CorrectChecksumByteOrdering(tSave_game* pSaved_game) {
int i;

SWAP32_LE(pSaved_game->version);
SWAP32_LE(pSaved_game->rank);
SWAP32_LE(pSaved_game->credits);
SWAP32_LE(pSaved_game->skill_level);
SWAP32_LE(pSaved_game->frank_or_annitude);
SWAP32_LE(pSaved_game->game_completed);
SWAP32_LE(pSaved_game->current_race_index);
SWAP32_LE(pSaved_game->number_of_cars);
for (i = 0; i < COUNT_OF(pSaved_game->cars_available); i++) {
SWAP32_LE(pSaved_game->cars_available[i]);
}
SWAP32_LE(pSaved_game->current_car_index);
SWAP32_LE(pSaved_game->redo_race_index);
for (i = 0; i < COUNT_OF(pSaved_game->power_up_levels); i++) {
SWAP32_LE(pSaved_game->power_up_levels[i]);
}
}
#endif

// IDA: tU32 __usercall CalcLSChecksum@<EAX>(tSave_game *pSaved_game@<EAX>)
tU32 CalcLSChecksum(tSave_game* pSaved_game) {
tU32 checksum;
Expand All @@ -70,12 +98,20 @@ tU32 CalcLSChecksum(tSave_game* pSaved_game) {
}
#endif

#if BR_ENDIAN_BIG
CorrectChecksumByteOrdering(pSaved_game);
#endif
checksum = 0;
for (i = 0, ptr = (tU8*)pSaved_game; i < (sizeof(tSave_game) - sizeof(tU32)); i++, ptr++) {
checksum2 = (*ptr ^ 0xbd) + checksum;
checksum = checksum ^ checksum2 << 25 ^ checksum2 >> 7;
}
#if BR_ENDIAN_BIG
CorrectChecksumByteOrdering(pSaved_game);
return BrHtoLL(checksum);
#else
return checksum;
#endif
}

// IDA: void __cdecl LoadSavedGames()
Expand Down
6 changes: 6 additions & 0 deletions src/DETHRACE/common/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,15 @@ void PrintScreenFile(FILE* pF) {
// 3. Color table (=palette)
for (i = 0; i < 256; i++) {
// red, green, blue, unused
#if BR_ENDIAN_BIG
WriteU8L(pF, ((tU8*)gCurrent_palette->pixels)[4 * i + 3]);
WriteU8L(pF, ((tU8*)gCurrent_palette->pixels)[4 * i + 2]);
WriteU8L(pF, ((tU8*)gCurrent_palette->pixels)[4 * i + 1]);
#else
WriteU8L(pF, ((tU8*)gCurrent_palette->pixels)[4 * i]);
WriteU8L(pF, ((tU8*)gCurrent_palette->pixels)[4 * i + 1]);
WriteU8L(pF, ((tU8*)gCurrent_palette->pixels)[4 * i + 2]);
#endif
WriteU8L(pF, 0);
}

Expand Down
6 changes: 6 additions & 0 deletions src/DETHRACE/pc-dos/dossys.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ void PDInitialiseSystem(void) {
fread(gASCII_table, len / 2, 1, f);
fread(gASCII_shift_table, len / 2, 1, f);
fclose(f);
#if BR_ENDIAN_BIG
for (int i = 0; i < 128; i++) {
gASCII_table[i] = BrSwap32(gASCII_table[i]);
gASCII_shift_table[i] = BrSwap32(gASCII_shift_table[i]);
}
#endif
} else {
memcpy(gASCII_table, harness_game_info.defines.ascii_table, sizeof(gASCII_table));
memcpy(gASCII_shift_table, harness_game_info.defines.ascii_shift_table, sizeof(gASCII_shift_table));
Expand Down
12 changes: 12 additions & 0 deletions src/DETHRACE/pc-win95/win95sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,12 @@ void PDInitialiseSystem(void) {
fread(gASCII_table, len, 1u, f);
fread(gASCII_shift_table, len, 1u, f);
fclose(f);
#if BR_ENDIAN_BIG
for (int i = 0; i < 128; i++) {
gASCII_table[i] = BrSwap32(gASCII_table[i]);
gASCII_shift_table[i] = BrSwap32(gASCII_shift_table[i]);
}
#endif
}
Win32InitInputDevice();
}
Expand Down Expand Up @@ -719,9 +725,15 @@ void Win32SetPaletteEntries(uint8_t* entries, int pFirst_colour, int pCount) {
}
for (i = pFirst_colour; i <= last_colour; i++) {
gWin32_palette[i].peFlags = 0;
#if BR_ENDIAN_BIG
gWin32_palette[i].peRed = entries[i * 4 + 1];
gWin32_palette[i].peGreen = entries[i * 4 + 2];
gWin32_palette[i].peBlue = entries[i * 4 + 3];
#else
gWin32_palette[i].peRed = entries[i * 4 + 2];
gWin32_palette[i].peGreen = entries[i * 4 + 1];
gWin32_palette[i].peBlue = entries[i * 4];
#endif
}
SSDXSetPaleeteEntries(gWin32_palette, 0, 256);
}
Expand Down
7 changes: 7 additions & 0 deletions src/S3/s3sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ int S3LoadSample(tS3_sound_id id) {
if (memcmp(buf, "RIFF", 4) == 0) {
wav_header* hdr = (wav_header*)buf;
sample->dataptr = &buf[sizeof(wav_header)];
#if BR_ENDIAN_BIG
sample->size = BrSwap32(hdr->data_bytes);
sample->rate = BrSwap32(hdr->sample_rate);
sample->resolution = BrSwap16(hdr->bit_depth);
sample->channels = BrSwap16(hdr->num_channels);
#else
sample->size = hdr->data_bytes;
sample->rate = hdr->sample_rate;
sample->resolution = hdr->bit_depth;
sample->channels = hdr->num_channels;
#endif
} else {
sample->rate = 16000;
sample->resolution = 8;
Expand Down

0 comments on commit 2236974

Please sign in to comment.