;APS000003ED000003ED000003ED000003ED000003ED000003ED000003ED000003ED000003ED000003ED

; ####################################################################
; ## R-TYPE II (C) ACTIVISON                                        ##
; ## DISKIMAGE (133 TRACKS) FOR FLASHTRO TUTORIAL.                  ##
; ## !! USE CHIPMEM FOR THIS FOR WRITEBACK !!                       ##
; ## -------------------------------------------------------------- ##
; ## USAGE:                                                         ##
; ## 1. <A>  ASSEMBLE SOURCECODE                                    ##
; ## 2. <J>  EXECUTE SOURCE TO MAKE NECCESSARY PATCHES IN DISKIMAGE ##
; ## 3. <WT> WRITE BACK 133 TRACKS FROM LABEL #DISK_START           ##
; ## 4. <CC> CALCULATE BOOTBLOCK-CHECKSUM                           ##
; ## -------------------------------------------------------------- ##
; ####################################################################


DISKOFFSET_COPYLOCK_CODE   = 2*$1800+$200		
DISKOFFSET_ORIGINAL_LOADER = 2*$1800+$200+$F960-$500	
DISKOFFSET_FILETABLE	   = 2*$1800+$200+$FB20-$500


	INCDIR	"SOURCES:TUTORIAL/"

	; OVERWRITE OLD COPYLOCK CODE WITH OUR NEW LOADER
	; -----------------------------------------------

	LEA	NEW_COPYLOCK_CODE(PC),A0
	LEA	DISK_START+DISKOFFSET_COPYLOCK_CODE,A1
	MOVE.L	#(COPYLOCK_CODE_END-NEW_COPYLOCK_CODE)-1,D0
COPY_CL:MOVE.B	(A0)+,(A1)+
	DBF	D0,COPY_CL

	; OVERWRITE FIRST INSTRUCTION OF ORIGINAL LOADER WITH
	; A "JMP $500" -> SO OUR LOADER IS EXECUTED!
	; ---------------------------------------------------

	LEA	DISK_START+DISKOFFSET_ORIGINAL_LOADER,A0
	MOVE.W	#$4EF9,(A0)+		
	MOVE.L	#$500,(A0)+

	; THE FILETABLE IS LOCATED AT TRACK #2 SECTOR #0 ON DISK
	; AND IS LOADED SEPERATELY BY THE ORIGINAL CODE. WE SKIP THIS PART
	; AND COPY IT INTO THE RIGHT PLACE HERE ($FB20 IN MEMORY).
	; ----------------------------------------------------------------

	LEA	DISK_START+2*$1800,A0			; TR #2 SEC #0
	LEA	DISK_START+DISKOFFSET_FILETABLE,A1
	MOVE.L	#$90,D0					; SIZE OF FILETABLE
COPY_FT:MOVE.B	(A0)+,(A1)+
	DBF	D0,COPY_FT

	MOVEQ	#0,D0					; END OF PATCHING!!
	RTS

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

; ===========================================================================
; THE NEW TRACKLOADER
; -------------------
; HERE COMES OUR NEW LOADER THAT  IS WRITTEN TO THE PLACE OF THE
; ORGINAL COPYLOCK, IN MEMORY AT $500, ON DISK LOCATED ON TRACK #2, SECTOR #1
; ===========================================================================


NEW_COPYLOCK_CODE:			; <- $500 IN MEMORY !

	MOVEQ	#0,D1			; HANDLE THE FILES LIKE THE ORIGINAL
	MOVEQ	#0,D2
	ASL.W	#3,D0
	LEA	$FB20,A1
	MOVE.B	(A1,D0.W),D1		; D1 = TRACKNUM
	MULS	#$1800,D1		; * TRACKSIZE $1800
	MOVE.B	1(A1,D0.W),D2		; D2 = SECTORNUM
	MULS.W	#512,D2			; * SECTORSIZE $200
	ADD.L	D2,D1			; GET CORRECT BYTEOFFSET
	DIVS.W	#512,D1			; AND TURN IT INTO AN SECTOROFFSET
	MOVE.W	2(A1,D0.W),D2		; GET FILESIZE IN SECTORS
	ADDQ.W	#1,D2			; 
	MOVEQ	#0,D3			
	MOVE.L	4(A1,D0.W),-(A7)	; SAVE REAL BYTESIZE OF FILE ON STACK	
	LEA	$2FD80,A1		; MFM POINTER
AGAIN:	MOVEQ	#0,D0			; DRIVENUMBER (DF0:)
	BSR.W	TLOADER			; CALL RNC TRACKLOADER
	TST.B	D0			; LOADING OK?
	BEQ.B	LGUT			; YEAH -> END TLOADER
BLINKER:BTST	#6,$BFE001		;
	BNE.B	BLINK			; OTHERWISE MAKE SCREEN FLASH
	BRA.B	AGAIN			; AND WAIT FOR LEFT MOUSE TO 
BLINK:	MOVE.W	D0,$DFF180		; REPEAT PROCEDURE :P
	SUBQ.W	#1,D0			;
	BRA.B	BLINKER			;
LGUT:	MOVE.L	(A7)+,D0		; RESTORE BYTESIZE OF FILE
	RTS				; FOR FURTHER GAME-USAGE
	
TLOADER:INCBIN	"RNCLOADER.BIN"		; ROB NORTHEN SECTORLOADER

COPYLOCK_CODE_END:

; ==<DISKIMAGE>========================================================

DISK_START:

	DC.B	"DOS",0				; TRACK 0 (BOOT)
	DC.L	0
	DC.L	0

	MOVE.W	#$2,$1C(A1)			; READTRACK
	MOVE.L	#2*$1800+$200,$2C(A1)		; DISKPOSITION
	MOVE.L	#$24400,$24(A1)			; SIZE
	MOVE.L	#$50000,$28(A1)			; ADDRESS
	JSR	-$1C8(A6)			; DOIO()

	LEA	COPY_C(PC),A0			; COPY NEXT PART OF THE 
	LEA	$78000,A1			; BOOTCODE TO $78000
	MOVE.L	#(BOOTEND-COPY_C)-1,D0		; SO THAT WE DONT OVERWRITE
COPY:	MOVE.B	(A0)+,(A1)+			; OUR OWN BOOTCODE WHEN
	DBF	D0,COPY				; COPYING GAMEDATA TO $500 !!

	MOVE.W	#$7FFF,D0			; KILL DMA/INTERRUPTS
	MOVE.W	D0,$DFF096
	MOVE.W	D0,$DFF09A
	MOVE.L	#$78000,$80.W			; CONTINUE AT $78000 IN 
	TRAP	#0				; SUPERVISOR MODE

COPY_C:	LEA	$50000,A0			; COPY OUR GAMEDATA DOWN
	LEA	$500.W,A1			; FROM $50000 TO $500 !!
	MOVE.L	#$24400,D0
COPY_D:	MOVE.B	(A0)+,(A1)+
	MOVE.W	D0,$DFF180
	SUBQ.L	#1,D0
	BNE.B	COPY_D

	MOVE.L	#$4E714E71,$E86.W		; NOP OUT LOADING OF FILETABLE
	MOVE.W	#$4E71,$E8A.W			;

	MOVE.L	#$AE3B9CE3,D0			; MOVE COPYLOCK KEY TO D0
	MOVE.L	D0,$60.W			; AND TO ALL MEMORYLOCATIONS
	MOVE.L	D0,$E2C.W
	MOVE.L	D0,$7FF0C
	MOVE.L	D0,$7FF24
	MOVE.L	D0,$7FFBA
	JMP	$E2A.W				; NO NEED FOR A JMP TO $500.
						; WE GO DIRECTLY TO $E2A!

BOOTEND:BLK.B	$1800-(BOOTEND-DISK_START),0	; ZEROES REST OF LONGTRACK 0

	BLK.B	$1800,0				; TRACK 1 (EMPTY)

DUMP:	INCBIN	"DUMP"				; TRACK 2 - END

	BLK.B	2577,0				; ZERO TO GET AN EVEN DISKSIZE
						; 133 DOS TRACKS IN ALL

DISK_END:
