summaryrefslogtreecommitdiff
path: root/platform-sdl.c
blob: 5b4e1bdfa50b4c0b00bbf9078be7558fc2c6ed27 (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
// Copyright 2008,2009  Segher Boessenkool  <segher@kernel.crashing.org>
// Licensed under the terms of the GNU GPL, version 2
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <SDL.h>

#include "types.h"
#include "emu.h"
#include "video.h"
#include "audio.h"

#include "platform.h"


#define SIZE_3X3


static void *rom_file;

void open_rom(const char *path)
{
	rom_file = fopen(path, "rb");
	if (!rom_file)
		fatal("Cannot read ROM file %s\n", path);
}

void read_rom(u32 offset)
{
	u32 n;

	fseek(rom_file, 2*(offset + 0x4000), SEEK_SET);
	n = fread(mem + 0x4000, 2, N_MEM - 0x4000, rom_file);

// gross, but whatever.  one day i'll fix this, but not today
#ifdef _BIG_ENDIAN
	for (u32 i = 0x4000; i < n; i++)
		mem[i] = (mem[i] << 8) | (mem[i] >> 8);
#endif
}


void *open_eeprom(const char *name, u8 *data, u32 len)
{
	char path[256];
	char *home;
	FILE *fp;

	memset(data, 0, len);

	home = getenv("HOME");
	if (!home)
		fatal("cannot find HOME\n");
	snprintf(path, sizeof path, "%s/.unununium", home);
	mkdir(path, 0777);
	snprintf(path, sizeof path, "%s/.unununium/%s.eeprom", home, name);

	fp = fopen(path, "rb");
	if (fp) {
		fread(data, 1, len, fp);
		if (ferror(fp))
			fatal("error reading EEPROM file %s\n", path);
		fclose(fp);
	}

	fp = fopen(path, "wb");
	if (!fp)
		fatal("cannot open EEPROM file %s\n", path);

	fwrite(data, 1, len, fp);

	return fp;
}

void save_eeprom(void *cookie, u8 *data, u32 len)
{
	FILE *fp = cookie;

	rewind(fp);

	fwrite(data, 1, len, fp);
}


static SDL_Surface *sdl_surface;

void update_screen(void)
{
	blit_screen();

	if (SDL_MUSTLOCK(sdl_surface))
		if (SDL_LockSurface(sdl_surface) < 0)
			fatal("oh crap\n");

	u32 x, y;
	for (y = 0; y < 240; y++) {
#ifdef SIZE_3X3
		u32 *p = sdl_surface->pixels + 3*y*sdl_surface->pitch;
		u32 *p2 = sdl_surface->pixels + (3*y+1)*sdl_surface->pitch;
		u32 *p3 = sdl_surface->pixels + (3*y+2)*sdl_surface->pitch;
		u8 *s_r = screen_r + 320*y;
		u8 *s_g = screen_g + 320*y;
		u8 *s_b = screen_b + 320*y;

		for (x = 0; x < 320; x++) {
			u32 c = SDL_MapRGB(sdl_surface->format, *s_r++, *s_g++, *s_b++);

			p[0] = c;
			p[1] = c;
			p[2] = c;
			p += 3;

			p2[0] = c;
			p2[1] = c;
			p2[2] = c;
			p2 += 3;

			p3[0] = c;
			p3[1] = c;
			p3[2] = c;
			p3 += 3;
		}
#else
		u32 *p = sdl_surface->pixels + 2*y*sdl_surface->pitch;
		u32 *p2 = sdl_surface->pixels + (2*y+1)*sdl_surface->pitch;
		u8 *s = screen + 320*y;

		for (x = 0; x < 320; x += 4) {
			u32 c0 = s[0];
			u32 c1 = s[1];
			u32 c2 = s[2];
			u32 c3 = s[3];
			c0 = palette[c0];
			c1 = palette[c1];
			c2 = palette[c2];
			c3 = palette[c3];
			s += 4;

			p[0] = c0;
			p[1] = c0;
			p[2] = c1;
			p[3] = c1;
			p[4] = c2;
			p[5] = c2;
			p[6] = c3;
			p[7] = c3;
			p += 8;

			p2[0] = c0;
			p2[1] = c0;
			p2[2] = c1;
			p2[3] = c1;
			p2[4] = c2;
			p2[5] = c2;
			p2[6] = c3;
			p2[7] = c3;
			p2 += 8;
		}
#endif
	}

	if (SDL_MUSTLOCK(sdl_surface))
		SDL_UnlockSurface(sdl_surface);

	SDL_UpdateRect(sdl_surface, 0, 0, 0, 0);
}


u8 controller_input[8];
u8 controller_output[7];
int controller_should_be_rotated;

u8 button_up;
u8 button_down;
u8 button_left;
u8 button_right;
u8 button_A;
u8 button_B;
u8 button_C;
u8 button_menu;

static char handle_debug_key(int key)
{
	switch (key) {
	case SDLK_ESCAPE:
		return 0x1b;
	case '0' ... '8':
	case 't':
	case 'y':
	case 'u':
	case 'x':
	case 'q':
	case 'a': case 's': case 'd':
	case 'v': case 'c':
		return (key);
	}

	return 0;
}

static void handle_controller_key(int key, int down)
{
	u8 bit;

	switch (key) {
	case 'j':
		button_up = down;
		break;
	case 'm':
		button_down = down;
		break;
	case 'n':
		button_left = down;
		break;
	case ',':
		button_right = down;
		break;
	case 'h':
		button_A = down;
		break;
	case 'k':
		button_B = down;
		break;
	case 'g':
		button_C = down;
		break;
	case 'l':
		button_menu = down;
		break;

	case SDLK_UP:
		bit = controller_should_be_rotated ? 0x08 : 0x01;
		break;
	case SDLK_DOWN:
		bit = controller_should_be_rotated ? 0x04 : 0x02;
		break;
	case SDLK_LEFT:
		bit = controller_should_be_rotated ? 0x01 : 0x04;
		break;
	case SDLK_RIGHT:
		bit = controller_should_be_rotated ? 0x02 : 0x08;
		break;
	case SDLK_SPACE:	// "A" button
		bit = 0x10;
		break;
	case 'b':		// "B" button
		bit = 0x20;
		break;
	default:
		return;
	}

	if (down)
		controller_input[0] |= bit;
	else
		controller_input[0] &= ~bit;

	u32 x = random() & 0x3ff;
	u32 y = random() & 0x3ff;
	u32 z = random() & 0x3ff;

	controller_input[1] = x;
	controller_input[2] = y;
	controller_input[3] = z;

	x >>= 8;
	y >>= 8;
	z >>= 8;

	controller_input[5] = (z << 4) | (y << 2) | x;

	controller_input[4] = 0;
	controller_input[6] = 0xff;
	controller_input[7] = 0;
}

char update_controller(void)
{
	SDL_Event event;
	char key;

	while (SDL_PollEvent(&event)) {

		switch (event.type) {
		case SDL_KEYDOWN:
			key = handle_debug_key(event.key.keysym.sym);
			if (key)
				return (key);

			// Fallthrough.
		case SDL_KEYUP:
			handle_controller_key(event.key.keysym.sym,
			                      (event.type == SDL_KEYDOWN));
			break;

		case SDL_QUIT:
			return 0x1b;

		case SDL_ACTIVEEVENT:
		case SDL_MOUSEMOTION:
		case SDL_MOUSEBUTTONDOWN:
		case SDL_MOUSEBUTTONUP:
			continue;

		default:
			fprintf(stderr, "Unknown SDL event type %d\n", event.type);
			continue;
		}
	}

	return 0;
}

void warn(const char *format, ...)
{
	va_list ap;
	va_start(ap, format);
	vfprintf(stderr, format, ap);
}

void fatal(const char *format, ...)
{
	va_list ap;
	va_start(ap, format);
	vfprintf(stderr, format, ap);
	exit(1);
}

static void mix(void *cookie, u8 *data, int n)
{
	if (mute_audio) {
		memset(data, 0, n);
		return;
	}

	audio_render((s16 *)data, n/2);
}

void platform_init(void)
{
	if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0)
		fatal("Unable to init SDL: %s\n", SDL_GetError());
	atexit(SDL_Quit);

	SDL_WM_SetCaption("Unununium", 0);

#ifdef SIZE_3X3
	sdl_surface = SDL_SetVideoMode(960, 720, 32, SDL_SWSURFACE);
#else
	sdl_surface = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
#endif
	if (!sdl_surface)
		fatal("Unable to initialise video: %s\n", SDL_GetError());

	SDL_AudioSpec spec = {
		.freq = 44100,
		.format = AUDIO_S16SYS,
		.channels = 1,
		.samples = 1024,
		.callback = mix,
		.userdata = 0
	}, actual;
	int ret = SDL_OpenAudio(&spec, &actual);
	printf("ret: %d (%s)\n", ret, SDL_GetError());
printf("--> fr=%d ch=%d sam=%d size=%d sil=x'%04x\n", actual.freq, actual.channels, actual.samples, actual.size, actual.silence);
	SDL_PauseAudio(0);

	// First SDL input does a lot of init, do it now.
	update_controller();
}