sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
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
34void 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. */
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
59static u8 ___last_sio_putc = 0;
60int 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
78int 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
92{
93 /* Do we have something in the RX FIFO? */
94 while (!(_lw(SIO_ISR) & 0xf00))
95 ;
96 return sio_getc();
97}
98
99size_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
110size_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
127int 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
138int 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
148char *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
168{
169 while (_lw(SIO_ISR) & 0xf00)
170 _lb(SIO_RXFIFO);
171}
#define SIO_LCR
Definition sio.h:30
#define SIO_BGR
Definition sio.h:78
#define SIO_IER
Definition sio.h:56
void sio_flush(void)
Definition sio.c:167
#define SIO_FCR_RFRST
Definition sio.h:73
#define SIO_FCR_TFRST
Definition sio.h:75
int sio_getc_block(void)
Definition sio.c:91
#define SIO_ISR
Definition sio.h:63
#define SIO_RXFIFO
Definition sio.h:83
#define SIO_FCR_FRSTE
Definition sio.h:71
#define SIO_TXFIFO
Definition sio.h:81
size_t sio_read(void *buf, size_t size)
Definition sio.c:110
char * sio_gets(char *str)
Definition sio.c:148
#define SIO_FCR
Definition sio.h:69
size_t sio_write(void *buf, size_t size)
Definition sio.c:99
#define LCR_SCS_VAL
Definition sio.c:32
#define CPUCLK
Definition sio.c:30