forked from djhworld/gomeboycolor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
94 lines (79 loc) · 1.67 KB
/
utils.go
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
package utils
import (
"errors"
"fmt"
"strconv"
"strings"
)
func ByteToString(b byte) string {
var zeroes string
if b < 0x10 {
zeroes += "0"
}
return fmt.Sprintf("0x%s%X", zeroes, b)
}
func StringToByte(s string) (byte, error) {
s = strings.Replace(s, "0x", "", 1)
if len(s) > 2 {
return 0x0, errors.New("Please enter an address between 00 and FF")
}
result, err := strconv.ParseInt(s, 16, 64)
return byte(result), err
}
func StringToWord(s string) (uint16, error) {
s = strings.Replace(s, "0x", "", 1)
if len(s) > 4 {
return 0x0, errors.New("Please enter an address between 0000 and FFFF")
}
result, err := strconv.ParseInt(s, 16, 64)
return uint16(result), err
}
//Joins two bytes together to form a 16 bit integer
func JoinBytes(hob, lob byte) uint16 {
return (uint16(hob) << 8) ^ uint16(lob)
}
//Splits one 16 bit integer to two bytes
func SplitIntoBytes(bb uint16) (byte, byte) {
return byte(bb >> 8), byte(bb & 0x00FF)
}
//swaps the nibbles of a byte around
func SwapNibbles(a byte) byte {
return (a&0xF0)>>4 ^ ((a & 0x0F) << 4)
}
func CompareBytes(a, b byte, operator string) bool {
switch operator {
case "==":
return a == b
case ">":
return a > b
case "<":
return a < b
case ">=":
return a >= b
case "<=":
return a <= b
}
return false
}
func CompareWords(a, b uint16, operator string) bool {
switch operator {
case "==":
return a == b
case ">":
return a > b
case "<":
return a < b
case ">=":
return a >= b
case "<=":
return a <= b
}
return false
}
func PadRight(s string, maxLen int, padStr string) string {
if l := len(s); l < maxLen {
padAmount := maxLen - l
s = s + strings.Repeat(padStr, padAmount)
}
return s
}