-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexTable.bb
More file actions
134 lines (108 loc) · 3.93 KB
/
Copy pathHexTable.bb
File metadata and controls
134 lines (108 loc) · 3.93 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
Global HexT%[256] ; This array allows for quick conversion of Hex to Decimal
CreateHexTable() ; We must initialize the HexTable
Function CreateHexTable() ;;a very quick lookup table To convert Hex To Dec
Local z
For z%=48 To 57 ; Create listings for "0" to "9"
HexT[z%]=z%-48
Next
For z% =65 To 70 ; Create listings for "A" to "F"
HexT[z%]=z%-55
Next
For z% =97 To 102 ; Create listings for "a" to "f"
HexT[z%]=z%-87
Next
End Function
Function HtoD(H$) ; Converts a hex-string into a decimal value, input limit = "FFFFFFF"
Local z,L,v
L% = Len(H$)
v%=0 : z%=0
If L=0 Then Return v
v =HexT[Asc(H$)]
For z = 2 To L
v = (v Shl 4)+ HexT[Asc(Mid$(H$,z,1))]
Next
Return v
End Function
;;___________________________________________________________________________________
;; This function transfers hex values (stored in a string) into a bank
;;
;; If the given bank 'b' does not exist (=0), then a bank is created for you.
;; All non-hex characters shall be ignored, and shall not be poked into bank.
;;
;; This function expects that each hex byte value must be at least 2 ascii characters.
;;
;; ( For example, always represent zero with "00", and 10 with "0A" )
;;
;; THis function returns the Bank handle,
;; or else, it shall return a negative value if an error occurres.
;;
Function HexToBank%(B,h$)
Local L=Len(h)
Local I$, A$, ascii, z, offset=0, CodeLen=0
h = Upper$(h)
For z=1 To L
A =Mid$(h,z,1)
ascii = Asc(A)
r = isHexDigit(ascii)
If isHexDigit(ascii)
I = I + A
End If
Next
StrLen = Len(I)
CodeLen = StrLen Shr 1 ;; CodeLen =length of code in bytes (pure machine code only)
If StrLen And 1 ;; An error occures if it's not a multiple of 2 ..
Return -CodeLen ;; Indicate an error by returning a negative value
End If
If b=0
b = CreateBank(CodeLen)
Else
If BankSize(b)<CodeLen
Return -CodeLen ;; Indicate an error by returning a negative value
End If
End If
For z=1 To StrLen Step 2
PokeByte b,offset,HtoD(Mid$(I,z,2))
offset = offset +1
Next
Return B ;; return the bank handle
End Function
;; This function is the same as the previous one, except that it
;; automatically creates a bank for you
;;
Function HexToNewBank%(h$)
Local L=Len(h)
Local I$, A$, ascii, z, offset=0, CodeLen=0, B=0
h = Upper$(h)
For z=1 To L
A =Mid$(h,z,1)
ascii = Asc(A)
r = isHexDigit(ascii)
If isHexDigit(ascii)
I = I + A
End If
Next
StrLen = Len(I)
CodeLen = StrLen Shr 1 ;; CodeLen = length of code in bytes (no spaces are left)
If StrLen And 1 ;; An error occures if it's not a multiple of 2 ..
Return -CodeLen ;; Indicate an error by returning a negative value
End If
B = CreateBank(CodeLen)
For z=1 To StrLen Step 2
PokeByte b,offset,HtoD(Mid$(I,z,2))
offset = offset +1
Next
Return B ;; return the bank handle
End Function
;; Returns True if it's a hex digit
Function isHexDigit%( ascii )
If ascii>=48 And ascii<=57 ; "0" to "9"
Return True
End If
If ascii>=65 And ascii<=70 ; "A" to "F"
Return True
End If
If ascii>=97 And ascii<=102 ; "a" to "f"
Return True
End If
Return False
End Function