;;;
;;; memory-nubus.lisp
;;;
;;; NuBus interface to the memory board.
;;;

(in-package :nevermore)

(defvar *memory-config-rom* (make-array '(#x800) :element-type '(unsigned-byte 8)
				     :initial-element 0))

(defvar *memory-buffer* (make-array '(#x800000) :element-type '(unsigned-byte 8)
				      :initial-element 0))

(defun memory-nubus-read (address width)
  (cond ((= (logand address #x00ffe000) #x00ffe000)
	 (setf *memory-data* (dpb (aref *memory-config-rom* (ldb (byte 11 2) address)) (byte 8 (* 8 (logand 3 address))) 0)))
	((= (logand address #x00ffffe0) #x00ffc000)
	 ;; control space
	 (setf *memory-data* 0))
	((= (logand address #x00800000) #x00000000)
	 (if (eq width :byte)
	     (setf *memory-data* (dpb (aref *memory-buffer* (logand #x7fffff address)) (byte 8 (* 8 (logand 3 address))) 0))
	   (setf *memory-data* (dpb (aref *memory-buffer* (+ (logand #x7fffff address) 3)) (byte 8 24)
				    (dpb (aref *memory-buffer* (+ (logand #x7fffff address) 2)) (byte 8 16)
					 (dpb (aref *memory-buffer* (+ (logand #x7fffff address) 1)) (byte 8 8)
					      (aref *memory-buffer* (logand #x7fffff address))))))))
	(t (setf *nubus-error* t))))

(defun memory-nubus-write (address width)
  (cond ((= (logand address #x00800000) #x00000000)
	 (if (eq width :byte)
	     (setf (aref *memory-buffer* (logand #x7fffff address)) (ldb (byte 8 (* 8 (logand 3 address))) *memory-data*))
	   (progn
	     (setf (aref *memory-buffer* (+ (logand #x7fffff address) 3)) (ldb (byte 8 24) *memory-data*))
	     (setf (aref *memory-buffer* (+ (logand #x7fffff address) 2)) (ldb (byte 8 16) *memory-data*))
	     (setf (aref *memory-buffer* (+ (logand #x7fffff address) 1)) (ldb (byte 8  8) *memory-data*))
	     (setf (aref *memory-buffer* (+ (logand #x7fffff address) 0)) (ldb (byte 8  0) *memory-data*)))))
	((= (logand address #x00ffffe0) #x00ffc000)
	 ;; control space
	 (case (ldb (byte 3 2) address)
	   (0 ;; memory config register
	    )
	   (2 ;; memory base test register
	    )
	   (4 ;; failure location / test register
	    )
	   (5 ;; status error latch
	    )
	   (6 ;; failure register
	    )))
	(t (setf *nubus-error* t))))

#|
(with-open-file (romfile #p"/home/nyef/src/lisp/aek/E1_eproms/2243924-2_27S291.8MB"
			 :direction :input :element-type '(unsigned-byte 8))
		(dotimes (i #x800)
		  (setf (aref *memory-config-rom* (logxor #x400 i)) (read-byte romfile))))
|#

;;; EOF
