hostio_pt.c 8.34 KB
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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/time.h>
#ifdef __sgi__
#include <bstring.h>
#endif
#include <stdlib.h>
#include <string.h>

int uhOpenGame_pt(void);
int uhCloseGame_pt(int);
int uhReadGame_pt(int hfd,void *buf,int count);
int uhWriteGame_pt(int hfd,void *buf,int count);
int uhReadRamrom_pt(int hfd,void *ramrom_adr,void *buf,int count);
int uhWriteRamrom_pt(int hfd,void *ramrom_adr,void *buf,int count);
int uhGload_pt(int hfd,char *loadfile);

int execl_pt(char *path,...);

static int n64read(int fd,void *buf,int count);
static int n64write(int fd,void *buf,int count);
static int n64_init(void);
static int n64_SetFlag(int val);
static int n64_GetFlag(void);
static int n64_sleep(int t);
static void n64_gload_wait(void);

int rfd;
int wfd;
int hfd;

#define HOST_FD 1234

/*
FIFO Packet format

	long	command
	long	data1
	long	data2
	long	size;
	buf[size]

	read mem
	 SEND
		command	1
		data1	addr
		data2	read len
		size	0
	 RECV
		command	1
		data1	0
		data2	0
		size	n byte + 4
		data	(n byte)
		long	status

	write mem
	 SEND
		command	2
		data1	addr
		data2	0
		siz	n byte
		data	n byte
	 RECV
		command	2
		data1	status
		data2	0
		size	0
*/

#define	MEMREAD		1
#define	MEMWRITE	2
#define	BLKREAD		3
#define	BLKWRITE	4
#define	PTN64CMD	5

int uhReadRamrom_pt(int hfd,void *ramrom_adr,void *buf,int count)
{
/*	return		< 0	error
			< -4	n64 target bus error
			> 0	read count
*/

	unsigned	pipe_buf[4];

	if(hfd!=HOST_FD) return -1;
	if(count==0) return 0;
	pipe_buf[0] = MEMREAD;
	pipe_buf[1] = (unsigned)ramrom_adr+0xb0000000;
	pipe_buf[2] = count;
	pipe_buf[3] = 0;
	if(n64write(wfd,pipe_buf,sizeof(pipe_buf)) < 0)
		return -1;
	if(n64read(rfd,pipe_buf,sizeof(pipe_buf)) < 0)
		return -2;
	if(n64read(rfd,buf,pipe_buf[3] - 4) < 0)
		return -3;
	if(n64read(rfd,pipe_buf,4) < 0)
		return -4;
	if(pipe_buf[0])
		return (-4 - pipe_buf[0]);
	return (pipe_buf[3] - 4);		/* read count */
}

int uhWriteRamrom_pt(int hfd,void *ramrom_adr,void *buf,int count)
{
/*	return		< 0	error
			< -4	n64 target bus error
			> 0	write count
*/

	unsigned	pipe_buf[4];

	if(hfd!=HOST_FD) return -1;
	if(count==0) return 0;
	pipe_buf[0] = MEMWRITE;
	pipe_buf[1] = (unsigned)ramrom_adr+0xb0000000;
	pipe_buf[2] = 0;
	pipe_buf[3] = count;
	if(n64write(wfd,pipe_buf,sizeof(pipe_buf)) < 0)
		return -1;
	if(n64write(wfd,buf,count) < 0)
		return -2;
	if(n64read(rfd,pipe_buf,sizeof(pipe_buf)) < 0)
		return -3;
	if(pipe_buf[1])
		return (-4 - pipe_buf[1]);
	return count;
}

int uhReadGame_pt(int hfd,void *buf,int count)
{
/*	return		< 0	error
			< -4	n64 target bus error
			> 0	read count
*/

	unsigned	pipe_buf[4];

	if(hfd!=HOST_FD) return -1;
	if(count==0) return 0;
	n64_gload_wait();
	pipe_buf[0] = BLKREAD | 0x80000000;
	pipe_buf[1] = count;
	pipe_buf[2] = 0;
	pipe_buf[3] = 0;
	if(n64write(wfd,pipe_buf,sizeof(pipe_buf)) < 0)
		return -1;
	if(n64read(rfd,pipe_buf,sizeof(pipe_buf)) < 0)
		return -2;
	if(n64read(rfd,buf,pipe_buf[3] - 4) < 0)
		return -3;
	if(n64read(rfd,pipe_buf,4) < 0)
		return -4;
	if(pipe_buf[0])
		return (-4 - pipe_buf[0]);
	return (pipe_buf[3] - 4);		/* read count */
}

int uhWriteGame_pt(int hfd,void *buf,int count)
{
/*	return		< 0	error
			< -4	n64 target bus error
			> 0	write count
*/

	unsigned	pipe_buf[4];

	if(hfd!=HOST_FD) return -1;
	if(count==0) return 0;
	n64_gload_wait();
	pipe_buf[0] = BLKWRITE | 0x80000000;
	pipe_buf[1] = 0;
	pipe_buf[2] = 0;
	pipe_buf[3] = count;
	if(n64write(wfd,pipe_buf,sizeof(pipe_buf)) < 0)
		return -1;
	if(n64write(wfd,buf,count) < 0)
		return -2;
	if(n64read(rfd,pipe_buf,sizeof(pipe_buf)) < 0)
		return -3;
	if(pipe_buf[1])
		return (-4 - pipe_buf[1]);
	return count;
}

int uhPartnerCmd_pt(int hfd,void *buf)
{
/*	return		< 0	error
			< -4	n64 target bus error
			> 0	write count
*/
	int count;
	unsigned pipe_buf[4];

	if(hfd!=HOST_FD) return -1;
	count=strlen(buf);
	if(count>=512) count=512-1;
	pipe_buf[0] = PTN64CMD;
	pipe_buf[1] = 0;
	pipe_buf[2] = 0;
	pipe_buf[3] = count;
	if(n64write(wfd,pipe_buf,sizeof(pipe_buf)) < 0)
		return -1;
	if(n64write(wfd,buf,count) < 0)
		return -2;
	if(n64read(rfd,pipe_buf,sizeof(pipe_buf)) < 0)
		return -3;
	if(pipe_buf[1])
		return (-4 - pipe_buf[1]);
	return count;
}

static int n64read(int fd,void *buf,int count)
{
	int	rc;
	int	count_bak = count;

	while(count != 0){
		if((rc = read(fd,buf,count)) < 0)
			return rc;
		buf = (char *)buf + rc;
		count -= rc;
	}
	return count_bak;
}

static int n64write(int fd,void *buf,int count)
{
	int	rc;
	int count_bak = count;

	while(count != 0){
		if((rc = write(fd,buf,count)) < 0)
			return rc;
		buf = (char *)buf + rc;
		count -= rc;
	}
	return count_bak;
}


static int	shmid;
static int	*shmptr;
static char	commemname[] = "/tmp/ptn64_c";
/*
	*(shmptr)	status
	*(shmptr+1)	not use
	*(shmptr+2)	not use
	*(shmptr+3)	cancel
*/


static int n64_init(void)
{
	int	i;

	if(*(shmptr+3)) return -1;
	*(shmptr+3) = 1;
	for(i = 0 ; i < 20 ; i++){
		if(*(shmptr+3) == 0) return 0;
		n64_sleep(100);		/* 100 mS sleep */
	}
	return -2;			/* 2 Sec wait error */
}

static int n64_SetFlag(int val)
{
	*shmptr = val;
	return 0;
}

static int n64_GetFlag(void)
{
	n64_sleep(100);			/* 100mS wait */
	return *shmptr;
}

static int n64_sleep(int t)		/* t mS wait */
{
	struct timeval timed;

	timed.tv_sec = 0;
	timed.tv_usec = 1000*t;
	select(0,(fd_set *)NULL,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)&timed);
	return 0;
}

static void n64_gload_wait(void)
{
    static int gload_ok=0;

    if(gload_ok==0){
	while(n64_GetFlag()!=0){
	    printf("\n***** WAIT ******\n");
	}
	gload_ok=1;
    }
}

int uhOpenGame_pt(void)
{
	int	rc;
	char	buf[256];
	struct	timeval timed;
	char	svrname[] = "/tmp/ptn64";
	char	recvname[] = "/tmp/ptn64_s";
	int	shmfd;

	if((wfd = open(svrname,O_WRONLY)) < 0)
		return -1;
	if((rfd = open(recvname,O_RDONLY)) < 0)
		return -2;
	if((rc = fcntl(rfd,F_SETFL,O_RDONLY | O_NDELAY)) < 0)
		return -4;

	do{
	    rc = 1;
	    while(rc){
		if((rc = read(rfd,buf,256)) < 0)
		return -3;
	    }

	    timed.tv_sec = 0;			/* wait 100 uS */
	    timed.tv_usec = 100;
	    select(1,(fd_set * )NULL,(fd_set *)NULL,
		(fd_set *)NULL,(struct timeval *)&timed);

	}while(read(rfd,buf,256) != 0);
	if((rc = fcntl(rfd,F_SETFL,O_RDONLY)) < 0)
		return -5;

	if((shmfd = open(commemname,O_RDONLY)) < 0){
	    printf("[%s] open error !!(Eno = %d)",commemname,errno);
	    return -6;
	}
	if(read(shmfd,buf,256) < 0){
	    printf("[%s] read error !!(Eno = %d)",commemname,errno);
	    return -7;
	}
	shmid = atoi(buf);
	if((shmptr = shmat(shmid,0,0)) == (void *)-1){
	    printf("shmptr error !!(Eno = %d)",errno);
	    return -8;
	}
/*	printf("\nshm ID = %d addr = %08x",shmid,shmptr); */
	n64_init();
	hfd=HOST_FD;
	return HOST_FD;
}


int uhCloseGame_pt(int hfd)
{
	if(hfd!=HOST_FD) return -1;
	hfd=0;
	close(rfd);
	close(wfd);
	return 0;
}

int uhGload_pt(int hfd,char *loadfile)
{
    int len;
    char *p,*q,*loadname;
    char cmd[512];
    struct stat file_stat;

    if(hfd!=HOST_FD) return -1;
    strcpy(cmd,"rd ");
    len=strlen(cmd);
    loadname=cmd+len;
    getcwd(loadname,sizeof(cmd)-len);
    if(loadfile==0){
	p="rom";
    }
    else{
	p=loadfile;
    }
    if(*p!='/' && *p!='~'){
	q=cmd+strlen(cmd);
	if(*(q-1)!='/'){
	    *(q++)='/';
	}
	strcpy(q,p);
    }
    else{
	strcpy(loadname,p);
    }
    if(stat(loadname,&file_stat)!=0){
	return -1;
    }
/*
    PARTNER-N64 Command
	rd loadfile,0xb0000000
	reset
	g
*/
    strcat(cmd,",0xb0000000\nreset\ng\n");
    uhPartnerCmd_pt(hfd,cmd);
    n64_SetFlag(0);
    return 0;
}

#include <stdarg.h>

int execl_pt(char *path,...)
{
    va_list ap;
    int len;
    char *p,*q,*loadname;
    char cmd[512];
    struct stat file_stat;

    va_start(ap,path);
    if(strcmp(path,"/usr/sbin/gload")==0){
	p=va_arg(ap,char *);
	p=va_arg(ap,char *);
	strcpy(cmd,"rd ");
	len=strlen(cmd);
	loadname=cmd+len;
	getcwd(loadname,sizeof(cmd)-len);
	if(p==0){
	    p="rom";
	}
	if(*p!='/' && *p!='~'){
	    q=cmd+strlen(cmd);
	    if(*(q-1)!='/'){
		*(q++)='/';
	    }
	    strcpy(q,p);
	}
	else{
	     strcpy(loadname,p);
	}
        if(stat(loadname,&file_stat)!=0){
	    return -1;
	}
/*
    PARTNER-N64 Command
	rd loadfile,0xb0000000
	reset
	g
*/
	strcat(cmd,",0xb0000000\nreset\n\g\n");
	uhPartnerCmd_pt(hfd,cmd);
	n64_SetFlag(0);
	for(;;){
	    n64_sleep(100);			/* 100mS wait */
	}
/*	exit(0); */
	return 0;
    }
    else{
	return execv(path,(char **)ap);
     }
}