shithub: drawcpu

ref: b35328f248cab12ae0760c237d1544842200510c
dir: /main.c/

View raw version
#include "u.h"
#include "lib.h"
#include "kern/dat.h"
#include "kern/fns.h"
#include "user.h"
#include "drawcpu.h"
#include "arg.h"
#include "proc.h"

char *argv0;
int debug;

void
sizebug(void)
{
	/*
	 * Needed by various parts of the code.
	 * This is a huge bug.
	 */
	assert(sizeof(char)==1);
	assert(sizeof(short)==2);
	assert(sizeof(ushort)==2);
	assert(sizeof(int)==4);
	assert(sizeof(uint)==4);
	assert(sizeof(long)==4);
	assert(sizeof(ulong)==4);
	assert(sizeof(vlong)==8);
	assert(sizeof(uvlong)==8);
}

// TODO: remove libgui, or at least revamp as cpubody goes away
void cpubody(void) {}

char*
estrdup(char *s)
{
	s = strdup(s);
	if(s == nil)
		sysfatal("out of memory");
    return s;
}


static void
usage(void)
{
	fprintf(stderr, "usage: drawcpu [-p 9bin]\n");
	exit(1);
}

void
notehandler(void *d, char *note)
{
	USED(d);
	if(strncmp(note, "sys:", 4) == 0)
		return;
	
	if(strncmp(note, "emu:", 4) == 0)
		exits(note);

	addnote(note);
	return;
}

void
dump(void)
{
	print("R00 %.8ux\tR01 %.8ux\tR02 %.8ux\tR03 %.8ux\tpid: %d\nR04 %.8ux\tR05 %.8ux\tR06 %.8ux\tR07 %.8ux\nR08 %.8ux\tR09 %.8ux\tR10 %.8ux\tR11 %.8ux\nR12 %.8ux\tR13 %.8ux\tR14 %.8ux\tR15 %.8ux\n", 
		up->R[0], up->R[1], up->R[2], up->R[3], up->pid,
		up->R[4], up->R[5], up->R[6], up->R[7], 
		up->R[8], up->R[9], up->R[10], up->R[11], 
		up->R[12], up->R[13], up->R[14], up->R[15]
	);
}

int
main(int argc, char **argv)
{
	extern ulong kerndate;
	char *path, *file, *rc;

	debug = 0;
	path = nil;
	kerndate = seconds();
	eve = getuser();
	if(eve == nil)
		eve = "drawcpu";

	ARGBEGIN {
	case 'r':
		rc = EARGF(usage());
		break;
	case 'p':
		path = EARGF(usage());
		break;
	case 'd':
		debug++;
		break;
	default:
		usage();
	} ARGEND;

	sizebug();
	osinit();
	procinit0();
	printinit();

	chandevreset();
	chandevinit();
	quotefmtinstall();

	if(bind("#c", "/dev", MBEFORE) < 0)
		panic("bind #c: %r");
	if(bind("#e", "/env", MREPL|MCREATE) < 0)
		panic("bind #e: %r");
	if(bind("#I", "/net", MBEFORE) < 0)
		panic("bind #I: %r");
	if(bind("#U", "/root", MREPL|MCREATE) < 0)
		panic("bind #U: %r");
	//if(bind("#P", "/proc", MBEFORE) < 0)
	//	panic("bind #P: %r");
	if(bind("#L", "/fd", MAFTER) < 0)
		panic("bind #L");

	bind("#A", "/dev", MAFTER);
	bind("#N", "/dev", MAFTER);
	bind("#C", "/", MAFTER);

	if(bind("/root", "/", MAFTER) < 0)
		panic("bind /root: %r");

	/* TODO: /path/to/root, root has /rc and /arm 
	 * Bind in /arm/bin, /rc/bin/ and /rc/lib
	 */
	if(rc != nil)
		bind(rc, "/rc", MAFTER);

	if(path != nil)
		bind(path, "/bin", MAFTER);

	if(**argv == '/' || **argv == '.' || **argv == '#') {
		if(loadtext(*argv, argc, argv) < 0)
			panic("loadtext: %r");
	} else {
		file = smprint("/bin/%s", *argv);
		if(loadtext(file, argc, argv) < 0)
			panic("loadtext: %r");
		free(file);
	}

	if(open("/dev/cons", OREAD) != 0)
			panic("open0: %r");
	if(open("/dev/cons", OWRITE) != 1)
			panic("open1: %r");
	if(open("/dev/cons", OWRITE) != 2)
			panic("open2: %r");

	notify(notehandler);
	for(;;) {
		if(debug > 2)
			dump();
		step();
		while((up->notein - up->noteout) % NNOTE) {
			donote(up->notes[up->noteout % NNOTE], 0);
			up->noteout++;
		}
	}

	exits(0);
}