;*---------------------------------------------------------------------------
;  :Program.	visionmd4.asm
;  :Contents.	Slave for "Vision Megademo IV"
;  :Author.	BJ
;  :History.	29.09.96
;  :Requires.	-
;  :Copyright.	Public Domain
;  :Language.	68000 Assembler
;  :Translator.	Barfly V1.131
;  :To Do.
;  :Time.	3h 40min
;---------------------------------------------------------------------------*

	INCDIR	Includes:
	INCLUDE	whdload.i
	INCLUDE	graphics/gfxbase.i
	INCLUDE	lvo/exec.i

	OUTPUT	"wart:Vision MD IV/visionmd4.slave"
	BOPT	O+ OG+			;enable optimizing
	BOPT	w4-			;disable 64k warnings

;======================================================================

.base		SLAVE_HEADER		;ws_Security + ws_ID
		dc.w	1		;ws_Version
		dc.w	WHDLF_Disk	;ws_flags
		dc.l	$80000		;ws_BaseMemSize	
		dc.l	$400		;ws_ExecInstall
		dc.w	.Start-.base	;ws_GameLoader
		dc.w	0		;ws_CurrentDir
		dc.w	0		;ws_DontCache

;======================================================================
.Start	;	A0 = resident loader
;======================================================================

		lea	(_resload,pc),a1
		move.l	a0,(a1)			;save for later use

	;build simple exec=graphics
		lea	$1000,a6	;execbase/graphicsbase
		move.l	a6,4
		patch	_LVOForbid(a6),.forbid
		patch	_LVOPermit(a6),.permit
		patch	_LVOOpenLibrary(a6),.openlibrary
		lea	(-4,a6),a0
		move.l	#-2,(a0)	;copperlist
		move.l	a0,(gb_copinit,a6)
		bra	.go

.openlibrary	move.l	a6,d0
.forbid
.permit		rts

.go
	;init keyboard
		bsr	_KeyboardInit

	;bootblock
		moveq	#0,d0		;offset
		move.l	#2*512,d1	;size
		moveq	#1,d2		;disk
		lea	$7f000-$f4-12,a0
		bsr	_LoadDisk
		jsr	$7f000

		move.l	#$400,d0	;offset
		move.l	#$f400,d1	;size
		moveq	#1,d2		;disk
		lea	$30000,a0	;dest
		bsr	_LoadDisk
		patch	$300ce,.1	;jmp $1a000
		jmp	$30024		;decrunch exe
.1
		lea	$1a000,a6
		lea	.dummyint6c,a0
		move.l	a0,$6c
		lea	.dummyint78,a0
		move.l	a0,$78
		move.w	#INTF_SETCLR|INTF_INTEN|INTF_VERTB|INTF_EXTER,_custom+intena
		patch	$1ec(a6),.load
		jmp	$3c(a6)		;go !

.load		move.l	$1a1a8,a0
		move.l	(a0)+,d1	;size
		move.l	(a0)+,d0	;offset
		moveq	#1,d2		;disk
		lea	$2f000,a0	;dest
		bra	_LoadDisk

.dummyint6c	move.w	#INTF_VERTB,_custom+intreq
		rte
.dummyint78	move.w	#INTF_EXTER,_custom+intreq
		rte

;--------------------------------

_KeyboardInit	lea	_IntLevel2,a0
		move.l	a0,$68
		move.b  #CIAICRF_SP,(ciaicr+_ciaa)      ;nur tastatur
		move.w  #INTF_PORTS,(intreq+_custom)
		move.w  #INTF_SETCLR|INTF_INTEN|INTF_PORTS,(intena+_custom)
		move.b  #~%00000011,(ciapra+_ciaa)
		rts

_IntLevel2	movem.l d0/a1,-(a7)
		lea     (_ciaa),a1
		btst    #CIAICRB_SP,(ciaicr,a1)
		beq     .exit
                
                moveq   #0,d0
                move.b  (ciasdr,a1),d0               ;read ~(raw-code)

                clr.b   (ciasdr,a1)                  ;ausgabe LOW
                or.b    #CIACRAF_SPMODE,(ciacra,a1)  ;auf ausgabe
        
                not.b   d0
                ror.b   #1,d0
                bmi     .keyup

; RAW-Key-Codes:
;	ESC	$45
;	DEL	$46
;	F1..F10	$50..$59
;	HELP	$5f

.keydown	cmp.b	#$58,d0
		bne	.1
		movem.l	(a7)+,d0/a1
		bra	_debug			;coredump & quit
.1
		cmp.b	#$45,d0
		beq	_exit			;exit
		cmp.b	#$59,d0
		beq	_exit			;exit

.keyup
.done           move.b  #-1,(ciasdr,a1)              ;ausgabe HIGH
                and.b   #~(CIACRAF_SPMODE),(ciacra,a1);wieder auf eingabe

.exit           move.w  #INTF_PORTS,(intreq+_custom)
                movem.l (a7)+,d0/a1
                rte

;--------------------------------

_resload	dc.l	0		;address of resident loader

;--------------------------------
; IN:	d0=offset d1=size d2=disk a0=dest
; OUT:	d0=success

_LoadDisk	movem.l	d0-d2/a0-a2,-(a7)
		move.l	(_resload),a2
		jsr	(resload_DiskLoad,a2)
		tst.l	d0
		beq	.err
		movem.l	(a7)+,d0-d2/a0-a2
		moveq	#-1,d0
		rts
		
.err		move.l	d1,-(a7)
		movem.l	(4,a7),d0-d2/a0-a2
		move.l	d2,(4+5*4,a7)		;disk number
		move.l	(a7),(4+4*4,a7)		;doserror
		add.w	#4+4*4,a7
		move.l	#TDREASON_DISKLOAD,-(a7)
		move.l	(_resload),-(a7)
		add.l	#resload_Abort,(a7)
		rts

;--------------------------------

_exit		move.l	#TDREASON_OK,-(a7)
		bra	_end
_debug		clr.l	-(a7)
		clr.l	-(a7)
		move.l	#TDREASON_DEBUG,-(a7)
_end		move.l	(_resload,pc),-(a7)
		add.l	#resload_Abort,(a7)
		rts

;======================================================================

	END
