commit c297dd2d14c328bffebdad6433e837b4a7d1a03c
parent c19a47877c9ba3fbdb5a49026d24d3a0cf81783c
Author: Luxferre <lux@ferre>
Date: Mon, 22 Aug 2022 08:20:51 +0300
Some opti
Diffstat:
M | nrj.c | | | 15 | +++++++-------- |
1 file changed, 7 insertions(+), 8 deletions(-)
diff --git 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]) {