the VIF: VIF codes as an instruction set on a vector unit's memories and registers.
(defconstant +vif-stat+) source(defconstant +vif-stat+ 0)(defconstant +vif-mark+) source(defconstant +vif-mark+ 3)(defconstant +vif-cycle+) source(defconstant +vif-cycle+ 4)(defconstant +vif-mode+) source(defconstant +vif-mode+ 5)(defconstant +vif-num+) source(defconstant +vif-num+ 6)(defconstant +vif-mask+) source(defconstant +vif-mask+ 7)(defconstant +vif-code+) source(defconstant +vif-code+ 8)(defconstant +vif-itops+) source(defconstant +vif-itops+ 9)(defconstant +vif-base+) source(defconstant +vif-base+ 10)(defconstant +vif-ofst+) source(defconstant +vif-ofst+ 11)(defconstant +vif-tops+) source(defconstant +vif-tops+ 12)(defconstant +vif-itop+) source(defconstant +vif-itop+ 13)(defconstant +vif-top+) source(defconstant +vif-top+ 14)(defconstant +vif-r0+) source(defconstant +vif-r0+ 16)(defconstant +vif-c0+) source(defconstant +vif-c0+ 20)(defmacro vreg) source(defmacro vreg (v i) `(aref (vif-regs ,v) ,i))(defun vif-reg-index) source(defun vif-reg-index (offset)
"the register index of OFFSET from the VIF's register base"
(declare (type (unsigned-byte 32) offset))
(ldb (byte 5 4) offset))(defun vif-direct-qword) source(defun vif-direct-qword (v lo hi)
"a quadword of DIRECT/DIRECTHL for the GIF (the machine's PATH2)"
(declare (type vif v) (type (unsigned-byte 64) lo hi) (ignore v lo hi))
nil)(defun vif-start-micro) source(defun vif-start-micro (v u pc)
"MSCAL/MSCALF/MSCNT: start U's micro program at PC (bytes)"
(declare (type vif v) (type vu u) (type (unsigned-byte 32) pc) (ignore v))
(vu-run u pc)
nil)(defun vif-raise) source(defun vif-raise (v)
"a VIF code with the interrupt bit has run"
(declare (type vif v) (ignore v))
nil)(defun vif-mskpath3) source(defun vif-mskpath3 (v on)
(declare (type vif v) (type (unsigned-byte 1) on) (ignore v on))
nil)(defun vif-unpack-bits) source(defun vif-unpack-bits (cmd)
"the bits of one packed vector of UNPACK CMD"
(declare (type (unsigned-byte 32) cmd))
(let ((vn (ldb (byte 2 2) cmd)) (vl (ldb (byte 2 0) cmd)))
(if (= vl 3) 16 (* (+ vn 1) (ash 32 (- vl))))))(defun vif-unpack-words) source(defun vif-unpack-words (v cmd num)
"the data words of UNPACK CMD writing NUM quadwords with the current CYCLE"
(declare (type vif v) (type (unsigned-byte 32) cmd num))
(let* ((cl (ldb (byte 8 0) (vreg v +vif-cycle+))) (wl (max 1 (ldb (byte 8 8) (vreg v +vif-cycle+))))
(vectors (if (>= cl wl) num (+ (* (floor num wl) cl) (min (mod num wl) cl)))))
(ceiling (* vectors (vif-unpack-bits cmd)) 32)))(defun vif-data-element) source(defun vif-data-element (v byte bits usn)
"the element of BITS at BYTE of UNPACK's data, extended to 32 bits"
(declare (type vif v) (type (unsigned-byte 32) byte) (type (unsigned-byte 8) bits) (type (unsigned-byte 1) usn))
(let* ((w (aref (vif-buf v) (logand (ash byte -2) 1023)))
(e (ldb (byte bits (* 8 (logand byte 3))) w)))
(cond ((= bits 32) e)
((= usn 1) e)
((logbitp (- bits 1) e) (wrap 32 (- e (ash 1 bits))))
(t e))))(defun vif-write-element) source(defun vif-write-element (v u addr k data cl doMask mode)
"element K of quadword ADDR of U's data memory, from DATA, by MASK and MODE"
(declare (type vif v) (type vu u) (type (unsigned-byte 32) addr data) (type (unsigned-byte 8) k cl mode)
(type (unsigned-byte 1) doMask))
(let* ((m (if (= doMask 1) (ldb (byte 2 (+ (* 8 (min cl 3)) (* 2 k))) (vreg v +vif-mask+)) 0))
(a (+ (vu-data-base u) (logand (+ (* 16 addr) (* 4 k)) (vu-data-mask u))))
(row (+ +vif-r0+ k)))
(case m
(0 (mem-write 32 a (bswap 32 (case mode
(1 (wrap 32 (+ data (vreg v row))))
(2 (setf (vreg v row) (wrap 32 (+ (vreg v row) data))) (vreg v row))
(3 (setf (vreg v row) data) data)
(t data)))))
(1 (mem-write 32 a (bswap 32 (vreg v row))))
(2 (mem-write 32 a (bswap 32 (vreg v (+ +vif-c0+ (min cl 3))))))
(t nil)))
nil)(defun vif-unpack) source(defun vif-unpack (v u)
"UNPACK, its data in BUF"
(declare (type vif v) (type vu u))
(let* ((cmd (vif-cmd v)) (imm (ldb (byte 16 0) cmd)) (code (ldb (byte 7 24) cmd))
(vn (ldb (byte 2 2) code)) (vl (ldb (byte 2 0) code)) (domask (ldb (byte 1 4) code))
(usn (ldb (byte 1 14) imm))
(num (let ((n (ldb (byte 8 16) cmd))) (if (= n 0) 256 n)))
(cl (ldb (byte 8 0) (vreg v +vif-cycle+))) (wl (max 1 (ldb (byte 8 8) (vreg v +vif-cycle+))))
(filling (< cl wl)) (mode (if (and (= vn 3) (= vl 3)) 0 (logand (vreg v +vif-mode+) 3)))
(bits (if (= vl 3) 16 (ash 32 (- vl)))) (vbytes (ash (vif-unpack-bits code) -3))
(addr (logand (+ (ldb (byte 10 0) imm) (if (and (= (vif-id v) 1) (logbitp 15 imm)) (vreg v +vif-tops+) 0))
#x3FF))
(data 0) (c 0))
(declare (type (unsigned-byte 32) addr data c))
(setf (vif-unpacks v) (wrap 64 (+ (vif-unpacks v) 1)))
(dotimes (i num)
(if (and (= vn 3) (= vl 3))
(let ((h (vif-data-element v data 16 1)))
(vif-write-element v u addr 0 (ash (ldb (byte 5 0) h) 3) c domask 0)
(vif-write-element v u addr 1 (ash (ldb (byte 5 5) h) 3) c domask 0)
(vif-write-element v u addr 2 (ash (ldb (byte 5 10) h) 3) c domask 0)
(vif-write-element v u addr 3 (ash (ldb (byte 1 15) h) 7) c domask 0))
(let* ((eb (ash bits -3))
(e0 (vif-data-element v data bits usn))
(e1 (if (= vn 0) e0 (vif-data-element v (+ data eb) bits usn)))
(e2 (case vn (0 e0) (1 e0) (t (vif-data-element v (+ data (* 2 eb)) bits usn))))
(e3 (case vn (0 e0) (1 e1) (t (vif-data-element v (+ data (* 3 eb)) bits usn)))))
(vif-write-element v u addr 0 e0 c domask mode)
(vif-write-element v u addr 1 e1 c domask mode)
(vif-write-element v u addr 2 e2 c domask mode)
(vif-write-element v u addr 3 e3 c domask mode)))
(setq addr (logand (+ addr 1) #x3FF) c (+ c 1))
(if filling
(progn
(when (<= c cl) (setq data (+ data vbytes)))
(when (= c wl) (setq c 0)))
(progn
(setq data (+ data vbytes))
(when (>= c wl)
(setq addr (logand (+ addr (- cl wl)) #x3FF) c 0)))))
(setf (vreg v +vif-num+) 0))
nil)(defun vif-micro) source(defun vif-micro (v u pc)
"MSCAL / MSCALF / MSCNT: ITOP <- ITOPS; VIF1's double buffer: TOP <- TOPS, DBF flips, TOPS
<- BASE (+ OFST when DBF is now set); then the program"
(declare (type vif v) (type vu u) (type (unsigned-byte 32) pc))
(setf (vreg v +vif-itop+) (vreg v +vif-itops+))
(when (= (vif-id v) 1)
(setf (vreg v +vif-top+) (logand (vreg v +vif-tops+) #x3FF))
(if (logbitp 7 (vreg v +vif-stat+))
(setf (vreg v +vif-tops+) (vreg v +vif-base+)
(vreg v +vif-stat+) (logand (vreg v +vif-stat+) (lognot #x80)))
(setf (vreg v +vif-tops+) (logand (+ (vreg v +vif-base+) (vreg v +vif-ofst+)) #x3FF)
(vreg v +vif-stat+) (logior (vreg v +vif-stat+) #x80))))
(vif-start-micro v u pc)
nil)(defun vif-code) source(defun vif-code (v u w)
"a VIF code: run it, or note the data words it takes"
(declare (type vif v) (type vu u) (type (unsigned-byte 32) w))
(let ((code (ldb (byte 7 24) w)) (imm (ldb (byte 16 0) w)) (num (ldb (byte 8 16) w)))
(setf (vreg v +vif-code+) w (vif-cmd v) w (vif-pos v) 0 (vif-left v) 0
(vif-codes v) (wrap 64 (+ (vif-codes v) 1)))
(cond
((= code #x00))
((= code #x01) (setf (vreg v +vif-cycle+) imm))
((= code #x02) (setf (vreg v +vif-ofst+) (logand imm #x3FF)
(vreg v +vif-stat+) (logand (vreg v +vif-stat+) (lognot #x80))
(vreg v +vif-tops+) (vreg v +vif-base+)))
((= code #x03) (setf (vreg v +vif-base+) (logand imm #x3FF)))
((= code #x04) (setf (vreg v +vif-itops+) (logand imm #x3FF)))
((= code #x05) (setf (vreg v +vif-mode+) (logand imm 3)))
((= code #x06) (vif-mskpath3 v (ldb (byte 1 15) imm)))
((= code #x07) (setf (vreg v +vif-mark+) imm (vreg v +vif-stat+) (logior (vreg v +vif-stat+) #x40)))
((or (= code #x10) (= code #x11) (= code #x13)))
((or (= code #x14) (= code #x15)) (vif-micro v u (* 8 imm)))
((= code #x17) (vif-micro v u (vu-tpc u)))
((= code #x20) (setf (vif-left v) 1))
((or (= code #x30) (= code #x31)) (setf (vif-left v) 4))
((= code #x4A) (setf (vif-left v) (* 2 (if (= num 0) 256 num)) (vif-addr v) (* 8 imm)))
((or (= code #x50) (= code #x51)) (setf (vif-left v) (* 4 (if (= imm 0) 65536 imm))))
((>= code #x60)
(let ((n (if (= num 0) 256 num)))
(setf (vreg v +vif-num+) num
(vif-left v) (vif-unpack-words v code n))
(when (= (vif-left v) 0)
(vif-unpack v u))))
(t (setf (vif-unknown v) (wrap 32 (+ (vif-unknown v) 1)))))
(when (and (logbitp 31 w) (= (vif-left v) 0))
(vif-raise v)))
nil)(defun vif-data) source(defun vif-data (v u w)
"a data word of the current code"
(declare (type vif v) (type vu u) (type (unsigned-byte 32) w))
(let ((code (ldb (byte 7 24) (vif-cmd v))) (pos (vif-pos v)))
(cond
((= code #x20) (setf (vreg v +vif-mask+) w))
((= code #x30) (setf (vreg v (+ +vif-r0+ (logand pos 3))) w))
((= code #x31) (setf (vreg v (+ +vif-c0+ (logand pos 3))) w))
((= code #x4A) (mem-write 32 (+ (vu-micro-base u) (logand (vif-addr v) (vu-micro-mask u))) (bswap 32 w))
(setf (vif-addr v) (wrap 32 (+ (vif-addr v) 4))))
((or (= code #x50) (= code #x51))
(setf (aref (vif-qacc v) (logand pos 3)) w)
(when (= (logand pos 3) 3)
(vif-direct-qword v (logior (aref (vif-qacc v) 0) (ash (aref (vif-qacc v) 1) 32))
(logior (aref (vif-qacc v) 2) (ash (aref (vif-qacc v) 3) 32)))))
(t (setf (aref (vif-buf v) (logand pos 1023)) w)))
(setf (vif-pos v) (+ pos 1) (vif-left v) (- (vif-left v) 1))
(when (= (vif-left v) 0)
(when (>= code #x60)
(vif-unpack v u))
(when (logbitp 31 (vif-cmd v))
(vif-raise v))))
nil)(defun vif-word) source(defun vif-word (v u w)
"the next word of the VIF's stream"
(declare (type vif v) (type vu u) (type (unsigned-byte 32) w))
(if (> (vif-left v) 0)
(vif-data v u w)
(vif-code v u w))
nil)Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.