;;;
;;; raven-cpu-nubus.lisp
;;;
;;; CPU NuBus slot space handlers.
;;;

(in-package :nevermore)

(defvar *interrupt-sources* 0
  "A bitmask of enabled interrupt sources")

(defvar *current-interrupt-level* 0
  "Highest-priority active interrupt source or 0")

(defun check-interrupt-status ()
  (setf *current-interrupt-level* 0)
  (dotimes (i 16)
    (unless (zerop (ldb (byte 1 (- 15 i)) *interrupt-sources*))
      (setf *current-interrupt-level* (- 15 i))))
  (if (or (zerop *current-interrupt-level*)
	  (zerop (logand #x8000 *machine-control-register*)))
      (setf *interrupt-pending* nil)
    (setf *interrupt-pending* t)))

(defun cpu-nubus-read (address width)
  (cond ((= (logand address #x00ffffff) #x00c00000)
	 (if (= #x80 (logand #x80 *machine-control-register*))
	     (setf *memory-data* #x3)
	   (setf *memory-data* #x7)))))

(defun cpu-nubus-write (address width)
  (cond ((= (logand address #x00ffffc3) #x00e00000)
	 ;; Interrupt control
	 (let ((interrupt-number (ldb (byte 4 2) address)))
	   (if (zerop (logand 1 *memory-data*))
	       (setf *interrupt-sources* (logand *interrupt-sources* (dpb 0 (byte 1 interrupt-number) #xffff)))
	     (setf *interrupt-sources* (logior *interrupt-sources* (dpb 1 (byte 1 interrupt-number) 0)))))
	 (check-interrupt-status))))


;;; EOF