nrj-oisc

NOR and Reference Jump OISC platform
git clone git://git.luxferre.top/nrj-oisc.git
Log | Files | Refs | README

commit b7e539596dcbb82539906fa3fe974cf18cdffb5e
parent c297dd2d14c328bffebdad6433e837b4a7d1a03c
Author: Luxferre <lux@ferre>
Date:   Mon, 22 Aug 2022 18:36:26 +0300

Some optimizations

Diffstat:
Mnrj.c | 76++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 34 insertions(+), 42 deletions(-)

diff --git a/nrj.c b/nrj.c @@ -1,18 +1,19 @@ -/* Build with: cc -Os -o nrj nrj.c */ +/* + * NRJ (NOR and Reference Jump) OISC ANSI C emulator by Luxferre + * Public domain + * Build with: cc -Os -o nrj nrj.c + */ #include <stdlib.h> #include <stdio.h> #include <termios.h> #include <unistd.h> #include <sys/select.h> -#ifndef NRJBITS -#define NRJBITS 16 -#endif #ifndef NRJWORD #define NRJWORD unsigned short #endif -#define NRJSIZE (1 << NRJBITS) +#define NRJWSIZE (sizeof(NRJWORD)) +#define NRJSIZE (1 << NRJWSIZE) #define MAXADDR ((NRJWORD) (NRJSIZE - 1)) -#define NRJWSIZE sizeof(NRJWORD) int kbhit() { struct timeval tv = { 0L, 0L }; @@ -22,26 +23,18 @@ int kbhit() { return select(1, &fds, NULL, NULL, &tv) > 0; } -int getch() { - int r; - unsigned char c; - if((r = read(0, &c, 1)) < 0) return r; - else return c; -} - struct termios tty_opts_backup, tty_opts_raw; void restore_term() { tcsetattr(STDIN_FILENO, TCSANOW, &tty_opts_backup); } void nrj_in(NRJWORD *ctxid, NRJWORD *val) { - int x; + unsigned char c; if(*ctxid == (NRJWORD) 0) { /* for now, only emulate standard context id */ *val = (NRJWORD) 0; if(kbhit()) { - x = getch(); - if(x > -1) - *val = (NRJWORD) x; + if(read(0, &c, 1) > -1) + *val = (NRJWORD) c; } } } @@ -51,26 +44,7 @@ void nrj_out(NRJWORD *ctxid, NRJWORD *val) { putchar((unsigned char) *val); } -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, NRJWSIZE, (flen/NRJWSIZE) & MAXADDR, prog); - fclose(prog); - cfmakeraw(&tty_opts_raw); - tcsetattr(STDIN_FILENO, TCSANOW, &tty_opts_raw); - } - else { - printf("NRJ16: could not open the input file %s\r\n", fname); - exit(1); - } -} - -void nrj_run(char *program) { - NRJWORD mem[NRJSIZE], pc = (NRJWORD) 3; /* 0 - input, 1 - output, 2 - I/O context, 3 - program start */ - nrj_load(mem, program); +void nrj_run(NRJWORD *mem, NRJWORD pc) { /* 0 - input, 1 - output, 2 - I/O context, 3 - program start */ while(pc != MAXADDR) { if(mem[0]) nrj_in(&mem[2], &mem[mem[0]]); mem[mem[pc]] = (~(mem[mem[pc]] | mem[mem[pc+1]])) & MAXADDR; @@ -84,11 +58,29 @@ void nrj_run(char *program) { } int main(int argc, char* argv[]) { - tcgetattr(STDIN_FILENO, &tty_opts_backup); - atexit(&restore_term); - if(argc > 1) - nrj_run(argv[1]); - else { + if(argc > 1) { + NRJWORD mem[NRJSIZE]; + FILE *prog = fopen(argv[1], "rb"); + if(prog) { + /* load the program */ + fseek(prog, 0, SEEK_END); + int flen = ftell(prog); + fseek(prog, 0, SEEK_SET); + fread(mem, NRJWSIZE, (flen/NRJWSIZE) & MAXADDR, prog); + fclose(prog); + /* prepare the terminal */ + tcgetattr(STDIN_FILENO, &tty_opts_backup); + atexit(&restore_term); + cfmakeraw(&tty_opts_raw); + tcsetattr(STDIN_FILENO, TCSANOW, &tty_opts_raw); + /* start the engine */ + nrj_run(mem, 3); + } + else { + printf("NRJ16: could not open the input file %s\r\n", argv[1]); + return 1; + } + } else { puts("NRJ16: no binary specified\r"); return 1; }