-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFlashCart.sbp
More file actions
828 lines (648 loc) · 21.2 KB
/
FlashCart.sbp
File metadata and controls
828 lines (648 loc) · 21.2 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/* ported from: https:'github.com/sanni/cartreader/blob/master/Cart_Reader/N64.ino
*
*/
Sub flash_printID(baseAdr As DWord)
NUS_SetAddress(baseAdr + (&H555 << 1))
NUS_writeData(&HAA)
NUS_SetAddress(baseAdr + (&H2AA << 1))
NUS_writeData(&H55)
NUS_SetAddress(baseAdr + (&H555 << 1))
NUS_writeData(&H90)
'' Read manufacturer ID
NUS_SetAddress(baseAdr)
NUS_readRequest(&H100)
'NUS_readRequest(&H8)
' NUS_SetAddress(baseAdr + &H100)
' NUS_readRequest(&H90)
' NUS_SetAddress(baseAdr + &H1c)
' NUS_readRequest(&H2)
'RESET
NUS_SetAddress(baseAdr )
NUS_writeData(&HFF)
Dim inbuf[&H200] As Byte, inSize As DWord
FillMemory(inbuf, &H200, &HCC)
FTDI_SendCommand()
FT_Read(hFT, inbuf, &H200, inSize)
' FT_Read(hFT, inbuf+&H20, &H120+4, inSize)
Print "ID-CFI area"
Dump(inbuf, &H200, baseAdr)
Dim mfID As Word, deviceID As Word, second_id As DWord
mfID = ChangeEndianWord(GetWord(inbuf+0))
deviceID = ChangeEndianWord(GetWord(inbuf+2))
second_id = ChangeEndianWord(GetWord(inbuf+&H1c))
printf(ex"ManufactureID : %04X DeviceID : %04X, SencondDevId:%x\n", mfID, deviceID, second_id)
'https:'www.jedec.org/sites/default/files/docs/JEP106AJ.pdf
if mfID = &H89 then
Print "Intel"
select case deviceID
Case &H8817
Print "-T 64Mbit"
Case &H881A
Print "-B 64Mbit"
Case &H8818
Print "-T 128Mbit"
Case &H881B
Print "-B 128Mbit"
Case &H8819
Print "-T 256Mbit"
Case &H881C
Print "-B 256Mbit"
End Select
' See intel dataseet
Dim block As Word, RCR As Word
block = ChangeEndianWord(GetWord(inbuf+4))
printf(ex"BLC = %04X ...\n", block)
if block And 1 then
Print " - Block is Locked"
else
Print " - Block is Unlocked"
endif
if block And 2 then
Print " - Block is Locked-Down"
else
Print " - Block is not Locked-Down"
endif
RCR = ChangeEndianWord(GetWord(inbuf+10))
printf(ex"RCR = %04X ...\n", RCR)
if RCR And &H8000 then
Print " - Read Mode: Asynchronous page-mode read (default)"
else
Print " - Read Mode: Synchronous burst-mode read"
endif
printf(ex" - Latency: Code%x\n", (RCR>>11) And &H7)
if RCR And &H0400 then
Print " - Wait Polarity: WAIT signal is active high (default)"
else
Print " - Wait Polarity: WAIT signal is active low"
endif
if RCR And &H0200 then
Print " - Data Hold: Data held for a 2-clock data cycle (default)"
else
Print " - Data Hold: Data held for a 1-clock data cycle"
endif
if RCR And &H0100 then
Print " - Wait Delay: WAIT deasserted one data cycle before valid data (default)"
else
Print " - Wait Delay: WAIT deasserted with valid data"
endif
if RCR And &H0080 then
Print " - Burst Sequence: Linear (default)"
else
Print " - Burst Sequence: Reserved"
endif
if RCR And &H0040 then
Print " - Clock Edge: Rising edge (default)"
else
Print " - Clock Edge: Falling edge"
endif
if RCR And &H0008 then
Print " - Burst Wrap: No Wrap; Burst accesses do not wrap within burst length (default)"
else
Print " - Burst Wrap: Wrap; Burst accesses wrap within burst length set by BL[2:0]"
endif
select case RCR And &H7
Case 1
Print " - Burst Length: 4-word burst"
Case 2
Print " - Burst Length: 8-word burst"
Case 3
Print " - Burst Length: 6-word burst"
Case 7
Print " - Burst Length: Continuous-word burst"
Case else
Print " - Burst Length: reserved"
End Select
Print
Elseif mfID = 1 And deviceID=&H227E then
Print "Infineon? S29GL series"
Select case second_id
Case &H2228
Print "1Gbit"
Case &H2223
Print "512Mbit"
Case &H2222
Print "256Mbit"
Case &H2221
Print "128Mbit"
Case Else
Print "Unkwon"
End Select
endif
End Sub
Function IntelFlash_SetReadConfigRegister(baseAdr As DWord) As BOOL
' unlock
/* NUS_SetAddress(baseAdr)
NUS_writeData(&H60)
NUS_SetAddress(baseAdr)
NUS_writeData(&HD0)
*/
NUS_SetAddress(baseAdr)
NUS_writeData(&HFF)
' Dim RCR = &HBFCF As DWord 'default
Dim RCR = &H009FCF As DWord 'Code3
NUS_SetAddress(baseAdr Or (RCR<<1))
NUS_writeData(&H60)
NUS_SetAddress(baseAdr Or (RCR<<1))
NUS_writeData(&H03)
FTDI_SendCommand()
' flash_printID(baseAdr)
End Function
Function IntelFlash_WaitWritable(baseAddr As DWord) As BOOL
Dim retry As DWord, status As DWord, inSize As DWord
Do
NUS_SetAddress(baseAddr)
FTDI_SendCommand()
NUS_readRequest(1)
FTDI_SendCommand()
FT_Read(hFT, VarPtr(status), 2, inSize)
if status And &H7FFF then
printf(ex"\t\t\t\t[info] Status = %08X, %d \t\t\t\t\r", status, retry)
' Clear Error
NUS_SetAddress(baseAddr)
NUS_writeData(&H50)
if retry>&HFFFF then ExitDo
endif
if status And &H8000 then _
ExitDo
retry++
Loop
if status And &H0200 then _
printf(ex"[Error][FlashStatusCheck] Device Protect Error\n")
if status And &H0800 then _
printf(ex"[Error][FlashStatusCheck] Program Error\n")
if status And &H1000 then _
printf(ex"[Error][FlashStatusCheck] Vpp Range Error\n")
if retry<&HFFFF then
IntelFlash_WaitWritable = TRUE
else
IntelFlash_WaitWritable = FALSE
endif
End Function
Sub InterlFlash_Print(status As Word)
printf(ex"status: %04X ... ", status)
if (status And &H8000) = 0 then _
printf(ex"[DWS:Write not ready]")
if status And &H4000 then _
printf(ex"[ESS:Erase suspend]")
if status And &H2000 then _
printf(ex"[ES:Erase fail or program error]")
if status And &H1000 then _
printf(ex"[PS:Program failed]")
if status And &H0800 then _
printf(ex"[VPPS:Vpp error. Vpp < VppLK]")
if status And &H0400 then _
printf(ex"[PSS:Program susupend]")
if status And &H0200 then _
printf(ex"[BLS:Block]")
if status And &H0100 then _
printf(ex"[BWS:BEEP in-progress]")
End Sub
Const INTERL_WAIT_BUFFER_RETRY = 2000
Function IntelFlash_WaitBuffer(baseAddr As DWord) As BOOL
Dim retry As DWord, status As DWord, inSize As DWord
Do
NUS_SetAddress(baseAddr)
FTDI_SendCommand()
NUS_readRequest(1)
FTDI_SendCommand()
FT_Read(hFT, VarPtr(status), 2, inSize)
if status And &H7FFF then
InterlFlash_Print(status)
printf(ex" []\r")
endif
if status And &H8000 then ExitDo
retry++
if (retry Mod 1000) = 0 then
InterlFlash_Print(status)
printf(ex":::[Warning] Waiting write buffer... retry %d\n", retry)
endif
if retry > INTERL_WAIT_BUFFER_RETRY then
InterlFlash_Print(status)
printf(ex":::[Error] Wait write buffer timeout.\n", retry)
IntelFlash_WaitBuffer = FALSE
ExitFunction
endif
Loop
IntelFlash_WaitBuffer = TRUE
End Function
Const INTERL_FLASH_CMD_BUFFER_WRITE = &HE8
Const INTERL_FLASH_CMD_READ_STATUS = &H70
Const INTERL_FLASH_CMD_CLEAR_STATUS = &H50
Const INTERL_FLASH_CMD_DO_WRITE = &HD0
Sub IntelFhash_WriteFile(baseAdr As DWord, fileName As BytePtr)
N64PowerON()
FTDI_SendCommand()
IntelFhash_Reset(baseAdr)
IntelFlash_SetReadConfigRegister(baseAdr)
Const IF_WRITE_PAGE_SIZE = 64
Dim in As File, fileSize As DWord
if in.openFile(fileName, GENERIC_READ) = FALSE then
Print "Open Err"
ExitFunction
endif
fileSize = in.length()
Dim buf[IF_WRITE_PAGE_SIZE] As Byte
Dim totalBytes As DWord, i As DWord
For totalBytes = 0 To fileSize-1 Step 64
'unlock
NUS_SetAddress(baseAdr + totalBytes)
NUS_writeData(&H60)
NUS_SetAddress(baseAdr + totalBytes)
NUS_writeData(&HD0)
' Read File
in.read(buf, IF_WRITE_PAGE_SIZE)
'Dump(buf, 64, totalBytes + baseAdr)
if (totalBytes Mod 16384) = 0 then _
printf(ex"[WriteIntelFlash] %Xh- %dMB/%dMB %d%% \n", _
baseAdr + totalBytes, _
totalBytes/(1024*1024), _
fileSize/(1024*1024), _
totalBytes*100/fileSize
)
' Buffered program command
NUS_SetAddress(baseAdr + totalBytes)
NUS_writeData(INTERL_FLASH_CMD_BUFFER_WRITE)
FTDI_SendCommand()
'Wait buffer available
if IntelFlash_WaitBuffer(baseAdr + totalBytes) = FALSE then
' if IntelFlash_WaitWritable(baseAdr + totalBytes) = FALSE then
printf(ex"WriteError\n")
' Issue Read Statu Register Command
NUS_SetAddress(baseAdr + totalBytes)
NUS_writeData(INTERL_FLASH_CMD_READ_STATUS)
Goto *READ_STATUS_REG2
endif
' Write word count (minus 1)
NUS_SetAddress(baseAdr + totalBytes)
NUS_writeData(&H1F)
FTDI_SendCommand()
For i = 0 To IF_WRITE_PAGE_SIZE-1 Step 2
' printf(ex"\t%08X :: %04X\n", baseAdr + totalBytes + i, ((buf[ i ] << 8) And &HFF00) Or (buf[ i+1 ] And &HFF))
NUS_SetAddress(baseAdr + totalBytes + i)
NUS_writeData( ((buf[ i ] << 8) And &HFF00) Or (buf[ i+1 ] And &HFF) )
Next i
FTDI_SendCommand()
NUS_SetAddress(baseAdr + totalBytes + 62)
NUS_writeData(INTERL_FLASH_CMD_DO_WRITE)
*READ_STATUS_REG2
if IntelFlash_WaitBuffer(baseAdr + totalBytes + 62) = FALSE then
printf(ex"IntelFlash_WaitBuffer ... READ_STATUS_REG2 fail\n")
ExitFor
endif
' Full Status Check
Dim status As Word, dwab As DWord
NUS_SetAddress(baseAdr + totalBytes + 62)
NUS_writeData(INTERL_FLASH_CMD_READ_STATUS)
FTDI_SendCommand()
NUS_SetAddress(baseAdr + totalBytes + 62)
NUS_readRequest(1)
FTDI_SendCommand()
FT_Read(hFT, VarPtr(status), 2, dwab)
if status And &H7FFF then
'printf(ex"[WriteIntelFlash] status = %04X ", status)
printf(ex" (clear) ")
InterlFlash_Print(status)
' printf(ex"\t\t\t\t\t\t")
printf(ex"\n")
' Clear error status
NUS_SetAddress(baseAdr + totalBytes)
NUS_writeData(INTERL_FLASH_CMD_CLEAR_STATUS)
FTDI_SendCommand()
endif
' printf(ex"\n")
' ExitFor
Next totalBytes
*WRITE_EXIT
IntelFhash_Reset(baseAdr)
FTDI_SendCommand()
in.close()
N64PowerOFF()
FTDI_SendCommand()
End Sub
' ChipEraseは聞かなかった
Sub IntelFhash_ChipErase(baseAdr As DWord)
N64PowerON()
IntelFhash_Reset(baseAdr)
NUS_SetAddress(baseAdr + (&H555 << 1))
NUS_writeData(&HAA)
NUS_SetAddress(baseAdr + (&H2AA << 1))
NUS_writeData(&H55)
NUS_SetAddress(baseAdr + (&H555 << 1))
NUS_writeData(&H80)
NUS_SetAddress(baseAdr + (&H555 << 1))
NUS_writeData(&HAA)
NUS_SetAddress(baseAdr + (&H2AA << 1))
NUS_writeData(&H55)
NUS_SetAddress(baseAdr + (&H555 << 1))
NUS_writeData(&H10)
FTDI_SendCommand()
IntelFlash_WaitWritable(baseAdr)
Print
IntelFhash_Reset(baseAdr)
N64PowerOFF()
End Sub
Sub IntelFhash_Reset(baseAdr As DWord)
NUS_SetAddress(baseAdr )
NUS_writeData(&HFF)
End Sub
'http://www.mipsasoft.com/MS7/Hardware/FLASH/StrataFlash%20P30-T%20Data%20Sheet.pdf
Sub IntelFhash_Erase(baseAdr As DWord)
N64PowerON()
IntelFhash_Reset(baseAdr)
FTDI_SendCommand()
IntelFlash_SetReadConfigRegister(baseAdr)
flash_printID(baseAdr)
Dim status As DWord, inSize As DWord, retry As DWord
Dim currBlock As DWord, lastBlock As DWord
lastBlock = &H1FFFFFF
' Erase 4 blocks with 16kwords each
For currBlock = 0 To &H20000 Step &H8000
printf(ex"16kword block: %08Xh- \r", baseAdr + currBlock)
' Unlock block command
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&H60)
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&HD0)
FTDI_SendCommand()
' Erase command
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&H20)
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&HD0)
FTDI_SendCommand()
IntelFlash_WaitWritable(baseAdr + currBlock)
printf(ex"\n")
Next currBlock
' Erase up to 255 blocks with 64kwords each
For currBlock = &H20000 To lastBlock Step &H20000
printf(ex"64kword block: %08Xh- %d%% \r", baseAdr + currBlock, (currBlock*100)/lastBlock)
' Unlock block command
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&H60)
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&HD0)
' Erase command
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&H20)
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&HD0)
FTDI_SendCommand()
IntelFlash_WaitWritable(baseAdr + currBlock)
printf(ex"\n")
Next currBlock
IntelFhash_Reset(baseAdr)
IntelFlash_SetReadConfigRegister(baseAdr)
flash_printID(baseAdr)
N64PowerOFF()
End Sub
'http://www.mipsasoft.com/MS7/Hardware/FLASH/StrataFlash%20P30-T%20Data%20Sheet.pdf
Sub IntelFhash_Test(baseAdr As DWord)
N64PowerON()
IntelFhash_Reset(baseAdr)
FTDI_SendCommand()
Dim status As DWord, inSize As DWord, retry As DWord
Dim currBlock As DWord, lastBlock As DWord
lastBlock = &H1FFFFFF
' Erase 4 blocks with 16kwords each
For currBlock = 0 To &H20000 Step &H8000
printf(ex"16kword block: %08Xh- \r", baseAdr + currBlock)
' Unlock block command
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&H60)
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&HD0)
FTDI_SendCommand()
IntelFlash_WaitWritable(baseAdr + currBlock)
printf(ex"\n")
Next currBlock
' Erase up to 255 blocks with 64kwords each
For currBlock = &H20000 To lastBlock Step &H20000
printf(ex"64kword block: %08Xh- %d%% \r", baseAdr + currBlock, (currBlock*100)/lastBlock)
' Unlock block command
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&H60)
NUS_SetAddress(baseAdr + currBlock)
NUS_writeData(&HD0)
FTDI_SendCommand()
IntelFlash_WaitWritable(baseAdr + currBlock)
printf(ex"\n")
Next currBlock
IntelFhash_Reset(baseAdr)
N64PowerOFF()
End Sub
Sub ReadCFI()
'N64 flash cart test (ref:https://github.com/sanni/cartreader/blob/master/Cart_Reader/N64.ino)
Dim inbuf[NUS_DUMP_BUFFER_SIZE] As Byte, inSize As DWord
flash_printID(CART_BASEADR_ROM )
flash_printID(CART_BASEADR_ROM + &H2000000)
' flash_printID(CART_BASEADR_ROM + &H4000000)
' flash_printID(CART_BASEADR_ROM + &H6000000)
Print
Print "CFI INFO"
'CFI READ
'00000055 98
NUS_SetAddress(CART_BASEADR_ROM + (&H55 << 1))
NUS_writeData(&H98)
NUS_SetAddress(CART_BASEADR_ROM)
NUS_readRequest(NUS_DUMP_BUFFER_SIZE/2)
NUS_SetAddress(CART_BASEADR_ROM+NUS_DUMP_BUFFER_SIZE)
NUS_readRequest(NUS_DUMP_BUFFER_SIZE/2)
'RESET
NUS_SetAddress(CART_BASEADR_ROM )
NUS_writeData(&HFF)
FTDI_SendCommand()
FT_Read(hFT, inbuf, NUS_DUMP_BUFFER_SIZE, inSize)
Dump(inbuf, NUS_DUMP_BUFFER_SIZE)
FT_Read(hFT, inbuf, NUS_DUMP_BUFFER_SIZE, inSize)
Dump(inbuf, NUS_DUMP_BUFFER_SIZE, NUS_DUMP_BUFFER_SIZE)
Dim out As File
out.openFile("CFI.bin", GENERIC_WRITE)
out.write(inbuf, NUS_DUMP_BUFFER_SIZE)
out.close()
N64PowerOFF()
End Sub
Sub ROM_FlashCommand(base As DWord, adr AS DWord, data AS Word)
NUS_SetAddress(base + adr )
NUS_writeData(data)
'printf(ex"FLASH_CMD: %08x(%x) -> %04x\n", base+adr, adr>>1, data)
End Sub
function SPFlash_Status(address As DWord) AS Word
Dim inSize AS DWord
' NUS_SetAddress(CART_BASEADR_ROM + (&H555 << 1))
' NUS_writeData(&HAA)
/* ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &HAA)
ROM_FlashCommand(CART_BASEADR_ROM, &H2AA<< 1, &H55)
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &H90)
'' Read manufacturer ID
NUS_SetAddress(CART_BASEADR_ROM)
NUS_readRequest(&H1)
NUS_writeData(&HF0)
FTDI_SendCommand()
FT_Read(hFT, VarPtr(SPFlash_Status), 2, inSize)
printf(ex"tmp=%x\n", SPFlash_Status)
NUS_writeData(&HF0)
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &HAA)
ROM_FlashCommand(CART_BASEADR_ROM, &H2AA<< 1, &H55)*/
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &HF0)
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &H71)
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &H70)
' NUS_SetAddress(CART_BASEADR_ROM)
' NUS_SetAddress(CART_BASEADR_ROM + address)
' FTDI_SendCommand()
/* FT_WAIT(10)
NUS_readRequest(8)
FTDI_SendCommand()
Dim buffer[16] As Byte
FT_Read(hFT, buffer, 16, inSize)
' Dump(buffer,16,address)
*/
' FT_Read(hFT, VarPtr(SPFlash_Status), 2, inSize)
End function
Function ReadImmediate(adr AS DWord) As Word
NUS_SetAddress(adr )
FTDI_SendCommand()
NUS_readRequest(1)
FTDI_SendCommand()
Dim inSize As DWord
FT_Read(hFT, VarPtr(ReadImmediate), 2, inSize)
End Function
Sub SPFlash_WaitDone(address As DWord, expected_word As Word)
Dim poll As Word
expected_word = ChangeEndianWord(expected_word)
Do
poll = ReadImmediate(address)
printf(ex"Flash_wait : %x -> %4x expect=%4x\r", address,poll, expected_word)
if (poll OR &H7FFF) = (expected_word OR &H7FFF) then
'printf(ex"よいのでは\n")
exitdo
Else
if (poll And &H2000) OR (poll And &H0200) Then
poll = ReadImmediate(address)
printf(ex"Flash_wait2: %x -> %4x expect=%4x\r", address,poll, expected_word)
if (poll OR &H7FFF) = (expected_word OR &H7FFF) then
exitdo
Else
printf(ex"FLASH OPERATION FAIL: %X\n", address)
Sleep(-1)
End If
Else
'DO NOTHING
End If
endif
'status = SPFlash_Status(address)
'printf(ex"Flash_wait: %X -> %X expect=%X\n", address,status, expected_word)
Sleep(16)
Loop
' NUS_SetAddress(CART_BASEADR_ROM)
' NUS_readRequest(64)
/* Dim buf[128] as byte
FTDI_SendCommand()
FT_Read(hFT, buf, 128, inSize)
Dump(buf,128)*/
End Sub
Sub SPFlash_WaitErase(address As DWord, expected_word As Word)
Dim poll As Word
expected_word = ChangeEndianWord(expected_word)
Do
poll = ReadImmediate(address)
printf(ex"erase_wait : %x -> %4x expect=%4x\n", address,poll, expected_word)
if (poll OR &H7FFF) = (expected_word OR &H7FFF) then
'printf(ex"よいのでは\n")
exitdo
endif
'status = SPFlash_Status(address)
'printf(ex"Flash_wait: %X -> %X expect=%X\n", address,status, expected_word)
Sleep(16)
Loop
End Sub
' https://www.infineon.com/dgdl/Infineon-S29GL01GS_S29GL512S_S29GL256S_S29GL128S_128_Mb_256_Mb_512_Mb_1_Gb_GL-S_MIRRORBIT_TM_Flash_Parallel_3-DataSheet-v20_00-EN.pdf?fileId=8ac78c8c7d0d8da4017d0ed07ac14bd5
' and cartreader
Sub SPFlash_SectorErase(sector_address AS DWord)
printf(ex"sector %x erase...", CART_BASEADR_ROM + sector_address)
' WRITE UNLOCK
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &HAA)
ROM_FlashCommand(CART_BASEADR_ROM, &H2AA<< 1, &H55)
' WRITE SECTOR ERACE CYCLES\
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &H80)
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &HAA)
ROM_FlashCommand(CART_BASEADR_ROM, &H2AA<< 1, &H55)
ROM_FlashCommand(CART_BASEADR_ROM, sector_address, &H30)
FTDI_SendCommand()
SPFlash_WaitErase(CART_BASEADR_ROM + sector_address, &HFFFF)
printf(ex"done\n")
End Sub
Sub SPFlash_ChipErase()
' WRITE UNLOCK
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &HAA)
ROM_FlashCommand(CART_BASEADR_ROM, &H2AA<< 1, &H55)
' WRITE SECTOR ERACE CYCLES\
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &H80)
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &HAA)
ROM_FlashCommand(CART_BASEADR_ROM, &H2AA<< 1, &H55)
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &H10)
FTDI_SendCommand()
SPFlash_WaitErase(CART_BASEADR_ROM, &HFFFF)
End Sub
Sub SPFlash_WriteSector(address As DWord, data AS BytePtr, size As DWord)
Dim offset = 0 As DWord, bi AS DWord, word As Word
' offset=size-32
Do
' WRITE UNLOCK
ROM_FlashCommand(CART_BASEADR_ROM, &H555<< 1, &HAA)
ROM_FlashCommand(CART_BASEADR_ROM, &H2AA<< 1, &H55)
Const flash_buf_size = 32
ROM_FlashCommand(CART_BASEADR_ROM, address+offset, &H25)
ROM_FlashCommand(CART_BASEADR_ROM, address+offset, (flash_buf_size/2)-1)
for bi=0 to flash_buf_size-2 step 2
word = ((data[offset] AND &HFF)<<8) OR (data[offset+1] AND &HFF)
ROM_FlashCommand(CART_BASEADR_ROM, address + offset, word)
offset+=2
'NUS_SetAddress( CART_BASEADR_ROM + address + offset)
'NUS_writeData(data[offset]<<8 OR (data[offset+1] AND &HFF))
Next bi
ROM_FlashCommand(CART_BASEADR_ROM, address+offset-2, &H29)
FTDI_SendCommand()
SPFlash_WaitDone(CART_BASEADR_ROM+address+offset-2,/*ChangeEndianWord*/(word))
Dim progress As DWord
progress = (offset As QWord *10000) / size
printf(ex" writing sector... %08X : %d.%03d%% \r",
CART_BASEADR_ROM+address, progress / 100, progress Mod 100)
if offset>=size then ExitDo
Loop
End Sub
Sub SPFlash_WriteFile(filename AS BytePtr)
EnterSlowMode()
N64PowerON()
FTDI_SendCommand()
dim infile As File
infile.openFile(filename, GENERIC_READ)
' infile.openFile("T:\edit.n64", GENERIC_READ)
' infile.openFile("T:\viewer.z64", GENERIC_READ)
Dim buf As BytePtr, bufSize AS DWord
bufSize=infile.length()
buf=malloc(bufSize)
infile.read(buf,bufSize)
Dim offset As DWord, sector_size As DWord, total_size As DWord
offset = 0'&H01fc0000
sector_size = &H20000
total_size = infile.length()
Dim start As DWord
start = GetTickCount()
Do
Dim progress AS DWord
progress = ((offset As QWord * 10000) / total_size) As DWord
printf(ex"[WRITE] %08X/%08X ... %d.%03d%% ... %dKB/s\n",
offset, total_size, progress/100, progress Mod 100,
sector_size / ((GetTickCount()-start)/1000))
SPFlash_SectorErase(offset)
SPFlash_WriteSector(offset, buf+offset, sector_size)
offset += sector_size
if offset>=total_size then exitdo
Dim tmp AS DWord
' printf(ex"next sector: %X\n", offset)
' Input "[PRESS ENTER TO CONTINUE]", tmp
Loop
infile.close()
N64PowerOFF()
printf(ex"done\n")
End Sub