mips: gte.lisp

the PS1's coprocessor 2, the Geometry Transformation Engine: its registers, moves and commands.

0/1 a fixed bit, name a field,   a bit the decoder ignores

mfc2 source

mfc2 rt rd

Move From Coprocessor 2 (GTE data)

MFC2: "rt = cop2datRd", after the next instruction

r3000a:

313029282726252423222120191817161514131211109876543210
01001000000rtrd00000000000
(defmips mfc2 (:only :r3000a) (:asm "mfc2" rt rd) (:doc "Move From Coprocessor 2 (GTE data)")
  (if (r3k-cop2-usable) (set-gpr-load rt (gte-read cpu rd)) (raise-cpu 2)))

cfc2 source

cfc2 rt rd

Move Control From Coprocessor 2 (GTE control)

CFC2: "rt = cop2cntRd", after the next instruction

r3000a:

313029282726252423222120191817161514131211109876543210
01001000010rtrd00000000000
(defmips cfc2 (:only :r3000a) (:asm "cfc2" rt rd) (:doc "Move Control From Coprocessor 2 (GTE control)")
  (if (r3k-cop2-usable) (set-gpr-load rt (gte-read cpu (+ rd 32))) (raise-cpu 2)))

mtc2 source

mtc2 rt rd

Move To Coprocessor 2 (GTE data)

MTC2: "cop2datRd = rt"

r3000a:

313029282726252423222120191817161514131211109876543210
01001000100rtrd00000000000
(defmips mtc2 (:only :r3000a) (:asm "mtc2" rt rd) (:doc "Move To Coprocessor 2 (GTE data)")
  (if (r3k-cop2-usable) (gte-write cpu rd (wrap 32 (gpr rt))) (raise-cpu 2)))

ctc2 source

ctc2 rt rd

Move Control To Coprocessor 2 (GTE control)

CTC2: "cop2cntRd = rt"

r3000a:

313029282726252423222120191817161514131211109876543210
01001000110rtrd00000000000
(defmips ctc2 (:only :r3000a) (:asm "ctc2" rt rd) (:doc "Move Control To Coprocessor 2 (GTE control)")
  (if (r3k-cop2-usable) (gte-write cpu (+ rd 32) (wrap 32 (gpr rt))) (raise-cpu 2)))

lwc2 source

lwc2 rt (simm imm) rs

Load Word to Coprocessor 2 (GTE data)

LWC2: "cop2dat_rt = [rs+imm]", a word, aligned (rt names the GTE data register)

r3000a:

313029282726252423222120191817161514131211109876543210
110010rsrtimm
(defmips lwc2 (:only :r3000a) (:asm "lwc2" rt (simm imm) rs) (:doc "Load Word to Coprocessor 2 (GTE data)")
  (let ((a (vaddr rs imm)))
    (cond ((not (r3k-cop2-usable)) (raise-cpu 2))
          ((/= (wrap 2 a) 0) (raise-address +exc-adel+ a))
          (t (gte-write cpu rt (mips-load 32 a))))))

swc2 source

swc2 rt (simm imm) rs

Store Word from Coprocessor 2 (GTE data)

SWC2: "[rs+imm] = cop2dat_rt"

r3000a:

313029282726252423222120191817161514131211109876543210
111010rsrtimm
(defmips swc2 (:only :r3000a) (:asm "swc2" rt (simm imm) rs) (:doc "Store Word from Coprocessor 2 (GTE data)")
  (let ((a (vaddr rs imm)))
    (cond ((not (r3k-cop2-usable)) (raise-cpu 2))
          ((/= (wrap 2 a) 0) (raise-address +exc-ades+ a))
          (t (mips-store 32 a (gte-read cpu rt))))))

bc2f source

bc2f (b-rel offset)

Branch on Coprocessor 2 False (always)

BC2F/BC2T: "jt cop2flg,dest ;-jump never ... jf cop2flg,dest ;-jump
always ... flag seems to be always false"

r3000a:

313029282726252423222120191817161514131211109876543210
0100100100000000offset
(defmips bc2f (:only :r3000a) (:asm "bc2f" (b-rel offset)) (:doc "Branch on Coprocessor 2 False (always)")
  (if (r3k-cop2-usable) (branch-to (branch-target offset)) (raise-cpu 2)))

bc2t source

bc2t (b-rel offset)

Branch on Coprocessor 2 True (never)

r3000a:

313029282726252423222120191817161514131211109876543210
0100100100000001offset
(defmips bc2t (:only :r3000a) (:asm "bc2t" (b-rel offset)) (:doc "Branch on Coprocessor 2 True (never)")
  (unless (r3k-cop2-usable) (raise-cpu 2)))

rtps source

rtps sf lm

GTE: Perspective Transformation single

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm000001
(defmips rtps (:only :r3000a) (:asm "rtps" sf lm) (:doc "GTE: Perspective Transformation single")
  (gte-command (gte-rtp cpu 0 sf lm 1)))

rtpt source

rtpt sf lm

GTE: Perspective Transformation triple

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm110000
(defmips rtpt (:only :r3000a) (:asm "rtpt" sf lm) (:doc "GTE: Perspective Transformation triple")
  (gte-command (gte-rtp cpu 0 sf lm 0) (gte-rtp cpu 1 sf lm 0) (gte-rtp cpu 2 sf lm 1)))

nclip source

nclip sf lm

GTE: Normal clipping

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm000110
(defmips nclip (:only :r3000a) (:asm "nclip" sf lm) (:doc "GTE: Normal clipping")
  (gte-command (gte-nclip cpu)))

avsz3 source

avsz3 sf lm

GTE: Average of three Z values

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm101101
(defmips avsz3 (:only :r3000a) (:asm "avsz3" sf lm) (:doc "GTE: Average of three Z values")
  (gte-command (gte-avsz cpu (sext 16 (gte-c 29)) (+ (gte-d 17) (gte-d 18) (gte-d 19)))))

avsz4 source

avsz4 sf lm

GTE: Average of four Z values

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm101110
(defmips avsz4 (:only :r3000a) (:asm "avsz4" sf lm) (:doc "GTE: Average of four Z values")
  (gte-command (gte-avsz cpu (sext 16 (gte-c 30)) (+ (gte-d 16) (gte-d 17) (gte-d 18) (gte-d 19)))))

mvmva source

mvmva sf mx v cv lm

GTE: Multiply vector by matrix and add vector

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sfmxvcvlm010010
(defmips mvmva (:only :r3000a) (:asm "mvmva" sf mx v cv lm) (:doc "GTE: Multiply vector by matrix and add vector")
  (gte-command (gte-mvmva cpu sf mx v cv lm)))

sqr source

sqr sf lm

GTE: Square of vector IR

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm101000
(defmips sqr (:only :r3000a) (:asm "sqr" sf lm) (:doc "GTE: Square of vector IR")
  (gte-command (gte-sqr cpu sf lm)))

op source

op sf lm

GTE: Cross product of 2 vectors

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm001100
(defmips op (:only :r3000a) (:asm "op" sf lm) (:doc "GTE: Cross product of 2 vectors")
  (gte-command (gte-op cpu sf lm)))

gpf source

gpf sf lm

GTE: General purpose interpolation

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm111101
(defmips gpf (:only :r3000a) (:asm "gpf" sf lm) (:doc "GTE: General purpose interpolation")
  (gte-command (gte-gpf cpu sf lm)))

gpl source

gpl sf lm

GTE: General purpose interpolation with base

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm111110
(defmips gpl (:only :r3000a) (:asm "gpl" sf lm) (:doc "GTE: General purpose interpolation with base")
  (gte-command (gte-gpl cpu sf lm)))

dpcs source

dpcs sf lm

GTE: Depth Cueing single

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm010000
(defmips dpcs (:only :r3000a) (:asm "dpcs" sf lm) (:doc "GTE: Depth Cueing single")
  (gte-command (gte-dpc cpu (gte-rgbc cpu 1) (gte-rgbc cpu 2) (gte-rgbc cpu 3) sf lm)))

dpct source

dpct sf lm

GTE: Depth Cueing triple

"DPCT executes thrice, and reads the R,G,B values from RGB0"

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm101010
(defmips dpct (:only :r3000a) (:asm "dpct" sf lm) (:doc "GTE: Depth Cueing triple")
  (gte-command
    (gte-dpc cpu (bit-field (gte-d 20) 0 8) (bit-field (gte-d 20) 8 8) (bit-field (gte-d 20) 16 8) sf lm)
    (gte-dpc cpu (bit-field (gte-d 20) 0 8) (bit-field (gte-d 20) 8 8) (bit-field (gte-d 20) 16 8) sf lm)
    (gte-dpc cpu (bit-field (gte-d 20) 0 8) (bit-field (gte-d 20) 8 8) (bit-field (gte-d 20) 16 8) sf lm)))

intpl source

intpl sf lm

GTE: Interpolation of a vector and far color vector

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm010001
(defmips intpl (:only :r3000a) (:asm "intpl" sf lm) (:doc "GTE: Interpolation of a vector and far color vector")
  (gte-command (gte-intpl cpu sf lm)))

dcpl source

dcpl sf lm

GTE: Depth Cue Color light

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm101001
(defmips dcpl (:only :r3000a) (:asm "dcpl" sf lm) (:doc "GTE: Depth Cue Color light")
  (gte-command (gte-dcpl cpu sf lm)))

ncs source

ncs sf lm

GTE: Normal color single

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm011110
(defmips ncs (:only :r3000a) (:asm "ncs" sf lm) (:doc "GTE: Normal color single")
  (gte-command (gte-nc cpu 0 sf lm 0)))

nct source

nct sf lm

GTE: Normal color triple

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm100000
(defmips nct (:only :r3000a) (:asm "nct" sf lm) (:doc "GTE: Normal color triple")
  (gte-command (gte-nc cpu 0 sf lm 0) (gte-nc cpu 1 sf lm 0) (gte-nc cpu 2 sf lm 0)))

nccs source

nccs sf lm

GTE: Normal Color Color single vector

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm011011
(defmips nccs (:only :r3000a) (:asm "nccs" sf lm) (:doc "GTE: Normal Color Color single vector")
  (gte-command (gte-nc cpu 0 sf lm 1)))

ncct source

ncct sf lm

GTE: Normal Color Color triple vector

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm111111
(defmips ncct (:only :r3000a) (:asm "ncct" sf lm) (:doc "GTE: Normal Color Color triple vector")
  (gte-command (gte-nc cpu 0 sf lm 1) (gte-nc cpu 1 sf lm 1) (gte-nc cpu 2 sf lm 1)))

ncds source

ncds sf lm

GTE: Normal color depth cue single vector

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm010011
(defmips ncds (:only :r3000a) (:asm "ncds" sf lm) (:doc "GTE: Normal color depth cue single vector")
  (gte-command (gte-nc cpu 0 sf lm 2)))

ncdt source

ncdt sf lm

GTE: Normal color depth cue triple vectors

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm010110
(defmips ncdt (:only :r3000a) (:asm "ncdt" sf lm) (:doc "GTE: Normal color depth cue triple vectors")
  (gte-command (gte-nc cpu 0 sf lm 2) (gte-nc cpu 1 sf lm 2) (gte-nc cpu 2 sf lm 2)))

cc source

cc sf lm

GTE: Color Color

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm011100
(defmips cc (:only :r3000a) (:asm "cc" sf lm) (:doc "GTE: Color Color")
  (gte-command (gte-nc cpu 3 sf lm 1)))

cdp source

cdp sf lm

GTE: Color Depth Que

r3000a:

313029282726252423222120191817161514131211109876543210
0100101sflm010100
(defmips cdp (:only :r3000a) (:asm "cdp" sf lm) (:doc "GTE: Color Depth Que")
  (gte-command (gte-nc cpu 3 sf lm 2)))

c2 source

c2 cofun

GTE: a command it does not have (no effect)

a COP2 command number the GTE does not have: nothing

r3000a:

313029282726252423222120191817161514131211109876543210
0100101cofun
(defmips c2 (:only :r3000a) (:asm "c2" cofun) (:doc "GTE: a command it does not have (no effect)")
  (unless (r3k-cop2-usable) (raise-cpu 2)))

Other definitions

(defmacro gte-d) source
(defmacro gte-d (n) `(aref (mips-cop2d cpu) ,n))
(defmacro gte-c) source
(defmacro gte-c (n) `(aref (mips-cop2c cpu) ,n))
(defmacro r3k-cop2-usable) source
(defmacro r3k-cop2-usable () `(= (bit-at (aref (mips-cop0 cpu) 12) 30) 1))
(defmacro gte-flag-word) source
(defmacro gte-flag-word (f)
  (let ((v (gensym "F")))
    `(let ((,v ,f)) (logior ,v (if (= (logand ,v #x7F87E000) 0) 0 #x80000000)))))
(defmacro gte-setflag) source
(defmacro gte-setflag (b) `(setf (gte-c 31) (logior (gte-c 31) (ash 1 ,b))))
(defun gte-orgb) source
(defun gte-orgb (cpu)
  "ORGB: IR1-3 divided by 80h, saturated to 0..1Fh, as 5:5:5 bits"
  (declare (type mips cpu))
  (logior (saturate 0 #x1F (ash (sext 32 (gte-d 9)) -7))
          (ash (saturate 0 #x1F (ash (sext 32 (gte-d 10)) -7)) 5)
          (ash (saturate 0 #x1F (ash (sext 32 (gte-d 11)) -7)) 10)))
(defun gte-read) source
(defun gte-read (cpu r)
  "the value mfc2/cfc2/swc2 read from GTE register R (0-63)"
  (declare (type mips cpu) (type (unsigned-byte 6) r))
  (cond ((= r 15) (gte-d 14))
        ((or (= r 28) (= r 29)) (gte-orgb cpu))
        ((< r 32) (gte-d r))
        (t (gte-c (- r 32)))))
(defun gte-write) source
(defun gte-write (cpu r v)
  "mtc2/ctc2/lwc2: write V to GTE register R (0-63)"
  (declare (type mips cpu) (type (unsigned-byte 6) r) (type (unsigned-byte 32) v))
  (case r
    ((1 3 5 8 9 10 11) (setf (gte-d r) (wrap 32 (sext 16 v))))
    ((36 44 52 58 59 61 62) (setf (gte-c (- r 32)) (wrap 32 (sext 16 v))))
    ((7 16 17 18 19) (setf (gte-d r) (wrap 16 v)))
    (15 (setf (gte-d 12) (gte-d 13) (gte-d 13) (gte-d 14) (gte-d 14) v))
    (28 (setf (gte-d 28) (wrap 15 v)
              (gte-d 9) (* (bit-field v 0 5) #x80)
              (gte-d 10) (* (bit-field v 5 5) #x80)
              (gte-d 11) (* (bit-field v 10 5) #x80)))
    ((29 31) nil)
    (30 (setf (gte-d 30) v
              (gte-d 31) (if (= (bit-at v 31) 1) (clz 32 (lognot v)) (clz 32 v))))
    (63 (setf (gte-c 31) (gte-flag-word (logand v #x7FFFF000))))
    (t (if (< r 32) (setf (gte-d r) v) (setf (gte-c (- r 32)) v)))))
(defun gte-mac-check) source
(defun gte-mac-check (cpu i v)
  "flag a MACi (I 1-3) sum V beyond 44 bits (FLAG 30-28 positive, 27-25
   negative); -> V sign-extended from 44 bits, as the accumulator keeps it"
  (declare (type mips cpu) (type (unsigned-byte 2) i) (type (signed-byte 64) v))
  (cond ((> v #x7FFFFFFFFFF) (gte-setflag (- 31 i)))
        ((< v #x-80000000000) (gte-setflag (- 28 i))))
  (sext 44 v))
(defun gte-set-mac) source
(defun gte-set-mac (cpu i v sf)
  "MACi <- the sum V SAR (sf*12), its low 32 bits, V checked against 44 bits"
  (declare (type mips cpu) (type (unsigned-byte 2) i) (type (signed-byte 64) v) (type (unsigned-byte 1) sf))
  (gte-mac-check cpu i v)
  (setf (gte-d (+ 24 i)) (wrap 32 (ash v (- (* 12 sf))))))
(defun gte-mac) source
(defun gte-mac (cpu i)
  "MACi, signed"
  (declare (type mips cpu) (type (unsigned-byte 2) i))
  (sext 32 (gte-d (+ 24 i))))
(defun gte-ir) source
(defun gte-ir (cpu i)
  "IRi (I 0-3), signed"
  (declare (type mips cpu) (type (unsigned-byte 2) i))
  (sext 16 (gte-d (+ 8 i))))
(defun gte-sat-ir) source
(defun gte-sat-ir (cpu i v lm)
  "V saturated as IRi (I 1-3) is: -8000h or, with LM, 0 .. 7FFFh (FLAG 24-22)"
  (declare (type mips cpu) (type (unsigned-byte 2) i) (type (signed-byte 64) v) (type (unsigned-byte 1) lm))
  (let ((lo (if (= lm 1) 0 #x-8000)))
    (cond ((< v lo) (gte-setflag (- 25 i)) lo)
          ((> v #x7FFF) (gte-setflag (- 25 i)) #x7FFF)
          (t v))))
(defun gte-set-ir) source
(defun gte-set-ir (cpu i v lm)
  "IRi <- V saturated"
  (declare (type mips cpu) (type (unsigned-byte 2) i) (type (signed-byte 64) v) (type (unsigned-byte 1) lm))
  (setf (gte-d (+ 8 i)) (wrap 32 (gte-sat-ir cpu i v lm))))
(defun gte-ir-from-mac) source
(defun gte-ir-from-mac (cpu lm)
  "[IR1,IR2,IR3] = [MAC1,MAC2,MAC3], saturated"
  (declare (type mips cpu) (type (unsigned-byte 1) lm))
  (gte-set-ir cpu 1 (gte-mac cpu 1) lm)
  (gte-set-ir cpu 2 (gte-mac cpu 2) lm)
  (gte-set-ir cpu 3 (gte-mac cpu 3) lm))
(defun gte-mac0) source
(defun gte-mac0 (cpu v)
  "flag a MAC0 value V beyond 32 bits (FLAG 16 positive, 15 negative); -> V"
  (declare (type mips cpu) (type (signed-byte 64) v))
  (cond ((> v #x7FFFFFFF) (gte-setflag 16))
        ((< v #x-80000000) (gte-setflag 15)))
  v)
(defun gte-color) source
(defun gte-color (cpu i v)
  "a colour component: V saturated to 0..FFh (FLAG 21-19)"
  (declare (type mips cpu) (type (unsigned-byte 2) i) (type (signed-byte 64) v))
  (cond ((< v 0) (gte-setflag (- 22 i)) 0)
        ((> v #xFF) (gte-setflag (- 22 i)) #xFF)
        (t v)))
(defun gte-push-color) source
(defun gte-push-color (cpu)
  "\"Color FIFO = [MAC1/16,MAC2/16,MAC3/16,CODE]\": RGB0 <- RGB1 <- RGB2 <- the new one"
  (declare (type mips cpu))
  (let ((r (gte-color cpu 1 (ash (gte-mac cpu 1) -4)))
        (g (gte-color cpu 2 (ash (gte-mac cpu 2) -4)))
        (b (gte-color cpu 3 (ash (gte-mac cpu 3) -4))))
    (setf (gte-d 20) (gte-d 21)
          (gte-d 21) (gte-d 22)
          (gte-d 22) (logior r (ash g 8) (ash b 16) (logand (gte-d 6) #xFF000000)))))
(defun gte-mat) source
(defun gte-mat (cpu mx k)
  "element K (row * 3 + column) of matrix MX: 0 RT, 1 LLM, 2 LCM, 3 the
   garbage matrix of MVMVA's mx=3 (\"-R*10h, +R*10h, IR0, RT13, RT13, RT13,
   RT22, RT22, RT22\", R the red of RGBC)"
  (declare (type mips cpu) (type (unsigned-byte 2) mx) (type (unsigned-byte 4) k))
  (if (= mx 3)
      (case k
        (0 (- (ash (bit-field (gte-d 6) 0 8) 4)))
        (1 (ash (bit-field (gte-d 6) 0 8) 4))
        (2 (gte-ir cpu 0))
        ((3 4 5) (gte-mat cpu 0 2))
        (t (gte-mat cpu 0 4)))
      (let ((w (gte-c (+ (* 8 mx) (ash k -1)))))
        (if (= (logand k 1) 0) (sext 16 w) (sext 16 (ash w -16))))))

Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.