Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More mips tweaks #1482

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/mips/common/psxlibc/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ SOFTWARE.
#include <stddef.h>
#include <stdint.h>

static __attribute__((always_inline)) uint8_t* safeMemZero(uint8_t* ptr, int size) {
static __attribute__((always_inline)) void* safeMemZero(void* ptr_, int size) {
uint8_t* ptr = (uint8_t*) ptr_;
if (!ptr || size <= 0) return NULL;
uint8_t* orig = ptr;
for (; size > 0; ptr++) {
Expand Down
6 changes: 4 additions & 2 deletions src/mips/common/syscalls/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ static __attribute__((always_inline)) int syscall_setjmp(struct JmpBuf *buf) {
return ((int (*)(struct JmpBuf * buf))0xa0)(buf);
}

static __attribute__((always_inline)) __attribute__((noreturn)) void syscall_longjmp(struct JmpBuf *buf, int ret) {
static __attribute__((always_inline, noreturn)) void syscall_longjmp(struct JmpBuf *buf, int ret) {
register int n asm("t1") = 0x14;
__asm__ volatile("" : "=r"(n) : "r"(n));
((void (*)(struct JmpBuf *, int))0xa0)(buf, ret);
__builtin_unreachable();
}

static __attribute__((always_inline)) char *syscall_strcat(char *dst, const char *src) {
Expand Down Expand Up @@ -396,10 +397,11 @@ static __attribute__((always_inline)) void syscall_stopPad() {
((void (*)())0xb0)();
}

static __attribute__((noreturn)) __attribute__((always_inline)) void syscall_returnFromException() {
static __attribute__((always_inline, noreturn)) void syscall_returnFromException() {
register int n asm("t1") = 0x17;
__asm__ volatile("" : "=r"(n) : "r"(n));
((__attribute__((noreturn)) void (*)())0xb0)();
__builtin_unreachable();
}

static __attribute__((always_inline)) void syscall_setDefaultExceptionJmpBuf() {
Expand Down
3 changes: 1 addition & 2 deletions src/mips/openbios/card/backupunit.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ static __attribute__((noreturn)) void buUnimplemented(const char *function, int
osDbgPrintf("=== Unimplemented backup unit function %s, op %i ===\r\n", function, op);
osDbgPrintf("=== halting ===\r\n");
pcsx_debugbreak();
while (1)
;
while (1) asm("");
}

static void buClear() {
Expand Down
3 changes: 1 addition & 2 deletions src/mips/openbios/card/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ static __attribute__((noreturn)) void dev_bu_unimplemented(const char *function,
osDbgPrintf("=== Unimplemented memory card function %s from %p ===\r\n", function, ra);
osDbgPrintf("=== halting ===\r\n");
pcsx_debugbreak();
while (1)
;
while (1) asm("");
}

int dev_bu_open(struct File *file, const char *path, int mode) {
Expand Down
5 changes: 3 additions & 2 deletions src/mips/openbios/cdrom/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ static int findDirectoryEntryForFilename(int id, const char *filename) {
return -1;
}

int dev_cd_open(struct File *file, char *filename, int mode) {
int dev_cd_open(struct File *file, const char *filename, int mode) {
if ((cdromBlockGetStatus() & 0x10) && !cdromReadPathTable()) {
file->errno = PSXEBUSY;
return -1;
Expand Down Expand Up @@ -262,7 +262,8 @@ int dev_cd_open(struct File *file, char *filename, int mode) {
return 0;
}

int dev_cd_read(struct File *file, char *buffer, int size) {
int dev_cd_read(struct File *file, void *buffer_, int size) {
char* buffer = (char*) buffer_;
if ((size & 0x7ff) || (file->offset & 0x7ff) || (size < 0) || (file->offset >= file->length)) {
file->errno = PSXEINVAL;
return -1;
Expand Down
4 changes: 2 additions & 2 deletions src/mips/openbios/cdrom/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ SOFTWARE.
extern char g_cdromCWD[128];

int cdromReadPathTable();
int dev_cd_open(struct File* file, char* filename, int mode);
int dev_cd_read(struct File* file, char* buffer, int size);
int dev_cd_open(struct File* file, const char* filename, int mode);
int dev_cd_read(struct File* file, void* buffer, int size);
struct DirEntry* dev_cd_firstFile(struct File* file, const char* filename, struct DirEntry* entry);
struct DirEntry* dev_cd_nextFile(struct File* file, struct DirEntry* entry);
int dev_cd_chdir(struct File* file, char* name);
1 change: 1 addition & 0 deletions src/mips/openbios/handlers/irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SOFTWARE.
#include "common/psxlibc/string.h"
#include "openbios/handlers/handlers.h"
#include "openbios/kernel/events.h"
#include "openbios/kernel/handlers.h"

static int s_IRQsAutoAck[11];

Expand Down
3 changes: 1 addition & 2 deletions src/mips/openbios/handlers/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ SOFTWARE.
#include "openbios/handlers/handlers.h"
#include "openbios/kernel/events.h"
#include "openbios/kernel/globals.h"
#include "openbios/kernel/handlers.h"
#include "openbios/kernel/threads.h"

void __attribute__((noreturn)) returnFromException();

static __attribute__((section(".ramtext"))) int syscallVerifier() {
struct Thread* currentThread = __globals.processes[0].thread;
unsigned exCode = currentThread->registers.Cause & 0x3c;
Expand Down
2 changes: 0 additions & 2 deletions src/mips/openbios/kernel/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ void unimplemented(uint32_t table, uint32_t call, uint32_t ra) {

static void installExceptionHandler() { installHandler((uint32_t *)exceptionVector, (uint32_t *)0x80); }

void __attribute__((noreturn)) returnFromException();

#define EXCEPTION_STACK_SIZE 0x800

static uint32_t s_exceptionStack[EXCEPTION_STACK_SIZE];
Expand Down
2 changes: 1 addition & 1 deletion src/mips/openbios/kernel/handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ void installKernelHandlers();
void copyDataAndInitializeBSS();
void copyA0table();

void returnFromException();
void __attribute__((noreturn)) returnFromException();
1 change: 1 addition & 0 deletions src/mips/openbios/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ struct JmpBuf g_ioAbortJmpBuf;
static __attribute__((noreturn)) void fatal(int code) {
POST = 0x0f;
syscall_exception(0x42, code);
__builtin_unreachable();
}

static char s_binaryPath[128];
Expand Down
49 changes: 49 additions & 0 deletions src/mips/psyqo/fragment-concept.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*

MIT License

Copyright (c) 2023 PCSX-Redux authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

#pragma once

#include <concepts>
#include <cstddef>

namespace psyqo {

/**
* @brief The Fragment concept.
* @details This concept can be used as a template type constraint
* to ensure that a type is a valid fragment.
*/

template<typename Frag>
concept Fragment = requires(Frag frag) {
{ new int[(alignof(Frag) & 3) == 0 ? 1 : -1] };
{ new int[(sizeof(Frag) & 3) == 0 ? 1 : -1] };
{ new int[(sizeof(frag.head)) == 4 ? 1 : -1] };
{ new int[((offsetof(Frag, head)) & 3) == 0 ? 1 : -1] };
{ frag.getActualFragmentSize() } -> std::convertible_to<size_t>;
};

} // namespace psyqo
32 changes: 17 additions & 15 deletions src/mips/psyqo/fragments.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ SOFTWARE.
#include <EASTL/array.h>
#include <stdint.h>

#include "psyqo/primitive-concept.hh"

namespace psyqo {

/**
Expand Down Expand Up @@ -60,17 +62,17 @@ namespace Fragments {
* @tparam T The primitive type.
*/

template <typename T>
template <Primitive Prim>
struct SimpleFragment {
constexpr size_t maxSize() const { return 1; }
SimpleFragment() {
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(T)),
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(Prim)),
"Spurious padding in simple fragment");
}
typedef T FragmentBaseType;
constexpr size_t getActualFragmentSize() const { return sizeof(T) / sizeof(uint32_t); }
typedef Prim FragmentBaseType;
constexpr size_t getActualFragmentSize() const { return sizeof(Prim) / sizeof(uint32_t); }
uint32_t head;
T primitive;
Prim primitive;
};

/**
Expand All @@ -84,18 +86,18 @@ struct SimpleFragment {
* @tparam N The maximum number of primitives in the payload.
*/

template <typename T, size_t N>
template <Primitive Prim, size_t N>
struct FixedFragment {
constexpr size_t maxSize() const { return N; }
FixedFragment() {
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(T) * N),
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(Prim) * N),
"Spurious padding in fixed fragment");
}
typedef T FragmentBaseType;
size_t getActualFragmentSize() const { return (sizeof(T) * count) / sizeof(uint32_t); }
typedef Prim FragmentBaseType;
size_t getActualFragmentSize() const { return (sizeof(Prim) * count) / sizeof(uint32_t); }
unsigned count = N;
uint32_t head;
eastl::array<T, N> primitives;
eastl::array<Prim, N> primitives;
};

/**
Expand All @@ -112,19 +114,19 @@ struct FixedFragment {
* @tparam N The maximum number of primitives in the payload.
*/

template <typename P, typename T, size_t N>
template <Primitive P, Primitive Prim, size_t N>
struct FixedFragmentWithPrologue {
constexpr size_t maxSize() const { return N; }
FixedFragmentWithPrologue() {
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(P) + sizeof(T) * N),
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(P) + sizeof(Prim) * N),
"Spurious padding in fixed fragment");
}
typedef T FragmentBaseType;
size_t getActualFragmentSize() const { return (sizeof(P) + sizeof(T) * count) / sizeof(uint32_t); }
typedef Prim FragmentBaseType;
size_t getActualFragmentSize() const { return (sizeof(P) + sizeof(Prim) * count) / sizeof(uint32_t); }
unsigned count = N;
uint32_t head;
P prologue;
eastl::array<T, N> primitives;
eastl::array<Prim, N> primitives;
};

} // namespace Fragments
Expand Down
27 changes: 15 additions & 12 deletions src/mips/psyqo/gpu.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ SOFTWARE.
#include <EASTL/utility.h>
#include <stdint.h>

#include "psyqo/fragment-concept.hh"
#include "psyqo/hardware/gpu.hh"
#include "psyqo/primitives.hh"
#include "psyqo/primitive-concept.hh"
#include "psyqo/primitives/common.hh"
#include "psyqo/primitives/control.hh"
#include "psyqo/primitives/misc.hh"

namespace psyqo {

Expand Down Expand Up @@ -179,8 +183,8 @@ class GPU {
*
* @param fragment The fragment to send to the GPU.
*/
template <typename Fragment>
void sendFragment(const Fragment &fragment) {
template <Fragment Frag>
void sendFragment(const Frag &fragment) {
sendFragment(&fragment.head + 1, fragment.getActualFragmentSize());
}

Expand All @@ -193,8 +197,8 @@ class GPU {
* @param callback The callback to call upon completion.
* @param dmaCallback `DMA::FROM_MAIN_LOOP` or `DMA::FROM_ISR`.
*/
template <typename Fragment>
void sendFragment(const Fragment &fragment, eastl::function<void()> &&callback,
template <Fragment Frag>
void sendFragment(const Frag &fragment, eastl::function<void()> &&callback,
DMA::DmaCallback dmaCallback = DMA::FROM_MAIN_LOOP) {
sendFragment(&fragment.head + 1, fragment.getActualFragmentSize(), eastl::move(callback), dmaCallback);
}
Expand Down Expand Up @@ -252,14 +256,13 @@ class GPU {
* @details This method will immediately send the specified primitive to the GPU.
* @param primitive The primitive to send to the GPU.
*/
template <typename Primitive>
static void sendPrimitive(const Primitive &primitive) {
static_assert((sizeof(Primitive) % 4) == 0, "Primitive's size must be a multiple of 4");
template <Primitive Prim>
static void sendPrimitive(const Prim &primitive) {
waitReady();
const uint32_t *ptr = reinterpret_cast<const uint32_t *>(&primitive);
size_t size = sizeof(Primitive) / sizeof(uint32_t);
constexpr size_t size = sizeof(Prim) / sizeof(uint32_t);
for (int i = 0; i < size; i++) {
if constexpr (sizeof(Primitive) > 56) waitFifo();
if constexpr (sizeof(Prim) > 56) waitFifo();
sendRaw(*ptr++);
}
}
Expand All @@ -277,8 +280,8 @@ class GPU {
* if applicable.
* @param fragment The fragment to chain.
*/
template <typename Fragment>
void chain(Fragment &fragment) {
template <Fragment Frag>
void chain(Frag &fragment) {
chain(&fragment.head, fragment.getActualFragmentSize());
}

Expand Down
46 changes: 46 additions & 0 deletions src/mips/psyqo/primitive-concept.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*

MIT License

Copyright (c) 2023 PCSX-Redux authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

#pragma once

#include <concepts>
#include <cstddef>

namespace psyqo {

/**
* @brief The Primitive concept.
* @details This concept can be used as a template type constraint
* to ensure that a type is a valid primitive.
*/

template <typename Prim>
concept Primitive = requires {
{ new int[(alignof(Prim) & 3) == 0 ? 1 : -1] };
{ new int[(sizeof(Prim) & 3) == 0 ? 1 : -1] };
};

} // namespace psyqo
6 changes: 6 additions & 0 deletions src/mips/psyqo/primitives.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ SOFTWARE.

#pragma once

/**
* @file primitives.hh
* @brief This file is a convenience header that includes all the primitive
* headers.
*/

#include "psyqo/primitives/control.hh"
#include "psyqo/primitives/lines.hh"
#include "psyqo/primitives/misc.hh"
Expand Down
Loading