;-*- Mode:LISP; Base:8; readtable: ZL -*-

(LOCALITY A-MEM)
a-crash-record-pointer	(0)
(LOCALITY I-MEM)

;;; call very early in boot process
;;; allocate a crash record to this boot.
;;; the crash record pointer always points to a 
;;; a free crash record. So, save the current 
;;; crash record pointer and increment it to point
;;; to the next free crash record.
setup-crash-record-pointer
		; read crash record pointer
	(Call-xct-next Read-NVRAM-16)
       ((vma) (a-constant 144.))
	((a-crash-rec-pointer) m-1)
		; read last crash record pointer
	(Call-xct-next Read-NVRAM-16)
       ((vma) (a-constant 160.))
	(jump-equal m-1 a-crash-rec-pointer set-crash-record-pointer)
		; increment crash record pointer and write to NVRAM
	(Call-xct-next Read-NVRAM-16)
       ((vma) (a-constant 152.))
	(jump-xct-next write-crash-record-pointer)
       ((md) add m-1 a-crash-rec-pointer)
		; set crash record pointer to first record
set-crash-record-pointer
	(Call-xct-Next Read-NVRAM-16)
       ((vma) (a-constant 168.))
	((md) m-1)
write-crash-record-pointer
	(Call-xct-next Write-NVRAM-16)
       ((vma) (a-constant 144.))
	(popj)

;;; *jump* here from illop to save crash record info
;;; probably want test at illop to not call here for
;;; debugging purposes as this code trashes some registers

;;; Things yet to be saved
;;; Progress 0
;;; CONTROLLER 4
;;; Ucode-Unit 8.
;;; Load-Unit 12.
;;; Ucode-Part 16.
;;; Load-Part 32.
;;; Ucode-Version 48.
;;; Report-Flags 112.
;;; HALT-KIND 124.
;;; M-FEF 192.

write-crash-record
		; save registers
	((m-a) md)
	((m-b) vma)
		; halt address
	((md) micro-stack-data)
	(call-xct-next Write-CR-16)
       ((vma) (a-constant 116.))
		; save m-1
	((md) m-1)
	(call-xct-next Write-CR-32)
       ((vma) (a-constant 128.)
		; save m-2
	((md) m-2)
	(call-xct-next Write-CR-32)
       ((vma) (a-constant 144.)
		; save MD
	((md) m-a)
	(call-xct-next Write-CR-32)
       ((vma) (a-constant160.))
		; save VMA
	((md) m-b)
	(call-xct-next Write-CR-32)
       ((vma) (a-constant 176.))
		; halt machine
	(no-op halt-cons)
	

; read a 16 bit number from the nvram
; vma has the offset of the first byte (low byte)
; result in m-1, trashes md and m-tem
Read-NVRAM-16
	(call-xct-next nubus-read)
       ((vma) add vma (a-constant #xF5FA0000))
	((m-1) ldb md (byte-field 10 0))
	(call-xct-next nubus-read)
       ((vma) add vma (a-constant 4))
	(popj-xct-next)
       ((m-1) dpb md (byte-field 10 10) a-1)

; write a 32 bit number to the crash record
; vma has the offset of the first byte (low byte)
; md has the value, trashes m-tem and m-1
Write-CR-32
	((vma) add vma a-crash-rec-pointer)
		; drop through to Write-NVRAM-32

; write a 32 bit number to the nvram
; vma has the offset of the first byte (low byte)
; md has the value, trashes m-tem and m-1
Write-NVRAM-32
	((m-1) md)
	(call-xct-next nubus-write)
       ((vma) add vma (a-constant #xF5FA0000))
	((md) ldb (byte-field 10 10) m-1)
	(call-xct-next nubus-write)
       ((vma) add vma (a-constant 4))
	((md) ldb (byte-field 10 20) m-1)
	(call-xct-next nubus-write)
       ((vma) add vma (a-constant 4))
	((md) ldb (byte-field 10 30) m-1)
	(call-xct-next nubus-write)
       ((vma) add vma (a-constant 4))
	(popj)

; write a 16 bit number to the crash record
; vma has the offset of the first byte (low byte)
; md has the value, trashes m-tem and m-1
Write-CR-16
	((vma) add vma a-crash-rec-pointer)
		; drop through to Write-NVRAM-16

; write a 16 bit number to the nvram
; vma has the offset of the first byte (low byte)
; md has the value, trashes m-tem and m-1
Write-NVRAM-16
	((m-1) md)
	(call-xct-next nubus-write)
       ((vma) add vma (a-constant #xF5FA0000))
	((md) ldb (byte-field 10 10) m-1)
	(call-xct-next nubus-write)
       ((vma) add vma (a-constant 4))
	(popj)
