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

;; definition of type rpc-buffer
(deftype rpc-buffer (basic)
  ((elt-size  uint32           :offset-assert   4)
   (elt-count uint32           :offset-assert   8)
   (elt-used  uint32           :offset-assert  12)
   (busy      basic            :offset-assert  16)
   (base      pointer          :offset-assert  20)
   (data      uint8   :dynamic :offset         32)
   )
  :method-count-assert 9
  :size-assert         #x20
  :flag-assert         #x900000020
  (:methods
    (new (symbol type uint uint) rpc-buffer 0)
    )
  )

;; definition for method 3 of type rpc-buffer
(defmethod inspect rpc-buffer ((obj rpc-buffer))
  (format #t "[~8x] ~A~%" obj (-> obj type))
  (format #t "~Telt-size: ~D~%" (-> obj elt-size))
  (format #t "~Telt-count: ~D~%" (-> obj elt-count))
  (format #t "~Telt-used: ~D~%" (-> obj elt-used))
  (format #t "~Tbusy: ~A~%" (-> obj busy))
  (format #t "~Tbase: ~D~%" (-> obj base))
  (format #t "~Tdata[0] @ #x~X~%" (-> obj data))
  obj
  )

;; definition for method 0 of type rpc-buffer
(defmethod new rpc-buffer ((allocation symbol) (type-to-make type) (arg0 uint) (arg1 uint))
  (let* ((a2-2 (+ (-> type-to-make size) 63 (* (the-as int arg0) (the-as int arg1))))
         (v0-0 (object-new allocation type-to-make (the-as int a2-2)))
         )
    (set! (-> v0-0 elt-size) arg0)
    (set! (-> v0-0 elt-count) arg1)
    (set! (-> v0-0 elt-used) (the-as uint 0))
    (set! (-> v0-0 busy) #f)
    (set! (-> v0-0 base) (logand -64 (&-> (-> v0-0 data) 63)))
    v0-0
    )
  )

;; definition of type rpc-buffer-pair
(deftype rpc-buffer-pair (basic)
  ((buffer           rpc-buffer 2 :offset-assert   4)
   (current          rpc-buffer   :offset-assert  12)
   (last-recv-buffer pointer      :offset-assert  16)
   (rpc-port         int32        :offset-assert  20)
   )
  :method-count-assert 15
  :size-assert         #x18
  :flag-assert         #xf00000018
  (:methods
    (new (symbol type uint uint int) rpc-buffer-pair 0)
    (call (rpc-buffer-pair uint pointer uint) int 9)
    (add-element (rpc-buffer-pair) pointer 10)
    (decrement-elt-used (rpc-buffer-pair) int 11)
    (sync (rpc-buffer-pair symbol) int 12)
    (check-busy (rpc-buffer-pair) symbol 13)
    (pop-last-received (rpc-buffer-pair) pointer 14)
    )
  )

;; definition for method 3 of type rpc-buffer-pair
(defmethod inspect rpc-buffer-pair ((obj rpc-buffer-pair))
  (format #t "[~8x] ~A~%" obj (-> obj type))
  (format #t "~Tbuffer[2] @ #x~X~%" (-> obj buffer))
  (format #t "~Tcurrent: ~A~%" (-> obj current))
  (format #t "~Tlast-recv-buffer: #x~X~%" (-> obj last-recv-buffer))
  (format #t "~Trpc-port: ~D~%" (-> obj rpc-port))
  obj
  )

;; definition for method 0 of type rpc-buffer-pair
(defmethod new rpc-buffer-pair ((allocation symbol) (type-to-make type) (arg0 uint) (arg1 uint) (arg2 int))
  (let ((s3-0 (object-new allocation type-to-make (the-as int (-> type-to-make size)))))
    (set! (-> s3-0 buffer 0) (new 'global 'rpc-buffer arg0 arg1))
    (set! (-> s3-0 buffer 1) (new 'global 'rpc-buffer arg0 arg1))
    (set! (-> s3-0 current) (-> s3-0 buffer 0))
    (set! (-> s3-0 last-recv-buffer) (the-as pointer #f))
    (set! (-> s3-0 rpc-port) arg2)
    s3-0
    )
  )

;; definition for method 12 of type rpc-buffer-pair
(defmethod sync rpc-buffer-pair ((obj rpc-buffer-pair) (arg0 symbol))
  (let ((s5-0 (if (= (-> obj current) (-> obj buffer 0))
                  (-> obj buffer 1)
                  (-> obj buffer 0)
                  )
              )
        )
    (when (-> s5-0 busy)
      (when (nonzero? (rpc-busy? (-> obj rpc-port)))
        (if arg0
            (format 0 "STALL: waiting for IOP on RPC port #~D~%" (-> obj rpc-port))
            )
        (while (nonzero? (rpc-busy? (-> obj rpc-port)))
          (nop!)
          (nop!)
          (nop!)
          (nop!)
          (nop!)
          (nop!)
          (nop!)
          (nop!)
          )
        )
      (set! (-> s5-0 busy) #f)
      (set! (-> s5-0 elt-used) (the-as uint 0))
      0
      )
    )
  0
  )

;; definition for method 13 of type rpc-buffer-pair
(defmethod check-busy rpc-buffer-pair ((obj rpc-buffer-pair))
  (let ((gp-0 (if (= (-> obj current) (-> obj buffer 0))
                  (-> obj buffer 1)
                  (-> obj buffer 0)
                  )
              )
        )
    (when (-> gp-0 busy)
      (if (nonzero? (rpc-busy? (-> obj rpc-port)))
          (return #t)
          )
      (set! (-> gp-0 busy) #f)
      (set! (-> gp-0 elt-used) (the-as uint 0))
      0
      )
    )
  #f
  )

;; definition for method 9 of type rpc-buffer-pair
(defmethod call rpc-buffer-pair ((obj rpc-buffer-pair) (arg0 uint) (arg1 pointer) (arg2 uint))
  (when (nonzero? (-> obj current elt-used))
    (let ((s2-0 (if (= (-> obj current) (-> obj buffer 0))
                    (-> obj buffer 1)
                    (-> obj buffer 0)
                    )
                )
          )
      (when (-> s2-0 busy)
        (when (nonzero? (rpc-busy? (-> obj rpc-port)))
          (format 0 "STALL: waiting for IOP on RPC port #~D~%" (-> obj rpc-port))
          (while (nonzero? (rpc-busy? (-> obj rpc-port)))
            (nop!)
            (nop!)
            (nop!)
            (nop!)
            (nop!)
            (nop!)
            (nop!)
            (nop!)
            )
          )
        (set! (-> s2-0 busy) #f)
        (set! (-> s2-0 elt-used) (the-as uint 0))
        0
        )
      (let ((s1-0 (-> obj current)))
        (rpc-call
          (-> obj rpc-port)
          arg0
          (the-as uint 1)
          (the-as uint (-> s1-0 base))
          (the-as int (* (-> s1-0 elt-size) (-> s1-0 elt-used)))
          (the-as uint arg1)
          (the-as int arg2)
          )
        (set! (-> s1-0 busy) #t)
        )
      (set! (-> obj last-recv-buffer) arg1)
      (set! (-> obj current) s2-0)
      )
    )
  0
  )

;; definition for method 14 of type rpc-buffer-pair
(defmethod pop-last-received rpc-buffer-pair ((obj rpc-buffer-pair))
  (let ((v0-0 (-> obj last-recv-buffer)))
    (set! (-> obj last-recv-buffer) (the-as pointer #f))
    v0-0
    )
  )

;; definition for method 10 of type rpc-buffer-pair
(defmethod add-element rpc-buffer-pair ((obj rpc-buffer-pair))
  (let ((v1-0 (-> obj current)))
    (when (= (-> v1-0 elt-used) (-> v1-0 elt-count))
      (if (zero? (-> obj rpc-port))
          (format 0 "WARNING: too many sound commands queued~%")
          )
      (call obj (the-as uint 0) (the-as pointer 0) (the-as uint 0))
      (set! v1-0 (-> obj current))
      )
    (let ((v0-2 (&+ (-> v1-0 base) (* (-> v1-0 elt-used) (-> v1-0 elt-size)))))
      (+! (-> v1-0 elt-used) 1)
      v0-2
      )
    )
  )

;; definition for method 11 of type rpc-buffer-pair
(defmethod decrement-elt-used rpc-buffer-pair ((obj rpc-buffer-pair))
  (if (> (-> obj current elt-used) 0)
      (+! (-> obj current elt-used) -1)
      )
  0
  )

;; failed to figure out what this is:
0
