2022-08-22 18:36:26 +03:00
|
|
|
/*
|
|
|
|
|
* NRJ (NOR and Reference Jump) OISC ANSI C emulator by Luxferre
|
|
|
|
|
* Public domain
|
|
|
|
|
* Build with: cc -Os -o nrj nrj.c
|
|
|
|
|
*/
|
2022-08-19 21:55:12 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <termios.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
#ifndef NRJWORD
|
|
|
|
|
#define NRJWORD unsigned short
|
|
|
|
|
#endif
|
2022-08-22 18:36:26 +03:00
|
|
|
#define NRJWSIZE (sizeof(NRJWORD))
|
|
|
|
|
#define NRJSIZE (1 << NRJWSIZE)
|
2022-08-22 08:20:51 +03:00
|
|
|
#define MAXADDR ((NRJWORD) (NRJSIZE - 1))
|
2022-08-19 21:55:12 +03:00
|
|
|
|
|
|
|
|
int kbhit() {
|
|
|
|
|
struct timeval tv = { 0L, 0L };
|
|
|
|
|
fd_set fds;
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
|
FD_SET(0, &fds);
|
|
|
|
|
return select(1, &fds, NULL, NULL, &tv) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2022-08-22 18:36:26 +03:00
|
|
|
unsigned char c;
|
2022-08-19 21:55:12 +03:00
|
|
|
if(*ctxid == (NRJWORD) 0) { /* for now, only emulate standard context id */
|
|
|
|
|
*val = (NRJWORD) 0;
|
|
|
|
|
if(kbhit()) {
|
2022-08-22 18:36:26 +03:00
|
|
|
if(read(0, &c, 1) > -1)
|
|
|
|
|
*val = (NRJWORD) c;
|
2022-08-19 21:55:12 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nrj_out(NRJWORD *ctxid, NRJWORD *val) {
|
|
|
|
|
if(*ctxid == (NRJWORD) 0) /* for now, only emulate standard context id */
|
|
|
|
|
putchar((unsigned char) *val);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-22 18:36:26 +03:00
|
|
|
void nrj_run(NRJWORD *mem, NRJWORD pc) { /* 0 - input, 1 - output, 2 - I/O context, 3 - program start */
|
2022-08-22 08:20:51 +03:00
|
|
|
while(pc != MAXADDR) {
|
2022-08-21 20:10:03 +03:00
|
|
|
if(mem[0]) nrj_in(&mem[2], &mem[mem[0]]);
|
2022-08-22 08:20:51 +03:00
|
|
|
mem[mem[pc]] = (~(mem[mem[pc]] | mem[mem[pc+1]])) & MAXADDR;
|
2022-08-21 20:10:03 +03:00
|
|
|
pc = mem[mem[pc+2]];
|
|
|
|
|
mem[0] = (NRJWORD) 0;
|
|
|
|
|
if(mem[1]) {
|
|
|
|
|
nrj_out(&mem[2], &mem[mem[1]]);
|
|
|
|
|
mem[1] = (NRJWORD) 0;
|
2022-08-19 21:55:12 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2022-08-22 18:36:26 +03:00
|
|
|
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 {
|
2022-08-19 21:55:12 +03:00
|
|
|
puts("NRJ16: no binary specified\r");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|