Prepare renering for improving game with OpenGL
[mercenary-reloaded.git] / src / mercenary / mercenary3.c
1 /* game specials
2  *
3  * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
4  * All Rights Reserved
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include "../libsdl/print.h"
24 #include "../libcpu/m68k.h"
25 #include "../libcpu/execute.h"
26 #include "mercenary.h"
27
28 #define INITIAL_STACK   0x7fffa
29 #define RESET_VECTOR    0x5a16c
30 // #define RESET_VECTOR 0x5a1a4 /* strange reset vector that causes the player to fly around */
31
32 /* interrupt CPU execution at special break points and tell emulation what to do */
33 const struct cpu_stop mercenary_stop_at[] = {
34         { 0x5a826,      STOP_AT_WAIT_VBL },             /* done with rendering, waiting for VBL */
35         { 0x55d8c,      STOP_AT_WAIT_VBL },             /* after pressing 'HELP' key before showing menu line on benson */
36         { 0x56398,      STOP_AT_WAIT_VBL },             /* waiting for menu command */
37         { 0x55d94,      STOP_AT_WAIT_VBL },             /* after pressing 'HELP' key while showing menu line on benson */
38         { 0x563a6,      STOP_AT_WAIT_VBL },             /* after pressing 'RETURN' while game waits for other key to resume */
39         { 0x52946,      STOP_AT_WAIT_VBL },             /* after dying, waiting for VBL to fade out palette */
40         { 0x5408E,      STOP_AT_PARSE_OBJECT },         /* an object is about to be rendered, process vertex data */
41         { 0x0,          STOP_AT_END },                  /* end */
42 };
43
44 extern const uint32_t mercenary3_hex[];
45 extern int mercenary3_hex_size;
46
47 void mercenary_load(void)
48 {
49         int i;
50
51         /* load game binary from constant to volatile memory */
52         for (i = 0; i < mercenary3_hex_size; i += 4) {
53                 m68k_write_memory_32(i, mercenary3_hex[i / 4]);
54         }
55 }
56
57 void mercenary_patch(void)
58 {
59         /* initial stack */
60         m68k_write_memory_32(0x00000, INITIAL_STACK);
61
62         /* reset vector */
63         m68k_write_memory_32(0x00004, RESET_VECTOR);
64
65         /* patch away the function call that is outside chip memory
66          * is this a copy protection????
67          */
68         m68k_write_memory_16(0x5a17c, 0x4e71); /* nop */
69         m68k_write_memory_16(0x5a17e, 0x4e71); /* nop */
70         m68k_write_memory_16(0x5a180, 0x4e71); /* nop */
71
72         /* remove wait for VBL */
73         m68k_write_memory_16(0x5a82C, 0x4e71); /* nop */
74
75         /* reduce loop that waits for disk stepper to move */
76         if (m68k_read_memory_32(0x562f8) != 0x000091b0) {
77                 print_error("expecting loop counter of 0x000091b0 here, please fix!\n");
78                 exit(0);
79         }
80         m68k_write_memory_32(0x562f8, 1);
81         /* reduce loop that waits for disk side change */
82         if (m68k_read_memory_32(0x55f5c) != 0x0000d020) {
83                 print_error("expecting loop counter of 0x0000d020 here, please fix!\n");
84                 exit(0);
85         }
86         m68k_write_memory_32(0x55f5c, 1);
87 }
88
89 uint32_t mercenary_palette(void)
90 {
91         return m68k_read_memory_32(0x0072b0);
92 }
93
94 const char *mercenary_gamesavesuffix = ".m3save";
95