;*---------------------------------------------------------------------------
;  :Program.	visionmd4.asm
;  :Contents.	Slave for "Vision Megademo IV"
;  :Author.	BJ
;  :History.	29.09.96
;		23.03.97 output path changed
;		24.06.97 keyboard routine fixed
;  :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	"dwart:Vision Megademo IV/visionmd4.slave"
	BOPT	O+ OG+			;enable optimizing
	BOPT	w4-			;disable 64k warnings
	SUPER

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

.base		SLAVE_HEADER		;ws_Security + ws_ID
		dc.w	1		;ws_Version
		dc.w	WHDLF_Disk|WHDLF_NoError	;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

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

	DOSCMD	"WDate >T:date"
		dc.b	"$VER: WEPL "
	INCBIN	"T:date"
		dc.b	0

;======================================================================
.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),.rts
		patch	_LVOPermit(a6),.rts
		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
.rts		rts
.go

	;install keyboard quitter
		lea	(.int2),a0
		move.l	a0,$68				;set vector
		lea	(_ciaa),a1
	;	move.b	#~(CIAICRF_SETCLR),(ciaicr,a1)
		move.b	#CIAICRF_SETCLR|CIAICRF_SP,(ciaicr,a1)	;allow ints from keyboard
		tst.b	(ciaicr,a1)			;clear all intreq
		and.b	#~(CIACRAF_SPMODE),(ciacra,a1)	;input mode
		move.w	#INTF_PORTS,(intreq+_custom)
		move.w	#INTF_SETCLR|INTF_INTEN|INTF_PORTS,(intena+_custom)
		bra	.int2_after
.int2		movem.l	d0-d1/a1,-(a7)
		lea	(_ciaa),a1
		btst	#CIAICRB_SP,(ciaicr,a1)		;check int reason
		beq	.int2_exit
		move.b	(ciasdr,a1),d0			;read code
		clr.b	(ciasdr,a1)			;output LOW (handshake)
		or.b	#CIACRAF_SPMODE,(ciacra,a1)	;to output
		not.b	d0
		ror.b	#1,d0
		cmp.b	#$45,d0				;ESC
		beq	_exit
		cmp.b	#$58,d0				;F9
		bne	.int2_1
		movem.l	(a7)+,d0-d1/a1
		move.w	(a7),(6,a7)			;sr
		move.l	(2,a7),(a7)			;pc
		clr.w	(4,a7)				;ext.l sr
		bra	_debug
.int2_1		cmp.b	#$59,d0				;F10
		beq	_exit
		moveq	#2-1,d1				;wait because handshake min 75 µs
.int2_w1	move.b	(_custom+vhposr),d0
.int2_w2	move.b	(_custom+vhposr),d0		;one line is 63.5 µs
		beq	.int2_w2
		dbf	d1,.int2_w2
		move.b	#-1,(ciasdr,a1)			;output HIGH (handshake)
		and.b	#~(CIACRAF_SPMODE),(ciacra,a1)	;to input
.int2_exit	move.w	#INTF_PORTS,(intreq+_custom)
		movem.l	(a7)+,d0-d1/a1
		rte
.int2_after


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

		move.l	#$400,d0	;offset
		move.l	#$f400,d1	;size
		moveq	#1,d2		;disk
		lea	$30000,a0	;dest
		move.l	(_resload),a2
		jsr	(resload_DiskLoad,a2)
		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
		move.l	a1,-(a7)
		move.l	(_resload),a1
		jsr	(resload_DiskLoad,a1)
		move.l	(a7)+,a1
		rts

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

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

_resload	dc.l	0		;address of resident loader

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

_exit		pea	TDREASON_OK.w
		bra	_end
_debug		pea	TDREASON_DEBUG.w
_end		move.l	(_resload),-(a7)
		addq.l	#resload_Abort,(a7)
		rts

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

	END
