Skip to content

Commit

Permalink
bootstrapper + spu
Browse files Browse the repository at this point in the history
  • Loading branch information
cuckydev committed Jan 27, 2024
1 parent 47527cb commit 9d8b6bf
Show file tree
Hide file tree
Showing 26 changed files with 10,877 additions and 111 deletions.
62 changes: 62 additions & 0 deletions cmake/FindToolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
## Find toolchain paths

set(
CKSDK_TC ""
CACHE PATH "Path to the GCC toolchain's installation directory (if not in PATH)"
)
set(
CKSDK_TARGET mipsel-none-elf
CACHE STRING "GCC toolchain target triplet"
)

# Attempt to find GCC using a list of common installation locations.
# CKSDK_TC can be left unset if the toolchain can be found in any of these
# or in the PATH environment variable.
find_program(
_gcc ${CKSDK_TARGET}-gcc
HINTS
${CKSDK_TC}/bin
${CKSDK_TC}/../bin
# Same as ${CMAKE_INSTALL_PREFIX}/bin
${CMAKE_CURRENT_LIST_DIR}/../../../bin
# Same as ${CMAKE_INSTALL_PREFIX}/${CKSDK_TARGET}/bin
${CMAKE_CURRENT_LIST_DIR}/../../../${CKSDK_TARGET}/bin
PATHS
"C:/Program Files/${CKSDK_TARGET}/bin"
"C:/Program Files (x86)/${CKSDK_TARGET}/bin"
"C:/${CKSDK_TARGET}/bin"
/opt/${CKSDK_TARGET}/bin
/usr/local/${CKSDK_TARGET}/bin
/usr/${CKSDK_TARGET}/bin
NO_CACHE REQUIRED
)
cmake_path(GET _gcc PARENT_PATH _bin)
cmake_path(GET _bin PARENT_PATH _toolchain)

# Overwrite the empty cache entry, so it won't have to be found again.
if(NOT IS_DIRECTORY CKSDK_TC)
set(
CKSDK_TC ${_toolchain}
CACHE PATH "Path to the GCC toolchain's installation directory (if not in PATH)"
FORCE
)
endif()

## Toolchain executables

# As we have overridden ${CMAKE_EXECUTABLE_SUFFIX} we can't rely on it to
# determine the host OS extension for executables. A workaround is to extract
# the extension from the path returned by find_program() using a regex.
set(_prefix ${_bin}/${CKSDK_TARGET})
string(REGEX MATCH ".+-gcc(.*)$" _dummy ${_gcc})

set(FOUND_CMAKE_ASM_COMPILER ${_prefix}-gcc${CMAKE_MATCH_1})
set(FOUND_CMAKE_C_COMPILER ${_prefix}-gcc${CMAKE_MATCH_1})
set(FOUND_CMAKE_CXX_COMPILER ${_prefix}-g++${CMAKE_MATCH_1})
set(FOUND_CMAKE_AR ${_prefix}-ar${CMAKE_MATCH_1})
set(FOUND_CMAKE_LINKER ${_prefix}-ld${CMAKE_MATCH_1})
set(FOUND_CMAKE_RANLIB ${_prefix}-ranlib${CMAKE_MATCH_1})
set(FOUND_CMAKE_OBJCOPY ${_prefix}-objcopy${CMAKE_MATCH_1})
set(FOUND_CMAKE_SIZE ${_prefix}-size${CMAKE_MATCH_1})
set(FOUND_CMAKE_STRIP ${_prefix}-strip${CMAKE_MATCH_1})
set(FOUND_TOOLCHAIN_NM ${_prefix}-nm${CMAKE_MATCH_1})
72 changes: 12 additions & 60 deletions cmake/Toolchain.cmake
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
cmake_minimum_required(VERSION 3.8)

set(
CKSDK_TC ""
CACHE PATH "Path to the GCC toolchain's installation directory (if not in PATH)"
)
set(
CKSDK_TARGET mipsel-none-elf
CACHE STRING "GCC toolchain target triplet"
)

## CMake configuration

set(CMAKE_SYSTEM_NAME Generic)
Expand All @@ -32,57 +23,18 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

## Toolchain path setup

# Attempt to find GCC using a list of common installation locations.
# CKSDK_TC can be left unset if the toolchain can be found in any of these
# or in the PATH environment variable.
find_program(
_gcc ${CKSDK_TARGET}-gcc
HINTS
${CKSDK_TC}/bin
${CKSDK_TC}/../bin
# Same as ${CMAKE_INSTALL_PREFIX}/bin
${CMAKE_CURRENT_LIST_DIR}/../../../bin
# Same as ${CMAKE_INSTALL_PREFIX}/${CKSDK_TARGET}/bin
${CMAKE_CURRENT_LIST_DIR}/../../../${CKSDK_TARGET}/bin
PATHS
"C:/Program Files/${CKSDK_TARGET}/bin"
"C:/Program Files (x86)/${CKSDK_TARGET}/bin"
"C:/${CKSDK_TARGET}/bin"
/opt/${CKSDK_TARGET}/bin
/usr/local/${CKSDK_TARGET}/bin
/usr/${CKSDK_TARGET}/bin
NO_CACHE REQUIRED
)
cmake_path(GET _gcc PARENT_PATH _bin)
cmake_path(GET _bin PARENT_PATH _toolchain)

# Overwrite the empty cache entry, so it won't have to be found again.
if(NOT IS_DIRECTORY CKSDK_TC)
set(
CKSDK_TC ${_toolchain}
CACHE PATH "Path to the GCC toolchain's installation directory (if not in PATH)"
FORCE
)
endif()

## Toolchain executables

# As we have overridden ${CMAKE_EXECUTABLE_SUFFIX} we can't rely on it to
# determine the host OS extension for executables. A workaround is to extract
# the extension from the path returned by find_program() using a regex.
set(_prefix ${_bin}/${CKSDK_TARGET})
string(REGEX MATCH ".+-gcc(.*)$" _dummy ${_gcc})

set(CMAKE_ASM_COMPILER ${_prefix}-gcc${CMAKE_MATCH_1})
set(CMAKE_C_COMPILER ${_prefix}-gcc${CMAKE_MATCH_1})
set(CMAKE_CXX_COMPILER ${_prefix}-g++${CMAKE_MATCH_1})
set(CMAKE_AR ${_prefix}-ar${CMAKE_MATCH_1})
set(CMAKE_LINKER ${_prefix}-ld${CMAKE_MATCH_1})
set(CMAKE_RANLIB ${_prefix}-ranlib${CMAKE_MATCH_1})
set(CMAKE_OBJCOPY ${_prefix}-objcopy${CMAKE_MATCH_1})
set(CMAKE_SIZE ${_prefix}-size${CMAKE_MATCH_1})
set(CMAKE_STRIP ${_prefix}-strip${CMAKE_MATCH_1})
set(TOOLCHAIN_NM ${_prefix}-nm${CMAKE_MATCH_1})
include(${CMAKE_CURRENT_LIST_DIR}/FindToolchain.cmake)

set(CMAKE_ASM_COMPILER ${FOUND_CMAKE_ASM_COMPILER})
set(CMAKE_C_COMPILER ${FOUND_CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER ${FOUND_CMAKE_CXX_COMPILER})
set(CMAKE_AR ${FOUND_CMAKE_AR})
set(CMAKE_LINKER ${FOUND_CMAKE_LINKER})
set(CMAKE_RANLIB ${FOUND_CMAKE_RANLIB})
set(CMAKE_OBJCOPY ${FOUND_CMAKE_OBJCOPY})
set(CMAKE_SIZE ${FOUND_CMAKE_SIZE})
set(CMAKE_STRIP ${FOUND_CMAKE_STRIP})
set(TOOLCHAIN_NM ${FOUND_TOOLCHAIN_NM})

## SDK setup

Expand Down
68 changes: 49 additions & 19 deletions include/CKSDK/OS.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ namespace CKSDK
uint16_t addr;
/// @brief Voice ADSR
uint32_t adsr;

uint16_t pad;
/// @brief Voice loop address
uint16_t loop_addr;
uint16_t pad;
};
static_assert(sizeof(SPU_VOICE_CTRL) == 16);

/// @brief SPU voice control ports (0-23)
inline constexpr volatile SPU_VOICE_CTRL &SpuVoiceCtrl(int i) { return (&MMIO<SPU_VOICE_CTRL>(0x1D80))[i]; }
inline constexpr volatile SPU_VOICE_CTRL &SpuVoiceCtrl(int i) { return (&MMIO<SPU_VOICE_CTRL>(0x1C00))[i]; }

// MDEC
/// @brief MDEC0 control port
Expand Down Expand Up @@ -279,23 +280,52 @@ namespace CKSDK
/// @brief EXP3 address port
inline constexpr volatile uint32_t &Exp2Addr() { return MMIO<uint32_t>(0x1004); }

/// @brief EXP1 delay size port
inline constexpr volatile uint32_t &Exp1DelaySize() { return MMIO<uint32_t>(0x1008); }
/// @brief EXP3 delay size port
inline constexpr volatile uint32_t &Exp3DelaySize() { return MMIO<uint32_t>(0x100C); }
/// @brief BIOS delay size port
inline constexpr volatile uint32_t &BiosDelaySize() { return MMIO<uint32_t>(0x1010); }
/// @brief SPU delay size port
inline constexpr volatile uint32_t &SpuDelaySize() { return MMIO<uint32_t>(0x1014); }
/// @brief CD delay size port
inline constexpr volatile uint32_t &CdDelaySize() { return MMIO<uint32_t>(0x1018); }
/// @brief EXP2 delay size port
inline constexpr volatile uint32_t &Exp2DelaySize() { return MMIO<uint32_t>(0x101C); }

/// @brief Common delay configuration port
inline constexpr volatile uint32_t &ComDelayCfg() { return MMIO<uint32_t>(0x1020); }
/// @brief RAM size configuration port
inline constexpr volatile uint32_t &RamSizeCfg() { return MMIO<uint32_t>(0x1060); }
/// @brief BIU control bits
enum BIU_BITS : uint32_t
{
BIU_WriteDelayShift = 0,
BIU_WriteDelay = 15 << BIU_WriteDelayShift,

BIU_ReadDelayShift = 4,
BIU_ReadDelay = 15 << BIU_ReadDelayShift,

BIU_Recovery = 1 << 8,
BIU_Hold = 1 << 9,
BIU_Float = 1 << 10,
BIU_PreStrobe = 1 << 11,
BIU_Width8 = 0 << 12,
BIU_Width16 = 1 << 12,
BIU_AutoIncrement = 1 << 13,

BIU_SizeShift = 16,
BIU_Size = 31 << BIU_SizeShift,

BIU_DmaDelayShift = 24,
BIU_DmaDelay = 15 << BIU_DmaDelayShift,

BIU_AddrError = 1 << 28,
BIU_UseDmaDelay = 1 << 29,
BIU_Dma32 = 1 << 30,
BIU_Wait = 1U << 31
};

/// @brief EXP1 BIU control port
inline constexpr volatile uint32_t &Exp1BIU() { return MMIO<uint32_t>(0x1008); }
/// @brief EXP3 BIU control port
inline constexpr volatile uint32_t &Exp3BIU() { return MMIO<uint32_t>(0x100C); }
/// @brief BIOS BIU control port
inline constexpr volatile uint32_t &BiosBIU() { return MMIO<uint32_t>(0x1010); }
/// @brief SPU BIU control port
inline constexpr volatile uint32_t &SpuBIU() { return MMIO<uint32_t>(0x1014); }
/// @brief CD BIU control port
inline constexpr volatile uint32_t &CdBIU() { return MMIO<uint32_t>(0x1018); }
/// @brief EXP2 BIU control port
inline constexpr volatile uint32_t &Exp2BIU() { return MMIO<uint32_t>(0x101C); }

/// @brief Common delay control port
inline constexpr volatile uint32_t &ComDelay() { return MMIO<uint32_t>(0x1020); }
/// @brief RAM size control port
inline constexpr volatile uint32_t &RamSize() { return MMIO<uint32_t>(0x1060); }

// DUART
/// @brief DUART mode port
Expand Down
9 changes: 9 additions & 0 deletions include/CKSDK/SPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@ namespace CKSDK
/// @brief Initialize SPU
/// @note For internal use only
void Init();

/// @brief DMA between SPU RAM and main RAM
/// @param data Pointer in main RAM to DMA
/// @param length Length in bytes to DMA
/// @param addr Address in SPU RAM to DMA
/// @param write `true` if DMAing from main RAM to SPU RAM, `false` if DMAing from SPU RAM to main RAM
void DMA(void *data, size_t length, uint16_t addr, bool write);
/// @brief Wait for SPU dma to finish
void DMASync();
}
}
4 changes: 2 additions & 2 deletions ld/exe.ld
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ ENTRY(_start)

MEMORY {
/* Mapped into KSEG0 */
KERNEL_RAM (rwx) : ORIGIN = 0x80000000, LENGTH = 0x010000
APP_RAM (rwx) : ORIGIN = 0x80010000, LENGTH = 0x7f0000
KERNEL_RAM (rwx) : ORIGIN = 0x80000000, LENGTH = 0x000088
APP_RAM (rwx) : ORIGIN = 0x80001000, LENGTH = 0x7ff000
}

SECTIONS {
Expand Down
8 changes: 6 additions & 2 deletions src/CD/CD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,12 @@ namespace CKSDK
OS::EnableIRQ();

// Setup CD
OS::CdDelaySize() = 0x00020943;
OS::ComDelayCfg() = 0x1325;
OS::CdBIU() = 0
| (3 << OS::BIU_WriteDelayShift)
| (4 << OS::BIU_ReadDelayShift)
| OS::BIU_Recovery
| OS::BIU_PreStrobe
| (2 << OS::BIU_SizeShift);

OS::CdStat() = 0x01;
OS::CdIrq() = 0x1F; // Acknowledge all IRQs
Expand Down
19 changes: 10 additions & 9 deletions src/OS/OS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ namespace CKSDK
static InterruptCallback dma_callbacks[8] = {};
static uint32_t dma_callbacks_count = 0;

static void **bios_a0_tbl = (void**)0x80000200;
static void (*bios_FlushICache)();

// ISR callback
extern "C" Thread *CKSDK_OS_ISR_Callback(Thread *fp)
{
Expand Down Expand Up @@ -115,6 +112,13 @@ namespace CKSDK
// OS functions
KEEP void Init()
{
// Setup common delay
OS::ComDelay() = 0
| (5 << 0) // COM0 - Recovery period cycles
| (2 << 4) // COM1 - Hold period cycles
| (3 << 8) // COM2 - Floating release cycles
| (1 << 12); // COM3 - Strobe active-going edge delay

// Reset interrupt controller
DisableIRQ_SR();
OS::IrqMask() = 0;
Expand All @@ -124,9 +128,6 @@ namespace CKSDK
OS::DmaDpcr() &= ~0x0888888;
OS::DmaDicr() = 0;

// Recall OS functions
bios_FlushICache = (void(*)())bios_a0_tbl[0x44];

// Install ISR
uint32_t *ip = (uint32_t*)0x80000080;

Expand All @@ -135,8 +136,8 @@ namespace CKSDK
// nop
ip[1] = 0;

// Flush I-cache
bios_FlushICache();
// Flush cache as we just modified executable RAM
FlushICache();

// Enable ISR
EnableIRQ_SR();
Expand Down Expand Up @@ -207,7 +208,7 @@ namespace CKSDK

KEEP void FlushICache()
{
bios_FlushICache();
((void(*)())(*((uint32_t *)0x80000000)))();
}
}
}
8 changes: 7 additions & 1 deletion src/OS/TTY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ namespace CKSDK
KEEP void Init()
{
// Initialize TTY on EXP2
OS::Exp2DelaySize() = 0x70777;
OS::Exp2BIU() = 0
| (7 << OS::BIU_WriteDelayShift)
| (7 << OS::BIU_ReadDelayShift)
| OS::BIU_Recovery
| OS::BIU_Hold
| OS::BIU_Float
| (7 << OS::BIU_SizeShift);
}

KEEP void Out(const char *str)
Expand Down
Loading

0 comments on commit 9d8b6bf

Please sign in to comment.