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"
38 static int config_amiga_speed = 1;
39 static const char *config_gamesave_dir = ".mercenary";
40 static int config_video_filter = 1;
41 static int config_audio_filter = 1;
42 static int config_render = 0;
43 static int config_skip_intro = 0;
44 static int config_debug_opengl = 0;
46 #define CPU_SPEED 7093790.0;
48 #define SOUND_SAMPLERATE 48000
50 * - must be used for sound rendering chunk
53 /* numbe of buffer chunks to dejitter:
54 * - SDL render interval
55 * - sound rendering interval
56 * - sound card interval
57 * 4 should be minimum, if video rate is >=50 Hz
59 #define SOUND_CHUNKS 4
61 /* test sound to check if buffer does not overflow / underrun */
62 //#define DEBUG_SOUND_BUFFERING
64 #define IOSIZE 0x1000 /* bytes in io space */
65 #define MEMORY_SIZE 0x80000
66 static uint8_t *memory = NULL;
67 static uint8_t *stop_event = NULL;
68 static uint8_t *image = NULL;
69 #define SCREEN_WIDTH 320
70 #define SCREEN_HEIGHT 200
71 #define IMAGE_WIDTH 320
72 #define IMAGE_HEIGHT 200
73 #define BENSON_AT_LINE 136
74 #define IMAGE_DIWSTART 0x2c
75 static uint16_t *chipreg = NULL;
76 static stereo_t *sound_buffer = NULL; /* sound buffer memory */
77 static int sound_buffer_size; /* buffer sample size */
78 static int sound_buffer_writep; /* write pointer at buffer */
79 static int sound_buffer_readp; /* read pointer at buffer */
80 static int double_size = 1; /* render in double size, so each pixle is 2*2 pixles wide */
81 static int render_legacy = 0; /* render original amiga screen, if set */
82 static int render_improved = 0; /* render improved image, if set */
83 static int render_debug = 0; /* render both, amiga screen and improved image, if set */
85 static const char *home_dir;
89 int parse_args(int argc, char *argv[])
94 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
95 print_info("Usage: %s\n", argv[0]);
96 print_info(" -s --amiga-speed original | full\n");
97 print_info(" Set speed of rendering to original Amiga or full speed.\n");
98 print_info(" -v --video-filter on | off\n");
99 print_info(" Set video filter.\n");
100 print_info(" -a --audio-filter on | off\n");
101 print_info(" Set audio filter.\n");
102 print_info(" -r --render original | opegl\n");
103 print_info(" Set speed of rendering to original Amiga or full speed.\n");
104 print_info(" -i --skip-intro\n");
105 print_info(" Skip intro sequence approaching to Eris space port.\n");
106 print_info("Debug options:\n");
107 print_info(" -o --debug-opengl\n");
108 print_info(" Use split screen to debug opengl rendering vs. amiga rendering.\n");
111 if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--amiga-speed")) {
115 print_info("Missing parameter, use '--help'!\n");
118 if (!strcmp(argv[i], "original"))
119 config_amiga_speed = 1;
121 if (!strcmp(argv[i], "full"))
122 config_amiga_speed = 0;
125 print_info("Illegal parameter, use '--help'!\n");
129 if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--video-filter")) {
132 goto missing_parameter;
133 if (!strcmp(argv[i], "on"))
134 config_video_filter = 1;
136 if (!strcmp(argv[i], "off"))
137 config_video_filter = 0;
139 goto illegal_parameter;
141 if (!strcmp(argv[i], "-a") || !strcmp(argv[i], "--audio-filter")) {
144 goto missing_parameter;
145 if (!strcmp(argv[i], "on"))
146 config_audio_filter = 1;
148 if (!strcmp(argv[i], "off"))
149 config_audio_filter = 0;
151 goto illegal_parameter;
153 if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--render")) {
156 goto missing_parameter;
157 if (!strcmp(argv[i], "original"))
160 if (!strcmp(argv[i], "opengl"))
163 goto illegal_parameter;
165 if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "--skip-intro")) {
166 config_skip_intro = 1;
168 if (!strcmp(argv[i], "-o") || !strcmp(argv[i], "--debug-opengl")) {
169 config_debug_opengl = 1;
171 print_info("Illegal option '%s', use '--help'!\n", argv[i]);
180 static void special_event(int event)
183 render_improved_event(event);
186 static void main_loop(void)
188 int had_first_irq = 0;
189 static uint32_t current_time, last_time = 0, diff;
192 int cycle_count, event;
193 double render_delay = 0.0;
194 uint32_t palette_address;
195 uint16_t palette[16];
197 last_time = SDL_GetTicks();
199 /* render result on window */
201 /* handle SDL events */
209 /* initialize rendering */
210 render_debug = config_debug_opengl;
211 render_legacy = (!config_render) || render_debug;
212 render_improved = config_render || render_debug;
213 /* start rendering for improved graphics */
215 opengl_viewport_improved(render_debug, (double_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE);
217 /* STEP 1: let the CPU render/process the game */
218 /* don't render if we still delay */
220 /* execute until the rendered image is ready (wait for VBL) */
223 cycle_count += execute_cpu(0, &event);
224 /* handle special events */
225 if (event != STOP_AT_WAIT_VBL)
226 special_event(event);
227 } while (event != STOP_AT_WAIT_VBL);
229 palette_address = mercenary_palette();
230 for (i = 0; i < 16; i++)
231 palette[i] = m68k_read_memory_16(palette_address + i*2);
232 /* for amiga speed: set delay by the number of cycles */
233 if (config_amiga_speed)
234 render_delay = (double)cycle_count / CPU_SPEED;
237 /* STEP 2: transfer legacy image in memory to OpenGL texture */
239 /* render game view without benson
240 * because benson is not updated before VBL IRQ, we don't want old image from double buffer
243 emul_video(image, memory, palette, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_DIWSTART, chipreg, 0, BENSON_AT_LINE, double_size);
245 /* render benson on improved rendering, if enabled */
247 opengl_render_benson(image, config_video_filter, (double_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE);
248 /* setup viewport for legacy image and render image, if enabled */
250 opengl_viewport_legacy(render_debug);
251 opengl_render_legacy(image, config_video_filter);
255 /* STEP 3: execute interrupt at rate of 50Hz, render sound */
256 current_time = SDL_GetTicks();
257 diff = current_time - last_time;
258 /* in case of timer glitch, execute IRQ only by maximum number of SOUND_CHUNKS */
259 if (diff > 1000 * SOUND_CHUNKS / IRQ_RATE) {
260 diff = 1000 * SOUND_CHUNKS / IRQ_RATE;
261 last_time = current_time - 1000 * SOUND_CHUNKS / IRQ_RATE;
263 while (diff > 1000 / IRQ_RATE) {
264 execute_cpu(3, NULL);
265 execute_cpu(4, NULL);
267 /* render benson without game view
268 * because we only got benson refreshed during VBL IRQ
270 emul_video(image, memory, palette, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_DIWSTART, chipreg, BENSON_AT_LINE, IMAGE_HEIGHT, double_size);
271 /* render sound to sound buffer
272 * buffer pointer read and write is atomic, so no locking required!
274 space = (sound_buffer_readp - sound_buffer_writep - 1 + sound_buffer_size) % sound_buffer_size;
275 if (space < SOUND_SAMPLERATE / IRQ_RATE) {
276 #ifdef DEBUG_SOUND_BUFFERING
277 fprintf(stderr, "sound buffer overflow\n");
281 length = SOUND_SAMPLERATE / IRQ_RATE;
282 #ifdef DEBUG_SOUND_BUFFERING
283 printf("can write %d, write %d\n", space, length);
286 for (s = 0; s < length; s++) {
287 sound_buffer[(sound_buffer_writep + s) % sound_buffer_size].left =
288 sound_buffer[(sound_buffer_writep + s) % sound_buffer_size].right
289 = sin(2 * M_PI * 999 * cnt / SOUND_SAMPLERATE)
290 * sin(2 * M_PI * 21 * cnt / SOUND_SAMPLERATE)
291 * sin(2 * M_PI * 0.5 * cnt / SOUND_SAMPLERATE);
295 render_sound(memory, sound_buffer, sound_buffer_size, sound_buffer_writep, length, config_audio_filter);
297 sound_buffer_writep = (sound_buffer_writep + length) % sound_buffer_size;
298 diff -= 1000 / IRQ_RATE;
299 last_time += 1000 / IRQ_RATE;
301 /* count down render delay */
303 render_delay -= 1.0 / (double)IRQ_RATE;
304 if (render_delay < 0.0)
311 static uint16_t io_read(uint32_t address)
313 uint16_t value = 0xffff;
315 /* joystick and fire button */
316 if (address == 0xbfe000 || address == 0xdff00c)
317 value &= emulate_joystick_read(address);
319 if (address == 0xbfec00 || address == 0xbfed00 || address == 0xbfee00)
320 value &= emulate_keyboard_read(address);
322 if (address == 0xbfd100 || address == 0xbfe000 || address == 0xdff01a || address == 0xdff01e)
323 value &= emulate_disk_read(address);
328 static void io_write(uint32_t address, uint16_t value)
330 /* dmacon and sound registers */
331 if (address == 0xdff096 || (address >= 0xdff0a0 && address <= 0xdff0df))
332 emulate_sound_write(address, value, SOUND_SAMPLERATE);
333 if (address == 0xbfd100 || (address >= 0xdff020 && address <= 0xdff024))
334 emulate_disk_write(address, value);
337 /* two tracks with 0x1820 words of length */
338 static uint8_t game_save[2][0x1820 << 1];
339 static int last_track = 0;
341 /* game reads track with saved game */
342 static void disk_read(int track, int __attribute__((unused)) side, uint32_t data, uint16_t length)
344 if (length > sizeof(game_save[0])) {
345 print_error("loading game state failed, because length exceeds game save data size, please fix!\n");
349 /* if even track is selected, load game */
352 int gamesave_num = (track - 2) >> 1;
356 memset(game_save, 0, sizeof(game_save)); /* clear so make the game fail, if we fail */
360 sprintf(filename, "%s/%s/", home_dir, config_gamesave_dir);
361 mkdir(filename, 0777);
363 sprintf(filename + strlen(filename), "%d%s", gamesave_num, mercenary_gamesavesuffix);
364 fp = fopen(filename, "r");
367 print_error("failed to load game from '%s'\n", filename);
370 got = fread(game_save, sizeof(game_save[0]), 2, fp);
378 memcpy(memory + data, game_save[track & 1], length /* sizeof(game_save[0])*/);
381 /* game writes track with saved game */
382 static void disk_write(int track, int __attribute__((unused)) side, uint32_t data, uint16_t length)
384 /* skip sync info that is provided by game and only relevant for a real disk track */
388 if (length != sizeof(game_save[0])) {
389 print_error("saving game state failed, because length of data does not match, please fix!\n");
393 /* don't save if last track is the same, because disk is written on both sides with the same data */
394 if (track == last_track) {
395 if (memcmp(memory + data, game_save[track & 1], length)) {
396 print_error("saving game data on other side of the disk is different, please fix!\n");
403 memcpy(game_save[track & 1], memory + data, length);
405 /* if done with saving */
408 int gamesave_num = (track - 2) >> 1;
415 sprintf(filename, "%s/%s/", home_dir, config_gamesave_dir);
416 mkdir(filename, 0777);
418 sprintf(filename + strlen(filename), "%d%s", gamesave_num, mercenary_gamesavesuffix);
419 fp = fopen(filename, "w");
422 print_error("failed to save game to '%s'\n", filename);
425 print_info("Game state saved to '%s'\n", filename);
426 wrote = fwrite(game_save, sizeof(game_save[0]), 2, fp);
433 static int shift = 0, ctrl = 0;
435 static void keyboard_sdl(int down, SDL_Keycode sym)
444 config_video_filter ^= 1;
445 print_info("video filter: %s\n", (config_video_filter) ? "on" : "off");
450 config_audio_filter ^= 1;
451 print_info("audio filter: %s\n", (config_audio_filter) ? "on" : "off");
456 config_amiga_speed ^= 1;
457 print_info("amiga speed: %s\n", (config_amiga_speed) ? "original" : "full");
463 printf("render: %s\n", (config_render) ? "opengl" : "original");
474 set_key("LSH", down);
478 set_key("RSH", down);
484 set_joystick(-1, -1, -1, -1, -1);
488 set_joystick(down, -1, -1, -1, -1);
493 set_joystick(-1, -1, -1, -1, -1);
497 set_joystick(-1, down, -1, -1, -1);
502 set_joystick(-1, -1, -1, -1, -1);
506 set_joystick(-1, -1, down, -1, -1);
511 set_joystick(-1, -1, -1, -1, -1);
515 set_joystick(-1, -1, -1, down, -1);
518 set_joystick(-1, -1, -1, -1, down);
521 case SDLK_a: set_key("A", down); break;
522 case SDLK_b: set_key("B", down); break;
523 case SDLK_c: set_key("C", down); break;
524 case SDLK_d: set_key("D", down); break;
525 case SDLK_e: set_key("E", down); break;
526 case SDLK_f: set_key("F", down); break;
527 case SDLK_g: set_key("G", down); break;
528 case SDLK_h: set_key("H", down); break;
529 case SDLK_i: set_key("I", down); break;
530 case SDLK_j: set_key("J", down); break;
531 case SDLK_k: set_key("K", down); break;
532 case SDLK_l: set_key("L", down); break;
533 case SDLK_m: set_key("M", down); break;
534 case SDLK_n: set_key("N", down); break;
535 case SDLK_o: set_key("O", down); break;
536 case SDLK_p: set_key("P", down); break;
537 case SDLK_q: set_key("Q", down); break;
538 case SDLK_r: set_key("R", down); break;
539 case SDLK_s: set_key("S", down); break;
540 case SDLK_t: set_key("T", down); break;
541 case SDLK_u: set_key("U", down); break;
542 case SDLK_v: set_key("V", down); break;
543 case SDLK_w: set_key("W", down); break;
544 case SDLK_x: set_key("X", down); break;
545 case SDLK_y: set_key("Y", down); break;
546 case SDLK_z: set_key("Z", down); break;
548 case SDLK_0: set_key("0", down); break;
549 case SDLK_1: set_key("1", down); break;
550 case SDLK_2: set_key("2", down); break;
551 case SDLK_3: set_key("3", down); break;
552 case SDLK_4: set_key("4", down); break;
553 case SDLK_5: set_key("5", down); break;
554 case SDLK_6: set_key("6", down); break;
555 case SDLK_7: set_key("7", down); break;
556 case SDLK_8: set_key("8", down); break;
557 case SDLK_9: set_key("9", down); break;
559 case SDLK_KP_0: set_key("NP0", down); break;
560 case SDLK_KP_1: set_key("NP1", down); break;
561 case SDLK_KP_2: set_key("NP2", down); break;
562 case SDLK_KP_3: set_key("NP3", down); break;
563 case SDLK_KP_4: set_key("NP4", down); break;
564 case SDLK_KP_5: set_key("NP5", down); break;
565 case SDLK_KP_6: set_key("NP6", down); break;
566 case SDLK_KP_7: set_key("NP7", down); break;
567 case SDLK_KP_8: set_key("NP8", down); break;
568 case SDLK_KP_9: set_key("NP9", down); break;
569 case SDLK_KP_DIVIDE: set_key("NPDIV", down); break;
570 case SDLK_KP_MULTIPLY: set_key("NPMUL", down); break;
571 case SDLK_KP_MINUS: set_key("NPSUB", down); break;
572 case SDLK_KP_PLUS: set_key("NPADD", down); break;
573 case SDLK_KP_PERIOD: set_key("NPDEL", down); break;
574 // NPLPAREN and NPRPAREN are not emulated
576 case SDLK_F1: set_key("F1", down); break;
577 case SDLK_F2: set_key("F2", down); break;
578 case SDLK_F3: set_key("F3", down); break;
579 case SDLK_F4: set_key("F4", down); break;
580 case SDLK_F5: set_key("F5", down); break;
581 case SDLK_F6: set_key("F6", down); break;
582 case SDLK_F7: set_key("F7", down); break;
583 case SDLK_F8: set_key("F8", down); break;
584 case SDLK_F9: set_key("F9", down); break;
585 case SDLK_F10: set_key("F10", down); break;
587 case SDLK_SPACE: set_key("SPC", down); break;
588 case SDLK_BACKSPACE: set_key("BS", down); break;
589 case SDLK_TAB: set_key("TAB", down); break;
590 case SDLK_KP_ENTER: set_key("ENT", down); break;
591 case SDLK_RETURN: set_key("RET", down); break;
592 case SDLK_ESCAPE: set_key("ESC", down); break;
593 case SDLK_DELETE: set_key("DEL", down); break;
594 case SDLK_INSERT: set_key("HELP", down); break;
599 void audio_sdl(float *data, int length)
603 /* read sound from sound buffer
604 * buffer pointer read and write is atomic, so no locking required!
606 fill = (sound_buffer_writep - sound_buffer_readp + sound_buffer_size) % sound_buffer_size;
608 #ifdef DEBUG_SOUND_BUFFERING
609 fprintf(stderr, "sound buffer underrun\n");
611 /* correct read pointer as if the buffer would have 'length' of samples stored inside */
612 sound_buffer_readp = (sound_buffer_readp + fill - length + sound_buffer_size) % sound_buffer_size;
614 for (s = 0; s < length; s++) {
615 *data++ = sound_buffer[(sound_buffer_readp + s) % sound_buffer_size].left;
616 *data++ = sound_buffer[(sound_buffer_readp + s) % sound_buffer_size].right;
618 #ifdef DEBUG_SOUND_BUFFERING
619 printf("fill %d = %.4f\n", length, sound_buffer[sound_buffer_readp][0]);
621 sound_buffer_readp =(sound_buffer_readp + length) % sound_buffer_size;
624 int main(int argc, char *argv[])
629 home_dir = getenv("HOME");
633 rc = parse_args(argc, argv);
638 image = calloc(IMAGE_WIDTH * IMAGE_HEIGHT * ((double_size) ? 4 : 1), 3);
640 print_error("Failed to allocate image buffer\n");
644 if ((SOUND_SAMPLERATE % IRQ_RATE)) {
645 print_error("Sample rate must be a multiple of IRQ rate, please fix!\n");
648 if ((1000 % IRQ_RATE)) {
649 print_error("1000 (Ticks per second) rate must be a multiple of IRQ rate, please fix!\n");
653 /* calculate SDL chunk size for audio
654 * it must be a power of two, but not more than the chunk size for each IRQ!
656 for (sdl_sound_chunk = 2; sdl_sound_chunk <= (SOUND_SAMPLERATE / IRQ_RATE); sdl_sound_chunk <<= 1)
658 sdl_sound_chunk >>= 1;
659 // printf("samples per IRQ = %d, samples per SDL audio = %d\n", SOUND_SAMPLERATE / IRQ_RATE, sdl_sound_chunk); exit(0);
661 /* allocate sound buffers */
662 sound_buffer_size = SOUND_SAMPLERATE / IRQ_RATE * SOUND_CHUNKS;
663 sound_buffer = calloc(sound_buffer_size, sizeof(*sound_buffer));
665 print_error("Failed to allocate image buffer\n");
669 /* allocate memory */
670 memory = calloc(MEMORY_SIZE, 1);
672 print_error("Failed to allocate cpu's memory\n");
675 stop_event = calloc(MEMORY_SIZE, 1);
677 print_error("Failed to allocate cpu's stop_event memory\n");
680 chipreg = calloc(IOSIZE, 1);
682 print_error("Failed to allocate chip register\n");
687 execute_init(MEMORY_SIZE, memory, stop_event, chipreg, io_read, io_write, mercenary_stop_at);
689 /* init disk emulation */
690 disk_init(disk_read, disk_write);
695 /* patch some stuff */
698 /* init SDL and OpenGL */
699 rc = init_sdl(argv[0], (config_debug_opengl) ? SCREEN_WIDTH * 2 : SCREEN_WIDTH * 3, (config_debug_opengl) ? SCREEN_HEIGHT * 4 : SCREEN_HEIGHT * 3, SOUND_SAMPLERATE, sdl_sound_chunk, keyboard_sdl, audio_sdl);
702 rc = init_opengl((double_size) ? IMAGE_WIDTH * 2 : IMAGE_WIDTH, (double_size) ? IMAGE_HEIGHT * 2 : IMAGE_HEIGHT);
705 resize_opengl((config_debug_opengl) ? SCREEN_WIDTH * 2 : SCREEN_WIDTH * 3, (config_debug_opengl) ? SCREEN_HEIGHT * 4 : SCREEN_HEIGHT * 3);
707 /* init audio filter */
708 sound_init_filter(SOUND_SAMPLERATE);
713 if (config_skip_intro) {
716 print_info("*** Skipping intro, fast forwarding... ***\n\n");
718 execute_cpu(0, &event);
719 } while (event != STOP_AT_CLEAR_SCREEN1);
721 execute_cpu(0, &event);
722 } while (event != STOP_AT_WAIT_VBL);
725 print_info("**********************************\n");
726 print_info("* Welcome to Mercenary Reloaded! *\n");
727 print_info("**********************************\n\n");
728 print_info("Press CTRL + cursor keys to select inventory or pickup/drop item.\n");
729 print_info("Press CTRL + f to toggle full screen.\n");
730 print_info("Press CTRL + s to toggle rendering speed.\n");
731 print_info("Press CTRL + v to toggle video filter.\n");
732 print_info("Press CTRL + a to toggle audio filter.\n");
733 print_info("Press CTRL + r to toggle rendering with opengl or orignal code.\n");
734 print_info("Press CTRL + c to exit game.\n\n");
735 print_info("Use '--help' as command line option for configuration settings.\n\n");