Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
transport.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
31/* Monitor frame layer, over whichever 16-bit word link the build selects
32 (monitor/link.h). Frame shape (protocol design section 2):
33 [SYNC:u16=0x55AA] [TYPE:u16] [LEN:u16] [payload: LEN words] [CKSUM:u32]
34 CKSUM is Fletcher-32 over the 16-bit words TYPE, LEN, payload (sent
35 endian (each word contributes low byte then high byte), transmitted
36 low-word-first. SYNC is not part of the checksum. */
37
38#define FRAME_SYNC 0x55AA
39
40/* Receive return codes. 0 = a valid frame was decoded. */
41#define TRANSPORT_OK 0
42#define TRANSPORT_EBADLEN (-1) /* payload longer than caller's buffer */
43#define TRANSPORT_ECKSUM (-2) /* checksum mismatch */
44
45/* Largest LEN a stream link accepts: an 8 KiB bulk payload plus the header
46 words any frame type puts in front of it (DESIGN section 13). */
47#define TRANSPORT_STREAM_MAX_LEN (4096 + 16)
48
49/* Bring up the link. Idempotent; safe to call once at monitor entry. */
50void transportInit(void);
51
52/* Send one frame on the word channel. Blocks on the STAT TX-word-ready bit
53 (bit2) for each word. */
54void transportSendFrame(uint16_t type, const uint16_t *payload, uint16_t len);
55
56/* Streaming send, for responses whose payload is generated on the fly (e.g.
57 READ_MEM copying straight out of target memory without a staging buffer).
58 Call Begin with the exact word count, push exactly that many words with
59 SendWord, then End. Not reentrant: one frame at a time (the monitor is
60 single-threaded). */
61void transportSendBegin(uint16_t type, uint16_t len);
62void transportSendWord(uint16_t w);
63void transportSendEnd(void);
64
65/* Block until a full, checksum-valid frame arrives. On success returns
66 TRANSPORT_OK and fills type/payload/lenOut; payload holds up to maxLen
67 words. On a length overflow or checksum mismatch the frame is fully drained
68 (both ends stay word-aligned) and a negative TRANSPORT_* code is returned. */
69/* SET_BAUD's second half (design section 2a): switch the link to `reload`,
70 wait up to a window for the exact PING frame, answer it with a pongType
71 frame, and wait up to another window for a second PING. 1: both came, the
72 new rate stays and the caller answers the second. 0: the old rate is back.
73 -1: the link has no rate. */
74int transportTryRate(uint16_t reload, uint16_t pongType, uint16_t pongWord);
75int transportHasRate(void);
76
77int transportRecvFrame(uint16_t *type, uint16_t *payload, uint16_t maxLen, uint16_t *lenOut);
78
79/* Streaming receive, the counterpart to the streaming send. Lets a caller pull
80 a frame's payload word by word straight into its final destination (e.g.
81 PCread writing decoded bytes directly to the target's own buffer, no staging
82 RAM, no memcpy). Call Begin (returns TYPE and payload word count), pull
83 exactly `len` words with RecvWord, then End (validates the checksum). Not
84 reentrant: one frame at a time. */
85void transportRecvBegin(uint16_t *type, uint16_t *len);
86
87/* STOP while RUNNING (byte links; always 0 on ATCONS). Called from the
88 exception entry on an interrupt, never blocks when nothing was received.
89 If a byte is waiting, everything the host sends is read and dropped until
90 the line has been quiet for MONITOR_STOP_QUIET_SPINS polls. Returns 1 when
91 a 0 was among it - a frame start, which is all a host sends a running
92 target (a STOP frame) - and 0 when it was console text only. The frame is
93 not parsed: the 8-byte SIO1 FIFO may well have overrun by the time the
94 next interrupt comes, so its tail cannot be relied on. */
95int transportStopPending(void);
96uint16_t transportRecvWord(void);
97int transportRecvEnd(void);
int transportRecvFrame(uint16_t *type, uint16_t *payload, uint16_t maxLen, uint16_t *lenOut)
Definition transport.c:228
uint16_t transportRecvWord(void)
Definition transport.c:159
void transportSendWord(uint16_t w)
Definition transport.c:61
int transportRecvEnd(void)
Definition transport.c:165
void transportRecvBegin(uint16_t *type, uint16_t *len)
Definition transport.c:114
void transportSendEnd(void)
Definition transport.c:66
int transportStopPending(void)
Definition transport.c:156
int transportTryRate(uint16_t reload, uint16_t pongType, uint16_t pongWord)
Definition transport.c:225
int transportHasRate(void)
Definition transport.c:224
void transportInit(void)
Definition transport.c:46
void transportSendBegin(uint16_t type, uint16_t len)
Definition transport.c:51
void transportSendFrame(uint16_t type, const uint16_t *payload, uint16_t len)
Definition transport.c:72