Skip to content

Commit 701ab24

Browse files
authored
Merge pull request #458 from israpps/ppc-uart
add PPC UART IRX driver
2 parents f89257a + 3221b66 commit 701ab24

File tree

8 files changed

+320
-1
lines changed

8 files changed

+320
-1
lines changed

iop/debug/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Licenced under Academic Free License version 2.0
77
# Review ps2sdk README & LICENSE files for further details.
88

9-
SUBDIRS = iopdebug iop_sbusdbg ioptrap sior
9+
SUBDIRS = iopdebug iop_sbusdbg ioptrap sior ppctty
1010

1111
include $(PS2SDKSRC)/Defs.make
1212
include $(PS2SDKSRC)/Rules.make

iop/debug/ppctty/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# _____ ___ ____ ___ ____
2+
# ____| | ____| | | |____|
3+
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
4+
#-----------------------------------------------------------------------
5+
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
6+
# Licenced under Academic Free License version 2.0
7+
# Review ps2sdk README & LICENSE files for further details.
8+
9+
IOP_INCS +=
10+
11+
IOP_OBJS = main.o tty.o imports.o
12+
13+
ifeq ($(KPRINTF), 1)
14+
IOP_CFLAGS += -DKPRINTF
15+
endif
16+
17+
ifeq ($(DEBUG), 1)
18+
IOP_CFLAGS += -DDEBUG
19+
endif
20+
21+
include $(PS2SDKSRC)/Defs.make
22+
include $(PS2SDKSRC)/iop/Rules.bin.make
23+
include $(PS2SDKSRC)/iop/Rules.make
24+
include $(PS2SDKSRC)/iop/Rules.release

iop/debug/ppctty/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# PPC TTY
2+
3+
IOP Module to redirect IOP Printf from stdout to PowerPC UART on DECKARD Models (`SCPH-75xxx` and up)
4+
5+
Coded by @asmblur

iop/debug/ppctty/src/imports.lst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
loadcore_IMPORTS_start
2+
I_FlushIcache
3+
I_FlushDcache
4+
loadcore_IMPORTS_end
5+
6+
7+
#ifdef KPRINTF
8+
sysmem_IMPORTS_start
9+
I_Kprintf
10+
I_KprintfSet
11+
sysmem_IMPORTS_end
12+
#endif
13+
14+
stdio_IMPORTS_start
15+
I_printf
16+
stdio_IMPORTS_end
17+
18+
sysclib_IMPORTS_start
19+
#ifdef KPRINTF
20+
I_prnt
21+
#endif
22+
I_memcpy
23+
sysclib_IMPORTS_end
24+
25+
ioman_IMPORTS_start
26+
I_open
27+
I_close
28+
I_AddDrv
29+
I_DelDrv
30+
ioman_IMPORTS_end
31+
32+
thsemap_IMPORTS_start
33+
I_CreateSema
34+
I_DeleteSema
35+
I_SignalSema
36+
I_WaitSema
37+
thsemap_IMPORTS_end

iop/debug/ppctty/src/irx_imports.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* irx_imports.h - Defines all IRX imports.
3+
*
4+
* See the file LICENSE included with this distribution for licensing terms.
5+
*/
6+
7+
#ifndef IOP_IRX_IMPORTS_H
8+
#define IOP_IRX_IMPORTS_H
9+
10+
#include "irx.h"
11+
12+
/* Please keep these in alphabetical order! */
13+
#include "loadcore.h"
14+
#include "stdio.h"
15+
#include "sysmem.h"
16+
#include "sysclib.h"
17+
#include "ioman.h"
18+
#include "modload.h"
19+
#include "thsemap.h"
20+
21+
#endif /* IOP_IRX_IMPORTS_H */

iop/debug/ppctty/src/main.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <tamtypes.h>
2+
#include <stdio.h>
3+
#include <loadcore.h>
4+
#include <excepman.h>
5+
#include "tty.h"
6+
7+
IRX_ID(MODNAME, 1, 0);
8+
9+
#define IS_DECKARD() \
10+
({ \
11+
u16 iop_revision; \
12+
asm("mfc0 %0, $15\n" \
13+
: "=r"(iop_revision):); \
14+
iop_revision == 0x30; \
15+
})
16+
17+
volatile uint8_t *PPC_UART_TX = (volatile uint8_t *) 0x1F80380C;
18+
19+
// send a string to PPC TTY for output.
20+
void tty_puts(const char *str)
21+
{
22+
const char *p = str;
23+
while(p && *p) *PPC_UART_TX = *(p++);
24+
}
25+
26+
int _start(int argc, char *argv[])
27+
{
28+
(void)argc;
29+
(void)argv;
30+
if (!IS_DECKARD())
31+
{
32+
printf(MODNAME ": THIS PS2 IS NOT DECKARD\n");
33+
return MODULE_NO_RESIDENT_END;
34+
}
35+
36+
if(tty_init() != 0)
37+
{
38+
printf("Failed initializing PPC TTY system!\n");
39+
return MODULE_NO_RESIDENT_END;
40+
}
41+
42+
FlushIcache();
43+
FlushDcache();
44+
45+
printf("IOP PPC TTY installed!\n");
46+
47+
return MODULE_RESIDENT_END;
48+
}

iop/debug/ppctty/src/tty.c

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
3+
IOP->PPC TTY
4+
5+
*/
6+
7+
#include <tamtypes.h>
8+
#include <stdio.h>
9+
#include <stdarg.h>
10+
#include <sysclib.h>
11+
#include <sysmem.h>
12+
#include <excepman.h>
13+
#include <intrman.h>
14+
#include <ioman.h>
15+
#include <thsemap.h>
16+
#include "tty.h"
17+
18+
#ifdef DEBUG
19+
#define DPRINTF(fmt, x...) printf(MODNAME ": " fmt, ##x)
20+
#else
21+
#define DPRINTF(x...)
22+
#endif
23+
24+
static int tty_sema = -1;
25+
26+
extern void tty_puts(const char *str);
27+
28+
static int ttyfs_error() { return -1; }
29+
30+
static int ttyfs_init()
31+
{
32+
DPRINTF("FS Init()\n");
33+
if ((tty_sema = CreateMutex(IOP_MUTEX_UNLOCKED)) < 0)
34+
{
35+
DPRINTF("Failed to create mutex\n");
36+
return -1;
37+
}
38+
return 0;
39+
}
40+
41+
static int ttyfs_deinit()
42+
{
43+
DPRINTF("FS Deinit()\n");
44+
DeleteSema(tty_sema);
45+
return 0;
46+
}
47+
48+
static int ttyfs_open(iop_file_t *file, const char *name, int flags)
49+
{
50+
(void)file;
51+
(void)name;
52+
(void)flags;
53+
54+
DPRINTF("FS Open()\n");
55+
return 0;
56+
}
57+
58+
static int ttyfs_dopen(iop_file_t *file, const char *name)
59+
{
60+
(void)file;
61+
(void)name;
62+
63+
DPRINTF("FS Dopen()\n");
64+
return 0;
65+
}
66+
67+
static int ttyfs_close(iop_file_t *file)
68+
{
69+
(void)file;
70+
71+
DPRINTF("FS Close()\n");
72+
return(0);
73+
}
74+
75+
static int ttyfs_write(iop_file_t *file, void *ptr, int size) {
76+
char temp[65];
77+
int bCount = 0;
78+
79+
(void)file;
80+
81+
DPRINTF("FS Write()\n");
82+
83+
WaitSema(tty_sema);
84+
while(bCount < size)
85+
{
86+
int toWrite;
87+
88+
toWrite = (size - bCount);
89+
if(toWrite > 64)
90+
toWrite = 64;
91+
92+
memcpy(temp, &(((u8 *)ptr)[bCount]), toWrite);
93+
temp[toWrite] = '\0';
94+
tty_puts(temp);
95+
96+
bCount += toWrite;
97+
}
98+
SignalSema(tty_sema);
99+
100+
return(bCount);
101+
}
102+
103+
static iop_device_ops_t fsd_ops =
104+
{
105+
&ttyfs_init,
106+
&ttyfs_deinit,
107+
(void *)&ttyfs_error,
108+
&ttyfs_open,
109+
&ttyfs_close,
110+
(void *)&ttyfs_error,
111+
&ttyfs_write,
112+
(void *)&ttyfs_error,
113+
(void *)&ttyfs_error,
114+
(void *)&ttyfs_error,
115+
(void *)&ttyfs_error,
116+
(void *)&ttyfs_error,
117+
&ttyfs_dopen,
118+
&ttyfs_close,
119+
(void *)&ttyfs_error,
120+
(void *)&ttyfs_error,
121+
(void *)&ttyfs_error,
122+
};
123+
124+
static iop_device_t tty_fsd =
125+
{
126+
"tty",
127+
IOP_DT_FS,
128+
1,
129+
"TTY via PPC SIO",
130+
&fsd_ops,
131+
};
132+
133+
#ifdef KPRINTF
134+
void sprintf_putchar(void *context, int c)
135+
{
136+
char **string = (char **)context;
137+
138+
if(c < 0x100) { ((*string)++)[0] = c; }
139+
else { (*string)[0] = 0; }
140+
}
141+
142+
extern int _vsprintf(char * str, const char * format, va_list ap);
143+
144+
static char kprint_buffer[1024];
145+
146+
int _kPrintf(void *context, const char * format, va_list ap)
147+
{
148+
(void)context;
149+
150+
int r = prnt(&sprintf_putchar, &format, format, ap);
151+
tty_puts(kprint_buffer);
152+
return r;
153+
}
154+
#endif
155+
156+
int tty_init(void)
157+
{
158+
DelDrv(tty_fsd.name);
159+
DelDrv("dummytty");
160+
161+
if(AddDrv(&tty_fsd) != 0) { return(-1); }
162+
163+
// open stdin
164+
close(0);
165+
open("tty:", O_RDONLY);
166+
// open stdout
167+
close(1);
168+
open("tty:", O_WRONLY);
169+
#ifdef KPRINTF
170+
printf("PPCTTY: KprintfSet\n");
171+
KprintfSet(&_kPrintf, NULL);
172+
#endif
173+
return(0);
174+
}

iop/debug/ppctty/src/tty.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef __TTY_H
2+
#define __TTY_H
3+
4+
#include "tamtypes.h"
5+
#include <stdint.h>
6+
#define MODNAME "ppctty"
7+
// Prototypes
8+
int tty_init(void);
9+
10+
#endif

0 commit comments

Comments
 (0)