the PS1's coprocessor 2, the Geometry Transformation Engine: its registers, moves and commands.
(defun gte-vec) source(defun gte-vec (cpu v j)
"component J (0 X, 1 Y, 2 Z) of vector V: 0-2 V0-V2, 3 [IR1,IR2,IR3]"
(declare (type mips cpu) (type (unsigned-byte 2) v j))
(cond ((= v 3) (gte-ir cpu (+ j 1)))
((= j 0) (sext 16 (gte-d (* 2 v))))
((= j 1) (sext 16 (ash (gte-d (* 2 v)) -16)))
(t (sext 16 (gte-d (+ (* 2 v) 1))))))(defun gte-tr) source(defun gte-tr (cpu cv i)
"component I (1-3) of translation vector CV: 0 TR, 1 BK, 2 FC, 3 none (0)"
(declare (type mips cpu) (type (unsigned-byte 2) cv i))
(if (= cv 3) 0 (sext 32 (gte-c (+ 4 (* 8 cv) i)))))(defun gte-rgbc) source(defun gte-rgbc (cpu i)
"component I (1 R, 2 G, 3 B) of RGBC"
(declare (type mips cpu) (type (unsigned-byte 2) i))
(bit-field (gte-d 6) (* 8 (- i 1)) 8))(defun gte-dot3) source(defun gte-dot3 (cpu i tr m1 m2 m3 x y z)
"TR*1000h + M1*X + M2*Y + M3*Z, each partial sum checked as MACi's (\"MVMVA\")"
(declare (type mips cpu) (type (unsigned-byte 2) i) (type (signed-byte 32) tr)
(type (signed-byte 17) m1 m2 m3) (type (signed-byte 16) x y z))
(+ (gte-mac-check cpu i (+ (gte-mac-check cpu i (+ (ash tr 12) (* m1 x))) (* m2 y))) (* m3 z)))(defun gte-mvm) source(defun gte-mvm (cpu mx cv x y z sf lm)
"[IR1,IR2,IR3] = [MAC1,MAC2,MAC3] = (Tx*1000h + Mx*[X,Y,Z]) SAR (sf*12)"
(declare (type mips cpu) (type (unsigned-byte 2) mx cv) (type (signed-byte 16) x y z)
(type (unsigned-byte 1) sf lm))
(gte-set-mac cpu 1 (gte-dot3 cpu 1 (gte-tr cpu cv 1) (gte-mat cpu mx 0) (gte-mat cpu mx 1) (gte-mat cpu mx 2) x y z) sf)
(gte-set-mac cpu 2 (gte-dot3 cpu 2 (gte-tr cpu cv 2) (gte-mat cpu mx 3) (gte-mat cpu mx 4) (gte-mat cpu mx 5) x y z) sf)
(gte-set-mac cpu 3 (gte-dot3 cpu 3 (gte-tr cpu cv 3) (gte-mat cpu mx 6) (gte-mat cpu mx 7) (gte-mat cpu mx 8) x y z) sf)
(gte-ir-from-mac cpu lm))(defparameter *gte-unr*) source(defparameter *gte-unr*
(let ((a (make-array 257 :element-type '(unsigned-byte 8))))
(dotimes (i 257 a)
(setf (aref a i) (max 0 (- (floor (+ (floor #x40000 (+ i #x100)) 1) 2) #x101))))))(defun gte-divide) source(defun gte-divide (cpu h sz3)
"H / SZ3 as RTPS/RTPT compute it (17 bits, 16 of fraction), FLAG 17 on overflow:
if (H < SZ3*2) then z = count_leading_zeroes(SZ3); n = (H SHL z); d =
(SZ3 SHL z); u = unr_table[(d-7FC0h) SHR 7] + 101h; d = ((2000080h - (d
* u)) SHR 8); d = ((0000080h + (d * u)) SHR 8); n = min(1FFFFh, (((n*d)
+ 8000h) SHR 16)) else n = 1FFFFh, FLAG.Bit17=1"
(declare (type mips cpu) (type (unsigned-byte 16) h sz3))
(if (< h (* sz3 2))
(let* ((z (clz 16 sz3))
(n (ash h z))
(d (ash sz3 z))
(u (+ (aref *gte-unr* (ash (- d #x7FC0) -7)) #x101))
(d1 (ash (- #x2000080 (* d u)) -8))
(d2 (ash (+ #x80 (* d1 u)) -8))
(q (ash (+ (* n d2) #x8000) -16)))
(if (> q #x1FFFF) #x1FFFF q))
(progn (gte-setflag 17) #x1FFFF)))(defun gte-rtp) source(defun gte-rtp (cpu v sf lm dq)
"RTPS on vector V (and the depth cue with DQ):
IR1 = MAC1 = (TRX*1000h + RT11*VX0 + RT12*VY0 + RT13*VZ0) SAR (sf*12) ...
SZ3 = MAC3 SAR ((1-sf)*12); MAC0=(((H*20000h/SZ3)+1)/2)*IR1+OFX, SX2=MAC0/10000h ...
MAC0=(((H*20000h/SZ3)+1)/2)*DQA+DQB, IR0=MAC0/1000h. IR3's flag is
checked as with lm=0 on MAC3 SAR 12 (\"The IR3 saturation flag (FLAG.22)
is always checked as if lm=0\"), its value from MAC3 with lm."
(declare (type mips cpu) (type (unsigned-byte 2) v) (type (unsigned-byte 1) sf lm dq))
(let ((x (gte-vec cpu v 0)) (y (gte-vec cpu v 1)) (z (gte-vec cpu v 2)))
(gte-set-mac cpu 1 (gte-dot3 cpu 1 (gte-tr cpu 0 1) (gte-mat cpu 0 0) (gte-mat cpu 0 1) (gte-mat cpu 0 2) x y z) sf)
(gte-set-mac cpu 2 (gte-dot3 cpu 2 (gte-tr cpu 0 2) (gte-mat cpu 0 3) (gte-mat cpu 0 4) (gte-mat cpu 0 5) x y z) sf)
(let* ((s3 (gte-dot3 cpu 3 (gte-tr cpu 0 3) (gte-mat cpu 0 6) (gte-mat cpu 0 7) (gte-mat cpu 0 8) x y z))
(z12 (sext 32 (ash s3 -12))))
(gte-set-mac cpu 3 s3 sf)
(gte-set-ir cpu 1 (gte-mac cpu 1) lm)
(gte-set-ir cpu 2 (gte-mac cpu 2) lm)
(when (or (< z12 #x-8000) (> z12 #x7FFF)) (gte-setflag 22))
(setf (gte-d 11) (wrap 32 (saturate (if (= lm 1) 0 #x-8000) #x7FFF (gte-mac cpu 3))))
;; the SZ FIFO: SZ3 saturated to 0..FFFFh (FLAG 18)
(setf (gte-d 16) (gte-d 17)
(gte-d 17) (gte-d 18)
(gte-d 18) (gte-d 19)
(gte-d 19) (cond ((< z12 0) (gte-setflag 18) 0)
((> z12 #xFFFF) (gte-setflag 18) #xFFFF)
(t z12))))
(let* ((q (gte-divide cpu (wrap 16 (gte-c 26)) (gte-d 19)))
(sx (ash (gte-mac0 cpu (+ (* q (gte-ir cpu 1)) (sext 32 (gte-c 24)))) -16))
(sy (ash (gte-mac0 cpu (+ (* q (gte-ir cpu 2)) (sext 32 (gte-c 25)))) -16)))
;; the SXY FIFO: SX2, SY2 saturated to -400h..3FFh (FLAG 14, 13)
(setf (gte-d 12) (gte-d 13)
(gte-d 13) (gte-d 14)
(gte-d 14) (logior (wrap 16 (cond ((< sx #x-400) (gte-setflag 14) #x-400)
((> sx #x3FF) (gte-setflag 14) #x3FF)
(t sx)))
(ash (wrap 16 (cond ((< sy #x-400) (gte-setflag 13) #x-400)
((> sy #x3FF) (gte-setflag 13) #x3FF)
(t sy)))
16)))
(when (= dq 1)
(let* ((m0 (gte-mac0 cpu (+ (* q (sext 16 (gte-c 27))) (sext 32 (gte-c 28)))))
(i0 (ash m0 -12)))
;; IR0 saturated to 0..1000h (FLAG 12)
(setf (gte-d 24) (wrap 32 m0)
(gte-d 8) (cond ((< i0 0) (gte-setflag 12) 0)
((> i0 #x1000) (gte-setflag 12) #x1000)
(t i0))))))))(defun gte-nclip) source(defun gte-nclip (cpu)
"MAC0 = SX0*SY1 + SX1*SY2 + SX2*SY0 - SX0*SY2 - SX1*SY0 - SX2*SY1"
(declare (type mips cpu))
(let ((x0 (sext 16 (gte-d 12))) (y0 (sext 16 (ash (gte-d 12) -16)))
(x1 (sext 16 (gte-d 13))) (y1 (sext 16 (ash (gte-d 13) -16)))
(x2 (sext 16 (gte-d 14))) (y2 (sext 16 (ash (gte-d 14) -16))))
(setf (gte-d 24) (wrap 32 (gte-mac0 cpu (- (+ (* x0 y1) (* x1 y2) (* x2 y0))
(+ (* x0 y2) (* x1 y0) (* x2 y1))))))))(defun gte-avsz) source(defun gte-avsz (cpu zsf sum)
"MAC0 = ZSFn*(SZ...); OTZ = MAC0/1000h (saturated to 0..FFFFh, FLAG 18)"
(declare (type mips cpu) (type (signed-byte 16) zsf) (type (unsigned-byte 18) sum))
(let* ((m0 (gte-mac0 cpu (* zsf sum))) (o (sext 32 (ash m0 -12))))
(setf (gte-d 24) (wrap 32 m0)
(gte-d 7) (cond ((< o 0) (gte-setflag 18) 0)
((> o #xFFFF) (gte-setflag 18) #xFFFF)
(t o)))))(defun gte-mvmva) source(defun gte-mvmva (cpu sf mx v cv lm)
"MAC1 = (Tx1*1000h + Mx11*Vx1 + Mx12*Vx2 + Mx13*Vx3) SAR (sf*12) ...
[IR1,IR2,IR3] = [MAC1,MAC2,MAC3]; with cv=2 (FC, \"not added correctly
by the hardware\") MAC1=(Mx12*Vx2+Mx13*Vx3) SAR (sf*12) ..., the flags
\"as if the full operation would have been executed\": those of
Tx1*1000h + Mx11*Vx1 and of its IR saturation (as lm=0)"
(declare (type mips cpu) (type (unsigned-byte 1) sf lm) (type (unsigned-byte 2) mx v cv))
(let ((x (gte-vec cpu v 0)) (y (gte-vec cpu v 1)) (z (gte-vec cpu v 2)))
(if (= cv 2)
(progn
(gte-sat-ir cpu 1 (sext 32 (ash (gte-mac-check cpu 1 (+ (ash (gte-tr cpu 2 1) 12) (* (gte-mat cpu mx 0) x)))
(- (* 12 sf))))
0)
(gte-sat-ir cpu 2 (sext 32 (ash (gte-mac-check cpu 2 (+ (ash (gte-tr cpu 2 2) 12) (* (gte-mat cpu mx 3) x)))
(- (* 12 sf))))
0)
(gte-sat-ir cpu 3 (sext 32 (ash (gte-mac-check cpu 3 (+ (ash (gte-tr cpu 2 3) 12) (* (gte-mat cpu mx 6) x)))
(- (* 12 sf))))
0)
(gte-set-mac cpu 1 (+ (gte-mac-check cpu 1 (* (gte-mat cpu mx 1) y)) (* (gte-mat cpu mx 2) z)) sf)
(gte-set-mac cpu 2 (+ (gte-mac-check cpu 2 (* (gte-mat cpu mx 4) y)) (* (gte-mat cpu mx 5) z)) sf)
(gte-set-mac cpu 3 (+ (gte-mac-check cpu 3 (* (gte-mat cpu mx 7) y)) (* (gte-mat cpu mx 8) z)) sf)
(gte-ir-from-mac cpu lm))
(gte-mvm cpu mx cv x y z sf lm))))(defun gte-sqr) source(defun gte-sqr (cpu sf lm)
"[MAC1,MAC2,MAC3] = [IR1*IR1,IR2*IR2,IR3*IR3] SHR (sf*12); [IR1,IR2,IR3] = [MAC1,MAC2,MAC3]"
(declare (type mips cpu) (type (unsigned-byte 1) sf lm))
(gte-set-mac cpu 1 (* (gte-ir cpu 1) (gte-ir cpu 1)) sf)
(gte-set-mac cpu 2 (* (gte-ir cpu 2) (gte-ir cpu 2)) sf)
(gte-set-mac cpu 3 (* (gte-ir cpu 3) (gte-ir cpu 3)) sf)
(gte-ir-from-mac cpu lm))(defun gte-op) source(defun gte-op (cpu sf lm)
"[MAC1,MAC2,MAC3] = [IR3*D2-IR2*D3, IR1*D3-IR3*D1, IR2*D1-IR1*D2] SAR (sf*12),
D1-D3 = RT11, RT22, RT33; [IR1,IR2,IR3] = [MAC1,MAC2,MAC3]"
(declare (type mips cpu) (type (unsigned-byte 1) sf lm))
(let ((d1 (gte-mat cpu 0 0)) (d2 (gte-mat cpu 0 4)) (d3 (gte-mat cpu 0 8))
(i1 (gte-ir cpu 1)) (i2 (gte-ir cpu 2)) (i3 (gte-ir cpu 3)))
(gte-set-mac cpu 1 (- (* i3 d2) (* i2 d3)) sf)
(gte-set-mac cpu 2 (- (* i1 d3) (* i3 d1)) sf)
(gte-set-mac cpu 3 (- (* i2 d1) (* i1 d2)) sf)
(gte-ir-from-mac cpu lm)))(defun gte-gpf) source(defun gte-gpf (cpu sf lm)
"[MAC1,MAC2,MAC3] = ([IR1,IR2,IR3] * IR0) SAR (sf*12); Color FIFO, IR"
(declare (type mips cpu) (type (unsigned-byte 1) sf lm))
(gte-set-mac cpu 1 (* (gte-ir cpu 1) (gte-ir cpu 0)) sf)
(gte-set-mac cpu 2 (* (gte-ir cpu 2) (gte-ir cpu 0)) sf)
(gte-set-mac cpu 3 (* (gte-ir cpu 3) (gte-ir cpu 0)) sf)
(gte-ir-from-mac cpu lm)
(gte-push-color cpu))(defun gte-gpl) source(defun gte-gpl (cpu sf lm)
"[MAC1,MAC2,MAC3] = (([IR1,IR2,IR3] * IR0) + [MAC1,MAC2,MAC3] SHL (sf*12)) SAR (sf*12)"
(declare (type mips cpu) (type (unsigned-byte 1) sf lm))
(gte-set-mac cpu 1 (+ (ash (gte-mac cpu 1) (* 12 sf)) (* (gte-ir cpu 1) (gte-ir cpu 0))) sf)
(gte-set-mac cpu 2 (+ (ash (gte-mac cpu 2) (* 12 sf)) (* (gte-ir cpu 2) (gte-ir cpu 0))) sf)
(gte-set-mac cpu 3 (+ (ash (gte-mac cpu 3) (* 12 sf)) (* (gte-ir cpu 3) (gte-ir cpu 0))) sf)
(gte-ir-from-mac cpu lm)
(gte-push-color cpu))(defun gte-interp) source(defun gte-interp (cpu m1 m2 m3 sf lm)
"\"[MAC1,MAC2,MAC3] = MAC+(FC-MAC)*IR0\" then SAR (sf*12), M1-M3 the MAC
values before the shift: [IR1,IR2,IR3] = (([RFC,GFC,BFC] SHL 12) -
[MAC1,MAC2,MAC3]) SAR (sf*12), saturated as if lm=0; [MAC1,MAC2,MAC3] =
(([IR1,IR2,IR3] * IR0) + [MAC1,MAC2,MAC3]) SAR (sf*12); IR from MAC"
(declare (type mips cpu) (type (signed-byte 48) m1 m2 m3) (type (unsigned-byte 1) sf lm))
(gte-set-mac cpu 1 (- (ash (gte-tr cpu 2 1) 12) m1) sf)
(gte-set-mac cpu 2 (- (ash (gte-tr cpu 2 2) 12) m2) sf)
(gte-set-mac cpu 3 (- (ash (gte-tr cpu 2 3) 12) m3) sf)
(gte-ir-from-mac cpu 0)
(gte-set-mac cpu 1 (+ (* (gte-ir cpu 1) (gte-ir cpu 0)) m1) sf)
(gte-set-mac cpu 2 (+ (* (gte-ir cpu 2) (gte-ir cpu 0)) m2) sf)
(gte-set-mac cpu 3 (+ (* (gte-ir cpu 3) (gte-ir cpu 0)) m3) sf)
(gte-ir-from-mac cpu lm))(defun gte-dpc) source(defun gte-dpc (cpu r g b sf lm)
"DPCS/DPCT on the colour R G B: [MAC1,MAC2,MAC3] = [R,G,B] SHL 16, interpolated, Color FIFO"
(declare (type mips cpu) (type (unsigned-byte 8) r g b) (type (unsigned-byte 1) sf lm))
(gte-interp cpu (ash r 16) (ash g 16) (ash b 16) sf lm)
(gte-push-color cpu))(defun gte-intpl) source(defun gte-intpl (cpu sf lm)
"[MAC1,MAC2,MAC3] = [IR1,IR2,IR3] SHL 12, interpolated, Color FIFO"
(declare (type mips cpu) (type (unsigned-byte 1) sf lm))
(gte-interp cpu (ash (gte-ir cpu 1) 12) (ash (gte-ir cpu 2) 12) (ash (gte-ir cpu 3) 12) sf lm)
(gte-push-color cpu))(defun gte-dcpl) source(defun gte-dcpl (cpu sf lm)
"[MAC1,MAC2,MAC3] = [R*IR1,G*IR2,B*IR3] SHL 4, interpolated, Color FIFO"
(declare (type mips cpu) (type (unsigned-byte 1) sf lm))
(gte-interp cpu (ash (* (gte-rgbc cpu 1) (gte-ir cpu 1)) 4) (ash (* (gte-rgbc cpu 2) (gte-ir cpu 2)) 4)
(ash (* (gte-rgbc cpu 3) (gte-ir cpu 3)) 4) sf lm)
(gte-push-color cpu))(defun gte-nc) source(defun gte-nc (cpu v sf lm kind)
"the normal colour commands on vector V (0-2), from the light step, or
CC/CDP from the colour step (V = 3): [IR1,IR2,IR3] = [MAC1,MAC2,MAC3] =
(LLM*V0) SAR (sf*12); [IR1,IR2,IR3] = [MAC1,MAC2,MAC3] = (BK*1000h +
LCM*IR) SAR (sf*12); then KIND 0 (NCS): Color FIFO; 1 (NCCS, CC):
[MAC1,MAC2,MAC3] = [R*IR1,G*IR2,B*IR3] SHL 4, SAR (sf*12), Color FIFO, IR;
2 (NCDS, CDP): the same interpolated with FC"
(declare (type mips cpu) (type (unsigned-byte 2) v kind) (type (unsigned-byte 1) sf lm))
(unless (= v 3)
(gte-mvm cpu 1 3 (gte-vec cpu v 0) (gte-vec cpu v 1) (gte-vec cpu v 2) sf lm))
(gte-mvm cpu 2 1 (gte-ir cpu 1) (gte-ir cpu 2) (gte-ir cpu 3) sf lm)
(let ((m1 (ash (* (gte-rgbc cpu 1) (gte-ir cpu 1)) 4))
(m2 (ash (* (gte-rgbc cpu 2) (gte-ir cpu 2)) 4))
(m3 (ash (* (gte-rgbc cpu 3) (gte-ir cpu 3)) 4)))
(cond ((= kind 1)
(gte-set-mac cpu 1 m1 sf)
(gte-set-mac cpu 2 m2 sf)
(gte-set-mac cpu 3 m3 sf)
(gte-ir-from-mac cpu lm))
((= kind 2)
(gte-interp cpu m1 m2 m3 sf lm))))
(gte-push-color cpu))(defmacro gte-command) source(defmacro gte-command (&body body)
`(if (r3k-cop2-usable)
(progn (setf (gte-c 31) 0)
,@body
(setf (gte-c 31) (gte-flag-word (gte-c 31))))
(raise-cpu 2)))Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.