Skip to content

Commit

Permalink
Merge branch 'port' into port-net
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfdsfgs committed May 19, 2024
2 parents 0d6a5f2 + de3b8ff commit a5a2970
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 174 deletions.
4 changes: 2 additions & 2 deletions Makefile.port
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ B_DIR := build/$(ROMID)-port
G_DIR := src/generated/$(ROMID)

ifneq (,$(findstring windows,$(TARGET_PLATFORM)))
TARGET_CFLAGS := $(shell pkg-config sdl2 --cflags-only-I) -pthread
TARGET_CFLAGS := $(shell pkg-config sdl2 --cflags-only-I)
TARGET_LDFLAGS := $(shell pkg-config sdl2 --libs) -lz -lopengl32 -lws2_32 -lwinmm -ldbghelp
# on windows/mingw we need this to be built with a 32-bit compiler so it finds the correct libs
TOOLCHAIN ?= i686-w64-mingw32-
Expand All @@ -161,7 +161,7 @@ ifneq (,$(findstring windows,$(TARGET_PLATFORM)))
TARGET_LDFLAGS += -lstdc++
endif
else # assume *nix
TARGET_CFLAGS := $(shell pkg-config sdl2 --cflags-only-I) -pthread
TARGET_CFLAGS := $(shell pkg-config sdl2 --cflags-only-I)
TARGET_LDFLAGS := $(shell pkg-config sdl2 --libs) -lGL -lz -ldl
ifneq (,$(findstring arm,$(TARGET_PLATFORM))$(findstring aarch,$(TARGET_PLATFORM)))
# linux on arm or aarch64
Expand Down
2 changes: 1 addition & 1 deletion port/fast3d/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ static inline void sync_framerate_with_timer(void) {
// We want to exit a bit early, so we can busy-wait the rest to never miss the deadline
left -= 15000UL;
if (left > 0) {
sysFrameLimiterSleep(left);
sysSleep(left);
}

do {
Expand Down
3 changes: 2 additions & 1 deletion port/include/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
s32 audioInit(void);
s32 audioGetBytesBuffered(void);
s32 audioGetSamplesBuffered(void);
void audioSubmitBuffer(const s16 *buf, u32 size);
void audioSetNextBuffer(const s16 *buf, u32 len);
void audioEndFrame(void);

#endif
6 changes: 1 addition & 5 deletions port/include/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ void *sysMemZeroAlloc(const u32 size);
void sysMemFree(void *ptr);

// hns is specified in 100ns units
// do not use sysFrameLimiterSleep on any thread other than the main one
void sysFrameLimiterSleep(const s64 hns);

// us is specified in microseconds
void sysSleep(const s64 us);
void sysSleep(const s64 hns);

// yield CPU if supported (e.g. during a busy loop)
void sysCpuRelax(void);
Expand Down
27 changes: 0 additions & 27 deletions port/include/thread.h

This file was deleted.

40 changes: 16 additions & 24 deletions port/src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#include "system.h"

static SDL_AudioDeviceID dev;
static const s16 *nextBuf;
static u32 nextSize = 0;

static s32 bufferSize = 512;
static s32 queueLimit = 8192;

static f32 offsetFrames = 0.5f;
static u32 offsetBytes;

s32 audioInit(void)
{
if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) {
Expand All @@ -29,8 +28,7 @@ s32 audioInit(void)
want.samples = bufferSize;
want.callback = NULL;

const f32 spf = (f32)want.freq / (PAL ? 50.f : 60.f);
offsetBytes = offsetFrames * spf * (f32)sizeof(int16_t);
nextBuf = NULL;

dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
if (dev == 0) {
Expand All @@ -45,39 +43,33 @@ s32 audioInit(void)

s32 audioGetBytesBuffered(void)
{
u32 count = SDL_GetQueuedAudioSize(dev);

// pd's audio code decides how many samples of audio to produce based on the amount of audio
// currently buffered, so we adjust the reported count to be some number of frames in the future
// to counteract lag; idea shamelessly stolen from mm recomp
if (offsetBytes) {
if (count > offsetBytes) {
count -= offsetBytes;
} else {
count = 0;
}
}

return count;
return SDL_GetQueuedAudioSize(dev);
}

s32 audioGetSamplesBuffered(void)
{
return audioGetBytesBuffered() / (2 * sizeof(s16));
return audioGetBytesBuffered() / 4;
}

void audioSetNextBuffer(const s16 *buf, u32 len)
{
nextBuf = buf;
nextSize = len;
}

void audioSubmitBuffer(const s16 *buf, u32 size)
void audioEndFrame(void)
{
if (buf && size) {
if (nextBuf && nextSize) {
if (audioGetSamplesBuffered() < queueLimit) {
SDL_QueueAudio(dev, buf, size);
SDL_QueueAudio(dev, nextBuf, nextSize);
}
nextBuf = NULL;
nextSize = 0;
}
}

PD_CONSTRUCTOR static void audioConfigInit(void)
{
configRegisterInt("Audio.BufferSize", &bufferSize, 0, 1 * 1024 * 1024);
configRegisterFloat("Audio.OffsetFrames", &offsetFrames, 0.f, 5.f);
configRegisterInt("Audio.QueueLimit", &queueLimit, 0, 1 * 1024 * 1024);
}
2 changes: 1 addition & 1 deletion port/src/libultra.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ u32 osAiGetLength(void)

s32 osAiSetNextBuffer(void *bufPtr, u32 size)
{
audioSubmitBuffer(bufPtr, size);
audioSetNextBuffer(bufPtr, size);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion port/src/pdmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ void mainLoop(void)
schedEndFrame(&g_Sched);
}
if (g_TickExtraSleep) {
sysFrameLimiterSleep(EXTRA_SLEEP_TIME);
sysSleep(EXTRA_SLEEP_TIME);
}
}

Expand Down
25 changes: 25 additions & 0 deletions port/src/pdsched.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ void schedStartFrame(OSSched *sc)
}
}

void schedAudioFrame(OSSched *sc)
{
s32 i;

if (!g_SndDisabled) {
for (i = 0; i < g_Vars.diffframe60; i++) {
amgrFrame();
audioEndFrame();
}
}
}

/**
* Handle a retrace (vsync) event.
*
Expand All @@ -272,6 +284,18 @@ void schedEndFrame(OSSched *sc)

sc->frameCount++;

#if PAL
if (!g_Resetting && (sc->frameCount & 1)) {
// osStopTimer(&g_SchedRspTimer);
// osSetTimer(&g_SchedRspTimer, 280000, 0, amgrGetFrameMesgQueue(), &g_SchedRspMsg);
}
#else
if (!g_Resetting && ((sc->frameCount & 1) || IS4MB())) {
// osStopTimer(&g_SchedRspTimer);
// osSetTimer(&g_SchedRspTimer, 280000, 0, amgrGetFrameMesgQueue(), &g_SchedRspMsg);
}
#endif

if (!g_Resetting) {
viHandleRetrace();
}
Expand All @@ -294,6 +318,7 @@ void schedEndFrame(OSSched *sc)
}

sndHandleRetrace();
schedAudioFrame(sc);
schedRenderCrashPeriodically(sc->frameCount);
videoEndFrame();

Expand Down
11 changes: 3 additions & 8 deletions port/src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <strings.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <SDL.h>
#include <PR/ultratypes.h>
#include "platform.h"
Expand All @@ -34,6 +33,8 @@ __attribute__((dllexport)) u32 AmdPowerXpressRequestHighPerformance = 1;

#else

#include <unistd.h>

// figure out how to yield
#if defined(PLATFORM_X86) || defined(PLATFORM_X86_64)
// this should work even if the code is not built with SSE enabled, at least on gcc and clang,
Expand Down Expand Up @@ -304,7 +305,7 @@ void sysMemFree(void *ptr)
free(ptr);
}

void sysFrameLimiterSleep(const s64 hns)
void sysSleep(const s64 hns)
{
#ifdef PLATFORM_WIN32
static LARGE_INTEGER li;
Expand All @@ -317,12 +318,6 @@ void sysFrameLimiterSleep(const s64 hns)
#endif
}

void sysSleep(const s64 us)
{
const struct timespec spec = { 0, us * 1000 };
nanosleep(&spec, NULL);
}

void sysCpuRelax(void)
{
DO_YIELD();
Expand Down
27 changes: 0 additions & 27 deletions port/src/thread.c

This file was deleted.

10 changes: 8 additions & 2 deletions src/game/chraicommands.c
Original file line number Diff line number Diff line change
Expand Up @@ -9868,14 +9868,20 @@ bool aiIfMusicEventQueueIsEmpty(void)
{
f32 value = (u64)osGetCount() * 64 / 3000;

#ifdef PLATFORM_N64 // will hang forever until the audio thread wakes it up
if (g_MusicEventQueueLength) {
#ifndef PLATFORM_N64
musicTickEvents();
#else // HACK: will wait 1 frame and get on with it
static bool waited = false;
if (g_MusicEventQueueLength && !waited) {
waited = true;
#endif
g_Vars.aioffset += 4;
} else {
u8 *cmd = g_Vars.ailist + g_Vars.aioffset;
g_Vars.aioffset = chraiGoToLabel(g_Vars.ailist, g_Vars.aioffset, cmd[3]);
#ifndef PLATFORM_N64
waited = false;
#endif
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/game/timing.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void frametimeCalculate(void)

#ifndef PLATFORM_N64
if (g_TickExtraSleep) {
sysFrameLimiterSleep(EXTRA_SLEEP_TIME);
sysSleep(EXTRA_SLEEP_TIME);
}
#endif
} while (g_Vars.mininc60 && diffframe60 < g_Vars.mininc60);
Expand Down
6 changes: 1 addition & 5 deletions src/include/lib/audiomgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ OSMesgQueue *amgrGetFrameMesgQueue(void);
void amgrStopThread(void);

#ifndef PLATFORM_N64
void amgrLock(void);
void amgrUnlock(void);
#else
#define amgrLock() do {} while(0)
#define amgrUnlock() do {} while(0)
void amgrFrame(void);
#endif

#endif
3 changes: 0 additions & 3 deletions src/include/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
#error "64-bit platforms are currently not supported."
#endif

// threading backend
#define PLATFORM_USE_PTHREAD 1

// detect endianness
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
Expand Down
Loading

0 comments on commit a5a2970

Please sign in to comment.