aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/psxsio/siocons.c
blob: cd99d68f1a702ae724ec1e06f5a0b3fc675554e5 (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
#include <stdio.h>
#include <psxapi.h>
#include <psxgpu.h>
#include <psxsio.h>

static void _sio_init() {
}

static int _sio_open(FCB *fcb, const char* file, int mode) {
	
	fcb->diskid = 1;
	
	return 0;
	
}

static int _sio_inout(FCB *fcb, int cmd) {
	
	int i;
	
	if(cmd == 2) {	// Write
		
		for(i=0; i<fcb->trns_len; i++) {
			while(!(_sio_control(0, 0, 0) & SR_TXU));
			_sio_control(1, 4, ((char*)fcb->trns_addr)[i]);
		}
		
		return fcb->trns_len;
		
	} else if (cmd == 1) { // Read
	
		for(i=0; i<fcb->trns_len; i++) {
			while(!(_sio_control(0, 0, 0) & SR_RXRDY));
			((char*)fcb->trns_addr)[i] = _sio_control(0, 4, 0);
		}
		
		return fcb->trns_len;
		
	}
	
	return 0;
	
}

static int _sio_close(int h) {
	
	return h;
	
}

static DCB _sio_dcb = {
	"tty",
	0x3,
	0x1,
	0x0,
	(void*)_sio_init,	// init
	(void*)_sio_open,	// open
	(void*)_sio_inout,	// inout
	_sio_close,			// close
	NULL,				// ioctl
	NULL,				// read
	NULL,				// write
	NULL,				// erase
	NULL,				// undelete
	NULL,				// firstfile
	NULL,				// nextfile
	NULL,				// format
	NULL,				// chdir
	NULL,				// rename
	NULL,				// remove
	NULL				// testdevice
};


volatile void (*_sio_callback)(void) = NULL;

extern void _sio_irq_handler(void);


void AddSIO(int baud) {
	
	_sio_control(2, 0, 0);
	_sio_control(1, 2, MR_SB_01|MR_CHLEN_8|0x02);
	_sio_control(1, 3, baud);
	_sio_control(1, 1, CR_RXEN|CR_TXEN);
	
	close(0);
	close(1);
	
	DelDev(_sio_dcb.name);
	AddDev(&_sio_dcb);
	
	open(_sio_dcb.name, 2);
	open(_sio_dcb.name, 1);
	
}

void DelSIO(void) {
	
	// Reset serial interface
	_sio_control(2, 0, 0);
	
	// Remove TTY device
	DelDev(_sio_dcb.name);
	
}

void WaitSIO(void) {
	
	while((_sio_control(0, 0, 0)&(SR_RXRDY)) != (SR_RXRDY));
	_sio_control(0, 4, NULL);
	
}

void *Sio1Callback(void (*func)()) {
	
	void *old_isr; //= *((int*)&_sio_callback);
	
	EnterCriticalSection();
	
	if( func ) { 
		
		old_isr = InterruptCallback(8, func);
		//_sio_callback = func;
		
	} else {
		
		old_isr = InterruptCallback(8, NULL);
		//_sio_callback = NULL;
		
	}
	
	ExitCriticalSection();
	
	return old_isr;
	
}