std_visual.c 6.98 KB
/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1994, Silicon Graphics, Inc.               *
 *                                                                        *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright  law.  They  may not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *                                                                        *
 *************************************************************************/

/*
 * File:	std_visual.c
 * Creator:	jeffd@sgi.com
 * Create Date:	Thu Jan 27 15:29:55 PST 1994
 *
 * This file implements the curses panels for interactive display.
 *
 */

#include <curses.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <term.h>
#include <stdarg.h>
#include <stdio.h>
#include "rsp.h"
#include "rspctl.h"
#include "std_visual.h"
#include "vu.h"
#include "cop0.h"

#define	MIN_CMD_WSIZE		2
#define	MIN_REG_WSIZE		1
#define	MAX_REG_WSIZE		39
#define	MIN_OUT_WSIZE		3

static WINDOW	*cmdWindow, *regWindow, *outWindow;
static int	cmdRow, regRow, outRow;
static int	cmdNumRows, regNumRows, outNumRows, numRows, numColumns;
static char	*regLineBuf, *outLineBuf;
static int	outLineCount;
static boolean	doResize = FALSE;

boolean
rsp_VisualInit(void)
{
    struct winsize	win;
    int rv;

    initscr();

    if ((ioctl(0, TIOCGWINSZ, &win) < 0) &&
	(win.ws_row > 0) && (win.ws_col > 0)) {
	numRows = win.ws_row;
	numColumns = win.ws_col;
    } else {
	numRows = lines;
	numColumns = columns;
    }

    if (numRows < (MIN_CMD_WSIZE+MIN_REG_WSIZE+MIN_OUT_WSIZE)) {
	endwin();
	fprintf(stderr, "rsp: not enough lines for visual mode\n");
	return(FALSE);
    }

    if (((regLineBuf = (char *)malloc(numColumns)) == NULL) ||
        ((outLineBuf = (char *)malloc(3*numColumns)) == NULL)) {
	endwin();
	fprintf(stderr, "rsp: malloc failed\n");
	return(FALSE);
    }

    /*
     * Divvy up total number of lines among command,
     * register view, and output window.
     */

    cmdNumRows = MIN_CMD_WSIZE;
    if (numRows - cmdNumRows >= MAX_REG_WSIZE + MIN_OUT_WSIZE) {
	regNumRows = MAX_REG_WSIZE;
	outNumRows = numRows - cmdNumRows - regNumRows; /* plenty 'o room */
    } else {
	outNumRows = MIN_OUT_WSIZE;
	regNumRows = numRows - cmdNumRows - outNumRows;
    }

    cmdRow = 0;
    regRow = cmdRow + cmdNumRows;
    outRow = regRow + regNumRows;

    cmdWindow = newwin(cmdNumRows, numColumns, cmdRow, 0);
    regWindow = newwin(regNumRows, numColumns, regRow, 0);
    outWindow = newwin(outNumRows, numColumns, outRow, 0);

    wmove(regWindow, regNumRows-1, 0);
    whline(regWindow, '-', numColumns);
    wrefresh(regWindow);

    wmove(cmdWindow, 0, 0);
    waddstr(cmdWindow, "rsp> ");
    wmove(cmdWindow, cmdNumRows-1, 0);
    whline(cmdWindow, '-', numColumns);
    wrefresh(cmdWindow);

    scrollok(outWindow, TRUE);
    outLineCount = 0;

    return(TRUE);
}

int rsp_VisualGets(char *s, int n)
{
    int		i, j, c;
    char	*sp = s;

    wmove(cmdWindow, 0, 5);
    wclrtoeol(cmdWindow);
    wrefresh(cmdWindow);

    for (i = 0; i < n; i++) {
	if ((c = wgetch(cmdWindow)) == ERR) {
	    if (!doResize)
		break;
	    if (!rsp_VisualUpdate())
		return(-1);
	    wmove(cmdWindow, 0, 5);
	    for (j = 0; j < sp-s; j++)
		waddch(cmdWindow, s[j]);
	    wrefresh(cmdWindow);
	    continue;
	}

	if ((c == 8 || c == 127) && sp > s) {  /* Backspace or delete */
		sp--;
	} else if ((*sp++ = c)  == '\n') {
		break;
	}
    }
    if (s == sp)
	return(-1);
    *sp = '\0';
    return(0);
}

#define	outRegWindow(s)	{   if (lineCount >= regNumRows-1) { \
				wrefresh(regWindow); \
   				wmove(cmdWindow, 0, 5); \
				return(TRUE); \
			     } \
			     wmove(regWindow, lineCount++, 0); \
			     waddstr(regWindow, (s)); \
			}

boolean
rsp_VisualUpdate(void)
{
    int		i, j;
    i128	regValue;
    i32		gpregValue;
    u32		readRegValue, writeRegValue;
    int		lineCount = 0;
    int		numChars;

    if (doResize) {
	rsp_VisualCleanup();
	if (!rsp_VisualInit()) {
	    return(FALSE);
	}
	doResize = FALSE;
    }
    sprintf(regLineBuf, "VAC = 0x%012llx  VCO = 0x%04x  VCC = 0x%04x",
			rsp_ACC[0]&VU_ACC_MASK, rsp_VCO, rsp_VCC);
    outRegWindow(regLineBuf);
    for (i=1; i<8; i++) {
	sprintf(regLineBuf, "      0x%012llx",rsp_ACC[i]&VU_ACC_MASK);
	outRegWindow(regLineBuf);
    }
    outRegWindow("");

    numChars = 0;
    for (i=0; i < 32; i++) {
	numChars += sprintf(regLineBuf + numChars,
			    "%sv%d = ", (i < 10 ? " " : ""), i);
	regValue = rsp_VURGet(i);
	for (j=0; j<16; j++) {
	    numChars += sprintf(regLineBuf + numChars, "%02x%s",regValue.b[j],
				(j&rsp_VUSpacing)?" ":"" );
	}
	if ((i % 2) == 0) {
	    *(regLineBuf + numChars++) = ' ';
	} else {
	    outRegWindow(regLineBuf);
	    numChars = 0;
	}
    }
    outRegWindow("");
    sprintf(regLineBuf, "PC  = 0x%08x  CTL = 0x%08x  CLK = 0x%08x",
	    rsp_programCounter, rsp_controlReg, rsp_clock);
    outRegWindow(regLineBuf);
    sprintf(regLineBuf,
	    "DMA:  Dmem = 0x%04x  Dbus = 0x%06x  Busy = %d  Full = %d",
	    cop0_RegGet(0), cop0_RegGet(1),
	    (cop0_DmaBusy() ? 1 : 0),  (cop0_DmaFull() ? 1 : 0));
    outRegWindow(regLineBuf);
    readRegValue  = cop0_RegGet(2);
    writeRegValue = cop0_RegGet(3);
    sprintf(regLineBuf,
	    "      Read Length = (%d,%d,%d)  Write Length = (%d,%d,%d)",
	    readRegValue == -1 ? -1 :  LINESTRIDE(readRegValue),
	    readRegValue == -1 ? -1 : LINECOUNT(readRegValue),
	    readRegValue == -1 ? -1 : BYTECOUNT(readRegValue),
	    writeRegValue == -1 ? -1 :  LINESTRIDE(writeRegValue),
	    writeRegValue == -1 ? -1 : LINECOUNT(writeRegValue),
	    writeRegValue == -1 ? -1 : BYTECOUNT(writeRegValue));
    outRegWindow(regLineBuf);
    outRegWindow("");

    numChars = 0;
    for (i=0; i < 32; i++) {
	gpregValue = rsp_SuGPRGet(i);

	numChars += sprintf(regLineBuf + numChars, "%sr%d = 0x%08x    ",
			    (i < 10 ? " " : ""), i, gpregValue);

	if (((i+1) % 4) == 0) {
	    outRegWindow(regLineBuf);
	    numChars = 0;
	}
    }
    wrefresh(regWindow);
    wmove(cmdWindow, 0, 5);
    return(TRUE);
}

void
rsp_VisualClear(void)
{
    wclear(outWindow);
    wmove(outWindow, 0, 0);
}

void
rsp_VisualPrint(FILE *out, char *fmt, va_list ap)
{
    vsprintf(outLineBuf, fmt, ap);
    waddstr(outWindow, outLineBuf);
    wrefresh(outWindow);
}

void
rsp_VisualCleanup(void)
{
    delwin(cmdWindow);
    delwin(regWindow);
    delwin(outWindow);

    endwin();

    free(regLineBuf);
    free(outLineBuf);
}

int ViewFile(char *filename)
{
    fprintf(stderr, "Function not supported in non-gl mode. \n");
    fprintf(stderr, "bug Kevin to fix me \n");
    
    return 1;
}

void
rsp_VisualResize(void)
{
	doResize = TRUE;
}

void
rsp_VisualData (u32 datastart)
{
}

void
rsp_VisualSymbolTable (char *fn)
{
}

int
rsp_VisualNameInst(char *name)
{
    return -1;
}