PS2SDK
PS2 Homebrew Libraries
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
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#ifdef F_sio_init
35void sio_init(u32 baudrate, u8 lcr_ueps, u8 lcr_upen, u8 lcr_usbl, u8 lcr_umode)
36{
37 u32 brd; /* Baud rate divisor. */
38 u8 bclk = 0; /* Baud rate generator clock. */
39
40 _sw(LCR_SCS_VAL | ((lcr_ueps & 1) << 4) | ((lcr_upen & 1) << 3) |
41 ((lcr_usbl & 1) << 2) | (lcr_umode & 1),
42 SIO_LCR);
43
44 /* Disable all interrupts. */
45 _sw(0, SIO_IER);
46
47 /* Reset the FIFOs. */
49 /* Enable them. */
50 _sw(0, SIO_FCR);
51
52 brd = CPUCLK / (baudrate * 256);
53
54 while ((brd >= 256) && (++bclk < 4))
55 brd /= 4;
56
57 _sw((bclk << 8) | brd, SIO_BGR);
58}
59#endif
60
61#ifdef F_sio_putc
62static u8 ___last_sio_putc = 0;
63int sio_putc(int c)
64{
65 if ((c == '\n') && (___last_sio_putc != '\r')) {
66 // hack: if the character to be outputted is a '\n'
67 // and the previously-outputted character is not a '\r',
68 // output a '\r' first.
69 sio_putc('\r');
70 }
71
72 /* Block until we're ready to transmit. */
73 while ((_lw(SIO_ISR) & 0xf000) == 0x8000)
74 ;
75
76 _sb(c, SIO_TXFIFO);
77 ___last_sio_putc = c;
78 return c;
79}
80#endif
81
82#ifdef F_sio_getc
83int sio_getc()
84{
85 /* Do we have something in the RX FIFO? */
86 if (_lw(SIO_ISR) & 0xf00) {
87 u8 b = _lb(SIO_RXFIFO);
88 _sw(7, SIO_ISR);
89 return b;
90 }
91
92 /* Return EOF. */
93 return -1;
94}
95#endif
96
97#ifdef F_sio_getc_block
99{
100 /* Do we have something in the RX FIFO? */
101 while (!(_lw(SIO_ISR) & 0xf00))
102 ;
103 return sio_getc();
104}
105#endif
106
107#ifdef F_sio_write
108size_t sio_write(void *buf, size_t size)
109{
110 u8 *p = (u8 *)buf;
111 size_t i;
112
113 for (i = 0; i < size; i++)
114 sio_putc(p[i]);
115
116 return size;
117}
118#endif
119
120#ifdef F_sio_read
121size_t sio_read(void *buf, size_t size)
122{
123 u8 *p = (u8 *)buf;
124 size_t i;
125
126 for (i = 0; i < size; i++) {
127 int c;
128
129 if ((c = sio_getc()) == -1)
130 break;
131
132 p[i] = (u8)c;
133 }
134
135 return i;
136}
137#endif
138
139#ifdef F_sio_puts
140int sio_puts(const char *str)
141{
142 int res;
143
144 res = sio_putsn(str);
145
146 sio_putc('\r');
147 sio_putc('\n');
148 return res + 2;
149}
150#endif
151
152#ifdef F_sio_putsn
153int sio_putsn(const char *str)
154{
155 int res;
156
157 for (res = 0; *str; res++, str++)
158 sio_putc(*str);
159
160 return res;
161}
162#endif
163
164#ifdef F_sio_gets
165char *sio_gets(char *str)
166{
167 char *s = str;
168
169 while (1) {
170 int c;
171
172 c = sio_getc_block();
173 /* Check for newline. */
174 if (c == '\n' || c == '\r')
175 break;
176
177 *s++ = c;
178 }
179
180 *s = '\0';
181 return str;
182}
183#endif
184
185#ifdef F_sio_flush
186void sio_flush()
187{
188 while (_lw(SIO_ISR) & 0xf00)
189 _lb(SIO_RXFIFO);
190}
191#endif
#define LCR_SCS_VAL
Definition sio.c:32
#define CPUCLK
Definition sio.c:30
#define SIO_LCR
Definition sio.h:29
#define SIO_BGR
Definition sio.h:77
#define SIO_IER
Definition sio.h:55
void sio_flush(void)
Definition sior.c:89
#define SIO_FCR_RFRST
Definition sio.h:72
#define SIO_FCR_TFRST
Definition sio.h:74
int sio_getc_block(void)
Definition sior.c:52
#define SIO_ISR
Definition sio.h:62
#define SIO_RXFIFO
Definition sio.h:82
#define SIO_FCR_FRSTE
Definition sio.h:70
#define SIO_TXFIFO
Definition sio.h:80
size_t sio_read(void *buf, size_t size)
char * sio_gets(char *str)
Definition sior.c:83
#define SIO_FCR
Definition sio.h:68
size_t sio_write(void *buf, size_t size)