top	=$80000
buffer	=$75000
stack	=top
start = $400			; Start address (JUMP IN address)
startunpacked = $400		; Start of unpacked file
endpackedfile	=$400+153264
data		=$400


;	dc.b "DOS",0
;	dc.l 0,0

	LEA	l5107E(PC),A0
	MOVE.L	A0,$C.W
	MOVEA.L	3.W,A0
	MOVE.L	(A0),D0
l5107e	MOVE.W	#$2700,SR
	LEA	l5109C(PC),A0
	LEA	$7f000,A1
	LEA	endcode,A2
l51090	MOVE.L	(A0)+,(A1)+
	CMPA.L	A2,A0
	BCS.S	l51090
	JMP	$7f000

l5109c	lea stack,a7

dnld	lea data,a4
	move.w #882,d6
	bsr doload
	bsr _protec
	bra decrunch

doload	MOVE.W	#0,$DFF180
	LEA	l513FC(PC),A6
	clr.l (a6)
	clr.l 4(a6)
	clr.l 8(a6)
	clr.l 12(a6)
	clr.l 16(a6)
	clr.l 20(a6)
	MOVE.W	#$4B00,D0
	MOVE.W	D0,$12(A6)
	MOVE.W	#$3FF,$DFF096
	MOVE.W	#$7FFF,$DFF09A
	MOVE.B	#$1F,D0
	MOVE.B	D0,$BFED01
	MOVE.B	D0,$BFDD00
	ORI.B	#$F8,$BFD100
	MOVE.W	#3,D0
l510e2	BCLR	D0,$BFD100
	BSET	D0,$BFD100
	ADDQ.W	#1,D0
	CMP.W	#7,D0
	BNE.S	l510E2
	BSR	l5111A
	MOVE.W	d6,D0
	BSR	l5119E
	MOVE.L	D0,D7
	move.l	a4,A0
l51108	BSR	l511C0
	MOVE.B	D0,(A0)+
	SUBQ.L	#1,D7
	BNE.S	l51108
	BSR	l51186
	rts
l5111a	LEA	$BFD100,A0
	CLR.W	6(A6)
	CLR.W	(A6)
	CLR.W	2(A6)
	CLR.W	4(A6)
	BSR	l51168
	BSET	#1,(A0)
l51136	CLR.W	D1
l51138	BTST	#4,$BFE001
	BEQ	l51164
	BCLR	#0,(A0)
	move.w #1000,d0
	bsr l513f6
	BSET	#0,(A0)
	MOVE.W	$12(A6),D0
	BSR	l513F6
	ADDQ.W	#1,D1
	CMP.W	#$5A,D1
	BNE.S	l51138
	ADDI.W	#$64,$12(A6)
	BRA.S	l51136
l51164	BRA	l51186
l51168	LEA	$BFD100,A0
	BSET	#3,(A0)
	BCLR	#7,(A0)
	BCLR	#3,(A0)
l5117a	BTST	#5,$BFE001
	BNE.S	l5117A
	RTS
l51186	LEA	$BFD100,A0
	BSET	#3,(A0)
	BSET	#7,(A0)
	BCLR	#3,(A0)
	BSET	#3,(A0)
	RTS
l5119e	MOVEM.L	D1-d2/D7/A0-a1,-(A7)
	CLR.W	$10(A6)
	BSR.S	l51168
	BSR	l51312
	MOVE.L	$144(A0),-(A7)
	MOVE.L	$10(A0),D0
	BSR	l51312
	MOVE.L	(A7)+,D0
	MOVEM.L	(A7)+,D1-d2/D7/A0-a1
	RTS
l511c0	MOVEM.L	D1-d2/D7/A0-a1,-(A7)
	CLR.W	D0
	TST.W	$10(A6)
	BNE	l511FA
	MOVEA.L	8(A6),A0
	MOVE.B	(A0)+,D0
	MOVE.L	A0,8(A6)
	SUBQ.W	#1,$C(A6)
	BNE	l511FA
	MOVE.W	D0,-(A7)
	MOVE.W	$E(A6),D0
	BNE	l511F4
	BSR.S	l51186
	ST	$10(A6)
	BRA	l511F8
l511f4	BSR	l51312
l511f8	MOVE.W	(A7)+,D0
l511fa	MOVEM.L	(A7)+,D1-d2/D7/A0-a1
	RTS
l51200	BSR	l513D8
	CMP.W	6(A6),D1
	BNE	l5121C
	CMP.W	2(A6),D2
	BNE	l5121C
	MOVE.W	D0,4(A6)
	BRA	l512F2
l5121c	MOVE.W	D0,4(A6)
	MOVE.W	D1,(A6)
	MOVE.W	D2,2(A6)
	LEA	$BFD100,A0
	BSET	#2,(A0)
	MOVE.W	2(A6),D0
	BEQ	l5123C
	BCLR	#2,(A0)
l5123c	BSET	#1,(A0)
	MOVE.W	(A6),D0
	CMP.W	6(A6),D0
	BEQ	l5126C
	BLS	l51256
	BCLR	#1,(A0)
	ADDQ.W	#2,6(A6)
l51256	SUBQ.W	#1,6(A6)
	BCLR	#0,(A0)
	move.w #$1000,d0
	bsr l513f6
	BSET	#0,(A0)
	MOVE.W	$12(A6),D0
	BSR	l513F6
	BRA.S	l5123C
l5126c	BRA	l51270
l51270	MOVE.W	#$4489,$DFF07E
	MOVE.W	#$6600,$DFF09E
	MOVE.W	#$9500,$DFF09E
	MOVE.L	#buffer,$DFF020
	MOVE.W	#2,$DFF09C
	MOVE.W	#$8210,$DFF096
	MOVE.W	#$4000,$DFF024
	MOVE.W	#$1CBE,D0
	ORI.W	#$8000,D0
	MOVE.W	D0,$DFF024
	MOVE.W	D0,$DFF024
	CLR.W	D0
	CLR.W	D1
l512c2	ADDQ.W	#1,D1
	CMP.W	#$FFFF,D1
	BNE	l512D2
	MOVE.L	(A7)+,D0
	BRA	l51330
l512d2	MOVE.W	$DFF01E,D0
	BTST	#1,D0
	BEQ.S	l512C2
	MOVE.W	#2,$DFF09C
	MOVE.W	#$4000,$DFF024
	BSR	l51358
l512f2	MOVEQ	#$A,D1
	CLR.W	D0
	LEA	buffer,A0
l512fc	MOVE.B	2(A0),D0
	CMP.W	4(A6),D0
	BEQ	l51310
	LEA	$21C(A0),A0
	DBF	D1,l512FC
l51310	RTS
l51312	MOVE.W	D0,$18(A6)
	BSR	l51200
	MOVE.W	#$7F,D1
	LEA	$1C(A0),A1
	CLR.L	D0
l51324	ADD.L	(A1)+,D0
	DBF	D1,l51324
	TST.L	D0
	BEQ	l5133E
l51330	BSR	l5111A
	BSR	l51168
	MOVE.W	$18(A6),D0
	BRA.S	l51312
l5133e	MOVE.W	$2A(A0),$C(A6)
	MOVE.W	$2E(A0),$E(A6)
	LEA	$34(A0),A1
	MOVE.L	A1,8(A6)
	LEA	$1C(A0),A0
	RTS
l51358	LEA	buffer,A0
	MOVEA.L	A0,A1
	MOVE.W	#$A,D7
l51364	BSR	l51380
l51368	MOVE.W	(A0)+,D0
	CMP.W	#$4489,D0
	BNE.S	l51368
	CMPI.W	#$4489,(A0)
	BEQ	l5137A
	MOVE.W	-(A0),D0
l5137a	DBF	D7,l51364
	RTS
l51380	ADDQ.L	#2,A0
	BSR	l51398
	BSR	l513A0
	BSR	l51398
	BSR	l51398
	BSR	l513A8
	RTS
l51398	MOVE.W	#4,D0
	BRA	l513AC
l513a0	MOVE.W	#$10,D0
	BRA	l513AC
l513a8	MOVE.W	#$200,D0
l513ac	MOVE.W	D0,D3
	LSR.W	#2,D3
	SUBQ.W	#1,D3
l513b2	MOVE.L	(A0),D1
	ANDI.L	#$55555555,D1
	ASL.L	#1,D1
	MOVE.L	0(A0,D0.W),D2
	ANDI.L	#$55555555,D2
	OR.L	D2,D1
	MOVE.L	D1,(A1)+
	LEA	4(A0),A0
	DBF	D3,l513B2
	LEA	0(A0,D0.W),A0
	RTS
l513d8	EXT.L	D0
	DIVU	#$16,D0
	MOVE.W	D0,D1
	CLR.W	D2
	SWAP	D0
	CMP.W	#$B,D0
	BCS	l513F4
	MOVE.W	#1,D2
	SUBI.W	#$B,D0
l513f4	RTS
l513f6	SUBQ.W	#1,D0
	BNE.S	l513F6
	RTS


decrunch
		bsr.s	unpack
		jmp	start
unpack:
		lea	endpackedfile,a0
		lea	startunpacked,a1
		move.l	-(a0),a2
		adda.l	a1,a2
		move.l	-(a0),d0
more_bytes_to_go:
		moveq	#3,d1
		bsr.s	read_1_bit
		tst.b	d2
		beq.S	newcommand1
		cmpi.w	#7,d2
		bne.S	minijump
		lsr.l	#1,d0
		bne.S	go_on_1
		bsr	read_32_new_bits
go_on_1:
		bcc.S	midjmp
		moveq	#10,d1
		bsr.s	read_1_bit
		tst.w	d2
		bne.S	minijump
		moveq	#18,d1
		bsr.s	read_1_bit
		bra.S	minijump
read_1_bit:
		subq.w	#1,d1
		moveq	#0,d2
getbits:
		lsr.l	#1,d0
		beq	empty
		roxl.l	#1,d2
		dbf	d1,getbits
		rts
midjmp:
		moveq	#4,d1
		bsr.s	read_1_bit
		addq.w	#7,d2
minijump:
		subq.w	#1,d2
getd2chr:
		moveq	#7,d1
get8bits:
		lsr.l	#1,d0
		beq.S	empty3
		roxl.l	#1,d3
		dbf	d1,get8bits
		move.b	d3,-(a2)
		;move.b	d3,$dff180		; FLASH SCREEN
		dbf	d2,getd2chr
		bra.S	newcommand1
empty3:
		move.l	-(a0),d0
		move.w	#$10,ccr
		roxr.l	#1,d0
		roxl.l	#1,d3
		dbf	d1,get8bits
		move.b	d3,-(a2)
		dbf	d2,getd2chr
newcommand1:
		cmpa.l	a2,a1
		bge.s	exit_unpacker
go_on_2:	moveq	#2,d1
		bsr.s	read_1_bit
		moveq	#2,d3
		moveq	#8,d1
		tst.w	d2
		beq.S	duplicate
		moveq	#4,d3
		cmpi.w	#2,d2
		beq.S	bigger_offset
		moveq	#3,d3
		cmpi.w	#1,d2
		beq.S	small_offset
		moveq	#2,d1
		bsr.s	read_1_bit
		cmpi.w	#3,d2
		beq.S	length_bigger
		cmpi.w	#2,d2
		beq.S	length_seven
		addq.w	#5,d2
		move.w	d2,d3
		bra.S	bigger_offset
length_seven:
		moveq	#2,d1
		bsr	read_1_bit
		addq.w	#7,d2
		move.w	d2,d3
		bra.S	bigger_offset
length_bigger:
		moveq	#8,d1
		bsr	read_1_bit
		move.w	d2,d3
		bra.S	bigger_offset
small_offset:
		moveq	#8,d1
		lsr.l	#1,d0
		bne.S	go_on_3
		bsr.S	read_32_new_bits
go_on_3:
		bcs.S	duplicate
		moveq	#14,d1
		bra.S	duplicate
bigger_offset:	moveq	#16,d1
		lsr.l	#1,d0
		bne.S	go_on_4
		bsr.S	read_32_new_bits
go_on_4:
		bcc.S	duplicate
		moveq	#8,d1
		lsr.l	#1,d0
		bne.S	go_on_5
		bsr.S	read_32_new_bits
go_on_5:
		bcs.S	duplicate
		moveq	#12,d1
duplicate:	bsr	read_1_bit
		subq.w	#1,d3
copynextbyte:
		move.b	-1(a2,d2.l),-(a2)
		dbf	d3,copynextbyte
newcommand2:
		bra	more_bytes_to_go
exit_unpacker:	moveq	#0,d0
		rts
read_32_new_bits:
		move.l	-(a0),d0
		move.w	#$10,ccr
		roxr.l	#1,d0
		rts
empty:		move.l	-(a0),d0
		move.w	#$10,ccr
		roxr.l	#1,d0
		roxl.l	#1,d2
		dbf	d1,getbits
		rts
;data:			; Length of the packed file 628,
;endpackedfile:			; filename "HBSTART.DATA"



l513fc	
	ds.l 20

_ciaa   EQU     $bfe001
_ciab   EQU     $bfd000
_custom EQU     $DFF000
LENGTH  EQU     28400           bytes
SIG     EQU     $4454
GAP     EQU     3350
TRACK   EQU     0

dskdatr	equ $008
dskbytr	equ $01a
intenar	equ $01c
intreqr	equ $01e
dskpt	equ $020
dsklen	equ $024
dskdat	equ $026
dsksync	equ $07e
dmacon	equ $096
intena	equ $09a
intreq	equ $09c
dmaconr	equ $002
adkconr	equ $010
adkcon	equ $09e

************************************************************
*    PROTEC for the Amiga                                  *
************************************************************

_protec  
;
        movem.l   d1-d7/a1-a6,-(sp)
	move.l #buffer,dubuff

        jsr     init
        jsr     startd          start drive
        jsr     gotot0          goto track 0
        jsr     readtr          read track 0
        jsr     clear
        jsr     fsig             look for sig

        move.b  status,d0
        movem.l   (sp)+,d1-d7/a1-a6
        rts

*****************************************************
*       INIT
*****************************************************
*
init
        movea.l #_custom,a6
        move.w  intenar(a6),d0
        move.w  d0,intesav
        move.w  intreqr(a6),d0
        move.w  d0,intrsav
        move.w  dmaconr(a6),d0
        move.w  d0,dmasav
        move.w  adkconr(a6),d0
        move.w  d0,adksav
        move.w  #$0010,dmacon(a6)
        move.w  #$4000,intena(a6)
        move.w  #$0002,intreq(a6)
        move.w  #$7fff,adkcon(a6)
        move.w  #$8100,adkcon(a6)
        or.b    #$78,$bfd100
        rts

startd  bclr    #7,$bfd100
        moveq   #100,d0
st1     bclr    #3,$bfd100
        dbf     d0,st1
        bsr     wtrdy
        rts

wtrdy   move.w  #3000,d6
wt1     dbf     d6,wt1
wt2     btst    #5,$bfe001
        bne.s   wt2
        rts

gotot0  btst    #4,$bfe001
        beq.s   t0e
        bset    #1,$bfd100
        bclr    #0,$bfd100
        bset    #0,$bfd100
        bsr     wtrdy
        bra.s   gotot0
t0e     rts

gototr
        btst    #4,$bfe001
        beq.s   go1
        bsr     gotot0
        bra.s   gototr
go1     move.w  #TRACK,d2
go2     cmpi.w  #0,d2
        beq.s   go3
        bclr    #1,$bfd100
        nop
        nop
        nop
        bclr    #0,$bfd100
        nop
        nop
        nop
        bset    #0,$bfd100
        bsr     wtrdy
        subq.w  #1,d2
        bra.s   go2
go3     rts

readtr  bsr.s   gototr
2$      bclr    #2,$bfd100       set side 1
        move.w  #$8210,dmacon(a6)
        move.l  DUBUFF,dskpt(a6)
        clr.w   dsklen(a6)
        bsr     waitidx
        move.w  #$4000,dsklen(a6)
        move.w  #(LENGTH/2)!$8000,dsklen(a6)
        move.w  #(LENGTH/2)!$8000,dsklen(a6)
        move.l   #200000,d1
1$      move.w  intreqr(a6),d0
        subq.l   #1,d1
        beq.s   2$
        btst    #1,d0
        beq.s   1$
        move.w  #2,intreq(a6)
        rts

        clr.w   dsklen(a6)
        move.w  #$0010,dmacon(a6)
        rts

waitidx move.b  $bfdd00,d6
idx1    move.b  $bfdd00,d6
        btst    #4,d6
        beq.s   idx1
        rts

*
clear
        bset    #7,$bfd100
        bset    #3,$bfd100
        bclr    #3,$bfd100
        move.w  intesav,d0
        bset    #15,d0
        move.w  d0,intena(a6)
        move.w  intrsav,d0
        bset    #15,d0
        move.w  d0,intreq(a6)
        move.w  dmasav,d0
        bset    #15,d0
        move.w  d0,dmacon(a6)
        move.w  adksav,d0
        bset    #15,d0
        move.w  d0,adkcon(a6)
        rts

******************************************************
*               FIND SIGNATURE
*******************************************************
*
fsig    movea.l DUBUFF,a0
        move.l  #LENGTH/2,d3
fsig2   move.l  (a0),d0
        addq.l   #2,a0
        move.l  #15,d2
fsig1   move.l  d0,d1
        swap    d1
        cmpi.w  #SIG,d1
        beq.s   found1
        lsl.l   #1,d0
        dbra    d2,fsig1
        dbra    d3,fsig2
*NOT FOUND
        bra     fsig3
*FOUND!!
found1
*
        move.l  #0,d5                   init count
fsig5   move.l  (a0),d0
        addq.l   #2,a0
        move.l  #15,d2
fsig4   move.l  d0,d1
        swap    d1
        cmpi.w  #SIG,d1
        beq.s     found2
        lsl.l   #1,d0
        dbra    d2,fsig4
        addq.l  #1,d5           increment gap count
*
        dbra    d3,fsig5
*
        bra.s   fsig3
found2 
*
*
        subi.l  #GAP*2,d5
        bmi.s     fsig3
*BRILLIANT!

        move.b  #0,status
        rts
*
fsig3   move.b  #1,status
	;clr.w 1
	nop
	nop
        rts

************************************************************

        DATA

**********************************************************

DUBUFF  DS.L    1
intesav ds.w    1
intrsav ds.w    1
dmasav  ds.w    1
adksav  ds.w    1
status  ds.b    1
*
endcode	
	END


