even more optimizations
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* NRJ (NOR and Reference Jump) OISC ANSI C emulator by Luxferre
|
* NRJ (NOR and Reference Jump) OISC ANSI C emulator
|
||||||
* Public domain
|
* Created by Luxferre, 2022, public domain
|
||||||
* Build with: cc -Os -o nrj nrj.c
|
* Build with: cc -Os -o nrj nrj.c
|
||||||
*/
|
*/
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -23,43 +23,43 @@ int kbhit() {
|
|||||||
return select(1, &fds, NULL, NULL, &tv) > 0;
|
return select(1, &fds, NULL, NULL, &tv) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct termios tty_opts_backup, tty_opts_raw;
|
void nrj_in(NRJWORD *ctxid, NRJWORD *val) { /* context-dependent input handler */
|
||||||
void restore_term() {
|
|
||||||
tcsetattr(STDIN_FILENO, TCSANOW, &tty_opts_backup);
|
|
||||||
}
|
|
||||||
|
|
||||||
void nrj_in(NRJWORD *ctxid, NRJWORD *val) {
|
|
||||||
unsigned char c;
|
unsigned char c;
|
||||||
if(*ctxid == (NRJWORD) 0) { /* for now, only emulate standard context id */
|
if(*ctxid == (NRJWORD) 0) { /* for now, only emulate standard context id */
|
||||||
*val = (NRJWORD) 0;
|
*val = (NRJWORD) 0;
|
||||||
if(kbhit()) {
|
if(kbhit() && read(0, &c, 1) > -1)
|
||||||
if(read(0, &c, 1) > -1)
|
*val = (NRJWORD) c;
|
||||||
*val = (NRJWORD) c;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void nrj_out(NRJWORD *ctxid, NRJWORD *val) {
|
void nrj_out(NRJWORD *ctxid, NRJWORD *val) { /* context-dependent output handler */
|
||||||
if(*ctxid == (NRJWORD) 0) /* for now, only emulate standard context id */
|
if(*ctxid == (NRJWORD) 0) /* for now, only emulate standard context id */
|
||||||
putchar((unsigned char) *val);
|
putchar((unsigned char) *val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void nrj_run(NRJWORD *mem, NRJWORD pc) { /* 0 - input, 1 - output, 2 - I/O context, 3 - program start */
|
void nrj_run(NRJWORD *mem, NRJWORD pc) { /* Main NRJ engine - just 12 lines of C within this function */
|
||||||
while(pc != MAXADDR) {
|
while(pc != MAXADDR) { /* halt if jumped to the last word */
|
||||||
if(mem[0]) nrj_in(&mem[2], &mem[mem[0]]);
|
if(mem[0]) { /* handle input if the first word is set, and clear it */
|
||||||
mem[mem[pc]] = (~(mem[mem[pc]] | mem[mem[pc+1]])) & MAXADDR;
|
nrj_in(&mem[2], &mem[mem[0]]);
|
||||||
pc = mem[mem[pc+2]];
|
mem[0] = (NRJWORD) 0;
|
||||||
mem[0] = (NRJWORD) 0;
|
}
|
||||||
if(mem[1]) {
|
mem[mem[pc]] = (~(mem[mem[pc]] | mem[mem[pc+1]])) & MAXADDR; /* then perform the NOR operation */
|
||||||
|
pc = mem[mem[pc+2]]; /* then perform the reference jump operation */
|
||||||
|
if(mem[1]) { /* then handle output if the second word is set, and clear it */
|
||||||
nrj_out(&mem[2], &mem[mem[1]]);
|
nrj_out(&mem[2], &mem[mem[1]]);
|
||||||
mem[1] = (NRJWORD) 0;
|
mem[1] = (NRJWORD) 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct termios tty_opts_backup, tty_opts_raw;
|
||||||
|
void restore_term() {
|
||||||
|
tcsetattr(STDIN_FILENO, TCSANOW, &tty_opts_backup);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if(argc > 1) {
|
if(argc > 1) {
|
||||||
NRJWORD mem[NRJSIZE];
|
NRJWORD mem[NRJSIZE]; /* NRJ memory: word 0 - input, 1 - output, 2 - I/O context, 3 - program start */
|
||||||
FILE *prog = fopen(argv[1], "rb");
|
FILE *prog = fopen(argv[1], "rb");
|
||||||
if(prog) {
|
if(prog) {
|
||||||
/* load the program */
|
/* load the program */
|
||||||
|
|||||||
Reference in New Issue
Block a user