From 68b949d871aa15bc11da33d953557ca3bb9a39c2 Mon Sep 17 00:00:00 2001 From: Ivan Izaguirre Date: Tue, 24 Sep 2019 23:32:03 +0200 Subject: [PATCH] Added the -traceCpu and -traceSS command line switches. --- README.md | 29 +++++++++++++---------- apple2.go | 5 ++-- apple2Setup.go | 3 +-- apple2console/main.go | 2 +- apple2main.go | 20 ++++++++++++++-- core6502/6502functional_test.go | 3 ++- core6502/execute.go | 13 +++++++--- core6502/nmos6502.go | 5 ++++ ioC0Page.go | 42 ++++++++++++++++++++++----------- 9 files changed, 84 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index de8dbab..02d955a 100644 --- a/README.md +++ b/README.md @@ -99,29 +99,34 @@ Only valid on SDL mode -base64a setup a Base64A clone -charRom string - rom file for the character generator (default "/Apple2rev7CharGen.rom") + rom file for the character generator (default "/Apple2rev7CharGen.rom") -disk string - file to load on the first disk drive (default "/dos33.dsk") + file to load on the first disk drive (default "/dos33.dsk") -disk2Slot int - slot for the disk driver. -1 for none. (default 6) + slot for the disk driver. -1 for none. (default 6) -diskRom string - rom file for the disk drive controller (default "/DISK2.rom") + rom file for the disk drive controller (default "/DISK2.rom") -dumpChars - shows the character map + shows the character map -fastDisk - set fast mode when the disks are spinning (default true) + set fast mode when the disks are spinning (default true) -languageCardSlot int - slot for the 16kb language card. -1 for none + slot for the 16kb language card. -1 for none -mhz float - cpu speed in Mhz, use 0 for full speed. Use F5 to toggle. (default 1.0227142857142857) + cpu speed in Mhz, use 0 for full speed. Use F5 to toggle. (default 1.0227142857142857) -mono - emulate a green phosphor monitor instead of a NTSC color TV. Use F6 to toggle. + emulate a green phosphor monitor instead of a NTSC color TV. Use F6 to toggle. -panicss - panic if a not implemented softswitch is used + panic if a not implemented softswitch is used -rom string - main rom file (default "/Apple2_Plus.rom") + main rom file (default "/Apple2_Plus.rom") -saturnCardSlot int - slot for the 256kb Saturn card. -1 for none (default -1) + slot for the 256kb Saturn card. -1 for none (default -1) + -traceCpu + dump to the console the CPU execution + -traceSS + dump to the console the sofswitches calls + ``` ## Building from source diff --git a/apple2.go b/apple2.go index 710e6d0..4cd96ec 100644 --- a/apple2.go +++ b/apple2.go @@ -20,7 +20,6 @@ type Apple2 struct { cg *CharacterGenerator cards [8]card isApple2e bool - panicSS bool commandChannel chan int cycleDurationNs float64 // Current speed. Inverse of the cpu clock in Ghz isColor bool @@ -37,14 +36,14 @@ const ( const maxWaitDuration = 100 * time.Millisecond // Run starts the Apple2 emulation -func (a *Apple2) Run(log bool) { +func (a *Apple2) Run() { // Start the processor a.cpu.Reset() referenceTime := time.Now() for { // Run a 6502 step - a.cpu.ExecuteInstruction(log && a.cycleDurationNs != 0) + a.cpu.ExecuteInstruction() // Execute meta commands commandsPending := true diff --git a/apple2Setup.go b/apple2Setup.go index 5b255d2..832506e 100644 --- a/apple2Setup.go +++ b/apple2Setup.go @@ -4,7 +4,7 @@ import "github.com/ivanizag/apple2/core6502" // NewApple2 instantiates an apple2 func NewApple2(charRomFile string, clockMhz float64, - isColor bool, fastMode bool, panicSS bool) *Apple2 { + isColor bool, fastMode bool) *Apple2 { var a Apple2 a.Name = "Apple ][+" @@ -16,7 +16,6 @@ func NewApple2(charRomFile string, clockMhz float64, a.commandChannel = make(chan int, 100) a.isColor = isColor a.fastMode = fastMode - a.panicSS = panicSS if clockMhz <= 0 { // Full speed diff --git a/apple2console/main.go b/apple2console/main.go index 6f05417..4ce1dd8 100644 --- a/apple2console/main.go +++ b/apple2console/main.go @@ -16,7 +16,7 @@ func main() { a.SetKeyboardProvider(fe) go fe.textModeGoRoutine(a) - a.Run(false) + a.Run() } /* diff --git a/apple2main.go b/apple2main.go index 9a10f1d..6683353 100644 --- a/apple2main.go +++ b/apple2main.go @@ -53,7 +53,18 @@ func MainApple() *Apple2 { panicSS := flag.Bool( "panicss", false, - "panic if a not implemented softswitch is used") + "panic if a not implemented softswitch is used", + ) + traceCPU := flag.Bool( + "traceCpu", + false, + "dump to the console the CPU execution operations", + ) + traceSS := flag.Bool( + "traceSS", + false, + "dump to the console the sofswitches calls", + ) dumpChars := flag.Bool( "dumpChars", false, @@ -66,7 +77,12 @@ func MainApple() *Apple2 { ) flag.Parse() - a := NewApple2(*charRomFile, *cpuClock, !*mono, *fastDisk, *panicSS) + a := NewApple2(*charRomFile, *cpuClock, !*mono, *fastDisk) + + a.cpu.SetTrace(*traceCPU) + a.io.setTrace(*traceSS) + a.io.setPanicNotImplemented(*panicSS) + if *base64a { NewBase64a(a) } else { diff --git a/core6502/6502functional_test.go b/core6502/6502functional_test.go index 098b1ec..9224880 100644 --- a/core6502/6502functional_test.go +++ b/core6502/6502functional_test.go @@ -22,8 +22,9 @@ func TestFunctional(t *testing.T) { if log { fmt.Printf("[ %d ] ", testCase) } + s.SetTrace(log) pc := s.reg.getPC() - s.ExecuteInstruction(log) + s.ExecuteInstruction() if pc == s.reg.getPC() { t.Errorf("Failure in test %v.", testCase) } diff --git a/core6502/execute.go b/core6502/execute.go index a56b245..d3c6622 100644 --- a/core6502/execute.go +++ b/core6502/execute.go @@ -17,6 +17,7 @@ type State struct { mem Memory cycles uint64 opcodes *[256]opcode + trace bool } const ( @@ -43,8 +44,13 @@ func (s *State) executeLine(line []uint8) { opcode.action(s, line, opcode) } +// SetTrace activates tracing of the cpu execution +func (s *State) SetTrace(trace bool) { + s.trace = trace +} + // ExecuteInstruction transforms the state given after a single instruction is executed. -func (s *State) ExecuteInstruction(log bool) { +func (s *State) ExecuteInstruction() { pc := s.reg.getPC() opcodeID := s.mem.Peek(pc) opcode := s.opcodes[opcodeID] @@ -60,12 +66,13 @@ func (s *State) ExecuteInstruction(log bool) { } s.reg.setPC(pc) - if log { + if s.trace { + //fmt.Printf("%#04x %#02x\n", pc-opcode.bytes, opcodeID) fmt.Printf("%#04x %-12s: ", pc-opcode.bytes, lineString(line, opcode)) } opcode.action(s, line, opcode) s.cycles += uint64(opcode.cycles) - if log { + if s.trace { fmt.Printf("%v, [%02x]\n", s.reg, line) } } diff --git a/core6502/nmos6502.go b/core6502/nmos6502.go index 6551ea6..33979f8 100644 --- a/core6502/nmos6502.go +++ b/core6502/nmos6502.go @@ -181,4 +181,9 @@ var opcodesNMOS6502 = [256]opcode{ 0x70: opcode{"BVS", 2, 2, modeRelative, buildOpBranch(flagV, true)}, // Extra cycles 0xEA: opcode{"NOP", 1, 2, modeImplicit, opNOP}, + + // Undocumented opcodes, see http://bbc.nvg.org/doc/6502OpList.txt + 0x1A: opcode{"NOP", 1, 2, modeImplicit, opNOP}, // INC A in the 65c02 + 0xC2: opcode{"NOP", 1, 2, modeImplicit, opNOP}, // Should be HALT? + 0x02: opcode{"NOP", 1, 2, modeImplicit, opNOP}, // Should be HALT? } diff --git a/ioC0Page.go b/ioC0Page.go index 3c097f2..4168aa3 100644 --- a/ioC0Page.go +++ b/ioC0Page.go @@ -7,14 +7,16 @@ import ( ) type ioC0Page struct { - softSwitchesR [256]softSwitchR - softSwitchesW [256]softSwitchW - softSwitchesData [128]uint8 - keyboard KeyboardProvider - speaker SpeakerProvider - paddlesStrobeCycle uint64 - joysticks JoysticksProvider - apple2 *Apple2 + softSwitchesR [256]softSwitchR + softSwitchesW [256]softSwitchW + softSwitchesData [128]uint8 + keyboard KeyboardProvider + speaker SpeakerProvider + paddlesStrobeCycle uint64 + joysticks JoysticksProvider + apple2 *Apple2 + trace bool + panicNotImplemented bool } type softSwitchR func(io *ioC0Page) uint8 @@ -57,6 +59,14 @@ func newIoC0Page(a *Apple2) *ioC0Page { return &io } +func (p *ioC0Page) setTrace(trace bool) { + p.trace = trace +} + +func (p *ioC0Page) setPanicNotImplemented(value bool) { + p.panicNotImplemented = value +} + func (p *ioC0Page) save(w io.Writer) { binary.Write(w, binary.BigEndian, p.softSwitchesData) } @@ -106,23 +116,27 @@ func (p *ioC0Page) peek(address uint16) uint8 { pageAddress := uint8(address) ss := p.softSwitchesR[pageAddress] if ss == nil { - if p.apple2.panicSS { - panic(fmt.Sprintf("Unknown softswitch on read to 0xC0%02x", pageAddress)) + if p.panicNotImplemented { + panic(fmt.Sprintf("Unknown softswitch on read to $%04x", address)) } return 0 } value := ss(p) - //fmt.Printf("Peek on $%02x: $%02x\n", address, value) + if p.trace { + fmt.Printf("Softswitch peek on $%04x: $%02x\n", address, value) + } return value } func (p *ioC0Page) poke(address uint16, value uint8) { - //fmt.Printf("Poke on $%02x with %02x\n", address, value) + if p.trace { + fmt.Printf("Softswtich poke on $%04x with %02x\n", address, value) + } pageAddress := uint8(address) ss := p.softSwitchesW[pageAddress] if ss == nil { - if p.apple2.panicSS { - panic(fmt.Sprintf("Unknown softswitch on write to 0xC0%02x", pageAddress)) + if p.panicNotImplemented { + panic(fmt.Sprintf("Unknown softswitch on write to $%04x", address)) } return }