;----------------------------------------------------------------------------
;File name:	URAn_DOS.S		Revision date:	1997.01.04
;Creator:	Ulf Ronald Andersson	Creation date:	1991.01.02
;(c)1992 by:	Ulf Ronald Andersson	All rights reserved
;Released as:	FREEWARE		(NB: commercial sales forbidden!)
;File purpose:	Symbol & Macro library for GEMDOS, XBIOS, and BIOS functions
;----------------------------------------------------------------------------
;
;	Condition ensures inclusion once per pass only
;
	ifnd	URAn_DOS_defined
URAn_DOS_defined	set	1
;
;----------------------------------------------------------------------------
;
	include	tos\URAn_SYS.S
;
;----------------------------------------------------------------------------
;	GEMDOS DEFINITIONS
;
;trap<<20 + args<<16 + ptrbits<<12 + longbits<<8 + opcode
;
Pterm0		=	$100000
Cconin		=	$100001
Cconout		=	$110002
Cauxin		=	$100003
Cauxout		=	$110004
Cprnout		=	$110005
Crawio		=	$110006
Crawcin		=	$100007
Cnecin		=	$100008
Cconws		=	$111009
Cconrs		=	$11100A
Cconis		=	$10000B
Dsetdrv		=	$11000E
Cconos		=	$100010
Cprnos		=	$100011
Cauxis		=	$100012
Cauxos		=	$100013
; Maddalt	=	$14	;???
Dgetdrv		=	$100019
Fsetdta		=	$11101A
Super		=	$111020
Tgetdate	=	$10002A
Tsetdate	=	$11002B
Tgettime	=	$10002C
Tsettime	=	$11002D
Fgetdta		=	$10002F
Sversion	=	$100030
Ptermres	=	$120131
Dfree		=	$121036
Dcreate		=	$111039
Ddelete		=	$11103A
Dsetpath	=	$11103B
Fcreate		=	$12103C
Fopen		=	$12103D
Fclose		=	$11003E
Fread		=	$13423F
Fwrite		=	$134240
Fdelete		=	$111041
Fseek		=	$130142
Fattrib		=	$131043
Mxalloc		=	$120144	;very modern TOS only
Fdup		=	$110045
Fforce		=	$120046
Dgetpath	=	$121047
Malloc		=	$110148
Mfree		=	$111049
Mshrink		=	$13244A
Pexec		=	$14E04B
Pterm		=	$11004C
Fsfirst		=	$12104E
Fsnext		=	$10004F
Frename		=	$136056
Fdatime		=	$131057
;
;
;----------------------------------------------------------------------------
;	BIOS DEFINITIONS
;
;trap<<20 + args<<16 + ptrbits<<12 + longbits<<8 + opcode
;
Getmpb		=	$D11000
Bconstat	=	$D10001
Bconin		=	$D10002
Bconout		=	$D20003
Rwabs		=	$D52004
Setexc		=	$D22005
Tickal		=	$D00006
Getbpb		=	$D10007
Bcostat		=	$D10008
Mediach		=	$D10009
Drvmap		=	$D0000A
Kbshift		=	$D1000B
;
;
;----------------------------------------------------------------------------
;	XBIOS DEFINITIONS
;
;trap<<20 + args<<16 + ptrbits<<12 + longbits<<8 + opcode
;
Initmous	=	$E36000
Ssbrk		=	$E20001
Physbase	=	$E00002
Logbase		=	$E00003
Getrez		=	$E00004
Setscreen	=	$E33005
Setpallete	=	$E11006
Setpalette	=	$E11006
Setcolor	=	$E20007
Floprd		=	$E71208
Flopwr		=	$E71209
Flopfmt		=	$E71F0A
;; CLR_D0		=	$E0000B
Midiws		=	$E2200C
Mfpint		=	$E2200D
Iorec		=	$E1000E
Rsconf		=	$E6000F
Keytbl		=	$E37010
Random		=	$E00011
Protobt		=	$E41212
Flopver		=	$E71213
Scrdmp		=	$E00014
Cursconf	=	$E20015
Settime		=	$E10116
Gettime		=	$E00017
Bioskeys	=	$E00018
Ikbdws		=	$E22019
Jdisint		=	$E1001A
Jenabint	=	$E1001B
Giaccess	=	$E2001C
Offgibit	=	$E1001D
Ongibit		=	$E1001E
Xbtimer		=	$E4801F
Dosound		=	$E11020
Setprt		=	$E10021
Kbdvbase	=	$E00022
Kbrate		=	$E20023
Prtblk		=	$E11024
Vsync		=	$E00025
Supexec		=	$E11026
Puntaes		=	$E00027
Floprate	=	$E20029     ;STE & Rainbow TOS
;-------------------------------------
; Dmaread	=	$E4412A	;=> E_INVFN if absent
; Dmawrite	=	$E4412B	;=> E_INVFN if absent
;-------------------------------------
Bconmap		=	$E1002C	;-2=>struct  -1=>currdev  0=>test  6=>ST-port  7=>SCC_B  8=>TTMFP  9=>SCC_A
;-------------------------------------
; Nvmaccess	=	$2E	;???
;-------------------------------------
;0 VOID Metainit ( META_INFO_1 *buffer );
;1 LONG Metaopen ( SHORT drive, META_DRVINFO *buffer );
;2 LONG Metaclose ( SHORT drive );
;3 LONG Metaread ( SHORT drive, VOID *buffer, LONG blockno, SHORT count );
;4 LONG Metawrite ( SHORT drive, VOID *buffer, LONG blockno, SHORT count );
;5
;6 LONG Metastatus ( SHORT drive, VOID *buffer );
;7 LONG Metaioctl ( SHORT drive, LONG magic, SHORT opcode, VOID *buffer );
;8
;9
;A
;B LONG Metastartaudio ( SHORT drive, SHORT flag, UBYTE *bytes_p);
;C LONG Metastopaudio ( SHORT drive );
;D LONG Metasetsongtime (SHORT drive,SHORT repeat,LONG starttime,LONG endtime );
;E LONG Metagettoc ( SHORT drive, SHORT flag, CD_TOC_ENTRY *buffer );
;F LONG Metadiscinfo ( SHORT drive, CD_DISC_INFO *p );
;-------------------------------------
Metainit	=	$E11030
Metaopen	=	$E22031
Metaclose	=	$E10032
Metaread	=	$E42433
Metawrite	=	$E42434
;code $35 is unknown
Metastatus	=	$E22036
Metaioctl	=	$E48237
;code $38 is unknown
;code $39 is unknown
;code $3A is unknown
Metastartaudio	=	$E3403B	;use: flag=0  bytes_p[2]={songs,startsong}
Metastopaudio	=	$E1003C
Metasetsongtime	=	$E40C3D
Metagettoc	=	$E3403E
Metadiscinfo	=	$E2203F
;-------------------------------------
Blitmode	=	$E10040	;all late TOS
; Esetshift	=	$50	;???
; Egetshift	=	$51	;???
Esetbank	=	$E10052	;TT ?
; Esetcolor	=	$53	;???
; Esetpalette	=	$54	;???
; Egetpalette	=	$55	;???
Esetgray	=	$E10056	;TT ?
; Esetsmear	=	$57	;???
; ???		?	$58	;??? Used by NVDI !!!
;
;----------------------------------------------------------------------------
;	Error code definitions
;
E_OK		=	  0
E_ERROR		=	 -1
E_DRVNR		=	 -2
E_UNCMD		=	 -3
E_CRC		=	 -4
E_BADRQ		=	 -5
E_SEEK		=	 -6
E_MEDIA		=	 -7
E_SECNF		=	 -8
E_PAPER		=	 -9
E_WRITF		=	-10
E_READF		=	-11
E_WRPRO		=	-12
; ???		=	-13
E_CHNG		=	-14
E_UNDEV		=	-15
E_BADSF		=	-16
E_OTHER		=	-17
;
;	--------
;
E_INVFN		=	-32
E_FILNF		=	-33
E_PTHNF		=	-34
E_NHNDL		=	-35
E_ACCDN		=	-36
E_IHNDL		=	-37
; ???		=	-38
E_NSMEM		=	-39
E_IMBA		=	-40
; ???		=	-41
E_DFULL		=	-42
; ???		=	-43
; ???		=	-44
; ???		=	-45
E_DRIVE		=	-46
; ???		=	-47
E_NSAME		=	-48
E_NMFIL		=	-49
;
;	--------
;
E_RANGE		=	-64
E_NAMETOOLONG	=	E_RANGE
E_INTRN		=	-65
E_PLFMT		=	-66
E_GSBF		=	-67
;
;	--------
;
E_LOCKED	=	-58
E_NSLOCK	=	-59
E_LOOP		=	-80
E_MOUNT		=	-200
;
;
;----------------------------------------------------------------------------
;	Boot sector offsets
;
bs_codebra	=  0	;$00  w $60xx
bs_filler	=  2	;$02 6b
bs_serial	=  8	;$08 3b
bs_sectbytes	= 11	;$0b iw
bs_clust_sects	= 13	;$0d  b
bs_boot_sects	= 14	;$0e iw
bs_disk_FATs	= 16	;$10  b
bs_root_files	= 17	;$11 iw
bs_disk_sects	= 19	;$13 iw
bs_media	= 21	;$15  b
bs_FAT_sects	= 22	;$16 iw
bs_track_sects	= 24	;$18 iw
bs_disk_sides	= 26	;$1a iw
bs_hide_sects	= 28	;$1c iw
;	next follows the part used only by executable boots
bs_execflg	= 30	;$1e   w
bs_ldmode	= 32	;$20   w
bs_ssect	= 34	;$22   w
bs_sectcnt	= 36	;$24   w
bs_ldaddr	= 38	;$26   L
bs_fatbuf	= 42	;$2a   L
bs_fname	= 46	;$2e 11b
bs_fname_end	= 57	;$39   b
bs_bootprog	= 58	;$3a ...
;
;
;----------------------------------------------------------------------------
;	BPB offsets
;
		rsreset
bpb_sect_bytes	rs.w	1	;typ:  512
bpb_clust_sects	rs.w	1	;typ:    2
bpb_clust_bytes	rs.w	1	;typ: 1024
bpb_root_sects	rs.w	1	;typ:    7
bpb_FAT_sects	rs.w	1	;typ:    5
bpb_FAT2_start	rs.w	1	;typ:    6
bpb_data_start	rs.w	1	;typ:   18 = 2*5 + 7 + 1
bpb_data_clusts	rs.w	1	;typ:  711 = 720 - 18/2
bpb_flag	rs.w	1	;b0==16bit_FAT/12bit_FAT typ: 0
bpb_size	=	__RS
;NB: data_start = FAT_sects*2 + root_sects + 1  rounded up for clust_sects alignment
;NB: data_clusts= Total_clusts - data_start/clust_sect
;
;
;----------------------------------------------------------------------------
;	Directory offsets
;
		rsreset
dir_fname	rs.b	8	;first char $00==unused $E5==erased
dir_fext	rs.b	3
dir_fattr	rs.b	1
dir_reserved	rs.b	10
dir_ftime	rs.w	1	;iw
dir_fdate	rs.w	1	;iw
dir_fcluster	rs.w	1	;iw
dir_fsize	rs.l	1	;il
dir_size	=	__RS
;
;
;----------------------------------------------------------------------------
;	DTA offsets
;
		rsreset
dta_gname	rs.b	12
dta_drive	rs.l	1
dta_dbpos	rs.w	1
dta_dsec0	rs.w	1
dta_gattr	rs.b	1
dta_fattr	rs.b	1
dta_ftime	rs.w	1
dta_fdate	rs.w	1
dta_fsize	rs.l	1
dta_fname	rs.b	14
dta_size	=	__RS
;
;
;----------------------------------------------------------------------------
;	Program file header offsets
;
		rsreset
ph_codebra	rs.w	1	;always $601A
ph_textlen	rs.l	1
ph_datalen	rs.l	1
ph_bss_len	rs.l	1
ph_symblen	rs.l	1
ph_res1		rs.l	1
ph_res2		rs.l	1
ph_flag		=	ph_res2	;only TOS >= 1.4: bit 0 == fastload
ph_res3		rs.w	1
ph_textbeg	=	__RS
ph_size		=	__RS
;
;
;----------------------------------------------------------------------------
;	Buffer Control Block offsets
;
bcb_link_p	rs.l	1
bcb_drive	rs.w	1
bcb_type	rs.w	1
bcb_record	rs.w	1
bcb_dirty	rs.w	1
bcb_dm_p	rs.l	1
bcb_data_p	rs.l	1
bcb_size	=	__RS
;
;
;----------------------------------------------------------------------------
;	Basepage offsets
;
	rsreset
bp_selfbeg_p	rs.l	1
bp_selfend_p	rs.l	1
bp_textbeg_p	rs.l	1
bp_textlen	rs.l	1
bp_databeg_p	rs.l	1
bp_datalen	rs.l	1
bp_bss_beg_p	rs.l	1
bp_bss_len	rs.l	1
bp_dta_p	rs.l	1
bp_parent_p	rs.l	1
bp_reserved	rs.l	1
bp_environ_p	rs.l	1
bp_undef	rs.l	20
bp_arglen	rs.b	1
bp_argstring	rs.b	127
;
;
;----------------------------------------------------------------------------
;	MACRO DEFINITIONS
;
gemdos	MACRO	op,v2,v3,v4,v5
	zz_prep_trap	\1,<\2>,<\3>,<\4>,<\5>
	trap	#zzDOSTR
	ifne	\1-Pterm0
	ifne	\1-Pterm
	ifne	\1-Ptermres
	ifne	(zzDOSAS>0)&(zzSTkeep=0)
	ifne	(zzDOSAS>8)|zzDUMBcomp
	lea	zzDOSAS(sp),sp
	elseif
	addq	#zzDOSAS,sp
	endc
	endc
	endc
	endc
	endc
	ENDM
;
sub_gemdos	MACRO	op,v2,v3,v4,v5
	zz_prep_trap	\1,<\2>,<\3>,<\4>,<\5>
	bsr	gemdos_sub
	ifne	\1-Pterm0
	ifne	\1-Pterm
	ifne	\1-Ptermres
	ifne	(zzDOSAS>0)&(zzSTkeep=0)
	ifne	(zzDOSAS>8)|zzDUMBcomp
	lea	zzDOSAS(sp),sp
	elseif
	addq	#zzDOSAS,sp
	endc
	endc
	endc
	endc
	endc
	ENDM
;
bios	MACRO	op,v2,v3,v4,v5,v6
	xbios	\1,<\2>,<\3>,<\4>,<\5>,<\6>
	ENDM
;
sub_bios	MACRO	op,v2,v3,v4,v5,v6
	zz_prep_trap	\1,<\2>,<\3>,<\4>,<\5>,<\6>
	bsr	bios_sub
	ifne	(zzDOSAS>0)&(zzSTkeep=0)
	ifne	(zzDOSAS>8)|zzDUMBcomp
	lea	zzDOSAS(sp),sp
	elseif
	addq	#zzDOSAS,sp
	endc
	endc
	ENDM
;
xbios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DOSAS
	FAIL	Stack offset is odd
	endc
	ifgt	zzDOSAS
	MOVE	#zzDOSOP,-(sp)
	elseif
	ifeq	zzDOSAS
	move	#zzDOSOP,(sp)
	elseif
	FAIL	Stack offset is too large
	endc
	endc
	ENDM
;
;This macro handles all argument checking for bios, xbios & gemdos functions
;special argument cases are:
;no argument = FAIL error
;'()'	= Assumes argument is on stack already
;'!'	= Places 0 on stack using "clr"
;'?'	= Places -1 on stack
DOSPUSH	MACRO	v1,v2
zzDOSAS	set	zzDOSAS+2
	IFNE	(1<<\1)&(zzDOSPF!zzDOSLF)
zzDOSAS	set	zzDOSAS+2
	ENDC
	ifne	1&zzDOSAS
	FAIL	Stack offset is odd
	endc
	IFC	'()','\2'
	MEXIT
	ENDC
	IFC	'','\2'
	FAIL	Missing operand in system call
	MEXIT
	ENDC
	ifgt	zzDOSAS
	IFNE	(1<<\1)&zzDOSPF
	iflt	zzDOSAS-4
	FAIL	Pointer argument crosses stack offset
	endc
	IFC	'!','\2'
	clr.l	-(sp)
	MEXIT
	ENDC
	IFC	'?','\2'
	pea	(-1).w
	MEXIT
	ENDC
	IFEQ	zzDOSIF
	pea	\2
	MEXIT
	ENDC
	move.l	\2,-(sp)
	MEXIT
	ENDC			;ends pointer case
	IFNE	(1<<\1)&zzDOSLF
	iflt	zzDOSAS-4
	FAIL	Long argument crosses stack offset
	endc
	IFC	'!','\2'
	clr.l	-(sp)
	MEXIT
	ENDC
	IFC	'?','\2'
	pea	(-1).w
	MEXIT
	ENDC
	move.l	\2,-(sp)
	MEXIT
	ENDC			;ends long case
	IFC	'!','\2'
	clr	-(sp)
	MEXIT
	ENDC
	IFC	'?','\2'
	move	#-1,-(sp)
	MEXIT
	ENDC
	move	\2,-(sp)	;ends word case
	MEXIT
	elseif		;start stack offset cases	
	ifeq	zzDOSAS
	IFNE	(1<<\1)&zzDOSPF
	IFC	'!','\2'
	clr.l	(sp)
	MEXIT
	ENDC
	IFC	'?','\2'
	move.l	#-1,(sp)
	MEXIT
	ENDC
	IFEQ	zzDOSIF
	addq	#4,sp
	pea	\2
	MEXIT
	ENDC
	move.l	\2,(sp)
	MEXIT
	ENDC			;ends pointer case
	IFNE	(1<<\1)&zzDOSLF
	IFC	'!','\2'
	clr.l	(sp)
	MEXIT
	ENDC
	IFC	'?','\2'
	move.l	#-1,(sp)
	MEXIT
	ENDC
	move.l	\2,(sp)
	MEXIT
	ENDC			;ends long case
	IFC	'!','\2'
	clr	(sp)
	MEXIT
	ENDC
	IFC	'?','\2'
	move	#-1,(sp)
	MEXIT
	ENDC
	move	\2,(sp)	;ends word case
	MEXIT
	elseif		;handle negative offset levels
	IFNE	(1<<\1)&zzDOSPF
	IFC	'!','\2'
	clr.l	-zzDOSAS(sp)
	MEXIT
	ENDC
	IFC	'?','\2'
	move.l	#-1,-zzDOSAS(sp)
	MEXIT
	ENDC
	IFEQ	zzDOSIF
	move.l	#\2,-zzDOSAS(sp)
	MEXIT
	ENDC
	move.l	\2,-zzDOSAS(sp)
	MEXIT
	ENDC			;ends pointer case
	IFNE	(1<<\1)&zzDOSLF
	IFC	'!','\2'
	clr.l	-zzDOSAS(sp)
	MEXIT
	ENDC
	IFC	'?','\2'
	move.l	#-1,-zzDOSAS(sp)
	MEXIT
	ENDC
	move.l	\2,-zzDOSAS(sp)
	MEXIT
	ENDC			;ends long case
	IFC	'!','\2'
	clr	-zzDOSAS(sp)
	MEXIT
	ENDC
	IFC	'?','\2'
	move	#-1,-zzDOSAS(sp)
	MEXIT
	ENDC
	move	\2,-zzDOSAS(sp)	;ends word case
	MEXIT
	endc
	endc
	ENDM
;
zzDOSAS		set	0
zzSToff		set	0
zzSTkeep	set	0
zzDUMBcomp	set	0
;
;----------------------------------------------------------------------------
	endc	;URAn_DOS_defined
;----------------------------------------------------------------------------
;End of file:	URAn_DOS.S
;----------------------------------------------------------------------------
