Skip to content

Commit 9f3727e

Browse files
committed
added pshittyload
1 parent 417c6bc commit 9f3727e

File tree

13 files changed

+789
-111
lines changed

13 files changed

+789
-111
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
command: |
2020
make -j 2
2121
make -C src/mips/hello -j 2
22-
make -C src/mips/sioload -j 2
22+
make -C src/mips/pshittyload -j 2
2323
make -C src/mips/shell -j 2
2424
make -C src/mips/openbios -j 2
2525
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
TARGET = sioload.psx
1+
TARGET = pshittyload.psx
22
TLOAD_ADDR = 0x80010000
33
SRCS = \
44
../common/hardware/cop0.s \
55
crt0/crt0.S \
66
src/main.c \
7-
src/serialio.c \
7+
src/ftfifo.c \
8+
9+
# src/serialio.c \
810
911
LIBS = ../ps1sdk/libps1sdk.a
1012

1113
LDSCRIPT = ../psx-exe.ld
12-
CPPFLAGS := -I../ps1sdk/include
14+
CPPFLAGS := -I../ps1sdk/include -Isrc -Iinclude
1315
LDFLAGS := -L../ps1sdk -lps1sdk
1416

1517
include ../common.mk
File renamed without changes.

src/mips/pshittyload/include/ftfifo.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef _FTFIFO_H
2+
#define _FTFIFO_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#define FTFIFO_DATA ((volatile uint8_t *)0x1F000000)
9+
#define FTFIFO_STAT ((volatile uint8_t *)0x1F000001)
10+
#define FTFIFO_STAT_RXRDY (1 << 0)
11+
#define FTFIFO_STAT_TXRDY (1 << 1)
12+
13+
uint8_t FT_peek8(uint32_t timeout, int *presult);
14+
uint16_t FT_peek16(uint32_t timeout, int *presult);
15+
uint32_t FT_peek32(uint32_t timeout, int *presult);
16+
17+
int FT_poke8(uint8_t d, uint32_t timeout);
18+
int FT_poke16(uint16_t d, uint32_t timeout);
19+
int FT_poke32(uint32_t d, uint32_t timeout);
20+
21+
#ifdef __cplusplus
22+
}
23+
#endif
24+
25+
#endif /* _FTFIFO_H */
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "include"
5+
},
6+
{
7+
"path": "src"
8+
}
9+
]
10+
}

src/mips/pshittyload/pshittyload.elf

74.8 KB
Binary file not shown.

src/mips/pshittyload/pshittyload.map

Lines changed: 463 additions & 0 deletions
Large diffs are not rendered by default.

src/mips/pshittyload/pshittyload.psx

5.57 KB
Binary file not shown.

src/mips/pshittyload/src/ftfifo.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/***************************************************************************
2+
* Copyright (C) 2019 PCSX-Redux authors *
3+
* *
4+
* This program is free software; you can redistribute it and/or modify *
5+
* it under the terms of the GNU General Public License as published by *
6+
* the Free Software Foundation; either version 2 of the License, or *
7+
* (at your option) any later version. *
8+
* *
9+
* This program is distributed in the hope that it will be useful, *
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12+
* GNU General Public License for more details. *
13+
* *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with this program; if not, write to the *
16+
* Free Software Foundation, Inc., *
17+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
18+
***************************************************************************/
19+
20+
#include "ftfifo.h"
21+
#include <fileio.h>
22+
#include <ps1hwregs.h>
23+
#include <ps1sdk.h>
24+
#include <serialio.h>
25+
#include <stdio.h>
26+
#include "common/hardware/cop0.h"
27+
28+
uint8_t FT_peek8(uint32_t timeout, int *presult) {
29+
int res = 0;
30+
uint8_t d = 0;
31+
32+
if (timeout == 0) {
33+
while (!(*FTFIFO_STAT & FTFIFO_STAT_RXRDY))
34+
;
35+
} else {
36+
while (!(*FTFIFO_STAT & FTFIFO_STAT_RXRDY)) {
37+
if (--timeout <= 0) {
38+
res = -1;
39+
goto fail;
40+
}
41+
}
42+
}
43+
44+
d = *FTFIFO_DATA;
45+
46+
fail:
47+
if (presult) *presult = res;
48+
49+
return d;
50+
}
51+
52+
int FT_poke8(uint8_t d, uint32_t timeout) {
53+
int res = 0;
54+
55+
if (timeout == 0) {
56+
while (!(*FTFIFO_STAT & FTFIFO_STAT_TXRDY))
57+
;
58+
} else {
59+
while (!(*FTFIFO_STAT & FTFIFO_STAT_TXRDY)) {
60+
if (--timeout == 0) {
61+
res = -1;
62+
goto fail;
63+
}
64+
}
65+
}
66+
67+
*FTFIFO_DATA = d;
68+
69+
fail:
70+
return res;
71+
}
72+
73+
uint32_t FT_peek32(uint32_t timeout, int *presult) {
74+
int res = 0;
75+
uint32_t d, d32 = 0;
76+
77+
for (int i = 0; i < 4; i++) {
78+
d = FT_peek8(0, &res);
79+
if (res != 0) break;
80+
d32 |= (d << (i * 8));
81+
}
82+
83+
if (presult) *presult = res;
84+
85+
return d32;
86+
}
87+
88+
int FT_poke32(uint32_t d, uint32_t timeout) {
89+
for (int i = 0; i < 4; i++) {
90+
if (FT_poke8(d & 0xFF, timeout) != 0) {
91+
return -1;
92+
}
93+
d >>= 8;
94+
}
95+
96+
return 0;
97+
}

src/mips/pshittyload/src/main.c

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/***************************************************************************
2+
* Copyright (C) 2019 PCSX-Redux authors *
3+
* *
4+
* This program is free software; you can redistribute it and/or modify *
5+
* it under the terms of the GNU General Public License as published by *
6+
* the Free Software Foundation; either version 2 of the License, or *
7+
* (at your option) any later version. *
8+
* *
9+
* This program is distributed in the hope that it will be useful, *
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12+
* GNU General Public License for more details. *
13+
* *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with this program; if not, write to the *
16+
* Free Software Foundation, Inc., *
17+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
18+
***************************************************************************/
19+
20+
#include <fileio.h>
21+
#include <ps1hwregs.h>
22+
#include <ps1sdk.h>
23+
#include <serialio.h>
24+
#include <stdio.h>
25+
#include "common/hardware/cop0.h"
26+
#include "ftfifo.h"
27+
28+
// switch between implementations
29+
#if 1
30+
#define COMMS_INIT() (0)
31+
#define COMMS_PEEK8 FT_peek8
32+
#define COMMS_PEEK16 FT_peek16
33+
#define COMMS_PEEK32 FT_peek32
34+
35+
#define COMMS_POKE8(__d, __timeout) FT_poke8(__d, __timeout)
36+
#define COMMS_POKE16(__d, __timeout) FT_poke16(__d, __timeout)
37+
#define COMMS_POKE32(__d, __timeout) FT_poke32(__d, __timeout)
38+
#else
39+
#define COMMS_INIT() sio_init(115200)
40+
#endif
41+
42+
/*
43+
* pshitty protocol:
44+
*
45+
* host writes 'P','S' then reads a byte from comms
46+
* ps reads 1 byte, if it's not 'P', discard it. and try gain.
47+
* ps reads 1 byte, if it's not 'S', writes '!'(NAK) then returns to waiting for 'P'
48+
* ps writes '+'(ACK)
49+
*
50+
* Host writes 32-bit load address(little endian)
51+
* Host writes 32-bit file size(little endian)
52+
* Reads 1 byte response from ps
53+
* ps reads load address and file size.
54+
* if load address + (file size - 2048) is invalid, ps writes '!' NAK and returns to syncing
55+
* ps writes '+'(ACK)
56+
* If host receives a NAK, returns to start of protocol.
57+
* Host writes the whole file(file size number bytes)
58+
* Host reads 1 byte, fails if not received after a resonable time(maybe 5 seconds)
59+
* ps reads 2048 bytes into exehdr buffer.
60+
* ps reads (file size - 2048) bytes to load address
61+
* ps writes ACK
62+
* ps calls Exec(&(exehdr->exec), 1, 0);
63+
* If host read a NAK, success! Otherwise failure.
64+
* ps writes NAK(this should never happen unless Exec fails or the main() of the exe returns.
65+
* ps returns to syncing
66+
*
67+
*/
68+
69+
// wait for remote to send 'P', 'S'
70+
// if the second char is not 'S', responds with a '!'(NAK) before continuing to wait.
71+
// otherwise sends a '+'(ACK) and returns
72+
void pshitty_sync(void) {
73+
int res = 0;
74+
while (1) {
75+
uint8_t d = COMMS_PEEK8(0, &res);
76+
if (res != 0) continue;
77+
if (d != 'P') {
78+
continue;
79+
}
80+
81+
d = COMMS_PEEK8(0, &res);
82+
if (res != 0) continue;
83+
if (d != 'S') {
84+
COMMS_POKE8('!', 0); // NAK
85+
} else {
86+
COMMS_POKE8('+', 0); // ACK
87+
return;
88+
}
89+
}
90+
}
91+
92+
void pshitty_loader(void) {
93+
uint8_t buf[2048];
94+
int err = 0;
95+
uint32_t load_addr, load_len;
96+
EXE_Header *exehdr = (EXE_Header *)buf;
97+
98+
while (1) {
99+
int rv = -1;
100+
int res = 0;
101+
102+
pshitty_sync();
103+
104+
load_addr = COMMS_PEEK32(1000, &res);
105+
if (res != 0) goto error;
106+
107+
load_len = COMMS_PEEK32(1000, &res);
108+
if (res != 0) goto error;
109+
110+
if (load_len < sizeof(buf)) {
111+
goto error;
112+
}
113+
114+
for (int i = 0; i < sizeof(buf); i++) {
115+
buf[i] = COMMS_PEEK8(1000, &res);
116+
if (res != 0) break;
117+
}
118+
if (res != 0) goto error;
119+
120+
load_len -= sizeof(buf);
121+
122+
for (int i = 0; i < load_len; i++) {
123+
((uint8_t *)load_addr)[i] = COMMS_PEEK8(1000, &res);
124+
if (res != 0) break;
125+
}
126+
if (res != 0) goto error;
127+
128+
exehdr->exec.stack_addr = 0x801FFF00;
129+
exehdr->exec.stack_size = 0;
130+
EnterCriticalSection();
131+
rv = Exec(&(exehdr->exec), 1, 0);
132+
133+
error:
134+
COMMS_POKE32(rv, 10000);
135+
}
136+
}
137+
138+
//~ void load_exec_this(void *dest, void *src, int len, void *entry)
139+
//~ {
140+
//~ memcpy(dest, src, len);
141+
//~ FlushCache();
142+
//~ if(entry == NULL) entry = dest;
143+
144+
//~ ((void) entry)();
145+
//~ }
146+
147+
//~ load_exec_this((void *) 0x80100000, &fifo_echo_shell_bin, sizeof(fifo_echo_shell_bin), 0);
148+
149+
// extern long _sio_control(unsigned long cmd, unsigned long arg, unsigned long param);
150+
151+
//~ int Sio1Callback (void (*func)())
152+
//~ {
153+
//~ return InterruptCallback(8, func);
154+
//~ }
155+
156+
// NOTE: This will remove whatever "tty" device is installed and
157+
// install the kernel "dummy" console driver.
158+
int DelSIO(void) {
159+
close(stdin);
160+
close(stdout);
161+
DelDevice("tty");
162+
163+
// sio_reset();
164+
165+
AddDummyConsoleDevice();
166+
167+
if (open("tty00:", O_RDONLY) != stdin) return 1;
168+
if (open("tty00:", O_WRONLY) != stdout) return 1;
169+
170+
return 0;
171+
}
172+
173+
int main(void) {
174+
// DelSIO(); // removes the "tty" device
175+
176+
COMMS_INIT();
177+
178+
pshitty_loader();
179+
180+
return 0;
181+
}

0 commit comments

Comments
 (0)