sdk-core
v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Toggle main menu visibility
Loading...
Searching...
No Matches
sio.c
Go to the documentation of this file.
1
/*
2
# _____ ___ ____ ___ ____
3
# ____| | ____| | | |____|
4
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5
#-----------------------------------------------------------------------
6
# (c) 2003 Marcus R. Brown <mrbrown@0xd6.org>
7
# Licenced under Academic Free License version 2.0
8
# Review ps2sdk README & LICENSE files for further details.
9
*/
10
15
16
#include <
stdio.h
>
17
#include <stddef.h>
18
#include <stdarg.h>
19
20
#include "
tamtypes.h
"
21
#include "
kernel.h
"
22
#include "
string.h
"
23
#include "sio.h"
24
25
#ifndef PS2LIB_STR_MAX
26
#define PS2LIB_STR_MAX 4096
27
#endif
28
30
#define CPUCLK 294912000
32
#define LCR_SCS_VAL (1 << 5)
33
34
void
sio_init(u32 baudrate, u8 lcr_ueps, u8 lcr_upen, u8 lcr_usbl, u8 lcr_umode)
35
{
36
u32 brd;
/* Baud rate divisor. */
37
u8 bclk = 0;
/* Baud rate generator clock. */
38
39
_sw(
LCR_SCS_VAL
| ((lcr_ueps & 1) << 4) | ((lcr_upen & 1) << 3) |
40
((lcr_usbl & 1) << 2) | (lcr_umode & 1),
41
SIO_LCR
);
42
43
/* Disable all interrupts. */
44
_sw(0,
SIO_IER
);
45
46
/* Reset the FIFOs. */
47
_sw(
SIO_FCR_FRSTE
|
SIO_FCR_RFRST
|
SIO_FCR_TFRST
,
SIO_FCR
);
48
/* Enable them. */
49
_sw(0,
SIO_FCR
);
50
51
brd =
CPUCLK
/ (baudrate * 256);
52
53
while
((brd >= 256) && (++bclk < 4))
54
brd /= 4;
55
56
_sw((bclk << 8) | brd,
SIO_BGR
);
57
}
58
59
static
u8 ___last_sio_putc = 0;
60
int
sio_putc(
int
c)
61
{
62
if
((c ==
'\n'
) && (___last_sio_putc !=
'\r'
)) {
63
// hack: if the character to be outputted is a '\n'
64
// and the previously-outputted character is not a '\r',
65
// output a '\r' first.
66
sio_putc(
'\r'
);
67
}
68
69
/* Block until we're ready to transmit. */
70
while
((_lw(
SIO_ISR
) & 0xf000) == 0x8000)
71
;
72
73
_sb(c,
SIO_TXFIFO
);
74
___last_sio_putc = c;
75
return
c;
76
}
77
78
int
sio_getc()
79
{
80
/* Do we have something in the RX FIFO? */
81
if
(_lw(
SIO_ISR
) & 0xf00) {
82
u8 b = _lb(
SIO_RXFIFO
);
83
_sw(7,
SIO_ISR
);
84
return
b;
85
}
86
87
/* Return EOF. */
88
return
-1;
89
}
90
91
int
sio_getc_block
()
92
{
93
/* Do we have something in the RX FIFO? */
94
while
(!(_lw(
SIO_ISR
) & 0xf00))
95
;
96
return
sio_getc();
97
}
98
99
size_t
sio_write
(
void
*buf,
size_t
size)
100
{
101
u8 *p = (u8 *)buf;
102
size_t
i;
103
104
for
(i = 0; i < size; i++)
105
sio_putc(p[i]);
106
107
return
size;
108
}
109
110
size_t
sio_read
(
void
*buf,
size_t
size)
111
{
112
u8 *p = (u8 *)buf;
113
size_t
i;
114
115
for
(i = 0; i < size; i++) {
116
int
c;
117
118
if
((c = sio_getc()) == -1)
119
break
;
120
121
p[i] = (u8)c;
122
}
123
124
return
i;
125
}
126
127
int
sio_puts(
const
char
*str)
128
{
129
int
res;
130
131
res = sio_putsn(str);
132
133
sio_putc(
'\r'
);
134
sio_putc(
'\n'
);
135
return
res + 2;
136
}
137
138
int
sio_putsn(
const
char
*str)
139
{
140
int
res;
141
142
for
(res = 0; *str; res++, str++)
143
sio_putc(*str);
144
145
return
res;
146
}
147
148
char
*
sio_gets
(
char
*str)
149
{
150
char
*s = str;
151
152
while
(1) {
153
int
c;
154
155
c =
sio_getc_block
();
156
/* Check for newline. */
157
if
(c ==
'\n'
|| c ==
'\r'
)
158
break
;
159
160
*s++ = c;
161
}
162
163
*s =
'\0'
;
164
return
str;
165
}
166
167
void
sio_flush
()
168
{
169
while
(_lw(
SIO_ISR
) & 0xf00)
170
_lb(
SIO_RXFIFO
);
171
}
SIO_LCR
#define SIO_LCR
Definition
sio.h:30
SIO_BGR
#define SIO_BGR
Definition
sio.h:78
SIO_IER
#define SIO_IER
Definition
sio.h:56
sio_flush
void sio_flush(void)
Definition
sio.c:167
SIO_FCR_RFRST
#define SIO_FCR_RFRST
Definition
sio.h:73
SIO_FCR_TFRST
#define SIO_FCR_TFRST
Definition
sio.h:75
sio_getc_block
int sio_getc_block(void)
Definition
sio.c:91
SIO_ISR
#define SIO_ISR
Definition
sio.h:63
SIO_RXFIFO
#define SIO_RXFIFO
Definition
sio.h:83
SIO_FCR_FRSTE
#define SIO_FCR_FRSTE
Definition
sio.h:71
SIO_TXFIFO
#define SIO_TXFIFO
Definition
sio.h:81
sio_read
size_t sio_read(void *buf, size_t size)
Definition
sio.c:110
sio_gets
char * sio_gets(char *str)
Definition
sio.c:148
SIO_FCR
#define SIO_FCR
Definition
sio.h:69
sio_write
size_t sio_write(void *buf, size_t size)
Definition
sio.c:99
kernel.h
LCR_SCS_VAL
#define LCR_SCS_VAL
Definition
sio.c:32
CPUCLK
#define CPUCLK
Definition
sio.c:30
stdio.h
string.h
tamtypes.h
core
kernel
src
sio.c
Generated by
1.18.0