apple2-go/utils/utils.go

81 lines
2.0 KiB
Go
Raw Normal View History

2018-04-29 19:41:11 +00:00
package utils
import (
"compress/gzip"
2018-05-10 12:32:42 +00:00
"encoding/hex"
2018-05-17 12:13:10 +00:00
"fmt"
2018-04-29 19:41:11 +00:00
"io/ioutil"
"os"
2018-05-17 12:13:10 +00:00
"testing"
2018-05-27 10:05:00 +00:00
2019-11-02 13:33:05 +00:00
"github.com/freewilll/apple2-go/cpu"
"github.com/freewilll/apple2-go/system"
2018-04-29 19:41:11 +00:00
)
2018-05-28 09:54:36 +00:00
// ReadMemoryFromGzipFile just reads and uncompresses a gzip file
func ReadMemoryFromGzipFile(filename string) (data []byte, err error) {
2018-04-29 19:41:11 +00:00
f, err := os.Open(filename)
if err != nil {
return nil, err
}
reader, err := gzip.NewReader(f)
if err != nil {
return nil, err
}
defer reader.Close()
data, err = ioutil.ReadAll(reader)
return
}
2018-05-10 12:32:42 +00:00
2018-05-28 09:54:36 +00:00
// DecodeCmdLineAddress decodes a 4 byte string hex address
2018-05-10 12:32:42 +00:00
func DecodeCmdLineAddress(s *string) (result *uint16) {
if *s != "" {
breakAddressValue, err := hex.DecodeString(*s)
if err != nil {
panic(err)
}
var value uint16
if len(breakAddressValue) == 1 {
value = uint16(breakAddressValue[0])
} else if len(breakAddressValue) == 2 {
value = uint16(breakAddressValue[0])*uint16(0x100) + uint16(breakAddressValue[1])
} else {
panic("Invalid break address")
}
result = &value
}
return result
}
2018-05-17 12:13:10 +00:00
2018-05-28 09:54:36 +00:00
// RunUntilBreakPoint runs the CPU until it either hits a breakpoint or a time
// has expired. An assertion is done at the end to ensure the breakpoint has
// been reached.
2018-05-17 12:13:10 +00:00
func RunUntilBreakPoint(t *testing.T, breakAddress uint16, seconds int, showInstructions bool, message string) {
fmt.Printf("Running until %#04x: %s \n", breakAddress, message)
system.LastAudioCycles = 0
2018-05-17 12:13:10 +00:00
exitAtBreak := false
disableFirmwareWait := false
2018-05-28 16:31:52 +00:00
cpu.Run(showInstructions, &breakAddress, exitAtBreak, disableFirmwareWait, uint64(system.CPUFrequency*seconds))
2018-05-17 12:13:10 +00:00
if cpu.State.PC != breakAddress {
t.Fatalf("Did not reach breakpoint at %04x. Got to %04x", breakAddress, cpu.State.PC)
}
}
2018-05-19 10:42:14 +00:00
2018-05-28 09:54:36 +00:00
// Disassemble disassembles and prints the code in memory between start and end
2018-05-19 10:42:14 +00:00
func Disassemble(start uint16, end uint16) {
oldPC := cpu.State.PC
cpu.State.PC = start
for cpu.State.PC <= end {
cpu.PrintInstruction(false)
cpu.AdvanceInstruction()
}
cpu.State.PC = oldPC
}