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
/
Copy pathsegment.go
114 lines (97 loc) · 3.31 KB
/
segment.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
package main
import (
"fmt"
"strconv"
"strings"
)
type Segment struct {
Id int // unique segment ID. Symbols link to this.
Name string // name of the segment ("PAGE0", "BSS", etc).
Start int // start of address space
Size int // size of the assembled segment
Type string //
OutputFile string // File this segment will be linked into
OutputOffset int // Offset in the output file
}
func (s *Segment) String() string {
return fmt.Sprintf("[%d] %s type:%s start:$%04X Size:$%04X [%s @ $%04X]",
s.Id, s.Name, s.Type, s.Start, s.Size, s.OutputFile, s.OutputOffset)
}
// IsSegment returns true if the given line describes a Segment
func IsSegment(line string) bool {
if line[0:4] == "seg\t" {
return true
}
return false
}
func (s *Segment) IsRam() bool {
if s.Type == "rw" {
return true
}
return false
}
// TODO: make this not hard coded
func (s *Segment) PageID() int {
switch s.Id {
case 12:
return 0
case 13:
return 1
case 14:
return 2
case 15, 9:
return 3
}
return -1
}
// ParseSegment returns a *Segment given a line
func ParseSegment(line string) (*Segment, error) {
if len(line) < 4 || line[0:4] != "seg\t" {
return nil, fmt.Errorf("Invalid segment line: %q", line)
}
items := strings.Split(line[4:], ",")
seg := &Segment{}
for _, item := range items {
keyval := strings.SplitN(item, "=", 2)
switch keyval[0] {
// Symbols reference this ID.
case "id":
id, err := strconv.ParseInt(keyval[1], 10, 32)
if err != nil {
return nil, fmt.Errorf("Invalid segment Id: %q: %s", keyval[1], err)
}
seg.Id = int(id)
// Address of the start of the segment
case "start":
val, err := strconv.ParseInt(keyval[1], 0, 32)
if err != nil {
return nil, fmt.Errorf("Invalid segment Start value: %q: %s", keyval[1], err)
}
seg.Start = int(val)
// Unneeded, but parsed anyway
case "name":
seg.Name = keyval[1][1:len(keyval[1]) - 1]
// Also unneeded, but parsed anyway.
case "size":
val, err := strconv.ParseInt(keyval[1], 0, 32)
if err != nil {
return nil, fmt.Errorf("Invalid segment Size value: %q: %s", keyval[1], err)
}
seg.Size = int(val)
case "type":
seg.Type = keyval[1]
// Output file offset. Used to calculate a symbol's location in the ROM file.
case "ooffs":
val, err := strconv.ParseInt(keyval[1], 0, 32)
if err != nil {
return nil, fmt.Errorf("Invalid segment OutputOffset value: %q: %s", keyval[1], err)
}
seg.OutputOffset = int(val)
// Output filename. If blank, it's not written to the ROM and is
// ignored later on (eg "ZEROPAGE" and "BSS" segments)
case "oname":
seg.OutputFile = keyval[1][1:len(keyval[1]) - 1]
}
}
return seg, nil
}