Core commit. Compile and run on the OpenPandora
[mupen64plus-pandora.git] / source / mupen64plus-core / src / memory / flashram.c
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus - flashram.c                                              *
3  *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4  *   Copyright (C) 2002 Hacktarux                                          *
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 2 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, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
20  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "memory.h"
27 #include "flashram.h"
28
29 #include "r4300/r4300.h"
30
31 #include "api/m64p_types.h"
32 #include "api/callbacks.h"
33 #include "main/main.h"
34 #include "main/rom.h"
35 #include "main/util.h"
36
37 Flashram_info flashram_info;
38
39 typedef enum flashram_mode
40 {
41     NOPES_MODE = 0,
42     ERASE_MODE,
43     WRITE_MODE,
44     READ_MODE,
45     STATUS_MODE
46 } Flashram_mode;
47
48 static unsigned char flashram[0x20000];
49
50 static char *get_flashram_path(void)
51 {
52     return formatstr("%s%s.fla", get_savesrampath(), ROM_SETTINGS.goodname);
53 }
54
55 static void flashram_format(void)
56 {
57     memset(flashram, 0xff, sizeof(flashram));
58 }
59
60 static void flashram_read_file(void)
61 {
62     char *filename = get_flashram_path();
63
64     flashram_format();
65     switch (read_from_file(filename, flashram, sizeof(flashram)))
66     {
67         case file_open_error:
68             DebugMessage(M64MSG_WARNING, "couldn't open flash ram file '%s' for reading", filename);
69             flashram_format();
70             break;
71         case file_read_error:
72             DebugMessage(M64MSG_WARNING, "couldn't read 128kb flash ram file '%s'", filename);
73             break;
74         default: break;
75     }
76
77     free(filename);
78 }
79
80 static void flashram_write_file(void)
81 {
82     char *filename = get_flashram_path();
83
84     switch (write_to_file(filename, flashram, sizeof(flashram)))
85     {
86         case file_open_error:
87             DebugMessage(M64MSG_WARNING, "couldn't open flash ram file '%s' for writing", filename);
88             break;
89         case file_write_error:
90             DebugMessage(M64MSG_WARNING, "couldn't write 128kb flash ram file '%s'", filename);
91             break;
92         default: break;
93     }
94
95     free(filename);
96 }
97
98 void init_flashram(void)
99 {
100     flashram_info.mode = NOPES_MODE;
101     flashram_info.status = 0;
102 }
103
104 unsigned int flashram_status(void)
105 {
106     return (unsigned int) (flashram_info.status >> 32);
107 }
108
109 void flashram_command(unsigned int command)
110 {
111     switch (command & 0xff000000)
112     {
113     case 0x4b000000:
114         flashram_info.erase_offset = (command & 0xffff) * 128;
115         break;
116     case 0x78000000:
117         flashram_info.mode = ERASE_MODE;
118         flashram_info.status = 0x1111800800c20000LL;
119         break;
120     case 0xa5000000:
121         flashram_info.erase_offset = (command & 0xffff) * 128;
122         flashram_info.status = 0x1111800400c20000LL;
123         break;
124     case 0xb4000000:
125         flashram_info.mode = WRITE_MODE;
126         break;
127     case 0xd2000000:  // execute
128         switch (flashram_info.mode)
129         {
130         case NOPES_MODE:
131             break;
132         case ERASE_MODE:
133         {
134             unsigned int i;
135             flashram_read_file();
136             for (i=flashram_info.erase_offset; i<(flashram_info.erase_offset+128); i++)
137             {
138                 flashram[i^S8] = 0xff;
139             }
140             flashram_write_file();
141         }
142         break;
143         case WRITE_MODE:
144         {
145             int i;
146             flashram_read_file();
147             for (i=0; i<128; i++)
148             {
149                 flashram[(flashram_info.erase_offset+i)^S8]=
150                     ((unsigned char*)rdram)[(flashram_info.write_pointer+i)^S8];
151             }
152             flashram_write_file();
153         }
154         break;
155         case STATUS_MODE:
156             break;
157         default:
158             DebugMessage(M64MSG_WARNING, "unknown flashram command with mode:%x", (int)flashram_info.mode);
159             stop=1;
160             break;
161         }
162         flashram_info.mode = NOPES_MODE;
163         break;
164     case 0xe1000000:
165         flashram_info.mode = STATUS_MODE;
166         flashram_info.status = 0x1111800100c20000LL;
167         break;
168     case 0xf0000000:
169         flashram_info.mode = READ_MODE;
170         flashram_info.status = 0x11118004f0000000LL;
171         break;
172     default:
173         DebugMessage(M64MSG_WARNING, "unknown flashram command: %x", (int)command);
174         break;
175     }
176 }
177
178 void dma_read_flashram(void)
179 {
180     unsigned int i;
181
182     switch (flashram_info.mode)
183     {
184     case STATUS_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);
187         break;
188     case READ_MODE:
189         flashram_read_file();
190         for (i=0; i<(pi_register.pi_wr_len_reg & 0x0FFFFFF)+1; i++)
191         {
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];
194         }
195         break;
196     default:
197         DebugMessage(M64MSG_WARNING, "unknown dma_read_flashram: %x", flashram_info.mode);
198         stop=1;
199         break;
200     }
201 }
202
203 void dma_write_flashram(void)
204 {
205     switch (flashram_info.mode)
206     {
207     case WRITE_MODE:
208         flashram_info.write_pointer = pi_register.pi_dram_addr_reg;
209         break;
210     default:
211         DebugMessage(M64MSG_ERROR, "unknown dma_write_flashram: %x", flashram_info.mode);
212         stop=1;
213         break;
214     }
215 }
216