2013-04-21 23:49:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-12-19 14:11:03 +00:00
|
|
|
"fmt"
|
2013-04-21 23:49:58 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/gonuts/commander"
|
|
|
|
"github.com/zellyn/go6502/asm"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdDisasm = &commander.Command{
|
|
|
|
Run: runDisasm,
|
|
|
|
UsageLine: "disasm [-a address] filename",
|
|
|
|
Short: "disassemble binary files",
|
|
|
|
Long: `
|
|
|
|
Disasm is a very simple disassembler for 6502 binary files.
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var disasmAddress uint // disasm -a flag
|
2014-08-06 05:17:11 +00:00
|
|
|
var symbolFile string // disasm -s flag
|
|
|
|
var printLabels bool // disasm -p flag
|
2013-04-21 23:49:58 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdDisasm.Flag.UintVar(&disasmAddress, "a", 0, "The starting memory address.")
|
2014-08-06 05:17:11 +00:00
|
|
|
cmdDisasm.Flag.StringVar(&symbolFile, "s", "", "File of symbol definitions.")
|
|
|
|
cmdDisasm.Flag.BoolVar(&printLabels, "p", false, "Print labels for symbols.")
|
2013-04-21 23:49:58 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 14:11:03 +00:00
|
|
|
func runDisasm(cmd *commander.Command, args []string) error {
|
2013-04-21 23:49:58 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
cmd.Usage()
|
2013-12-19 14:11:03 +00:00
|
|
|
return nil
|
2013-04-21 23:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadFile(args[0])
|
|
|
|
if err != nil {
|
2014-08-06 05:17:11 +00:00
|
|
|
return err
|
2013-04-21 23:49:58 +00:00
|
|
|
}
|
|
|
|
if len(bytes) > 0x10000 {
|
2013-12-19 14:11:03 +00:00
|
|
|
return fmt.Errorf("File %s is %04X bytes long, which is more than $10000.", args[0], len(bytes))
|
2013-04-21 23:49:58 +00:00
|
|
|
}
|
|
|
|
if int(disasmAddress)+len(bytes) > 0x10000 {
|
2013-12-19 14:11:03 +00:00
|
|
|
return fmt.Errorf("Starting address ($%04X) + file length ($%04X) = $%X, which is > $10000",
|
2013-04-21 23:49:58 +00:00
|
|
|
disasmAddress, len(bytes), int(disasmAddress)+len(bytes))
|
|
|
|
}
|
|
|
|
|
2014-08-06 05:17:11 +00:00
|
|
|
var s asm.Symbols
|
|
|
|
if symbolFile != "" {
|
|
|
|
s, err = asm.ReadSymbols(symbolFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if printLabels {
|
|
|
|
return fmt.Errorf("-p (print labels) specified without -s (symbol table file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
asm.DisasmBlock(bytes, uint16(disasmAddress), os.Stdout, s, 2, printLabels)
|
2013-12-19 14:11:03 +00:00
|
|
|
return nil
|
2013-04-21 23:49:58 +00:00
|
|
|
}
|