Turn VBL break point list into a structure to make game stop at various events
[mercenary-reloaded.git] / src / mercenary / mercenary2.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 "../libcpu/m68k.h"
24 #include "../libcpu/execute.h"
25 #include "mercenary.h"
26
27 #define INITIAL_STACK   0x7fffa
28 #define RESET_VECTOR    0x59484
29
30 /* interrupt CPU execution at special break points and tell emulation what to do */
31 const struct cpu_stop mercenary_stop_at[] = {
32         { 0x59a4e,      STOP_AT_WAIT_VBL },     /* done with rendering, waiting for VBL */
33         { 0x54c26,      STOP_AT_WAIT_VBL },     /* after pressing 'HELP' key before showing menu line on benson */
34         { 0x55438,      STOP_AT_WAIT_VBL },     /* waiting for menu command */
35         { 0x54c2e,      STOP_AT_WAIT_VBL },     /* after pressing 'HELP' key while showing menu line on benson */
36         { 0x55446,      STOP_AT_WAIT_VBL },     /* after pressing 'RETURN' while game waits for other key to resume */
37         { 0x51620,      STOP_AT_WAIT_VBL },     /* after dying, waiting for VBL to fade out palette */
38         { 0x0,          STOP_AT_END },          /* end */
39 };
40
41 extern const uint32_t mercenary2_hex[];
42 extern int mercenary2_hex_size;
43
44 void mercenary_load(void)
45 {
46         int i;
47
48         /* load game binary from constant to volatile memory */
49         for (i = 0; i < mercenary2_hex_size; i += 4) {
50                 m68k_write_memory_32(i, mercenary2_hex[i / 4]);
51         }
52 }
53
54 void mercenary_patch(void)
55 {
56         /* initial stack */
57         m68k_write_memory_32(0x00000, INITIAL_STACK);
58
59         /* reset vector */
60         m68k_write_memory_32(0x00004, RESET_VECTOR);
61
62         /* remove wait for VBL */
63         m68k_write_memory_16(0x59a54, 0x4e71); /* nop */
64
65         /* reduce loop that waits for disk stepper to move */
66         if (m68k_read_memory_32(0x55398) != 0x0000091b) {
67                 fprintf(stderr, "expecting loop counter of 0x0000091b here, please fix!\n");
68                 exit(0);
69         }
70         m68k_write_memory_32(0x55398, 1);
71         /* reduce loop that waits for disk side change */
72         if (m68k_read_memory_32(0x54ffc) != 0x00000d02) {
73                 fprintf(stderr, "expecting loop counter of 0x00000d02 here, please fix!\n");
74                 exit(0);
75         }
76         m68k_write_memory_32(0x54ffc, 1);
77 }
78
79 uint32_t mercenary_palette(void)
80 {
81         return m68k_read_memory_32(0x007c14);
82 }
83