diff --git a/nrj.c b/nrj.c index f6040b2..5859977 100644 --- a/nrj.c +++ b/nrj.c @@ -11,6 +11,7 @@ #define NRJWORD unsigned short #endif #define NRJSIZE (1 << NRJBITS) +#define MAXADDR ((NRJWORD) (NRJSIZE - 1)) #define NRJWSIZE sizeof(NRJWORD) int kbhit() { @@ -50,13 +51,13 @@ void nrj_out(NRJWORD *ctxid, NRJWORD *val) { putchar((unsigned char) *val); } -void nrj_load(NRJWORD* m, NRJWORD offset, char *fname) { +void nrj_load(NRJWORD* m, char *fname) { FILE *prog = fopen(fname, "rb"); if(prog) { fseek(prog, 0, SEEK_END); int flen = ftell(prog); fseek(prog, 0, SEEK_SET); - fread(m+offset, NRJWSIZE, flen/NRJWSIZE, prog); + fread(m, NRJWSIZE, (flen/NRJWSIZE) & MAXADDR, prog); fclose(prog); cfmakeraw(&tty_opts_raw); tcsetattr(STDIN_FILENO, TCSANOW, &tty_opts_raw); @@ -68,13 +69,11 @@ void nrj_load(NRJWORD* m, NRJWORD offset, char *fname) { } void nrj_run(char *program) { - NRJWORD maxaddr = (NRJWORD) (NRJSIZE - 1), mem[NRJSIZE], pc; - mem[0] = mem[1] = mem[2] = (NRJWORD) 0; - pc = (NRJWORD) 3; - nrj_load(mem, pc, program); - while(pc != maxaddr) { + NRJWORD mem[NRJSIZE], pc = (NRJWORD) 3; /* 0 - input, 1 - output, 2 - I/O context, 3 - program start */ + nrj_load(mem, program); + while(pc != MAXADDR) { if(mem[0]) nrj_in(&mem[2], &mem[mem[0]]); - mem[mem[pc]] = (~(mem[mem[pc]] | mem[mem[pc+1]])) & maxaddr; + mem[mem[pc]] = (~(mem[mem[pc]] | mem[mem[pc+1]])) & MAXADDR; pc = mem[mem[pc+2]]; mem[0] = (NRJWORD) 0; if(mem[1]) {