/
instructions.go
119 lines (100 loc) · 3.09 KB
/
instructions.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
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package instructions
import "fmt"
// AddressingMode describes the method data for the instruction should be received.
type AddressingMode int
func (m AddressingMode) String() string {
switch m {
case Implied:
return "Implied"
case Immediate:
return "Immediate"
case Relative:
return "Relative"
case Absolute:
return "Absolute"
case ZeroPage:
return "ZeroPage"
case Indirect:
return "Indirec"
case IndexedIndirect:
return "IndexedIndirect"
case IndirectIndexed:
return "IndirectIndexed"
case AbsoluteIndexedX:
return "AbsoluteIndexedX"
case AbsoluteIndexedY:
return "AbsoluteIndexedY"
case ZeroPageIndexedX:
return "ZeroPageIndexedX"
case ZeroPageIndexedY:
return "ZeroPageIndexedY"
}
return "unknown addressing mode"
}
// List of supported addressing modes.
const (
Implied AddressingMode = iota
Immediate
Relative // relative addressing is used for branch instructions
Absolute // abs
ZeroPage // zpg
Indirect // ind
IndexedIndirect // (ind,X)
IndirectIndexed // (ind), Y
AbsoluteIndexedX // abs,X
AbsoluteIndexedY // abs,Y
ZeroPageIndexedX // zpg,X
ZeroPageIndexedY // zpg,Y
)
// EffectCategory categorises an instruction by the effect it has.
type EffectCategory int
// List of effect categories.
const (
Read EffectCategory = iota
Write
RMW
// the following three effects have a variable effect on the program
// counter, depending on the instruction's precise operand.
// flow consists of the Branch and JMP instructions. Branch instructions
// specifically can be distinguished by the AddressingMode.
Flow
Subroutine
Interrupt
)
// Definition defines each instruction in the instruction set; one per instruction.
type Definition struct {
OpCode uint8
Operator string
Bytes int
Cycles int
AddressingMode AddressingMode
PageSensitive bool
Effect EffectCategory
// Whether instruction is "undocumented".
Undocumented bool
}
// String returns a single instruction definition as a string.
func (defn Definition) String() string {
if defn.Operator == "" {
return "undecoded instruction"
}
return fmt.Sprintf("%02x %s +%dbytes (%d cycles) [mode=%d pagesens=%t effect=%d]", defn.OpCode, defn.Operator, defn.Bytes, defn.Cycles, defn.AddressingMode, defn.PageSensitive, defn.Effect)
}
// IsBranch returns true if instruction is a branch instruction.
func (defn Definition) IsBranch() bool {
return defn.AddressingMode == Relative && defn.Effect == Flow
}