Added `-binary` CLI flag to NEDsim for loading a.out format programs.
[screensavers] / hacks / NEDsim / simulator.h
CommitLineData
84b74595
AT
1/* (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com> */
2/* See LICENSE.txt file for copyright and license details. */
3
4// TODO: Prune includes in both this header and the corresponding source file.
5#include <stdio.h>
6#include <stdint.h>
7#include <inttypes.h>
8#include <stdlib.h>
9#include <stdbool.h>
10#include <unistd.h>
11#include <fcntl.h>
12#include <string.h>
13#include <errno.h>
14#include <time.h>
15#include <termios.h>
16#include <signal.h>
17#include <sys/socket.h>
18#include <sys/types.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <netdb.h>
22
23#include "./a.out.h"
24
84b74595
AT
25/* Bytes per word. */
26#define BPW 4
27
28/* Number of stack words. */
d87b1e06 29#define STACK_LENGTH 65536
84b74595
AT
30
31/* Number of bytes of RAM. */
d87b1e06
AT
32#define RAM_LENGTH 67108864
33#define RAM_BASE_ADDRESS 0x20000000
84b74595
AT
34
35/* Number of syllables per word. */
36#define SPW 5
37
38struct NEDpsw {
39 bool zero;
40 bool negative;
41};
42
43struct NEDthread {
44 uint32_t stack[STACK_LENGTH];
45 uint32_t sp;
46 uint32_t pc;
47 uint8_t sc;
48 struct NEDpsw * psw;
49};
50
51struct NEDhack {
52 uint32_t iw;
53 bool resume_word;
54};
55
84b74595
AT
56struct NEDstate {
57 bool halted;
58 uint8_t ram[RAM_LENGTH];
d87b1e06
AT
59 /* Although NED is multi-threaded, this screensaver is restricted to a single thread. */
60 struct NEDthread * thread[1];
84b74595
AT
61 struct NEDthread * active_thread;
62 struct NEDhack * hack;
63};
64
65enum syllables {
66 MVSTCK = 0b00001111,
67 JMP = 0b00001110,
68 SWAP = 0b00001101,
69 ADD = 0b00001100,
70 XOR = 0b00001011,
71 NOT = 0b00001010,
72 OR = 0b00001001,
73 AND = 0b00001000,
74 BRZ = 0b00000111,
75 TEST = 0b00000110,
76 CMPSWP = 0b00000101,
77 SHIFT = 0b00000100,
78 STORE = 0b00000011,
79 LOAD = 0b00000010,
80 NOP = 0b00000001,
81 HALT = 0b00000000
82};
83
5923644e 84struct NEDstate * init_simulator(char * input_file);
84b74595
AT
85struct NEDstate * run_simulator(struct NEDstate * state);
86uint32_t ram_r_word(struct NEDstate * state, uint32_t address);
87