mips: cop0.lisp

the R5900's coprocessor 0: moves, ERET, EI/DI, BC0, the TLB instructions, the debug and performance registers, and its exception entry.

0/1 a fixed bit, name a field,   a bit the decoder ignores

mfc0 source

mfc0 rt rd

Move from System Control Coprocessor

MFC0: "GPR[rt] <- sign_extend(CPR[0, rd])"

r5900:

313029282726252423222120191817161514131211109876543210
01000000000rtrd00000000000
(defmips mfc0 (:only :r5900) (:asm "mfc0" rt rd) (:doc "Move from System Control Coprocessor")
  (set-gpr rt (reg32 (aref (mips-cop0 cpu) rd))))

mtc0 source

mtc0 rt rd

Move to System Control Coprocessor

MTC0: "CPR[0, rd] <- GPR[rt]31..0". Read-only registers keep their value
(Random, BadVAddr, PRId, BadPAddr and Cause, whose bits the hardware
sets); Status keeps its read-only bits; writing Compare clears the timer
interrupt (Cause.IP7) [EEUM] "Compare Register".

r5900:

313029282726252423222120191817161514131211109876543210
01000000100rtrd00000000000
(defmips mtc0 (:only :r5900) (:asm "mtc0" rt rd) (:doc "Move to System Control Coprocessor")
  (let ((v (wrap 32 (gpr rt))) (c (mips-cop0 cpu)))
    (case rd
      ((1 8 13 15 23) nil)
      (12 (setf (aref c 12) (logior (logand v +st-writable+)
                                    (logand (aref c 12) (wrap 32 (lognot +st-writable+))))))
      (11 (setf (aref c 11) v
                (aref c 13) (logand (aref c 13) (wrap 32 (lognot +cause-ip7+)))))
      (t (setf (aref c rd) v)))))

mfiab source

mfiab rt

Move from Instruction Address Breakpoint

r5900:

313029282726252423222120191817161514131211109876543210
01000000000rt1100000000000010
(defmips mfiab (:only :r5900) (:asm "mfiab" rt) (:doc "Move from Instruction Address Breakpoint")
  (set-gpr rt (reg32 (aref (mips-cop0x cpu) 1))))

mfiabm source

mfiabm rt

Move from Instruction Address Breakpoint Mask

r5900:

313029282726252423222120191817161514131211109876543210
01000000000rt1100000000000011
(defmips mfiabm (:only :r5900) (:asm "mfiabm" rt) (:doc "Move from Instruction Address Breakpoint Mask")
  (set-gpr rt (reg32 (aref (mips-cop0x cpu) 2))))

mfdab source

mfdab rt

Move from Data Address Breakpoint

r5900:

313029282726252423222120191817161514131211109876543210
01000000000rt1100000000000100
(defmips mfdab (:only :r5900) (:asm "mfdab" rt) (:doc "Move from Data Address Breakpoint")
  (set-gpr rt (reg32 (aref (mips-cop0x cpu) 3))))

mfdabm source

mfdabm rt

Move from Data Address Breakpoint Mask

r5900:

313029282726252423222120191817161514131211109876543210
01000000000rt1100000000000101
(defmips mfdabm (:only :r5900) (:asm "mfdabm" rt) (:doc "Move from Data Address Breakpoint Mask")
  (set-gpr rt (reg32 (aref (mips-cop0x cpu) 4))))

mfdvb source

mfdvb rt

Move from Data Value Breakpoint

r5900:

313029282726252423222120191817161514131211109876543210
01000000000rt1100000000000110
(defmips mfdvb (:only :r5900) (:asm "mfdvb" rt) (:doc "Move from Data Value Breakpoint")
  (set-gpr rt (reg32 (aref (mips-cop0x cpu) 5))))

mfdvbm source

mfdvbm rt

Move from Data Value Breakpoint Mask

r5900:

313029282726252423222120191817161514131211109876543210
01000000000rt1100000000000111
(defmips mfdvbm (:only :r5900) (:asm "mfdvbm" rt) (:doc "Move from Data Value Breakpoint Mask")
  (set-gpr rt (reg32 (aref (mips-cop0x cpu) 6))))

mtiab source

mtiab rt

Move to Instruction Address Breakpoint

r5900:

313029282726252423222120191817161514131211109876543210
01000000100rt1100000000000010
(defmips mtiab (:only :r5900) (:asm "mtiab" rt) (:doc "Move to Instruction Address Breakpoint")
  (setf (aref (mips-cop0x cpu) 1) (wrap 32 (gpr rt))))

mtiabm source

mtiabm rt

Move to Instruction Address Breakpoint Mask

r5900:

313029282726252423222120191817161514131211109876543210
01000000100rt1100000000000011
(defmips mtiabm (:only :r5900) (:asm "mtiabm" rt) (:doc "Move to Instruction Address Breakpoint Mask")
  (setf (aref (mips-cop0x cpu) 2) (wrap 32 (gpr rt))))

mtdab source

mtdab rt

Move to Data Address Breakpoint

r5900:

313029282726252423222120191817161514131211109876543210
01000000100rt1100000000000100
(defmips mtdab (:only :r5900) (:asm "mtdab" rt) (:doc "Move to Data Address Breakpoint")
  (setf (aref (mips-cop0x cpu) 3) (wrap 32 (gpr rt))))

mtdabm source

mtdabm rt

Move to Data Address Breakpoint Mask

r5900:

313029282726252423222120191817161514131211109876543210
01000000100rt1100000000000101
(defmips mtdabm (:only :r5900) (:asm "mtdabm" rt) (:doc "Move to Data Address Breakpoint Mask")
  (setf (aref (mips-cop0x cpu) 4) (wrap 32 (gpr rt))))

mtdvb source

mtdvb rt

Move to Data Value Breakpoint

r5900:

313029282726252423222120191817161514131211109876543210
01000000100rt1100000000000110
(defmips mtdvb (:only :r5900) (:asm "mtdvb" rt) (:doc "Move to Data Value Breakpoint")
  (setf (aref (mips-cop0x cpu) 5) (wrap 32 (gpr rt))))

mtdvbm source

mtdvbm rt

Move to Data Value Breakpoint Mask

r5900:

313029282726252423222120191817161514131211109876543210
01000000100rt1100000000000111
(defmips mtdvbm (:only :r5900) (:asm "mtdvbm" rt) (:doc "Move to Data Value Breakpoint Mask")
  (setf (aref (mips-cop0x cpu) 6) (wrap 32 (gpr rt))))

mfps source

mfps rt f1_5

Move from Performance Counter Control

MFPS/MTPS: the counter control PCCR (CPR[0,25]); the register field
must be 0. MFPC/MTPC: counter PCR0 or PCR1 (the field's low bit).

r5900:

313029282726252423222120191817161514131211109876543210
01000000000rt1100100000f1_50
(defmips mfps (:only :r5900) (:asm "mfps" rt f1_5) (:doc "Move from Performance Counter Control")
  (set-gpr rt (reg32 (aref (mips-cop0 cpu) 25))))

mtps source

mtps rt f1_5

Move to Performance Counter Control

r5900:

313029282726252423222120191817161514131211109876543210
01000000100rt1100100000f1_50
(defmips mtps (:only :r5900) (:asm "mtps" rt f1_5) (:doc "Move to Performance Counter Control")
  (setf (aref (mips-cop0 cpu) 25) (wrap 32 (gpr rt))))

mfpc source

mfpc rt f1_5

Move from Performance Counter

r5900:

313029282726252423222120191817161514131211109876543210
01000000000rt1100100000f1_51
(defmips mfpc (:only :r5900) (:asm "mfpc" rt f1_5) (:doc "Move from Performance Counter")
  (set-gpr rt (reg32 (aref (mips-cop0x cpu) (+ 8 (wrap 1 f1_5))))))

mtpc source

mtpc rt f1_5

Move to Performance Counter

r5900:

313029282726252423222120191817161514131211109876543210
01000000100rt1100100000f1_51
(defmips mtpc (:only :r5900) (:asm "mtpc" rt f1_5) (:doc "Move to Performance Counter")
  (setf (aref (mips-cop0x cpu) (+ 8 (wrap 1 f1_5))) (wrap 32 (gpr rt))))

eret source

eret

Exception Return

ERET: "if Status.ERL = 1 then PC <- ErrorEPC; Status.ERL <- 0 else PC <-
EPC; Status.EXL <- 0". It has no delay slot: the next instruction is the
target's (npc, not nnpc).

r5900:

313029282726252423222120191817161514131211109876543210
01000010000000000000000000011000
(defmips eret (:only :r5900) (:asm "eret") (:doc "Exception Return")
  (let* ((c (mips-cop0 cpu)) (s (aref c 12))
         (erl (/= (logand s +st-erl+) 0))
         (target (if erl (aref c 30) (aref c 14))))
    (setf (aref c 12) (logand s (wrap 32 (lognot (if erl +st-erl+ +st-exl+))))
          (mips-npc cpu) target
          (mips-nnpc cpu) (wrap 32 (+ target 4)))))

ei source

ei

Enable Interrupt

EI, DI: "if (Status.EDI = 1) or kernel mode then Status.EIE <- 1 (0)",
otherwise nothing

r5900:

313029282726252423222120191817161514131211109876543210
01000010000000000000000000111000
(defmips ei (:only :r5900) (:asm "ei") (:doc "Enable Interrupt")
  (when (or (/= (logand (aref (mips-cop0 cpu) 12) +st-edi+) 0) (cop0-kernel-p))
    (setf (aref (mips-cop0 cpu) 12) (logior (aref (mips-cop0 cpu) 12) +st-eie+))))

di source

di

Disable Interrupt

r5900:

313029282726252423222120191817161514131211109876543210
01000010000000000000000000111001
(defmips di (:only :r5900) (:asm "di") (:doc "Disable Interrupt")
  (when (or (/= (logand (aref (mips-cop0 cpu) 12) +st-edi+) 0) (cop0-kernel-p))
    (setf (aref (mips-cop0 cpu) 12) (logand (aref (mips-cop0 cpu) 12) (wrap 32 (lognot +st-eie+))))))

bc0f source

bc0f (b-rel offset)

Branch on Coprocessor 0 False

r5900:

313029282726252423222120191817161514131211109876543210
0100000100000000offset
(defmips bc0f (:only :r5900) (:asm "bc0f" (b-rel offset)) (:doc "Branch on Coprocessor 0 False")
  (when (= (mips-cpcond0 cpu) 0) (branch-to (branch-target offset))))

bc0t source

bc0t (b-rel offset)

Branch on Coprocessor 0 True

r5900:

313029282726252423222120191817161514131211109876543210
0100000100000001offset
(defmips bc0t (:only :r5900) (:asm "bc0t" (b-rel offset)) (:doc "Branch on Coprocessor 0 True")
  (when (/= (mips-cpcond0 cpu) 0) (branch-to (branch-target offset))))

bc0fl source

bc0fl (b-rel offset)

Branch on Coprocessor 0 False Likely

r5900:

313029282726252423222120191817161514131211109876543210
0100000100000010offset
(defmips bc0fl (:only :r5900) (:asm "bc0fl" (b-rel offset)) (:doc "Branch on Coprocessor 0 False Likely")
  (if (= (mips-cpcond0 cpu) 0) (branch-to (branch-target offset)) (nullify)))

bc0tl source

bc0tl (b-rel offset)

Branch on Coprocessor 0 True Likely

r5900:

313029282726252423222120191817161514131211109876543210
0100000100000011offset
(defmips bc0tl (:only :r5900) (:asm "bc0tl" (b-rel offset)) (:doc "Branch on Coprocessor 0 True Likely")
  (if (/= (mips-cpcond0 cpu) 0) (branch-to (branch-target offset)) (nullify)))

tlbwi source

tlbwi

Write Indexed TLB Entry

r5900:

313029282726252423222120191817161514131211109876543210
01000010000000000000000000000010
(defmips tlbwi (:only :r5900) (:asm "tlbwi") (:doc "Write Indexed TLB Entry")
  (tlb-write (wrap 6 (aref (mips-cop0 cpu) 0))))

tlbwr source

tlbwr

Write Random TLB Entry

r5900:

313029282726252423222120191817161514131211109876543210
01000010000000000000000000000110
(defmips tlbwr (:only :r5900) (:asm "tlbwr") (:doc "Write Random TLB Entry")
  (tlb-write (wrap 6 (aref (mips-cop0 cpu) 1))))

tlbr source

tlbr

Read Indexed TLB Entry

r5900:

313029282726252423222120191817161514131211109876543210
01000010000000000000000000000001
(defmips tlbr (:only :r5900) (:asm "tlbr") (:doc "Read Indexed TLB Entry")
  (let ((i (wrap 6 (aref (mips-cop0 cpu) 0))) (c (mips-cop0 cpu)) (tlb (mips-tlb cpu)))
    (when (< i +tlb-entries+)
      (let ((k (* 4 i)))
        (setf (aref c 5) (aref tlb k)
              (aref c 10) (aref tlb (+ k 1))
              (aref c 2) (aref tlb (+ k 2))
              (aref c 3) (aref tlb (+ k 3)))))))

tlbp source

tlbp

Probe TLB for Matching Entry

TLBP: "Index <- the matching entry, or Index.P (bit 31) <- 1 when none"

r5900:

313029282726252423222120191817161514131211109876543210
01000010000000000000000000001000
(defmips tlbp (:only :r5900) (:asm "tlbp") (:doc "Probe TLB for Matching Entry")
  (let* ((c (mips-cop0 cpu)) (tlb (mips-tlb cpu)) (probe (aref c 10)) (found #x80000000))
    (declare (type (unsigned-byte 32) found))
    (dotimes (i +tlb-entries+)
      (let* ((k (* 4 i))
             (mask (wrap 32 (lognot (logior (aref tlb k) #x1FFF))))
             (ehi (aref tlb (+ k 1)))
             (g (logand (aref tlb (+ k 2)) (aref tlb (+ k 3)) 1)))
        (when (and (= found #x80000000)
                   (= (logand ehi mask) (logand probe mask))
                   (or (= g 1) (= (wrap 8 ehi) (wrap 8 probe))))
          (setq found i))))
    (setf (aref c 0) found)))

Other definitions

(defconstant +st-ie+) source
(defconstant +st-ie+ #x1)
(defconstant +st-exl+) source
(defconstant +st-exl+ #x2)
(defconstant +st-erl+) source
(defconstant +st-erl+ #x4)
(defconstant +st-ksu+) source
(defconstant +st-ksu+ #x18)
(defconstant +st-eie+) source
(defconstant +st-eie+ #x10000)
(defconstant +st-edi+) source
(defconstant +st-edi+ #x20000)
(defconstant +st-bev+) source
(defconstant +st-bev+ #x400000)
(defconstant +st-dev+) source
(defconstant +st-dev+ #x800000)
(defconstant +st-writable+) source
(defconstant +st-writable+ #xF0C78C1F)
(defconstant +cause-bd+) source
(defconstant +cause-bd+ #x80000000)
(defconstant +cause-bd2+) source
(defconstant +cause-bd2+ #x40000000)
(defconstant +cause-ip7+) source
(defconstant +cause-ip7+ #x8000)
(defmacro cop0-kernel-p) source
(defmacro cop0-kernel-p ()
  `(let ((s (aref (mips-cop0 cpu) 12)))
     (or (= (logand s +st-ksu+) 0) (/= (logand s (logior +st-exl+ +st-erl+)) 0))))
(defconstant +tlb-entries+) source
(defconstant +tlb-entries+ 48)
(defmacro tlb-write) source
(defmacro tlb-write (i)
  `(let ((k (* 4 ,i)) (c (mips-cop0 cpu)) (tlb (mips-tlb cpu)))
     (when (< ,i +tlb-entries+)
       (setf (aref tlb k) (logand (aref c 5) #x01FFE000)
             (aref tlb (+ k 1)) (logand (aref c 10) #xFFFFE0FF)
             (aref tlb (+ k 2)) (aref c 2)
             (aref tlb (+ k 3)) (aref c 3)))))
(defun r5900-exc-code) source
(defun r5900-exc-code (exc)
  "the Cause.ExcCode of the kit's pending exception EXC (+exc-...+, mips-exc)"
  (declare (type (unsigned-byte 8) exc))
  (case exc
    (1 10) (2 8) (3 9) (4 12) (5 4) (6 5) (7 13) (8 11)
    (t 0)))
(defun r5900-take-exception) source
(defun r5900-take-exception (cpu code)
  "enter the level 1 exception CODE (a Cause.ExcCode) for the instruction at
   pc (in a delay slot when bd is 1): Cause.ExcCode, and, unless Status.EXL
   is already set, EPC (the branch's address for a delay slot, with
   Cause.BD) and EXL; then pc at the vector: base #x80000000, or
   #xBFC00200 with Status.BEV, plus #x000 for a TLB refill (TLBL/TLBS with
   EXL clear), #x200 for an interrupt, #x180 for the rest. Clears mips-exc."
  (declare (type mips cpu) (type (unsigned-byte 8) code))
  (let* ((c (mips-cop0 cpu)) (s (aref c 12))
         (offset (cond ((/= (logand s +st-exl+) 0) #x180)
                       ((or (= code 2) (= code 3)) #x000)
                       ((= code 0) #x200)
                       (t #x180)))
         (vector (+ (if (/= (logand s +st-bev+) 0) #xBFC00200 #x80000000) offset)))
    (setf (aref c 13) (logior (logand (aref c 13) (wrap 32 (lognot #x7C))) (ash code 2)))
    (when (= (logand s +st-exl+) 0)
      (if (= (mips-bd cpu) 1)
          (setf (aref c 14) (wrap 32 (- (mips-pc cpu) 4))
                (aref c 13) (logior (aref c 13) +cause-bd+))
          (setf (aref c 14) (mips-pc cpu)
                (aref c 13) (logand (aref c 13) (wrap 32 (lognot +cause-bd+)))))
      (setf (aref c 12) (logior s +st-exl+)))
    (mips-reset cpu vector)))
(defun r5900-take-level2) source
(defun r5900-take-level2 (cpu kind)
  "enter a level 2 exception: KIND 0 reset, 1 NMI, 2 performance counter, 3
   debug (Cause.EXC2). ErrorEPC (the branch for a delay slot, Cause.BD2)
   and Status.ERL; pc at #xBFC00000 for reset and NMI, else #x80000080
   (counter) or #x80000100 (debug), #xBFC00280/#xBFC00300 with Status.DEV"
  (declare (type mips cpu) (type (unsigned-byte 8) kind))
  (let* ((c (mips-cop0 cpu)) (s (aref c 12))
         (vector (cond ((< kind 2) #xBFC00000)
                       ((/= (logand s +st-dev+) 0) (if (= kind 2) #xBFC00280 #xBFC00300))
                       (t (if (= kind 2) #x80000080 #x80000100)))))
    (setf (aref c 13) (logior (logand (aref c 13) (wrap 32 (lognot #x70000))) (ash (wrap 3 kind) 16)))
    (if (= (mips-bd cpu) 1)
        (setf (aref c 30) (wrap 32 (- (mips-pc cpu) 4))
              (aref c 13) (logior (aref c 13) +cause-bd2+))
        (setf (aref c 30) (mips-pc cpu)
              (aref c 13) (logand (aref c 13) (wrap 32 (lognot +cause-bd2+)))))
    (setf (aref c 12) (logior s +st-erl+))
    (mips-reset cpu vector)))
(defun r5900-interrupt-p) source
(defun r5900-interrupt-p (cpu)
  "is an interrupt taken now: Status.IE and EIE set, EXL and ERL clear, and
   a Cause.IP bit (10 INT0, 11 INT1, 15 timer) enabled by its Status.IM bit"
  (declare (type mips cpu))
  (let ((s (aref (mips-cop0 cpu) 12)) (cause (aref (mips-cop0 cpu) 13)))
    (and (/= (logand s +st-ie+) 0) (/= (logand s +st-eie+) 0)
         (= (logand s (logior +st-exl+ +st-erl+)) 0)
         (/= (logand s cause #x8C00) 0))))
(defun r5900-take-interrupt) source
(defun r5900-take-interrupt (cpu)
  "enter the interrupt exception (ExcCode 0) before the instruction at pc"
  (declare (type mips cpu))
  (r5900-take-exception cpu 0))
(defun r5900-reset-cop0) source
(defun r5900-reset-cop0 (cpu)
  "the reset values: PRId #x2E20 (the R5900 as PCSX2 reports it), Status
   BEV and ERL with CU0-2, Config #x440 (PCSX2 cpuReset)"
  (declare (type mips cpu))
  (setf (aref (mips-cop0 cpu) 15) #x2E20
        (aref (mips-cop0 cpu) 12) #x70400004
        (aref (mips-cop0 cpu) 16) #x440))

Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.