-
Notifications
You must be signed in to change notification settings - Fork 8
/
slot2.go
87 lines (71 loc) · 1.4 KB
/
slot2.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
package main
import (
"io"
"io/ioutil"
"ndsemu/homebrew"
"os"
)
var highz [16]byte = [...]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
type HwSlot2 struct {
Rom []byte
Ram [64 * 1024]byte
}
func NewHwSlot2() *HwSlot2 {
return &HwSlot2{
Rom: highz[:],
}
}
func roundup2(v int) int {
v--
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v++
return v
}
func (slot *HwSlot2) mapCart(data []byte, concat bool) error {
if concat {
slot.Rom = append(slot.Rom, data...)
} else {
slot.Rom = data
}
sz := roundup2(len(slot.Rom))
if sz != len(slot.Rom) {
data2 := make([]byte, sz)
for i := 0; i < len(data2); i++ {
data2[i] = 0xFF
}
copy(data2, slot.Rom)
slot.Rom = data2
}
return nil
}
func (slot *HwSlot2) MapCart(f io.Reader) error {
data, err := ioutil.ReadAll(f)
if err != nil {
return err
}
return slot.mapCart(data, false)
}
func (slot *HwSlot2) MapCartFile(fn string) error {
f, err := os.Open(fn)
if err != nil {
return err
}
defer f.Close()
return slot.MapCart(f)
}
func (slot *HwSlot2) HomebrewMapFatFile(fn string) error {
data, err := ioutil.ReadFile(fn)
if err != nil {
return err
}
// Patch the FAT partition to be usable by the FCSR backend of libfat
homebrew.FcsrPatchFatPartition(data)
return slot.mapCart(data, true)
}
func (slot *HwSlot2) UnmapCart() {
slot.Rom = highz[:]
}