Added `-binary` CLI flag to NEDsim for loading a.out format programs.
[screensavers] / hacks / NEDsim / simulator.h
/* (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com> */
/* See LICENSE.txt file for copyright and license details. */
// TODO: Prune includes in both this header and the corresponding source file.
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <termios.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "./a.out.h"
/* Bytes per word. */
#define BPW 4
/* Number of stack words. */
#define STACK_LENGTH 65536
/* Number of bytes of RAM. */
#define RAM_LENGTH 67108864
#define RAM_BASE_ADDRESS 0x20000000
/* Number of syllables per word. */
#define SPW 5
struct NEDpsw {
bool zero;
bool negative;
};
struct NEDthread {
uint32_t stack[STACK_LENGTH];
uint32_t sp;
uint32_t pc;
uint8_t sc;
struct NEDpsw * psw;
};
struct NEDhack {
uint32_t iw;
bool resume_word;
};
struct NEDstate {
bool halted;
uint8_t ram[RAM_LENGTH];
/* Although NED is multi-threaded, this screensaver is restricted to a single thread. */
struct NEDthread * thread[1];
struct NEDthread * active_thread;
struct NEDhack * hack;
};
enum syllables {
MVSTCK = 0b00001111,
JMP = 0b00001110,
SWAP = 0b00001101,
ADD = 0b00001100,
XOR = 0b00001011,
NOT = 0b00001010,
OR = 0b00001001,
AND = 0b00001000,
BRZ = 0b00000111,
TEST = 0b00000110,
CMPSWP = 0b00000101,
SHIFT = 0b00000100,
STORE = 0b00000011,
LOAD = 0b00000010,
NOP = 0b00000001,
HALT = 0b00000000
};
struct NEDstate * init_simulator(char * input_file);
struct NEDstate * run_simulator(struct NEDstate * state);
uint32_t ram_r_word(struct NEDstate * state, uint32_t address);