;---------------------------------------------------------------------
;    **   **   **  ***   ***   ****     **    ***  **  ****
;   ****  *** *** ** ** **     ** **   ****  **    ** **  **
;  **  ** ** * ** ** **  ***   *****  **  **  ***  ** **
;  ****** **   ** ** **    **  **  ** ******    ** ** **
;  **  ** **   ** ** ** *  **  **  ** **  ** *  ** ** **  **
;  **  ** **   **  ***   ***   *****  **  **  ***  **  ****
;---------------------------------------------------------------------
; AMOS Screen dump extension, V1.1
; By Alex J.Grant & F.Lionet
; AMOS and AMOS Compiler (c) Europress Software 1991
; To be used with AMOS1.3 and over
;--------------------------------------------------------------------- 
; This file is public domain
;---------------------------------------------------------------------
; Please refer to the _Music.s file for more informations
;---------------------------------------------------------------------
; Trackdisk.device commands added by Alex J Grant
;---------------------------------------------------------------------
*
ExtNb		equ	20-1
TRUE		equ	-1
FALSE		equ	0
AllocSignal	equ	-$014a
FreeSignal	equ	-$0150
*
		INCDIR	"dh1:amos_system/amos_include_files/"
		Include	"_Equ.s"
		RsSet	DataLong
		Include "_Pointe.s"
		Include	"_WEqu.s"
		Include "_CEqu.s"
		Include	"_LEqu.s"
*
DLea		MACRO
		move.l	ExtAdr+ExtNb*16(a5),\2
		add.w	#\1-MB,\2
		ENDM
DLoad		MACRO
		move.l	ExtAdr+ExtNb*16(a5),\1
		ENDM
*
Start		dc.l	C_Tk-C_Off
		dc.l	C_Lib-C_Tk
		dc.l	C_Title-C_Lib
		dc.l	C_End-C_Title
		dc.w	0

***********************************************************
* 		OFFSETS TO FUNCTIONS
C_Off   dc.w (L1-L0)/2,(L2-L1)/2,(L3-L2)/2,(L4-L3)/2
        dc.w (L5-L4)/2,(L6-L5)/2,(L7-L6)/2,(L8-L7)/2
        dc.w (L9-L8)/2,(L10-L9)/2,(L11-L10)/2,(L12-L11)/2
        dc.w (L13-L12)/2,(L14-L13)/2,(L15-L14)/2,(L16-L15)/2
        dc.w (L17-L16)/2,(L18-L17)/2,(L19-L18)/2,(L20-L19)/2
        dc.w (L21-L20)/2,(L22-L21)/2,(L23-L22)/2,(L24-L23)/2
        dc.w (L25-L24)/2,(L26-L25)/2,(L27-L26)/2,(L28-L27)/2
        dc.w (L29-L28)/2,(L30-L29)/2,(L31-L30)/2,(L32-L31)/2
        dc.w (L33-L32)/2,(L34-L33)/2,(L35-L34)/2,(L36-L35)/2
        dc.w (L37-L36)/2,(L38-L37)/2,(L39-L38)/2,(L40-L39)/2
        dc.w (L41-L40)/2,(L42-L41)/2,(L43-L42)/2,(L44-L43)/2
        dc.w (L45-L44)/2,(L46-L45)/2,(L47-L46)/2,(L48-L47)/2
        dc.w (L49-L48)/2,(L50-L49)/2,(L51-L50)/2

***********************************************************
* 		DUMP TOKENS
C_Tk		dc.w	1,0
		dc.b	$80,-1
		dc.w	-1,L_Perr
		dc.b	"dump err","$"+$80,"2",-1
		dc.w	-1,L_Dump1
		dc.b	"!dum","p"+$80,"0",-2	; just screen#
		dc.w	-1,L_Dump2
		dc.b	$80,"00,0t0,0",-2	; srcx,srcy,x,y 
		dc.w	-1,L_Dump3
		dc.b	$80,"00,0t0,0,0,0,0",-1 ; srcx,srcy to x,y,destx,desty,special	
		dc.w	-1,L_DiskIn
		dc.b	"diski","n"+$80,"00",-1	; unit number
		dc.w	-1,L_WProtect
		dc.b	"writeenabl","e"+$80,"00",-1 ; unit number
		dc.w	-1,L_ReadTrack
		dc.b	"secrea","d"+$80,"20,0,0",-1 ; unit,track,sector
		dc.w	-1,L_WriteTrack
		dc.b	"secwrit","e"+$80,"00,0,0,2",-1 ; unit,track,sector,a$
		dc.w	-1,L_FormatTrack
		dc.b	"trackforma","t"+$80,"00,0",-1  ; unti,track,sector
		dc.w	-1,L_Derr
		dc.b	"disk err","$"+$80,"0",-1
		
		dc.w	0

******************************************************************
*		Start of library
C_Lib

******************************************************************
*		COLD START

L0:	lea	MB(pc),a0
	move.l	a0,ExtAdr+ExtNb*16(a5)

	lea	dumpend(pc),a0
	move.l	a0,ExtAdr+ExtNb*16+8(a5)

	Rbsr	L_ReserveTrack		; this reserves some memory for
	tst.l	d0			; a sector buffer.
	beq	openOK			; it is messy because it is not
	moveq	#-1,d0			; required by every command
	rts				; but it's only 514 bytes

openOK	moveq	#ExtNb,d0
	rts

dumpend:
	Rbsr	L_FreeTrack		; Deallocate this messy memory
	rts

*************** Datazone
MB:
width:		dc.w	0
height:		dc.w	0
viewModes	dc.w	0
depth:		dc.w	0
screen		dc.l	0
vPort		dc.l	0
rPort		dc.l	0
colourMap	dc.l	0
oldCtable	dc.l	0
special		dc.w	$84
srcX		dc.w	0
srcY		dc.w	0
pwidth		dc.w	0
pheight		dc.w	0
temp1		dc.w	0
temp2		dc.w	0
destCols	dc.l	0
destRows	dc.l	0

error		dc.l	0

JT:		dc.l 0

IntuitionBase   dc.l 0
GfxBase		dc.l 0
      
ioRequest:	dc.l 0
msgPort:	dc.l 0
ScreenBase:	dc.l 0
newScreen	dc.l 0
bitMap		dc.l 0
sigTask		dc.l 0
sigBit		dc.b 0		

		dc.l 0
; Dump string data segment
PrinterName:	dc.b	"printer.device",0
ScTitle:	dc.b	"Screen",0
Pname:		dc.b	"MyPrtPort",0
		even

; Trackdisk.device variables

tsk		dc.l 0
pk		dc.l 0
onflag		dc.l 0
diskreq		dc.l 0
diskport	dc.l 0
monoff		dc.l 0
drivenum	dc.l 0
tracknumber	dc.l 0
secnumber	dc.l 0
DBase		dc.l 0
diskop		dc.l 0
vartype		dc.l 0
TDerror		dc.l 0 
TDreturn        dc.l 0 
TDbuffer	dc.l 0
string		dc.l 0
Trackbuffer	dc.l 0
drive		dc.b 0

		dc.l 0

; Trackdisk string data seqment
Dname:		dc.b	"MyDiskPort",0
DiskName:	dc.b	"trackdisk.device",0
DosName:	dc.b	"dos.library",0
		even

L1
L2

********************************************************************
* Dump - a function to dump an amos screen to the printer.
*        the error is returned  eg. e = Dump(0)
***********************************************************
* This version does a straight full-screen dump.
*************************************************
L_Dump1		equ	3  
L3:	movem.l	a4-a6,-(sp)
	DLea	MB,a2
	Rbsr	L_GetScr
	move.w	width-MB(a2),pwidth-MB(a2)
	move.w	height-MB(a2),pheight-MB(a2)
	move.l	#0,destCols-MB(a2)
	move.l	#0,destRows-MB(a2)
	move.w	#$8c,special-MB(a2)	; ASPECT | FULLROWS | FULLCOLS
	Rbra	L_Dump

**************************************************************
* This version does a partial dump, retaining the proportions
* of the page to printer.
* ie. 1/2 screen fills 1/2 page
**************************************************************
L_Dump2		equ	4
L4:	movem.l	a4-a6,-(sp)
	DLoad	a2

	Rbsr	L_Dump2a
	
; Calculate proportional DestX, DestY
	clr.l	d0
	clr.l	d1
	move.w	pwidth-MB(a2),d0
	tst.w	d0
	Rbeq	L_BadDim	
	move.w	width-MB(a2),d1
	divu.w	d0,d1
	tst.w	d1
	Rbeq	L_BadDim	
	lea	destCols-MB(a2),a0
	move.l	#$ffff,d0
	divu.w	d1,d0
	move.l	d0,(a0)
	andi.l	#$ffff,(a0)
	move.l	#$10,d1
	move.l	(a0),d0
	rol.l	d1,d0
	move.l	d0,(a0)

	clr.l	d0
	clr.l	d1
	move.w	pheight-MB(a2),d0
	tst.w	d0
	Rbeq	L_BadDim
	move.w	height-MB(a2),d1
	divu.w	d0,d1
	tst.w	d1
	Rbeq	L_BadDim
	lea	destRows-MB(a2),a0
	move.l	#$ffff,d0
	divu.w	d1,d0
	move.l	d0,(a0)
	andi.l	#$ffff,(a0)
	move.l	#$10,d1
	move.l	(a0),d0
	rol.l	d1,d0
	move.l	d0,(a0)

	lea	special-MB(a2),a0
	move.w	#$b0,(a0)
	Rbra 	L_Dump

*****************************************************************
* This version is the most powerful
* it allows you to pass screen dimensions AND printer dimensions
* as well as the 'special' parameter to allow for any
* sort of printing.
*****************************************************************
L_Dump3:	equ	5
L5:	movem.l	a4-a6,-(sp)
	DLoad	a2
	Rbsr	L_Dump3a
	Rbra	L_Dump

******* Routine
L_Dump3a:	equ	6
L6:	lea	special-MB(a2),a0
	lea	2(a3),a3
	move.w	(a3)+,(a0)		; special
	lea	destRows-MB(a2),a0
	move.l	(a3)+,(a0)		; destY
	lea	destCols-MB(a2),a0
	move.l	(a3)+,(a0)		; destY
	Rbra	L_Dump2a

******* Routine
L_Dump2a:	equ	7
L7:	lea	pheight-MB(a2),a0
	lea	2(a3),a3
	move.w	(a3)+,(a0)		; bottom y
	lea	pwidth-MB(a2),a0
	lea	2(a3),a3
	move.w	(a3)+,(a0)		; bottom x
	lea	srcY-MB(a2),a0
	lea	2(a3),a3
	move.w	(a3)+,(a0)		; srcy
	lea	srcX-MB(a2),a0
	lea	2(a3),a3
	move.w	(a3)+,(a0)		; srcx
	Rbsr	L_GetScr

; get srcX,Y back

	move.w	srcX-MB(a2),d0
	move.w	srcY-MB(a2),d1

; calculate width, height

	neg.w	d0
	neg.w	d1

	lea	pwidth-MB(a2),a0
	add.w	d0,(a0)			; width = -srcX+bottomX
	lea	pheight-MB(a2),a0
	add.w	d1,(a0)			; height = -srcY+bottomY
	
	rts

********************************************************************
* A couple of little routines that are used
********************************************
L_BadDim:	equ	8
L8:	lea	error-MB(a2),a0		; we were given some zeros
	move.l	#4,(a0)			; to divide by!!!
	Rbra	L_exit

L_GetScr:	equ	9
L9:	move.l	ScOnAd(a5),d0	
	Rbeq	L_JScnop		; get the address of the screen 
	move.l	d0,a0
	move.l 	a0,ScreenBase-MB(a2)
; Now we need to extract all the information about the screen
	move.w	EcTx(a0),width-MB(a2)		; width
	move.w	EcTy(a0),height-MB(a2)		; height
	move.w	EcCon0(a0),d0
	and.w	#%1000100010000100,d0		; Hires / HAM / DualPF / Halfbrite
	move.w	d0,viewModes-MB(a2)		; View Modes
	move.w	EcNPlan(a0),depth-MB(a2)	; depth
	rts

********************************************************************
* Do the screen dump
*********************
L_Dump		equ	10
L10

; Initialise colour map
	move.l	T_GfxBase(a5),a6
	moveq	#32,d0
	jsr	-570(a6)
	tst.l	d0
	beq	cl5
	move.l	d0,colourMap-MB(a2)
	move.l	d0,a0
	moveq	#0,d0
	move.l	ScreenBase-MB(a2),a1
	lea	EcPal(a1),a1
.loop	move.w	(a1)+,d1
	move.w	d1,d3
	and.w	#$f,d3
	lsr.w	#4,d1
	move.w	d1,d2
	and.w	#$f,d2
	lsr.w	#4,d1
	movem.l	d0/a0-a1,-(sp)
	jsr	-$276(a6)
	movem.l	(sp)+,d0/a0-a1
	addq.w	#1,d0
	cmp.w	#32,d0
	bne.s	.loop

; Create port
	move.l	$4,a6
	move.l	#-1,d0
	jsr	-330(a6)		; AllocSignal(a6)		
	lea	sigBit-MB(a2),a0
	move.b	d0,(a0)
	
	move.l	#40,d0
	move.l	#65537,d1
	jsr	AllocMem(a6)		; Allocate msgPort memory
	tst.l	d0
	beq	cl5
	lea	msgPort-MB(a2),a0
	move.l	d0,(a0)
	
	move.l	d0,a0			; a0 = msgPort
	move.b	#4,8(a0)		; Type = NT_MSGPORT
	move.b	#0,9(a0)		; Priority = 0
	lea	Pname-MB(a2),a1
	move.l	a1,10(a0)		; name of port
	move.b	#0,14(a0)		; flags
	move.b	sigBit-MB(a2),15(a0)	

	move.l	#0,a1
	jsr	FindTask(a6)
	lea 	sigTask-MB(a2),a1
	move.l	d0,(a1)
	move.l	msgPort-MB(a2),a0
	move.l	d0,16(a0)

	move.l	a0,a1
	jsr	AddPort(a6)
	
; CreatExtIO

	move.l	#64,d0
	move.l	#65537,d1
	jsr	AllocMem(a6)		; Allocate ioRequest memory
	tst.l	d0
	beq	cl6
	lea	ioRequest-MB(a2),a0
	move.l	d0,(a0)

	move.l	d0,a0
	move.b	#5,8(a0)		; type = NT_MESSAGE
	move.b	#0,9(a0)		; priority 0	
	move.l	msgPort-MB(a2),14(a0)
	
; Open printer device.

	move.l	$4,a6
	lea	PrinterName-MB(a2),a0
	move.l	#0,d0
	move.l	ioRequest-MB(a2),a1
	move.l	#0,d1
	jsr	OpenDev(a6)
	tst.l	d0
	bne	cl7
	
; Dump the rast port!!!

	move.l	ioRequest-MB(a2),a1
	move.w	#11,28(a1)		; command
	move.l	T_RastPort(a5),32(a1)	; rastPort
	move.l	colourMap-MB(a2),36(a1)	; colourmap
	move.w	viewModes-MB(a2),40+2(a1)	; viewmodes
	move.w	srcX-MB(a2),44(a1)	; srcX
	move.w	srcY-MB(a2),46(a1)	; srcY
	move.w	pwidth-MB(a2),48(a1)	; srcWidth
	move.w	pheight-MB(a2),50(a1)	; srcHeight
	move.l	destCols-MB(a2),52(a1)	; destCols
	move.l	destRows-MB(a2),56(a1)	; destRows
	move.w	special-MB(a2),60(a1)	; special

	jsr	DoIO(a6)		; do it!!!! (at last)
	lea	error-MB(a2),a0
	move.l	d0,(a0)

; What follows is the several stages of cleaning up required

cl8:	move.l	$4,a6
	move.l	ioRequest-MB(a2),a1
	jsr	CloseDev(a6)
	
cl7:	move.l	ioRequest-MB(a2),a1
	move.b	#$ff,8(a1)
	move.l	#-1,20(a1)
	move.l	#-1,24(a1)

	move.l	$4,a6
	move.l	#64,d0
	move.l	ioRequest-MB(a2),a1
	jsr	FreeMem(a6)		: de-allocate ioRequest mem.

cl6:	move.l	$4,a6
	move.l	msgPort-MB(a2),a1
	jsr	RemPort(a6)		; Remove port

	move.l	msgPort-MB(a2),a0
	move.b	#$ff,8(a0)
	move.l	#-1,20(a0)

	move.b	sigBit,d0
	jsr	-336(a6)		;FreeSignal(a6)

	move.l	#40,d0
	move.l	msgPort-MB(a2),a1
	jsr	FreeMem(a6)		; de-allocate msgPort memory

cl5:	move.l	colourMap-MB(a2),a0
	move.l	T_GfxBase(a5),a6
	jsr	-576(a6)		; Free colourmap

	Rbra	L_exit

******* Return to user
L_exit:		equ	11
L11	move.l	error-MB(a2),d3		; return the error number
	movem.l	(sp)+,a4-a6
	moveq	#0,d2
  	rts

********************************************************************
* Perr: This reserved variable returns descriptions of
*        errors, either the last error - Perr$
*	 or any error - Perr$(n)
********************************************************
L_Perr		equ	12
L12	movem.l	a4-a6,-(sp)	; parameter specifies error
	DLoad	a2
	move.l	error-MB(a2),d0
	lea	PDERR(pc),a0
.lop	tst.l	d0
	beq	pe1
	clr.l	d1
	move.w	(a0),d1
	andi	#1,d1		; d1 contains alignment correction
	adda.w	(a0),a0		; allow for the text
	lea	2(a0),a0	; allow for the length word	
	adda.w	d1,a0		; align to word boundary
	subq	#1,d0
	bra.s	.lop
pe1:	move.l	a0,d3
	moveq	#2,d2
	movem.l	(sp)+,a4-a6
	rts
; Error messages list - Accessed via Perr
PDERR	dc.w	3
	dc.b	"Ok.",0
	dc.w	20
	dc.b	"User cancelled dump."
	dc.w	23
	dc.b	"Not a graphics printer.",0
	dc.w	1
	dc.b	" ",0
	dc.w	19
	dc.b	"Illegal dimensions.",0
	dc.w	1
	dc.b	" ",0
	dc.w	24
	dc.b	"No memory for variables."
	dc.w	21
	dc.b	"No memory for buffer.",0
	dc.w	14
	dc.b	"Unknown error."
	even

L13
L14
L15

***********************************************************
*		JUMP TO ERROR MESSAGES
L_JScnop	equ	16
L16	moveq	#47,d0
	Rjmp	L_Error
***********************************************************
*		ERROR MESSAGES
L17
L18
L19

***********************************************************
* 
* Trackdisk.device commands 
* by Alex J. Grant Apr 1991
* converted to Amos1.3/Compiler Oct 1991
***********************************************************


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

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



* First, the general routines
******************************

* The functions CreatePort & CreatExtIo follow,
* to be  used by all the trackdisk routines 
*****************************************************

* Open dos.library
*******************

L_OpenDos	equ 20
L20	DLoad	a2
	lea	DosName-MB(a2),a1	; library name
	move.l	#33,d0			; revision number
	move.l	$4,a6			; exec base
	jsr	-$228(a6)		; OpenLibrary
	tst.l	d0
	beq	ODerr
	lea	DBase-MB(a2),a0		; store library base
	move.l	d0,(a0)
	move.l	#0,d0
	rts

ODerr:	move.l	#1,d0
	rts

* CreatePort
*
* a0-> msgport
* a1-> name
*
* returns error in d0, if <>0 then error!
******************************************

L_CreateDiskPort	equ 21
L21	DLoad	a2
	move.l	a0,a3	; port
	move.l	a1,a4	; name
	move.l	$4,a6
	move.l	#-1,d0
	jsr	-330(a6)		
	lea	sigBit-MB(a2),a0
	move.b	d0,(a0)

	move.l	#40,d0
	move.l	#65537,d1
	jsr	AllocMem(a6)		; Allocate msgPort memory
	tst.l	d0
	beq	CPerr
	lea	(a3),a0
	move.l	d0,(a0)
	
	move.l	d0,a0			; a0 = msgPort
	move.b	#4,8(a0)		; Type = NT_MSGPORT
	move.b	#0,9(a0)		; Priority = 0
	lea	(a4),a1
	move.l	a1,10(a0)		; name of port
	move.b	#0,14(a0)		; flags
	move.b	sigBit-MB(a2),15(a0)	

	move.l	#0,a1
	jsr	FindTask(a6)
	lea 	sigTask-MB(a2),a1
	move.l	d0,(a1)
	move.l	(a3),a0
	move.l	d0,16(a0)

	move.l	a0,a1
	jsr	AddPort(a6)

	move.l	#0,d0
	rts

CPerr:	move.l	#1,d0
	rts

* CreateExtIO
*
* d0->size
* a0->IOreq
* a1->msgport
*
* error returned in d0
***********************

L_CreateExtIO	equ 22
L22	move.l	a0,a3			; save IOExtTD
	move.l 	a1,a4			; save msgport

	move.l	#56,d0
	move.l	#65537,d1
	jsr	AllocMem(a6)		; Allocate diskreq memory
	tst.l	d0
	beq	CEXerr
	move.l	a3,a0
	move.l	d0,(a0)

	move.l	d0,a0
	move.b	#5,8(a0)		; type = NT_MESSAGE
	move.b	#0,9(a0)		; priority 0	
	move.l	(a4),14(a0)		; msgport

	move.l	#0,d0
	rts

CEXerr:	move.l	#1,d0
	rts


L_MotorOnOff	equ 23
*
* a0-> struct IOExtTD
* d0-> On Off flag ... 0=off, 1=on
***********************************

L23	lea	36(a0),a1
	move.l	d0,(a1)
	lea	28(a0),a1
	move.w	#9,(a1)		; TD_MOTOR

	move.l	$4,a6
	move.l	a0,a1
	jsr	DoIO(a6)

	rts

L_RW_Track	equ 24
*
* Read or Write a track, putting error number into TDerror
* a0-> struct IOExtTD
* a1-> buffer
* d0.l-> track number
* d3.l-> sector number
* d1.w-> TD_Command 2=Read, 3=Write
**************************************************

L24	lea	36(a0),a0
	move.l	#512,(a0)+		; iotd_Req.io_Length
	move.l	a1,(a0)+		; iotd_Req.io_Data
	
	move.l	#5632,d2
	mulu.w	d0,d2			; track * TRACK_SIZE
	move.l	d2,(a0)			; io_Offset

	move.l	#512,d2			; sector * TD_SECTOR
	mulu.w	d3,d2			; 
	add.l	d2,(a0)			; add it to io_Offset

	lea	-16(a0),a0		; io_Command
	move.w	d1,(a0)			; d1=command

	move.l	$4,a6
	lea	-28(a0),a1		; a1=address of IOExtTD
	pea	0(a1)

	jsr	DoIO(a6)

	move.l	(sp)+,a0
	DLoad	a2
	lea	TDerror-MB(a2),a1
	move.b	31(a0),(a1)
	move.b	31(a0),d0

	rts

L_F_Track	equ 25
*
* Format a track, putting error number into TDerror
* a0-> struct IOExtTD
* a1-> buffer
* d0.l-> track number
**************************************************

L25	lea	36(a0),a0
	move.l	#5632,(a0)+		; iotd_Req.io_Length
	move.l	a1,(a0)+		; iotd_Req.io_Data
	
	move.l	#5632,d2
	mulu.w	d0,d2			; track * TRACK_SIZE
	move.l	d2,(a0)			; io_Offset

	lea	-16(a0),a0		; io_Command
	move.w	#11,(a0)		; d1=command

	move.l	$4,a6
	lea	-28(a0),a1		; a1=address of IOExtTD
	pea	0(a1)

	jsr	DoIO(a6)

	move.l	(sp)+,a0
	DLoad	a2
	lea	TDerror-MB(a2),a1
	move.b	31(a0),(a1)
	move.b	31(a0),d0

	rts
	
		
L26:

L_ChangeState	equ 27
*
* Returns the disk in drive status
* a0->IOExtTD
***********************************	

L27	pea	0(a0)
	lea	28(a0),a0
	move.w	#$e,(a0)	; TD_CHANGESTATE
	
	move.l	$4,a6
	jsr	DoIO(a6)

	move.l	(sp)+,a0
	move.l	32(a0),d0	; io_Actual

	rts

L_ProStatus	equ 28
*
* Returns the write protect status
* a0->IOExtTD
***********************************	

L28	pea	0(a0)
	lea	28(a0),a0
	move.w	#$f,(a0)	; TD_PROSTATUS
	
	move.l	$4,a6
	jsr	DoIO(a6)

	move.l	(sp)+,a0
	move.l	32(a0),d0	; io_Actual

	rts

*******************************************************
*
* Ok, here are the commands, starting with 
* one to see if the disk is inserted
*
*******************************************************

L_DiskIn	equ 29
L29	movem.l	a4-a6,-(sp)
	DLoad 	a2
	lea	drivenum-MB(a2),a0
	move.l	(a3)+,(a0)

	lea	diskop-MB(a2),a0
	move.l	#0,(a0)
	lea	vartype-MB(a2),a0		; returns integer
	move.l	#0,(a0)
	Rbra	L_Gen

L_WProtect	equ 30
L30	movem.l	a4-a6,-(sp)
	DLoad	a2
	lea	drivenum-MB(a2),a0
	move.l	(a3)+,(a0)

	lea	diskop-MB(a2),a0
	move.l	#4,(a0)
	lea	vartype-MB(a2),a0
	move.l	#0,(a0)			; returns integer
	Rbra	L_Gen

L_ReadTrack	equ 31
L31	movem.l	a4-a6,-(sp)

	DLoad	a2
	lea	secnumber-MB(a2),a0
	move.l	(a3)+,(a0)
	lea	tracknumber-MB(a2),a0
	move.l	(a3)+,(a0)
	lea	drivenum-MB(a2),a0
	move.l	(a3)+,(a0)

	lea	diskop-MB(a2),a0
	move.l	#8,(a0)
	lea	vartype-MB(a2),a0
	move.l	#2,(a0)			; returns a string

	Rbra 	L_Gen

					
L_WriteTrack	equ 32
L32	movem.l	a4-a6,-(sp)
	
	DLoad	a2
	lea	string-MB(a2),a0
	move.l	(a3)+,(a0)		; data to be written
	lea	secnumber-MB(a2),a0
	move.l	(a3)+,(a0)
	lea	tracknumber-MB(a2),a0
	move.l	(a3)+,(a0)
	lea	drivenum-MB(a2),a0
	move.l	(a3)+,(a0)

; now we need to copy the data from the string to the buffer

	move.l	#512,d0			; length to be copied
	move.l	#0,d1			; index register
	move.l	string-MB(a2),a0
	move.l	TDbuffer-MB(a2),a1
	cmpi.w	#512,(a0)		; check length of string
	bgt	copy1
	move.w	(a0),d0			; length = len(string)
copy1:	lea	2(a0),a0
cloop:	cmp.l	d1,d0
	beq	cdone			; finished
	move.b	(a0)+,(a1)+		; do the copy
	addq	#1,d1
	bra	cloop
					
cdone:	cmpi.l	#512,d1
	beq	calldone		; completely finished
	clr.b	(a1)+
	addq	#1,d1
	bra	cdone
	
calldone:
	lea	diskop-MB(a2),a0
	move.l	#12,(a0)
	lea	vartype-MB(a2),a0
	move.l	#0,(a0)			; returns an integer
	Rbra 	L_Gen

L_FormatTrack	equ 33
L33	movem.l	a4-a6,-(sp)

	DLoad	a2
	lea	tracknumber-MB(a2),a0
	move.l	(a3)+,(a0)
	lea	drivenum-MB(a2),a0
	move.l	(a3)+,(a0)

	move.l	#5632,d0	; allocate a track size buffer
	move.l	#3,d1		; MEMF_CHIP | MEMF_PUBLIC
	move.l	$4,a6
	jsr	AllocMem(a6)
	lea	Trackbuffer-MB(a2),a0		
	tst.l	d0
	beq	Ferr1
	move.l 	d0,(a0)	

	lea	diskop-MB(a2),a0
	move.l	#16,(a0)
	lea	vartype-MB(a2),a0
	move.l	#0,(a0)			; returns an integer
	Rbra 	L_Gen
	
Ferr1:	lea	TDerror-MB(a2),a0
	move.l	#-1,(a0)
	Rbra	L_TDcl0			; error!!	

L_Derr	equ 34
L34	movem.l	a4-a6,-(sp)

	DLoad 	a2
	move.l	TDerror-MB(a2),d3
	move.l	#0,d2

	movem.l	(sp)+,a4-a6
	rts

	
***********************************************
* Now the general skeleton  to set up all the
* things required by trackdisk.
* It branches off in the middle to do whatever
***********************************************

L_Gen	equ 35
L35	Rbsr	L_OpenDos
	tst.l	d0
	Rbne 	L_TDcl0

	DLoad	a2
	lea	diskport-MB(a2),a0
	lea	Dname-MB(a2),a1
	
	Rbsr	L_CreateDiskPort
	tst.l	d0
	Rbne	L_TDcl1

	lea	diskreq-MB(a2),a0
	lea	diskport-MB(a2),a1
	move.l	#68,d0
	Rbsr	L_CreateExtIO
	tst.l	d0
	Rbne	L_TDcl2

; Open the device

	move.l	$4,a6

	lea	DiskName-MB(a2),a0
	move.l	drivenum-MB(a2),d0
	move.l	diskreq-MB(a2),a1
	move.l	#0,d1
	
	jsr	OpenDev(a6)
	lea	TDerror-MB(a2),a0
	move.l	d0,(a0)
	tst.l	d0
	Rbne	L_TDcl3

; now jump to individual routines for each different
; operation

	move.l	diskop-MB(a2),d0
	lea	DJT(pc),a0
	jmp	(a0,d0.l)

; this is the disk operation jump table

DJT:	Rbra	L_DiskIn2
	Rbra	L_WProtect2
	Rbra	L_ReadTrack2
	Rbra	L_WriteTrack2
	Rbra	L_Formattrack2
	even
	
********************
* Clean up and exit
********************

L_TDcl4	equ 36
L36	move.l	$4,a6
	DLoad	a2
	move.l	diskreq-MB(a2),a1
	jsr	CloseDev(a6)
	Rbra	L_TDcl3

L_TDcl3	equ 37
L37	DLoad	a2
	move.l	diskreq-MB(a2),a1
	move.b	#$ff,8(a1)
	move.l	#-1,20(a1)
	move.l	#-1,24(a1)

	move.l	$4,a6
	move.l	#56,d0
	move.l	diskreq-MB(a2),a1
	jsr	FreeMem(a6)		: de-allocate ioRequest mem.
	Rbra	L_TDcl2

L_TDcl2	equ 38
L38	move.l	$4,a6
	DLoad	a2
	move.l	diskport-MB(a2),a1
	jsr	RemPort(a6)		; Remove port

	move.l	diskport-MB(a2),a0
	move.b	#$ff,8(a0)
	move.l	#-1,20(a0)

	move.b	sigBit-MB(a2),d0
	jsr	-336(a6)		; free signal

	move.l	#40,d0
	move.l	diskport-MB(a2),a1
	jsr	FreeMem(a6)		; de-allocate msgPort memory
	Rbra	L_TDcl1

L_TDcl1	equ 39
L39	move.l	$4,a6
	DLoad	a2
	move.l	DBase-MB(a2),a1		; Close dos.library
	jsr 	-$19e(a6)
	Rbra	L_TDcl0

L_TDcl0	equ 40
L40	DLoad	a2
	lea	diskop-MB(a2),a0
	cmpi.l	#8,(a0)
	bne 	notread

	move.l	TDbuffer-MB(a2),d3	; here we need to return the
	move.l	TDbuffer-MB(a2),a0	; string, not a number!
	move.w	#512,(a0)		; this is the length of the buffer
	move.l	#2,d2

	bra	byebye

notread:
	Rbsr	L_NiceErrors
	DLoad	a2
	move.l	TDreturn-MB(a2),d3
	move.l	vartype-MB(a2),d2

byebye:	movem.l	(sp)+,a4-a6
	rts

*
* Create nice error numbers to return with
* ie. TRUE or FALSE
*******************************************

L_NiceErrors	equ 41
L41	DLoad	a2
	lea	TDreturn-MB(a2),a0
	move.l	TDerror-MB(a2),d0
	tst.l	d0
	bne	Ferr
	move.l	#TRUE,(a0)	 
	rts
Ferr	move.l	#FALSE,(a0)
	rts

* 
* Find the disk in/out status
******************************

L_DiskIn2	equ 42
L42	DLoad	a2
	move.l	diskreq-MB(a2),a0
	Rbsr	L_ChangeState
	lea	TDerror-MB(a2),a0
	move.l	d0,(a0)		; Save actural error number
	Rbra	L_TDcl4

*
* See if disk is write protected
*********************************
L_WProtect2	equ 43
L43	DLoad	a2
	move.l	diskreq-MB(a2),a0
	Rbsr	L_ProStatus
	lea	TDerror-MB(a2),a0
	move.l	d0,(a0)
	Rbra	L_TDcl4

*
* Read a track
***************

L_ReadTrack2	equ 44
L44	DLoad	a2
	move.l	diskreq-MB(a2),a0
	move.l	#1,d0
	Rbsr	L_MotorOnOff		; turn the mC D!I !|&!|Nu"HLNA >0 1| 2 CBNh"@pE G(K&Q!6#L6` -x61| 1|  !|   N    HallonSoft 1990!         D   P  x    HallonBoot v1.4F1. Remove fastmem F2. Remove extra-drives 	(F3. Reset 1F4. Small CLI :F5. Large CLI CF6. Normal CLI R Mahoney & Kaktus 1990  disk.resource dos.library intuition.library     DOS    C6p N,@"z N: z p"<  @tNz z "z !I  |  #H 0< PBQ"z p7rN"z tA pN | $HC fp QA  !J Bh 1|  rNqQQ!n & Bh ,x C NJg
 @ h p NupNudos.library    P     @  8     $  
               graphics.library NO VIRUS ON BOOTBLOCK! a (W)XCOPY NEITHER BOOT OR VIRUS ON THIS DISK ... DOS       pC  NJg @ h p 3  NupNudos.library DOS       pC NJg
 @ h p NupNudos.library                       DISK MASTER V3.0                     Coded by GULLY   D               @        ll 8       <<<~~<<  <|<<f~88<~f     8               r~B<f~0 > ~~< ~  0p   0q<<`0f0fq0q =0f|0q3<  0f0f`q0qf  f <ll>lf   f8ff<`0fff` f<fflfffff`lflffZff``8  `  6 `  `             3f >6<f@ H3  f0   pcc# $f<f H $ l$<cf$ `f$ f  $ $ $   < `h08<   nl|`ffnv<ff``fl`ffpff<f`0l <l<6<0;l8ff|<=>>ffccf~8 fl0f<< f ~ <~00  0f&&f<<<<<<<~~~~f<<<<f6ffff~f<<<<<<~<<<<<8888|<<<<< >f  l<v `~ ~ ~|<>  g f|fxx~x`||8ff<`F  vfnfxfvlwffffv`ffk6fLc 3flx< f  ~  ` t 0p3,,,0ffffffo````fffffffffclfffff|ffffff~gf  0 @<   0v0ff  nv~ff``fflb`lf<<2`   f`f~0ffxkfffff<ffkfG f<>0B<<  f       0 f`~~~~~~|fxxxxf6ffff<cf`~~~~ffffff kf   l|f `f `ffffff`  fflf`fffffl`lfff<ff`   ffff`0<flcff|>`f<66<2c 3f<0  3           313f<````lfffffcfffff~ffffffff````ffffffs<  lv 8   <~~<<<<8x<>f~<8<<<><   ;<<;<x<f<cf<`|;6c~8 ~ ~ <<       ~       gbg<~~~~<<<<< >>>><`l;;;;;;w<<<<<<<<<|f<<<<< >          0              0                                         |  <