;;; -*- Mode:LISP; Package:USER; Readtable:ZL; Base:10 -*-

(fs:set-logical-pathname-host 
  "K-SYS"
  :physical-host "DJ"
  :translations
  '(("k;"           "<k-4>")
    ("k.*;"         "<k-4.*>")
    ("k.*.*;"       "<k-4.*.*>")
    ("kbug;"        "<k-4.kbug>")
    ("kbug2;"       "<k-4.kbug2>")
    ("kdoc;"        "<k-4.kdoc>")
    ("kdoc.*;"      "<k-4.kdoc.*>")
    ("kdoc.*.*;"    "<k-4.kdoc.*.*>")
    ("fleabit;"     "<k-4.fleabit>")
    ("fleabit.*;"   "<k-4.fleabit.*>")
    ("fleabit.*.*;" "<k-4.fleabit.*.*>")
    ("compiler"     "<k-4.compiler>")
    ("cold"         "<k-4.cold>")
    ("warm"         "<k-4.warm>")
    ("hot"          "<k-4.hot>")))

(defun make-k-system-release (host version)
  (let* ((dir-name-for-creation (format () "~A:K-~A;*.*" host version))
	 (dir-name-for-probe (format () "~A:~~;K-~A.directory" host version)))
    (if (probe-file dir-name-for-probe)
	(ferror "Version ~S of the K system already exists" version))
    (fs:create-directory dir-name-for-creation)
    (fs:copy-directory "jb:k;" dir-name-for-creation :copy-only :newest :report-stream standard-output)
    t))

(defsystem cross-compiler-for-k
  (:name             "K-cross-compiler")  ;;Use the Lambda compiler with new p2.
  (:pathname-default "k-sys:compiler;")
  (:component-systems compiler)
  (:module cross     "cross")
  (:module cross-np2 "cross-np2")
  (:compile-load cross)
  (:compile-load cross-np2 (:fasload cross) (:fasload cross)))

#+not-yet
(defsystem cross-compiler-for-k
  (:name             "K-cross-compiler")  ;;Use the Lambda compiler with new p2.
  (:pathname-default "k-sys:compiler;")
  (:component-systems compiler)
  (:module cross     "cross")
  (:module p2-defs   "p2-defs")
  (:module p2-stuff  ("p2-top" "p2-handlers" "p2-vars" "p2-frames" "p2-flow" "p2-support"))
  (:compile-load cross)
  (:compile-load p2-defs  (:fasload cross) (:fasload cross))
  (:compile-load p2-stuff (:fasload cross p2-defs) (:fasload cross p2-defs)))
  
 
(defsystem compiler-for-k
  (:name "K-Compiler")	;unfortunately conflicts with regular system compiler. rg 12/18/87
  (:pathname-default "k-sys:k;")

  ;;list of modules in the compiler
  (:module k-macros                  "k-macros")
  (:module k-macros-for-k-debugger   "k-macros-for-k-debugger")  ;;;Hack WKF 5/5/88
  (:module primitive-setf            "primitive-setf.lisp")
  (:module conversions               "conversions.lisp")
  (:module global-registers          "global-registers.lisp")
  (:module mini-fasload-opcodes      "mini-fasload-opcodes.lisp")
  (:module new-fasdump               "new-fasdump")
  (:module defstruct                 "defstruct")
  (:module li-imports-for-k-debugger "li-imports")

  (:compile-load                   k-macros)
  (:compile                        k-macros-for-k-debugger)
  (:readfile primitive-setf       ((:fasload k-macros)))
  (:readfile conversions          ((:fasload k-macros)
				   (:readfile primitive-setf)))
  (:readfile global-registers     ((:fasload k-macros)
				   (:readfile primitive-setf conversions)))
  (:readfile mini-fasload-opcodes ((:fasload k-macros)
				   (:readfile primitive-setf conversions global-registers)))
  (:compile-load new-fasdump      ((:fasload k-macros)
				   (:readfile primitive-setf conversions global-registers
					      mini-fasload-opcodes)))
  (:compile-load defstruct        ((:fasload k-macros)
				   (:readfile primitive-setf conversions global-registers
					      mini-fasload-opcodes)
				   (:fasload new-fasdump)))
  (:compile li-imports-for-k-debugger))
  
;;Files which are in both COMPILER-FOR-K and K-DEBUGGER systems.
;;k;k-macros                compiled           compiled
;;k;conversions             read               read
;;k;global-registers        read               read
;;k;mini-fasload-opcodes    read               read
;;k;new-fasdump             compiled           compiled
;;
;;To fix problem created duplicate versions of these files with file-name-FOR-K-DEBUGGER.  WKF 5/5/88

(defsystem k-debugger
  ;; This system is loaded into the K-xxx package hierarchy!!!
  (:name "K-DEBUGGER")
  (:pathname-default "k-sys:kbug;")

  ;; List of modules in the debugger.
  (:module debug-board            "debug-board")
  (:module li-imports             "k;li-imports")
  (:module debug-crock            "debug-crock0")
  (:module debug-macros           "k;k-macros-for-k-debugger")  ;;wkf this is compiled in COMPILER-FOR-K make-system.
  (:module hw-constants           "k;hardware-constants")
  (:module spy-utilities          "new-spy-utilities")
  (:module debug-support          "debug-support")  
  (:module constants              ("k;instructions"  ;this is readfile'd
				   "k;data-types" "k;firm-definitions"
				   "k;alu-opcodes"
				   "k;conversions-for-k-debugger"))
  (:module global-registers       "k;global-registers-for-k-debugger")

    ;following module contains code for both machines.  Compile it for the lambda as well as K.
  (:module both-stuff-1		  ("cold;common-definitions"))
  (:module both-stuff-2           ("cold;streams"                   	;has compile-in-roots. 
				   "k;new-fasdump-for-k-debugger"))

;;We are only loading the cold files here to get defconstants into k-xxx pkgs on lambda for kold-loader.
;;Fix this by moving the defconstants to hardware-constants.  If we want these constants on K put constants onto
;;*warm-eval-list* and make hot-boot work.   --wkf  @@@ +++

  (:module k-stuff                ("cold;trap"              "cold;trap-handlers"  "cold;gc-ram"           "cold;datatype-ram"
				   "cold;nuclear-control"   "cold;memory-map"     "cold;vmem"             "cold;quantum-map"
				   "cold;transporter-ram"   "cold;timers"         "cold;pcd-table"
				   "cold;memory-management" "cold;region-bits"    "cold;region-data"
				   "cold;gc-fault.lisp"     "cold;area-data.lisp" "cold;nubus-interrupts"
				   "cold;memory-interface"  "cold;cons"           "cold;symbols"          "cold;array"
				   "cold;boot"
				   "k;mini-fasload-opcodes-for-k-debugger"
				   ))
  (:module kold-loader    	  "k;kold-loader")
  (:module lambda-stuff-2         ("kbug;kbug"          "kbug2;kbug2"
				   "Kbug2;kbug-generic" "k;warm-files"))

  (:compile-load  debug-board)
  (:fasload       li-imports    (:fasload debug-board))             ;;This should be compiled in COMPILER-FOR-K make-system.
  (:compile-load  debug-crock   (:fasload debug-board li-imports)
				(:fasload debug-board li-imports))
  (:fasload       debug-macros  (:fasload debug-board debug-crock)) ;;This should be compiled in COMPILER-FOR-K make-system
  (:readfile      hw-constants  (:fasload debug-crock debug-board debug-macros))
  (:compile-load  spy-utilities ((:fasload debug-crock debug-board debug-macros)
				 (:readfile hw-constants))
			        ((:fasload debug-crock debug-board debug-macros)
				 (:readfile hw-constants)))
  (:compile-load  debug-support ((:fasload debug-crock debug-board debug-macros)
				 (:readfile hw-constants)
				 (:fasload spy-utilities))
			        ((:fasload debug-crock debug-board debug-macros)
				 (:readfile hw-constants)
				 (:fasload spy-utilities)))
  (:readfile      constants     ((:fasload debug-crock debug-support)  ;;might be screwed by changes to (debug-macros)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)))
  (:readfile   global-registers ((:fasload debug-crock debug-support)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)
				 (:readfile constants)))
  (:compile-load  both-stuff-1  ((:fasload debug-crock debug-support)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)
				 (:readfile constants global-registers))
				((:fasload debug-crock debug-support)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)
				 (:readfile constants global-registers)))
  (:compile-load  both-stuff-2  ((:fasload debug-crock debug-support)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)
				 (:readfile constants global-registers)
				 (:fasload both-stuff-1))
				((:fasload debug-crock debug-support)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)
				 (:readfile constants global-registers)
				 (:fasload both-stuff-1)))
  (:readfile      k-stuff       ((:fasload debug-crock debug-support)  ;;might be screwed by changes to (debug-macros)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)
				 (:readfile constants global-registers)
				 (:fasload both-stuff-1 both-stuff-2)))
  (:compile-load-init
     kold-loader (debug-macros) ((:fasload debug-crock debug-support)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)
				 (:readfile constants global-registers)
				 (:fasload both-stuff-1 both-stuff-2)
				 (:readfile k-stuff))
				((:fasload debug-crock debug-support)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)
				 (:readfile constants global-registers)
				 (:fasload both-stuff-1 both-stuff-2)
				 (:readfile k-stuff)))
  (:compile-load lambda-stuff-2 ((:fasload debug-crock debug-support)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)
				 (:readfile constants global-registers)
				 (:fasload both-stuff-1 both-stuff-2)
				 (:readfile k-stuff)
				 (:fasload kold-loader))
				((:fasload debug-crock debug-support)
				 (:readfile hw-constants)
				 (:fasload spy-utilities debug-support)
				 (:readfile constants global-registers)
				 (:fasload both-stuff-1 both-stuff-2)
				 (:readfile k-stuff)
				 (:fasload kold-loader))))

(defsystem k-sysdef   ;;Used for getting the most recent version of this file.
  (:name "K-SYSDEF")
  (:pathname-default "k-sys:k;")
  ;; List of modules in the debugger.
  (:module sysdef   ("sysdef" "sysdef-fleabit"))
  (:module packages  "package-definitions")
  (:module k-imports "imported-syms")
  (:module illop     "illop")
  (:module wimp      "kbug2;wimp-terminal")
  (:readfile sysdef)
  (:readfile packages  ((:readfile sysdef)))
  (:readfile k-imports ((:readfile sysdef) (:readfile packages)))
  (:compile-load illop ((:readfile sysdef) (:readfile packages) (:readfile k-imports))
		       ((:readfile sysdef) (:readfile packages) (:readfile k-imports)))
  (:compile-load wimp  ((:readfile sysdef) (:readfile packages) (:readfile k-imports) (:fasload illop))
		       ((:readfile sysdef) (:readfile packages) (:readfile k-imports) (:fasload illop))))

(pushnew :for-falcon *features*) ;;Make #+for-falcon work.

(defvar *k-system-loaded* nil "T if load-k-system function has run.")

(defun make-cold-band (&optional warnings)
  "This builds a K world including cold download.
To then boot the K:  (lam:k-local-setup xx) (mega-boot :fast t)"
  (make-k-system (if warnings :just-warn t))
  (telnet:without-more-processing *terminal-io*
    (let ((*package* (find-package 'k-kbug)))
      (format t "~%~%Making the Cold Load.")
      (make-cold-load :format-stream warnings))))

(defun load-k-system ()
  "This function will load all the K software needed for a mega-boot, without compiling or warnings."
  (make-k-system t))

(defun update-k-system ()
  "This function will update a K world with warnings and the current system changes, compiling as needed."
  (make-k-system :just-warn :compile))

(defun make-k-system (&optional inhibit-warnings &rest make-system-keywords)
  (telnet:without-more-processing *terminal-io*
    (let ((inhibit-fdefine-warnings inhibit-warnings)
	  (*package* (find-package 'user)))
      (setq si::inhibit-displacing-flag t)  ;; prevent moby lossage with fucking si::displaced macros.
      (make-sysdef-system)
      (make-k-system-internal make-system-keywords)
      (setq *k-system-loaded* t))))

(defun make-k-system-internal (make-system-keywords &aux (sys-keywords (cons :noconfirm (cons :no-reload-system-declaration
											      make-system-keywords))))
      (make-fleabit-system        sys-keywords)
      (make-compiler-system       sys-keywords)
      (make-debugger-system       sys-keywords)
      (make-cross-compiler-system sys-keywords))

(defun make-sysdef-system ()
  (format t "~%~%Make 'k-sysdef system")
  (make-system 'k-sysdef :noconfirm))

(defun make-fleabit-system (make-system-keywords)
  (format t "~%~%Make 'fleabit system")
  (let ((*package* (find-package 'global)))
    (apply 'make-system 'fleabit make-system-keywords)))

(defun make-debugger-system (make-system-keywords)
  (format t "~%~%Make 'K-debugger system")
  (let ((*package* (find-package 'k-user)))
    (apply 'make-system 'k-debugger make-system-keywords)))

(defun make-compiler-system (make-system-keywords)
  (format t "~%~%Make 'compiler-for-k system")
  (apply 'make-system 'compiler-for-k make-system-keywords))

(defun make-cross-compiler-system (make-system-keywords)
  (format t "~%~%Make 'cross-compiler-for-k system")
  (apply 'make-system 'cross-compiler-for-k make-system-keywords))


;;This is decomitted.
(defun load-k-system-on-lambda (&rest make-system-keywords)
  "This function will load all the K software needed for a mega-boot"
  "Type (load-k-system :keywords) instead"
  )

;;***************************************************************************************************

(defparameter *cold-files*
	      '("k-sys:cold;trap"
		"k-sys:cold;trap-handlers"
		"k-sys:cold;nuclear-control"
		"k-sys:cold;gc-ram"
		"k-sys:cold;datatype-ram"
		"k-sys:cold;memory-map"
		"k-sys:cold;vmem"
		"k-sys:cold;transporter-ram"
		"k-sys:cold;timers"
		"k-sys:cold;pcd-table"
		"k-sys:cold;quantum-map"
		"k-sys:cold;memory-management"
		"k-sys:cold;region-bits"
		"k-sys:cold;map-fault"
		"k-sys:cold;region-data"
		"k-sys:cold;gc-fault"
		"k-sys:cold;area-data"
		"k-sys:cold;nubus-interrupts"
		"k-sys:cold;memory-interface"
		"k-sys:cold;type-predicates" 
		"k-sys:cold;cons"		
		"k-sys:cold;array"	
		"k-sys:cold;symbols"		
		"k-sys:cold;structure"	
		"k-sys:cold;common-definitions"
		"k-sys:cold;streams"		
		"k-sys:cold;k2"			
		"k-sys:cold;warm-loader"		
		"k-sys:cold;lisp-internals"		
		"k-sys:cold;error"			
		"k-sys:cold;dt-ovf-trap"		
		"k-sys:cold;boot"			
		))
  
(defparameter *warm-loaded-files*
	      ;; These are the files which are needed to get the package system and interpreter running.
	      '("k-sys:warm;generic"
		"k-sys:warm;arithmetic"
		"k-sys:warm;convert"
		"k-sys:warm;fixnum"
		
		"k-sys:warm;array2"
		"k-sys:warm;character"
		"k-sys:warm;string"
		
		"k-sys:warm;lists"
		"k-sys:warm;bald"
		"k-sys:warm;nseq"
		
		"k-sys:warm;equal"
		"k-sys:warm;hash"
		
		"k-sys:warm;throw"
		"k-sys:warm;stack-groups"
		"k-sys:warm;control-pdl"
		"k-sys:warm;boot-stack-groups"
		"k-sys:warm;package"
		"k-sys:warm;warm-boot"
		
		"k-sys:warm;vanilla-interpreter"
		"k-sys:warm;defmacro"
		"k-sys:warm;top-level-forms"
		
		"k-sys:warm;miscellaneous-functions"
		))

(defparameter *hot-loaded-files*
	      ;;These are all the other K source files we want to load.
	      '(
	       ("k-sys:hot;readtable" NIL)           ;;Dont't load KENV file
		"k-sys:hot;reader"
	       ("k-sys:hot;high-level-streams" NIL)  ;;Dont't load KENV file
		"k-sys:hot;printer"
		"k-sys:hot;format"
		
		"k-sys:hot;mini-lisp-listener"
		
;;              "k-sys:hot;vcmem-driver"
;;              "k-sys:hot;k-uc-tv"
		
		"k-sys:hot;bignum"
		"k-sys:hot;float"
		"k-sys:hot;rational"
		"k-sys:hot;complex"
		
;;              "k-sys:hot;hot-boot"
		
		"k-sys:hot;cross-support"	;functions in here exist mostly for cross compiler.
		))

(defun remove-kenv-flags (file-list &aux answer)
  (dolist (file file-list (nreverse answer))
    (setq answer (cons (if (consp file) (car file) file)
		       answer))))

;;***************************************************************************************************
;;***         the following defsystems are currently useful only for tags search.                 ***
;;***************************************************************************************************

(defsystem k-diag
  (:name "K-DIAG")
  (:pathname-default "k-sys:kb;")
  (:module stuff (;"k-regadr" "k-config" "k-regint"
		  "kbug;spy-diags"
		  "k-sys:k;test-strategy" "k-sys:kbug;test-vectors" "k-sys:kbug;test-vectors-support"))
  (:compile-load stuff)	   
  )

(defsystem k-cold-load
   (:name "K-COLD-LOAD")
   (:pathname-default "k-sys:k;")
   (:module cold-load #.*cold-files*)
   (:compile-load cold-load))

(defsystem k-warm-load
 ;useful only for tags search.
  (:name "K-WARM-LOAD")
  (:pathname-default "k-sys:k;")
  (:module stuff #.*warm-loaded-files*)
  (:compile-load stuff)
  )

(defsystem k-hot-load
 ;useful only for tags search
  (:name "K-HOT-LOAD")
  (:pathname-default "k-sys:k;")
  (:module stuff #.(remove-kenv-flags *hot-loaded-files*))
  (:compile-load stuff)
  )

(defsystem k-code
  ;useful only for tags search
  (:name "K-CODE")
  (:component-systems k-cold-load k-warm-load k-hot-load))
		     

(defsystem k-misc
 ;useful only for tagsearch.
  (:name "K-MISC")
  (:module stuff ("k-sys:k;imported-syms"
		  "k-sys:k;daisy-prom"
		  "k-sys:k;daisy-sim"
		  "k-sys:kbug2;wimp-terminal"
		  "k-sys:k;lambda-to-k-streams"
		  ))
  (:compile-load stuff)
  )

(defsystem k-runtime
  (:name "K-RUNTIME")
  (:component-systems k-cold-load k-warm-load k-hot-load k-debugger k-diag k-sysdef)
  )

(setq si:*source-file-types*
      (pushnew :botex si:*source-file-types*))

(si:define-simple-transformation :botex ignore nil (:botex) NIL)

(defsystem k-documentation
  (:name "k-DOCUMENTATION")
  (:pathname-default "k-sys:kdoc.falcon;")
  (:module k-technical-manual
	   ; contains Falcon manual
	   ("title-page.botex"
	    "copyright-page.botex"
	    "introduction.botex"
	    "architecture-overview.botex"
	    "timing.botex"
	    "instruction-set.botex"
	    "program-counter.botex"
	    "call-hardware.botex"
	    "instruction-cache.botex"
	    "functional-io.botex"
	    "alu-opcodes.botex"
	    "transporter-ram.botex"
	    "gc-ram.botex"
	    "storage-conventions.botex"
	    ))
  (:botex k-technical-manual))

(defsystem cross-compiler-for-tags-search
  (:name             "K-cross-compiler")  ;;Use the Lambda compiler with new p2.
  (:pathname-default "k-sys:compiler;")
;  (:component-systems compiler)
  (:module cross     "cross")
  (:module cross-np2 "cross-np2")
  (:compile-load cross)
  (:compile-load cross-np2 (:fasload cross) (:fasload cross)))

#+not-yet
(defsystem cross-compiler-for-tags-search
  (:name             "K-cross-compiler")  ;;Use the Lambda compiler with new p2.
  (:pathname-default "k-sys:compiler;")
;  (:component-systems compiler)
  (:module cross     "cross")
  (:module p2-defs   "p2-defs")
  (:module p2-stuff  ("p2-top" "p2-handlers" "p2-vars" "p2-frames" "p2-flow" "p2-support"))
  (:compile-load cross)
  (:compile-load p2-defs  (:fasload cross) (:fasload cross))
  (:compile-load p2-stuff (:fasload cross p2-defs) (:fasload cross p2-defs)))

(defsystem k-everything
  ;this should include every single file in any way connected with K.
  ;useful for tags search.
  (:name "K-EVERYTHING")
  (:component-systems k-runtime compiler-for-k fleabit cross-compiler-for-tags-search k-misc k-documentation))
