;;;
;;; romload.lisp
;;;
;;; loaders for all of the romfiles needed for the emulator
;;;

(in-package :nevermore)

;; with-open-files macro by antifuchs of freenode #lisp.
(defmacro with-open-files ((&body openspecs) &body body)
  (if (consp openspecs)
      `(with-open-file ,(first openspecs)
        (with-open-files ,(rest openspecs)
          ,@body))
      `(progn ,@body)))

(defun load-romfiles ()
  (with-open-file (romfile #p"/home/nyef/src/lisp/aek/E1_eproms/2236662_SIB"
			   :direction :input :element-type '(unsigned-byte 8))
    (dotimes (i #x2000)
      (setf (aref *sib-config-rom* i) (read-byte romfile))))
  
  (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))))

  (with-open-files ((f1 #p"/home/nyef/src/lisp/aek/E1_eproms/2236480-03"
			:direction :input :element-type '(unsigned-byte 8))
		    (f2 #p"/home/nyef/src/lisp/aek/E1_eproms/2236481-03"
			:direction :input :element-type '(unsigned-byte 8))
		    (f3 #p"/home/nyef/src/lisp/aek/E1_eproms/2236482-03"
			:direction :input :element-type '(unsigned-byte 8))
		    (f4 #p"/home/nyef/src/lisp/aek/E1_eproms/2236483-03"
			:direction :input :element-type '(unsigned-byte 8))
		    (f5 #p"/home/nyef/src/lisp/aek/E1_eproms/2236484-03"
			:direction :input :element-type '(unsigned-byte 8))
		    (f6 #p"/home/nyef/src/lisp/aek/E1_eproms/2236485-03"
			:direction :input :element-type '(unsigned-byte 8))
		    (f7 #p"/home/nyef/src/lisp/aek/E1_eproms/2236486-03"
			:direction :input :element-type '(unsigned-byte 8)))
    (dotimes (i #x800)
      (let* ((val1 (read-byte f1))
	     (val2 (dpb (read-byte f2) (byte 8 8) val1))
	     (val3 (dpb (read-byte f3) (byte 8 16) val2))
	     (val4 (dpb (read-byte f4) (byte 8 24) val3))
	     (val5 (dpb (read-byte f5) (byte 8 32) val4))
	     (val6 (dpb (read-byte f6) (byte 8 40) val5))
	     (val7 (dpb (read-byte f7) (byte 8 48) val6)))
	(setf (aref *prom-memory* (- #x7ff i)) val7)))))

;;; EOF
