1 /*  gngeo a neogeo emulator
2  *  Copyright (C) 2001 Peponas Mathieu
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU Library General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #ifndef _MEMORY_H_
20 #define _MEMORY_H_
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include "SDL.h"
27 #include "SDL_endian.h"
28 #include "video.h"
29 #include "ym2610/2610intf.h"
30 #include "state.h"
31 #include "roms.h"
32 
33 #ifdef GP2X
34 #include "gp2x.h"
35 #endif
36 
37 #define READ_WORD(a)          (*(Uint16 *)(a))
38 #define WRITE_WORD(a,d)       (*(Uint16 *)(a) = (d))
39 #define READ_BYTE(a)          (*(Uint8 *)(a))
40 #define WRITE_BYTE(a,d)       (*(Uint8 *)(a) = (d))
41 #define SWAP_BYTE_ADDRESS(a)  ((Uintptr)(a)^1)
42 #define SWAP16(y) SDL_Swap16(y)
43 #define SWAP32(y) SDL_Swap32(y)
44 
45 #if defined(USE_GENERATOR68K) // || defined(USE_CYCLONE)
46 /* Programs are stored as BIGENDIAN */
47 #  ifdef WORDS_BIGENDIAN
48 #    define WRITE_WORD_ROM WRITE_WORD
49 #    define READ_WORD_ROM READ_WORD
50 #    define WRITE_BYTE_ROM WRITE_BYTE
51 #    define READ_BYTE_ROM READ_BYTE
52 #  else /* WORDS_BIGENDIAN */
53 #    define WRITE_WORD_ROM(a,d) (WRITE_WORD(a,SWAP16(d)))
54 #    define READ_WORD_ROM(a) (SWAP16(READ_WORD(a)))
55 #    define WRITE_BYTE_ROM WRITE_BYTE
56 #    define READ_BYTE_ROM READ_BYTE
57 #  endif
58 #else /* USE_GENERATOR68K */
59 /* Programs are stored as LITTLEENDIAN */
60 #  define WRITE_WORD_ROM WRITE_WORD
61 #  define READ_WORD_ROM READ_WORD
62 #  define WRITE_BYTE_ROM(a,d) WRITE_BYTE(SWAP_BYTE_ADDRESS(a),(d))
63 #  define READ_BYTE_ROM(a) READ_BYTE(SWAP_BYTE_ADDRESS(a))
64 #endif
65 
66 #if defined(USE_CYCLONE)
67 #  undef WRITE_WORD_ROM
68 #  undef READ_WORD_ROM
69 #  undef WRITE_BYTE_ROM
70 #  undef READ_BYTE_ROM
71 
72 #  define WRITE_WORD_ROM WRITE_WORD
73 #  define READ_WORD_ROM READ_WORD
74 
75 #  define WRITE_BYTE_ROM(a,d) WRITE_BYTE(SWAP_BYTE_ADDRESS(a),(d))
76 #  define READ_BYTE_ROM(a) READ_BYTE(SWAP_BYTE_ADDRESS(a))
77 #endif
78 
79 #define CHECK_ALLOC(a) {if (!a) {printf("Out of Memory\n");exit(1);}}
80 #define GFX_MAPPED 1
81 #define GZX_MAPPED 2
82 
83 
84 
85 typedef struct neo_mem {
86 	GAME_ROMS rom;
87 	Uint8 ram[0x10000];
88 	VIDEO vid;
89 	Uint8 *ng_lo;                          /* Put it in memory.vid? use zoom table in rom */
90 
91 	Uint32 nb_of_tiles;
92 
93 	Uint8 sram[0x10000];
94 
95     //	Uint32 *pen_usage;                      /* TODO: it's also in rom  */
96 	Uint8 fix_board_usage[4096];
97 	Uint8 *fix_game_usage;
98 
99 	Uint8 z80_ram[0x800];
100 	Uint8 game_vector[0x80];
101         int   current_vector;
102 	/* internal representation of joystick */
103 	Uint8 intern_p1, intern_p2, intern_coin, intern_start;
104 
105 	/* crypted rom bankswitch system */
106 
107 	Uint32 bksw_handler;
108 
109 	Uint8 *bksw_unscramble;
110 	int *bksw_offset;
111 	Uint16 sma_rng_addr;
112 	Uint8 memcard[0x800];
113 
114 	Uint32 watchdog;
115 } neo_mem;
116 
117 neo_mem memory;
118 
119 /* video related */
120 //extern int irq2start, irq2control;
121 Uint8 *current_pal;
122 Uint32 *current_pc_pal;
123 Uint8 *current_fix;
124 Uint8 *fix_usage;
125 
126 /* sram */
127 Uint8 sram_lock;
128 //Uint32 sram_protection_hack;
129 //int sram_protection_hack;
130 
131 /* Sound control */
132 Uint8 sound_code;
133 Uint8 pending_command;
134 Uint8 result_code;
135 
136 
137 /* 68k cpu Banking control */
138 extern Uint32 bankaddress;		/* current bank */
139 //Uint8 current_cpu_bank;
140 Uint16 z80_bank[4];
141 
142 /* misc utility func */
143 void update_all_pal(void);
144 void dump_hardware_reg(void);
145 
146 /* cpu 68k interface */
147 int cpu_68k_getcycle(void);
148 void cpu_68k_init(void);
149 void cpu_68k_reset(void);
150 int cpu_68k_run(Uint32 nb_cycle);
151 void cpu_68k_interrupt(int a);
152 void cpu_68k_bankswitch(Uint32 address);
153 void cpu_68k_disassemble(int pc, int nb_instr);
154 void cpu_68k_dumpreg(void);
155 int cpu_68k_run_step(void);
156 Uint32 cpu_68k_getpc(void);
157 void cpu_68k_fill_state(M68K_STATE *st);
158 void cpu_68k_set_state(M68K_STATE *st);
159 int cpu_68k_debuger(void (*execstep)(void),void (*dump)(void));
160 
161 
162 /* cpu z80 interface */
163 void cpu_z80_run(int nbcycle);
164 void cpu_z80_nmi(void);
165 void cpu_z80_raise_irq(int l);
166 void cpu_z80_lower_irq(void);
167 void cpu_z80_init(void);
168 void cpu_z80_switchbank(Uint8 bank, Uint16 PortNo);
169 Uint8 z80_port_read(Uint16 PortNo);
170 void z80_port_write(Uint16 PortNb, Uint8 Value);
171 void cpu_z80_set_state(Z80_STATE *st);
172 void cpu_z80_fill_state(Z80_STATE *st);
173 
174 /* memory handler prototype */
175 void neogeo_sound_irq(int irq);
176 
177 
178 #define LONG_FETCH(fetchname) Uint32 fetchname ## _long(Uint32 addr) { \
179       return (fetchname ## _word(addr) << 16) |	fetchname ## _word(addr+2); \
180 }
181 
182 #define LONG_STORE(storename) void storename ## _long(Uint32 addr, Uint32 data) { \
183       storename ## _word(addr,data>>16); \
184       storename ## _word(addr+2,data & 0xffff); \
185 }
186 
187 /* 68k fetching function */
188 Uint8 mem68k_fetch_ram_byte(Uint32 addr);
189 Uint16 mem68k_fetch_ram_word(Uint32 addr);
190 Uint32 mem68k_fetch_ram_long(Uint32 addr);
191 Uint8 mem68k_fetch_invalid_byte(Uint32 addr);
192 Uint16 mem68k_fetch_invalid_word(Uint32 addr);
193 Uint32 mem68k_fetch_invalid_long(Uint32 addr);
194 Uint8 mem68k_fetch_bk_normal_byte(Uint32 addr);
195 Uint16 mem68k_fetch_bk_normal_word(Uint32 addr);
196 Uint32 mem68k_fetch_bk_normal_long(Uint32 addr);
197 Uint8 mem68k_fetch_cpu_byte(Uint32 addr);
198 Uint16 mem68k_fetch_cpu_word(Uint32 addr);
199 Uint32 mem68k_fetch_cpu_long(Uint32 addr);
200 Uint8 mem68k_fetch_bios_byte(Uint32 addr);
201 Uint16 mem68k_fetch_bios_word(Uint32 addr);
202 Uint32 mem68k_fetch_bios_long(Uint32 addr);
203 Uint8 mem68k_fetch_sram_byte(Uint32 addr);
204 Uint16 mem68k_fetch_sram_word(Uint32 addr);
205 Uint32 mem68k_fetch_sram_long(Uint32 addr);
206 Uint8 mem68k_fetch_pal_byte(Uint32 addr);
207 Uint16 mem68k_fetch_pal_word(Uint32 addr);
208 Uint32 mem68k_fetch_pal_long(Uint32 addr);
209 Uint8 mem68k_fetch_video_byte(Uint32 addr);
210 Uint16 mem68k_fetch_video_word(Uint32 addr);
211 Uint32 mem68k_fetch_video_long(Uint32 addr);
212 Uint8 mem68k_fetch_ctl1_byte(Uint32 addr);
213 Uint16 mem68k_fetch_ctl1_word(Uint32 addr);
214 Uint32 mem68k_fetch_ctl1_long(Uint32 addr);
215 Uint8 mem68k_fetch_ctl2_byte(Uint32 addr);
216 Uint16 mem68k_fetch_ctl2_word(Uint32 addr);
217 Uint32 mem68k_fetch_ctl2_long(Uint32 addr);
218 Uint8 mem68k_fetch_ctl3_byte(Uint32 addr);
219 Uint16 mem68k_fetch_ctl3_word(Uint32 addr);
220 Uint32 mem68k_fetch_ctl3_long(Uint32 addr);
221 Uint8 mem68k_fetch_coin_byte(Uint32 addr);
222 Uint16 mem68k_fetch_coin_word(Uint32 addr);
223 Uint32 mem68k_fetch_coin_long(Uint32 addr);
224 Uint8 mem68k_fetch_memcrd_byte(Uint32 addr);
225 Uint16 mem68k_fetch_memcrd_word(Uint32 addr);
226 Uint32 mem68k_fetch_memcrd_long(Uint32 addr);
227 Uint8 mem68k_fetch_bk_kof2003_byte(Uint32 addr);
228 Uint16 mem68k_fetch_bk_kof2003_word(Uint32 addr);
229 Uint32 mem68k_fetch_bk_kof2003_long(Uint32 addr);
230 
231 /* 68k storring function */
232 void mem68k_store_invalid_byte(Uint32 addr, Uint8 data);
233 void mem68k_store_invalid_word(Uint32 addr, Uint16 data);
234 void mem68k_store_invalid_long(Uint32 addr, Uint32 data);
235 void mem68k_store_ram_byte(Uint32 addr, Uint8 data);
236 void mem68k_store_ram_word(Uint32 addr, Uint16 data);
237 void mem68k_store_ram_long(Uint32 addr, Uint32 data);
238 void mem68k_store_bk_normal_byte(Uint32 addr, Uint8 data);
239 void mem68k_store_bk_normal_word(Uint32 addr, Uint16 data);
240 void mem68k_store_bk_normal_long(Uint32 addr, Uint32 data);
241 void mem68k_store_sram_byte(Uint32 addr, Uint8 data);
242 void mem68k_store_sram_word(Uint32 addr, Uint16 data);
243 void mem68k_store_sram_long(Uint32 addr, Uint32 data);
244 void mem68k_store_pal_byte(Uint32 addr, Uint8 data);
245 void mem68k_store_pal_word(Uint32 addr, Uint16 data);
246 void mem68k_store_pal_long(Uint32 addr, Uint32 data);
247 void mem68k_store_video_byte(Uint32 addr, Uint8 data);
248 void mem68k_store_video_word(Uint32 addr, Uint16 data);
249 void mem68k_store_video_long(Uint32 addr, Uint32 data);
250 void mem68k_store_pd4990_byte(Uint32 addr, Uint8 data);
251 void mem68k_store_pd4990_word(Uint32 addr, Uint16 data);
252 void mem68k_store_pd4990_long(Uint32 addr, Uint32 data);
253 void mem68k_store_z80_byte(Uint32 addr, Uint8 data);
254 void mem68k_store_z80_word(Uint32 addr, Uint16 data);
255 void mem68k_store_z80_long(Uint32 addr, Uint32 data);
256 void mem68k_store_setting_byte(Uint32 addr, Uint8 data);
257 void mem68k_store_setting_word(Uint32 addr, Uint16 data);
258 void mem68k_store_setting_long(Uint32 addr, Uint32 data);
259 void mem68k_store_memcrd_byte(Uint32 addr, Uint8 data);
260 void mem68k_store_memcrd_word(Uint32 addr, Uint16 data);
261 void mem68k_store_memcrd_long(Uint32 addr, Uint32 data);
262 void mem68k_store_bk_kof2003_byte(Uint32 addr, Uint8 data);
263 void mem68k_store_bk_kof2003_word(Uint32 addr, Uint16 data);
264 void mem68k_store_bk_kof2003_long(Uint32 addr, Uint32 data);
265 
266 Uint8 (*mem68k_fetch_bksw_byte)(Uint32);
267 Uint16 (*mem68k_fetch_bksw_word)(Uint32);
268 Uint32 (*mem68k_fetch_bksw_long)(Uint32);
269 void (*mem68k_store_bksw_byte)(Uint32,Uint8);
270 void (*mem68k_store_bksw_word)(Uint32,Uint16);
271 void (*mem68k_store_bksw_long)(Uint32,Uint32);
272 #endif
273