MIPS as a guest: per model, the executor, decoder, step and run loop.
(defun mips-fetch) source(defun mips-fetch (addr)
"the instruction word at ADDR: 32 bits, little-endian"
(declare (type (unsigned-byte 32) addr))
(mips-load 32 addr))(defmacro define-mips-cpu) source(defmacro define-mips-cpu (m)
(let ((isa (mips-model-isa m))
(execute (mips-sym m "EXECUTE")) (decode (mips-sym m "DECODE"))
(execute-w (mips-sym m "EXECUTE-W"))
(illegal (mips-sym m "ILLEGAL")) (step (mips-sym m "STEP")) (run (mips-sym m "RUN"))
(delay (member :load-delay (getf (mips-model-plist m) :features))))
`(progn
(defun ,illegal (cpu insn)
(declare (type mips cpu) (type (unsigned-byte 32) insn) (ignore insn))
(setf (mips-exc cpu) +exc-illegal+))
(define-executor ,execute ,isa :illegal ,illegal)
(define-decoder ,decode ,isa)
,@(when delay
;; M-execute that also returns what the instruction wrote (header)
`((defun ,execute-w (cpu insn)
(declare (type mips cpu) (type (unsigned-byte 32) insn))
,(decode-tree (isa-insn-list isa) 0 #'mips-delay-leaf `(progn (,illegal cpu insn) 0)))))
(defun ,step (cpu)
(declare (type mips cpu))
,@(mips-hook m "STEP-PRE")
(if (/= (wrap 2 (mips-pc cpu)) 0)
(setf (mips-exc cpu) +exc-adel+)
(let ((insn (mips-fetch (mips-pc cpu)))
,@(when delay '((r (mips-ldr cpu)) (w 0))))
,@(when delay
'((setf (mips-lfr cpu) r (mips-lfv cpu) (mips-ldv cpu) (mips-ldr cpu) 0)))
(setf (mips-nnpc cpu) (wrap 32 (+ (mips-npc cpu) 4)) (mips-jmp cpu) 0)
,(if delay
`(setq w (,execute-w cpu insn))
`(,execute cpu insn))
,@(when delay
;; the load in flight lands unless the instruction wrote its register
'((when (and (/= r 0) (or (/= (mips-exc cpu) 0) (/= (wrap 5 w) r)))
(setf (aref (mips-gpr cpu) r) (mips-lfv cpu)))
(setf (mips-lfr cpu) 0)))
,@(mips-hook m "STEP-POST")
(when (= (mips-exc cpu) 0)
(setf (mips-pc cpu) (mips-npc cpu)
(mips-npc cpu) (mips-nnpc cpu))
,(if delay
'(setf (mips-bd cpu) (bit-at w 5) (mips-bt cpu) (mips-jmp cpu))
'(setf (mips-bd cpu) (mips-jmp cpu)))))))
;; step until pc = STOP, an exception is pending, or MAX steps ran; ->
;; the number of steps (the contract of ppu-run, docs/rec.md)
(defun ,run (cpu stop max)
(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) (/= (mips-exc cpu) 0) (>= n max)) (return n))
(,step cpu)
(setq n (+ n 1))))))))(defmacro define-mips-cpus) source(defmacro define-mips-cpus ()
`(progn ,@(mapcar (lambda (m) `(define-mips-cpu ,m)) (mips-models))))(defun mips-reset) source(defun mips-reset (cpu pc)
"start CPU at PC: npc and nnpc follow it, nothing pending"
(declare (type mips cpu) (type (unsigned-byte 32) pc))
(setf (mips-pc cpu) pc
(mips-npc cpu) (wrap 32 (+ pc 4))
(mips-nnpc cpu) (wrap 32 (+ pc 8))
(mips-bd cpu) 0 (mips-jmp cpu) 0 (mips-ldr cpu) 0 (mips-exc cpu) 0
(mips-lfr cpu) 0 (mips-bt cpu) 0))Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.