1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - flashram.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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
29 #include "r4300/r4300.h"
31 #include "api/m64p_types.h"
32 #include "api/callbacks.h"
33 #include "main/main.h"
35 #include "main/util.h"
37 Flashram_info flashram_info;
39 typedef enum flashram_mode
48 static unsigned char flashram[0x20000];
50 static char *get_flashram_path(void)
52 return formatstr("%s%s.fla", get_savesrampath(), ROM_SETTINGS.goodname);
55 static void flashram_format(void)
57 memset(flashram, 0xff, sizeof(flashram));
60 static void flashram_read_file(void)
62 char *filename = get_flashram_path();
65 switch (read_from_file(filename, flashram, sizeof(flashram)))
68 DebugMessage(M64MSG_WARNING, "couldn't open flash ram file '%s' for reading", filename);
72 DebugMessage(M64MSG_WARNING, "couldn't read 128kb flash ram file '%s'", filename);
80 static void flashram_write_file(void)
82 char *filename = get_flashram_path();
84 switch (write_to_file(filename, flashram, sizeof(flashram)))
87 DebugMessage(M64MSG_WARNING, "couldn't open flash ram file '%s' for writing", filename);
89 case file_write_error:
90 DebugMessage(M64MSG_WARNING, "couldn't write 128kb flash ram file '%s'", filename);
98 void init_flashram(void)
100 flashram_info.mode = NOPES_MODE;
101 flashram_info.status = 0;
104 unsigned int flashram_status(void)
106 return (unsigned int) (flashram_info.status >> 32);
109 void flashram_command(unsigned int command)
111 switch (command & 0xff000000)
114 flashram_info.erase_offset = (command & 0xffff) * 128;
117 flashram_info.mode = ERASE_MODE;
118 flashram_info.status = 0x1111800800c20000LL;
121 flashram_info.erase_offset = (command & 0xffff) * 128;
122 flashram_info.status = 0x1111800400c20000LL;
125 flashram_info.mode = WRITE_MODE;
127 case 0xd2000000: // execute
128 switch (flashram_info.mode)
135 flashram_read_file();
136 for (i=flashram_info.erase_offset; i<(flashram_info.erase_offset+128); i++)
138 flashram[i^S8] = 0xff;
140 flashram_write_file();
146 flashram_read_file();
147 for (i=0; i<128; i++)
149 flashram[(flashram_info.erase_offset+i)^S8]=
150 ((unsigned char*)rdram)[(flashram_info.write_pointer+i)^S8];
152 flashram_write_file();
158 DebugMessage(M64MSG_WARNING, "unknown flashram command with mode:%x", (int)flashram_info.mode);
162 flashram_info.mode = NOPES_MODE;
165 flashram_info.mode = STATUS_MODE;
166 flashram_info.status = 0x1111800100c20000LL;
169 flashram_info.mode = READ_MODE;
170 flashram_info.status = 0x11118004f0000000LL;
173 DebugMessage(M64MSG_WARNING, "unknown flashram command: %x", (int)command);
178 void dma_read_flashram(void)
182 switch (flashram_info.mode)
185 rdram[pi_register.pi_dram_addr_reg/4] = (unsigned int)(flashram_info.status >> 32);
186 rdram[pi_register.pi_dram_addr_reg/4+1] = (unsigned int)(flashram_info.status);
189 flashram_read_file();
190 for (i=0; i<(pi_register.pi_wr_len_reg & 0x0FFFFFF)+1; i++)
192 ((unsigned char*)rdram)[(pi_register.pi_dram_addr_reg+i)^S8]=
193 flashram[(((pi_register.pi_cart_addr_reg-0x08000000)&0xFFFF)*2+i)^S8];
197 DebugMessage(M64MSG_WARNING, "unknown dma_read_flashram: %x", flashram_info.mode);
203 void dma_write_flashram(void)
205 switch (flashram_info.mode)
208 flashram_info.write_pointer = pi_register.pi_dram_addr_reg;
211 DebugMessage(M64MSG_ERROR, "unknown dma_write_flashram: %x", flashram_info.mode);