Skip to content

Commit

Permalink
add support for bank switches in data sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelk committed Aug 24, 2023
1 parent 4493d6b commit b72bc53
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
"github.com/retroenv/retrogolib/arch/nes/cartridge"
)

// WriteCallbackFunc is a custom callback function that gets called before the offset is written.
type WriteCallbackFunc func(writer io.Writer) error

// Offset defines the content of an offset in a program that can represent data or code.
type Offset struct {
// data byte or all opcode bytes that are part of the instruction
OpcodeBytes []byte
// custom callback function that gets called before the offset is written, this
// allows custom bank switch code to be written at specific offsets
WriteCallback func(writer io.Writer) error
// WriteCallback is a custom callback function that gets called before the offset is
// written, this allows custom bank switch code to be written at specific offsets.
// The output of bundled data bytes will be interrupted.
WriteCallback WriteCallbackFunc

Type OffsetType

Expand Down
7 changes: 7 additions & 0 deletions internal/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func getPrgData(bank *program.PRGBank, startIndex, endIndex int) []byte {

for i := startIndex; i < endIndex; i++ {
offset := bank.PRG[i]

// opcode bytes can be nil if data bytes have been combined for an unofficial nop
if !offset.IsType(program.DataOffset) || len(offset.OpcodeBytes) == 0 {
break
Expand All @@ -281,6 +282,12 @@ func getPrgData(bank *program.PRGBank, startIndex, endIndex int) []byte {
if i > startIndex && (offset.IsType(program.CodeOffset|program.CodeAsData) || offset.Label != "") {
break
}
// break at potential bank switch, do ignore callback on first iteration as it has been handled
// by the caller already.
if offset.WriteCallback != nil && i != startIndex {
break
}

data = append(data, offset.OpcodeBytes...)
}

Expand Down

0 comments on commit b72bc53

Please sign in to comment.