This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
163 lines (139 loc) · 4.21 KB
/
main.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
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
package main
// TODO: Add option to output FCEUX symbol files
import (
"io/ioutil"
"fmt"
"os"
"sort"
"strings"
)
var (
verbose bool
inputName string
outputMesen bool
outputFceux bool
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Missing input file")
os.Exit(1)
}
inputname := os.Args[1]
file, err := ioutil.ReadFile(inputname)
if err != nil {
fmt.Println("Unable to read input file: ", err)
os.Exit(1)
}
lines := strings.Split(strings.Replace(string(file), "\r", "", -1), "\n")
segments := []*Segment{}
symbols := []*Symbol{}
segmentMap := make(map[int]*Segment)
segmentTree := make(map[int][]*Symbol)
// Read input file and parse all segments and symbols
for _, line := range lines {
// Ignore invalid lines
if len(line) < 4 {
continue
}
if IsSegment(line) {
seg, err := ParseSegment(line)
if err != nil {
fmt.Println(err)
}
segments = append(segments, seg)
segmentMap[seg.Id] = seg
} else if IsSymbol(line) {
sym, err := ParseSymbol(line)
if err != nil {
fmt.Println(err)
}
symbols = append(symbols, sym)
segmentTree[sym.Segment] = append(segmentTree[sym.Segment], sym)
}
}
for seg, syms := range segmentTree {
if segment, ok := segmentMap[seg]; ok {
if segment.Type != "ro" { // Don't bother with non-labels
continue
}
} else if (seg == -1) { // Or with things that don't have a segment (eg, id -1)
continue
} else {
fmt.Printf("Unable to find segment in map: %d\n", seg)
continue
}
sort.Sort(SymbolSlice(syms))
parent := ""
for _, sym := range syms {
if (sym.Name[0] != '@') {
parent = sym.Name
} else {
sym.Parent = parent
}
}
}
// Create the .mlb output file for Mesen
outfile, err := os.Create(strings.Replace(inputname, ".nes.db", ".mlb", -1))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer outfile.Close()
// Init the FCEUX helper class. This holds references to a bunch of
// files that are created on demand for each page.
fceux, err := NewFceux(inputname)
if err != nil {
fmt.Printf("Unable to init FCEUX helper: %s\n", err)
os.Exit(1)
}
defer fceux.Close()
count := 0 // Just used to show the user at the end
for _, sym := range symbols {
// Ignore symbols that don't have a segment (usually internal symbols)
if sym.Segment == -1 {
continue
}
// Find the segment for the current symbol
var seg *Segment
for _, s := range segments {
if s.Id == sym.Segment {
seg = s
break
}
}
// skip labels that don't have a segment
if seg == nil {
continue
}
abs := sym.Value
t := "P"
// Variable addresses don't need to be adjusted.
if sym.Value >= 0x6000 && sym.Value < 0x8000 {
t = "S"
abs = sym.Value - 0x6000
} else if len(seg.OutputFile) == 0 {
t = "R"
} else {
// minus start of PRG, minus iNES header, plus output file offset
abs = sym.Value - seg.Start + seg.OutputOffset - 16
}
if abs < 0 {
fmt.Printf("ERROR: absolute address is negative!\n Label:%q Segment:%q abs:%d sym.Value:%d seg.Start:%d seg.OutputOffset:%d\n",
sym.Name,
seg.Name,
abs,
sym.Value,
seg.Start,
seg.OutputOffset,
)
os.Exit(1)
}
// Type (PRG/RAM), Address (loaded into CPU space), Name
fmt.Fprintf(outfile, "%s:%04X:%s%s\n", t, abs, sym.Parent, sym.Name)
// FCEUX stuff
fceux.Write(sym, seg)
count += 1
}
// Report how many we found
fmt.Printf(" Exported labels: %d\n", count)
}