aboutsummaryrefslogtreecommitdiff
path: root/src/orgaemu.c
blob: 4de09cfb07f7fcfce10dcfb4939a860cb027bf74 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "drain.h"
#include "ops.h"
#include <stdio.h>
#include <stdint.h>
#include <SDL2/SDL.h>

#define MEM_SIZE 0xffff

static const uint16_t buttons[SDL_NUM_SCANCODES] = {
	[SDL_SCANCODE_BACKSPACE] = 1 << 2,
	[SDL_SCANCODE_RETURN] = 1 << 3,
	/* WASD JK */
	[SDL_SCANCODE_J] = 1 << 0,
	[SDL_SCANCODE_K] = 1 << 1,
	[SDL_SCANCODE_W] = 1 << 4,
	[SDL_SCANCODE_S] = 1 << 5,
	[SDL_SCANCODE_A] = 1 << 6,
	[SDL_SCANCODE_D] = 1 << 7,
	/* arrows XC */
	[SDL_SCANCODE_X] = 1 << 0,
	[SDL_SCANCODE_C] = 1 << 1,
	[SDL_SCANCODE_UP] = 1 << 4,
	[SDL_SCANCODE_DOWN] = 1 << 5,
	[SDL_SCANCODE_LEFT] = 1 << 6,
	[SDL_SCANCODE_RIGHT] = 1 << 7,
};

static uint16_t *mem = NULL, *stack = NULL, *rstack = NULL;
static size_t stack_ptr = 0, rstack_ptr = 0;
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
static SDL_Texture *target = NULL;
static Uint64 next_time = 0;

static int
fail(void)
{
	if (target != NULL)
		SDL_DestroyTexture(target);
	if (renderer != NULL)
		SDL_DestroyRenderer(renderer);
	if (window != NULL)
		SDL_DestroyWindow(window);
	SDL_Quit();
	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 int
cycle_events(uint16_t *input)
{
	SDL_Event e;
	while (SDL_PollEvent(&e)) {
		switch (e.type) {
		case SDL_QUIT:
			return 1;
		case SDL_KEYDOWN:
			if (e.key.repeat)
				break;
			if (e.key.keysym.scancode == SDL_SCANCODE_F) {
				static int fullscreen = 0;
				fullscreen = !fullscreen;
				SDL_SetWindowFullscreen(window, fullscreen);
			}
			*input |= buttons[e.key.keysym.scancode];
			break;
		case SDL_KEYUP:
			*input &= ~(buttons[e.key.keysym.scancode]);
			break;
		default:
			break;
		}
	}
	return 0;
}

static void
render(uint16_t *mem)
{
	int win_w, win_h;
	SDL_GetWindowSize(window, &win_w, &win_h);
	win_w = win_w ? win_w : 1;
	win_h = win_h ? win_h : 1;
	const float ratio_w = win_w / 128.0;
	const float ratio_h = win_h / 128.0;
	float scale = (ratio_w < ratio_h) ? ratio_w : ratio_h;
	if (scale > 1.0)
		scale = (int)scale;
	const int off_x = (win_w - 128 * scale) / 2;
	const int off_y = (win_h - 128 * scale) / 2;
	const SDL_Rect dest = { off_x, off_y, 128 * scale, 128 * scale };

	SDL_SetRenderTarget(renderer, target);
	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
	SDL_RenderClear(renderer);
	for (int y = 0; y < 128; y++) {
		for (int x = 0; x < 128; x++) {
			const uint16_t pixel = mem[x + y * 128];
			if (!pixel)
				continue;
			const uint16_t r = (pixel & 0x1f) << 3;
			const uint16_t g = ((pixel >> 5) & 0x3f) << 2;
			const uint16_t b = ((pixel >> 11) & 0x1f) << 3;
			SDL_SetRenderDrawColor(renderer, r, g, b, 255);
			SDL_RenderDrawPoint(renderer, x, y);
		}
	}
	SDL_SetRenderTarget(renderer, NULL);
	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
	SDL_RenderClear(renderer);
	SDL_RenderCopy(renderer, target, NULL, &dest);

	next_time += 1000 / 30;
	const Uint64 cur_time = SDL_GetTicks64();
	if (next_time <= cur_time) {
		fprintf(stderr, "lagging %lu\n", cur_time - next_time);
		next_time = cur_time;
	} else
		SDL_Delay(next_time - cur_time);

	SDL_RenderPresent(renderer);
}

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 long
exec_op(uint16_t *mem, long pc)
{
	switch (mem[pc]) {
	case OP_NOP:
		return pc + 1;
	case OP_LIT:
		push(mem[pc + 1]);
		return pc + 2;
	case OP_POP:
		pop();
		return pc + 1;
	case OP_NIP: {
		const uint16_t a = pop();
		pop();
		push(a);
		return pc + 1;
	}
	case OP_SWP: {
		const uint16_t a = pop();
		const uint16_t b = pop();
		push(a);
		push(b);
		return pc + 1;
	}
	case OP_ROT: {
		const uint16_t a = pop();
		const uint16_t b = pop();
		const uint16_t c = pop();
		push(b);
		push(a);
		push(c);
	} return pc + 1;
	case OP_DUP: {
		const uint16_t a = pop();
		push(a);
		push(a);
		return pc + 1;
	}
	case OP_OVR: {
		const uint16_t a = pop();
		const uint16_t b = pop();
		push(b);
		push(a);
		push(b);
		return pc + 1;
	}
	case OP_EQU:
		push(pop() == pop());
		return pc + 1;
	case OP_NEQ:
		push(pop() != pop());
		return pc + 1;
	case OP_GTH: {
		const uint16_t b = pop();
		const uint16_t a = pop();
		push(a > b);
		return pc + 1;
	}
	case OP_LTH: {
		const uint16_t b = pop();
		const uint16_t a = pop();
		push(a < b);
		return pc + 1;
	}
	case OP_JEZ:
		if (pop() == 0)
			return mem[pc + 1];
		return pc + 2;
	case OP_JNZ:
		if (pop() != 0)
			return mem[pc + 1];
		return pc + 2;
	case OP_JMP:
		return mem[pc + 1];
	case OP_JRT:
		push_rs(pc + 2);
		return mem[pc + 1];
	case OP_RET:
		if (rstack_ptr > 0)
			return pop_rs();
		return MEM_SIZE;
	case OP_LDA:
		push(mem[pop()]);
		return pc + 1;
	case OP_STA: {
		const uint16_t a = pop();
		const uint16_t b = pop();
		mem[a] = b;
		return pc + 1;
	}
	case OP_RED:
		push(getchar());
		return pc + 1;
	case OP_WRT:
		putchar(pop());
		return pc + 1;
	case OP_ADD:
		push(pop() + pop());
		return pc + 1;
	case OP_SUB: {
		const uint16_t b = pop();
		const uint16_t a = pop();
		push(a - b);
		return pc + 1;
	}
	case OP_MUL:
		push(pop() * pop());
		return pc + 1;
	case OP_DIV: {
		const uint16_t b = pop();
		const uint16_t a = pop();
		push(a / b);
		return pc + 1;
	}
	case OP_AND:
		push(pop() & pop());
		return pc + 1;
	case OP_ORA:
		push(pop() | pop());
		return pc + 1;
	case OP_XOR:
		push(pop() ^ pop());
		return pc + 1;
	case OP_LSF: {
		const uint16_t shift = pop();
		const uint16_t a = pop();
		push(a << shift);
		return pc + 1;
	}
	case OP_RSF: {
		const uint16_t shift = pop();
		const uint16_t a = pop();
		push(a >> shift);
		return pc + 1;
	}
	case OP_SLP:
		if (cycle_events(mem + 0xbffe))
			return MEM_SIZE;
		render(mem + 0xbfff);
		return pc + 1;
	case OP_INC:
		push(pop() + 1);
		return pc + 1;
	case OP_DEC:
		push(pop() - 1);
		return pc + 1;
	case OP_DBG:
		fprintf(stderr, "<%04x> ", (unsigned)stack_ptr);
		for (size_t i = 0; i < stack_ptr; i++)
			fprintf(stderr, "%04x ", stack[i]);
		fputc('\n', stderr);
		return pc + 1;
	default:
		fprintf(stderr, "unhandled opcode %04x\n", mem[pc]);
		if (mem[pc] <= OP_SLP)
			fprintf(stderr, "(%s)\n", ops[mem[pc]]);
		exit(fail());
	}
}

static void
exec_data(uint16_t *mem)
{
	long pc = 0;
	do
		pc = exec_op(mem, pc);
	while (pc < MEM_SIZE);
}

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();
	}

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		fprintf(stderr, "%s\n", SDL_GetError());
		return fail();
	}
	if (SDL_CreateWindowAndRenderer(384, 384, SDL_WINDOW_RESIZABLE,
	                                &window, &renderer) < 0) {
		fprintf(stderr, "%s\n", SDL_GetError());
		return fail();
	}
	target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888,
	                           SDL_TEXTUREACCESS_TARGET, 128, 128);
	if (target == NULL) {
		fprintf(stderr, "%s\n", SDL_GetError());
		return fail();
	}
	next_time = SDL_GetTicks64();

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