-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGB_HardwareLevel.sbp
151 lines (122 loc) · 5.14 KB
/
GB_HardwareLevel.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
Const DMG_ACCESS_RD_PULSE = &H01
Const DMG_ACCESS_DEEP_READ = &H02
Const DMG_ACCESS_NO_CS_PULSE=&H04
'GBのカートリッジアクセス
Const FTE_READ_PACKET_SIZE = 1024*2
Const FTE_WRITE_PACKET_SIZE = 1024*16
Dim drop_count As DWord
Function DMG_ReadCart(fe AS *FT232H_DMGROM,buffer AS BytePtr,Address AS DWord, Length AS DWord, flags AS DWord) AS BOOL'(bRD_Pulse AS BOOL, deepRead As BOOL) AS BOOL
Dim readCount AS Long, lastReadSize AS Long
readCount = Length / FTE_READ_PACKET_SIZE
lastReadSize = Length - readCount*FTE_READ_PACKET_SIZE
Dim bRD_Pulse AS BOOL, deepRead As BOOL
bRD_Pulse = flags And DMG_ACCESS_RD_PULSE
deepRead = flags And DMG_ACCESS_DEEP_READ
if flags And DMG_ACCESS_NO_CS_PULSE then
fe->ROMEnable(FALSE)
Else
fe->ROMEnable(TRUE)
endif
Dim i As Long,readOffset AS DWord
Do
if i => readCount Then ExitDo
readOffset = i*FTE_READ_PACKET_SIZE
' printf(ex"READ %02X:%04X +%04X¥n",readOffset>>16,readOffset And &HFFFF,FTE_READ_PACKET_SIZE)
FT232H_ReadEPROM(fe, buffer + readOffset,Address + readOffset, FTE_READ_PACKET_SIZE, bRD_Pulse, deepRead)
i++
Loop
if lastReadSize > 0 Then
readOffset = i*FTE_READ_PACKET_SIZE
' printf(ex"Last %02X:%04X +%04X¥n",readOffset>>16,readOffset And &HFFFF,lastReadSize)
FT232H_ReadEPROM(fe, buffer + readOffset,Address + readOffset, lastReadSize , bRD_Pulse, deepRead)
End If
fe->ROMEnable(FALSE)
DMG_ReadCart=TRUE
End Function
Function DMG_WriteCart(fe AS *FT232H_DMGROM,buffer AS BytePtr,Address AS DWord, Length AS DWord) AS BOOL
Dim WriteCount AS Long, lastWriteSize AS Long
WriteCount = Length / FTE_WRITE_PACKET_SIZE
lastWriteSize = Length - WriteCount*FTE_WRITE_PACKET_SIZE
fe->ROMEnable(TRUE)
Dim i As Long,WriteOffset AS DWord
Do
if i => WriteCount Then ExitDo
WriteOffset = i*FTE_WRITE_PACKET_SIZE
' printf(ex"Write %02X:%04X +%04X¥n",WriteOffset>>16,WriteOffset And &HFFFF,FTE_WRITE_PACKET_SIZE)
fe->AreaWirte(Address+WriteOffset, buffer+WriteOffset, FTE_WRITE_PACKET_SIZE)
i++
Loop
if lastWriteSize > 0 Then
WriteOffset = i*FTE_WRITE_PACKET_SIZE
fe->AreaWirte(Address+WriteOffset, buffer+WriteOffset, lastWriteSize)
End If
fe->ROMEnable(FALSE)
DMG_WriteCart=TRUE
End Function
Function winner(a As Byte, b As Byte, c As Byte) As Byte
winner = (a And b) Or (b And c) Or (a And c)
End Function
Function winner5(a As Byte, b As Byte, c As Byte, d As Byte, e As Byte) As Byte
winner5 = (a And b) Or (b And c) Or (a And c) Or (a And d) Or (b And d) Or (c And d) _
Or (a And e) Or (b And e) Or (c And e) Or (d And e)
End Function
'[低レイヤな転送] 1回の転送
Function FT232H_ReadEPROM(fe AS *FT232H_DMGROM,buffer AS BytePtr,Address AS DWord, Length AS DWord)(bRD_Pulse AS BOOL, deepRead As BOOL) As BOOL
fe->AreaReadRequest(Address,Length,bRD_Pulse, deepRead)
fe->FT_SendCommands()
if deepRead=FALSE Then
FT232H_ReadEPROM = fe->FT_ReceiveData(buffer,Length)
Else
/* Dim localbuf As BytePtr, ofs As DWord,i AS Long
localbuf = calloc(Length*3)
FT232H_ReadEPROM = fe->FT_ReceiveData(localbuf,Length*3)
for i=0 To Length-1
ofs=i*3
buffer[i] = winner(localbuf[ofs],localbuf[ofs+1],localbuf[ofs+2])
if localbuf[ofs]<>localbuf[ofs+1] OR localbuf[ofs+2]<>localbuf[ofs] OR localbuf[ofs+2]<>localbuf[ofs+1] then
printf(ex"filter: %08x:%02x = %02x %02x %02x¥n", Address+i, buffer[i], localbuf[ofs],localbuf[ofs+1],localbuf[ofs+2])
End If
Next
free(localbuf)
*/
Dim localbuf As BytePtr, ofs As DWord,i AS Long
localbuf = calloc(Length*5)
FT232H_ReadEPROM = fe->FT_ReceiveData(localbuf,Length*5)
for i=0 To Length-1
ofs=i*5
buffer[i] = winner5(localbuf[ofs],localbuf[ofs+1],localbuf[ofs+2],localbuf[ofs+3],localbuf[ofs+4])
if localbuf[ofs]<>buffer[i] OR localbuf[ofs+1]<>buffer[i] OR localbuf[ofs+2]<>buffer[i] OR localbuf[ofs+3]<>buffer[i] OR localbuf[ofs+4]<>buffer[i] then
drop_count++
printf(ex"filter: %08x:%02x = %02x %02x %02x %02x %02x cnt=%d¥n", Address+i, buffer[i], localbuf[ofs],localbuf[ofs+1],localbuf[ofs+2],localbuf[ofs+3],localbuf[ofs+4], drop_count)
End If
Next
free(localbuf)
endif
End Function
'1バイトRead
Function FT232H_ReadByte(fe AS *FT232H_DMGROM,Address AS DWord) As Byte
fe->ReadDataRequest()
fe->FT_SendCommands()
fe->FT_ReceiveData(VarPtr(FT232H_ReadByte),1)
End Function
Sub MBC_RegisterWrite(fe AS *FT232H_DMGROM, Address AS DWord,val AS Byte)
' fe->ROMEnable(FALSE)
fe->SetAddress(Address)
fe->WriteByte( val)
' fe->ROMEnable(TRUE)
End Sub
Sub GBMC_RegisterWrite(fe AS *FT232H_DMGROM, Address AS DWord,val AS Byte)
' fe->ROMEnable(FALSE)
fe->SetAddress(Address)
fe->WriteByte( val)
' fe->ROMEnable(TRUE)
End Sub
Sub flush(hFT AS HANDLE)
Dim ftStatus As Long,bufsize As DWord,buf As BytePtr,dwAB As DWord
ftStatus = ftStatus or FT_GetQueueStatus(hFT, bufsize) ' Get the number of bytes in the FT2232H receive buffer
buf=calloc(bufsize)
if bufsize>0 then printf(ex"[flush] %d bytes¥n", bufsize)
if (ftStatus = FT_OK) And (bufsize > 0) Then _
FT_Read(hFT, buf, bufsize, dwAB) 'Read out the data from FT2232H receive buffer
free(buf)
End Sub