Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asm6: multi bank support #78

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 49 additions & 24 deletions internal/assembler/asm6/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ type headerByteWrite struct {
comment string
}

type segmentWrite struct {
address string
}

type prgBankWrite struct {
bank *program.PRGBank
address string
bank *program.PRGBank
lastBank bool
}

type customWrite func() error
Expand Down Expand Up @@ -77,18 +75,21 @@ func (f FileWriter) Write() error {
headerByteWrite{value: f.app.RAM, comment: "Number of 8KB PRG-RAM banks"},
headerByteWrite{value: f.app.VideoFormat, comment: "Video format NTSC/PAL"},
lineWrite{line: ".dsb 6", comment: "Padding to fill 16 BYTE iNES Header"},
segmentWrite{address: fmt.Sprintf("$%04x", f.app.CodeBaseAddress)},
}
}

for _, bank := range f.app.PRG {
for i, bank := range f.app.PRG {
lastBank := i == len(f.app.PRG)-1
writes = append(writes,
prgBankWrite{bank: bank},
prgBankWrite{
address: fmt.Sprintf("$%04x", f.app.CodeBaseAddress),
bank: bank,
lastBank: lastBank,
},
)
}

writes = append(writes,
customWrite(f.writeVectors),
customWrite(f.writeCHR),
)

Expand All @@ -99,11 +100,6 @@ func (f FileWriter) Write() error {
return fmt.Errorf("writing header: %w", err)
}

case segmentWrite:
if err := f.writeSegment(t.address); err != nil {
return err
}

case lineWrite:
if _, err := fmt.Fprintf(f.mainWriter, "%-30s ; %s\n", t.line, t.comment); err != nil {
return fmt.Errorf("writing line: %w", err)
Expand All @@ -115,13 +111,7 @@ func (f FileWriter) Write() error {
}

case prgBankWrite:
if err := f.writeConstants(t.bank); err != nil {
return err
}
if err := f.writeVariables(t.bank); err != nil {
return err
}
if err := f.writeCode(t.bank); err != nil {
if err := f.writeBank(t); err != nil {
return err
}
}
Expand All @@ -130,9 +120,38 @@ func (f FileWriter) Write() error {
return nil
}

func (f FileWriter) writeBank(w prgBankWrite) error {
if err := f.writeSegment(w.address); err != nil {
return err
}
if err := f.writeConstants(w.bank); err != nil {
return err
}
if err := f.writeVariables(w.bank); err != nil {
return err
}
if err := f.writeCode(w.bank); err != nil {
return err
}

if w.lastBank {
if err := f.writeVectors(f.app.Handlers.NMI, f.app.Handlers.Reset, f.app.Handlers.IRQ); err != nil {
return err
}
} else {
nmi := fmt.Sprintf("$%04X", w.bank.Vectors[0])
reset := fmt.Sprintf("$%04X", w.bank.Vectors[1])
irq := fmt.Sprintf("$%04X", w.bank.Vectors[2])
if err := f.writeVectors(nmi, reset, irq); err != nil {
return err
}
}
return nil
}

// writeSegment writes a segment header to the output.
func (f FileWriter) writeSegment(address string) error {
_, err := fmt.Fprintf(f.mainWriter, "\n.org %s\n\n", address)
_, err := fmt.Fprintf(f.mainWriter, "\n.base %s\n\n", address)
if err != nil {
return fmt.Errorf("writing segment: %w", err)
}
Expand Down Expand Up @@ -180,16 +199,22 @@ func (f FileWriter) writeCHR() error {
}

// writeVectors writes the IRQ vectors.
func (f FileWriter) writeVectors() error {
func (f FileWriter) writeVectors(nmi, reset, irq string) error {
if f.options.CodeOnly {
return nil
}

addr := fmt.Sprintf("$%04X", f.app.VectorsStartAddress)

_, err := fmt.Fprintf(f.mainWriter, "\n.pad %s\n", addr)
if err != nil {
return fmt.Errorf("writing padding: %w", err)
}

if err := f.writeSegment(addr); err != nil {
return err
}
if _, err := fmt.Fprintf(f.mainWriter, vectors, f.app.Handlers.NMI, f.app.Handlers.Reset, f.app.Handlers.IRQ); err != nil {
if _, err := fmt.Fprintf(f.mainWriter, vectors, nmi, reset, irq); err != nil {
return fmt.Errorf("writing vectors: %w", err)
}
return nil
Expand Down
14 changes: 14 additions & 0 deletions internal/disasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ func (dis *Disasm) convertToProgram() (*program.Program, error) {
prgBank.Variables[varInfo.name] = address
}

setBankVectors(bnk, prgBank)

app.PRG = append(app.PRG, prgBank)
}

Expand Down Expand Up @@ -353,6 +355,18 @@ func (dis *Disasm) loadCodeDataLog() error {
return nil
}

func setBankVectors(bnk *bank, prgBank *program.PRGBank) {
idx := len(bnk.prg) - 6
for i := range 3 {
b1 := bnk.prg[idx]
idx++
b2 := bnk.prg[idx]
idx++
addr := uint16(b2)<<8 | uint16(b1)
prgBank.Vectors[i] = addr
}
}

func getProgramOffset(address uint16, offsetInfo offset, options *options.Disassembler) (program.Offset, error) {
programOffset := offsetInfo.Offset
programOffset.Address = address
Expand Down
4 changes: 3 additions & 1 deletion internal/program/prg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func NewPRGBank(size int) *PRGBank {

// PRGBank defines a PRG bank.
type PRGBank struct {
PRG []Offset // PRG-ROM banks
PRG []Offset
Vectors [3]uint16

Constants map[string]uint16
Variables map[string]uint16
}
Expand Down
Loading