Skip to content

Commit

Permalink
[jak3] dma, dma-bucket, dma-buffer (#3331)
Browse files Browse the repository at this point in the history
  • Loading branch information
water111 authored Jan 27, 2024
1 parent f045e6c commit 4bd5111
Show file tree
Hide file tree
Showing 12 changed files with 809 additions and 46 deletions.
101 changes: 61 additions & 40 deletions decompiler/config/jak3/all-types.gc
Original file line number Diff line number Diff line change
Expand Up @@ -4215,42 +4215,44 @@
;; dma ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; (define-extern dma-sync-hang function) ;; (function dma-bank none)
;; (define-extern dma-sync-crash function) ;; (function dma-bank none)
;; (define-extern dma-send function) ;; (function dma-bank uint uint none)
;; (define-extern dma-send-chain function) ;; (function dma-bank-source uint none)
;; (define-extern dma-send-chain-no-tte function) ;; (function dma-bank-source uint none)
;; (define-extern dma-send-chain-no-flush function) ;; (function dma-bank-source uint none)
;; (define-extern dma-send-to-spr function) ;; (function uint uint uint symbol none)
;; (define-extern dma-send-to-spr-no-flush function) ;; (function uint uint uint symbol none)
;; (define-extern dma-send-from-spr function) ;; (function uint uint uint symbol none)
;; (define-extern dma-send-from-spr-no-flush function) ;; (function uint uint uint symbol none)
;; (define-extern dma-initialize function) ;; (function none)
;; (define-extern clear-vu0-mem function) ;; (function none)
;; (define-extern clear-vu1-mem function) ;; (function none)
;; (define-extern dump-vu1-mem function) ;; (function none)
;; (define-extern dump-vu1-range function) ;; (function uint uint symbol)
;; (define-extern reset-vif1-path function) ;; (function none)
;; (define-extern ultimate-memcpy function) ;; (function pointer pointer uint none)
;; (define-extern symlink2 function) ;; (function none)
;; (define-extern symlink3 function) ;; (function none)
(define-extern dma-sync-hang (function dma-bank none))
(define-extern dma-sync-crash (function dma-bank none))
(define-extern dma-send (function dma-bank uint uint none))
(define-extern dma-send-chain (function dma-bank-source uint none))
(define-extern dma-send-chain-no-tte (function dma-bank-source uint none))
(define-extern dma-send-chain-no-flush (function dma-bank-source uint none))
(define-extern dma-send-to-spr (function uint uint uint symbol none))
(define-extern dma-send-to-spr-no-flush (function uint uint uint symbol none))
(define-extern dma-send-from-spr (function uint uint uint symbol none))
(define-extern dma-send-from-spr-no-flush (function uint uint uint symbol none))
(define-extern dma-initialize (function none))
(define-extern clear-vu0-mem (function none))
(define-extern clear-vu1-mem (function none))
(define-extern dump-vu1-mem (function none))
(define-extern dump-vu1-range (function uint uint symbol))
(define-extern reset-vif1-path (function none))
(define-extern ultimate-memcpy (function pointer pointer uint none))
(define-extern symlink2 (function none))
(define-extern symlink3 (function none))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; dma-buffer ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(deftype dma-packet (structure)
((dma dma-tag :offset-assert 0) ;; dma-tag
"The header for a DMA transfer, containing an DMA tag, and VIF tags."
((dma dma-tag :offset-assert 0)
(vif0 vif-tag :offset-assert 8) ;; guessed by decompiler
(vif1 vif-tag :offset-assert 12) ;; guessed by decompiler
(quad uint128 :offset 0)
(quad uint128 :offset-assert 0 :overlay-at dma)
)
:method-count-assert 9
:size-assert #x10
:flag-assert #x900000010
)

(deftype dma-packet-array (inline-array-class)
"Unused dma array. Unclear how it should be used."
((data dma-packet :dynamic :offset-assert 16) ;; guessed by decompiler
)
:method-count-assert 14
Expand All @@ -4259,31 +4261,37 @@
)

(deftype dma-gif (structure)
"Believed unused GIF header type."
((gif uint64 2 :offset-assert 0) ;; guessed by decompiler
(quad uint128 :offset 0)
(quad uint128 :offset-assert 0 :overlay-at gif)
)
:method-count-assert 9
:size-assert #x10
:flag-assert #x900000010
)

(deftype dma-gif-packet (structure)
"The header for a DMA transfer that goes directly to GIF, containing DMA, VIF, GIF tags."
((dma-vif dma-packet :inline :offset-assert 0)
(gif uint64 2 :offset-assert 16) ;; guessed by decompiler
(quad uint128 2 :offset 0) ;; guessed by decompiler
(quad uint128 2 :offset-assert 0 :overlay-at dma-vif) ;; guessed by decompiler
)
:method-count-assert 9
:size-assert #x20
:flag-assert #x900000020
)

(deftype dma-buffer (basic)
"A buffer for DMA data."
((allocated-length int32 :offset-assert 4)
(base pointer :offset-assert 8) ;; guessed by decompiler
(end pointer :offset-assert 12) ;; guessed by decompiler
(real-buffer-end int32 :offset-assert 16)
(data uint64 1 :offset 32) ;; guessed by decompiler
)
(:methods
(new "Allocate a DMA buffer to hold the given size" (symbol type int) _type_) ;; 0
)
:method-count-assert 9
:size-assert #x28
:flag-assert #x900000028
Expand All @@ -4292,20 +4300,33 @@
)
)

;; (define-extern dma-buffer-inplace-new function) ;; (function dma-buffer int dma-buffer)
;; (define-extern dma-buffer-length function) ;; (function dma-buffer int)
;; (define-extern dma-buffer-free function) ;; (function dma-buffer int)
;; (define-extern dma-buffer-add-vu-function function) ;; (function dma-buffer vu-function int symbol)
;; (define-extern dma-buffer-send function) ;; (function dma-bank dma-buffer none)
;; (define-extern dma-buffer-send-chain function) ;; (function dma-bank-source dma-buffer none)
(define-extern dma-buffer-inplace-new
"Create a dma-buffer in-place. Does not set the type of the dma-buffer object."
(function dma-buffer int dma-buffer))
(define-extern dma-buffer-length
"Get length used in quadwords, rounded up."
(function dma-buffer int))
(define-extern dma-buffer-free
"Get the number of free quadwords between base and end pointers."
(function dma-buffer int))
(define-extern dma-buffer-add-vu-function
"Add DMA tags to load the given VU function. The destination in vu instruction memory
is specific inside the vu-function. This does NOT copy the vu-function into the buffer,
but creates a reference to the existing VU function." (function dma-buffer vu-function int symbol))
(define-extern dma-buffer-send
"Send the DMA buffer! DOES NOT TRANSFER TAG, you probably want dma-buffer-send-chain instead."
(function dma-bank dma-buffer none))
(define-extern dma-buffer-send-chain
"Send the DMA buffer! Sends the tags, so this is suitable for the main graphics chain."
(function dma-bank-source dma-buffer none))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; dma-bucket ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; (define-extern dma-buffer-add-buckets function) ;; (function dma-buffer int (inline-array dma-bucket))
;; (define-extern dma-buffer-patch-buckets function) ;; (function (inline-array dma-bucket) int (inline-array dma-bucket))
;; (define-extern dma-bucket-insert-tag function) ;; (function (inline-array dma-bucket) bucket-id pointer (pointer dma-tag) pointer)
(define-extern dma-buffer-add-buckets "Initialize an array of dma-buckets in a DMA buffer." (function dma-buffer int (inline-array dma-bucket)))
(define-extern dma-buffer-patch-buckets "Patch together DMA buckets after they have been filled." (function dma-bucket int dma-bucket))
(define-extern dma-bucket-insert-tag "Add a dma chain to the bucket." (function (inline-array dma-bucket) bucket-id pointer (pointer dma-tag) pointer))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; dma-disasm ;;
Expand Down Expand Up @@ -4464,7 +4485,7 @@
:size-assert #x10
:flag-assert #x900000010
(:methods
(new
(new
"Create a cpad-list for 2 controllers. It's fine to do this even if one or both controllers
aren't connected yet."
(symbol type) _type_) ;; 0
Expand Down Expand Up @@ -5519,7 +5540,7 @@
and arg2 is down (-y). Will have the pitch of forward."
(function matrix vector vector matrix))
(define-extern forward-down-nopitch->inv-matrix
"Create a matrix representing an inverse transform where arg1 is forward (+z)
"Create a matrix representing an inverse transform where arg1 is forward (+z)
and arg2 is down (-y). Will not use the pitch of forward."
(function matrix vector vector matrix))
(define-extern forward-up->inv-matrix
Expand Down Expand Up @@ -7712,7 +7733,7 @@
"The math-camera matrices are used to compute fogging values, which are a per-vertex uint8 that
tells the GS how 'foggy' the color should be. This should be proportional to how far away the vertex
is. There is a scaling factor applied so the fog intensity isn't affected by the field of view angle.

The fog-corrector stores a fog-end fog-start value that is corrected for the field of view.
The actual correction factor is computed in cam-update.gc.
Without this corrector, the fogginess of the world would change as the FOV changes
Expand Down Expand Up @@ -8151,7 +8172,7 @@
(deftype connectable (structure)
"A connectable is the linked-list node part of a connection.
The connections themselves are owned by the engine.

The `next0`/`prev0` references are used for how this belongs in the connectable list
belonging to the [[engine]]. These terminate on special nodes stored in the engine:
`alive-list`/`alive-list-end` for the active connections, and `dead-list`/`dead-list-end`
Expand Down Expand Up @@ -8244,7 +8265,7 @@
"Add a connection between this engine and a given process."
(engine process object object object object) connection) ;; 15
(remove-from-process "Remove all connections from process for this engine." (engine process) int) ;; 16
(remove-matching
(remove-matching
"Call the given function on each connection and the engine.
If it returns truthy, `move-to-dead` that connection."
(engine (function connection engine symbol)) int) ;; 17
Expand Down Expand Up @@ -11167,15 +11188,15 @@
;; (define-extern *stats-bsp* object) ;; symbol
;; (define-extern *stats-buffer* object) ;; symbol
;; (define-extern *stats-target* object) ;; symbol
;; (define-extern *stats-profile-bars* object) ;; symbol
(define-extern *stats-profile-bars* symbol)
;; (define-extern *stats-perf* object) ;; symbol
;; (define-extern *artist-all-visible* object) ;; symbol
;; (define-extern *artist-flip-visible* object) ;; symbol
;; (define-extern *artist-fix-visible* object) ;; symbol
;; (define-extern *artist-fix-frustum* object) ;; symbol
;; (define-extern *artist-error-spheres* object) ;; symbol
;; (define-extern *artist-use-menu-subdiv* object) ;; symbol
;; (define-extern *display-profile* object) ;; symbol
(define-extern *display-profile* symbol)
;; (define-extern *display-sidekick-stats* object) ;; symbol
;; (define-extern *display-quad-stats* object) ;; symbol
;; (define-extern *display-tri-stats* object) ;; symbol
Expand Down Expand Up @@ -11211,7 +11232,7 @@
;; (define-extern *display-sprite-marks* object) ;; symbol
;; (define-extern *display-sprite-spheres* object) ;; symbol
;; (define-extern *display-entity-errors* object) ;; symbol
;; (define-extern *display-capture-mode* object)
(define-extern *display-capture-mode* symbol)
;; (define-extern *display-instance-info* object) ;; symbol
;; (define-extern *display-deci-count* object) ;; symbol
;; (define-extern *sync-dma* object) ;; symbol
Expand Down
15 changes: 15 additions & 0 deletions decompiler/config/jak3/ntsc_v1/type_casts.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,20 @@
[96, "t2", "(pointer gs-xyzf)"],
[97, "t2", "(pointer uint64)"],
[[110, 117], "v1", "(pointer uint64)"]
],
"dma-bucket-insert-tag": [
[[2, 6], "v1", "dma-bucket"],
[3, "a0", "dma-bucket"]
],
"dma-buffer-add-buckets": [
[[1, 4], "v1", "dma-bucket"],
[5, "v1", "pointer"],
[[9, 11], "v1", "dma-bucket"],
[11, "v1", "pointer"]
],
"dma-buffer-patch-buckets": [
[[3, 34], "a0", "dma-bucket"],
[[34, 38], "a0", "dma-packet"]
// [34, "a0", "(inline-array dma-bucket)"]
]
}
30 changes: 30 additions & 0 deletions decompiler/config/jak3/ntsc_v1/var_names.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -458,5 +458,35 @@
"vars": {
"gp-0": "mouse"
}
},
"(method 0 dma-buffer)": {
"args": ["allocation", "type-to-make", "size-bytes"]
},
"dma-buffer-inplace-new": {
"args": ["dma-buff", "size-bytes"]
},
"dma-buffer-length": {
"args": ["dma-buf"]
},
"dma-buffer-free": {
"args": ["dma-buf"]
},
"dma-buffer-add-vu-function": {
"args": ["dma-buf", "vu-func", "flush-path-3"]
},
"dma-buffer-send": {
"args": ["chan", "buf"]
},
"dma-buffer-send-chain": {
"args": ["chan", "buf"]
},
"dma-buffer-patch-buckets": {
"args": ["base", "count"]
},
"dma-buffer-add-buckets": {
"args": ["dma-buf", "bucket-count"]
},
"dma-bucket-insert-tag": {
"args": ["buckets", "bucket", "start-tag", "end-tag-to-patch"]
}
}
60 changes: 60 additions & 0 deletions goal_src/jak3/engine/dma/dma-bucket.gc
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,63 @@

;; DECOMP BEGINS

;; WARN: Return type mismatch pointer vs (inline-array dma-bucket).
(defun dma-buffer-add-buckets ((dma-buf dma-buffer) (bucket-count int))
"Initialize an array of dma-buckets in a DMA buffer."
(let ((v0-0 (-> dma-buf base)))
(let ((v1-0 (the-as object v0-0)))
(dotimes (a2-0 bucket-count)
(set! (-> (the-as dma-bucket v1-0) tag)
(new 'static 'dma-tag :id (dma-tag-id next) :addr (the-as int (&+ (the-as pointer v1-0) 16)))
)
(set! (-> (the-as dma-bucket v1-0) last) (the-as (pointer dma-tag) v1-0))
(set! v1-0 (&+ (the-as pointer v1-0) 16))
)
(set! (-> dma-buf base) (the-as pointer v1-0))
)
(the-as (inline-array dma-bucket) v0-0)
)
)

(defun dma-buffer-patch-buckets ((base dma-bucket) (count int))
"Patch together DMA buckets after they have been filled."
(when (nonzero? base)
(dotimes (v1-1 count)
(cond
((= base (-> base last))
(set! (-> base tag) (new 'static 'dma-tag :id (dma-tag-id cnt)))
(set! (-> base clear) (the-as uint 0))
0
)
(else
(set! (-> base last 0 addr) (the-as int (&+ base 16)))
(cond
((and (or *display-profile* *stats-profile-bars*) (not *display-capture-mode*))
(set! (-> (the-as dma-packet base) vif0) (new 'static 'vif-tag :cmd (vif-cmd mark) :imm v1-1))
(set! (-> (the-as dma-packet base) vif1) (new 'static 'vif-tag :irq #x1))
)
(else
(set! (-> base clear) (the-as uint 0))
0
)
)
)
)
(&+! base 16)
)
)
base
)

(defun dma-bucket-insert-tag ((buckets (inline-array dma-bucket))
(bucket bucket-id)
(start-tag pointer)
(end-tag-to-patch (pointer dma-tag))
)
"Add a dma chain to the bucket."
(let ((v1-1 (-> buckets bucket)))
(set! (-> (the-as dma-bucket (-> v1-1 last)) next) (the-as uint start-tag))
(set! (-> v1-1 last) end-tag-to-patch)
)
start-tag
)
Loading

0 comments on commit 4bd5111

Please sign in to comment.