Files
jak-project/goal_src/jak1/engine/ps2/timer-h.gc
T
2026-07-26 14:43:53 -04:00

136 lines
5.0 KiB
Common Lisp

;;-*-Lisp-*-
(in-package goal)
(bundles "ENGINE.CGO" "GAME.CGO")
(require "kernel-defs.gc")
;; There are two sources for timing:
;; - EE TIMER1, used for the frame profiler. NTSC has 9765 counts per frame; drawable resets it.
;; - Stopwatches, which read the 32-bit 300 MHz CPU cycle counter.
;; The Emotion Engine has four hardware timers. Timer 1 drives the frame profiler.
(defconstant TIMER0_BANK #x10000000) ;; has HOLD register!
(defconstant TIMER1_BANK #x10000800) ;; has HOLD register!
(defconstant TIMER2_BANK #x10001000) ;; does NOT have HOLD register!
(defconstant TIMER3_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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defenum timer-clock-selection
:type uint8
(busclk 0)
(busclk/16 1)
(busclk/256 2)
(hblank 3))
;; DECOMP BEGINS
;; Tn_MODE register layout. Only the low 16 bits are implemented; the upper half reads as zero.
(deftype timer-mode (uint32)
((clks timer-clock-selection :offset 0 :size 2)
(gate uint8 :offset 2 :size 1) ;; Gate counting with HBLANK or VBLANK.
(gats uint8 :offset 3 :size 1) ;; Select HBLANK (0) or VBLANK (1).
(gatm uint8 :offset 4 :size 2) ;; Select low-level, rising, falling, or either-edge gating.
(zret uint8 :offset 6 :size 1) ;; Clear the counter when it equals COMP.
(cue uint8 :offset 7 :size 1) ;; Enable counting.
(cmpe uint8 :offset 8 :size 1) ;; Enable compare interrupts.
(ovfe uint8 :offset 9 :size 1) ;; Enable overflow interrupts.
(equf uint8 :offset 10 :size 1) ;; Compare status flag.
(ovff uint8 :offset 11 :size 1))) ;; Overflow status flag.
;; 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.
(deftype timer-bank (structure)
((count uint32 :offset 0)
(mode timer-mode :offset 16)
(comp uint32 :offset 32)))
;; this matches an EE timer (with a HOLD register, timers 0 and 1)
(deftype timer-hold-bank (timer-bank)
((hold uint32 :offset 48)))
;; 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
(deftype stopwatch (basic)
((prev-time-elapsed time-frame)
(start-time time-frame)
(begin-level int32)))
;; BUSCLK/256 ticks per video frame. Profiler drawing divides timer counts by this value to
;; express durations as fractions of a frame. This is the NTSC default; video setup replaces it
;; with 11718 for PAL.
(define *ticks-per-frame* (/ 2500000 256)) ;; 2,500,000 bus clocks per NTSC frame.
(defun timer-init ((timer timer-bank) (mode timer-mode))
"Configure an EE hardware timer and clear its count."
(set! (-> timer mode) mode)
(set! (-> timer count) 0))
;; Timer 1 counts one tick per 256 bus clocks.
(#unless PC_PORT
(timer-init (the-as timer-bank TIMER1_BANK) (new 'static 'timer-mode :clks (timer-clock-selection busclk/256) :cue 1)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Profiler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The profiler uses the EE Timer 1 to record how long is spent in each part of the frame.
;; There is a EE and VU1 profiler. The EE profiler relies on code manually reporting how long
;; it takes to run, and the VU1 profiler uses VIF interrupts to do this automatically on
;; microprogram completion.
;; One timestamped boundary in a profiler bar.
(deftype profile-frame (structure)
((name symbol)
(time-stamp uint32)
(color rgba)))
;; A bar containing every timed event in one frame.
(declare-type dma-buffer basic)
(deftype profile-bar (basic)
((profile-frame-count int32)
(cache-time time-frame)
(data profile-frame 1024 :inline))
(:methods
(new (symbol type) _type_)
(get-last-frame-time-stamp (_type_) uint)
(reset (_type_) _type_)
(add-frame (_type_ symbol rgba) profile-frame)
(add-end-frame (_type_ symbol rgba) profile-frame)
(draw (_type_ dma-buffer int) float)))
(defmacro add-ee-profile-frame (name &key (r 0) &key (g 0) &key (b 0) &key (a #x80))
"Append a colored EE profiler boundary in debug builds."
`(if *debug-segment*
(add-frame (-> *display* frames (-> *display* on-screen) frame profile-bar 0)
,name
(new 'static 'rgba :r ,r :g ,g :b ,b :a ,a))))
(defmethod get-last-frame-time-stamp ((this profile-bar))
"Return the timestamp of the last completed interval on the profiler bar."
(-> this data (+ (-> this profile-frame-count) -2) time-stamp))