-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmode.go
62 lines (53 loc) · 1.24 KB
/
mode.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
package main
import (
"math"
"strconv"
)
// Mode defines the mode of an argument, i.e. whether it points to a Position or
// is the value itself. See const declaration for concrete values.
type Mode uint8
type ModeInfo struct {
Name string
Fn func(*Program, int) int
}
var Modes = [...]ModeInfo{
0: {
Name: "Position",
Fn: Position,
},
1: {
Name: "Immediate",
Fn: Immediate,
},
2: {
Name: "Relative Base",
Fn: RelativeBase,
},
}
func Position(program *Program, index int) int {
return int(program.Ints[index])
}
func Immediate(_ *Program, index int) int {
return index
}
func RelativeBase(program *Program, index int) int {
return int(program.RelBase + program.Ints[index])
}
// NewModeList creates a new mode list with num entries from val. Thus, the
// right-most digit is the first and the left-most digit is the last.
func NewModeList(val int64, num int) []Mode {
val /= 1e2
modes := make([]Mode, num)
for i := 0; i < num; i++ {
// The division results in the Position mode (0), if no parameter is specified
modes[i] = Mode((val / int64(math.Pow10(i))) % 10)
}
return modes
}
func (m Mode) String() string {
if int(m) < len(Modes) {
return Modes[m].Name
} else {
return "Mode_" + strconv.Itoa(int(m))
}
}