;;; -*- Mode:LISP; Package:MICRO; Readtable:CL; Base:8 -*-

(defconst %%usim-0-l-offset (byte 4 0))
(defconst %%usim-0-l-base (byte 3 4))
(defconst %%usim-0-r-offset (byte 4 7))
(defconst %%usim-0-r-base (byte 3 11.))
(defconst %%usim-0-d-offset (byte 4 14.))
(defconst %%usim-0-d-base (byte 3 18.))
(defconst %%usim-0-imm (byte 8 21.))

(defconst %%usim-1-adr (byte 26. 0))
(defconst %%usim-1-aluf (byte 26. 0))

(defconst %%usim-2-cont (byte 3 0))
(defconst %%usim-2-cond (byte 4 3))
(defconst %%usim-2-opcode (byte 4 7))
(defconst %%usim-2-stat (byte 4 11.))
(defconst %%usim-2-halt (byte 1 15.))
(defconst %%usim-2-noop-next (byte 1 16.))

(define-micro-function %sim-step ()
 (locality m-mem)
m-sim-src-1-ptr (0)
m-sim-src-2-ptr (0)
m-sim-dest-ptr (0)
m-sim-last-src-1 (0)
m-sim-last-aluf (0)
m-sim-inst-0 (0)
m-sim-inst-1 (0)
m-sim-inst-2 (0)
m-open-frame (0)
m-active-frame (0)
m-return-frame (0)

 (locality a-mem)
a-sim-main-memory (0)
a-sim-frames (0)
a-sim-pc (0)
a-sim-next-pc (0)
a-sim-noop-next-bit (0)
a-sim-last-src-2 (0)
a-sim-last-result (0)

a-sim-src-ptrs-valid (0)

 
 (locality i-mem)
sim-next-inst
	((vma) a-sim-pc)
	((vma) dpb vma (byte 30. 2) a-zero)	;times 4
	((vma-start-read) add vma a-sim-main-memory)
	(check-page-read)
	((m-sim-inst-0) md)
	((vma-start-read) add vma (a-constant 1))
	(check-page-read)
	((m-sim-inst-1) md)
	((vma-start-read) add vma (a-constant 1))
	(check-page-read)
	((m-sim-inst-2) md)

	(call-equal m-zero a-sim-src-ptrs-valid get-src-ptrs)

	((a-sim-pc) a-sim-next-pc)
	((a-sim-next-pc) m+a+1 m-zero a-sim-next-pc)
	((m-a) a-sim-noop-next-bit)
	((a-sim-noop-next-bit) setz)

	(jump-not-equal m-a a-zero sim-inst-exit)

	(dispatch (lisp-byte %%usim-2-opcode) m-sim-inst-2 d-sim-opcode)
	(call trap)
    (error-table sim-error)

 (locality d-mem)
 (start-dispatch 4 0)
d-sim-opcode
	(sim-alu-op n-bit)			;0
	(sim-jump-op n-bit)			;1
	(p-bit r-bit)				;2
	(p-bit r-bit)				;3
	(p-bit r-bit)				;4
	(p-bit r-bit)				;5
	(p-bit r-bit)				;6
	(p-bit r-bit)				;7
	(p-bit r-bit)				;8
	(p-bit r-bit)				;9
	(p-bit r-bit)				;10
	(p-bit r-bit)				;11
	(p-bit r-bit)				;12
	(p-bit r-bit)				;13
	(p-bit r-bit)				;14
	(p-bit r-bit)				;15
 (end-dispatch)
 (locality i-mem)

sim-alu-op
	((vma-start-read) add m-sim-src-1-ptr a-sim-frames)
	(check-page-read)
	((m-sim-last-src-1) md)
	((vma-start-read) add m-sim-src-2-ptr a-sim-frames)
	(check-page-read)
	((a-sim-last-src-2) md)
	((m-sim-last-aluf) ldb (byte 4 0) m-sim-inst-1)
	((oa-reg-low) oal-aluf m-sim-last-aluf)
	((md m-1) m-sim-last-src-1 setz a-sim-last-src-2)
	((vma-start-write) add m-sim-dest-ptr a-sim-frames)
	(check-page-write)
	((a-sim-last-result) m-1)
	(jump sim-inst-exit)

sim-jump-op
	(dispatch (lisp-byte %%usim-2-cond) m-sim-inst-2 d-sim-jump-cond)
	(call trap)
    (error-table sim-error)

 (locality d-mem)
 (start-dispatch 4 0)
d-sim-jump-cond
	(sim-jump-unc n-bit)			;0
	(sim-jump-less-than n-bit)		;1
	(p-bit r-bit)				;2
	(p-bit r-bit)				;3
	(p-bit r-bit)				;4
	(p-bit r-bit)				;5
	(p-bit r-bit)				;6
	(p-bit r-bit)				;7
	(p-bit r-bit)				;8
	(p-bit r-bit)				;9
	(p-bit r-bit)				;10
	(p-bit r-bit)				;11
	(p-bit r-bit)				;12
	(p-bit r-bit)				;13
	(p-bit r-bit)				;14
	(p-bit r-bit)				;15
 (end-dispatch)
 (locality i-mem)

sim-jump-less-than
	(jump-less-than m-sim-last-src-1 a-sim-last-src-2 sim-jump-unc)
	(jump sim-inst-exit)

sim-jump-unc
	((a-sim-next-pc) dpb m-sim-inst-1 (byte 8 0) a-sim-next-pc)
	((a-sim-noop-next-bit) ldb (lisp-byte %%usim-2-noop-next) m-sim-inst-2)
	(jump sim-inst-exit)

sim-inst-exit
	((M-ZR) setz)
	((M-A) setz)
	((M-B) setz)
	((M-C) setz)
	((M-D) setz)
	((M-E) setz)
	((M-T) setz)
	((M-R) setz)
	((M-Q) setz)
	((M-I) setz)
	((M-J) setz)
	((M-S) setz)
	((M-K) setz)

	(popj)

get-src-ptrs
	((m-1) ldb (lisp-byte %%usim-0-l-offset) m-sim-inst-0)
	(dispatch (lisp-byte %%usim-0-l-base) m-sim-inst-0 d-sim-get-src-ptr)
	((m-sim-src-1-ptr) m-1)

	((m-1) ldb (lisp-byte %%usim-0-r-offset) m-sim-inst-0)
	(dispatch (lisp-byte %%usim-0-r-base) m-sim-inst-0 d-sim-get-src-ptr)
	((m-sim-src-2-ptr) m-1)

	((m-1) ldb (lisp-byte %%usim-0-d-offset) m-sim-inst-0)
	(dispatch (lisp-byte %%usim-0-d-base) m-sim-inst-0 d-sim-get-src-ptr)
	((m-sim-dest-ptr) m-1)

	((a-sim-src-ptrs-valid) seto)
	(popj)

 (locality d-mem)
 (start-dispatch 3 0)
d-sim-get-src-ptr
	(sim-get-src-ptr-open p-bit n-bit)
	(sim-get-src-ptr-active p-bit n-bit)
	(sim-get-src-ptr-return p-bit n-bit)
	(sim-get-src-ptr-global p-bit n-bit)
	(sim-get-src-ptr-func p-bit n-bit)
	(sim-get-src-ptr-func p-bit n-bit)
	(sim-get-src-ptr-func p-bit n-bit)
	(sim-get-src-ptr-func p-bit n-bit)
 (end-dispatch)
 (locality i-mem)
sim-get-src-ptr-open
	((m-1) dpb m-open-frame (byte 8 4) a-1)
	(popj)

sim-get-src-ptr-active
	((m-1) dpb m-active-frame (byte 8 4) a-1)
	(popj)

sim-get-src-ptr-return
	((m-1) dpb m-return-frame (byte 8 4) a-1)
	(popj)

sim-get-src-ptr-global
	((m-tem) ldb (lisp-byte %%usim-0-imm) m-sim-inst-0)
	((m-1) dpb m-tem (byte 8 4) a-1)
	(popj)

sim-get-src-ptr-func
	((m-1) add m-1 (a-constant 4096.))
	(popj)

	)