; Load QDOS file, and start QDOS
;
        INCLUDE "includes/MACS.ASM"
        INCLUDE "includes/FileMacs.ASM"
        INCLUDE "includes/TTY.ASM"

       INIT_AMIGA

RED:     EQU   $0F00
GREEN:   EQU   $00F0
BLUE:    EQU   $000F
WHITE:   EQU   $0FFF
YELLOW:  EQU   $0FF0
CYAN:    EQU   $00FF
MAGENTA: EQU   $0F0F
INTREQ:  EQU   $DFF09C
INTREQR: EQU   $DFF01E
INTENA:  EQU   $DFF09A
INTENAR: EQU   $DFF01C
DMACON:  EQU   $DFF096
BPLCON0: EQU   $DFF100
DIWSTRT: EQU   $DFF08E
DIWSTOP: EQU   $DFF090
DDFSTRT: EQU   $DFF092
DDFSTOP: EQU   $DFF094
COP1LC:  EQU   $DFF080
COP2LC:  EQU   $DFF084
COPJMP1: EQU   $DFF088
COPJMP2: EQU   $DFF08A
COPCON:  EQU   $DFF02E
COLOR00: EQU   $DFF180
COLOR01: EQU   $DFF182
COLOR02: EQU   $DFF184
COLOR03: EQU   $DFF186
BLTCPTH: EQU    $DFF048
BLTCPTL: EQU    $DFF04A
BLTBPTH: EQU    $DFF04C
BLTBPTL: EQU    $DFF04E
BLTAPTH: EQU    $DFF050
BLTAPTL: EQU    $DFF052
BLTDPTH: EQU    $DFF054
BLTDPTL: EQU    $DFF056
BLTCMOD: EQU    $DFF060
BLTBMOD: EQU    $DFF062
BLTAMOD: EQU    $DFF064
BLTDMOD: EQU    $DFF066
BLTSIZE: EQU    $DFF058
BLTCON0: EQU    $DFF040
BLTCON1: EQU    $DFF042
BLTAFWM: EQU    $DFF044
BLTALWM: EQU    $DFF046
BLTDDAT: EQU    $DFF000
BLTCDAT: EQU    $DFF070
BLTBDAT: EQU    $DFF072
BLTADAT: EQU    $DFF074
DMACONW: EQU    $DFF096
DMACONR: EQU    $DFF002
; -----------------------------------------------------------------
; getting parameters
      MOVEM.L        A0/D0,-(A7) ; store string and length for further use
      BSR            SKIP_PARM
      MOVE.L         A0,A3       ; store begin of 2nd parameter
	LEA	STO_PC,A5
	BSR	HexA0_D0	; get RAM bottom
	MOVE.L  D0,0(A5)	; store RAM bottom
	BSR	HexA0_D0	; get RAM top
	MOVE.L  D0,8(A5)	; store RAM top
	BSR	HexA0_D0	; get initial SSP
	MOVE.L  D0,4(A5)	; store initial SSP

	MOVEQ	#0,D1		; no restrictions where
	MOVE.L	#$10000,D0	; but we need 64 kB RAM   
   	CALLSYS	AllocMem	; allocate memory
	TST.L	D0
	BEQ	EXIT_1
	MOVE.L	D0,A2		; store address of memory
	LEA	STO_PC,A5
	MOVE.L	D0,16(A5)	; store in 16(A5) too

      MOVEM.L        (A7)+,A0/D0
      MOVE.L         A3,D0       ; get begin of 2nd parameter
      SUB.L          A0,D0       ; subtract begin of first parameter
skip_spc:
      SUBQ.W         #1,D0    ; length -1
      CMP.B          #32,(A0,D0.W) ; blank ?
      BEQ.S          skip_spc
      MOVE.B         #0,1(A0,D0.W) ; replace blank with NULL

; load file to Memory address
      OPEN_IN        1,A0
      FSIZE          1,D0 ; get length of file
; now load File
      LOAD           1,A2
      MOVE.L         CHNR1(PC),D0
      TST.L          D0
      BEQ            EXIT_2
; close file
      FCLOSE         1
; -----------------------------------------------------------------
; get time
;	LINKSYS DateStamp,dos	; get time vector in D1

	MOVE.L	4,A6
	LEA	DOS_Name,A1
	JSR	-408(A6)	; open DOS library
	MOVE.L	D0,A6		; we only want to call a DOS function
	JSR	-192(A6)	; get time vector in D1

	MOVE.L	D1,A0
	MOVE.L	0(A0),D0	; get days since 1.1.1978
	ADD.L	#365*17,D0	; we need days since 1.1.1961
	ADD.L	#4,D0		; add "Schaltjahre"
	MULU	#24*60*60/8,D0	; make seconds since this day
	LSL.L	#3,D0		; only word operand for MULU
	MOVE.L	4(A0),D1	; get minutes since midnight
	MULU	#60,D1		; make seconds
	ADD.L	D1,D0		; now we have seconds since 1.1.1961
	LEA	STO_PC,A5
	MOVE.L	D0,12(A5)	; store as offset for QDOS
	CLR.B	$BFEA01		; reset CIA-A event counter
	CLR.B	$BFE901
	CLR.B	$BFE801
; -------------------------------------------------------------------
; move the whole operating system (48 K) to start of RAM
      LEA      TRAP0,A0
      MOVE.L   A0,$80  ; Initialize TRAP #0 to enter supervisor mode
      TRAP     #0 ; enter supervisor mode
      ORI      #$0700,SR   ; Disable all interrupts
WAITBLIT:
      BTST     #14,DMACONR
      BNE.S    WAITBLIT
      MOVE.W   #$3FFF,INTENA   ; disable all possible interrupt sources
      MOVE.W   #$3FFF,INTREQ   ; clear all pending interrupts
      MOVE.W   #$01FF,DMACON   ; disable DMA (bitplane, copper, blitter)
      MOVE.L   #$FFFFFFFE,D0   ; Copper instruction wait for ever
      MOVE.L   D0,$20000   ; Write "copperlist" to QL-screen
      MOVE.L   #$20000,COP1LC   ; Set copper start address
      MOVE.L   #$20000,COP2LC
      MOVE.W   #0,COPCON   ; inhibit blitter control by copper
      MOVE.W   #0,COPJMP1   ; set copper PC
      LEA      STO_PC,A5
      MOVE.L   16(A5),A0    ; get address of QDOS
      MOVE.L   #0,A1
      MOVE.L   #$3000,D0   ; (48K = $3000 Long words)
MOVQDOS:
	MOVE.L   (A0)+,(A1)+
	DBRA   D0,MOVQDOS
	LEA	STO_PC,A5
	MOVE.L  8(A5),$28020	; set RAM top
	MOVE.W  #$D254,$28000	; set QDOS identification
	MOVE.L	12(A5),$280A2	; set offset for timer
	MOVE.L	4(A5),A7	; get initial SSP
	MOVE.L  A7,0		; store itial SSP
	MOVE.L  4,A0		; get initial PC
	MOVE.L	0(A5),4		; set RAM bottom
	JMP   (A0)		; start Qdos
TRAP0: ADDQ.L #2,A7
      RTS
STO_PC: DS.L 8

DOS_Name: DC.B "dos.library",0
	NOP
HexA0_D0:                 ; get Hex number from text (A0)  -> D0
        CLR.L D0
; Hack leading spaces
Hack_Blank:
         CMP.B    #' ',(A0)+
         BEQ.S    Hack_Blank
         SUBQ.L   #1,A0
LC6642C:
        MOVE.B (A0)+,D1
        CMPI.B #' ',D1    ; space ?
        BEQ.S LC66454
        CMPI.B #10,D1     ; Line Feed ?
        BEQ.S LC66454
        CMPI.B #'A',D1    ; letter 'A' ?
        BLT.S LC66446
        SUBI.B #55,D1
        BRA.S LC6644A
LC66446:
        SUBI.B #$30,D1    ; generate number from 10 to 15
LC6644A:
        ANDI.B #$F,D1     ; only numbers from 10 to 15 are relevant
        LSL.L #4,D0       ; place for next 4 bits
        OR.B D1,D0        ; insert next 4 bits
        BRA.S LC6642C     ; continue until end of Number
LC66454:RTS

SKIP_PARM:                ; skip one Parameter in (A0)
         CMP.B    #' ',(A0)+ ; find first non blank character
         BEQ.S    SKIP_PARM
_skip_l1:
         CMP.B    #' ',(A0)+ ; find first Blank after letters
         BGT.S    _skip_l1
         SUBQ.L   #1,A0
         RTS              ; thats it.
EXIT_1:
     BSR  PRINT
     DC.B 'Sorry, could not allocate memory anyway',10,0
     BRA  EXIT_X
EXIT_2:
     BSR  PRINT
     DC.B 'could not load File',10,0
     BRA  EXIT_X
EXIT_3:
     NOP
EXIT_X:
     MOVEQ #$64,D0 ; signal error
     EXIT_AMIGA

      END
