client.c 5.92 KB

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

#include <PR/rsp_ipc.h>

#include "rsp.h"

static char		*targv[] = {"rsp", "-S", "000000", 0};

/*
 */
static void
spawn_rsp(int memkey)
{
    pid_t	pid;

    pid = fork();

    sprintf(targv[2], "%d", memkey);

    switch (pid) {
      case -1:
	fprintf(stderr,"fork() failed %x.\n", errno);
	return;

      case 0:
	execvp(targv[0], targv);
	/* NOTREACHED */
	fprintf(stderr,"exec() failed. (errno = %d)\n",errno);
	exit(1);
	
      default:
	/* fork() sucessful, return */
	(void)waitpid(-1, 0, WNOHANG);	/* reaps one child process, if avail */
	break;
    }
}

/*
 *
 * Mon Feb  6 13:19:25 PST 1995
 * 
 */
int
main(int argc, char *argv[])
{
    int		i, j, shmid, docont = 1, semid;
    rsp_shm_buf_t	*bufptr;
    char	*lp, line[80], cmdstring[80], cmd[32], regsym[32], *s;
    unsigned long int	word, addr, len;
    int		memkey, semkey;

    while ((argc > 1) && (argv[1][0] == '-')) {
	switch(argv[1][1]) {
	    
	  default:
	    fprintf(stderr,"option [%s] not recognized.\n", argv[1]);
	    break;

	}
	argc--;
	argv++;
    }

    memkey = getpid();
    semkey = memkey + 10;

    /*
     * Create the shared memory segment. The server 'owns' the
     * memory, and is responsible for creating it.
     *
     */
    if ((shmid = shmget(memkey, sizeof(rsp_shm_buf_t), 
			(IPC_CREAT | SHM_R | SHM_W))) < 0) {
	fprintf(stderr,"%s : can't create shared memory.\n", "rsp");
	exit(-1);
    }
    
    /*
     * Attach to the shared memory. This returns a pointer we
     * can use in our program to access it.
     */
    if ((bufptr = (rsp_shm_buf_t *) shmat(shmid, (char *) 0x0, 0)) ==
	(rsp_shm_buf_t *) -1) {
	fprintf(stderr,"%s : can't attach shared memory.\n", argv[0]);
	exit(-1);
    }

    /*
     * create a semaphore.
     */
    if ((semid = sem_create(semkey, 1)) < 0) {
	fprintf(stderr,"%s : can't create semaphore.\n", argv[0]);
	exit(-1);
    }

    /*
     * fork of rsp:
     */
    spawn_rsp(memkey);

#if 0
    sem_wait(semid);
    strcpy(bufptr->string,"reg\n");
    bufptr->cmd = CMD_CMD;
    sem_signal(semid);

    while (bufptr->cmd != CMD_NULL)
	;

    sem_wait(semid);
    strcpy(bufptr->string,"reg\n");
    bufptr->cmd = CMD_CMD;
    sem_signal(semid);
#endif

    while (docont) {

	fprintf(stdout,"> ");
	fflush(stdout);
	if (fgets(line,80,stdin) == NULL) {
	    strcpy(cmdstring, "quit");
	    bufptr->cmd = CMD_QUIT;
	    docont = 0;
	} else {
	    sscanf(line,"%s",cmdstring);
	}
	
	sem_wait(semid);
	if (cmdstring[0] == 'q') {
	    bufptr->cmd = CMD_QUIT;
	    docont = 0;
	    fprintf(stdout,"client: received quit command...");
	} else if (cmdstring[0] == 'c') {
	    lp = &(line[0]);
	    while (*lp != ' ' && *lp != '\0')
		lp++;
	    if (*lp == '\0') {
		*lp = '\n';
		*(lp+1) = '\0';
	    } else {
		lp++;
	    }
	    
	    fprintf(stdout,"cmd = [%s]\n",lp);
	    fflush(stdout);
	    strcpy(bufptr->string,lp);
	    bufptr->cmd = CMD_CMD;
	} else if (cmdstring[0] == 'r') {	/* read memory */

	    sscanf(line,"%s %x %d",cmdstring,&addr,&len);
	    bufptr->addr = addr;
	    bufptr->length = len;
	    bufptr->cmd = CMD_DMA_READ;

	    sem_signal(semid);

	    sleep(1);

	    sem_wait(semid);
	    bcopy(&(bufptr->buff[0]), &word, len);

	} else if (cmdstring[0] == 'w') {	/* write memory */

	    word = 0xdeadbeef;
	    bcopy(&word, &(bufptr->buff[0]), 4);

	    sscanf(line,"%s %x %d",cmdstring,&addr,&len);
	    bufptr->addr = addr;
	    bufptr->length = len;
	    bufptr->cmd = CMD_DMA_WRITE;

	} else if (cmdstring[0] == 'd') {	/* deposit register */

#if 0
	    sscanf(line,"d %s %i", regsym, &word);

	    fprintf(stderr,"regsym = [%s] word = %08x\n",
		    regsym, word);

	    bcopy(&word, &(bufptr->buff[0]), 4);
	    
	    /* translate symbolic registers: */
	    if (strcasecmp(regsym,"pc")==0) {
		bufptr->reg = 32;
	    } else if (strcasecmp(regsym,"ctl")==0) {
		bufptr->reg = 33;
	    } else if (strcasecmp(regsym,"clk")==0) {
		bufptr->reg = 34;
	    } else if (regsym[0] == '$') {
		if (regsym[1] == 'v') {
		    s = &(regsym[2]);		/* vector register */
		    bufptr->reg = atoi(s);
		    bufptr->reg += 64;
		} else if (regsym[1] == 'c') {
		    s = &(regsym[2]);		/* cop0 register */
		    bufptr->reg = atoi(s);
		    bufptr->reg += 128;
		} else {
		    s = &(regsym[1]);		/* scalar register */
		    bufptr->reg = atoi(s);
		}
	    }

	    fprintf(stderr,"deposit: %d with %08x\n",
		    bufptr->reg, bufptr->buff[0]);
	    bufptr->cmd = CMD_DEPOSIT;
#endif
	} else if (cmdstring[0] == 'e') {	/* examine register */

#if 0
	    sscanf(line,"e %s", regsym);

	    fprintf(stderr,"examine: register %s\n",regsym);

	    /* translate symbolic registers: */
	    if (strcasecmp(regsym,"pc")==0) {
		bufptr->reg = 32;
	    } else if (strcasecmp(regsym,"ctl")==0) {
		bufptr->reg = 33;
	    } else if (strcasecmp(regsym,"clk")==0) {
		bufptr->reg = 34;
	    } else if (regsym[0] == '$') {
		if (regsym[1] == 'v') {
		    s = &(regsym[2]);		/* vector register */
		    bufptr->reg = atoi(s);
		    bufptr->reg += 64;
		} else if (regsym[1] == 'c') {
		    s = &(regsym[2]);		/* cop0 register */
		    bufptr->reg = atoi(s);
		    bufptr->reg += 128;
		} else {
		    s = &(regsym[1]);		/* scalar register */
		    bufptr->reg = atoi(s);
		}
	    }

	    bufptr->cmd = CMD_REG;

	    sem_signal(semid);

	    sleep(1);

	    sem_wait(semid);
	    bcopy(&(bufptr->buff[0]), &word, 4);
	    fprintf(stderr,"REG : %08x\n",word);
#endif

	} else {
	    fprintf(stdout,"client : unknown command [%s].\n",cmdstring);
	}

	sem_signal(semid);
    }

#if 0
    if (fgets(line,80,stdin) == NULL) {
	;
    }
    
    sem_wait(semid);
    bufptr->cmd = CMD_QUIT;
    sem_signal(semid);
#endif

    /*
     * Detach from the shared memory segment.
     */
    shmdt(bufptr);

    fprintf(stdout,"[client] exiting.\n");

    sleep(1);

/*
    sem_close(semid);
*/
    sem_rm(semid);
}