forked from autch/piemu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflash.c
251 lines (228 loc) · 6.04 KB
/
flash.c
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
/*
* flash.c
*
* P/EMU - P/ECE Emulator
* Copyright (C) 2003 Naoyuki Sawa
*
* * Mon Apr 14 00:00:00 JST 2003 Naoyuki Sawa
* - 作成開始。
*/
#include "app.h"
/****************************************************************************
*
****************************************************************************/
#define SECTOR_SIZE 0x01000 /* セクタ 消去単位= 4KB */
#define BLOCK_SIZE 0x10000 /* ブロック消去単位=64KB */
/****************************************************************************
* グローバル変数
****************************************************************************/
//FLASH flash;
/****************************************************************************
* グローバル関数
****************************************************************************/
void
flash_init(PIEMU_CONTEXT* context)
{
#define FLASH_TOP 0x0c00000
#define READ_UNIT 0x1000
int size, bits;
memset(&context->flash, 0, sizeof context->flash);
/* CFIクエリー情報作成。(とりあえず必要なフィールドだけ) */
size = (int)context->emu.sysinfo.pffs_end - FLASH_TOP;
bits = 0;
for(;;) {
if(1 << bits >= size) break;
bits++;
}
context->flash.cfiinfo.device_size = bits;
/* メモリ割り当て。 */
context->flash.mem_size = 1 << context->flash.cfiinfo.device_size;
context->flash.mem = (unsigned char*)calloc(context->flash.mem_size, 1);
/* イメージ読み込み。 */
context->pfnLoadFlashImage(context, &context->flash, context->pUser);
#undef FLASH_TOP
#undef READ_UNIT
}
#define FLASH_READ(ofs, size, type) \
{ \
ofs &= context->flash.mem_size - 1; \
switch(context->flash.stat) \
{ \
case FLASH_NORMAL: \
return READ_MEM(context->flash.mem + ofs, type); \
case FLASH_CFI_QUERY: \
ofs -= CFIINFO_OFFSET * sizeof(short); \
if(ofs > sizeof(CFIINFO)) DIE(); \
return READ_MEM((char*)&context->flash.cfiinfo + ofs, type); \
} \
DIE(); \
return -1; /* 警告抑制 */ \
}
int
flash_read(PIEMU_CONTEXT* context, unsigned ofs, int size)
{
ofs &= context->flash.mem_size - 1;
switch(context->flash.stat) {
case FLASH_NORMAL:
switch(size) {
case 1: return READ_MEM_B(context->flash.mem + ofs);
case 2: return READ_MEM_H(context->flash.mem + ofs);
case 4: return READ_MEM_W(context->flash.mem + ofs);
}
DIE();
case FLASH_CFI_QUERY:
/* ※ソフトウェアIDモード未対応 */
ofs -= CFIINFO_OFFSET * sizeof(short);
if(ofs > sizeof(CFIINFO)) DIE();
switch(size) {
case 1: return READ_MEM_B((char*)&context->flash.cfiinfo + ofs);
case 2: return READ_MEM_H((char*)&context->flash.cfiinfo + ofs);
case 4: return READ_MEM_W((char*)&context->flash.cfiinfo + ofs);
}
DIE();
}
DIE();
return -1; /* 警告抑制 */
}
void
flash_write(PIEMU_CONTEXT* context, unsigned ofs, int data, int size)
{
int cmd;
if(size != 2) DIE(); /* 書き込みはハーフワード単位のみ! */
ofs &= context->flash.mem_size - 1;
cmd = data & 0xff;
/* WORD PROGRAM以外での0xf0書き込みは、汎用EXITコマンドと見なします。 */
if(context->flash.stat != FLASH_WORD_PROGRAM && cmd == 0xf0) {
context->flash.stat = FLASH_NORMAL; /* MANUAL EXIT */
return;
}
switch(context->flash.stat) {
case FLASH_NORMAL:
if(ofs == 0x5555 * 2 && cmd == 0xaa) {
context->flash.stat = FLASH_ENTER1;
} else {
DIE();
}
break;
case FLASH_ENTER1:
if(ofs == 0x2aaa * 2 && cmd == 0x55) {
context->flash.stat = FLASH_ENTER2;
} else {
DIE();
}
break;
case FLASH_ENTER2:
if(ofs == 0x5555 * 2) {
switch(cmd) {
case 0x80:
context->flash.stat = FLASH_ENTER3;
break;
case 0x90: /* SOFTWARE ID と */
case 0x98: /* CFI QUERY は、同じものとして扱う */
context->flash.stat = FLASH_CFI_QUERY;
break;
case 0xa0:
context->flash.stat = FLASH_WORD_PROGRAM;
break;
default:
DIE();
}
} else {
DIE();
}
break;
case FLASH_ENTER3:
if(ofs == 0x5555 * 2 && cmd == 0xaa) {
context->flash.stat = FLASH_ENTER4;
} else {
DIE();
}
break;
case FLASH_ENTER4:
if(ofs == 0x2aaa * 2 && cmd == 0x55) {
context->flash.stat = FLASH_ENTER5;
} else {
DIE();
}
break;
case FLASH_ENTER5:
switch(cmd) {
case 0x10: /* CHIP ERASE */
if(ofs == 0x5555 * 2) {
// dbg("CHIP ERASE");
memset(context->flash.mem, -1, context->flash.mem_size);
context->flash.stat = FLASH_NORMAL; /* AUTO EXIT */
} else {
DIE();
}
break;
case 0x30: /* SECTOR ERASE */
ofs &= ~(SECTOR_SIZE - 1);
// dbg("SECTOR ERASE: %07x", ofs);
memset(&context->flash.mem[ofs], -1, SECTOR_SIZE);
context->flash.stat = FLASH_NORMAL; /* AUTO EXIT */
break;
case 0x50: /* BLOCK ERASE */
ofs &= ~(BLOCK_SIZE - 1);
// dbg("BLOCK ERASE: %07x", ofs);
memset(&context->flash.mem[ofs], -1, BLOCK_SIZE);
context->flash.stat = FLASH_NORMAL; /* AUTO EXIT */
break;
default:
DIE();
}
break;
case FLASH_CFI_QUERY:
if((ofs == 0x5555 * 2 && cmd == 0xaa) ||
(ofs == 0x2aaa * 2 && cmd == 0x55)) {
/* 長い方のEXITシーケンス三命令(aa-55-f0)のうち、先行する二命令を無視します。
* 最後の一命令は汎用EXITコマンドとして、この関数の最初でまとめて処理しています。
*/
} else {
DIE();
}
break;
case FLASH_WORD_PROGRAM:
// dbg("WORD PROGRAM: %07x=%04x", ofs, data & 0xffff);
*(short*)&context->flash.mem[ofs] = data;
context->flash.stat = FLASH_NORMAL; /* AUTO EXIT */
break;
default:
DIE();
}
}
IOHANDLER_R(flash, B)
{
FLASH_READ(ofs, size, char);
}
IOHANDLER_W(flash, B)
{
DIE();
}
IOHANDLER_R(flash, H)
{
FLASH_READ(ofs, size, short);
}
IOHANDLER_W(flash, H)
{
flash_write(context, ofs, data, 2);
}
IOHANDLER_R(flash, W)
{
ofs &= context->flash.mem_size - 1;
switch(context->flash.stat)
{
case FLASH_NORMAL:
return READ_MEM_W(context->flash.mem + ofs);
case FLASH_CFI_QUERY:
ofs -= CFIINFO_OFFSET * sizeof(short);
if(ofs > sizeof(CFIINFO)) DIE();
return READ_MEM_W((char*)&context->flash.cfiinfo + ofs);
}
DIE();
return -1; /* 警告抑制 */
}
IOHANDLER_W(flash, W)
{
DIE();
}