;----------------------------------------------------------------------------
;File name:	URAn_SYS.S			Revision date:	1996.02.13
;Creator:	Ulf Ronald Andersson		Creation date:	1992.11.15
;(c)1992 by:	Ulf Ronald Andersson	All rights reserved
;Released as:	FREEWARE		(NB: commercial sales forbidden!)
;File purpose:	Main macro & symbol library for Atari ST systems
;----------------------------------------------------------------------------
;	Condition ensures inclusion once per pass only
;
	ifnd	URAn_SYS_defined
URAn_SYS_defined	set	1
;
;----------------------------------------------------------------------------
;	The following macros implement module linking
;	on assembly source level of the code
;
;
_uniref		MACRO	symbol
		ifnd	_uni_ct\1
		ifd	_uni_rf\1
		ifnd	_uni_ok\1
		FAIL	Missing macro links, use 'make' or '_unidef'
		endc
		endc
_uni_ct\1	set	1
_uni_rf\1	=	1
		elseif
_uni_ct\1	set	_uni_ct\1+1
		endc
		endm	;_uniref
;
;
_unidef		macro	symbols
		ifnd	_uni_df\1
		ifd	_uni_ct\1
_uni_df\1	set	1
_uni_ok\1	=	1
_uni_flag	set	1
\1_code:
		\1_code
		endc
		endc
		ifnc	'','\2'
		_unidef	\2,\3,\4,\5,\6,\7,\8,\9,\10
		endc
		endm	;_unidef
;
;
make		macro	linkmacros
_uni_flag	set	0
		\1
		ifne	_uni_flag
		make	\1
		endc
		ifnc	'','\2'
		make	\2,\3,\4,\5,\6,\7,\8,\9,\10
		endc
		endm	;make
;
;----------------------------------------------------------------------------
;	The following macro defines unique labels
;
_unilab		macro	symbol
		ifnd	_uni_lb\1
_uni_lb\1	set	1
\1:
_uni_flag	set	1
		elseif
_uni_flag	set	0
		endc
		endm
;
;----------------------------------------------------------------------------
;	The following macro defines unique symbols
;
_unisym		macro	symbol,value
		ifnd	_uni_lb\1
_uni_lb\1	set	1
\1		= \2
_uni_flag	set	1
		elseif
_uni_flag	set	0
		endc
		endm
;
;----------------------------------------------------------------------------
;	Macro to XDEF labels only at requested debugging levels
;
_debdef	macro	symbol,levlim
	ifd	_deblev
	ifne	_deblev>=\2
	XDEF	\1
	endc
	endc
	endm
;
	ifnd	_deblev
_deblev	set	0
	endc
;
;----------------------------------------------------------------------------
;	Macro to create an exception frame on supervisor stack
;
push_ex	macro	v1,v2
	tst	_longframe
	beq.s	.cpu_adapted_\@
	clr	-(sp)
.cpu_adapted_\@:
	ifne	(NARG=1)
	pea	\1
	elseif
	ifne	(NARG=2)
	ifne	(_ind=\1)
	move.l	\2,-(sp)
	elseif
	FAIL	Strange 'push_ex' arguments: \1,\2
	endc
	elseif
	FAIL	Strange 'push_ex' arguments: \1,\2
	endc
	endc
	move	sr,-(sp)
	endm
;
;----------------------------------------------------------------------------
;	Macro to copy a string    NB: 2 adress regs are also changed
;
strcpy	macro	p1,p2
.strcpy_\@:
	move.b	(\1)+,(\2)+
	bne.s	.strcpy_\@
	endm
;
;----------------------------------------------------------------------------
;	Ascii constants
;
NUL	=	$00
BEL	=	$07
BS	=	$08
HT	=	$09
LF	=	$0A
VT	=	$0B
FF	=	$0C
CR	=	$0D
ESC	=	$1B
;
;
;	Diverse constants
;
maxsbyte	=	$7F
maxubyte	=	$FF
maxsword	=	$7FFF
maxuword	=	$FFFF
maxslong	=	$7FFFFFFF
maxulong	=	$FFFFFFFF
;
_ind	= 1<<30	;bit 30 is indirection flag for some macro libraries
;
;
;----------------------------------------------------------------------------
;	System init vectors
;
ssp_init	=	$000
ev_reset	=	$004
;
;
;	System exception vectors
;
ev_buserr	=	$008
ev_adrerr	=	$00C
ev_illegal	=	$010
ev_divby0	=	$014
ev_chk_ofl	=	$018
ev_trapv	=	$01C
ev_priverr	=	$020
ev_trace	=	$024
ev_a_line	=	$028
ev_f_line	=	$02C
;
; vectors at $030..$03B are unassigned
; vector at $3C is dubiously specified, but can not be considered free
; vectors at $040..$05F are unassigned
;
ev_spurerr	=	$060
ev_level1	=	$064
ev_level2	=	$068
ev_level3	=	$06C
ev_level4	=	$070
ev_level5	=	$074
ev_level6	=	$078
ev_level7	=	$07C
; the odd vectors above are unavailable since IPL0 is tied high
ev_HBI		=	ev_level2
ev_VBI		=	ev_level4
ev_MFP		=	ev_level6
;
ev_trap0	=	$080
ev_trap1	=	$084
ev_trap2	=	$088
ev_trap3	=	$08C
ev_trap4	=	$090
ev_trap5	=	$094
ev_trap6	=	$098
ev_trap7	=	$09C
ev_trap8	=	$0A0
ev_trap9	=	$0A4
ev_trap10	=	$0A8
ev_trap11	=	$0AC
ev_trap12	=	$0B0
ev_trap13	=	$0B4
ev_trap14	=	$0B8
ev_trap15	=	$0BC
;
ev_gemdos	=	ev_trap1
ev_xgemdos	=	ev_trap2
ev_bios		=	ev_trap13
ev_xbios	=	ev_trap14
;
; vectors at $0C0..$0FF are unassigned
;
;----------------------------------------------------------------------------
; 	interrupt vectors for MFP interrupts
;
iv_cenbusy	=	$100
iv_v24dcd	=	$104
iv_v24cts	=	$108
iv_blitter	=	$10C
iv_time_d	=	$110
iv_time_c	=	$114
iv_kb_midi	=	$118
iv_disk		=	$11C
iv_time_b	=	$120
iv_v24terr	=	$124
iv_v24treq	=	$128
iv_v24rerr	=	$12C
iv_v24rreq	=	$130
iv_time_a	=	$134
iv_v24ring	=	$138
iv_monodet	=	$13C
;
; vectors at $140..$1FF are unassigned
; vectors at $200..$37F are reserved for OEM products
;
;----------------------------------------------------------------------------
; 	System bomb info for debug analysis
;
bombflag	=	$380
bomb_d0		=	$384
bomb_d1		=	$388
bomb_d2		=	$38C
bomb_d3		=	$390
bomb_d4		=	$394
bomb_d5		=	$398
bomb_d6		=	$39C
bomb_d7		=	$3A0
bomb_a0		=	$3A4
bomb_a1		=	$3A8
bomb_a2		=	$3AC
bomb_a3		=	$3B0
bomb_a4		=	$3B4
bomb_a5		=	$3B8
bomb_a6		=	$3BC
bomb_a7		=	$3C0
bombvector	=	$3C4
bomb_usp	=	$3C8
bomb_ssp	=	bomb_a7
bombstack	=	$3CC
;
; vector area at $3EC..$3FF is unassigned, but known to be used by
; some system support programs for non_vector purposes (eg. timesave)
;
;
;----------------------------------------------------------------------------
; 	System variables
;
etv_timer	=	$400
etv_critic	=	$404
etv_term	=	$408
etv_extra	=	$40C	;5.L
memvalid	=	$420
memctrl		=	$424
resvalid	=	$426
resvector	=	$42A
phystop		=	$42E
_membot		=	$432
_memtop		=	$436
memval2		=	$43A
flock		=	$43E
seekrate	=	$440
_timr_ms	=	$442
_fverify	=	$444
_bootdev	=	$446
_palmode	=	$448
defshiftmd	=	$44A
sshiftmd	=	$44C
_v_bas_ad	=	$44E
vblsem		=	$452
nvbls		=	$454
_vblqueue	=	$456
colorptr	=	$45A
screenpt	=	$45E
_vbclock	=	$462
_frclock	=	$466
hdv_init	=	$46A
swv_vec		=	$46E
hdv_bpb		=	$472
hdv_rw		=	$476
hdv_boot	=	$480
hdv_mediach	=	$47E
_cmdload	=	$482
conterm		=	$484
trp14ret	=	$486
criticret	=	$48A
themd		=	$48E
_md		=	$49E
savptr		=	$4A2
_nflops		=	$4A6
constate	=	$4A8
save_row	=	$4AC
sav_contxt	=	$4AE
_bufl		=	$4B2
_hz_200		=	$4BA
the_env		=	$4BE
_drvbits	=	$4C2
_dskbufp	=	$4C6
_autopath	=	$4CA
_vbl_list	=	$4CE	;8.L
_prt_cnt	=	$4EE
_prtabt		=	$4F0
_sysbase	=	$4F2
_shell_p	=	$4F6
end_os		=	$4FA
exec_os		=	$4FE
scr_dump	=	$502
prv_lsto	=	$506
prv_lst		=	$50A
prv_auxo	=	$50E
prv_aux		=	$512
pun_ptr		=	$516
memval3		=	$51A
dev_vecs	=	$51E	;8.L (1.L per device) per I/O function
xconstat	=	$51E
xconin		=	$53E
xcostat		=	$55E
xconout		=	$57E
_longframe	=	$59E
_cookies	=	$5A0
;
;
;----------------------------------------------------------------------------
;	System vector numbers
;-------------------------------------
;		;vector $00 does not exist (SSP value for reset)
;-------------------------------------
evn_reset	=	$01
evn_buserr	=	$02
evn_adrerr	=	$03
evn_illegal	=	$04
evn_divby0	=	$05
evn_chk_ofl	=	$06
evn_trapv	=	$07
evn_priverr	=	$08
evn_trace	=	$09
evn_a_line	=	$0a
evn_f_line	=	$0b
;-------------------------------------
;		;vectors $0C..$0E are unassigned
;		;vector $0F is dubiously specified, but is not free
;		;vectors $10..$17 are unassigned
;-------------------------------------
evn_spurerr	=	$18
evn_level1	=	$19
evn_level2	=	$1a
evn_level3	=	$1b
evn_level4	=	$1c
evn_level5	=	$1d
evn_level6	=	$1e
evn_level7	=	$1f
; 		;odd vectors above are unavailable with IPL0 tied high
;-------------------------------------
evn_HBI		=	evn_level2
evn_VBI		=	evn_level4
evn_MFP		=	evn_level6
;-------------------------------------
evn_trap0	=	$20
evn_trap1	=	$21
evn_trap2	=	$22
evn_trap3	=	$23
evn_trap4	=	$24
evn_trap5	=	$25
evn_trap6	=	$26
evn_trap7	=	$27
evn_trap8	=	$28
evn_trap9	=	$29
evn_trap10	=	$2a
evn_trap11	=	$2b
evn_trap12	=	$2c
evn_trap13	=	$2d
evn_trap14	=	$2e
evn_trap15	=	$2f
;-------------------------------------
evn_gemdos	=	evn_trap1
evn_xgemdos	=	evn_trap2
evn_bios	=	evn_trap13
evn_xbios	=	evn_trap14
;-------------------------------------
;		;vectors $30..$3F are unassigned
;-------------------------------------
; 	MFP interrupt vector numbers
;-------------------------------------
ivn_cenbusy	=	$40
ivn_v24dcd	=	$41
ivn_v24cts	=	$42
ivn_blitter	=	$43
ivn_time_d	=	$44
ivn_time_c	=	$45
ivn_kb_midi	=	$46
ivn_disk	=	$47
ivn_time_b	=	$48
ivn_v24terr	=	$49
ivn_v24treq	=	$4a
ivn_v24rerr	=	$4b
ivn_v24rreq	=	$4c
ivn_time_a	=	$4d
ivn_v24ring	=	$4e
ivn_monodet	=	$4f
;-------------------------------------
;		;vectors $50..$7F are unassigned
;		;vectors $80..$DF are reserved for OEM products
;		;vectors $E0..$FA do not exist (bomb info area)
;		;vectors $FB..$FF may not exist (unassigned but often used!)
;-------------------------------------
; 	System variable vector numbers
;-------------------------------------
etvn_timer	=	$100
etvn_critic	=	$101
etvn_term	=	$102
;----------------------------------------------------------------------------
;	OS header offsets
;
os_codebra	= $00	;w $60xx
os_version	= $02	;w $0v0r
os_reset_p	= $04	;L
os_selfbeg_p	= $08	;L
os_varend_p	= $0c	;L
os_unknown_p	= $10	;L
os_gem_mpb_p	= $14	;L
os_date_bcd	= $18	;L
os_config	= $1c	;w
os_date_gem	= $1e	;w
;	next follows the part valid only for TOS 1.02 & later
os_pool_p	= $20
os_kbshift_p	= $24
os_currbp_p_p	= $28
os_reserved	= $2c
;
TOS_100_kbshift	= $E1B
TOS_100_currbp_p= $602C
;
;-------------------------------------
;	Cartridge ROM definitions
;
rom_diag_id	= $fa52255f
rom_appl_id	= $abcdef42
rom_base	= $fa0000
rom_head	= $fa0004
;
rh_diagcode	= 0
rh_applnext_p	= 0
rh_applinit_p	= 4
rh_applcode_p	= 8
rh_appltime	= 12
rh_appldate	= 14
rh_applsize	= 16
rh_applname	= 20	;<=13b incl terminal NUL
;
;-------------------------------------
;	IOREC data structure
;
io_sel_serial	= 0
io_sel_keyboard	= 1
io_sel_midi	= 2
;
io_buffer_p	=  0	;L-> data buffer (NB: kbd uses 4 bytes/char)
io_size_ix	=  4	;w = total size of buffer, in bytes
io_head_ix	=  6	;w = indexes position of next write
io_tail_ix	=  8	;w = indexes position of next read
io_lo_fill	= 10	;w = flow reactivation limit
io_hi_fill	= 12	;w = flow deactivation limit
;
;-------------------------------------
;	KBDVBASE data structure
;
kv_midi_input	= $00
kv_keybrd_err	= $04
kv_midi_err	= $08
kv_ikbd_stat	= $0C
kv_mouse_pack	= $10
kv_clock_pack	= $14
kv_joyst_pack	= $18
kv_midi_vec	= $1C
kv_ikbd_vec	= $20
;
;-------------------------------------
;	Virtual Screen cookie structure
;
vs_vs_magic	= $00	;l = 'VSCR' when screen driver is _active_ !!!
vs_driver_magic = $04	;l = XBRA/Cookie id of screen driver
vs_version	= $08	;w = version of VSCR protocol (for future exp)
vs_x		= $0A	;w = left edge \
vs_y		= $0C	;w = top edge   \/ of visible
vs_w		= $0E	;w = width      /\ rectangle
vs_h		= $10	;w = height    /
vs_size		= $12	;size of virtual screen structure
vs_rect		= vs_x
;
;-------------------------------------
;	Font header offsets
;
;NB: Intel format is used in all words and longs, when fnt_flag bit 2 is zero.
;NB: Test this as 68000 word, however, for the first byte contains that flag
;NB: in Intel mode only, with the second byte zeroed so 68000 test will work.
;NB: In 68000 mode the first byte is always zero, so Intel test would fail.
;
fnt_id		= $00	;w 
fnt_pts		= $02	;w
fnt_name	= $04	;32b
fnt_lasc	= $24	;w
fnt_hasc	= $26	;w
fnt_dtop	= $28	;w
fnt_dasce	= $2A	;w
fnt_dhalf	= $2C	;w
fnt_ddesc	= $2E	;w
fnt_dbott	= $30	;w
fnt_charw	= $32	;w
fnt_cellw	= $34	;w
fnt_loff	= $36	;w
fnt_roff	= $38	;w
fnt_weight	= $3A	;w
fnt_ulheight	= $3C	;w
fnt_litemask	= $3E	;w
fnt_skewmask	= $40	;w
fnt_flag	= $42	;w
fnt_hor_tp	= $44	;l->horiz. offset table, or = fnt_chr_tp
fnt_chr_tp	= $48	;l->char. offset table, in files use $58 (fnt_next+4)
fnt_fbase	= $4C	;l->
fnt_fwidth	= $50	;w
fnt_fheight	= $52	;w
fnt_next	= $54	;l->next font in chain
;
;
;----------------------------------------------------------------------------
; 	fpu offsets (68881 or 68882)
;
fpu_base	=	$00
fpu_stat	=	$00	;W Rd
fpu_cont	=	$02	;W Wr
fpu_save	=	$04	;W Rd
fpu_rest	=	$06	;W R/W
fpu_comm	=	$0A	;W Wr
fpu_cond	=	$0E	;W Wr
fpu_oper	=	$10	;L R/W
fpu_rsel	=	$14	;W Rd
fpu_iadr	=	$18	;L Wr
fpu_opad	=	$1C	;L R/W (dummy in 68881 and 68882)
;
;
;----------------------------------------------------------------------------
; 	System hardware
;
hw_mapper	=	$ffff8001
;
hw_vbase2	=	$ffff8201	;base MSB
hw_vbase1	=	$ffff8203
hw_vpos2	=	$ffff8205	;pos MSB
hw_vpos1	=	$ffff8207
hw_vpos0	=	$ffff8209	;pos LSB
hw_syn		=	$ffff820A
;
hw_vbase0H	=	$ffff820C	;STE (unused)
hw_vbase0	=	$ffff820D	;base LSB, STE
hw_horextH	=	$ffff820E	;STE (unused)
hw_horext	=	$ffff820F	;STE
;
hw_pal		=	$ffff8240
hw_pal_00	=	hw_pal+00
hw_pal_01	=	hw_pal+02
hw_pal_02	=	hw_pal+04
hw_pal_03	=	hw_pal+06
hw_pal_04	=	hw_pal+08
hw_pal_05	=	hw_pal+10
hw_pal_06	=	hw_pal+12
hw_pal_07	=	hw_pal+14
hw_pal_08	=	hw_pal+16
hw_pal_09	=	hw_pal+18
hw_pal_10	=	hw_pal+20
hw_pal_11	=	hw_pal+22
hw_pal_12	=	hw_pal+24
hw_pal_13	=	hw_pal+26
hw_pal_14	=	hw_pal+28
hw_pal_15	=	hw_pal+30
;
hw_rez		=	$ffff8260
;
hw_pixoffH	=	$ffff8264	;STE (unused)
hw_pixoff	=	$ffff8265	;STE
;
hw_dmadata	=	$ffff8604
hw_dmascnt	=	$ffff8604
hw_dmastat	=	$ffff8606
hw_dmacont	=	$ffff8606
hw_dmamode	=	$ffff8606
dma_srcmd	= $080
dma_srtrk	= $082
dma_srsec	= $084
dma_srdat	= $086
dma_srcnt	= $090
dma_wr_bit	= $100
dma_swcmd	= $180
dma_swtrk	= $182
dma_swsec	= $184
dma_swdat	= $186
dma_swcnt	= $190
hw_dmabase2	=	$ffff8609	;MSB
hw_dmabase1	=	$ffff860B
hw_dmabase0	=	$ffff860D	;LSB
;
hw_giselect	=	$ffff8800
hw_psgsel	=	$ffff8800
hw_psgrd	=	$ffff8800
hw_giwrite	=	$ffff8802
hw_psgwr	=	$ffff8802
;
hw_sdmacontH	=	$ffff8900	;STE (unused)
hw_sdmacont	=	$ffff8901	;STE
hw_sdmabeg2	=	$ffff8903	;STE
hw_sdmabeg1	=	$ffff8905	;STE
hw_sdmabeg0	=	$ffff8907	;STE
hw_sdmaloop2	=	$ffff8909	;STE
hw_sdmaloop1	=	$ffff890B	;STE
hw_sdmaloop0	=	$ffff890D	;STE
hw_sdmaend2	=	$ffff890F	;STE
hw_sdmaend1	=	$ffff8911	;STE
hw_sdmaend0	=	$ffff8913	;STE
;
hw_sdmamode	=	$ffff8921	;STE
;
hw_mywiredata	=	$ffff8922	;STE 16 bits
hw_mywiremask	=	$ffff8924	;STE 16 bits
;
hw_switches	=	$ffff9200	;FALCON
hw_joyfire	=	$ffff9201	;STE 4 bits
hw_joydirect	=	$ffff9202	;STE 4*4bits
;
hw_pad0_y	=	$ffff9211	;STE
hw_pad0_x	=	$ffff9213	;STE
hw_pad1_y	=	$ffff9215	;STE
hw_pad1_x	=	$ffff9217	;STE
;
hw_light_x	=	$ffff9220	;STE
hw_light_y	=	$ffff9222	;STE
;
hw_mfp		=	$ffffFA01
hw_gpip		=	$ffffFA01
hw_aer		=	$ffffFA03
hw_ddr		=	$ffffFA05
hw_iera		=	$ffffFA07
hw_ierb		=	$ffffFA09
hw_ipra		=	$ffffFA0B
hw_iprb		=	$ffffFA0D
hw_isra		=	$ffffFA0F
hw_isrb		=	$ffffFA11
hw_imra		=	$ffffFA13
hw_imrb		=	$ffffFA15
hw_vr		=	$ffffFA17
hw_tacr		=	$ffffFA19
hw_tbcr		=	$ffffFA1B
hw_tcdcr	=	$ffffFA1D
hw_tadr		=	$ffffFA1F
hw_tbdr		=	$ffffFA21
hw_tcdr		=	$ffffFA23
hw_tddr		=	$ffffFA25
hw_scr		=	$ffffFA27
hw_ucr		=	$ffffFA29
hw_rsr		=	$ffffFA2B
hw_tsr		=	$ffffFA2D
hw_udr		=	$ffffFA2F
;
hw_fpu_base	=	$ffffFA40	;SFP-004 with 68881 or 68882
hw_fpu_stat	=	$ffffFA40	;W Rd
hw_fpu_cont	=	$ffffFA42	;W Wr
hw_fpu_save	=	$ffffFA44	;W Rd
hw_fpu_rest	=	$ffffFA46	;W R/W
hw_fpu_comm	=	$ffffFA4A	;W Wr
hw_fpu_cond	=	$ffffFA4E	;W Wr
hw_fpu_oper	=	$ffffFA50	;L R/W
hw_fpu_rsel	=	$ffffFA54	;W Rd
hw_fpu_iadr	=	$ffffFA58	;L Wr
hw_fpu_opad	=	$ffffFA5C	;L R/W (dummy in 68881 and 68882)
;
hw_kbstat	=	$ffffFC00
hw_kbcont	=	$ffffFC00
hw_kbdata	=	$ffffFC02
;
hw_midistat	=	$ffffFC04
hw_midicont	=	$ffffFC04
hw_mididata	=	$ffffFC06
;
;----------------------------------------------------------------------------
	endc	;URAn_SYS_defined
;----------------------------------------------------------------------------
; End of file:	URAn_SYS.S
;----------------------------------------------------------------------------
