***************************************************
*  KICKPATCH V1.3 - ändert Kickstart in df0: zur  *
*           ANTI-VIRUS KICKSTART V1.3             *
*            FÜR KICKSTART V1.3 34.5              *
*           written by Dirk Meyer 1989            *
*         © BY MAXON COMPUTER GmbH 1990           *
***************************************************

run:
	move.l	4.w,a6			; execbase
	move.l	#$40200,d0		; 513 KB
	move.l	#$10002,d1		; CHIP + CLR
	jsr	-198(a6)		; AllocMem
	move.l	d0,kick_buffer
	bne.s	kick_alloced
	lea	no_kick_buffer(pc),a0
	bra	kick_error
kick_alloced:
	sub.l	a1,a1
	jsr	-294(a6)		; Findtask
	move.l	d0,reply_port+16
	lea	reply_port(pc),a1
	jsr	-354(a6)		; Addport

	lea	trackdiskname(pc),a0
	lea	disk_io(pc),a1
	move.l	#reply_port,14(a1)
	moveq	#0,d0			; Laufwerk df0:
	moveq	#0,d1			; keine Flags
	jsr	-444(a6)		; OpenDevice
	beq.s	trackdisk_ok
	lea	no_td(pc),a0
	bra	kick_error
trackdisk_ok:
	btst	#6,$bfe001		; Auf LMB warten
	bne.s	trackdisk_ok

	moveq	#0,d0			; io_offset
	move.l	#$40200,d1		; io_length
	move.l	kick_buffer,d2		; io_adr
	bsr	read_track		; Kickstart lesen
	move.l	kick_buffer,a0
	cmp.l	#"KICK",(a0)		; Kickstart Disk ?
	beq.s	kick_ok
	lea	not_kick_disk(pc),a0
	bra	kick_error
kick_ok:
	lea	start_patch(pc),a0
	move.l	kick_buffer,a3
	add.l	#$200,a3
copy_loop:
	cmp.w	#$4afc,(a0)		; Illegal-Befehl ?
	beq.s	set_new_adr
	cmp.l	#"ENDE",(a0)		; End-Kennung ?
	beq.s	end_of_copy
	move.w	(a0)+,(a2)+ 
	bra.s	copy_loop
set_new_adr:
	addq.l	#2,a0			; Illegal überspr.
	move.l	(a0)+,a1		; Startadr nach d0
	sub.l	#$fc0000,a1		; Offset berechnen
	move.l	a3,a2			; Startadr nach a2
	add.l	a1,a2
	bra.s	copy_loop
end_of_copy:	
	move.l	kick_buffer,a0
	add.l	#$200,a0
	move.l	a0,a1
	add.l	#$40000-$200+$1e8,a0	; Checksumoffset
	clr.l	(a0)			; clr Checksum
	move.l	#$40000/4,d1		; $10000 Langwörter
	move.l	d1,d2
	swap	d2
	moveq	#0,d0
	bra.s	checksum_loop
checksum_loop2:
	add.l	(a1)+,d0		; Checksum berechnen
	bcc.s	checksum_loop		; kein Übertrag -->
	addq.l	#1,d0			; Übertrag addieren
checksum_loop:
	move.w	(a1),$dff180		; just for fun ....
	dbra	d1,checksum_loop2
	dbra	d2,checksum_loop2
	not.l	d0			; Ergebnis invert.
	move.l	d0,(a0)			; neue Checksumme
wait:
	btst	#6,$bfe001		; auf LMB warten
	bne.s	wait
	moveq	#0,d0			; io_offset
	move.l	#$40200,d1		; io_length
	move.l	kick_buffer,d2		; io_adr
	bsr.s	write_track
not_kick_disk:
	lea	reply_port(pc),a1
	jsr	-360(a6)		; Remport
	lea	disk_io(pc),a1
	jsr	-450(a6)		; CloseDevice
no_td:
	move.l	kick_buffer,a1
	move.l	#$40200,d0		; 513 KB
	jsr	-210(a6)		; Freemem
no_kick_buffer:
	moveq	#0,d0
	rts

kick_error:
	move.w	#$fff,$dff180		; Errorroutine
	jmp	(a0)

read_track:
	lea	disk_io(pc),a1
	move.w	#2,28(a1)		; io_command = Read
	bra.s	do_it
write_track:
	lea	disk_io(pc),a1
	move.w	#3,28(a1)		; io_command = Write
do_it:
	move.l	d0,44(a1)		; io_offset
	move.l	d1,36(a1)		; io_length
	move.l	d2,40(a1)		; io_data - Adresse
	jsr	-456(a6)		; read / write
	cmp.w	#3,28(a1)		; wurde gelesen ?
	bne.s	no_read			; -->
	lea	disk_io(pc),a1
	move.w	#4,28(a1)		; cmp_update
	jsr	-456(a6)
no_read:
	lea	disk_io(pc),a1
	move.w	#9,28(a1)		; io_command - Motor
	move.l	#0,36(a1)		; Motor off
	jsr	-456(a6)		; DoIo
	rts

trackdiskname:	dc.b	"trackdisk.device",0
even
kick_buffer:	dc.l	0
disk_io:	blk.l	20,0
reply_port:	blk.l	8,0

***********************************************************
start_patch:
	illegal				; Kennung neue Adr
	dc.l	$fc0172
	jmp	reset_vector_check-check_bootblock+$fc2336


	illegal				; Kennung neue Adr
	dc.l	$fc0258
	nop				; hier stand mal ..
	nop			     ; move.w #$888,$180(a0)
	nop

	illegal				; Kennung neue Adr
	dc.l	$fc043a
	nop				; hier wurde mal das
	nop				; Romwrack init.

	illegal				; Kennung neue Adr
	dc.l	$fc2336
					; Start Rom-Wrack
check_bootblock:
	btst	#10,$dff016		; Rechte Maustaste ? 
	beq.s	execute_bootblock
	lea	bootstart(pc),a0	; Begin ORG-Bootcode
	lea	12(a4),a1		; Begin Bootcodes
	moveq	#bootend-bootstart-1,d0
boot_search:
	cmpm.b	(a0)+,(a1)+		; vergleichen
	bne.s	unknown_bootblock
	dbra	d0,boot_search
execute_bootblock:
	lea	44(a5),a1		; io_request
	jsr	12(a4)			; jump in Bootcode 
	jmp	$fe85ca

unknown_bootblock:
	lea	alert_text(pc),a0
	lea	1160(a4),a1		; Alertbuffer
	move.w	#alert_size-1,d0
alert_copy:
	move.b	(a0)+,(a1)+
	dbra	d0,alert_copy

	move.l	a4,a0			; Bootblock_adr
	addq.l	#4,a0			; "DOS" überspringen
	moveq	#17-1,d3		; 17 Zeilen
	moveq	#80,d2			; y_pos = 80
convert_loop2:	
	move.w	#20,(a1)+		; x_pos = 20
	move.b	d2,(a1)+		; y_pos
	moveq	#60-1,d1		; 60 Bytes (1 Zeile)
convert_loop:
	move.b	(a0)+,d0		; 1. Byte Bootblook
	cmp.b	#$20,d0			; > erstes Ascii-
	bge.s	ascii			; Zeichen !!!
	cmp.b	#$80,d0			; < letzes Ascii-
	blt.s	ascii			; Zeichen !!!
	move.b	#".",d0			; sonst Punkt nehmen
ascii:
	move.b	d0,(a1)+		; Char in Text-Buf.
	dbra	d1,convert_loop
	move.b	#" ",(a1)+		; Space ->gerade Adr
	move.w	#$ff,(a1)+
	add.b	#10,d2			; x_pos + 10
	dbra	d3,convert_loop2
	clr.b	(a1)+			; Abschlußbyte = 0

	move.l	4.w,a6			; execbase
	lea	intname(pc),a1
	jsr	-408(a6)		; OpenLibrary
	move.l	d0,a6
	moveq	#0,d0			; Alert_nummer 0
	lea	1160(a4),a0		; Alert_adr
	move.l	#256,d1			; Alert_height
	jsr	-90(a6)			; DisplayAlert
	move.l	d0,d3			; d3 -  Alert_Status
	move.l	a6,a1			; intbase in a1
	move.l	4.w,a6			; execbase
	jsr	-414(a6)		; CloseLibrary
	tst.l	d3
	bne	execute_bootblock
kill_bootblock:
	lea	boottrack(pc),a0	; ORG-Bootcode
	lea	4(a4),a1		; Boottrack "DOS"
	moveq	#bootend-boottrack-1,d0
boot_copy_loop:
	move.b	(a0)+,(a1)+		; Boottrack verbess.
	dbra	d0,boot_copy_loop
	move.w	#1024-[bootend-boottrack]-1,d0
boot_clr_loop:
	clr.b	(a1)+			; Rest löschen
	dbra	d0,boot_clr_loop

	lea	44(a5),a1		; io_request
	move.w	#3,28(a1)		; io_command - Write
	move.l	#0,44(a1)		; io_offset
	move.l	#$400,36(a1)		; io_length
	move.l	a4,40(a1)		; io_data - Adresse
	jsr	-456(a6)
	lea	44(a5),a1		; io_request
	move.w	#2,28(a1)		; io_command - Read
	move.l	#$2c00,44(a1)		; io_offset
	move.l	#$400,36(a1)		; io_length
	move.l	a4,40(a1)		; io_data - Adresse
	jsr	-456(a6)

	move.l	#$fc00d2,$80.w
	trap	#0			; Reset !!

reset_vector_check:
	lea	$dff180,a0		; Hintergrundfarbe
	tst.l	42(a6)			; ColdCapture ?
	bne.s	belegt
	tst.l	46(a6)			; CoolCapture ?
	bne.s	belegt
	tst.l	546(a6)			; KickMemPtr ?
	bne.s	belegt
	tst.l	550(a6)			; KickTagPtr ?
	bne.s	belegt
	move.w	#$0f0,(a0)		; Farbe = Grün (OK)
	bra.s	exit_check
belegt:
	move.w	#$f00,(a0)		; Farbe = Rot
	btst	#6,$bfe001		; linke Mausetaste ?
	bne.s	exit_check		; ---> EXIT
	move.w	#$0f0,(a0)		; Farbe = Grün (OK)
	clr.l	42(a6)			; ColdCapture
	clr.l	46(a6)			; CoolCapture
	clr.l	546(a6)			; KickMemPrt
	clr.l	550(a6)			; KickTagPrt
exit_check:

; ORG - Routine aus Kickstart

	move.l	42(a6),d0	
	beq.s	no_ColdCapture
	jmp	$fc0178
no_ColdCapture:
	jmp	$fc0184

;	Hier steht der Orginale Boottrack !!

boottrack:
	dc.l	$c0200f19		; Checksum
	dc.l	880			; Rootblock

bootstart:
	lea	dosname(pc),a1
	jsr	-96(a6)
	tst.l	d0
	beq.s	error
	move.l	d0,a0
	move.l	22(a0),a0
	moveq	#0,d0
boot_end:
	rts
error:
	moveq	#-1,d0
	bra.s	boot_end
dosname:
	dc.b	"dos.library",0
bootend:

intname:	dc.b	"intuition.library",0
even
alert_text:
	dc.b	0,90,20,"ACHTUNG ! Dies ist kein"
	dc.b	" normaler Bootblock !",0,1
	dc.b	0,30,33,"LEFT MOUSE weiter          "
	dc.b	"          RIGHT MOUSE löschen",0,1
	dc.b	0,120,50,"ANTI-VIRUS-KICK written by Dirk"
	dc.b	" Meyer",0,1
	dc.b	0,180,64,"© BY MAXON COMPUTER GmbH ",0,1
alert_size = * - alert_text

	illegal				; Kennung neue Adr
	dc.l	$fe846a
	move.l	#1160+1306,d0		; 1160 für Bootblock
					; 1306 für Alert

	illegal				; Kennung neue Adr
	dc.l	$fe86d4
	move.l	#1160+1306,d0		; 1160 für Bootblock
					; 1306 für Alert

	illegal				; Kennung neue Adr
	dc.l	$fe85c2
	jmp	$fc2336			; Boottest
	nop

	illegal
	dc.l	$ff886c
	dc.b	$18,"CON:0/0/640/256/AmigaDOS",0

	illegal
	dc.l	$fcb00c

	moveq	#4,d1			; Neuer PALCheck !!!
wait1:
	jsr	$fc5ece
	cmp.w	#$19,d0
	bne.s	wait1
wait2:
	jsr	$fc5ece
	cmp.b	#$18,d0
	bne.s	wait2
	and.w	#$100,d0
	beq.s	NTSC_Amiga
PAL_Amiga:
	moveq	#4,d0
	rts
NTSC_Amiga:
	moveq	#1,d0
	rts

;	Keymap V1.3 German
	DC.W $4AFC,$00FE,$7FD4,$0307,$4AFC,$00FE,$7FF0,$0703
	DC.W $0301,$8003,$0303,$4AFC,$00FE,$8000,$0303,$0380
	DC.W $0003,$0303,$4AFC,$00FE,$8042,$5D5B,$4AFC,$00FE
	DC.W $8048,$4032,$2232,$2323,$A733,$24A2,$2434,$25BC
	DC.W $2535,$5E36,$2636,$2637,$2F37,$2A38,$2838,$2839
	DC.W $2939,$2930,$3D30,$5F2D,$3FDF,$2B3D,$602C,$4AFC
	DC.W $00FE,$80AA,$DCFC,$7D5D,$2A2B,$4AFC,$00FE,$80E6
	DC.W $D6F6,$2227,$C4E4,$B423,$B423,$0000,$0000,$7B7B
	DC.W $7B34,$7D7D,$7D35,$7E7E,$7E36,$BBAB,$3E3C,$5A7A
	DC.W $5979,$4AFC,$00FE,$8122,$3B2C,$4AFC,$00FE,$8126
	DC.W $3A2E,$4AFC,$00FE,$812A,$5F2D,$4AFC,$00FE,$8134
	DC.W $4040,$4037,$B0B0,$B038,$5E5E,$5E39,$4AFC,$00FE
	DC.W $831C,$0079,$0059,$4AFC,$00FE,$8328,$7AFD,$4AFC
	DC.W $00FE,$832E,$5ADD,$4AFC,$00FE,$83DA,$6400
	dc.b "ENDE"			; Endkennung
