This commit is contained in:
n64 committed 2019-08-25 00:46:40 -04:00
commit 89e8690857
2846 files changed
+625030

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
typedef union {
int i;
float f;
} fu;
const fu NAN = { 0x7f810000 };
+270
View File
@@ -0,0 +1,270 @@
#include "libultra_internal.h"
#include <macros.h>
#include <stdlib.h>
#include <string.h>
#include "printf.h"
#define BUFF_LEN 0x20
static s16 _Ldunscale(s16 *, printf_struct *);
static void _Genld(printf_struct *, u8, u8 *, s16, s16);
const double D_80338670[] = { 10e0L, 10e1L, 10e3L, 10e7L, 10e15L, 10e31L, 10e63L, 10e127L, 10e255L };
/* float properties */
#define _D0 0
#define _DBIAS 0x3ff
#define _DLONG 1
#define _DOFF 4
#define _FBIAS 0x7e
#define _FOFF 7
#define _FRND 1
#define _LBIAS 0x3ffe
#define _LOFF 15
/* integer properties */
#define _C2 1
#define _CSIGN 1
#define _ILONG 0
#define _MBMAX 8
#define NAN 2
#define INF 1
#define FINITE -1
#define _DFRAC ((1 << _DOFF) - 1)
#define _DMASK (0x7fff & ~_DFRAC)
#define _DMAX ((1 << (15 - _DOFF)) - 1)
#define _DNAN (0x8000 | _DMAX << _DOFF | 1 << (_DOFF - 1))
#define _DSIGN 0x8000
#if _D0 == 3
#define _D1 2 /* little-endian order */
#define _D2 1
#define _D3 0
#else
#define _D1 1 /* big-endian order */
#define _D2 2
#define _D3 3
#endif
void _Ldtob(printf_struct *args, u8 type) {
u8 buff[BUFF_LEN];
u8 *ptr;
UNUSED u32 sp70;
f64 val;
/* maybe struct? */
s16 err;
s16 nsig;
s16 exp;
s32 i;
s32 n;
f64 factor;
s32 gen;
s32 j;
s32 lo;
ldiv_t qr;
u8 drop;
s32 n2;
/* */
UNUSED u8 unused[0x4];
ptr = buff;
val = args->value.f64;
if (args->precision < 0) {
args->precision = 6;
} else {
if (args->precision == 0 && (type == 'g' || type == 'G')) {
args->precision = 1;
}
}
err = _Ldunscale(&exp, args);
if (err > 0) {
memcpy(args->buff, err == 2 ? "NaN" : "Inf", args->part2_len = 3);
return;
}
if (err == 0) {
nsig = 0;
exp = 0;
} else {
if (val < 0)
val = -val;
exp = exp * 30103 / 0x000186A0 - 4;
if (exp < 0) {
n = (3 - exp) & ~3;
exp = -n;
for (i = 0; n > 0; n >>= 1, i++) {
if ((n & 1) != 0) {
val *= D_80338670[i];
}
}
} else {
if (exp > 0) {
factor = 1;
exp &= ~3;
n = exp;
for (i = 0; n > 0; n >>= 1, i++) {
if ((n & 1) != 0) {
factor *= D_80338670[i];
}
}
val /= factor;
}
}
gen = ((type == 'f') ? exp + 10 : 6) + args->precision;
if (gen > 0x13)
gen = 0x13;
*ptr++ = '0';
while (gen > 0 && 0 < val) {
lo = val;
if ((gen -= 8) > 0) {
val = (val - lo) * 1.0e8;
}
ptr = ptr + 8;
for (j = 8; lo > 0 && --j >= 0;) {
qr = ldiv(lo, 10);
*--ptr = qr.rem + '0';
lo = qr.quot;
}
while (--j >= 0) {
ptr--;
*ptr = '0';
}
ptr += 8;
}
gen = ptr - &buff[1];
for (ptr = &buff[1], exp += 7; *ptr == '0'; ptr++) {
--gen, --exp;
}
nsig = ((type == 'f') ? exp + 1 : ((type == 'e' || type == 'E') ? 1 : 0)) + args->precision;
if (gen < nsig)
nsig = gen;
if (nsig > 0) {
if (nsig < gen && ptr[nsig] > '4')
drop = '9';
else
drop = '0';
for (n2 = nsig; ptr[--n2] == drop;) {
nsig--;
}
if (drop == '9') {
ptr[n2]++;
}
if (n2 < 0) {
--ptr, ++nsig, ++exp;
}
}
}
_Genld(args, type, ptr, nsig, exp);
}
static s16 _Ldunscale(s16 *pex, printf_struct *px) {
unsigned short *ps = (unsigned short *) px;
short xchar = (ps[_D0] & _DMASK) >> _DOFF;
if (xchar == _DMAX) { /* NaN or INF */
*pex = 0;
return (s16)(ps[_D0] & _DFRAC || ps[_D1] || ps[_D2] || ps[_D3] ? NAN : INF);
} else if (0 < xchar) {
ps[_D0] = (ps[_D0] & ~_DMASK) | (_DBIAS << _DOFF);
*pex = xchar - (_DBIAS - 1);
return (FINITE);
}
if (0 > xchar)
return NAN;
else {
*pex = 0;
return (0);
}
}
static void _Genld(printf_struct *px, u8 code, u8 *p, s16 nsig, s16 xexp) {
u8 point = '.';
if (nsig <= 0)
nsig = 1,
p = (u8 *) "0";
if (code == 'f'
|| ((code == 'g' || code == 'G') && (-4 <= xexp) && (xexp < px->precision))) { /* 'f' format */
++xexp; /* change to leading digit count */
if (code != 'f') { /* fixup for 'g' */
if (!(px->flags & FLAGS_HASH) && nsig < px->precision)
px->precision = nsig;
if ((px->precision -= xexp) < 0)
px->precision = 0;
}
if (xexp <= 0) { /* digits only to right of point */
px->buff[px->part2_len++] = '0';
if (0 < px->precision || px->flags & FLAGS_HASH)
px->buff[px->part2_len++] = point;
if (px->precision < -xexp)
xexp = -px->precision;
px->num_mid_zeros = -xexp;
px->precision += xexp;
if (px->precision < nsig)
nsig = px->precision;
memcpy(&px->buff[px->part2_len], p, px->part3_len = nsig);
px->num_trailing_zeros = px->precision - nsig;
} else if (nsig < xexp) { /* zeros before point */
memcpy(&px->buff[px->part2_len], p, nsig);
px->part2_len += nsig;
px->num_mid_zeros = xexp - nsig;
if (0 < px->precision || px->flags & FLAGS_HASH)
px->buff[px->part2_len] = point, ++px->part3_len;
px->num_trailing_zeros = px->precision;
} else { /* enough digits before point */
memcpy(&px->buff[px->part2_len], p, xexp);
px->part2_len += xexp;
nsig -= xexp;
if (0 < px->precision || px->flags & FLAGS_HASH)
px->buff[px->part2_len++] = point;
if (px->precision < nsig)
nsig = px->precision;
memcpy(&px->buff[px->part2_len], p + xexp, nsig);
px->part2_len += nsig;
px->num_mid_zeros = px->precision - nsig;
}
} else { /* 'e' format */
if (code == 'g' || code == 'G') { /* fixup for 'g' */
if (nsig < px->precision)
px->precision = nsig;
if (--px->precision < 0)
px->precision = 0;
code = code == 'g' ? 'e' : 'E';
}
px->buff[px->part2_len++] = *p++;
if (0 < px->precision || px->flags & FLAGS_HASH)
px->buff[px->part2_len++] = point;
if (0 < px->precision) { /* put fraction digits */
if (px->precision < --nsig)
nsig = px->precision;
memcpy(&px->buff[px->part2_len], p, nsig);
px->part2_len += nsig;
px->num_mid_zeros = px->precision - nsig;
}
p = (u8 *) &px->buff[px->part2_len]; /* put exponent */
*p++ = code;
if (0 <= xexp)
*p++ = '+';
else { /* negative exponent */
*p++ = '-';
xexp = -xexp;
}
if (100 <= xexp) { /* put oversize exponent */
if (1000 <= xexp)
*p++ = xexp / 1000 + '0', xexp %= 1000;
*p++ = xexp / 100 + '0', xexp %= 100;
}
*p++ = xexp / 10 + '0', xexp %= 10;
*p++ = xexp + '0';
px->part3_len = (u32) p - (u32) &px->buff[px->part2_len];
}
if ((px->flags & (FLAGS_ZERO | FLAGS_MINUS)) == FLAGS_ZERO) { /* pad with leading zeros */
int n =
px->part1_len + px->part2_len + px->num_mid_zeros + px->part3_len + px->num_trailing_zeros;
if (n < px->width)
px->num_leading_zeros = px->width - n;
}
}
+54
View File
@@ -0,0 +1,54 @@
#include "libultra_internal.h"
#include <stdlib.h>
#include <string.h>
#include "printf.h"
#define BUFF_LEN 0x18
static u8 D_80334960[] = "0123456789abcdef";
static u8 D_80334974[] = "0123456789ABCDEF";
void _Litob(printf_struct *args, u8 type) {
u8 buff[BUFF_LEN];
const u8 *num_map;
s32 base;
s32 buff_ind;
u64 num;
lldiv_t quotrem;
if (type == 'X')
num_map = D_80334974;
else
num_map = D_80334960;
base = (type == 'o') ? 8 : ((type != 'x' && type != 'X') ? 10 : 16);
buff_ind = BUFF_LEN;
num = args->value.s64;
if ((type == 'd' || type == 'i') && args->value.s64 < 0)
num = -num;
if (num != 0 || args->precision != 0)
buff[--buff_ind] = num_map[num % base];
args->value.s64 = num / base;
while (args->value.s64 > 0 && buff_ind > 0) {
quotrem = lldiv(args->value.s64, base);
args->value.s64 = quotrem.quot;
buff[--buff_ind] = num_map[quotrem.rem];
}
args->part2_len = BUFF_LEN - buff_ind;
memcpy(args->buff, buff + buff_ind, args->part2_len);
if (args->part2_len < args->precision)
args->num_leading_zeros = args->precision - args->part2_len;
if (args->precision < 0 && (args->flags & (FLAGS_ZERO | FLAGS_MINUS)) == FLAGS_ZERO) {
buff_ind = args->width - args->part1_len - args->num_leading_zeros - args->part2_len;
if (buff_ind > 0)
args->num_leading_zeros += buff_ind;
}
}
+218
View File
@@ -0,0 +1,218 @@
#include "libultra_internal.h"
#include <stdarg.h>
#include <string.h>
#include "printf.h"
#define ATOI(i, a) \
for (i = 0; *a >= '0' && *a <= '9'; a++) \
if (i < 999) \
i = *a + i * 10 - '0';
#define _PROUT(dst, fmt, _size) \
if (_size > 0) { \
dst = prout(dst, fmt, _size); \
if (dst != 0) \
sp78.size += _size; \
else \
return sp78.size; \
}
#define _PAD(i, m, c, src, extracond) \
if (extracond && m > 0) \
for (i = m; i > 0; i -= c) { \
if ((u32) i > 32) \
c = 32; \
else \
c = i; \
_PROUT(dst, src, c); \
}
const u8 length_str[] = "hlL";
const u8 flags_str[] = " +-#0";
const u32 flags_arr[] = { FLAGS_SPACE, FLAGS_PLUS, FLAGS_MINUS, FLAGS_HASH, FLAGS_ZERO, 0 };
char _spaces[] = " ";
char _zeroes[] = "00000000000000000000000000000000";
static void _Putfld(printf_struct *, va_list *, u8, u8 *);
s32 _Printf(char *(*prout)(char *, const char *, size_t), char *dst, const char *fmt, va_list args) {
printf_struct sp78;
const u8 *fmt_ptr;
u8 c;
const u8 *flag_index;
u8 sp4c[0x20]; // probably a buffer?
s32 sp48, sp44, sp40, sp3c, sp38, sp34, sp30, sp2c, sp28, sp24;
sp78.size = 0;
while (1) {
fmt_ptr = (u8 *) fmt;
while ((c = *fmt_ptr++) > 0) {
if (c == '%') {
fmt_ptr--;
break;
}
}
_PROUT(dst, fmt, fmt_ptr - (u8 *) fmt);
if (c == 0)
return sp78.size;
fmt = (char *) ++fmt_ptr;
sp78.flags = 0;
for (; (flag_index = strchr(flags_str, *fmt_ptr)) != NULL; fmt_ptr++) {
sp78.flags |= flags_arr[flag_index - flags_str];
}
if (*fmt_ptr == '*') {
sp78.width = va_arg(args, s32);
if (sp78.width < 0) {
sp78.width = -sp78.width;
sp78.flags |= FLAGS_MINUS;
}
fmt_ptr++;
} else {
ATOI(sp78.width, fmt_ptr);
}
if (*fmt_ptr != '.') {
sp78.precision = -1;
} else {
fmt_ptr++;
if (*fmt_ptr == '*') {
sp78.precision = va_arg(args, s32);
fmt_ptr++;
} else {
ATOI(sp78.precision, fmt_ptr);
}
}
if (strchr(length_str, *fmt_ptr) != NULL)
sp78.length = *fmt_ptr++;
else
sp78.length = 0;
if (sp78.length == 'l' && *fmt_ptr == 'l') {
sp78.length = 'L';
fmt_ptr++;
}
_Putfld(&sp78, &args, *fmt_ptr, sp4c);
sp78.width -= sp78.part1_len + sp78.num_leading_zeros + sp78.part2_len + sp78.num_mid_zeros
+ sp78.part3_len + sp78.num_trailing_zeros;
_PAD(sp44, sp78.width, sp48, _spaces, !(sp78.flags & FLAGS_MINUS));
_PROUT(dst, (char *) sp4c, sp78.part1_len);
_PAD(sp3c, sp78.num_leading_zeros, sp40, _zeroes, 1);
_PROUT(dst, sp78.buff, sp78.part2_len);
_PAD(sp34, sp78.num_mid_zeros, sp38, _zeroes, 1);
_PROUT(dst, (char *) (&sp78.buff[sp78.part2_len]), sp78.part3_len)
_PAD(sp2c, sp78.num_trailing_zeros, sp30, _zeroes, 1);
_PAD(sp24, sp78.width, sp28, _spaces, sp78.flags & FLAGS_MINUS);
fmt = (char *) fmt_ptr + 1;
}
}
static void _Putfld(printf_struct *a0, va_list *args, u8 type, u8 *buff) {
a0->part1_len = a0->num_leading_zeros = a0->part2_len = a0->num_mid_zeros = a0->part3_len =
a0->num_trailing_zeros = 0;
switch (type) {
case 'c':
buff[a0->part1_len++] = va_arg(*args, u32);
break;
case 'd':
case 'i':
if (a0->length == 'l')
a0->value.s64 = va_arg(*args, s32);
else if (a0->length == 'L')
a0->value.s64 = va_arg(*args, s64);
else
a0->value.s64 = va_arg(*args, s32);
if (a0->length == 'h')
a0->value.s64 = (s16) a0->value.s64;
if (a0->value.s64 < 0)
buff[a0->part1_len++] = '-';
else if (a0->flags & FLAGS_PLUS)
buff[a0->part1_len++] = '+';
else if (a0->flags & FLAGS_SPACE)
buff[a0->part1_len++] = ' ';
a0->buff = (char *) &buff[a0->part1_len];
_Litob(a0, type);
break;
case 'x':
case 'X':
case 'u':
case 'o':
if (a0->length == 'l')
a0->value.s64 = va_arg(*args, s32);
else if (a0->length == 'L')
a0->value.s64 = va_arg(*args, s64);
else
a0->value.s64 = va_arg(*args, s32);
if (a0->length == 'h')
a0->value.s64 = (u16) a0->value.s64;
else if (a0->length == 0)
a0->value.s64 = (u32) a0->value.s64;
if (a0->flags & FLAGS_HASH) {
buff[a0->part1_len++] = '0';
if (type == 'x' || type == 'X')
buff[a0->part1_len++] = type;
}
a0->buff = (char *) &buff[a0->part1_len];
_Litob(a0, type);
break;
case 'e':
case 'f':
case 'g':
case 'E':
case 'G':
//... okay?
a0->value.f64 = a0->length == 'L' ? va_arg(*args, f64) : va_arg(*args, f64);
if (a0->value.u16 & 0x8000) {
buff[a0->part1_len++] = '-';
} else {
if (a0->flags & FLAGS_PLUS)
buff[a0->part1_len++] = '+';
else if (a0->flags & FLAGS_SPACE)
buff[a0->part1_len++] = ' ';
}
a0->buff = (char *) &buff[a0->part1_len];
_Ldtob(a0, type);
break;
case 'n':
if (a0->length == 'h')
*(va_arg(*args, u16 *)) = a0->size;
else if (a0->length == 'l')
*va_arg(*args, u32 *) = a0->size;
else if (a0->length == 'L')
*va_arg(*args, u64 *) = a0->size;
else
*va_arg(*args, u32 *) = a0->size;
break;
case 'p':
a0->value.s64 = (long) va_arg(*args, void *); // void*
a0->buff = (char *) &buff[a0->part1_len];
_Litob(a0, 'x');
break;
case 's':
a0->buff = va_arg(*args, char *);
a0->part2_len = strlen((u8 *) a0->buff);
if (a0->precision >= 0 && a0->part2_len > a0->precision)
a0->part2_len = a0->precision;
break;
case '%':
buff[a0->part1_len++] = '%';
break;
default:
buff[a0->part1_len++] = type;
break;
}
}
+10
View File
@@ -0,0 +1,10 @@
#include "libultra_internal.h"
#include "hardware.h"
s32 __osAiDeviceBusy(void) {
register s32 status = HW_REG(AI_STATUS_REG, u32);
if ((status & AI_STATUS_AI_FULL) != 0)
return 1;
else
return 0;
}
+17
View File
@@ -0,0 +1,17 @@
#include "libultra_internal.h"
s32 __osAtomicDec(u32 *a0) {
s32 sp1c;
s32 sp18;
sp1c = __osDisableInt();
if (*a0 != 0) {
(*a0)--;
sp18 = 1;
} else {
sp18 = 0;
}
__osRestoreInt(sp1c);
return sp18;
}
+25
View File
@@ -0,0 +1,25 @@
#include "libultra_internal.h"
// these don't feel like they belong here
// but it makes the most logical since there was printf data before
OSThread *D_80334890 = NULL;
u32 unknown = -1;
OSThread *D_80334898 = (OSThread *) &D_80334890;
OSThread *D_8033489C = (OSThread *) &D_80334890;
OSThread *D_803348A0 = NULL;
u32 D_803348A4 = 0; // UNKNOWN
void __osDequeueThread(OSThread **queue, OSThread *thread) {
register OSThread **a2;
register OSThread *a3;
a2 = queue;
a3 = *a2;
while (a3 != NULL) {
if (a3 == thread) {
*a2 = thread->next;
return;
}
a2 = &a3->next;
a3 = *a2;
}
}
+37
View File
@@ -0,0 +1,37 @@
#include "libultra_internal.h"
void __osDevMgrMain(void *args) {
OSIoMesg *sp34;
OSMesg sp30;
OSMesg sp2c;
s32 sp28;
OSMgrArgs *sp24;
sp34 = NULL;
sp28 = 0;
sp24 = (OSMgrArgs *) args;
while (1) {
osRecvMesg(sp24->unk08, (OSMesg) &sp34, OS_MESG_BLOCK);
switch (sp34->hdr.type) {
case 11:
osRecvMesg(sp24->unk10, &sp2c, OS_MESG_BLOCK);
sp28 = sp24->dma_func(OS_READ, sp34->devAddr, sp34->dramAddr, sp34->size);
break;
case 12:
osRecvMesg(sp24->unk10, &sp2c, OS_MESG_BLOCK);
sp28 = sp24->dma_func(OS_WRITE, sp34->devAddr, sp34->dramAddr, sp34->size);
break;
case 10:
osSendMesg(sp34->hdr.retQueue, sp34, OS_MESG_NOBLOCK);
sp28 = -1;
break;
default:
sp28 = -1;
break;
}
if (sp28 == 0) {
osRecvMesg(sp24->unk0c, &sp30, OS_MESG_BLOCK);
osSendMesg(sp34->hdr.retQueue, sp34, OS_MESG_NOBLOCK);
osSendMesg(sp24->unk10, NULL, OS_MESG_NOBLOCK);
}
}
}
+24
View File
@@ -0,0 +1,24 @@
#include "libultra_internal.h"
#define PIAccessQueueSize 2
OSMesg osPiMesgBuff[PIAccessQueueSize];
OSMesgQueue gOsPiMessageQueue;
u32 gOsPiAccessQueueCreated = 0;
void __osPiCreateAccessQueue() {
gOsPiAccessQueueCreated = 1;
osCreateMesgQueue(&gOsPiMessageQueue, &osPiMesgBuff[0], PIAccessQueueSize - 1);
osSendMesg(&gOsPiMessageQueue, NULL, OS_MESG_NOBLOCK);
}
void __osPiGetAccess() {
OSMesg sp1c;
if (!gOsPiAccessQueueCreated)
__osPiCreateAccessQueue();
osRecvMesg(&gOsPiMessageQueue, &sp1c, OS_MESG_BLOCK);
}
void __osPiRelAccess() {
osSendMesg(&gOsPiMessageQueue, NULL, OS_MESG_NOBLOCK);
}
+22
View File
@@ -0,0 +1,22 @@
#include "libultra_internal.h"
#define SIAccessQueueSize 2
OSMesg osSiMesgBuff[SIAccessQueueSize];
OSMesgQueue gOsSiMessageQueue;
u32 gOsSiAccessQueueCreated = 0;
void __osSiCreateAccessQueue() {
gOsSiAccessQueueCreated = 1;
osCreateMesgQueue(&gOsSiMessageQueue, &osSiMesgBuff[0], SIAccessQueueSize - 1);
osSendMesg(&gOsSiMessageQueue, NULL, OS_MESG_NOBLOCK);
}
void __osSiGetAccess(void) {
OSMesg sp1c;
if (!gOsSiAccessQueueCreated)
__osSiCreateAccessQueue();
osRecvMesg(&gOsSiMessageQueue, &sp1c, OS_MESG_BLOCK);
}
void __osSiRelAccess(void) {
osSendMesg(&gOsSiMessageQueue, NULL, OS_MESG_NOBLOCK);
}
+12
View File
@@ -0,0 +1,12 @@
#include "libultra_internal.h"
#include "hardware.h"
s32 __osSiDeviceBusy() {
register u32 status;
status = HW_REG(SI_STATUS_REG, u32);
if (status & (SI_STATUS_DMA_BUSY | SI_STATUS_IO_READ_BUSY)) {
return 1;
} else {
return 0;
}
}
+8
View File
@@ -0,0 +1,8 @@
#include "libultra_internal.h"
#include "hardware.h"
s32 __osSiRawReadIo(void *a0, u32 *a1) {
if (__osSiDeviceBusy())
return -1;
*a1 = HW_REG((u32) a0, u32);
return 0;
}
+25
View File
@@ -0,0 +1,25 @@
#include "libultra_internal.h"
#include "hardware.h"
s32 __osSiRawStartDma(s32 dir, void *addr) {
if (__osSiDeviceBusy()) {
return -1;
}
if (dir == OS_WRITE) {
osWritebackDCache(addr, 64);
}
HW_REG(SI_DRAM_ADDR_REG, void *) = (void *) osVirtualToPhysical(addr);
if (dir == OS_READ) {
HW_REG(SI_PIF_ADDR_RD64B_REG, u32) = 0x1FC007C0;
} else {
HW_REG(SI_PIF_ADDR_WR64B_REG, u32) = 0x1FC007C0;
}
if (dir == OS_READ) {
osInvalDCache(addr, 64);
}
return 0;
}
+8
View File
@@ -0,0 +1,8 @@
#include "libultra_internal.h"
#include "hardware.h"
s32 __osSiRawWriteIo(void *a0, u32 a1) {
if (__osSiDeviceBusy())
return -1;
HW_REG((u32) a0, u32) = a1;
return 0;
}
+9
View File
@@ -0,0 +1,9 @@
#include "libultra_internal.h"
#include "hardware.h"
s32 __osSpDeviceBusy() {
register u32 status = HW_REG(SP_STATUS_REG, u32);
if (status & (SPSTATUS_IO_FULL | SPSTATUS_DMA_FULL | SPSTATUS_DMA_BUSY))
return 1;
return 0;
}
+5
View File
@@ -0,0 +1,5 @@
#include "libultra_internal.h"
#include "hardware.h"
u32 __osSpGetStatus() {
return HW_REG(SP_STATUS_REG, u32);
}
+14
View File
@@ -0,0 +1,14 @@
#include "libultra_internal.h"
#include "hardware.h"
s32 __osSpRawStartDma(u32 dir, void *sp_ptr, void *dram_ptr, size_t size) {
if (__osSpDeviceBusy())
return -1;
HW_REG(SP_MEM_ADDR_REG, void *) = sp_ptr;
HW_REG(SP_DRAM_ADDR_REG, void *) = (void *) osVirtualToPhysical(dram_ptr);
if (dir == 0)
HW_REG(SP_WR_LEN_REG, u32) = size - 1;
else
HW_REG(SP_RD_LEN_REG, u32) = size - 1;
return 0;
}
+12
View File
@@ -0,0 +1,12 @@
#include "libultra_internal.h"
#include "hardware.h"
s32 __osSpSetPc(void *pc) {
register u32 status = HW_REG(SP_STATUS_REG, u32);
if (!(status & SPSTATUS_HALT)) {
return -1;
} else {
HW_REG(SP_PC_REG, void *) = pc;
return 0;
}
}
+5
View File
@@ -0,0 +1,5 @@
#include "libultra_internal.h"
#include "hardware.h"
void __osSpSetStatus(u32 status) {
HW_REG(SP_STATUS_REG, u32) = status;
}
+35
View File
@@ -0,0 +1,35 @@
#include "libultra_internal.h"
typedef struct {
u8 unk00 : 2;
u8 pad : 4;
u8 unk01 : 2;
u8 unk2[3];
} unkStruct;
u32 D_80334A40 = 0;
u32 D_80334A44 = 1;
void __osSyncPutChars(s32 a0, s32 a1, u8 *a2) {
unkStruct sp24;
s32 sp20;
u32 sp1c;
sp24.unk00 = a0;
sp24.unk01 = a1;
for (sp20 = 0; sp20 < a1; sp20++) {
sp24.unk2[sp20] = a2[sp20];
}
while (!__osAtomicDec(&D_80334A44))
;
sp1c = __osDisableInt();
*(u32 *) 0xC0000000 = *(u32 *) &sp24;
while (!(__osGetCause() & 0x2000))
;
*(u32 *) 0xC000000C = 0;
D_80334A44++;
__osRestoreInt(sp1c);
}
+5
View File
@@ -0,0 +1,5 @@
#include "libultra_internal.h"
extern OSViContext *D_80334910;
OSViContext *__osViGetCurrentContext() {
return D_80334910;
}
+50
View File
@@ -0,0 +1,50 @@
#include "libultra_internal.h"
#include "hardware.h"
#ifndef VERSION_JP
extern u32 osTvType;
#endif
OSViContext D_803348B0[2] = { 0 };
OSViContext *D_80334910 = &D_803348B0[0];
OSViContext *D_80334914 = &D_803348B0[1];
u32 D_80334918 = TV_TYPE_NTSC;
u32 D_8033491C = 0x02E6D354;
extern OSViMode D_80334990;
extern OSViMode D_803349E0;
void __osViInit(void) {
//#ifdef VERSION_JP
#ifndef VERSION_JP
D_80334918 = osTvType;
#endif
bzero(D_803348B0, sizeof(D_803348B0));
D_80334910 = &D_803348B0[0];
D_80334914 = &D_803348B0[1];
D_80334914->retraceCount = 1;
D_80334910->retraceCount = 1;
#ifdef VERSION_JP
if (D_80334918 != TV_TYPE_PAL)
#else
if (D_80334918 == TV_TYPE_NTSC)
#endif
{
D_80334914->unk08 = &D_80334990;
D_8033491C = 0x02E6D354;
} else {
D_80334914->unk08 = &D_803349E0;
#ifdef VERSION_JP
D_8033491C = 0x02F5B2D2;
#else
D_8033491C = 0x02E6025C;
#endif
}
D_80334914->unk00 = 32;
D_80334914->features = D_80334914->unk08->comRegs.ctrl;
#ifndef VERSION_JP
while (HW_REG(VI_CURRENT_REG, u32) > 0xa)
;
HW_REG(VI_STATUS_REG, u32) = 0;
#endif
__osViSwapContext();
}
+58
View File
@@ -0,0 +1,58 @@
#include "libultra_internal.h"
#include "hardware.h"
extern OSViContext *D_80334914;
extern OSViContext *D_80334910;
void __osViSwapContext() {
register OSViMode *s0;
register OSViContext *s1;
u32 origin;
u32 hStart;
u32 sp34;
u32 field;
register u32 s2;
field = 0;
s1 = D_80334914;
s0 = s1->unk08;
field = HW_REG(VI_V_CURRENT_LINE_REG, u32) & 1;
s2 = osVirtualToPhysical(s1->buffer);
origin = (s0->fldRegs[field].origin) + s2;
if (s1->unk00 & 2) {
s1->unk20 |= s0->comRegs.xScale & ~0xfff;
} else {
s1->unk20 = s0->comRegs.xScale;
}
if (s1->unk00 & 4) {
sp34 = (u32)(s0->fldRegs[field].yScale & 0xfff);
s1->unk2c = s1->unk24 * sp34;
s1->unk2c |= s0->fldRegs[field].yScale & ~0xfff;
} else {
s1->unk2c = s0->fldRegs[field].yScale;
}
hStart = s0->comRegs.hStart;
if (s1->unk00 & 0x20)
hStart = 0;
if (s1->unk00 & 0x40) {
s1->unk2c = 0;
origin = osVirtualToPhysical(s1->buffer);
}
if (s1->unk00 & 0x80) {
s1->unk2c = (s1->unk28 << 0x10) & 0x3ff0000;
origin = osVirtualToPhysical(s1->buffer);
}
HW_REG(VI_ORIGIN_REG, u32) = origin;
HW_REG(VI_WIDTH_REG, u32) = s0->comRegs.width;
HW_REG(VI_BURST_REG, u32) = s0->comRegs.burst;
HW_REG(VI_V_SYNC_REG, u32) = s0->comRegs.vSync;
HW_REG(VI_H_SYNC_REG, u32) = s0->comRegs.hSync;
HW_REG(VI_LEAP_REG, u32) = s0->comRegs.leap;
HW_REG(VI_H_START_REG, u32) = hStart;
HW_REG(VI_V_START_REG, u32) = s0->fldRegs[field].vStart;
HW_REG(VI_V_BURST_REG, u32) = s0->fldRegs[field].vBurst;
HW_REG(VI_INTR_REG, u32) = s0->fldRegs[field].vIntr;
HW_REG(VI_X_SCALE_REG, u32) = s1->unk20;
HW_REG(VI_Y_SCALE_REG, u32) = s1->unk2c;
HW_REG(VI_CONTROL_REG, u32) = s1->features;
D_80334914 = D_80334910;
D_80334910 = s1;
*D_80334914 = *D_80334910;
}
+86
View File
@@ -0,0 +1,86 @@
#include "libultra_internal.h"
#include "libaudio_internal.h"
#define PATCH(SRC, BASE, TYPE) SRC = (TYPE)((u32) SRC + (u32) BASE)
void alSeqFileNew(ALSeqFile *f, u8 *base) {
int i;
for (i = 0; i < f->seqCount; i++) {
f->seqArray[i].offset = (u32)(f->seqArray[i].offset) + base;
}
}
static void _bnkfPatchBank(ALInstrument *inst, ALBankFile *f, u8 *table) {
int i;
ALSound *sound;
ALWaveTable *wavetable;
u8 *table2;
if (inst->flags)
return;
inst->flags = 1;
for (i = 0; i < inst->soundCount; i++) {
PATCH(inst->soundArray[i], f, ALSound *);
sound = inst->soundArray[i];
if (sound->flags)
continue;
table2 = table;
sound->flags = 1;
PATCH(sound->envelope, f, ALEnvelope *);
PATCH(sound->keyMap, f, ALKeyMap *);
PATCH(sound->wavetable, f, ALWaveTable *);
wavetable = sound->wavetable;
if (wavetable->flags)
continue;
wavetable->flags = 1;
PATCH(wavetable->base, table2, u8 *);
if (wavetable->type == 0) {
PATCH(wavetable->waveInfo.adpcmWave.book, f, ALADPCMBook *);
if (wavetable->waveInfo.adpcmWave.loop != NULL)
PATCH(wavetable->waveInfo.adpcmWave.loop, f, ALADPCMloop *);
} else if (wavetable->type == 1) {
if (wavetable->waveInfo.rawWave.loop != NULL)
PATCH(wavetable->waveInfo.rawWave.loop, f, ALRawLoop *);
}
}
}
// Force adding another jr $ra. Has to be called or it doesn't get put in the
// right place.
static void unused() {
}
void alBnkfNew(ALBankFile *f, u8 *table) {
ALBank *bank;
int i;
int j;
unused();
if (f->revision != AL_BANK_VERSION)
return;
for (i = 0; i < f->bankCount; i++) {
PATCH(f->bankArray[i], f, ALBank *);
if (f->bankArray[i] == NULL)
continue;
bank = f->bankArray[i];
if (bank->flags == 0) {
bank->flags = 1;
if (bank->percussion != NULL) {
PATCH(bank->percussion, f, ALInstrument *);
_bnkfPatchBank(bank->percussion, f, table);
}
for (j = 0; j < bank->instCount; j++) {
PATCH(bank->instArray[j], f, ALInstrument *);
if (bank->instArray[j] != NULL) {
_bnkfPatchBank(bank->instArray[j], f, table);
}
}
}
}
}
+114
View File
@@ -0,0 +1,114 @@
/*
* This file originates from the standard Nintendo 64 SDK libultra src
* directory, and it used exclusively when building with the Fast3DEX2
* microcode. Not not using it breaks environment mapping.
*
* Apart from the modifications listed below, this file is unmodified.
*/
/**************************************************************************
* *
* Copyright (C) 1994, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
**************************************************************************/
/* Minor modifications */
#include <ultra64.h>
#define FTOFRAC8(x) ((int) MIN(((x) * (128.0)), 127.0) & 0xff)
void guLookAtReflectF(float mf[4][4], LookAt *l, float xEye, float yEye, float zEye, float xAt,
float yAt, float zAt, float xUp, float yUp, float zUp) {
float len, xLook, yLook, zLook, xRight, yRight, zRight;
guMtxIdentF(mf);
xLook = xAt - xEye;
yLook = yAt - yEye;
zLook = zAt - zEye;
/* Negate because positive Z is behind us: */
len = -1.0 / sqrtf(xLook * xLook + yLook * yLook + zLook * zLook);
xLook *= len;
yLook *= len;
zLook *= len;
/* Right = Up x Look */
xRight = yUp * zLook - zUp * yLook;
yRight = zUp * xLook - xUp * zLook;
zRight = xUp * yLook - yUp * xLook;
len = 1.0 / sqrtf(xRight * xRight + yRight * yRight + zRight * zRight);
xRight *= len;
yRight *= len;
zRight *= len;
/* Up = Look x Right */
xUp = yLook * zRight - zLook * yRight;
yUp = zLook * xRight - xLook * zRight;
zUp = xLook * yRight - yLook * xRight;
len = 1.0 / sqrtf(xUp * xUp + yUp * yUp + zUp * zUp);
xUp *= len;
yUp *= len;
zUp *= len;
/* reflectance vectors = Up and Right */
l->l[0].l.dir[0] = FTOFRAC8(xRight);
l->l[0].l.dir[1] = FTOFRAC8(yRight);
l->l[0].l.dir[2] = FTOFRAC8(zRight);
l->l[1].l.dir[0] = FTOFRAC8(xUp);
l->l[1].l.dir[1] = FTOFRAC8(yUp);
l->l[1].l.dir[2] = FTOFRAC8(zUp);
l->l[0].l.col[0] = 0x00;
l->l[0].l.col[1] = 0x00;
l->l[0].l.col[2] = 0x00;
l->l[0].l.pad1 = 0x00;
l->l[0].l.colc[0] = 0x00;
l->l[0].l.colc[1] = 0x00;
l->l[0].l.colc[2] = 0x00;
l->l[0].l.pad2 = 0x00;
l->l[1].l.col[0] = 0x00;
l->l[1].l.col[1] = 0x80;
l->l[1].l.col[2] = 0x00;
l->l[1].l.pad1 = 0x00;
l->l[1].l.colc[0] = 0x00;
l->l[1].l.colc[1] = 0x80;
l->l[1].l.colc[2] = 0x00;
l->l[1].l.pad2 = 0x00;
mf[0][0] = xRight;
mf[1][0] = yRight;
mf[2][0] = zRight;
mf[3][0] = -(xEye * xRight + yEye * yRight + zEye * zRight);
mf[0][1] = xUp;
mf[1][1] = yUp;
mf[2][1] = zUp;
mf[3][1] = -(xEye * xUp + yEye * yUp + zEye * zUp);
mf[0][2] = xLook;
mf[1][2] = yLook;
mf[2][2] = zLook;
mf[3][2] = -(xEye * xLook + yEye * yLook + zEye * zLook);
mf[0][3] = 0;
mf[1][3] = 0;
mf[2][3] = 0;
mf[3][3] = 1;
}
void guLookAtReflect(Mtx *m, LookAt *l, float xEye, float yEye, float zEye, float xAt, float yAt,
float zAt, float xUp, float yUp, float zUp) {
float mf[4][4];
guLookAtReflectF(mf, l, xEye, yEye, zEye, xAt, yAt, zAt, xUp, yUp, zUp);
guMtxF2L(mf, m);
}
+54
View File
@@ -0,0 +1,54 @@
#include "libultra_internal.h"
void guMtxF2L(float mf[4][4], Mtx *m) {
int r, c;
long tmp1;
long tmp2;
long *m1 = &m->m[0][0];
long *m2 = &m->m[2][0];
for (r = 0; r < 4; r++) {
for (c = 0; c < 2; c++) {
tmp1 = mf[r][2 * c] * 65536.0f;
tmp2 = mf[r][2 * c + 1] * 65536.0f;
*m1++ = (tmp1 & 0xffff0000) | ((tmp2 >> 0x10) & 0xffff);
*m2++ = ((tmp1 << 0x10) & 0xffff0000) | (tmp2 & 0xffff);
}
}
}
void guMtxL2F(float mf[4][4], Mtx *m) {
int r, c;
u32 tmp1;
u32 tmp2;
u32 *m1;
u32 *m2;
s32 stmp1, stmp2;
m1 = (u32 *) &m->m[0][0];
m2 = (u32 *) &m->m[2][0];
for (r = 0; r < 4; r++) {
for (c = 0; c < 2; c++) {
tmp1 = (*m1 & 0xffff0000) | ((*m2 >> 0x10) & 0xffff);
tmp2 = ((*m1++ << 0x10) & 0xffff0000) | (*m2++ & 0xffff);
stmp1 = *(s32 *) &tmp1;
stmp2 = *(s32 *) &tmp2;
mf[r][c * 2 + 0] = stmp1 / 65536.0f;
mf[r][c * 2 + 1] = stmp2 / 65536.0f;
}
}
}
void guMtxIdentF(float mf[4][4]) {
int r, c;
for (r = 0; r < 4; r++) {
for (c = 0; c < 4; c++) {
if (r == c)
mf[r][c] = 1.0f;
else
mf[r][c] = 0.0f;
}
}
}
void guMtxIdent(Mtx *m) {
float mf[4][4];
guMtxIdentF(mf);
guMtxF2L(mf, m);
}
+8
View File
@@ -0,0 +1,8 @@
#include "libultra_internal.h"
void guNormalize(f32 *x, f32 *y, f32 *z) {
f32 tmp = 1.0f / sqrtf(*x * *x + *y * *y + *z * *z);
*x = *x * tmp;
*y = *y * tmp;
*z = *z * tmp;
}
+26
View File
@@ -0,0 +1,26 @@
#include "libultra_internal.h"
void guOrthoF(float m[4][4], float left, float right, float bottom, float top, float near, float far,
float scale) {
int row;
int col;
guMtxIdentF(m);
m[0][0] = 2 / (right - left);
m[1][1] = 2 / (top - bottom);
m[2][2] = -2 / (far - near);
m[3][0] = -(right + left) / (right - left);
m[3][1] = -(top + bottom) / (top - bottom);
m[3][2] = -(far + near) / (far - near);
m[3][3] = 1;
for (row = 0; row < 4; row++) {
for (col = 0; col < 4; col++)
m[row][col] *= scale;
}
}
void guOrtho(Mtx *m, float left, float right, float bottom, float top, float near, float far,
float scale) {
float sp28[4][4];
guOrthoF(sp28, left, right, bottom, top, near, far, scale);
guMtxF2L(sp28, m);
}
+37
View File
@@ -0,0 +1,37 @@
#include "libultra_internal.h"
void guPerspectiveF(float mf[4][4], u16 *perspNorm, float fovy, float aspect, float near, float far,
float scale) {
float yscale;
int row;
int col;
guMtxIdentF(mf);
fovy *= GU_PI / 180.0;
yscale = cosf(fovy / 2) / sinf(fovy / 2);
mf[0][0] = yscale / aspect;
mf[1][1] = yscale;
mf[2][2] = (near + far) / (near - far);
mf[2][3] = -1;
mf[3][2] = 2 * near * far / (near - far);
mf[3][3] = 0.0f;
for (row = 0; row < 4; row++) {
for (col = 0; col < 4; col++) {
mf[row][col] *= scale;
}
}
if (perspNorm != NULL) {
if (near + far <= 2.0) {
*perspNorm = 65535;
} else {
*perspNorm = (double) (1 << 17) / (near + far);
if (*perspNorm <= 0)
*perspNorm = 1;
}
}
}
void guPerspective(Mtx *m, u16 *perspNorm, float fovy, float aspect, float near, float far,
float scale) {
float mat[4][4];
guPerspectiveF(mat, perspNorm, fovy, aspect, near, far, scale);
guMtxF2L(mat, m);
}
+42
View File
@@ -0,0 +1,42 @@
#include "libultra_internal.h"
void guRotateF(float m[4][4], float a, float x, float y, float z) {
float sin_a;
float cos_a;
float sp2c;
float sp28;
float sp24;
float xx, yy, zz;
static float D_80365D70 = GU_PI / 180;
guNormalize(&x, &y, &z);
a = a * D_80365D70;
sin_a = sinf(a);
cos_a = cosf(a);
sp2c = x * y * (1 - cos_a);
sp28 = y * z * (1 - cos_a);
sp24 = z * x * (1 - cos_a);
guMtxIdentF(m);
xx = x * x;
m[0][0] = (1 - xx) * cos_a + xx;
m[2][1] = sp28 - x * sin_a;
m[1][2] = sp28 + x * sin_a;
yy = y * y;
m[1][1] = (1 - yy) * cos_a + yy;
m[2][0] = sp24 + y * sin_a;
m[0][2] = sp24 - y * sin_a;
zz = z * z;
m[2][2] = (1 - zz) * cos_a + zz;
m[1][0] = sp2c - z * sin_a;
m[0][1] = sp2c + z * sin_a;
}
void guRotate(Mtx *m, float a, float x, float y, float z) {
float mf[4][4];
guRotateF(mf, a, x, y, z);
guMtxF2L(mf, m);
}
+14
View File
@@ -0,0 +1,14 @@
#include "libultra_internal.h"
void guScaleF(float mf[4][4], float x, float y, float z) {
guMtxIdentF(mf);
mf[0][0] = x;
mf[1][1] = y;
mf[2][2] = z;
mf[3][3] = 1.0;
}
void guScale(Mtx *m, float x, float y, float z) {
float mf[4][4];
guScaleF(mf, x, y, z);
guMtxF2L(mf, m);
}
+13
View File
@@ -0,0 +1,13 @@
#include "libultra_internal.h"
void guTranslateF(float m[4][4], float x, float y, float z) {
guMtxIdentF(m);
m[3][0] = x;
m[3][1] = y;
m[3][2] = z;
}
void guTranslate(Mtx *m, float x, float y, float z) {
float mf[4][4];
guTranslateF(mf, x, y, z);
guMtxF2L(mf, m);
}
+85
View File
@@ -0,0 +1,85 @@
#ifndef _HARDWARE_H_
#define _HARDWARE_H_
#define HW_REG(reg, type) *(volatile type *)(reg | 0xa0000000)
#define AI_DRAM_ADDR_REG 0x04500000
#define AI_LEN_REG 0x04500004
#define AI_CONTROL_REG 0x04500008
#define AI_STATUS_REG 0x0450000C
#define AI_STATUS_AI_FULL (1 << 31)
#define AI_STATUS_AI_BUSY (1 << 30)
#define AI_DACRATE_REG 0x04500010
#define AI_BITRATE_REG 0x04500014
#define VI_STATUS_REG 0x04400000
#define VI_CONTROL_REG 0x04400000
#define VI_ORIGIN_REG 0x04400004
#define VI_DRAM_ADDR_REG 0x04400004
#define VI_WIDTH_REG 0x04400008
#define VI_H_WIDTH_REG 0x04400008
#define VI_INTR_REG 0x0440000C
#define VI_V_INTER_REG 0x0440000C
#define VI_CURRENT_REG 0x04400010
#define VI_V_CURRENT_LINE_REG 0x04400010
#define VI_BURST_REG 0x04400014
#define VI_TIMING_REG 0x04400014
#define VI_V_SYNC_REG 0x04400018 //VI vertical sync
#define VI_H_SYNC_REG 0x0440001C //VI horizontal sync
#define VI_LEAP_REG 0x04400020 //VI horizontal sync leap
#define VI_H_SYNC_LEAP_REG 0x04400020
#define VI_H_START_REG 0x04400024 //VI horizontal video
#define VI_H_VIDEO_REG 0x04400024
#define VI_V_START_REG 0x04400028 //VI vertical video
#define VI_V_VIDEO_REG 0x04400028
#define VI_V_BURST_REG 0x0440002C //VI vertical burst
#define VI_X_SCALE_REG 0x04400030 //VI x-scale
#define VI_Y_SCALE_REG 0x04400034 //VI y-scale
#define SP_IMEM_START 0x04001000
#define SP_DMEM_START 0x04000000
#define SP_MEM_ADDR_REG 0x04040000
#define SP_DRAM_ADDR_REG 0x04040004
#define SP_RD_LEN_REG 0x04040008
#define SP_WR_LEN_REG 0x0404000C
#define SP_STATUS_REG 0x04040010
#define SP_PC_REG 0x04080000
#define PI_DRAM_ADDR_REG 0x04600000 //PI DRAM address
#define PI_CART_ADDR_REG 0x04600004 //PI pbus (cartridge) address
#define PI_RD_LEN_REG 0x04600008 //PI read length
#define PI_WR_LEN_REG 0x0460000C //PI write length
#define PI_STATUS_REG 0x04600010 //PI status
#define PI_BSD_DOM1_LAT_REG 0x04600014 //PI dom1 latency
#define PI_DOMAIN1_REG 0x04600014
#define PI_BSD_DOM1_PWD_REG 0x04600018 //PI dom1 pulse width
#define PI_BSD_DOM1_PGS_REG 0x0460001C //PI dom1 page size
#define PI_BSD_DOM1_RLS_REG 0x04600020 //PI dom1 release
#define PI_BSD_DOM2_LAT_REG 0x04600024 //PI dom2 latency
#define PI_DOMAIN2_REG 0x04600024
#define PI_BSD_DOM2_PWD_REG 0x04600028 //PI dom2 pulse width
#define PI_BSD_DOM2_PGS_REG 0x0460002C //PI dom2 page size
#define PI_BSD_DOM2_RLS_REG 0x04600030 //PI dom2 release
#define PI_STATUS_BUSY 0x1
#define PI_STATUS_IOBUSY 0x2
#define PI_STATUS_ERROR 0x3
#define SI_DRAM_ADDR_REG 0x04800000
#define SI_PIF_ADDR_RD64B_REG 0x04800004
#define SI_PIF_ADDR_WR64B_REG 0x04800010
#define SI_STATUS_REG 0x04800018
#define SI_STATUS_DMA_BUSY 0x1
#define SI_STATUS_IO_READ_BUSY 0x2
#define SI_STATUS_DMA_ERROR 0x8
#define SI_STATUS_INTERRUPT (1 << 12)
#define MI_INIT_MODE_REG 0x04300000
#define MI_MODE_REG MI_INIT_MODE_REG
#define MI_VERSION_REG 0x04300004
#define MI_INTR_REG 0x04300008
#define MI_INTR_MASK_REG 0x0430000C
#endif
+129
View File
@@ -0,0 +1,129 @@
#include "libultra_internal.h"
typedef struct {
u8 unk00 : 2;
u8 pad : 4;
u8 unk01 : 2;
u8 unk2[3];
} unkStruct;
extern u32 D_80334A44;
u32 D_80334A30 = 0;
u32 D_80334A34 = 0;
s32 D_80334A38 = 0;
u8 D_80365E40[0x100];
OSThread gInterruptedThread;
void u32_to_string(u32 i, u8 *str) {
str[0] = (i >> 0x18) & 0xff;
str[1] = (i >> 0x10) & 0xff;
str[2] = (i >> 0x8) & 0xff;
str[3] = i & 0xff;
}
u32 string_to_u32(u8 *str) {
u32 i;
i = (str[0] & 0xff) << 0x18;
i |= (str[1] & 0xff) << 0x10;
i |= (str[2] & 0xff) << 0x8;
i |= (str[3] & 0xff);
return i;
}
void send_packet(u8 *a0, s32 a1) {
unkStruct sp1c;
s32 i;
sp1c.unk00 = 2;
for (sp1c.unk01 = a1, i = 0; i < a1; i++) {
sp1c.unk2[i] = a0[i];
}
*(volatile u32 *) 0xc0000000 = *(u32 *) &sp1c;
while (!(__osGetCause() & 0x2000))
;
*(volatile u32 *) 0xc000000c = 0;
}
void send(u8 *buff, s32 len) {
s32 i;
s32 end;
s32 rem;
if (!D_80334A44) {
while (!(__osGetCause() & 0x2000))
;
*(volatile u32 *) 0xc000000c = 0;
D_80334A44 = 1;
}
i = 0;
rem = len % 3;
end = len - rem;
for (; i < end; i += 3) {
send_packet(&buff[i], 3);
}
if (rem > 0) {
send_packet(&buff[end], rem);
}
}
void process_command_memory() {
u32 sp1c;
u32 sp18;
sp1c = string_to_u32(&D_80365E40[1]);
sp18 = string_to_u32(&D_80365E40[5]);
send((u8 *) sp1c, sp18);
}
void process_command_register() {
send((u8 *) &gInterruptedThread.context, sizeof(__OSThreadContext));
}
void kdebugserver(u32 a0) {
u32 sp2c;
unkStruct sp28;
*(u32 *) &sp28 = a0;
for (sp2c = 0; sp2c < sp28.unk01; sp2c++) {
D_80365E40[D_80334A34] = sp28.unk2[sp2c];
D_80334A34++;
}
D_80334A38 -= sp28.unk01;
switch (D_80334A30) {
case 0:
switch (sp28.unk2[0]) {
case 1:
D_80334A30 = 1;
D_80334A38 = 9 - sp28.unk01;
break;
case 2:
process_command_register();
D_80334A30 = 0;
D_80334A34 = 0;
D_80334A38 = 0;
break;
default:
D_80334A30 = 0;
D_80334A34 = 0;
D_80334A38 = 0;
break;
}
break;
case 1:
if (D_80334A38 <= 0) {
if (D_80365E40[0] == 1) {
process_command_memory();
D_80334A30 = 0;
D_80334A34 = 0;
D_80334A38 = 0;
} else {
D_80334A30 = 0;
D_80334A34 = 0;
D_80334A38 = 0;
}
}
break;
default:
D_80334A30 = 0;
D_80334A34 = 0;
D_80334A38 = 0;
break;
}
}
+28
View File
@@ -0,0 +1,28 @@
#include "libultra_internal.h"
#include <stdlib.h>
lldiv_t lldiv(long long num, long long denom) {
lldiv_t ret;
ret.quot = num / denom;
ret.rem = num - denom * ret.quot;
if (ret.quot < 0 && ret.rem > 0) {
ret.quot++;
ret.rem -= denom;
}
return ret;
}
ldiv_t ldiv(long num, long denom) {
ldiv_t ret;
ret.quot = num / denom;
ret.rem = num - denom * ret.quot;
if (ret.quot < 0 && ret.rem > 0) {
ret.quot++;
ret.rem -= denom;
}
return ret;
}
+127
View File
@@ -0,0 +1,127 @@
#ifndef _LIBAUDIO_INTERNAL_H_
#define _LIBAUDIO_INTERNAL_H_
#include <ultra64.h>
#define AL_BANK_VERSION 0x4231 /* 'B1' */
typedef u8 ALPan;
typedef s32 ALMicroTime;
/* Possible wavetable types */
enum
{
AL_ADPCM_WAVE = 0,
AL_RAW16_WAVE
};
typedef struct
{
u32 start;
u32 end;
u32 count;
} ALRawLoop;
typedef struct
{
u32 start;
u32 end;
u32 count;
ADPCM_STATE state;
} ALADPCMloop;
typedef struct
{
s32 order;
s32 npredictors;
s16 book[1]; // variable size, 8-byte aligned
} ALADPCMBook;
typedef struct
{
ALMicroTime attackTime;
ALMicroTime decayTime;
ALMicroTime releaseTime;
u8 attackVolume;
u8 decayVolume;
} ALEnvelope;
typedef struct
{
u8 velocityMin;
u8 velocityMax;
u8 keyMin;
u8 keyMax;
u8 keyBase;
s8 detune;
} ALKeyMap;
typedef struct
{
ALADPCMloop *loop;
ALADPCMBook *book;
} ALADPCMWaveInfo;
typedef struct
{
ALRawLoop *loop;
} ALRAWWaveInfo;
typedef struct ALWaveTable_s
{
u8 *base; /* ptr to start of wave data */
s32 len; /* length of data in bytes */
u8 type; /* compression type */
u8 flags; /* offset/address flags */
union {
ALADPCMWaveInfo adpcmWave;
ALRAWWaveInfo rawWave;
} waveInfo;
} ALWaveTable;
typedef struct ALSound_s
{
ALEnvelope *envelope;
ALKeyMap *keyMap;
ALWaveTable *wavetable; /* offset to wavetable struct */
ALPan samplePan;
u8 sampleVolume;
u8 flags;
} ALSound;
typedef struct
{
u8 volume; /* overall volume for this instrument */
ALPan pan; /* 0 = hard left, 127 = hard right */
u8 priority; /* voice priority for this instrument */
u8 flags;
u8 tremType; /* the type of tremelo osc. to use */
u8 tremRate; /* the rate of the tremelo osc. */
u8 tremDepth; /* the depth of the tremelo osc */
u8 tremDelay; /* the delay for the tremelo osc */
u8 vibType; /* the type of tremelo osc. to use */
u8 vibRate; /* the rate of the tremelo osc. */
u8 vibDepth; /* the depth of the tremelo osc */
u8 vibDelay; /* the delay for the tremelo osc */
s16 bendRange; /* pitch bend range in cents */
s16 soundCount; /* number of sounds in this array */
ALSound *soundArray[1];
} ALInstrument;
typedef struct ALBank_s
{
s16 instCount; /* number of programs in this bank */
u8 flags;
u8 pad;
s32 sampleRate; /* e.g. 44100, 22050, etc... */
ALInstrument *percussion; /* default percussion for GM */
ALInstrument *instArray[1]; /* ARRAY of instruments */
} ALBank;
typedef struct
{ /* Note: sizeof won't be correct */
s16 revision; /* format revision of this file */
s16 bankCount; /* number of banks */
ALBank *bankArray[1]; /* ARRAY of bank offsets */
} ALBankFile;
void alBnkfNew(ALBankFile *f, u8 *table);
#endif
+51
View File
@@ -0,0 +1,51 @@
#ifndef _LIBULTRA_INTERNAL_H_
#define _LIBULTRA_INTERNAL_H_
#include <ultra64.h>
typedef struct
{
u32 initialized; //probably something like initialized?
OSThread *mgrThread;
OSMesgQueue *unk08;
OSMesgQueue *unk0c;
OSMesgQueue *unk10;
s32 (*dma_func)(s32,u32,void*,size_t);
u64 force_align;
} OSMgrArgs;
s32 __osDisableInt();
void __osRestoreInt(s32);
void __osEnqueueAndYield(OSThread**);
void __osDequeueThread(OSThread**, OSThread*);
void __osEnqueueThread(OSThread**, OSThread*);
OSThread* __osPopThread(OSThread**);
s32 __osSiRawStartDma(s32, void*);
void __osSiCreateAccessQueue();
void __osSiGetAccess();
void __osSiRelAccess();
u32 __osProbeTLB(void*);
void __osPiCreateAccessQueue();
void __osPiGetAccess();
void __osSetSR(u32);
u32 __osGetSR();
void __osSetFpcCsr(u32);
s32 __osSiRawReadIo(void*, u32*);
s32 __osSiRawWriteIo(void*, u32);
s32 osPiRawReadIo(u32 a0, u32 *a1);
void __osSpSetStatus(u32);
u32 __osSpGetStatus();
s32 __osSpSetPc(void*);
s32 __osSpDeviceBusy();
s32 __osSiDeviceBusy();
s32 __osSpRawStartDma(u32 dir, void *sp_ptr, void *dram_ptr, size_t size);
void __osViInit();
OSViContext * __osViGetCurrentContext();
void __osViSwapContext();
void __osSetTimerIntr(u64);
u64 __osInsertTimer(OSTimer *);
void __osSetCompare(u32);
s32 __osAiDeviceBusy();
void __osDispatchThread();
u32 __osGetCause();
s32 __osAtomicDec(u32*);
#endif
+94
View File
@@ -0,0 +1,94 @@
// These unions are necessary to put the constants in .rodata rather than .data.
// TODO: is it possible to remove them somehow?
typedef union {
/* 0x0 */ double d;
/* 0x0 */ struct
{
/* 0x0 */ unsigned int hi;
/* 0x4 */ unsigned int lo;
} word;
} du;
typedef union {
/* 0x0 */ float f;
/* 0x0 */ unsigned int i;
} fu;
static const du P[5] = {{1.0},
{-0.16666659550427756},
{0.008333066246082155},
{-1.980960290193795E-4},
{2.605780637968037E-6}};
static const du rpi = {0.3183098861837907};
static const du pihi = {
3.1415926218032837};
static const du pilo = {
3.178650954705639E-8};
static const fu zero = {0.0};
extern const fu NAN;
float cosf(float x)
{
double dx; // double x
double xsq; // x squared
double poly;
double dn;
float xabs;
int n;
double result;
int ix; // int x
int xpt;
ix = *(int *)&x;
xpt = (ix >> 22) & 0x1FF;
if (xpt < 310)
{
if (0 < x)
xabs = x;
else
xabs = -x;
dx = xabs;
dn = dx * rpi.d + .5;
if (0 <= dn)
{
n = dn + .5;
}
else
{
n = dn - .5;
}
dn = n;
dx -= (dn - .5) * pihi.d;
dx -= (dn - .5) * pilo.d;
xsq = dx * dx;
poly = (((((P[4].d * xsq) + P[3].d) * xsq) + P[2].d) * xsq) + P[1].d;
result = ((dx * xsq) * poly) + dx;
if ((n & 0x1) == 0)
{
return result;
}
else
{
return -(float)result;
}
}
if (x != x)
{
return NAN.f;
}
return zero.f;
}
+34
View File
@@ -0,0 +1,34 @@
typedef signed long long int s64;
typedef unsigned long long int u64;
s64 __d_to_ll(double d)
{
return d;
}
s64 __f_to_ll(float f)
{
return f;
}
u64 __d_to_ull(double d)
{
return d;
}
u64 __f_to_ull(float f)
{
return f;
}
double __ll_to_d(s64 s)
{
return s;
}
float __ll_to_f(s64 s)
{
return s;
}
double __ull_to_d(u64 u)
{
return u;
}
float __ull_to_f(u64 u)
{
return u;
}
+53
View File
@@ -0,0 +1,53 @@
unsigned long long __ull_rshift(unsigned long long a0, unsigned long long a1)
{
return a0 >> a1;
}
unsigned long long __ull_rem(unsigned long long a0, unsigned long long a1)
{
return a0 % a1;
}
unsigned long long __ull_div(unsigned long long a0, unsigned long long a1)
{
return a0 / a1;
}
unsigned long long __ll_lshift(unsigned long long a0, unsigned long long a1)
{
return a0 << a1;
}
long long __ll_rem(unsigned long long a0, long long a1)
{
return a0 % a1;
}
long long __ll_div(long long a0, long long a1)
{
return a0 / a1;
}
unsigned long long __ll_mul(unsigned long long a0, unsigned long long a1)
{
return a0 * a1;
}
void __ull_divremi(unsigned long long *div, unsigned long long *rem, unsigned long long a2, unsigned short a3)
{
*div = a2 / a3;
*rem = a2 % a3;
}
long long __ll_mod(long long a0, long long a1)
{
long long tmp = a0 % a1;
if ((tmp < 0 && a1 > 0) || (tmp > 0 && a1 < 0))
{
tmp += a1;
}
return tmp;
}
long long __ll_rshift(long long a0, long long a1)
{
return a0 >> a1;
}
+111
View File
@@ -0,0 +1,111 @@
// These unions are necessary to put the constants in .rodata rather than .data.
// TODO: is it possible to remove them somehow?
typedef union {
/* 0x0 */ double d;
/* 0x0 */ struct
{
/* 0x0 */ unsigned int hi;
/* 0x4 */ unsigned int lo;
} word;
} du;
typedef union {
/* 0x0 */ float f;
/* 0x0 */ unsigned int i;
} fu;
static const du P[5] = {{1.0},
{-0.16666659550427756},
{0.008333066246082155},
{-1.980960290193795E-4},
{2.605780637968037E-6}};
static const du rpi = {0.3183098861837907};
static const du pihi = {
3.1415926218032837};
static const du pilo = {
3.178650954705639E-8};
static const fu zero = {0.0};
extern const fu NAN;
float sinf(float x)
{
double dx; // double x
double xsq; // x squared
double poly;
double dn;
int n;
double result;
int ix; // int x
int xpt;
ix = *(int *)&x;
xpt = (ix >> 22) & 0x1FF;
if (xpt < 255)
{
dx = x;
if (xpt >= 230)
{
xsq = dx * dx;
poly = (((((P[4].d * xsq) + P[3].d) * xsq) + P[2].d) * xsq) + P[1].d;
result = ((dx * xsq) * poly) + dx;
return result;
}
else
{
return x;
}
}
if (xpt < 310)
{
dx = x;
dn = dx * rpi.d;
if (dn >= 0)
{
n = dn + 0.5;
}
else
{
n = dn - 0.5;
}
dn = n;
dx -= dn * pihi.d;
dx -= dn * pilo.d;
xsq = dx * dx;
poly = (((((P[4].d * xsq) + P[3].d) * xsq) + P[2].d) * xsq) + P[1].d;
result = ((dx * xsq) * poly) + dx;
if ((n & 0x1) == 0)
{
return result;
}
else
{
return -(float)result;
}
}
if (x != x)
{
return NAN.f;
}
return zero.f;
}
+7
View File
@@ -0,0 +1,7 @@
#ifndef _OSAI_H_
#define _OSAI_H_
s32 osAiSetFrequency(u32);
s32 osAiSetNextBuffer(void *, u32);
u32 osAiGetLength(void);
#endif
+7
View File
@@ -0,0 +1,7 @@
#include "libultra_internal.h"
#include "osAi.h"
#include "hardware.h"
u32 osAiGetLength() {
return HW_REG(AI_LEN_REG, u32);
}
+32
View File
@@ -0,0 +1,32 @@
#include "libultra_internal.h"
#include "osAi.h"
#include "hardware.h"
extern s32 D_8033491C;
s32 osAiSetFrequency(u32 freq) {
register u32 a1;
register s32 a2;
register float ftmp;
ftmp = D_8033491C / (float) freq + .5f;
a1 = ftmp;
if (a1 < 0x84)
return -1;
a2 = (a1 / 66) & 0xff;
if (a2 > 16)
a2 = 16;
HW_REG(AI_DACRATE_REG, u32) = a1 - 1;
HW_REG(AI_BITRATE_REG, u32) = a2 - 1;
HW_REG(AI_CONTROL_REG, u32) = 1; // enable dma
return D_8033491C / (s32) a1;
}
// put some extra jr $ra's down there please
static void filler1(void) {
}
static void filler2(void) {
}
+39
View File
@@ -0,0 +1,39 @@
#include "libultra_internal.h"
#include "osAi.h"
#include "hardware.h"
u8 D_80334820 = 0;
/**
* It is worth noting that a previous hardware bug has been fixed by a software
* patch in osAiSetNextBuffer. This bug occurred when the address of the end of the
* buffer specified by osAiSetNextBuffer was at a specific value. This value
* occurred when the following was true:
*
* (vaddr + nbytes) & 0x00003FFF == 0x00002000
*
* (when the buffer ends with address of lower 14 bits 0x2000) In this case, the
* DMA transfer does not complete successfully. This can cause clicks and pops in
* the audio output. This bug no longer requires special handling by the application
* because it is now patched by osAiSetNextBuffer.
*/
s32 osAiSetNextBuffer(void *buff, u32 len) {
u8 *sp1c = buff;
if (D_80334820 != 0) {
sp1c -= 0x2000;
}
if ((((u32) buff + len) & 0x3fff) == 0x2000) {
D_80334820 = 1;
} else {
D_80334820 = 0;
}
if (__osAiDeviceBusy())
return -1;
HW_REG(AI_DRAM_ADDR_REG, void *) = (void *) osVirtualToPhysical(sp1c);
HW_REG(AI_LEN_REG, u32) = len;
return 0;
}
+96
View File
@@ -0,0 +1,96 @@
#include "libultra_internal.h"
#include "osContInternal.h"
void __osPackRequestData(u8);
void __osContGetInitData(u8 *, OSContStatus *);
u32 D_80334810 = 0; // probably initialized
extern u32 D_80365D1C;
extern u64 osClockRate;
// these probably belong in EEPROMlongread or something
u8 D_80365D20;
u8 _osCont_numControllers;
OSTimer D_80365D28; // some kind of buffer, or maybe an as yet unknown struct
OSMesgQueue _osContMesgQueue;
OSMesg _osContMesgBuff[4];
s32 osContInit(OSMesgQueue *mq, u8 *a1, OSContStatus *status) {
OSMesg mesg;
u32 sp78 = 0;
OSTime currentTime;
OSTimer sp50;
OSMesgQueue sp38;
if (D_80334810) {
return 0;
}
D_80334810 = 1;
currentTime = osGetTime();
if (500000 * osClockRate / 1000000 > currentTime) {
osCreateMesgQueue(&sp38, &mesg, 1);
osSetTimer(&sp50, 500000 * osClockRate / 1000000 - currentTime, 0, &sp38, &mesg);
osRecvMesg(&sp38, &mesg, OS_MESG_BLOCK);
}
_osCont_numControllers = 4; // TODO: figure out what it means
#ifdef VERSION_EU
__osPackRequestData(0);
#else
__osPackRequestData(255);
#endif
sp78 = __osSiRawStartDma(1, D_80365CE0);
osRecvMesg(mq, &mesg, OS_MESG_BLOCK);
sp78 = __osSiRawStartDma(0, D_80365CE0);
osRecvMesg(mq, &mesg, OS_MESG_BLOCK);
__osContGetInitData(a1, status);
D_80365D20 = 255;
__osSiCreateAccessQueue();
osCreateMesgQueue(&_osContMesgQueue, _osContMesgBuff, 1);
return sp78;
}
void __osContGetInitData(u8 *a0, OSContStatus *status) {
OSContPackedRequest *sp14;
OSContPackedRequest spc;
s32 i;
u8 sp7;
sp7 = 0;
sp14 = &(D_80365CE0[0].request);
for (i = 0; i < _osCont_numControllers; i++, sp14++, status++) {
spc = *(OSContPackedRequest *) sp14;
status->errno = (spc.unk02 & 0xc0) >> 4;
if (status->errno == 0) {
status->type = spc.unk05 << 8 | spc.unk04;
status->status = spc.unk06;
sp7 |= 1 << i;
}
}
*a0 = sp7;
}
void __osPackRequestData(u8 a0) {
OSContPackedRequest *spc;
OSContPackedRequest sp4;
s32 i;
// some kind of weird zeroing code
for (i = 0; i < 0x10; i++) {
*((u32 *) &D_80365CE0 + i) = 0;
}
D_80365D1C = 1;
spc = &D_80365CE0[0].request;
sp4.unk00 = 255;
sp4.unk01 = 1;
sp4.unk02 = 3;
sp4.unk03 = a0;
sp4.unk04 = 255;
sp4.unk05 = 255;
sp4.unk06 = 255;
sp4.unk07 = 255;
for (i = 0; i < _osCont_numControllers; i++) {
*spc++ = sp4;
}
spc->unk00 = 254;
}
+35
View File
@@ -0,0 +1,35 @@
#ifndef _ULTRA64_CONTROLLER_INTERNAL_H_
#define _ULTRA64_CONTROLLER_INTERNAL_H_
#include <types.h>
typedef struct
{
u8 unk00; //unknown, set to 255 a lot
u8 unk01;
u8 unk02; //probably status
u8 unk03; //maybe errno
u16 button;
s8 rawStickX;
s8 rawStickY;
} OSContPackedRead;
typedef struct
{
u8 unk00; //unknown, set to 255 a lot
u8 unk01;
u8 unk02; //probably status
u8 unk03; //maybe errno
u8 unk04;
u8 unk05;
u8 unk06;
u8 unk07;
} OSContPackedRequest;
typedef union {
OSContPackedRead read;
OSContPackedRequest request;
} OSContPackedStruct;
extern OSContPackedStruct D_80365CE0[7];
#endif
+67
View File
@@ -0,0 +1,67 @@
#include "libultra_internal.h"
#include "osContInternal.h"
#include <macros.h>
OSContPackedStruct D_80365CE0[7];
UNUSED static u32 unused; // padding between these two variables
u32 D_80365D1C;
extern u8 D_80365D20;
extern u8 _osCont_numControllers;
void __osPackReadData(void);
s32 osContStartReadData(OSMesgQueue *mesg) {
s32 ret = 0;
s32 i;
__osSiGetAccess();
if (D_80365D20 != 1) {
__osPackReadData();
ret = __osSiRawStartDma(1, D_80365CE0);
osRecvMesg(mesg, NULL, OS_MESG_BLOCK);
}
for (i = 0; i < 0x10; i++) {
*((u32 *) &D_80365CE0 + i) = 255;
}
D_80365D1C = 0;
ret = __osSiRawStartDma(0, &D_80365CE0);
D_80365D20 = 1;
__osSiRelAccess();
return ret;
}
void osContGetReadData(OSContPad *pad) {
OSContPackedRead *spc;
OSContPackedRead sp4;
s32 i;
spc = &D_80365CE0[0].read;
for (i = 0; i < _osCont_numControllers; i++, spc++, pad++) {
sp4 = *spc;
pad->errno = (sp4.unk02 & 0xc0) >> 4;
if (pad->errno == 0) {
pad->button = sp4.button;
pad->stick_x = sp4.rawStickX;
pad->stick_y = sp4.rawStickY;
}
};
}
void __osPackReadData() {
OSContPackedRead *spc;
OSContPackedRead sp4;
s32 i;
spc = &D_80365CE0[0].read;
for (i = 0; i < 0x10; i++) {
*((u32 *) &D_80365CE0 + i) = 0;
}
D_80365D1C = 1;
sp4.unk00 = 255;
sp4.unk01 = 1;
sp4.unk02 = 4;
sp4.unk03 = 1;
sp4.button = 65535;
sp4.rawStickX = -1;
sp4.rawStickY = -1;
for (i = 0; i < _osCont_numControllers; i++) {
*spc++ = sp4;
}
spc->unk00 = 254;
}
+13
View File
@@ -0,0 +1,13 @@
#include "libultra_internal.h"
extern OSThread *D_80334890;
void osCreateMesgQueue(OSMesgQueue *mq, OSMesg *msgBuf, s32 count) {
mq->mtqueue = (OSThread *) &D_80334890; //?
mq->fullqueue = (OSThread *) &D_80334890;
mq->validCount = 0;
mq->first = 0;
mq->msgCount = count;
mq->msg = msgBuf;
return;
}
+47
View File
@@ -0,0 +1,47 @@
#include "libultra_internal.h"
#define OS_PI_MGR_MESG_BUFF_SIZE 1
OSMgrArgs piMgrArgs = { 0 };
OSThread piMgrThread;
u32 piMgrStack[0x400]; // stack bottom
OSMesgQueue __osPiMesgQueue;
OSMesg piMgrMesgBuff[OS_PI_MGR_MESG_BUFF_SIZE + 1];
s32 osPiRawStartDma(s32 dir, u32 cart_addr, void *dram_addr, size_t size);
extern u32 gOsPiAccessQueueCreated;
extern OSMesgQueue gOsPiMessageQueue;
void __osDevMgrMain(void *);
void osCreatePiManager(OSPri pri, OSMesgQueue *cmdQ, OSMesg *cmdBuf, s32 cmdMsgCnt) {
u32 int_disabled;
OSPri newPri;
OSPri currentPri;
if (!piMgrArgs.initialized) {
osCreateMesgQueue(cmdQ, cmdBuf, cmdMsgCnt);
osCreateMesgQueue(&__osPiMesgQueue, &piMgrMesgBuff[0], OS_PI_MGR_MESG_BUFF_SIZE);
if (!gOsPiAccessQueueCreated) {
__osPiCreateAccessQueue();
} // what is this constant geez
osSetEventMesg(OS_EVENT_PI, &__osPiMesgQueue, (void *) 0x22222222);
newPri = -1;
currentPri = osGetThreadPri(NULL);
if (currentPri < pri) {
newPri = currentPri;
osSetThreadPri(NULL, pri);
}
int_disabled = __osDisableInt();
piMgrArgs.initialized = TRUE;
piMgrArgs.mgrThread = &piMgrThread;
piMgrArgs.unk08 = cmdQ;
piMgrArgs.unk0c = &__osPiMesgQueue;
piMgrArgs.unk10 = &gOsPiMessageQueue;
piMgrArgs.dma_func = osPiRawStartDma;
osCreateThread(&piMgrThread, 0, __osDevMgrMain, (void *) &piMgrArgs, &piMgrStack[0x400], pri);
osStartThread(&piMgrThread);
__osRestoreInt(int_disabled);
if (newPri != -1) {
osSetThreadPri(NULL, newPri);
}
}
}
+35
View File
@@ -0,0 +1,35 @@
#include "libultra_internal.h"
extern OSThread *D_8033489C;
void __osCleanupThread(void);
// Don't warn about pointer->u64 cast
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
void osCreateThread(OSThread *thread, OSId id, void (*entry)(void *), void *arg, void *sp, OSPri pri) {
register u32 int_disabled;
u32 tmp;
thread->id = id;
thread->priority = pri;
thread->next = NULL;
thread->queue = NULL;
thread->context.pc = (u32) entry;
thread->context.a0 = (u64) arg;
thread->context.sp = (u64) sp - 16;
thread->context.ra = (u64) __osCleanupThread;
tmp = 0x003FFF01;
thread->context.sr = 65283;
thread->context.rcp = (tmp & 0x3f0000) >> 16;
thread->context.fpcsr = (u32) 0x01000800;
thread->fp = 0;
thread->state = OS_STATE_STOPPED;
thread->flags = 0;
int_disabled = __osDisableInt();
thread->tlnext = D_8033489C;
D_8033489C = thread;
__osRestoreInt(int_disabled);
}
#pragma GCC diagnostic pop
File diff suppressed because it is too large. Load diff
+40
View File
@@ -0,0 +1,40 @@
#include "libultra_internal.h"
extern OSThread *D_803348A0;
extern OSThread *D_8033489C;
void osDestroyThread(OSThread *thread) {
register s32 int_disabled;
register OSThread *s1;
register OSThread *s2;
int_disabled = __osDisableInt();
if (thread == NULL) {
thread = D_803348A0;
} else if (thread->state != OS_STATE_STOPPED) {
__osDequeueThread(thread->queue, thread);
}
if (D_8033489C == thread) {
D_8033489C = D_8033489C->tlnext;
} else {
s1 = D_8033489C;
s2 = s1->tlnext;
while (s2 != NULL) {
if (s2 == thread) {
s1->tlnext = thread->tlnext;
break;
} else {
s1 = s2;
s2 = s1->tlnext;
}
}
}
if (thread == D_803348A0) {
__osDispatchThread();
}
__osRestoreInt(int_disabled);
}
+29
View File
@@ -0,0 +1,29 @@
#include "libultra_internal.h"
extern u64 osClockRate;
extern u8 D_80365D20;
extern u8 _osCont_numControllers;
extern OSTimer D_80365D28; // not sure what this is yet
extern OSMesgQueue _osContMesgQueue;
extern OSMesg _osContMesgBuff[4];
s32 osEepromLongRead(OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) {
s32 status = 0;
if (address > 0x40) {
return -1;
}
while (nbytes > 0) {
status = osEepromRead(mq, address, buffer);
if (status != 0) {
return status;
}
nbytes -= 8;
address += 1;
buffer += 8;
osSetTimer(&D_80365D28, 12000 * osClockRate / 1000000, 0, &_osContMesgQueue, _osContMesgBuff);
osRecvMesg(&_osContMesgQueue, NULL, OS_MESG_BLOCK);
}
return status;
}
+30
View File
@@ -0,0 +1,30 @@
#include "libultra_internal.h"
extern u64 osClockRate;
extern u8 D_80365D20;
extern u8 _osCont_numControllers;
extern OSTimer D_80365D28;
extern OSMesgQueue _osContMesgQueue;
extern OSMesg _osContMesgBuff[4];
// exactly the same as osEepromLongRead except for osEepromWrite call
s32 osEepromLongWrite(OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) {
s32 result = 0;
if (address > 0x40) {
return -1;
}
while (nbytes > 0) {
result = osEepromWrite(mq, address, buffer);
if (result != 0) {
return result;
}
nbytes -= 8;
address += 1;
buffer += 8;
osSetTimer(&D_80365D28, 12000 * osClockRate / 1000000, 0, &_osContMesgQueue, _osContMesgBuff);
osRecvMesg(&_osContMesgQueue, NULL, OS_MESG_BLOCK);
}
return result;
}
+24
View File
@@ -0,0 +1,24 @@
#include "libultra_internal.h"
// TODO: merge with osEepromWrite
typedef struct {
u16 unk00;
u8 unk02;
u8 unk03;
} unkStruct;
s32 __osEepStatus(OSMesgQueue *, unkStruct *);
s32 osEepromProbe(OSMesgQueue *mq) {
s32 status = 0;
unkStruct sp18;
__osSiGetAccess();
status = __osEepStatus(mq, &sp18);
if (status == 0 && (sp18.unk00 & 0x8000) != 0) {
status = 1;
} else {
status = 0;
}
__osSiRelAccess();
return status;
}
+100
View File
@@ -0,0 +1,100 @@
#include "libultra_internal.h"
extern u32 D_80365E00[15];
extern u32 D_80365E3C;
extern u8 D_80365D20;
typedef struct {
u16 unk00;
u8 unk02;
u8 unk03;
} unkStruct;
typedef struct {
u8 unk00;
u8 unk01;
u8 unk02;
u8 unk03;
u8 unk04;
u8 unk05;
u8 unk06;
u8 unk07;
} unkStruct3;
typedef struct {
u8 unk00;
u8 unk01;
u8 unk02;
u8 unk03;
unkStruct3 unk04;
} unkStruct2;
s32 __osEepStatus(OSMesgQueue *, unkStruct *);
s32 __osPackEepReadData(u8);
s32 osEepromRead(OSMesgQueue *mq, u8 address, u8 *buffer) {
s32 sp34;
s32 sp30;
u8 *sp2c;
unkStruct sp28;
unkStruct2 sp20;
sp34 = 0;
sp30 = 0;
sp2c = (u8 *) &D_80365E00;
if (address > 0x40)
return -1;
__osSiGetAccess();
sp34 = __osEepStatus(mq, &sp28);
if (sp34 != 0 || sp28.unk00 != 0x8000) {
return 8;
}
while (sp28.unk02 & 0x80) {
__osEepStatus(mq, &sp28);
}
__osPackEepReadData(address);
sp34 = __osSiRawStartDma(OS_WRITE, &D_80365E00);
osRecvMesg(mq, NULL, OS_MESG_BLOCK);
for (sp30 = 0; sp30 < 0x10; sp30++) {
(D_80365E00)[sp30] = 255;
}
D_80365E3C = 0;
sp34 = __osSiRawStartDma(OS_READ, D_80365E00);
D_80365D20 = 4;
osRecvMesg(mq, NULL, OS_MESG_BLOCK);
for (sp30 = 0; sp30 < 4; sp30++) {
sp2c++;
}
sp20 = *(unkStruct2 *) sp2c;
sp34 = (sp20.unk01 & 0xc0) >> 4;
if (sp34 == 0) {
for (sp30 = 0; sp30 < 8; sp30++) {
*buffer++ = ((u8 *) &sp20.unk04)[sp30];
}
}
__osSiRelAccess();
return sp34;
}
s32 __osPackEepReadData(u8 address) {
u8 *sp14;
unkStruct2 sp8;
s32 sp4;
sp14 = (u8 *) &D_80365E00;
for (sp4 = 0; sp4 < 0x10; sp4++) {
D_80365E00[sp4] = 255;
}
D_80365E3C = 1;
sp8.unk00 = 2;
sp8.unk01 = 8;
sp8.unk02 = 4;
sp8.unk03 = address;
for (sp4 = 0; sp4 < 8; sp4++) {
((u8 *) &sp8.unk04)[sp4] = 0;
}
for (sp4 = 0; sp4 < 4; sp4++) {
*sp14++ = 0;
}
*(unkStruct2 *) sp14 = sp8;
sp14 += 0xc;
*sp14 = 254;
}
+161
View File
@@ -0,0 +1,161 @@
#include "libultra_internal.h"
#include "osContInternal.h"
u32 D_80365E00[0x3c >> 2];
u32 D_80365E3C;
extern u8 D_80365D20;
typedef struct {
u16 unk00;
u8 unk02;
u8 unk03;
} unkStruct;
typedef struct {
u8 unk00;
u8 unk01;
u8 unk02;
u8 unk03;
u8 unk04;
u8 unk05;
u8 unk06;
u8 unk07;
} unkStruct3;
typedef struct {
u8 unk00;
u8 unk01;
u8 unk02;
u8 unk03;
unkStruct3 unk04;
} unkStruct2;
s32 __osEepStatus(OSMesgQueue *, unkStruct *);
s32 __osPackEepWriteData(u8, u8 *);
s32 osEepromWrite(OSMesgQueue *mq, u8 address, u8 *buffer) {
s32 sp34;
s32 sp30;
u8 *sp2c;
unkStruct2 sp20;
unkStruct sp1c;
sp34 = 0;
sp2c = (u8 *) &D_80365E00;
if (address > 0x40) {
return -1;
}
__osSiGetAccess();
sp34 = __osEepStatus(mq, &sp1c);
if (sp34 != 0 || sp1c.unk00 != 0x8000) {
return 8;
}
while (sp1c.unk02 & 0x80) {
__osEepStatus(mq, &sp1c);
}
__osPackEepWriteData(address, buffer);
sp34 = __osSiRawStartDma(OS_WRITE, &D_80365E00);
osRecvMesg(mq, NULL, OS_MESG_BLOCK);
for (sp30 = 0; sp30 < 0x10; sp30++) {
(D_80365E00)[sp30] = 255;
}
D_80365E3C = 0;
sp34 = __osSiRawStartDma(OS_READ, D_80365E00);
D_80365D20 = 5;
osRecvMesg(mq, NULL, OS_MESG_BLOCK);
for (sp30 = 0; sp30 < 4; sp30++) {
sp2c++;
}
sp20 = *(unkStruct2 *) sp2c;
sp34 = (sp20.unk01 & 0xc0) >> 4;
__osSiRelAccess();
return sp34;
}
s32 __osPackEepWriteData(u8 address, u8 *buffer) {
u8 *sp14;
unkStruct2 sp8;
s32 sp4;
sp14 = (u8 *) &D_80365E00;
for (sp4 = 0; sp4 < 0x10; sp4++) {
D_80365E00[sp4] = 255;
}
D_80365E3C = 1;
sp8.unk00 = 10;
sp8.unk01 = 1;
sp8.unk02 = 5;
sp8.unk03 = address;
for (sp4 = 0; sp4 < 8; sp4++) {
((u8 *) &sp8.unk04)[sp4] = *buffer++;
}
for (sp4 = 0; sp4 < 4; sp4++) {
*sp14++ = 0;
}
*(unkStruct2 *) sp14 = sp8;
sp14 += 0xc;
*sp14 = 254;
}
s32 __osEepStatus(OSMesgQueue *a0, unkStruct *a1) {
u32 sp2c = 0;
s32 sp28;
u8 *sp24 = (u8 *) D_80365E00;
unkStruct3 sp1c;
for (sp28 = 0; sp28 < 0x10; sp28++) {
D_80365E00[sp28] = 0;
}
D_80365E3C = 1;
sp24 = (u8 *) D_80365E00;
for (sp28 = 0; sp28 < 4; sp28++) {
*sp24++ = 0;
}
sp1c.unk00 = 255;
sp1c.unk01 = 1;
sp1c.unk02 = 3;
sp1c.unk03 = 0;
sp1c.unk04 = 255;
sp1c.unk05 = 255;
sp1c.unk06 = 255;
sp1c.unk07 = 255;
*(unkStruct3 *) sp24 = sp1c;
sp24 += 8;
sp24[0] = 254;
sp2c = __osSiRawStartDma(OS_WRITE, D_80365E00);
osRecvMesg(a0, NULL, OS_MESG_BLOCK);
D_80365D20 = 5;
sp2c = __osSiRawStartDma(OS_READ, D_80365E00);
osRecvMesg(a0, NULL, OS_MESG_BLOCK);
if (sp2c != 0) {
return sp2c;
}
sp24 = (u8 *) D_80365E00;
for (sp28 = 0; sp28 < 4; sp28++) {
*sp24++ = 0;
}
sp1c = *(unkStruct3 *) sp24;
a1->unk03 = (sp1c.unk02 & 0xc0) >> 4;
a1->unk00 = (sp1c.unk05 << 8) | sp1c.unk04;
a1->unk02 = sp1c.unk06;
if (a1->unk03 != 0) {
return a1->unk03;
}
return 0;
}
+10
View File
@@ -0,0 +1,10 @@
#include "libultra_internal.h"
extern OSThread *D_803348A0;
OSPri osGetThreadPri(OSThread *thread) {
if (thread == NULL) {
thread = D_803348A0;
}
return thread->priority;
}
+17
View File
@@ -0,0 +1,17 @@
#include "libultra_internal.h"
extern OSTime _osCurrentTime;
extern u32 D_80365DA8;
OSTime osGetTime() {
u32 sp34;
u32 sp30;
OSTime sp28;
register u32 int_disabled;
int_disabled = __osDisableInt();
sp34 = osGetCount();
sp30 = sp34 - D_80365DA8;
sp28 = _osCurrentTime;
__osRestoreInt(int_disabled);
return sp28 + sp30;
}
+54
View File
@@ -0,0 +1,54 @@
#include "libultra_internal.h"
#include <macros.h>
#define PIF_ADDR_START (void *) 0x1FC007FC
typedef struct {
u32 instr00;
u32 instr01;
u32 instr02;
u32 instr03;
} exceptionPreamble;
u32 D_80365CD0; // maybe initialized?
u64 osClockRate = 62500000;
u32 D_80334808 = 0;
#define EXCEPTION_TLB_MISS 0x80000000
#define EXCEPTION_XTLB_MISS 0x80000080
#define EXCEPTION_CACHE_ERROR 0x80000100
#define EXCEPTION_GENERAL 0x80000180
extern u32 osResetType;
extern exceptionPreamble __osExceptionPreamble;
void osInitialize(void) {
u32 sp34;
u32 sp30 = 0;
UNUSED u32 sp2c;
D_80365CD0 = TRUE;
__osSetSR(__osGetSR() | 0x20000000);
__osSetFpcCsr(0x01000800);
while (__osSiRawReadIo(PIF_ADDR_START, &sp34))
;
while (__osSiRawWriteIo(PIF_ADDR_START, sp34 | 8))
;
*(exceptionPreamble *) EXCEPTION_TLB_MISS = __osExceptionPreamble;
*(exceptionPreamble *) EXCEPTION_XTLB_MISS = __osExceptionPreamble;
*(exceptionPreamble *) EXCEPTION_CACHE_ERROR = __osExceptionPreamble;
*(exceptionPreamble *) EXCEPTION_GENERAL = __osExceptionPreamble;
osWritebackDCache((void *) 0x80000000,
EXCEPTION_GENERAL + sizeof(exceptionPreamble) - EXCEPTION_TLB_MISS);
osInvalICache((void *) 0x80000000,
EXCEPTION_GENERAL + sizeof(exceptionPreamble) - EXCEPTION_TLB_MISS);
osMapTLBRdb();
osPiRawReadIo(4, &sp30);
sp30 &= ~0xf;
if (sp30) {
osClockRate = sp30;
}
osClockRate = osClockRate * 3 / 4;
if (osResetType == RESET_TYPE_COLD_RESET) {
bzero(osAppNmiBuffer, sizeof(osAppNmiBuffer));
}
}
+26
View File
@@ -0,0 +1,26 @@
#include "libultra_internal.h"
extern OSThread *D_803348A0;
s32 osJamMesg(OSMesgQueue *mq, OSMesg msg, s32 flag) {
register s32 int_disabled;
int_disabled = __osDisableInt();
while (mq->validCount >= mq->msgCount) {
if (flag == OS_MESG_BLOCK) {
D_803348A0->state = OS_STATE_WAITING;
__osEnqueueAndYield(&mq->fullqueue);
} else {
__osRestoreInt(int_disabled);
return -1;
}
}
mq->first = (mq->first + mq->msgCount - 1) % mq->msgCount;
mq->msg[mq->first] = msg;
mq->validCount++;
if (mq->mtqueue->next != NULL) {
osStartThread(__osPopThread(&mq->mtqueue));
}
__osRestoreInt(int_disabled);
return 0;
}
+9
View File
@@ -0,0 +1,9 @@
#include "libultra_internal.h"
extern OSMgrArgs piMgrArgs;
OSMesgQueue *osPiGetCmdQueue(void) {
if (!piMgrArgs.initialized)
return NULL;
return piMgrArgs.unk08;
}
+14
View File
@@ -0,0 +1,14 @@
#include "libultra_internal.h"
#include "hardware.h"
extern u32 osRomBase;
s32 osPiRawReadIo(u32 a0, u32 *a1) {
register int status;
status = HW_REG(PI_STATUS_REG, u32);
while (status & (PI_STATUS_BUSY | PI_STATUS_IOBUSY | PI_STATUS_ERROR)) {
status = HW_REG(PI_STATUS_REG, u32);
}
*a1 = HW_REG(osRomBase | a0, u32);
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
#include "libultra_internal.h"
#include "hardware.h"
extern u32 osRomBase; // TODO: figure out why this is like this
s32 osPiRawStartDma(s32 dir, u32 cart_addr, void *dram_addr, size_t size) {
register int status;
status = HW_REG(PI_STATUS_REG, u32);
while (status & (PI_STATUS_BUSY | PI_STATUS_IOBUSY | PI_STATUS_ERROR)) {
status = HW_REG(PI_STATUS_REG, u32);
}
HW_REG(PI_DRAM_ADDR_REG, void *) = (void *) osVirtualToPhysical(dram_addr);
HW_REG(PI_CART_ADDR_REG, void *) =
(void *) ((osRomBase /*(*(u32 *)80000308)*/ | cart_addr) & 0x1fffffff);
switch (dir) {
case 0:
HW_REG(PI_WR_LEN_REG, u32) = size - 1;
break;
case 1:
HW_REG(PI_RD_LEN_REG, u32) = size - 1;
break;
default:
return -1;
break;
}
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
#include "libultra_internal.h"
extern OSMgrArgs piMgrArgs;
s32 osPiStartDma(OSIoMesg *mb, s32 priority, s32 direction, u32 devAddr, void *vAddr, u32 nbytes,
OSMesgQueue *mq) {
register s32 result;
register OSMesgQueue *cmdQueue;
if (!piMgrArgs.initialized)
return -1;
// TODO: name magic constants
if (direction == OS_READ) {
mb->hdr.type = 11;
} else {
mb->hdr.type = 12;
}
mb->hdr.pri = priority;
mb->hdr.retQueue = mq;
mb->dramAddr = vAddr;
mb->devAddr = devAddr;
mb->size = nbytes;
if (priority == OS_MESG_PRI_HIGH) {
cmdQueue = osPiGetCmdQueue();
result = osJamMesg(cmdQueue, mb, OS_MESG_NOBLOCK);
} else {
cmdQueue = osPiGetCmdQueue();
result = osSendMesg(cmdQueue, mb, OS_MESG_NOBLOCK);
}
return result;
}
+33
View File
@@ -0,0 +1,33 @@
#include "libultra_internal.h"
extern OSThread *D_803348A0;
s32 osRecvMesg(OSMesgQueue *mq, OSMesg *msg, s32 flag) {
register u32 int_disabled;
register OSThread *thread;
int_disabled = __osDisableInt();
while (!mq->validCount) {
if (!flag) {
__osRestoreInt(int_disabled);
return -1;
}
D_803348A0->state = OS_STATE_WAITING;
__osEnqueueAndYield(&mq->mtqueue);
}
if (msg != NULL) {
*msg = *(mq->first + mq->msg);
}
mq->first = (mq->first + 1) % mq->msgCount;
mq->validCount--;
if (mq->fullqueue->next != NULL) {
thread = __osPopThread(&mq->fullqueue);
osStartThread(thread);
}
__osRestoreInt(int_disabled);
return 0;
}
+32
View File
@@ -0,0 +1,32 @@
#include "libultra_internal.h"
extern OSThread *D_803348A0;
s32 osSendMesg(OSMesgQueue *mq, OSMesg msg, s32 flag) {
register u32 int_disabled;
register s32 index;
register OSThread *s2;
int_disabled = __osDisableInt();
while (mq->validCount >= mq->msgCount) {
if (flag == OS_MESG_BLOCK) {
D_803348A0->state = 8;
__osEnqueueAndYield(&mq->fullqueue);
} else {
__osRestoreInt(int_disabled);
return -1;
}
}
index = (mq->first + mq->validCount) % mq->msgCount;
mq->msg[index] = msg;
mq->validCount++;
if (mq->mtqueue->next != NULL) {
s2 = __osPopThread(&mq->mtqueue);
osStartThread(s2);
}
__osRestoreInt(int_disabled);
return 0;
}
+18
View File
@@ -0,0 +1,18 @@
#include "libultra_internal.h"
typedef struct OSEventMessageStruct_0_s {
OSMesgQueue *queue;
OSMesg msg;
} OSEventMessageStruct_0;
OSEventMessageStruct_0 D_80363830[16];
void osSetEventMesg(OSEvent e, OSMesgQueue *mq, OSMesg msg) {
register u32 int_disabled;
OSEventMessageStruct_0 *msgs;
int_disabled = __osDisableInt();
msgs = D_80363830 + e;
msgs->queue = mq;
msgs->msg = msg;
__osRestoreInt(int_disabled);
}
+27
View File
@@ -0,0 +1,27 @@
#include "libultra_internal.h"
extern OSThread *D_803348A0;
extern OSThread *D_80334898;
void osSetThreadPri(OSThread *thread, OSPri pri) {
register u32 int_disabled = __osDisableInt();
if (thread == NULL) {
thread = D_803348A0;
}
if (thread->priority != pri) {
thread->priority = pri;
if (thread != D_803348A0) {
if (thread->state != OS_STATE_STOPPED) {
__osDequeueThread(thread->queue, thread);
__osEnqueueThread(thread->queue, thread);
}
}
if (D_803348A0->priority < D_80334898->priority) {
D_803348A0->state = OS_STATE_RUNNABLE;
__osEnqueueAndYield(&D_80334898);
}
}
__osRestoreInt(int_disabled);
}
+7
View File
@@ -0,0 +1,7 @@
#include "libultra_internal.h"
extern OSTime _osCurrentTime;
void osSetTime(OSTime time) {
_osCurrentTime = time;
}
+22
View File
@@ -0,0 +1,22 @@
#include "libultra_internal.h"
extern OSTimer *D_80334830;
extern u64 __osInsertTimer(OSTimer *);
u32 osSetTimer(OSTimer *a0, OSTime a1, u64 a2, OSMesgQueue *a3, OSMesg a4) {
u64 sp18;
a0->next = NULL;
a0->prev = NULL;
a0->interval = a2;
if (a1 != 0) {
a0->remaining = a1;
} else {
a0->remaining = a2;
}
a0->mq = a3;
a0->msg = a4;
sp18 = __osInsertTimer(a0);
if (D_80334830->next == a0) {
__osSetTimerIntr(sp18);
}
return 0;
}
+53
View File
@@ -0,0 +1,53 @@
#include "libultra_internal.h"
#include "hardware.h"
#include <macros.h>
#define _osVirtualToPhysical(ptr) \
if (ptr != NULL) { \
ptr = (void *) osVirtualToPhysical(ptr); \
}
OSTask D_803638B0;
OSTask *_VirtualToPhysicalTask(OSTask *task) {
OSTask *physicalTask;
physicalTask = &D_803638B0;
bcopy(task, physicalTask, sizeof(OSTask));
_osVirtualToPhysical(physicalTask->t.ucode);
_osVirtualToPhysical(physicalTask->t.ucode_data);
_osVirtualToPhysical(physicalTask->t.dram_stack);
_osVirtualToPhysical(physicalTask->t.output_buff);
_osVirtualToPhysical(physicalTask->t.output_buff_size);
_osVirtualToPhysical(physicalTask->t.data_ptr);
_osVirtualToPhysical(physicalTask->t.yield_data_ptr);
return physicalTask;
}
void osSpTaskLoad(OSTask *task) {
OSTask *physicalTask;
physicalTask = _VirtualToPhysicalTask(task);
if (physicalTask->t.flags & M_TASK_FLAG0) {
physicalTask->t.ucode_data = physicalTask->t.yield_data_ptr;
physicalTask->t.ucode_data_size = physicalTask->t.yield_data_size;
task->t.flags &= ~M_TASK_FLAG0;
}
osWritebackDCache(physicalTask, sizeof(OSTask));
__osSpSetStatus(SPSTATUS_CLEAR_SIGNAL0 | SPSTATUS_CLEAR_SIGNAL1 | SPSTATUS_CLEAR_SIGNAL2
| SPSTATUS_SET_INTR_ON_BREAK);
while (__osSpSetPc((void *) SP_IMEM_START) == -1)
;
while (__osSpRawStartDma(1, (void *) (SP_IMEM_START - sizeof(*physicalTask)), physicalTask,
sizeof(OSTask))
== -1)
;
while (__osSpDeviceBusy())
;
while (__osSpRawStartDma(1, (void *) SP_IMEM_START, physicalTask->t.ucode_boot,
physicalTask->t.ucode_boot_size)
== -1)
;
}
void osSpTaskStartGo(UNUSED OSTask *task) {
while (__osSpDeviceBusy())
;
__osSpSetStatus(SPSTATUS_SET_INTR_ON_BREAK | SPSTATUS_CLEAR_SSTEP | SPSTATUS_CLEAR_BROKE
| SPSTATUS_CLEAR_HALT);
}
+5
View File
@@ -0,0 +1,5 @@
#include "libultra_internal.h"
void osSpTaskYield(void) {
__osSpSetStatus(SPSTATUS_SET_SIGNAL0);
}
+18
View File
@@ -0,0 +1,18 @@
#include "libultra_internal.h"
OSYieldResult osSpTaskYielded(OSTask *task) {
s32 status;
u32 int_disabledult;
status = __osSpGetStatus();
if (status & SPSTATUS_SIGNAL1_SET) {
int_disabledult = 1;
} else {
int_disabledult = 0;
}
if (status & SPSTATUS_SIGNAL0_SET) {
task->t.flags |= int_disabledult;
task->t.flags &= ~(M_TASK_FLAG1);
}
return int_disabledult;
}
+38
View File
@@ -0,0 +1,38 @@
#include "libultra_internal.h"
extern OSThread *D_803348A0;
extern OSThread *D_80334898;
void osStartThread(OSThread *thread) {
register u32 int_disabled;
register u32 state;
int_disabled = __osDisableInt();
state = thread->state;
if (state != OS_STATE_STOPPED) {
if (state == OS_STATE_WAITING) {
do {
} while (0);
thread->state = OS_STATE_RUNNABLE;
__osEnqueueThread(&D_80334898, thread);
}
} else {
if (thread->queue == NULL || thread->queue == &D_80334898) {
thread->state = OS_STATE_RUNNABLE;
__osEnqueueThread(&D_80334898, thread);
} else {
thread->state = OS_STATE_WAITING;
__osEnqueueThread(thread->queue, thread);
state = (u32) __osPopThread(thread->queue);
__osEnqueueThread(&D_80334898, (OSThread *) state);
}
}
if (D_803348A0 == NULL) {
__osDispatchThread();
} else {
if (D_803348A0->priority < D_80334898->priority) {
D_803348A0->state = OS_STATE_RUNNABLE;
__osEnqueueAndYield(&D_80334898);
}
}
__osRestoreInt(int_disabled);
}
+86
View File
@@ -0,0 +1,86 @@
#include "libultra_internal.h"
// TODO: document
OSTimer D_80365D80;
OSTimer *D_80334830 = &D_80365D80;
OSTime _osCurrentTime;
u32 D_80365DA8;
u32 D_80365DAC;
u32 D_80365DB0;
void __osTimerServicesInit() {
_osCurrentTime = 0;
D_80365DA8 = 0;
D_80365DAC = 0;
D_80334830->prev = D_80334830;
D_80334830->next = D_80334830->prev;
D_80334830->remaining = 0;
D_80334830->interval = D_80334830->remaining;
D_80334830->mq = NULL;
D_80334830->msg = NULL;
}
void __osTimerInterrupt() {
OSTimer *sp24;
u32 sp20;
u32 sp1c;
if (D_80334830->next == D_80334830)
return;
while (1) {
sp24 = D_80334830->next;
if (sp24 == D_80334830) {
__osSetCompare(0);
D_80365DB0 = 0;
break;
}
sp20 = osGetCount();
sp1c = sp20 - D_80365DB0;
D_80365DB0 = sp20;
if (sp1c < sp24->remaining) {
sp24->remaining -= sp1c;
__osSetTimerIntr(sp24->remaining);
return;
} else {
sp24->prev->next = sp24->next;
sp24->next->prev = sp24->prev;
sp24->next = NULL;
sp24->prev = NULL;
if (sp24->mq != NULL) {
osSendMesg(sp24->mq, sp24->msg, OS_MESG_NOBLOCK);
}
if (sp24->interval != 0) {
sp24->remaining = sp24->interval;
__osInsertTimer(sp24);
}
}
}
}
void __osSetTimerIntr(u64 a0) {
u64 tmp;
s32 intDisabled = __osDisableInt();
D_80365DB0 = osGetCount();
tmp = a0 + D_80365DB0;
__osSetCompare(tmp);
__osRestoreInt(intDisabled);
}
u64 __osInsertTimer(OSTimer *a0) {
OSTimer *sp34;
u64 sp28;
s32 intDisabled;
intDisabled = __osDisableInt();
for (sp34 = D_80334830->next, sp28 = a0->remaining; sp34 != D_80334830 && sp28 > sp34->remaining;
sp28 -= sp34->remaining, sp34 = sp34->next) {
;
}
a0->remaining = sp28;
if (sp34 != D_80334830) {
sp34->remaining -= sp28;
}
a0->next = sp34;
a0->prev = sp34->prev;
sp34->prev->next = a0;
sp34->prev = a0;
__osRestoreInt(intDisabled);
return sp28;
}
+12
View File
@@ -0,0 +1,12 @@
#include "libultra_internal.h"
extern OSViContext *D_80334914;
// TODO: name magic constants
void osViBlack(u8 active) {
register u32 int_disabled = __osDisableInt();
if (active) {
D_80334914->unk00 |= 0x20;
} else {
D_80334914->unk00 &= ~0x20;
}
__osRestoreInt(int_disabled);
}
+50
View File
@@ -0,0 +1,50 @@
#include "libultra_internal.h"
OSViMode D_80334990 = {
/*type*/ 2,
/*comRegs*/
{ /*ctrl*/ 12574,
/*width*/ 320,
/*burst*/ 65348153,
/*vSync*/ 525,
/*hSync*/ 3093,
/*leap*/ 202705941,
/*hStart*/ 7078636,
/*xScale*/ 512,
/*vCurrent*/ 0 },
/*fldRegs*/
{ { /*origin*/ 640,
/*yScale*/ 1024,
/*vStart*/ 2425343,
/*vBurst*/ 918020,
/*vIntr*/ 2 },
{ /*origin*/ 640,
/*yScale*/ 1024,
/*vStart*/ 2425343,
/*vBurst*/ 918020,
/*vIntr*/ 2 } }
};
OSViMode D_803349E0 = {
/*type*/ 16,
/*comRegs*/
{ /*ctrl*/ 12574,
/*width*/ 320,
/*burst*/ 67380026,
/*vSync*/ 625,
/*hSync*/ 1379433,
/*leap*/ 208604270,
/*hStart*/ 8389376,
/*xScale*/ 512,
/*vCurrent*/ 0 },
/*fldRegs*/
{ { /*origin*/ 640,
/*yScale*/ 1024,
/*vStart*/ 6226489,
/*vBurst*/ 590443,
/*vIntr*/ 2 },
{ /*origin*/ 640,
/*yScale*/ 1024,
/*vStart*/ 6226489,
/*vBurst*/ 590443,
/*vIntr*/ 2 } }
};
+10
View File
@@ -0,0 +1,10 @@
#include "libultra_internal.h"
extern OSViContext *D_80334914;
void osViSetEvent(OSMesgQueue *mq, OSMesg msg, u32 retraceCount) {
register u32 int_disabled = __osDisableInt();
(D_80334914)->mq = mq;
(D_80334914)->msg = msg;
(D_80334914)->retraceCount = retraceCount;
__osRestoreInt(int_disabled);
}
+9
View File
@@ -0,0 +1,9 @@
#include "libultra_internal.h"
extern OSViContext *D_80334914;
void osViSetMode(OSViMode *mode) {
register u32 int_disabled = __osDisableInt();
D_80334914->unk08 = mode;
D_80334914->unk00 = 1;
D_80334914->features = D_80334914->unk08->comRegs.ctrl;
__osRestoreInt(int_disabled);
}
+36
View File
@@ -0,0 +1,36 @@
#include "libultra_internal.h"
extern OSViContext *D_80334914;
void osViSetSpecialFeatures(u32 func) {
register u32 int_disabled = __osDisableInt();
if (func & OS_VI_GAMMA_ON) {
D_80334914->features |= OS_VI_GAMMA;
}
if (func & OS_VI_GAMMA_OFF) {
D_80334914->features &= ~OS_VI_GAMMA;
}
if (func & OS_VI_GAMMA_DITHER_ON) {
D_80334914->features |= OS_VI_GAMMA_DITHER;
}
if (func & OS_VI_GAMMA_DITHER_OFF) {
D_80334914->features &= ~OS_VI_GAMMA_DITHER;
}
if (func & OS_VI_DIVOT_ON) {
D_80334914->features |= OS_VI_DIVOT;
}
if (func & OS_VI_DIVOT_OFF) {
D_80334914->features &= ~OS_VI_DIVOT;
}
if (func & OS_VI_DITHER_FILTER_ON) {
D_80334914->features |= OS_VI_DITHER_FILTER;
D_80334914->features &= ~(OS_VI_UNK200 | OS_VI_UNK100);
}
if (func & OS_VI_DITHER_FILTER_OFF) {
D_80334914->features &= ~OS_VI_DITHER_FILTER;
D_80334914->features |= D_80334914->unk08->comRegs.ctrl & (OS_VI_UNK200 | OS_VI_UNK100);
}
D_80334914->unk00 |= 8;
__osRestoreInt(int_disabled);
}
+8
View File
@@ -0,0 +1,8 @@
#include "libultra_internal.h"
extern OSViContext *D_80334914;
void osViSwapBuffer(void *vaddr) {
u32 int_disabled = __osDisableInt();
D_80334914->buffer = vaddr;
D_80334914->unk00 |= 0x10; // TODO: figure out what this flag means
__osRestoreInt(int_disabled);
}
+13
View File
@@ -0,0 +1,13 @@
#include "libultra_internal.h"
u32 osVirtualToPhysical(void *addr) {
if ((u32) addr >= 0x80000000 && (u32) addr < 0xa0000000) {
return ((u32) addr & 0x1fffffff);
}
else if ((u32) addr >= 0xa0000000 && (u32) addr < 0xc0000000) {
return ((u32) addr & 0x1fffffff);
} else {
return __osProbeTLB(addr);
}
}
+36
View File
@@ -0,0 +1,36 @@
#ifndef _PRINTF_H_
#define _PRINTF_H_
#include <stdarg.h>
typedef struct
{
union {
/* 00 */ s64 s64;
u64 u64;
f64 f64;
u32 u32;
u16 u16;
} value;
/* 08 */ char *buff;
/* 0c */ s32 part1_len;
/* 10 */ s32 num_leading_zeros;
/* 14 */ s32 part2_len;
/* 18 */ s32 num_mid_zeros;
/* 1c */ s32 part3_len;
/* 20 */ s32 num_trailing_zeros;
/* 24 */ s32 precision;
/* 28 */ s32 width;
/* 2c */ u32 size;
/* 30 */ u32 flags;
/* 34 */ u8 length;
} printf_struct;
#define FLAGS_SPACE 1
#define FLAGS_PLUS 2
#define FLAGS_MINUS 4
#define FLAGS_HASH 8
#define FLAGS_ZERO 16
s32 _Printf(char *(*prout)(char *, const char *, size_t), char *dst, const char *fmt, va_list args);
void _Litob(printf_struct *args, u8 type);
void _Ldtob(printf_struct *args, u8 type);
#endif
+20
View File
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include "libultra_internal.h"
#include "printf.h"
#include <string.h>
char *proutSprintf(char *dst, const char *src, size_t count);
int sprintf(char *dst, const char *fmt, ...) {
s32 written;
va_list args;
va_start(args, fmt);
written = _Printf(proutSprintf, dst, fmt, args);
if (written >= 0) {
dst[written] = 0;
}
return written;
}
char *proutSprintf(char *dst, const char *src, size_t count) {
return (char *) memcpy((u8 *) dst, (u8 *) src, count) + count;
}
+10
View File
@@ -0,0 +1,10 @@
#include <math.h>
#ifndef __GNUC__
#pragma intrinsic(sqrtf)
#define __builtin_sqrtf sqrtf
#endif
float sqrtf(float f) {
return __builtin_sqrtf(f);
}
+27
View File
@@ -0,0 +1,27 @@
#include "libultra_internal.h"
#include <string.h>
void *memcpy(void *dst, const void *src, size_t size) {
u8 *_dst = dst;
const u8 *_src = src;
while (size > 0) {
*_dst++ = *_src++;
size--;
}
return dst;
}
size_t strlen(const u8 *str) {
const u8 *ptr = str;
while (*ptr)
ptr++;
return ptr - str;
}
const u8 *strchr(const u8 *str, s32 ch) {
u8 c = ch;
while (*str != c) {
if (*str == 0)
return NULL;
str++;
}
return str;
}