Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
link-sio1.h
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2026 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <stdint.h>
30
32#include "common/hardware/sio.h"
33
34/* SIO1 byte link at 115200 8N1 (x16, reload 18: 117600 baud, the setting the
35 Unirom-carrying cables are known to run at).
36
37 TX is gated by CTS in hardware. RX is not: the FIFO holds 8 bytes and the
38 hardware never drops RTS by itself, so RTS is raised only while the frame
39 layer is inside a receive (linkRxOpen .. linkRxClose) and polling. The host
40 may still push a few bytes after RTS drops, which the FIFO absorbs. The
41 sticky overrun bit is what says whether that held. */
42
43#define MONITOR_LINK_IS_STREAM 1
44
45#define SIO1_CTRL_BASE (SIO_CTRL_TXEN | SIO_CTRL_RXE)
46
47/* Baud = 2073600 / reload at x16: 18 -> 115200 (117600 actual), 5 -> 414720,
48 4 -> 518400. */
49#ifndef MONITOR_SIO1_RELOAD
50#define MONITOR_SIO1_RELOAD 18
51#endif
52
53static inline void linkInit(void) {
54 SIOS[1].ctrl = SIO_CTRL_IR;
55 SIOS[1].baudRate = MONITOR_SIO1_RELOAD;
56 SIOS[1].mode = 0x4e; /* 1 stop bit, 8 bits, no parity, x16 */
57 SIOS[1].ctrl = SIO1_CTRL_BASE;
58}
59
60static inline void linkRxOpen(void) { SIOS[1].ctrl = SIO1_CTRL_BASE | SIO_CTRL_RTS; }
61
62static inline void linkRxClose(void) { SIOS[1].ctrl = SIO1_CTRL_BASE; }
63
64static inline uint8_t linkGetByte(void) {
65 while ((SIOS[1].stat & SIO_STAT_RXRDY) == 0) {
66 }
67 return SIOS[1].fifo;
68}
69
70static inline void linkPutByte(uint8_t b) {
71 while ((SIOS[1].stat & SIO_STAT_TXRDY) == 0) {
72 }
73 SIOS[1].fifo = b;
74}
75
76/* Sticky receive overrun since the last linkClearErrors(). */
77static inline int linkOverrun(void) { return (SIOS[1].stat & SIO_STAT_OE) != 0; }
78
79static inline void linkClearErrors(void) { SIOS[1].ctrl |= SIO_CTRL_ERRRES; }
80
81/* Line rate, for SET_BAUD (design section 2a). */
82#define MONITOR_LINK_HAS_RATE 1
83
84static inline uint16_t linkGetRate(void) { return SIOS[1].baudRate; }
85
86/* Let the transmitter drain before changing the rate, so the ACK that
87 precedes the switch leaves at the old one. */
88static inline void linkSetRate(uint16_t reload) {
89 while ((SIOS[1].stat & SIO_STAT_TXEMPTY) == 0) {
90 }
91 SIOS[1].baudRate = reload;
92}
93
94/* The received-byte test the exception entry makes in assembly on every
95 interrupt (monitor.c, monitorSlotEntry): LINK_RX_STAT_LOAD from
96 LINK_RX_STAT_ADDR, then LINK_RX_STAT_BIT. SIO1 STAT bit 1, RX ready. */
97#define LINK_RX_STAT_ADDR 0x1f801054
98#define LINK_RX_STAT_LOAD "lhu"
99#define LINK_RX_STAT_BIT 0x02
100
101/* Non-blocking read: 1 and the byte if one was waiting, else 0. */
102static inline int linkTryGetByte(uint8_t *b) {
103 if ((SIOS[1].stat & SIO_STAT_RXRDY) == 0) return 0;
104 *b = SIOS[1].fifo;
105 return 1;
106}
uint32_t stat
Definition dma.c:128
uint8_t b
Definition gte-depthcue.c:39
#define SIOS
Definition sio.h:43
@ SIO_CTRL_RTS
Definition sio.h:51
@ SIO_CTRL_IR
Definition sio.h:52
@ SIO_CTRL_ERRRES
Definition sio.h:50
@ SIO_STAT_TXEMPTY
Definition sio.h:63
@ SIO_STAT_RXRDY
Definition sio.h:62
@ SIO_STAT_OE
Definition sio.h:65
@ SIO_STAT_TXRDY
Definition sio.h:61