the GS's privileged registers: the display circuits, CSR, IMR, SIGLBLID; the picture shown.
(defconstant +gs-csr-signal+) source(defconstant +gs-csr-signal+ #x1)(defconstant +gs-csr-finish+) source(defconstant +gs-csr-finish+ #x2)(defconstant +gs-csr-vsint+) source(defconstant +gs-csr-vsint+ #x8)(defun gs-priv-index) source(defun gs-priv-index (addr)
"the register index of the privileged address ADDR (an offset from #x12000000)"
(declare (type (unsigned-byte 32) addr))
(if (< addr #x1000) (ldb (byte 4 4) addr) (+ 16 (ldb (byte 4 4) addr))))(defun gs-priv-write) source(defun gs-priv-write (g addr value bits)
"the EE writes BITS (32 or 64) of VALUE at ADDR (an offset from #x12000000; a 32-bit write
at +4 is the register's upper half)"
(declare (type gsm g) (type (unsigned-byte 32) addr) (type (unsigned-byte 64) value) (type (unsigned-byte 8) bits))
(let* ((i (gs-priv-index addr)) (old (aref (gsm-priv g) i))
(v (cond ((= bits 64) value)
((logbitp 2 addr) (logior (logand old #xFFFFFFFF) (ash (ldb (byte 32 0) value) 32)))
(t (logior (logand old #xFFFFFFFF00000000) (ldb (byte 32 0) value))))))
(declare (type (unsigned-byte 64) v))
(cond ((= i 16) ; CSR
(unless (logbitp 2 addr)
(when (logbitp 9 value)
(gs-reset g)
(return-from gs-priv-write nil))
;; the event bits clear where 1 is written; the rest is the GS's
(setf (gsm-csr g) (logand old (lognot (logand value #x1F)) #xFFFFFFFFFFFFFFFF))))
(t (setf (aref (gsm-priv g) i) v))))
nil)(defun gs-priv-read) source(defun gs-priv-read (g addr)
"the value read at ADDR (an offset from #x12000000), 64 bits"
(declare (type gsm g) (type (unsigned-byte 32) addr))
(aref (gsm-priv g) (gs-priv-index addr)))(defun gs-vsync) source(defun gs-vsync (g)
"a vertical blank begins: CSR.VSINT, the field changes"
(declare (type gsm g))
(let ((csr (gsm-csr g)))
(setf (gsm-csr g) (logxor (logior csr +gs-csr-vsint+) #x2000)))
nil)(defun gs-interrupt-p) source(defun gs-interrupt-p (g)
"is an event of CSR (SIGNAL FINISH HSINT VSINT EDWINT) pending and not masked by IMR"
(declare (type gsm g))
(/= (logand (gsm-csr g) (lognot (ldb (byte 5 8) (gsm-imr g))) #x1F) 0))(defun gs-circuit) source(defun gs-circuit (g)
"the circuit read for the picture: 2 when EN2, else 1 when EN1, else 0"
(declare (type gsm g))
(let ((pmode (aref (gsm-priv g) 0)))
(cond ((logbitp 1 pmode) 2) ((logbitp 0 pmode) 1) (t 0))))(defun gs-display-size) source(defun gs-display-size (g)
"the picture's size: width in the low 16 bits, height above (0 when no circuit is on)"
(declare (type gsm g))
(let ((c (gs-circuit g)))
(if (= c 0)
0
(let* ((disp (aref (gsm-priv g) (if (= c 2) 10 8)))
(w (floor (+ 1 (gsf disp 32 12)) (+ 1 (gsf disp 23 4))))
(h (floor (+ 1 (gsf disp 44 11)) (+ 1 (gsf disp 27 2))))
(smode2 (aref (gsm-priv g) 2)))
(when (and (logbitp 0 smode2) (logbitp 1 smode2))
(setq h (ash h -1)))
(logior (min w 2048) (ash (min h 2048) 16))))))(defun gs-circuit-pixel) source(defun gs-circuit-pixel (g dispfb x y)
"pixel (X, Y) of the picture of the circuit reading DISPFB, as A8B8G8R8"
(declare (type gsm g) (type (unsigned-byte 64) dispfb) (type (unsigned-byte 32) x y))
(let ((psm (gsf dispfb 15 5)))
(gs-color32 psm (gs-read-pixel g psm (* 32 (gsf dispfb 0 9)) (gsf dispfb 9 6)
(logand (+ (gsf dispfb 32 11) x) 2047) (logand (+ (gsf dispfb 43 11) y) 2047)))))(defun gs-display-pixel) source(defun gs-display-pixel (g x y)
"pixel (X, Y) of the picture shown, as A8B8G8R8"
(declare (type gsm g) (type (unsigned-byte 32) x y))
(let* ((pmode (aref (gsm-priv g) 0)) (en1 (logbitp 0 pmode)) (en2 (logbitp 1 pmode)))
(cond ((and en1 en2)
(let* ((c1 (gs-circuit-pixel g (aref (gsm-priv g) 7) x y))
(c2 (gs-circuit-pixel g (aref (gsm-priv g) 9) x y))
(a (min 255 (* 2 (if (logbitp 5 pmode) (gsf pmode 8 8) (ldb (byte 8 24) c1)))))
(out 0))
(declare (type (unsigned-byte 32) out))
(dotimes (k 3)
(let ((sh (* 8 k)))
(setq out (logior out (ash (floor (+ (* a (ldb (byte 8 sh) c1)) (* (- 255 a) (ldb (byte 8 sh) c2))) 255)
sh)))))
out))
(en2 (gs-circuit-pixel g (aref (gsm-priv g) 9) x y))
(en1 (gs-circuit-pixel g (aref (gsm-priv g) 7) x y))
(t 0))))Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.