|
| 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 | +} |
0 commit comments