From: khamidou Date: Wed, 23 Feb 2011 17:02:43 +0000 (+0100) Subject: added and fixed PEEK and POKE X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=ee088df78b1bdd265de969c29c7b381f6f295364;p=miniforth.git added and fixed PEEK and POKE --- diff --git a/as.c b/as.c index 59a1fce..ae17885 100644 --- a/as.c +++ b/as.c @@ -119,15 +119,10 @@ int parse() { yylex(); writeint(PUSH, outfp); writeint(atoi(yytext), outfp); - current_offset++; /* we're pushing to values to the file, so update - current_offset accordingly - */ break; case T_POP: - yylex(); writeint(POP, outfp); - writeint(atoi(yytext), outfp); break; case T_DPRINT: @@ -173,6 +168,39 @@ int parse() { writeint(l->offset, outfp); break; + case T_PEEK: + t = yylex(); + if (t == WORD) { + l = search_label(yytext); + if (l == NULL) + fail("the label %s doesn't exists", yytext); + val = l->offset; + } else if (t == NUMBER) { + val = atoi(yytext); + } + + writeint(PEEK, outfp); + writeint(val, outfp); + break; + + case T_POKE: + t = yylex(); + if (t == WORD) { + l = search_label(yytext); + if (l == NULL) + fail("the label %s doesn't exists", yytext); + + printf("%s:%d", l->name, l->offset); + val = l->offset; + } else if (t == NUMBER) { + val = atoi(yytext); + } + + writeint(POKE, outfp); + writeint(val, outfp); + break; + + case T_DB: yylex(); val = atoi(yytext); @@ -192,8 +220,6 @@ int parse() { writeint(val, outfp); break; - - default: fail("unexpected %s in input at line %d", yytext, yylineno); break; diff --git a/aslexer.l b/aslexer.l index e744ce9..75c60ca 100644 --- a/aslexer.l +++ b/aslexer.l @@ -24,6 +24,8 @@ "jmp" { return T_JMP; } "db" { return T_DB; } "times" { return T_TIMES; } +"peek" { return T_PEEK; } +"poke" { return T_POKE; } [ \t\n]+ /* ignore whitespace */ [0-9]+ { return NUMBER; } diff --git a/main.c b/main.c index 47feedd..76ce991 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,8 @@ #include "vm.h" +enum modes {RUN_MODE, DUMP_MODE}; + int filelength(FILE *fp) { if (fp == NULL) return -1; @@ -16,12 +18,25 @@ int filelength(FILE *fp) { } int main(int argc, char **argv) { - if (argc != 2) { + if (argc != 2 && argc != 3) { puts("vm file.bin"); + puts("vm -d file.bin"); exit(0); } - FILE *fp = fopen(argv[1], "r"); + FILE *fp; + int mode; + + if (strcmp(argv[1], "-d") == 0) + mode = DUMP_MODE; + else + mode = RUN_MODE; + + if (mode == DUMP_MODE) + fp = fopen(argv[2], "r"); + else + fp = fopen(argv[1], "r"); + if (fp == NULL) error("unable to find the requested file"); @@ -31,6 +46,10 @@ int main(int argc, char **argv) { load_file(v, fp); printf("vm launched, memsize: %d, stacksize: %d\n", v->memsize, v->stacksize); - run(v); + + if (mode == DUMP_MODE) + dump_mem(v); + else + run(v); } diff --git a/tokens.h b/tokens.h index 928a0ba..94826e4 100644 --- a/tokens.h +++ b/tokens.h @@ -15,6 +15,8 @@ enum tokens { NUMBER, T_JMP, T_DB, T_TIMES, + T_PEEK, + T_POKE, }; diff --git a/vm.c b/vm.c index 34838fe..ac33014 100644 --- a/vm.c +++ b/vm.c @@ -36,12 +36,27 @@ int load_file(struct vm *v, FILE *fd) fread(v->mem, sizeof(int), v->memsize, fd); } +int dump_mem(struct vm *v) +{ + /* display the contents of the memory of a vm */ + int offset; + for(offset = 0; offset < v->memsize - 3; offset += 4) { + printf("%xh:\t%d\t%d\t%d\t%d\n", offset, v->mem[offset], v->mem[offset+1], + v->mem[offset+2], v->mem[offset+3]); + } + + for (; offset < v->memsize; offset++) { + printf("%d\t", v->mem[offset]); + } +} + int run(struct vm *v) { if (v == NULL) return -1; int *ip = v->mem; + int *addr; /* a temporary address */ while(ip < v->mem + v->memsize) { switch(*ip) { case NOP: @@ -88,14 +103,10 @@ int run(struct vm *v) break; case RET: - /* get ip from the stack. it must be the thing just before - * the top of the stack - it implies that programs must return - * one and only one return value - */ if (v->callstackoffset < 0) fail("call stack underflow"); - ip = v->callstack[v->callstackoffset]; + ip = (int *) v->callstack[v->callstackoffset]; v->callstackoffset--; break; @@ -104,6 +115,24 @@ int run(struct vm *v) ip = v->mem + (*ip) - 1; /* because the pointer gets automatically incremented */ break; + case PEEK: + ip++; + v->stackoffset++; + if (v->stackoffset >= v->stacksize) + fail("peek: stack overflow"); + + v->stack[v->stackoffset] = *(v->mem + *ip); + break; + + case POKE: + ip++; + if (v->stackoffset < 0) + fail("poke: stack underflow"); + + addr = v->mem + *ip; + *addr = v->stack[v->stackoffset]; + break; + } ip++; diff --git a/vm.h b/vm.h index 0cd2537..19825f3 100644 --- a/vm.h +++ b/vm.h @@ -4,7 +4,7 @@ #include #include -enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, JMP, RET, DPRINT, INC, DEC}; +enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, JMP, RET, DPRINT, INC, DEC, PEEK, POKE}; struct vm { int *mem;