mips

371 definitions in 21 files, 371 with an encoding diagram. src/isa/mips

Files

FileDefinitionsOtherWhat
package.lisp00the ISA.MIPS package: MIPS (the PS1's R3000A, the PS2's R5900) on one abstract machine; its state contract.
model.lisp06MIPS CPU models: several in one build, each instantiated from the one description.
state.lisp031the one MIPS state every model runs on, and the accessors every semantics body uses.
bus.lisp012a machine's memory map under the MIPS models' loads, stores and fetches (XSPE_MIPS_BUS).
defmips.lisp02MIPS encodings (generated, gen/opcodes.lisp) and DEFMIPS, an instruction's semantics instantiated per model.
gen/opcodes.lisp00the encodings table, shown with each instruction
mips1.lisp592MIPS I semantics, for every model (the R3000A runs them as they are, the R5900 as MIPS III does).
cop0r3k.lisp715the R3000A's coprocessor 0 (the PS1's): its registers, rfe, exception entry and interrupts.
gte.lisp3140the PS1's coprocessor 2, the Geometry Transformation Engine: its registers, moves and commands.
mips3.lisp480MIPS II, III and IV instructions of the R5900: 64-bit arithmetic, doubleword memory, likely branches, traps.
ee.lisp2215the R5900's own non-multimedia instructions: 3-operand multiply, multiply-add, pipeline 1, SA, 128-bit loads and stores.
cop0.lisp2921the R5900's coprocessor 0: moves, ERET, EI/DI, BC0, the TLB instructions, the debug and performance registers, and its exception entry.
mmi.lisp9113the R5900's multimedia instructions (MMI): parallel operations on its 128-bit GPRs, HI and LO.
eefloat.lisp027the Emotion Engine's single precision as rules around the IEEE primitives: the arithmetic of its FPU (COP1) and of the VUs.
fpu.lisp3411the R5900's FPU (coprocessor 1): single precision only, not IEEE 754.
vu0.lisp5037the R5900's coprocessor 2: VU0 in macro mode (its upper and lower instructions issued by the EE core), moves to and from it.
exec.lisp04MIPS as a guest: per model, the executor, decoder, step and run loop.
enc.lisp02MIPS as a host: run-time encoders per model, made from the DEFMIPS encodings, and the emitter.
asm.lisp00a compile-time MIPS assembler per model, built from the DEFMIPS records.
model/r3000a.lisp01the MIPS model of the PS1's CPU (and of the PS2's IOP): an LSI CW33300, an R3000A core.
model/r5900.lisp01the MIPS model of the PS2's Emotion Engine core, the R5900.

README

src/isa/mips: MIPS, the PS1's R3000A and the PS2's R5900

Package ISA.MIPS. One description of MIPS, instantiated per CPU model on one abstract machine (the mips state struct). Each model is an ISA of its own to the rest of the kit (R3000A, R5900): its own executor, decoder, step, run loop, encoders and isel tiles, all made from the same defmips forms. Both CPUs are guests (the PS1, the PS2's EE and IOP) and both can be hosts: the PS2 can run the PS1's code, as its IOP does. See docs/isa.md for the general recipe.

FileWhat
package.lispISA.MIPS and its exports
model.lispdefmipsmodel, defmipsmacro, mips-resolve, with-mips-model
state.lispthe mips struct; (gpr n), set-gpr, set-gpr32, set-gprx, xgpr, sgpr, reg32, set-gpr-load; mips-load/mips-store (little-endian), vaddr; branch-to, branch-target, nullify, link-address; raise and the exception codes
model/r3000a.lispthe PS1's CPU: MIPS I, 32-bit, :load-delay, :gte
model/r5900.lispthe PS2's EE core: MIPS III, 64-bit, :mmi, :pipe1, :fpu-single, :vu0
defmips.lispdefmipsenc (the table), defmips (semantics, per model)
gen/opcodes.lispthe encodings (generated, do not edit)
mips1.lispMIPS I semantics, for every model, quoting [MIPSIV] and [PSXSPX]
cop0r3k.lispthe R3000A's coprocessor 0 (mfc0, mtc0, rfe, the absent coprocessors 1 and 3), r3000a-take-exception, r3000a-interrupt, r3000a-sys-step/-run, r3000a-reset (loaded when r3000a is a model)
gte.lispthe R3000A's coprocessor 2, the PS1's GTE: registers, moves, the 22 commands and their encodings (loaded when r3000a is a model)
mips3.lispthe R5900's MIPS II/III/IV: 64-bit arithmetic and shifts, ld/sd/lwu/ldl/ldr/sdl/sdr, branch likely, traps, movn/movz, sync, pref, cache [MIPSIV]
ee.lispthe R5900's own: 3-operand mult/multu, div/divu, madd/maddu, pipeline 1 (mult1 ... mtlo1 on hi1/lo1), SA (mfsa, mtsa, mtsab, mtsah), lq/sq; gprq/set-gprq (128-bit GPRs) [EECORE] [EEUM]
cop0.lispthe R5900's COP0: mfc0/mtc0, the debug and counter moves, eret, ei/di, bc0*, the TLB instructions; r5900-take-exception, r5900-take-level2, r5900-interrupt-p, r5900-take-interrupt, r5900-reset-cop0 [EEUM]
mmi.lispthe multimedia instructions (all of the table's) on 128-bit GPRs, HI and LO; lane order stated [EECORE]
eefloat.lispthe EE's single precision (no Inf/NaN/denormals, truncation, +-MAX) as rules around the IEEE primitives (an EE value is exactly a double; the double operation toward zero, then truncated to the EE's format), for the FPU and the VUs
fpu.lispthe R5900's FPU (COP1): moves, arithmetic, ACC, compares, conversions, bc1*, FCR31's flags
vu0.lispVU0 in macro mode (COP2): every macro instruction of the table, the flags, VU0 data memory, the EE's moves [VUUM]
exec.lispper model M-execute, M-decode, M-step, M-run (and M-execute-w for the load delay); mips-fetch, mips-reset
enc.lispper model (M-enc/<name> field...) (fields MSB first), mips-emit32
asm.lisp(mips-assemble model program origin), mips-load-program

The R3000A (the PS1's CPU)

What is described, from [MIPSIV], [R3000] and [PSXSPX] (docs/refs.md):

Tests (tests/isa/mips): mips_enc1 (the new words against llvm-mc, decoded back), mips_sem1 (sample values, both models), mips_delay and lower_mips_delay (the load delay), mips_cop0, mips_gte (values from [PSXSPX]'s formulas and tables), mips_gtediff and mips_cpudiff (with their lower_ twins: random GTE commands and random programs against xps1emu's gte.c and cpu.c built on the host from XSPE_XPS1EMU_DIR; the differences found are psxe's, listed in cpudiff.c), mips_disc (a PS1 disc's boot executable decoded and run, XSPE_TEST_PS1_DISC).

Not described yet: the debug breakpoints (BPC/BDA/DCIC are stored, never raised), bus errors (the machine's memory map raises them), cache isolation (SR.Isc, the machine's stores), timing (the GTE's and the multiplier's cycles, the GTE's store delay, SR.CU2's 2-cycle delay), and unused instruction fields: the decoder takes binutils' encodings, which require them zero, while the R3000A ignores them ([PSXSPX] "Illegal Opcodes"; compiled code has them zero, the Vib-Ribbon executable has none set).

The R5900 (the PS2's EE core)

What a PS2 machine needs from here

src/sys/ps2 (not written) would: map memory (kseg0/kseg1, the scratchpad, VU0's data memory at #x11004000 and the registers) and the TLB's translations; call r5900-take-exception when r5900-run stops on mips-exc (with BadVAddr/EntryHi for address and TLB errors) and r5900-take-interrupt when r5900-interrupt-p (Cause.IP2/IP3/IP7 from the INTC, the DMAC and Count/Compare, which it advances per cycle); set mips-cpcond0 from the DMAC; run VU0 micro programs (VPU-STAT, TPC) and VU1; and run the IOP as an R3000A beside the EE.

Adding instructions

  1. Check that the encoding is in gen/opcodes.lisp for the model (:in). An instruction no machine-readable source has (the GTE's commands) gets a hand-written defmipsenc beside its semantics, citing its source.
  2. Write (defmips name (:asm "mnemonic" operand...) (:doc "...") body), quoting the manual. Use the model helpers of state.lisp for anything whose meaning depends on the register width or the load delay, and (:only :r5900) or mips-model-case where a model differs. Load the file in files.cmake before exec.lisp.
  3. Add sample values to tests/isa/mips/sem.lisp (in every model that has the instruction) and an independent assembler's word to enc.lisp.

Regenerating

CPM_SOURCE_CACHE=$HOME/.cache/cpm cmake -S . -B build -DXSPE_REGEN=ON
cmake --build build --target mips-regen && git diff src/isa/mips/gen

A new model's target is added to XSPE_MIPSGEN_ARGS (tools/mipsgen/CMakeLists.txt) as name:ISA-level[:binutils CPU flag]. The test mips_regen (REGEN mode only) requires the committed file to match a fresh run.


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