3 * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
25 #include "../libsdl/sdl.h"
26 #include "../libsdl/opengl.h"
27 #include "../libsdl/print.h"
28 #include "../libcpu/m68k.h"
29 #include "../libcpu/execute.h"
30 #include "../libvideo/video.h"
31 #include "../libsound/sound.h"
32 #include "../libjoystick/joystick.h"
33 #include "../libkeyboard/keyboard.h"
34 #include "../libdisk/disk.h"
35 #include "mercenary.h"
37 static int config_amiga_speed = 1;
38 static const char *config_gamesave_dir = ".mercenary";
39 static int config_video_filter = 1;
40 static int config_audio_filter = 1;
42 #define CPU_SPEED 7093790.0;
44 #define SOUND_SAMPLERATE 48000
46 * - must be used for sound rendering chunk
49 /* numbe of buffer chunks to dejitter:
50 * - SDL render interval
51 * - sound rendering interval
52 * - sound card interval
53 * 4 should be minimum, if video rate is >=50 Hz
55 #define SOUND_CHUNKS 4
57 /* test sound to check if buffer does not overflow / underrun */
58 //#define DEBUG_SOUND_BUFFERING
60 #define IOSIZE 0x1000 /* bytes in io space */
61 #define MEMORY_SIZE 0x80000
62 static uint8_t *memory = NULL;
63 static uint8_t *image = NULL;
64 #define SCREEN_WIDTH (320*3)
65 #define SCREEN_HEIGHT (200*3)
66 #define IMAGE_WIDTH 320
67 #define IMAGE_HEIGHT 200
68 #define BENSON_AT_LINE 136
69 #define IMAGE_DIWSTART 0x2c
70 static uint16_t *chipreg = NULL;
71 static float *sound_buffer = NULL; /* sound buffer memory */
72 static int sound_buffer_size; /* buffer sample size */
73 static int sound_buffer_writep; /* write pointer at buffer */
74 static int sound_buffer_readp; /* read pointer at buffer */
75 static int double_size = 1; /* render in double size, so each pixle is 2*2 pixles wide */
77 static const char *home_dir;
81 int parse_args(int argc, char *argv[])
86 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
87 print_info("Usage: %s\n", argv[0]);
88 print_info(" -s --amiga-speed original | full\n");
89 print_info(" Set speed of rendering to original Amiga or full speed.\n");
90 print_info(" -v --video-filter on | off\n");
91 print_info(" Set video filter.\n");
92 print_info(" -a --audio-filter on | off\n");
93 print_info(" Set audio filter.\n");
96 if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--amiga-speed")) {
100 print_info("Missing parameter, use '--help'!\n");
103 if (!strcmp(argv[i], "original"))
104 config_amiga_speed = 1;
106 if (!strcmp(argv[i], "full"))
107 config_amiga_speed = 0;
110 print_info("Illegal parameter, use '--help'!\n");
114 if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--video-filter")) {
117 goto missing_parameter;
118 if (!strcmp(argv[i], "on"))
119 config_video_filter = 1;
121 if (!strcmp(argv[i], "off"))
122 config_video_filter = 0;
124 goto illegal_parameter;
126 if (!strcmp(argv[i], "-a") || !strcmp(argv[i], "--audio-filter")) {
129 goto missing_parameter;
130 if (!strcmp(argv[i], "on"))
131 config_audio_filter = 1;
133 if (!strcmp(argv[i], "off"))
134 config_audio_filter = 0;
136 goto illegal_parameter;
138 print_info("Illegal option '%s', use '--help'!\n", argv[i]);
147 static void special_event(int event)
151 static void main_loop(void)
153 int had_first_irq = 0;
154 static uint32_t current_time, last_time = 0, diff;
157 int cycle_count, event;
158 double render_delay = 0.0;
159 uint32_t palette_address;
160 uint16_t palette[16];
162 last_time = SDL_GetTicks();
164 /* render result on window */
166 /* STEP 1: let the CPU render/process the game */
167 /* don't render if we still delay */
169 /* execute until the rendered image is ready (wait for VBL) */
172 cycle_count += execute_cpu(0, mercenary_stop_at, &event);
173 /* handle special events */
174 if (event != STOP_AT_WAIT_VBL)
175 special_event(event);
176 } while (event != STOP_AT_WAIT_VBL);
178 palette_address = mercenary_palette();
179 for (i = 0; i < 16; i++)
180 palette[i] = m68k_read_memory_16(palette_address + i*2);
181 /* for amiga speed: set delay by the number of cycles */
182 if (config_amiga_speed)
183 render_delay = (double)cycle_count / CPU_SPEED;
186 /* STEP 2: render image in memory via OpenGL */
188 /* render game view without benson
189 * because benson is not updated before VBL IRQ, we don't want old image from double buffer
191 emul_video(image, memory, palette, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_DIWSTART, chipreg, 0, BENSON_AT_LINE, double_size);
196 render_opengl(image, config_video_filter);
199 /* STEP 3: execute interrupt at rate of 50Hz, render sound */
200 current_time = SDL_GetTicks();
201 diff = current_time - last_time;
202 /* in case of timer glitch, execute IRQ only by maximum number of SOUND_CHUNKS */
203 if (diff > 1000 * SOUND_CHUNKS / IRQ_RATE) {
204 diff = 1000 * SOUND_CHUNKS / IRQ_RATE;
205 last_time = current_time - 1000 * SOUND_CHUNKS / IRQ_RATE;
207 while (diff > 1000 / IRQ_RATE) {
208 execute_cpu(3, NULL, NULL);
209 execute_cpu(4, NULL, NULL);
211 /* render benson without game view
212 * because we only got benson refreshed during VBL IRQ
214 emul_video(image, memory, palette, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_DIWSTART, chipreg, BENSON_AT_LINE, IMAGE_HEIGHT, double_size);
215 /* render sound to sound buffer
216 * buffer pointer read and write is atomic, so no locking required!
218 space = (sound_buffer_readp - sound_buffer_writep - 1 + sound_buffer_size) % sound_buffer_size;
219 if (space < SOUND_SAMPLERATE / IRQ_RATE) {
220 #ifdef DEBUG_SOUND_BUFFERING
221 fprintf(stderr, "sound buffer overflow\n");
225 length = SOUND_SAMPLERATE / IRQ_RATE;
226 #ifdef DEBUG_SOUND_BUFFERING
227 printf("can write %d, write %d\n", space, length);
230 for (s = 0; s < length; s++) {
231 sound_buffer[(sound_buffer_writep + s) % sound_buffer_size]
232 = sin(2 * M_PI * 999 * cnt / SOUND_SAMPLERATE)
233 * sin(2 * M_PI * 21 * cnt / SOUND_SAMPLERATE)
234 * sin(2 * M_PI * 0.5 * cnt / SOUND_SAMPLERATE);
238 render_sound(memory, sound_buffer, sound_buffer_size, sound_buffer_writep, length, config_audio_filter);
240 sound_buffer_writep = (sound_buffer_writep + length) % sound_buffer_size;
241 diff -= 1000 / IRQ_RATE;
242 last_time += 1000 / IRQ_RATE;
244 /* count down render delay */
246 render_delay -= 1.0 / (double)IRQ_RATE;
247 if (render_delay < 0.0)
254 static uint16_t io_read(uint32_t address)
256 uint16_t value = 0xffff;
258 /* joystick and fire button */
259 if (address == 0xbfe000 || address == 0xdff00c)
260 value &= emulate_joystick_read(address);
262 if (address == 0xbfec00 || address == 0xbfed00 || address == 0xbfee00)
263 value &= emulate_keyboard_read(address);
265 if (address == 0xbfd100 || address == 0xbfe000 || address == 0xdff01a || address == 0xdff01e)
266 value &= emulate_disk_read(address);
271 static void io_write(uint32_t address, uint16_t value)
273 /* dmacon and sound registers */
274 if (address == 0xdff096 || (address >= 0xdff0a0 && address <= 0xdff0df))
275 emulate_sound_write(address, value, SOUND_SAMPLERATE);
276 if (address == 0xbfd100 || (address >= 0xdff020 && address <= 0xdff024))
277 emulate_disk_write(address, value);
280 /* two tracks with 0x1820 words of length */
281 static uint8_t game_save[2][0x1820 << 1];
282 static int last_track = 0;
284 /* game reads track with saved game */
285 static void disk_read(int track, int __attribute__((unused)) side, uint32_t data, uint16_t length)
287 if (length > sizeof(game_save[0])) {
288 print_error("loading game state failed, because length exceeds game save data size, please fix!\n");
292 /* if even track is selected, load game */
295 int gamesave_num = (track - 2) >> 1;
299 memset(game_save, 0, sizeof(game_save)); /* clear so make the game fail, if we fail */
303 sprintf(filename, "%s/%s/", home_dir, config_gamesave_dir);
304 mkdir(filename, 0777);
306 sprintf(filename + strlen(filename), "%d%s", gamesave_num, mercenary_gamesavesuffix);
307 fp = fopen(filename, "r");
310 print_error("failed to load game from '%s'\n", filename);
313 got = fread(game_save, sizeof(game_save[0]), 2, fp);
321 memcpy(memory + data, game_save[track & 1], length /* sizeof(game_save[0])*/);
324 /* game writes track with saved game */
325 static void disk_write(int track, int __attribute__((unused)) side, uint32_t data, uint16_t length)
327 /* skip sync info that is provided by game and only relevant for a real disk track */
331 if (length != sizeof(game_save[0])) {
332 print_error("saving game state failed, because length of data does not match, please fix!\n");
336 /* don't save if last track is the same, because disk is written on both sides with the same data */
337 if (track == last_track) {
338 if (memcmp(memory + data, game_save[track & 1], length)) {
339 print_error("saving game data on other side of the disk is different, please fix!\n");
346 memcpy(game_save[track & 1], memory + data, length);
348 /* if done with saving */
351 int gamesave_num = (track - 2) >> 1;
358 sprintf(filename, "%s/%s/", home_dir, config_gamesave_dir);
359 mkdir(filename, 0777);
361 sprintf(filename + strlen(filename), "%d%s", gamesave_num, mercenary_gamesavesuffix);
362 fp = fopen(filename, "w");
365 print_error("failed to save game to '%s'\n", filename);
368 print_info("Game state saved to '%s'\n", filename);
369 wrote = fwrite(game_save, sizeof(game_save[0]), 2, fp);
376 static int shift = 0, ctrl = 0;
378 static void keyboard_sdl(int down, SDL_Keycode sym)
387 config_video_filter ^= 1;
388 print_info("video filter: %s\n", (config_video_filter) ? "on" : "off");
393 config_audio_filter ^= 1;
394 print_info("audio filter: %s\n", (config_audio_filter) ? "on" : "off");
399 config_amiga_speed ^= 1;
400 print_info("amiga speed: %s\n", (config_amiga_speed) ? "original" : "full");
411 set_key("LSH", down);
415 set_key("RSH", down);
421 set_joystick(-1, -1, -1, -1, -1);
425 set_joystick(down, -1, -1, -1, -1);
430 set_joystick(-1, -1, -1, -1, -1);
434 set_joystick(-1, down, -1, -1, -1);
439 set_joystick(-1, -1, -1, -1, -1);
443 set_joystick(-1, -1, down, -1, -1);
448 set_joystick(-1, -1, -1, -1, -1);
452 set_joystick(-1, -1, -1, down, -1);
455 set_joystick(-1, -1, -1, -1, down);
458 case SDLK_a: set_key("A", down); break;
459 case SDLK_b: set_key("B", down); break;
460 case SDLK_c: set_key("C", down); break;
461 case SDLK_d: set_key("D", down); break;
462 case SDLK_e: set_key("E", down); break;
463 case SDLK_f: set_key("F", down); break;
464 case SDLK_g: set_key("G", down); break;
465 case SDLK_h: set_key("H", down); break;
466 case SDLK_i: set_key("I", down); break;
467 case SDLK_j: set_key("J", down); break;
468 case SDLK_k: set_key("K", down); break;
469 case SDLK_l: set_key("L", down); break;
470 case SDLK_m: set_key("M", down); break;
471 case SDLK_n: set_key("N", down); break;
472 case SDLK_o: set_key("O", down); break;
473 case SDLK_p: set_key("P", down); break;
474 case SDLK_q: set_key("Q", down); break;
475 case SDLK_r: set_key("R", down); break;
476 case SDLK_s: set_key("S", down); break;
477 case SDLK_t: set_key("T", down); break;
478 case SDLK_u: set_key("U", down); break;
479 case SDLK_v: set_key("V", down); break;
480 case SDLK_w: set_key("W", down); break;
481 case SDLK_x: set_key("X", down); break;
482 case SDLK_y: set_key("Y", down); break;
483 case SDLK_z: set_key("Z", down); break;
485 case SDLK_0: set_key("0", down); break;
486 case SDLK_1: set_key("1", down); break;
487 case SDLK_2: set_key("2", down); break;
488 case SDLK_3: set_key("3", down); break;
489 case SDLK_4: set_key("4", down); break;
490 case SDLK_5: set_key("5", down); break;
491 case SDLK_6: set_key("6", down); break;
492 case SDLK_7: set_key("7", down); break;
493 case SDLK_8: set_key("8", down); break;
494 case SDLK_9: set_key("9", down); break;
496 case SDLK_KP_0: set_key("NP0", down); break;
497 case SDLK_KP_1: set_key("NP1", down); break;
498 case SDLK_KP_2: set_key("NP2", down); break;
499 case SDLK_KP_3: set_key("NP3", down); break;
500 case SDLK_KP_4: set_key("NP4", down); break;
501 case SDLK_KP_5: set_key("NP5", down); break;
502 case SDLK_KP_6: set_key("NP6", down); break;
503 case SDLK_KP_7: set_key("NP7", down); break;
504 case SDLK_KP_8: set_key("NP8", down); break;
505 case SDLK_KP_9: set_key("NP9", down); break;
506 case SDLK_KP_DIVIDE: set_key("NPDIV", down); break;
507 case SDLK_KP_MULTIPLY: set_key("NPMUL", down); break;
508 case SDLK_KP_MINUS: set_key("NPSUB", down); break;
509 case SDLK_KP_PLUS: set_key("NPADD", down); break;
510 case SDLK_KP_PERIOD: set_key("NPDEL", down); break;
511 // NPLPAREN and NPRPAREN are not emulated
513 case SDLK_F1: set_key("F1", down); break;
514 case SDLK_F2: set_key("F2", down); break;
515 case SDLK_F3: set_key("F3", down); break;
516 case SDLK_F4: set_key("F4", down); break;
517 case SDLK_F5: set_key("F5", down); break;
518 case SDLK_F6: set_key("F6", down); break;
519 case SDLK_F7: set_key("F7", down); break;
520 case SDLK_F8: set_key("F8", down); break;
521 case SDLK_F9: set_key("F9", down); break;
522 case SDLK_F10: set_key("F10", down); break;
524 case SDLK_SPACE: set_key("SPC", down); break;
525 case SDLK_BACKSPACE: set_key("BS", down); break;
526 case SDLK_TAB: set_key("TAB", down); break;
527 case SDLK_KP_ENTER: set_key("ENT", down); break;
528 case SDLK_RETURN: set_key("RET", down); break;
529 case SDLK_ESCAPE: set_key("ESC", down); break;
530 case SDLK_DELETE: set_key("DEL", down); break;
531 case SDLK_INSERT: set_key("HELP", down); break;
536 void audio_sdl(float *data, int length)
540 /* read sound from sound buffer
541 * buffer pointer read and write is atomic, so no locking required!
543 fill = (sound_buffer_writep - sound_buffer_readp + sound_buffer_size) % sound_buffer_size;
545 #ifdef DEBUG_SOUND_BUFFERING
546 fprintf(stderr, "sound buffer underrun\n");
548 /* correct read pointer as if the buffer would have 'length' of samples stored inside */
549 sound_buffer_readp = (sound_buffer_readp + fill - length + sound_buffer_size) % sound_buffer_size;
551 for (s = 0; s < length; s++)
552 *data++ = sound_buffer[(sound_buffer_readp + s) % sound_buffer_size];
553 #ifdef DEBUG_SOUND_BUFFERING
554 printf("fill %d = %.4f\n", length, sound_buffer[sound_buffer_readp]);
556 sound_buffer_readp =(sound_buffer_readp + length) % sound_buffer_size;
559 int main(int argc, char *argv[])
564 home_dir = getenv("HOME");
568 rc = parse_args(argc, argv);
573 image = calloc(IMAGE_WIDTH * IMAGE_HEIGHT * ((double_size) ? 4 : 1), 3);
575 print_error("Failed to allocate image buffer\n");
579 if ((SOUND_SAMPLERATE % IRQ_RATE)) {
580 print_error("Sample rate must be a multiple of IRQ rate, please fix!\n");
583 if ((1000 % IRQ_RATE)) {
584 print_error("1000 (Ticks per second) rate must be a multiple of IRQ rate, please fix!\n");
588 /* calculate SDL chunk size for audio
589 * it must be a power of two, but not more than the chunk size for each IRQ!
591 for (sdl_sound_chunk = 2; sdl_sound_chunk <= (SOUND_SAMPLERATE / IRQ_RATE); sdl_sound_chunk <<= 1)
593 sdl_sound_chunk >>= 1;
594 // printf("samples per IRQ = %d, samples per SDL audio = %d\n", SOUND_SAMPLERATE / IRQ_RATE, sdl_sound_chunk); exit(0);
596 /* allocate sound buffers */
597 sound_buffer_size = SOUND_SAMPLERATE / IRQ_RATE * SOUND_CHUNKS;
598 sound_buffer = calloc(sound_buffer_size, sizeof(*sound_buffer));
600 print_error("Failed to allocate image buffer\n");
604 /* allocate memory */
605 memory = calloc(MEMORY_SIZE, 1);
607 print_error("Failed to allocate cpu's memory\n");
610 chipreg = calloc(IOSIZE, 1);
612 print_error("Failed to allocate chip register\n");
617 execute_init(MEMORY_SIZE, memory, chipreg, io_read, io_write);
619 /* init disk emulation */
620 disk_init(disk_read, disk_write);
625 /* patch some stuff */
628 /* init SDL and OpenGL */
629 rc = init_sdl(argv[0], SCREEN_WIDTH, SCREEN_HEIGHT, SOUND_SAMPLERATE, sdl_sound_chunk, keyboard_sdl, audio_sdl);
632 rc = init_opengl((double_size) ? IMAGE_WIDTH * 2 : IMAGE_WIDTH, (double_size) ? IMAGE_HEIGHT * 2 : IMAGE_HEIGHT);
635 resize_opengl(SCREEN_WIDTH, SCREEN_HEIGHT);
637 /* init audio filter */
638 sound_init_filter(SOUND_SAMPLERATE);
643 print_info("**********************************\n");
644 print_info("* Welcome to Mercenary Reloaded! *\n");
645 print_info("**********************************\n\n");
646 print_info("Press CTRL + cursor keys to select inventory or pickup/drop item.\n");
647 print_info("Press CTRL + f to toggle full screen.\n");
648 print_info("Press CTRL + s to toggle rendering speed.\n");
649 print_info("Press CTRL + v to toggle video filter.\n");
650 print_info("Press CTRL + a to toggle audio filter.\n");
651 print_info("Press CTRL + c to exit game.\n\n");
652 print_info("Use '--help' as command line option for configuration settings.\n\n");