the R3000A's coprocessor 0 (the PS1's): its registers, rfe, exception entry and interrupts.
0/1 a fixed bit, name a field, a bit the decoder ignores
mfc0 rt rd
Move From Coprocessor 0
MFC0: "rt = cop0datRd", after the next instruction
r3000a:
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | rt | rd | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||||||||
(defmips mfc0 (:only :r3000a) (:asm "mfc0" rt rd) (:doc "Move From Coprocessor 0")
(cond ((and (< rd 16) (not (r3k-cop0-usable))) (raise-cpu 0))
((or (< rd 3) (= rd 4) (= rd 10)) (raise +exc-illegal+))
(t (set-gpr-load rt (cop0 rd)))))
mtc0 rt rd
Move To Coprocessor 0
MTC0: "cop0datRd = rt", the read-only bits and registers kept: SR's unused bits 6-7, 23-24, 26-27 stay zero, Cause takes Sw (bits 8-9) only, DCIC's unused bits 6-11 and 16-22 stay zero; TAR, BadVaddr, EPC, PRId and the N/A and garbage registers are not written
r3000a:
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | rt | rd | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||||||||
(defmips mtc0 (:only :r3000a) (:asm "mtc0" rt rd) (:doc "Move To Coprocessor 0")
(if (and (< rd 16) (not (r3k-cop0-usable)))
(raise-cpu 0)
(case rd
((3 5 9 11) (setf (cop0 rd) (wrap 32 (gpr rt))))
(7 (setf (cop0 7) (logand (gpr rt) #xFF80F03F)))
(12 (setf (cop0 12) (logand (gpr rt) #xF27FFF3F)))
(13 (setf (cop0 13) (logior (logand (cop0 13) #xFFFFFCFF) (logand (gpr rt) #x300)))))))
rfe
Restore From Exception
RFE: "bit2-3 are copied to bit0-1, and bit4-5 are copied to bit2-3, all other bits (including bit4-5) are left unchanged"
r3000a:
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
(defmips rfe (:only :r3000a) (:asm "rfe") (:doc "Restore From Exception")
(if (r3k-cop0-usable)
(setf (cop0 12) (logior (logand (cop0 12) #xFFFFFFF0) (bit-field (cop0 12) 2 4)))
(raise-cpu 0)))
lwc0 rt (simm imm) rs
Load Word to Coprocessor 0
"mov [mem],cop0reg / mov cop0reg,[mem]", "jf/jt cop0flg": "Trying to execute these opcodes causes a Coprocessor Unusable Exception"
r3000a:
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 | 0 | rs | rt | imm | |||||||||||||||||||||||
(defmips lwc0 (:only :r3000a) (:asm "lwc0" rt (simm imm) rs) (:doc "Load Word to Coprocessor 0")
(raise-cpu 0))
swc0 rt (simm imm) rs
Store Word from Coprocessor 0
r3000a:
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 0 | 0 | 0 | rs | rt | imm | |||||||||||||||||||||||
(defmips swc0 (:only :r3000a) (:asm "swc0" rt (simm imm) rs) (:doc "Store Word from Coprocessor 0")
(raise-cpu 0))
bc0f (b-rel offset)
Branch on Coprocessor 0 False
r3000a:
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | offset | |||||||||||||||
(defmips bc0f (:only :r3000a) (:asm "bc0f" (b-rel offset)) (:doc "Branch on Coprocessor 0 False")
(raise-cpu 0))
bc0t (b-rel offset)
Branch on Coprocessor 0 True
r3000a:
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | offset | |||||||||||||||
(defmips bc0t (:only :r3000a) (:asm "bc0t" (b-rel offset)) (:doc "Branch on Coprocessor 0 True")
(raise-cpu 0))
(defconstant +cop0-sr+) source(defconstant +cop0-sr+ 12)(defconstant +cop0-cause+) source(defconstant +cop0-cause+ 13)(defconstant +cop0-epc+) source(defconstant +cop0-epc+ 14)(defconstant +cop0-prid+) source(defconstant +cop0-prid+ 15)(defmacro cop0) source(defmacro cop0 (n) `(aref (mips-cop0 cpu) ,n))(defmacro r3k-cop0-usable) source(defmacro r3k-cop0-usable () `(or (= (bit-at (cop0 12) 1) 0) (= (bit-at (cop0 12) 28) 1)))(defmacro raise-cpu) source(defmacro raise-cpu (n)
`(progn (setf (cop0 13) (logior (logand (cop0 13) #xCFFFFFFF) (ash ,n 28)))
(raise +exc-cpu+)))(defmacro define-r3k-absent-cop) source(defmacro define-r3k-absent-cop (n &rest names)
`(progn
,@(mapcar (lambda (name)
`(defmips ,name (:only :r3000a) (:doc ,(format nil "coprocessor ~D (absent on the PS1)" n))
(raise-cpu ,n)))
names)))(defun r3000a-exc-code) source(defun r3000a-exc-code (exc)
"the Cause.ExcCode of the pending exception EXC (a +exc-...+ code) [PSXSPX] \"cop0r13\""
(declare (type (unsigned-byte 8) exc))
(cond ((= exc +exc-interrupt+) 0)
((= exc +exc-adel+) 4)
((= exc +exc-ades+) 5)
((= exc +exc-ibe+) 6)
((= exc +exc-dbe+) 7)
((= exc +exc-syscall+) 8)
((= exc +exc-break+) 9)
((= exc +exc-cpu+) 11)
((= exc +exc-overflow+) 12)
(t 10)))(defun r3000a-take-exception) source(defun r3000a-take-exception (cpu)
"enter the exception pending in mips-exc (header); nothing if none"
(declare (type mips cpu))
(let ((exc (mips-exc cpu)) (pc (mips-pc cpu)) (r (mips-ldr cpu)))
(unless (= exc 0)
;; a load in flight completes
(unless (= r 0)
(setf (aref (mips-gpr cpu) r) (mips-ldv cpu)))
(setf (mips-ldr cpu) 0)
;; a misaligned fetch: BadVaddr is the pc
(when (and (= exc +exc-adel+) (/= (wrap 2 pc) 0))
(setf (aref (mips-cop0 cpu) 8) pc))
(let ((bd (mips-bd cpu)) (bt (mips-bt cpu)) (sr (aref (mips-cop0 cpu) +cop0-sr+)))
(when (and (= bd 1) (= bt 1))
(setf (aref (mips-cop0 cpu) 6) (mips-npc cpu)))
(setf (aref (mips-cop0 cpu) +cop0-cause+)
(logior (logand (aref (mips-cop0 cpu) +cop0-cause+)
(if (= exc +exc-cpu+) #x3000FF00 #x0000FF00))
(ash (r3000a-exc-code exc) 2)
(ash bd 31) (ash (logand bd bt) 30))
(aref (mips-cop0 cpu) +cop0-epc+) (if (= bd 1) (wrap 32 (- pc 4)) pc)
(aref (mips-cop0 cpu) +cop0-sr+) (logior (logand sr #xFFFFFFC0) (wrap 6 (ash sr 2))))
(let ((v (if (= (bit-at sr 22) 1) #xBFC00180 #x80000080)))
(setf (mips-pc cpu) v
(mips-npc cpu) (+ v 4)
(mips-nnpc cpu) (+ v 8)
(mips-bd cpu) 0 (mips-bt cpu) 0 (mips-jmp cpu) 0 (mips-lfr cpu) 0
(mips-exc cpu) 0))))))(defun r3000a-interrupt-pending-p) source(defun r3000a-interrupt-pending-p (cpu)
"an interrupt would be taken now: SR.IEc and a bit of Cause.IP/Sw enabled in SR.Im"
(declare (type mips cpu))
(and (= (bit-at (aref (mips-cop0 cpu) +cop0-sr+) 0) 1)
(/= (logand (aref (mips-cop0 cpu) +cop0-sr+) (aref (mips-cop0 cpu) +cop0-cause+) #xFF00) 0)))(defun r3000a-interrupt) source(defun r3000a-interrupt (cpu)
"take an interrupt before the next instruction if one is pending (header): -> 1 if taken, else 0"
(declare (type mips cpu))
(if (r3000a-interrupt-pending-p cpu)
(let ((pc (mips-pc cpu)))
(when (and (= (wrap 2 pc) 0) (= (logand (mips-fetch pc) #xFE000000) #x4A000000))
;; "the GTE command is executed, but nethertheless, the return
;; address in EPC points to the GTE command"
(let ((r (mips-ldr cpu)))
(unless (= r 0)
(setf (aref (mips-gpr cpu) r) (mips-ldv cpu) (mips-ldr cpu) 0)))
(r3000a-execute cpu (mips-fetch pc)))
(setf (mips-exc cpu) +exc-interrupt+)
(r3000a-take-exception cpu)
1)
0))(defun r3000a-sys-step) source(defun r3000a-sys-step (cpu)
"a machine's step: an interrupt, or an instruction and its exception if it raised one"
(declare (type mips cpu))
(when (= (r3000a-interrupt cpu) 0)
(r3000a-step cpu)
(unless (= (mips-exc cpu) 0)
(r3000a-take-exception cpu))))(defun r3000a-sys-run) source(defun r3000a-sys-run (cpu stop max)
"r3000a-sys-step until pc = STOP or MAX steps ran: -> the number of steps"
(declare (type mips cpu) (type (unsigned-byte 32) stop max))
(let ((n 0))
(declare (type (unsigned-byte 32) n))
(loop
(when (or (= (mips-pc cpu) stop) (>= n max)) (return n))
(r3000a-sys-step cpu)
(setq n (+ n 1)))))(defun r3000a-reset) source(defun r3000a-reset (cpu)
"the R3000A's reset: pc at the reset vector BFC00000h, SR.BEV set and
kernel mode with interrupts off ([R3000] \"Reset\"), PRId 2 [PSXSPX]"
(declare (type mips cpu))
(mips-reset cpu #xBFC00000)
(setf (aref (mips-cop0 cpu) +cop0-sr+) #x00400000
(aref (mips-cop0 cpu) +cop0-cause+) 0
(aref (mips-cop0 cpu) +cop0-prid+) 2))Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.