aboutsummaryrefslogtreecommitdiff
path: root/src/orgaemu.c
blob: b8244156d634b1e5b88cd95959a8a9e5a27ebd11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "drain.h"
#include "ops.h"
#include <stdio.h>
#include <stdint.h>

#define MEM_SIZE 0xffff

static uint16_t *mem = NULL, *stack = NULL, *rstack = NULL;
static size_t stack_ptr = 0, rstack_ptr = 0;

static int
fail(void)
{
	if (mem != NULL)
		free(mem);
	if (stack != NULL)
		free(stack);
	if (rstack != NULL)
		free(rstack);
	return 1;
}

static uint16_t
get_short(const unsigned char *s)
{
	return s[0] | (s[1] * 0x0100);
}

static void
push(uint16_t v)
{
	stack[stack_ptr] = v;
	stack_ptr += 1;
	if (stack_ptr >= MEM_SIZE) {
		fprintf(stderr, "stack overflow\n");
		exit(fail());
	}
}

static uint16_t
pop(void)
{
	if (stack_ptr == 0) {
		fprintf(stderr, "stack underflow\n");
		exit(fail());
	}
	stack_ptr -= 1;
	return stack[stack_ptr];
}

static void
push_rs(uint16_t v)
{
	rstack[rstack_ptr] = v;
	rstack_ptr += 1;
	if (rstack_ptr >= MEM_SIZE) {
		fprintf(stderr, "return stack overflow\n");
		exit(fail());
	}
}
 
static uint16_t
pop_rs(void)
{
	if (rstack_ptr == 0) {
		fprintf(stderr, "return stack underflow\n");
		exit(fail());
	}
	rstack_ptr -= 1;
	return rstack[rstack_ptr];
}

static void
exec_data(uint16_t *mem)
{
	for (long pc = 0; pc < MEM_SIZE;) {
		switch (mem[pc]) {
		case OP_NOP:
			break;
		case OP_LIT:
			push(mem[pc + 1]);
			pc += 1;
			break;
		case OP_POP:
			pop();
			break;
		case OP_NIP: {
			const uint16_t a = pop();
			pop();
			push(a);
		} break;
		case OP_SWP: {
			const uint16_t a = pop();
			const uint16_t b = pop();
			push(a);
			push(b);
		} break;
		case OP_DUP: {
			const uint16_t a = pop();
			push(a);
			push(a);
		} break;
		case OP_ADD:
			push(pop() + pop());
			break;
		case OP_WRT:
			putchar(pop());
			break;
		case OP_JNZ:
			if (pop() != 0) {
				pc = mem[pc + 1];
				continue;
			}
			pc += 1;
			break;
		case OP_JRT:
			push_rs(pc);
			pc = mem[pc + 1];
			continue;
		case OP_RET:
			if (rstack_ptr > 0) {
				pc = pop_rs() + 1;
				break;
			}
			return;
		case OP_LDA:
			push(mem[pop()]);
			break;
		case OP_STA: {
			const uint16_t a = pop();
			const uint16_t b = pop();
			mem[a] = b;
		} break;
		default:
			fprintf(stderr, "unhandled opcode %04x\n", mem[pc]);
			if (mem[pc] <= OP_SLP)
				fprintf(stderr, "(%s)\n", ops[mem[pc]]);
			exit(fail());
		}
		pc += 1;
	}
}

int
main(int argc, char **argv)
{
	if (argc != 2) {
		fprintf(stderr, "usage: %s <rom>\n", argv[0]);
		return 1;
	}

	FILE *file = fopen(argv[1], "rb");
	if (file == NULL) {
		perror(argv[1]);
		return 1;
	}

	long size;
	unsigned char *const data = (unsigned char*)drain(file, &size);
	fclose(file);
	if (data == NULL)
		return 1;

	mem = calloc(MEM_SIZE, sizeof(uint16_t));
	if (mem == NULL) {
		perror("main:calloc");
		free(data);
		return 1;
	}

	for (long i = 0; i < size; i += 2)
		mem[i / 2] = get_short(data + i);
	free(data);

	stack = calloc(MEM_SIZE, sizeof(uint16_t));
	if (stack == NULL) {
		perror("main:calloc");
		return fail();
	}

	rstack = calloc(MEM_SIZE, sizeof(uint16_t));
	if (rstack == NULL) {
		perror("main:calloc");
		return fail();
	}

	exec_data(mem);
	return fail(), 0;
}