Skip to content

Integrating uC-sdk to OpenBIOS #168

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

Merged
merged 16 commits into from
Oct 11, 2019
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: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
[submodule "third_party/zstr"]
path = third_party/zstr
url = https://github.com/mateidavid/zstr.git
[submodule "third_party/uC-sdk"]
path = third_party/uC-sdk
url = https://github.com/grumpycoders/uC-sdk.git
9 changes: 5 additions & 4 deletions src/mips/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ PREFIX = mipsel-linux-gnu

CC = $(PREFIX)-gcc

ARCHFLAGS = -march=mips1 -mabi=32 -EL -msoft-float -Wa,-msoft-float -fno-pic -mno-shared -mno-abicalls
CPPFLAGS = -mno-gpopt -fomit-frame-pointer
ARCHFLAGS = -march=mips1 -mabi=32 -EL -fno-pic -mno-shared -mno-abicalls -mfp32
ARCHFLAGS += -fno-stack-protector -nostdlib -ffreestanding
CPPFLAGS += -mno-gpopt -fomit-frame-pointer -ffunction-sections
CPPFLAGS += -fno-builtin
CPPFLAGS += $(ARCHFLAGS)
CPPFLAGS += -I..

LDFLAGS = -Wl,-Map=$(TARGET).map -nostdlib -T$(LDSCRIPT) -static -Wl,--gc-sections
LDFLAGS += $(ARCHFLAGS)

LDFLAGS += -g -O3 -flto
CPPFLAGS += -g -O3 -flto
LDFLAGS += -g -Os
CPPFLAGS += -g -Os

OBJS += $(addsuffix .o, $(basename $(SRCS)))

Expand Down
6 changes: 6 additions & 0 deletions src/mips/common/hardware/hwregs.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@
#define SPU_MVOL_R HW_U16(0x1f801d82)
#define SPU_REVERB_L HW_U16(0x1f801d84)
#define SPU_REVERB_R HW_U16(0x1f801d86)

#define SIO1_DATA HW_U8(0x1f801050)
#define SIO1_STAT HW_U16(0x1f801054)
#define SIO1_MODE HW_U16(0x1f801058)
#define SIO1_CTRL HW_U16(0x1f80105a)
#define SIO1_BAUD HW_U16(0x1f80105e)
40 changes: 40 additions & 0 deletions src/mips/common/hardware/sio1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/***************************************************************************
* Copyright (C) 2019 PCSX-Redux authors *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/

#include "common/hardware/hwregs.h"
#include "common/hardware/sio1.h"

void sio1_init() {
// enable TX and RX, and nothing else
SIO1_CTRL = 5;
// 01001110
// Baudrate Reload Factor: MUL16 (2)
// Character length: 8 (3)
// Parity Disabled
// Parity Type: irrelevant
// Stop bit length: 1 (1)
// --> 8N1
SIO1_MODE = 0x4e;
SIO1_BAUD = 2073600 / 115200;
}

void sio1_putc(uint8_t byte) {
while ((SIO1_STAT & 1) == 0);
SIO1_DATA = byte;
}
25 changes: 25 additions & 0 deletions src/mips/common/hardware/sio1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/***************************************************************************
* Copyright (C) 2019 PCSX-Redux authors *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/

#pragma once

#include <stdint.h>

void sio1_init();
void sio1_putc(uint8_t byte);
33 changes: 33 additions & 0 deletions src/mips/openbios/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,45 @@ TARGET = openbios

SRCS = \
../common/hardware/cop0.s \
../common/hardware/sio1.c \
boot/boot.s \
kernel/flushcache.s \
kernel/handlers.c \
kernel/vectors.s \
main/main.c \
\
uC-sdk-glue/BoardConsole.c \
uC-sdk-glue/BoardInit.c \
uC-sdk-glue/init.c \
\
../../../third_party/uC-sdk/libc/src/cxx-glue.c \
../../../third_party/uC-sdk/libc/src/errno.c \
../../../third_party/uC-sdk/libc/src/initfini.c \
../../../third_party/uC-sdk/libc/src/malloc.c \
../../../third_party/uC-sdk/libc/src/qsort.c \
../../../third_party/uC-sdk/libc/src/rand.c \
../../../third_party/uC-sdk/libc/src/reent.c \
../../../third_party/uC-sdk/libc/src/stdio.c \
../../../third_party/uC-sdk/libc/src/strto.c \
../../../third_party/uC-sdk/libc/src/unistd.c \
../../../third_party/uC-sdk/libc/src/xprintf.c \
../../../third_party/uC-sdk/libc/src/xscanf.c \
../../../third_party/uC-sdk/libc/src/yscanf.c \
../../../third_party/uC-sdk/os/src/devfs.c \
../../../third_party/uC-sdk/os/src/filesystem.c \
../../../third_party/uC-sdk/os/src/fio.c \
../../../third_party/uC-sdk/os/src/hash-djb2.c \
../../../third_party/uC-sdk/os/src/init.c \
../../../third_party/uC-sdk/os/src/osdebug.c \
../../../third_party/uC-sdk/os/src/romfs.c \
../../../third_party/uC-sdk/os/src/sbrk.c \


LDSCRIPT = psx-bios.ld

CPPFLAGS = -DNOFLOATINGPOINT
CPPFLAGS += -I../../../third_party/uC-sdk/libc/include
CPPFLAGS += -I../../../third_party/uC-sdk/os/include
CPPFLAGS += -IuC-sdk-glue

include ../common.mk
63 changes: 62 additions & 1 deletion src/mips/openbios/boot/boot.s
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,67 @@ bss_init:
bne $t0, $t1, bss_init

bss_init_skip:
/* Displays the following:

**********
0123456789
**********

*/
li $t3, 42
lui $t1, 0x1f00
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
li $t0, 13
sb $t0, 0($t1)
li $t0, 10
sb $t0, 0($t1)
li $t0, '0'
sb $t0, 0($t1)
addiu $t0, 1
sb $t0, 0($t1)
addiu $t0, 1
sb $t0, 0($t1)
addiu $t0, 1
sb $t0, 0($t1)
addiu $t0, 1
sb $t0, 0($t1)
addiu $t0, 1
sb $t0, 0($t1)
addiu $t0, 1
sb $t0, 0($t1)
addiu $t0, 1
sb $t0, 0($t1)
addiu $t0, 1
sb $t0, 0($t1)
addiu $t0, 1
sb $t0, 0($t1)
li $t0, 13
sb $t0, 0($t1)
li $t0, 10
sb $t0, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
sb $t3, 0($t1)
li $t0, 13
sb $t0, 0($t1)
li $t0, 10
sb $t0, 0($t1)

/* technically have to set $gp, but we are not using it, so, not */
la $sp, __sp
Expand All @@ -182,7 +243,7 @@ bss_init_skip:
li $t0, 0xb88
sw $t0, RAM_SIZE

jal main
jal _ucsdk_start

stop:
b stop
17 changes: 17 additions & 0 deletions src/mips/openbios/kernel/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/

#include "osdebug.h"

#include "common/compiler/stdint.h"
#include "openbios/kernel/handlers.h"

Expand Down Expand Up @@ -147,8 +149,23 @@ typedef struct {
uint32_t EPC;
} InterruptData;

static void printInterruptData(InterruptData* data) {
osDbgPrintf("epc = %p - status = %p - cause = %p\r\n", data->EPC, data->SR, data->Cause);
osDbgPrintf("r0 = %p - at = %p - v0 = %p - v1 = %p\r\n", data->GPR.r[ 0], data->GPR.r[ 1], data->GPR.r[ 2], data->GPR.r[ 3]);
osDbgPrintf("a0 = %p - a1 = %p - a2 = %p - a3 = %p\r\n", data->GPR.r[ 4], data->GPR.r[ 5], data->GPR.r[ 6], data->GPR.r[ 7]);
osDbgPrintf("t0 = %p - t1 = %p - t2 = %p - t3 = %p\r\n", data->GPR.r[ 8], data->GPR.r[ 9], data->GPR.r[10], data->GPR.r[11]);
osDbgPrintf("t4 = %p - t5 = %p - t6 = %p - t7 = %p\r\n", data->GPR.r[12], data->GPR.r[13], data->GPR.r[14], data->GPR.r[15]);
osDbgPrintf("s0 = %p - s1 = %p - s2 = %p - s3 = %p\r\n", data->GPR.r[16], data->GPR.r[17], data->GPR.r[18], data->GPR.r[19]);
osDbgPrintf("s4 = %p - s5 = %p - s6 = %p - s7 = %p\r\n", data->GPR.r[20], data->GPR.r[21], data->GPR.r[22], data->GPR.r[23]);
osDbgPrintf("t8 = %p - t9 = %p - k0 = %p - k1 = %p\r\n", data->GPR.r[24], data->GPR.r[25], data->GPR.r[26], data->GPR.r[27]);
osDbgPrintf("gp = %p - sp = %p - s8 = %p - ra = %p\r\n", data->GPR.r[28], data->GPR.r[29], data->GPR.r[30], data->GPR.r[31]);
osDbgPrintf("hi = %p - lo = %p\r\n", data->GPR.r[32], data->GPR.r[33]);
}

void breakHandler(InterruptData* data) {
}

void interruptHandler(InterruptData* data) {
osDbgPrintf("***Exception***\r\n");
printInterruptData(data);
}
20 changes: 19 additions & 1 deletion src/mips/openbios/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/

#include <devfs.h>
#include <stdio.h>
#include <fio.h>

#include "common/hardware/cop0.h"
#include "common/hardware/sio1.h"
#include "common/hardware/spu.h"
#include "common/util/djbhash.h"
#include "openbios/kernel/handlers.h"
Expand All @@ -29,8 +34,21 @@ int main() {
*((uint32_t*) 0x64) = 0x00;
*((uint32_t*) 0x68) = 0xff;
muteSpu();

sio1_init();
register_devfs();
register_stdio_devices();

printf("OpenBIOS starting.\r\n");

printf("Checking for EXP1...\r\n");

if (djbHash((const char *) 0x1f000084, 44) == 0xf0772daf) {
(*((void(**)()) 0x1f000080))();
void(*ptr)() = *(void(**)()) 0x1f000080;
printf("Signature match, jumping to %p\r\n", ptr);
(*ptr)();
} else {
printf("Signature not matching - skipping EXP1\r\n");
}

start("cdrom:SYSTEM.CNF;1", "cdrom:PSX.EXE;1");
Expand Down
30 changes: 28 additions & 2 deletions src/mips/openbios/psx-bios.ld
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ MEMORY {
dcache : ORIGIN = 0x1f800000, LENGTH = 0x400
}

STACK_SIZE = 0x10000;

__ram_top = ORIGIN(ram) + LENGTH(ram);
__sp = __ram_top - 0x100;

Expand All @@ -40,12 +42,36 @@ __dcache_top = ORIGIN(dcache) + LENGTH(dcache);
__rom_data_len = (__data_end - __data_start);
__bss_len = (__bss_end - __bss_start);

__stack_start = ORIGIN(ram) + LENGTH(ram) - STACK_SIZE;

SECTIONS {
__text_start = .;
.text : {
*(.boot)

*(.init)
. = ALIGN(4);
__preinit_array_start = .;
KEEP (*(SORT(.preinit_array.*)))
KEEP (*(.preinit_array))
. = ALIGN(4);
__preinit_array_end = .;
__init_array_start = .;
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array))
KEEP (*(.ctors))
. = ALIGN(4);
__init_array_end = .;

KEEP (*(SORT_NONE(.fini)))
. = ALIGN(4);
__fini_array_start = .;
KEEP (*(.fini_array))
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.dtors))
. = ALIGN(4);
__fini_array_end = .;

*(.text.unlikely .text.*_unlikely .text.unlikely.*)
*(.text.exit .text.exit.*)
*(.text.startup .text.startup.*)
Expand All @@ -54,8 +80,6 @@ SECTIONS {
. = ALIGN(4);
} > rom

.fini : {
} > rom
. = ALIGN(4);
__text_end = .;

Expand Down Expand Up @@ -93,6 +117,8 @@ SECTIONS {
. = ALIGN(4);
__bss_end = .;

__heap_start = .;

__end = .;

/DISCARD/ : { *(.MIPS.abiflags) }
Expand Down
Loading