1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - dma.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2002 Hacktarux *
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 2 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, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #include "api/m64p_types.h"
33 #include "r4300/r4300.h"
34 #include "r4300/interupt.h"
35 #include "r4300/macros.h"
36 #include "r4300/ops.h"
37 #include "../r4300/new_dynarec/new_dynarec.h"
39 #define M64P_CORE_PROTOTYPES 1
40 #include "api/m64p_config.h"
41 #include "api/config.h"
42 #include "api/callbacks.h"
43 #include "main/main.h"
45 #include "main/util.h"
47 static unsigned char sram[0x8000];
49 static char *get_sram_path(void)
51 return formatstr("%s%s.sra", get_savesrampath(), ROM_SETTINGS.goodname);
54 static void sram_format(void)
56 memset(sram, 0, sizeof(sram));
59 static void sram_read_file(void)
61 char *filename = get_sram_path();
64 switch (read_from_file(filename, sram, sizeof(sram)))
67 DebugMessage(M64MSG_VERBOSE, "couldn't open sram file '%s' for reading", filename);
71 DebugMessage(M64MSG_WARNING, "fread() failed on 32kb read from sram file '%s'", filename);
80 static void sram_write_file(void)
82 char *filename = get_sram_path();
84 switch (write_to_file(filename, sram, sizeof(sram)))
87 DebugMessage(M64MSG_WARNING, "couldn't open sram file '%s' for writing.", filename);
89 case file_write_error:
90 DebugMessage(M64MSG_WARNING, "fwrite() failed on 32kb write to sram file '%s'", filename);
98 void dma_pi_read(void)
102 if (pi_register.pi_cart_addr_reg >= 0x08000000
103 && pi_register.pi_cart_addr_reg < 0x08010000)
105 if (flashram_info.use_flashram != 1)
109 for (i=0; i < (pi_register.pi_rd_len_reg & 0xFFFFFF)+1; i++)
111 sram[((pi_register.pi_cart_addr_reg-0x08000000)+i)^S8] =
112 ((unsigned char*)rdram)[(pi_register.pi_dram_addr_reg+i)^S8];
117 flashram_info.use_flashram = -1;
121 dma_write_flashram();
126 DebugMessage(M64MSG_WARNING, "Unknown dma read in dma_pi_read()");
129 pi_register.read_pi_status_reg |= 1;
131 add_interupt_event(PI_INT, 0x1000/*pi_register.pi_rd_len_reg*/);
134 void dma_pi_write(void)
136 unsigned int longueur;
139 if (pi_register.pi_cart_addr_reg < 0x10000000)
141 if (pi_register.pi_cart_addr_reg >= 0x08000000
142 && pi_register.pi_cart_addr_reg < 0x08010000)
144 if (flashram_info.use_flashram != 1)
150 for (i=0; i<(int)(pi_register.pi_wr_len_reg & 0xFFFFFF)+1; i++)
152 ((unsigned char*)rdram)[(pi_register.pi_dram_addr_reg+i)^S8]=
153 sram[(((pi_register.pi_cart_addr_reg-0x08000000)&0xFFFF)+i)^S8];
156 flashram_info.use_flashram = -1;
163 else if (pi_register.pi_cart_addr_reg >= 0x06000000
164 && pi_register.pi_cart_addr_reg < 0x08000000)
169 DebugMessage(M64MSG_WARNING, "Unknown dma write 0x%x in dma_pi_write()", (int)pi_register.pi_cart_addr_reg);
172 pi_register.read_pi_status_reg |= 1;
174 add_interupt_event(PI_INT, /*pi_register.pi_wr_len_reg*/0x1000);
179 if (pi_register.pi_cart_addr_reg >= 0x1fc00000) // for paper mario
181 pi_register.read_pi_status_reg |= 1;
183 add_interupt_event(PI_INT, 0x1000);
188 longueur = (pi_register.pi_wr_len_reg & 0xFFFFFF)+1;
189 i = (pi_register.pi_cart_addr_reg-0x10000000)&0x3FFFFFF;
190 longueur = (i + (int) longueur) > rom_size ?
191 (rom_size - i) : longueur;
192 longueur = (pi_register.pi_dram_addr_reg + longueur) > 0x7FFFFF ?
193 (0x7FFFFF - pi_register.pi_dram_addr_reg) : longueur;
195 if (i>rom_size || pi_register.pi_dram_addr_reg > 0x7FFFFF)
197 pi_register.read_pi_status_reg |= 3;
199 add_interupt_event(PI_INT, longueur/8);
204 if (r4300emu != CORE_PURE_INTERPRETER)
206 for (i=0; i<(int)longueur; i++)
208 unsigned long rdram_address1 = pi_register.pi_dram_addr_reg+i+0x80000000;
209 unsigned long rdram_address2 = pi_register.pi_dram_addr_reg+i+0xa0000000;
210 ((unsigned char*)rdram)[(pi_register.pi_dram_addr_reg+i)^S8]=
211 rom[(((pi_register.pi_cart_addr_reg-0x10000000)&0x3FFFFFF)+i)^S8];
213 if (!invalid_code[rdram_address1>>12])
215 if (!blocks[rdram_address1>>12] ||
216 blocks[rdram_address1>>12]->block[(rdram_address1&0xFFF)/4].ops !=
217 current_instruction_table.NOTCOMPILED)
219 invalid_code[rdram_address1>>12] = 1;
222 invalidate_block(rdram_address1>>12);
225 if (!invalid_code[rdram_address2>>12])
227 if (!blocks[rdram_address1>>12] ||
228 blocks[rdram_address2>>12]->block[(rdram_address2&0xFFF)/4].ops !=
229 current_instruction_table.NOTCOMPILED)
231 invalid_code[rdram_address2>>12] = 1;
238 for (i=0; i<(int)longueur; i++)
240 ((unsigned char*)rdram)[(pi_register.pi_dram_addr_reg+i)^S8]=
241 rom[(((pi_register.pi_cart_addr_reg-0x10000000)&0x3FFFFFF)+i)^S8];
245 // Set the RDRAM memory size when copying main ROM code
246 // (This is just a convenient way to run this code once at the beginning)
247 if (pi_register.pi_cart_addr_reg == 0x10001000)
256 if (ConfigGetParamInt(g_CoreConfig, "DisableExtraMem"))
258 rdram[0x318/4] = 0x400000;
262 rdram[0x318/4] = 0x800000;
268 if (ConfigGetParamInt(g_CoreConfig, "DisableExtraMem"))
270 rdram[0x3F0/4] = 0x400000;
274 rdram[0x3F0/4] = 0x800000;
281 pi_register.read_pi_status_reg |= 3;
283 add_interupt_event(PI_INT, longueur/8);
288 void dma_sp_write(void)
292 unsigned int l = sp_register.sp_rd_len_reg;
294 unsigned int length = ((l & 0xfff) | 7) + 1;
295 unsigned int count = ((l >> 12) & 0xff) + 1;
296 unsigned int skip = ((l >> 20) & 0xfff);
298 unsigned int memaddr = sp_register.sp_mem_addr_reg & 0xfff;
299 unsigned int dramaddr = sp_register.sp_dram_addr_reg & 0xffffff;
301 unsigned char *spmem = ((sp_register.sp_mem_addr_reg & 0x1000) != 0) ? (unsigned char*)SP_IMEM : (unsigned char*)SP_DMEM;
302 unsigned char *dram = (unsigned char*)rdram;
304 for(j=0; j<count; j++) {
305 for(i=0; i<length; i++) {
306 spmem[memaddr^S8] = dram[dramaddr^S8];
314 void dma_sp_read(void)
318 unsigned int l = sp_register.sp_wr_len_reg;
320 unsigned int length = ((l & 0xfff) | 7) + 1;
321 unsigned int count = ((l >> 12) & 0xff) + 1;
322 unsigned int skip = ((l >> 20) & 0xfff);
324 unsigned int memaddr = sp_register.sp_mem_addr_reg & 0xfff;
325 unsigned int dramaddr = sp_register.sp_dram_addr_reg & 0xffffff;
327 unsigned char *spmem = ((sp_register.sp_mem_addr_reg & 0x1000) != 0) ? (unsigned char*)SP_IMEM : (unsigned char*)SP_DMEM;
328 unsigned char *dram = (unsigned char*)rdram;
330 for(j=0; j<count; j++) {
331 for(i=0; i<length; i++) {
332 dram[dramaddr^S8] = spmem[memaddr^S8];
340 void dma_si_write(void)
344 if (si_register.si_pif_addr_wr64b != 0x1FC007C0)
346 DebugMessage(M64MSG_ERROR, "dma_si_write(): unknown SI use");
350 for (i=0; i<(64/4); i++)
352 PIF_RAM[i] = sl(rdram[si_register.si_dram_addr/4+i]);
357 add_interupt_event(SI_INT, /*0x100*/0x900);
360 void dma_si_read(void)
364 if (si_register.si_pif_addr_rd64b != 0x1FC007C0)
366 DebugMessage(M64MSG_ERROR, "dma_si_read(): unknown SI use");
372 for (i=0; i<(64/4); i++)
374 rdram[si_register.si_dram_addr/4+i] = sl(PIF_RAM[i]);
378 add_interupt_event(SI_INT, /*0x100*/0x900);