Skip to content

Commit

Permalink
improve vectors start address calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelk committed Jun 1, 2024
1 parent 8d9a04b commit 6172911
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
4 changes: 1 addition & 3 deletions internal/assembler/asm6/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ func (f FileWriter) writeVectors() error {
return nil
}

vectorStart := int(f.app.CodeBaseAddress) + f.app.PrgSize() - 6
addr := fmt.Sprintf("$%04X", vectorStart)

addr := fmt.Sprintf("$%04X", f.app.VectorsStartAddress)
if err := f.writeSegment(addr); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/assembler/nesasm/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ func (f FileWriter) writeVectors() error {
return nil
}

vectorStart := int(f.app.CodeBaseAddress) + f.app.PrgSize() - 6
if _, err := fmt.Fprintf(f.mainWriter, "\n .org $%04X\n", vectorStart); err != nil {
if _, err := fmt.Fprintf(f.mainWriter, "\n .org $%04X\n", f.app.VectorsStartAddress); err != nil {
return fmt.Errorf("writing segment: %w", err)
}

Expand Down
7 changes: 6 additions & 1 deletion internal/disasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type Disasm struct {
handlers program.Handlers
noUnofficialInstruction bool // enable for assemblers that do not support unofficial opcodes

codeBaseAddress uint16 // codebase address of the cartridge, as it can be different from 0x8000
codeBaseAddress uint16 // codebase address of the cartridge, as it can be different from 0x8000
vectorsStartAddress uint16

constants map[uint16]constTranslation
usedConstants map[uint16]constTranslation
Expand Down Expand Up @@ -250,12 +251,15 @@ func (dis *Disasm) initializeIrqHandlers() {
func (dis *Disasm) calculateCodeBaseAddress(resetHandler uint16) {
halfPrg := len(dis.cart.PRG) % 0x8000
dis.codeBaseAddress = uint16(0x8000 + halfPrg)
dis.vectorsStartAddress = uint16(irqStartAddress)

// fix up calculated code base address for half sized PRG ROMs that have a different
// code base address configured in the assembler, like "M.U.S.C.L.E."
if resetHandler < dis.codeBaseAddress {
dis.codeBaseAddress = nes.CodeBaseAddress
dis.vectorsStartAddress -= uint16(halfPrg)
}

dis.logger.Debug("Code base address",
log.String("address", fmt.Sprintf("0x%04X", dis.codeBaseAddress)))
}
Expand All @@ -270,6 +274,7 @@ func (dis *Disasm) CodeBaseAddress() uint16 {
func (dis *Disasm) convertToProgram() (*program.Program, error) {
app := program.New(dis.cart)
app.CodeBaseAddress = dis.codeBaseAddress
app.VectorsStartAddress = dis.vectorsStartAddress
app.Handlers = dis.handlers

for _, bnk := range dis.banks {
Expand Down
16 changes: 9 additions & 7 deletions internal/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ type Program struct {
RAM byte // PRG-RAM offsets
Trainer []byte

CodeBaseAddress uint16
Checksums Checksums
Handlers Handlers
Battery byte
Mirror cartridge.MirrorMode
Mapper byte
VideoFormat byte
CodeBaseAddress uint16
VectorsStartAddress uint16

Checksums Checksums
Handlers Handlers
Battery byte
Mirror cartridge.MirrorMode
Mapper byte
VideoFormat byte

// keep constants and variables in the banks and global in the app to let the chosen assembler decide
// how to output them
Expand Down

0 comments on commit 6172911

Please sign in to comment.