Skip to content

Commit

Permalink
Add support for DS expansion pak in VanillaTD
Browse files Browse the repository at this point in the history
  • Loading branch information
giulianobelinassi committed Aug 18, 2022
1 parent dcea1ae commit d49b424
Show file tree
Hide file tree
Showing 27 changed files with 930 additions and 58 deletions.
58 changes: 46 additions & 12 deletions arm7/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
// Calculate the ceil of division x / y.
#define CEIL_DIV(x, y) (1 + (((x) - 1) / (y)))

/* Variable used to busy wait while the ARM9 copy data from an unreachable
address to somewhere we can reach. */
volatile bool WaitingARM9 = false;

/* Pointer to an region of memory that the ARM9 can write. */
unsigned char *SharedArea = NULL;

// Provide an implementation of timerElapsed. Stolen from libnds.

//---------------------------------------------------------------------------------
Expand Down Expand Up @@ -72,7 +79,7 @@ enum
VOLUME_MAX = 255,
PRIORITY_MIN = 0,
PRIORITY_MAX = 255,
MAX_SAMPLE_TRACKERS = 5,
MAX_SAMPLE_TRACKERS = NUM_TRACKERS,
DECOMP_BUFFER_COUNT = 2,
BUFFER_CHUNK_SIZE = 4096,
UNCOMP_BUFFER_SIZE = 2098,
Expand All @@ -81,17 +88,6 @@ enum
INVALID_FILE_HANDLE = -1,
};

/*
** Define the different type of sound compression avaliable to the westwood
** library.
*/
typedef enum
{
SCOMP_NONE = 0, // No compression -- raw data.
SCOMP_WESTWOOD = 1, // Special sliding window delta compression.
SCOMP_SOS = 99 // SOS frame compression.
} SCompressType;

class SoundTracker;
static inline int __attribute__((pure)) Get_Channel_Index(SoundTracker*);

Expand Down Expand Up @@ -225,6 +221,27 @@ class SoundTracker
SoundHandle = handle;
IsMusic = is_music;

/* If the AUD is at an unreachable address, ask for the ARM9 to copy it
to somewhere we can reach. This should never run on DSi mode. */
if ((u32)sample >= 0x08000000) {
int index = Get_Channel_Index();

const void *src = sample;
void *dst = &SharedArea[SHARED_CHUNK_SIZE * index];

USR2::FifoMemcpyMessage msg;
msg.src = src;
msg.dst = dst;

fifoSendDatamsg(FIFO_USER_02, sizeof(msg), (u8*)&msg);

while (WaitingARM9)
; // Busy wait the ARM9 to answer.

/* Override unreachable pointer with the one that I can reach. */
sample = dst;
}

// Load the AUD header;
AUDHeaderType raw_header;
memcpy(&raw_header, sample, sizeof(raw_header));
Expand Down Expand Up @@ -261,9 +278,17 @@ class SoundTracker
sosinfo.dwUnCompSize = raw_header.Size * (sosinfo.wBitSize / 4);
sosCODECInitStream(&sosinfo);
} else if (Compression == SCOMP_WESTWOOD) {
if (!isDSiMode()) {
/* SCOMP_WESTWOOD on retail DS crashes the system for some
resason. */
Stop_Sample();
return 0;
}

/* There is a bug in SCOMP_WESTWOOD on DS in which sounds compressed
by it get wavely loud in some audios. So we lower their volume
so that it doesn't bother the user too much. */

Volume = Volume / 6;
Bits = 8;
}
Expand Down Expand Up @@ -869,12 +894,19 @@ void user01CommandHandler(u32 command, void* userdata)

case USR1::STOP_SAMPLE_HANDLE:
Trackers.Stop_Sample_Handle(data);
break;

case USR1::STOP_SAMPLE:
Trackers.Stop_Sample(data);
break;

case USR1::SET_MUSIC_VOL:
Trackers.Set_Music_Vol(data);
break;

case USR1::ARM9_AUDCPY_DONE:
WaitingARM9 = false;
break;

default:
break;
Expand Down Expand Up @@ -913,6 +945,8 @@ void Process_Queue()
SCHANNEL_ENABLE | SOUND_VOL(volume) | SOUND_PAN(64) | (format << 29) | (SOUND_REPEAT);

SCHANNEL_REPEAT_POINT(VQA_CHANNEL) = 0;
} else if (msg.type == USR1::SET_SHARED_AREA) {
SharedArea = (unsigned char *) msg.SetSharedArea.ptr;
}
}

Expand Down
2 changes: 1 addition & 1 deletion common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ set(COMMON_SRC
if (WIN32)
list(APPEND COMMON_SRC file_win.cpp paths_win.cpp wintimer.cpp)
elseif (NDS)
list(APPEND COMMON_SRC fnmatch.cpp file_posix.cpp paths_nds.cpp wintimer_nds.cpp memcpy.s rmemcpy.s memmove.s)
list(APPEND COMMON_SRC fnmatch.cpp file_posix.cpp paths_nds.cpp wintimer_nds.cpp expansionpak_nds.cpp memcpy.s rmemcpy.s memmove.s)
else()
list(APPEND COMMON_SRC file_posix.cpp paths_posix.cpp wintimer.cpp)
endif()
Expand Down
38 changes: 37 additions & 1 deletion common/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <unistd.h>
#include <malloc.h>
#include <nds.h>
#include "expansionpak_nds.h"
#endif

#if defined(__unix__) || defined(__unix)
Expand Down Expand Up @@ -101,15 +102,44 @@ extern void (*Memory_Error_Exit)(char* string) = NULL;
* 03/09/1995 JLB : Fixed *
* 09/28/1995 ST : Simplified for win95 *
*=========================================================================*/

#ifdef _NDS
bool ExpansionMemoryInstalled;
size_t StackTop = 0;
void *ExpansionAddr;

#endif

void* Alloc(size_t bytes_to_alloc, MemoryFlagType flags)
{

void* mem_ptr;

#ifdef _NDS
static bool expansion_initialized = false;
if (flags & MEM_EXPANSION) {
if (!expansion_initialized) {
expansion_initialized = true;
ExpansionMemoryInstalled = ram_init(DETECT_RAM);
ExpansionAddr = (void *) ram_unlock();
}

if (ExpansionMemoryInstalled) {
/* 4 bytes aligned. */
mem_ptr = (void*) ((((uintptr_t)ExpansionAddr + 3UL) & ~3UL) + StackTop);
StackTop += (bytes_to_alloc + 3UL) & ~3UL;

return mem_ptr;
}
}
#endif

#ifdef MEM_CHECK
bytes_to_alloc += sizeof(uintptr_t) * 8;
#endif // MEM_CHECK

mem_ptr = malloc(bytes_to_alloc);

if (mem_ptr == NULL) {
DBG_LOG("Unable to allocate memory\n");
}
Expand Down Expand Up @@ -178,7 +208,13 @@ void Free(void const* pointer)

pointer = (void*)(((char*)pointer) - 16);
#endif // MEM_CHECK

#ifdef _NDS
/* Expansion memory is not administrated by malloc. */
if ((uintptr_t)pointer >= 0x08000000) {
Memory_Calls--;
return;
}
#endif
free((void*)pointer);
Memory_Calls--;
}
Expand Down
39 changes: 39 additions & 0 deletions common/audio_fifocommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
// have to stream it from the SD Card.
#define MUSIC_CHUNK_SIZE 32768

// Chunk size of the shared memory area per tracker. Must be at least equal
// to the largest AUD in a *cached* mixfile.
#define SHARED_CHUNK_SIZE (70 * 1024)

// Number of trackers to use;
#define NUM_TRACKERS 5

/*
** Define the different type of sound compression avaliable to the westwood
** library.
*/
typedef enum
{
SCOMP_NONE = 0, // No compression -- raw data.
SCOMP_WESTWOOD = 1, // Special sliding window delta compression.
SCOMP_SOS = 99 // SOS frame compression.
} SCompressType;

// USR1: ARM9 to ARM7
namespace USR1
{
Expand All @@ -32,6 +50,10 @@ namespace USR1

// Set volume of music.
SET_MUSIC_VOL = 4 << 20,

// Confirmation that copy from ARM9 is done.
ARM9_AUDCPY_DONE = 5 << 20,

} FifoSoundCommand;

// Define message kinds. Used to distinguish packages one from another.
Expand All @@ -42,6 +64,11 @@ namespace USR1

// Sound message comming from VQA Player.
SOUND_VQA_MESSAGE,

// Set shared memory area to store stuff comming from regions that
// the ARM7 can't reach.
SET_SHARED_AREA,

} FifoSoundMessageType;

// Define what can be in a message. Message must have a maximum length of
Expand Down Expand Up @@ -71,6 +98,11 @@ namespace USR1
u8 volume;
u8 bits;
} SoundVQAChunk;

struct
{
void* ptr;
} SetSharedArea;
};

} ALIGN(4) FifoSoundMessage;
Expand All @@ -85,6 +117,13 @@ namespace USR2
// Ask the ARM9 for more music data.
MUSIC_REQUEST_CHUNK = 1 << 20,
} SoundMusicChunk;

// Parameters of a memcpy call.
typedef struct FifoMessage
{
const void *src;
void *dst;
} ALIGN(4) FifoMemcpyMessage;
} // namespace USR2

#endif //AUDIO_FIFOCOMMON
4 changes: 2 additions & 2 deletions common/buff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ Buffer::Buffer(void const* buffer, int size)
* HISTORY: *
* 07/29/1996 JLB : Created. *
*=============================================================================================*/
Buffer::Buffer(int size)
Buffer::Buffer(int size, MemoryFlagType flags)
: BufferPtr(NULL)
, Size(size)
, IsAllocated(false)
{
if (size > 0) {
BufferPtr = new char[size];
BufferPtr = new (flags) char[size];
IsAllocated = true;
}
}
Expand Down
4 changes: 3 additions & 1 deletion common/buff.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ typedef int bool;
#endif
#endif

#include "memflag.h"

/*
** A general purpose buffer pointer handler object. It holds not only the pointer to the
** buffer, but its size as well. By using this class instead of separate pointer and size
Expand All @@ -63,7 +65,7 @@ class Buffer
Buffer(char* ptr, int size = 0);
Buffer(void* ptr = 0, int size = 0);
Buffer(void const* ptr, int size = 0);
Buffer(int size);
Buffer(int size, MemoryFlagType flags = MEM_NORMAL);
Buffer(Buffer const& buffer);
~Buffer(void);

Expand Down
Loading

0 comments on commit d49b424

Please sign in to comment.