;;-*-Lisp-*-
(in-package goal)

;; definition of type tr-stat
(deftype tr-stat (structure)
  ((groups    uint16  :offset-assert   0)
   (fragments uint16  :offset-assert   2)
   (tris      uint32  :offset-assert   4)
   (dverts    uint32  :offset-assert   8)
   (instances uint16  :offset-assert  12)
   (pad       uint16  :offset-assert  14)
   )
  :method-count-assert 9
  :size-assert         #x10
  :flag-assert         #x900000010
  )

;; definition for method 3 of type tr-stat
(defmethod inspect tr-stat ((obj tr-stat))
  (format #t "[~8x] ~A~%" obj 'tr-stat)
  (format #t "~Tgroups: ~D~%" (-> obj groups))
  (format #t "~Tfragments: ~D~%" (-> obj fragments))
  (format #t "~Ttris: ~D~%" (-> obj tris))
  (format #t "~Tdverts: ~D~%" (-> obj dverts))
  (format #t "~Tinstances: ~D~%" (-> obj instances))
  (format #t "~Tpad: ~D~%" (-> obj pad))
  obj
  )

;; definition of type merc-global-stats
(deftype merc-global-stats (structure)
  ((merc      tr-stat :inline :offset-assert   0)
   (mercneric tr-stat :inline :offset-assert  16)
   )
  :method-count-assert 9
  :size-assert         #x20
  :flag-assert         #x900000020
  )

;; definition for method 3 of type merc-global-stats
(defmethod inspect merc-global-stats ((obj merc-global-stats))
  (format #t "[~8x] ~A~%" obj 'merc-global-stats)
  (format #t "~Tmerc: #<tr-stat @ #x~X>~%" (-> obj merc))
  (format #t "~Tmercneric: #<tr-stat @ #x~X>~%" (-> obj mercneric))
  obj
  )

;; definition of type perf-stat
(deftype perf-stat (structure)
  ((frame-number   uint32  :offset-assert   0)
   (count          uint32  :offset-assert   4)
   (cycles         uint32  :offset-assert   8)
   (instructions   uint32  :offset-assert  12)
   (icache         uint32  :offset-assert  16)
   (dcache         uint32  :offset-assert  20)
   (select         uint32  :offset-assert  24)
   (ctrl           uint32  :offset-assert  28)
   (accum0         uint32  :offset-assert  32)
   (accum1         uint32  :offset-assert  36)
   (to-vu0-waits   uint32  :offset-assert  40)
   (to-spr-waits   uint32  :offset-assert  44)
   (from-spr-waits uint32  :offset-assert  48)
   )
  :pack-me
  :method-count-assert 14
  :size-assert         #x34
  :flag-assert         #xe00000034
  (:methods
    (dummy-9 (_type_) none 9)
    (print-to-stream (_type_ string basic) none 10)
    (reset! (_type_) none 11)
    (read! (_type_) none 12)
    (update-wait-stats (_type_ uint uint uint) none 13)
    )
  )

;; definition for method 3 of type perf-stat
(defmethod inspect perf-stat ((obj perf-stat))
  (format #t "[~8x] ~A~%" obj 'perf-stat)
  (format #t "~Tframe-number: ~D~%" (-> obj frame-number))
  (format #t "~Tcount: ~D~%" (-> obj count))
  (format #t "~Tcycles: ~D~%" (-> obj cycles))
  (format #t "~Tinstructions: ~D~%" (-> obj instructions))
  (format #t "~Ticache: ~D~%" (-> obj icache))
  (format #t "~Tdcache: ~D~%" (-> obj dcache))
  (format #t "~Tselect: ~D~%" (-> obj select))
  (format #t "~Tctrl: ~D~%" (-> obj ctrl))
  (format #t "~Taccum0: ~D~%" (-> obj accum0))
  (format #t "~Taccum1: ~D~%" (-> obj accum1))
  (format #t "~Tto-vu0-waits: ~D~%" (-> obj to-vu0-waits))
  (format #t "~Tto-spr-waits: ~D~%" (-> obj to-spr-waits))
  (format #t "~Tfrom-spr-waits: ~D~%" (-> obj from-spr-waits))
  obj
  )

;; definition of type perf-stat-array
(deftype perf-stat-array (inline-array-class)
  ((data perf-stat :inline :dynamic :offset-assert  16)
   )
  :method-count-assert 9
  :size-assert         #x10
  :flag-assert         #x900000010
  )

;; definition for method 3 of type perf-stat-array
(defmethod inspect perf-stat-array ((obj perf-stat-array))
  (format #t "[~8x] ~A~%" obj (-> obj type))
  (format #t "~Tlength: ~D~%" (-> obj length))
  (format #t "~Tallocated-length: ~D~%" (-> obj allocated-length))
  (format #t "~Tdata[0] @ #x~X~%" (-> obj data))
  obj
  )

;; failed to figure out what this is:
(set! (-> perf-stat-array heap-base) (the-as uint 52))

;; definition for method 11 of type perf-stat
;; INFO: Return type mismatch int vs none.
;; WARN: Unsupported inline assembly instruction kind - [mtc0 Perf, r0]
;; WARN: Unsupported inline assembly instruction kind - [sync.l]
;; WARN: Unsupported inline assembly instruction kind - [sync.p]
;; WARN: Unsupported inline assembly instruction kind - [mtpc pcr0, r0]
;; WARN: Unsupported inline assembly instruction kind - [mtpc pcr1, r0]
;; WARN: Unsupported inline assembly instruction kind - [sync.l]
;; WARN: Unsupported inline assembly instruction kind - [sync.p]
;; WARN: Unsupported inline assembly instruction kind - [mtc0 Perf, v1]
;; WARN: Unsupported inline assembly instruction kind - [sync.l]
;; WARN: Unsupported inline assembly instruction kind - [sync.p]
(defmethod reset! perf-stat ((obj perf-stat))
  (let ((v1-0 (-> obj ctrl)))
    (+! (-> obj count) 1)
    (b! (zero? v1-0) cfg-2 :delay (nop!))
    (.mtc0 Perf r0)
    (.sync.l)
    (.sync.p)
    (.mtpc pcr0 r0)
    (.mtpc pcr1 r0)
    (.sync.l)
    (.sync.p)
    (.mtc0 Perf v1-0)
    )
  (.sync.l)
  (.sync.p)
  (label cfg-2)
  0
  (none)
  )

;; definition for method 12 of type perf-stat
;; INFO: Return type mismatch int vs none.
;; WARN: Unsupported inline assembly instruction kind - [mtc0 Perf, r0]
;; WARN: Unsupported inline assembly instruction kind - [sync.l]
;; WARN: Unsupported inline assembly instruction kind - [sync.p]
;; WARN: Unsupported inline assembly instruction kind - [mfpc v1, pcr0]
;; WARN: Unsupported inline assembly instruction kind - [mfpc v1, pcr1]
(defmethod read! perf-stat ((obj perf-stat))
  (local-vars (v1-1 int) (v1-3 int))
  (b! (zero? (-> obj ctrl)) cfg-2 :delay (nop!))
  (.mtc0 Perf r0)
  (.sync.l)
  (.sync.p)
  (.mfpc v1-1 pcr0)
  (+! (-> obj accum0) v1-1)
  (.mfpc v1-3 pcr1)
  (+! (-> obj accum1) v1-3)
  (label cfg-2)
  0
  (none)
  )

;; definition for method 13 of type perf-stat
;; INFO: Return type mismatch int vs none.
(defmethod update-wait-stats perf-stat ((obj perf-stat) (arg0 uint) (arg1 uint) (arg2 uint))
  (when (nonzero? (-> obj ctrl))
    (+! (-> obj to-vu0-waits) arg0)
    (+! (-> obj to-spr-waits) arg1)
    (+! (-> obj from-spr-waits) arg2)
    )
  0
  (none)
  )

;; failed to figure out what this is:
(when (not *debug-segment*)
  (set! (-> perf-stat method-table 11) nothing)
  (set! (-> perf-stat method-table 12) nothing)
  (set! (-> perf-stat method-table 13) nothing)
  )
