diff --git a/apple2.go b/apple2.go index f4c6e40..b198e0d 100644 --- a/apple2.go +++ b/apple2.go @@ -27,11 +27,12 @@ type Apple2 struct { fastMode bool fastRequestsCounter int profile bool + showSpeed bool } const ( - // CpuClockMhz is the actual Apple II clock speed - CpuClockMhz = 14.318 / 14 + // CPUClockMhz is the actual Apple II clock speed + CPUClockMhz = 14.318 / 14 cpuClockEuroMhz = 14.238 / 14 ) @@ -48,6 +49,8 @@ func (a *Apple2) Run() { // Start the processor a.cpu.Reset() referenceTime := time.Now() + speedReferenceTime := referenceTime + speedReferenceCycles := uint64(0) for { // Run a 6502 step @@ -81,6 +84,17 @@ func (a *Apple2) Run() { time.Sleep(waitDuration) } } + + if a.showSpeed && a.cpu.GetCycles()-speedReferenceCycles > 1000000 { + // Calculate speed in MHz every million cycles + newTime := time.Now() + newCycles := a.cpu.GetCycles() + elapsedCycles := float64(newCycles - speedReferenceCycles) + freq := 1000.0 * elapsedCycles / float64(newTime.Sub(speedReferenceTime).Nanoseconds()) + fmt.Printf("Freq: %f Mhz\n", freq) + speedReferenceTime = newTime + speedReferenceCycles = newCycles + } } } @@ -91,6 +105,8 @@ func (a *Apple2) setProfile(value bool) { const ( // CommandToggleSpeed toggles cpu speed between full speed and actual Apple II speed CommandToggleSpeed = iota + 1 + // CommandShowSpeed toggles printinf the current freq in Mhz + CommandShowSpeed // CommandToggleColor toggles between NTSC color TV and Green phospor monitor CommandToggleColor // CommandSaveState stores the state to file @@ -117,11 +133,13 @@ func (a *Apple2) executeCommand(command int) { case CommandToggleSpeed: if a.cycleDurationNs == 0 { fmt.Println("Slow") - a.cycleDurationNs = 1000.0 / CpuClockMhz + a.cycleDurationNs = 1000.0 / CPUClockMhz } else { fmt.Println("Fast") a.cycleDurationNs = 0 } + case CommandShowSpeed: + a.showSpeed = !a.showSpeed case CommandToggleColor: a.isColor = !a.isColor case CommandSaveState: diff --git a/apple2Setup.go b/apple2Setup.go index d1f2a82..58be655 100644 --- a/apple2Setup.go +++ b/apple2Setup.go @@ -23,7 +23,6 @@ func newApple2e() *Apple2 { a.Name = "Apple IIe" a.mmu = newMemoryManager(&a) a.cpu = core6502.NewNMOS6502(a.mmu) - //a.cpu = core6502.NewCMOS65c02(a.mmu) a.io = newIoC0Page(&a) a.mmu.InitRAMalt() addApple2SoftSwitches(a.io) diff --git a/apple2main.go b/apple2main.go index 68c2526..35319fd 100644 --- a/apple2main.go +++ b/apple2main.go @@ -35,7 +35,7 @@ func MainApple() *Apple2 { "slot for the hard drive if present. -1 for none.") cpuClock := flag.Float64( "mhz", - CpuClockMhz, + CPUClockMhz, "cpu speed in Mhz, use 0 for full speed. Use F5 to toggle.") charRomFile := flag.String( "charRom", diff --git a/apple2sdl/sdlKeyboard.go b/apple2sdl/sdlKeyboard.go index 8102870..966bed1 100644 --- a/apple2sdl/sdlKeyboard.go +++ b/apple2sdl/sdlKeyboard.go @@ -93,7 +93,11 @@ func (k *sdlKeyboard) putKey(keyEvent *sdl.KeyboardEvent) { // Control of the emulator case sdl.K_F5: - k.a.SendCommand(apple2.CommandToggleSpeed) + if ctrl { + k.a.SendCommand(apple2.CommandShowSpeed) + } else { + k.a.SendCommand(apple2.CommandToggleSpeed) + } case sdl.K_F6: k.a.SendCommand(apple2.CommandToggleColor) case sdl.K_F7: diff --git a/apple2sdl/sdlSpeaker.go b/apple2sdl/sdlSpeaker.go index d26adcf..98e413a 100644 --- a/apple2sdl/sdlSpeaker.go +++ b/apple2sdl/sdlSpeaker.go @@ -18,7 +18,7 @@ const ( samplingHz = 48000 bufferSize = 10000 // bufferSize/samplingHz will be the max delay of the sound - sampleDurationCycles = 1000000 * apple2.CpuClockMhz / samplingHz + sampleDurationCycles = 1000000 * apple2.CPUClockMhz / samplingHz // each sample on the sound stream is 21.31 cpu cycles approx maxOutOfSyncMs = 2000 decayLevel = 128 @@ -83,7 +83,7 @@ func SpeakerCallback(userdata unsafe.Pointer, stream *C.Uint8, length C.int) { } // Verify that we are not too long behind - var maxOutOfSyncCyclesFloat = 1000 * apple2.CpuClockMhz * maxOutOfSyncMs + var maxOutOfSyncCyclesFloat = 1000 * apple2.CPUClockMhz * maxOutOfSyncMs var maxOutOfSyncCycles = uint64(maxOutOfSyncCyclesFloat) for _, pc := range s.pendingClicks { if pc-s.lastCycle > maxOutOfSyncCycles { diff --git a/softSwitches2e.go b/softSwitches2e.go index 0408037..c9c5e1d 100644 --- a/softSwitches2e.go +++ b/softSwitches2e.go @@ -40,8 +40,7 @@ func addApple2ESoftSwitches(io *ioC0Page) { }, "BSRREADRAM") // TOOD: - // AKD read on 0x10 - // VBL read on 0x19 + // VBL or VERTBLANK read on 0x19 //io.softSwitchesData[ioFlagAltChar] = ssOn // Not sure about this.