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];
50 static char *get_sram_path(void)
52 return formatstr("%s%s.sra", get_savesrampath(), ROM_SETTINGS.goodname);
55 static void sram_format(void)
57 memset(sram, 0, sizeof(sram));
60 static void sram_read_file(void)
62 char *filename = get_sram_path();
65 switch (read_from_file(filename, sram, sizeof(sram)))
68 DebugMessage(M64MSG_VERBOSE, "couldn't open sram file '%s' for reading", filename);
72 DebugMessage(M64MSG_WARNING, "fread() failed on 32kb read from sram file '%s'", filename);
81 static void sram_write_file(void)
83 char *filename = get_sram_path();
85 switch (write_to_file(filename, sram, sizeof(sram)))
88 DebugMessage(M64MSG_WARNING, "couldn't open sram file '%s' for writing.", filename);
90 case file_write_error:
91 DebugMessage(M64MSG_WARNING, "fwrite() failed on 32kb write to sram file '%s'", filename);
99 void dma_pi_read(void)
103 if (pi_register.pi_cart_addr_reg >= 0x08000000
104 && pi_register.pi_cart_addr_reg < 0x08010000)
106 if (flashram_info.use_flashram != 1)
110 for (i=0; i < (pi_register.pi_rd_len_reg & 0xFFFFFF)+1; i++)
112 sram[((pi_register.pi_cart_addr_reg-0x08000000)+i)^S8] =
113 ((unsigned char*)rdram)[(pi_register.pi_dram_addr_reg+i)^S8];
118 flashram_info.use_flashram = -1;
122 dma_write_flashram();
127 DebugMessage(M64MSG_WARNING, "Unknown dma read in dma_pi_read()");
130 pi_register.read_pi_status_reg |= 1;
132 add_interupt_event(PI_INT, 0x1000/*pi_register.pi_rd_len_reg*/);
135 void dma_pi_write(void)
137 unsigned int longueur;
140 if (pi_register.pi_cart_addr_reg < 0x10000000)
142 if (pi_register.pi_cart_addr_reg >= 0x08000000
143 && pi_register.pi_cart_addr_reg < 0x08010000)
145 if (flashram_info.use_flashram != 1)
151 for (i=0; i<(int)(pi_register.pi_wr_len_reg & 0xFFFFFF)+1; i++)
153 ((unsigned char*)rdram)[(pi_register.pi_dram_addr_reg+i)^S8]=
154 sram[(((pi_register.pi_cart_addr_reg-0x08000000)&0xFFFF)+i)^S8];
157 flashram_info.use_flashram = -1;
164 else if (pi_register.pi_cart_addr_reg >= 0x06000000
165 && pi_register.pi_cart_addr_reg < 0x08000000)
170 DebugMessage(M64MSG_WARNING, "Unknown dma write 0x%x in dma_pi_write()", (int)pi_register.pi_cart_addr_reg);
173 pi_register.read_pi_status_reg |= 1;
175 add_interupt_event(PI_INT, /*pi_register.pi_wr_len_reg*/0x1000);
180 if (pi_register.pi_cart_addr_reg >= 0x1fc00000) // for paper mario
182 pi_register.read_pi_status_reg |= 1;
184 add_interupt_event(PI_INT, 0x1000);
189 longueur = (pi_register.pi_wr_len_reg & 0xFFFFFF)+1;
190 i = (pi_register.pi_cart_addr_reg-0x10000000)&0x3FFFFFF;
191 longueur = (i + (int) longueur) > rom_size ?
192 (rom_size - i) : longueur;
193 longueur = (pi_register.pi_dram_addr_reg + longueur) > 0x7FFFFF ?
194 (0x7FFFFF - pi_register.pi_dram_addr_reg) : longueur;
196 if (i>rom_size || pi_register.pi_dram_addr_reg > 0x7FFFFF)
198 pi_register.read_pi_status_reg |= 3;
200 add_interupt_event(PI_INT, longueur/8);
205 if (r4300emu != CORE_PURE_INTERPRETER)
207 for (i=0; i<(int)longueur; i++)
209 unsigned long rdram_address1 = pi_register.pi_dram_addr_reg+i+0x80000000;
210 unsigned long rdram_address2 = pi_register.pi_dram_addr_reg+i+0xa0000000;
211 ((unsigned char*)rdram)[(pi_register.pi_dram_addr_reg+i)^S8]=
212 rom[(((pi_register.pi_cart_addr_reg-0x10000000)&0x3FFFFFF)+i)^S8];
214 if (!invalid_code[rdram_address1>>12])
216 if (!blocks[rdram_address1>>12] ||
217 blocks[rdram_address1>>12]->block[(rdram_address1&0xFFF)/4].ops !=
218 current_instruction_table.NOTCOMPILED)
220 invalid_code[rdram_address1>>12] = 1;
223 invalidate_block(rdram_address1>>12);
226 if (!invalid_code[rdram_address2>>12])
228 if (!blocks[rdram_address1>>12] ||
229 blocks[rdram_address2>>12]->block[(rdram_address2&0xFFF)/4].ops !=
230 current_instruction_table.NOTCOMPILED)
232 invalid_code[rdram_address2>>12] = 1;
239 for (i=0; i<(int)longueur; i++)
241 ((unsigned char*)rdram)[(pi_register.pi_dram_addr_reg+i)^S8]=
242 rom[(((pi_register.pi_cart_addr_reg-0x10000000)&0x3FFFFFF)+i)^S8];
246 // Set the RDRAM memory size when copying main ROM code
247 // (This is just a convenient way to run this code once at the beginning)
248 if (pi_register.pi_cart_addr_reg == 0x10001000)
257 if (ConfigGetParamInt(g_CoreConfig, "DisableExtraMem"))
259 rdram[0x318/4] = 0x400000;
263 rdram[0x318/4] = 0x800000;
269 if (ConfigGetParamInt(g_CoreConfig, "DisableExtraMem"))
271 rdram[0x3F0/4] = 0x400000;
275 rdram[0x3F0/4] = 0x800000;
282 pi_register.read_pi_status_reg |= 3;
284 add_interupt_event(PI_INT, longueur/8);
289 void dma_sp_write(void)
293 unsigned int l = sp_register.sp_rd_len_reg;
295 unsigned int length = ((l & 0xfff) | 7) + 1;
296 unsigned int count = ((l >> 12) & 0xff) + 1;
297 unsigned int skip = ((l >> 20) & 0xfff);
299 unsigned int memaddr = sp_register.sp_mem_addr_reg & 0xfff;
300 unsigned int dramaddr = sp_register.sp_dram_addr_reg & 0xffffff;
302 unsigned char *spmem = ((sp_register.sp_mem_addr_reg & 0x1000) != 0) ? (unsigned char*)SP_IMEM : (unsigned char*)SP_DMEM;
303 unsigned char *dram = (unsigned char*)rdram;
305 for(j=0; j<count; j++) {
306 for(i=0; i<length; i++) {
307 spmem[memaddr^S8] = dram[dramaddr^S8];
315 void dma_sp_read(void)
319 unsigned int l = sp_register.sp_wr_len_reg;
321 unsigned int length = ((l & 0xfff) | 7) + 1;
322 unsigned int count = ((l >> 12) & 0xff) + 1;
323 unsigned int skip = ((l >> 20) & 0xfff);
325 unsigned int memaddr = sp_register.sp_mem_addr_reg & 0xfff;
326 unsigned int dramaddr = sp_register.sp_dram_addr_reg & 0xffffff;
328 unsigned char *spmem = ((sp_register.sp_mem_addr_reg & 0x1000) != 0) ? (unsigned char*)SP_IMEM : (unsigned char*)SP_DMEM;
329 unsigned char *dram = (unsigned char*)rdram;
331 for(j=0; j<count; j++) {
332 for(i=0; i<length; i++) {
333 dram[dramaddr^S8] = spmem[memaddr^S8];
341 void dma_si_write(void)
345 if (si_register.si_pif_addr_wr64b != 0x1FC007C0)
347 DebugMessage(M64MSG_ERROR, "dma_si_write(): unknown SI use");
351 for (i=0; i<(64/4); i++)
353 PIF_RAM[i] = sl(rdram[si_register.si_dram_addr/4+i]);
360 add_interupt_event(SI_INT, /*0x100*/0x900);
362 MI_register.mi_intr_reg |= 0x02; // SI
363 si_register.si_stat |= 0x1000; // INTERRUPT
368 void dma_si_read(void)
372 if (si_register.si_pif_addr_rd64b != 0x1FC007C0)
374 DebugMessage(M64MSG_ERROR, "dma_si_read(): unknown SI use");
380 for (i=0; i<(64/4); i++)
382 rdram[si_register.si_dram_addr/4+i] = sl(PIF_RAM[i]);
388 add_interupt_event(SI_INT, /*0x100*/0x900);
390 MI_register.mi_intr_reg |= 0x02; // SI
391 si_register.si_stat |= 0x1000; // INTERRUPT