~grimmware/lc3

3b51013e42ad07d6ad3b643ef8a635b3ab2d631c — glenda 1 year, 8 months ago 97de430
Can read pc from the filesystem
2 files changed, 111 insertions(+), 1 deletions(-)

A fs.c
M lc3.c
A fs.c => fs.c +94 -0
@@ 0,0 1,94 @@
#include <fcall.h>
#include <9p.h>

char *mtpt = nil;
Channel* clk;

enum {
	Lclk,
	Lpc,
};

typedef struct Lfid Lfid;
struct Lfid {
	int type;
};

Lfid
*lfidgen(int type)
{
	struct Lfid *lfid;
	lfid = emalloc9p(sizeof *lfid);
	lfid->type = type;
	return lfid;
}

static void
fsstart(Srv*)
{
	if(mtpt != nil) unmount(nil, mtpt);
}

static void
fsend(Srv*)
{
	postnote(PNGROUP, getpid(), "shutdown");
	exits(nil);
}

static void
fsopen(Req *r)
{
	respond(r, nil);
	return;
}

static void
fsread(Req *r)
{
	Lfid *l = r->fid->file->aux;
	switch(l->type){
	case Lclk:
		sendul(clk, (ulong) 1);
		readstr(r, "");
		break;
	case Lpc:
		readstr(r, smprint("%x\n",reg[R_PC]));
		break;
	}
	respond(r, nil);
	return;
}

static void
fswrite(Req *r)
{
	int n;
	char *argv;
	n = r->ofcall.count = r->ifcall.count;
	argv = emalloc9p(n+1);
	memmove(argv, r->ifcall.data, n);
	sendul(clk, strtoul(argv, 0, 10));
	respond(r, nil);
	return;
}

Srv fs = {
	.start = fsstart,
	.end = fsend,
	.read = fsread,
	.open = fsopen,
	.write = fswrite,
};

void
newfs(void)
{
	Tree *tree;
	clk = chancreate(sizeof(ulong), 20);
	tree = alloctree(nil, nil, DMDIR|0555, nil);
	fs.tree = tree;
	createfile(tree->root, "clk", nil, 0644, lfidgen(Lclk));
	createfile(tree->root, "pc", nil, 0644, lfidgen(Lpc));
	threadpostmountsrv(&fs, "lc3fs", mtpt, MREPL | MCREATE);
}

M lc3.c => lc3.c +17 -1
@@ 2,8 2,10 @@
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include <thread.h>
#include "lc3.h"
#include "util.c"
#include "fs.c"

int debug = 0;
int consctl;


@@ 219,8 221,9 @@ op_trap(u16int in)
}

void
main(int argc, char* argv[])
threadmain(int argc, char* argv[])
{
	ulong steps = 0;
	char *imagefile;
	ARGBEGIN {
	case 'd':


@@ 231,8 234,16 @@ main(int argc, char* argv[])
		if(!readimage(imagefile)) {
			exits("Could not read image file");
		}
		break;
	case 'm':
		mtpt = EARGF(usage());
		break;
	} ARGEND;

	if(mtpt){
		newfs();
	}

	consctl = open("/dev/consctl", OWRITE);
	if(consctl == -1) exits("can't open /dev/consctl: %r\n");	// exactly one cond flag should be set at all times
	reg[R_COND] = FL_ZRO;


@@ 241,6 252,9 @@ main(int argc, char* argv[])
	int running = 1;
	while (running)
	{
		print("%d\n", (int) steps);
		if(mtpt && steps == 0)
			steps += recvul(clk);
		// fetch instruction
		u16int instr = memread(reg[R_PC]++);
		u16int op = instr >> 12;


@@ 307,6 321,8 @@ main(int argc, char* argv[])
				print("%04x ", memory[i]);
			print("\n");
		}
		if(mtpt && steps > 0)
			steps--;
	}
	exits(nil);
}