aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/psxsio/sio.c
blob: b4871487bc75d34466d394bfcd444e1b835d29e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * PSn00bSDK buffered serial port driver
 * (C) 2022 spicyjpeg - MPL licensed
 *
 * TODO: add proper support for DTR/DSR flow control
 */

#include <stdint.h>
#include <assert.h>
#include <psxetc.h>
#include <psxapi.h>
#include <psxsio.h>
#include <hwregs_c.h>

#define BUFFER_LENGTH		128
#define SIO_SYNC_TIMEOUT	0x100000

/* Private types */

typedef struct {
	uint8_t data[BUFFER_LENGTH];
	uint8_t head, tail, length;
} RingBuffer;

/* Internal globals */

static SIO_FlowControl _flow_control;
static uint16_t _ctrl_reg_flag;

static int  (*_read_callback)(uint8_t) = (void *) 0;
static void (*_old_sio_handler)(void)  = (void *) 0;

static volatile RingBuffer _tx_buffer, _rx_buffer;

/* Private interrupt handler */

static void _sio_handler(void) {
	// Handle any incoming bytes.
	while (SIO_STAT(1) & SR_RXRDY) {
		uint8_t value  = SIO_DATA(1);

		// Skip storing this byte into the RX buffer if the callback returns a
		// non-zero value.
		if (_read_callback) {
			if (_read_callback(value))
				continue;
		}

		int length = _rx_buffer.length;

		if (length >= BUFFER_LENGTH) {
			//_sdk_log("RX overrun, dropping bytes\n");
			break;
		}

		int tail          = _rx_buffer.tail;
		_rx_buffer.tail   = (tail + 1) % BUFFER_LENGTH;
		_rx_buffer.length = length + 1;

		_rx_buffer.data[tail] = value;
	}

	// Send the next byte in the buffer if the TX unit is ready. Note that
	// checking for CTS is unnecessary as the serial port is already hardwired
	// to do so.
	if (SIO_STAT(1) & (SR_TXRDY | SR_TXU)) {
		int length = _tx_buffer.length;

		if (length) {
			int head          = _tx_buffer.head;
			_tx_buffer.head   = (head + 1) % BUFFER_LENGTH;
			_tx_buffer.length = length - 1;

			SIO_CTRL(1) |= CR_TXIEN;
			SIO_DATA(1)  = _tx_buffer.data[head];
		} else {
			SIO_CTRL(1) &= CR_TXIEN ^ 0xffff;
		}
	}

	// Acknowledge the IRQ and update flow control signals.
	if (_rx_buffer.length < BUFFER_LENGTH)
		SIO_CTRL(1) = CR_INTRST | (SIO_CTRL(1) | _ctrl_reg_flag);
	else
		SIO_CTRL(1) = CR_INTRST | (SIO_CTRL(1) & (_ctrl_reg_flag ^ 0xffff));
}

/* Serial port initialization API */

void SIO_Init(int baud, uint16_t mode) {
	int _exit        = EnterCriticalSection();
	_old_sio_handler = InterruptCallback(IRQ_SIO1, &_sio_handler);

	SIO_CTRL(1) = CR_ERRRST;
	SIO_MODE(1) = (mode & 0xfffc) | MR_BR_16;
	SIO_BAUD(1) = (uint16_t) ((int) 0x1fa400 / baud);
	SIO_CTRL(1) = CR_TXEN | CR_RXEN | CR_RXIEN;

	_tx_buffer.head   = 0;
	_tx_buffer.tail   = 0;
	_tx_buffer.length = 0;
	_rx_buffer.head   = 0;
	_rx_buffer.tail   = 0;
	_rx_buffer.length = 0;

	_flow_control  = SIO_FC_NONE;
	_ctrl_reg_flag = 0;

	if (_exit)
		ExitCriticalSection();
}

void SIO_Quit(void) {
	int _exit = EnterCriticalSection();

	InterruptCallback(IRQ_SIO1, _old_sio_handler);
	SIO_CTRL(1) = CR_ERRRST;

	if (_exit)
		ExitCriticalSection();
}

void SIO_SetFlowControl(SIO_FlowControl mode) {
	FastEnterCriticalSection();

	switch (mode) {
		case SIO_FC_NONE:
			_flow_control  = SIO_FC_NONE;
			_ctrl_reg_flag = 0;

			SIO_CTRL(1) &= 0xffff ^ CR_DSRIEN;
			break;

		case SIO_FC_RTS_CTS:
			_flow_control  = SIO_FC_RTS_CTS;
			_ctrl_reg_flag = CR_RTS;

			SIO_CTRL(1) &= 0xffff ^ CR_DSRIEN;
			break;

		/*case SIO_FC_DTR_DSR:
			_flow_control  = SIO_FC_DTR_DSR;
			_ctrl_reg_flag = CR_DTR;

			SIO_CTRL(1) |= CR_DSRIEN;
			break;*/
	}

	FastExitCriticalSection();
}

/* Reading API */

int SIO_ReadByte(void) {
	/*for (int i = SIO_SYNC_TIMEOUT; i; i--) {
		if (_rx_buffer.length)
			return SIO_ReadByte2();
	}*/
	while (!_rx_buffer.length)
		__asm__ volatile("");

	return SIO_ReadByte2();
}

int SIO_ReadByte2(void) {
	if (!_rx_buffer.length)
		return -1;

	FastEnterCriticalSection();

	int head        = _rx_buffer.head;
	_rx_buffer.head = (head + 1) % BUFFER_LENGTH;
	_rx_buffer.length--;

	FastExitCriticalSection();
	return _rx_buffer.data[head];
}

int SIO_ReadSync(int mode) {
	if (mode)
		return _rx_buffer.length;

	/*for (int i = SIO_SYNC_TIMEOUT; i; i--) {
		if (_rx_buffer.length)
			return 0;
	}*/
	while (!_rx_buffer.length)
		__asm__ volatile("");

	return 0;
}

void *SIO_ReadCallback(int (*func)(uint8_t)) {
	FastEnterCriticalSection();

	void *old_callback  = _read_callback;
	_read_callback      = func;

	FastExitCriticalSection();
}

/* Writing API */

int SIO_WriteByte(uint8_t value) {
	for (int i = SIO_SYNC_TIMEOUT; i; i--) {
		if (_tx_buffer.length < BUFFER_LENGTH)
			return SIO_WriteByte2(value);
	}

	//_sdk_log("SIO_WriteByte() timeout\n");
	return -1;
}

int SIO_WriteByte2(uint8_t value) {
	// If the TX unit is currently busy, append the byte to the buffer instead
	// of sending it immediately. Note that interrupts must be disabled *prior*
	// to checking if TX is busy; disabling them afterwards would create a race
	// condition where the transfer could end while interrupts are being
	// disabled. Interrupts are disabled through the IRQ_MASK register rather
	// than via syscalls for performance reasons.
	FastEnterCriticalSection();

	if (SIO_STAT(1) & (SR_TXRDY | SR_TXU)) {
		SIO_DATA(1) = value;
		FastExitCriticalSection();
		return 0;
	}

	int length = _tx_buffer.length;

	if (length >= BUFFER_LENGTH) {
		FastExitCriticalSection();

		//_sdk_log("TX overrun, dropping bytes\n");
		return -1;
	}

	int tail          = _tx_buffer.tail;
	_tx_buffer.tail   = (tail + 1) % BUFFER_LENGTH;
	_tx_buffer.length = length + 1;

	_tx_buffer.data[tail] = value;
	SIO_CTRL(1) |= CR_TXIEN;

	FastExitCriticalSection();
	return length;
}

int SIO_WriteSync(int mode) {
	if (mode)
		return _tx_buffer.length;

	// Wait for the buffer to become empty.
	for (int i = SIO_SYNC_TIMEOUT; i; i--) {
		if (!_tx_buffer.length)
			break;
	}

	if (!_tx_buffer.length) {
		// Wait for the TX unit to finish sending the last byte.
		while (!(SIO_STAT(1) & (SR_TXRDY | SR_TXU)))
			__asm__ volatile("");
	} else {
		//_sdk_log("SIO_WriteSync() timeout\n");
	}

	return _tx_buffer.length;
}