;;-*-Lisp-*- (in-package goal) ;; name: timer.gc ;; name in dgo: timer ;; dgos: ENGINE, GAME ;; DECOMP BEGINS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Timer (EE timers) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun timer-count ((arg0 timer-bank)) "Return a timer's counter value" (#when PC_PORT (when (= arg0 TIMER1_BANK) (return (- (get-bus-clock/256) *timer-reset-value*)) ) (format 0 "Unknown timer #x~X requested.~%" arg0) ) (.sync.l) (let ((v0-0 (-> arg0 count))) (.sync.l) v0-0 ) ) (defmacro timer1-time () `(timer-count TIMER1_BANK) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Interrupt Control ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; cop0 status register "interrupt enable" flag ;; if cop0 status is needed anywhere else, move this elsewhere (defconstant COP0_STATUS_IE (the-as uint #x1)) (defun disable-irq () "Disable all interrupts. Has no effect on PC Port" (rlet ((status :class gpr :type uint)) (let ((status-mask (lognot COP0_STATUS_IE))) (.mfc0 status Status) (logand! status status-mask) ;; should status-mask be replaced directly? (.mtc0 Status status) (.sync.p) ) ) ) (defun enable-irq () "Enable all interrupts. Has no effect on PC Port." (rlet ((status :class gpr :type uint)) (.mfc0 status Status) (logior! status COP0_STATUS_IE) (.mtc0 Status status) (.sync.p) ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Stopwatch (CPU clock cycle counting) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun stopwatch-init ((this stopwatch)) "Init a stopwatch" (set! (-> this begin-level) 0) (set! (-> this prev-time-elapsed) 0) ) (defun stopwatch-reset ((this stopwatch)) "Restart a stopwatch's times" (set! (-> this prev-time-elapsed) 0) (when (> (-> this begin-level) 0) (let ((count 0)) (.mfc0 count Count) (#when PC_PORT (set! count (the int (get-cpu-clock))) ) (set! (-> this start-time) count) ) ) ) (defun stopwatch-start ((this stopwatch)) "Start a stopwatch from scratch" (when (zero? (-> this begin-level)) (set! (-> this begin-level) 1) (let ((count 0)) (.mfc0 count Count) (#when PC_PORT (set! count (the int (get-cpu-clock))) ) (set! (-> this start-time) count) ) ) ) (defun stopwatch-stop ((this stopwatch)) "Fully stop a stopwatch and save its elapsed time" (when (> (-> this begin-level) 0) (set! (-> this begin-level) 0) (let ((count 0)) (let ((count 0)) (.mfc0 count Count) ;; wrong register? a typo in a rlet? who knows. (#when PC_PORT (set! count (the int (get-cpu-clock))) ) (+! (-> this prev-time-elapsed) (- count (-> this start-time))) ) ) ) (none) ) (defun stopwatch-begin ((this stopwatch)) "Begin a stopwatch level, and starts it if it hasn't yet" (when (zero? (-> this begin-level)) (let ((count 0)) (.mfc0 count Count) (#when PC_PORT (set! count (the int (get-cpu-clock))) ) (set! (-> this start-time) count) ) ) (+! (-> this begin-level) 1) ) (defun stopwatch-end ((this stopwatch)) "End a stopwatch level. Stops the stopwatch if it's back to level zero. There is no guard against ending a stopwatch too many times, and a negative level will cause errors!" (+! (-> this begin-level) -1) (when (zero? (-> this begin-level)) (set! (-> this begin-level) 0) (let ((count 0)) (.mfc0 count Count) (#when PC_PORT (set! count (the int (get-cpu-clock))) ) (+! (-> this prev-time-elapsed) (- count (-> this start-time))) ) ) (none) ) (defun stopwatch-elapsed-ticks ((this stopwatch)) "Returns the elapsed time so far (in clock cycles) of a stopwatch" (let ((elapsed (-> this prev-time-elapsed))) (when (> (-> this begin-level) 0) (let ((count 0)) (.mfc0 count Count) (#when PC_PORT (set! count (the int (get-cpu-clock))) ) (+! elapsed (- count (-> this start-time))) (set! count elapsed) ;; ?? ) ) elapsed ) ) (defglobalconstant EE_SECONDS_PER_TICK (/ 1.0 300000000)) ;; 300MHz is a "decent enough" estimate (defmacro cpu-ticks-to-seconds (ticks) `(* ,EE_SECONDS_PER_TICK ,ticks) ) (defun stopwatch-elapsed-seconds ((this stopwatch)) "Returns the elapsed time so far (in seconds) of a stopwatch" (cpu-ticks-to-seconds (stopwatch-elapsed-ticks this)) ) (defmethod update-rates! ((this clock) (arg0 float)) "Recompute all clock values for the given clock ratio (arg0)." ;; remember our ratio (set! (-> this clock-ratio) arg0) ;; next, compute the time adjust ratio. This will be 1.0 for NTSC 60 fps. ;; It will be adjusted for PAL (1.2), and also for lag (dog-ratio). ;; The time ratio (and derived quantities) should be used for most per-frame calculations. (let ((f0-6 (if (nonzero? *display*) (* (-> *display* time-factor) (-> *display* dog-ratio) arg0) (* 5.0 arg0) ) ) ) (set! (-> this time-adjust-ratio) (* 0.2 f0-6)) ;; will be 5 for no-lag 60fps. ) ;; real seconds per frame (of game logic, not vsycns) (set! (-> this seconds-per-frame) (* 0.016666668 (-> this time-adjust-ratio))) ;; inverse of the above. (set! (-> this frames-per-second) (if (= (-> this time-adjust-ratio) 0.0) 0.0 (* 60.0 (/ 1.0 (-> this time-adjust-ratio))) ) ) ;; convert to sparticle format. (let* ((v1-12 (- (-> this frame-counter) (-> this old-frame-counter))) (f0-14 v1-12) (f1-9 (* 0.2 (the float v1-12))) ) (set-vector! (-> this sparticle-data) (the-as float f0-14) (* 5.0 f1-9) f1-9 f1-9) ) arg0 ) (defmethod advance-by! ((this clock) (arg0 float)) "Advance the clock by arg0 timeframes (as a float). Both counters keep a separate fractional and integer counter." (the int (+ arg0 (-> this accum))) ;; unused ;; add to accumulated time (+! (-> this integral-accum) arg0) ;; remember the old frame-count from last time (set! (-> this old-integral-frame-counter) (-> this integral-frame-counter)) ;; time-factor tells us how much time per vsync, if we've accumulated at least that much, increment integral ;; frame counter as needed. (while (>= (-> this integral-accum) (-> *display* time-factor)) (+! (-> this integral-frame-counter) 1) (set! (-> this integral-accum) (- (-> this integral-accum) (-> *display* time-factor))) ) ;; now the same for timeframe counter. (let ((v1-7 (the int (+ arg0 (-> this accum))))) (set! (-> this accum) (- (+ arg0 (-> this accum)) (the float v1-7))) (set! (-> this old-frame-counter) (-> this frame-counter)) (+! (-> this frame-counter) v1-7) ) ;; recompute rates. (update-rates! this (-> this clock-ratio)) this ) (defmethod tick! ((this clock)) "Per-game-frame clock tick forward." (if (zero? (logand (-> this mask) (-> *kernel-context* prevent-from-run))) ;; include: ntsc/pal scale, lag, and clock ratio. (advance-by! this (* (-> *display* time-factor) (-> *display* dog-ratio) (-> this clock-ratio))) (set! (-> this sparticle-data x) 0.0) ) this ) (defmethod reset! ((this clock)) "Reset a clock to 1000s, rate of 1." (set! (-> this frame-counter) (seconds 1000)) (set! (-> this integral-frame-counter) (the-as uint #x493e0)) (set! (-> this accum) 0.0) (set! (-> this old-frame-counter) (+ (-> this frame-counter) -1)) (set! (-> this old-integral-frame-counter) (+ (-> this integral-frame-counter) -1)) (update-rates! this 1.0) 0 (none) ) (defmethod save! ((this clock) (arg0 (pointer uint64))) "Save a clock's state to a buffer, return bytes used." (set! (-> arg0 0) (the-as uint (-> this frame-counter))) (set! (-> arg0 1) (-> this integral-frame-counter)) 16 ) (defmethod load! ((this clock) (arg0 (pointer uint64))) "Load a clock's state from a buffer, return bytes used." (set! (-> this frame-counter) (the-as time-frame (-> arg0 0))) (set! (-> this integral-frame-counter) (-> arg0 1)) (set! (-> this accum) 0.0) (set! (-> this integral-accum) 0.0) (set! (-> this old-frame-counter) (-> this frame-counter)) (set! (-> this old-integral-frame-counter) (-> this integral-frame-counter)) 16 )