mips: mips1.lisp

MIPS I semantics, for every model (the R3000A runs them as they are, the R5900 as MIPS III does).

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

lwr source

lwr rt (simm imm) rs

Load Word Right

LWR on a 64-bit CPU ([MIPSIV] LWR, PCSX2's interpreter [PCSX2]): the
merged word is sign-extended when the whole word was loaded (byte 0);
otherwise only the low 32 bits change and the high 32 keep their value.

r5900:

313029282726252423222120191817161514131211109876543210
100110rsrtimm
(defmips lwr (:only :r5900) (:asm "lwr" rt (simm imm) rs) (:doc "Load Word Right")
  (let* ((a (vaddr rs imm)) (b (wrap 2 a)) (m (mips-load 32 (wrap 32 (- a b))))
         (w (logior (ash m (- (* 8 b))) (logand (gpr rt) (wrap 32 (ash #xFFFFFF00 (- 24 (* 8 b))))))))
    (if (= b 0)
        (set-gpr rt (wrap 64 (sext 32 w)))
        (set-gpr rt (logior (logand (gpr rt) #xFFFFFFFF00000000) (wrap 32 w))))))

sb source

sb rt (simm imm) rs

Store Byte

SB: "memory[vAddr] <- GPR[rt]7..0"

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
101000rsrtimm
(defmips sb (:asm "sb" rt (simm imm) rs) (:doc "Store Byte")
  (mips-store 8 (vaddr rs imm) (wrap 8 (gpr rt))))

sh source

sh rt (simm imm) rs

Store Halfword

SH: "if vAddr0 /= 0 then SignalException(AddressError) ... memory[vAddr] <- GPR[rt]15..0"

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
101001rsrtimm
(defmips sh (:asm "sh" rt (simm imm) rs) (:doc "Store Halfword")
  (let ((a (vaddr rs imm)))
    (if (/= (wrap 1 a) 0)
        (raise-address +exc-ades+ a)
        (mips-store 16 a (wrap 16 (gpr rt))))))

sw source

sw rt (simm imm) rs

Store Word

SW: "memory[vAddr] <- GPR[rt]31..0"

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
101011rsrtimm
(defmips sw (:asm "sw" rt (simm imm) rs) (:doc "Store Word")
  (let ((a (vaddr rs imm)))
    (if (/= (wrap 2 a) 0)
        (raise-address +exc-ades+ a)
        (mips-store 32 a (gpr rt)))))

swl source

swl rt (simm imm) rs

Store Word Left

SWL, little-endian: the register's high 8 * (byte + 1) bits to the bytes
of the word from its start up to vAddr ("swl [N*4+0] transfer upper
8bit of Rt to [N*4+0] ... swl [N*4+3] whole 32bit"); the word's other
bytes are left. Written as a read-modify-write of the aligned word,
which is the same for memory (not for a device register with side
effects on reads, which the machine's memory map handles).

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
101010rsrtimm
(defmips swl (:asm "swl" rt (simm imm) rs) (:doc "Store Word Left")
  (let* ((a (vaddr rs imm)) (b (wrap 2 a)) (w (wrap 32 (- a b))) (m (mips-load 32 w)))
    (mips-store 32 w (logior (logand m (wrap 32 (ash #xFFFFFF00 (* 8 b))))
                             (ash (wrap 32 (gpr rt)) (- (* 8 b) 24))))))

swr source

swr rt (simm imm) rs

Store Word Right

SWR, little-endian: the register's low 8 * (4 - byte) bits to the bytes
of the word from vAddr to its end ("swr [N*4+0] transfer whole 32bit ...
swr [N*4+3] lower 8bit")

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
101110rsrtimm
(defmips swr (:asm "swr" rt (simm imm) rs) (:doc "Store Word Right")
  (let* ((a (vaddr rs imm)) (b (wrap 2 a)) (w (wrap 32 (- a b))) (m (mips-load 32 w)))
    (mips-store 32 w (logior (logand m (ash #x00FFFFFF (- (* 8 b) 24)))
                             (wrap 32 (ash (gpr rt) (* 8 b)))))))

beq source

beq rs rt (b-rel offset)

Branch on Equal

BEQ: "target <- sign_extend(offset || 0^2); condition <- GPR[rs] = GPR[rt];
if condition then PC <- PC + target" (PC the delay slot's address)

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000100rsrtoffset
(defmips beq (:asm "beq" rs rt (b-rel offset)) (:doc "Branch on Equal")
  (when (= (gpr rs) (gpr rt))
    (branch-to (branch-target offset))))

bne source

bne rs rt (b-rel offset)

Branch on Not Equal

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000101rsrtoffset
(defmips bne (:asm "bne" rs rt (b-rel offset)) (:doc "Branch on Not Equal")
  (when (/= (gpr rs) (gpr rt))
    (branch-to (branch-target offset))))

blez source

blez rs (b-rel offset)

Branch on Less Than or Equal to Zero

BLEZ: "condition <- GPR[rs] <= 0", BGTZ "> 0", BLTZ "< 0", BGEZ ">= 0",
GPR[rs] signed

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000110rs00000offset
(defmips blez (:asm "blez" rs (b-rel offset)) (:doc "Branch on Less Than or Equal to Zero")
  (when (<= (sgpr rs) 0)
    (branch-to (branch-target offset))))

bgtz source

bgtz rs (b-rel offset)

Branch on Greater Than Zero

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000111rs00000offset
(defmips bgtz (:asm "bgtz" rs (b-rel offset)) (:doc "Branch on Greater Than Zero")
  (when (> (sgpr rs) 0)
    (branch-to (branch-target offset))))

bltz source

bltz rs (b-rel offset)

Branch on Less Than Zero

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000001rs00000offset
(defmips bltz (:asm "bltz" rs (b-rel offset)) (:doc "Branch on Less Than Zero")
  (when (< (sgpr rs) 0)
    (branch-to (branch-target offset))))

bgez source

bgez rs (b-rel offset)

Branch on Greater Than or Equal to Zero

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000001rs00001offset
(defmips bgez (:asm "bgez" rs (b-rel offset)) (:doc "Branch on Greater Than or Equal to Zero")
  (when (>= (sgpr rs) 0)
    (branch-to (branch-target offset))))

bltzal source

bltzal rs (b-rel offset)

Branch on Less Than Zero and Link

BLTZAL: "condition <- GPR[rs] < 0; GPR[31] <- PC + 8; if condition then
PC <- PC + target": the link is written whether or not the branch is
taken, and the condition reads rs before it ([PSXSPX] "jumps and
branches": "if rs is $ra, then the value used for the comparison is $ra's
value before linking")

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000001rs10000offset
(defmips bltzal (:asm "bltzal" rs (b-rel offset)) (:doc "Branch on Less Than Zero and Link")
  (let ((c (< (sgpr rs) 0)))
    (set-gpr 31 (reg32 (link-address)))
    (when c (branch-to (branch-target offset)))))

bgezal source

bgezal rs (b-rel offset)

Branch on Greater Than or Equal to Zero and Link

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000001rs10001offset
(defmips bgezal (:asm "bgezal" rs (b-rel offset)) (:doc "Branch on Greater Than or Equal to Zero and Link")
  (let ((c (>= (sgpr rs) 0)))
    (set-gpr 31 (reg32 (link-address)))
    (when c (branch-to (branch-target offset)))))

j source

j (j-abs target)

Jump

J: "PC <- PC(GPRLEN-1)..28 || instr_index || 0^2", PC the delay slot's

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000010target
(defmips j (:asm "j" (j-abs target)) (:doc "Jump")
  (branch-to (logior (logand (+ (mips-pc cpu) 4) #xF0000000) (ash target 2))))

jal source

jal (j-abs target)

Jump and Link

JAL: as J, and "GPR[31] <- PC + 8"

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000011target
(defmips jal (:asm "jal" (j-abs target)) (:doc "Jump and Link")
  (set-gpr 31 (reg32 (link-address)))
  (branch-to (logior (logand (+ (mips-pc cpu) 4) #xF0000000) (ash target 2))))

jr source

jr rs

Jump Register

JR: "temp <- GPR[rs]; PC <- temp" (an address with its low two bits set
raises an address error at the fetch, exec.lisp)

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000000rs000000000000000001000
(defmips jr (:asm "jr" rs) (:doc "Jump Register")
  (branch-to (gpr rs)))

jalr source

jalr rd rs

Jump and Link Register

JALR: "temp <- GPR[rs]; GPR[rd] <- PC + 8; PC <- temp": rs is read
before rd is written

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000000rs00000rd00000001001
(defmips jalr (:asm "jalr" rd rs) (:doc "Jump and Link Register")
  (let ((target (gpr rs)))
    (set-gpr rd (reg32 (link-address)))
    (branch-to target)))

syscall source

syscall code

System Call

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000000code001100
(defmips syscall (:asm "syscall" code) (:doc "System Call")
  (raise +exc-syscall+))

break source

break code code2

Breakpoint

r3000a, r5900:

313029282726252423222120191817161514131211109876543210
000000codecode2001101
(defmips break (:asm "break" code code2) (:doc "Breakpoint")
  (raise +exc-break+))

Other definitions

(defmipsmacro gpr-inflight) source
(defmipsmacro gpr-inflight (n)
  (if (member :load-delay (mips-model-features))
      `(if (= (mips-lfr cpu) ,n) (mips-lfv cpu) (gpr ,n))
      `(gpr ,n)))
(defmacro raise-address) source
(defmacro raise-address (code a)
  `(progn (setf (aref (mips-cop0 cpu) 8) (wrap 32 ,a))
          (raise ,code)))

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