~ubuntu-branches/ubuntu/wily/mupen64plus/wily

« back to all changes in this revision

Viewing changes to memory/flashram.c

  • Committer: Bazaar Package Importer
  • Author(s): Sven Eckelmann
  • Date: 2011-07-24 14:23:26 UTC
  • mfrom: (10.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110724142326-x9z5qu8j9jecrmod
Tags: 1.99.4+2
* Upload to unstable
* Remove overrides for lintian warning about change to native package
* Update Vcs-* fields to new anonscm.debian.org URLs in debian/control
* Fix spelling of "Flexible" in debian/control (Closes: #633693)
* Mark all targets in debian/rules as phony
* Add some information about the mupen64plus 2.0 vision in debian/NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 <stdlib.h>
24
 
 
25
 
#include "memory.h"
26
 
 
27
 
#include "../r4300/r4300.h"
28
 
 
29
 
#include "../main/main.h"
30
 
 
31
 
int use_flashram;
32
 
 
33
 
typedef enum flashram_mode
34
 
{
35
 
   NOPES_MODE = 0,
36
 
   ERASE_MODE,
37
 
   WRITE_MODE,
38
 
   READ_MODE,
39
 
   STATUS_MODE
40
 
} Flashram_mode;
41
 
 
42
 
static int mode;
43
 
static unsigned long long status;
44
 
static unsigned char flashram[0x20000];
45
 
static unsigned int erase_offset, write_pointer;
46
 
 
47
 
void save_flashram_infos(char *buf)
48
 
{
49
 
    memcpy(buf+0 , &use_flashram , 4);
50
 
    memcpy(buf+4 , &mode            , 4);
51
 
    memcpy(buf+8 , &status         , 8);
52
 
    memcpy(buf+16, &erase_offset , 4);
53
 
    memcpy(buf+20, &write_pointer, 4);
54
 
}
55
 
 
56
 
void load_flashram_infos(char *buf)
57
 
{
58
 
    memcpy(&use_flashram , buf+0 , 4);
59
 
    memcpy(&mode            , buf+4 , 4);
60
 
    memcpy(&status         , buf+8 , 8);
61
 
    memcpy(&erase_offset , buf+16, 4);
62
 
    memcpy(&write_pointer, buf+20, 4);
63
 
}
64
 
 
65
 
void init_flashram()
66
 
{
67
 
    mode = NOPES_MODE;
68
 
    status = 0;
69
 
}
70
 
 
71
 
unsigned int flashram_status()
72
 
{
73
 
    return (status >> 32);
74
 
}
75
 
 
76
 
void flashram_command(unsigned int command)
77
 
{
78
 
    switch(command & 0xff000000)
79
 
    {
80
 
        case 0x4b000000:
81
 
            erase_offset = (command & 0xffff) * 128;
82
 
            break;
83
 
        case 0x78000000:
84
 
            mode = ERASE_MODE;
85
 
            status = 0x1111800800c20000LL;
86
 
            break;
87
 
        case 0xa5000000:
88
 
            erase_offset = (command & 0xffff) * 128;
89
 
            status = 0x1111800400c20000LL;
90
 
            break;
91
 
        case 0xb4000000:
92
 
            mode = WRITE_MODE;
93
 
            break;
94
 
        case 0xd2000000:  // execute
95
 
            switch (mode)
96
 
            {
97
 
                case NOPES_MODE:
98
 
                    break;
99
 
                case ERASE_MODE:
100
 
                {
101
 
                   char *filename;
102
 
                   FILE *f;
103
 
                   int i;
104
 
                   filename = malloc(strlen(get_savespath())+
105
 
                              strlen(ROM_SETTINGS.goodname)+4+1);
106
 
                   strcpy(filename, get_savespath());
107
 
                   strcat(filename, ROM_SETTINGS.goodname);
108
 
                   strcat(filename, ".fla");
109
 
                   f = fopen(filename, "rb");
110
 
                   if (f)
111
 
                   {
112
 
                       fread(flashram, 1, 0x20000, f);
113
 
                       fclose(f);
114
 
                   }
115
 
                   else
116
 
                   {
117
 
                       for (i=0; i<0x20000; i++)
118
 
                       {
119
 
                           flashram[i] = 0xff;
120
 
                       }
121
 
                   }
122
 
                   for (i=erase_offset; i<(erase_offset+128); i++)
123
 
                   {
124
 
                       flashram[i^S8] = 0xff;
125
 
                   }
126
 
                   f = fopen(filename, "wb");
127
 
                   if (f == NULL)
128
 
                   {
129
 
                       printf("Warning: couldn't open flashram file '%s' for erasing.\n", filename);
130
 
                   }
131
 
                   else
132
 
                   {
133
 
                       fwrite(flashram, 1, 0x20000, f);
134
 
                       fclose(f);
135
 
                   }
136
 
                   free(filename);
137
 
                }
138
 
                    break;
139
 
                case WRITE_MODE:
140
 
                {
141
 
                    char *filename;
142
 
                    FILE *f;
143
 
                    int i;
144
 
                    filename = malloc(strlen(get_savespath())+
145
 
                               strlen(ROM_SETTINGS.goodname)+4+1);
146
 
                    strcpy(filename, get_savespath());
147
 
                    strcat(filename, ROM_SETTINGS.goodname);
148
 
                    strcat(filename, ".fla");
149
 
                    f = fopen(filename, "rb");
150
 
                    if (f)
151
 
                    {
152
 
                        fread(flashram, 1, 0x20000, f);
153
 
                        fclose(f);
154
 
                    }
155
 
                    else
156
 
                    {
157
 
                        for (i=0; i<0x20000; i++)
158
 
                        {
159
 
                            flashram[i] = 0xff;
160
 
                        }
161
 
                    }
162
 
                    for (i=0; i<128; i++)
163
 
                    {
164
 
                        flashram[(erase_offset+i)^S8]=
165
 
                        ((unsigned char*)rdram)[(write_pointer+i)^S8];
166
 
                    }
167
 
                    f = fopen(filename, "wb");
168
 
                    if (f == NULL)
169
 
                    {
170
 
                        printf("Warning: couldn't open flashram file '%s' for writing.\n", filename);
171
 
                    }
172
 
                    else
173
 
                    {
174
 
                        fwrite(flashram, 1, 0x20000, f);
175
 
                        fclose(f);
176
 
                    }
177
 
                    free(filename);
178
 
                }
179
 
                    break;
180
 
                case STATUS_MODE:
181
 
                    break;
182
 
                default:
183
 
                    printf("unknown flashram command with mode:%x\n", (int)mode);
184
 
                    stop=1;
185
 
                    break;
186
 
            }
187
 
            mode = NOPES_MODE;
188
 
            break;
189
 
        case 0xe1000000:
190
 
            mode = STATUS_MODE;
191
 
            status = 0x1111800100c20000LL;
192
 
            break;
193
 
        case 0xf0000000:
194
 
            mode = READ_MODE;
195
 
            status = 0x11118004f0000000LL;
196
 
            break;
197
 
        default:
198
 
            printf("unknown flashram command:%x\n", (int)command);
199
 
            //stop=1;
200
 
            break;
201
 
      }
202
 
}
203
 
 
204
 
void dma_read_flashram()
205
 
{
206
 
    int i;
207
 
    char *filename;
208
 
    FILE *f;
209
 
    
210
 
    switch(mode)
211
 
    {
212
 
        case STATUS_MODE:
213
 
            rdram[pi_register.pi_dram_addr_reg/4] = (unsigned int)(status >> 32);
214
 
            rdram[pi_register.pi_dram_addr_reg/4+1] = (unsigned int)(status);
215
 
            break;
216
 
        case READ_MODE:
217
 
            filename = malloc(strlen(get_savespath())+
218
 
                       strlen(ROM_SETTINGS.goodname)+4+1);
219
 
            strcpy(filename, get_savespath());
220
 
            strcat(filename, ROM_SETTINGS.goodname);
221
 
            strcat(filename, ".fla");
222
 
            f = fopen(filename, "rb");
223
 
            if (f)
224
 
            {
225
 
                fread(flashram, 1, 0x20000, f);
226
 
                fclose(f);
227
 
            }
228
 
            else
229
 
            {
230
 
                for (i=0; i<0x20000; i++) 
231
 
                {
232
 
                    flashram[i] = 0xff;
233
 
                }
234
 
            }
235
 
            free(filename);
236
 
            for (i=0; i<(pi_register.pi_wr_len_reg & 0x0FFFFFF)+1; i++)
237
 
            {
238
 
                ((unsigned char*)rdram)[(pi_register.pi_dram_addr_reg+i)^S8]=
239
 
                flashram[(((pi_register.pi_cart_addr_reg-0x08000000)&0xFFFF)*2+i)^S8];
240
 
            }
241
 
            break;
242
 
        default:  
243
 
            printf("unknown dma_read_flashram:%x\n", mode);
244
 
            stop=1;
245
 
            break;
246
 
      }
247
 
}
248
 
 
249
 
void dma_write_flashram()
250
 
{
251
 
    switch(mode)
252
 
    {
253
 
        case WRITE_MODE:
254
 
            write_pointer = pi_register.pi_dram_addr_reg;
255
 
            break;
256
 
        default:
257
 
            printf("unknown dma_read_flashram:%x\n", mode);
258
 
            stop=1;
259
 
            break;
260
 
    }
261
 
}
262