;;; -*- Mode:LISP; Package:LAMBDA; Base:8; Readtable:ZL -*-

(DefConst exp-sib-nvram-offset #xFA0000)

      (setq NVRAM-SLOT TV:TV-QUAD-SLOT
	    NVRAM-SLOT-OFFSET si:SIB-NVRAM-OFFSET)

(Defsubst Exp-read-nVRAM (offset)
  "Read byte of NVRAM at address OFFSET from start of NVRAM."
  (%NuBus-Read-8 NVRAM-SLOT (+ NVRAM-SLOT-OFFSET offset)))

(Defsubst Exp-write-nVRAM (offset value)
  "Write least significant byte of VALUE into NVRAM at address OFFSET from start of NVRAM."
  (%NuBus-Write-8 NVRAM-SLOT (+ NVRAM-SLOT-OFFSET offset) value))

(Defsetf Exp-read-nVRAM Exp-write-nVRAM)

(Defun Exp-read-nVRAM-16B (offset)
  "Read half-word of NVRAM (LSB first) at address OFFSET from start of NVRAM." 
  (Let ((lo-8 (Exp-read-nVRAM offset))
	(hi-8 (Exp-read-nVRAM (+ offset 4))))
    (Dpb hi-8 1010 lo-8)))

(Defun Exp-write-nVRAM-16B (offset value)
  "Write least significant 16 bits of VALUE into NVRAM.  LSB is written first."
  (Let ((lo-8 (Ldb 0010 value))
	(hi-8 (Ldb 1010 value)))
    (Exp-write-nVRAM offset lo-8)
    (Exp-write-nVRAM (+ offset 4) hi-8)))

(DefSetf Exp-read-nVRAM-16B Exp-write-nVRAM-16B)

(Defun Exp-write-nVRAM-24B (offset value)
  "Write least significant 24 bits of VALUE into NVRAM, LSB first."
  (Let ((lo-8 (Ldb 0010 value))
	(mid-8 (Ldb 1010 value))
	(hi-8 (Ldb 2010 value)))
    (Exp-write-nVRAM offset lo-8)
    (Exp-write-nVRAM (+ offset 4) mid-8)
    (Exp-write-nVRAM (+ offset 8.) hi-8)))

(Defun Exp-read-nVRAM-24B (offset)
  "Read 3 bytes of NVRAM starting at OFFSET, LSB first."
  (Let ((lo-8 (Exp-read-nVRAM offset))
	(mid-8 (Exp-read-nVRAM (+ offset 4)))
	(hi-8 (Exp-read-nVRAM (+ offset 8.))))
    (Dpb hi-8 2010 (Dpb mid-8 1010 lo-8))))

(DefSetf Exp-read-nVRAM-24B Exp-write-nVRAM-24B)

(Defun Exp-write-nVRAM-32B (offset value)
  "Write least significant 32 bits of VALUE into NVRAM, LSB first."
  (Let ((lo-16 (Ldb 0020 value))
	(hi-16 (Ldb 2020 value)))
    (Exp-write-nVRAM-16B offset lo-16)
    (Exp-write-nVRAM-16B (+ offset 8.) hi-16)))

(Defun Exp-read-nVRAM-32B (offset)
  "Read 4 bytes of NVRAM starting at OFFSET, LSB first."
  (Let ((lo-16 (Exp-read-nVRAM-16B offset))
	(hi-16 (Exp-read-nVRAM-16B (+ offset 8.))))
    (Dpb hi-16 2020 lo-16))) 

(DefSetf Exp-read-nVRAM-32B Exp-write-nVRAM-32B)