Skip to content

Commit

Permalink
simplified RunForFrameCount()
Browse files Browse the repository at this point in the history
  • Loading branch information
JetSetIlly committed Sep 14, 2024
1 parent 66fde04 commit 08b1a94
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
32 changes: 18 additions & 14 deletions hardware/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,36 @@ func (vcs *VCS) Run(continueCheck func() (govern.State, error)) error {
// RunForFrameCount sets emulator running for the specified number of frames.
// Useful for FPS and regression tests. Not used by the debugger because traps
// (and volatile traps) are more flexible.
func (vcs *VCS) RunForFrameCount(numFrames int, continueCheck func(frame int) (govern.State, error)) error {
//
// continueCheck can be used to instruct the emulation to end before the
// specified number of frames has elapsed.
func (vcs *VCS) RunForFrameCount(numFrames int, continueCheck func() (govern.State, error)) error {
if continueCheck == nil {
continueCheck = func(frame int) (govern.State, error) { return govern.Running, nil }
continueCheck = func() (govern.State, error) { return govern.Running, nil }
}

frameNum := vcs.TV.GetCoords().Frame
targetFrame := frameNum + numFrames
targetFrame := vcs.TV.GetCoords().Frame + numFrames

state := govern.Running
for frameNum != targetFrame && state != govern.Ending {
for state != govern.Ending {
// check if CPU has been killed. emulation will run forever if we don't
// check for this
if vcs.CPU.Killed {
return nil
}

err := vcs.Step(nil)
if err != nil {
return err
}

frameNum = vcs.TV.GetCoords().Frame
if vcs.TV.IsFrameNum(targetFrame) {
state = govern.Ending
} else {
err := vcs.Step(nil)
if err != nil {
return err
}

state, err = continueCheck(frameNum)
if err != nil {
return err
state, err = continueCheck()
if err != nil {
return err
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions hardware/television/television.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,10 @@ func (tv *Television) GetCoords() coords.TelevisionCoords {
return tv.state.GetCoords()
}

func (tv *Television) IsFrameNum(frame int) bool {
return tv.state.frameNum == frame
}

// SetRotation instructs the television to a different orientation. In truth,
// the television just forwards the request to the pixel renderers.
func (tv *Television) SetRotation(rotation specification.Rotation) {
Expand Down
2 changes: 1 addition & 1 deletion preview/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (em *Emulation) RunN(loader cartridgeloader.Loader, N int) error {
return fmt.Errorf("preview: %w", err)
}

err = em.vcs.RunForFrameCount(N, func(_ int) (govern.State, error) {
err = em.vcs.RunForFrameCount(N, func() (govern.State, error) {
select {
case <-timeout:
return govern.Ending, nil
Expand Down
3 changes: 2 additions & 1 deletion regression/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (reg *LogRegression) regress(newRegression bool, output io.Writer, msg stri
logOutput := &strings.Builder{}

// run emulation
err = vcs.RunForFrameCount(reg.NumFrames, func(frame int) (govern.State, error) {
err = vcs.RunForFrameCount(reg.NumFrames, func() (govern.State, error) {
// if the CPU is in the KIL state then the test will never end normally
if vcs.CPU.Killed {
return govern.Ending, fmt.Errorf("CPU in KIL state")
Expand All @@ -174,6 +174,7 @@ func (reg *LogRegression) regress(newRegression bool, output io.Writer, msg stri
// display progress meter every 1 second
select {
case <-tck.C:
frame := vcs.TV.GetCoords().Frame
output.Write([]byte(fmt.Sprintf("\r%s [%d/%d (%.1f%%)]", msg, frame, reg.NumFrames, 100*(float64(frame)/float64(reg.NumFrames)))))
default:
}
Expand Down
3 changes: 2 additions & 1 deletion regression/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (reg *VideoRegression) regress(newRegression bool, output io.Writer, msg st
tck := time.NewTicker(dur)

// run emulation
err = vcs.RunForFrameCount(reg.NumFrames, func(frame int) (govern.State, error) {
err = vcs.RunForFrameCount(reg.NumFrames, func() (govern.State, error) {
// if the CPU is in the KIL state then the test will never end normally
if vcs.CPU.Killed {
return govern.Ending, fmt.Errorf("CPU in KIL state")
Expand All @@ -248,6 +248,7 @@ func (reg *VideoRegression) regress(newRegression bool, output io.Writer, msg st
// display progress meter every 1 second
select {
case <-tck.C:
frame := vcs.TV.GetCoords().Frame
output.Write([]byte(fmt.Sprintf("\r%s [%d/%d (%.1f%%)]", msg, frame, reg.NumFrames, 100*(float64(frame)/float64(reg.NumFrames)))))
default:
}
Expand Down

0 comments on commit 08b1a94

Please sign in to comment.