aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorspicyjpeg <thatspicyjpeg@gmail.com>2022-12-18 15:44:22 +0100
committerspicyjpeg <thatspicyjpeg@gmail.com>2022-12-18 15:44:22 +0100
commitb3fc13c36c4ed666e34e292a9ed7d45ae4f49454 (patch)
tree5a6af613dc529cd5dd5c6ef022be44d2508a6782
parent3b7c46ab74548a9a79bfb867551c51dd877c8f4d (diff)
downloadpsn00bsdk-b3fc13c36c4ed666e34e292a9ed7d45ae4f49454.tar.gz
Rearrange hwregs_c.h and k573io.h, add clz intrinsics
-rw-r--r--examples/cdrom/cdbrowse/main.c1
-rw-r--r--examples/demos/n00bdemo/main.c6
-rw-r--r--examples/io/pads/spi.c28
-rw-r--r--examples/io/system573/k573io.c126
-rw-r--r--examples/io/system573/k573io.h274
-rw-r--r--examples/io/system573/main.c80
-rw-r--r--libpsn00b/include/hwregs_a.inc64
-rw-r--r--libpsn00b/include/hwregs_c.h61
-rw-r--r--libpsn00b/libc/clz.s43
-rw-r--r--libpsn00b/psxcd/common.c2
-rw-r--r--libpsn00b/psxetc/interrupts.c2
-rw-r--r--libpsn00b/psxsio/sio.c40
-rw-r--r--libpsn00b/psxsio/tty.c4
-rw-r--r--libpsn00b/psxspu/common.c6
14 files changed, 334 insertions, 403 deletions
diff --git a/examples/cdrom/cdbrowse/main.c b/examples/cdrom/cdbrowse/main.c
index f614f1d..c554f64 100644
--- a/examples/cdrom/cdbrowse/main.c
+++ b/examples/cdrom/cdbrowse/main.c
@@ -55,6 +55,7 @@
*/
#include <stdint.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
diff --git a/examples/demos/n00bdemo/main.c b/examples/demos/n00bdemo/main.c
index 069b549..6d0be3c 100644
--- a/examples/demos/n00bdemo/main.c
+++ b/examples/demos/n00bdemo/main.c
@@ -76,7 +76,7 @@ volatile int timer_counter = 0, frame_counter = 0, frame_rate = 0;
void sort_overlay(int showlotl);
void lightdemo();
-#define K573_WATCHDOG *((volatile unsigned short *) 0x1f5c0000)
+#define K573_WATCHDOG *((volatile unsigned short *) 0xbf5c0000)
#define K573_EXP1_CFG 0x24173f47
void timerTick() {
@@ -101,8 +101,8 @@ void timerSetup() {
EnterCriticalSection();
#ifdef SYSTEM_573_SUPPORT
- EXP1_ADDR = 0x1f000000;
- EXP1_DELAY_SIZE = K573_EXP1_CFG;
+ BUS_EXP1_ADDR = 0x1f000000;
+ BUS_EXP1_CFG = K573_EXP1_CFG;
#endif
TIMER_CTRL(2) = 0x0258; // CLK/8 input, IRQ on reload
diff --git a/examples/io/pads/spi.c b/examples/io/pads/spi.c
index 292e682..234bdba 100644
--- a/examples/io/pads/spi.c
+++ b/examples/io/pads/spi.c
@@ -93,8 +93,8 @@ static void _spi_next_req(void) {
static void _spi_poll_irq_handler(void) {
// Fetch the last response byte, which wasn't followed by a pulse on /ACK,
// from the RX FIFO.
- if (JOY_STAT & 0x0002)
- _context.rx_buff[_context.rx_len - 1] = (uint8_t) JOY_TXRX;
+ if (SIO_STAT(0) & 0x0002)
+ _context.rx_buff[_context.rx_len - 1] = (uint8_t) SIO_DATA(0);
if (_context.callback)
_context.callback(_context.port, _context.rx_buff, _context.rx_len);
@@ -109,49 +109,49 @@ static void _spi_poll_irq_handler(void) {
// enabling the /ACK IRQ. In order to communicate with controllers, /CS has
// to be driven low again for about 20 us before sending the first byte.
// TODO: these delays can be probably tweaked for better performance
- JOY_CTRL = 0x0010;
+ SIO_CTRL(0) = 0x0010;
for (uint32_t i = 0; i < 1000; i++)
__asm__ volatile("");
- JOY_CTRL = 0x1003 | (_context.port << 13);
+ SIO_CTRL(0) = 0x1003 | (_context.port << 13);
for (uint32_t i = 0; i < 2000; i++)
__asm__ volatile("");
// Send the first byte indicating which device to address. If the matching
// device is connected, it will reply by triggering the /ACK IRQ.
- JOY_TXRX = _context.tx_buff[0];
+ SIO_DATA(0) = _context.tx_buff[0];
}
static void _spi_ack_irq_handler(void) {
// Wait until /ACK is pulled up by the controller before sending the next
// byte. According to nocash docs, this has to be done before resetting the
// IRQ.
- while (JOY_STAT & 0x0080)
+ while (SIO_STAT(0) & 0x0080)
__asm__ volatile("");
// Keep /CS pulled low and acknowledge the IRQ (bit 4) to ensure it can be
// triggered again.
- JOY_CTRL = 0x1013 | (_context.port << 13);
+ SIO_CTRL(0) = 0x1013 | (_context.port << 13);
if (!_context.rx_len) {
// We just sent the first address byte. Obviously the response we
// received was read from an open bus, so the SPI port's internal FIFO
// must be flushed (by performing dummy reads) to ensure we are only
// going to read valid data from now on.
- JOY_TXRX;
+ SIO_DATA(0);
} else if (_context.rx_len <= SPI_BUFF_LEN) {
// If this is not the first byte, put it in the RX buffer.
- _context.rx_buff[_context.rx_len - 1] = (uint8_t) JOY_TXRX;
+ _context.rx_buff[_context.rx_len - 1] = (uint8_t) SIO_DATA(0);
}
// Send the next byte, or a null byte if there is no more data to send and
// we're just reading a response.
_context.rx_len++;
if (_context.rx_len < _context.tx_len)
- JOY_TXRX = (uint32_t) _context.tx_buff[_context.rx_len];
+ SIO_DATA(0) = (uint32_t) _context.tx_buff[_context.rx_len];
else
- JOY_TXRX = 0x00;
+ SIO_DATA(0) = 0x00;
}
/* Public API */
@@ -198,9 +198,9 @@ void SPI_Init(SPI_Callback callback) {
InterruptCallback(7, &_spi_ack_irq_handler);
ExitCriticalSection();
- JOY_CTRL = 0x0040; // Reset all registers
- JOY_MODE = 0x000d; // 1x multiplier, 8 data bits, no parity
- JOY_BAUD = 0x0088; // 250000 bps
+ SIO_CTRL(0) = 0x0040; // Reset all registers
+ SIO_MODE(0) = 0x000d; // 1x multiplier, 8 data bits, no parity
+ SIO_BAUD(0) = 0x0088; // 250000 bps
SPI_SetPollRate(250);
_current_req = 0;
diff --git a/examples/io/system573/k573io.c b/examples/io/system573/k573io.c
index 53c109f..69cc871 100644
--- a/examples/io/system573/k573io.c
+++ b/examples/io/system573/k573io.c
@@ -1,12 +1,6 @@
/*
* PSn00bSDK Konami System 573 example (I/O driver)
* (C) 2022 spicyjpeg - MPL licensed
- *
- * Note that this is far from being a complete driver. It currently lacks:
- * - ATAPI driver
- * - Flash erasing/writing APIs
- * - JVS bus APIs
- * - Functions for accessing the digital I/O board's MP3 decoder
*/
#include <stdint.h>
@@ -15,115 +9,47 @@
#include "k573io.h"
-K573_IOBoardType _board_type = IO_TYPE_ANALOG;
-
-/* I/O board light control */
-
-static void _k573_set_lights_analog(uint32_t lights) {
- uint32_t bits = 0xffffffff;
-
- bits ^= (lights & 0x01010101) << 7; // Lamp n*8+0 -> bit n*8+7
- bits ^= (lights & 0x02020202) << 5; // Lamp n*8+1 -> bit n*8+6
- bits ^= (lights & 0x04040404) >> 1; // Lamp n*8+2 -> bit n*8+1
- bits ^= (lights & 0x08080808) >> 3; // Lamp n*8+3 -> bit n*8+0
- bits ^= (lights & 0x10101010) << 1; // Lamp n*8+4 -> bit n*8+5
- bits ^= (lights & 0x20202020) >> 1; // Lamp n*8+5 -> bit n*8+4
- bits ^= (lights & 0x40404040) >> 3; // Lamp n*8+6 -> bit n*8+3
- bits ^= (lights & 0x80808080) >> 5; // Lamp n*8+7 -> bit n*8+2
-
- K573_IO_BOARD[ANALOG_IO_REG_LIGHTS0] = (bits) & 0xff;
- K573_IO_BOARD[ANALOG_IO_REG_LIGHTS1] = (bits >> 8) & 0xff;
- K573_IO_BOARD[ANALOG_IO_REG_LIGHTS2] = (bits >> 16) & 0xff;
- K573_IO_BOARD[ANALOG_IO_REG_LIGHTS3] = (bits >> 24) & 0xff;
-}
-
-// This function controls light outputs on digital I/O boards (i.e. the ones
-// that also include MP3 playback hardware, used by most Bemani games).
-// TODO: test this on real hardware -- it might not work if lights are handled
-// by the board's FPGA, which requires a binary blob...
-static void _k573_set_lights_digital(uint32_t lights) {
- uint32_t bits = 0xffffffff;
-
- bits ^= (lights & 0x11111111); // Lamp n*4+0 -> bit n*4+0
- bits ^= (lights & 0x22222222) << 1; // Lamp n*4+1 -> bit n*4+2
- bits ^= (lights & 0x44444444) << 1; // Lamp n*4+2 -> bit n*4+3
- bits ^= (lights & 0x88888888) >> 2; // Lamp n*4+3 -> bit n*4+1
-
- K573_IO_BOARD[DIGITAL_IO_REG_LIGHTS0] = ((bits) & 0xf) << 12;
- K573_IO_BOARD[DIGITAL_IO_REG_LIGHTS1] = ((bits >> 4) & 0xf) << 12;
- K573_IO_BOARD[DIGITAL_IO_REG_LIGHTS2] = ((bits >> 8) & 0xf) << 12;
- K573_IO_BOARD[DIGITAL_IO_REG_LIGHTS3] = ((bits >> 12) & 0xf) << 12;
- K573_IO_BOARD[DIGITAL_IO_REG_LIGHTS4] = ((bits >> 16) & 0xf) << 12;
- K573_IO_BOARD[DIGITAL_IO_REG_LIGHTS5] = ((bits >> 20) & 0xf) << 12;
- //K573_IO_BOARD[DIGITAL_IO_REG_LIGHTS6] = ((bits >> 24) & 0xf) << 12;
- K573_IO_BOARD[DIGITAL_IO_REG_LIGHTS7] = ((bits >> 28) & 0xf) << 12;
-}
-
-static const void (*_k573_set_lights[])(uint32_t) = {
- &_k573_set_lights_analog,
- &_k573_set_lights_digital
-};
-
-/* Public API */
-
uint32_t K573_GetJAMMAInputs(void) {
uint32_t inputs;
- inputs = K573_IO_CHIP[IO_REG_IN2];
- inputs |= ((K573_IO_CHIP[IO_REG_IN3_LOW] >> 8) & 0x0f) << 16;
- inputs |= ((K573_IO_CHIP[IO_REG_IN3_HIGH] >> 8) & 0x0f) << 20;
- inputs |= ((K573_IO_CHIP[IO_REG_IN1_HIGH] >> 8) & 0x1f) << 24;
- inputs |= (K573_IO_CHIP[IO_REG_IN1_LOW] & 0x07) << 29;
+ inputs = K573_JAMMA_IN;
+ inputs |= ((K573_EXT_IN_P1 >> 8) & 0x0f) << 16;
+ inputs |= ((K573_EXT_IN_P2 >> 8) & 0x0f) << 20;
+ inputs |= ((K573_MISC_IN >> 8) & 0x1f) << 24;
+ inputs |= (K573_DIP_CART_IN & 0x07) << 29;
return ~inputs;
}
-void K573_SetLights(uint32_t lights) {
- if (_board_type > IO_TYPE_DIGITAL)
- return;
-
- _k573_set_lights[_board_type](lights);
-}
+void K573_SetAnalogLights(uint32_t lights) {
+ uint32_t bits = 0xffffffff;
-void K573_SetBoardType(K573_IOBoardType type) {
- _k573_set_lights[_board_type](0);
- _board_type = type;
+ bits ^= (lights & 0x00010101) << 7; // Bit 0 -> bit 7
+ bits ^= (lights & 0x00020202) >> 1; // Bit 1 -> bit 0
+ bits ^= (lights & 0x00040404) << 4; // Bit 2 -> bit 6
+ bits ^= (lights & 0x00080808) >> 2; // Bit 3 -> bit 1
+ bits ^= (lights & 0x00101010) << 1; // Bit 4 -> bit 5
+ bits ^= (lights & 0x00202020) >> 3; // Bit 5 -> bit 2
+ bits ^= (lights & 0x00404040) >> 2; // Bit 6 -> bit 4
+ bits ^= (lights & 0x00808080) >> 4; // Bit 7 -> bit 3
+ bits ^= (lights & 0x0f000000) >> 24;
+
+ K573_IO_BOARD[ANALOG_IO_REG_LIGHTS_A] = (bits) & 0xff;
+ K573_IO_BOARD[ANALOG_IO_REG_LIGHTS_B] = (bits >> 8) & 0xff;
+ K573_IO_BOARD[ANALOG_IO_REG_LIGHTS_C] = (bits >> 16) & 0xff;
+ K573_IO_BOARD[ANALOG_IO_REG_LIGHTS_D] = (bits >> 24) & 0xff;
}
-/*void K573_DDRStageCommand(uint32_t value, uint32_t length) {
- uint32_t last_bit = 0;
- uint32_t mask = 1;
-
- for (uint32_t i = 0; i < length; i++) {
- uint32_t bit = DDR_LIGHT_P1_MUX_DATA | DDR_LIGHT_P2_MUX_DATA;
- if (value & mask)
- bit = 0;
-
- K573_SetLights(last_bit | DDR_LIGHT_P1_MUX_CLK | DDR_LIGHT_P2_MUX_CLK);
- _k573_delay_hblanks(20);
- K573_SetLights(bit | DDR_LIGHT_P1_MUX_CLK | DDR_LIGHT_P2_MUX_CLK);
- _k573_delay_hblanks(20);
- K573_SetLights(bit);
- _k573_delay_hblanks(20);
-
- last_bit = bit;
- mask <<= 1;
- }
-
- K573_SetLights(0);
-}*/
-
-//K573_DDRStageCommand(0x000c90, 13);
-//K573_DDRStageCommand(0x000001, 22);
-
void K573_Init(void) {
- EXP1_ADDR = 0x1f000000;
- EXP1_DELAY_SIZE = 0x24173f47; // 573 BIOS uses this value
+ BUS_EXP1_ADDR = 0x1f000000;
+ BUS_EXP1_CFG = 0x24173f47; // 573 BIOS uses this value
// Bit 6 of this register controls the audio DAC and must be set, otherwise
// no sound will be output. Most of the other bits are data clocks/strobes
// and should be pulled high when not in use.
- K573_IO_CHIP[IO_REG_OUT0] = 0x01e7;
+ K573_MISC_OUT =
+ MISC_OUT_ADC_MOSI | MISC_OUT_ADC_CS | MISC_OUT_ADC_SCK |
+ MISC_OUT_CDDA_ENABLE | MISC_OUT_SPU_ENABLE | MISC_OUT_MCU_CLOCK;
K573_RESET_WATCHDOG();
}
diff --git a/examples/io/system573/k573io.h b/examples/io/system573/k573io.h
index 8655237..552a93c 100644
--- a/examples/io/system573/k573io.h
+++ b/examples/io/system573/k573io.h
@@ -1,57 +1,49 @@
/*
* PSn00bSDK Konami System 573 example (I/O driver)
* (C) 2022 spicyjpeg - MPL licensed
+ *
+ * Reference: https://psx-spx.consoledev.net/konamisystem573/#register-map
*/
#ifndef __K573IO_H
#define __K573IO_H
#include <stdint.h>
+#include <hwregs_c.h>
/* Register definitions */
-#define K573_BANK_SWITCH *((volatile uint16_t *) 0xbf500000)
-#define K573_IDE_RESET *((volatile uint16_t *) 0xbf560000)
-#define K573_WATCHDOG *((volatile uint16_t *) 0xbf5c0000)
-#define K573_EXT_OUT *((volatile uint16_t *) 0xbf600000)
-#define K573_JVS_INPUT *((volatile uint16_t *) 0xbf680000)
-#define K573_SECURITY_OUT *((volatile uint16_t *) 0xbf6a0000)
-
-#define K573_FLASH ((volatile uint16_t *) 0xbf000000)
-#define K573_IO_CHIP ((volatile uint16_t *) 0xbf400000)
-#define K573_IDE_CS0 ((volatile uint16_t *) 0xbf480000)
-#define K573_IDE_CS1 ((volatile uint16_t *) 0xbf4c0000)
-#define K573_RTC ((volatile uint16_t *) 0xbf620000)
-#define K573_IO_BOARD ((volatile uint16_t *) 0xbf640000)
-
-typedef enum _K573_IOChipRegister {
- IO_REG_OUT0 = 0x0,
- IO_REG_IN0 = 0x0,
- IO_REG_IN1_LOW = 0x2,
- IO_REG_IN1_HIGH = 0x3,
- IO_REG_IN2 = 0x4,
- IO_REG_IN3_LOW = 0x6,
- IO_REG_IN3_HIGH = 0x7
-} K573_IOChipRegister;
+#define K573_MISC_OUT _MMIO16(EXP1BASE | 0x400000)
+#define K573_DIP_CART_IN _MMIO16(EXP1BASE | 0x400004)
+#define K573_MISC_IN _MMIO16(EXP1BASE | 0x400006)
+#define K573_JAMMA_IN _MMIO16(EXP1BASE | 0x400008)
+#define K573_JVS_RX_DATA _MMIO16(EXP1BASE | 0x40000a)
+#define K573_EXT_IN_P1 _MMIO16(EXP1BASE | 0x40000c)
+#define K573_EXT_IN_P2 _MMIO16(EXP1BASE | 0x40000e)
+
+#define K573_BANK_SEL _MMIO16(EXP1BASE | 0x500000)
+#define K573_JVS_RESET _MMIO16(EXP1BASE | 0x520000)
+#define K573_IDE_RESET _MMIO16(EXP1BASE | 0x560000)
+#define K573_WATCHDOG _MMIO16(EXP1BASE | 0x5c0000)
+#define K573_EXT_OUT _MMIO16(EXP1BASE | 0x600000)
+#define K573_JVS_TX_DATA _MMIO16(EXP1BASE | 0x680000)
+#define K573_CART_OUT _MMIO16(EXP1BASE | 0x6a0000)
+
+#define K573_FLASH _ADDR16(EXP1BASE | 0x000000)
+#define K573_IDE_CS0 _ADDR16(EXP1BASE | 0x480000)
+#define K573_IDE_CS1 _ADDR16(EXP1BASE | 0x4c0000)
+#define K573_RTC _ADDR16(EXP1BASE | 0x620000)
+#define K573_IO_BOARD _ADDR16(EXP1BASE | 0x640000)
typedef enum _K573_IOBoardRegister {
- ANALOG_IO_REG_LIGHTS0 = 0x40,
- ANALOG_IO_REG_LIGHTS1 = 0x44,
- ANALOG_IO_REG_LIGHTS2 = 0x48,
- ANALOG_IO_REG_LIGHTS3 = 0x4c,
+ ANALOG_IO_REG_LIGHTS_A = 0x40,
+ ANALOG_IO_REG_LIGHTS_B = 0x44,
+ ANALOG_IO_REG_LIGHTS_C = 0x48,
+ ANALOG_IO_REG_LIGHTS_D = 0x4c,
- // The digital I/O board has a lot more registers than these, but there
- // seems to be no DIGITAL_IO_LIGHTS6 register. WTF
- DIGITAL_IO_REG_LIGHTS1 = 0x70,
- DIGITAL_IO_REG_LIGHTS0 = 0x71,
- DIGITAL_IO_REG_LIGHTS3 = 0x72,
- DIGITAL_IO_REG_LIGHTS7 = 0x73,
- DIGITAL_IO_REG_DS2401 = 0x77,
DIGITAL_IO_REG_FPGA_STATUS = 0x7b,
- DIGITAL_IO_REG_FPGA_UPLOAD = 0x7c,
- DIGITAL_IO_REG_LIGHTS4 = 0x7d,
- DIGITAL_IO_REG_LIGHTS5 = 0x7e,
- DIGITAL_IO_REG_LIGHTS2 = 0x7f,
+ DIGITAL_IO_REG_FPGA_PROGRAM = 0x7c,
+ DIGITAL_IO_REG_FPGA_DATA = 0x7d,
FISHBAIT_IO_REG_UNKNOWN = 0x08,
FISHBAIT_IO_REG_MOTOR = 0x40,
@@ -74,10 +66,22 @@ typedef enum _K573_RTCRegister {
RTC_REG_YEAR = 0x1fff
} K573_RTCRegister;
-/* Inputs and lights bitfields */
-
-typedef enum _K573_JAMMAInputs {
- // IO_REG_IN2 bits 0-15
+/* Bitfields */
+
+typedef enum _K573_MiscOutFlag {
+ MISC_OUT_ADC_MOSI = 1 << 0,
+ MISC_OUT_ADC_CS = 1 << 1,
+ MISC_OUT_ADC_SCK = 1 << 2,
+ MISC_OUT_COIN_COUNTER1 = 1 << 3,
+ MISC_OUT_COIN_COUNTER2 = 1 << 4,
+ MISC_OUT_AMP_ENABLE = 1 << 5,
+ MISC_OUT_CDDA_ENABLE = 1 << 6,
+ MISC_OUT_SPU_ENABLE = 1 << 7,
+ MISC_OUT_MCU_CLOCK = 1 << 8
+} K573_MiscOutFlag;
+
+typedef enum _K573_JAMMAInput {
+ // K573_JAMMA_IN bits 0-15
JAMMA_P2_LEFT = 1 << 0,
JAMMA_P2_RIGHT = 1 << 1,
JAMMA_P2_UP = 1 << 2,
@@ -95,108 +99,114 @@ typedef enum _K573_JAMMAInputs {
JAMMA_P1_BUTTON3 = 1 << 14,
JAMMA_P1_START = 1 << 15,
- // IO_REG_IN3_LOW bits 8-11
+ // K573_EXT_IN_P1 bits 8-11
JAMMA_P1_BUTTON4 = 1 << 16,
JAMMA_P1_BUTTON5 = 1 << 17,
JAMMA_TEST = 1 << 18,
JAMMA_P1_BUTTON6 = 1 << 19,
- // IO_REG_IN3_HIGH bits 8-11
+ // K573_EXT_IN_P2 bits 8-11
JAMMA_P2_BUTTON4 = 1 << 20,
JAMMA_P2_BUTTON5 = 1 << 21,
JAMMA_UNKNOWN = 1 << 22,
JAMMA_P2_BUTTON6 = 1 << 23,
- // IO_REG_IN1_HIGH bits 8-12
+ // K573_MISC_IN bits 8-12
JAMMA_COIN1 = 1 << 24,
JAMMA_COIN2 = 1 << 25,
JAMMA_PCMCIA1 = 1 << 26,
JAMMA_PCMCIA2 = 1 << 27,
JAMMA_SERVICE = 1 << 28,
- // IO_REG_IN1_LOW bits 0-2
+ // K573_DIP_CART_IN bits 0-2
JAMMA_DIP1 = 1 << 29,
JAMMA_DIP2 = 1 << 30,
JAMMA_DIP3 = 1 << 31
-} K573_JAMMAInputs;
+} K573_JAMMAInput;
typedef enum _K573_Light {
+ LIGHT_A0 = 1 << 0,
+ LIGHT_A1 = 1 << 1,
+ LIGHT_A2 = 1 << 2,
+ LIGHT_A3 = 1 << 3,
+ LIGHT_A4 = 1 << 4,
+ LIGHT_A5 = 1 << 5,
+ LIGHT_A6 = 1 << 6,
+ LIGHT_A7 = 1 << 7,
+ LIGHT_B0 = 1 << 8,
+ LIGHT_B1 = 1 << 9,
+ LIGHT_B2 = 1 << 10,
+ LIGHT_B3 = 1 << 11,
+ LIGHT_B4 = 1 << 12,
+ LIGHT_B5 = 1 << 13,
+ LIGHT_B6 = 1 << 14,
+ LIGHT_B7 = 1 << 15,
+ LIGHT_C0 = 1 << 16,
+ LIGHT_C1 = 1 << 17,
+ LIGHT_C2 = 1 << 18,
+ LIGHT_C3 = 1 << 19,
+ LIGHT_C4 = 1 << 20,
+ LIGHT_C5 = 1 << 21,
+ LIGHT_C6 = 1 << 22,
+ LIGHT_C7 = 1 << 23,
+ LIGHT_D0 = 1 << 24,
+ LIGHT_D1 = 1 << 25,
+ LIGHT_D2 = 1 << 26,
+ LIGHT_D3 = 1 << 27,
+
// Dance Dance Revolution (2-player)
- DDR_LIGHT_P1_UP = 1 << 0,
- DDR_LIGHT_P1_LEFT = 1 << 1,
- DDR_LIGHT_P1_RIGHT = 1 << 2,
- DDR_LIGHT_P1_DOWN = 1 << 3,
- DDR_LIGHT_P1_MUX_DATA = 1 << 4, // Used for stage commands
- DDR_LIGHT_P1_MUX_CLK = 1 << 7, // Used for stage commands
- DDR_LIGHT_P2_UP = 1 << 8,
- DDR_LIGHT_P2_LEFT = 1 << 9,
- DDR_LIGHT_P2_RIGHT = 1 << 10,
- DDR_LIGHT_P2_DOWN = 1 << 11,
- DDR_LIGHT_P2_MUX_DATA = 1 << 12, // Used for stage commands
- DDR_LIGHT_P2_MUX_CLK = 1 << 15, // Used for stage commands
- DDR_LIGHT_P1_BUTTONS = 1 << 17,
- DDR_LIGHT_P2_BUTTONS = 1 << 18,
- DDR_LIGHT_MARQUEE_BR = 1 << 20,
- DDR_LIGHT_MARQUEE_BL = 1 << 21,
- DDR_LIGHT_MARQUEE_TL = 1 << 22,
- DDR_LIGHT_MARQUEE_TR = 1 << 23,
- DDR_LIGHT_SPEAKER_DIGITAL = 1 << 28, // Speaker neon on digital I/O boards
- DDR_LIGHT_SPEARKER_ANALOG = 1 << 30, // Speaker neon on analog I/O boards
+ LIGHT_DDR_P1_UP = 1 << 0,
+ LIGHT_DDR_P1_DOWN = 1 << 1,
+ LIGHT_DDR_P1_LEFT = 1 << 2,
+ LIGHT_DDR_P1_RIGHT = 1 << 3,
+ LIGHT_DDR_P1_IO_DATA = 1 << 4,
+ LIGHT_DDR_P1_IO_CLK = 1 << 5,
+ LIGHT_DDR_P2_UP = 1 << 8,
+ LIGHT_DDR_P2_DOWN = 1 << 9,
+ LIGHT_DDR_P2_LEFT = 1 << 10,
+ LIGHT_DDR_P2_RIGHT = 1 << 11,
+ LIGHT_DDR_P2_IO_DATA = 1 << 12,
+ LIGHT_DDR_P2_IO_CLK = 1 << 13,
+ LIGHT_DDR_P1_BUTTONS = 1 << 18,
+ LIGHT_DDR_P2_BUTTONS = 1 << 19,
+ LIGHT_DDR_MARQUEE_BR = 1 << 20,
+ LIGHT_DDR_MARQUEE_TR = 1 << 21,
+ LIGHT_DDR_MARQUEE_BL = 1 << 22,
+ LIGHT_DDR_MARQUEE_TL = 1 << 23,
+ LIGHT_DDR_SPEAKER = 1 << 24,
// Dance Dance Revolution Solo
- DDRSOLO_LIGHT_EXTRA4 = 1 << 8,
- DDRSOLO_LIGHT_EXTRA2 = 1 << 9,
- DDRSOLO_LIGHT_EXTRA1 = 1 << 10,
- DDRSOLO_LIGHT_EXTRA3 = 1 << 11,
- DDRSOLO_LIGHT_SPEAKER = 1 << 16,
- DDRSOLO_LIGHT_BUTTONS = 1 << 20,
- DDRSOLO_LIGHT_BODY_CENTER = 1 << 21,
- DDRSOLO_LIGHT_BODY_RIGHT = 1 << 22,
- DDRSOLO_LIGHT_BODY_LEFT = 1 << 23,
+ LIGHT_SOLO_SPEAKER = 1 << 16,
+ LIGHT_SOLO_BUTTONS = 1 << 20,
+ LIGHT_SOLO_BODY_LEFT = 1 << 21,
+ LIGHT_SOLO_BODY_CENTER = 1 << 22,
+ LIGHT_SOLO_BODY_RIGHT = 1 << 23,
// DrumMania 1st Mix
- DM_LIGHT_HIHAT = 1 << 16,
- DM_LIGHT_HIGH_TOM = 1 << 17,
- DM_LIGHT_LOW_TOM = 1 << 18,
- DM_LIGHT_SNARE = 1 << 19,
- DM_LIGHT_CYMBAL = 1 << 20,
- DM_LIGHT_START_BUTTON = 1 << 21,
- DM_LIGHT_SELECT_BUTTON = 1 << 22,
- DM_LIGHT_NEON_BOTTOM = 1 << 27,
- DM_LIGHT_SPOT = 1 << 30,
- DM_LIGHT_NEON_TOP = 1 << 31,
+ LIGHT_DM_HIHAT = 1 << 16,
+ LIGHT_DM_SNARE = 1 << 17,
+ LIGHT_DM_HIGH_TOM = 1 << 18,
+ LIGHT_DM_LOW_TOM = 1 << 19,
+ LIGHT_DM_CYMBAL = 1 << 20,
+ LIGHT_DM_START_BUTTON = 1 << 22,
+ LIGHT_DM_SELECT_BUTTON = 1 << 23,
+ LIGHT_DM_SPOT = 1 << 24,
+ LIGHT_DM_NEON_TOP = 1 << 25,
+ LIGHT_DM_NEON_BOTTOM = 1 << 27,
// DrumMania 2nd Mix and later
- DM2_LIGHT_HIHAT = 1 << 0,
- DM2_LIGHT_HIGH_TOM = 1 << 1,
- DM2_LIGHT_LOW_TOM = 1 << 2,
- DM2_LIGHT_SNARE = 1 << 3,
- DM2_LIGHT_SPOT = 1 << 8,
- DM2_LIGHT_NEON_TOP = 1 << 9,
- DM2_LIGHT_NEON_BOTTOM = 1 << 11,
- DM2_LIGHT_CYMBAL = 1 << 12,
- DM2_LIGHT_START_BUTTON = 1 << 13,
- DM2_LIGHT_SELECT_BUTTON = 1 << 14
+ LIGHT_DM2_HIHAT = 1 << 0,
+ LIGHT_DM2_SNARE = 1 << 1,
+ LIGHT_DM2_HIGH_TOM = 1 << 2,
+ LIGHT_DM2_LOW_TOM = 1 << 3,
+ LIGHT_DM2_SPOT = 1 << 8,
+ LIGHT_DM2_NEON_BOTTOM = 1 << 9,
+ LIGHT_DM2_NEON_TOP = 1 << 10,
+ LIGHT_DM2_CYMBAL = 1 << 12,
+ LIGHT_DM2_START_BUTTON = 1 << 14,
+ LIGHT_DM2_SELECT_BUTTON = 1 << 15
} K573_Light;
-/* System information structures */
-
-typedef enum _K573_IOBoardType {
- IO_TYPE_ANALOG = 0, // Light control board (early Bemani)
- IO_TYPE_DIGITAL = 1, // Light control + MP3 playback board (late Bemani)
- IO_TYPE_FISHBAIT = 2, // Fishing reel controls interface (Fisherman's Bait)
- IO_TYPE_GUNMANIA = 3, // Gun control board (Gun Mania)
- IO_TYPE_KARAOKE = 4, // Karaoke I/O + video mux board (DDR Karaoke Mix)
- IO_TYPE_SERIAL = 5 // Serial port (debug?) board (Great Bishi Bashi Champ)
- // TODO: does PunchMania have its own board?
-} K573_IOBoardType;
-
-typedef enum _K573_DDRStageType {
- DDR_TYPE_NONE = 0,
- DDR_TYPE_2PLAYER = 1,
- DDR_TYPE_SOLO = 2
-} K573_DDRStageType;
-
/* Public API */
#define K573_RESET_WATCHDOG() { \
@@ -208,43 +218,27 @@ extern "C" {
#endif
/**
- * @brief Returns a bitfield containing the state of all JAMMA inputs and DIP
+ * @brief Returns the state of JAMMA inputs and DIP switches.
+ *
+ * @details Returns a bitfield containing the state of all JAMMA inputs and DIP
* switches. All bits are inverted as they represent the actual signal levels
* on the JAMMA pins (i.e. normally pulled up by resistors, shorted to ground
* when a button is pressed).
*
- * @return Inverted logical OR of K573_JAMMAInputs flags
+ * @return Binary OR of K573_JAMMAInputs flags
*/
uint32_t K573_GetJAMMAInputs(void);
/**
- * @brief Sets the 32 light outputs provided by the the analog and digital I/O
- * boards to match the provided bitfield. K573_SetBoardType(IO_TYPE_ANALOG) or
- * K573_SetBoardType(IO_TYPE_DIGITAL) must be called beforehand to set the I/O
- * board type.
- *
- * @param lights Non-inverted logical OR of K573_Light flags
- */
-void K573_SetLights(uint32_t lights);
-
-/**
- * @brief Sets the installed I/O board type. Currently only IO_TYPE_ANALOG and
- * IO_TYPE_DIGITAL are supported.
+ * @brief Sets light outputs on the analog I/O board.
*
- * @param type
- */
-void K573_SetBoardType(K573_IOBoardType type);
-
-/**
- * @brief Sends a command to the multiplexer PCB embedded into DDR stage units
- * (if the system is a DDR cabinet) by bitbanging it through the light outputs.
- * K573_SetBoardType(IO_TYPE_ANALOG) or K573_SetBoardType(IO_TYPE_DIGITAL) must
- * be called beforehand to set the I/O board type.
+ * @details Sets the 32 light outputs provided by the analog I/O board (if
+ * installed) to match the provided bitfield. No other I/O boards are currently
+ * supported.
*
- * @param value
- * @param length Number of bits to send (1-32)
+ * @param lights Binary OR of K573_Light flags
*/
-//void K573_DDRStageCommand(uint32_t value, uint32_t length);
+void K573_SetAnalogLights(uint32_t lights);
/**
* @brief Initializes the expansion port registers to enable System 573 I/O
diff --git a/examples/io/system573/main.c b/examples/io/system573/main.c
index 39ddb64..2878508 100644
--- a/examples/io/system573/main.c
+++ b/examples/io/system573/main.c
@@ -66,12 +66,7 @@
#include "k573io.h"
-const char *const IO_BOARD_TYPES[] = {
- "ANALOG",
- "DIGITAL"
-};
-
-#define btoi(x) ((((x) >> 4) & 0xf) * 10 + ((x) & 0xf))
+#define _btoi(x) ((((x) >> 4) & 0xf) * 10 + ((x) & 0xf))
/* Display/GPU context utilities */
@@ -137,30 +132,18 @@ void display(RenderContext *ctx) {
static RenderContext ctx;
-#define SHOW_STATUS(...) { FntPrint(-1, __VA_ARGS__); FntFlush(-1); display(&ctx); }
-#define SHOW_ERROR(...) { SHOW_STATUS(__VA_ARGS__); while (1) __asm__("nop"); }
-
int main(int argc, const char* argv[]) {
init_context(&ctx);
K573_Init();
const char *const version = (const char *const) GetSystemInfo(0x02);
- //if (strncmp(version, "Konami OS", 9))
- //SHOW_ERROR("ERROR: NOT RUNNING ON A SYSTEM 573!\n\n[%s]\n", version);
uint32_t counter = 0;
uint32_t inputs = K573_GetJAMMAInputs();
uint32_t last_inputs = 0xff;
uint32_t current_light = 0;
- // DIP switch 1 is used to determine if an analog or digital I/O board is
- // installed.
- K573_IOBoardType io_type = (inputs & JAMMA_DIP1)
- ? IO_TYPE_ANALOG
- : IO_TYPE_DIGITAL;
-
- K573_SetBoardType(io_type);
- K573_SetLights(1);
+ K573_SetAnalogLights(1);
while (1) {
inputs = K573_GetJAMMAInputs();
@@ -168,16 +151,14 @@ int main(int argc, const char* argv[]) {
FntPrint(-1, "COUNTER=%d\n", counter++);
FntPrint(-1, "\nJAMMA INPUTS:\n");
- FntPrint(-1, " IN2 =%016@\n", inputs & 0xffff);
- FntPrint(-1, " IN3_L=%04@\n", (inputs >> 16) & 0x0f);
- FntPrint(-1, " IN3_H=%04@\n", (inputs >> 20) & 0x0f);
- FntPrint(-1, " IN1_H=%05@\n", (inputs >> 24) & 0x1f);
+ FntPrint(-1, " MAIN=%016@\n", inputs & 0xffff);
+ FntPrint(-1, " EXT1=%04@\n", (inputs >> 16) & 0x0f);
+ FntPrint(-1, " EXT2=%04@\n", (inputs >> 20) & 0x0f);
+ FntPrint(-1, " COIN=%05@\n", (inputs >> 24) & 0x1f);
- FntPrint(-1, "\nCABINET LIGHTS:\n");
- FntPrint(-1, " BOARD=%s\n", IO_BOARD_TYPES[io_type]);
- FntPrint(-1, " LIGHT=%d\n", current_light);
- FntPrint(-1, "\n [DIP SW1] CHANGE BOARD TYPE\n");
- FntPrint(-1, "\n [TEST SW] CHANGE ACTIVE LIGHT\n");
+ FntPrint(-1, "\nANALOG IO LIGHT TEST:\n");
+ FntPrint(-1, " LAMP=%d\n", current_light);
+ FntPrint(-1, " PRESS [TEST] TO CHANGE LAMP\n");
// Request the current date/time from the RTC and display it.
K573_RTC[RTC_REG_CTRL] |= 0x40;
@@ -185,18 +166,17 @@ int main(int argc, const char* argv[]) {
FntPrint(
-1,
" %02d-%02d-%02d %02d:%02d:%02d\n",
- btoi(K573_RTC[RTC_REG_YEAR]),
- btoi(K573_RTC[RTC_REG_MONTH]),
- btoi(K573_RTC[RTC_REG_DAY_OF_MONTH] & 0x3f),
- btoi(K573_RTC[RTC_REG_HOURS]),
- btoi(K573_RTC[RTC_REG_MINUTES]),
- btoi(K573_RTC[RTC_REG_SECONDS] & 0x7f)
+ _btoi(K573_RTC[RTC_REG_YEAR]),
+ _btoi(K573_RTC[RTC_REG_MONTH]),
+ _btoi(K573_RTC[RTC_REG_DAY_OF_MONTH] & 0x3f),
+ _btoi(K573_RTC[RTC_REG_HOURS]),
+ _btoi(K573_RTC[RTC_REG_MINUTES]),
+ _btoi(K573_RTC[RTC_REG_SECONDS] & 0x7f)
);
FntPrint(-1, "\nSYSTEM:\n");
FntPrint(-1, " KERNEL=%s\n", version);
FntPrint(-1, " DIP SW=%03@\n", inputs >> 29);
- FntPrint(-1, " PCMCIA=%02@\n", (inputs >> 26) & 0x3);
FntFlush(-1);
display(&ctx);
@@ -208,26 +188,16 @@ int main(int argc, const char* argv[]) {
// Change the currently active light if the test button on the 573's
// front panel is pressed. DDR non-light outputs are skipped.
if (!(last_inputs & JAMMA_TEST) && (inputs & JAMMA_TEST)) {
- current_light++;
- if (
- (current_light == 4) || // DDR_LIGHT_P1_MUX_DATA
- (current_light == 7) || // DDR_LIGHT_P1_MUX_CLK
- (current_light == 12) || // DDR_LIGHT_P2_MUX_DATA
- (current_light == 15) // DDR_LIGHT_P2_MUX_CLK
- ) current_light++;
-
- current_light %= 32;
- K573_SetLights(1 << current_light);
- }
-
- // if DIP switch 1 is toggled, change the I/O board type.
- if ((last_inputs & JAMMA_DIP1) != (inputs & JAMMA_DIP1)) {
- io_type = (inputs & JAMMA_DIP1)
- ? IO_TYPE_ANALOG
- : IO_TYPE_DIGITAL;
-
- K573_SetBoardType(io_type);
- K573_SetLights(1 << current_light);
+ do {
+ current_light = (current_light + 1) % 28;
+ } while (
+ (current_light == 4) || // LIGHT_DDR_P1_IO_DATA
+ (current_light == 5) || // LIGHT_DDR_P1_IO_CLK
+ (current_light == 12) || // LIGHT_DDR_P2_IO_DATA
+ (current_light == 13) // LIGHT_DDR_P2_IO_CLK
+ );
+
+ K573_SetAnalogLights(1 << current_light);
}
last_inputs = inputs;
diff --git a/libpsn00b/include/hwregs_a.inc b/libpsn00b/include/hwregs_a.inc
index b0c6954..02c9a5a 100644
--- a/libpsn00b/include/hwregs_a.inc
+++ b/libpsn00b/include/hwregs_a.inc
@@ -4,6 +4,8 @@
## Constants
.set IOBASE, 0xbf80
+.set EXP1BASE, 0xbf00
+
.set F_CPU, 33868800
.set F_GPU, 53222400
@@ -61,34 +63,32 @@
.set SPU_CURRENT_VOL_L, 0x1db8
.set SPU_CURRENT_VOL_R, 0x1dba
-.set SPU_VOICE_VOL_L, 0x00
-.set SPU_VOICE_VOL_R, 0x02
-.set SPU_VOICE_FREQ, 0x04
-.set SPU_VOICE_ADDR, 0x06
-.set SPU_VOICE_ADSR1, 0x08
-.set SPU_VOICE_ADSR2, 0x0a
-.set SPU_VOICE_LOOP, 0x0e
+.set SPU_CH_VOL_L, 0x00
+.set SPU_CH_VOL_R, 0x02
+.set SPU_CH_FREQ, 0x04
+.set SPU_CH_ADDR, 0x06
+.set SPU_CH_ADSR1, 0x08
+.set SPU_CH_ADSR2, 0x0a
+.set SPU_CH_LOOP_ADDR, 0x0e
## MDEC
.set MDEC0, 0x1820
.set MDEC1, 0x1824
-## SPI controller port
-
-.set JOY_TXRX, 0x1040
-.set JOY_STAT, 0x1044
-.set JOY_MODE, 0x1048
-.set JOY_CTRL, 0x104a
-.set JOY_BAUD, 0x104e
+## SPI and serial interfaces
-## Serial port
+.set SIO0_DATA, 0x1040
+.set SIO0_STAT, 0x1044
+.set SIO0_MODE, 0x1048
+.set SIO0_CTRL, 0x104a
+.set SIO0_BAUD, 0x104e
-.set SIO_TXRX, 0x1050
-.set SIO_STAT, 0x1054
-.set SIO_MODE, 0x1058
-.set SIO_CTRL, 0x105a
-.set SIO_BAUD, 0x105e
+.set SIO1_DATA, 0x1050
+.set SIO1_STAT, 0x1054
+.set SIO1_MODE, 0x1058
+.set SIO1_CTRL, 0x105a
+.set SIO1_BAUD, 0x105e
## IRQ controller
@@ -142,15 +142,15 @@
.set TIMER2_CTRL, 0x1124
.set TIMER2_RELOAD, 0x1128
-## Memory control
-
-.set EXP1_ADDR, 0x1000
-.set EXP2_ADDR, 0x1004
-.set EXP1_DELAY_SIZE, 0x1008
-.set EXP3_DELAY_SIZE, 0x100c
-.set BIOS_DELAY_SIZE, 0x1010
-.set SPU_DELAY_SIZE, 0x1014
-.set CD_DELAY_SIZE, 0x1018
-.set EXP2_DELAY_SIZE, 0x101c
-.set COM_DELAY_CFG, 0x1020
-.set RAM_SIZE_CFG, 0x1060
+## Memory/bus control
+
+.set BUS_EXP1_ADDR, 0x1000
+.set BUS_EXP2_ADDR, 0x1004
+.set BUS_EXP1_CFG, 0x1008
+.set BUS_EXP3_CFG, 0x100c
+.set BUS_BIOS_CFG, 0x1010
+.set BUS_SPU_CFG, 0x1014
+.set BUS_CD_CFG, 0x1018
+.set BUS_EXP2_CFG, 0x101c
+.set BUS_COM_DELAY, 0x1020
+.set BUS_RAM_SIZE, 0x1060
diff --git a/libpsn00b/include/hwregs_c.h b/libpsn00b/include/hwregs_c.h
index 0e21922..7015101 100644
--- a/libpsn00b/include/hwregs_c.h
+++ b/libpsn00b/include/hwregs_c.h
@@ -8,15 +8,20 @@
#include <stdint.h>
-#define _MMIO8(addr) *((volatile uint8_t *) (addr))
-#define _MMIO16(addr) *((volatile uint16_t *) (addr))
-#define _MMIO32(addr) *((volatile uint32_t *) (addr))
+#define _ADDR8(addr) ((volatile uint8_t *) (addr))
+#define _ADDR16(addr) ((volatile uint16_t *) (addr))
+#define _ADDR32(addr) ((volatile uint32_t *) (addr))
+#define _MMIO8(addr) (*_ADDR8(addr))
+#define _MMIO16(addr) (*_ADDR16(addr))
+#define _MMIO32(addr) (*_ADDR32(addr))
/* Constants */
#define IOBASE 0xbf800000
-#define F_CPU 33868800UL
-#define F_GPU 53222400UL
+#define EXP1BASE 0xbf000000
+
+#define F_CPU 33868800L
+#define F_GPU 53222400L
/* GPU */
@@ -82,24 +87,16 @@
#define MDEC0 _MMIO32(IOBASE | 0x1820)
#define MDEC1 _MMIO32(IOBASE | 0x1824)
-/* SPI controller port */
+/* SPI and serial interfaces */
-// IMPORTANT: even though JOY_TXRX is a 32-bit register, it should only be
+// IMPORTANT: even though SIO_DATA is a 32-bit register, it should only be
// accessed as 8-bit. Reading it as 16 or 32-bit works fine on real hardware,
// but leads to problems in some emulators.
-#define JOY_TXRX _MMIO8 (IOBASE | 0x1040)
-#define JOY_STAT _MMIO16(IOBASE | 0x1044)
-#define JOY_MODE _MMIO16(IOBASE | 0x1048)
-#define JOY_CTRL _MMIO16(IOBASE | 0x104a)
-#define JOY_BAUD _MMIO16(IOBASE | 0x104e)
-
-/* Serial port */
-
-#define SIO_TXRX _MMIO8 (IOBASE | 0x1050)
-#define SIO_STAT _MMIO16(IOBASE | 0x1054)
-#define SIO_MODE _MMIO16(IOBASE | 0x1058)
-#define SIO_CTRL _MMIO16(IOBASE | 0x105a)
-#define SIO_BAUD _MMIO16(IOBASE | 0x105e)
+#define SIO_DATA(N) _MMIO8 (IOBASE | 0x1040 + 16 * (N))
+#define SIO_STAT(N) _MMIO16(IOBASE | 0x1044 + 16 * (N))
+#define SIO_MODE(N) _MMIO16(IOBASE | 0x1048 + 16 * (N))
+#define SIO_CTRL(N) _MMIO16(IOBASE | 0x104a + 16 * (N))
+#define SIO_BAUD(N) _MMIO16(IOBASE | 0x104e + 16 * (N))
/* IRQ controller */
@@ -121,17 +118,17 @@
#define TIMER_CTRL(N) _MMIO32(IOBASE | 0x1104 + 16 * (N))
#define TIMER_RELOAD(N) _MMIO32(IOBASE | 0x1108 + 16 * (N))
-/* Memory control */
-
-#define EXP1_ADDR _MMIO32(IOBASE | 0x1000)
-#define EXP2_ADDR _MMIO32(IOBASE | 0x1004)
-#define EXP1_DELAY_SIZE _MMIO32(IOBASE | 0x1008)
-#define EXP3_DELAY_SIZE _MMIO32(IOBASE | 0x100c)
-#define BIOS_DELAY_SIZE _MMIO32(IOBASE | 0x1010)
-#define SPU_DELAY_SIZE _MMIO32(IOBASE | 0x1014)
-#define CD_DELAY_SIZE _MMIO32(IOBASE | 0x1018)
-#define EXP2_DELAY_SIZE _MMIO32(IOBASE | 0x101c)
-#define COM_DELAY_CFG _MMIO32(IOBASE | 0x1020)
-#define RAM_SIZE_CFG _MMIO32(IOBASE | 0x1060)
+/* Memory/bus control */
+
+#define BUS_EXP1_ADDR _MMIO32(IOBASE | 0x1000)
+#define BUS_EXP2_ADDR _MMIO32(IOBASE | 0x1004)
+#define BUS_EXP1_CFG _MMIO32(IOBASE | 0x1008)
+#define BUS_EXP3_CFG _MMIO32(IOBASE | 0x100c)
+#define BUS_BIOS_CFG _MMIO32(IOBASE | 0x1010)
+#define BUS_SPU_CFG _MMIO32(IOBASE | 0x1014)
+#define BUS_CD_CFG _MMIO32(IOBASE | 0x1018)
+#define BUS_EXP2_CFG _MMIO32(IOBASE | 0x101c)
+#define BUS_COM_DELAY _MMIO32(IOBASE | 0x1020)
+#define BUS_RAM_SIZE _MMIO32(IOBASE | 0x1060)
#endif
diff --git a/libpsn00b/libc/clz.s b/libpsn00b/libc/clz.s
new file mode 100644
index 0000000..28a28eb
--- /dev/null
+++ b/libpsn00b/libc/clz.s
@@ -0,0 +1,43 @@
+# PSn00bSDK leading zero count intrinsics
+# (C) 2022 spicyjpeg - MPL licensed
+#
+# libgcc provides two functions used internally by GCC to count the number of
+# leading zeroes or ones in a value, _clzsi2() (32-bit) and _clzdi2() (64-bit).
+# This file overrides them with faster implementations that make use of the
+# GTE's LZCS/LZCR registers.
+
+.set noreorder
+
+.section .text._clzsi2
+.global _clzsi2
+.type _clzsi2, @function
+_clzsi2:
+ mtc2 $a0, $30
+ nop
+ nop
+ mfc2 $v0, $31
+
+ jr $ra
+ nop
+
+.section .text._clzdi2
+.global _clzdi2
+.type _clzdi2, @function
+_clzdi2:
+ bnez $a1, .Lhas_msb
+ nop
+
+ mtc2 $a0, $30 # if (!msb) return 32 + clz(lsb)
+ b .Lreturn
+ li $v1, 32
+
+.Lhas_msb:
+ mtc2 $a1, $30 # if (msb) return 0 + clz(msb)
+ nop
+ li $v1, 0
+
+.Lreturn:
+ mfc2 $v0, $31
+
+ jr $ra
+ addu $v0, $v1
diff --git a/libpsn00b/psxcd/common.c b/libpsn00b/psxcd/common.c
index 6b20df2..8b8030b 100644
--- a/libpsn00b/psxcd/common.c
+++ b/libpsn00b/psxcd/common.c
@@ -206,7 +206,7 @@ int CdInit(void) {
InterruptCallback(IRQ_CD, &_cd_irq_handler);
ExitCriticalSection();
- CD_DELAY_SIZE = 0x00020943;
+ BUS_CD_CFG = 0x00020943;
CD_REG(0) = 1;
CD_REG(3) = 0x1f; // Acknowledge all IRQs
diff --git a/libpsn00b/psxetc/interrupts.c b/libpsn00b/psxetc/interrupts.c
index f05b089..f2a273c 100644
--- a/libpsn00b/psxetc/interrupts.c
+++ b/libpsn00b/psxetc/interrupts.c
@@ -173,7 +173,7 @@ int ResetCallback(void) {
for (int i = 0; i < NUM_DMA_CHANNELS; i++)
_dma_handlers[i] = (void *) 0;
- COM_DELAY_CFG = 0x00001325;
+ BUS_COM_DELAY = 0x00001325;
_96_remove();
RestartCallback();
return 0;
diff --git a/libpsn00b/psxsio/sio.c b/libpsn00b/psxsio/sio.c
index cdcef0e..919f0cb 100644
--- a/libpsn00b/psxsio/sio.c
+++ b/libpsn00b/psxsio/sio.c
@@ -36,8 +36,8 @@ static volatile RingBuffer _tx_buffer, _rx_buffer;
static void _sio_handler(void) {
// Handle any incoming bytes.
- while (SIO_STAT & SR_RXRDY) {
- uint8_t value = SIO_TXRX;
+ while (SIO_STAT(1) & SR_RXRDY) {
+ uint8_t value = SIO_DATA(1);
// Skip storing this byte into the RX buffer if the callback returns a
// non-zero value.
@@ -63,7 +63,7 @@ static void _sio_handler(void) {
// Send the next byte in the buffer if the TX unit is ready. Note that
// checking for CTS is unnecessary as the serial port is already hardwired
// to do so.
- if (SIO_STAT & (SR_TXRDY | SR_TXU)) {
+ if (SIO_STAT(1) & (SR_TXRDY | SR_TXU)) {
int length = _tx_buffer.length;
if (length) {
@@ -71,18 +71,18 @@ static void _sio_handler(void) {
_tx_buffer.head = (head + 1) % BUFFER_LENGTH;
_tx_buffer.length = length - 1;
- SIO_CTRL |= CR_TXIEN;
- SIO_TXRX = _tx_buffer.data[head];
+ SIO_CTRL(1) |= CR_TXIEN;
+ SIO_DATA(1) = _tx_buffer.data[head];
} else {
- SIO_CTRL &= CR_TXIEN ^ 0xffff;
+ SIO_CTRL(1) &= CR_TXIEN ^ 0xffff;
}
}
// Acknowledge the IRQ and update flow control signals.
if (_rx_buffer.length < BUFFER_LENGTH)
- SIO_CTRL = CR_INTRST | (SIO_CTRL | _ctrl_reg_flag);
+ SIO_CTRL(1) = CR_INTRST | (SIO_CTRL(1) | _ctrl_reg_flag);
else
- SIO_CTRL = CR_INTRST | (SIO_CTRL & (_ctrl_reg_flag ^ 0xffff));
+ SIO_CTRL(1) = CR_INTRST | (SIO_CTRL(1) & (_ctrl_reg_flag ^ 0xffff));
}
/* Serial port initialization API */
@@ -91,10 +91,10 @@ void SIO_Init(int baud, uint16_t mode) {
EnterCriticalSection();
_old_sio_handler = InterruptCallback(IRQ_SIO1, &_sio_handler);
- SIO_CTRL = CR_ERRRST;
- SIO_MODE = (mode & 0xfffc) | MR_BR_16;
- SIO_BAUD = (uint16_t) ((int) 0x1fa400 / baud);
- SIO_CTRL = CR_TXEN | CR_RXEN | CR_RXIEN;
+ SIO_CTRL(1) = CR_ERRRST;
+ SIO_MODE(1) = (mode & 0xfffc) | MR_BR_16;
+ SIO_BAUD(1) = (uint16_t) ((int) 0x1fa400 / baud);
+ SIO_CTRL(1) = CR_TXEN | CR_RXEN | CR_RXIEN;
_tx_buffer.head = 0;
_tx_buffer.tail = 0;
@@ -113,7 +113,7 @@ void SIO_Quit(void) {
EnterCriticalSection();
InterruptCallback(IRQ_SIO1, _old_sio_handler);
- SIO_CTRL = CR_ERRRST;
+ SIO_CTRL(1) = CR_ERRRST;
ExitCriticalSection();
}
@@ -126,21 +126,21 @@ void SIO_SetFlowControl(SIO_FlowControl mode) {
_flow_control = SIO_FC_NONE;
_ctrl_reg_flag = 0;
- SIO_CTRL &= 0xffff ^ CR_DSRIEN;
+ SIO_CTRL(1) &= 0xffff ^ CR_DSRIEN;
break;
case SIO_FC_RTS_CTS:
_flow_control = SIO_FC_RTS_CTS;
_ctrl_reg_flag = CR_RTS;
- SIO_CTRL &= 0xffff ^ CR_DSRIEN;
+ SIO_CTRL(1) &= 0xffff ^ CR_DSRIEN;
break;
/*case SIO_FC_DTR_DSR:
_flow_control = SIO_FC_DTR_DSR;
_ctrl_reg_flag = CR_DTR;
- SIO_CTRL |= CR_DSRIEN;
+ SIO_CTRL(1) |= CR_DSRIEN;
break;*/
}
@@ -218,8 +218,8 @@ int SIO_WriteByte2(uint8_t value) {
// than via syscalls for performance reasons.
FastEnterCriticalSection();
- if (SIO_STAT & (SR_TXRDY | SR_TXU)) {
- SIO_TXRX = value;
+ if (SIO_STAT(1) & (SR_TXRDY | SR_TXU)) {
+ SIO_DATA(1) = value;
FastExitCriticalSection();
return 0;
}
@@ -238,7 +238,7 @@ int SIO_WriteByte2(uint8_t value) {
_tx_buffer.length = length + 1;
_tx_buffer.data[tail] = value;
- SIO_CTRL |= CR_TXIEN;
+ SIO_CTRL(1) |= CR_TXIEN;
FastExitCriticalSection();
return length;
@@ -256,7 +256,7 @@ int SIO_WriteSync(int mode) {
if (!_tx_buffer.length) {
// Wait for the TX unit to finish sending the last byte.
- while (!(SIO_STAT & (SR_TXRDY | SR_TXU)))
+ while (!(SIO_STAT(1) & (SR_TXRDY | SR_TXU)))
__asm__ volatile("");
} else {
//_sdk_log("SIO_WriteSync() timeout\n");
diff --git a/libpsn00b/psxsio/tty.c b/libpsn00b/psxsio/tty.c
index 4dc9fd1..a1b33c8 100644
--- a/libpsn00b/psxsio/tty.c
+++ b/libpsn00b/psxsio/tty.c
@@ -32,10 +32,10 @@ static int _sio_inout(FCB *fcb, int cmd) {
case 2: // write
for (int i = 0; i < fcb->trns_len; i++) {
- while (!(SIO_STAT & (SR_TXRDY | SR_TXU)))
+ while (!(SIO_STAT(1) & (SR_TXRDY | SR_TXU)))
__asm__ volatile("");
- SIO_TXRX = *(ptr++);
+ SIO_DATA(1) = *(ptr++);
}
return fcb->trns_len;
diff --git a/libpsn00b/psxspu/common.c b/libpsn00b/psxspu/common.c
index 5dae473..45654ad 100644
--- a/libpsn00b/psxspu/common.c
+++ b/libpsn00b/psxspu/common.c
@@ -45,9 +45,9 @@ static size_t _dma_transfer(uint32_t *data, size_t length, int write) {
}
// Increase bus delay for DMA reads
- SPU_DELAY_SIZE &= 0xf0ffffff;
+ BUS_SPU_CFG &= ~(0xf << 24);
if (!write)
- SPU_DELAY_SIZE = 2 << 24;
+ BUS_SPU_CFG = 2 << 24;
SPU_CTRL &= 0xffcf; // Disable DMA request
_wait_status(0x0030, 0x0000);
@@ -107,7 +107,7 @@ static size_t _manual_write(const uint16_t *data, size_t length) {
/* Public API */
void SpuInit(void) {
- SPU_DELAY_SIZE = 0x200931e1;
+ BUS_SPU_CFG = 0x200931e1;
SPU_CTRL = 0x0000; // SPU disabled
_wait_status(0x001f, 0x0000);