-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHardwareLevel.abp
418 lines (323 loc) · 11 KB
/
HardwareLevel.abp
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
'ファーム制御系
Const CBUS_OE = 1
Const CBUS_ROMSEL = 2
Const CBUS_WE = 4
Const CBUS_RST= 8
Const CBUS_DEFAULT = &H0C 'CBUS_RST CBUS_WE
Sub ChangeBaudrate(hSerial AS HANDLE, rate AS DWord)
'Arduinoレート変更コマンド送信
Dim cmd[7] AS Byte, dwAB AS DWord
cmd[0] = Asc("b")
memcpy(cmd + 1, VarPtr(rate), 4)
if WriteFile(hSerial, cmd, 5, VarPtr(dwAB), ByVal NULL) = FALSE or dwAB<>5 Then DBM("BC_ERR " + Str$(GetLastError()))
'待たないと送信途中でパケットが
Sleep(29) ' 1.44 byte/s * 5bytes * 4倍(安全)
FlushFileBuffers(hSerial)
FlushCom(hSerial)
'PC側レート変更
ChangeComBaudrate(hSerial, rate)
End Sub
Function OpenComPort(PortName AS BytePtr,rate AS DWord)(inBuffer As DWord,outBuffer AS DWord) AS HANDLE
Dim hSerial As HANDLE
Dim dcb As DCB_SETTING
if rate=0 Then rate=DEFAULT_BAUDRATE
Dim portPath As BytePtr
portPath=calloc(lstrlen(PortName)+6)
wsprintf(portPath,"\\.\%s",PortName)
hSerial=CreateFile(portPath,GENERIC_READ Or GENERIC_WRITE,NULL,ByVal NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)
free(portPath):portPath=NULL
if hSerial=INVALID_HANDLE_VALUE Then OpenComPort=NULL AS VoidPtr :ExitFunction
'バッファ設定
if inBuffer=0 Then inBuffer=1024*10
if outBuffer=0 Then outBuffer=1024*10
if SetupComm(hSerial,inBuffer,outBuffer)=0 Then
outBuffer=GetLastError()
DBM("Init com error")
debug
End If
FlushCom(hSerial)
dcb.BaudRate = rate '転送速度の指定
dcb.ByteSize = 8 'ビット長の指定
dcb.fBitFields = TRUE 'バイナリモードのフラグを有効にし
dcb.Parity = 0 'パリティの設定(パリティなし)
dcb.StopBits = 0 'ストップビット数を指定(1ビット)
SetCommState(hSerial,dcb)
'通信タイムアウトの設定
SetComTimeOut(hSerial,1000)
' DBM("baudrate="+Str$(rate)+"bps")
OpenComPort=hSerial
End Function
'RST OE WE CS などを制御
Function SendControl(hSerial AS HANDLE,b AS Byte) As BOOL
Dim cmd[1] AS Byte,dwAB AS DWord
cmd[0]=Asc("c")
cmd[1]=b
if WriteFile(hSerial,cmd,2,VarPtr(dwAB),ByVal NULL)=FALSE or dwAB<>2 Then
DBM("CTL_ERR "+Str$(GetLastError()))
SendControl=FALSE
Else
SendControl = TRUE
endif
End Function
'アドレスセット
Function SetAddress(hSerial AS HANDLE,address AS DWord, isLoROM AS Byte) As BOOL
Dim cmd[7] AS Byte
Dim ReadByte As DWord
Dim dwReadByte As DWord
'[0]コマンド
if isLoROM Then
cmd[0]=Asc("a")
Else
cmd[0]=Asc("A")
End if
'[1-3] アドレス(ビッグエンディアン)
cmd[1] = (address >> 0) AS Byte
cmd[2] = (address >> 8) AS Byte
cmd[3] = (address >> 16) AS Byte
if WriteFile(hSerial,cmd,4,VarPtr(dwReadByte),ByVal NULL)=FALSE or dwReadByte<>4 Then
DBM("CTL_ERR "+Str$(GetLastError()))
SetAddress=FALSE
Else
SetAddress = TRUE
endif
End Function
'カートリッジからのデータ受信(ROMだけでなくカートリッジ上のメモリ空間も対象)
'コントロールバスはいじらないので手動でやる必要あり
Function ReadROM(hSerial AS HANDLE,data AS BytePtr, offset AS Long,address AS DWord, size AS Long, isLoROM AS BOOL) As Long
Dim cmd[7] AS Byte
Dim dwReadByte As DWord
if isLoROM Then
cmd[0]=Asc("r") 'LoROM
Else
cmd[0]=Asc("R") 'HiROM
End if
cmd[1] = (address >> 0) AS Byte
cmd[2] = (address >> 8) AS Byte
cmd[3] = (address >> 16) AS Byte
if size>&H10000 Then
DBM("ReadROM()で不正なサイズが指定されました")
debug
'size=&HFFFF
End if
cmd[4] = (size >> 0) AS Byte
cmd[5] = (size >> 8) AS Byte
WriteFile(hSerial,cmd,6,VarPtr(dwReadByte),ByVal NULL)
dwReadByte=0
ReadROM=ReceiveFromReader(hSerial, data, size)
printf(ex"$%02X:%04X %X/%X bytes received.\n",address>>16,address And &HFFFF,ReadROM,size)
End Function
Function ReceiveFromReader(hSerial AS HANDLE,data AS BytePtr, size AS Long)(TimeoutMul AS Single) As Long
Dim TotalReadByte As DWord
Dim dwReadByte As DWord
if TimeoutMul <= 1.0 Then
TimeoutMul=1.0
End If
Const SleepTime = 32 As DWord
'デフォルトのリトライ回数(=タイムアウトのms)
'TIMEOUT = 転送時間(理論値)× 10 + 10
Dim rx_time AS Single
rx_time = ( (size*1000*8) / (GetComBaudrate(hSerial)) )
Dim HKAC_HW_RETRY As DWord
HKAC_HW_RETRY = ( (rx_time * HKAC_TRANSFER_FLOAT_TIME_MUL * TimeoutMul + HKAC_RX_TIMEOUT_MARGIN) / SleepTime) + 1 AS DWord
printf(ex"論理転送時間:%dms ",(rx_time) As DWord)
Dim dwErrors AS DWord,retry AS DWord
Dim cs AS COMSTAT
Dim requestSize As Long
Do
if retry>HKAC_HW_RETRY or GetCancelKey() And &H80 Then ExitDo
ClearCommError(hSerial,VarPtr(dwErrors),cs)
if cs.cbInQue=0 Then
retry++
'if retry Mod 100 Then Sleep(1)
Sleep(SleepTime)
Continue
End If
' limit request size
if TotalReadByte+cs.cbInQue > size Then
requestSize = size - TotalReadByte
Else
requestSize = cs.cbInQue
End If
if ReadFile(hSerial,(data) + TotalReadByte, requestSize, VarPtr(dwReadByte),ByVal NULL) = FALSE Then DBM(ex"\r\n[Connection Error!] GLE:"+Str$(GetLastError())):ExitDo
TotalReadByte+=dwReadByte
if TotalReadByte > size Then debug
if TotalReadByte >= size Then exitdo
Loop
printf(ex"retry %03d/%03d ",retry,HKAC_HW_RETRY)
ReceiveFromReader=TotalReadByte
End Function
Function WaitForRequest(hSerial AS HANDLE,requestChar AS Byte,timedOut_ms AS DWord) AS BOOL
Dim dwErrors AS DWord,retry AS DWord,rsv AS Byte
Dim cs AS COMSTAT,readLen AS DWord
Do
'受信バッファ数取得
ClearCommError(hSerial,VarPtr(dwErrors),cs)
if cs.cbInQue=0 Then
retry++
if retry>timedOut_ms Then WaitForRequest=FALSE:ExitDo
readLen=0
Else
readLen=1
End If
ReadFile(hSerial,VarPtr(rsv),readLen ,VarPtr(dwErrors),ByVal NULL)
if rsv=requestChar Then
WaitForRequest=TRUE
ExitDo
Else
'なぜかここに来て読んだバイト数が0になることがある
if dwErrors<>readLen Then
printf(ex"[WaitRequest]Invailed Request : %X[%c] <> %X[%c] %d \n",rsv,rsv,requestChar,requestChar,dwErrors)
End if
End If
Sleep(1)
retry++
if retry<timedOut_ms Then Continue
Loop
' if retry>10 Then
printf(ex"[WaitRequest] char:%c wait:%d ms ok:%d\n",requestChar,retry,WaitForRequest)
' End If
End Function
'カートリッジへの出力データ送信(ROMだけでなくカートリッジ上のメモリ空間も対象)
'コントロールバスはいじらないので手動でやる必要あり
Function WriteROM(hSerial AS HANDLE,data AS BytePtr, offset AS Long,address AS DWord, size AS Long, isLoROM AS BOOL) As Long
Dim cmd[7] AS Byte
Dim WriteCount As DWord
Dim dwWD As DWord
if isLoROM Then
cmd[0]=Asc("w")
Else
cmd[0]=Asc("W")
End if
cmd[1] = (address >> 0) AS Byte
cmd[2] = (address >> 8) AS Byte
cmd[3] = (address >> 16) AS Byte
cmd[4] = (size >> 0) AS Byte
cmd[5] = (size >> 8) AS Byte
' cmd[6] = (size >> 16) AS Byte
WriteFile(hSerial,cmd,6,VarPtr(dwWD),ByVal NULL)
Sleep(10)
WriteCount=0
Do
printf("[WRITE] %X-%Xh ",address+WriteCount,address+WriteCount+HKAC_WRITE_BUFFER_LEN-1)
if WaitForRequest(hSerial,Asc("R"),1000) = FALSE Then ExitDo
WriteFile(hSerial, data+WriteCount , HKAC_WRITE_BUFFER_LEN ,VarPtr(dwWD),ByVal NULL)
WriteCount+=dwWD
if WriteCount => size Then exitdo
if dwWD=0 Then debug
Loop
WriteROM=WriteCount
if WaitForRequest(hSerial,Asc("E"),1000) = FALSE Then
DBM("[DBG]Firmware Error!")
DBM("書込ルーチン暴走中")
End If
End Function
Enum HKAC_CLOCK_STATE
HKAC_CLOCK_NORMAL = &H35,
HKAC_CLOCK_CPU_ENABLED = &H37,
HKAC_CLOCK_CPU_OVERCLOCKED = &H3f,
HKAC_CLOCK_DISABLED = &H30
End Enum
Sub SetCPU_Clock(hSerial AS HANDLE, flag AS HKAC_CLOCK_STATE)
Dim dwWB AS DWord
Dim cmd[1] AS Byte
cmd[0] = GetByte("g")
cmd[1] = flag as Byte
WriteFile(hSerial, cmd, 2, VarPtr(dwWB) , ByVal NULL)
ReadFile(hSerial, cmd, 1, VarPtr(dwWB), ByVal NULL)
printf(ex"ClockOutput: %d, %d, %d, oc:%d ret:%X dwAB:%d\n", (flag And 1), (flag And 2)>>1, (flag And 4)>>2, (flag And 8)>>3, cmd[0], dwWB)
End Sub
Function hasClockModule(hSerial AS HANDLE) AS BOOL
Dim dwWB AS DWord
Dim cmd[1] AS Byte
cmd[0] = Asc("G")
WriteFile(hSerial, cmd, 2, VarPtr(dwWB) , ByVal NULL)
ReadFile(hSerial, cmd, 1, VarPtr(dwWB), ByVal NULL)
if cmd[0] = &H31 Then
hasClockModule = TRUE
Else
hasClockModule = FALSE
End If
End Function
Const HKAC_GETFIRM_RETRY = 25
Const HKAC_GETFIRM_WAIT = 10
Function GetFirmware(hSerial As HANDLE, firmStr AS BytePtr, firmStrLen As DWord) As BOOL
Dim cmd[2] AS Byte
Dim dwWD As DWord, ioOk AS BOOL, retry As DWord
GetFirmware = FALSE
*HW_GF_RETRY
ioOk = WriteFile(hSerial, "v", 1, VarPtr(dwWD), ByVal NULL)
if ioOk<>TRUE Then ExitFunction
Sleep(10)
printf(ex"\n[GetFirm%d] ", retry)
dwWD = ReceiveFromReader(hSerial, firmStr, firmStrLen, 10)
if dwWD<>firmStrLen Then
printf(ex" retry #%d, %d ", retry, dwWD)
if retry>=HKAC_GETFIRM_RETRY Then ExitFunction
FlushCom(hSerial)
Sleep(HKAC_GETFIRM_WAIT)
retry++
Goto *HW_GF_RETRY
End If
FlushCom(hSerial)
printf(ex"\n")
GetFirmware = TRUE
End Function
Sub FlushCom(hSerial AS HANDLE)
PurgeComm(hSerial, PURGE_TXABORT)
PurgeComm(hSerial, PURGE_RXABORT)
Dim cs AS COMSTAT, dwErrors AS DWord
ClearCommError(hSerial, VarPtr(dwErrors), cs)
' printf("[flush] i%d o%d", cs.cbInQue, cs.cbOutQue)
if cs.cbInQue = 0 Then ExitSub
'受信バッファを全部取り出す
Dim tmp aS BytePtr
tmp = calloc(cs.cbInQue)
LoadFile(hSerial, tmp, cs.cbInQue)
printf("ゴミ %d", cs.cbInQue)
Dump(tmp, cs.cbInQue)
free(tmp)
tmp = NULL
FlushFileBuffers(hSerial)
End Sub
Sub SetCartRegister(bank AS Byte, address AS Word, val AS Byte)(isLoROM AS BOOL)
Dim cmd[5] AS Byte, dwAB AS DWord, retry AS BOOL
if isLoROM Then
cmd[0] = Asc("t")
Else
cmd[0] = Asc("T")
End If
cmd[1] = bank
cmd[2] = (address >> 0) AS Byte
cmd[3] = (address >> 8) AS Byte
cmd[4] = val
WriteFile(hCOM, cmd, 5, VarPtr(dwAB), ByVal NULL)
printf(ex"[CMD_%c]%02X%04X = %X\n", cmd[0], bank, address, val)
End Sub
Function ST018_BiosDump(hSerial AS HANDLE, data AS BytePtr)(progress AS * ProgressCallback) As BOOL
Dim dwReadByte As DWord
WriteFile(hSerial, "z", 1, VarPtr(dwReadByte), ByVal NULL)
dwReadByte = 0
if progress<>NULL Then progress->Init(160)
if WaitForRequest(hSerial, GetByte("S"), 2000) = FALSE Then Goto * SBD_ERR
Const ST018_BIOS_TRANSFER_SIZE = (1024) AS DWord
Dim ReadByteSum AS DWord, i AS Long
For i = 0 To 159
dwReadByte = ReceiveFromReader(hSerial, data + ReadByteSum, ST018_BIOS_TRANSFER_SIZE, 4)
if dwReadByte <> ST018_BIOS_TRANSFER_SIZE Then
printf(ex"[BIOS DUMP] TRANSFER ERROR %X/%X\n", dwReadByte, ST018_BIOS_TRANSFER_SIZE)
Goto * SBD_ERR
End if
printf(ex"[BIOS DUMP] %03X:%X/%X bytes received.\n", i * ST018_BIOS_TRANSFER_SIZE, dwReadByte, ST018_BIOS_TRANSFER_SIZE)
ReadByteSum = ReadByteSum + dwReadByte
'Continue 送信
WriteFile(hSerial, "c", 1, VarPtr(dwReadByte), ByVal NULL)
if progress<>NULL Then progress->Step(1, ReadByteSum)
Next i
if progress<>NULL Then progress->Final()
ST018_BiosDump = TRUE
ExitFunction
*SBD_ERR
if progress<>NULL Then progress->Final()
'キャンセル送信
WriteFile(hSerial, "e", 1, VarPtr(dwReadByte), ByVal NULL)
End Function