-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlash.cpp
More file actions
383 lines (344 loc) · 8.24 KB
/
Flash.cpp
File metadata and controls
383 lines (344 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* ES40 emulator.
* Copyright (C) 2007 by the ES40 Emulator Project
*
* Website: http://sourceforge.net/projects/es40
* E-mail : camiel@camicom.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Although this is not required, the author would appreciate being notified of,
* and receiving any modifications you may make to the source code that might serve
* the general public.
*/
/**
* \file
* Contains the code for the emulated Flash ROM devices.
*
* X-1.15 Camiel Vanderhoeven 30-DEC-2007
* Print file id on initialization.
*
* X-1.14 Camiel Vanderhoeven 28-DEC-2007
* Throw exceptions rather than just exiting when errors occur.
*
* X-1.13 Camiel Vanderhoeven 17-DEC-2007
* SaveState file format 2.1
*
* X-1.12 Camiel Vanderhoeven 10-DEC-2007
* Changes to make the TraceEngine work again after recent changes.
*
* X-1.11 Camiel Vanderhoeven 10-DEC-2007
* Use configurator.
*
* X-1.10 Camiel Vanderhoeven 31-MAR-2007
* Added old changelog comments.
*
* X-1.9 Camiel Vanderhoeven 16-FEB-2007
* Added functions SaveStateF and RestoreStateF.
*
* X-1.8 Brian Wheeler 13-FEB-2007
* Formatting.
*
* X-1.7 Camiel Vanderhoeven 12-FEB-2007
* Added comments.
*
* X-1.6 Camiel Vanderhoeven 9-FEB-2007
* Added comments.
*
* X-1.5 Camiel Vanderhoeven 7-FEB-2007
* Calls to trace_dev now use the TRC_DEVx macro's.
*
* X-1.4 Brian Wheeler 3-FEB-2007
* Formatting.
*
* X-1.3 Brian Wheeler 3-FEB-2007
* 64-bit literals made compatible with Linux/GCC/glibc.
*
* X-1.2 Brian Wheeler 3-FEB-2007
* Includes are now case-correct (necessary on Linux)
*
* X-1.1 Camiel Vanderhoeven 19-JAN-2007
* Initial version in CVS.
*
* \author Camiel Vanderhoeven (camiel@camicom.com / http://www.camicom.com)
**/
#include "StdAfx.h"
#include "Flash.h"
#include "System.h"
#include "AlphaCPU.h"
#define MODE_READ 0
#define MODE_STEP1 1
#define MODE_STEP2 2
#define MODE_AUTOSEL 3
#define MODE_PROGRAM 4
#define MODE_ERASE_STEP3 5
#define MODE_ERASE_STEP4 6
#define MODE_ERASE_STEP5 7
#define MODE_CONFIRM_0 8
#define MODE_CONFIRM_1 9
extern CAlphaCPU * cpu[4];
/**
* Constructor.
**/
CFlash::CFlash(CConfigurator * cfg, CSystem * c) : CSystemComponent(cfg,c)
{
if (theSROM)
FAILURE("More than one SROM!!");
theSROM = this;
c->RegisterMemory(this, 0, X64(0000080100000000),0x8000000); // 2MB
memset(state.Flash,0xff,2*1024*1024);
state.mode = MODE_READ;
printf("%s: $Id: Flash.cpp,v 1.15 2007/12/30 15:10:22 iamcamiel Exp $\n",devid_string);
}
/**
* Destructor.
**/
CFlash::~CFlash()
{
}
/**
* Read a byte from flashmemory.
* Normally, this returns one byte from flash, however, after some commands
* sent to the flash-rom, this returns identification of status information.
**/
u64 CFlash::ReadMem(int index, u64 address, int dsize)
{
u64 data = 0;
int a = (int)(address>>6);
switch (state.mode)
{
case MODE_AUTOSEL:
switch (a)
{
case 0:
data = 1; // manufacturer
break;
case 1:
data = 0xad; // device
break;
default:
data = 0;
}
break;
case MODE_CONFIRM_0:
data = 0x80;
state.mode = MODE_READ;
break;
case MODE_CONFIRM_1:
data = 0x80;
state.mode = MODE_CONFIRM_0;
break;
default:
data = state.Flash[a];
}
return data;
}
/**
* Write command or programming data to flash-rom.
**/
void CFlash::WriteMem(int index, u64 address, int dsize, u64 data)
{
int a = (int)(address>>6);
switch(state.mode)
{
case MODE_READ:
case MODE_AUTOSEL:
if ((a == 0x5555) && (data==0xaa))
{
state.mode = MODE_STEP1;
return;
}
state.mode = MODE_READ;
return;
case MODE_STEP1:
if ((a == 0x2aaa) && (data==0x55))
{
state.mode = MODE_STEP2;
return;
}
state.mode = MODE_READ;
return;
case MODE_STEP2:
if (a != 0x5555)
{
state.mode = MODE_READ;
return;
}
switch (data)
{
case 0x90:
state.mode = MODE_AUTOSEL;
return;
case 0xa0:
state.mode = MODE_PROGRAM;
return;
case 0x80:
state.mode = MODE_ERASE_STEP3;
return;
}
state.mode = MODE_READ;
return;
case MODE_ERASE_STEP3:
if ((a == 0x5555) && (data==0xaa))
{
state.mode = MODE_ERASE_STEP4;
return;
}
state.mode = MODE_READ;
return;
case MODE_ERASE_STEP4:
if ((a == 0x2aaa) && (data==0x55))
{
state.mode = MODE_ERASE_STEP5;
return;
}
state.mode = MODE_READ;
return;
case MODE_ERASE_STEP5:
if ((a == 0x5555) && (data==0x10))
{
memset(state.Flash, 0xff, 1<<21);
state.mode = MODE_CONFIRM_1;
return;
}
if (data==0x30)
{
memset(&state.Flash[(a>>16)<<16], 0xff, 1<<16);
state.mode = MODE_CONFIRM_1;
return;
}
state.mode = MODE_READ;
return;
}
// we must now be in mode program...
state.Flash[a] = (u8)data;
state.mode = MODE_READ;
}
/**
* Save state to a flash rom file.
**/
void CFlash::SaveStateF(char * fn)
{
FILE * ff;
ff = fopen(fn,"wb");
if (ff)
{
SaveState(ff);
fclose(ff);
printf("%%FLS-I-SAVEST: Flash state saved to %s\n",fn);
}
else
{
printf("%%FLS-F-NOSAVE: Flash could not be saved to %s\n",fn);
}
}
/**
* Save state to the default flash rom file.
**/
void CFlash::SaveStateF()
{
SaveStateF(myCfg->get_text_value("rom.flash","flash.rom"));
}
/**
* Restore state from a flash rom file.
**/
void CFlash::RestoreStateF(char * fn)
{
FILE * ff;
ff = fopen(fn,"rb");
if (ff)
{
RestoreState(ff);
fclose(ff);
printf("%%FLS-I-RESTST: Flash state restored from %s\n",fn);
}
else
{
printf("%%FLS-F-NOREST: Flash could not be restored from %s\n",fn);
}
}
/**
* Restore state from the default flash rom file.
**/
void CFlash::RestoreStateF()
{
RestoreStateF(myCfg->get_text_value("rom.flash","flash.rom"));
}
static u32 flash_magic1 = 0xFF3E3FF3;
static u32 flash_magic2 = 0x3FF3E3FF;
/**
* Save state to a Virtual Machine State file.
**/
int CFlash::SaveState(FILE *f)
{
long ss = sizeof(state);
fwrite(&flash_magic1,sizeof(u32),1,f);
fwrite(&ss,sizeof(long),1,f);
fwrite(&state,sizeof(state),1,f);
fwrite(&flash_magic2,sizeof(u32),1,f);
printf("flash: %d bytes saved.\n",ss);
return 0;
}
/**
* Restore state from a Virtual Machine State file.
**/
int CFlash::RestoreState(FILE *f)
{
long ss;
u32 m1;
u32 m2;
size_t r;
r = fread(&m1,sizeof(u32),1,f);
if (r!=1)
{
printf("flash: unexpected end of file!\n");
return -1;
}
if (m1 != flash_magic1)
{
printf("flash: MAGIC 1 does not match!\n");
return -1;
}
fread(&ss,sizeof(long),1,f);
if (r!=1)
{
printf("flash: unexpected end of file!\n");
return -1;
}
if (ss != sizeof(state))
{
printf("flash: STRUCT SIZE does not match!\n");
return -1;
}
fread(&state,sizeof(state),1,f);
if (r!=1)
{
printf("flash: unexpected end of file!\n");
return -1;
}
r = fread(&m2,sizeof(u32),1,f);
if (r!=1)
{
printf("flash: unexpected end of file!\n");
return -1;
}
if (m2 != flash_magic2)
{
printf("flash: MAGIC 1 does not match!\n");
return -1;
}
printf("flash: %d bytes restored.\n",ss);
return 0;
}
CFlash * theSROM = 0;