Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skew in timing #1

Open
npatrick04 opened this issue Dec 21, 2016 · 1 comment
Open

Skew in timing #1

npatrick04 opened this issue Dec 21, 2016 · 1 comment

Comments

@npatrick04
Copy link

Hey Steve,

I was planning on commenting on your blog...but since there is no way to do that...and you linked to your github account, I figured I'd put this here.

The sleeps you're doing in run-cpu, emulate-cycle, run-sound, and run-timers all will result in skew that varies from cycle to cycle. It'd be better to use a constant frequency tick for those things. I actually have a library that could be used (though it's probably more complicated than what this needs).

Using https://github.com/npatrick04/timer-wheel,

(defconstant +cycles-per-second+ 500)
(defconstant +cycles-before-sleep+ 10)
(defconstant +cycles-period+ (/ +cycles-before-sleep+ +cycles-per-second+))

(defparameter cycle-lock (bt:make-lock))
(defparameter cycle-cv (bt:make-condition-variable))

(defun timeout (wheel timer)
  (bt:with-lock-held (cycle-lock)
    (bt:condition-notify cycle-cv))
  (tw:schedule-timer wheel timer :seconds +cycles-period+))

(defun wait ()
  (bt:with-lock-held (cycle-lock)
    (bt:condition-wait cycle-cv cycle-lock)))

(defun run-cpu (chip)
  (let ((wheel (tw:make-wheel))
	(timer (tw:make-timer #'timeout)))
    (tw:with-timer-wheel wheel
      (tw:schedule-timer wheel timer :seconds +cycles-period+)
      (iterate
	  (while (chip-running chip))
	  (emulate-cycle chip)
	(for tick :every-nth +cycles-before-sleep+ :do (wait))))))
@npatrick04
Copy link
Author

Or shorter and more understandable using https://github.com/npatrick04/rate-monotonic...

(defconstant +cycles-per-second+ 500)
(defconstant +cycles-before-sleep+ 10)
(defconstant +cycles-period+ (/ +cycles-before-sleep+ +cycles-per-second+))

(defun run-cpu (chip)
  (let ((cycle-period (rm:make-timer-period)))
    (rm:with-timer-period (+cycles-period+)
      ;; Kick the first period off
      (rm:period cycle-period :seconds +cycles-period+)
      (iterate
	  (while (chip-running chip))
	  (emulate-cycle chip)
	(for tick :every-nth +cycles-before-sleep+
	     :do (rm:period cycle-period :seconds +cycles-period+))))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant