Skip to content

Commit

Permalink
Support Big Endianness (at least for unions) and add a few tests to v…
Browse files Browse the repository at this point in the history
…erify it works fine. #190
  • Loading branch information
ColinPitrat committed Apr 1, 2021
1 parent 40ed93b commit 60f0963
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/cap32.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <unistd.h>
#include <string>
#include <vector>
#include "SDL_endian.h"

class InputMapper;
//#define DEBUG
Expand Down Expand Up @@ -212,8 +213,13 @@ typedef struct {
unsigned char *snd_bufferptr;
union {
struct {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
unsigned int low;
unsigned int high;
#else
unsigned int high;
unsigned int low;
#endif
};
int64_t both;
} snd_cycle_count_init;
Expand Down Expand Up @@ -338,8 +344,13 @@ typedef struct {
typedef struct {
union {
struct {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
unsigned int low;
unsigned int high;
#else
unsigned int high;
unsigned int low;
#endif
};
int64_t both;
} cycle_count;
Expand All @@ -349,13 +360,23 @@ typedef struct {
union {
unsigned char Index[16];
struct {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
unsigned char TonALo, TonAHi;
unsigned char TonBLo, TonBHi;
unsigned char TonCLo, TonCHi;
#else
unsigned char TonAHi, TonALo;
unsigned char TonBHi, TonBLo;
unsigned char TonCHi, TonCLo;
#endif
unsigned char Noise;
unsigned char Mixer;
unsigned char AmplitudeA, AmplitudeB, AmplitudeC;
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
unsigned char EnvelopeLo, EnvelopeHi;
#else
unsigned char EnvelopeHi, EnvelopeLo;
#endif
unsigned char EnvType;
unsigned char PortA;
unsigned char PortB;
Expand Down
17 changes: 17 additions & 0 deletions src/crtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,45 @@
#define CRTC_H

#include "types.h"
#include "SDL_endian.h"

// The next 4 bytes must remain together
typedef union {
dword combined;
struct {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
byte monVSYNC;
byte inHSYNC;
#endif
union {
word combined;
struct {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
byte DISPTIMG;
byte HDSPTIMG;
#else
byte HDSPTIMG;
byte DISPTIMG;
#endif
};
} dt;
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
byte inHSYNC;
byte monVSYNC;
#endif
};
} t_flags1;
// The next two bytes must remain together
typedef union {
word combined;
struct {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
byte NewDISPTIMG;
byte NewHDSPTIMG;
#else
byte NewHDSPTIMG;
byte NewDISPTIMG;
#endif
};
} t_new_dt;

Expand Down
21 changes: 21 additions & 0 deletions src/psg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "cap32.h"
#include "z80.h"
#include "log.h"
#include "SDL_endian.h"

extern t_CPC CPC;
extern t_PSG PSG;
Expand All @@ -48,8 +49,13 @@ int Level_PP[256];

union TLoopCount {
struct {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
dword Lo;
dword Hi;
#else
dword Hi;
dword Lo;
#endif
};
int64_t Re;
} LoopCount;
Expand All @@ -61,25 +67,40 @@ void (*Case_EnvType)();

union TCounter {
struct {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
word Lo;
word Hi;
#else
word Hi;
word Lo;
#endif
};
dword Re;
};
TCounter Ton_Counter_A, Ton_Counter_B, Ton_Counter_C, Noise_Counter;

union TNoise {
struct {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
word Low;
word Val;
#else
word Val;
word Low;
#endif
};
dword Seed;
} Noise;

union TEnvelopeCounter {
struct {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
dword Lo;
dword Hi;
#else
dword Hi;
dword Lo;
#endif
};
int64_t Re;
} Envelope_Counter;
Expand Down
2 changes: 1 addition & 1 deletion src/z80.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef Z80_H
#define Z80_H

#include "SDL.h"
#include "SDL_endian.h"
#include "types.h"
#include "crtc.h"

Expand Down
16 changes: 15 additions & 1 deletion test/crtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ TEST_F(CrtcTest, ShiftLittleEndianDwordTriplet)
EXPECT_EQ(0x01234567, val);
}

// This test verifies that access to union as they are used extensively in the code works.
// Verifies that access to t_new_dt union works fine
TEST_F(CrtcTest, NewDtCombinedAccess)
{
t_new_dt new_dt;
Expand All @@ -40,3 +40,17 @@ TEST_F(CrtcTest, NewDtCombinedAccess)

EXPECT_EQ(0x0123, new_dt.combined);
}

// Verifies that access to t_flags1 unions works fine
TEST_F(CrtcTest, Flags1CombinedAccess)
{
t_flags1 flags1;

flags1.dt.HDSPTIMG = 0x01;
flags1.dt.DISPTIMG = 0x23;
flags1.inHSYNC = 0x45;
flags1.monVSYNC = 0x67;

EXPECT_EQ(0x01234567, flags1.combined);
EXPECT_EQ(0x0123, flags1.dt.combined);
}
17 changes: 13 additions & 4 deletions test/psg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ TEST(PsgTest, RegisterAYAlignment)
t_PSG psg;
psg.RegisterAY.TonALo = 3;
psg.RegisterAY.TonAHi = 5;
EXPECT_EQ(*reinterpret_cast<word *>(&psg.RegisterAY.TonALo), psg.RegisterAY.TonA);
EXPECT_EQ(0x0503, psg.RegisterAY.TonA);
psg.RegisterAY.TonBLo = 3;
psg.RegisterAY.TonBHi = 5;
EXPECT_EQ(*reinterpret_cast<word *>(&psg.RegisterAY.TonBLo), psg.RegisterAY.TonB);
EXPECT_EQ(0x0503, psg.RegisterAY.TonB);
psg.RegisterAY.TonCLo = 3;
psg.RegisterAY.TonCHi = 5;
EXPECT_EQ(*reinterpret_cast<word *>(&psg.RegisterAY.TonCLo), psg.RegisterAY.TonC);
EXPECT_EQ(0x0503, psg.RegisterAY.TonC);
psg.RegisterAY.EnvelopeLo = 3;
psg.RegisterAY.EnvelopeHi = 5;
EXPECT_EQ(*reinterpret_cast<word *>(&psg.RegisterAY.EnvelopeLo), psg.RegisterAY.Envelope);
EXPECT_EQ(0x0503, psg.RegisterAY.Envelope);
}

TEST(PsgTest, CycleCountCombinedAccess)
{
t_PSG psg;
psg.cycle_count.high = 0x00112233;
psg.cycle_count.low = 0x44556677;

EXPECT_EQ(0x0011223344556677, psg.cycle_count.both);
}

0 comments on commit 60f0963

Please sign in to comment.