;;-*-Lisp-*- (in-package goal) ;; name: timer-h.gc ;; name in dgo: timer-h ;; dgos: ENGINE, GAME #|@file There are two sources for timing: - EE TIMER1, used for the frame profiler. There are 9765 counts of this per frame. It gets reset in drawable. - The "stopwatch" system, used for reading the CPU clock cycle counter, at 300 MHz (32-bit) |# ;; The Emotion Engine has 4 hardware timers, timer1 is used as the (defconstant TIMER0_BANK (the timer-bank #x10000000)) ;; has HOLD register! (defconstant TIMER1_BANK (the timer-bank #x10000800)) ;; has HOLD register! (defconstant TIMER2_BANK (the timer-bank #x10001000)) ;; does NOT have HOLD register! (defconstant TIMER3_BANK (the timer-bank #x10001800)) ;; does NOT have HOLD register! ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PC Port Timer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defmacro get-cpu-clock () "Read the 300 MHz clock." ;; __read-ee-timer is a 300 MHz timer from the C Kernel. ;; it's a real timer. `(the uint (logand #xffffffff (__read-ee-timer))) ) (defmacro get-bus-clock/256 () "Read the 150 MHz / 256 clock." ;; 300 MHz / (2^9) `(the uint (logand #xffffffff (shr (__read-ee-timer) 9))) ) (#when PC_PORT ;; the bus clock can be reset, which just stores the current count here. (define *timer-reset-value* (the uint 0)) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Timer HW ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; +++timer-clock-selection (defenum timer-clock-selection :type uint8 (busclk 0) (busclk/16 1) (busclk/256 2) (hblank 3) ) ;; ---timer-clock-selection ;; DECOMP BEGINS (deftype timer-mode (uint32) "This matches the Tn_MODE register structure of the ps2 EE timers. Only the lower 32 bits of these registers are usable, and the upper 16 hardwired to zero." ((clks timer-clock-selection :offset 0 :size 2) (gate uint8 :offset 2 :size 1) ;; gate function enable (gats uint8 :offset 3 :size 1) ;; gate selection: 0 = hblank, 1 = vblank ;; gate mode: ;; 0: count while gate signal is low ;; 1: start when gate signal rises ;; 2: start when gate signal falls ;; 3: start when gate signal rises/falls (gatm uint8 :offset 4 :size 2) (zret uint8 :offset 6 :size 1) ;; zero return: clear counter when equal to reference value (cue uint8 :offset 7 :size 1) ;; count-up enable (cmpe uint8 :offset 8 :size 1) ;; compare-interrupt enable (ovfe uint8 :offset 9 :size 1) ;; overflow-interrupt enable (equf uint8 :offset 10 :size 1) ;; equal-flag (ovff uint8 :offset 11 :size 1) ;; overflow-flag ) ) (deftype timer-bank (structure) "This matches an EE timer (without a HOLD register, timers 2 and 3). Each register is 128-bits wide, but only the lower 32-bits are usable, and the upper 16-bits of that are hardwired to zero." ((count uint32) (mode timer-mode :offset 16) (comp uint32 :offset 32) ) ) (deftype timer-hold-bank (timer-bank) "This matches an EE timer (with a HOLD register, timers 0 and 1)." ((hold uint32 :offset 48) ) ) (deftype stopwatch (basic) "Stopwatches are used to measure CPU clock cycles. They don't use the timer above, but instead the Count COP0 register, which counts CPU clock cycles directly." ((prev-time-elapsed time-frame) (start-time time-frame) (begin-level int32) ) ) (define *ticks-per-frame* 9765) (defun timer-init ((arg0 timer-bank) (arg1 timer-mode)) "Initiate a timer, start counting at a rate of 1 every 256 bus clocks (BUSCLK: ~147.456MHz)." (set! (-> arg0 mode) arg1) (set! (-> arg0 count) (the-as uint 0)) 0 ) (timer-init (the-as timer-bank #x10000800) (new 'static 'timer-mode :clks (timer-clock-selection busclk/256) :cue #x1) ) (deftype sce-cd-clock (structure) ((stat uint8) (second uint8) (minute uint8) (hour uint8) (pad uint8) (day uint8) (month uint8) (year uint8) ) ) (defun bcd-conv ((arg0 uint)) "Binary coded decimal to integer" (+ (* 10 (the-as int (/ (the-as int arg0) 16))) (logand arg0 15)) ) (define *month-days* (new 'static 'array uint32 12 #x1f #x1c #x1f #x1e #x1f #x1e #x1f #x1f #x1e #x1f #x1e #x1f) ) (defun is-leap ((arg0 uint)) (or (zero? (mod (the-as int arg0) 400)) (and (not (logtest? arg0 3)) (nonzero? (mod (the-as int arg0) 100)))) ) (defun mdy-to-day ((arg0 int) (arg1 int) (arg2 uint)) (if (is-leap arg2) (set! (-> *month-days* 1) (the-as uint 29)) (set! (-> *month-days* 1) (the-as uint 28)) ) (let ((v1-2 (+ arg0 -1)) (v0-1 (+ arg1 -1)) ) (dotimes (a0-4 v1-2) (+! v0-1 (-> (the-as (pointer int32) (&+ *month-days* (* a0-4 4))))) ) v0-1 ) ) (defun rtclock-to-secs ((arg0 sce-cd-clock)) (let ((s5-0 0) (s4-0 (bcd-conv (-> arg0 year))) ) (if (< s4-0 2000) (+! s4-0 2000) ) (dotimes (s3-0 (+ s4-0 -1970)) (+! s5-0 (* #x15180 (mdy-to-day 12 32 (the-as uint (+ s3-0 1970))))) ) (+ s5-0 (* #x15180 (mdy-to-day (bcd-conv (-> arg0 month)) (bcd-conv (-> arg0 day)) (the-as uint s4-0))) (* 3600 (bcd-conv (-> arg0 hour))) (* 60 (bcd-conv (-> arg0 minute))) (bcd-conv (-> arg0 second)) ) ) )