-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnand.cpp
413 lines (366 loc) · 13.1 KB
/
nand.cpp
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "ansi/w65c02.h"
#include "comm.h"
#include "state.h"
#include <cassert>
#include <cstdio>
extern WqxRom nc1020_rom;
extern nc1020_states_t nc1020_states;
static uint8_t* ram_buff = nc1020_states.ram;
static uint8_t* ram_io = nc1020_states.ram_io;
static uint64_t last_tick=0;
static deque<uint8_t> nand_cmd;
static deque<uint8_t> nand_addr;
static deque<uint8_t> nand_data;
static int nand_read_cnt=0;
//char nand_ori[65536*2][512];
char nand[65536*2+64][528];
//char nand_spare[65536+64][16];
void read_nand_file(){
memset(nand,0xff,sizeof(nand));
char *p0= &nand[64][0];
FILE *f = fopen(nc1020_rom.nandFlashPath.c_str(), "rb");
if(f==0) {
printf("file %s not exist!\n",nc1020_rom.nandFlashPath.c_str());
exit(-1);
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
fread(p0, fsize, 1, f);
fclose(f);
printf("<nand_file_size=%lu>\n",fsize);
assert(fsize + 64*528 <= sizeof(nand));
if(nc2000mode){
if(!nc2000_use_2600_rom){
//if it's not 2600 rom, then it should always be this
memcpy(&nand[0][0]+0x200+0x10 /*512+16=528*/,"ggv nc2000",strlen("ggv nc2000"));
}
else{
if(!nc2600_nandmagic_ggvsim){
//2600 physical nor expect this to be "ggv nc2010"
//nc2kutil dump shows there is a '\n' or 0x0A. But this doesn't matter. It boots either with or without `\n`
memcpy(&nand[0][0]+0x200+0x10 /*512+16=528*/,"ggv nc2010\n",strlen("ggv nc2010\n"));
}else{
memcpy(&nand[0][0]+0x200+0x10 /*512+16=528*/,"ggv nc2000",strlen("ggv nc2000"));
}
}
}
if(nc3000mode){
memcpy(&nand[0][0]+0x200+0x10 /*512+16=528*/,"ggv nc3000",strlen("ggv nc3000"));
}
}
void write_nand_file(){
FILE *f = fopen(nc1020_rom.nandFlashPath.c_str(), "wb");
fwrite(&nand[0][0]+ 64*528, num_nand_pages*528, 1 , f);
assert(num_nand_pages*528 + 64*528 <= sizeof(nand));
fclose(f);
}
void clear_nand_status(){
nand_cmd.clear();
nand_data.clear();
nand_addr.clear();
nand_read_cnt = 0;
}
uint8_t read_nand(){
bool CLE;
bool ALE;
bool CE;
if(nc3000mode){
CLE = ram_io[0x18]&0x20;
ALE = ram_io[0x18]&0x10;
CE = ram_io[0x18]&0x04;
if(CE) {
printf("read while no CE\n");
}
}
if(nc2000mode){
CLE = ram_io[0x18]&0x01;
ALE = ram_io[0x18]&0x02;
CE = ram_io[0x18]&0x40;
if(CE) {
printf("read while no CE\n");
}
}
if(CLE && ALE){
printf("oops, in nand read, both CLE and ALE true!\n");
}
//printf("tick=%lld, read %x %02x\n",tick, addr, ram_io[addr]);
uint8_t roa_bbs=ram_io[0x0a];
uint8_t ramb_vol=ram_io[0x0d];
uint8_t bs=ram_io[0x00];
///////// uint16_t p=nc1020_states.cpu.reg_pc-4;
if(enable_debug_nand) printf("tick=%llu read $29\n",tick%10000);
if(nand_cmd.size()==0) {
printf("oops! no nand cmd %d %d %d\n",CLE,ALE,CE);
return 0xff;
}
assert(nand_cmd.size()>0);
/*
special handle of read status after a long time
*/
if(nand_cmd[0]==0x70 && nand_cmd.size()==1 && nand_addr.size()==0 &&nand_data.size()==0) {
clear_nand_status();
return 0x40;
}
if(nand_cmd[0]==0x90 &&nand_cmd.size()==1 && nand_addr.size()==1 && nand_addr[0]==0x00 &&nand_data.size()==0) {
if(nand_read_cnt==0) {
nand_read_cnt++;
return 0xec;
}
if(nand_read_cnt==1) {
clear_nand_status();
return 0x75;
}
assert(false);
return 0;
}
/*
robust check
*/
if(nand_cmd[0]!=0x0 && nand_cmd[0]!=0x1 &&nand_cmd[0]!=0x60 &&nand_cmd[0]!=0x50){
printf("<<%x>>!!!\n",(unsigned char)nand_cmd[0]);
for(int i=0;i<nand_cmd.size();i++){
printf("<%x>",(unsigned char)nand_cmd[i]);
}
printf("\n");
assert(false);
return 0;
}
/*
read low/high and read spare
*/
unsigned char cmd=nand_cmd[0];
if(cmd ==0 ||cmd==1||cmd==0x50){
if(nand_cmd.size()!=1 || nand_addr.size()!=4 ||nand_data.size()!=0){
printf("oops cmd size!=5\n");
for(int i=0;i<nand_cmd.size();i++){
printf("<%x>",(unsigned char)nand_cmd[i]);
}
printf("\n");
/*if(nand_cmd.size()==1 && nand_cmd[0]==0x0){
printf("oops!! nand_cmd=[0] but trying to read\n");
return 0xff;
}*/
assert(false);
return 0xff;
}
assert(nand_cmd.size()==1 && nand_addr.size()==4 && nand_data.size()==0);
unsigned char low=nand_addr[0];
unsigned char mid=nand_addr[1];
unsigned char high=nand_addr[2];
unsigned char a25=nand_addr[3]&0x01;
uint32_t pos=a25*256u*256u+ high*256u+mid;
/*
if(nand_cmd.size()==5&&false){
printf("[%x %x]",low,high);
for(int i=0;i<nand_cmd.size();i++){
printf("<%x>",(unsigned char)nand_cmd[i]);
}
printf("\n");
exit(-1);
//printf("<%x;%x,%x:%x,%d>", final, pos, low,cmd,nand_read_cnt);
}*/
unsigned int x=pos;
unsigned int y=low;
//unsigned int pre_final= pos*528u+ y +nand_read_cnt;
//assert(pre_final%528==0);
if(cmd==0x1) y+=256u;
if(cmd==0x50) y+=512u;
unsigned int final= pos*528u+ y +nand_read_cnt;
if(nand_read_cnt!=0||cmd!=0){
//assert(final%528!=0);
if(final%528==0) printf("warn: read %04x accross 528 boundary\n",final);
}
//final-=32*1024;
if(nand_read_cnt==0 && enable_debug_nand){
printf("[%x %x]",low,high);
for(int i=0;i<nand_cmd.size();i++){
printf("<%x>",(unsigned char)nand_cmd[i]);
}
printf("<%x;%x,%x:%x,%d>\n", final, pos, low,cmd,nand_read_cnt);
}
char *p=&nand[0][0];
uint8_t result=p[final];
//if(final<0) return 0x00;
//uint8_t result=nand[pos][low+off+nand_read_cnt];
nand_read_cnt++;
//printf("<<%02x>>",result);
return result;
}
assert(false);
}
void debug_show_nand_cmd(){
if(enable_debug_nand)
{
for(int i=0;i<nand_cmd.size();i++){
printf("<%02x>",(unsigned char)nand_cmd[i]);
}
printf("\n");
}
}
void nand_write(uint8_t value){
bool CLE;
bool ALE;
bool CE;
if(nc3000mode){
CLE = ram_io[0x18]&0x20;
ALE = ram_io[0x18]&0x10;
CE = ram_io[0x18]&0x04;
}
if(nc2000mode){
CLE = ram_io[0x18]&0x01;
ALE = ram_io[0x18]&0x02;
CE = ram_io[0x18]&0x40;
}
if(CLE && ALE){
printf("oops, in nand write, both CLE and ALE true!\n");
return;
}
//printf("tick=%llu write $29 %x CLE=%d ALE=%d %d\n",tick%10000,value,CLE,ALE,(int)nand_cmd.size());
if(enable_debug_nand) printf("tick=%llu write $29 %x CLE=%d ALE=%d\n",tick%10000,value,CLE,ALE);
//printf("tick=%lld, write %x %02x\n",tick, addr, value);
uint8_t roa_bbs=ram_io[0x0a];
uint8_t ramb_vol=ram_io[0x0d];
uint8_t bs=ram_io[0x00];
if(CLE){
//note: the datasheet says 0xff doesn't need CLE, but in wqx code seems like CLE is always enabled when 0xff is used
if(value ==0xff || value == 0x00|| value==0x01 || value ==0x50 ||value==0x60||value ==0x70||value==0x90){
debug_show_nand_cmd();
if(nand_cmd.size()>0){
if(nand_cmd.size()==1 && nand_addr.size()==4 && nand_data.size()==0) assert(nand_cmd[0]==0x00||nand_cmd[0]==0x01||nand_cmd[0]==0x50);
else if(nand_cmd.size()==2 && nand_addr.size()==3 &&nand_data.size()==0) assert(nand_cmd[0]==0x60);
else assert(false);
}
clear_nand_status();
if(value!=0xff){
nand_cmd.push_back(value);
}
goto out;
}
if(value ==0x10) {
if(nand_cmd[0]==0x50 && nand_cmd.size()== 2&& nand_addr.size()==4 && nand_data.size()==16) {
assert(nand_cmd[1]==0x80);
unsigned char low=nand_addr[0];
unsigned char mid=nand_addr[1];
unsigned char high=nand_addr[2];
unsigned char a25=nand_addr[3]&0x01;
uint32_t pos=a25*256u*256u+high*256u+mid;
unsigned int x=pos;
unsigned int y=low;
unsigned int final= pos*528u+ y +512;
assert((final-512)%(528)==0);
unsigned char *p=(unsigned char*)&nand[0][0];
bool warn=false;
for(int i=0;i<16;i++){
if(p[final+i]!=0xff){
warn=true;
//this is allowed, but wqx's software always erase before write
if(correct_non_erased_nand_write) p[final+i]=0xff;
}
p[final+i]&=nand_data[i];
}
if(warn){
printf("oops writing to non-erased byte at %x!!!!!!!!!!\n",final);
}
printf("nand program spare, offset=%x\n",final);
clear_nand_status();
}
else if(nand_cmd[0]==0x0 && nand_cmd.size()==2 && nand_addr.size()==4 && nand_data.size()==528){
assert(nand_cmd[1]==0x80);
unsigned char low=nand_addr[0];
unsigned char mid=nand_addr[1];
unsigned char high=nand_addr[2];
unsigned char a25=nand_addr[3]&0x01;
uint32_t pos=a25*256u*256u+high*256u+mid;
unsigned int x=pos;
unsigned int y=low;
unsigned int final= pos*528u+ y;
assert(final%(528)==0);
unsigned char *p=(unsigned char*)&nand[0][0];
printf("nand program, offset=%x\n",final);
bool warn=false;
for(int i=0;i<528;i++){
if(p[final+i]!=0xff){
warn=true;
//this is allowed, but wqx's software always erase before write
if(correct_non_erased_nand_write) p[final+i]=0xff;
}
p[final+i]&=nand_data[i];
}
if(warn){
printf("oops writing to non-erased byte at %x!!!!!!!!!!\n",final);
}
clear_nand_status();
}
else{
debug_show_nand_cmd();
printf("unexpected situation for cmd 0x10 %d",(int)nand_cmd.size());
assert(false);
}
goto out;
}
if(value==0xd0||value==0x80){
if(value==0xd0){
assert(nand_cmd.size()==1);
assert(nand_cmd[0]==0x60);
assert(nand_addr.size()==3);
assert(nand_data.size()==0);
unsigned char low=nand_addr[0];
unsigned char mid=nand_addr[1];
unsigned char high=nand_addr[2]&0x01;
unsigned int final= (high*256u*256u + mid*256u+low)*528u;
nand_read_cnt++;
char *p=&nand[0][0];
printf("nand erase!!! %x tick=%lld pc=%x %x\n",final,tick, mPC, ram_io[0x00]);
assert(final%(32*528)==0);
assert(final +32*528 <= sizeof(nand));
memset(p+final,0xff,32*528);
}
if(value==0x80){
assert(nand_cmd.size()>=1);
assert(nand_cmd[0]==0x50||nand_cmd[0]==0x00);
assert(nand_addr.size()==0);
assert(nand_data.size()==0);
}
nand_cmd.push_back(value);
goto out;
}
assert(false);
}
if(ALE){
if(nand_cmd.size()==0){
printf("got addr %02x while nand_cmd is empty\n",value);
assert(false);
}
nand_addr.push_back(value);
goto out;
}
if(nand_cmd.size()!=0) {
if(nand_cmd[0]==0x50) {
assert(nand_cmd.size()>=2);
assert(nand_cmd[1]==0x80);
assert(nand_cmd.size()<22);
}else if (nand_cmd[0]==0x00){
assert(nand_cmd.size()>=2);
assert(nand_cmd[1]==0x80);
assert(nand_cmd.size()<534);
}else{
assert(false);
}
nand_data.push_back(value);
}
else{
for(int i=0;i<nand_cmd.size();i++){
printf("<%02x>",nand_cmd[i]);
}
printf("[%02x]\n",value);
printf("got data %02x while nand_cmd is empty\n",value);
assert(false);
}
goto out;
out:;
// the out label here is for put some print cmd for debug
//if(nand_cmd.size()==1&& nand_cmd[0]==0xff) nand_cmd.clear();
//printf("bs=%x roa_bbs=%x pc=%x %x %x %x %x \n",ram_io[0x00], ram_io[0x0a], reg_pc, Peek16(p), Peek16(p+1),Peek16(p+2),Peek16(p+3));
//if(do_inject) wanna_inject=true;
}