the PS1 GPU as a machine: VRAM, GPUSTAT, the command FIFO, transfers, the display.
(defconstant +gpu-vram-words+) source(defconstant +gpu-vram-words+ 524288)(defstruct gpu) source(defstruct (gpu (:conc-name gpu-))
(vram (make-array 524288 :element-type '(unsigned-byte 16) :initial-element 0)
:type (simple-array (unsigned-byte 16) (524288)))
(gfx (make-gfx) :type gfx)
(stat #x14802000 :type u32)
(fifo (make-array 16 :element-type '(unsigned-byte 32) :initial-element 0)
:type (simple-array (unsigned-byte 32) (16)))
(count 0 :type u8)
(need 0 :type u8)
(mode 0 :type u8)
;; a polyline: its flags (the command word), the last vertex and colour
(line-cmd 0 :type u32)
(line-xy 0 :type u32)
(line-color 0 :type u32)
(line-color-next 0 :type u32)
(line-have-color 0 :type u8)
;; CPU-to-VRAM
(xfer-x 0 :type u16) (xfer-y 0 :type u16) (xfer-w 0 :type u16) (xfer-h 0 :type u16)
(xfer-i 0 :type u32) (xfer-n 0 :type u32)
;; VRAM-to-CPU
(rd-x 0 :type u16) (rd-y 0 :type u16) (rd-w 0 :type u16) (rd-h 0 :type u16)
(rd-i 0 :type u32) (rd-n 0 :type u32)
(latch 0 :type u32)
(window 0 :type u32)
;; the drawing area's corners as GP0(E3h)/GP0(E4h) set them (GP1(10h) reads them)
(ax1 0 :type u16) (ay1 0 :type u16) (ax2 0 :type u16) (ay2 0 :type u16)
;; the display (GP1)
(disp-x 0 :type u16) (disp-y 0 :type u16)
(hr1 #x200 :type u16) (hr2 #xC00 :type u16) (vr1 #x10 :type u16) (vr2 #x100 :type u16)
(dmode 0 :type u32)
(irq 0 :type u8)
(draws 0 :type u64) (pixels 0 :type u64) (flips 0 :type u64))(defmacro vram-ref) source(defmacro vram-ref (gpu x y)
"the halfword at (X, Y), both wrapped to VRAM ([PSXSPX] \"Wrapping\")"
`(aref (gpu-vram ,gpu) (+ (logand ,x 1023) (* 1024 (logand ,y 511)))))(defmacro vram-set) source(defmacro vram-set (gpu x y v)
`(setf (aref (gpu-vram ,gpu) (+ (logand ,x 1023) (* 1024 (logand ,y 511)))) ,v))(defun gpu-status) source(defun gpu-status (gpu)
"GPUSTAT ([PSXSPX] \"1F801814h - GPUSTAT - GPU Status Register (R)\"): the
stored bits; 26 ready for a command word and 28 for a DMA block (the
commands complete at once here), 27 VRAM-to-CPU data ready, 25 the DMA
request of the direction in bits 29-30 (0 off, 1 FIFO, 2 = bit 28, 3 =
bit 27); bit 24 IRQ1"
(declare (type gpu gpu))
(let* ((s (logand (gpu-stat gpu) #xE1FFFFFF))
(rd (if (< (gpu-rd-i gpu) (gpu-rd-n gpu)) 1 0))
(cmd (if (= (gpu-mode gpu) 0) 1 0))
(dir (bit-field s 29 2))
(req (case dir (0 0) (1 1) (2 1) (t rd))))
(logior (logand s #xFEFFFFFF) (ash (gpu-irq gpu) 24) (ash req 25) (ash cmd 26) (ash rd 27) (ash 1 28))))(defun gpu-display-width) source(defun gpu-display-width (gpu)
"the horizontal resolution of GP1(08h): 256, 320, 512 or 640, or 368 with bit 6"
(declare (type gpu gpu))
(let ((m (gpu-dmode gpu)))
(if (logbitp 6 m)
368
(case (logand m 3) (0 256) (1 320) (2 512) (t 640)))))(defun gpu-display-height) source(defun gpu-display-height (gpu)
"the vertical resolution: 480 with GP1(08h) bit 2, else the vertical
range in lines when under 239, else 240 (as [XPS1EMU] shows it: a PAL
picture's lines past 240 are not shown)"
(declare (type gpu gpu))
(let ((lines (if (> (gpu-vr2 gpu) (gpu-vr1 gpu)) (- (gpu-vr2 gpu) (gpu-vr1 gpu)) 0)))
(cond ((logbitp 2 (gpu-dmode gpu)) 480)
((and (> lines 0) (< lines 239)) lines)
(t 240))))(defun gpu-pal-p) source(defun gpu-pal-p (gpu)
(declare (type gpu gpu))
(logbitp 3 (gpu-dmode gpu)))(defun gpu-24bit-p) source(defun gpu-24bit-p (gpu)
(declare (type gpu gpu))
(logbitp 4 (gpu-dmode gpu)))(defun gpu-display-enabled-p) source(defun gpu-display-enabled-p (gpu)
(declare (type gpu gpu))
(not (logbitp 23 (gpu-stat gpu))))(defun vram-rgb) source(defun vram-rgb (h)
"the 15-bit pixel H as #xRRGGBB, each 5-bit channel shifted to the top of
its byte (the low 3 bits 0, as [XPS1EMU] writes its frames)"
(declare (type u16 h))
(logior (ash (logand h 31) 19) (ash (logand (ash h -5) 31) 11) (ash (logand (ash h -10) 31) 3)))(defun display-rgb) source(defun display-rgb (gpu x y)
"pixel (X, Y) of the displayed picture as #xRRGGBB: 15-bit pixels, or
24-bit ones packed in VRAM (\"24bit Mode\": 3 bytes per pixel from the
display start)"
(declare (type gpu gpu) (type u32 x y))
(let ((sx (gpu-disp-x gpu)) (sy (wrap 9 (+ (gpu-disp-y gpu) y))))
(if (gpu-24bit-p gpu)
(let* ((byte (* 3 x))
(w0 (vram-ref gpu (+ sx (ash byte -1)) sy))
(w1 (vram-ref gpu (+ sx (ash byte -1) 1) sy)))
(if (= (logand byte 1) 0)
(logior (ash (logand w0 #xFF) 16) (logand w0 #xFF00) (logand w1 #xFF))
(logior (ash (ash w0 -8) 16) (ash (logand w1 #xFF) 8) (ash w1 -8))))
(vram-rgb (vram-ref gpu (+ sx x) sy)))))(defun gpu-vram-hash) source(defun gpu-vram-hash (gpu x y w h)
"a 64-bit FNV-1a hash of the VRAM rectangle (X, Y, W, H): what the tests compare"
(declare (type gpu gpu) (type u32 x y w h))
(let ((hash #xCBF29CE484222325))
(declare (type u64 hash))
(dotimes (j h)
(dotimes (i w)
(setq hash (wrap 64 (* (logxor hash (vram-ref gpu (+ x i) (+ y j))) #x100000001B3)))))
hash))(defun gpu-drawn) source(defun gpu-drawn (gpu)
"a primitive was drawn: (gpu-gfx gpu) holds it"
(declare (type gpu gpu) (ignorable gpu))
nil)(defun gpu-written) source(defun gpu-written (gpu x y w h)
"VRAM's rectangle (X, Y, W, H) was written other than by drawing"
(declare (type gpu gpu) (type u32 x y w h) (ignorable gpu x y w h))
nil)Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.