Skip to content

Commit

Permalink
adapt addressing imports to latest retrogolib changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelk committed Dec 18, 2024
1 parent 4d5688a commit 0923e9f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 63 deletions.
6 changes: 0 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,10 @@ linters-settings:
min-complexity: 8
revive:
rules:
- name: dot-imports
disabled: true
- name: exported
disabled: true
- name: var-naming
disabled: true
stylecheck:
checks:
- "all"
- "-ST1001" # should not use dot imports
whitespace:
multi-if: true # Enforces newlines (or comments) after every multi-line if statement
multi-func: true # Enforces newlines (or comments) after every multi-line function signature
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/retroenv/nesgodisasm

go 1.22

require github.com/retroenv/retrogolib v0.0.0-20241211024829-974a9c462c2d
require github.com/retroenv/retrogolib v0.0.0-20241218182911-894f1c8e84e3
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/retroenv/retrogolib v0.0.0-20241211024829-974a9c462c2d h1:m4nCFBbox2t++8XPAIwJb5+6Z42lZuWQOKankkKVzSc=
github.com/retroenv/retrogolib v0.0.0-20241211024829-974a9c462c2d/go.mod h1:8pe9mEjbKL9Z5L4FFzYGSk1Ovhrq1LR6ucwRFj5CIXs=
github.com/retroenv/retrogolib v0.0.0-20241218182911-894f1c8e84e3 h1:cBz3rYoG9I91H/U4MsfRft7VvEe5XtlnDl41Oa/FK+8=
github.com/retroenv/retrogolib v0.0.0-20241218182911-894f1c8e84e3/go.mod h1:zyJCaDI509lBE90xkZJxc0vGZGK5MnY9NIL3ckK2MEE=
7 changes: 3 additions & 4 deletions internal/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"strings"

. "github.com/retroenv/retrogolib/addressing"
"github.com/retroenv/retrogolib/arch/cpu/m6502"
"github.com/retroenv/retrogolib/arch/nes/register"
)
Expand Down Expand Up @@ -52,19 +51,19 @@ func buildConstMap() (map[uint16]constTranslation, error) {
return m, nil
}

func mergeConstantsMaps(destination map[uint16]constTranslation, source map[uint16]AccessModeConstant) error {
func mergeConstantsMaps(destination map[uint16]constTranslation, source map[uint16]m6502.AccessModeConstant) error {
for address, constantInfo := range source {
translation := destination[address]
translation.address = address

if constantInfo.Mode&ReadAccess != 0 {
if constantInfo.Mode&m6502.ReadAccess != 0 {
if translation.Read != "" {
return fmt.Errorf("constant with address 0x%04X and read mode is defined twice", address)
}
translation.Read = constantInfo.Constant
}

if constantInfo.Mode&WriteAccess != 0 {
if constantInfo.Mode&m6502.WriteAccess != 0 {
if translation.Write != "" {
return fmt.Errorf("constant with address 0x%04X and write mode is defined twice", address)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/jumpengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/retroenv/nesgodisasm/internal/program"
. "github.com/retroenv/retrogolib/addressing"
"github.com/retroenv/retrogolib/arch/cpu/m6502"
"github.com/retroenv/retrogolib/log"
)
Expand All @@ -27,7 +26,7 @@ type jumpEngineCaller struct {
// This can be found in some official games like Super Mario Bros.
func (dis *Disasm) checkForJumpEngineJmp(jumpAddress uint16, offsetInfo *offset) error {
instruction := offsetInfo.opcode.Instruction
if instruction.Name != m6502.Jmp.Name || offsetInfo.opcode.Addressing != IndirectAddressing {
if instruction.Name != m6502.Jmp.Name || offsetInfo.opcode.Addressing != m6502.IndirectAddressing {
return nil
}

Expand Down Expand Up @@ -150,7 +149,7 @@ func (dis *Disasm) jumpContextInfo(jumpAddress uint16, offsetInfo *offset) ([]*o
// checkForJumpEngineCall checks if the current instruction is a call into a jump engine function.
func (dis *Disasm) checkForJumpEngineCall(address uint16, offsetInfo *offset) error {
instruction := offsetInfo.opcode.Instruction
if instruction.Name != m6502.Jsr.Name || offsetInfo.opcode.Addressing != AbsoluteAddressing {
if instruction.Name != m6502.Jsr.Name || offsetInfo.opcode.Addressing != m6502.AbsoluteAddressing {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package disasm
import (
"fmt"

. "github.com/retroenv/retrogolib/addressing"
"github.com/retroenv/retrogolib/arch/cpu/m6502"
"github.com/retroenv/retrogolib/arch/nes"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func (dis *Disasm) readMemoryWord(address uint16) (uint16, error) {

// readOpParam reads the opcode parameters after the first opcode byte
// and translates it into emulator specific types.
func (dis *Disasm) readOpParam(addressing Mode, address uint16) (any, []byte, error) {
func (dis *Disasm) readOpParam(addressing m6502.AddressingMode, address uint16) (any, []byte, error) {
fun, ok := paramReader[addressing]
if !ok {
return nil, nil, fmt.Errorf("unsupported addressing mode %00x", addressing)
Expand Down
52 changes: 26 additions & 26 deletions internal/params.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package disasm

import (
. "github.com/retroenv/retrogolib/addressing"
"github.com/retroenv/retrogolib/arch/cpu/m6502"
)

type paramReaderFunc func(dis *Disasm, address uint16) (any, []byte, error)

var paramReader = map[Mode]paramReaderFunc{
ImpliedAddressing: paramReaderImplied,
ImmediateAddressing: paramReaderImmediate,
AccumulatorAddressing: paramReaderAccumulator,
AbsoluteAddressing: paramReaderAbsolute,
AbsoluteXAddressing: paramReaderAbsoluteX,
AbsoluteYAddressing: paramReaderAbsoluteY,
ZeroPageAddressing: paramReaderZeroPage,
ZeroPageXAddressing: paramReaderZeroPageX,
ZeroPageYAddressing: paramReaderZeroPageY,
RelativeAddressing: paramReaderRelative,
IndirectAddressing: paramReaderIndirect,
IndirectXAddressing: paramReaderIndirectX,
IndirectYAddressing: paramReaderIndirectY,
var paramReader = map[m6502.AddressingMode]paramReaderFunc{
m6502.ImpliedAddressing: paramReaderImplied,
m6502.ImmediateAddressing: paramReaderImmediate,
m6502.AccumulatorAddressing: paramReaderAccumulator,
m6502.AbsoluteAddressing: paramReaderAbsolute,
m6502.AbsoluteXAddressing: paramReaderAbsoluteX,
m6502.AbsoluteYAddressing: paramReaderAbsoluteY,
m6502.ZeroPageAddressing: paramReaderZeroPage,
m6502.ZeroPageXAddressing: paramReaderZeroPageX,
m6502.ZeroPageYAddressing: paramReaderZeroPageY,
m6502.RelativeAddressing: paramReaderRelative,
m6502.IndirectAddressing: paramReaderIndirect,
m6502.IndirectXAddressing: paramReaderIndirectX,
m6502.IndirectYAddressing: paramReaderIndirectY,
}

func paramReaderImplied(*Disasm, uint16) (any, []byte, error) {
Expand All @@ -36,7 +36,7 @@ func paramReaderImmediate(dis *Disasm, address uint16) (any, []byte, error) {
}

func paramReaderAccumulator(*Disasm, uint16) (any, []byte, error) {
return Accumulator(0), nil, nil
return m6502.Accumulator(0), nil, nil
}

func paramReaderAbsolute(dis *Disasm, address uint16) (any, []byte, error) {
Expand All @@ -45,23 +45,23 @@ func paramReaderAbsolute(dis *Disasm, address uint16) (any, []byte, error) {
return nil, nil, err
}

return Absolute(w), opcodes, nil
return m6502.Absolute(w), opcodes, nil
}

func paramReaderAbsoluteX(dis *Disasm, address uint16) (any, []byte, error) {
w, opcodes, err := paramReadWord(dis, address)
if err != nil {
return nil, nil, err
}
return AbsoluteX(w), opcodes, nil
return m6502.AbsoluteX(w), opcodes, nil
}

func paramReaderAbsoluteY(dis *Disasm, address uint16) (any, []byte, error) {
w, opcodes, err := paramReadWord(dis, address)
if err != nil {
return nil, nil, err
}
return AbsoluteY(w), opcodes, nil
return m6502.AbsoluteY(w), opcodes, nil
}

func paramReaderZeroPage(dis *Disasm, address uint16) (any, []byte, error) {
Expand All @@ -70,7 +70,7 @@ func paramReaderZeroPage(dis *Disasm, address uint16) (any, []byte, error) {
return nil, nil, err
}
opcodes := []byte{b}
return ZeroPage(b), opcodes, nil
return m6502.ZeroPage(b), opcodes, nil
}

func paramReaderZeroPageX(dis *Disasm, address uint16) (any, []byte, error) {
Expand All @@ -79,7 +79,7 @@ func paramReaderZeroPageX(dis *Disasm, address uint16) (any, []byte, error) {
return nil, nil, err
}
opcodes := []byte{b}
return ZeroPageX(b), opcodes, nil
return m6502.ZeroPageX(b), opcodes, nil
}

func paramReaderZeroPageY(dis *Disasm, address uint16) (any, []byte, error) {
Expand All @@ -88,7 +88,7 @@ func paramReaderZeroPageY(dis *Disasm, address uint16) (any, []byte, error) {
return nil, nil, err
}
opcodes := []byte{b}
return ZeroPageY(b), opcodes, nil
return m6502.ZeroPageY(b), opcodes, nil
}

func paramReaderRelative(dis *Disasm, address uint16) (any, []byte, error) {
Expand All @@ -105,7 +105,7 @@ func paramReaderRelative(dis *Disasm, address uint16) (any, []byte, error) {
}

opcodes := []byte{byte(offset)}
return Absolute(address), opcodes, nil
return m6502.Absolute(address), opcodes, nil
}

func paramReaderIndirect(dis *Disasm, address uint16) (any, []byte, error) {
Expand All @@ -114,7 +114,7 @@ func paramReaderIndirect(dis *Disasm, address uint16) (any, []byte, error) {
if err != nil {
return nil, nil, err
}
return Indirect(w), opcodes, nil
return m6502.Indirect(w), opcodes, nil
}

func paramReaderIndirectX(dis *Disasm, address uint16) (any, []byte, error) {
Expand All @@ -123,7 +123,7 @@ func paramReaderIndirectX(dis *Disasm, address uint16) (any, []byte, error) {
return nil, nil, err
}
opcodes := []byte{b}
return IndirectX(b), opcodes, nil
return m6502.IndirectX(b), opcodes, nil
}

func paramReaderIndirectY(dis *Disasm, address uint16) (any, []byte, error) {
Expand All @@ -132,7 +132,7 @@ func paramReaderIndirectY(dis *Disasm, address uint16) (any, []byte, error) {
return nil, nil, err
}
opcodes := []byte{b}
return IndirectY(b), opcodes, nil
return m6502.IndirectY(b), opcodes, nil
}

func paramReadWord(dis *Disasm, address uint16) (uint16, []byte, error) {
Expand Down
27 changes: 13 additions & 14 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/retroenv/nesgodisasm/internal/program"
. "github.com/retroenv/retrogolib/addressing"
"github.com/retroenv/retrogolib/arch/cpu/m6502"
"github.com/retroenv/retrogolib/arch/nes"
"github.com/retroenv/retrogolib/arch/nes/parameter"
Expand Down Expand Up @@ -42,7 +41,7 @@ func (dis *Disasm) followExecutionFlow() error {

instruction := offsetInfo.opcode.Instruction

if offsetInfo.opcode.Addressing == ImpliedAddressing {
if offsetInfo.opcode.Addressing == m6502.ImpliedAddressing {
offsetInfo.Code = instruction.Name
} else {
params, err := dis.processParamInstruction(dis.pc, offsetInfo)
Expand Down Expand Up @@ -150,7 +149,7 @@ func (dis *Disasm) processParamInstruction(address uint16, offsetInfo *offset) (
paramAsString = dis.replaceParamByAlias(address, opcode, param, paramAsString)

if _, ok := m6502.BranchingInstructions[opcode.Instruction.Name]; ok {
addr, ok := param.(Absolute)
addr, ok := param.(m6502.Absolute)
if ok {
dis.addAddressToParse(uint16(addr), offsetInfo.context, dis.pc, opcode.Instruction, true)
}
Expand Down Expand Up @@ -288,23 +287,23 @@ func (dis *Disasm) handleInstructionIRQOverlap(address uint16, offsetInfo *offse
// getAddressingParam returns the address of the param if it references an address.
func getAddressingParam(param any) (uint16, bool) {
switch val := param.(type) {
case Absolute:
case m6502.Absolute:
return uint16(val), true
case AbsoluteX:
case m6502.AbsoluteX:
return uint16(val), true
case AbsoluteY:
case m6502.AbsoluteY:
return uint16(val), true
case Indirect:
case m6502.Indirect:
return uint16(val), true
case IndirectX:
case m6502.IndirectX:
return uint16(val), true
case IndirectY:
case m6502.IndirectY:
return uint16(val), true
case ZeroPage:
case m6502.ZeroPage:
return uint16(val), true
case ZeroPageX:
case m6502.ZeroPageX:
return uint16(val), true
case ZeroPageY:
case m6502.ZeroPageY:
return uint16(val), true
default:
return 0, false
Expand All @@ -315,10 +314,10 @@ func getAddressingParam(param any) (uint16, bool) {
// and forces variable usage.
func checkBranchingParam(address uint16, opcode m6502.Opcode) (bool, bool) {
switch {
case opcode.Instruction.Name == m6502.Jmp.Name && opcode.Addressing == IndirectAddressing:
case opcode.Instruction.Name == m6502.Jmp.Name && opcode.Addressing == m6502.IndirectAddressing:
return true, false
case opcode.Instruction.Name == m6502.Jmp.Name || opcode.Instruction.Name == m6502.Jsr.Name:
if opcode.Addressing == AbsoluteAddressing && address < nes.CodeBaseAddress {
if opcode.Addressing == m6502.AbsoluteAddressing && address < nes.CodeBaseAddress {
return true, true
}
}
Expand Down
13 changes: 6 additions & 7 deletions internal/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"sort"

"github.com/retroenv/nesgodisasm/internal/program"
. "github.com/retroenv/retrogolib/addressing"
"github.com/retroenv/retrogolib/arch/cpu/m6502"
"github.com/retroenv/retrogolib/arch/nes"
"github.com/retroenv/retrogolib/arch/nes/parameter"
Expand Down Expand Up @@ -70,9 +69,9 @@ func (dis *Disasm) addVariableReference(addressReference, usageAddress uint16,
}

switch opcode.Addressing {
case ZeroPageXAddressing, ZeroPageYAddressing,
AbsoluteXAddressing, AbsoluteYAddressing,
IndirectXAddressing, IndirectYAddressing:
case m6502.ZeroPageXAddressing, m6502.ZeroPageYAddressing,
m6502.AbsoluteXAddressing, m6502.AbsoluteYAddressing,
m6502.IndirectXAddressing, m6502.IndirectYAddressing:
varInfo.indexedUsage = true
}
}
Expand Down Expand Up @@ -120,11 +119,11 @@ func (dis *Disasm) processVariables() error {
}

switch offsetInfo.opcode.Addressing {
case ZeroPageAddressing, ZeroPageXAddressing, ZeroPageYAddressing:
case m6502.ZeroPageAddressing, m6502.ZeroPageXAddressing, m6502.ZeroPageYAddressing:
offsetInfo.Code = fmt.Sprintf("%s %s", offsetInfo.opcode.Instruction.Name, converted)
case AbsoluteAddressing, AbsoluteXAddressing, AbsoluteYAddressing:
case m6502.AbsoluteAddressing, m6502.AbsoluteXAddressing, m6502.AbsoluteYAddressing:
offsetInfo.Code = fmt.Sprintf("%s %s", offsetInfo.opcode.Instruction.Name, converted)
case IndirectAddressing, IndirectXAddressing, IndirectYAddressing:
case m6502.IndirectAddressing, m6502.IndirectXAddressing, m6502.IndirectYAddressing:
offsetInfo.Code = fmt.Sprintf("%s %s", offsetInfo.opcode.Instruction.Name, converted)
}
}
Expand Down

0 comments on commit 0923e9f

Please sign in to comment.