

;Versie 2.3 Device wordt netter afgesloten en Port wordt 
;	    verwijderd als het device niet geopend kan worden




ExecBase = 4
FindTask = -294
AddPort  = -354
RemPort  = -360
OpenLib  = -408
CloseLib = -414
OpenDev  = -444
CloseDev = -450
DoIO     = -456
OpenLibrary = -552
Output   = -60
Write    = -48		
AllocMem = -198
FreeMem  = -210
Open     = -30
Close    = -36
Read     = -42
run:
	movem.l	d0/a0,-(sp)
	move.l	execbase,a6
	lea.l	DosName,a1
	clr.l	d0
	jsr	OpenLibrary(a6)
	move.l	d0,DosBase
	bne	goed
	rts
goed:
	move.l	d0,a6
	jsr	Output(a6)
	move.l	d0,OutputHandle	

arg:
	movem.l	(sp)+,d0/a0
	lea	argtekst,a2
	lea	argpointers,a3
	move.l	#0,d2		;arg counter
spaties:
	move.b	(a0)+,d1	;spaties opruimen tussen
	subq.l	#1,d0		;argumenten
	ble.s	parmexit
	cmp.b	#' ',d1
	ble.s	spaties	

;Copieer argumenten naar eigen buffer	
	addq.l	#1,d2		;Weer een argument erbij
	move.l	a2,(a3)+
	bra.s	copyarg

argcopyloop:
	move.b	(a0)+,d1
	subq.l	#1,d0
	cmp.b	#' ',d1
	ble.s	eindcopy
copyarg:
	move.b	d1,(a2)+
	bra	argcopyloop
eindcopy:	
	clr.b	(a2)+		;arg afsluiten met een 0
	bra.s	spaties
parmexit:
	cmpi.l	#2,d2		;# arg = 1  ? 
	bne	usage		;Niet het juiste aantal arg

	move.l	#argpointers,a0
	move.l	4(a0),a0 ;pointer naar het tweede arg
	cmpi.b	#'d',(a0)	
	beq	dgoed
	cmpi.b	#'D',(a0)
	bne	usage
dgoed:	cmpi.b	#'f',1(a0)
	beq	fgoed
	cmpi.b	#'F',1(a0)
	bne	usage
fgoed:	cmpi.b	#':',3(a0)
	bne	usage
	cmpi.b	#'0',2(a0)
	blo	usage
	cmpi.b	#'3',2(a0)
	bhi	usage
;Argument klopt dus doorgaan !!
	clr.l	d0
	move.b	2(a0),d0
	subi.b	#'0',d0
	move.l	d0,drivenummer	

;Geheugen voor file reserveren en file lezen

	move.l	ExecBase,a6
	move.l	#$10002,d1		;Chip of Fastmem
	move.l	#1048,d0
	jsr	AllocMem(a6)
	move.l	d0,FileMem
	beq	eind1

	move.l	#argpointers,a0
	move.l	(a0),d1 ;pointer naar het eerste arg
	move.l	#1005,d2
	move.l	DosBase,a6
	jsr	Open(a6)
	move.l	d0,filepointer
	bne.s	OpenSuccessFull

	move.l	#tekst4,d2	;fout melding kan file niet
	move.w	#endtekst4-tekst4,d3	;openen
	jsr	print
	bra	eind2
OpenSuccessFull:

	move.l	filepointer,d1
	move.l	filemem,d2
	move.l	#1048,d3
	move.l	DosBase,a6
	jsr	Read(a6)
	move.l	filepointer,d1
	jsr	Close(a6)


	move.l	#tekst1,d2
	move.w	#endtekst1-tekst1,d3
	jsr	print

	move.l	ExecBase,a6		;ExecBase laden
	move.l	#1024,d0	;aantal bytes
	move.l	#2,d1		;ChipMem
	jsr	AllocMem(a6)
	move.l	d0,bootmem	;Bootmem komt bootblock in te staan



opnieuw:
	move.l	#tekst2,d2
	move.w	#endtekst2-tekst2,d3
	jsr	print

WaitMouseButton:
	btst	#6,$bfe001
	beq.s	LeftMouseButton
	btst	#10,$dff016
	beq.s	RightMouseButton
	bra.s	WaitMouseButton

RightMouseButton:
	btst	#10,$dff016
	beq.s	RightMouseButton
	bra	eind3


LeftMouseButton:
	btst	#6,$bfe001
	beq.s	LeftMouseButton

	;Checksum berekenen en filemem -> bootmem	

	move.l	filemem,a0
	move.l	bootmem,a1
	
	add.l	#36,a0	;Bij seka porg's #36 andere #32
	add.l	#12,a1
	move.w	#1011,d0	;1012 bytes copieeren
copyloop2:
	move.b	(a0)+,(a1)+
	dbra	d0,copyloop2

	move.l	bootmem,a0
	move.l	#$444F5300,(a0)
	move.l	#0,4(a0)
	move.l	#$00000370,8(a0)
	clr.l	d0
	move.l	#255,d1
checksumloop:
	add.l	(a0)+,d0
	bcc.s	checksumcont
	add.l	#1,d0
checksumcont:
	dbra	d1,checksumloop
	move.l	#$ffffffff,d1
	sub.l	d0,d1
	move.l	bootmem,a0
	move.l	d1,4(a0)


	;Bootblock wegschrijven
	sub.l	a1,a1
	jsr	FindTask(a6)
	move.l	d0,readreply+$10


	lea	readreply,a1
	jsr	AddPort(a6)

	lea	diskio,a1
	move.l	drivenummer,d0		;drive nummer
	clr.l	d1
	lea	trddevice,a0
	jsr	OpenDev(a6)
	tst.l	d0
	bne	eind4

	lea	diskio,a1
	move.l	#readreply,14(a1)
	move.w	#15,28(a1)		;TD_PROTSTATUS
	move.l	execbase,a6
	jsr	DoIO(a6)	
	move.l	diskio+32,d0
	beq	schrijven
	
	move.l	#tekst5,d2
	move.w	#endtekst5-tekst5,d3
	jsr	print
	bra	verwijderdev		

schrijven:	
	lea	diskio,a1
	move.w	#3,28(a1)
	move.l	bootmem,40(a1)
	move.l	#2*512,36(a1)
	move.l	#0,44(a1)
	move.l	execbase,a6
	jsr	DoIO(a6)

	move.l	diskio+32,d6
	lea	diskio,a1
	move.w	#4,28(a1)
	move.l	#0,36(a1)
	jsr	DoIO(a6)

	lea	diskio,a1
	move.w	#9,28(a1)
	move.l	#0,36(a1)
	jsr	DoIO(a6)

verwijderdev:	
	lea	diskio,a1
	jsr	CloseDev(a6)

	lea	readreply,a1
	jsr	RemPort(a6)
	
	bra	opnieuw
eind4:
	lea	readreply,a1
	jsr	RemPort(a6)


eind3:

	move.l	ExecBase,a6
	move.l	#1024,d0
	move.l	bootmem,a1
	jsr	FreeMem(a6)

eind2:
	move.l	ExecBase,a6
	move.l	#1048,d0
	move.l	Filemem,a1
	jsr	FreeMem(a6)
eind1:
	rts


print:		;print tekst op scherm d3.w = lengte d2 = tekst
	movem.l	d1/a6,-(sp)
	move.l	DosBase,a6
	move.l	OutputHandle,d1
	jsr	Write(a6)
	movem.l	(sp)+,d1/a6
	rts

usage:
	move.l	#tekst3,d2
	move.w	#endtekst3-tekst3,d3
	jsr	print
	rts				;einde prog

DosName:
	dc.b	'dos.library',0
	even
DosBase:
	dc.l	0
OutputHandle:
	dc.l	0
bootmem:
	dc.l	0
drivenummer:
	dc.l	0
filemem:	
	dc.l	0
filePointer:
	dc.l	0
argtekst:
	blk.l	40
argpointers:
	blk.l	10
trddevice:	
	dc.b	'trackdisk.device',0
	even
diskio:
	blk.l	20,0
readreply:
	blk.l	8,0

tekst1:
	dc.b	'Boot V2.3 (C) SYSTEM Z Holland',10
	dc.b	'This program write a SEKA program',10 
	dc.b	'in the bootblock of a disk.',10
	dc.b	'Coding by A.Nonymous',10,10
endtekst1:
tekst2:
	dc.b	'Press [L-MOUSE] to WRITE',10
	dc.b	'Press [R-MOUSE] to CANCEL',10,10
endtekst2:
tekst3:
	dc.b	'USAGE: boot <filename> dfx:',10
endtekst3:
tekst4:
	dc.b	'Error: File not found!',10
endtekst4:
tekst5:
	dc.b	'Error: Disk is writeprotect!',10,10
endtekst5:


