-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWnd.sbp
1835 lines (1463 loc) · 49.9 KB
/
MainWnd.sbp
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'-----------------------------------------------------------------------------
' イベント プロシージャ
'-----------------------------------------------------------------------------
' このファイルには、ウィンドウ [MainWnd] に関するイベントをコーディングします。
' ウィンドウ ハンドル: hMainWnd
' TODO: この位置にグローバルな変数、構造体、定数、関数を定義します。
Dim hCOM As HANDLE
Dim DlgStatus As DLG_STATUS
Dim fWindowActive AS BOOL
Dim hMinimizeRomTool As HANDLE
Dim MinimizeRomTool As TOOLINFO
Dim isFirstBaudrateError=FALSE As BOOL
'ダイアログの状態
Enum DLG_STATUS
STATUS_CONNECTED
STATUS_DUMPING
STATUS_CLOSED
End Enum
'-----------------------------------------------------------------------------
' ウィンドウメッセージを処理するためのコールバック関数
Function MainWndProc(hWnd As HWND, dwMsg As DWord, wParam As WPARAM, lParam As LPARAM) As DWord
' TODO: この位置にウィンドウメッセージを処理するためのコードを記述します。
'デバイス切断接続メッセージ
If dwMsg=WM_DEVICECHANGE Then
If DlgStatus=STATUS_CLOSED Then
'接続されていなかったら
ListUpComPort(GetDlgItem(hMainWnd,ComList))
Else if DlgStatus=STATUS_CONNECTED Then
'Arduinoが抜けてないかチェック
If CheckFirmware(hCOM, FALSE, hMainWnd)=FALSE Then
DisconnectDevice()
DBM("Device disconnected.")
End If
End If
Else if dwMsg=WM_COMMAND then
'アクセラレータキー経由だと、ActiveBasicのEventCallに引っかからないので、アクセラレータフラグを消す
'printf(ex"WM_COMMAND nofty=%X %X\n",wParam>>16,lParam)
If lParam=NULL Then wParam = wParam And &HFFFeFFFF
'タブストップの処理(手動)
If wParam=ID_DLG_TAB Then
SetFocus( GetNextDlgTabItem(hMainWnd, GetFocus(), FALSE))
ExitFunction
End If
End If
' イベントプロシージャの呼び出しを行います。
MainWndProc=EventCall_MainWnd(hWnd,dwMsg,wParam,lParam)
End Function
'-----------------------------------------------------------------------------
' ここから下は、イベントプロシージャを記述するための領域になります。
Function MWnd(DlgItem As Long) As HWND
MWnd=GetDlgItem(hMainWnd,DlgItem)
End Function
Sub MainWnd_Destroy()
If hCOM<>0 Then CloseHandle(hCOM)
HongKongArduinoClone_DestroyObjects()
PostQuitMessage(0)
End Sub
Sub ResetDBM()
SetWindowText(MWnd(EditBox1),"")
DBM(ProgramName+" "+VerStr+ex"\r\nProgrammed by RGBA_CRT 2016-2021.\r\nThis program is third-party impremention of HongKongArduino.\r\nOrg Copyright (C) 2014 たにやま\r\n")
End Sub
Dim hFont_LogBox As HFONT
Sub MainWnd_Create(ByRef CreateStruct As CREATESTRUCT)
ResetDBM()
SetIniPath()
ListUpComPort(GetDlgItem(hMainWnd, ComList))
' setup mapper type list box
Dim i as Long
for i = 0 To SFC_MAP_TYPE__NUM-1
SendMessage(MWnd(MAPPING), CB_ADDSTRING, 0, GetMappingString(i) As Long)
Next i
'SendMessage(MWnd(EditBox1), WM_SETFONT, CreateFont( -12, 0, 0, 0, 400, 0, 0, 0, SHIFTJIS_CHARSET, OUT_RASTER_PRECIS, 0, DEFAULT_QUALITY, FIXED_PITCH, "MS ゴシック") As DWord , TRUE)
SendMessage(hMainWnd, WM_SETICON, ICON_SMALL, LoadImage(GetModuleHandle(0), IDI_ICON1 As BytePtr, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR) As Long)
SetCheckBox(MWnd(CheckBox6), TRUE)
SetDlgEnable(STATUS_CLOSED)
Dim buf[100] AS Byte
Dim tmp AS DWord
For i = 0 To 7
tmp = &H80 << i
SendMessage(MWnd(ROM_SIZE), CB_ADDSTRING, 0, sprintfStr("%dKB [%dMbit]", (tmp) As DWord, (tmp * 8 / 1024) As DWord) )
tmp = &H02 << i
SendMessage(MWnd(SRAM_SIZE), CB_ADDSTRING, 0, sprintfStr("%dKB [%dKbit]", (tmp) As DWord, (tmp * 8) As DWord) )
Next i
Dim baudrate As DWord
baudrate = GetPrivateProfileInt(ProgramName, "BaudRate", DEFAULT_BAUDRATE, INIFILE_PATH)
If baudrate = 0 Then baudrate = DEFAULT_BAUDRATE
#IFDEF _DEBUG
MoveWindow(hMainWnd, 0, 0, 505, 330, TRUE)
#endif
' setup baudrate combobox
Dim default_baudrates[15] = [115200, 230400, 460800, 500000, 921600, 1000000, 1500000, 2000000, 0] As Long
i=0
' SetWindowText(MWnd(EditBox6), Str$(baudrate))
Do
if default_baudrates[i] <> 0 Then
wsprintf(buf, "%d", default_baudrates[i])
SendMessage(MWnd(EditBox6), CB_ADDSTRING, 0, buf As Long)
Else
wsprintf(buf, "%d", baudrate)
if SendMessage(MWnd(EditBox6), CB_FINDSTRING, 0, buf As Long) = CB_ERR Then
SendMessage(MWnd(EditBox6), CB_ADDSTRING, 0, buf As Long)
endif
SendMessage(MWnd(EditBox6), CB_SELECTSTRING, 0, buf As Long)
ExitDo
endif
i++
Loop
wsprintf(buf, "%s %s", ProgramName, VerStr)
SetWindowText(hMainWnd, buf)
' set monoscape font
hFont_LogBox=CreateFont(-12,0,0,0,400,0,0,0,128,3,2,1,50,"MS Gothic")
SendMessage(GetDlgItem(hMainWnd,EditBox1),WM_SETFONT,hFont_LogBox As WPARAM,0)
InitCommonControls()
CreateTooltip(MWnd(CB_MINIMIZE_ROM), VarPtr(hMinimizeRomTool), VarPtr(MinimizeRomTool), "Detect an overdump and trim ROM automatically.")
' trim default
SetCheckBox(MWnd(CB_MINIMIZE_ROM), GetPrivateProfileInt(ProgramName, "TrimRomEnable", DEFAULT_TRIM_ENABLE, INIFILE_PATH))
End Sub
Sub SetIniPath()
'INIファイルへのパスを記録
Dim Path[MAX_PATH + 20] As Byte, i As Long
'自己のファイルパスを取得
GetModuleFileName(NULL, Path, MAX_PATH)
For i = lstrlen(Path) To 0 Step -1
If Path[i] = &H5C then ExitFor' \ だったら
Next i
Path[i + 1] = 0'¥以下を潰す
INIFILE_PATH = calloc(lstrlen(Path) + Len(INIFILE_NAME) + 10)
lstrcpy(INIFILE_PATH, Path)
lstrcat(INIFILE_PATH, INIFILE_NAME)
End Sub
Sub ListUpComPort_Callback(userdata AS VoidPtr, PortName As BytePtr, FriendlyName AS BytePtr)
Dim hCombobox As HWND
hCombobox = userdata
Dim textbuf[64] As Byte
wsprintf(textbuf, ex"%s - %s",PortName,FriendlyName)
PumpMessage()
' Get Text Width
Dim size As SIZE, hdc As HDC, hOldFont As HFONT, hNowFont As HFONT
hdc = GetDC(hCombobox)
hOldFont = SelectObject(hdc, hFont_MainWnd)
GetTextExtentPoint32(hdc, textbuf, lstrlen(textbuf), size)
ReleaseDC(hCombobox,hdc)
' Adjust Dropdown list width
Dim currentWidth AS Long
currentWidth = SendMessage(hCombobox, CB_GETDROPPEDWIDTH, 0, 0)
if currentWidth < size.cx Then SendMessage(hCombobox, CB_SETDROPPEDWIDTH, size.cx+10, 0)
' add string
SendMessage(hCombobox,CB_ADDSTRING,0,textbuf As Long)
Endsub
Sub ListUpComPort(Wnd As HWND)
SendMessage(Wnd,CB_RESETCONTENT,0,0)
' 最後に接続したデバイスを取得
Dim lastDevice aS BytePtr, lastDeviceIdx AS Long
lastDevice=calloc(128)
GetPrivateProfileString(ProgramName,"LastDevice","None",lastDevice,128,INIFILE_PATH)
' デバイス名付きリストアップを試す(Win2K以降)
Dim sa AS SetupApiWrapper, failed AS BOOL
if sa.Load() Then
if EnumSerialPorts(VarPtr(sa), AddressOf(ListUpComPort_Callback), Wnd) = FALSE Then failed=TRUE
Else
failed=TRUE
Endif
if failed Then
DBM("Listup serial port failed.")
SendMessage(Wnd,CB_ADDSTRING,0,lastDevice As Long)
SendMessage(Wnd,CB_ADDSTRING,0,"COM1" As Long)
SendMessage(Wnd,CB_ADDSTRING,0,"COM2" As Long)
SendMessage(Wnd,CB_ADDSTRING,0,"COM3" As Long)
SendMessage(Wnd,CB_ADDSTRING,0,"<Error>" As Long)
End If
'最後に選択されたポートを選ぶ
lastDeviceIdx=SendMessage(Wnd,CB_FINDSTRINGEXACT,-1,lastDevice As DWord)
If lastDeviceIdx=CB_ERR Then lastDeviceIdx=0
'セットする
SendMessage(Wnd,CB_SETCURSEL,lastDeviceIdx,0)
free(lastDevice)
End Sub
Sub DBM(Text As BytePtr)
Dim LastPos As DWord, hEB As HANDLE
hEB = MWnd(EditBox1)
LastPos = GetWindowTextLength(hEB)
'10KBを超えてたらログをクリア
If LastPos>1024 * 10 Then
'1KB残す
Dim txt AS BytePtr
txt = calloc(LastPos + 1)
GetWindowText(hEB, txt, LastPos)
SetWindowText(hEB, txt + LastPos -1024)
free(txt)
LastPos = 1025
End If
SendMessage(hEB, EM_SETSEL, LastPos, LastPos)
SendMessage(hEB, EM_REPLACESEL, 0, Text As Long)
SendMessage(hEB, EM_REPLACESEL, 0, ex"\r\n" As Long)
OutputDebugString(Text)
OutputDebugString(ex"\r\n")
End Sub
Sub DBMN(Text As BytePtr)
Dim LastPos As DWord, hEB As HANDLE
hEB = MWnd(EditBox1)
LastPos = GetWindowTextLength(hEB)
SendMessage(hEB, EM_SETSEL, LastPos, LastPos)
SendMessage(hEB, EM_REPLACESEL, 0, Text As Long)
End Sub
'printfのDBM版
Sub DBMf(lpFormat As BytePtr)(a As DWord ,b As DWord ,c As DWord ,d As DWord ,e As DWord ,f As DWord ,g As DWord ,h As DWord ,i As DWord ,j As DWord ,k As DWord ,l As DWord ,m As DWord ,n As DWord )
Dim dbuf[1024] As Byte
Dim p AS DWord
p=wsprintf(dbuf,lpFormat,a,b,c,d,e,f,g,h,i,j,k,l,m,n)
DBM(dbuf)
EndSub
Sub BaudrateErrorMessage()
' 初回
if isFirstBaudrateError = FALSE Then
Dim text_jpn=ex"通信に失敗しました。\nリトライしても治らない場合はボーレートを調整してください。(詳細はReadme参照)\n\n推奨ボーレートは使用しているArduino(シリアルコンバータ)に依存します。\n例:\n [公式Arduino UNO] ATmega8U2: 500,000 bps\n [Arduino互換機] CH340: 1,000,000 bps" As BytePtr
Dim text_eng=ex"Communication failed. \r\nPlease adjust baudrate for your Arduino (see README.md)" As BytePtr
DBM(text_eng)
DBM(ex"Recommended baudrate:\r\n- Offical Arduino: 500000bps\r\n- Compatible(CH340): 1000000bps")
MessageBox(hMainWnd, text_jpn, text_eng, MB_OK OR MB_ICONERROR)
isFirstBaudrateError=TRUE
End If
End Sub
Sub MainWnd_CommandButton5_Click()
'設定保存
SaveIniFile()
If hCOM = 0 then
ConnectDevice()
Else
'切断
DisconnectDevice()
End If
End Sub
Sub ConnectDevice()
'接続
Dim txCom As BytePtr, rate As DWord, i As Long
SetWindowText(MWnd(CommandButton5), "&Close")
SetDlgEnable(STATUS_CONNECTED)
Dim buf[1024] As Byte
GetDlgItemText(hMainWnd, EditBox6, buf, 1024)
rcAsciiToInt(buf, VarPtr(rate))
If rate = 0 Then
ErrMes(hMainWnd, "ボーレートが不正です。", "Invalid baud rate", 0)
ExitSub
End If
GetDlgItemText(hMainWnd, ComList, buf, 1024)
txCom = buf
DBMf("Connecting to %s...",txCom As DWord)
for i=0 To 10
if txCom[i] = 0 Then ExitFor
if txCom[i] = &H20 Then
txCom[i] = 0
ExitFor
End If
Next i
'bufferlen
Dim bufferSize AS DWord
bufferSize = GetPrivateProfileInt(ProgramName, "DumpBufferSize", DEFAULT_DUMP_SIZE, INIFILE_PATH)
If bufferSize <= 0 Then bufferSize = DEFAULT_DUMP_SIZE
Dim retry=0 As Long
*RECONNECT
'connect and reset ボーレート
hCOM = OpenComPort(txCom, INITIAL_BAUDRATE, bufferSize, 1024 * 10)
if hCOM=NULL Then
ErrMes(hMainWnd, "シリアルポートを開けませんでした。","Failed to open port", MB_OK,GetLastError())
goto * HKAF_CON_ERR
End If
*RESET_ARDUINO
FlushCom(hCOM)
ResetArduino(hCOM)
'正常なデータ通信を確立(できるようになるまでリトライ)
If CheckFirmware(hCOM, TRUE, hMainWnd) = FALSE Then
goto * HKAF_CON_ERR
End If
DBM(ex"Connection Stage2 OK.\r\n")
'ボーレート変更コマンド送信
if rate <> INITIAL_BAUDRATE Then
ChangeBaudrate(hCOM, rate)
'変更後の正常なデータ通信を確立
DBMf("Switch baudrate to %dbps...", rate)
If CheckFirmware(hCOM, FALSE, hMainWnd) = FALSE Then
'ちゃんとリトライカウントつけること
FlushCom(hCOM)
CloseHandle(hCOM): hCOM=NULL
Sleep(500)
retry++
if retry<3 Then
DBM(ex"failed. retry.\r\n")
Goto *RECONNECT
endif
BaudrateErrorMessage()
goto * HKAF_CON_ERR
End If
End If
DBM(ex"Connection Successful!!!\r\n")
'GetCartInfo
MainWnd_CommandButton1_Click()
ExitSub
*HKAF_CON_ERR
DisconnectDevice()
SetWindowText(MWnd(SRAM_SIZE), "")
SetWindowText(MWnd(ROM_SIZE), "")
SetWindowText(MWnd(ROMTITLE), "")
SetWindowText(MWnd(CART_CIPS), "")
SetWindowText(MWnd(MAPPING), "")
End Sub
Sub DisconnectDevice()
If hCOM <> 0 Then
CloseHandle(hCOM)
hCOM = 0
endif
SetWindowText(MWnd(CommandButton5), "&Connect")
SetDlgEnable(STATUS_CLOSED)
DBM(ex"Connection is closed.\r\n")
End Sub
Sub SetDlgEnable(Mode As DLG_STATUS)
'ボタンの有効/無効セット
Dim isEnable As Long
If Mode = STATUS_CONNECTED Or Mode = STATUS_DUMPING then
isEnable = FALSE
Else Mode = STATUS_CLOSED Then
isEnable = TRUE
End If
EnableWindow(MWnd(ComList), isEnable)
EnableWindow(MWnd(EditBox6), isEnable)
EnableWindow(MWnd(Static9), isEnable)
' EnableWindow(MWnd(Static10), isEnable)
If Mode = STATUS_DUMPING Then isEnable = NOT(isEnable) And &H01
EnableWindow(MWnd(CommandButton1), NOT(isEnable) And &H01)
EnableWindow(MWnd(CommandButton2), NOT(isEnable) And &H01)
EnableWindow(MWnd(CommandButton3), NOT(isEnable) And &H01)
EnableWindow(MWnd(CommandButton4), NOT(isEnable) And &H01)
EnableWindow(MWnd(CommandButton17), NOT(isEnable) And &H01)
EnableWindow(MWnd(ROMTITLE), NOT(isEnable) And &H01)
EnableWindow(MWnd(CART_CIPS), NOT(isEnable) And &H01)
EnableWindow(MWnd(MAPPING), NOT(isEnable) And &H01)
EnableWindow(MWnd(ROM_SIZE), NOT(isEnable) And &H01)
EnableWindow(MWnd(SRAM_SIZE), NOT(isEnable) And &H01)
EnableWindow(MWnd(CB_MANUAL), NOT(isEnable) And &H01)
' EnableWindow(MWnd(CB_MINIMIZE_ROM), NOT(isEnable) And &H01)
If Mode = STATUS_DUMPING Then
EnableWindow(MWnd(CommandButton5), FALSE)
Else
EnableWindow(MWnd(CommandButton5), TRUE)
End If
DlgStatus = Mode
End Sub
Function GetWndTextMalloc(hWnd As HWND) As BytePtr
'コントロールのテキスト取得
Dim buf As BytePtr
buf = calloc(GetWindowTextLength(hWnd) + 20)
GetWindowText(hWnd, buf, GetWindowTextLength(hWnd) + 1)
GetWndTextMalloc = buf
End Function
Sub MainWnd_KeyUp(KeyCode As Long, flags As Long)
' DBM("PressKey:"+Hex$(KeyCode))
Select Case KeyCode
' Case &H70 : SpeedTest() 'F1
' Case &H71 : CheckFirmware(hCOM,FALSE,hMainWnd) 'F2
Case &H74 : ListUpComPort(GetDlgItem(hMainWnd,ComList))
' Case &H78 : MainWnd_CommandButton12_Click()
' Case &H7A : MainWnd_CommandButton15_Click()
' Case &H7B : MainWnd_CommandButton13_Click()
'(このメッセージはフォーカスが無い状態なので)TABキーが押されたら適当なボタンにフォーカスを当てる
'Case VK_TAB : SetFocus( GetNextDlgTabItem(hMainWnd, 0, TRUE))
End Select
End Sub
Sub SpeedTest()
'Arduinoとの接続速度テスト
Dim rd As BytePtr, mes[256] As Byte, total As DWord, lt As DWord, i As Long
Const TEST_READ_SIZE = 1024 * 10
DBM("Speedtest : Read " + Str$(TEST_READ_SIZE / 1024) + "KB*10")
rd = calloc(TEST_READ_SIZE)
total = 0
lt = 0
For i = 0 To 9
lt = GetTickCount()
ReadROM(hCOM, rd, 0, i * TEST_READ_SIZE, TEST_READ_SIZE, FALSE)
total + = GetTickCount() - lt
wsprintf(mes, ex"\t[%d]Time : %dms", i, GetTickCount() - lt)
DBM(mes)
Next i
wsprintf(mes, ex"Total:%dms\r\nAverage:%dms\r\nSpeed : %dbyte/sec (%dKB/s)\r\n", total, total / 9, (TEST_READ_SIZE * 10) / (total / 1000), (TEST_READ_SIZE * 10) / (total / 1000) / 1000)
DBM(mes)
free(rd)
End Sub
Sub MainWnd_CommandButton2_Click()
'ROM吸出しボタン
Dim info As SFC_CART_INFO
Dim path As BytePtr
If GetDumpConfig(VarPtr(info)) <> TRUE Then
DBM("GetDumpConfig() Error")
ExitSub
End If
if info.RomSize=0 Then
DBM("ROM size is zero.")
ExitSub
End If
Dim rom_title[MAX_PATH] As Byte
MakeRomFileName(rom_title, MAX_PATH, VarPtr(info),0)
#ifndef _DEBUG
path=SaveDialogCalloc(ex"SFC ROMイメージファイル(*.sfc)\0*.sfc\0すべてのファイル(*.*)\0*.*\0\0", "sfc", rom_title)
If path=0 Then ExitSub
#else
path="dump.sfc"
#endif
Dim ret AS Long
ret = MainWnd_DumpRomToFile(VarPtr(info), NULL, path)
if ret = HKAC_DUMPROM_OK Then
SE_OK()
Else
if ret = HKAC_DUMPROM_RETRY_OVER Then
BaudrateErrorMessage()
End If
End If
#ifndef _DEBUG
free(path)
#endif
End Sub
Sub MainWnd_QueryClose(ByRef cancel As Integer)
SaveIniFile()
End Sub
Sub SaveIniFile()
Dim Tmp As DWord
Const TEXTBUFSIZE = 255
Dim textbuf[TEXTBUFSIZE+1] As Byte
GetWindowText(MWnd(EditBox6), textbuf, TEXTBUFSIZE)
WritePrivateProfileString(ProgramName, "BaudRate", textbuf, INIFILE_PATH)
GetWindowText(MWnd(ComList), textbuf, TEXTBUFSIZE)
WritePrivateProfileString(ProgramName, "LastDevice", textbuf, INIFILE_PATH)
Tmp = GetPrivateProfileInt(ProgramName, "DumpErrorRetryLimit", DEFAULT_CONTINUE, INIFILE_PATH)
wsprintf(textbuf, "%d", Tmp)
WritePrivateProfileString(ProgramName, "DumpErrorRetryLimit", textbuf, INIFILE_PATH)
Tmp = GetPrivateProfileInt(ProgramName, "QDDSize", DEFAULT_QDD_BUFSIZE, INIFILE_PATH)
If Tmp = 0 Then Tmp = DEFAULT_QDD_BUFSIZE
wsprintf(textbuf, "%d", Tmp)
WritePrivateProfileString(ProgramName, "QDDSize", textbuf, INIFILE_PATH)
Tmp = GetPrivateProfileInt(ProgramName, "DumpBufferSize", DEFAULT_DUMP_SIZE, INIFILE_PATH)
If Tmp <= 0 Then Tmp = DEFAULT_DUMP_SIZE
wsprintf(textbuf, "%d", Tmp)
WritePrivateProfileString(ProgramName, "DumpBufferSize", textbuf, INIFILE_PATH)
Tmp = GetPrivateProfileInt(ProgramName, "SoundEnable", DEFAULT_SOUND_ENABLE, INIFILE_PATH)
wsprintf(textbuf, "%d", Tmp)
WritePrivateProfileString(ProgramName, "SoundEnable", textbuf, INIFILE_PATH)
Tmp = GetCheckBox(MWnd(CB_MINIMIZE_ROM))
wsprintf(textbuf, "%d", Tmp)
WritePrivateProfileString(ProgramName, "TrimRomEnable", textbuf, INIFILE_PATH)
End Sub
Sub MainWnd_CommandButton3_Click()
Dim fout As File
Dim info As SFC_CART_INFO
Dim path As BytePtr
If GetDumpConfig(VarPtr(info)) <> TRUE Then
DBM("GetDumpConfig() Error")
ExitSub
End If
if info.SramSize=0 Then
DBM("SRAM size is zero.")
ExitSub
End If
Dim rom_title[MAX_PATH] As Byte
MakeRomFileName(rom_title, MAX_PATH, VarPtr(info),0)
#ifndef _DEBUG
path=SaveDialogCalloc(ex"SRAMイメージファイル(*.srm)\0*.srm\0すべてのファイル(*.*)\0*.*\0\0","srm",rom_title)
If path=0 Then ExitSub
#else
path="dump.srm"
#endif
If fout.openFile(path, GENERIC_WRITE) = FALSE Then
DBMf("Cannot open file: %s",path As DWord)
Goto *D_EXIT
ExitSub
End If
Dim sram As BytePtr
sram = calloc(info.SramSize)
Dim mmap As SFC_MMAP_MODEL
GetMmapModel(VarPtr(info), FALSE, VarPtr(mmap))
DBM("Dumping SRAM...")
DBMf("SramSize = %d KB", info.SramSize>>10)
DBMf("bankSize = %d KB", mmap.sram.bankSize>>10)
DBMf("bankCount= %d", Int(info.SramSize / mmap.sram.bankSize))
Dim pc AS *SramProgressCallBack
pc = new_SramProgressCallBack(VarPtr(mmap), FALSE)
DumpSRAM(hCOM, sram, info.SramSize, VarPtr(mmap), pc As *ProgressCallBack)
delete_SramProgressCallBack(pc)
DBM(ex"SRAM dumping successful!!!\r\n")
fout.write(sram, info.SramSize)
*D_EXIT
fout.close()
if sram Then free(sram)
#ifndef _DEBUG
free(path)
#endif
' MessageBeep(MB_ICONASTERISK)
SE_OK()
End Sub
Sub MainWnd_CommandButton4_Click()
Dim fin As File
Dim path As BytePtr
Dim info As SFC_CART_INFO
If GetDumpConfig(VarPtr(info)) <> TRUE Then
DBM("GetDumpConfig() Error")
ExitSub
End If
if info.SramSize=0 Then
DBM("SRAM size is zero.")
ExitSub
End If
If info.isFlashSave Then
ErrMes(hMainWnd, "フラッシュメモリ方式のセーブデータ書き込みは現在未実装です。", "未実装", 0)
If (GetKeyState(VK_ESCAPE) And &H80) = 0 Then exitsub
DBMf("key%x", GetKeyState(VK_ESCAPE))
End If
#ifndef _DEBUG
path = LoadDialogCalloc(ex"SRAMイメージファイル(*.srm)\0*.srm\0すべてのファイル(*.*)\0*.*\0\0", "srm")
If path = 0 Then ExitSub
#else
If GetKeyState(VK_SHIFT) And &H80 Then
path = LoadDialogCalloc(ex"SRAMイメージファイル(*.srm)\0*.srm\0すべてのファイル(*.*)\0*.*\0\0", "srm")
If path = 0 Then ExitSub
Else
path = "write.srm"
End If
#endif
If MessageBox(hMainWnd, ex"セーブデータが上書きされます。\nデータは復元できません。\n続行しますか?", ProgramName+" - SRAM write", MB_YESNO or MB_ICONWARNING) <> IDYES Then ExitSub
'READ FILE
Dim writeBuffer As BytePtr
If fin.openFile(path, GENERIC_READ) = FALSE Then
DBMf("Cannot open file: %s", path As DWord)
ErrMes(hMainWnd, "SRAMファイルが開けませんでした。", "File Open Error", 0, GetLastError())
Goto *WS_EXIT
End If
writeBuffer = calloc(info.SramSize)
fin.read(writeBuffer, info.SramSize)
fin.close()
Dim retry As Long
*MWWS_RETRY
Dim mmap As SFC_MMAP_MODEL
GetMmapModel(VarPtr(info), TRUE, VarPtr(mmap))
DBMf("SramSize = %d KB", info.SramSize>>10)
DBMf("bankSize = %d KB", mmap.sram.bankSize>>10)
DBMf("bankCount= %d", Int(info.SramSize / mmap.sram.bankSize))
Dim pc AS *SramProgressCallBack
pc = new_SramProgressCallBack(VarPtr(mmap), TRUE)
DBM("Writing SRAM...")
WriteSRAM(hCOM, writeBuffer, info.SramSize, VarPtr(mmap), pc)
delete_SramProgressCallBack(pc)
'verify
DBMN("Verifying...")
Dim verifyBuffer As BytePtr, diffPos AS DWord
verifyBuffer = calloc(info.SramSize)
GetMmapModel(VarPtr(info), FALSE, VarPtr(mmap))
DumpSRAM(hCOM, verifyBuffer, info.SramSize, VarPtr(mmap), NULL)
if compareBinaly(verifyBuffer, writeBuffer, info.SramSize, VarPtr(diffPos)) Then
DBM("OK")
DBM(ex"SRAM writing successful!!!\r\n")
SE_OK()
Else
DBMf(ex"NG at %08Xh", diffPos)
DBM(ex"Failed to write SRAM.\r\n")
printf(ex"=== write ===\n")
Dump(writeBuffer+(diffPos And &HFFFFFFF0), 32, (diffPos And &HFFFFFFF0))
printf(ex"=== actual ===\n")
Dump(verifyBuffer+(diffPos And &HFFFFFFF0), 32, (diffPos And &HFFFFFFF0))
if retry<HKAC_SRAM_WRITE_RETRY_LIMIT Then
retry++
DBM("retry...")
Goto *MWWS_RETRY
Endif
MessageBeep(MB_ICONHAND)
End If
*WS_EXIT
#ifndef _DEBUG
free(path)
#endif
if writeBuffer Then free(writeBuffer): writeBuffer=NULL
if verifyBuffer Then free(verifyBuffer): verifyBuffer=NULL
End Sub
Function compareBinaly(dat1 AS BytePtr, dat2 AS BytePtr,length aS DWord)( pos As *DWord) AS BOOL
Dim i AS DWord
For i = 0 To length-1
if dat1[i]<>dat2[i] Then
compareBinaly=FALSE
if pos Then SetDWord(pos, i)
ExitFunction
EndIf
Next i
compareBinaly=TRUE
End Function
Function SaveDialogCalloc(filter As BytePtr, ext As String)(defFileName As BytePtr,filterIndex As *DWord) As BytePtr
Dim tOfn As OPENFILENAME, i As DWord
'ファイルオープン構造体の初期化
tOfn.lStructSize = SizeOf(OPENFILENAME) - SizeOf(DWord) * 2 - SizeOf(VoidPtr)
tOfn.hwndOwner = hMainWnd
tOfn.lpstrFilter = filter'ex"wavファイル(*.wav)\0*.wav\0すべてのファイル(*.*)\0*.*\0\0"'StrPtr(sFilter)
tOfn.nFilterIndex = 1
tOfn.nMaxFile = MAX_PATH
tOfn.Flags = OFN_OVERWRITEPROMPT
tOfn.lpstrDefExt = StrPtr(ext)'"wav"
tOfn.lpstrFile = calloc(MAX_PATH + 10)
If defFileName Then lstrcpy(tOfn.lpstrFile, defFileName)
'ファイルオープンダイアログを表示する
If GetSaveFileName(tOfn) = 0 Then
ExitFunction
End If
SaveDialogCalloc = calloc(lstrlen(tOfn.lpstrFile) + 1)
lstrcpy(SaveDialogCalloc, tOfn.lpstrFile)
free(tOfn.lpstrFile)
if filterIndex Then SetDWord(filterIndex,tOfn.nFilterIndex)
End Function
Function LoadDialogCalloc(filter As BytePtr, ext As String) As BytePtr
Dim tOfn As OPENFILENAME, i As DWord
'ファイルオープン構造体の初期化
tOfn.lStructSize = SizeOf(OPENFILENAME) - SizeOf(DWord) * 2 - SizeOf(VoidPtr)
tOfn.hwndOwner = hMainWnd
tOfn.lpstrFilter = filter'ex"wavファイル(*.wav)\0*.wav\0すべてのファイル(*.*)\0*.*\0\0"'StrPtr(sFilter)
tOfn.nFilterIndex = 1
tOfn.nMaxFile = MAX_PATH
tOfn.Flags = OFN_OVERWRITEPROMPT
tOfn.lpstrDefExt = StrPtr(ext)'"wav"
tOfn.lpstrFile = calloc(MAX_PATH + 10)
'ファイルオープンダイアログを表示する
If GetOpenFileName(tOfn) = 0 Then
ExitFunction
End If
LoadDialogCalloc = calloc(lstrlen(tOfn.lpstrFile) + 1)
lstrcpy(LoadDialogCalloc, tOfn.lpstrFile)
free(tOfn.lpstrFile)
End Function
Sub MainWnd_RButtonDblClick(flags As Long, x As Integer, y As Integer)
ResetDBM()
DBM("Cleared Log.")
End Sub
Sub MainWnd_Static1_DblClick()
exec("http://susumutaniyama.github.io/HongKongArduino/")
End Sub
'リードテスト
Sub MainWnd_CommandButton6_Click()
Dim f As File
If hCOM = 0 Then ExitSub
Dim data As BytePtr, adr As DWord
data = calloc(&H10000)
adr = GetDlgItemHex(MWnd(EditBox2))
DBM("Dump 0x" + Hex$(adr))
ReadROM(hCOM, data, 0, adr, &h5000, NOT(GetCheckBox(MWnd(CheckBox6))) And &H01)
f.openFile("test.bin", GENERIC_WRITE)
f.write(data, &h5000)
f.close()
DBM("OK")
Dim p As BytePtr, i As Long
p = data + &H5000
For i = 0 To 7
p + = wsprintf(p, "%02X ", data[i])
Next i
p + = wsprintf(p, ex"\r\n", data[i])
For i = 0 To 7
p + = wsprintf(p, "[%c]", data[i])
Next i
DBM(data + &H5000)
Dump(data, 128)
End Sub
Sub MainWnd_MButtonDblClick(flags As Long, x As Integer, y As Integer)
'ESC+マウス中ボタンダブルクリックでテストモード
If GetCancelKey() Then
MoveWindow(hMainWnd,0,0,505,330,TRUE)
AllocConsole()
_System_hConsoleOut=GetStdHandle(STD_OUTPUT_HANDLE)
printf(ex"Welcome to debug console\n")
End If
End Sub
Sub MainWnd_CommandButton8_Click()
If hCOM=0 Then ExitSub
Dim ctrl As Byte
ctrl=(GetCheckBox(MWnd(CheckBox2))<<3 or GetCheckBox(MWnd(CheckBox3))<<2 or _
GetCheckBox(MWnd(CheckBox4))<<1 or GetCheckBox(MWnd(CheckBox5))) As Byte
DBM("SetCtrlBus : 0"+Hex$(ctrl))
SendControl(hCOM,ctrl)
End Sub
Sub MainWnd_CommandButton9_Click()
DBM("回路デバッグモード")
DBM("ESCキーで抜ける")
Do
MainWnd_CommandButton1_Click()
Sleep(300)
PumpMessage()
If GetKeyState(VK_ESCAPE) And &H80 Then ExitDo
Loop
End Sub
Sub MainWnd_CommandButton1_Click()
SetRomInfoToDlg_FromROM()
End Sub
Sub SetRomInfoToDlg_FromROM()
Dim buf[SFC_SPEC_HEADER_SIZE+2] As Byte,cartInfo As SFC_CART_INFO
'DBM( "Cartridge Infomation")
'ヘッダを読み出し
FlushCom(hCOM)
getCartInfo(VarPtr(cartInfo))
printf(ex"=== ROM HEADER ===\n")
Dump(VarPtr(cartInfo.rawHeader), sizeof(SFC_CART_HEADER))
'ダイアログに表示
SetCartInfo(VarPtr(cartInfo))
Dim info_text[1024] as Byte
GenerateHeaderInfoText(VarPtr(cartInfo), info_text, 1024)
DBMN(info_text)
'Manualのチェックを外す
SetCheckBox(MWnd(CB_MANUAL),FALSE)
'BSスロットがあればBSMWndを出す
If cartInfo.isBSX Then
BSM_open()
Dim idx AS Long
If cartInfo.Chipset=&HE5 Then
idx=BSM_SLOT_BIOS
Elseif cartInfo.MapType=SFC_MAP_LoROM or cartInfo.MapType=SFC_MAP_SpLoROM then
idx=BSM_SLOT_SpLoROM
Elseif cartInfo.MapType=SFC_MAP_HiROM then
idx=BSM_SLOT_HiROM
Elseif cartInfo.MapType=SFC_MAP_SA1 then
idx=BSM_SLOT_SA1
Else
DBM("[!!!]Warning: unknown BS-X slot")
End If
' debug
SendMessage(BSMWnd(BSM_CT),CB_SETCURSEL,idx,0)
PumpMessage()
Else
BSM_close()
End If
If cartInfo.MapType=SFC_MAP_SF_MENU Then
SFM_open()
SFTurbo_close()
Else If cartInfo.MapType=SFC_MAP_SFTURBO Then
SFTurbo_open()
SFM_close()
Else
SFM_close()
SFTurbo_close()
End If
SetCheckBox(MWnd(CheckBox7),hasClockModule(hCOM))
'アクセスランプをLow
SetAddress(hCOM,&H000000,FALSE)
DBM("")
End Sub
Sub MainWnd_CommandButton10_Click()
SPC7110_PrintBanks(hCOM)
SPC7110_SetBanks(hCOM,0,TRUE)
SPC7110_PrintBanks(hCOM)
SPC7110_SetBanks(hCOM,0,FALSE)
SPC7110_PrintBanks(hCOM)
End Sub
'---------------------------------------------------------------------------------------------------
'吸出し設定をROMとGUIから読み出し
Function GetDumpConfig(info As *SFC_CART_INFO) As BOOL
if getCartInfo(info) <> TRUE Then ExitFunction
GetDlgOption(info) 'infoにGUIの設定を反映
SetCartInfo(info)
GetDumpConfig=TRUE
End Function
Function getCartInfo(info As *SFC_CART_INFO) As BOOL
'カートリッジからヘッダをロード
Dim buf[SFC_SPEC_HEADER_SIZE+5] As Byte
/*ReadROM(hCOM,buf, 0, SFC_SPEC_HEADER_ADDR, SFC_SPEC_HEADER_SIZE,FALSE)
SloveCartInfo(buf As *SFC_CART_HEADER,VarPtr(info))*/
' SetCPU_Clock(hCOM,HKAC_CLOCK_NORMAL) 'clock
if SendControl(hCOM,CBUS_DEFAULT)=FALSE Then 'default
Goto *GCI_ERR
End If
If ReadROM(hCOM,buf, 0, SFC_SPEC_HEADER_ADDR, SFC_SPEC_HEADER_SIZE,FALSE)<SFC_SPEC_HEADER_SIZE Then
Goto *GCI_ERR
End If
SloveCartInfo(buf As *SFC_CART_HEADER,info)
getCartInfo = TRUE
ExitFunction
*GCI_ERR
lstrcpy(info->Title,"[ERR]通信エラー")
SetCartInfo(info)
ErrMes(hMainWnd,ex"通信エラーが発生しました。\nArduinoをリセットしてください。","Device was lost",0)
DisconnectDevice()
ExitSub
End Function
Function getSelectedMapMode() AS Long
Dim idx AS Long
idx=SendMessage(MWnd(MAPPING),CB_GETCURSEL,0,0) Mod SFC_MAP_TYPE__NUM
if idx<0 or idx>=SFC_MAP_TYPE__NUM Then
DBM("!!!!ASSERT, idx is over.")
idx=0
End If
getSelectedMapMode=idx
End Function
Sub GetDlgOption(info As *SFC_CART_INFO)
'LoROM/HiROMの選択とMAD1のチェック取得
If GetCheckBox(MWnd(CB_MANUAL)) Then
info->MapType = getSelectedMapMode()
' info->MapMode = (info->MapMode And &HF0) or info->MapType
info->RomSize = HKAC_GetWindowInt(MWnd(ROM_SIZE))
info->SramSize = HKAC_GetWindowInt(MWnd(SRAM_SIZE))
printf(ex"custom rom size = %d\n", (info->RomSize / 1024 / 1024) AS DWord)
End If
End Sub
'MapMode (20 SrowRom LoROMのやつ)のGUI用関数
Function MapModeToComboboxIndex(mapMode As SFC_MAP_TYPE) As Long
MapModeToComboboxIndex = mapMode As Long
End Function
'カートリッジ情報をGUIに反映