;APS0003337E0002F3170000F1590000AE900001153900002E5E0004DA5C0003139B0003139B000321D4
;---------------T---------T-----------------------------T-----------------T
;* SOS *	-Save Our Sources-	coded by Valenta Ferenc
;
; vss.asm - VF Sound System driver software
;
; WARNING: » exec/initializers.i has been modified (MEXIT problem)
;          » libraries/mui.i has been modified
;          » exec/typesext.i no longer needed by my programs
;          » Devs:AHI/vss.audio now built into the main file
;
; To be compiled with Asm-Pro


	incdir	inc:

	include	exec_lib.i
	include	dos_lib.i
	include	graphics_lib.i
	include	intuition_lib.i
	include	expansion_lib.i
	include	layers_lib.i
	include	gadtools_lib.i
	include	utility_lib.i
	include	icon_lib.i
	include	commodities_lib.i
	include	reqtools_lib.i
	include	mpega_lib.i
	include	screennotify_lib.i
	include	timer_lib.i
	include	ahi_lib.i
	include	ahi_sub_lib.i
	include	VSS:Sources/include/muimaster_lib.i
	include	VSS:Sources/include/vss_lib.i

	include	exec/types.i
	include	exec/nodes.i
	include	exec/lists.i
	include	exec/resident.i
	include	exec/libraries.i
	include	exec/initializers.i
	include	exec/interrupts.i
	include	exec/memory.i
	include	exec/ports.i
	include	exec/tasks.i
	include	exec/semaphores.i
	include	exec/execbase.i
	include	exec/devices.i
	include	exec/io.i
	include	exec/errors.i
	include	exec/macros.i
	include	exec/alerts.i
	include	dos/dos.i
	include	dos/dostags.i
	include	dos/dosextens.i
	include	dos/datetime.i
	include	dos/rdargs.i
	include	graphics/gfx.i
	include	graphics/gfxbase.i
	include	graphics/gfxnodes.i
	include	graphics/view.i
	include	graphics/monitor.i
	include	graphics/copper.i
	include	graphics/displayinfo.i
	include	graphics/modeid.i
	include	graphics/text.i
	include	graphics/clip.i
	include	graphics/rastport.i
	include	graphics/layers.i
	include	intuition/intuition.i
	include	intuition/intuitionbase.i
	include	intuition/iobsolete.i
	include	intuition/classes.i
	include	intuition/classusr.i
	include	intuition/imageclass.i
	include	intuition/gadgetclass.i
	include	intuition/preferences.i
	include	intuition/screens.i
	include	intuition/sghooks.i
	include	utility/tagitem.i
	include	utility/pack.i
	include	utility/hooks.i
	include	workbench/workbench.i
	include	workbench/startup.i
	include	reqtools/reqtools.i
	include	libraries/ahi_sub.i
	include	libraries/commodities.i
	include	libraries/iffparse.i
	include	libraries/dos.i
	include	libraries/dosextens.i
	include	libraries/asl.i
	include	libraries/configregs.i
	include	libraries/configvars.i
	include	devices/ahi.i
	include	devices/clipboard.i
	include	devices/inputevent.i
	include	devices/timer.i
	include	devices/melodympeg.i
	include	devices/serial.i
	include	hardware/intbits.i
	include	VSS:Sources/include/mui.i
	include	VSS:Sources/include/vss.i

failonpass1	set	TRUE
;testmode	set	TRUE
;mpusemagic	set	TRUE	;check magic number
;mpmagicbefore	set	TRUE	;magic before stream
;checkptr	set	TRUE	;check ptr2
;makecolor	set	TRUE	;bg color timing monitor
quantizeneg	set	TRUE	;quantize negativ samples
melodyport2	set	TRUE
dragsetcurdir	set	TRUE
;playlistquiet	set	TRUE
cs8130	set	TRUE

melodybufdiv	equ	2

;failonpass1:	Az ellenorzo makrok a forditast mar a pass 1 kozben
;	megszakitjak. Igy gyorsabban kiderulnek a hibak, viszont a
;	makrok nem tudnak hibauzenetet kiirni.
;
;testmode:	Megvaltoztatott azonositokat hasznal, igy egyszerre ket
;	peldanyban is futtathato a driver (debuggolaskor hasznos)
;
;mpusemagic:	Ellenorzi az mpega streameket, hogy a driver emulacioja
;	keszitette-e vagy nem. Ha nem, tovabbpasszolja az eredeti
;	librarynek. A magic number mindenkepp felulir valamit, es
;	hiaba allitja vissza a driver, ugy tunik nem biztonsagos.
;	Ha le van tiltva, es a forcempa flag nem aktiv, es az
;	mpega libet nem lehet flusholni, az emulacio nem indul el.
;
;mpmagicbefore:	Az ellenorzeshez szukseges magic numbert a stream struktura
;	elejere teszi, egyebkent a vegere. Egyik sem tokeletes :(
;
;checkptr:	A kartya ptr2 regiszterenek olvasasakor visszaellenoriz, csak
;	akkor megy tovabb ha ketszer ugyanazt olvassa vissza.
;	(Nincs ra szukseg, mar biztonsagos az olvasas, hardveresen
;	megoldottam)
;
;makecolor:	A hatter csikozasaval mutatja hogy mennyi idot tolt a driver
;	az adatmasolo hookokban.
;
;quantizeneg:	A levelmeterek hisztogram uzemmodjaban kulon figyelembe veszi a
;	negativ mintakat is. Egyebkent valamivel gyorsabb lenne, csak
;	az alacsony frekvencias komponenseknel bugzik.
;
;melodyport2:	Egy kulon message port segitsegevel korrektul lekezeli a
;	Quick IO-val erkezo CMD_WRITE requesteket is. Sajnos valamiert
;	osszeveszik az Amplifierrel. Vagy nincs eleg signal, vagy a
;	verem fogy el... Ezert ki lehet kapcsolni. Kulonben sincs
;	ertelme a quick write requesteknek, lehetetlenne tenne a
;	double bufferinget, csak a korrektseg kedveert implementaltam.
;
;melodybufdiv:	Az adatmasolo ciklus 2^ adatot visz at. A buffer es a chunk meret
;	ennek tobbszorose kell legyen.
;
;dragsetcurdir:	A player file requester current dir beallitodik a drag'n'drop-al
;	betoltott fajlokra is.
;
;playlistquiet:	A player kikapcsolja a file-listat a fajlok hozzadasa kozben.
;
;cs8130:	CS8130 alapu soros portos IRDA port taviranyito tamogatas

; Hands off, no user serviceable code below ;)

packetnum	equ	50	;total number of packets
packetarg_size	equ	50	;pkt arg size
cstack_size	equ	12288	;control panel stack
lstack_size	equ	1024	;lowlevel task's stack
mstack_size	equ	1024	;mixer task's stack
shlength	equ	500	;shell max line length
shlines	equ	1500	;shell max lines
shlinrem	equ	100	;lines 2 delete when full
shellargsize	equ	40	;max arg memory
chnnamelength	equ	12	;max channel name length
ahi_minbytes	equ	64	;min chunk
ahi_recbuffer	equ	4096	;record buffer
sambufdiv	equ	3	;buf 2^x-ed resze a chunk
maxpathlen	equ	500	;max path+filename
maxdoserr	equ	150	;max dos err string len
mpbufclrpasses	equ	50	;hanyszor toroljuk
samplerguiupdat	equ	100000	;only for file progress
playerguiupdat	equ	100000	;player time slider

page_info	equ	0
page_shell	equ	1
page_mixer	equ	2
page_player	equ	3
page_sampler	equ	4
page_options	equ	5
pages	equ	6	;number of main pages

debug_error	equ	0
debug_info	equ	1
debug_debug	equ	2
debug_detail	equ	3
debug_max	equ	debug_detail

largs	equ	5	;launcher shell args

;mpaframedelay	equ	5000	;decode_frame timing

mp3dsp_trebmin	equ	300	;min/max frequencies
mp3dsp_trebmax	equ	5000
mp3dsp_bassmin	equ	50
mp3dsp_bassmax	equ	700
freqgadstep	equ	10

seekbytes	equ	200000	;player forward/backward
datelength	equ	30	;date and time string


	; my libraries/devices

	STRUCTURE	mylib,LIB_SIZE
	APTR	mylib_var
	LABEL	mylib_size

	; vss.audio base

	STRUCTURE	vssaud,mylib_size
	LABEL	vssaud_size

	; melodympeg.device base

	STRUCTURE	mmpdevbase,mylib_size
	LABEL	mmpbase_size

	; mpega.lib base

	STRUCTURE	mpalib,mylib_size
	LABEL	mpalib_size

	; vss.lib base

	STRUCTURE	vsslib,mylib_size
	APTR	vsslib_lowlevelport
	APTR	vsslib_controlport
	APTR	vsslib_mixtab
	LABEL	vsslib_size

	; channel names

	STRUCTURE	channelnames,0
	STRUCT	cname0,chnnamelength
	STRUCT	cname1,chnnamelength
	STRUCT	cname2,chnnamelength
	STRUCT	cname3,chnnamelength
	STRUCT	cname4,chnnamelength
	LABEL	cnames_size

	; channel modes

	STRUCTURE	chnmodes,0
	UWORD	cmode1
	UWORD	cmode2
	UWORD	cmode3
	UWORD	cmode4
	UWORD	cmode0
	LABEL	cmodes_size

	; configuration file

	STRUCTURE	conf,0
	ULONG	conf_id	;'VSSC'
	UWORD	conf_version
	UWORD	conf_chksum	;file checksum

	ULONG	conf_addr	;address
	ULONG	conf_mpbufsize
	ULONG	conf_mpchunk
	ULONG	conf_mpmaxchunk
	ULONG	conf_mptimecorr
	ULONG	conf_mpfakecnt
	ULONG	conf_ahibuftime
	ULONG	conf_melbuftime
	ULONG	conf_playbufsize
	ULONG	conf_sambufsize
	ULONG	conf_irbaud
	UWORD	conf_irlen
	UWORD	conf_quantum
	UWORD	conf_guiquantum
	UWORD	conf_pri
	UWORD	conf_spri
	UWORD	conf_sercon	;external addr...
	UWORD	conf_debuglevel
	ULONG	conf_flags
	UBYTE	conf_dacon
	UBYTE	conf_adcon
	UBYTE	conf_samdeclev
	UBYTE	conf_levdiv
	;UBYTE	conf_kludgefill
	ALIGNWORD
	STRUCT	conf_names,cnames_size
	STRUCT	conf_mixer,VMT_SIZE
	STRUCT	conf_modes,cmodes_size
	STRUCT	conf_irinit,100
	LABEL	conf_size

	; VSSPacket structure

	STRUCTURE	packet,MN_SIZE
	;APTR	pkt_sender
	ULONG	pkt_cmd
	ULONG	pkt_arg
	STRUCT	pkt_args,packetarg_size
	LONG	pkt_result
	ULONG	pkt_sleeptime
	FPTR	pkt_funcptr
	ULONG	pkt_used	;usage flag
	LABEL	pkt_size

	; PlayerControl

	STRUCTURE	PlayControl,0
; In D7:filehandle A3:playnode  Out D0:success	fill duration only (fast)
	FPTR	pc_Examine
; In A2:buffer A3:playnode	read ID3 etc... (slow)
	FPTR	pc_InfoText
; In A3:playnode  Out Z:error	open file
	FPTR	pc_Open
; In A3:playnode		close file
	FPTR	pc_Close
; In D2:buffer D3:data_req A3:playnode  Out:bytesdone	fill buffer
	FPTR	pc_FillBuffer
	FPTR	pc_Seek
	FPTR	pc_Time
	LABEL	pc_size

	; AHI player/mixer hook registers

	STRUCTURE	ahimixreg,0
	APTR	ahir_hook
	APTR	ahir_mixbuf
	APTR	ahir_drvctrl
	FPTR	ahir_entry
	LABEL	ahiregset_size

	; Shell argument buffer

	STRUCTURE	shellargbuf,0
	ULONG	sha_help
	STRUCT	sha_arg,shellargsize
	LABEL	sha_size


; --------------- global variables and lowlevel task (a5 indexed)

	STRUCTURE	GlobVar,TC_SIZE	;lowlevel task
	STRUCT	bufmantask,TC_SIZE	;mixer task
	STRUCT	lstack,lstack_size
	STRUCT	mstack,mstack_size
	STRUCT	pktsem,SS_SIZE	;packets
	STRUCT	pktlist,MLH_SIZE
	STRUCT	pktwaitlist,MLH_SIZE	;packets waiting
	STRUCT	handsem,SS_SIZE	;handles
	STRUCT	handlist,MLH_SIZE
	STRUCT	notifysem,SS_SIZE	;notify requests
	STRUCT	notifylist,MLH_SIZE
	STRUCT	melpktsem,SS_SIZE	;melody write requests
	STRUCT	melpktlist,MLH_SIZE
	STRUCT	stolensem,SS_SIZE	;suspended AHI processes
	STRUCT	stolenlist,MLH_SIZE
	STRUCT	poolsem,SS_SIZE
	STRUCT	packets,pkt_size*packetnum
	STRUCT	mixt,VMT_SIZE	;belso tabla
	STRUCT	mixtab,VMT_SIZE	;user ezt latja
	STRUCT	conftmp,conf_size
	STRUCT	cnames,cnames_size
	STRUCT	levhist1,1024
	STRUCT	levhist2,1024

	; main task

	APTR	excbase	;exec
	APTR	dosbase	;dos
	APTR	gfxbase	;graphics
	APTR	intbase	;intuition
	APTR	gadbase	;gadtools
	APTR	muibase	;muimaster
	APTR	utibase	;utility
	APTR	icobase	;icon
	APTR	expbase	;expansion
	APTR	cdtbase	;commodities
	APTR	reqbase	;reqtools
	APTR	vssbase	;vss
	APTR	mpabase	;mpega
	APTR	vahbase	;vss.audio
	APTR	mmpbase	;melodympeg.device
	APTR	timbase	;timer.device

	APTR	mainproc	;main process (control)
	APTR	windowptr	;pr_WindowPtr mentése
	BPTR	stdout	;debug console
	APTR	cport	;comm. port
	ULONG	cport_sig	;port -> sig
	ULONG	updatsig
	APTR	main_handle	;main task vssHandle
	APTR	main_port	;handle -> port
	APTR	main_notify
	ULONG	sigrcvd
	APTR	pool1

	APTR	mp_req	;patch remove requester
	ULONG	subtasksig

;	APTR	tp_port	;timer patch
;	APTR	tp_timereq
;	FPTR	tp_oldreadeclock

	UBYTE	flag
	UBYTE	debuglevel
	UBYTE	updatsignal
	ALIGNLONG

	; melody emulation

	APTR	mel_handle
	APTR	mel_saveport
	IFD	melodyport2
	APTR	mel_port2
	APTR	mel_port3
	ENDIF
	APTR	mel_curpkt	;packet under processing
	APTR	mel_pktptr
	ULONG	mel_pktdata	;data remaining
	UBYTE	mel_streamtype	;audio/mpeg
	UBYTE	mel_flags
	ALIGNLONG

	; mpega emulation

	FPTR	oldMPEGA_open	;save patched functions
	FPTR	oldMPEGA_close
	FPTR	oldMPEGA_decode_frame
	FPTR	oldMPEGA_seek
	FPTR	oldMPEGA_time
	FPTR	oldMPEGA_find_sync
	FPTR	oldMPEGA_scale
	APTR	oldlibID

	APTR	stream	;active stream
	ULONG	mpbufsize
	APTR	mpbuffer
	ULONG	mpbufeof
	ULONG	mpbufcorr
	ULONG	ptr1
	ULONG	ptr2
	ULONG	mpchunk
	ULONG	mpmaxchunk

	; lowlevel task & vss.library

	APTR	lport	;lowlevel task's port
	ULONG	lport_sig	; and it's signal
	APTR	ltport	;timer.device port
	ULONG	ltport_sig	;and it's signal
	ULONG	lsigrcvd
	APTR	timereq
	APTR	sleeper	;sleeper under processing
	ULONG	updatecount
	ULONG	updatequantum

	APTR	int6
	APTR	softint

	APTR	address	;board address
	APTR	acthook
	APTR	acthandle
	ULONG	quantum	;ms
	ULONG	quantum2	;us
	ULONG	lastfreq
	ULONG	mixerchanged
	APTR	mixerchgmsg
	UWORD	remain

	UBYTE	con
	UBYTE	con2
	UBYTE	freqdiv
	UBYTE	deemp
	UBYTE	ser
	UBYTE	bufsize	;[128bytes]
	UBYTE	intcnt
	UBYTE	loadcnt
	UBYTE	maxbuf	;8k/32k
	UBYTE	blocksperint	;[128bytes]
	UBYTE	cardid
	UBYTE	tasktransfer	;0 ha SoftInt
	UBYTE	ll_flags
	ALIGNLONG

	; bufman task

	ULONG	msig
	APTR	mport
	ULONG	mport_sig
	APTR	mtport
	ULONG	mtport_sig
	APTR	mtimereq
	ULONG	msigrcvd
	APTR	mcurpkt

	UBYTE	msigbit
	ALIGNLONG

	; ahi interface

	STRUCT	ahi_playerregs,ahiregset_size
	STRUCT	ahi_mixerregs,ahiregset_size
	STRUCT	ahi_pretimerregs,ahiregset_size
	STRUCT	ahi_samplerregs,ahiregset_size

	STRUCT	ahi_hook,h_SIZEOF
	STRUCT	ahi_recmsg,AHIRecordMessage_SIZEOF
	APTR	ahi_mixbuf	;mixing buffer
	ULONG	ahi_bytes	;data left in buffer
	APTR	ahi_bufstart
	ULONG	ahi_mixedbytes
	APTR	ahi_drvctrl
	APTR	ahi_handle
	ULONG	ahi_mult	;byte/sample
	ULONG	ahi_buffer	;max buffer [ms]
	FPTR	ahi_loopjump24
	UWORD	ahi_discnt	;disable nest count
	UBYTE	ahi_flags
	UBYTE	ahi_status24
	ALIGNLONG

	; player process

	APTR	ply_playproc
	APTR	ply_port
	ULONG	ply_portsig
	APTR	ply_cport
	APTR	ply_cportsig
	ULONG	ply_sig	;buffer fill signal
	ULONG	ply_sigrcvd
	APTR	ply_vhandle
	APTR	ply_vsslib

	APTR	ply_fib
	APTR	ply_rdargs
	APTR	ply_trymem

	STRUCT	ply_pc,pc_size


	APTR	ply_fname
	BPTR	ply_fhandle
	ULONG	ply_filesize
	ULONG	ply_duration
	ULONG	ply_frequency

	ULONG	ply_updatcnt

	APTR	ply_bufsize
	APTR	ply_buffer
	ULONG	ply_bufeof
	ULONG	ply_pos
	APTR	ply_ptr1
	APTR	ply_ptr2

	UWORD	ply_nodes
	UWORD	ply_nodeindex
	APTR	ply_actnode

	UWORD	ply_volremain
	UBYTE	ply_volume
	UBYTE	ply_volsave
	BYTE	ply_balance
	UBYTE	ply_signal
	UBYTE	ply_flags
	UBYTE	ply_flags2
	ALIGNLONG

	; sampler process

	STRUCT	sam_hook,h_SIZEOF
	APTR	sam_hist
	APTR	sam_hist2
	ULONG	sam_hists

	APTR	samplerproc
	APTR	sam_port
	ULONG	sam_portsig
	APTR	sam_sig
	ULONG	sam_sigrcvd
	APTR	sam_vhandle

	APTR	sam_filename
	BPTR	sam_file

	ULONG	sam_chunk
	ULONG	sam_maxchunk
	APTR	sam_buffer
	ULONG	sam_bufsize
	ULONG	sam_ptr1
	ULONG	sam_ptr2
	ULONG	sam_pos

	ULONG	sam_updatcnt

	UBYTE	sam_leftlev
	UBYTE	sam_rightlev
	UBYTE	sam_leftlev2
	UBYTE	sam_rightlev2
	UBYTE	sam_levmode
	UBYTE	sam_levdiv
	UBYTE	sam_mx1
	UBYTE	sam_mx2
	UBYTE	sam_mx3
	UBYTE	sam_mx4
	UBYTE	sam_signal
	UBYTE	sam_flags
	ALIGNLONG

	; command shell

	STRUCT	shellcs,CS_SIZEOF	;CSource
	STRUCT	shellhist,MLH_SIZE
	STRUCT	sharg,sha_size	;arguments
	STRUCT	formatbuf,shlength	;printxxx formatting buf.
	APTR	shellhistnext
	UWORD	shellhistlines
	UWORD	gui_shlines
	UBYTE	shellchanged
	ALIGNLONG

	; IRDA TV remote

	APTR	ir_port
	APTR	ir_request
	ULONG	ir_signal
	ULONG	ir_bufsize
	ULONG	ir_pktsize

	STRUCT	ir_buffer,256

	UBYTE	ir_flags
	ALIGNLONG

	; GUI engine

	APTR	gui_icon
	APTR	gui_tagspace
	APTR	gui_app
	APTR	gui_myslidermcc	;my slider subclass
	APTR	gui_mysliderclass
	APTR	gui_mybodymcc	;my group/bodychunk subclass
	APTR	gui_mybodyclass
	ULONG	gui_signal
	APTR	gui_window
	APTR	gui_register
	APTR	gad_optreg
	UBYTE	gui_actpage1
	BYTE	gui_actpage
	ALIGNLONG

	APTR	gad_aboutm	;info page

	APTR	gad_shellinput	;shell page
	APTR	gad_shellist

	APTR	gad_s2	;mixer page
	APTR	gad_s3	;a cimszamitas miatt
	APTR	gad_s4	;a sorrend nem valtozhat
	APTR	gad_s5
	APTR	gad_s6
	APTR	gad_s7
	APTR	gad_s8
	APTR	gad_s9
	APTR	gad_s0
	APTR	gad_s1
	APTR	gad_mm0	;mixer mode cycles
	APTR	gad_mm1
	APTR	gad_mm2
	APTR	gad_mm3
	APTR	gad_mm4

	APTR	gad_ms1	;mpeg page   bassf
	APTR	gad_ms2	;basse
	APTR	gad_ms3	;trebf
	APTR	gad_ms4	;trebe
	APTR	gad_ms5	;tonattn
	APTR	gad_ms6	;stereo
	STRUCT	gui_modes,cmodes_size

	APTR	gad_unload	;options page buttons
	APTR	gad_saveconf
	APTR	gad_hide
	APTR	gad_default

	APTR	gad_name0	;channel names &
	APTR	gad_name1	;debuglevel
	APTR	gad_name2
	APTR	gad_name3
	APTR	gad_name4
	APTR	gad_debugl
	APTR	gad_guiquantum

	APTR	gad_ahibuf	;ahi & mpeg options
	APTR	gad_melbuf
	APTR	gad_quantum
	APTR	gad_llpri
	APTR	gad_spri
	APTR	gad_endeemp
	APTR	gad_mpbufsize
	APTR	gad_mptimecorr

	APTR	gad_chname0	;channel name text
	APTR	gad_chname1	;egymas utan kell
	APTR	gad_chname2	;lenniuk a cimszamitas
	APTR	gad_chname3	;miatt!!!
	APTR	gad_chname4

	APTR	gad_ahiint
	APTR	gad_melint
	APTR	gad_mpaint
	APTR	gad_ahivol
	APTR	gad_melvol
	APTR	gad_mpavol
	APTR	gad_ahiena
	APTR	gad_melena
	APTR	gad_mpaena
	APTR	gad_delflush
	APTR	gad_ahimpsupp
	APTR	gad_mpassw
	APTR	gad_mpload
	APTR	gad_mploadram

	APTR	gad_optlist	;!!!

	APTR	gad_sbuf	;sampler
	APTR	gad_sfreq
	APTR	gad_histogram
	APTR	gad_levmode
	APTR	gad_samappend

	APTR	gad_sfname
	APTR	gad_samfilebut
	APTR	gad_sftype	;8/16bit etc..
	APTR	gad_ftyp2	;RAW/AIFF/WAV

	APTR	gad_sopen
	APTR	gad_sclose
	APTR	gad_sstart
	APTR	gad_sstop
	APTR	gad_leftlev
	APTR	gad_rightlev
	APTR	gad_leftlev2
	APTR	gad_rightlev2

	APTR	gad_samdeclev

	APTR	gad_smix1	;a cimszamitas miatt a
	APTR	gad_smix2	;sorrend nem valtozhat!
	APTR	gad_smix3
	APTR	gad_smix4

	APTR	gad_levelgroup
	APTR	gad_samstatus

	APTR	gad_random	;player
	APTR	gad_intro
	APTR	gad_loop
	APTR	gad_clearlist
	APTR	gad_prev
	APTR	gad_forw
	APTR	gad_backw
	APTR	gad_next
	APTR	gad_pause
	APTR	gad_play
	APTR	gad_stop
	APTR	gad_closefile
	APTR	gad_tagedit
	APTR	gad_savelist
	APTR	gad_cdda

	APTR	gad_playlist
	APTR	gad_playinfo
	APTR	gad_playfile	;hidden string object
	APTR	gad_savefile	;hidden string object
	APTR	gad_playtime
	APTR	gad_playstatus

	APTR	gad_playvol
	APTR	gad_volreset
	APTR	gad_balance
	APTR	gad_balreset
	APTR	gad_playbass
	APTR	gad_bassswitch
	APTR	gad_playtreble
	APTR	gad_trebswitch

	LABEL	var_size

; launcher-variables

	STRUCTURE	launchervars,0
	APTR	lv_excbase
	APTR	lv_dosbase
	APTR	lv_utibase
	APTR	lv_icobase
	APTR	lv_startup	;wb-msg
	APTR	lv_thisproc
	APTR	lv_nexthunk
	BPTR	lv_output	;standard output
	UBYTE	lv_flags
	ALIGNWORD
	LABEL	lv_size

lvf_open	equ	0	;launcher console open

flg_patch	equ	1	;mpega patched
flg_start	equ	2	;mp3 first buffer load
flg_under	equ	3	;mp3 buffer underrun
flg_eof	equ	5	;mp3 eof reached
flg_enable	equ	6	;libs accept new openings
flg_req	equ	4	;patch requester

ahif_busy	equ	0	;hardware allocated
ahif_hifi	equ	1
ahif_vssopen	equ	2
ahif_play	equ	3
ahif_record	equ	4
ahif_recbuf	equ	5

; Config flags
;---------------$00000008
cflg_nomemtest	equ	0	;skip cache test
cflg_deemphasis	equ	1	;enable deemphasis
cflg_pktpanic	equ	2	;panic if no free packets
cflg_nostrsw	equ	3	;disable stream switching
;---------------$00000020
cflg_mpegadis	equ	4	;disable mpega emulation
cflg_mploadlib	equ	5	;load mpega.lib
cflg_bytewr	equ	6	;disable word write
cflg_byterd	equ	7	;disable word read
;---------------$00000b00
cflg_ahiena	equ	8
cflg_ahiint	equ	9
cflg_ahivol	equ	10
cflg_melena	equ	11
;---------------$0000f000
cflg_melint	equ	12
cflg_melvol	equ	13
cflg_mpaena	equ	14
cflg_mpaint	equ	15
;---------------$00010000
cflg_mpavol	equ	16
cflg_forcempa	equ	17	;skip mpega usage check
cflg_mpaloadram	equ	18	;load full file
cflg_levelhist	equ	19	;use histogram
;---------------$00x00000
cflg_samappend	equ	20
cflg_playloop	equ	21
cflg_ignoreaddr	equ	22	;don't trust board addr
cflg_plynoparse	equ	23	;don't examine files
;---------------$0x000000
cflg_startir	equ	24	;auto start ir server


vssver	equ	4	;vss.library version
vssrev	equ	9
mpaver	equ	4	;mpega.library version
mparev	equ	9	;(emulation mode only)
ahiver	equ	4
ahirev	equ	9
mmpver	equ	4
mmprev	equ	9
confver	equ	$3439

version	MACRO
	dc.b	vssver+'0','.',vssrev+'0'
	ENDM

copyright	MACRO
	dc.b	'1999-2002'
	ENDM

; sajnos makroban nem muxik a %getdate/%gettime

;date	MACRO
;	dc.b	'09.06.2001'
;	ENDM

; BUILD = _build.cnt file hossza
; Max 4 digitet (ASCII) beszur a 'build' makro

buc	equ	filesize(VSS:Sources/_build.cnt)+1

build2	MACRO
buc2	set	buc3/\1
buc3	set	buc3-buc2*\1
	IFEQ	buc2
	IFNE	buc4
	dc.b	'0'
	ENDIF
	ELSE
	dc.b	buc2+'0'
buc4	set	1
	ENDIF
	ENDM

build	MACRO
buc3	set	buc
buc4	set	0
	build2	1000
	build2	100
	build2	10
	dc.b	buc3+'0'
	ENDM

; Forditas leallitasa (ha failonpass1 definialva van, akkor mar PASS1 alatt)

cfail	MACRO
	IFD	failonpass1
	FAIL
	ELSE
	IF2
	FAIL
	ENDIF
	ENDIF
	ENDM

; Szoveg byteok szamlalasa

txtsize	set	0
txtlab	set	-1

txtstart	MACRO
	IFNE	txtlab+1	;double txtstart?
	printt	'** double TXTSTART'
	cfail
	ENDIF
txtlab	set	*-Main
	ENDM

txtend	MACRO
	IFEQ	txtlab+1	;txtend without txtstart?
	printt	'** TXTEND without TXTSTART'
	cfail
	ENDIF

txtsize	set	txtsize+*-Main-txtlab
txtlab	set	-1
	ENDM

txtlast	MACRO
	IFNE	txtlab+1	;last block not ended?
	printt	'** last textblock not closed'
	cfail
	ENDIF
	ENDM

; '_' beszurasa teszt forditaskor

testm	MACRO
	IFD	testmode
	dc.b	'_'
	ENDIF
	ENDM

; Relativ cimkek

relbeg	MACRO
	IFC	\1,''
reloffset	set	*
	ELSE
reloffset	set	\1
	ENDIF
	ENDM

rdc.l	MACRO
	dc.l	\1-reloffset
	ENDM

rdc.w	MACRO
	dc.w	\1-reloffset
	ENDM


;------< detach seglist, launch main task in the background >--------------------

; A program szegmens split technikat hasznal a hatterben futashoz.
; Ertelmezi a parametereket (shell/WB), elinditja a masodik szegmenst egy
; kulon processben, majd kilepes elott megszakitja a szegmenslista linkeleset.



	section	starter,code



Start	movem.l	d1-a6,-(sp)
	move.l	sp,d1
	moveq	#lv_size/4,d0	;helyfoglalas a veremben
.2	clr.l	-(sp)
	dbf	d0,.2
	move.l	sp,a5
	move.l	d1,-(sp)
	move.l	4.w,a6	;AbsExecBase
	move.l	a6,lv_excbase(a5)
	sub.l	a1,a1
	jsr	_LVOFindTask(a6)
	move.l	d0,a3
	move.l	d0,lv_thisproc(a5)
	tst.l	pr_CLI(a3)	;WB or CLI?
	bne.b	.cli
	lea	pr_MsgPort(a3),a0	;get WB startup-msg
	jsr	_LVOWaitPort(a6)
	lea	pr_MsgPort(a3),a0
	jsr	_LVOGetMsg(a6)
	move.l	d0,lv_startup(a5)

.cli	lea	la.dosname(pc),a1	;open DOS
	clr.l	d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,lv_dosbase(a5)
	bne.b	.ok
	move.l	#AN_Unknown!AG_OpenLib!AO_DOSLib,d7
	jsr	_LVOAlert(a6)	;fail, alert
	bra.w	.nodos
.ok	move.l	d0,a6
	tst.l	lv_startup(a5)
	bne.b	.open
	jsr	_LVOOutput(a6)
	move.l	d0,lv_output(a5)
	bne.b	.std
.open	bset	#lvf_open,lv_flags(a5)
	lea	la.conname(pc),a0
	move.l	a0,d1
	move.l	#MODE_NEWFILE,d2
	jsr	_LVOOpen(a6)
	move.l	d0,lv_output(a5)
	beq.w	.nocon
.std	move.l	lv_excbase(a5),a0	;verzio ellenorzes
	cmp.w	#39,LIB_VERSION(a0)
	blt.b	.bver
	cmp.w	#39,LIB_VERSION(a6)
	bge.b	.gver
.bver	move.l	lv_output(a5),d1	;tul regi!
	lea	la.e_bver(pc),a0
	move.l	a0,d2
	moveq	#la.e_badcpu-la.e_bver,d3
	jsr	_LVOWrite(a6)
	bra.w	.nouti
.gver	move.l	lv_excbase(a5),a6	;open UTILITY
	lea	la.utiname(pc),a1
	moveq	#39,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,lv_utibase(a5)
	bne.b	.utiok
	move.l	#AN_Unknown!AG_OpenLib!AO_UtilityLib,d7
	jsr	_LVOAlert(a6)	;fail, alert
	bra.w	.nouti
.utiok	lea	la.e_badcpu(pc),a0	;CPU tipus ellenorzes
	move.w	AttnFlags(a6),d0
	btst	#AFB_68020,d0
	beq.w	.nemjo

	move.l	Start-4(pc),a4	;BCPL pointer to next seg
	add.l	a4,a4	;BPTR -> APTR conversion
	add.l	a4,a4
	addq.l	#4,a4
	move.l	a4,lv_nexthunk(a5)
	cmp.l	#'dos.',dosname-Main(a4)	;check next seg
	lea	la.e_seg(pc),a0
	bne.w	.nemjo
	tst.l	lv_startup(a5)
	bne.w	.wb

	move.l	lv_dosbase(a5),a6	;parse CLI arguments
	moveq	#DOS_RDARGS,d1
	clr.l	d2
	jsr	_LVOAllocDosObject(a6)
	move.l	d0,d3
	beq.w	.nk
	move.l	d3,a0
	clr.l	(a0)	;RDA_Source.CS_Buffer
	clr.l	RDA_DAList(a0)
	clr.l	RDA_Buffer(a0)
	clr.l	RDA_BufSiz(a0)
	lea	la.rdahelp(pc),a1
	move.l	a1,RDA_ExtHelp(a0)
	move.l	#RDAF_NOPROMPT,RDA_Flags(a0)
	moveq	#largs-1,d0	;args-1
.1	clr.l	-(sp)
	dbf	d0,.1
	lea	la.t_templ(pc),a0
	move.l	a0,d1
	move.l	sp,d2
	jsr	_LVOReadArgs(a6)
	move.l	d0,d2
	beq.b	.argh2
	tst.l	12(sp)	;ha ?, help
	sne	d4
	bne.b	.endarg
	tst.l	16(sp)	;dis
	sne	startdis-Main(a4)
	move.l	(sp),d0
	beq.b	.nopo
	move.l	d0,a0
	lea	la.t_popval(pc),a1
	move.l	lv_utibase(a5),a6
	jsr	_LVOStricmp(a6)
	move.l	lv_dosbase(a5),a6
	tst.l	d0
	seq	startpopup-Main(a4)
.nopo	tst.l	4(sp)
	sne	starthide-Main(a4)
	move.l	8(sp),d0
	beq.b	.endarg
	move.l	d0,a0
	move.l	(a0),d0
	cmp.l	#pages,d0
	bcc.b	.endarg
	move.b	d0,startpage-Main(a4)
.endarg	move.l	d2,d1
	jsr	_LVOFreeArgs(a6)
.noarg	lea	largs*4(sp),sp
	moveq	#DOS_RDARGS,d1
	move.l	d3,d2
	jsr	_LVOFreeDosObject(a6)
	tst.b	d4
	bne.b	.arghelp
	lea	progname-Main(a4),a0
	move.l	a0,d1
	moveq	#110,d2
	jsr	_LVOGetProgramName(a6)
	bra.w	.nk
.argh2	lea	largs*4(sp),sp
.arghelp	lea	la.rdahelp(pc),a0	;help kiirasa, kilepes
	bra.w	.nemjo

.wb	lea	la.iconame(pc),a1	;icon tooltypes
	moveq	#39,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,lv_icobase(a5)
	beq.w	.noicon
	move.l	lv_startup(a5),a2
	move.l	sm_ArgList(a2),a2
	move.l	(a2),d1	;wa_Lock
	move.l	lv_dosbase(a5),a6
	jsr	_LVOCurrentDir(a6)
	move.l	d0,-(sp)
	move.l	lv_icobase(a5),a6
	move.l	wa_Name(a2),a0
	jsr	_LVOGetDiskObject(a6)
	tst.l	d0
	beq.b	.nodo
	move.l	d0,-(sp)
	move.l	d0,a0
	move.l	do_ToolTypes(a0),a2
	move.l	a2,a0
	lea	la.t_popup(pc),a1
	jsr	_LVOFindToolType(a6)
	tst.l	d0
	beq.b	.nopop
	move.l	d0,a0
	lea	la.t_popval(pc),a1
	jsr	_LVOMatchToolValue(a6)
	tst.l	d0
	sne	startpopup-Main(a4)
.nopop	move.l	a2,a0
	lea	la.t_auto(pc),a1
	jsr	_LVOFindToolType(a6)
	tst.l	d0
	sne	startdis-Main(a4)
	move.l	a2,a0
	lea	la.t_hide(pc),a1
	jsr	_LVOFindToolType(a6)
	tst.l	d0
	sne	starthide-Main(a4)
	move.l	a2,a0
	lea	la.t_page(pc),a1
	jsr	_LVOFindToolType(a6)
	move.l	d0,d1
	beq.b	.nowbpage
	clr.l	-(sp)
	move.l	lv_dosbase(a5),a6
	move.l	sp,d2
	jsr	_LVOStrToLong(a6)
	move.l	(sp)+,d1
	tst.l	d0
	bmi.b	.nowbpage
	cmp.l	#pages,d1
	bcc.b	.nowbpage
	move.b	d1,startpage-Main(a4)
.nowbpage	move.l	lv_icobase(a5),a6
	move.l	(sp)+,a0
	jsr	_LVOFreeDiskObject(a6)
.nodo	move.l	(sp)+,d1
	move.l	lv_dosbase(a5),a6
	jsr	_LVOCurrentDir(a6)
	move.l	lv_excbase(a5),a6
	move.l	lv_icobase(a5),a1
	jsr	_LVOCloseLibrary(a6)
.noicon	move.l	lv_thisproc(a5),a0	;programnev masolasa
	move.l	LN_NAME(a0),a0
	lea	progname-Main(a4),a1
	moveq	#109,d0
.0	move.b	(a0)+,(a1)+
	dbeq	d0,.0

.nk	move.l	lv_excbase(a5),a6	;split seglist
	lea	la.procname(pc),a1
	jsr	_LVOForbid(a6)
	jsr	_LVOFindTask(a6)
	tst.l	d0
	beq.b	.notask
	move.l	d0,a1	;found, signal
	move.l	#$8000,d0
	jsr	_LVOSignal(a6)
	jsr	_LVOPermit(a6)
	lea	la.t_running(pc),a0
	bra.b	.kiir
.notask	move.l	lv_dosbase(a5),a6
	lea	la.proctags(pc),a0
	move.l	a0,d1
	lea	Start-4(pc),a2	;next segment
	move.l	(a2),ti_Data(a0)
	jsr	_LVOCreateNewProc(a6)
	move.l	lv_excbase(a5),a6
	jsr	_LVOPermit(a6)
	lea	la.e_error(pc),a0
	tst.l	d0
	beq.b	.nemjo
	clr.l	(a2)	;detach next segment
	lea	la.t_launch(pc),a0
.kiir	btst	#lvf_open,lv_flags(a5)
	bne.b	.nemir
.nemjo	move.l	lv_output(a5),d1
	move.l	a0,d2
	move.l	lv_dosbase(a5),a6
	jsr	_LVOFPuts(a6)
	move.l	lv_output(a5),d1
	jsr	_LVOFlush(a6)
.nemir	move.l	lv_excbase(a5),a6
	move.l	lv_utibase(a5),a1
	jsr	_LVOCloseLibrary(a6)
.nouti	btst	#lvf_open,lv_flags(a5)
	beq.b	.nocon
	move.l	lv_output(a5),d1
	move.l	lv_dosbase(a5),a6
	jsr	_LVOClose(a6)
.nocon	move.l	a6,a1
	move.l	lv_excbase(a5),a6
	jsr	_LVOCloseLibrary(a6)
.nodos	move.l	lv_startup(a5),d0
	beq.b	.nomsg
	move.l	d0,a1
	jsr	_LVOForbid(a6)	;multitasking will resume
	jsr	_LVOReplyMsg(a6)	;when we are dead
.nomsg	move.l	(sp)+,sp	;restore stack
	movem.l	(sp)+,d1-a6
	clr.l	d0
	rts

la.proctags	dc.l	NP_Seglist,0
	dc.l	NP_FreeSeglist,TRUE
	dc.l	NP_Name,la.procname
	dc.l	NP_Priority,0
	dc.l	NP_StackSize,cstack_size
	dc.l	NP_WindowPtr,-1
	dc.l	TAG_DONE

la.dosname	dc.b	'dos.library',0
la.iconame	dc.b	'icon.library',0
la.utiname	dc.b	'utility.library',0

la.procname	testm
	dc.b	'VSS control panel',0

la.conname	dc.b	'CON:50/50/430/100/VSS launcher failed/Auto/Wait/Close'
	dc.b	0

la.e_bver	dc.b	'This program requires AmigaOS 3.0 (V39) or better',10
la.e_badcpu	dc.b	'This program requires 68ec020 processor or better'
	dc.b	10,0
la.e_seg	dc.b	'SegList modified',10,0
la.e_error	dc.b	'Process creation failed (Memory over?)',10,0
la.t_launch	dc.b	'Launched in the background',10,0
la.t_running	dc.b	'VSS already running',10,0

la.t_templ	dc.b	'POPUP/K,ICON/S,PAGE/K/N,?/S,NODETECT/S',10,0
la.t_popup	dc.b	'POPUP',0
la.t_hide	dc.b	'ICON',0
la.t_page	dc.b	'PAGE',0
la.t_popval	dc.b	'NO',0
la.t_auto	dc.b	'NODETECT',0

la.rdahelp	dc.b	'VSS Driver V'
	version
	dc.b	'.'
	build
	dc.b	' ('
	;date
	%getdate	3
	dc.b	')  ©'
	copyright
	dc.b	' by Valenta Ferenc',10
	IFD	testmode
	dc.b	'*** INTERNAL TEST VERSION, DO NOT SPREAD ***',10
	ENDIF
	dc.b	'This program accepts the same CLI arguments '
	dc.b	'and tooltypes.',10,10
	dc.b	'POPUP=(YES|NO)  If NO, the GUI won''t open',10
	dc.b	'ICON            The GUI will open in iconified '
	dc.b	'state',10
	dc.b	'PAGE=(number)   Jump to page 0..5, 0=info, 1=shell, '
	dc.b	'etc...',10
	dc.b	'NODETECT        Disable automatic hardware search'
	dc.b	10,10,0

txtsize	set	*-la.dosname
	even


;------------< main process >----------------------------------------------------



	section	vssmain,code



Main	movem.l	d1-a6,-(sp)	;regiszterek mentése
	move.l	4.w,a6	;ExecBase
	move.l	#var_size,d0
	move.l	#MEMF_PUBLIC!MEMF_CLEAR,d1
	jsr	_LVOAllocMem(a6)
	tst.l	d0
	bne.b	.varok
	move.l	#AN_Unknown!AG_NoMemory,d7	;no memory, panic
	jsr	_LVOAlert(a6)
	bra.w	.exit3
.varok	move.l	d0,a5
	lea	var(pc),a0
	move.l	a5,(a0)
	move.l	a6,excbase(a5)

	sub.l	a4,a4
	sub.l	a1,a1
	jsr	_LVOFindTask(a6)
	move.l	d0,mainproc(a5)
	move.l	d0,a0
	move.l	pr_WindowPtr(a0),windowptr(a5)

	lea	dosname(pc),a1	;open DOS
	moveq	#39,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,dosbase(a5)
	beq.w	.exit1
	move.l	d0,a6	;open console
	lea	conname(pc),a0
	move.l	a0,d1
	move.l	#MODE_NEWFILE,d2
	jsr	_LVOOpen(a6)
	move.l	d0,stdout(a5)
	beq.w	.noout

;	lea	Main(pc),a0	;hunk cim kiirasa
;	move.l	a0,d0
;	lea	d_hunk(pc),a0
;	bsr	debugarg

	move.l	excbase(a5),a6
	lea	expname(pc),a1	;open EXPANSION
	move.l	a1,a4
	moveq	#39,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,expbase(a5)
	beq.w	.exit
	lea	gfxname(pc),a1	;open GRAPHICS
	move.l	a1,a4
	moveq	#39,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,gfxbase(a5)
	beq.w	.exit
	lea	intname(pc),a1	;open INTUITION
	move.l	a1,a4
	moveq	#39,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,intbase(a5)
	beq.w	.exit
	lea	gadname(pc),a1	;open GADTOOLS
	move.l	a1,a4
	moveq	#39,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,gadbase(a5)
	beq.w	.exit
	lea	utiname(pc),a1	;open UTILITY
	move.l	a1,a4
	moveq	#39,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,utibase(a5)
	beq.w	.exit
	lea	e_nomui(pc),a4	;open MUIMASTER
	lea	muiname(pc),a1
	moveq	#19,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,muibase(a5)
	beq.w	.exit
	lea	e_noreq(pc),a4	;open REQTOOLS
	lea	reqname(pc),a1
	moveq	#38,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,reqbase(a5)
	beq.w	.exit
	lea	iconame(pc),a1	;open ICON
	lea	e_noicon(pc),a4
	moveq	#39,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,icobase(a5)
	beq.w	.exit
	lea	e_nocdt(pc),a4	;open COMMODITIES
	lea	cdtname(pc),a1
	moveq	#39,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,cdtbase(a5)
	beq.w	.exit

	lea	e_sys(pc),a4	;allocate update signal
	moveq	#-1,d0
	jsr	_LVOAllocSignal(a6)
	move.b	d0,updatsignal(a5)
	bmi.w	.exit
	clr.l	d1
	bset	d0,d1
	move.l	d1,updatsig(a5)
	jsr	_LVOCreateMsgPort(a6)	;create cport
	move.l	d0,cport(a5)
	beq.w	.exit
	bsr.w	portsig
	move.l	d0,cport_sig(a5)

	moveq	#MEMF_PUBLIC,d0	;create pool1
	move.l	#65536,d1
	move.l	#32768,d2
	jsr	_LVOCreatePool(a6)
	move.l	d0,pool1(a5)
	beq.w	.exit

	lea	TC_MEMENTRY(a5),a0	;init lists
	bsr.w	newlist
	lea	bufmantask+TC_MEMENTRY(a5),a0
	bsr.w	newlist
	lea	pktlist(a5),a0
	bsr.w	newlist
	lea	pktwaitlist(a5),a0
	bsr.w	newlist
	lea	handlist(a5),a0
	bsr.w	newlist
	lea	notifylist(a5),a0
	bsr.w	newlist
	lea	melpktlist(a5),a0
	bsr.w	newlist
	lea	stolenlist(a5),a0
	bsr.w	newlist
	lea	shellhist(a5),a0
	bsr.w	newlist

	lea	handsem(a5),a0	;init semaphores
	jsr	_LVOInitSemaphore(a6)
	lea	notifysem(a5),a0
	jsr	_LVOInitSemaphore(a6)
	lea	pktsem(a5),a0
	jsr	_LVOInitSemaphore(a6)
	lea	melpktsem(a5),a0
	jsr	_LVOInitSemaphore(a6)
	lea	stolensem(a5),a0
	jsr	_LVOInitSemaphore(a6)
	lea	poolsem(a5),a0
	jsr	_LVOInitSemaphore(a6)

	moveq	#packetnum-1,d2	;packetek listaba fuzese
	lea	packets(a5),a3
.0	lea	pktlist(a5),a0
	move.l	a3,a1
	move.w	#pkt_size,MN_LENGTH(a1)
	;move.l	#-1,LN_NAME(a1)	;LN_NAME=-1
	jsr	_LVOAddTail(a6)
	lea	pkt_size(a3),a3
	dbf	d2,.0

	move.b	#1,debuglevel(a5)	;init gui
	bsr.l	initgui
	lea	e_gui(pc),a4
	beq.w	.nolib
	bsr.w	readconf	;read config
	bset	#flg_enable,flag(a5)	;enable libraries
	bsr.w	makelib	;init libraries
	bne.w	.nolib

	lea	e_sys(pc),a4
	move.l	vssbase(a5),a6	;alloc handle
	lea	t_vssdrv(pc),a0
	jsr	_LVOvssAllocHandle(a6)
	move.l	d0,main_handle(a5)
	beq.b	.exit
	move.l	d0,a0
	move.l	vssh_port(a0),main_port(a5)
	move.l	#$2000,d0	;signal
	moveq	#-1,d1	;all channels
	jsr	_LVOvssAddMixerNotify(a6)
	move.l	d0,main_notify(a5)
	beq.b	.exit

	bsr.w	initserv	;init lowlevel task
	beq.b	.exittask

	move.l	vssbase(a5),a0
	move.l	lport(a5),vsslib_lowlevelport(a0)
	move.l	cport(a5),vsslib_controlport(a0)
	lea	mixtab(a5),a1
	move.l	a1,vsslib_mixtab(a0)

	bsr.w	searchboard
	bsr.l	playinit
	bsr.l	saminit
	move.l	conftmp+conf_flags(a5),d0
	btst	#cflg_startir,d0
	beq	.00
	moveq	#VSSpkt_IrMode,d0
	st	d1
	move.l	lport(a5),a0
	sub.l	a1,a1
	bsr	sendPkt
.00	bsr.w	mainprog	;föprogram hivasa

	;move.l	dosbase(a5),a6
	;moveq	#50,d1
	;jsr	_LVODelay(a6)
	;sub.l	a4,a4

	move.l	vssbase(a5),a6
	move.l	main_handle(a5),a0
	jsr	_LVOvssDisownCard(a6)
.exittask	bsr.w	killserv
.exit	move.l	vssbase(a5),a6
	move.l	main_notify(a5),a0
	jsr	_LVOvssRemMixerNotify(a6)
	move.l	main_handle(a5),a0
	jsr	_LVOvssFreeHandle(a6)
.nolib	bsr.l	killgui
	bsr.w	rmlib
	move.l	excbase(a5),a6
	move.l	pool1(a5),d0
	beq.b	.nopool
	move.l	d0,a0
	jsr	_LVODeletePool(a6)
.nopool	move.l	cport(a5),a0
	jsr	_LVODeleteMsgPort(a6)
	move.b	updatsignal(a5),d0
	jsr	_LVOFreeSignal(a6)

	move.l	cdtbase(a5),a1
	jsr	_LVOCloseLibrary(a6)
	move.l	icobase(a5),a1
	jsr	_LVOCloseLibrary(a6)
	move.l	muibase(a5),a1
	jsr	_LVOCloseLibrary(a6)
	move.l	reqbase(a5),a1
	jsr	_LVOCloseLibrary(a6)
	move.l	utibase(a5),a1
	jsr	_LVOCloseLibrary(a6)
	move.l	gadbase(a5),a1
	jsr	_LVOCloseLibrary(a6)
	move.l	intbase(a5),a1
	jsr	_LVOCloseLibrary(a6)
	move.l	gfxbase(a5),a1
	jsr	_LVOCloseLibrary(a6)
	move.l	expbase(a5),a1
	jsr	_LVOCloseLibrary(a6)

	move.l	dosbase(a5),a6
	move.l	stdout(a5),d1
	beq.b	.noout
	move.l	a4,d2
	beq.b	.noerr
	jsr	_LVOFPuts(a6)
	move.l	stdout(a5),d1
	lea	d_exit(pc),a0
	move.l	a0,d2
	jsr	_LVOFPuts(a6)
	move.l	stdout(a5),d1
	jsr	_LVOFlush(a6)
.noerr	move.l	stdout(a5),d1
	jsr	_LVOClose(a6)
.noout	move.l	a6,a1
	move.l	excbase(a5),a6
	jsr	_LVOCloseLibrary(a6)
.exit1	move.l	a5,a1
	move.l	#var_size,d0
	jsr	_LVOFreeMem(a6)
.exit3	movem.l	(sp)+,d1-a6
	clr.l	d0
	rts

; exec double linked lists
;
; «« List with 2 nodes »»
;
;list_head	dc.l	first_node
;list_tail	dc.l	0
;	dc.l	last_node
;
;first_node	dc.l	last_node
;	dc.l	list_head
;
;last_node	dc.l	list_tail
;	dc.l	first_node
;
; «« Empty list »»
;
;list_head	dc.l	list_tail
;list_tail	dc.l	0
;	dc.l	list_head

; In A0:list

newlist	move.l	a0,LH_TAILPRED(a0)	;lista elökészítése
	addq.l	#4,a0
	clr.l	(a0)
	move.l	a0,-(a0)
	rts

; In A0:list A1:node

abcEnqueue	movem.l	d2-d4/a2/a6,-(sp)	;beszúrás listába
	move.l	a0,d2	;ABC sorrendben
	move.l	a1,d3	;a0 list, a1 node
	move.l	LN_NAME(a1),d4	;kisbetu/nagybetu mindegy
	move.l	a0,a2
.1	move.l	(a2),a2
	tst.l	(a2)
	beq.b	.0
	move.l	LN_NAME(a2),a0
	move.l	d4,a1
	move.l	utibase(a5),a6
	jsr	_LVOStricmp(a6)
	tst.l	d0
	bmi.b	.1
.0	move.l	LN_PRED(a2),a2
	cmp.l	a2,d2
	bne.b	.2
	sub.l	a2,a2
.2	move.l	d2,a0
	move.l	d3,a1
	move.l	excbase(a5),a6
	jsr	_LVOInsert(a6)
	movem.l	(sp)+,d2-d4/a2/a6
	rts

; Memoria foglalasa a pool managerrel, blokk torlese
; Blokk meretere emlekszik
; In D0:size A0:pool  Out D0:memblock

AllocVecPooled	tst.l	d0
	beq.b	.fail
	move.l	a0,d1
	lea	poolsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	move.l	d1,a0
	addq.l	#4,d0
	move.l	d0,-(sp)
	jsr	_LVOAllocPooled(a6)
	lea	poolsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	move.l	(sp)+,d1
	tst.l	d0
	beq.b	.fail
	move.l	d0,a0
	move.l	d1,(a0)+
	move.l	a0,d0
	subq.l	#5,d1
.0	clr.b	(a0)+
	dbf	d1,.0
.fail	rts

; In A0:pool A1:memblock

FreeVecPooled	move.l	a0,d1
	lea	poolsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	move.l	d1,a0
	move.l	-(a1),d0
	jsr	_LVOFreePooled(a6)
	lea	poolsem(a5),a0
	jmp	_LVOReleaseSemaphore(a6)

; Debug functions, Output=VSS Debug Console

ddebug	move.l	a0,-(sp)	;'DEBUG'
	lea	d_debug(pc),a0
	bsr.b	debug
	move.l	(sp)+,a0
	rts

debug	movem.l	d0-d2/a1/a6,-(sp)	;a0 *text kiirasa
	move.l	dosbase(a5),a6
	move.l	stdout(a5),d1
	beq.b	.0
	move.l	a0,d2
	beq.b	.0
	jsr	_LVOFPuts(a6)
.0	movem.l	(sp)+,d0-d2/a1/a6
	rts

debugarg	movem.l	d0-a6,-(sp)	;A0:format D0:data
	move.l	dosbase(a5),a6
	move.l	stdout(a5),d1
	beq.b	.1
	move.l	a0,d2
	move.l	d0,-(sp)
	move.l	sp,d3
	jsr	_LVOVFPrintf(a6)
	move.l	stdout(a5),d1
	moveq	#10,d2
	jsr	_LVOFPutC(a6)
	addq.l	#4,sp
.1	movem.l	(sp)+,d0-a6
.0	rts

debugmarg	movem.l	d0-a6,-(sp)	;A0:format A1:*data
	move.l	dosbase(a5),a6
	move.l	stdout(a5),d1
	beq.b	.0
	move.l	a0,d2
	move.l	a1,d3
	jsr	_LVOVFPrintf(a6)
	moveq	#10,d2
	jsr	_LVOFPutC(a6)
.0	movem.l	(sp)+,d0-a6
	rts

; Debug functions, Output=VSS Shell

pdebug	movem.l	d0-d1/a1,-(sp)	;A0:format D2:*data
	moveq	#VSSpkt_PrintDebug,d0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	movem.l	(sp)+,d0-d1/a1
	rts

; Fatal error

panic	move.l	var(pc),a5
	move.l	(sp),d1

	move.l	excbase(a5),a0	;task
	move.l	ThisTask(a0),a0	;addr
	move.l	LN_NAME(a0),-(sp)	;& name
	move.l	a0,-(sp)	;

	lea	Main(pc),a0	;offset
	sub.l	a0,d1	;&
	or.l	#AN_Unknown!$f0000,d0	;code
	move.l	d1,-(sp)	;
	move.l	d0,-(sp)	;

	move.l	sp,a1
	lea	e_panic(pc),a0
	bsr.b	debugmarg
	lea	16(sp),sp	;restore stack

	move.l	dosbase(a5),a6
	move.l	stdout(a5),d1
	jsr	_LVOFlush(a6)

	;move.l	d0,d7
	;move.l	var(pc),a5
	;move.l	excbase(a5),a6
	;jsr	_LVOAlert(a6)	;alert
	;lea	e_alert(pc),a0
	;moveq	#100,d1
	;move.l	var(pc),a5
	;move.l	intbase(a5),a6
	;jsr	_LVODisplayAlert(a6)	;alert
	;move.l	excbase(a5),a6

	move.l	excbase(a5),a6
	sub.l	a1,a1
	jsr	_LVOFindTask(a6)
	move.l	d0,a1
	move.b	#-127,d0
	jsr	_LVOSetTaskPri(a6)
.0	clr.l	d0
	jsr	_LVOWait(a6)	;vegtelen varakozas
	bra.b	.0

;e_alert	dc.w	20
;	dc.b	20
;	dc.b	'System halted',0,0

; used library names

	txtstart

	even		;dosname paros cimen
dosname	dc.b	'dos.library',0	;kezdodik!
vssname	testm
	dc.b	'vss.library',0
iconame	dc.b	'icon.library',0
mpaname	testm
	dc.b	'mpega.library',0
layname	dc.b	'layers.library',0
tocname	dc.b	'toccata.library',0
utiname	dc.b	'utility.library',0
gadname	dc.b	'gadtools.library',0
reqname	dc.b	'reqtools.library',0
gfxname	dc.b	'graphics.library',0
expname	dc.b	'expansion.library',0
intname	dc.b	'intuition.library',0
muiname	dc.b	'muimaster.library',0
cdtname	dc.b	'commodities.library',0
timname	dc.b	'timer.device',0
sername	dc.b	'serial.device',0
mmpname	testm
	dc.b	'melodympeg.device',0
mauname	testm
	dc.b	'melodyaudio.device',0
vahname	testm
	dc.b	'vss.audio',0

wbname	dc.b	'Workbench',0
conname	dc.b	'CON:50/50/430/100/VSS debug console/Auto/Wait/Close',0

; library ID strings

vssid	dc.b	'VSS advanced features',0
ahiid	dc.b	'VSS ahi interface',0
mmpid	dc.b	'VSS melody emulation',0
mpaid	dc.b	'VSS mpega emulation',0

; task/process names

vsslowlevelname	testm
	dc.b	'VSS lowlevel',0	;task
vssmixername	testm
	dc.b	'VSS buffer manager',0	;task
smpname	testm
	dc.b	'VSS sampler slave',0	;process
plyname	testm
	dc.b	'VSS player slave',0	;process

; handle names

t_vssdrv	dc.b	'system',0
t_ahidrv	dc.b	'ahi driver',0
t_melody	dc.b	'melody emulation',0
t_mpega	dc.b	'mpega emulation',0
playername	dc.b	'player',0
samplername	dc.b	'sampler',0

; board names

t_v10	dc.b	'V1.0',0
t_v11a	dc.b	'V1.1a/24.000MHz',0
t_v11b	dc.b	'V1.1b/21.4772MHz',0
t_v11c	dc.b	'V1.1c/25.000MHz',0
t_v11d	dc.b	'V1.1d/14.31818MHz',0
t_v11e	dc.b	'V1.1e/16.000MHz',0
t_v11f	dc.b	'V1.1f/12.000MHz',0
t_v12	dc.b	'V1.2',0
t_v13	dc.b	'V1.3',0
t_v14	dc.b	'V1.4',0
t_v15	dc.b	'V1.5',0
t_v16	dc.b	'V1.6',0
t_v17	dc.b	'V1.7',0

; Emulated Melody description

t_melodydesc	dc.b	'VSS MelodyMPEG emulation',0

; options file name

f_conf	dc.b	'ENVARC:'
	testm
	dc.b	'vss.config',0

; misc texts (debug console/patch requester only)

e_nocdt	dc.b	'Commodities.library V39 required',10,0
e_noicon	dc.b	'Icon.library V39 required',10,0
e_noreq	dc.b	'Reqtools.library V38 required',10,0
e_sys	dc.b	'Unknown error. (Memory over?)',10,0
e_nomui	dc.b	'This program requires the MUI system',10
	dc.b	'(Free downloadable from Aminet)',10,0
e_task	dc.b	'Subtask initialization failed',10,0
e_gui	dc.b	'Error while creating GUI',10,0

e_patch	dc.b	'Cannot quit yet, somebody patched too.',10
	dc.b	'I will wait until I can safely remove',10
	dc.b	'the patches, but I won''t disturb you',10
	dc.b	'with more requesters.',0

e_panic	dc.b	'««« FATAL ERROR, TASK HELD »»»',10
	dc.b	'Code: $%08.8lx  Offset: $%08.8lx',10
	dc.b	'Task: %08.8lx (%s)',10,10
	dc.b	'Wait for disk activity to finish.',10,0

d_debug	dc.b	'DEBUG',10,0


d_exit	dc.b	10,'EXIT',10,0





t_mpreqgads	dc.b	'Ok',0
t_mpreqtitle	dc.b	'VSS Request',0

;t_eclock	dc.b	'E-clock modified (%ldHz -> %ldHz)',0



; misc texts (VSS Command Shell)

i_cardfound	dc.b	'RTC port: %s, $%08.8lx',0
t_xsurf	dc.b	'X-Surf',0
t_buddha	dc.b	'Buddha',0
t_12ap30	dc.b	'A1200/Apollo1230',0
t_12bppc	dc.b	'A1200/BlizzardPPC',0

i_conf	dc.b	'Configuration loaded',0
i_confw	dc.b	'Configuration saved',0
e_conf	dc.b	'*** Config file damaged/not exist',0
e_confw	dc.b	'*** Can''t create config file',0
e_obsconf	dc.b	'*** Unknown config version, ignored',0

e_iicc	dc.b	'*** i2c bus fault (CODEC)',0
e_iicd	dc.b	'*** i2c bus fault (DSP)',0
e_dspans	dc.b	'*** DSP configuration error',0
e_inter	dc.b	'*** Interrupt test failed',0
e_mem	dc.b	'*** Memory test failed (Addr=$%04.4lx)',0
e_board	dc.b	'*** Board not detected at $%08.8lx',0
e_obsh	dc.b	'*** This driver requires V1.4 or better',10
	dc.b	'*** Please upgrade board!',0
e_obss	dc.b	'*** Unrecognized board (ID $%02.2lx)',10
	dc.b	'*** Please upgrade driver!',0
e_busy	dc.b	'*** The board is currently busy',0
e_illfreq	dc.b	'*** Unsupported frequency request (%ldHz)',0
e_vss	dc.b	'*** vss.library initialization failed',10,0
e_mpa	dc.b	'*** mpega.library initialization failed',10,0
e_mpuse	dc.b	'*** mpega.library still in use, emu disabled',0
i_mpuse	dc.b	'*** WARNING: mpega.library in use yet',10
	dc.b	'*** Proceed at your own risk!',0
e_vah	dc.b	'*** vss.audio initialization failed',10,0
e_mmp	dc.b	'*** melodympeg.device initialization failed',10,0
e_cannot	dc.b	'*** Cannot quit yet, the libraries are in use',10
	dc.b	'*** vss.lib: %ld, mpega.lib: %ld, vss.audio: %ld',0

e_melbadcmd	dc.b	'*** Unexpected melody command received',0
e_mpaacc	dc.b	'*** Unrecognized stream access hook command',0

i_init	dc.b	'Found %ls (ID $%02.2lx) at $%08.8lx',0
i_selftest	dc.b	'Self test running...',0
i_cache	dc.b	'Cache: %ldk, %s read, %s write',0
i_cacheb	dc.b	'byte',0
i_cachew	dc.b	'word',0
i_caches	dc.b	'Memory test skipped',0
i_pass	dc.b	'Board initialized and READY.',0
i_mpegap	dc.b	'Mpega.library opened and patched',0
i_mpegae	dc.b	'Mpega.library emulation started',0
i_irstart	dc.b	'IRR Server starting',0
i_irstop	dc.b	'IRR Server exiting',0
e_irerr	dc.b	'*** IR-remote initialization failed (Device busy?)',0
d_irrestart	dc.b	'IRR Initializing',0

i_ahis	dc.b	'AHI driver installed',0
i_meles	dc.b	'MelodyMPEG emulation started',0

i_handle	dc.b	'*** Handle used by ''%s''',0

e_setaddr	dc.b	$1b,'3';'b';,$1b,'u'
	dc.b	'*** Please set board address!',0


; --> Compilation options
; *** Errors
; LIB vss.library
; LOW VSS lowlevel
; SYS VSS control panel (main task)
; AHI AHI
; MPG mpega emulation

	IFD	mpusemagic
i_mpmagic	dc.b	'--> Mpega stream checking enabled',0
	ENDIF

d_mixed	dc.b	'AHI Mixing buffer $%lx',0
d_chgmix	dc.b	'LOW MixChn #%ld',0
d_freq	dc.b	'LOW SetFreq %ldHz',0
d_disown	dc.b	'LOW DisownCard %08.8lx',0
d_own	dc.b	'LOW OwnCard %08.8lx',0
d_patch	dc.b	'SYS Trying to remove patches...',0
d_allochand	dc.b	'LIB AllocHandle(''%ls'')',0
d_freehand	dc.b	'LIB FreeHandle(''%ls'')',0
d_allocaudio	dc.b	'LIB AllocAudio',0
d_freeaudio	dc.b	'LIB FreeAudio(''%ls'')',0
d_initstream	dc.b	'LIB Init(''%s'')',0
d_start	dc.b	'LIB Start',0
d_stop	dc.b	'LIB Stop',0

d_ahialloc	dc.b	'AHI AllocAudio',0
d_ahifree	dc.b	'AHI FreeAudio',0
d_ahistart	dc.b	'AHI Start',0
d_ahistop	dc.b	'AHI Stop',0
d_lowmaxbuf	dc.b	'LOW Buffer size %ld',0

d_eoferr	dc.b	'MPG EOF reached or ERROR (pos %ld)',0
d_mpgplayeof	dc.b	'MPG Buffer empty',0

d_playeof	dc.b	'PLY EOF reached or ERROR (pos %ld)',0
d_playplay	dc.b	'PLY Play',0
d_playstop	dc.b	'PLY Stop',0
d_playsel	dc.b	'PLY Entry #%ld activated',0
d_playstart	dc.b	'PLY Slave starting',0
d_playex	dc.b	'PLY Slave exiting',0

d_underrun	dc.b	'MPG Buffer underrun',0
d_playunder	dc.b	'*** Buffer underrun',0

d_melopen	dc.b	'MEL DevOpen Unit %ld, Flags $%08.8lx',0
d_melclose	dc.b	'MEL DevClose',0
d_meldevinfo	dc.b	'MEL GetDevInfo',0
d_melplay	dc.b	'MEL Play',0
d_melflush	dc.b	'MEL Flush',0
d_melreset	dc.b	'MEL Reset',0
d_melpause	dc.b	'MEL Pause',0
d_melsetpar	dc.b	'MEL SetAudioParams',0
d_melwrite	dc.b	'MEL %ld bytes done',0

d_mpaopen	dc.b	'MPG MPEGA_open(''%ls'')',0
d_mpaclose	dc.b	'MPG MPEGA_close()',0
d_mpaseek	dc.b	'MPG MPEGA_seek(%lds)',0
d_mpafindsync	dc.b	'MPG MPEGA_find_sync = $%lx',0
d_mpascale	dc.b	'MPG MPEGA_scale(%ld%%)',0
d_mpahook	dc.b	'MPG Internal stream access hook used',0
d_duration	dc.b	'MPG Duration %d:%02.2d, Size %ld bytes',0
d_zeros	dc.b	'MPG %ld zeros sent',0
d_bytesdone	dc.b	'MPG %ld bytes done',0

d_samstart	dc.b	'SAM Slave starting',0
d_samex	dc.b	'SAM Slave exiting',0

t_ahiaut	dc.b	'Ferenc Valenta <vf@elte.hu>',0
t_ahicop	dc.b	'(c)'
	copyright
	dc.b	' by VF',0
t_ahiver	dc.b	'vss.audio '
	version
	dc.b	' ('
	;date
	%getdate	3
	dc.b	')',0
t_ahiann	dc.b	'Visit me at http://ludens.elte.hu/~vf/',0

	txtend

progname	dcb.b	110,0
startpopup	dc.b	0
starthide	dc.b	0
startpage	dc.b	0
startdis	dc.b	0

	even


;-----------< kartya keresese >--------------------------------------------------

; Ha a configban volt elmentve cim, es cflg_ignoreaddr nincs bekapcsolva,
; kiprobalja.
; Ha nem volt, vagy nem jo, es nincs tiltva a detektalas, vegignezi az
; oraporttal rendelkezo AutoConfig eszkozoket.
; Az A1200 oraportot az A1200 kartyakrol ismeri fel: ha ilyet talal,
; feltetelezi hogy az oraport cime $00d80001 ($00d84001, $00d88001, $00d8c001)

offsets	equ	5

	STRUCTURE	cardtab,0
	ULONG	ct_vendor
	ULONG	ct_product
	APTR	ct_name
	STRUCT	ct_offsets,offsets*4	;negative = abs addr
	LABEL	cardtab_size

searchboard	move.l	conftmp+conf_flags(a5),d0
	btst	#cflg_ignoreaddr,d0
	bne.b	.noboard
	move.l	conftmp+conf_addr(a5),d0
	beq.b	.noboard
	move.l	vssbase(a5),a6
	move.l	main_handle(a5),a0	;init board
	jsr	_LVOvssOwnCard(a6)
	tst.l	d0
	bne.b	.megvan

.noboard	tst.b	startdis(pc)
	bne.b	.nincs
	lea	.cardtab(pc),a2
.0	move.l	(a2),d2
	beq.b	.nincs
	move.l	ct_product(a2),d3
	sub.l	a3,a3
.1	move.l	expbase(a5),a6
	move.l	a3,a0	;oldConfigDev
	move.l	d2,d0	;manufacturer
	move.l	d3,d1	;product
	jsr	_LVOFindConfigDev(a6)
	tst.l	d0
	bne.b	.2
	lea	cardtab_size(a2),a2
	bra.b	.0
.2	move.l	d0,a3
	lea	i_cardfound(pc),a0
	move.l	cd_BoardAddr(a3),-(sp)
	move.l	ct_name(a2),-(sp)
	;move.l	LN_NAME(a3),-(sp)	;null pointer :(
	move.l	sp,a1
	bsr.w	printInfo
	addq.l	#8,sp
	move.l	vssbase(a5),a6	;try all offsets...
	moveq	#offsets-1,d4
	lea	ct_offsets(a2),a4
.3	move.l	(a4)+,d0
	beq.b	.1
	bpl.b	.4
	neg.l	d0
	bra.b	.5
.4	add.l	cd_BoardAddr(a3),d0
.5	move.l	main_handle(a5),a0
	jsr	_LVOvssOwnCard(a6)
	tst.l	d0
	bne.b	.megvan
	dbf	d4,.3
	bra.b	.1
.nincs	moveq	#VSSpkt_PrintFault,d0
	lea	e_setaddr(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
.megvan	rts

; offset >0	relative to autoconfig addr
;        <0	absolute addr

.cardtab	;X-Surf LAN card
	dc.l	$1212,$0017,t_xsurf,0,0,0,0,0
	;Buddha FDD card
	dc.l	$1212,$0000,t_buddha,0,0,0,0,0

	;A1200 clock port (Apollo 1230)
  dc.l  $2222,$0023,t_12ap30,-$d80001,-$d84001,-$d88001,-$d8c001,0
	;Blizzard PPC
  dc.l  $2140,$006e,t_12bppc,-$d80001,-$d84001,-$d88001,-$d8c001,0

	dc.l	0


;----------< vss/mpega library letrehozasa/eltavolitasa >------------------------

;eclock	equ	825005	;33MHz

makelib	move.l	excbase(a5),a6	;E-clock correction
;	lea	e_sys(pc),a4
;	jsr	_LVOCreateMsgPort(a6)
;	move.l	d0,tp_port(a5)
;	beq	.err
;	move.l	d0,a0
;	moveq	#IOTV_SIZE,d0
;	jsr	_LVOCreateIORequest(a6)
;	move.l	d0,tp_timereq(a5)
;	beq.w	.err
;	move.l	d0,a1
;	lea	timname(pc),a0
;	moveq	#UNIT_MICROHZ,d0
;	clr.l	d1
;	jsr	_LVOOpenDevice(a6)
;	tst.b	d0
;	bne	.err
;	move.l	tp_timereq(a5),a0
;	move.l	IO_DEVICE(a0),a6
;	move.l	a6,timbase(a5)
;	subq.l	#8,sp
;	move.l	sp,a0
;	jsr	_LVOReadEClock(a6)
;	move.l	d0,(sp)
;	move.l	#eclock,4(sp)
;	move.l	sp,a1
;	lea	t_eclock(pc),a0
;	bsr	printLine
;	addq.l	#8,sp
;	move.l	a6,a1	;patch
;	move.l	excbase(a5),a6
;	lea	.readeclock(pc),a0
;	move.l	a0,d0
;	move.l	#_LVOReadEClock,a0
;	jsr	_LVODisable(a6)
;	jsr	_LVOSetFunction(a6)
;	move.l	d0,tp_oldreadeclock(a5)
;	jsr	_LVOEnable(a6)

	move.l	#vsslib_size,d0	;setup vss.library
	move.l	#vssbase,d3
	lea	vssvectors(pc),a0
	lea	vssinitstr(pc),a1
	bsr.w	.mk
	lea	e_vss(pc),a4
	beq.w	.err

	move.l	conftmp+conf_flags(a5),d0	;ahi enabled?
	btst	#cflg_ahiena,d0
	beq.b	.noahi
	move.l	#vssaud_size,d0	;setup vss.audio
	move.l	#vahbase,d3
	lea	ahivectors(pc),a0
	lea	ahiinitstr(pc),a1
	bsr.w	.mk
	lea	e_vah(pc),a4
	beq.w	.err
	lea	i_ahis(pc),a0	;ahi started
	bsr.w	printInfo

.noahi	move.l	conftmp+conf_flags(a5),d0	;melody enabled?
	btst	#cflg_melena,d0
	beq.b	.nomel
	move.l	#mmpbase_size,d0	;setup melodympeg.device
	move.l	#mmpbase,d3
	lea	mmpvectors(pc),a0
	lea	mmpinitstr(pc),a1
	bsr.w	.mkdev
	lea	e_mmp(pc),a4
	beq.w	.err
	lea	i_meles(pc),a0	;melody emulation started
	bsr.w	printInfo

.nomel	move.l	excbase(a5),a6
	move.l	conftmp+conf_flags(a5),d0	;mpega enabled?
	btst	#cflg_mpaena,d0
	beq.w	.ok2
	btst	#cflg_mploadlib,d0
	bne.b	.load	;patch mode

	jsr	_LVOForbid(a6)
	lea	LibList(a6),a0
	lea	mpaname(pc),a1
	jsr	_LVOFindName(a6)
	move.l	d0,d2
	beq.b	.l0
	move.l	d0,a1	;try to flush
	jsr	_LVORemLibrary(a6)
	lea	LibList(a6),a0	;succeed?
	lea	mpaname(pc),a1
	jsr	_LVOFindName(a6)
	move.l	d0,d2
.l0	jsr	_LVOPermit(a6)
	tst.l	d2
	beq.w	.mpa	;emulation mode!

.load	lea	mpaname(pc),a1	;setup mpega.library
	clr.l	d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,mpabase(a5)
	beq.w	.mpa

	IFND	mpusemagic	;ha nincs magic, es a
	move.l	d0,a0	;lib mar nyitva, baj van!
	move.w	LIB_OPENCNT(a0),d0
	subq.w	#1,d0
	beq.b	.l1
	move.l	conftmp+conf_flags(a5),d1
	btst	#cflg_forcempa,d1	;force?
	beq.b	.l2
	lea	i_mpuse(pc),a0	;warning
	bsr.w	printFault
	bra.b	.l1
.l2	lea	e_mpuse(pc),a0
	bsr.w	printFault
	move.l	mpabase(a5),a1
	jsr	_LVOCloseLibrary(a6)
	clr.l	mpabase(a5)
	bra.w	.ok2
	ENDIF

.l1	bset	#flg_patch,flag(a5)	;lib loaded, patch mode
	move.l	mpabase(a5),a1
	move.l	LIB_IDSTRING(a1),oldlibID(a5)
	lea	mpaid(pc),a0
	move.l	a0,LIB_IDSTRING(a1)
	move.w	#_LVOMPEGA_open,a0
	lea	mpaMPEGA_open(pc),a1
	bsr.w	.lp
	move.l	d0,oldMPEGA_open(a5)
	move.w	#_LVOMPEGA_close,a0
	lea	mpaMPEGA_close(pc),a1
	bsr.w	.lp
	move.l	d0,oldMPEGA_close(a5)
	move.w	#_LVOMPEGA_decode_frame,a0
	lea	mpaMPEGA_decode_frame(pc),a1
	bsr.w	.lp
	move.l	d0,oldMPEGA_decode_frame(a5)
	move.w	#_LVOMPEGA_seek,a0
	lea	mpaMPEGA_seek(pc),a1
	bsr.w	.lp
	move.l	d0,oldMPEGA_seek(a5)
	move.w	#_LVOMPEGA_time,a0
	lea	mpaMPEGA_time(pc),a1
	bsr.w	.lp
	move.l	d0,oldMPEGA_time(a5)
	move.w	#_LVOMPEGA_find_sync,a0
	lea	mpaMPEGA_find_sync(pc),a1
	bsr.w	.lp
	move.l	d0,oldMPEGA_find_sync(a5)
	move.w	#_LVOMPEGA_scale,a0
	lea	mpaMPEGA_scale(pc),a1
	bsr.w	.lp
	move.l	d0,oldMPEGA_scale(a5)
	lea	i_mpegap(pc),a0	;mpega patched
	bsr.w	printInfo
	bra.b	.ok
.mpa	bclr	#flg_patch,flag(a5)	;make mpega.library
	moveq	#mpalib_size,d0	;(emulation mode)
	move.l	#mpabase,d3
	lea	mpavectors(pc),a0
	lea	mpainitstr(pc),a1
	bsr.b	.mk
	lea	e_mpa(pc),a4
	beq.b	.err
	lea	i_mpegae(pc),a0	;mpega emulation started
	bsr.w	printInfo
.ok	IFD	mpusemagic
	lea	i_mpmagic(pc),a0
	bsr.w	printInfo
	ENDIF
.ok2	clr.l	d0	;OK
	rts
.err	moveq	#-1,d0	;hiba jelzese
	rts

; In D0:base_size D3:store_base_a5-rel A0:vectortable A1:initstruct

.mk	clr.l	d1
	sub.l	a2,a2
	jsr	_LVOMakeLibrary(a6)
	move.l	d0,(a5,d3.w)
	beq.b	.1
	move.l	d0,a0
	move.l	a5,mylib_var(a0)
	move.l	LN_NAME(a0),a1
	lea	LibList(a6),a0
	jsr	_LVOForbid(a6)
	jsr	_LVOFindName(a6)
	move.l	d0,d2
	bne.b	.0
	move.l	(a5,d3.w),a1
	jsr	_LVOAddLibrary(a6)
.0	jsr	_LVOPermit(a6)
	tst.l	d2
	beq.b	.1
	move.l	(a5,d3.w),a1	;mar van ilyen nevu,
	clr.l	d0	;torles
	move.w	LIB_NEGSIZE(a1),d0
	move.w	LIB_POSSIZE(a1),d1
	sub.l	d0,a1
	add.w	d1,d0
	jsr	_LVOFreeMem(a6)
	clr.l	(a5,d3.w)
.1	move.l	(a5,d3.w),d0
	rts

; In D0:base_size D3:store_base_a5-rel A0:vectortable A1:initstruct

.mkdev	clr.l	d1
	sub.l	a2,a2
	jsr	_LVOMakeLibrary(a6)
	move.l	d0,(a5,d3.w)
	beq.b	.1d
	move.l	d0,a0
	move.l	a5,mylib_var(a0)
	move.l	LN_NAME(a0),a1
	lea	DevList(a6),a0
	jsr	_LVOForbid(a6)
	jsr	_LVOFindName(a6)
	move.l	d0,d2
	bne.b	.0d
	move.l	(a5,d3.w),a1
	jsr	_LVOAddDevice(a6)
.0d	jsr	_LVOPermit(a6)
	tst.l	d2
	beq.b	.1d
	move.l	(a5,d3.w),a1	;mar van ilyen nevu,
	clr.l	d0	;torles
	move.w	LIB_NEGSIZE(a1),d0
	move.w	LIB_POSSIZE(a1),d1
	sub.l	d0,a1
	add.w	d1,d0
	jsr	_LVOFreeMem(a6)
	clr.l	(a5,d3.w)
.1d	move.l	(a5,d3.w),d0
	rts

; In A0:offset A1:funcptr

.lp	move.l	a1,d0
	move.l	mpabase(a5),a1
	jmp	_LVOSetFunction(a6)

;; timer.device/ReadEClock
;
;.readeclock	movem.l	a4-a5,-(sp)
;	move.l	var(pc),a5
;	move.l	tp_oldreadeclock(a5),a4
;	jsr	(a4)
;	move.l	#825005,d0	;33MHz
;	movem.l	(sp)+,a4-a5
;	rts

; libraryk felszabaditasa

rmlib	move.l	excbase(a5),a6
	btst	#flg_patch,flag(a5)
	beq.w	.mpa
	move.l	mpabase(a5),a0
	move.l	oldlibID(a5),LIB_IDSTRING(a0)
	move.w	#_LVOMPEGA_open,a0	;patch mode
	move.l	oldMPEGA_open(a5),d0
	lea	mpaMPEGA_open(pc),a2
	bsr.w	.lup
	move.w	#_LVOMPEGA_close,a0
	move.l	oldMPEGA_close(a5),d0
	lea	mpaMPEGA_close(pc),a2
	bsr.w	.lup
	move.w	#_LVOMPEGA_decode_frame,a0
	move.l	oldMPEGA_decode_frame(a5),d0
	lea	mpaMPEGA_decode_frame(pc),a2
	bsr.w	.lup
	move.w	#_LVOMPEGA_seek,a0
	move.l	oldMPEGA_seek(a5),d0
	lea	mpaMPEGA_seek(pc),a2
	bsr.w	.lup
	move.w	#_LVOMPEGA_time,a0
	move.l	oldMPEGA_time(a5),d0
	lea	mpaMPEGA_time(pc),a2
	bsr.w	.lup
	move.w	#_LVOMPEGA_find_sync,a0
	move.l	oldMPEGA_find_sync(a5),d0
	lea	mpaMPEGA_find_sync(pc),a2
	bsr.w	.lup
	move.w	#_LVOMPEGA_scale,a0
	move.l	oldMPEGA_scale(a5),d0
	lea	mpaMPEGA_scale(pc),a2
	bsr.b	.lup
	move.l	mpabase(a5),a1
	jsr	_LVOCloseLibrary(a6)
	tst.l	mp_req(a5)	;kill request
	beq.b	.mpa1
	move.l	reqbase(a5),a6
	clr.l	d0
	lea	.endtag(pc),a0
	move.l	mp_req(a5),a1
	jsr	_LVOrtReqHandlerA(a6)
	move.l	excbase(a5),a6
	bra.b	.mpa1
.mpa	jsr	_LVOForbid(a6)
	lea	mpabase(a5),a2	;mpega.library
	bsr.b	.rm
	bra.b	.mpa2
.mpa1	jsr	_LVOForbid(a6)
.mpa2	lea	vssbase(a5),a2	;vss.library
	bsr.b	.rm
	lea	vahbase(a5),a2	;vss.audio
	bsr.b	.rm
	lea	mmpbase(a5),a2	;melodympeg.device
	bsr.b	.rm
	jsr	_LVOPermit(a6)
;	move.l	timbase(a5),a1
;	move.l	tp_oldreadeclock(a5),d0
;	move.l	#_LVOReadEClock,a0
;	jsr	_LVOSetFunction(a6)
;	move.l	tp_timereq(a5),a1	;E-clock correction
;	jsr	_LVOCloseDevice(a6)
;	move.l	tp_timereq(a5),a0
;	jsr	_LVODeleteIORequest(a6)
;	move.l	tp_port(a5),a0
;	jsr	_LVODeleteMsgPort(a6)
	move.l	dosbase(a5),a6	;megvarjuk mig minden
	moveq	#20,d1	;taszk befejezi a
	jmp	_LVODelay(a6)	;probalgatast :)

.rm	move.l	(a2),d2
	beq.b	.ok
	move.l	d2,a1
	jsr	_LVORemove(a6)
	move.l	d2,a1
	clr.l	d0
	move.w	LIB_NEGSIZE(a1),d0
	move.w	LIB_POSSIZE(a1),d1
	sub.l	d0,a1
	add.w	d1,d0
	jsr	_LVOFreeMem(a6)
	clr.l	(a2)
.ok	rts


; Patchek torlese
; In D0:oldfunc A0:offset A2:newfunc

; Ha nem lehet az eredeti vectort visszaallitani (mert pl kozben mas is
; patchelte ugyanazt a fuggvenyt), feldob egy async reqtools requestert,
; es a hatterben idonkent ujra megprobalja.
; PatchControl vagy egyeb hasonlo program hasznalata eseten ez nem
; fordulhat elo.

.lup	move.l	excbase(a5),a6
	move.l	mpabase(a5),a1
	movem.l	d0/a0,-(sp)
	pea	(a0)
	jsr	_LVOForbid(a6)
	jsr	_LVOSetFunction(a6)
	move.l	(sp)+,a0
	cmp.l	a2,d0
	seq	d2
	beq.b	.lu
	move.l	mpabase(a5),a1	;nem sikerult :(
	jsr	_LVOSetFunction(a6)
.lu	jsr	_LVOPermit(a6)
	movem.l	(sp)+,d0/a0
	tst.b	d2
	beq.b	.meg
	rts
.meg	movem.l	d0/a0/a2/a4,-(sp)	;nem sikerult
	tst.l	mp_req(a5)	;requester aktiv?
	bne.b	.noreq
	bset	#flg_req,flag(a5)	;mar volt?
	bne.b	.nosig
	lea	.tags(pc),a0	;requester keszitese
	lea	mp_req(a5),a1
	move.l	a1,.tags_hand-.tags(a0)	;hand **
	lea	e_patch(pc),a1	;body
	lea	t_mpreqgads(pc),a2	;gadgets
	sub.l	a3,a3	;reqinfo
	sub.l	a4,a4	;args
	move.l	reqbase(a5),a6
	jsr	_LVOrtEZRequestA(a6)
	cmp.l	#CALL_HANDLER,d0
	bne.b	.baj
	clr.l	d0
	bra.b	.call
.baj	clr.l	mp_req(a5)	;gaz van, reqtools failed
	bra.b	.nosig
.noreq	clr.l	d0	;requester aktiv,
	clr.l	d1	;kezeles
	move.l	excbase(a5),a6	;read signals
	jsr	_LVOSetSignal(a6)
	move.l	mp_req(a5),a2
	move.l	d0,d2
	move.l	d0,d1
	and.l	rthi_WaitMask(a2),d1
	beq.b	.ncall
	clr.l	d0	;clear signals
	jsr	_LVOSetSignal(a6)
	move.l	d2,d0
	bra.b	.call
.ncall	tst.l	rthi_DoNotWait(a2)
	beq.b	.nosig
.call	move.l	mp_req(a5),a1	;handler hivasa
	lea	.tagend(pc),a0
	move.l	reqbase(a5),a6
	jsr	_LVOrtReqHandlerA(a6)
	cmp.l	#CALL_HANDLER,d0
	beq.b	.noreq
	clr.l	mp_req(a5)
.nosig	;lea	d_patch(pc),a0	;debug
	;bsr.w	printLine
	move.l	dosbase(a5),a6	;varunk egy kicsit,
	moveq	#10,d1	;majd ujra probalkozunk
	jsr	_LVODelay(a6)	;(Forbid() kozben.. hmm)
	movem.l	(sp)+,d0/a0/a2/a4
	bra.w	.lup

.tags	dc.l	RT_ReqHandler	;async request tags
.tags_hand	dc.l	0
	dc.l	RTEZ_ReqTitle,t_mpreqtitle
	dc.l	TAG_DONE

.endtag	dc.l	RTRH_EndRequest,0	;requester torlese
.tagend	dc.l	TAG_DONE	;no more tags

; general library system functions

libgen_Open	move.l	a5,-(sp)	;eloszor letiltjuk,
	move.l	mylib_var(a6),a5
	addq.w	#1,LIB_OPENCNT(a6)	;utana ellenorizzuk
	move.l	a6,d0	;hogy szabad-e, igy majd
	btst	#flg_enable,flag(a5)	;biztonsagos a kilepes!
	bne.b	.0
	subq.w	#1,LIB_OPENCNT(a6)
.no	clr.l	d0
.0	move.l	(sp)+,a5
	rts

libgen_Close	subq.w	#1,LIB_OPENCNT(a6)
	bpl.b	.0
	moveq	#VSSERR_Sys,d0
	bsr.w	panic
	illegal
.0
libgen_Expunge
libgen_ExtFunc	clr.l	d0
	rts

; vss.library

vssinitstr	INITBYTE	LN_TYPE,NT_LIBRARY
	INITLONG	LN_NAME,vssname
	INITBYTE	LIB_FLAGS,LIBF_CHANGED!LIBF_SUMUSED
	INITWORD	LIB_VERSION,vssver
	INITWORD	LIB_REVISION,vssrev
	INITLONG	LIB_IDSTRING,vssid
	dc.l	0

vssvectors	relbeg
	dc.w	-1
	rdc.w	libgen_Open
	rdc.w	libgen_Close
	rdc.w	libgen_Expunge
	rdc.w	libgen_ExtFunc
	rdc.w	vssInterface
	rdc.w	vssAllocHandle
	rdc.w	vssFreeHandle
	rdc.w	vssAllocAudio
	rdc.w	vssFreeAudio
	rdc.w	vssStealAudio
	rdc.w	vssAddMixerNotify
	rdc.w	vssRemMixerNotify
	rdc.w	vssRefreshMixer
	rdc.w	vssFindFreq
	rdc.w	vssInit
	rdc.w	vssStart
	rdc.w	vssStop
	rdc.w	vssFlush
	rdc.w	vssBuffered
	rdc.w	vssOwnCard
	rdc.w	vssDisownCard
	dc.w	-1

; mpega.library (emulation mode only)

mpainitstr	INITBYTE	LN_TYPE,NT_LIBRARY
	INITLONG	LN_NAME,mpaname
	INITBYTE	LIB_FLAGS,LIBF_CHANGED!LIBF_SUMUSED
	INITWORD	LIB_VERSION,mpaver
	INITWORD	LIB_REVISION,mparev
	INITLONG	LIB_IDSTRING,mpaid
	dc.l	0

mpavectors	relbeg
	dc.w	-1
	rdc.w	libmpa_Open
	rdc.w	libgen_Close
	rdc.w	libgen_Expunge
	rdc.w	libgen_ExtFunc
	rdc.w	mpaMPEGA_open
	rdc.w	mpaMPEGA_close
	rdc.w	mpaMPEGA_decode_frame
	rdc.w	mpaMPEGA_seek
	rdc.w	mpaMPEGA_time
	rdc.w	mpaMPEGA_find_sync
	rdc.w	mpaMPEGA_scale
	dc.w	-1

; vss.audio

ahiinitstr	INITBYTE	LN_TYPE,NT_LIBRARY
	INITLONG	LN_NAME,vahname
	INITBYTE	LIB_FLAGS,LIBF_CHANGED!LIBF_SUMUSED
	INITWORD	LIB_VERSION,ahiver
	INITWORD	LIB_REVISION,ahirev
	INITLONG	LIB_IDSTRING,ahiid
	dc.l	0

ahivectors	relbeg
	dc.w	-1
	rdc.w	libgen_Open
	rdc.w	libgen_Close
	rdc.w	libgen_Expunge
	rdc.w	libgen_ExtFunc
	rdc.w	ahisub_AllocAudio
	rdc.w	ahisub_FreeAudio
	rdc.w	ahisub_Disable
	rdc.w	ahisub_Enable
	rdc.w	ahisub_Start
	rdc.w	ahisub_Update
	rdc.w	ahisub_Stop
	rdc.w	ahisub_SetVol
	rdc.w	ahisub_SetFreq
	rdc.w	ahisub_SetSound
	rdc.w	ahisub_SetEffect
	rdc.w	ahisub_LoadSound
	rdc.w	ahisub_UnloadSound
	rdc.w	ahisub_GetAttr
	rdc.w	ahisub_HardwareControl
	dc.w	-1

; melodympeg.device

mmpinitstr	INITBYTE	LN_TYPE,NT_DEVICE
	INITLONG	LN_NAME,mmpname
	INITBYTE	LIB_FLAGS,LIBF_CHANGED!LIBF_SUMUSED
	INITWORD	LIB_VERSION,mmpver
	INITWORD	LIB_REVISION,mmprev
	INITLONG	LIB_IDSTRING,mmpid
	dc.l	0

mmpvectors	relbeg
	dc.w	-1
	rdc.w	mmpOpen
	rdc.w	mmpClose
	rdc.w	libgen_Expunge
	rdc.w	libgen_ExtFunc
	rdc.w	mmpBeginIO
	rdc.w	mmpAbortIO
	rdc.w	mmpSetScr	;FALSE
	rdc.w	mmpGetScr	;RTS
	rdc.w	mmpPriv1	;RTS
	rdc.w	mmpPriv2	;FALSE
	dc.w	-1


;-----------< low level hardware services >--------------------------------------

; Subtaskok elinditasa, segedport a playernek

micro	equ	50000
micro_inttest	equ	200000	;200ms
micro_DSPreset	equ	1000	;1ms
memtestdata	equ	$00f80000	;Kickstart

initserv	move.l	excbase(a5),a6
	moveq	#-1,d0
	jsr	_LVOAllocSignal(a6)
	move.l	d0,-(sp)
	tst.b	d0
	bmi.w	.notask
	clr.l	d1
	bset	d0,d1
	move.l	d1,subtasksig(a5)

	lea	bufmantask(a5),a3	;bufman
	lea	mstack(a5),a0
	move.l	a0,TC_SPLOWER(a3)
	lea	mstack_size(a0),a0
	move.l	a0,TC_SPUPPER(a3)
	lea	-16(a0),a0
	move.l	a0,TC_SPREG(a3)
	move.b	#NT_TASK,LN_TYPE(a3)
	move.w	conftmp+conf_spri(a5),d0
	move.b	d0,LN_PRI(a3)
	lea	vssmixername(pc),a0
	move.l	a0,LN_NAME(a3)
	move.l	a3,a1
	lea	bufmanentry(pc),a2
	sub.l	a3,a3
	jsr	_LVOAddTask(a6)
	tst.l	d0
	beq.b	.notask
	move.l	subtasksig(a5),d0	;megvarjuk
	jsr	_LVOWait(a6)
	tst.l	mport(a5)	;dead or alive?
	beq.b	.notask

	lea	lstack(a5),a0	;lowlevel task
	move.l	a0,TC_SPLOWER(a5)
	lea	lstack_size(a0),a0
	move.l	a0,TC_SPUPPER(a5)
	lea	-16(a0),a0
	move.l	a0,TC_SPREG(a5)
	move.b	#NT_TASK,LN_TYPE(a5)
	move.w	conftmp+conf_pri(a5),d0
	move.b	d0,LN_PRI(a5)
	lea	vsslowlevelname(pc),a0
	move.l	a0,LN_NAME(a5)
	move.l	a5,a1
	lea	servtask(pc),a2
	sub.l	a3,a3
	jsr	_LVOAddTask(a6)
	tst.l	d0
	beq.b	.notask
	move.l	subtasksig(a5),d0	;megvarjuk
	jsr	_LVOWait(a6)
	tst.l	lport(a5)	;dead or alive?
	beq.b	.notask

	jsr	_LVOCreateMsgPort(a6)
	move.l	d0,ply_cport(a5)
	beq.b	.notask
	bsr.w	portsig
	move.l	d0,ply_cportsig(a5)

	move.l	a5,d2	;OK
	bra.b	.1
.notask	clr.l	d2
.1	move.l	(sp)+,d0
	jsr	_LVOFreeSignal(a6)
	lea	e_task(pc),a4
	move.l	d2,d0
	rts

killserv	move.l	excbase(a5),a6
	move.l	ply_cport(a5),a0
	jsr	_LVODeleteMsgPort(a6)
	clr.l	ply_cport(a5)
	move.l	mport(a5),d0	;mixer
	bsr.b	.0
	move.l	lport(a5),d0	;lowlevel
.0	beq.b	.noport
	move.l	d0,a0	;port
	move.l	main_port(a5),a1	;replyport
	moveq	#VSSpkt_Kill,d0	;pkt_cmd
	clr.l	d1	;pkt_arg
	bra.w	doPkt	;do!
.noport	rts


lfl_hardunder	equ	0	;hard buffer underrun
lfl_32k	equ	1	;cache size 32k
lfl_hardok	equ	2	;hardware O.K.
lfl_busy	equ	3	;in use
lfl_poll	equ	4	;poll enabled
lfl_inter	equ	5
lfl_monitor	equ	6
lfl_deemp	equ	7

	ENUM	0
	EITEM	VSSpkt_Kill
	; control panel commands
	EITEM	VSSpkt_PrintFault
	EITEM	VSSpkt_PrintInfo
	EITEM	VSSpkt_PrintDebug
	EITEM	VSSpkt_CallHook
	; lowlevel task commands
	EITEM	VSSpkt_OwnCard
	EITEM	VSSpkt_DisownCard
	EITEM	VSSpkt_InitCard
	EITEM	VSSpkt_SetFreq
	EITEM	VSSpkt_RefreshMixer
	EITEM	VSSpkt_ReadCodecRegs
	EITEM	VSSpkt_IICRead
	EITEM	VSSpkt_IICWrite
	EITEM	VSSpkt_Delay
	EITEM	VSSpkt_IrMode
	EITEM	VSSpkt_LastSupported
	; other packet commands
	EITEM	VSSpkt_StartSlave
	EITEM	VSSpkt_StopSlave
	EITEM	VSSpkt_Play
	EITEM	VSSpkt_Sample
	EITEM	VSSpkt_Stop
	EITEM	VSSpkt_Pause
	EITEM	VSSpkt_Seek
	EITEM	VSSpkt_PlayEOF


; VSS register offsets

_cahi	equ	4
_intack	equ	4
_id	equ	8
_con	equ	8
_status	equ	12
_ser	equ	12

; Packet commands and parameters
;
; pkt_cmd	pkt_arg	pkt_result
;
; « Special packet commands »
;
; Kill	-	- (control -> subtasks)
;		kill subtask
; PrintFault	form,data	- (ALL -> control)
; PrintInfo	form,data	- (ALL -> control)
; PrintDebug	form,data	- (ALL -> control)
;		display text, notify user
;
; « General packet commands » (ALL -> lowlevel)
;
; OwnCard	address	-
;		a driver birtokba veszi a kartyat
; DisownCard	-	TRUE
;		a driver leallitja a kartyat
; InitCard	-	FALSE ha foglalt vagy nincs
;		kartya inicializalasa, pl OwnCard utan
; SetFreq	freq	FALSE ha gond van
;		frekvencia bealitasa, codec ujrainditasa
;		ha MSB set, feltetlen frissites (mp3 mod)
; RefreshMixer	-	-
;		csatornak frissitese ha szukseges
; ReadCodecRegs	addr	FALSE ha rendben van
;		codec regiszterek beolvasasa a cimre
; IICRead	iicaddr	data, hiba eseten MSB set
; IICWrite	iicaddr	FALSE ha rendben van
; Delay	micro	-
;		idozites
; IrMode	start	-
;		IR szerver inditas/leallitas
;
; « BufMan packet commands »
;
; StartSlave		start/stop buffer manager
; StopSlave


servtask	move.l	var(pc),a5
	move.l	excbase(a5),a6
	clr.l	d7	;no packet (finalizernek)
	jsr	_LVOCreateMsgPort(a6)
	move.l	d0,lport(a5)
	beq.w	ll.err
	bsr.w	portsig
	move.l	d0,lport_sig(a5)
	jsr	_LVOCreateMsgPort(a6)
	move.l	d0,ltport(a5)
	beq.w	ll.err
	bsr.w	portsig
	move.l	d0,ltport_sig(a5)
	moveq	#IOTV_SIZE,d0
	jsr	_LVOCreateIORequest(a6)
	move.l	d0,timereq(a5)
	beq.w	ll.err
	move.l	d0,a1
	lea	timname(pc),a0
	moveq	#UNIT_VBLANK,d0	;unit
	clr.l	d1	;flags
	jsr	_LVOOpenDevice(a6)
	tst.b	d0
	bne.w	ll.err
	move.l	timereq(a5),a0
	move.l	IO_DEVICE(a0),timbase(a5)
	bsr.w	ll.timerstart
	bsr.w	intprep


	; setup actions


	move.l	mainproc(a5),a1	;we are alive!
	move.l	subtasksig(a5),d0
	jsr	_LVOSignal(a6)

ll.loop	move.l	lport_sig(a5),d0	;wait for new messages
	or.l	ltport_sig(a5),d0
	or.l	ir_signal(a5),d0
	;move.w	#$0009,$dff180
	jsr	_LVOWait(a6)
	;move.w	#$0f00,$dff180
	move.l	d0,lsigrcvd(a5)
	and.l	ir_signal(a5),d0
	beq.b	.noir
	bsr	irProcess
	bsr	irInit
.noir	move.l	lsigrcvd(a5),d0
	and.l	ltport_sig(a5),d0
	beq.w	ll.port
	bsr.w	ll.timerclear	;clear and restart timer
	bsr.w	ll.timerstart
	lea	pktwaitlist(a5),a1	;check sleeper packets
	bra.b	ll.megsleep
ll.unsleep	move.l	a3,a1	;reply sleeping packet
	bsr.w	replyPkt
ll.sleepers	move.l	sleeper(a5),a1	;continue checking
	bra.b	ll.0
ll.megsleep	move.l	(a1),a1
ll.0	move.l	(a1),sleeper(a5)	;continue here
	beq.b	ll.timer
	sub.l	#micro,pkt_sleeptime(a1)
	bcc.b	ll.megsleep
	move.l	a1,a3	;time expired, continue
	jsr	_LVORemove(a6)
	move.l	pkt_funcptr(a3),a1
	jmp	(a1)

; normal packet handling:
;   A3: packet under processing, *DO NOT CHANGE POINTER*
;	...		;packet actions
;	bra	ll.done	;reply
;
; sleep handling:
;	...		;packet actions
;	move.l	#micro,d0	;first sleep
;	lea	continue(pc),a0
;	bsr	ll.sleep
;	bra	ll.port	;don't reply now!
;
;continue	...		;wake up
;	move.l	#micro,d0	;and sleep again
;	lea	continue2(pc),a0
;	bsr	ll.sleep
;	bra	ll.sleepers	;process other sleepers
;
;continue2	...		;finish processing
;	bra	ll.unsleep	;reply

ll.sleep	move.l	d0,pkt_sleeptime(a3)	;put packet sleeping
	move.l	a0,pkt_funcptr(a3)
	lea	pktwaitlist(a5),a0
	move.l	a3,a1
	jmp	_LVOAddTail(a6)

ll.timer	sub.l	#micro,updatecount(a5)	;update timer
	bcc.b	.0
	move.l	updatequantum(a5),updatecount(a5)

; Kivezerles adatok olvasasa a codec-bol, ha szukseges

	cmp.b	#page_sampler,gui_actpage(a5)	;sampler lapon vagyunk?
	bne.b	.22
	btst	#lfl_monitor,ll_flags(a5)	;codec monitor aktiv?
	beq.b	.2
	tst.b	sam_levmode(a5)	;monitor bekapcsolva?
	beq.b	.2
	move.l	#$200600ff,d0	;olvasas a codecbol
	bsr.w	i2cWrite
	move.b	#$20,d0
	bsr.w	i2cRead
	move.l	d0,d4
	bmi.b	.2
	move.l	#$200500ff,d0	;reset levels
	bsr.w	i2cWrite
	move.b	#$20,d0
	bsr.w	i2cRead
	move.l	d4,d5	;konvertalas
	and.b	#7,d5
	lsr.b	#3,d4
	and.b	#7,d4
	move.b	d4,d2	;*9
	move.b	d5,d3
	asl.b	#3,d4
	asl.b	#3,d5
	add.b	d2,d4
	add.b	d3,d5
	move.b	d4,sam_leftlev2(a5)
	move.b	d5,sam_rightlev2(a5)
;	move.b	sam_leftlev2(a5),d2	;tarolas
;	move.b	sam_rightlev2(a5),d3
;	cmp.b	d2,d4
;	bcc	.3
;	move.b	d2,d4
;.3	cmp.b	d3,d5
;	bcc	.4
;	move.b	d3,d5
;.4	move.b	d4,sam_leftlev(a5)
;	move.b	d5,sam_rightlev(a5)
	bra	.2

; Ha a player vagy sampler lapon vagyunk, signalozunk, kulonben nem!

.22	cmp.b	#page_player,gui_actpage(a5)	;player lapon vagyunk?
	bne.b	.0
.2	move.l	mainproc(a5),a1	;signal main task
	move.l	updatsig(a5),d0
	jsr	_LVOSignal(a6)
.0	tst.l	mixerchanged(a5)
	beq.b	.1
	bsr.w	ll.notify
.1

	; timer actions


	move.l	lsigrcvd(a5),d0
	and.l	lport_sig(a5),d0
	beq.w	ll.loop
ll.port	move.l	lport(a5),a0	;process packets
	jsr	_LVOGetMsg(a6)
	tst.l	d0
	beq.w	ll.loop
	move.l	d0,a3
	cmp.l	mixerchgmsg(a5),d0
	beq.b	ll.notifylock
	move.l	pkt_cmd(a3),d0
	cmp.l	#VSSpkt_LastSupported,d0	;command recognized?
	bcc.b	ll.bad
	move.w	(.jumps,pc,d0.w*2),d0
	jmp	(.jumps,pc,d0.w)

.jumps	relbeg
	rdc.w	ll.kill
	rdc.w	ll.bad	;PrintFault
	rdc.w	ll.bad	;PrintInfo
	rdc.w	ll.bad	;PrintDebug
	rdc.w	ll.bad	;CallHook
	rdc.w	ll.own
	rdc.w	ll.disown
	rdc.w	ll.init
	rdc.w	ll.freq
	rdc.w	ll.refresh
	rdc.w	ll.readc
	rdc.w	ll.read
	rdc.w	ll.write
	rdc.w	ll.delay
	rdc.w	ll.irmode
	rdc.w	ll.bad	;LastSupported

ll.bad	moveq	#VSSERR_BadPkt,d0	;unexpected packet
	bsr.w	panic
	illegal

; now we own the semaphore, notify the clients!

ll.notifylock	move.l	SSM_SEMAPHORE(a3),d3
	beq.b	.ok
	bsr.w	ll.donotify
	move.l	d3,a0	;vacate
	move.l	a3,a1
	jsr	_LVOVacate(a6)
.ok	clr.l	mixerchgmsg(a5)
	clr.l	MN_REPLYPORT(a3)	;free msg
	clr.b	LN_PRI(a3)	;prioritas visszaallitasa
	;move.l	#-1,LN_NAME(a3)
	bra.w	ll.done

; start/stop remote server

ll.irmode	tst.l	pkt_arg(a3)
	bne	.start
	bsr	irExit
	bra	ll.done
.start	btst	#irf_opened,ir_flags(a5)	;mar futunk?
	bne	.running
	moveq	#VSSpkt_PrintInfo,d0	;info
	lea	i_irstart(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr	sendPkt
	jsr	_LVOCreateMsgPort(a6)
	move.l	d0,ir_port(a5)
	beq	.0
	bsr	portsig
	move.l	d0,ir_signal(a5)
	move.l	#IOEXTSER_SIZE,d0
	jsr	_LVOCreateIORequest(a6)
	move.l	d0,ir_request(a5)
	beq	.0
	move.l	d0,a1
	lea	sername(pc),a0
	clr.l	d0	;unit!
	clr.l	d1	;flags!
	jsr	_LVOOpenDevice(a6)
	tst.b	d0
	bne	.0
	bset	#irf_opened,ir_flags(a5)
	move.l	ir_request(a5),a1	;set mode
	move.w	#SDCMD_SETPARAMS,IO_COMMAND(a1)
	clr.l	d0
	move.w	conftmp+conf_irlen(a5),d0
	move.l	d0,ir_pktsize(a5)
	add.w	#64,d0
	and.b	#%11000000,d0
	move.l	d0,ir_bufsize(a5)
	move.l	d0,IO_RBUFLEN(a1)
	clr.l	IO_EXTFLAGS(a1)
	move.l	conftmp+conf_irbaud(a5),IO_BAUD(a1)
	move.b	#8,IO_READLEN(a1)
	move.b	#8,IO_WRITELEN(a1)
	move.b	#1,IO_STOPBITS(a1)
	move.b	#SERF_XDISABLED!SERF_RAD_BOOGIE,IO_SERFLAGS(a1)
	jsr	_LVODoIO(a6)
	tst.b	d0
	bne	.0

	bclr	#6,$bfd000	;power
	bclr	#7,$bfd000

	move.l	#micro_irreset,d0	;alszunk amig feleled
	lea	.00(pc),a0
	bsr	ll.sleep
	bra	ll.port
.00	bsr	irInit
	bra	ll.unsleep
.running	bsr	irInit
	bra	ll.done
.0	moveq	#VSSpkt_PrintFault,d0	;error!
	lea	e_irerr(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr	sendPkt
	bsr	irExit2
	bra	ll.done

; mixtab-ot kell allitani, frissitett ertekek a mixt-be kerulnek
;
; valtozott flageket beallitja a mixerchg-ben
; notify kivaltasa csak akkor, ha van ra ido
; (a bidMessage prioritasa kisebb mint a packeteke)

ll.refresh	move.w	mixt+VMT_switch1(a5),d3
	move.w	mixtab+VMT_switch1(a5),d0
	move.w	d0,mixt+VMT_switch1(a5)
	eor.w	d0,d3	;allitando analog bitek
	move.w	mixt+VMT_switch2(a5),d4
	move.w	mixtab+VMT_switch2(a5),d0
	move.w	d0,mixt+VMT_switch2(a5)
	eor.w	d0,d4	;allitando dig. bitek
	lea	mixt+VMT_0l(a5),a0
	clr.l	d0
	clr.l	d5
.0	move.b	mixtab-mixt(a0),d1	;mixtab
	move.b	(a0)+,d2	;mixt
	cmp.b	d2,d1
	bne.b	.1	;valtozott, frissites
	moveq	#1,d2
	cmp.b	#8,d0
	bcs.b	.3
	beq.b	.4
	cmp.b	#9,d0
	bne.b	.2	;nincs tobb flag
	asl.w	#1,d2	;digitalis csatorna bitek
.4	and.w	d4,d2
	beq.b	.2
	bra.b	.1
.3	move.w	d3,d2	;analog inputs
	lsr.w	#2,d3	;kell frissiteni?
	and.b	#3,d2
	beq.b	.2
.1	move.b	d1,-1(a0)	;uj ertek tarolasa
	btst	#lfl_hardok,ll_flags(a5)
	beq.b	.nohard
	bsr.w	setmixer	;mixer beallitasa
.nohard	bset	d0,d5
;	movem.l	d0-d2/a0,-(sp)	;debug
;	move.l	d0,d2
;	;move.l	d1,d2
;	moveq	#VSSpkt_PrintDebug,d0
;	lea	d_chgmix(pc),a0
;	move.l	a0,d1
;	move.l	cport(a5),a0
;	sub.l	a1,a1
;	bsr.w	sendPkt
;	movem.l	(sp)+,d0-d2/a0
.2	addq.l	#1,d0
	cmp.b	#VMT_SIZE-VMT_0l,d0
	bne.b	.0
	or.l	d5,mixerchanged(a5)
	move.l	d5,pkt_result(a3)
	beq.b	.nochg
	bsr.b	ll.notify
.nochg	bra.w	ll.done

; Mixer semaphore elojegyzese

ll.notify	tst.l	mixerchgmsg(a5)
	beq.b	.0
	rts
.0	bsr.w	allocPkt
	clr.l	LN_NAME(a1)	;exclusive lock
	clr.l	SSM_SEMAPHORE(a1)
	move.b	#-10,LN_PRI(a1)
	move.l	lport(a5),MN_REPLYPORT(a1)
	move.l	a1,mixerchgmsg(a5)
	lea	notifysem(a5),a0
	jmp	_LVOProcure(a6)

; Mixer notify, signal clients

ll.donotify	move.l	mixerchanged(a5),d5
	clr.l	mixerchanged(a5)
	lea	notifylist(a5),a2
	move.l	(a2),a2
.5	move.l	(a2),d2
	beq.b	.kesz
	move.l	vssn_sigtask(a2),a1
	;cmp.l	pkt_sender(a3),a1	;don't notify ourselves
	;beq	.no
	move.l	vssn_channels(a2),d0
	and.l	d5,d0
	beq.b	.no
	or.l	d0,vssn_channelchg(a2)
	move.l	vssn_signal(a2),d0
	jsr	_LVOSignal(a6)
.no	move.l	d2,a2
	bra.b	.5
.kesz	rts

; Frekvencia beallitasa
; Mp3 eseten MSB set, ilyenkor mindig atprogramozzuk a DSP-t.
; Egyebkent csak akkor, ha valtozott a frekvencia.

ll.freq	clr.l	pkt_result(a3)
	bset	#lfl_busy,ll_flags(a5)
	bne.w	ll.done
	btst	#lfl_hardok,ll_flags(a5)
	beq.w	ll.done
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_freq(pc),a0
	move.l	a0,d1
	move.l	pkt_arg(a3),d2
	bclr	#31,d2
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt

	move.l	pkt_arg(a3),d0
	bne.b	.1
	clr.l	lastfreq(a5)	;0, nem csinalunk semmit
	bra.w	.ok
.1	bclr	#31,d0
	bne.b	.set	;mp3, flush buffer
	cmp.l	lastfreq(a5),d0
	beq.w	.ok	;nem mp3, nem valtozott
	move.l	d0,lastfreq(a5)
	bra.b	.setnclr
.set	clr.l	lastfreq(a5)
.setnclr	move.l	#$2002ff00,d0	;power down codec
	bsr.w	i2cWrite	;(resetelni kell, mert
	move.l	#$2001ff00,d0	;neha nem indul ujra
	bsr.w	i2cWrite	;frekivaltas utan!)
	lea	DSPPre(pc),a0	;stop DSP, flush buffers
	bsr.w	configDSP
	moveq	#10,d0	;DSP regiszterek
	moveq	#7,d1	;visszaallitasa
.01	bsr.w	setmixer	;(resetkor torlodtek)
	addq.b	#1,d0
	dbf	d1,.01
	move.l	pkt_arg(a3),d0	;frekvencia keresese
	bclr	#31,d0
	lea	freqtab(pc),a0
.0	move.l	(a0)+,d1
	beq.b	.err	;illegal freq?
	cmp.l	d0,d1
	bne.b	.0
	move.l	freqdivtab-freqtab-4(a0),d1	;megvan!
	move.b	d1,freqdiv(a5)
	move.b	#$89,d1	;deemphasis
	btst	#lfl_deemp,ll_flags(a5)
	beq.b	.11
	move.l	deemptab-freqtab-4(a0),d1
.11	move.b	d1,deemp(a5)
	move.l	freqconftab-freqtab-4(a0),a0	;start DSP
	bsr.w	configDSP
	lea	DSPPost(pc),a0
	move.l	pkt_arg(a3),d0
	btst	#31,d0	;mp3?
	beq.b	.13
	lea	DSPPostMP3(pc),a0	;igen, inditas
.13	bsr.w	configDSP
	move.l	#$20010000,d0	;codec elesztese
	move.b	conftmp+conf_adcon(a5),d0
	asl.w	#8,d0
	bsr.w	i2cWrite
	move.l	#$20020000,d0
	move.b	conftmp+conf_dacon(a5),d0
	asl.w	#8,d0
	bsr.w	i2cWrite
	move.l	#$20050089,d0	;set de-emphasis
	move.b	deemp(a5),d0
	asl.w	#8,d0
	bsr.w	i2cWrite
.ok	st	pkt_result(a3)	;OK jelzese
	bclr	#lfl_busy,ll_flags(a5)
	bra.w	ll.done
.err	lea	e_illfreq(pc),a0	;illegal freq kiiras
	move.l	a0,d1
	move.l	d0,d2
	moveq	#VSSpkt_PrintFault,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	bclr	#lfl_busy,ll_flags(a5)
	bra.w	ll.done

; Varunk pkt_arg us-t

ll.delay	move.l	pkt_arg(a3),d0
	lea	ll.unsleep(pc),a0
	bsr	ll.sleep
	bra	ll.port

; A codec osszes regiszterenek olvasasa
; (argumentum nelkuli 'cs' parancs a shellben)

ll.readc	btst	#lfl_hardok,ll_flags(a5)
	beq.b	.err
	move.l	#$208000ff,d0
	bsr.w	i2cWrite
	beq.b	.ok
.err	lea	e_iicc(pc),a0
	move.l	a0,d1
	moveq	#VSSpkt_PrintFault,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	st	pkt_result(a3)
	bra.w	ll.done
.ok	move.l	pkt_arg(a3),a0
	moveq	#6,d4
.0	move.b	#$20,d0
	bsr.w	i2cRead
	bmi.b	.err
	move.l	d0,(a0)+
	dbf	d4,.0
	clr.l	pkt_result(a3)
	bra.w	ll.done

; i2c olvasas

ll.read	btst	#lfl_hardok,ll_flags(a5)
	beq.b	.err
	move.l	pkt_arg(a3),d0
	move.l	d0,d4
	bsr.w	i2cWrite
	bne.b	.err
	rol.l	#8,d4
	move.b	d4,d0
	bsr.w	i2cRead
	bmi.b	.err
	move.l	d0,pkt_result(a3)
	bra.w	ll.done
.err	st	pkt_result(a3)
	bra.w	ll.done

; i2c iras

ll.write	btst	#lfl_hardok,ll_flags(a5)
	beq.b	.err
	move.l	pkt_arg(a3),d0
	bsr.w	i2cWrite
	bmi.b	.err
	move.l	d0,pkt_result(a3)
	bra.w	ll.done
.err	st	pkt_result(a3)
	bra.w	ll.done

ll.disown	st	pkt_result(a3)
	lea	d_disown(pc),a0	;debug
	move.l	a0,d1
	move.l	address(a5),d2
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	bclr	#lfl_hardok,ll_flags(a5)
	beq.w	ll.done
	move.l	address(a5),a4
	lea	CODinit(pc),a0
	moveq	#6,d4
	bsr.w	i2cMultiW
	lea	DSPStop(pc),a0
	bsr.w	configDSP
	clr.b	_con(a4)
	bra.w	ll.done

ll.own	move.l	pkt_arg(a3),address(a5)
	bclr	#lfl_hardok,ll_flags(a5)
	bclr	#lfl_busy,ll_flags(a5)
	lea	d_own(pc),a0	;debug
	move.l	a0,d1
	move.l	address(a5),d2
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	bra.w	ll.done

ll.init	clr.l	pkt_result(a3)	;pkt_Init
	clr.l	lastfreq(a5)
	move.l	address(a5),d0
	beq.w	.ini3
	move.l	d0,a4
	bclr	#lfl_hardok,ll_flags(a5)
	bclr	#lfl_32k,ll_flags(a5)
	lea	e_board(pc),a0
	move.l	a4,d2
	move.b	_status(a4),d0	;check status
	and.b	#%11111100,d0
	cmp.b	#$80,d0
	bne.w	.ini
	move.b	_id(a4),d0
	move.b	d0,cardid(a5)
;----- Verzio fuggo parameterek, 8k cache:
	move.b	#62,maxbuf(a5)	;8k -2 (128byte blocks)
	move.b	#36,blocksperint(a5)	;4k +4
	;move.b	#80,blocksperint(a5)
;-----
	lea	idtab(pc),a0
	lea	nametab(pc),a2
.0	move.b	(a0)+,d1
	beq.b	.ini_obss	;nem talaljuk? (ujabb)
	move.l	(a2)+,d2
	move.b	sptab-idtab-1(a0),d3
	cmp.b	d0,d1
	bne.b	.0
	bsr.w	allocPkt	;Found vxx at..
	clr.l	MN_REPLYPORT(a1)
	move.l	#VSSpkt_PrintInfo,pkt_cmd(a1)
	lea	i_init(pc),a0
	move.l	a0,pkt_arg(a1)
	move.l	d2,pkt_args(a1)
	clr.l	d0
	move.b	cardid(a5),d0
	move.l	d0,pkt_args+4(a1)
	move.l	a4,pkt_args+8(a1)
	move.l	cport(a5),a0
	jsr	_LVOPutMsg(a6)
	lea	e_obsh(pc),a0
	tst.b	d3	;supported?
	beq.b	.ini
	lea	i_selftest(pc),a0	;Self test running...
	moveq	#VSSpkt_PrintInfo,d0
	bsr.w	.inierr2
; Kartya azonositva, most mar irhatunk is...
	clr.b	_con(a4)	;control registers
	move.b	#%10000000,_con(a4)
	move.b	#$1f,_ser(a4)
	lea	DSPreset(pc),a0	;reset DSP
	bsr.w	configDSP
	lea	e_iicd(pc),a0
	bne.b	.ini
	move.l	#micro_DSPreset,d0	;sleep
	lea	.cont(pc),a0
	bsr.w	ll.sleep
	bra.w	ll.port
.ini_obss	lea	e_obss(pc),a0
	clr.l	d2
	move.b	d0,d2
.ini	bsr.w	.inierr
.ini3	bclr	#lfl_busy,ll_flags(a5)
	bra.w	ll.done
.cont	move.l	address(a5),a4
	move.w	#$2080,d0	;config codec
	lea	CODinit(pc),a0
	moveq	#6,d4
	bsr.w	i2cMultiW
	bne.w	.ini2_codec
	move.b	#$89,deemp(a5)
	move.l	#$860100ff,d0	;test and configure DSP
	bsr.w	i2cWrite
	bmi.w	.ini2_dsp
	move.b	#$86,d0
	bsr.w	i2cRead
	lea	e_dspans(pc),a0
	cmp.b	#$ac,d0
	bne.w	.ini2
	lea	DSPinit(pc),a0	;load patches
	bsr.w	configDSP
	bne.w	.ini2_dsp

	move.l	conftmp+conf_flags(a5),d3	;memoria teszt tiltasa
	btst	#cflg_nomemtest,d3	;(Docky kedveert :)
	beq.b	.memtest
	lea	i_caches(pc),a0	;..test skipped
	moveq	#VSSpkt_PrintInfo,d0
	bsr.w	.inierr2
	bra.w	.nomemtest
.memtest	btst	#cflg_bytewr,d3	;word write enabled?
	bne.b	.bytewrite
	clr.b	_con(a4)	;memory test, word write
	move.b	#%10000000,_con(a4)
	lea	memtestdata,a0	;fill with pattern
	move.w	#$3fff,d0
.3	move.w	(a0)+,(a4)
	dbf	d0,.3
	btst	#cflg_byterd,d3
	bne.b	.byteread
	clr.l	d2	;word read check
	lea	memtestdata,a0
	move.w	#$3fff,d0
.2	move.w	(a0)+,d1
	cmp.w	(a4),d1
	bne.b	.wrbad
	addq.l	#1,d2
	dbf	d0,.2
	bra.w	.nomemtest	;32k word read/write!
.wrbad	cmp.w	#$2000,d2
	beq.w	.6	;8k word read/write!
.byteread	clr.l	d2	;byte read check
	clr.b	_con(a4)
	move.b	#%10000000,_con(a4)
	lea	memtestdata,a0
	move.w	#$7fff,d0
.5	move.b	(a0)+,d1
	cmp.b	(a4),d1
	bne.b	.brbad
	addq.l	#1,d2
	dbf	d0,.5
	bset	#cflg_byterd,d3
	bra.w	.nomemtest	;32k word wr, byte rd!
.brbad	cmp.w	#$2000,d2
	bne.b	.bytewrite	;8k word wr, byte rd!
	bset	#cflg_byterd,d3
	bra.w	.6
.bytewrite	clr.b	_con(a4)	;memory test, byte write
	move.b	#%10000000,_con(a4)
	lea	memtestdata,a1	;fill with pattern
	move.w	#$7fff,d0
.1	move.b	(a1)+,(a4)
	dbf	d0,.1
	btst	#cflg_byterd,d3
	bne.b	.byteread2
	clr.l	d2	;word read check
	lea	memtestdata,a0
	move.w	#$3fff,d0
.8	move.w	(a0)+,d1
	cmp.w	(a4),d1
	bne.b	.wrbad2
	addq.l	#1,d2
	dbf	d0,.8
	bset	#cflg_bytewr,d3
	bra.b	.nomemtest	;32k byte wr, word rd!
.wrbad2	cmp.w	#$2000,d2
	bne.b	.byteread2	;8k byte wr, word rd!
	bset	#cflg_bytewr,d3
	bra.b	.6
.byteread2	clr.l	d2	;byte read check
	clr.b	_con(a4)
	move.b	#%10000000,_con(a4)
	lea	memtestdata,a0
	move.w	#$7fff,d0
.9	move.b	(a0)+,d1
	cmp.b	(a4),d1
	bne.b	.brbad2
	addq.l	#1,d2
	dbf	d0,.9
	bset	#cflg_bytewr,d3
	bset	#cflg_byterd,d3
	bra.b	.nomemtest	;32k byte read/write!
.brbad2	bset	#cflg_bytewr,d3
	bset	#cflg_byterd,d3
	bra.b	.6
.nomemtest	bset	#lfl_32k,ll_flags(a5)
;----- Verzio fuggo parameterek, 32k cache:
	move.b	#254,maxbuf(a5)	;32k -4
;-----
	moveq	#32,d2
	bra.b	.7
.6	lea	e_mem(pc),a0
	cmp.w	#$2000,d2	;az elso 8k is rossz?
	bcs.w	.ini2
	moveq	#8,d2
.7	move.l	d3,conftmp+conf_flags(a5)

	bsr.w	allocPkt
	clr.l	MN_REPLYPORT(a1)
	move.l	#VSSpkt_PrintInfo,pkt_cmd(a1)
	lea	i_cache(pc),a0
	move.l	a0,pkt_arg(a1)
	move.l	d2,pkt_args(a1)
	lea	i_cachew(pc),a0
	btst	#cflg_byterd,d3
	beq.b	.10
	lea	i_cacheb(pc),a0
.10	move.l	a0,pkt_args+4(a1)
	lea	i_cachew(pc),a0
	btst	#cflg_bytewr,d3
	beq.b	.11
	lea	i_cacheb(pc),a0
.11	move.l	a0,pkt_args+8(a1)
	move.l	cport(a5),a0
	jsr	_LVOPutMsg(a6)

	bsr.w	intentest
	clr.b	intcnt(a5)	;mexakitas inditasa
	move.b	#%10101010,con(a5)
	move.b	#%10101010,_con(a4)
	move.l	#micro_inttest,d0	;sleep
	lea	.cont2(pc),a0
	bsr.w	ll.sleep
	bra.w	ll.sleepers
.cont2	move.l	address(a5),a4
	move.b	#$8c,_con(a4)
	clr.b	con(a5)
	clr.b	con2(a5)
	bsr.w	intdis
	lea	e_inter(pc),a0
	tst.b	intcnt(a5)
	beq.b	.ini2
	move.w	#$2080,d0	;config codec
	lea	CODinit1(pc),a0
	move.b	conftmp+conf_adcon(a5),1(a0)
	move.b	conftmp+conf_dacon(a5),2(a0)
	moveq	#6,d4
	bsr.w	i2cMultiW
	bne.b	.ini2_codec
	bsr.w	updatmix	;set mixer
	bset	#lfl_hardok,ll_flags(a5)
	bclr	#lfl_busy,ll_flags(a5)
	moveq	#VSSpkt_PrintInfo,d0
	lea	i_pass(pc),a0
	bsr.b	.inierr2
	st	pkt_result(a3)
	bra.w	ll.unsleep
.inierr	moveq	#VSSpkt_PrintFault,d0
.inierr2	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bra.w	sendPkt
.ini2_codec	lea	e_iicc(pc),a0
	bra.b	.ini2
.ini2_dsp	lea	e_iicd(pc),a0
.ini2	bsr.b	.inierr
	bclr	#lfl_busy,ll_flags(a5)
	bra.w	ll.unsleep

ll.done	move.l	a3,a1	;reply packet, continue
	bsr.w	replyPkt
	bra.w	ll.port
ll.kill	move.l	a3,d7	;pkt_Kill
	bsr	irExit
	bsr.b	ll.timerabort
	move.l	timereq(a5),a1
	jsr	_LVOCloseDevice(a6)
	move.l	mixerchgmsg(a5),d0
	beq.b	ll.err
	move.l	d0,a1
	lea	notifysem(a5),a0
	jsr	_LVOVacate(a6)
ll.err	move.l	timereq(a5),a0
	jsr	_LVODeleteIORequest(a6)
	move.l	ltport(a5),a0
	jsr	_LVODeleteMsgPort(a6)
.0	move.l	lport(a5),a0
	jsr	_LVODeleteMsgPort(a6)
	clr.l	lport(a5)
	jsr	_LVOForbid(a6)
	tst.l	d7
	bne.b	.2
	move.l	mainproc(a5),a1	;we are dead!
	move.l	subtasksig(a5),d0
	jsr	_LVOSignal(a6)
	bra.b	.4
.2	move.l	d7,a1	;the killer msg :)
	bsr.w	replyPkt
.4	clr.l	d0
	rts

; In D0:msgport  Out D0:signal

portsig	move.l	d0,a0
	clr.l	d0
	move.b	MP_SIGBIT(a0),d1
	bset	d1,d0
	rts

; timer inditasa, #micro us

ll.timerstart	move.l	timereq(a5),a1
	move.w	#TR_ADDREQUEST,IO_COMMAND(a1)
	clr.l	IOTV_TIME+TV_SECS(a1)
	move.l	#micro,IOTV_TIME+TV_MICRO(a1)
	jmp	_LVOSendIO(a6)

ll.timerabort	move.l	timereq(a5),a1
	jsr	_LVOAbortIO(a6)
ll.timerclear	move.l	timereq(a5),a1
	jmp	_LVOWaitIO(a6)

CODinit	dc.b	$00,$84,$80,$ff,$ff,$89
CODinit1	dc.b	$00,$00,$00,$ff,$ff,$89

DSPreset	dc.b	$10,$01
	dc.w	0

DSPinit	incbin	VSS:Sources/include/dspconf.bin
	dc.b	$63,$00	;dac_clk_mode
	dc.b	$54,$03	;pcmdivider
	dc.b	$55,$3b	;pcmconf
	dc.b	$07,$00	;pllctl(N)
	dc.b	$06,$0b	;pllctl(M)
	dc.b	$0b,$03	;reserved
	dc.b	$64,$d3	;pllfrac_l
	dc.b	$65,$03	;pllfrac_h
	dc.b	$61,$06	;mfsdf
	dc.b	$51,$9d	;pllfrac_441_l
	dc.b	$52,$9d	;pllfrac_441_h
	dc.b	$50,$07	;mfsdf_441
	dc.b	$05,$a1	;pllctl
	dc.b	$0d,$00	;sclk_pol 0|4
	dc.b	$18,$04	;req_enable
	dc.b	$0c,$01	;req_pol 1|5
	dc.b	$13,$00	;play
	dc.b	$14,$00	;mute
	dc.b	$72,$00	;run
	dc.b	$4e,$01	;CRC check off
	dc.w	0

DSPStop	dc.b	$72,$00
	dc.b	$14,$00
	dc.b	$13,$00
	dc.b	$10,$01
	dc.w	0

DSP16000	dc.b	$64,$4b
	dc.b	$65,$96
	dc.b	$61,$0a
	dc.b	$08,$4b
	dc.b	$09,$96
	dc.b	$0a,$15
	dc.w	0

DSP22050	dc.b	$08,$9d
	dc.b	$09,$9d
	dc.b	$0a,$0f
	dc.w	0

DSP24000	dc.b	$64,$d3
	dc.b	$65,$03
	dc.b	$61,$06
	dc.b	$08,$d3
	dc.b	$09,$03
	dc.b	$0a,$0d
	dc.w	0

DSP32000	dc.b	$64,$4b
	dc.b	$65,$96
	dc.b	$61,$0a
	dc.b	$08,$4b
	dc.b	$09,$96
	dc.b	$0a,$0a
	dc.w	0

DSP44100	dc.b	$08,$9d
	dc.b	$09,$9d
	dc.b	$0a,$07
	dc.w	0

DSP48000	dc.b	$64,$d3
	dc.b	$65,$03
	dc.b	$61,$06
	dc.b	$08,$d3
	dc.b	$09,$03
	dc.b	$0a,$06
	dc.w	0

DSPPre	dc.b	$10,$01	;reset
	dc.b	$63,$00	;dac_clk_mode
	dc.b	$54,$03	;pcmdivider
	dc.b	$55,$3b	;pcmconf
	dc.b	$07,$00	;pllctl(N)
	dc.b	$06,$0b	;pllctl(M)
	dc.b	$0b,$03	;reserved
	dc.b	$64,$d3	;pllfrac_l
	dc.b	$65,$03	;pllfrac_h
	dc.b	$61,$06	;mfsdf
	dc.b	$51,$9d	;pllfrac_441_l
	dc.b	$52,$9d	;pllfrac_441_h
	dc.b	$50,$07	;mfsdf_441
	dc.b	$05,$a1	;pllctl
	dc.b	$0d,$00	;sclk_pol 0|4
	dc.b	$18,$04	;req_enable
	dc.b	$0c,$01	;req_pol 1|5
	;dc.b	$72,$00	;run
	;dc.b	$13,$00	;play
	;dc.b	$14,$00	;mute
	dc.b	$4e,$01	;CRC check off
	dc.w	0

DSPPost	dc.b	$05,$a0
	dc.b	$05,$a1
	dc.b	$14,$01
	dc.b	$13,$00
	dc.b	$72,$01
	dc.w	0

DSPPostMP3	dc.b	$05,$a0
	dc.b	$05,$a1
	dc.b	$13,$01
	dc.b	$72,$01
	dc.b	$14,$00
	dc.w	0

; Regi modszer, programozas framekkel
;
;frame_16000	incbin	VSS:Sources/include/frame_16000.mp3
;frame_16000_l	equ	*-frame_16000
;frame_22050	incbin	VSS:Sources/include/frame_22050.mp3
;frame_22050_l	equ	*-frame_22050
;frame_24000	incbin	VSS:Sources/include/frame_24000.mp3
;frame_24000_l	equ	*-frame_24000
;frame_32000	incbin	VSS:Sources/include/frame_32000.mp3
;frame_32000_l	equ	*-frame_32000
;frame_44100	incbin	VSS:Sources/include/frame_44100.mp3
;frame_44100_l	equ	*-frame_44100
;frame_48000	incbin	VSS:Sources/include/frame_48000.mp3
;frame_48000_l	equ	*-frame_48000
;	even

freqtab	dc.l	8000,11025,12000,16000,22050,24000,32000,44100,48000,0
freqconftab	dc.l	DSP16000,DSP22050,DSP24000
	dc.l	DSP32000,DSP44100,DSP48000
	dc.l	DSP32000,DSP44100,DSP48000
freqdivtab	dc.l	$c0,$c0,$c0,$c0,$c0,$c0,$80,$80,$80,0

deemptab	dc.l	$89,$89,$89,$89,$89,$89,$69,$29,$49

idtab	dc.b	$ff,$c3,$c2,$c6,$c7,$c5,$c4,$d4,$d3,$a4,$9c,$9c,$98,0

sptab	dc.b	0,0,0,0,0,0,0,0,0,1,1,1,1
	even

nametab	dc.l	t_v10,t_v11a,t_v11b,t_v11c,t_v11d,t_v11e,t_v11f
	dc.l	t_v12,t_v13,t_v14,t_v15,t_v16,t_v17


; buffer manager subtask

bufmanentry	move.l	var(pc),a5
	move.l	excbase(a5),a6
	clr.l	d7	;no packet (finalizernek)
	moveq	#-1,d0	;alloc signal
	jsr	_LVOAllocSignal(a6)
	move.b	d0,msigbit(a5)
	bmi.w	.err
	clr.l	d1
	bset	d0,d1
	move.l	d1,msig(a5)
	jsr	_LVOCreateMsgPort(a6)	;port
	move.l	d0,mport(a5)
	beq.w	.err
	bsr.w	portsig
	move.l	d0,mport_sig(a5)
	jsr	_LVOCreateMsgPort(a6)	;open timer
	move.l	d0,mtport(a5)
	beq.w	.err
	bsr.w	portsig
	move.l	d0,mtport_sig(a5)
	moveq	#IOTV_SIZE,d0
	jsr	_LVOCreateIORequest(a6)
	move.l	d0,mtimereq(a5)
	beq.w	.err
	move.l	d0,a1
	lea	timname(pc),a0
	moveq	#UNIT_MICROHZ,d0	;unit
	clr.l	d1	;flags
	jsr	_LVOOpenDevice(a6)
	tst.b	d0
	bne.w	.err
	bsr.w	.timerstart
	bsr.w	.timerabort


	; setup actions


	move.l	mainproc(a5),a1	;we are alive!
	move.l	subtasksig(a5),d0
	jsr	_LVOSignal(a6)

.loop	move.l	mport_sig(a5),d0	;wait for new messages
	or.l	mtport_sig(a5),d0
	or.l	msig(a5),d0
	jsr	_LVOWait(a6)
	move.l	d0,msigrcvd(a5)
	and.l	mtport_sig(a5),d0
	beq.b	.chkpoll
	bsr.w	.timerclear	;clear and restart timer
	btst	#lfl_poll,ll_flags(a5)
	beq.b	.chkpoll
	bsr.w	.timerstart
	bra.b	.poll

.chkpoll	move.l	msigrcvd(a5),d0
	and.l	msig(a5),d0
	beq.w	.port

.poll	btst	#lfl_poll,ll_flags(a5)	;poll board
	beq.w	.nopoll
	tst.b	tasktransfer(a5)
	bne.b	.tasktr
	bclr	#lfl_inter,ll_flags(a5)
	bne.w	.nopoll
	move.l	softint(a5),a1
	jsr	_LVOCause(a6)
	bra.b	.nopoll

.tasktr	IFD	makecolor
	move.w	#$0f00,$dff180
	ENDIF
	move.l	address(a5),a4

	move.b	loadcnt(a5),d1	;play
.p2	move.b	_cahi(a4),d0
	IFD	checkptr
	cmp.b	_cahi(a4),d0
	bne.b	.p2
	ENDIF
	asl.w	#1,d0
	sub.b	d0,d1
	move.b	bufsize(a5),d0
	sub.b	d1,d0
	btst	#lfl_hardunder,ll_flags(a5)
	bne.b	.underrun
.cont	asl.w	#7,d0
	sub.w	remain(a5),d0
	bls.b	.don
	move.l	acthook(a5),a0	;a0:hook a5:card
	move.l	h_Entry(a0),a1
	exg	a4,a5
	btst	#lfl_hardunder,ll_flags(a4)	;underrun eseten Z=0
	jsr	(a1)
	exg	a4,a5
	move.w	d0,d1
	beq.b	.don
	lsr.w	#7,d0
	and.w	#$7f,d1
	add.w	remain(a5),d1
	tst.b	d1
	bpl.b	.p0
	and.b	#$7f,d1
	addq.b	#1,d0
.p0	move.w	d1,remain(a5)
	add.b	d0,loadcnt(a5)

.don	IFD	makecolor
	move.w	#$0009,$dff180
	ENDIF

	move.l	excbase(a5),a6
	bra.b	.nopoll

.underrun	cmp.b	blocksperint(a5),d1	;underrun volt!
	bls.b	.cont
	move.b	con2(a5),d1	;eleg, DMA ujrainditasa
	move.b	d1,con(a5)
	move.b	d1,_con(a4)
	bclr	#lfl_hardunder,ll_flags(a5)
	bra.b	.cont

.nopoll	move.l	mcurpkt(a5),d0
	beq.b	.nore
	clr.l	mcurpkt(a5)
	move.l	d0,a1
	;jsr	_LVOReplyMsg(a6)
	bsr.w	replyPkt
	bra.b	.port

.nore	move.l	msigrcvd(a5),d0
	and.l	mport_sig(a5),d0
	beq.w	.loop
.port	move.l	mport(a5),a0	;process packets
	jsr	_LVOGetMsg(a6)
	tst.l	d0
	beq.w	.loop
	move.l	d0,a3
	move.l	pkt_cmd(a3),d0
	cmp.l	#VSSpkt_StopSlave,d0
	beq.b	.pktStopSlave
	cmp.l	#VSSpkt_StartSlave,d0
	beq.b	.pktStartSlave
	cmp.l	#VSSpkt_Kill,d0
	bne.w	.bad
.kill	move.l	a3,d7	;pkt_Kill
	bsr.w	.timerabort
	move.l	mtimereq(a5),a1
	jsr	_LVOCloseDevice(a6)
.err	move.l	mtimereq(a5),a0
	jsr	_LVODeleteIORequest(a6)
	move.l	mtport(a5),a0
	jsr	_LVODeleteMsgPort(a6)
	move.l	mport(a5),a0
	jsr	_LVODeleteMsgPort(a6)
	clr.l	mport(a5)
	move.b	msigbit(a5),d0
	jsr	_LVOFreeSignal(a6)
	jsr	_LVOForbid(a6)
	tst.l	d7
	bne.b	.2
	move.l	mainproc(a5),a1	;we are dead!
	move.l	subtasksig(a5),d0
	jsr	_LVOSignal(a6)
	bra.b	.4
.2	move.l	d7,a1	;the killer msg :)
	bsr.w	replyPkt
.4	clr.l	d0
	rts

.pktStartSlave	move.l	a3,mcurpkt(a5)	;csak akkor valaszoljuk
	bset	#lfl_poll,ll_flags(a5)	;meg, ha buffer feltoltve
	bne.w	.poll
	bsr.b	.timerstart
	bra.w	.poll
.pktStopSlave	bclr	#lfl_poll,ll_flags(a5)
	beq.b	.done
	bsr.b	.timerabort
	clr.l	d0
	move.l	mtport_sig(a5),d1
	jsr	_LVOSetSignal(a6)
.done	move.l	a3,a1
	bsr.w	replyPkt
	bra.w	.loop

.bad	moveq	#VSSERR_BadPkt,d0	;unexpected packet
	bsr.w	panic
	illegal

.timerstart	move.l	mtimereq(a5),a1
	move.w	#TR_ADDREQUEST,IO_COMMAND(a1)
	clr.l	IOTV_TIME+TV_SECS(a1)
	move.l	quantum2(a5),IOTV_TIME+TV_MICRO(a1)
	jmp	_LVOSendIO(a6)

.timerabort	move.l	mtimereq(a5),a1
	jsr	_LVOAbortIO(a6)
.timerclear	move.l	mtimereq(a5),a1
	jmp	_LVOWaitIO(a6)


;----< CS8130 IRDA as TV remote controller >-------------------------------------

micro_irreset	equ	250000
irf_opened	equ	0

; allocate resources, start server

irInit	move.l	excbase(a5),a6

	bset	#6,$bfd000	;reset
	bclr	#6,$bfd000

	move.l	ir_request(a5),a1
	jsr	_LVOAbortIO(a6)
	move.l	ir_request(a5),a1
	jsr	_LVOWaitIO(a6)
	clr.l	d0
	move.l	ir_signal(a5),d1
	jsr	_LVOSetSignal(a6)

	bset	#7,$bfd000	;command mode

	move.l	ir_request(a5),a1	;init string
	move.w	#CMD_WRITE,IO_COMMAND(a1)
	move.l	#-1,IO_LENGTH(a1)
	lea	conftmp+conf_irinit(a5),a0
	move.l	a0,IO_DATA(a1)
	jsr	_LVODoIO(a6)
	tst.b	d0
	bne	.0

	bclr	#7,$bfd000	;command mode off

	move.l	ir_request(a5),a1	;clear buffers
	move.w	#CMD_CLEAR,IO_COMMAND(a1)
	jsr	_LVODoIO(a6)
	move.l	ir_request(a5),a1	;post read request
	move.w	#CMD_READ,IO_COMMAND(a1)
	move.l	ir_pktsize(a5),IO_LENGTH(a1)
	lea	ir_buffer(a5),a0
	move.l	a0,IO_DATA(a1)
	jsr	_LVOSendIO(a6)

	bclr	#6,$bfd000
	bclr	#7,$bfd000

	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_irrestart(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr	sendPkt

.0	rts


; stop server

irExit	btst	#irf_opened,ir_flags(a5)	;ha nem futott, nem
	beq	irExit2	;kell szoveg
	moveq	#VSSpkt_PrintInfo,d0	;info
	lea	i_irstop(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr	sendPkt
irExit2	move.l	excbase(a5),a6
	bclr	#irf_opened,ir_flags(a5)
	beq	.noclose
	move.l	ir_request(a5),a1
	jsr	_LVOAbortIO(a6)
	move.l	ir_request(a5),a1
	jsr	_LVOWaitIO(a6)
	move.l	ir_request(a5),a1
	jsr	_LVOCloseDevice(a6)
.noclose	move.l	ir_request(a5),a0
	clr.l	ir_request(a5)
	jsr	_LVODeleteIORequest(a6)
	move.l	ir_port(a5),a0
	clr.l	ir_port(a5)
	jsr	_LVODeleteMsgPort(a6)
	clr.l	d0
	move.l	ir_signal(a5),d1
	jsr	_LVOSetSignal(a6)
	clr.l	ir_signal(a5)
	rts

; process remote codes

irProcess	move.l	excbase(a5),a6
	move.l	ir_request(a5),a1
	jsr	_LVOCheckIO(a6)
	tst.l	d0
	beq	.0
	move.l	ir_request(a5),a1
	jsr	_LVOWaitIO(a6)

	lea	creative(pc),a0
	bsr	irFindCode
	tst.l	d0
	beq	.2

	bsr	allocPkt
	clr.l	MN_REPLYPORT(a1)
	move.l	#VSSpkt_PrintDebug,pkt_cmd(a1)
	lea	.t2(pc),a0
	move.l	a0,pkt_arg(a1)
	lea	pkt_args+4(a1),a0
	move.l	a0,pkt_args(a1)
	move.l	d0,(a0)
	clr.b	4(a0)
	move.l	cport(a5),a0
	jsr	_LVOPutMsg(a6)

	bra	.0




.2	bsr	allocPkt
	clr.l	MN_REPLYPORT(a1)
	move.l	#VSSpkt_PrintDebug,pkt_cmd(a1)
	lea	.t(pc),a0
	move.l	a0,pkt_arg(a1)

	move.l	ir_buffer(a5),pkt_args(a1)
	move.l	ir_buffer+4(a5),pkt_args+4(a1)
	move.l	ir_buffer+8(a5),pkt_args+8(a1)
	move.l	ir_buffer+12(a5),pkt_args+12(a1)
	move.l	ir_buffer+16(a5),pkt_args+16(a1)
	move.l	ir_buffer+20(a5),pkt_args+20(a1)
	move.l	ir_buffer+24(a5),pkt_args+24(a1)
	move.l	ir_buffer+28(a5),pkt_args+28(a1)
	move.l	ir_buffer+32(a5),pkt_args+32(a1)
	move.l	ir_buffer+36(a5),pkt_args+36(a1)

	move.l	cport(a5),a0
	jsr	_LVOPutMsg(a6)

.0	rts

.t	dc.b	'%08.8lx %08.8lx %08.8lx %08.8lx ',10
	dc.b	'%08.8lx %08.8lx %08.8lx %08.8lx ',10
	dc.b	'%08.8lx %08.8lx',0

.t2	dc.b	'IRR Command ''%s'' received',0

	even


; In A0:table  Out D0:code or null

irFindCode	move.l	(a0)+,d2	;length
	lsr.w	#2,d2
	subq.w	#1,d2
.nextcode	move.l	(a0)+,d0
	beq	.ok
	move.l	d0,a1
	lea	ir_buffer(a5),a2
	move.l	(a0)+,d0
	move.w	d2,d1
.nextlong	move.l	(a1)+,d3
	cmp.l	(a2)+,d3
	bne	.nextcode
	dbf	d1,.nextlong
.ok	rts

creative	dc.l	40	;code length
	dc.l	.creative_rept,'REPT'
	dc.l	.creative_rep2,'REPT'
	dc.l	.creative_0,'   0'
	dc.l	.creative_1,'   1'
	dc.l	.creative_2,'   2'
	dc.l	.creative_3,'   3'
	dc.l	.creative_4,'   4'
	dc.l	.creative_5,'   5'
	dc.l	.creative_6,'   6'
	dc.l	.creative_7,'   7'
	dc.l	.creative_8,'   8'
	dc.l	.creative_9,'   9'
	dc.l	.creative_shift,'SHFT'
	dc.l	.creative_enter,'ENTR'
	dc.l	.creative_magni,'MAGN'
	dc.l	.creative_volup,'VOL+'
	dc.l	.creative_voldn,'VOL-'
	dc.l	.creative_mute,'MUTE'
	dc.l	.creative_mouse,'MOUS'
	dc.l	.creative_menu,'MENU'
	dc.l	.creative_play,'PLAY'
	dc.l	.creative_stop,'STOP'
	dc.l	.creative_prev,'PREV'
	dc.l	.creative_pause,'PAUS'
	dc.l	.creative_forw,'FORW'
	dc.l	.creative_backw,'BACK'
	dc.l	0,0

.creative_1	dc.l	$0000ffae,$eaaaeeba,$aeeaeaee,$eebaebff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5dd5d5dd,$dd75d7ff
.creative_2	dc.l	$0000ffae,$eaaaeeba,$aeeabaee,$eebaeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5dd575dd,$dd75ddff
.creative_3	dc.l	$0000ffae,$eaaaeeba,$aeeababb,$bbebeaff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5dd57577,$77d7d5ff
.creative_4	dc.l	$0000ffae,$eaaaeeba,$aeeaaebb,$bbabebff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5dd55d77,$7757d7ff
.creative_5	dc.l	$0000ffae,$eaaaeeba,$aeeaeeba,$bbabeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5dd5dd75,$7757ddff
.creative_6	dc.l	$0000ffae,$eaaaeeba,$aeeaeeee,$eeaeeaff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5dd5dddd,$dd5dd5ff
.creative_7	dc.l	$0000ffae,$eaaaeeba,$aebaeaee,$aebbebff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d75d5dd,$5d77d7ff
.creative_8	dc.l	$0000ffae,$eaaaeeba,$aebabaee,$aebbeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d7575dd,$5d77ddff
.creative_9	dc.l	$0000ffae,$eaaaeeba,$aebababb,$bbeeeaff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d757577,$77ddd5ff
.creative_0	dc.l	$0000ffae,$eaaaeeba,$aebaeeba,$bbaeeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d75dd75,$775dddff
.creative_shift	dc.l	$0000ffae,$eaaaeeba,$aebaaebb,$bbaeebff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d755d77,$775dd7ff
.creative_enter	dc.l	$0000ffae,$eaaaeeba,$aebaeeee,$eebaeaff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d75dddd,$dd75d5ff
.creative_magni	dc.l	$0000ffae,$eaaaeeba,$aebaabee,$aeeeeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d7557dd,$5dddddff
.creative_volup	dc.l	$0000ffae,$eaaaeeba,$aebaaabb,$ebeeeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d755577,$d7ddddff
.creative_voldn	dc.l	$0000ffae,$eaaaeeba,$aebaaeee,$aeebeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d755ddd,$5dd7ddff
.creative_mute	dc.l	$0000ffae,$eaaaeeba,$aeeaaeee,$eeeaeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5dd55ddd,$ddd5ddff
.creative_mouse	dc.l	$0000ffae,$eaaaeeba,$aeeaaabb,$bbeeeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5dd55577,$77ddddff
.creative_menu	dc.l	$0000ffae,$eaaaeeba,$aeaaabbb,$bbebeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d555777,$77d7ddff
.creative_pause	dc.l	$0000ffae,$eaaaeeba,$aeaababb,$bbbbebff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d557577,$7777d7ff
.creative_stop	dc.l	$0000ffae,$eaaaeeba,$aeaaeeee,$eeeeeaff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d55dddd,$ddddd5ff
.creative_play	dc.l	$0000ffae,$eaaaeeba,$aeaaaebb,$bbbbeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d555d77,$7777ddff
.creative_prev	dc.l	$0000ffae,$eaaaeeba,$aeaabbee,$eeaeeeff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d5577dd,$dd5dddff
.creative_backw	dc.l	$0000ffae,$eaaaeeba,$aeaaebee,$eeaeebff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d55d7dd,$dd5dd7ff
.creative_forw	dc.l	$0000ffae,$eaaaeeba,$aeaabbbb,$bbbbeaff
	dc.l	$ffffffff,$ffffffff,$0100fe5d,$d555dd75
	dc.l	$5d557777,$7777d5ff
.creative_rept	dc.l	$0000efff,$ffffffff,$ffffffff,$ffffffff
	dc.l	$ffffffff,$ffffffff,$0300bcff,$ffffffff
	dc.l	$ffffffff,$ffffffff
.creative_rep2	dc.l	$0000efff,$ffffffff,$ffffffff,$ffffffff
	dc.l	$ffffffff,$ffffffff,$ffffffff,$ffffffff
	dc.l	$ffffffff,$ffffffff

	even


;-----------< packet handling functions >----------------------------------------

; In -  Out A1:packet

allocPkt	movem.l	d0-d1/a0/a6,-(sp)
.loop	move.l	excbase(a5),a6
	lea	pktsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	lea	pktlist(a5),a0
	jsr	_LVORemHead(a6)
	lea	pktsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	tst.l	d0
	bne.b	.ok
	move.l	conftmp+conf_flags(a5),d0	;ha true, akkor fail
	btst	#cflg_pktpanic,d0
	beq.b	.try
	moveq	#VSSERR_AllocPkt,d0	;panic
	bsr.w	panic
	illegal
.try	move.l	dosbase(a5),a6
	moveq	#2,d1
	jsr	_LVODelay(a6)
	bra.b	.loop
.ok	move.l	d0,a1
	;move.l	ThisTask(a6),pkt_sender(a1)
	st	pkt_used(a1)
	movem.l	(sp)+,d0-d1/a0/a6
	rts

; In A0:port A1:replyport D0:cmd D1:arg D2:arg2

sendPkt	movem.l	d0-d1/a0-a1/a6,-(sp)
	move.l	excbase(a5),a6
	move.l	a1,-(sp)
	bsr.b	allocPkt
	move.l	(sp)+,MN_REPLYPORT(a1)
	move.l	d0,pkt_cmd(a1)
	move.l	d1,pkt_arg(a1)
	move.l	d2,pkt_args(a1)
	clr.l	pkt_result(a1)
	jsr	_LVOPutMsg(a6)
	movem.l	(sp)+,d0-d1/a0-a1/a6
	rts

; In A1:packet

replyPkt	movem.l	d0-d1/a0-a2/a6,-(sp)
	move.l	excbase(a5),a6
	cmp.b	#NT_MESSAGE,LN_TYPE(a1)
	bne.b	.free
	tst.l	MN_REPLYPORT(a1)
	bne.b	.reply
.free	sf	pkt_used(a1)
	lea	pktsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	lea	pktlist(a5),a0
	jsr	_LVOAddHead(a6)
	lea	pktsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	bra.b	.1
.reply	jsr	_LVOReplyMsg(a6)
.1	movem.l	(sp)+,d0-d1/a0-a2/a6
	rts

; In A0:port  Out A1:packet

waitPkt	movem.l	d0-d1/a0/a2/a6,-(sp)
	move.l	excbase(a5),a6
	move.l	a0,a2
.0	move.l	a2,a0
	jsr	_LVOWaitPort(a6)
	move.l	a2,a0
	jsr	_LVOGetMsg(a6)
	tst.l	d0
	beq.b	.0
	move.l	d0,a1
	movem.l	(sp)+,d0-d1/a0/a2/a6
	rts

; In A0:port  Out A1:packet

getPkt	movem.l	d0-d1/a0/a6,-(sp)
	move.l	excbase(a5),a6
	jsr	_LVOGetMsg(a6)
	move.l	d0,a1
	tst.l	d0
	movem.l	(sp)+,d0-d1/a0/a6
	rts

; In A0:port A1:replyport D0:cmd D1:arg D2:arg2  Out D0:result

doPkt	;move.l	d0,-(sp)
	bsr.w	sendPkt
	move.l	a1,a0
	bsr.b	waitPkt

	;move.l	pkt_cmd(a1),d0
	;cmp.l	(sp)+,d0
	;bne	ll.bad
	;tst.l	MN_REPLYPORT(a1)
	;bne	ll.bad

	move.l	pkt_result(a1),d0
	bra.w	replyPkt


;-----------< interrupt processing >---------------------------------------------

; megszakitasok elokeszitese

intprep	lea	int6_play(pc),a0
	move.l	a5,IS_DATA(a0)
	move.l	a5,int6_rec-int6_play+IS_DATA(a0)
	move.l	a5,int6_playt-int6_play+IS_DATA(a0)
	move.l	a5,int6_rect-int6_play+IS_DATA(a0)
	move.l	a5,int6_test-int6_play+IS_DATA(a0)
	move.l	a5,softint_play-int6_play+IS_DATA(a0)
	move.l	a5,softint_rec-int6_play+IS_DATA(a0)
	rts

; megszakitas engedelyezese

intenplay	lea	softint_play(pc),a0
	lea	int6_play(pc),a1
	tst.b	tasktransfer(a5)
	beq.b	inten
	lea	int6_playt(pc),a1
	bra.b	inten
intenrec	lea	softint_rec(pc),a0
	lea	int6_rec(pc),a1
	tst.b	tasktransfer(a5)
	beq.b	inten
	lea	int6_rect(pc),a1
	bra.b	inten
intentest	sub.l	a0,a0
	lea	int6_test(pc),a1
inten	tst.l	int6(a5)
	beq.b	.0
	movem.l	a0-a1,-(sp)
	bsr.b	intdis
	movem.l	(sp)+,a0-a1
.0	move.l	a0,softint(a5)
	move.l	a1,int6(a5)
	move.l	excbase(a5),a6
	moveq	#INTB_EXTER,d0
	jmp	_LVOAddIntServer(a6)

; megszakitas tiltasa

intdis	move.l	int6(a5),d0
	beq.b	.noint
	move.l	d0,a1
	move.l	excbase(a5),a6
	moveq	#INTB_EXTER,d0
	jsr	_LVORemIntServer(a6)
	clr.l	int6(a5)
	clr.l	softint(a5)
	;bclr	#lfl_poll,ll_flags(a5)
.noint	rts

; play interrupts, leallas ha load-cahi < blocksperint

PLAYINT_PRE	MACRO
	;move.w	#$0f00,$dff180
	move.l	address(a1),a5
	btst	#1,_status(a5)
	beq.b	.noint

	move.b	loadcnt(a1),d1	;mennyi adat van
.0	move.b	_cahi(a5),d0	;a hardware bufferben?
	IFD	checkptr
	cmp.b	_cahi(a5),d0
	bne.b	.0
	ENDIF
	asl.b	#1,d0
	sub.b	d0,d1
	cmp.b	blocksperint(a1),d1	;kitart a kovetkezo
	bhi.b	.ok	;megszakitasig?

	move.b	con(a1),d0	;nem, DMA leallitasa
	and.b	#%11111100,d0
	move.b	d0,_con(a5)
	move.b	d0,con(a1)
	bset	#lfl_hardunder,ll_flags(a1)	;underrun jelzese
.ok	;ENDM nem lehet a cimkevel egy sorban?! (Asm-Pro bug)
	ENDM

PLAYINT_POST	MACRO
	;move.w	#$0009,$dff180
	move.b	#-1,_intack(a5)	;mexakitas torlese
.noint	rts
	ENDM

int6_play	dc.l	0,0
	dc.b	NT_INTERRUPT
	dc.b	100	;pri
	dc.l	vssname
	dc.l	0
	dc.l	.code

.code	PLAYINT_PRE
	move.l	excbase(a1),a6	;softint
	lea	softint_play(pc),a1
	jsr	_LVOCause(a6)
	bset	#lfl_inter,ll_flags(a1)
	PLAYINT_POST

int6_playt	dc.l	0,0
	dc.b	NT_INTERRUPT
	dc.b	100	;pri
	dc.l	vssname
	dc.l	0
	dc.l	.code

.code	PLAYINT_PRE
	move.l	excbase(a1),a6	;signal
	move.l	msig(a1),d0
	lea	bufmantask(a1),a1
	jsr	_LVOSignal(a6)
	PLAYINT_POST

; record interrupt, leallas ha cahi-load+blocksperint > maxbuf

RECINT_PRE	MACRO
	;move.w	#$0f00,$dff180
	move.l	address(a1),a5
	btst	#1,_status(a5)
	beq.b	.noint

	move.b	loadcnt(a1),d1	;mennyi hely van meg
.0	move.b	_cahi(a5),d0	;a hardware bufferben?
	IFD	checkptr
	cmp.b	_cahi(a5),d0
	bne.b	.0
	ENDIF
	asl.b	#1,d0
	sub.b	d1,d0
	add.b	blocksperint(a1),d0
	cmp.b	maxbuf(a1),d0	;kitart a kovetkezo
	bcs.b	.ok	;megszakitasig?

	move.b	con(a1),d0	;nem, DMA leallitasa
	and.b	#%11111100,d0
	move.b	d0,con(a1)
	move.b	d0,_con(a5)
	bset	#lfl_hardunder,ll_flags(a1)	;underrun jelzese
.ok	;ki kell hagyni
	ENDM

RECINT_POST	MACRO
	;move.w	#$0009,$dff180
	move.b	#-1,_intack(a5)	;mexakitas torlese
.noint	rts
	ENDM

int6_rec	dc.l	0,0
	dc.b	NT_INTERRUPT
	dc.b	121	;pri
	dc.l	vssname
	dc.l	0
	dc.l	.code

.code	RECINT_PRE
	move.l	excbase(a1),a6	;softint
	lea	softint_rec(pc),a1
	jsr	_LVOCause(a6)
	bset	#lfl_inter,ll_flags(a1)
	RECINT_POST

int6_rect	dc.l	0,0
	dc.b	NT_INTERRUPT
	dc.b	121	;pri
	dc.l	vssname
	dc.l	0
	dc.l	.code

.code	RECINT_PRE
	move.l	excbase(a1),a6	;signal
	move.l	msig(a1),d0
	lea	bufmantask(a1),a1
	jsr	_LVOSignal(a6)
	RECINT_POST

; teszt interrupt rutin, csak a szamlalot noveli

int6_test	dc.l	0,0
	dc.b	NT_INTERRUPT
	dc.b	121	;pri
	dc.l	vssname
	dc.l	0
	dc.l	.code

.code	move.l	address(a1),a5
	btst	#1,_status(a5)
	beq.b	.0
	addq.b	#1,intcnt(a1)
	move.b	#-1,_intack(a5)	;mexakitas torlese
.0	rts

; play softint, bufsiz-(load-cahi) byte toltese
; DMA ujrainditas ha load-cahi > blocksperint

softint_play	dc.l	0,0
	dc.b	NT_INTERRUPT
	dc.b	0	;pri
	dc.l	vssname
	dc.l	0
	dc.l	.code

.code	IFD	makecolor
	move.w	#$00f0,$dff180
	ENDIF
	move.l	address(a1),a5

	move.b	loadcnt(a1),d1
.2	move.b	_cahi(a5),d0
	IFD	checkptr
	cmp.b	_cahi(a5),d0
	bne.b	.2
	ENDIF
	asl.w	#1,d0
	sub.b	d0,d1
	move.b	bufsize(a1),d0
	sub.b	d1,d0
	btst	#lfl_hardunder,ll_flags(a1)
	bne.b	.underrun
.cont	asl.w	#7,d0
	sub.w	remain(a1),d0
	bls.b	.done
	movem.l	a1/a5,-(sp)	;hook hivasa
	move.l	acthook(a1),a0	;a0:hook a5:card
	btst	#lfl_hardunder,ll_flags(a1)	;underrun eseten Z=0
	move.l	h_Entry(a0),a1
	jsr	(a1)
	movem.l	(sp)+,a1/a5
	move.w	d0,d1
	beq.b	.done
	lsr.w	#7,d0
	and.w	#$7f,d1
	add.w	remain(a1),d1
	tst.b	d1
	bpl.b	.0
	and.b	#$7f,d1
	addq.b	#1,d0
.0	move.w	d1,remain(a1)
	add.b	d0,loadcnt(a1)

.done	IFD	makecolor
	move.w	#$0009,$dff180
	ENDIF
	rts

.underrun	cmp.b	blocksperint(a1),d1
	bls.b	.no
	move.b	con2(a1),d1	;eleg, DMA ujrainditasa
	move.b	d1,con(a1)
	move.b	d1,_con(a5)
	bclr	#lfl_hardunder,ll_flags(a1)
.no	bra.b	.cont

; record softint, cahi-load byte atvitele
; DMA ujrainditas ha cahi-load+blocksperint < maxbuf

softint_rec	dc.l	0,0
	dc.b	NT_INTERRUPT
	dc.b	0	;pri
	dc.l	vssname
	dc.l	0
	dc.l	.code

.code	IFD	makecolor
	move.w	#$0f00,$dff180
	ENDIF
	move.l	address(a1),a5

	move.b	loadcnt(a1),d1
.2	move.b	_cahi(a5),d0
	IFD	checkptr
	cmp.b	_cahi(a5),d0
	bne.b	.2
	ENDIF
	asl.w	#1,d0
	sub.b	d1,d0
	btst	#lfl_hardunder,ll_flags(a1)
	bne.b	.overrun
.cont	asl.w	#7,d0
	sub.w	remain(a1),d0
	bls.b	.done
	movem.l	a1/a5,-(sp)	;hook hivasa
	move.l	acthook(a1),a0	;a0:hook a5:card
	btst	#lfl_hardunder,ll_flags(a1)	;underrun eseten Z=0
	move.l	h_Entry(a0),a1
	jsr	(a1)
	movem.l	(sp)+,a1/a5
	move.w	d0,d1
	beq.b	.done
	lsr.w	#7,d0
	and.w	#$7f,d1
	add.w	remain(a1),d1
	tst.b	d1
	bpl.b	.0
	and.b	#$7f,d1
	addq.b	#1,d0
.0	move.w	d1,remain(a1)
	add.b	d0,loadcnt(a1)

.done	IFD	makecolor
	move.w	#$0009,$dff180
	ENDIF
	rts

.overrun	move.b	blocksperint(a1),d1
	add.b	d0,d1
	cmp.b	maxbuf(a1),d1
	bcc.b	.no
	move.b	con2(a1),d1	;eleg, DMA ujrainditasa
	move.b	d1,con(a1)
	move.b	d1,_con(a5)
	bclr	#lfl_hardunder,ll_flags(a1)
.no	bra.b	.cont


;----------------< i2c communication >-------------------------------------------

;i2cRead
;   in:  d0.b dadr
;   out: d0.b data, -1 hiba
;i2cWrite
;   in:  d0.l dadr:map:dat:flg (ha flg=-1 csak a map allitodik be)
;   out: d0 hiba
;i2cMultiR  *NEM MUKODIK*
;   in:  d0.b dadr
;        a0 buffer
;        d4 bytecount
;   out: d0 hiba
;i2cMultiW
;   in:  d0.w dadr:map
;        a0 buffer
;        d4 bytecount
;   out: d0 hiba

i2cRead	bset	#0,d0
	ror.l	#8,d0
	bsr.w	i2cStart
	bsr.w	i2cOut8
	bne.b	i2cErr
	bsr.w	i2cIn8
	bsr.w	i2cStop
	tst.l	d0
	rts
i2cWrite	bclr	#24,d0
	bsr.w	i2cStart
	bsr.b	i2cOut8	;dadr
	bne.b	i2cErr
	bsr.b	i2cOut8	;map
	bne.b	i2cErr
	btst	#23,d0	;flag?
	bne.b	i2cOk
	bsr.b	i2cOut8	;data
	bne.b	i2cErr
i2cOk	bsr.w	i2cStop
	clr.l	d0
	rts
i2cErr	bsr.w	i2cStop
	moveq	#-1,d0
	rts
i2cMultiR	bset	#0,d0
	ror.l	#8,d0
	bsr.w	i2cStart
	bsr.b	i2cOut8
	bne.b	i2cErr
	subq.b	#1,d4
	bmi.b	i2cOk
.0	bsr.w	i2cIn8
	bclr	#0,d2
	move.b	d2,_ser(a4)
	bset	#0,d2
	move.b	d2,_ser(a4)
	move.b	d0,(a0)+
	dbf	d4,.0
	bra.b	i2cOk
i2cMultiW	bclr	#8,d0
	swap	d0
	bsr.w	i2cStart
	bsr.b	i2cOut8
	bne.b	i2cErr
	bsr.b	i2cOut8
	bne.b	i2cErr
	subq.b	#1,d4
	bmi.b	i2cOk
.0	move.b	(a0)+,d0
	ror.l	#8,d0
	bsr.b	i2cOut8
	bne.b	i2cErr
	dbf	d4,.0
	bra.b	i2cOk
i2cOut8	moveq	#7,d3
.0	lsl.l	#1,d0
	roxl.b	#1,d1
	and.b	#1,d1
	bclr	#0,d2
	or.b	d1,d2
	move.b	d2,_ser(a4)
	bset	#1,d2	;scl = 1
	move.b	d2,_ser(a4)
	bclr	#1,d2	;scl = 0
	move.b	d2,_ser(a4)
	dbf	d3,.0
	bset	#0,d2	;sda = 1 ACK
	move.b	d2,_ser(a4)
	bset	#1,d2	;scl = 1
	move.b	d2,_ser(a4)
	move.b	_status(a4),d1	;poll sda
	bclr	#1,d2	;scl = 0
	move.b	d2,_ser(a4)
	and.b	#1,d1
	rts
i2cIn8	moveq	#7,d3
.1	bset	#1,d2	;scl = 1
	move.b	d2,_ser(a4)
	move.b	_status(a4),d1
	lsr.b	#1,d1
	roxl.b	#1,d0
	bclr	#1,d2	;scl = 0
	move.b	d2,_ser(a4)
	dbf	d3,.1
	rts
i2cStart	move.l	address(a5),a4
	move.b	ser(a5),d2
	or.b	#3,d2	;sda,scl = 1
	move.b	d2,_ser(a4)
	bclr	#0,d2	;sda = 0
	move.b	d2,_ser(a4)
	bclr	#1,d2	;scl = 0
	move.b	d2,_ser(a4)
	rts
i2cStop	and.b	#$fc,d2	;sda,scl = 0
	move.b	d2,_ser(a4)
	bset	#1,d2	;scl = 1
	move.b	d2,_ser(a4)
	bset	#0,d2	;sda = 1
	move.b	d2,_ser(a4)
	rts

configDSP	move.b	#$86,d0
	swap	d0
	move.w	(a0)+,d0
	bne.b	.meg
	rts
.meg	asl.l	#8,d0
	bsr.w	i2cWrite
	beq.b	configDSP
	rts


;------------< mixer control via 3-line serial bus >-----------------------------

; 0..9    channels     Chn1..4, Digital
; 10..11  trebfreq     hi..lo
; 12..13  bassfreq     hi..lo
; 14      trebenhance
; 15      bassenhance
; 16      toneatten
; 17      channelmap


updatmix	moveq	#19,d0
.0	bsr.b	setmixer
	dbf	d0,.0
	rts


; In D0:channel

setmixer	movem.l	d0-d5/a0/a4/a6,-(sp)
	lea	mixt(a5),a0
	and.w	#$1f,d0
	move.w	d0,d3
	move.b	(.mixchn,pc,d0.w),d0
	beq.w	.ok
	bpl.w	.ssm
	and.b	#1,d0	;hangero
	move.l	#$2004,d1
	sub.b	d0,d1
	swap	d1	;csatornak csereje
	move.b	(VMT_4l,a0,d0.w),d1
	move.b	(.voltab,pc,d1.w),d1
	asl.w	#8,d1
	move.l	d1,d0
	bsr.w	i2cWrite
	bne.b	.err
	move.w	#$2002,d0	;mute
	swap	d0
	move.w	VMT_switch2(a0),d0
	and.w	#3,d0
	move.b	(.mutab,pc,d0.w),d0
	asl.w	#4,d0
	or.b	conftmp+conf_dacon(a5),d0
	asl.w	#8,d0
	bsr.w	i2cWrite
	beq.w	.ok
.err	moveq	#VSSpkt_PrintFault,d0
	lea	e_iicc(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	bra.w	.ok

.mutab	dc.b	%11,%01,%10,%00

.mixchn	dc.b	2,4,8,6,3,1,7,5,$80,$81
	dc.b	$78,$77,$7a,$79,$7b,$7c,$7d,$46
	dc.b	0,0,0,0,0,0,0,0,0,0,0,0,0,0

.voltab	dc.b	255,226,192,172,158,147,138,130,124,118
	dc.b	113,108,104,100,96,93,90,87,84,81
	dc.b	79,76,74,72,70,68,66,64,62,61
	dc.b	59,57,56,54,53,51,50,49,47,46
	dc.b	45,44,43,41,40,39,38,37,36,35
	dc.b	34,33,32,31,30,29,28,28,27,26
	dc.b	25,24,23,23,22,21,20,20,19,18
	dc.b	17,17,16,15,15,14,13,13,12,12
	dc.b	11,10,10,9,9,8,7,7,6,6
	dc.b	5,5,4,4,3,3,2,1,1,0
	dc.w	0

.voltab3	dc.b	46,39,35,32,30,28,27,25,24,23
	dc.b	22,21,20,20,19,18,18,17,17,16
	dc.b	16,15,15,14,14,13,13,13,12,12
	dc.b	12,11,11,11,10,10,10,10,9,9
	dc.b	9,9,8,8,8,8,8,7,7,7
	dc.b	7,7,6,6,6,6,6,5,5,5
	dc.b	5,5,5,4,4,4,4,4,4,4
	dc.b	3,3,3,3,3,3,3,2,2,2
	dc.b	2,2,2,2,2,2,1,1,1,1
	dc.b	1,1,1,1,1,0,0,0,0,0
	dc.w	0

	even

.sta	cmp.b	#$46,d0
	bne.w	.enh
	move.b	(VMT_channelmap,a0),d0	;channelmap
	add.b	#50,d0
	move.b	(.voltab3,pc,d0.w),d4
	moveq	#100,d1
	sub.w	d0,d1
	move.b	(.voltab3,pc,d1.w),d5
	asl.w	#8,d4
	asl.w	#8,d5
	move.l	#$86460000,d0
	move.w	d5,d0
	bsr.w	i2cWrite
	bne.w	.erro
	move.l	#$86470000,d0
	move.w	d4,d0
	bsr.w	i2cWrite
	bne.w	.erro
	move.l	#$86480000,d0
	move.w	d5,d0
	bsr.w	i2cWrite
	bne.w	.erro
	move.l	#$86490000,d0
	move.w	d4,d0
	bsr.w	i2cWrite
	bne.w	.erro
	bra.b	.ok

.ssm	cmp.b	#10,d3
	bcc.b	.sta
	subq.b	#1,d0
	move.w	VMT_switch1(a0),d1
	asl.w	#1,d3
	lsr.w	d3,d1
	lsr.w	#1,d3
	and.b	#3,d1
	asl.b	#3,d1
	or.b	#128,d1
	or.b	d0,d1
	asl.w	#8,d1
	clr.w	d2
	move.b	(VMT_0l,a0,d3.w),d2
	move.b	(.voltab2,pc,d2.w),d1
	move.l	address(a5),a4
	move.b	ser(a5),d2
	or.b	#%00011100,d2	;sda, scl, sce = 1
	move.b	d2,_ser(a4)
	bsr.w	.0
	bsr.w	.0
.ok	movem.l	(sp)+,d0-d5/a0/a4/a6
	rts

.voltab2	dc.b	63,55,49,45,42,39,37,35,34,32
	dc.b	31,30,29,28,27,26,25,24,23,23
	dc.b	22,21,21,20,19,19,18,18,17,17
	dc.b	16,16,16,15,15,14,14,14,13,13
	dc.b	12,12,12,11,11,11,11,10,10,10
	dc.b	9,9,9,9,8,8,8,8,7,7
	dc.b	7,7,6,6,6,6,6,5,5,5
	dc.b	5,5,4,4,4,4,4,3,3,3
	dc.b	3,3,3,2,2,2,2,2,2,1
	dc.b	1,1,1,1,1,1,0,0,0,0
	dc.w	0

	even

.enh	move.b	d0,d1
	move.l	#$8600,d0
	move.b	d1,d0
	swap	d0
	move.b	(VMT_0l,a0,d3.w),d0
	asl.w	#8,d0
	bsr.w	i2cWrite
	beq.w	.ok
.erro	moveq	#VSSpkt_PrintFault,d0
	lea	e_iicd(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	bra.w	.ok

.0	and.b	#%11100111,d2	;scl, sce = 0
	move.b	d2,_ser(a4)
	moveq	#7,d3
.1	asl.w	#1,d1
	roxl.b	#3,d0
	and.b	#4,d0
	and.b	#%11111011,d2
	or.b	d0,d2	;sda = data
	move.b	d2,_ser(a4)
	or.b	#%00001000,d2	;scl = 1
	move.b	d2,_ser(a4)
	and.b	#%11110111,d2	;scl = 0
	move.b	d2,_ser(a4)
	dbf	d3,.1
	or.b	#%00011100,d2	;sda, scl, sce = 1
	move.b	d2,_ser(a4)
	rts


;-----------< read configuration >-----------------------------------------------

readconf	move.l	dosbase(a5),a6
	lea	conftmp(a5),a2

	lea	f_conf(pc),a0
	move.l	a0,d1
	move.l	#MODE_OLDFILE,d2
	jsr	_LVOOpen(a6)
	move.l	d0,d4
	move.l	d0,d1
	beq.b	.derror
	move.l	a2,d2
	move.l	#conf_size,d3
	jsr	_LVORead(a6)
	move.l	d0,d2
	move.l	d4,d1
	jsr	_LVOClose(a6)
	tst.l	d2
	bmi.b	.derror
	cmp.l	#conf_size,d2	;check size
	bne.b	.error
	cmp.l	#'VSSC',(a2)	;check ID string
	bne.b	.error
	lea	e_obsconf(pc),a0
	cmp.w	#confver,conf_version(a2)
	bne.b	.err
	clr.w	d2	;calc chksum
	lea	conf_chksum(a2),a0
	move.l	#(conf_size-conf_chksum)/2-1,d0
.2	add.w	(a0)+,d2
	dbf	d0,.2
	tst.w	d2	;match?
	bne.b	.error
	lea	i_conf(pc),a0
	bsr.w	printInfo
	bra.b	.fileok

.derror	bsr.w	doserror
.error	lea	e_conf(pc),a0
.err	bsr.w	printFault
	move.l	excbase(a5),a6
	lea	defconf(pc),a0
	move.l	a2,a1
	move.l	#conf_size,d0
	jsr	_LVOCopyMem(a6)
	bsr.w	writedefconf
.fileok	move.w	conf_sercon(a2),d0
	move.b	d0,ser(a5)
	move.w	conf_debuglevel(a2),d0
	move.b	d0,debuglevel(a5)
	move.w	conf_quantum(a2),d0
	move.w	d0,quantum+2(a5)
	mulu.w	#1000,d0
	move.l	d0,quantum2(a5)
	move.l	excbase(a5),a6	;copy channel names
	lea	conf_names(a2),a0
	lea	cnames(a5),a1
	moveq	#cnames_size,d0
	jsr	_LVOCopyMem(a6)
	lea	conf_modes(a2),a0
	lea	gui_modes(a5),a1
	moveq	#cmodes_size,d0
	jsr	_LVOCopyMem(a6)
	lea	conf_mixer(a2),a0	;copy mixer tables
	lea	mixtab(a5),a1
	moveq	#VMT_SIZE,d0
	jsr	_LVOCopyMem(a6)
	lea	conf_mixer(a2),a0
	lea	mixt(a5),a1
	moveq	#VMT_SIZE,d0
	jmp	_LVOCopyMem(a6)

defconf	dc.l	'VSSC'
	dc.w	confver
	dc.w	0	;chksum
	dc.l	0	;addr
	dc.l	128*1024	;mpbufsize
	dc.l	16*1024	;mpchunk
	dc.l	32*1024	;mpmaxchunk
	dc.l	1100	;mptimecorr
	dc.l	200	;mpfakecnt
	dc.l	150	;ahibuf ms
	dc.l	24	;melbuf k
	dc.l	128	;playbufsize [k]
	dc.l	128	;sambufsize [k]
	dc.l	8254;9600!	;irbaud
	dc.w	40	;irlen
	dc.w	20	;quantum (bufman)
	dc.w	50	;guiquantum
	dc.w	0	;pri
	dc.w	1	;spri
	dc.w	$1f	;sercon
	dc.w	1	;debuglevel
	dc.l	$0001fb28	;flags
	dc.b	9	;dacon
	dc.b	0	;adcon
	dc.b	4	;samdeclev
	dc.b	1	;levdiv
	dc.b	'Digital',0,0,0,0,0
	dc.b	'Amiga',0,0,0,0,0,0,0
	dc.b	'PC',0,0,0,0,0,0,0,0,0,0
	dc.b	'CD audio',0,0,0,0
	dc.b	0,0,0,0,0,0,0,0,0,0,0,0
	dc.w	%1001100110011001	;switch1
	dc.w	%0000000000000011	;switch2
	dc.b	75,75	;Chn1
	dc.b	0,0	;Chn2
	dc.b	25,25	;Chn3
	dc.b	0,0	;Chn4
	dc.b	25,25	;Digital
	dc.w	3000,250	;trebf,bassf
	dc.b	3,3	;trebe,basse
	dc.b	3,-50	;toneatten,chnmap
	dc.w	0,0,0,0,0	;cmodes
.0	dc.b	$02,$02,$40,$5c,$61,$70,$ae,$b5,$c5
	dc.b	$d1,$81,$08,$10,$28,0
.1
.s	equ	.1-.0
	dcb.b	100-.s,0

; Ha veletlenul rosszul toltottuk ki a strukturat, forditas leallitasa
	IFEQ	*-defconf=conf_size
	printt	'** config file size mismatch'
	cfail
	ENDIF


;	STRUCTURE	conf,0
;	ULONG	conf_id	;'VSSC'
;	UWORD	conf_version
;	UWORD	conf_chksum	;file checksum
;
;	ULONG	conf_addr	;address
;	ULONG	conf_mpbufsize
;	ULONG	conf_mpchunk
;	ULONG	conf_mpmaxchunk
;	ULONG	conf_mptimecorr
;	ULONG	conf_mpfakecnt
;	ULONG	conf_ahibuftime
;	ULONG	conf_melbuftime
;	ULONG	conf_playbufsize
;	ULONG	conf_sambufsize
;	ULONG	conf_irbaud
;	UWORD	conf_irlen
;	UWORD	conf_quantum
;	UWORD	conf_guiquantum
;	UWORD	conf_pri
;	UWORD	conf_spri
;	UWORD	conf_sercon	;external addr...
;	UWORD	conf_debuglevel
;	ULONG	conf_flags
;	UBYTE	conf_dacon
;	UBYTE	conf_adcon
;	UBYTE	conf_samdeclev
;	UBYTE	conf_levdiv
;	;UBYTE	conf_kludgefill
;	ALIGNWORD
;	STRUCT	conf_names,cnames_size
;	STRUCT	conf_mixer,VMT_SIZE
;	STRUCT	conf_modes,cmodes_size
;	STRUCT	conf_irinit,100
;	LABEL	conf_size

	even

writedefconf	moveq	#TRUE,d3
	bra.b	wc_def
writeconf	moveq	#FALSE,d3
wc_def	move.l	dosbase(a5),a6
	lea	f_conf(pc),a0
	move.l	a0,d1
	move.l	#MODE_NEWFILE,d2
	jsr	_LVOOpen(a6)
	move.l	d0,d4
	beq.w	.derror

	lea	conftmp(a5),a2
	move.l	defconf(pc),(a2)	;copy ID
	tst.l	d3
	bne.b	.3

	move.l	address(a5),conf_addr(a2)
	move.w	quantum+2(a5),conf_quantum(a2)
	clr.l	d0
	move.b	ser(a5),d0
	move.w	d0,conf_sercon(a2)
	move.b	debuglevel(a5),d0
	move.w	d0,conf_debuglevel(a2)
	move.l	excbase(a5),a6
	lea	gui_modes(a5),a0
	lea	conf_modes(a2),a1
	moveq	#cmodes_size,d0
	jsr	_LVOCopyMem(a6)
	lea	cnames(a5),a0
	lea	conf_names(a2),a1
	moveq	#cnames_size,d0
	jsr	_LVOCopyMem(a6)
	lea	mixt(a5),a0
	lea	conf_mixer(a2),a1
	moveq	#VMT_SIZE,d0
	jsr	_LVOCopyMem(a6)
.3	clr.w	d2	;calc chksum
	lea	conf_chksum(a2),a0
	move.l	#(conf_size-conf_chksum)/2-1,d0
.2	add.w	(a0)+,d2
	dbf	d0,.2
	sub.w	d2,conf_chksum(a2)
	move.l	d4,d1	;write
	move.l	a2,d2
	move.l	#conf_size,d3
	move.l	dosbase(a5),a6
	jsr	_LVOWrite(a6)
	move.l	d0,d2
	move.l	d4,d1
	jsr	_LVOClose(a6)
	tst.l	d0
	beq.b	.derror
	tst.l	d2
	bmi.b	.derror
	lea	i_confw(pc),a0
	bra.w	printInfo
.derror	bsr.w	doserror
.error	lea	e_confw(pc),a0
	bra.w	printFault


;----------< vss.library functions >---------------------------------------------

; user functions

;AllocHandle	A0:name		D0:handle
;FreeHandle	A0:handle
;AllocAudio	A0:handle		D0:null/handle/-1
;FreeAudio	A0:handle
;AddMixerNotify	A0:handle D0:signal	D0:notify
;RemMixerNotify	A0:notify
;RefreshMixer	A0:handle
;Init	A0:handle A1:tags	D0:success
;Start	A0:handle
;Stop	A0:handle
;Flush	A0:handle
;Buffered	A0:handle		D0:buffered
;FindFreq	D0:req		D0:closest
;OwnCard	A0:handle D0:address	D0:success
;DisownCard	A0:handle		D0:success

;Interface
;StealAudio

	STRUCTURE	vsshandle,LN_SIZE
	APTR	vssh_port
	APTR	vssh_task
	APTR	vssh_stolen
	LABEL	vssh_size

	STRUCTURE	vssnotify,MLN_SIZE
	APTR	vssn_handle
	APTR	vssn_sigtask
	ULONG	vssn_signal
	ULONG	vssn_channels
	ULONG	vssn_channelchg
	;ULONG	vssn_flags
	LABEL	vssn_size

	STRUCTURE	vssstolen,0
	APTR	vsss_handle
	UBYTE	vsss_running
	ALIGNWORD
	LABEL	vssstolen_size


; In A0:name  Out D0:vssHandle

vssAllocHandle	movem.l	d2/a2/a5-a6,-(sp)
	move.l	var(pc),a5
	move.l	excbase(a5),a6
	move.l	a0,d2
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_allochand(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	lea	poolsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	moveq	#vssh_size,d0	;memory
	move.l	pool1(a5),a0
	jsr	_LVOAllocPooled(a6)
	lea	poolsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	tst.l	d0
	beq.b	.err
	move.l	d0,a2
	move.l	d2,LN_NAME(a2)
	clr.l	vssh_stolen(a2)
	move.b	#NT_USER,LN_TYPE(a2)
	clr.b	LN_PRI(a2)
	jsr	_LVOCreateMsgPort(a6)	;port
	move.l	d0,vssh_port(a2)
	beq.b	.err2
	sub.l	a1,a1	;task
	jsr	_LVOFindTask(a6)
	move.l	d0,vssh_task(a2)
	lea	handsem(a5),a0	;enqueue
	jsr	_LVOObtainSemaphore(a6)
	lea	handlist(a5),a0
	move.l	a2,a1
	bsr.w	abcEnqueue
	lea	handsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	move.l	a2,d0
	bra.b	.err
.err2	move.l	a2,a1	;error, free mem
	lea	poolsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	move.l	pool1(a5),a0
	move.l	#vssh_size,d0
	jsr	_LVOFreePooled(a6)
	lea	poolsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	clr.l	d0
.err	movem.l	(sp)+,d2/a2/a5-a6
	rts

; In A0:vssHandle

vssFreeHandle	movem.l	d2/a2/a5-a6,-(sp)
	move.l	var(pc),a5
	move.l	excbase(a5),a6
	move.l	a0,a2
	move.l	a0,d0
	beq.w	.nohand
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_freehand(pc),a0
	move.l	a0,d1
	move.l	LN_NAME(a2),d2
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt

	move.l	vssh_stolen(a2),d0
	beq.b	.nosto
	lea	stolensem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	move.l	vssh_stolen(a2),a1
	jsr	_LVORemove(a6)
	lea	stolensem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	lea	poolsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	move.l	pool1(a5),a0
	move.l	vssh_stolen(a2),a1
	moveq	#vssstolen_size,d0
	jsr	_LVOFreePooled(a6)
	lea	poolsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
.nosto	lea	handsem(a5),a0	;remove from list
	jsr	_LVOObtainSemaphore(a6)
	move.l	a2,a1
	jsr	_LVORemove(a6)
	lea	handsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	move.l	vssh_port(a2),a0	;delete port
	jsr	_LVODeleteMsgPort(a6)
	move.l	a2,a1	;free mem
	lea	poolsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	move.l	pool1(a5),a0
	move.l	#vssh_size,d0
	jsr	_LVOFreePooled(a6)
	lea	poolsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
.nohand	movem.l	(sp)+,d2/a2/a5-a6
	rts

; In A0:handle D0:signal D1:channels  Out D0:notify

vssAddMixerNotify
	movem.l	d2-d4/a2/a5-a6,-(sp)
	move.l	var(pc),a5
	move.l	excbase(a5),a6
	move.l	d0,d3
	beq.b	.err
	move.l	d1,d4
	beq.b	.err
	move.l	a0,d2
	beq.b	.err
	move.l	pool1(a5),a0
	moveq	#vssn_size,d0
	bsr.w	AllocVecPooled
	move.l	d0,a2
	tst.l	d0
	beq.b	.err
	move.l	d2,vssn_handle(a2)
	move.l	d2,a0
	move.l	vssh_port(a0),a0
	move.l	MP_SIGTASK(a0),vssn_sigtask(a2)
	move.l	d3,vssn_signal(a2)
	move.l	d4,vssn_channels(a2)
	;clr.l	vssn_flags(a2)
	lea	notifysem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	lea	notifylist(a5),a0
	move.l	a2,a1
	jsr	_LVOAddTail(a6)
	lea	notifysem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	move.l	a2,d0
	bra.b	.ok
.err	clr.l	d0
.ok	movem.l	(sp)+,d2-d4/a2/a5-a6
	rts

; In A0:notify

vssRemMixerNotify
	movem.l	a2/a5-a6,-(sp)
	move.l	var(pc),a5
	move.l	excbase(a5),a6
	move.l	a0,a2
	move.l	a0,d0
	beq.b	.nono
	lea	notifysem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	move.l	a2,a1
	jsr	_LVORemove(a6)
	lea	notifysem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	move.l	pool1(a5),a0
	move.l	a2,a1
	bsr.w	FreeVecPooled
.nono	movem.l	(sp)+,a2/a5-a6
	rts

; In A0:handle D0:async

vssRefreshMixer	movem.l	d2-d3/a2-a3/a5-a6,-(sp)
	move.l	var(pc),a5
	move.l	lport(a5),a0
	tst.l	d0
	bne.b	.async
	move.l	vssh_port(a0),a1
	moveq	#VSSpkt_RefreshMixer,d0
	bsr.w	doPkt
	bra.b	.ok
.async	moveq	#VSSpkt_RefreshMixer,d0
	sub.l	a1,a1
	bsr.w	sendPkt
.ok	movem.l	(sp)+,d2-d3/a2-a3/a5-a6
	rts

; In A0:handle  Out D0:handle/null/-1
; (0:ok, -1:error, other:current users's handle)

vssAllocAudio	move.l	a5,-(sp)
	move.l	var(pc),a5
	move.l	a0,-(sp)
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_allocaudio(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	(sp)+,a0
	moveq	#-1,d0
	btst	#lfl_hardok,ll_flags(a5)
	beq.b	.ok
	move.l	acthandle(a5),d0
	bne.b	.ok
	move.l	a0,acthandle(a5)
	clr.l	d0
.ok	move.l	(sp)+,a5
	rts

; In A0:handle

vssFreeAudio	movem.l	a4-a6,-(sp)
	move.l	var(pc),a5

	movem.l	d2/a0,-(sp)
	moveq	#VSSpkt_PrintDebug,d0	;debug
	move.l	LN_NAME(a0),d2
	lea	d_freeaudio(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	movem.l	(sp)+,d2/a0

	cmp.l	acthandle(a5),a0
	bne.b	.0

	bsr.w	intdis
	clr.l	acthandle(a5)
	move.l	address(a5),a4
	clr.b	_con(a4)
	clr.b	con(a5)

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

vssStealAudio	rts


; In D0:req  Out D0:closest (D1:index)

vssFindFreq	bsr.w	findfreq
	exg	d0,d1
	rts

; In A0:handle A1:tags  Out D0:success

	STRUCTURE	vssini_tags,0
	ULONG	vssini_bits	;0,8,16,20,24 bit
	ULONG	vssini_freq	;szabvanyos frekik
	APTR	vssini_hook	;struct hook
	ULONG	vssini_mode	;0 play, 1 rec
	ULONG	vssini_maxbuf	;bytes
	ULONG	vssini_channels	;1,2
	ULONG	vssini_task	;BOOL
	ULONG	vssini_emphasis	;BOOL
	LABEL	vssini_size

vssInit	movem.l	d2/a2/a5-a6,-(sp)
	move.l	var(pc),a5

	move.l	a1,a0

	move.l	utibase(a5),a6
	move.l	a0,a2
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_initstream(pc),a0
	move.l	a0,d1
	move.l	acthandle(a5),a0
	move.l	LN_NAME(a0),d2
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt

	lea	-vssini_size(sp),sp
	move.l	sp,a0
	move.l	a0,a1
	move.l	#16,(a1)+	;bits
	move.l	#44100,(a1)+	;freq
	clr.l	(a1)+	;hook
	clr.l	(a1)+	;play
	clr.l	d0
	move.b	maxbuf(a5),d0
	asl.l	#7,d0
	move.l	d0,(a1)+	;buf (default: max)
	move.l	#2,(a1)+	;stereo
	clr.l	(a1)+	;SoftInt transfer
	clr.l	(a1)+	;emphasis
	lea	.packtable(pc),a1
	jsr	_LVOPackStructureTags(a6)
	tst.l	vssini_task(sp)	;task/softint
	sne	tasktransfer(a5)
	moveq	#%1100,d2	;bits
	move.l	(sp),d0
	beq.b	.bitok	;mp3
	clr.l	d2
	subq.l	#8,d0
	beq.b	.bitok	;8bit
	moveq	#%0100,d2
	subq.l	#8,d0
	beq.b	.bitok	;16bit
	moveq	#%1000,d2
	subq.l	#4,d0
	beq.b	.bitok	;20bit
	subq.l	#4,d0
	bne.w	.error	;ha nem 24bit, hiba!
.bitok	moveq	#1,d1
	bclr	#5,ser(a5)
	move.l	vssini_mode(sp),d0	;mode
	beq.b	.modeok
	bset	#5,ser(a5)
	moveq	#2,d1
	subq.l	#1,d0
	bne.w	.error
	tst.l	(sp)	;mp3+record illegalis
	beq.w	.error
.modeok	or.b	d1,d2
	move.l	vssini_channels(sp),d0
	subq.l	#2,d0
	beq.b	.chnok
	addq.l	#1,d0
	bne.w	.error
	btst	#3,d2	;24bit/mp3 + mono illegal
	bne.w	.error
	or.b	#%00010000,d2	;mono
.chnok	move.l	vssini_maxbuf(sp),d0
	lsr.l	#7,d0
	clr.l	d1
	move.b	maxbuf(a5),d1	;ha nagyobb mint maxbuf
	cmp.l	d1,d0
	bcs.b	.0
	move.l	d1,d0	;=maxbuf
.0	move.b	blocksperint(a5),d1	;ha kisebb mint blox/int
	asl.b	#1,d1	;duplaja!!!
	cmp.l	d1,d0
	bcc.b	.1
	move.l	d1,d0	;=blocksperint
.1	move.b	d0,bufsize(a5)

	movem.l	d0-d2,-(sp)	;debug (hard buffer size)
	lea	d_lowmaxbuf(pc),a0
	asl.l	#7,d0
	move.l	d0,d2
	move.l	a0,d1
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	movem.l	(sp)+,d0-d2

	move.l	vssini_hook(sp),acthook(a5)	;ha nincs hook, hiba!
	beq.b	.error
	bclr	#lfl_deemp,ll_flags(a5)	;de-emphasis
	tst.l	vssini_emphasis(sp)
	beq	.22
	move.l	conftmp+conf_flags(a5),d0	;engedelyezett?
	btst	#cflg_deemphasis,d0
	beq	.22
	bset	#lfl_deemp,ll_flags(a5)
.22	move.l	vssini_freq(sp),d1	;set freq
	tst.l	(sp)
	bne.b	.2
	bset	#31,d1	;ha mp3, MSB=1
.2	moveq	#VSSpkt_SetFreq,d0
	move.l	lport(a5),a0
	move.l	acthandle(a5),a1
	move.l	vssh_port(a1),a1
	bsr.w	doPkt
	tst.l	d0
	beq.b	.error
	or.b	freqdiv(a5),d2
	or.b	#%10100000,d2
	move.b	d2,con2(a5)
	clr.b	con(a5)
	clr.b	loadcnt(a5)
	clr.w	remain(a5)
	tst.l	vssini_mode(sp)	;add interrupt vector
	beq.b	.play
	bsr.w	intenrec
	bra.b	.intok
.play	bsr.w	intenplay
.intok	moveq	#TRUE,d0
	bra.b	.ok
.error	clr.l	d0
.ok	lea	vssini_size(sp),sp

	movem.l	(sp)+,d2/a2/a5-a6
	rts

.packtable	dc.l	VSS_Dummy
	dc.w	$1000!(VSS_bits-VSS_Dummy)
	dc.w	vssini_bits-vssini_bits
	dc.w	$1000!(VSS_freq-VSS_Dummy)
	dc.w	vssini_freq-vssini_bits
	dc.w	$1000!(VSS_hook-VSS_Dummy)
	dc.w	vssini_hook-vssini_bits
	dc.w	$1000!(VSS_mode-VSS_Dummy)
	dc.w	vssini_mode-vssini_bits
	dc.w	$1000!(VSS_maxbuf-VSS_Dummy)
	dc.w	vssini_maxbuf-vssini_bits
	dc.w	$1000!(VSS_channels-VSS_Dummy)
	dc.w	vssini_channels-vssini_bits
	dc.w	$1000!(VSS_task-VSS_Dummy)
	dc.w	vssini_task-vssini_bits
	dc.w	$1000!(VSS_emphasis-VSS_Dummy)
	dc.w	vssini_emphasis-vssini_bits
	dc.l	0

; In A0:handle

vssStart	movem.l	a4-a6,-(sp)
	move.l	var(pc),a5
;	move.l	a0,-(sp)
;	moveq	#VSSpkt_PrintDebug,d0	;debug
;	lea	d_start(pc),a0
;	move.l	a0,d1
;	move.l	cport(a5),a0
;	sub.l	a1,a1
;	bsr.w	sendPkt
;	move.l	(sp)+,a0

	cmp.l	acthandle(a5),a0
	beq.b	.nosto
	move.l	vssh_stolen(a0),d0
	beq.b	.ok
	move.l	d0,a0
	st	vsss_running(a0)
	bra.b	.ok

.nosto	move.l	address(a5),a4
	move.b	ser(a5),_ser(a4)

	tst.b	con(a5)	;csak ujrainditas?
	bne.b	.st
	clr.b	loadcnt(a5)	;nem, reset counters
	clr.w	remain(a5)
	clr.b	_con(a4)
	move.b	con2(a5),d0
	and.b	#%11011100,d0
	move.b	d0,_con(a4)
.st	btst	#1,con2(a5)	;play/record
	beq.b	.read

	clr.b	(a4)	;adjust rec format
	bra.b	.st2

.read	move.l	vssh_port(a0),a1	;poll inditasa
	move.l	mport(a5),a0
	moveq	#VSSpkt_StartSlave,d0
	bsr.w	doPkt
.st2	move.b	con2(a5),d0	;DMA inditasa
	move.b	d0,con(a5)
	move.b	d0,_con(a4)
.ok	movem.l	(sp)+,a4-a6
	rts


; In A0:handle

vssStop	move.l	a5,-(sp)
	move.l	var(pc),a5

;	move.l	a0,-(sp)
;	moveq	#VSSpkt_PrintDebug,d0	;debug
;	lea	d_stop(pc),a0
;	move.l	a0,d1
;	move.l	cport(a5),a0
;	sub.l	a1,a1
;	bsr.w	sendPkt
;	move.l	(sp)+,a0

	cmp.l	acthandle(a5),a0
	beq.b	.nosto
	move.l	vssh_stolen(a0),d0
	beq.b	.ok
	move.l	d0,a0
	sf	vsss_running(a0)
	bra.b	.ok

.nosto	;bclr	#lfl_poll,ll_flags(a5)

	move.l	vssh_port(a0),a1	;megvarjuk amig
	move.l	mport(a5),a0	;befejezi
	moveq	#VSSpkt_StopSlave,d0
	bsr.w	doPkt

	move.l	address(a5),a1	;DMA leallitas
	move.b	con(a5),d0
	and.b	#%11011100,d0
	move.b	d0,_con(a1)
	move.b	d0,con(a5)

.ok	move.l	(sp)+,a5
	rts

; In A0:handle

vssFlush	move.l	var(pc),a1
	cmp.l	acthandle(a1),a0
	bne.b	.ok
	move.l	address(a1),a0
	clr.b	_con(a0)
	clr.b	con(a1)
	clr.b	loadcnt(a1)
	clr.w	remain(a1)
.ok	rts

; In A0:handle  Out D0:bytes in the hardware cache

vssBuffered	movem.l	a4-a5,-(sp)
	move.l	var(pc),a5
	cmp.l	acthandle(a5),a0
	beq.b	.nosto
	clr.l	d0
	bra.b	.ok
.nosto	move.l	address(a5),a4
	clr.l	d0
	move.b	loadcnt(a5),d0
.2	move.b	_cahi(a4),d1
	IFD	checkptr
	cmp.b	_cahi(a4),d1
	bne.b	.2
	ENDIF
	asl.w	#1,d1
	sub.b	d1,d0
	asl.w	#7,d0
	;move.b	con2(a5),d1	;ha mp3 mode,
	;lsr.b	#2,d1
	;and.b	#3,d1
	;cmp.b	#3,d1
	;bne	.0
	;add.w	#5000,d0	;DSP buffer hozzaadasa
.0	;add.w	remain(a5),d0
.ok	movem.l	(sp)+,a4-a5
	rts

; In D0:address A0:handle  Out D0:success

vssOwnCard	movem.l	d2-d3/a5-a6,-(sp)
	move.l	var(pc),a5
	clr.l	d3
	bset	#lfl_busy,ll_flags(a5)
	bne.b	.err2
	move.l	d0,d1
	beq.b	.err
	move.l	vssh_port(a0),d2

	moveq	#VSSpkt_OwnCard,d0
	move.l	lport(a5),a0
	move.l	d2,a1
	bsr.w	doPkt

	moveq	#VSSpkt_InitCard,d0
	move.l	lport(a5),a0
	move.l	d2,a1
	bsr.w	doPkt
	move.l	d0,d3
	beq.b	.baj	;ok

	moveq	#VSSpkt_SetFreq,d0
	move.l	#44100,d1
	move.l	lport(a5),a0
	move.l	d2,a1
	bsr.w	doPkt
	move.l	d0,d3
	bne.b	.err

.baj	moveq	#VSSpkt_DisownCard,d0	;hiba, disown()
	move.l	lport(a5),a0
	move.l	d2,a1
	bsr.w	doPkt

.err	bclr	#lfl_busy,ll_flags(a5)
.err2	move.l	d3,d0
	movem.l	(sp)+,d2-d3/a5-a6
	rts

; In A0:handle  Out D0:null for failure

vssDisownCard	movem.l	d2/a5-a6,-(sp)
	move.l	var(pc),a5
	clr.l	d0
	bset	#lfl_busy,ll_flags(a5)
	bne.b	.err2
	tst.l	acthandle(a5)
	bne.b	.err
	move.l	vssh_port(a0),a1
	moveq	#VSSpkt_DisownCard,d0
	move.l	lport(a5),a0
	bsr.w	doPkt
	moveq	#-1,d0	;nincs hiba
.err	bclr	#lfl_busy,ll_flags(a5)
.err2	movem.l	(sp)+,d2/a5-a6
	rts

; In D0:req  Out D0:closest_index D1:closest_freq

findfreq	movem.l	d4-d7,-(sp)
	move.l	d0,d1
	moveq	#-1,d6	;index
	moveq	#-1,d4	;diff
	lea	freqtab(pc),a0
.i1	addq.l	#1,d6
	move.l	d1,d7
	move.l	(a0,d6.w*4),d5
	beq.b	.i2
	sub.l	d5,d7
	bpl.b	.i0
	neg.l	d7
.i0	cmp.l	d4,d7
	bcc.b	.i1
	move.l	d7,d4
	move.l	d6,d0
	bra.b	.i1
.i2	move.l	(a0,d0.w*4),d1
	movem.l	(sp)+,d4-d7
	rts

vssInterface	clr.l	d0
	rts


;----------< vss.audio functions >-----------------------------------------------

; user functions

ahisub_SetFreq
ahisub_SetVol
ahisub_SetSound
ahisub_SetEffect
ahisub_LoadSound
ahisub_UnloadSound
	moveq	#AHIS_UNKNOWN,d0
	rts

ahisub_HardwareControl
	clr.l	d0
	rts

; In A1:tags A2:audioctrl  Out D0:flags

	STRUCTURE	ahialloc,0
	ULONG	ahialloc_bits
	ULONG	ahialloc_stereo
	ULONG	ahialloc_hifi
	LABEL	ahialloc_size

ahisub_AllocAudio
	movem.l	d2/d7/a3/a5-a6,-(sp)
	move.l	var(pc),a5

	move.l	a1,-(sp)
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_ahialloc(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	(sp)+,a1

	move.l	conftmp+conf_flags(a5),d0	;enabled?
	btst	#cflg_ahiena,d0
	beq.w	.err
	bset	#ahif_busy,ahi_flags(a5)	;busy?
	bne.w	.err
	tst.l	ahiac_DriverData(a2)
	bne.w	.err
	move.l	a1,d2

	move.l	excbase(a5),a6	;open vss
	lea	vssname(pc),a1
	moveq	#vssver,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,a6
	tst.l	d0
	beq.w	.error
	bset	#ahif_vssopen,ahi_flags(a5)
	lea	t_ahidrv(pc),a0	;alloc handle
	jsr	_LVOvssAllocHandle(a6)
	move.l	d0,ahi_handle(a5)
	beq.w	.error
	move.l	d0,a0	;alloc channels
	jsr	_LVOvssAllocAudio(a6)
	tst.l	d0
	bne.w	.error2

	move.l	conftmp+conf_ahibuftime(a5),ahi_buffer(a5)
	lea	ahitags(pc),a3
	move.l	ahiac_MixFreq(a2),d0	;update frequency
	bsr.w	findfreq
	move.l	d1,ahiac_MixFreq(a2)
	move.l	d1,ahit_freq-ahitags(a3)
	move.l	#1,ahiac_DriverData(a2)
	moveq	#AHISF_CANRECORD!AHISF_MIXING!AHISF_TIMING,d7

	move.l	utibase(a5),a6	;examine tags
	lea	-ahialloc_size(sp),sp
	move.l	sp,a0
	lea	.packtable(pc),a1
	exg	d2,a2
	jsr	_LVOPackStructureTags(a6)
	exg	d2,a2
	bclr	#ahif_hifi,ahi_flags(a5)
	tst.l	ahialloc_hifi(sp)
	beq.b	.0
	or.b	#AHISF_KNOWHIFI,d7
	bset	#ahif_hifi,ahi_flags(a5)
.0	moveq	#1,d0
	tst.l	ahialloc_stereo(sp)
	beq.b	.1
	or.b	#AHISF_KNOWSTEREO,d7
	moveq	#2,d0
.1	move.l	ahialloc_bits(sp),d1
	move.l	d1,ahit_bits-ahitags(a3)
	cmp.l	#20,d1	;24 bit modban nincs
	bcs.b	.2	;hardveres mono support,
	moveq	#2,d0	;majd a hook konvertal
	eor.b	#AHISF_CANRECORD,d7
.2	move.l	d0,ahit_channels-ahitags(a3)
	lea	ahialloc_size(sp),sp

	bsr.b	ahisub_Update
	bra.b	.exit

.error2	move.l	ahi_handle(a5),a0
	jsr	_LVOvssFreeHandle(a6)
	clr.l	ahi_handle(a5)
.error	bclr	#ahif_busy,ahi_flags(a5)
.err	moveq	#AHISF_ERROR,d7
.exit	move.l	d7,d0
	movem.l	(sp)+,d2/d7/a3/a5-a6
	rts

.packtable	dc.l	AHI_TagBase
	dc.w	$1000!(AHIDB_VSSBits-AHI_TagBase)
	dc.w	ahialloc_bits-ahialloc_bits
	dc.w	$1000!(AHIDB_Stereo-AHI_TagBase)
	dc.w	ahialloc_stereo-ahialloc_bits
	dc.w	$1000!(AHIDB_HiFi-AHI_TagBase)
	dc.w	ahialloc_hifi-ahialloc_bits
	dc.l	0

; In D0:flags A2:audioctrldrv

; bytes/sample:	8bit	1
;	16bit	2
;	20/24bit	3
; stereo		*2
;
; mixedbytes = buffsamples * mult

ahisub_Update	movem.l	d2/a3/a5,-(sp)
	move.l	var(pc),a5
	lea	ahitags(pc),a3
	lea	ahih_8bit(pc),a0	;hook
	moveq	#1,d0	;bytes/sample
	move.l	ahit_bits-ahitags(a3),d1
	subq.l	#8,d1
	beq.b	.bit
	lea	ahih_16bit(pc),a0
	btst	#ahif_hifi,ahi_flags(a5)
	beq.b	.0
	lea	ahih_h16bit(pc),a0
.0	addq.b	#1,d0
	subq.l	#8,d1
	beq.b	.bit
	addq.b	#1,d0
	lea	ahih_h24bit(pc),a0
.bit	move.l	ahiac_Flags(a2),d1	;channels
	and.b	#AHIACF_STEREO,d1
	beq.b	.mono
	addq.l	#4,a0
	asl.l	d0
	bra.b	.chnok
.mono	cmp.l	#20,ahit_bits-ahitags(a3)	;nincs hardveres mono
	bcs.b	.chnok	;24 biten, szoftveresen
	asl.l	d0	;konvertalunk
.chnok	move.l	conftmp+conf_flags(a5),d1	;byte/word
	tst.l	ahit_mode-ahitags(a3)	;mode
	beq.b	.1
	lea	ahih_rechooks-ahih_8bit(a0),a0
	btst	#cflg_byterd,d1
	bne.b	.2
.3	lea	ahih_opthooks-ahih_8bit(a0),a0
	bra.b	.2
.1	btst	#cflg_bytewr,d1
	beq.b	.3
.2	lea	ahi_hook(a5),a1	;store hook entry
	move.l	(a0),h_Entry(a1)
	move.l	a1,ahit_hook-ahitags(a3)
	move.l	d0,ahi_mult(a5)
	move.l	ahiac_BuffSamples(a2),d1	;force even number
	and.w	#$fffe,d1	;of samples
	move.l	d1,ahiac_BuffSamples(a2)

	tst.l	ahit_mode-ahitags(a3)
	beq.b	.play

	move.l	#ahi_recbuffer,d1
	cmp.l	#8,ahit_bits-ahitags(a3)
	bne.b	.4
	lsr.l	d1
.4	move.l	d1,ahi_mixedbytes(a5)
	move.l	ahiac_SamplerFunc(a2),a0	;update sampler regs
	lea	ahi_recmsg(a5),a1
	move.l	h_Entry(a0),a3
	movem.l	a0-a3,ahi_samplerregs(a5)
	move.l	#AHIST_S16S,ahirm_Type(a1)	;prepare RecordMessage
	move.l	ahi_mixbuf(a5),ahirm_Buffer(a1)
	move.l	#ahi_recbuffer/4,ahirm_Length(a1)
	bra.b	.ok

.play	mulu.w	d0,d1	;mixing buffer szamitasa
	move.l	d1,ahi_mixedbytes(a5)
	move.l	ahit_freq-ahitags(a3),d1	;hardware buffer
	mulu.w	d0,d1
	mulu.l	ahi_buffer(a5),d1
	divu.l	#1000,d1
	move.l	d1,ahit_maxbuf-ahitags(a3)
	clr.b	ahi_status24(a5)
	clr.l	ahi_loopjump24(a5)
	move.l	ahiac_PlayerFunc(a2),a0	;update player regs
	sub.l	a1,a1
	move.l	h_Entry(a0),a3
	movem.l	a0-a1/a3,ahi_playerregs(a5)
	move.l	ahiac_MixerFunc(a2),a0	;update mixer regs
	move.l	ahi_mixbuf(a5),a1
	move.l	h_Entry(a0),a3
	movem.l	a0-a1/a3,ahi_mixerregs(a5)
	clr.l	d0	;update pretimer regs
	clr.l	d1
	move.l	ahiac_PreTimer(a2),a3
	movem.l	d0-d1/a2-a3,ahi_pretimerregs(a5)

	cmp.b	#debug_detail,debuglevel(a5)	;detail debug
	bcs.b	.ok
	move.l	d2,-(sp)
	moveq	#VSSpkt_PrintDebug,d0
	lea	d_mixed(pc),a0
	move.l	a0,d1
	move.l	ahi_mixedbytes(a5),d2
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr	sendPkt
	move.l	(sp)+,d2

.ok	movem.l	(sp)+,d2/a3/a5
	rts

ahitags	dc.l	VSS_hook
ahit_hook	dc.l	0
	dc.l	VSS_freq
ahit_freq	dc.l	0
	dc.l	VSS_bits
ahit_bits	dc.l	0
	dc.l	VSS_channels
ahit_channels	dc.l	0
	dc.l	VSS_mode
ahit_mode	dc.l	0
	dc.l	VSS_maxbuf
ahit_maxbuf	dc.l	0
	dc.l	VSS_task
ahit_task	dc.l	0
	dc.l	TAG_DONE

; In D0:bytesreq A0:hook A5:ioaddr  Out:bytesdone

ahih_8bit	dc.l	convert_8m,convert_8s	;8
ahih_16bit	dc.l	convert_16mb,convert_16sb	;16
ahih_h16bit	dc.l	convert_h16mb,convert_h16sb	;16 HIFI
ahih_h24bit	dc.l	convert_h24mb,convert_h24sb	;24 HIFI
ahih_rechooks	dc.l	convert_r8m,convert_r8s	;8       REC
	dc.l	convert_r16mb,convert_r16sb	;16      REC
	dc.l	convert_r16mb,convert_r16sb	;16 HIFI REC
	dc.l	0,0	;24 HIFI REC
ahih_opthooks	dc.l	convert_8m,convert_8s	;8           OPT
	dc.l	convert_16mw,convert_16sw	;16          OPT
	dc.l	convert_h16mw,convert_h16sw	;16 HIFI     OPT
	dc.l	convert_h24mw,convert_h24sw	;24 HIFI     OPT
	dc.l	convert_r8m,convert_r8s	;8       REC OPT
	dc.l	convert_r16mw,convert_r16sw	;16      REC OPT
	dc.l	convert_r16mw,convert_r16sw	;16 HIFI REC OPT
	dc.l	0,0	;24 HIFI REC OPT

PLAY_PRE	MACRO
	movem.l	d2/d6-d7/a2-a4,-(sp)
	move.l	var(pc),a4
	movem.l	ahi_pretimerregs(a4),d6-d7/a2-a3
	cmp.w	#ahi_minbytes,d0
	bcs.w	.exit
	tst.w	ahi_discnt(a4)
	bne.w	.exit
	move.w	d0,d7
	jsr	(a3)	;PreTimer
	bne.w	.ok
.meg	move.l	ahi_bytes(a4),d2
	bne.b	.copy
	movem.l	ahi_playerregs(a4),a0-a1/a3
	jsr	(a3)	;player
	movem.l	ahi_mixerregs(a4),a0-a1/a3
	move.l	a1,ahi_bufstart(a4)
	jsr	(a3)	;mixer
	move.l	ahi_mixedbytes(a4),d2
	move.l	d2,ahi_bytes(a4)
.copy	cmp.l	d7,d2
	bcs.b	.1
	move.l	d7,d2
.1	move.l	d2,d0
	move.l	ahi_bufstart(a4),a0
	ENDM

; In D0:bytes D2:cnt (scratch) A0:pointer  Out D0:bytes_done A0:new_pointer

PLAY_POST	MACRO
	move.l	a0,ahi_bufstart(a4)
	sub.l	d0,ahi_bytes(a4)
	add.l	d0,d6
	sub.l	d0,d7
	bne.w	.meg
.ok	move.l	ahiac_PostTimer(a2),a3	;PostTimer
	jsr	(a3)
.exit	move.l	d6,d0
	movem.l	(sp)+,d2/d6-d7/a2-a4
	rts
	ENDM

;----> play hooks

; 8bit mono (in:4/out:2) BYTE

convert_8m	PLAY_PRE
	lsr.w	#1,d2
	subq.w	#1,d2
.2	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	dbf	d2,.2
	PLAY_POST

; 8bit stereo (in:8/out:4) BYTE

convert_8s	PLAY_PRE
	lsr.w	#2,d2
	subq.w	#1,d2
.2	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	dbf	d2,.2
	PLAY_POST

; 16bit mono (in:4/out:4) BYTE

convert_16mb	PLAY_PRE
	lsr.w	#2,d2
	subq.w	#1,d2
.0	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	dbf	d2,.0
	PLAY_POST

; 16bit mono (in:4/out:4) WORD

convert_16mw	PLAY_PRE
	lsr.w	#2,d2
	subq.w	#1,d2
.0	move.w	(a0)+,(a5)
	move.w	(a0)+,(a5)
	dbf	d2,.0
	PLAY_POST

; 16bit stereo (in:8/out:8) BYTE

convert_16sb	PLAY_PRE
	lsr.w	#3,d2
	subq.w	#1,d2
.0	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	dbf	d2,.0
	PLAY_POST

; 16bit stereo (in:8/out:8) WORD

convert_16sw	PLAY_PRE
	lsr.w	#3,d2
	subq.w	#1,d2
.0	move.w	(a0)+,(a5)
	move.w	(a0)+,(a5)
	move.w	(a0)+,(a5)
	move.w	(a0)+,(a5)
	dbf	d2,.0
	PLAY_POST

; HiFi 16bit mono (in:8/out:4) BYTE

convert_h16mb	PLAY_PRE
	lsr.w	#2,d2
	subq.w	#1,d2
.0	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#2,a0
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#2,a0
	dbf	d2,.0
	PLAY_POST

; HiFi 16bit mono (in:8/out:4) WORD

convert_h16mw	PLAY_PRE
	lsr.w	#2,d2
	subq.w	#1,d2
.0	move.w	(a0)+,(a5)
	addq.l	#2,a0
	move.w	(a0)+,(a5)
	addq.l	#2,a0
	dbf	d2,.0
	PLAY_POST

; HiFi 16bit stereo (in:16/out:8) BYTE

convert_h16sb	PLAY_PRE
	lsr.w	#3,d2
	subq.w	#1,d2
.0	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#2,a0
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#2,a0
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#2,a0
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#2,a0
	dbf	d2,.0
	PLAY_POST

; HiFi 16bit stereo (in:16/out:8) WORD

convert_h16sw	PLAY_PRE
	lsr.w	#3,d2
	subq.w	#1,d2
.0	move.w	(a0)+,(a5)
	addq.l	#2,a0
	move.w	(a0)+,(a5)
	addq.l	#2,a0
	move.w	(a0)+,(a5)
	addq.l	#2,a0
	move.w	(a0)+,(a5)
	addq.l	#2,a0
	dbf	d2,.0
	PLAY_POST

; HiFi 24bit stereo (in:16/out:12) BYTE

convert_h24sb	PLAY_PRE
	move.b	ahi_status24(a4),d1
	beq.b	.0

	subq.b	#1,d1
	bne.b	.w2
	move.b	(a0)+,(a5)
	moveq	#2,d1
	subq.l	#1,d2
	beq.b	.w3
.w2	move.b	(a0)+,(a5)
	addq.l	#1,a0
	subq.l	#1,d2
	clr.b	d1
.w3	move.b	d1,ahi_status24(a4)
	bra.b	.0

.go12	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
.go9	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
.go6	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
.go3	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0

.0	subq.l	#6,d2
	subq.l	#6,d2
	bpl.b	.go12
	addq.l	#3,d2
	bcs.b	.go9
	addq.l	#3,d2
	bcs.b	.go6
	addq.l	#3,d2
	bcs.b	.go3
	addq.l	#3,d2
	beq.b	.02

	move.b	d2,ahi_status24(a4)
	move.b	(a0)+,(a5)
	subq.l	#1,d2
	beq.b	.02
	move.b	(a0)+,(a5)
.02	PLAY_POST

; HiFi 24bit stereo (in:16/out:12) WORD

convert_h24sw	PLAY_PRE
	move.b	ahi_status24(a4),d1
	beq.b	.0

	subq.b	#1,d1
	bne.b	.w2
	move.b	(a0)+,(a5)
	moveq	#2,d1
	subq.l	#1,d2
	beq.b	.w3
.w2	move.b	(a0)+,(a5)
	addq.l	#1,a0
	subq.l	#1,d2
	clr.b	d1
.w3	move.b	d1,ahi_status24(a4)
	bra.b	.0

.go12	move.w	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
.go9	move.w	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
.go6	move.w	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
.go3	move.w	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0

.0	subq.l	#6,d2
	subq.l	#6,d2
	bpl.b	.go12
	addq.l	#3,d2
	bcs.b	.go9
	addq.l	#3,d2
	bcs.b	.go6
	addq.l	#3,d2
	bcs.b	.go3
	addq.l	#3,d2
	beq.b	.02

	move.b	d2,ahi_status24(a4)
	move.b	(a0)+,(a5)
	subq.l	#1,d2
	beq.b	.02
	move.b	(a0)+,(a5)
.02	PLAY_POST

; HiFi 24bit mono (in:8/out:12) BYTE

convert_h24mb	PLAY_PRE
	move.l	ahi_loopjump24(a4),d1
	beq.b	.0
	move.l	d1,a1
	jmp	(a1)

.w1	move.b	(a0)+,(a5)
	subq.l	#1,d2
	beq.b	.c2
.w2	move.b	(a0)+,(a5)
	addq.l	#1,a0
	subq.l	#1,d2
	beq.b	.c3
.w3	move.b	-4(a0),(a5)
	subq.l	#1,d2
	beq.b	.c4
.w4	move.b	-3(a0),(a5)
	subq.l	#1,d2
	beq.b	.c5
.w5	move.b	-2(a0),(a5)
	clr.l	ahi_loopjump24(a4)
	subq.l	#1,d2
	beq.w	.02
	bra.b	.0

.c2	lea	.w2(pc),a1
	bra.b	.cc
.c3	lea	.w3(pc),a1
	bra.b	.cc
.c4	lea	.w4(pc),a1
	bra.b	.cc
.c5	lea	.w5(pc),a1
.cc	move.l	a1,ahi_loopjump24(a4)
	bra.w	.02

.go12	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.b	-4(a0),(a5)
	move.b	-3(a0),(a5)
 	move.b	-2(a0),(a5)
.go6	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.b	-4(a0),(a5)
	move.b	-3(a0),(a5)
	move.b	-2(a0),(a5)

.0	subq.l	#6,d2
	subq.l	#6,d2
	bpl.b	.go12
	addq.l	#6,d2
	bcs.b	.go6
	addq.l	#6,d2

	lea	.jt(pc),a1
	asl.l	#2,d2
	move.l	(a1,d2.l),ahi_loopjump24(a4)
	move.l	(a1,d2.l,.jt2-.jt),a1
	jmp	(a1)

.jt	dc.l	0,.w1,.w2,.w3,.w4,.w5
.jt2	dc.l	.02,.g1,.g2,.g3,.g4,.g5

.g5	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.b	-4(a0),(a5)
	move.b	-3(a0),(a5)
	bra.b	.02
.g4	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.b	-4(a0),(a5)
	bra.b	.02
.g3	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	bra.b	.02
.g2	move.b	(a0)+,(a5)
.g1	move.b	(a0)+,(a5)
.02	PLAY_POST

; HiFi 24bit mono (in:8/out:12) WORD

convert_h24mw	PLAY_PRE
	move.l	ahi_loopjump24(a4),d1
	beq.b	.0
	move.l	d1,a1
	jmp	(a1)

.w1	move.b	(a0)+,(a5)
	subq.l	#1,d2
	beq.b	.c2
.w2	move.b	(a0)+,(a5)
	addq.l	#1,a0
	subq.l	#1,d2
	beq.b	.c3
.w3	move.b	-4(a0),(a5)
	subq.l	#1,d2
	beq.b	.c4
.w4	move.b	-3(a0),(a5)
	subq.l	#1,d2
	beq.b	.c5
.w5	move.b	-2(a0),(a5)
	clr.l	ahi_loopjump24(a4)
	subq.l	#1,d2
	beq.w	.02
	bra.b	.0

.c2	lea	.w2(pc),a1
	bra.b	.cc
.c3	lea	.w3(pc),a1
	bra.b	.cc
.c4	lea	.w4(pc),a1
	bra.b	.cc
.c5	lea	.w5(pc),a1
.cc	move.l	a1,ahi_loopjump24(a4)
	bra.w	.02

.go12	move.w	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.w	-4(a0),(a5)
 	move.b	-2(a0),(a5)
.go6	move.w	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.w	-4(a0),(a5)
	move.b	-2(a0),(a5)

.0	subq.l	#6,d2
	subq.l	#6,d2
	bpl.b	.go12
	addq.l	#6,d2
	bcs.b	.go6
	addq.l	#6,d2

	lea	.jt(pc),a1
	asl.l	#2,d2
	move.l	(a1,d2.l),ahi_loopjump24(a4)
	move.l	(a1,d2.l,.jt2-.jt),a1
	jmp	(a1)

.jt	dc.l	0,.w1,.w2,.w3,.w4,.w5
.jt2	dc.l	.02,.g1,.g2,.g3,.g4,.g5

.g5	move.w	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.w	-4(a0),(a5)
	bra.b	.02
.g4	move.w	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	move.b	-4(a0),(a5)
	bra.b	.02
.g3	move.w	(a0)+,(a5)
	move.b	(a0)+,(a5)
	addq.l	#1,a0
	bra.b	.02
.g2	move.b	(a0)+,(a5)
.g1	move.b	(a0)+,(a5)
.02	PLAY_POST

; AHI sampler hooks

; In D0:bytes_sampled A0:hook A5:ioaddr  Out:bytesdone

RECORD_PRE	MACRO
	movem.l	d2-d3/d6-d7/a2-a4,-(sp)
	move.l	var(pc),a4
	moveq	#0,d6
	cmp.w	#ahi_minbytes,d0
	bcs.w	.ok
	tst.w	ahi_discnt(a4)
	bne.w	.ok
	moveq	#0,d7
	move.w	d0,d7
.meg	move.l	ahi_bytes(a4),d2
	bne.b	.copy
	movem.l	ahi_samplerregs(a4),a0-a3	;SamplerFunc hivasa
	jsr	(a3)
	move.l	ahi_mixbuf(a4),a0
	bchg	#ahif_recbuf,ahi_flags(a4)
	bne.b	.3
	lea	ahi_recbuffer(a0),a0
.3	move.l	a0,ahi_bufstart(a4)
	move.l	a0,ahi_recmsg+ahirm_Buffer(a4)
	move.l	ahi_mixedbytes(a4),d2
	move.l	d2,ahi_bytes(a4)
.copy	cmp.l	d7,d2
	bcs.b	.1
	move.l	d7,d2
.1	move.l	d2,d0
	move.l	ahi_bufstart(a4),a0
	ENDM

; In D0:bytes D2:cnt(scratch) A0:pointer  Out D0:bytes_done A0:new_pointer

RECORD_POST	MACRO
	move.l	a0,ahi_bufstart(a4)
	sub.l	d0,ahi_bytes(a4)
	add.l	d0,d6
	sub.l	d0,d7
	bne.w	.meg
.ok	move.l	d6,d0
	movem.l	(sp)+,d2-d3/d6-d7/a2-a4
	rts
	ENDM

;----> record hooks

; 8bit mono (in:2/out:4) BYTE

convert_r8m	RECORD_PRE
	lsr.w	d2
	subq.w	#1,d2
.2	move.b	(a5),d1
	move.b	(a5),d3
	asr.b	d1
	asr.b	d3
	add.b	d3,d1
	move.b	d1,(a0)+	;L MSB
	clr.b	(a0)+	;L LSB
	move.b	d1,(a0)+	;R MSB
	clr.b	(a0)+	;R LSB
	dbf	d2,.2
	RECORD_POST

; 8bit stereo (in:4/out:8) BYTE

convert_r8s	RECORD_PRE
	lsr.w	#2,d2
	subq.w	#1,d2
.2	move.b	(a5),(a0)+
	clr.b	(a0)+
	move.b	(a5),(a0)+
	clr.b	(a0)+
	move.b	(a5),(a0)+
	clr.b	(a0)+
	move.b	(a5),(a0)+
	clr.b	(a0)+
	dbf	d2,.2
	RECORD_POST

; 16bit mono (in:4/out:4) BYTE

convert_r16mb	RECORD_PRE
	lsr.w	#2,d2
	subq.w	#1,d2
.0	move.b	(a5),d1
	asl.w	#8,d1
	move.b	(a5),d1
	move.b	(a5),d3
	asl.w	#8,d3
	move.b	(a5),d3
	asr.w	d1
	asr.w	d3
	add.l	d1,d3
	move.w	d3,(a0)+
	move.w	d3,(a0)+
	dbf	d2,.0
	RECORD_POST

; 16bit mono (in:4/out:4) WORD

convert_r16mw	RECORD_PRE
	lsr.w	#2,d2
	subq.w	#1,d2
.0	move.w	(a5),d1
	move.w	(a5),d3
	asr.w	d1
	asr.w	d3
	add.l	d1,d3
	move.w	d3,(a0)+
	move.w	d3,(a0)+
	dbf	d2,.0
	RECORD_POST

; 16bit stereo (in:8/out:8) BYTE

convert_r16sb	RECORD_PRE
	lsr.w	#3,d2
	subq.w	#1,d2
.0	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	dbf	d2,.0
	RECORD_POST

; 16bit stereo (in:8/out:8) WORD

convert_r16sw	RECORD_PRE
	lsr.w	#3,d2
	subq.w	#1,d2
.0	move.w	(a5),(a0)+
	move.w	(a5),(a0)+
	move.w	(a5),(a0)+
	move.w	(a5),(a0)+
	dbf	d2,.0
	RECORD_POST

; In A2:audioctrldrv

ahisub_FreeAudio
	movem.l	a5-a6,-(sp)
	move.l	var(pc),a5

	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_ahifree(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt

	tst.l	ahiac_DriverData(a2)
	beq.b	.ex
	clr.l	ahiac_DriverData(a2)
	bsr.w	ahisub_Stop
	move.l	ahi_handle(a5),d0
	beq.b	.nohandle
	move.l	vssbase(a5),a6
	move.l	d0,a0
	jsr	_LVOvssFreeAudio(a6)
	move.l	ahi_handle(a5),a0
	jsr	_LVOvssFreeHandle(a6)
	clr.l	ahi_handle(a5)
.nohandle	bclr	#ahif_vssopen,ahi_flags(a5)
	beq.b	.exx
	move.l	excbase(a5),a6
	move.l	vssbase(a5),a1
	jsr	_LVOCloseLibrary(a6)
.exx	bclr	#ahif_busy,ahi_flags(a5)
.ex	clr.l	d0
	movem.l	(sp)+,a5-a6
	rts

; In A2:audioctrldrv

ahisub_Disable	move.l	a0,-(sp)
	move.l	var(pc),a0
	addq.w	#1,ahi_discnt(a0)
	move.l	(sp)+,a0
	rts

; In A2:audioctrldrv

ahisub_Enable	move.l	a0,-(sp)
	move.l	var(pc),a0
	subq.w	#1,ahi_discnt(a0)
	move.l	(sp)+,a0
	rts

; In D0:flags A2:audioctrl  Out D0:error

ahisub_Start	movem.l	a3/a5-a6,-(sp)
	move.l	var(pc),a5

	move.l	d0,-(sp)
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_ahistart(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	(sp)+,d0

	lea	ahitags(pc),a3

	btst	#AHISB_PLAY,d0
	bne.b	.play
	btst	#AHISB_RECORD,d0
	beq.w	.ok
	btst	#ahif_play,ahi_flags(a5)
	bne.b	.duperr
	bset	#ahif_record,ahi_flags(a5)
	bne.w	.ok

	tst.l	ahiac_DriverData(a2)
	beq.w	.nomem
	move.l	#1,ahit_mode-ahitags(a3)
	move.l	#2,ahit_channels-ahitags(a3)
	moveq	#AHISF_RECORD,d0
	bsr.w	ahisub_Stop
	move.l	excbase(a5),a6
	move.l	#ahi_recbuffer*2,d0
	move.l	pool1(a5),a0
	bsr.w	AllocVecPooled
	move.l	d0,ahi_mixbuf(a5)
	beq.w	.nomem
	bsr.w	ahisub_Update
	move.l	ahi_mixedbytes(a5),ahi_bytes(a5)
	move.l	ahi_mixbuf(a5),ahi_bufstart(a5)
	bclr	#ahif_recbuf,ahi_flags(a5)
	bra.b	.cont

.play	btst	#AHISB_RECORD,d0
	beq.b	.playok
.duperr	moveq	#AHIE_ABORTED!AHIE_HALFDUPLEX,d0
	bra.b	.exit
.playok	btst	#ahif_record,ahi_flags(a5)
	bne.b	.duperr
	bset	#ahif_play,ahi_flags(a5)
	bne.b	.ok

	tst.l	ahiac_DriverData(a2)
	beq.b	.nomem
	clr.l	ahit_mode-ahitags(a3)
	moveq	#AHISF_PLAY,d0
	bsr.b	ahisub_Stop
	move.l	excbase(a5),a6
	move.l	ahiac_BuffSize(a2),d0
	move.l	pool1(a5),a0
	bsr.w	AllocVecPooled
	move.l	d0,ahi_mixbuf(a5)
	beq.b	.nomem
	bsr.w	ahisub_Update
	clr.l	ahi_bytes(a5)

.cont	move.l	a2,ahi_drvctrl(a5)
	move.l	conftmp+conf_flags(a5),d0	;task/int
	clr.l	d1
	btst	#cflg_ahiint,d0
	seq	d1
	move.l	d1,ahit_task-ahitags(a3)
	move.l	vssbase(a5),a6	;call init
	move.l	a3,a1
	move.l	ahi_handle(a5),a0
	jsr	_LVOvssInit(a6)
	tst.l	d0
	beq.b	.nomem
	move.l	ahi_handle(a5),a0
	jsr	_LVOvssStart(a6)
	bra.b	.ok
.nomem	moveq	#AHIE_NOMEM,d0
	bra.b	.exit
.ok	moveq	#AHIE_OK,d0
.exit	movem.l	(sp)+,a3/a5-a6
	rts

; In D0:flags A2:audioctrldrv

ahisub_Stop	movem.l	a5-a6,-(sp)
	move.l	var(pc),a5

	move.l	d0,-(sp)
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_ahistop(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	(sp)+,d0

	btst	#AHISB_PLAY,d0
	beq.b	.noplay
	bclr	#ahif_play,ahi_flags(a5)
.noplay	btst	#AHISB_RECORD,d0
	beq.b	.norec
	bclr	#ahif_record,ahi_flags(a5)
.norec	tst.l	ahiac_DriverData(a2)
	beq.b	.nostop

	move.l	vssbase(a5),a6
	move.l	ahi_handle(a5),a0
	jsr	_LVOvssStop(a6)
	move.l	ahi_mixbuf(a5),d0	;free mixing buffer
	beq.b	.nostop
	move.l	excbase(a5),a6
	move.l	d0,a1
	move.l	pool1(a5),a0
	bsr.w	FreeVecPooled
	clr.l	ahi_mixbuf(a5)
	clr.l	ahi_drvctrl(a5)
.nostop	clr.l	d0
	movem.l	(sp)+,a5-a6
	rts

; In D0:attr D1:arg D2:default A1:taglist A2:audioctrldrv  Out D0:result

ahisub_GetAttr	movem.l	d2-d3/a5-a6,-(sp)
	move.l	var(pc),a5
	move.l	utibase(a5),a6
	movem.l	d0-d1,-(sp)	;get bits
	moveq	#16,d3
	move.l	a1,a0
	move.l	a1,d0
	beq.b	.notags
	move.l	#AHIDB_VSSBits,d0
	move.l	d3,d1
	jsr	_LVOGetTagData(a6)
	move.l	d0,d3
.notags	movem.l	(sp)+,d0-d1
	and.l	#~AHI_TagBaseR,d0
	cmp.l	#AHIDB_Data&~AHI_TagBaseR,d0
	bhi.b	.default
	sub.w	#100,d0
	move.w	(.jt,pc,d0.w*2),d0
	beq.b	.default
	jmp	(.jt,pc,d0.w)

.default	move.l	d2,d0
.exit	movem.l	(sp)+,d2-d3/a5-a6
	rts

.jt	dc.w	0	; AHIDB_AudioID
	dc.w	0	; AHIDB_Driver
	dc.w	0	; AHIDB_Flags
	dc.w	0	; AHIDB_Volume
	dc.w	0	; AHIDB_Panning
	dc.w	0	; AHIDB_Stereo
	dc.w	0	; AHIDB_HiFi
	dc.w	0	; AHIDB_PingPong
	dc.w	0	; AHIDB_MultTable
	dc.w	0	; AHIDB_Name
	dc.w	.Bits-.jt	; AHIDB_Bits
	dc.w	0;.MaxChannels-.jt	; AHIDB_MaxChannels
	dc.w	0	; AHIDB_MinMixFreq
	dc.w	0	; AHIDB_MaxMixFreq
	dc.w	.Record-.jt	; AHIDB_Record
	dc.w	.Frequencies-.jt	; AHIDB_Frequencies
	dc.w	0	; AHIDB_FrequencyArg
	dc.w	.Frequency-.jt	; AHIDB_Frequency
	dc.w	.Author-.jt	; AHIDB_Author
	dc.w	.Copyright-.jt	; AHIDB_Copyright
	dc.w	.Version-.jt	; AHIDB_Version
	dc.w	.Annotation-.jt	; AHIDB_Annotation
	dc.w	0	; AHIDB_BufferLen
	dc.w	0	; AHIDB_IndexArg
	dc.w	.Index-.jt	; AHIDB_Index
	dc.w	.Realtime-.jt	; AHIDB_Realtime
	dc.w	0;.MaxPlaySamples-.jt	; AHIDB_MaxPlaySamples
	dc.w	.MaxRecordSamples-.jt	; AHIDB_MaxRecordSample
	dc.w	0	; 
	dc.w	.FullDuplex-.jt	; AHIDB_FullDuplex
	dc.w	.MinMonitorVolume-.jt	; AHIDB_MinMonitorVolum
	dc.w	.MaxMonitorVolume-.jt	; AHIDB_MaxMonitorVolum
	dc.w	.MinInputGain-.jt	; AHIDB_MinInputGain
	dc.w	.MaxInputGain-.jt	; AHIDB_MaxInputGain
	dc.w	.MinOutputVolume-.jt	; AHIDB_MinOutputVolume
	dc.w	.MaxOutputVolume-.jt	; AHIDB_MaxOutputVolume
	dc.w	.Inputs-.jt	; AHIDB_Inputs
	dc.w	0	; AHIDB_InputArg
	dc.w	.Input-.jt	; AHIDB_Input
	dc.w	.Outputs-.jt	; AHIDB_Outputs
	dc.w	0	; AHIDB_OutputArg
	dc.w	.Output-.jt	; AHIDB_Output
	dc.w	0	; AHIDB_Data

.Bits	move.l	d3,d0
	bra.b	.exit
.Record	clr.l	d0
	cmp.b	#20,d3
	bcc.b	.exit
	moveq	#1,d0
	bra.b	.exit
.Frequencies	moveq	#9,d0
	bra.b	.exit
.Frequency	lea	freqtab(pc),a0
	move.l	(a0,d1.w*4),d0
	bra.b	.exit
.Author	lea	t_ahiaut(pc),a0
	move.l	a0,d0
	bra.w	.exit
.Copyright	lea	t_ahicop(pc),a0
	move.l	a0,d0
	bra.w	.exit
.Version	lea	t_ahiver(pc),a0
	move.l	a0,d0
	bra.w	.exit
.Annotation	lea	t_ahiann(pc),a0
	move.l	a0,d0
	bra.w	.exit
.Index	move.l	d1,d0
	bsr.w	findfreq
	bra.w	.exit
.Realtime	moveq	#1,d0
	bra.w	.exit
;.MaxPlaySamples	move.l	#5000,d0
;	bra	.exit
.MaxRecordSamples
	move.l	#ahi_recbuffer/4,d0
	bra.w	.exit
.FullDuplex	clr.l	d0
	bra.w	.exit
.MinMonitorVolume
	clr.l	d0
	bra.w	.exit
.MaxMonitorVolume
	moveq	#1,d0
	swap	d0
	bra.w	.exit
.MinInputGain	clr.l	d0
	bra.w	.exit
.MaxInputGain	moveq	#1,d0
	swap	d0
	bra.w	.exit
.MinOutputVolume
	clr.l	d0
	bra.w	.exit
.MaxOutputVolume
	moveq	#1,d0
	swap	d0
	bra.w	.exit
.Inputs	moveq	#4,d0
	bra.w	.exit
.Input	mulu.w	#chnnamelength,d1
	lea	(a5,d1.w,cnames+cname1),a0
	move.l	a0,d0
	bra.w	.exit
.Outputs	moveq	#1,d0
	bra.w	.exit
.Output	lea	cnames(a5),a0
	move.l	a0,d0
	bra.w	.exit


;----------< melodympeg.device emulation >---------------------------------------

; standard device functions

mef_start	equ	0
mef_run	equ	1
mef_init	equ	2
mef_listlock	equ	3

; A1:ioreq D0:unit D1:flags

mmpOpen	movem.l	d2-d7/a2-a6,-(sp)
	move.l	mylib_var(a6),a5
	move.l	a1,a3	;ioreq
	move.l	a6,a4
	btst	#flg_enable,flag(a5)
	beq.w	.err
	move.l	conftmp+conf_flags(a5),d2
	btst	#cflg_melena,d2
	beq.w	.err
	bsr.w	allocPkt	;debug
	clr.l	MN_REPLYPORT(a1)
	move.l	#VSSpkt_PrintDebug,pkt_cmd(a1)
	lea	d_melopen(pc),a0
	move.l	a0,pkt_arg(a1)
	move.l	d0,pkt_args(a1)	;unit
	move.l	d1,pkt_args+4(a1)	;flags
	move.l	excbase(a5),a6
	move.l	cport(a5),a0
	jsr	_LVOPutMsg(a6)
	lea	vssname(pc),a1	;open VSS
	moveq	#vssver,d0
	jsr	_LVOOpenLibrary(a6)
	tst.l	d0
	beq.b	.err
	move.l	d0,a6	;alloc handle
	lea	t_melody(pc),a0
	jsr	_LVOvssAllocHandle(a6)
	move.l	d0,d2
	beq.b	.err3
	move.l	excbase(a5),a6	;ha mar van handle,
	jsr	_LVOForbid(a6)	;fail! (egyszerre csak
	move.l	mel_handle(a5),d1	;egy lehet)
	bne.b	.0
	move.l	d2,mel_handle(a5)
.0	jsr	_LVOPermit(a6)
	move.l	vssbase(a5),a6
	move.l	d2,a0
	tst.l	d1
	bne.b	.err4
	jsr	_LVOvssAllocAudio(a6)	;alloc hardware
	tst.l	d0
	bne.b	.err2
	move.l	mel_handle(a5),a0	;port mentese
	move.l	vssh_port(a0),mel_saveport(a5)
	clr.l	vssh_port(a0)
	clr.b	IO_ERROR(a3)
	addq.w	#1,LIB_OPENCNT(a4)
	move.l	a4,d0
	bra.b	.ok
.err2	move.l	mel_handle(a5),a0
	clr.l	mel_handle(a5)
.err4	jsr	_LVOvssFreeHandle(a6)
.err3	move.l	a6,a1
	move.l	excbase(a5),a6
	jsr	_LVOCloseLibrary(a6)
.err	clr.l	d0
	move.b	#IOERR_OPENFAIL,IO_ERROR(a3)
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

mmpClose	movem.l	d2-d7/a2-a6,-(sp)
	move.l	mylib_var(a6),a5
	move.l	a6,a4
	move.l	mel_handle(a5),a2
	move.l	vssh_port(a2),d0	;port torlese,
	beq.b	.ok	;eredeti visszaallitasa
	move.l	excbase(a5),a6
	move.l	d0,a0
	jsr	_LVODeleteMsgPort(a6)
.ok	move.l	mel_saveport(a5),vssh_port(a2)
	clr.l	mel_saveport(a5)
	move.l	a2,a0
	move.l	vssbase(a5),a6
	jsr	_LVOvssFreeAudio(a6)
	move.l	mel_handle(a5),a0
	jsr	_LVOvssFreeHandle(a6)
	clr.l	mel_handle(a5)
	move.l	a6,a1
	move.l	excbase(a5),a6
	jsr	_LVOCloseLibrary(a6)
	subq.w	#1,LIB_OPENCNT(a4)
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_melclose(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	movem.l	(sp)+,d2-d7/a2-a6

mmpSetScr
mmpPriv2	clr.l	d0
mmpGetScr
mmpPriv1
mmpAbortIO	rts

mmpBeginIO	movem.l	a3/a5-a6,-(sp)
	move.l	var(pc),a5
	move.l	a1,a3	;iorequest
	move.w	IO_COMMAND(a3),d0
	and.w	#$7fff,d0
	cmp.w	#(mel.jumpend-.jumps)/2,d0
	bcc.b	mel.nocmd
	move.w	(.jumps,pc,d0.w*2),d0
	jmp	(.jumps,pc,d0.w)

.jumps	relbeg
	; standard commands
	rdc.w	mel.nocmd	;invalid
	rdc.w	mel.reset	;reset
	rdc.w	mel.notimpl	;read
	rdc.w	mel.write	;write
	rdc.w	mel.notimpl	;update
	rdc.w	mel.notimpl	;clear
	rdc.w	mel.notimpl	;stop
	rdc.w	mel.notimpl	;start
	rdc.w	mel.flush	;flush
	; extended commands
	rdc.w	mel.play	;play
	rdc.w	mel.pause	;pause
	rdc.w	mel.nocmd	;slowmotion
	rdc.w	mel.nocmd	;singlestep
	rdc.w	mel.nocmd	;search
	rdc.w	mel.nocmd	;record
	rdc.w	mel.getdevinf	;getdevinfo
	rdc.w	mel.nocmd	;setwindow
	rdc.w	mel.nocmd	;setborder
	rdc.w	mel.nocmd	;getvideoparams
	rdc.w	mel.nocmd	;setvideoparams
	rdc.w	mel.setaudpar	;setaudioparams
	rdc.w	mel.nocmd	;playlsn
	rdc.w	mel.nocmd	;seeklsn
	rdc.w	mel.nocmd	;readframeyuv
	rdc.w	mel.nocmd	;???
mel.jumpend

mel.notimpl	clr.l	IO_ACTUAL(a3)	;do nothing
	bra.b	mel.bad
mel.nocmd	move.b	#IOERR_NOCMD,IO_ERROR(a3)	;nocmd!
mel.bad	moveq	#VSSpkt_PrintFault,d0	;debug
	lea	e_melbadcmd(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
mel.done	move.l	a3,d0
	beq.b	mel.noreply
	btst	#IOB_QUICK,IO_FLAGS(a3)
	bne.b	mel.noreply
	move.l	excbase(a5),a6
	move.l	a3,a1
	jsr	_LVOReplyMsg(a6)
mel.noreply	movem.l	(sp)+,a3/a5-a6
	rts

; fill out MPEGDevInfo structure

mel.getdevinf	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_meldevinfo(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt

	move.l	IO_DATA(a3),a0
	move.w	#1,(a0)	;mdi_Version
	clr.w	mdi_Flags(a0)
 move.l #MPEGCF_PLAYAUDLAYER_III!MPEGCF_PLAYRAWAUDIO!MPEGCF_PLAYSYSTEM,mdi_BoardCapabilities(a0)
	moveq	#-2,d0
	addq.l	#mdi_BoardDesc,a0
	lea	t_melodydesc(pc),a1
.0	move.b	(a1)+,(a0)+
	dbne	d0,.0
.11	clr.b	(a0)+
	dbf	d0,.11
	bra.b	mel.done

; Play jelenleg nem csinal SEMMIT.
; A doksi es a visszafejtett eredeti driver szerint kodolva nem mukodne.
;
; iomr_StreamType:
;   4 Audio 16bit (OctaMED SoundStudio)
;   2 MPEG (Amplifier)
;
; iomr_Arg1: (csak a write packetekben, a play packetben mindig 0)
;  64 mono
;   0 stereo

mel.play	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_melplay(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	bra.b	mel.done

; Requesteket beteszi a listaba, lejatszast inditja

mel.write	cmp.b	#debug_detail,debuglevel(a5)
	bcs.b	.nodetail
	bsr.w	allocPkt	;detail debug
	clr.l	MN_REPLYPORT(a1)
	move.l	#VSSpkt_PrintDebug,pkt_cmd(a1)
	lea	d_melwrite(pc),a0
	move.l	a0,pkt_arg(a1)
	move.l	IO_LENGTH(a3),pkt_args(a1)
	move.l	cport(a5),a0
	move.l	excbase(a5),a6
	jsr	_LVOPutMsg(a6)

.nodetail	bset	#mef_init,mel_flags(a5)
	bne.w	.run

	move.l	excbase(a5),a6

	IFD	melodyport2
	tst.l	mel_port2(a5)
	bne.b	.vanport2
	jsr	_LVOCreateMsgPort(a6)
	move.l	d0,mel_port2(a5)
	beq.w	.fail1
.vanport2	;bugfix
	ENDIF

	move.l	mel_handle(a5),a0	;init, van mar port?
	tst.l	vssh_port(a0)
	bne.b	.vanport
	jsr	_LVOCreateMsgPort(a6)	;create port
	move.l	mel_handle(a5),a0
	move.l	d0,vssh_port(a0)
	beq.w	.fail1
.vanport	move.b	iomr_StreamType(a3),d0	;init
	move.b	d0,mel_streamtype(a5)
	lea	.meltags(pc),a1
	;cmp.b	#MPEGSTREAM_AUDIO,d0
	cmp.b	#4,d0
	beq.b	.playaudio
	lea	.meltagsmp3(pc),a1
	;cmp.b	#MPEGSTREAM_SYSTEM,d0
	cmp.b	#2,d0
	beq.b	.playmp3
	move.b	#MPEGERR_BAD_PARAMETER,IO_ERROR(a3)
	bra.w	.fail

.playaudio	moveq	#2,d0
	btst	#6,iomr_Arg1+3(a3)	;mono/stereo?
	dbeq	d0,.22	;conditional decrement
.22	move.l	d0,.melchannels-.meltags(a1)
	bra.b	.playok

.playmp3	move.l	#44100,.melfreqmp3-.meltagsmp3(a1)
	movem.l	a1-a2,-(sp)
	move.l	IO_DATA(a3),a0
	sub.l	a1,a1
	sub.l	a2,a2
	move.l	IO_LENGTH(a3),d0
	bsr.w	findsync
	movem.l	(sp)+,a1-a2
	tst.l	d0
	bmi.b	.playok
	move.l mpastrbuf+stream_frequency(pc),.melfreqmp3-.meltagsmp3(a1)
	move.w mpastrextbuf+strext_emphasis(pc),d0
	ext.l	d0
	move.l	d0,.meldeemp-.meltagsmp3(a1)

.playok	move.l	conftmp+conf_melbuftime(a5),d0
	asl.l	#8,d0	;buffer szamitas
	asl.l	#2,d0
	move.l	d0,.melbuf-.meltags(a1)

	move.l	conftmp+conf_flags(a5),d0	;task/int/opt
	clr.l	d1
	btst	#cflg_melint,d0
	bne.b	.00
	moveq	#1,d1
.00	move.l	d1,.meltask-.meltags(a1)
	IFNE	melodybufdiv
	btst	#cflg_bytewr,d0
	bne.b	.01
	or.b	#2,d1
.01	ENDIF
	lea	melhooktab(pc),a0
	move.l	(a0,d1.w*4),d0
	lea	.melhookentry(pc),a0
	move.l	d0,(a0)

	move.l	vssbase(a5),a6	;call init
	move.l	mel_handle(a5),a0
	jsr	_LVOvssInit(a6)
	tst.l	d0
	beq.w	.fail1

.run	move.l	excbase(a5),a6

	IFD	melodyport2
	btst	#IOB_QUICK,IO_FLAGS(a3)	;ha quick, portok csereje
	beq.b	.r2	;a valaszt majd megvarjuk
	move.l	MN_REPLYPORT(a3),mel_port3(a5)
	move.l	mel_port2(a5),MN_REPLYPORT(a3)
.r2	ENDIF

	tst.b	tasktransfer(a5)
	beq.b	.r0
	lea	melpktsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	lea	melpktlist(a5),a0	;attach to list
	move.l	a3,a1
	jsr	_LVOAddTail(a6)
	lea	melpktsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	bra.b	.r1
.r0	;move.l	address(a5),a4
	;move.b	con(a5),d0
	;and.b	#%11111111,d0
	;move.b	d0,
	;jsr	_LVODisable(a6)
	bset	#mef_listlock,mel_flags(a5)
	lea	melpktlist(a5),a0	;attach to list
	move.l	a3,a1
	jsr	_LVOAddTail(a6)
	bclr	#mef_listlock,mel_flags(a5)
	;jsr	_LVOEnable(a6)

.r1	bset	#mef_run,mel_flags(a5)	;start playback
	bset	#mef_start,mel_flags(a5)
	bne.b	.r3
	move.l	vssbase(a5),a6
	move.l	mel_handle(a5),a0
	jsr	_LVOvssStart(a6)

.r3	IFND	melodyport2
	bra.w	mel.noreply
	ELSE
	move.l	excbase(a5),a6
	btst	#IOB_QUICK,IO_FLAGS(a3)	;quick?
	beq.w	mel.noreply
	move.l	mel_port2(a5),a0	;valaszt megvarjuk
	jsr	_LVOWaitPort(a6)
	move.l	mel_port2(a5),a0
	jsr	_LVOGetMsg(a6)
	move.l	mel_port3(a5),MN_REPLYPORT(a3)
	cmp.l	d0,a3
	beq.w	mel.noreply
	moveq	#VSSERR_Sys,d0
	bsr.w	panic
	illegal
	ENDIF

.fail1	move.b	#MPEGERR_CMD_FAILED,IO_ERROR(a3)
.fail	move.l	mel_handle(a5),a1
	move.l	vssh_port(a1),a0
	clr.l	vssh_port(a1)
	move.l	excbase(a5),a6
	jsr	_LVODeleteMsgPort(a6)
.fa	IFD	melodyport2
	move.l	mel_port2(a5),a0
	clr.l	mel_port2(a5)
	jsr	_LVODeleteMsgPort(a6)
	ENDIF
	bclr	#mef_init,mel_flags(a5)
	bra.w	mel.done

; Elso TAGek mindenkepp VSS_maxbuf/VSS_task!

.meltags	dc.l	VSS_maxbuf	;fix
.melbuf	dc.l	0	;
	dc.l	VSS_task	;
.meltask	dc.l	0	;
	dc.l	VSS_channels
.melchannels	dc.l	0
	dc.l	VSS_hook,.melhook
	dc.l	VSS_freq,48000
	dc.l	VSS_bits,16
	dc.l	VSS_mode,0
	dc.l	TAG_DONE

.meltagsmp3	dc.l	VSS_maxbuf,0	;sorrend nem valtozhat!
	dc.l	VSS_task,0	;
	dc.l	VSS_hook,.melhook
	dc.l	VSS_freq
.melfreqmp3	dc.l	0
	dc.l	VSS_emphasis
.meldeemp	dc.l	0
	dc.l	VSS_bits,0
	dc.l	VSS_mode,0
	dc.l	TAG_DONE

; IORequesteket kiveszi a melpktlist-bol, adatokat atadja a kartyanak
; Ha az osszes adatot atvitte, a request-et visszakuldi

.melhook	dc.l	0,0
.melhookentry	dc.l	0,0,0

MELHOOK_PRE	MACRO
	movem.l	d2/d6-d7/a4-a6,-(sp)
	move.l	var(pc),a4
	move.l	excbase(a4),a6
	clr.l	d6	;data sent
	move.l	d0,d7	;data req
.loop	move.l	mel_pktdata(a4),d2
	bne.b	.pkt
	ENDM

MELHOOK_POST	MACRO
	move.l	d0,mel_curpkt(a4)
	beq.b	.ok
	move.l	d0,a0
	move.l	IO_DATA(a0),mel_pktptr(a4)
	move.l	IO_LENGTH(a0),d2
	move.l	d2,mel_pktdata(a4)
	beq.b	.4
.pkt	cmp.l	d7,d2
	bcs.b	.1
	move.l	d7,d2
.1	move.l	mel_pktptr(a4),a0
	move.l	d2,d0
	IFNE	melodybufdiv
	lsr.l	#melodybufdiv,d0	;longword align
	ENDIF
	subq.w	#1,d0
	bls.b	.ok
	ENDM

MELHOOK_POST2	MACRO
	move.l	a0,mel_pktptr(a4)
	sub.l	d2,mel_pktdata(a4)
	bne.b	.3
.4	move.l	mel_curpkt(a4),a1
	jsr	_LVOReplyMsg(a6)
	clr.l	mel_curpkt(a4)
.3	add.l	d2,d6
	sub.l	d2,d7
	bne.b	.loop
.ok	move.l	d6,d0
	movem.l	(sp)+,d2/d6-d7/a4-a6
	rts
	ENDM

MELHOOK_INT	MACRO
	btst	#mef_listlock,mel_flags(a4)
	bne.b	.ok
	lea	melpktlist(a4),a0
	jsr	_LVORemHead(a6)
	ENDM

MELHOOK_TASK	MACRO
	lea	melpktsem(a4),a0
	jsr	_LVOObtainSemaphore(a6)
	lea	melpktlist(a4),a0
	jsr	_LVORemHead(a6)
	lea	melpktsem(a4),a0
	jsr	_LVOReleaseSemaphore(a6)
	ENDM

MELHOOK_BYTE	MACRO
.2	move.b	(a0)+,(a5)
	IFNE	melodybufdiv
	move.b	(a0)+,(a5)
	IFNE	melodybufdiv=2
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	ENDIF
	ENDIF
	dbf	d0,.2
	ENDM

MELHOOK_WORD	MACRO
.2	move.w	(a0)+,(a5)
	IFNE	melodybufdiv=2
	move.w	(a0)+,(a5)
	ENDIF
	dbf	d0,.2
	ENDM

melhooktab	dc.l	melhookint,melhooktask
	IFNE	melodybufdiv
	dc.l	melhookintopt,melhooktaskopt
	ENDIF

melhookint	MELHOOK_PRE
	MELHOOK_INT
	MELHOOK_POST
	MELHOOK_BYTE
	MELHOOK_POST2

melhooktask	MELHOOK_PRE
	MELHOOK_TASK
	MELHOOK_POST
	MELHOOK_BYTE
	MELHOOK_POST2

melhookintopt	MELHOOK_PRE
	MELHOOK_INT
	MELHOOK_POST
	MELHOOK_WORD
	MELHOOK_POST2

melhooktaskopt	MELHOOK_PRE
	MELHOOK_TASK
	MELHOOK_POST
	MELHOOK_WORD
	MELHOOK_POST2

; Mindent leallit, varakozo requesteket abortalja

mel.reset	lea	d_melreset(pc),a0
	bra.b	mel.flushres
mel.flush	lea	d_melflush(pc),a0
mel.flushres	move.l	a0,d1	;debug
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	bclr	#mef_start,mel_flags(a5)
	beq.b	.nostop
	move.l	vssbase(a5),a6	;stop playing
	move.l	mel_handle(a5),a0
	jsr	_LVOvssStop(a6)
.nostop	bset	#mef_listlock,mel_flags(a5)
	move.l	excbase(a5),a6	;reply pending requests
	lea	melpktsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	move.l	mel_curpkt(a5),d0
	bne.b	.reply
.meg	lea	melpktlist(a5),a0	;remaining in the list
	jsr	_LVORemHead(a6)
	tst.l	d0
	beq.b	.kesz
.reply	move.l	d0,a1
	move.b	#IOERR_ABORTED,IO_ERROR(a1)
	jsr	_LVOReplyMsg(a6)
	bra.b	.meg
.kesz	lea	melpktsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	bclr	#mef_listlock,mel_flags(a5)
	bclr	#mef_init,mel_flags(a5)
	bclr	#mef_run,mel_flags(a5)
	clr.l	mel_curpkt(a5)
	clr.l	mel_pktdata(a5)
	move.l	mel_handle(a5),a1	;delete port
	move.l	vssh_port(a1),a0
	clr.l	vssh_port(a1)
	jsr	_LVODeleteMsgPort(a6)
	IFD	melodyport2
	move.l	mel_port2(a5),a0
	clr.l	mel_port2(a5)
	jsr	_LVODeleteMsgPort(a6)
	ENDIF
	bra.w	mel.done

; pause

mel.pause	lea	d_melpause(pc),a0	;debug
	moveq	#VSSpkt_PrintDebug,d0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	vssbase(a5),a6
	move.l	mel_handle(a5),a0
	tst.l	iomr_Arg1(a3)
	beq.b	.normal
	bclr	#mef_start,mel_flags(a5)	;pause mode
	beq.w	mel.done
	jsr	_LVOvssStop(a6)
	bra.w	mel.done
.normal	btst	#mef_run,mel_flags(a5)	;restart
	beq.w	mel.done
	bset	#mef_start,mel_flags(a5)
	bne.w	mel.done
	jsr	_LVOvssStart(a6)
	bra.w	mel.done

; notify mixer clients

mel.setaudpar	move.l	conftmp+conf_flags(a5),d0
	btst	#cflg_melvol,d0
	beq.w	mel.done
	move.l	IO_DATA(a3),a0
	clr.l	d0
	clr.l	d1
	move.w	map_VolumeLeft(a0),d0
	move.w	map_VolumeRight(a0),d1
	divu.w	#655,d0
	divu.w	#655,d1
	move.b	d0,mixtab+VMT_4l(a5)
	move.b	d1,mixtab+VMT_4r(a5)

	move.w	gui_modes+cmode0(a5),d2
	cmp.w	#2,d2	;ha mute, nem bantjuk
	beq.b	.nomode
	moveq	#1,d2
	cmp.w	d0,d1
	dbne	d2,.nomode
.nomode	move.w	d2,gui_modes+cmode0(a5)

	move.l	vssbase(a5),a6
	move.l	mel_handle(a5),a0
	st	d0	;async processing
	jsr	_LVOvssRefreshMixer(a6)

	cmp.b	#debug_detail,debuglevel(a5)	;detail debug
	bcs.b	.nodetail
	moveq	#VSSpkt_PrintDebug,d0
	lea	d_melsetpar(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
.nodetail	bra.w	mel.done


;-------< mpega emulation library functions >------------------------------------

; system functions

libmpa_Open	move.l	a5,-(sp)
	move.l	mylib_var(a6),a5
	addq.w	#1,LIB_OPENCNT(a6)
	move.l	a6,d0

	btst	#flg_patch,flag(a5)
	bne.b	.0
	btst	#flg_enable,flag(a5)
	bne.b	.0

	subq.w	#1,LIB_OPENCNT(a6)	;nem nyitjuk
	clr.l	d0

.0	move.l	(sp)+,a5
	rts

; user functions

	; stream access hook commands

acc_open	equ	0
acc_close	equ	1
acc_read	equ	2
acc_seek	equ	3

mpae_eof	equ	-1
mpae_badframe	equ	-2
mpae_mem	equ	-3
mpae_nosync	equ	-4
mpae_badvalue	equ	-5

mpa_pcm_size	equ	1152
mpa_trysiz	equ	1500

mpmagic	equ	'MPEM'


; extended stream structure  *NEVER* pass this to the original mpega.library!
; (stream_extra and stream_magic must be restored first)

	STRUCTURE	mpega_stream,0
	UWORD	stream_norm
	UWORD	stream_layer
	UWORD	stream_mode
	UWORD	stream_bitrate
	ULONG	stream_frequency
	UWORD	stream_channels
	ULONG	stream_ms_duration
	UWORD	stream_private_bit
	UWORD	stream_copyright
	UWORD	stream_original
	UWORD	stream_dec_channels
	UWORD	stream_dec_quality
	ULONG	stream_dec_frequency
	APTR	stream_extra	;pointer to extensions
	IFD	mpusemagic
	IFND	mpmagicbefore
	ULONG	stream_magic	;magic number
	ENDIF
	ENDIF
	LABEL	stream_size
	; mpega private stuff follows

	IFD	mpusemagic
	IFD	mpmagicbefore
stream_magic	equ	-4
	ENDIF
	ENDIF

; stream extensions

	STRUCTURE	mpega_stream_extra,0
	APTR	strext_stream	;original stream
	ULONG	strext_save	;save stream_extra field
	IFD	mpusemagic
	ULONG	strext_savemagic	;save stream_magic field
	ENDIF
	APTR	strext_vhandle	;vsshandle
	BPTR	strext_fhandle	;filehandle
	APTR	strext_hook
	ULONG	strext_filesiz
	UBYTE	strext_flags
	ALIGNWORD
	UWORD	strext_crcprot	;filled by findsync
	UWORD	strext_padpriv	;
	UWORD	strext_emphasis	;
	UWORD	strext_framesize	;
	APTR	strext_h_handle	;data captured by
	ULONG	strext_h_filesiz	;trickyhook
	ULONG	strext_h_seekpos	;
	ULONG	strext_h_openbuf	;
	ULONG	strext_h_pos	;
	UWORD	strext_fillcnt
	LABEL	strext_size

	STRUCTURE	mpega_access,0
	ULONG	acc_func
	LABEL	acc_seekpos
	LABEL	acc_readbuf
	APTR	acc_openname
	LABEL	acc_readsize
	ULONG	acc_openbufsiz
	ULONG	acc_openstrsiz
	LABEL	acc_size

	STRUCTURE	mpega_output,0
	UWORD	mpout_freq_div
	UWORD	mpout_quality
	ULONG	mpout_freq_max
	LABEL	mpout_size

	STRUCTURE	mpega_layer,0
	UWORD	mplayer_force_mono
	STRUCT	mplayer_mono,mpout_size
	STRUCT	mplayer_stereo,mpout_size
	LABEL	mplayer_size

	STRUCTURE	mpega_ctrl,0
	APTR	ctrl_hook
	STRUCT	ctrl_lay2,mplayer_size
	STRUCT	ctrl_lay3,mplayer_size
	UWORD	ctrl_check_mpeg
	LONG	ctrl_strbufsiz
	LABEL	ctrl_size

	STRUCTURE	mpega_stream_extra_full,strext_size
	STRUCT	strext_access,acc_size
	STRUCT	strext_h,h_SIZEOF	; trickyhook, data=strext
	STRUCT	strext_ctrl,ctrl_size
	LABEL	strfull_size

	STRUCTURE	mpegid3,0
	STRUCT	mpid3_tag,3
	STRUCT	mpid3_title,30
	STRUCT	mpid3_artist,30
	STRUCT	mpid3_album,30
	STRUCT	mpid3_year,4
	STRUCT	mpid3_comments,30
	UBYTE	mpid3_genre
	LABEL	mpid3_size


strx_touch	equ	0
strx_noseek	equ	1
strx_loadram	equ	2

; Magic number visszaallitasa, regi fuggveny hivasa, stb..
; In A2:stream A4:strext

CALLOLD	MACRO
	move.l	old\1(a5),a3
	move.l	strext_save(a4),stream_extra(a2)
	IFD	mpusemagic
	move.l	strext_savemagic(a4),stream_magic(a2)
	ENDIF
	move.l	a2,a0
	jsr	(a3)
	move.l	stream_extra(a2),strext_save(a4)
	move.l	a4,stream_extra(a2)
	IFD	mpusemagic
	move.l	stream_magic(a2),strext_savemagic(a4)
	move.l	#mpmagic,stream_magic(a2)
	ENDIF
	ENDM

; A0/A1

CHKMAGIC	MACRO
	IFD	mpusemagic
	cmp.l	#mpmagic,stream_magic(a0)	;ha nem a mienk,
	beq.b	.magic	;eredeti hivasa
	move.l	old\1(a5),a2
	jsr	(a2)
	bra.w	.ok
.magic	ENDIF
	ENDM


; In A0:name A1:ctrl  Out D0:stream
; strext_h:	trickyhookfunc, meghivja strext_hook-ot
; strext_hook:	original hook or myhook

mpaMPEGA_open	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	btst	#flg_enable,flag(a5)
	beq.w	.nomem1
	move.l	conftmp+conf_flags(a5),d0
	btst	#cflg_mpaena,d0
	beq.w	.nomem1
	move.l	excbase(a5),a6
	move.l	a0,a2	;name
	move.l	a1,a3	;ctrl
	lea	vssname(pc),a1
	moveq	#vssver,d0
	jsr	_LVOOpenLibrary(a6)
	tst.l	d0
	beq.w	.nomem1
	move.l	#VSSpkt_PrintDebug,d0	;debug
	lea	d_mpaopen(pc),a0
	move.l	a0,d1
	move.l	a2,d2
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	#strfull_size,d0	;stream extra foglalas
	move.l	pool1(a5),a0
	bsr.w	AllocVecPooled
	tst.l	d0
	beq.w	.nomem
	move.l	d0,a4
	move.l	a3,a0	;ctrl masolasa
	lea	strext_ctrl(a4),a1
	moveq	#ctrl_size,d0
	jsr	_LVOCopyMem(a6)
	lea	trickyhookfunc(pc),a0	;init trickyhook
	move.l	a0,strext_h+h_Entry(a4)
	move.l	a4,strext_h+h_Data(a4)
	move.l	strext_ctrl+ctrl_hook(a4),strext_hook(a4)
	bne.b	.0
	lea	myhook(pc),a0
	move.l	a0,strext_hook(a4)
	lea	d_mpahook(pc),a0	;debug
	move.l	a0,d1
	move.l	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
.0	lea	strext_h(a4),a0
	move.l	a0,strext_ctrl+ctrl_hook(a4)

	btst	#flg_patch,flag(a5)	;open
	beq.b	.my
	move.l	a2,a0	;eredeti open hivasa
	lea	strext_ctrl(a4),a1
	move.l	oldMPEGA_open(a5),a2
	move.l	mpabase(a5),a6
	jsr	(a2)
	move.l	strext_h_handle(a4),strext_fhandle(a4)
	move.l	strext_h_filesiz(a4),strext_filesiz(a4)
	bra.w	.done

.my	move.l	excbase(a5),a6	;sajat MPEGA_open
	move.l	conftmp+conf_flags(a5),d0
	btst	#cflg_mpegadis,d0
	bne.w	.baj
	IFD	mpmagicbefore
	moveq	#stream_size+4,d0
	ELSE
	moveq	#stream_size,d0
	ENDIF
	move.l	pool1(a5),a0
	bsr.w	AllocVecPooled
	move.l	d0,a3	;stream A3
	tst.l	d0
	beq.w	.done
	IFD	mpmagicbefore
	addq.l	#4,a3
	ENDIF
	move.l	#mpa_trysiz,d0
	clr.l	d1
	jsr	_LVOAllocMem(a6)
	move.l	d0,d2
	beq.w	.err2
	move.l	#acc_open,strext_access+acc_func(a4)
	clr.l	strext_h_pos(a4)
	move.l	a2,strext_access+acc_openname(a4)
	clr.l	strext_access+acc_openbufsiz(a4)
	bsr.w	callhook
	move.l	d0,strext_fhandle(a4)
	beq.b	.err3
	move.l	strext_access+acc_openstrsiz(a4),strext_filesiz(a4)
	move.l	#acc_read,strext_access+acc_func(a4)
	move.l	d2,strext_access+acc_readbuf(a4)
	move.l	#mpa_trysiz,strext_access+acc_readsize(a4)
	bsr.w	callhook
	add.l	d0,strext_h_pos(a4)
	cmp.l	#mpa_trysiz,d0
	bne.b	.err4
	move.l	d2,a0
	move.l	a3,a1
	move.l	a4,a2
	move.l	#mpa_trysiz,d0
	bsr.w	findsync
	tst.l	d0
	bmi.b	.err4
	cmp.w	#3,stream_layer(a3)
	bne.b	.err4
	move.l	d2,a1
	move.l	#mpa_trysiz,d0
	jsr	_LVOFreeMem(a6)
	move.l	a3,d0
	bra.b	.done
.err4	move.l	#acc_close,strext_access+acc_func(a4)
	bsr.w	callhook
.err3	move.l	d2,a1
	move.l	#mpa_trysiz,d0
	jsr	_LVOFreeMem(a6)
.err2	move.l	a3,a1
	IFD	mpmagicbefore
	subq.l	#4,a1
	ENDIF
	move.l	pool1(a5),a0
	bsr.w	FreeVecPooled
	clr.l	d0

.done	tst.l	d0	;sikerult megnyitni?
	beq.b	.baj
	move.l	d0,a0	;OK, csere
	move.l	a0,strext_stream(a4)
	move.l	stream_extra(a0),strext_save(a4)
	move.l	a4,stream_extra(a0)
	IFD	mpusemagic
	move.l	stream_magic(a0),strext_savemagic(a4)
	move.l	#mpmagic,stream_magic(a0)
	ENDIF

	cmp.b	#debug_detail,debuglevel(a5)	;detail debug
	bcs.b	.nodetail
	move.l	d0,-(sp)
	bsr.w	allocPkt
	move.l	stream_ms_duration(a0),d0
	add.l	#500,d0
	divu.w	#1000,d0
	ext.l	d0
	divu.w	#60,d0
	swap	d0
	move.l	d0,pkt_args(a1)
	move.l	strext_filesiz(a4),pkt_args+4(a1)
	lea	d_duration(pc),a0
	move.l	a0,pkt_arg(a1)
	move.l	#VSSpkt_PrintDebug,pkt_cmd(a1)
	clr.l	MN_REPLYPORT(a1)
	move.l	cport(a5),a0
	move.l	excbase(a5),a6
	jsr	_LVOPutMsg(a6)
	move.l	(sp)+,d0
.nodetail	bra.b	.ok

.baj	move.l	excbase(a5),a6	;stream_extra torlese
	move.l	a4,a1
	move.l	pool1(a5),a0
	bsr.w	FreeVecPooled
.nomem	move.l	excbase(a5),a6	;close vss.lib
	move.l	vssbase(a5),a1
	jsr	_LVOCloseLibrary(a6)
.nomem1	clr.l	d0
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts


; In A0:strext_h  (meghivja az eredeti hook-ot, parametereket lementi)

trickyhookfunc	movem.l	a3/a4,-(sp)
	move.l	h_Data(a0),a4
	cmp.l	#acc_seek,acc_func(a1)
	bne.b	.1
	move.l	acc_seekpos(a1),a0
	move.l	a0,strext_h_seekpos(a4)
	btst	#strx_noseek,strext_flags(a4)
	beq.b	.2
	clr.l	d0
	bra.b	.ok
.2	move.l	a0,strext_h_pos(a4)
.1	move.l	strext_hook(a4),a0
	move.l	h_Entry(a0),a3
	pea	(a1)
	jsr	(a3)
	move.l	(sp)+,a1
	cmp.l	#acc_open,acc_func(a1)
	bne.b	.0
	clr.l	strext_h_pos(a4)
	move.l	acc_openstrsiz(a1),strext_h_filesiz(a4)
	move.l	acc_openbufsiz(a1),strext_h_openbuf(a4)
	move.l	d0,strext_h_handle(a4)
	bra.b	.ok
.0	cmp.l	#acc_read,acc_func(a1)
	bne.b	.ok
	tst.l	d0
	bmi.b	.ok
	add.l	d0,strext_h_pos(a4)
.ok	movem.l	(sp)+,a3/a4
	rts

; In A0:myhook A1:access A2:handle
; this is the default stream access hook, if not specified any by the caller

myhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d1-d3/a0-a2/a5-a6,-(sp)
	move.l	var(pc),a5
	move.l	dosbase(a5),a6
	move.l	acc_func(a1),d0
	beq.b	.open
	subq.l	#1,d0
	beq.w	.close
	subq.l	#1,d0
	beq.w	.read
	subq.l	#1,d0
	beq.b	.seek
	lea	e_mpaacc(pc),a0	;unrecognized command
	move.l	a0,d1
	moveq	#VSSpkt_PrintFault,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	clr.l	d0
	bra.b	.ret
.seek	move.l	a2,d1
	move.l	acc_seekpos(a1),d2
	moveq	#OFFSET_BEGINNING,d3
	jsr	_LVOSeek(a6)
	addq.l	#1,d0
	seq	d0
	extb.l	d0
.ret	movem.l	(sp)+,d1-d3/a0-a2/a5-a6
	rts
.open	move.l	a1,a2
	clr.l	acc_openbufsiz(a1)
	clr.l	acc_openstrsiz(a1)
	move.l	acc_openname(a1),d1
	move.l	#MODE_OLDFILE,d2
	jsr	_LVOOpen(a6)
	move.l	d0,-(sp)
	beq.b	.bad
	moveq	#DOS_FIB,d1
	clr.l	d2
	jsr	_LVOAllocDosObject(a6)
	move.l	d0,d2
	beq.b	.bad
	move.l	(sp),d1
	jsr	_LVOExamineFH(a6)
	tst.l	d0
	beq.b	.bad1
	move.l	d2,a0
	move.l	fib_Size(a0),acc_openstrsiz(a2)
.bad1	move.l	#DOS_FIB,d1
	jsr	_LVOFreeDosObject(a6)
.bad	move.l	(sp)+,d0
	bra.b	.ret
.close	move.l	a2,d1
	jsr	_LVOClose(a6)
	clr.l	d0
	bra.b	.ret
.read	move.l	a2,d1
	move.l	acc_readbuf(a1),d2
	move.l	acc_readsize(a1),d3
	jsr	_LVORead(a6)
	tst.l	d0
	bpl.b	.ok
	clr.l	d0
.ok	bra.b	.ret

; In A0:stream

mpaMPEGA_close	movem.l	a2-a6,-(sp)
	move.l	var(pc),a5

	CHKMAGIC	MPEGA_close

	move.l	a0,a2
	move.l	stream_extra(a2),a4

	move.l	#VSSpkt_PrintDebug,d0	;debug
	lea	d_mpaclose(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt

	cmp.l	stream(a5),a2	;stream active?
	bne.b	.nonactive
	move.l	vssbase(a5),a6
	clr.l	stream(a5)
	move.l	strext_vhandle(a4),a0
	jsr	_LVOvssStop(a6)
	move.l	strext_vhandle(a4),a0
	jsr	_LVOvssFreeAudio(a6)
	move.l	mpbuffer(a5),d0
	beq.b	.nonactive
	move.l	excbase(a5),a6
	move.l	d0,a1
	jsr	_LVOFreeVec(a6)
	clr.l	mpbuffer(a5)
.nonactive	move.l	vssbase(a5),a6	;ha a mienk, handle
	move.l	strext_vhandle(a4),a0	;torlese
	jsr	_LVOvssFreeHandle(a6)
	move.l	a6,a1
	move.l	excbase(a5),a6
	jsr	_LVOCloseLibrary(a6)
	btst	#flg_patch,flag(a5)	;ha patch, eredeti
	beq.b	.1	;MPEGA_close hivasa

	move.l	strext_save(a4),stream_extra(a2)
	IFD	mpusemagic
	move.l	strext_savemagic(a4),stream_magic(a2)
	ENDIF
	move.l	oldMPEGA_close(a5),a1
	move.l	mpabase(a5),a6
	move.l	a2,a0
	jsr	(a1)
	bra.b	.2
.1	move.l	#acc_close,strext_access+acc_func(a4)
	bsr.w	callhook	;close file
	move.l	a2,a1
	IFD	mpmagicbefore	;free stream
	subq.l	#4,a1
	ENDIF
	move.l	excbase(a5),a6	;free stream
	move.l	pool1(a5),a0
	bsr.w	FreeVecPooled
.2	move.l	excbase(a5),a6	;free stream_extra
	move.l	pool1(a5),a0
	move.l	a4,a1
	bsr.w	FreeVecPooled
.ok	movem.l	(sp)+,a2-a6
	rts

; In A0:stream A1:buffer

mpaMPEGA_decode_frame		;This is the big one ;)
	movem.l	d2/a2-a6,-(sp)
	move.l	var(pc),a5

	CHKMAGIC	MPEGA_decode_frame

	move.l	a0,a2
	move.l	a1,a3
	move.l	stream_extra(a2),a4
	cmp.w	#3,stream_layer(a2)	;ha nem mp3, nem
	bne.w	.nemjo	;foglalkozunk vele
	move.l	stream(a5),d0
	beq.b	.newstr
	cmp.l	a2,d0
	bne.w	.nemjo
	bra.w	.do
.newstr	move.l	conftmp+conf_flags(a5),d0
	btst	#cflg_mpaloadram,d0
	beq.b	.noloadram
	bset	#strx_loadram,strext_flags(a4)
.noloadram	btst	#cflg_mpegadis,d0
	bne.w	.nemjo
	btst	#cflg_nostrsw,d0
	beq.b	.newok
	btst	#strx_touch,strext_flags(a4)
	bne.w	.nemjo
.newok	move.l	vssbase(a5),a6
	move.l	strext_vhandle(a4),d0
	bne.b	.hand
	lea	t_mpega(pc),a0	;vsshandle foglalas
	jsr	_LVOvssAllocHandle(a6)
	move.l	d0,strext_vhandle(a4)
	beq.w	.nemjo
.hand	move.l	d0,a0
	jsr	_LVOvssAllocAudio(a6)
	tst.l	d0
	bne.w	.nemjo
	lea	.mp3tags(pc),a1
	lea	mp3opthook(pc),a0
	move.l	conftmp+conf_flags(a5),d0
	btst	#cflg_bytewr,d0
	beq.b	.6
	lea	mp3hook(pc),a0
.6	move.l	a0,.mp3tagshook-.mp3tags(a1)
	move.l	stream_frequency(a2),.mp3tagsfreq-.mp3tags(a1)
	move.w	strext_emphasis(a4),d0
	ext.l	d0
	move.l	d0,.mp3tagsdeemp-.mp3tags(a1)
	move.l	conftmp+conf_flags(a5),d0	;task/int
	clr.l	d1
	btst	#cflg_mpaint,d0
	seq	d1
	move.l	d1,.mp3tagstask-.mp3tags(a1)
	move.l	strext_vhandle(a4),a0
	jsr	_LVOvssInit(a6)
	tst.l	d0
	beq.w	.baj

	btst	#strx_loadram,strext_flags(a4)
	beq.b	.7
	move.l	strext_filesiz(a4),d1
	beq.b	.9
	bmi.b	.9
	;addq.l	#8,d1
	;addq.l	#8,d1
	add.l	#40000,d1
	and.b	#$f0,d1
	move.l	#16384,d0	;min 16k!
	cmp.l	d0,d1
	bcc.b	.8
	move.l	d0,d1
	bra.b	.8
.9	bclr	#strx_loadram,strext_flags(a4)
.7	move.l	conftmp+conf_mpbufsize(a5),d1
.8	move.l	d1,mpbufsize(a5)
	move.l	conftmp+conf_mpchunk(a5),mpchunk(a5)
	move.l	conftmp+conf_mpmaxchunk(a5),mpmaxchunk(a5)
	move.l	excbase(a5),a6
	move.l	mpbufsize(a5),d0
	move.l	#MEMF_PUBLIC!MEMF_CLEAR,d1
	jsr	_LVOAllocVec(a6)
	move.l	d0,mpbuffer(a5)
	beq.w	.baj
	move.l	a2,stream(a5)

	clr.l	ptr1(a5)
	clr.l	ptr2(a5)
	clr.l	mpbufeof(a5)
	clr.l	mpbufcorr(a5)
	bclr	#flg_start,flag(a5)
	bclr	#flg_eof,flag(a5)

; seek back to the beg of file if neccessary
; (because mpega.library filled it's private buffer yet when MPEGA_open called)

	btst	#strx_touch,strext_flags(a4)
	bne.b	.do
	move.l	#acc_seek,strext_access+acc_func(a4)
	clr.l	strext_access+acc_seekpos(a4)
	bsr.w	callhook
	clr.l	strext_h_pos(a4)

.do	;btst	#strx_loadram,strext_flags(a4)
	;beq.b	.d1
	;btst	#flg_eof,flag(a5)
	;bne.w	.eleg

.d1	move.l	ptr1(a5),d1
	move.l	mpbuffer(a5),d2
	add.l	d1,d2	;d2:iras kezdete
	; ha load ram, nem fordulunk korbe, nem toltunk a vegeig
	btst	#strx_loadram,strext_flags(a4)
	beq.b	.nolr
	move.l	mpbufsize(a5),d0
	sub.l	d1,d0
	bra.b	.read
.nolr	move.l	ptr2(a5),d0
	sub.l	d1,d0
	bls.b	.veg
	cmp.l	mpchunk(a5),d0
	bcs.w	.eleg
	bra.b	.read
.veg	move.l	mpbufsize(a5),d0
	sub.l	d1,d0
	tst.l	ptr2(a5)
	bne.b	.read1
.read	subq.l	#4,d0	;!!! always long align
	bls.w	.eleg
.read1	move.l	mpmaxchunk(a5),d1
	cmp.l	d1,d0
	bls.b	.1
	move.l	d1,d0
.1	btst	#flg_eof,flag(a5)
	beq.b	.hook
	move.l	d2,a0	;EOF, 0 kuldese
	move.l	d0,d1
	lsr.l	#2,d1
	beq.b	.4
	subq.l	#1,d1
.3	clr.l	(a0)+
	dbf	d1,.3
.4	cmp.b	#debug_detail,debuglevel(a5)	;detail debug
	bcs.b	.44
	move.l	d0,d2
	beq.b	.44
	lea	d_zeros(pc),a0
	move.l	a0,d1
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	d2,d0
.44	bra.w	.555
.hook	move.l	#acc_read,strext_access+acc_func(a4)
	move.l	d2,strext_access+acc_readbuf(a4)
	move.l	d0,strext_access+acc_readsize(a4)
	bsr.w	callhook
	tst.l	d0	;EOF/error?
	beq.b	.eof
	bpl.b	.5
.eof	lea	d_eoferr(pc),a0	;info: EOF
	move.l	a0,d1
	move.l	strext_h_pos(a4),d2
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	; kiszamoljuk mennyi adat van meg a bufferben
	bset	#flg_eof,flag(a5)
	move.l	ptr1(a5),d0
	move.l	d0,mpbufcorr(a5)
	sub.l	ptr2(a5),d0
	bcc.b	.01
	add.l	mpbufsize(a5),d0
.01	clr.l	d1	;hardware buffer
	move.b	bufsize(a5),d1
	asl.l	#7,d1
	add.l	d1,d0
	move.l	d0,mpbufeof(a5)
	bra.b	.eleg
.5	cmp.b	#debug_detail,debuglevel(a5)	;detail debug
	bcs.b	.555
	move.l	d0,d2
	lea	d_bytesdone(pc),a0
	move.l	a0,d1
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	d2,d0
.555	add.l	d0,strext_h_pos(a4)
	add.l	ptr1(a5),d0	;bump pointer
	cmp.l	mpbufsize(a5),d0
	bcs.b	.2
	; ha load_ram, nem fordul korbe a pointer!
	btst	#strx_loadram,strext_flags(a4)
	bne.b	.2
	clr.l	d0
.2	move.l	d0,ptr1(a5)
.eleg	;moveq	#VSSpkt_Delay,d0	;delay
	;move.l	#mpaframedelay,d1
	;move.l	lport(a5),a0
	;move.l	strext_vhandle(a4),a1
	;move.l	vssh_port(a1),a1
	;bsr	doPkt
	btst	#strx_loadram,strext_flags(a4)
	beq.b	.el
	move.l	ptr1(a5),d0	;a betoltott reszbe
	cmp.l	ptr2(a5),d0	;seekeltunk?
	bcs.b	.0
.el	bset	#flg_start,flag(a5)	;elso toltes?
	bne.b	.0
	move.l	vssbase(a5),a6	;igen, DMA inditas
	move.l	strext_vhandle(a4),a0
	jsr	_LVOvssStart(a6)
	bclr	#flg_under,flag(a5)
.0	btst	#flg_eof,flag(a5)
	bne.b	.56
	bclr	#flg_under,flag(a5)
	beq.b	.56
	lea	d_underrun(pc),a0	;underrun!
	move.l	a0,d1
	moveq	#VSSpkt_PrintInfo,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt

.56	move.w	strext_fillcnt(a4),d0	;bufferek torlese hogy
	cmp.w	#mpbufclrpasses,d0	;ne zorogjon az AHI/Paula
	bcc.b	.59
	addq.w	#1,d0
	move.w	d0,strext_fillcnt(a4)
	cmp.w	#2,stream_channels(a2)	;stereo
	bne.b	.57
	move.l	4(a3),a0	;right channel
	move.l	conftmp+conf_mpfakecnt(a5),d0
	subq.l	#1,d0
.55	clr.w	(a0)+
	dbf	d0,.55
.57	move.l	(a3),a0	;left channel
	move.l	conftmp+conf_mpfakecnt(a5),d0
	subq.l	#1,d0
.58	clr.w	(a0)+
	dbf	d0,.58

.59	;lea	.hh(pc),a0	;mpbufeof kiirasa
	;move.l	mpbufeof(a5),d0
	;bsr.w	debugarg
	move.l	conftmp+conf_mpfakecnt(a5),d0	;*faked* sample count
	tst.l	mpbufeof(a5)	;vege van mar?
	bne.b	.ok
	btst	#flg_eof,flag(a5)
	beq.b	.ok
	lea	d_mpgplayeof(pc),a0
	move.l	a0,d1
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	moveq	#mpae_eof,d0
	bra.b	.ok

.baj	move.l	vssbase(a5),a6
	move.l	strext_vhandle(a4),a0
	jsr	_LVOvssFreeAudio(a6)
.nemjo	moveq	#mpae_mem,d0
	btst	#flg_patch,flag(a5)
	beq.b	.ok
	bset	#strx_touch,strext_flags(a4)	;nem seekelunk vissza
	move.l	mpabase(a5),a6
	;move.l	a2,a0
	move.l	a3,a1
	CALLOLD	MPEGA_decode_frame
.ok	movem.l	(sp)+,d2/a2-a6
	rts

;.hh	dc.b	'%ld',0
;	even

.mp3tags	dc.l	VSS_freq
.mp3tagsfreq	dc.l	0
	dc.l	VSS_hook
.mp3tagshook	dc.l	0
	dc.l	VSS_task
.mp3tagstask	dc.l	0
	dc.l	VSS_emphasis
.mp3tagsdeemp	dc.l	0
	dc.l	VSS_bits,0	;mp3 mode
	dc.l	TAG_DONE

; Write (ptr1): 0-nal tovabbmegy, nem tolt 0-ig	(bufferbe)
; Read  (ptr2): 0-nal megall, 0-ig olvas	(bufferbol kartyara)

; In D0.w:bytesreq A0:hook A5:ioaddr  Out:bytes_done

MPCONV_PRE	MACRO
	movem.l	d6-d7,-(sp)
	move.l	var(pc),a1
	beq.b	.nounder
	bset	#flg_under,flag(a1)
.nounder	clr.l	d6	;bytes done
	clr.l	d7
	move.w	d0,d7	;bytes req
.meg	move.l	ptr2(a1),d1
	move.l	d1,a0
	add.l	mpbuffer(a1),a0	;a0:olvasas kezdete
	move.l	ptr1(a1),d0
	sub.l	d1,d0
	beq.b	.ok
	bcc.b	.do
	move.l	mpbufsize(a1),d0	;vegeig!
	sub.l	d1,d0
.do	cmp.l	d7,d0
	bcs.b	.0
	move.l	d7,d0
.0	and.b	#$fc,d0
	sub.l	d0,d7
	add.l	d0,d6
	add.l	d0,d1	;bump pointer
	lsr.l	#2,d0
	subq.l	#1,d0
	bcs.b	.ok	;semmi!
	ENDM

MPCONV_POST	MACRO
	cmp.l	mpbufsize(a1),d1
	bcs.b	.2
	clr.l	d1
.2	move.l	d1,ptr2(a1)	;!!!! nem jo!
	tst.l	d7
	bne.b	.meg
.ok	move.l	d6,d0
	move.l	mpbufeof(a1),d1
	beq.b	.3
	sub.l	d6,d1
	bcc.b	.4
	clr.l	d1
.4	move.l	d1,mpbufeof(a1)
.3	movem.l	(sp)+,d6-d7
	rts
	ENDM


mp3hook	dc.l	0,0,.entry,0,0

.entry	MPCONV_PRE
.1	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	dbf	d0,.1
	MPCONV_POST

; ugyanez optimalizalva, word irassal

mp3opthook	dc.l	0,0,.entry,0,0

.entry	MPCONV_PRE
.1	move.w	(a0)+,(a5)
	move.w	(a0)+,(a5)
	dbf	d0,.1
	MPCONV_POST


; In A4:strext
; stream access hook hivasa

callhook	movem.l	d2-d7/a2-a6,-(sp)
	move.l	strext_hook(a4),a0	;a0: hook
	lea	strext_access(a4),a1	;a1: access
	move.l	strext_fhandle(a4),a2	;a2: handle
	move.l	h_Entry(a0),a3
	jsr	(a3)
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; In A0:stream D0:timepos

mpaMPEGA_seek	movem.l	d2/a2-a6,-(sp)
	move.l	var(pc),a5

	CHKMAGIC	MPEGA_seek

	move.l	a0,a2
	move.l	d0,d2
	move.l	stream_extra(a2),a4

	move.l	d2,-(sp)	;debug
	moveq	#VSSpkt_PrintDebug,d0
	lea	d_mpaseek(pc),a0
	move.l	a0,d1
	divu.l	#1000,d2
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	(sp)+,d2

	btst	#flg_patch,flag(a5)
	bne.b	.patch

	move.l	strext_filesiz(a4),d0	;sajat seek rutin
	beq.w	.ok	;pozicio szamitasa
	mulu.l	d0,d1:d2
	move.l	stream_ms_duration(a2),d0
	beq.w	.ok
	divu.l	d0,d1:d2
	move.l	d2,strext_h_seekpos(a4)
	btst	#strx_loadram,strext_flags(a4)
	bne.b	.do
	move.l	#acc_seek,strext_access+acc_func(a4)
	move.l	d2,strext_h_pos(a4)
	move.l	d2,strext_access+acc_seekpos(a4)
	bsr.w	callhook
	bra.b	.do

.patch	move.b	strext_flags(a4),d1
	move.l	d2,d0
	beq.b	.2
	bset	#strx_touch,d1
.2	btst	#strx_loadram,d1
	beq.b	.3
	bset	#strx_noseek,d1
.3	move.b	d1,strext_flags(a4)
	move.l	mpabase(a5),a6	;eredeti seek hivasa
	CALLOLD	MPEGA_seek
	bclr	#strx_noseek,strext_flags(a4)

.do	cmp.l	stream(a5),a2	;active stream?
	bne.b	.ok
	move.l	d0,a2
	move.l	vssbase(a5),a6
	move.l	strext_vhandle(a4),a0
	jsr	_LVOvssStop(a6)
	move.l	strext_vhandle(a4),a0
	jsr	_LVOvssFlush(a6)
	btst	#strx_loadram,strext_flags(a4)
	beq.b	.0

	move.l	strext_h_seekpos(a4),d0	;load ram, ptr kerekites
	addq.l	#4,d0
	and.b	#$f8,d0
	move.l	d0,ptr2(a5)

	move.l	mpbufcorr(a5),d1	;a nullakat a vegen nem
	bne.b	.33	;szamoljuk!
	move.l	ptr1(a5),d1
.33	sub.l	d0,d1
	bcc.b	.d1
	add.l	mpbufsize(a5),d1
.d1	clr.l	d0	;hardware buffer
	move.b	bufsize(a5),d0
	asl.l	#7,d0
	add.l	d1,d0
	move.l	d0,mpbufeof(a5)
	bra.b	.1

.0	clr.l	ptr1(a5)	;clear and refill
	clr.l	ptr2(a5)
	bclr	#flg_eof,flag(a5)
.1	bclr	#flg_start,flag(a5)
	move.l	a2,d0
.ok	movem.l	(sp)+,d2/a2-a6
	rts

; In A0:stream A1:pointer to pos

mpaMPEGA_time	movem.l	d2/a2-a6,-(sp)
	move.l	var(pc),a5

	CHKMAGIC	MPEGA_time

	move.l	a0,a2
	move.l	a1,a3
	move.l	stream_extra(a2),a4

	;move.l	#VSSpkt_PrintDebug,d0	;debug
	;lea	d_mpatime(pc),a0
	;move.l	a0,d1
	;move.l	cport(a5),a0
	;sub.l	a1,a1
	;bsr.w	sendPkt

	cmp.l	stream(a5),a2
	bne.b	.nem

	move.l	vssbase(a5),a6	;sajat rutin
	move.l	strext_vhandle(a4),a0
	jsr	_LVOvssBuffered(a6)	;hardware buffer
	neg.l	d0
	add.l	strext_h_pos(a4),d0
	bpl.b	.1
	clr.l	d0
.1	move.l	ptr1(a5),d1	;software buffer
	sub.l	ptr2(a5),d1
	bcc.b	.0
	add.l	mpbufsize(a5),d1
.0	sub.l	d1,d0
	bpl.b	.2
	clr.l	d0
.2	move.l	stream_ms_duration(a2),d1	;ido szamitasa
	beq.b	.ok1
	mulu.l	d1,d2:d0
	move.l	strext_filesiz(a4),d1
	beq.b	.ok1
	divu.l	d1,d2:d0
	add.l	conftmp+conf_mptimecorr(a5),d0
	bmi.b	.ok1
	move.l	d0,(a3)

.ok1	clr.l	d0
	bra.b	.ok

.nem	clr.l	d0	;eredeti time hivasa
	btst	#flg_patch,flag(a5)
	beq.b	.ok
	move.l	a3,a1
	CALLOLD	MPEGA_time
.ok	movem.l	(sp)+,d2/a2-a6
	rts

; In A0:buffer D0:size

mpaMPEGA_find_sync
	movem.l	d2/a2/a5-a6,-(sp)
	move.l	var(pc),a5
	btst	#flg_patch,flag(a5)
	beq.b	.0
	move.l	oldMPEGA_find_sync(a5),a2
	jsr	(a2)
	bra.b	.1
.0	sub.l	a1,a1
	sub.l	a2,a2
	bsr.w	findsync
.1	move.l	d0,d2	;debug
	moveq	#VSSpkt_PrintDebug,d0
	lea	d_mpafindsync(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	d2,d0
	movem.l	(sp)+,d2/a2/a5-a6
	rts

; In A0:stream D0:scale

mpaMPEGA_scale	movem.l	d2/a2-a5,-(sp)
	move.l	var(pc),a5

	CHKMAGIC	MPEGA_scale

	move.l	a0,a2
	move.l	stream_extra(a2),a4
	move.l	d0,d2

	cmp.l	stream(a5),a2
	bne.b	.0
	move.l	conftmp+conf_flags(a5),d1
	btst	#cflg_mpavol,d1
	beq.b	.0
	sub.w	#100,d0	;notify control panel
	bcc.b	.2
	clr.l	d0
.2	cmp.w	#100,d0
	bcs.b	.1
	moveq	#100,d0
.1	move.b	d0,mixtab+VMT_4l(a5)
	move.b	d0,mixtab+VMT_4r(a5)
	move.l	vssbase(a5),a6
	move.l	strext_vhandle(a4),a0
	st	d0	;async processing
	jsr	_LVOvssRefreshMixer(a6)

.0	move.l	#VSSpkt_PrintDebug,d0	;debug
	lea	d_mpascale(pc),a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt

	clr.l	d0
	btst	#flg_patch,flag(a5)
	beq.b	.ok
	move.l	d2,d0
	CALLOLD	MPEGA_scale
.ok	movem.l	(sp)+,d2/a2-a5
	rts

; In A0:buffer A1:stream A2:strext D0:bufsize  Out D0:index/error

findsync	movem.l	d1-d7/a0-a3,-(sp)
	move.l	a1,d1
	bne.b	.22
	lea	mpastrbuf(pc),a1
.22	move.l	a2,d1
	bne.b	.33
	lea	mpastrextbuf(pc),a2
.33	moveq	#-1,d7
	clr.l	d2
	subq.l	#4,d0
	bmi.w	.nincs
.0	move.l	(a0),d1
	addq.l	#1,a0
	addq.l	#1,d7
	cmp.l	d7,d0
	beq.w	.nincs
	rol.l	#8,d1	;$ff
	cmp.b	#$ff,d1
	bne.b	.0
	rol.l	#3,d1	;meg 3 1bit
	move.b	d1,d2
	and.b	#7,d2
	cmp.b	#7,d2
	bne.b	.0
	rol.l	#2,d1	;version 1, 2, 2.5 (=2)
	move.b	d1,d2
	and.b	#3,d2
	seq	d4
	move.w	#1,d3	;1
	cmp.b	#3,d2
	beq.b	.1
	cmp.b	#1,d2	;reserved
	beq.b	.0
	move.w	#2,d3
.1	move.w	d3,stream_norm(a1)
	rol.l	#2,d1	;layer 1, 2, 3
	move.b	d1,d2
	and.b	#3,d2
	beq.b	.0
	moveq	#4,d3
	sub.b	d2,d3
	move.w	d3,stream_layer(a1)
	rol.l	#1,d1	;protection
	move.b	d1,d2
	and.b	#1,d2
	move.w	d2,strext_crcprot(a2)
	rol.l	#4,d1	;bitrate index
	move.b	d1,d2
	and.b	#15,d2
	beq.b	.0	;free
	cmp.b	#15,d2	;reserved
	beq.b	.0
	move.w	stream_norm(a1),d3	;tablazat offszet
	asl.w	#4,d3
	add.w	d3,d2
	move.w	stream_layer(a1),d3
	asl.w	#5,d3
	add.w	d3,d2
	lea	kbpstable(pc),a3
	move.w	(a3,d2.w*2,-96),stream_bitrate(a1)
	rol.l	#2,d1	;sampling frequency
	move.b	d1,d2
	and.b	#3,d2
	cmp.b	#3,d2	;illegal
	beq.w	.0
	move.l	#44100,d3
	cmp.b	#0,d2
	beq.b	.2
	move.l	#48000,d3
	cmp.b	#1,d2
	beq.b	.2
	move.l	#32000,d3
.2	move.w	stream_norm(a1),d0
	cmp.w	#1,d0
	beq.b	.3
	lsr.l	#1,d3
	tst.b	d4
	beq.b	.3
	lsr.l	#1,d3
.3	move.l	d3,stream_frequency(a1)
	move.l	d3,stream_dec_frequency(a1)
	rol.l	#2,d1	;padding, private
	move.b	d1,d2
	and.b	#3,d2
	move.w	d2,strext_padpriv(a2)
	rol.l	#2,d1	;mode
	move.b	d1,d2
	and.b	#3,d2
	move.w	d2,stream_mode(a1)
	move.w	#2,d3
	cmp.b	#3,d2
	bne.b	.5
	move.w	#1,d3
.5	move.w	d3,stream_channels(a1)
	move.w	d3,stream_dec_channels(a1)
	rol.l	#3,d1	;(modext) copyright
	move.b	d1,d2
	and.b	#1,d2
	move.w	d2,stream_copyright(a1)
	rol.l	#1,d1	;original
	move.b	d1,d2
	and.b	#1,d2
	move.w	d2,stream_original(a1)
	rol.l	#2,d1	;emphasis
	move.b	d1,d2
	and.b	#3,d2
	move.w	d2,strext_emphasis(a2)
	move.w	stream_bitrate(a1),d2	;frame hossz szamitasa
	mulu.w	#1000,d2
	mulu.l	#144,d2
	divu.l	stream_frequency(a1),d2
	move.w	strext_padpriv(a2),d3
	lsr.w	#1,d3
	add.w	d3,d2
	move.w	d2,strext_framesize(a2)
	move.l	strext_filesiz(a2),d3	;ido szamitasa
	asl.l	#3,d3
	move.w	stream_bitrate(a1),d2
	divu.l	d2,d3
	move.l	d3,stream_ms_duration(a1)
	move.l	d7,d0
	bra.b	.ok
.nincs	moveq	#mpae_nosync,d0
.ok	movem.l	(sp)+,d1-d7/a0-a3
	rts

mpastrbuf	dcb.b	stream_size,0
mpastrextbuf	dcb.b	strext_size,0
	even

kbpstable
; V1L1
	dc.w	0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0
; V2L1
	dc.w	0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0
	;dc.w	0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0
; V1L2
	dc.w	0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0
; V2L2
	dc.w	0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0
	;dc.w	0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0
; V1L3
	dc.w	0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0
; V2L3
	;dc.w	0,8,16,24,32,64,80,56,64,128,160,112,128,256,320,0
	dc.w	0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0


;----------< pointer to variable area >------------------------------------------

var	dc.l	0	;pointer to variables


;----------< the main program >--------------------------------------------------

mainprog

; atmenetileg letiltott, meg nem mukodo gadgetek

	seti	gad_ahivol(a5),#MUIA_Disabled,#TRUE
	seti	gad_delflush(a5),#MUIA_Disabled,#TRUE
	seti	gad_ahimpsupp(a5),#MUIA_Disabled,#TRUE
	seti	gad_melint(a5),#MUIA_Disabled,#TRUE
	seti	gad_default(a5),#MUIA_Disabled,#TRUE

; update mixer gadgets and options checkboxes

	moveq	#-1,d4
	bsr.w	main_mixupdate
	bsr.w	sam_mixupdate
	bsr.w	checkboxupd

; update channel name strings (options)

	lea	cnames+cname0(a5),a3
	seti	gad_name0(a5),#MUIA_String_Contents,a3
	lea	cnames+cname1(a5),a3
	seti	gad_name1(a5),#MUIA_String_Contents,a3
	lea	cnames+cname2(a5),a3
	seti	gad_name2(a5),#MUIA_String_Contents,a3
	lea	cnames+cname3(a5),a3
	seti	gad_name3(a5),#MUIA_String_Contents,a3
	lea	cnames+cname4(a5),a3
	seti	gad_name4(a5),#MUIA_String_Contents,a3

; update channel names (mixer)

	lea	cnames+cname0(a5),a3
	seti	gad_chname0(a5),#MUIA_Text_Contents,a3
	lea	cnames+cname1(a5),a3
	seti	gad_chname1(a5),#MUIA_Text_Contents,a3
	lea	cnames+cname2(a5),a3
	seti	gad_chname2(a5),#MUIA_Text_Contents,a3
	lea	cnames+cname3(a5),a3
	seti	gad_chname3(a5),#MUIA_Text_Contents,a3
	lea	cnames+cname4(a5),a3
	seti	gad_chname4(a5),#MUIA_Text_Contents,a3

; update debuglevel cycle

	clr.l	d3
	move.b	debuglevel(a5),d3
	seti	gad_debugl(a5),#MUIA_Cycle_Active,d3

; update ahi & mp3 options

          seti	gad_ahibuf(a5),#MUIA_Slider_Level,conftmp+conf_ahibuftime(a5)
          seti	gad_melbuf(a5),#MUIA_Slider_Level,conftmp+conf_melbuftime(a5)
	move.l	conftmp+conf_mpbufsize(a5),d3
	lsr.l	#8,d3
	lsr.l	#2,d3
	seti	gad_mpbufsize(a5),#MUIA_Slider_Level,d3
	move.l	conftmp+conf_mptimecorr(a5),d3
	seti	gad_mptimecorr(a5),#MUIA_Slider_Level,d3
	seti	gad_quantum(a5),#MUIA_Slider_Level,quantum(a5)
	move.w	conftmp+conf_pri(a5),d3
	ext.l	d3
	seti	gad_llpri(a5),#MUIA_Slider_Level,d3
	move.w	conftmp+conf_spri(a5),d3
	ext.l	d3
	seti	gad_spri(a5),#MUIA_Slider_Level,d3

; update sampler gadgets

	seti	gad_sstart(a5),#MUIA_Disabled,#TRUE
	seti	gad_sstop(a5),#MUIA_Disabled,#TRUE
	seti	gad_sclose(a5),#MUIA_Disabled,#TRUE
	clr.l	d3
	move.b	conftmp+conf_samdeclev(a5),d3
	seti	gad_samdeclev(a5),#MUIA_Slider_Level,d3
	move.b	conftmp+conf_levdiv(a5),d3
	seti	gad_levmode(a5),#MUIA_Cycle_Active,d3
          seti	gad_sbuf(a5),#MUIA_Slider_Level,conftmp+conf_sambufsize(a5)
	move.w	conftmp+conf_guiquantum(a5),d3
	seti	gad_guiquantum(a5),#MUIA_Slider_Level,d3

; popup/iconify (as given in the tooltypes/arguments)

	tst.b	startpopup(pc)
	bne.w	loop
	tst.b	starthide(pc)
	beq.b	.open
	seti	gui_app(a5),#MUIA_Application_Iconified,#TRUE
	bra.b	.openit
.open	seti	gui_app(a5),#MUIA_Application_Iconified,#FALSE
.openit	seti	gui_window(a5),#MUIA_Window_Open,#TRUE

loop	clr.l	sigrcvd(a5)	;main loop
	move.l	gui_signal(a5),d0
	beq.b	.gui1
	or.l	cport_sig(a5),d0
	or.l	updatsig(a5),d0
	or.w	#$b000,d0
	move.l	excbase(a5),a6
	jsr	_LVOWait(a6)
	move.l	d0,sigrcvd(a5)
	beq.b	loop

	move.l	gui_signal(a5),d1	;gui messages
	and.l	d0,d1
	beq.b	.updat
	move.l	d0,gui_signal(a5)
.gui1	lea	gui_signal(a5),a1
	DoMethod	gui_app(a5),#MUIM_Application_NewInput,a1
	cmp.l	#MUIV_Application_ReturnID_Quit,d0
	beq.w	.quit
	tst.l	gui_signal(a5)
	beq.b	.gui1

.updat	move.l	sigrcvd(a5),d0	;gui update
	and.l	updatsig(a5),d0
	beq.b	.msg
	bsr.w	playupdate	;player
	bsr.w	guiupdate	;sampler

;	bset	#lfl_monitor,ll_flags(a5)
;	clr.l	d0
;	move.b	sam_ilevel(a5),d0
;	move.l	d0,-(sp)
;	move.l	sp,a1
;	lea	.jj(pc),a0
;	bsr	printLine
;	addq.l	#4,sp
;	bra	.gg
;.jj	dc.b	'%02.2lx',0
;	even
;.gg
	;move.l	excbase(a5),a6

.msg	move.l	sigrcvd(a5),d0
	and.l	cport_sig(a5),d0
	beq.b	.popup
.msg1	move.l	cport(a5),a0	;packet
	bsr.w	getPkt
	beq.b	.popup
	move.l	a1,-(sp)
	move.l	pkt_cmd(a1),d0
	move.l	pkt_arg(a1),a0
	lea	pkt_args(a1),a1
	bsr.b	.prtpck
	move.l	(sp)+,a1
	bsr.w	replyPkt
	bra.b	.msg1
.prtpck	subq.l	#VSSpkt_PrintFault,d0
	beq.w	printFault
	subq.l	#1,d0
	beq.w	printInfo
	subq.l	#1,d0
	beq.w	printDebug
	subq.l	#1,d0
	beq.b	.callhook
	moveq	#VSSERR_BadPkt,d0	;unexpected packet
	bsr.w	panic
	illegal

.callhook	jsr	(a0)
	move.l	excbase(a5),a6
	rts

.popup	move.l	sigrcvd(a5),d0	;ctrl-f, popup
	and.w	#$8000,d0
	beq.b	.mixupdate
	bsr.l	popup

.mixupdate	move.l	sigrcvd(a5),d0
	and.w	#$2000,d0
	beq.b	.quit1
	move.l	main_notify(a5),a0
	move.l	vssn_channelchg(a0),d4
	clr.l	vssn_channelchg(a0)
	bsr.w	main_mixupdate

.quit1	move.l	sigrcvd(a5),d0
	and.w	#$1000,d0
	beq.w	loop

.quit	move.l	excbase(a5),a6
	bclr	#flg_enable,flag(a5)	;disable library openings
	clr.l	d2	;check if libraries are
	clr.l	d3	;opened or not
	clr.l	d4
	btst	#flg_patch,flag(a5)
	bne.b	.1

	move.l	mpabase(a5),d0
	move.l	d0,a0
	beq.b	.1
	move.w	LIB_OPENCNT(a0),d3
.1	move.l	vssbase(a5),d0
	move.l	d0,a0
	beq.b	.l1
	move.w	LIB_OPENCNT(a0),d2
.l1	move.l	vahbase(a5),d0
	move.l	d0,a0
	beq.b	.l2
	move.w	LIB_OPENCNT(a0),d4
.l2	move.l	d2,d0
	or.l	d3,d0
	or.l	d4,d0
	or.l	samplerproc(a5),d0
	beq.b	.quitok
	move.l	mpabase(a5),d0
	move.l	d0,a0
	beq.b	.l3
	move.w	LIB_OPENCNT(a0),d3
	btst	#flg_patch,flag(a5)
	beq.b	.l3
	subq.l	#1,d3	;a driver is megnyitotta

.l3	bset	#flg_enable,flag(a5)	;print open counts
	movem.l	d2-d4,-(sp)
	move.l	sp,a1
	lea	e_cannot(pc),a0
	bsr.w	printFault
	lea	12(sp),sp
	lea	handsem(a5),a0	;print active handles
	jsr	_LVOObtainSemaphore(a6)
	lea	handlist(a5),a3
.2	move.l	(a3),a3
	move.l	(a3),d0
	beq.b	.3
	lea	i_handle(pc),a0
	lea	LN_NAME(a3),a1
	bsr.w	printInfo
	bra.b	.2
.3	lea	handsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	bra.w	loop

.quitok	move.l	cport(a5),a0	;OK, wipe messages
	bsr.w	getPkt
	beq.b	.quitok3
	bsr.w	replyPkt
	bra.b	.quitok
.quitok3	bsr.l	hide	;now really safe to quit!
	sub.l	a4,a4
	rts


main_mixupdate

; 0..9    channels     Chn1..4, Digital
; 10..11  trebfreq     hi..lo
; 12..13  bassfreq     hi..lo
; 14      trebenhance
; 15      bassenhance
; 16      toneatten
; 17      channelmap

	clr.l	d3
	clr.l	d5
	btst	#8,d4
	beq.b	.0
	st	d5
	move.b	mixtab+VMT_4l(a5),d3
	seti	gad_s0(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.0	btst	#9,d4
	beq.b	.1
	st	d5
	move.b	mixtab+VMT_4r(a5),d3
	seti	gad_s1(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.1	tst.b	d5
	beq.b	.no
	bsr.w	setplayvolbal
.no	btst	#0,d4
	beq.b	.2
	move.b	mixtab+VMT_0l(a5),d3
	seti	gad_s2(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.2	btst	#1,d4
	beq.b	.3
	move.b	mixtab+VMT_0r(a5),d3
	seti	gad_s3(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.3	btst	#2,d4
	beq.b	.4
	move.b	mixtab+VMT_1l(a5),d3
	seti	gad_s4(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.4	btst	#3,d4
	beq.b	.5
	move.b	mixtab+VMT_1r(a5),d3
	seti	gad_s5(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.5	btst	#4,d4
	beq.b	.6
	move.b	mixtab+VMT_2l(a5),d3
	seti	gad_s6(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.6	btst	#5,d4
	beq.b	.7
	move.b	mixtab+VMT_2r(a5),d3
	seti	gad_s7(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.7	btst	#6,d4
	beq.b	.8
	move.b	mixtab+VMT_3l(a5),d3
	seti	gad_s8(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.8	btst	#7,d4
	beq.b	.9
	move.b	mixtab+VMT_3r(a5),d3
	seti	gad_s9(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.9

; update channel mode cycles

	move.w	gui_modes+cmode0(a5),d3
	seti	gad_mm0(a5),#MUIA_Cycle_Active,d3
	move.w	gui_modes+cmode1(a5),d3
	seti	gad_mm1(a5),#MUIA_Cycle_Active,d3
	move.w	gui_modes+cmode2(a5),d3
	seti	gad_mm2(a5),#MUIA_Cycle_Active,d3
	move.w	gui_modes+cmode3(a5),d3
	seti	gad_mm3(a5),#MUIA_Cycle_Active,d3
	move.w	gui_modes+cmode4(a5),d3
	seti	gad_mm4(a5),#MUIA_Cycle_Active,d3

	btst	#14,d4
	beq.b	.14
	move.b	mixtab+VMT_trebenhance(a5),d3
	extb.l	d3
	seti	gad_ms4(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.14	btst	#15,d4
	beq.b	.15
	move.b	mixtab+VMT_bassenhance(a5),d3
	extb.l	d3
	seti	gad_ms2(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.15	clr.l	d3
	btst	#16,d4
	beq.b	.16
	move.b	mixtab+VMT_toneatten(a5),d3
	seti	gad_ms5(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.16	btst	#17,d4
	beq.b	.17
	move.b	mixtab+VMT_channelmap(a5),d3
	extb.l	d3
	seti	gad_ms6(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.17	clr.l	d3
	btst	#12,d4
	bne.b	.12
	btst	#13,d4
	beq.b	.13
.12	move.w	mixtab+VMT_bassfreq(a5),d3
	seti	gad_ms1(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	;seti	gad_ms1(a5),#MUIA_Slider_Level,d3
.13	btst	#10,d4
	bne.b	.10
	btst	#11,d4
	beq.b	.11
.10	move.w	mixtab+VMT_trebfreq(a5),d3
	seti	gad_ms3(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	;seti	gad_ms3(a5),#MUIA_Slider_Level,d3
.11	rts

; update enable/interrupt/volacc/other checkboxes

checkboxupd	clr.l	d3
	move.l	conftmp+conf_flags(a5),d4
	; enable
	btst	#cflg_ahiena,d4
	sne	d3
	seti	gad_ahiena(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	btst	#cflg_melena,d4
	sne	d3
	seti	gad_melena(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	btst	#cflg_mpaena,d4
	sne	d3
	seti	gad_mpaena(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	; interrupt
	btst	#cflg_ahiint,d4
	sne	d3
	seti	gad_ahiint(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	btst	#cflg_melint,d4
	sne	d3
	seti	gad_melint(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	btst	#cflg_mpaint,d4
	sne	d3
	seti	gad_mpaint(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	; volume
	btst	#cflg_ahivol,d4
	sne	d3
	seti	gad_ahivol(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	btst	#cflg_melvol,d4
	sne	d3
	seti	gad_melvol(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	btst	#cflg_mpavol,d4
	sne	d3
	seti	gad_mpavol(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	; mp3
	btst	#cflg_mploadlib,d4
	sne	d3
	seti	gad_mpload(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	btst	#cflg_nostrsw,d4
	sne	d3
	seti	gad_mpassw(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	btst	#cflg_mpaloadram,d4
	sne	d3
	seti	gad_mploadram(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	; sampler
	btst	#cflg_levelhist,d4
	sne	d3
	seti	gad_histogram(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	btst	#cflg_samappend,d4
	sne	d3
	seti	gad_samappend(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	; player
	btst	#cflg_playloop,d4
	sne	d3
	seti	gad_loop(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	; vss
	btst	#cflg_deemphasis,d4
	sne	d3
	seti	gad_endeemp(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,d3
	rts

; Sor formazasa es kiirasa a shellbe
; (printFault megnyitja a GUI-t is)

; In A0:format A1:data

printDebug	cmp.b	#debug_debug,debuglevel(a5)
	bcc.w	printLine
	rts
printInfo	cmp.b	#debug_info,debuglevel(a5)
	bcc.w	printLine
	rts
printFault	movem.l	d3/a2-a3/a6,-(sp)
	movem.l	a0-a1,-(sp)
	seti	gui_window(a5),#MUIA_Window_Open,#TRUE
	seti	gui_app(a5),#MUIA_Application_Iconified,#FALSE
	seti	gui_register(a5),#MUIA_Group_ActivePage,#1
	movem.l	(sp)+,a0-a1
	bra.b	printIt2
printLine	movem.l	d3/a2-a3/a6,-(sp)
printIt2	move.l	excbase(a5),a6
	lea	.putchproc(pc),a2
	lea	formatbuf(a5),a3
	jsr	_LVORawDoFmt(a6)
	;seti	gad_shellist(a5),#MUIA_List_Quiet,#TRUE
.2	move.l	a3,a2
.0	move.b	(a2)+,d0
	bne.b	.4
.5	bsr.b	.1
	;seti	gad_shellist(a5),#MUIA_List_Quiet,#FALSE
	movem.l	(sp)+,d3/a2-a3/a6
	rts
.4	cmp.b	#10,d0
	bne.b	.0
	clr.b	-1(a2)
	tst.b	(a2)
	beq.b	.5
	bsr.b	.1
	move.l	a2,a3
	bra.b	.2
.1	move.l	a2,-(sp)
 DoMethod gad_shellist(a5),#MUIM_List_InsertSingle,a3,#MUIV_List_Insert_Bottom
	add.w	#1,gui_shlines(a5)
	cmp.w	#shlines,gui_shlines(a5)
	bcs.w	.3
; elso sorok torlese
	seti	gad_shellist(a5),#MUIA_List_Quiet,#TRUE
	moveq	#shlinrem-1,d3
	sub.w	#shlinrem,gui_shlines(a5)
.6 DoMethod gad_shellist(a5),#MUIM_List_Remove,#MUIV_List_Remove_First
	dbf	d3,.6
	seti	gad_shellist(a5),#MUIA_List_Quiet,#FALSE
.3  seti  gad_shellist(a5),#MUIA_List_Active,#MUIV_List_ActiveBottom
 DoMethod gad_shellist(a5),#MUIM_List_Jump,#MUIV_List_Jump_Active
	move.l	(sp)+,a2
	rts
.putchproc	move.b	d0,(a3)+
	rts


;----------< Graphical User Interface >------------------------------------------

; sajat tagek MUI object generalashoz

	ENUM	TAG_USER!$10000
	EITEM	dummytag
	EITEM	MUIA_My_Type
	EITEM	MUIA_My_Form
	EITEM	MUIA_My_FormHook
	EITEM	MUIA_My_DispHook
	EITEM	MUIA_My_Im1
	EITEM	MUIA_My_Im2
	EITEM	MUIA_My_InputMode
	EITEM	MUIA_My_Divider
	EITEM	MUIA_My_Flags

; Ezek az amiga.lib-ben vannak definialva, de ezekert felesleges lenne linkelni

; In stack:object, message

_DoMethod	move.l	a2,-(sp)
	move.l	8(sp),a2	;object A2
	move.l	a2,d0
	beq.b	.0
	lea	12(sp),a1	;message A1
	move.l	-4(a2),a0	;obj->class A0
	move.l	h_Entry(a0),a6
	jsr	(a6)
.0	move.l	(sp)+,a2
	rts

; In A0:Class A1:message A2:object

;DoSuperMethodA	movem.l	a0-a1/a6,-(sp)
;	move.l	cl_Super(a0),a0	;superclass A0
;	move.l	h_Entry(a0),a6
;	jsr	(a6)
;.0	movem.l	(sp)+,a0-a1/a6
;	rts

; Ez a ket makro generalja az objektumunkat, ugyanugy mint a
; SliderObject/Endi

MySliderObject	MACRO
	movem.l	a0/a2,-(sp)
	move.l	gui_mysliderclass(a5),a0
	move.l	a4,a2
	ENDM

MyBodyObject	MACRO
	movem.l	a0/a2,-(sp)
	move.l	gui_mybodyclass(a5),a0
	move.l	a4,a2
	ENDM

MyEndi	MACRO
	clr.l	(a4)+
	sub.l	a1,a1
	move.l	intbase(a5),a6
	jsr	_LVONewObjectA(a6)
	move.l	a2,a4
	movem.l	(sp)+,a0/a2
	cmv2	d0
	ENDM

MyImage	MACRO
	MyBodyObject
	 MUIT MUIA_My_Im1,\1
	 MUIT MUIA_My_Im2,\2
	MyEndi
	ENDM


; extended slider custom class dispatcher hook, ocsmanyul kodolva :(
; In A0:iclass A1:message A2:object

; Overrideolt metodusok:
;	OM_NEW
;	OM_SET
;	MUIM_Numeric_Stringify
;	MUIM_Numeric_ScaleToValue
;	MUIM_Numeric_Increase
;	MUIM_Numeric_Decrease

;Form
; exec/RawDoFmt hangulatu format-sztring
; (barmikor megvaltoztathato) (jelenleg ezt nem hasznaljuk ki, inkabb formhook)
;FormHook
; Ha nincs megadva Form, ez a hook meghivodik D0-ban az aktualis ertekkel,
; az eredmeny (D0) egy format sztring
;DispHook
; Ertek korrigalasa megjelenites elott. D0/D0
;Divider
; Ezzel mindig oszthato lesz az aktualis ertek
; (barmikor megvaltoztathato)

	STRUCTURE	odata,0
	ULONG	od_type	;parameters
	ULONG	od_divider
	ULONG	od_flags
	APTR	od_form
	APTR	od_formhook
	APTR	od_disphook

	ULONG	od_MethodID	;message
	ULONG	od_value
	STRUCT	od_buf,20	;buffer
	LABEL	od_size

slcf_nodragdiv	equ	0	;smooth dragging

sliderdisp	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5

; Megnezzuk hogy nem MUIA_My_Form/MUIA_My_Divider jott-e, mert azt updatelni kell

	cmp.l	#OM_SET,(a1)	;OM_SET
	bne.b	.noset
	move.l	ops_AttrList(a1),a3
	move.w	cl_InstOffset(a0),d0
	lea	(a2,d0.w),a4

	moveq	#od_form,d0
	cmp.l	#MUIA_My_Form,(a3)	;AttrID
	beq.b	.set
	moveq	#od_divider,d0
	cmp.l	#MUIA_My_Divider,(a3)
	bne.w	.callsuper

.set	move.l	ti_Data(a3),(a4,d0.w)
	bra.w	.ok

.noset

;; method debugger
;
;	cmp.l	gad_playtreble(a5),a2
;	bne	.nono
;	movem.l	a0-a1,-(sp)
;	move.l	(a1),d0
;	lea	.hhhh(pc),a0
;	bsr	debugarg
;	movem.l	(sp)+,a0-a1
;.nono	bra	.nonono
;.hhhh	dc.b	'%08.8lx',0
;	even
;.nonono

	cmp.l	#MUIM_Numeric_ScaleToValue,(a1)
	bne.w	.nostv

;; kiirjuk a debug konzolra az eredmenyt es a 3 argumentumot
;
;	move.l	12(a1),-(sp)
;	move.l	8(a1),-(sp)
;	move.l	4(a1),-(sp)
;	move.l	cl_Super(a0),a0
;	move.l	h_Entry(a0),a6
;	jsr	(a6)
;	move.l	d0,-(sp)
;	lea	.hhh(pc),a0
;	move.l	sp,a1
;	bsr	debugmarg
;	move.l	(sp)+,d0
;	lea	12(sp),sp
;	bra	.ok
;.hhh	dc.b	'stv %10ld %10ld %10ld %10ld',10,0
;	even

	move.w	cl_InstOffset(a0),d0
	move.l	(a2,d0.w,od_flags),d1
	btst	#slcf_nodragdiv,d1
	bne.w	.callsuper
	move.l	(a2,d0.w,od_divider),d2
	beq.w	.callsuper
	move.l	cl_Super(a0),a0
	move.l	h_Entry(a0),a6
	jsr	(a6)

	move.l	d0,d1
	divs.w	d2,d1
	swap	d1
	ext.l	d1
	sub.l	d1,d0
	bra	.ok

; Increase/Decrease
; A superclass hivasa elott megszorozzuk az amount-ot a divider-rel, ha az nem
; nulla. (Nem valami elegans megoldas atirni a message-t, de mukodik es gyors)
; Utana termeszetesen visszaallitjuk az eredeti erteket.

.nostv	cmp.l	#MUIM_Numeric_Increase,(a1)
	beq.b	.inc
	cmp.l	#MUIM_Numeric_Decrease,(a1)
	bne.b	.noinc
.inc	move.l	4(a1),d2	;amount
	movem.l	a1-a2,-(sp)
	move.l	d2,d1
	move.w	cl_InstOffset(a0),d0
	move.l	(a2,d0.w,od_divider),d0
	beq.b	.0
	muls.w	d0,d1
	move.l	d1,4(a1)
.0	move.l	cl_Super(a0),a0
	move.l	h_Entry(a0),a6
	jsr	(a6)
	movem.l	(sp)+,a1-a2
	move.l	d2,4(a1)
	bra	.ok

.noinc	cmp.l	#OM_NEW,(a1)	;OM_NEW
	bne.b	.nonew
	move.l	a0,a3	;class
	move.l	a1,a4	;message
	move.l	cl_Super(a0),a0	;call superclass
	move.l	h_Entry(a0),a6
	jsr	(a6)
	move.l	d0,a2
	tst.l	d0
	beq.w	.ok
	move.l	d0,-(sp)
	move.w	cl_InstOffset(a3),d0	;prepare message
	lea	(a2,d0.w),a0
	move.l	#MUIM_Numeric_Stringify,od_MethodID(a0)
	move.l	ops_AttrList(a4),a2	;parse tags
	lea	.packtable(pc),a1
	move.l	utibase(a5),a6
	jsr	_LVOPackStructureTags(a6)
	move.l	(sp)+,d0
	bra.b	.ok

.nonew	cmp.l	#MUIM_Numeric_Stringify,(a1)	;msg_MethodID=0
	bne.b	.callsuper
	move.w	cl_InstOffset(a0),d0	;MUIM_Numeric_Stringify
	lea	(a2,d0.w),a3
	move.l	4(a1),d0	;value
	move.l	od_disphook(a3),d1
	beq.b	.nohook
	move.l	a0,-(sp)
	move.l	d1,a0
	move.l	h_Entry(a0),a4	;DispHook
	jsr	(a4)
	move.l	(sp)+,a0
.nohook	move.l	d0,od_value(a3)
	lea	od_MethodID(a3),a1
	move.l	od_form(a3),d1
	bne.b	.form
	move.l	od_formhook(a3),d1
	beq.b	.callsuper
	move.l	d1,a0
	move.l	h_Entry(a0),a4
	jsr	(a4)
	move.l	d0,d1
.form	lea	od_buf(a3),a3	;sajat formazas
	move.l	d1,a0
	addq.l	#4,a1
	move.l	excbase(a5),a6
	lea	.putchproc(pc),a2
	jsr	_LVORawDoFmt(a6)
	move.l	a3,d0
	bra.b	.ok

.callsuper	move.l	cl_Super(a0),a0
	move.l	h_Entry(a0),a6
	jsr	(a6)
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

.putchproc	move.b	d0,(a3)+
	rts

.packtable	dc.l	dummytag
	dc.w	$1000!(MUIA_My_Type-dummytag)
	dc.w	od_type-od_type
	dc.w	$1000!(MUIA_My_Divider-dummytag)
	dc.w	od_divider-od_type
	dc.w	$1000!(MUIA_My_Flags-dummytag)
	dc.w	od_flags-od_type
	dc.w	$1000!(MUIA_My_Form-dummytag)
	dc.w	od_form-od_type
	dc.w	$1000!(MUIA_My_FormHook-dummytag)
	dc.w	od_formhook-od_type
	dc.w	$1000!(MUIA_My_DispHook-dummytag)
	dc.w	od_disphook-od_type
	dc.l	0


; sajat bodychunk subclass, fix meret stb... player gadgetekhez
; megnyomasra image-t valt
; (A lenyeg hogy az imaget nem kulso fajlbol veszi, kulonben az image
; class is jo lenne, like SongPlayer!)

; Overrideolt methodok:
;	OM_NEW
;	MUIM_Draw

; Az image cseret lehet hogy nem a MUIM_Draw-ba kene tenni?
; (Igy muxik. OM_SET nem hivodik meg a megnyomaskor)

	STRUCTURE	bodata,0
	LABEL	bo_size

bodydisp	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	a0,a3
	move.l	a1,a4
	move.l	(a1),d0
	cmp.l	#OM_NEW,d0
	beq.b	.new
	cmp.l	#MUIM_Draw,d0
	bne.w	.dosuper

	clr.l	-(sp)	;MUIM_Draw
	move.l	a2,a0
	move.l	#MUIA_Selected,d0	;benyomva?
	move.l	sp,a1
	move.l	intbase(a5),a6
	jsr	_LVOGetAttr(a6)
	clr.l	d2
	move.l	(sp)+,d1
	seq	d2
	addq.b	#1,d2
	tst.l	d0	;unknown attribute,
	beq.w	.dosuper	;call superclass
	move.l	a2,a0	;page beallitasa
	clr.l	-(sp)
	move.l	d2,-(sp)
	move.l	#MUIA_Group_ActivePage,-(sp)
	move.l	sp,a1
	jsr	_LVOSetAttrsA(a6)
	lea	12(sp),sp
	bra.w	.dosuper

	; ezt egyszerubb lenne PackStructureTags()-al...
.new	move.l	utibase(a5),a6	;parameterek behozatala
	move.l	ops_AttrList(a4),d4	;[image1]
	move.l	#MUIA_My_Im1,d0
	clr.l	d1
	move.l	d4,a0
	jsr	_LVOGetTagData(a6)
	move.l	d0,d2
	move.l	#MUIA_My_Im2,d0	;[image2]
	clr.l	d1
	move.l	d4,a0
	jsr	_LVOGetTagData(a6)
	move.l	d0,d3
	move.l	#MUIA_My_InputMode,d0	;[inputmode]
	move.l	#MUIV_InputMode_RelVerify,d1
	move.l	d4,a0
	jsr	_LVOGetTagData(a6)
	move.l	d0,.groupinputm

	move.l	muibase(a5),a6	;make objects
	lea	MUIC_Bodychunk_s(pc),a0
	lea	.bodytags(pc),a1
	move.l	d2,.bodytags_body-.bodytags(a1)
	jsr	_LVOMUI_NewObjectA(a6)
	move.l	d0,.gr1
	beq.b	.ok
	lea	MUIC_Bodychunk_s(pc),a0
	lea	.bodytags(pc),a1
	move.l	d3,.bodytags_body-.bodytags(a1)
	jsr	_LVOMUI_NewObjectA(a6)
	move.l	d0,.gr2
	bne.b	.1
.err	move.l	.gr1(pc),a0
	jsr	_LVOMUI_DisposeObject(a6)
	clr.l	d0
	bra.b	.ok
.1	lea	.message(pc),a1
	bra.b	.ds2
.dosuper	move.l	a4,a1
.ds2	move.l	cl_Super(a3),a0	;call superclass
	move.l	h_Entry(a0),a6
	jsr	(a6)
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

.message	dc.l	OM_NEW	;MethodID
	dc.l	.grouptags	;ops_AttrList
	dc.l	0	;ops_GInfo

.grouptags	dc.l	MUIA_Group_PageMode,TRUE
	dc.l	MUIA_InputMode
.groupinputm	dc.l	0
	dc.l	MUIA_Group_Child
.gr1	dc.l	0
	dc.l	MUIA_Group_Child
.gr2	dc.l	0
	dc.l	MUIA_CycleChain,1
	dc.l	TAG_DONE

.bodytags	dc.l	MUIA_Bodychunk_Body
.bodytags_body	dc.l	0
	dc.l	MUIA_Bodychunk_Depth,3
	dc.l	MUIA_Bitmap_Precision,PRECISION_IMAGE
	dc.l	MUIA_Bitmap_SourceColors,gui_logocolors
	dc.l	MUIA_Bitmap_Width,24
	dc.l	MUIA_Bitmap_Height,13
	dc.l	MUIA_FixWidth,24
	dc.l	MUIA_FixHeight,13
	dc.l	TAG_DONE


;----------< Player >------------------------------------------------------------

; Gadgetek engedelyezese
;
; Mindig aktiv:	Eject, Loop, Intro, Cdda
;
; « Listahoz adas »
;
; Ha az elso fajlok:
;	+CloseFile
;	+SaveList
;	+ClearList
;	+Play
;	+Edit tag
; Ha az elso fajlok, tobb mint 1, VAGY elotte pont 1 volt:
;	+Next
;	+Random
;
; « CloseFile »	Meghivja Stop-ot
; « Play » 	-Play
;	+Stop
;	+Forward/Backward
;	+Pause
;
; « Stop »	+Play
;	-Stop
;	-Forward/Backward
;	-Pause
;
; « Next »
;
; Ha az elso fajlon alltunk:
;	-Prev
; Ha az utolson:
;	-Next

plf_eof	equ	0	;EOF reached/error
plf_under	equ	1	;buffer underrun
plf_start	equ	2	;started
plf_msg	equ	3	;message pending
plf_playing	equ	4	;playing
plf_play	equ	5	;continue playback
plf_seeking	equ	6
plf_pause	equ	7	;paused

plf2_trebfreq	equ	0
plf2_bassfreq	equ	1
plf2_dataread	equ	2
plf2_syncfound	equ	3

play_trysize	equ	1500
plymaxrec	equ	4	;max recursion depth


	STRUCTURE	PlayNode,MLN_SIZE
	STRUCT	pln_pc,pc_size
	APTR	pln_fullname
	APTR	pln_filename
	ULONG	pln_namelength
	ULONG	pln_duration	;sec
	ULONG	pln_filesize

	UBYTE	pln_flags

	STRUCT	pln_formbuf1,10	;duration
	STRUCT	pln_formbuf2,300	;infotext
	STRUCT	pln_realname,30	;name stored in the file
	LABEL	pln_size
	; path
	; name





playinit	seti	gad_cdda(a5),#MUIA_Disabled,#TRUE
	seti	gad_intro(a5),#MUIA_Disabled,#TRUE

	bsr.b	play_gadgets

	; bass/treble
	move.b	mixtab+VMT_trebenhance(a5),d2
	extb.l	d2
          seti	gad_playtreble(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2
	move.b	mixtab+VMT_bassenhance(a5),d2
	extb.l	d2
          seti	gad_playbass(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2
	bra.w	setplayvolbal


; gadgetek engedelyezese az allapottol fuggoen

play_gadgets	movem.l	d2-d5,-(sp)

;	clr.l	d0
;	move.w	ply_nodes(a5),d0
;	lea	.jj(pc),a0
;	bsr	debugarg
;	bra	.hh
;.jj	dc.b	'%ld',0
;	even
;.hh

	move.w	ply_nodes(a5),d3
	beq.w	.emptylist
	move.l	conftmp+conf_flags(a5),d5
	cmp.w	#1,d3
	bne	.13
	; ha csak 1 node van, mindenkepp tiltunk
	seti	gad_prev(a5),#MUIA_Disabled,#TRUE
	sf	d2
	bra.w	.12
.13	; ha tobb mint 1 node, nem az elso, vagy loop mode,
	; akkor prev engedelyezese
	clr.l	d2
	btst	#cflg_playloop,d5
	bne	.1
	move.w	ply_nodeindex(a5),d4
	seq	d2
.1	seti	gad_prev(a5),#MUIA_Disabled,d2
	; nem az utolso (vagy loop) akkor next is
	sf	d2
	btst	#cflg_playloop,d5
	bne	.12
	addq.w	#1,d4
	cmp.w	d3,d4
	seq	d2
.12	seti	gad_next(a5),#MUIA_Disabled,d2
	; playing?
	btst	#plf_playing,ply_flags(a5)
	seq	d2
	seti	gad_pause(a5),#MUIA_Disabled,d2
	seti	gad_backw(a5),#MUIA_Disabled,d2
	seti	gad_forw(a5),#MUIA_Disabled,d2
	seti	gad_stop(a5),#MUIA_Disabled,d2
	tst.b	d2
	seq	d2
	seti	gad_play(a5),#MUIA_Disabled,d2
	; list related
	seti	gad_savelist(a5),#MUIA_Disabled,#FALSE
	seti	gad_clearlist(a5),#MUIA_Disabled,#FALSE
	seti	gad_closefile(a5),#MUIA_Disabled,#FALSE
	;seti	gad_random(a5),#MUIA_Disabled,#FALSE
	;seti	gad_tagedit(a5),#MUIA_Disabled,#FALSE
	bra.w	.2
	; ures lista
.emptylist	seti	gad_pause(a5),#MUIA_Disabled,#TRUE
	seti	gad_prev(a5),#MUIA_Disabled,#TRUE
	seti	gad_next(a5),#MUIA_Disabled,#TRUE
	seti	gad_backw(a5),#MUIA_Disabled,#TRUE
	seti	gad_forw(a5),#MUIA_Disabled,#TRUE
	seti	gad_play(a5),#MUIA_Disabled,#TRUE
	seti	gad_stop(a5),#MUIA_Disabled,#TRUE
	seti	gad_savelist(a5),#MUIA_Disabled,#TRUE
	seti	gad_clearlist(a5),#MUIA_Disabled,#TRUE
	seti	gad_closefile(a5),#MUIA_Disabled,#TRUE
	seti	gad_tagedit(a5),#MUIA_Disabled,#TRUE
	seti	gad_random(a5),#MUIA_Disabled,#TRUE
	seti	gad_playinfo(a5),#MUIA_Text_Contents,#guit_text
.2	movem.l	(sp)+,d2-d5
	rts

; eject gadget
; eltaroljuk a kivalasztott path-ot, fajlokat befuzzuk a listaba

gui_ejecthook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	a1,a3	;aslreq mentese
	lea	-maxpathlen(sp),sp	;path masolasa
	move.l	fr_Drawer(a3),a0	;a verembe
	move.l	sp,a1
	move.l	#maxpathlen-1,d0
.0	move.b	(a0)+,(a1)+
	dbeq	d0,.0
	move.l	dosbase(a5),a6	;file es drawer
	move.l	sp,d1	;osszefuzese
	move.l	fr_File(a3),d2
	move.l	#maxpathlen,d3
	jsr	_LVOAddPart(a6)
	move.l	sp,d3	;string elmentese
	seti	gad_playfile(a5),#MUIA_String_Contents,d3
	lea	maxpathlen(sp),sp
	move.l	fr_NumArgs(a3),d0	;insert files
	move.l	fr_ArgList(a3),a0
	bsr.w	gui_addfiles
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; file draggelese az ablakunkba

gui_apphook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1),a3	;AppMessage
	move.w	am_Type(a3),d0
	cmp.w	#AMTYPE_APPICON,d0	;ha icon, nem ugrunk
	beq.b	.nochg	;a player lapra
	cmp.w	#AMTYPE_APPWINDOW,d0
	bne.w	.ok
	seti	gui_register(a5),#MUIA_Group_ActivePage,#page_player
.nochg	move.l	am_ArgList(a3),a2
	IFD	dragsetcurdir	;path beallitasa
	lea	-maxpathlen(sp),sp
	move.l	dosbase(a5),a6
	move.l	wa_Lock(a2),d1
	move.l	sp,d2
	move.l	#maxpathlen,d3
	jsr	_LVONameFromLock(a6)
	move.l	sp,d1
	move.l	wa_Name(a2),d2
	move.l	#maxpathlen,d3
	jsr	_LVOAddPart(a6)
	move.l	sp,d3
	seti	gad_playfile(a5),#MUIA_String_Contents,d3
	lea	maxpathlen(sp),sp
	ENDIF
	move.l	am_NumArgs(a3),d0
	move.l	a2,a0
	bsr.b	gui_addfiles	;add files
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

; Insert files into the playlist
; In D0:NumArgs A0:ArgList

gui_addfiles	move.l	d0,d3
	beq.w	.4
	move.l	a0,a4
	move.l	dosbase(a5),a6	;allocate FileInfoBlock
	moveq	#DOS_FIB,d1
	clr.l	d2
	jsr	_LVOAllocDosObject(a6)
	move.l	d0,ply_fib(a5)
	beq.w	.4
	moveq	#DOS_RDARGS,d1	;allocate RDArgs
	clr.l	d2
	jsr	_LVOAllocDosObject(a6)
	move.l	d0,ply_rdargs(a5)
	beq.w	.44
	move.l	excbase(a5),a6	;allocate trymem
	move.l	#play_trysize,d0
	moveq	#MEMF_PUBLIC,d1
	jsr	_LVOAllocMem(a6)
	move.l	d0,ply_trymem(a5)
	beq.w	.3
	move.w	ply_nodes(a5),d0	;save node cnt
	move.l	d0,-(sp)
	IFD	playlistquiet
	seti	gad_playlist(a5),#MUIA_List_Quiet,#TRUE
	ENDIF
.2	move.l	d3,-(sp)
	clr.l	d6
	clr.l	d7
	bsr.w	.do
	move.l	(sp)+,d3
	subq.l	#1,d3
	bne.b	.2
	IFD	playlistquiet
	seti	gad_playlist(a5),#MUIA_List_Quiet,#FALSE
	ENDIF
	move.l	(sp)+,d0
	tst.w	ply_nodes(a5)
	beq.b	.5
	tst.w	d0
	bne.w	.5	;ha az elso fajlok,
	;bset	#plf_play,ply_flags(a5)	;lejatszas inditasa
	;seti	gad_playlist(a5),#MUIA_List_Active,#0
.5	bsr.w	play_gadgets
	move.l	excbase(a5),a6	;free memory
	move.l	ply_trymem(a5),a1
	move.l	#play_trysize,d0
	jsr	_LVOFreeMem(a6)
	clr.l	ply_trymem(a5)
.3	move.l	dosbase(a5),a6	;free FileInfoBlock
	move.l	#DOS_RDARGS,d1
	move.l	ply_rdargs(a5),d2
	jsr	_LVOFreeDosObject(a6)
	clr.l	ply_rdargs(a5)
.44	move.l	#DOS_FIB,d1
	move.l	ply_fib(a5),d2
	jsr	_LVOFreeDosObject(a6)
	clr.l	ply_fib(a5)
.4	rts

; In A4:arglist D6:duration D7:recursion_depth
;
; WBArg formatumu file-listabol 1 node elemzese
; Ha playlistet talal, rekurzivan meghivogatja onmagat

.do	bsr.w	procpackets

	sub.l	a3,a3
	lea	-maxpathlen(sp),sp	;lock+file nev osszefuzes
	move.l	dosbase(a5),a6
	move.l	wa_Lock(a4),d1	;ha lock=0, playlistbol
	move.l	sp,d2
	move.l	#maxpathlen,d3
	jsr	_LVONameFromLock(a6)
	tst.l	d0
	beq.w	.doend
	move.l	sp,d1
	move.l	wa_Name(a4),d2
	move.l	#maxpathlen,d3
	jsr	_LVOAddPart(a6)
	tst.l	d0
	beq.w	.doend
	move.l	sp,a0	;D4 karakterek szamolasa
	moveq	#-1,d4
.00	addq.l	#1,d4
	tst.b	(a0)+
	bne.b	.00
	move.l	sp,a2

	; kiterjesztes vizsgalata
.73	cmp.w	#5,d4	;ha tul rovid, nem jo
	bcs.w	.nopat
	lea	t_ext_plst(pc),a0	;playlist?
	bsr.w	exttest
	beq.b	.playlist
	lea	t_ext_m3u(pc),a0	;m3u?
	bsr.w	exttest
	beq.b	.playlist
	bra.w	.nopat

.playlist	cmp.w	#plymaxrec,d7
	bcc	.doend
	addq.l	#1,d7
	move.l	dosbase(a5),a6
	move.l	a2,d1
	move.l	#MODE_OLDFILE,d2	;open playlist
	jsr	_LVOOpen(a6)
	move.l	d0,d4
	beq.w	.plnofile
	; D4:fh D5:scratch D6:duration
	clr.l	d6
.plnext	move.l	dosbase(a5),a6	;read line
	move.l	d4,d1
	move.l	sp,d2
	move.l	#maxpathlen-1,d3
	jsr	_LVOFGets(a6)
	tst.l	d0
	beq.w	.plend
	move.l	sp,a0
	cmp.l	#'#EXT',(a0)	;'#' -> info sor (.m3u)
	bne.b	.nom3u
	cmp.l	#'INF:',4(a0)
	bne	.nom3u
	addq.l	#8,a0
	move.l	a0,d1
	clr.l	-(sp)
	move.l	sp,d2
	jsr	_LVOStrToLong(a6)
	move.l	(sp)+,d6
	tst.l	d0
	bpl.b	.plnext
.774	clr.l	d6
	bra.b	.plnext
.nom3u	cmp.b	#'#',(a0)	;'#' hibas fejlec
	beq	.774
	moveq	#-1,d3	;D3 filenev hossz
.74	addq.l	#1,d3
	move.b	(a0)+,d0	;LF/TAB helyett nulla
	beq.b	.75	;'\' helyett '/'
	cmp.b	#'\',d0
	bne	.775
	tst.l	d6
	beq	.775
	move.b	#'/',-1(a0)
	bra	.74
.775	cmp.b	#10,d0
	beq.b	.76
	cmp.b	#13,d0
	beq.b	.76
	cmp.b	#9,d0
	bne.b	.74
.76	clr.b	-1(a0)
.75	tst.l	d6
	bne	.dodo

	; parse SongPlayer-like playlist
;	move.l	a0,a1	;TAB -> CR
;.77	move.b	(a1)+,d0
;	beq	.777
;	cmp.b	#9,d0
;	bne	.77
;	;move.b	#13,-1(a1)
;	bra	.77
;.777
	clr.l	-(sp)	;get DURATION
	clr.l	-(sp)
	move.l	sp,d2
	move.l	ply_rdargs(a5),a1
	move.l	a2,(a1)	;RDA_Source+CS_Buffer
	move.l	#maxpathlen,RDA_Source+CS_Length(a1)
	sub.l	a2,a0
	move.l	a0,RDA_Source+CS_CurChr(a1)
	clr.l	RDA_DAList(a1)
	clr.l	RDA_Buffer(a1)
	move.l	a1,d3
	lea	.durtempl(pc),a0
	move.l	a0,d1
	jsr	_LVOReadArgs(a6)
	move.l	(sp)+,d3
	addq.l	#4,sp
	tst.l	d0
	beq	.argerr
	tst.l	d3
	beq	.argerr
	clr.l	-(sp)	;percek konvertalasa
	move.l	sp,d2
	move.l	d3,d1
	jsr	_LVOStrToLong(a6)
	move.l	(sp)+,d2
	tst.l	d0
	bmi	.argerr
	move.l	d2,d5
	mulu.w	#60,d5
	add.l	d0,d3	;percek es ':' atugrasa
	addq.l	#1,d3
	clr.l	-(sp)	;masodpercek konvertalasa
	move.l	sp,d2
	move.l	d3,d1
	jsr	_LVOStrToLong(a6)
	move.l	(sp)+,d2
	tst.l	d0
	bmi	.argerr
	add.l	d2,d5
	move.l	d5,d6
.argerr	move.l	ply_rdargs(a5),d1
	jsr	_LVOFreeArgs(a6)

.dodo	movem.l	d4/a2/a4,-(sp)	;rekurzio
	pea	(a2)
	move.l	wa_Lock(a4),-(sp)
	move.l	sp,a4
	bsr.w	.do
	addq.l	#8,sp
	movem.l	(sp)+,d4/a2/a4
	clr.l	d6
	bra	.plnext
.plend	move.l	d4,d1	;vege
	jsr	_LVOClose(a6)
.plnofile	sub.l	a3,a3
	subq.l	#1,d7
	bra	.doend

.nopat	move.l	d4,d0	;memoria foglalas
	and.b	#$fe,d0
	add.l	#pln_size+2,d0
	move.l	pool1(a5),a0
	move.l	excbase(a5),a6
	bsr.l	AllocVecPooled
	move.l	d0,a3
	tst.l	d0	;hiba, no mem
	beq.w	.doend
	move.l	d4,pln_namelength(a3)
	move.l	a2,a0	;filenev masolasa
	lea	pln_size(a3),a1
	move.l	a1,pln_fullname(a3)
	move.l	d4,d0
	jsr	_LVOCopyMem(a6)
	move.l	dosbase(a5),a6
	lea	pln_size(a3),a0
	move.l	a0,d1
	jsr	_LVOFilePart(a6)
	move.l	d0,pln_filename(a3)
	move.l	d6,pln_duration(a3)
	bne	.noparse
	move.l	conftmp+conf_flags(a5),d0
	btst	#cflg_plynoparse,d0
	bne	.noparse

	bsr	play_parse
	beq	.err	;ismeretlen, torolni!

.noparse	move.l	excbase(a5),a6	;min:sec szamitas
	move.l	pln_duration(a3),d0
	divu.w	#60,d0
	swap	d0
	move.l	d0,-(sp)
	lea	guip_form(pc),a0	;formazas
	move.l	sp,a1
	lea	.putchproc(pc),a2
	move.l	a3,-(sp)
	lea	pln_formbuf1(a3),a3
	jsr	_LVORawDoFmt(a6)
	move.l	(sp)+,a3
	addq.l	#4,sp
	bra.b	.doend
.err	move.l	a3,a1	;hiba, node torlese
	move.l	pool1(a5),a0
	move.l	excbase(a5),a6
	bsr.l	FreeVecPooled
	sub.l	a3,a3
.doend	lea	maxpathlen(sp),sp
	move.l	a3,d3
	beq.b	.33
  DoMethod  gad_playlist(a5),#MUIM_List_InsertSingle,d3,#MUIV_List_Insert_Bottom
.33	addq.l	#wa_SIZEOF,a4
	rts

.putchproc	move.b	d0,(a3)+
	rts

.durtempl	dc.b	'DUR=DURATION/K/A,OTHER/M',10,0
	even

; In A0:pattern A2:name D4:length  Out Z:match

exttest	lea	(a2,d4.l),a1	;terminating 0
.e0	move.b	(a0)+,d0
	beq	.eend
	cmp.b	-(a1),d0
	beq	.e0
.eend	rts

; patterns, byte order reversed(!)

t_ext_plst	dc.b	'tslp.',0
t_ext_m3u	dc.b	'u3m.',0
t_ext_cdda	dc.b	'addc.',0
	even

; packetek kezelese

procpackets	move.l	excbase(a5),a6	;uzenetek kezelese
	clr.l	d0	;(kulonben a debug miatt
	move.l	cport_sig(a5),d1	;elfogynak a packetek)
	jsr	_LVOSetSignal(a6)
	and.l	cport_sig(a5),d0
	beq.b	.0
.6	move.l	cport(a5),a0
	bsr.w	getPkt
	beq.b	.0
	move.l	a1,-(sp)
	move.l	pkt_cmd(a1),d0
	move.l	pkt_arg(a1),a0
	lea	pkt_args(a1),a1
	bsr.w	.prtpck
	move.l	(sp)+,a1
	bsr.w	replyPkt
	bra.b	.6
.0	rts
.prtpck	cmp.l	#VSSpkt_PrintFault,d0
	beq.w	printFault
	cmp.l	#VSSpkt_PrintInfo,d0
	beq.w	printInfo
	cmp.l	#VSSpkt_PrintDebug,d0
	beq.w	printDebug
	bra.w	play_msg
	moveq	#VSSERR_BadPkt,d0	;unexpected packet
	bsr.l	panic
	illegal

; Examine file, fill duration
; In A3:playnode  Out Z:error

play_parse	movem.l	d7/a3-a4,-(sp)
	clr.l	d3
	bclr	#plf2_dataread,ply_flags2(a5)
	bclr	#plf2_syncfound,ply_flags2(a5)

	move.l	dosbase(a5),a6
	move.l	pln_fullname(a3),d1	;filetype?
	move.l	#MODE_OLDFILE,d2	;open file
	jsr	_LVOOpen(a6)
	move.l	d0,d7
	beq.w	.nofile
	move.l	d0,d1	;examine
.0	move.l	ply_fib(a5),d2
	jsr	_LVOExamineFH(a6)
	tst.l	d0
	beq.b	.ferr
	move.l	ply_fib(a5),a0
	move.l	fib_Size(a0),pln_filesize(a3)
	beq.b	.ferr
	lea	.playcontab(pc),a4	;call handlers...
.next	move.l	(a4)+,d0
	beq	.ferr
	move.l	d0,a2
	move.l	pc_Examine(a2),a0
	jsr	(a0)
	beq	.next
	st	d3
	move.l	excbase(a5),a6	;accepted
	move.l	a2,a0
	lea	pln_pc(a3),a1
	moveq	#pc_size,d0
	jsr	_LVOCopyMem(a6)
.ferr	move.l	dosbase(a5),a6
	move.l	d7,d1
	jsr	_LVOClose(a6)
.nofile	movem.l	(sp)+,d7/a3-a4
	tst.l	d3
	rts

.playcontab	dc.l	.cdda
	dc.l	.mpeg
	dc.l	0

.cdda	dc.l	pccdda_Examine
	dc.l	pccdda_InfoText
	dc.l	pccdda_Open
	dc.l	pccdda_Close
	dc.l	pccdda_FillBuffer
	dc.l	pccdda_Seek
	dc.l	pccdda_Time

.mpeg	dc.l	pcmpeg_Examine
	dc.l	pcmpeg_InfoText
	dc.l	pcmp3_Open
	dc.l	pcmp3_Close
	dc.l	pcmp3_FillBuffer
	dc.l	pcmp3_Seek
	dc.l	pcmp3_Time

.mp2	dc.l	pcmpeg_Examine
	dc.l	pcmpeg_InfoText
	dc.l	pcmp2_Open
	dc.l	pcmp2_Close
	dc.l	pcmp2_FillBuffer
	dc.l	pcmp2_Seek
	dc.l	pcmp2_Time


; File elejenek beolvasasa, a filetipus megallapitasahoz
; Out Z:success

play_tryread	btst	#plf2_dataread,ply_flags2(a5)
	bne	.ok
	move.l	dosbase(a5),a6
	move.l	d7,d1
	clr.l	d2
	moveq	#OFFSET_BEGINNING,d3
	jsr	_LVOSeek(a6)
	tst.l	d0
	bmi	.0
	move.l	d7,d1
	move.l	ply_trymem(a5),d2
	move.l	#play_trysize,d3
	jsr	_LVORead(a6)
	cmp.l	#play_trysize,d0
	bne.b	.0
	bset	#plf2_dataread,ply_flags2(a5)
.ok	clr.l	d0
.0	rts

; In D7:filehandle A3:playnode  Out Z:error

pccdda_Examine	movem.l	a2/d3,-(sp)
	lea	t_ext_cdda(pc),a0	;check extension
	move.l	pln_fullname(a3),a2
	move.l	pln_namelength(a3),d4
	bsr	exttest
	bne	.err
	move.l	pln_filesize(a3),d0	;calculate duration
	divu.l	#176400,d0
	move.l	d0,pln_duration(a3)
	st	d0
	bra	.ok
.err	clr.l	d0
.ok	movem.l	(sp)+,a2/d3
	rts

; In A3:playnode

pccdda_InfoText	movem.l	a2-a3,-(sp)
	move.l	ply_fib(a5),a0
	pea	fib_Comment(a0)
	move.l	pln_fullname(a3),-(sp)
	lea	.t_cdda(pc),a0
	move.l	sp,a1
	lea	.putchproc(pc),a2
	lea	pln_formbuf2(a3),a3
	move.l	excbase(a5),a6
	jsr	_LVORawDoFmt(a6)
	addq.l	#8,sp
	movem.l	(sp)+,a2-a3
	rts
.putchproc	move.b	d0,(a3)+
	rts

.t_cdda	dc.b	$1b,'b%s',$1b,'n',10
	dc.b	'CDDA 44100Hz 16bit Stereo',10
	dc.b	'%s',0
	even

; In D7:filehandle A3:playnode  Out Z:error

pcmpeg_Examine	movem.l	a2/d3,-(sp)
	bsr	play_tryread	;read test data
	bne	.err
	lea	mpastrextbuf+strext_filesiz(pc),a0
	move.l	pln_filesize(a3),(a0)
	move.l	ply_trymem(a5),a0	;find sync
	sub.l	a1,a1
	sub.l	a2,a2
	move.l	#play_trysize,d0
	bsr	findsync
	tst.l	d0
	bmi	.err
	move.l	excbase(a5),a6	;erdekes adatok mentese
	lea	mpastrbuf(pc),a0
	;lea	pln_ext(a3),a1
	;moveq	#extmpeg_size,d0
	jsr	_LVOCopyMem(a6)
	bset	#plf2_syncfound,ply_flags2(a5)
	;cmp.w	#3,pln_ext+stream_layer(a3)	;layer 3?
	bne	.err
	;move.l	pln_ext+stream_ms_duration(a3),d0  ;calculate duration
	add.l	#500,d0
	divu.l	#1000,d0
	move.l	d0,pln_duration(a3)
	st	d0
	bra	.ok
.err	clr.l	d0
.ok	movem.l	(sp)+,a2/d3
	rts

; In A3:playnode

pcmpeg_InfoText	movem.l	d2-d7/a2-a4,-(sp)
	lea	-mpid3_size(sp),sp
	clr.b	pln_formbuf2(a3)
	clr.l	d4
	move.l	dosbase(a5),a6	;read ID3
	move.l	pln_fullname(a3),d1
	move.l	#MODE_OLDFILE,d2
	jsr	_LVOOpen(a6)
	move.l	d0,d7
	beq	.iderr
	move.l	d0,d1
	move.l	#-mpid3_size,d2
	moveq	#OFFSET_END,d3
	jsr	_LVOSeek(a6)
	tst.l	d0
	bmi	.iderr
	move.l	d7,d1
	move.l	sp,d2
	move.l	sp,a4
	move.l	#mpid3_size,d3
	jsr	_LVORead(a6)
	cmp.l	#mpid3_size,d0
	bne	.iderr
	cmp.w	#'TA',(a4)
	bne	.iderr
	cmp.b	#'G',2(a4)
	bne	.iderr
	lea	mpid3_title(sp),a0
	bsr	.terminate
	lea	mpid3_artist(sp),a0
	bsr	.terminate
	lea	mpid3_album(sp),a0
	bsr	.terminate
	lea	mpid3_year(sp),a0
	bsr	.terminate
	lea	mpid3_title(sp),a0	;title masolasa
	lea	pln_realname(a3),a1
	moveq	#29,d0
.4	move.b	(a0)+,(a1)+
	dbeq	d0,.4
	st	d4
.iderr	move.l	ply_fib(a5),a0	;comment
	pea	fib_Comment(a0)
	;move.w	pln_ext+stream_mode(a3),d0	;mode
	lea	guia_mpegmode(pc),a0
	move.l	(a0,d0.w*4),-(sp)
	;move.l	pln_ext+stream_frequency(a3),-(sp)  ;frequency
	clr.l	d0	;bitrate
	;move.w	pln_ext+stream_bitrate(a3),d0
	move.l	d0,-(sp)
	;move.w	pln_ext+stream_layer(a3),d0	;layer
	move.l	(a0,d0.w*4,guia_mpeglayer-guia_mpegmode-4),-(sp)
	;move.w	pln_ext+stream_norm(a3),d0	;norm
	move.l	d0,-(sp)
	tst.b	d4
	beq	.noid
	pea	mpid3_title(a4)	;ID3.title
	pea	mpid3_year(a4)	;ID3.year
	lea	gent_Unknown(pc),a1	;ID3.genre
	clr.w	d0
	move.b	mpid3_genre(a4),d0
	cmp.b	#80,d0
	bhi	.2
	lea	genretab(pc),a1
	move.l	(a1,d0.w*4),a1
.2	move.l	a1,-(sp)
	pea	mpid3_album(a4)	;ID3.album
	pea	mpid3_artist(a4)	;ID3.artist
	lea	.t_mp3(pc),a0
	bra	.id
.noid	move.l	pln_fullname(a3),-(sp)	;full name
	lea	.t_mp3_noid(pc),a0
.id	move.l	sp,a1
	move.l	a3,a4
	lea	pln_formbuf2(a3),a3
	lea	.putchproc(pc),a2
	move.l	excbase(a5),a6
	jsr	_LVORawDoFmt(a6)
	lea	7*4(sp),sp
	tst.b	d4
	beq	.1
	lea	4*4(sp),sp
.1	move.l	dosbase(a5),a6
	move.l	d7,d1
	jsr	_LVOClose(a6)
.nofile	lea	mpid3_size(sp),sp
	movem.l	(sp)+,d2-d7/a2-a4
	rts
.terminate	lea	30(a0),a0	;space-k levagasa
	moveq	#30,d0	;a sztring vegerol
.3	cmp.b	#' ',-(a0)
	dbne	d0,.3
	clr.b	1(a0)
	rts
.putchproc	move.b	d0,(a3)+
	rts

.t_mp3	dc.b	$1b,'b%.30s',$1b,'n / %.30s (%s) [%.4s] / %.30s',10
	dc.b	'MPEG%ld-%s %ldkbps %ldHz %s',10
	dc.b	'%s',0

.t_mp3_noid	dc.b	$1b,'b%s',$1b,'n',10
	dc.b	'MPEG%ld-%s %ldkbps %ldHz %s',10
	dc.b	'%s',0

	even





pccdda_Open
pcmp3_Open	move.l	dosbase(a5),a6	;open file
	move.l	pln_fullname(a3),d1
	move.l	#MODE_OLDFILE,d2
	jsr	_LVOOpen(a6)
	;move.l	d0,pln_fhandle(a3)
	rts

pccdda_Close
pcmp3_Close	move.l	dosbase(a5),a6	;close file
	;move.l	pln_fhandle(a3),d1
	;clr.l	ply_fhandle(a3)
	jmp	_LVOClose(a6)

pccdda_FillBuffer
pcmp3_FillBuffer		;fill buffer
	move.l	dosbase(a5),a6
	;move.l	pln_fhandle(a3),d1
	jmp	_LVORead(a6)

pccdda_Seek
pcmp3_Seek	move.l	dosbase(a5),a6
	moveq	#OFFSET_BEGINNING,d3
	jsr	_LVOSeek(a6)



pcmp3_Time

pcmp2_Open
pcmp2_Close
pcmp2_FillBuffer
pcmp2_Seek
pcmp2_Time

pccdda_Time
	clr.l	d0
	rts






; playlista generalas

gui_savelhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	a1,a3	;aslreq mentese
	lea	-maxpathlen-16(sp),sp	;path masolasa
	move.l	fr_Drawer(a3),a0	;a verembe
	move.l	sp,a1
	move.l	#maxpathlen-1,d0
.0	move.b	(a0)+,(a1)+
	dbeq	d0,.0
	move.l	dosbase(a5),a6	;file es drawer
	move.l	sp,d1	;osszefuzese
	move.l	fr_File(a3),d2
	move.l	#maxpathlen,d3
	jsr	_LVOAddPart(a6)
	move.l	sp,a2	;check extension
.00	tst.b	(a2)+	;nev veg kereses
	bne	.00
	subq.l	#1,a2
	clr.l	d4
	lea	t_ext_plst(pc),a0
	bsr	exttest
	beq	.extok
	lea	.ext(pc),a0	;add '.plst'
.01	move.b	(a0)+,(a2)+
	bne	.01
.extok	move.l	sp,d3	;string elmentese
	seti	gad_savefile(a5),#MUIA_String_Contents,d3
	move.l	dosbase(a5),a6	;open file
	move.l	d3,d1
	move.l	#MODE_NEWFILE,d2
	jsr	_LVOOpen(a6)
	move.l	d0,d5
	beq.b	.ok
	move.l	sp,d4
	clr.l	d3
.loop	cmp.w	ply_nodes(a5),d3
	beq.b	.2
	DoMethod	gad_playlist(a5),#MUIM_List_GetEntry,d3,d4
	move.l	(sp),a0
	move.l	pln_fullname(a0),(sp)
	move.l	pln_duration(a0),d0
	divu.w	#60,d0
	swap	d0
	move.l	d0,4(sp)
	lea	.form(pc),a0
	move.l	sp,a1
	lea	.putchproc(pc),a2
	lea	8(sp),a3
	move.l	a3,d2
	move.l	excbase(a5),a6
	jsr	_LVORawDoFmt(a6)
	move.l	dosbase(a5),a6
	move.l	d5,d1
	jsr	_LVOFPuts(a6)
	tst.l	d0
	bne.b	.2
	addq.l	#1,d3
	bra.b	.loop
.2	move.l	dosbase(a5),a6
	move.l	d5,d1
	jsr	_LVOClose(a6)
.ok	lea	maxpathlen+16(sp),sp
	movem.l	(sp)+,d2-d7/a2-a6
	rts
.putchproc	move.b	d0,(a3)+
	rts

.form	dc.b	'%s',9,'DUR=%d:%d',10,0
.ext	dc.b	'.plst',0

	even


; A0:hook A1:node A2:pool

gui_playconst	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	addq.w	#1,ply_nodes(a5)
	move.l	a1,d0
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; A0:hook A1:node A2:pool

gui_playdestr	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	excbase(a5),a6
	move.l	pool1(a5),a0
	bsr.l	FreeVecPooled
	subq.w	#1,ply_nodes(a5)
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; A0:hook A1:node A2:array

gui_playdisp	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	a1,a3	;entry
	tst.l	pln_duration(a3)
	bne	.0
	lea	.nodur(pc),a0
	bra	.1
.0	lea	pln_formbuf1(a3),a0
.1	move.l	a0,(a2)

	tst.b	pln_realname(a3)	;ha van realname,
	beq	.2	;akkor azt hasznaljuk
	lea	pln_realname(a3),a0
	move.l	a0,4(a2)
	bra	.ok

.2	move.l	pln_fullname(a3),4(a2)
	;move.l	pln_filename(a3),4(a2)

.ok	clr.l	d0
	movem.l	(sp)+,d2-d7/a2-a6
	rts

.nodur	dc.b	'--:--',0
	even

; clear filelist

gui_clearhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	bclr	#plf_play,ply_flags(a5)
	bsr.w	playstop
	bsr.w	playkill

	clr.w	ply_nodeindex(a5)
	clr.l	ply_actnode(a5)

	DoMethod	gad_playlist(a5),#MUIM_List_Clear
	bsr.w	play_gadgets
	seti	gad_playstatus(a5),#MUIA_Text_Contents,#guit_status
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; close file

gui_closehook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	bclr	#plf_play,ply_flags(a5)
	bsr.w	playstop
	bsr.w	playkill

	clr.l	ply_actnode(a5)

      DoMethod	gad_playlist(a5),#MUIM_List_Remove,#MUIV_List_Remove_Active
	bsr.w	play_gadgets
	seti	gad_playstatus(a5),#MUIA_Text_Contents,#guit_status
	movem.l	(sp)+,d2-d7/a2-a6
	rts








; volume mute/unmute

play_vreshook	dc.l	0,0,.entry,0,0	;mute/unmute

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1),d0
	bne.b	.mute
	tst.b	ply_volume(a5)
	bne.b	.ok
	clr.l	d2
	move.b	ply_volsave(a5),d2
	clr.b	ply_volsave(a5)
	seti	gad_playvol(a5),#MUIA_Slider_Level,d2
	bra.b	.ok
.mute	move.b	ply_volume(a5),ply_volsave(a5)
	seti	gad_playvol(a5),#MUIA_Slider_Level,#0
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

; play volume

play_mixhook	dc.l	0,0,.entry,0,0	;volume

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1),d0
	move.b	d0,ply_volume(a5)
	bsr.w	play_setvol
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; play balance

play_balhook	dc.l	0,0,.entry,0,0	;balance

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1),d0
	move.b	d0,ply_balance(a5)
	;btst	#plf_playing,ply_flags(a5)
	;beq	.no
	move.w	gui_modes+cmode0(a5),d2
	cmp.w	#2,d2	;ha mute, nem bantjuk
	beq.b	.nomode
	moveq	#1,d2
	tst.b	d0
	dbne	d2,.nomode
.nomode	move.w	d2,gui_modes+cmode0(a5)
.no	bsr.w	play_setvol
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; player bass/treble slider
; beallitja a frekvenciat vagy kiemelest az options lapon

play_trebhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1),d2
	btst	#plf2_trebfreq,ply_flags2(a5)
	bne.b	.freq
	seti	gad_ms4(a5),#MUIA_Slider_Level,d2
	bra.b	.ok
.freq	seti	gad_ms3(a5),#MUIA_Slider_Level,d2
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

play_basshook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1),d2
	btst	#plf2_bassfreq,ply_flags2(a5)
	bne.b	.freq
	seti	gad_ms2(a5),#MUIA_Slider_Level,d2
	bra.b	.ok
.freq	seti	gad_ms1(a5),#MUIA_Slider_Level,d2
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

; options bass/treble enhance sliders
; kiemeles mod eseten beallitja a player lapon a megfelelo slidereket

play_trebhook2	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	btst	#plf2_trebfreq,ply_flags2(a5)
	bne.b	.ok
	move.l	(a1),d2
          seti	gad_playtreble(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

play_basshook2	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	btst	#plf2_bassfreq,ply_flags2(a5)
	bne.b	.ok
	move.l	(a1),d2
          seti	gad_playbass(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

; options bass/treble frequency sliders
; frekvencia mod eseten beallitja a player lapon a megfelelo slidereket

play_trebhook3	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	btst	#plf2_trebfreq,ply_flags2(a5)
	beq.b	.ok
	move.l	(a1),d2
          seti	gad_playtreble(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

play_basshook3	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	btst	#plf2_bassfreq,ply_flags2(a5)
	beq.b	.ok
	move.l	(a1),d2
          seti	gad_playbass(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

; bass/treble switch gadgetek
; frekvencia/kiemeles mod kozott valtanak

play_trebswhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	tst.l	(a1)
	bne.b	.set
	bclr	#plf2_trebfreq,ply_flags2(a5)
	;seti	gad_playtreble(a5),#MUIA_My_Form,#guip_dbform
	move.b	mixtab+VMT_trebenhance(a5),d2
	extb.l	d2
	seti	gad_playtreble(a5),#MUIA_My_Divider,#0
 seti gad_playtreble(a5),#MUIA_Slider_Min,#-18,#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2,#MUIA_Slider_Max,#18
	bra.b	.ok
.set	bset	#plf2_trebfreq,ply_flags2(a5)
	;seti	gad_playtreble(a5),#MUIA_My_Form,#guip_hzform
	clr.l	d2
	move.w	mixtab+VMT_trebfreq(a5),d2
	seti	gad_playtreble(a5),#MUIA_My_Divider,#freqgadstep
 seti gad_playtreble(a5),#MUIA_Slider_Max,#mp3dsp_trebmax,#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2,#MUIA_Slider_Min,#mp3dsp_trebmin
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

play_bassswhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	tst.l	(a1)
	bne.b	.set
	bclr	#plf2_bassfreq,ply_flags2(a5)
	;seti	gad_playbass(a5),#MUIA_My_Form,#guip_dbform
	move.b	mixtab+VMT_bassenhance(a5),d2
	extb.l	d2
	seti	gad_playbass(a5),#MUIA_My_Divider,#0
 seti gad_playbass(a5),#MUIA_Slider_Min,#-18,#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2,#MUIA_Slider_Max,#18
	bra.b	.ok
.set	bset	#plf2_bassfreq,ply_flags2(a5)
	;seti	gad_playbass(a5),#MUIA_My_Form,#guip_hzform
	clr.l	d2
	move.w	mixtab+VMT_bassfreq(a5),d2
	seti	gad_playbass(a5),#MUIA_My_Divider,#freqgadstep
 seti gad_playbass(a5),#MUIA_Slider_Max,#mp3dsp_bassmax,#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2,#MUIA_Slider_Min,#mp3dsp_bassmin
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

; volume,balance -> left,right,remain
;
; vol>0 & bal=0:  l=r=vol
; vol>0 & bal<0:  l=vol, k=vol*(50+bal), r=k div 50, remain=k mod 50
; vol>0 & bal>0:  r=vol, k=vol*(50-bal), l=k div 50, remain=k mod 50
; vol=0:          l=r=0, remain=bal
;
; remain az inverz transzformaciohoz kell, az osztas hibajat javitani.
; Ha vol>0 akkor 0<remain<50, ha vol=0 akkor -50<remain<50
; vol=0 eseten jelenleg az inverz nem szamol balance-t, ezert felesleges
; foglalkozni vele, csak a korrektseg kedveert csinaltam meg :)

play_setvol	tst.b	ply_volume(a5)
	beq.b	.3
	tst.b	ply_volsave(a5)
	beq.b	.3
          seti  gad_volreset(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,#FALSE
.3	;btst	#plf_playing,ply_flags(a5)
	;bne	.2
	;rts
.2	clr.l	d0
	move.b	ply_volume(a5),d0
	clr.l	d2
	move.l	d0,d1
	bne	.4
	move.b	ply_balance(a5),d2
	ext.w	d2
	move.w	d2,d3
	bra	.5
.4	move.b	ply_balance(a5),d2
	beq.b	.0
	bpl.b	.1
	add.b	#50,d2	;bal<0
	mulu.w	d2,d1
	divu.w	#50,d1
	move.l	d1,d3
	bra.b	.0
.1	neg.w	d2	;bal>0
	add.w	#50,d2
	mulu.w	d2,d0
	divu.w	#50,d0
	move.l	d0,d3
.0	swap	d3
.5	move.w	d3,ply_volremain(a5)
	move.b	d0,mixtab+VMT_4l(a5)
	move.b	d1,mixtab+VMT_4r(a5)

	move.l	vssbase(a5),a6
	move.l	main_handle(a5),a0
	st	d0	;async
	jmp	_LVOvssRefreshMixer(a6)





; update time slider

playupdate	cmp.b	#page_player,gui_actpage(a5)
	bne	.ok
	btst	#plf_playing,ply_flags(a5)
	beq.w	.ok
	btst	#plf_seeking,ply_flags(a5)
	bne.w	.ok
	move.l	updatequantum(a5),d0	;update cnt
	sub.l	d0,ply_updatcnt(a5)
	bcc.b	.ok
	move.l	#playerguiupdat,ply_updatcnt(a5)

	move.l	vssbase(a5),a6	;sajat rutin
	move.l	ply_vhandle(a5),a0
	jsr	_LVOvssBuffered(a6)	;pozicio szamitas
	neg.l	d0
	add.l	ply_pos(a5),d0
	bpl.b	.1
	clr.l	d0
.1	move.l	ply_ptr1(a5),d1
	sub.l	ply_ptr2(a5),d1
	bcc.b	.0
	add.l	ply_bufsize(a5),d1
.0	sub.l	d1,d0
	bpl.b	.2
	clr.l	d0
.2	move.l	ply_duration(a5),d1	;ido szamitasa
	beq.b	.ok
	mulu.l	d1,d2:d0
	move.l	ply_filesize(a5),d1
	beq.b	.ok
	divu.l	d1,d2:d0
	bmi.b	.ok
          seti	gad_playtime(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d0
.ok	rts

; Dragging the time slider

play_seekhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	btst	#plf_playing,ply_flags(a5)
	beq.b	.ok
	tst.l	(a1)
	beq.b	.unselect
	bset	#plf_seeking,ply_flags(a5)
	seti	gad_playstatus(a5),#MUIA_Text_Contents,#guit_status3
	bra.b	.ok
.unselect	clr.l	-(sp)
	geti	gad_playtime(a5),#MUIA_Slider_Level,sp
	move.l	(sp)+,d2
	move.l	ply_filesize(a5),d0	;sajat seek rutin
	beq.b	.ok	;pozicio szamitasa
	mulu.l	d0,d1:d2
	move.l	ply_duration(a5),d0
	beq.b	.ok
	divu.l	d0,d1:d2
	moveq	#VSSpkt_Seek,d0
	lea	play_seekend(pc),a0
	move.l	a0,d1
	move.l	ply_port(a5),a0
	move.l	cport(a5),a1
	bsr.w	sendPkt
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

play_seekend	lea	guit_status1(pc),a3
	btst	#plf_pause,ply_flags(a5)
	beq.b	.0
	lea	guit_status2(pc),a3
.0	seti	gad_playstatus(a5),#MUIA_Text_Contents,a3
	bclr	#plf_seeking,ply_flags(a5)
	rts

; forward/backward

play_rseekhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	btst	#plf_playing,ply_flags(a5)
	beq.b	.ok
	;btst	#plf_msg,ply_flags(a5)
	;bne	.ok
	move.l	(a1),d2
	seti	gad_playstatus(a5),#MUIA_Text_Contents,#guit_status3
	move.l	vssbase(a5),a6
	move.l	ply_vhandle(a5),a0
	jsr	_LVOvssBuffered(a6)
	neg.l	d0
	add.l	ply_pos(a5),d0
	bpl.b	.1
	clr.l	d0
.1	move.l	ply_ptr1(a5),d1
	sub.l	ply_ptr2(a5),d1
	bcc.b	.0
	add.l	ply_bufsize(a5),d1
.0	sub.l	d1,d0
	bpl.b	.2
	clr.l	d0
.2	add.l	d0,d2
	bmi.b	.ok
	cmp.l	ply_filesize(a5),d2
	bcc.b	.ok
	bset	#plf_seeking,ply_flags(a5)
	moveq	#VSSpkt_Seek,d0
	lea	play_seekend(pc),a0
	move.l	a0,d1
	move.l	ply_port(a5),a0
	move.l	cport(a5),a1
	bsr.w	sendPkt
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

play_pausehook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	tst.l	ply_playproc(a5)
	beq.b	.ok
	;btst	#plf_msg,ply_flags(a5)
	;bne	.ok
	moveq	#VSSpkt_Pause,d0
	lea	.0(pc),a0
	move.l	a0,d1
	move.l	(a1),d2
	move.l	ply_port(a5),a0
	move.l	cport(a5),a1
	bsr.w	sendPkt
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts
.0	lea	guit_status2(pc),a3
	btst	#plf_pause,ply_flags(a5)
	bne.b	.1
	lea	guit_status1(pc),a3
.1	seti	gad_playstatus(a5),#MUIA_Text_Contents,a3
	rts

; A0:hook A1:parameters A2:object

gui_playselhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	clr.l	d2
	move.l	(a1),d3	;pos
	move.w	d3,ply_nodeindex(a5)

	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_playsel,a0
	move.l	a0,d1
	move.l	d3,d2
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt

	; prev/next tiltasa/engedelyezese
	;seq	d2
	;seti	gad_prev(a5),#MUIA_Disabled,d2
	;move.w	ply_nodes(a5),d0
	;subq.w	#1,d0
	;cmp.w	d0,d3
	;seq	d2
	;seti	gad_next(a5),#MUIA_Disabled,d2

	;move.l	d3,-(sp)
	;bsr	play_gadgets
	;move.l	(sp)+,d3


	clr.l	-(sp)
	move.l	sp,d4
	DoMethod	gad_playlist(a5),#MUIM_List_GetEntry,d3,d4
	move.l	(sp)+,d0
	beq.w	.ok
	bmi.w	.ok
	cmp.l	ply_actnode(a5),d0
	beq.w	.ok
	move.l	d0,ply_actnode(a5)
	move.l	d0,a2
	move.l	pln_filesize(a2),ply_filesize(a5)
	move.l	pln_duration(a2),ply_duration(a5)
	move.l	pln_fullname(a2),ply_fname(a5)
	;move.l	pln_ext+stream_frequency(a2),ply_frequency(a5)

	move.l	a2,-(sp)
	seti	gad_playstatus(a5),#MUIA_Text_Contents,#guit_status4
	bsr.w	playstop
	move.l	(sp)+,a2

	move.l	a2,a3
	;move.l	pln_pc+pc_InfoText(a3),a0
	;jsr	(a0)

	lea	pln_formbuf2(a3),a1

	move.l	a1,d3

	seti	gad_playinfo(a5),#MUIA_Text_Contents,d3

	move.l	ply_actnode(a5),a0
	move.l	pln_duration(a0),d3
	seti	gad_playtime(a5),#MUIA_Slider_Max,d3

	lea	guit_status(pc),a3
	bclr	#plf_play,ply_flags(a5)
	beq.b	.ok
	bsr.w	playplay
	beq.b	.ok

	seti	gad_playstatus(a5),#MUIA_Text_Contents,#guit_status1

.ok	bsr.w	play_gadgets
	movem.l	(sp)+,d2-d7/a2-a6
	rts

.putchproc	move.b	d0,(a3)+
	rts

; loop gadget, update prev/next

gui_loophook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	conftmp+conf_flags(a5),d0
	bclr	#cflg_playloop,d0
	tst.l	(a1)
	beq.b	.0
	bset	#cflg_playloop,d0
.0	move.l	d0,conftmp+conf_flags(a5)
	bsr.w	play_gadgets
	movem.l	(sp)+,d2-d7/a2-a6
	rts

gui_prevhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	#MUIV_List_ActiveUp,d2
	tst.w	ply_nodeindex(a5)
	bne	.0
	move.l	#MUIV_List_ActiveBottom,d2
.0        seti	gad_playlist(a5),#MUIA_List_Active,d2
	movem.l	(sp)+,d2-d7/a2-a6
	rts

gui_nexthook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	#MUIV_List_ActiveDown,d2
	move.w	ply_nodeindex(a5),d0
	addq.w	#1,d0
	cmp.w	ply_nodes(a5),d0
	bne	.0
	move.l	#MUIV_List_ActiveTop,d2
.0        seti	gad_playlist(a5),#MUIA_List_Active,d2
	movem.l	(sp)+,d2-d7/a2-a6
	rts

gui_stophook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	bclr	#plf_play,ply_flags(a5)
	bsr.w	playstop
	bsr.w	playkill
	seti	gad_pause(a5),#MUIA_Selected,#FALSE
	bclr	#plf_pause,ply_flags(a5)
	bsr.w	play_gadgets
	seti	gad_playstatus(a5),#MUIA_Text_Contents,#guit_status
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

gui_playhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	bsr.w	playplay
	beq.b	.ok
	seti	gad_playstatus(a5),#MUIA_Text_Contents,#guit_status1
.ok	bsr.w	play_gadgets
	movem.l	(sp)+,d2-d7/a2-a6
	rts

playstop	movem.l	d0-a6,-(sp)
	moveq	#VSSpkt_Stop,d0
	move.l	ply_port(a5),d1
	beq.b	.ok
	move.l	d1,a0
	move.l	ply_cport(a5),a1
	bsr.w	doPkt
.ok	movem.l	(sp)+,d0-a6
	rts

playkill	movem.l	d0-a6,-(sp)
	moveq	#VSSpkt_Kill,d0
	move.l	ply_port(a5),d1
	beq.b	.ok
	move.l	d1,a0
	move.l	ply_cport(a5),a1
	bsr.w	doPkt
.ok	movem.l	(sp)+,d0-a6
	rts

playplay	tst.l	ply_playproc(a5)
	bne.b	.running
	move.l	excbase(a5),a6
	moveq	#-1,d0
	jsr	_LVOAllocSignal(a6)
	move.l	d0,-(sp)
	tst.b	d0
	bmi.b	.baj
	clr.l	d1
	bset	d0,d1
	move.l	d1,subtasksig(a5)
	move.l	dosbase(a5),a6
	lea	.proctags(pc),a0
	move.l	a0,d1
	jsr	_LVOCreateNewProc(a6)
	move.l	excbase(a5),a6
	move.l	d0,ply_playproc(a5)
	beq.b	.baj
	move.l	subtasksig(a5),d0	;megvarjuk
	jsr	_LVOWait(a6)
.baj	move.l	(sp)+,d0
	jsr	_LVOFreeSignal(a6)
.running	move.l	ply_port(a5),d0	;dead or alive?
	beq.b	.baj2
	move.l	d0,a0
	moveq	#VSSpkt_Play,d0
	move.l	ply_cport(a5),a1
	bsr.w	doPkt
	tst.l	d0
.baj2	rts

.proctags	dc.l	NP_Entry,playproc
	dc.l	NP_Name,plyname
	dc.l	NP_Priority,0
	dc.l	NP_StackSize,4096
	dc.l	TAG_DONE

; ide csak reply packeteket varunk

play_msg	cmp.l	#VSSpkt_PlayEOF,d0
	beq.b	.eof
	jsr	(a0)
	bclr	#plf_msg,ply_flags(a5)
	rts

.eof	bsr.w	playstop

	move.w	ply_nodeindex(a5),d0
	addq.w	#1,d0
	cmp.w	ply_nodes(a5),d0	;utolso file?
	beq.b	.1
	;bset	#plf_play,ply_flags(a5)	;nem, kovetkezo
          seti	gad_playlist(a5),#MUIA_List_Active,#MUIV_List_ActiveDown
	bra	playplay

.1	move.l	conftmp+conf_flags(a5),d0
	btst	#cflg_playloop,d0
	beq	.2

	bsr.w	playstop	;vissza az elsore
          seti	gad_playlist(a5),#MUIA_List_Active,#MUIV_List_ActiveTop
	bra	playplay

.2	bclr	#plf_play,ply_flags(a5)	;leallas
	bsr.w	playkill
	bsr.w	play_gadgets
	seti	gad_playstatus(a5),#MUIA_Text_Contents,#guit_status
	rts


;--------< player process >------------------------------------------------------

; pkt_arg a hivo hookjat tartalmazza, nem szabad bantani
; (parameter a pkt_args-ban)

playproc	move.l	var(pc),a5
	lea	d_playstart,a0	;debug
	move.l	a0,d1
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	excbase(a5),a6
	clr.l	d7	;no packet (finalizernek)
	moveq	#-1,d0
	jsr	_LVOAllocSignal(a6)
	move.b	d0,ply_signal(a5)
	bmi.w	.err
	clr.l	d1
	bset	d0,d1
	move.l	d1,ply_sig(a5)
	jsr	_LVOCreateMsgPort(a6)
	move.l	d0,ply_port(a5)
	beq.w	.err
	bsr.l	portsig
	move.l	d0,ply_portsig(a5)
	lea	vssname,a1	;open vss.lib
	moveq	#vssver,d0
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,ply_vsslib(a5)
	beq.w	.err
	move.l	d0,a6	;alloc vsshandle
	lea	playername,a0
	jsr	_LVOvssAllocHandle(a6)
	move.l	d0,ply_vhandle(a5)
	beq.w	.err2
	move.l	d0,a0
	jsr	_LVOvssAllocAudio(a6)
	tst.l	d0
	bne.w	.err2


	; setup actions


	move.l	excbase(a5),a6	;we are alive!
	move.l	mainproc(a5),a1
	move.l	subtasksig(a5),d0
	jsr	_LVOSignal(a6)

.loop	move.l	ply_portsig(a5),d0	;wait for new messages
	or.l	ply_sig(a5),d0
	jsr	_LVOWait(a6)
	move.l	d0,ply_sigrcvd(a5)
	and.l	ply_sig(a5),d0
	beq.b	.nosig
	sub.l	a3,a3
	bra.w	.fillbuffer
.nosig	move.l	ply_sigrcvd(a5),d0
	and.l	ply_portsig(a5),d0
	beq.b	.loop
.port	move.l	ply_port(a5),a0	;process packets
	jsr	_LVOGetMsg(a6)
	tst.l	d0
	beq.b	.loop
	move.l	d0,a3
	move.l	pkt_cmd(a3),d0
	cmp.l	#VSSpkt_Kill,d0
	beq.w	.kill
	cmp.l	#VSSpkt_Pause,d0
	beq.b	.pause
	cmp.l	#VSSpkt_Seek,d0
	beq.b	.seek
	cmp.l	#VSSpkt_Play,d0
	beq.w	.play
	cmp.l	#VSSpkt_Stop,d0
	beq.w	.stop
	moveq	#VSSERR_BadPkt,d0
	bsr.l	panic
	illegal

.pause	btst	#plf_playing,ply_flags(a5)
	beq.w	.done
	tst.l	pkt_args(a3)
	bne.b	.pa
	bclr	#plf_pause,ply_flags(a5)
	bclr	#plf_start,ply_flags(a5)
	bra.w	.fillbuffer
.pa	bset	#plf_pause,ply_flags(a5)
	move.l	vssbase(a5),a6
	move.l	ply_vhandle(a5),a0
	jsr	_LVOvssStop(a6)
	bra.w	.done

.seek	btst	#plf_playing,ply_flags(a5)
	beq.w	.done
	move.l	vssbase(a5),a6
	move.l	ply_vhandle(a5),a0
	jsr	_LVOvssStop(a6)
	move.l	ply_vhandle(a5),a0
	jsr	_LVOvssFlush(a6)
	move.l	ply_fhandle(a5),d1
	move.l	pkt_args(a3),d2
	move.l	d2,ply_pos(a5)
	move.l	ply_pc+pc_Seek(a5),a0
	jsr	(a0)
	tst.l	d0
	bmi.w	.errstop
	clr.l	ply_ptr1(a5)	;clear and refill
	clr.l	ply_ptr2(a5)
	bclr	#plf_start,ply_flags(a5)
	bclr	#plf_eof,ply_flags(a5)
	btst	#plf_pause,ply_flags(a5)
	bne.w	.done
	bra.w	.fillbuffer

.play	btst	#plf_playing,ply_flags(a5)
	bne.w	.done
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_playplay,a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr	sendPkt

	move.l	conftmp+conf_playbufsize(a5),d0  ;buffer
	move.l	#128,d0	;!!!
	asl.l	#8,d0
	asl.l	#2,d0
	move.l	d0,ply_bufsize(a5)
	move.l	#MEMF_PUBLIC!MEMF_CLEAR,d1
	jsr	_LVOAllocVec(a6)
	move.l	d0,ply_buffer(a5)
	beq.w	.err






	clr.l	pkt_result(a3)
	move.l	vssbase(a5),a6
	lea	.playtags(pc),a1	;init
	lea	playerhook(pc),a0
	move.l	conftmp+conf_flags(a5),d0
	btst	#cflg_bytewr,d0
	bne.b	.3
	lea	playeropthook(pc),a0
.3	move.l	a0,.hook-.playtags+ti_Data(a1)
	move.l	ply_frequency(a5),ti_Data(a1)
	move.l	ply_vhandle(a5),a0
	jsr	_LVOvssInit(a6)
	tst.l	d0
	beq.w	.done
	move.l	ply_pc+pc_Open(a5),a0
	jsr	(a0)
	move.l	d0,ply_fhandle(a5)
	beq.w	.done
	clr.l	ply_pos(a5)	;Ok!
	clr.l	ply_bufeof(a5)
	clr.l	ply_ptr1(a5)
	clr.l	ply_ptr2(a5)
	bclr	#plf_start,ply_flags(a5)
	bclr	#plf_under,ply_flags(a5)
	bclr	#plf_eof,ply_flags(a5)
	bclr	#plf_pause,ply_flags(a5)
	bset	#plf_play,ply_flags(a5)	;misc flags
	bset	#plf_playing,ply_flags(a5)
	st	pkt_result(a3)
	bra.b	.fillbuffer

.stop	bclr	#plf_playing,ply_flags(a5)
	beq.w	.done
	moveq	#VSSpkt_PrintDebug,d0	;debug
	lea	d_playstop,a0
	move.l	a0,d1
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr	sendPkt
	move.l	vssbase(a5),a6	;stop playback
	move.l	ply_vhandle(a5),a0
	jsr	_LVOvssStop(a6)
	move.l	ply_pc+pc_Close(a5),a0
	jsr	(a0)
	bra.w	.done

; Write (ptr1): 0-nal tovabbmegy, nem tolt 0-ig	(bufferbe)
; Read  (ptr2): 0-nal megall, 0-ig olvas	(bufferbol kartyara)

.fillbuffer	move.l	ply_ptr1(a5),d1
	move.l	ply_buffer(a5),d2
	add.l	d1,d2	;d2:iras kezdete
	move.l	ply_ptr2(a5),d3
	sub.l	d1,d3
	bls.b	.veg
	cmp.l	conftmp+conf_mpchunk(a5),d3	;!!!
	bcs.w	.eleg
	bra.b	.read
.veg	move.l	ply_bufsize(a5),d3
	sub.l	d1,d3
	tst.l	ply_ptr2(a5)
	bne.b	.read1
.read	subq.l	#1,d3
	bls.w	.eleg
.read1	move.l	conftmp+conf_mpmaxchunk(a5),d1  ;!!!
	cmp.l	d1,d3
	bcs.b	.11
	move.l	d1,d3
.11	btst	#plf_eof,ply_flags(a5)
	beq.b	.read2
	move.l	d2,a0	;EOF, 0 kuldese
	move.l	d3,d1
	beq.b	.42
	subq.l	#1,d1
.32	clr.b	(a0)+
	dbf	d1,.32
.42	move.l	d3,d0
	bra.b	.5
.read2	move.l	ply_pc+pc_FillBuffer(a5),a0
	jsr	(a0)
	tst.l	d0	;EOF/error?
	beq.b	.eof
	bpl.b	.5
.eof	lea	d_playeof,a0	;info: EOF
	move.l	a0,d1
	move.l	ply_pos(a5),d2
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	bset	#plf_eof,ply_flags(a5)
	move.l	ply_ptr1(a5),d0
	sub.l	ply_ptr2(a5),d0
	bhi.b	.01
	add.l	ply_bufsize(a5),d0
.01	clr.l	d1
	move.b	bufsize(a5),d1
	asl.l	#7,d1
	add.l	d1,d0
	move.l	d0,ply_bufeof(a5)
	bra.b	.eleg

.5	add.l	d0,ply_pos(a5)
	add.l	ply_ptr1(a5),d0	;bump pointer
	cmp.l	ply_bufsize(a5),d0
	bcs.b	.22
	clr.l	d0
.22	move.l	d0,ply_ptr1(a5)

.eleg	bset	#plf_start,ply_flags(a5)	;elso toltes?
	bne.b	.00
	move.l	vssbase(a5),a6	;igen, DMA inditas
	move.l	ply_vhandle(a5),a0
	jsr	_LVOvssStart(a6)
.00	bclr	#plf_under,ply_flags(a5)
	beq.b	.56
	lea	d_playunder,a0	;underrun!
	move.l	a0,d1
	moveq	#VSSpkt_PrintInfo,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
.56	tst.l	ply_bufeof(a5)	;vege van mar?
	bne.b	.done
	btst	#plf_eof,ply_flags(a5)
	beq.b	.done
.errstop	move.l	vssbase(a5),a6	;vege
	move.l	ply_vhandle(a5),a0
	jsr	_LVOvssStop(a6)
	moveq	#VSSpkt_PlayEOF,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt

.done	move.l	excbase(a5),a6
	move.l	a3,d0
	beq.w	.nosig
	move.l	a3,a1	;reply packet, continue
	bsr.w	replyPkt
	bra.w	.port

.kill	move.l	a3,d7	;pkt_Kill
	move.l	vssbase(a5),a6
	move.l	ply_vhandle(a5),a0
	jsr	_LVOvssFreeAudio(a6)
.err2	move.l	ply_vhandle(a5),a0
	clr.l	ply_vhandle(a5)
	jsr	_LVOvssFreeHandle(a6)
	move.l	excbase(a5),a6
	move.l	ply_vsslib(a5),a1
	clr.l	ply_vsslib(a5)
	jsr	_LVOCloseLibrary(a6)
.err	move.l	ply_buffer(a5),a1
	clr.l	ply_buffer(a5)
	jsr	_LVOFreeVec(a6)
	move.l	ply_port(a5),a0
	clr.l	ply_port(a5)
	jsr	_LVODeleteMsgPort(a6)
	move.b	ply_signal(a5),d0
	move.b	#-1,ply_signal(a5)
	jsr	_LVOFreeSignal(a6)
	lea	d_playex,a0	;debug
	move.l	a0,d1
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	jsr	_LVOForbid(a6)
	tst.l	d7
	bne.b	.2
	move.l	mainproc(a5),a1	;we are dead!
	move.l	subtasksig(a5),d0
	jsr	_LVOSignal(a6)
	bra.b	.4
.2	move.l	d7,a1	;the killer msg :)
	bsr.w	replyPkt
.4	clr.l	ply_playproc(a5)
	clr.l	d0
	rts

.playtags	dc.l	VSS_freq,0	;atirodik!
.hook	dc.l	VSS_hook,0	;atirodik!
	dc.l	VSS_bits,0
	dc.l	VSS_mode,0
	dc.l	TAG_DONE

; Write (ptr1): 0-nal tovabbmegy, nem tolt 0-ig	(bufferbe)
; Read  (ptr2): 0-nal megall, 0-ig olvas	(bufferbol kartyara)

; In D0.w:bytesreq A0:hook A5:ioaddr  Out:bytes_done

PLAYERCONV_PRE	MACRO
	movem.l	d6-d7,-(sp)
	move.l	var(pc),a1
	beq.b	.nounder
	bset	#plf_under,ply_flags(a1)
.nounder	clr.l	d6
	clr.l	d7
	move.w	d0,d7
.meg	move.l	ply_ptr2(a1),d1
	move.l	d1,a0
	add.l	ply_buffer(a1),a0	;a0:olvasas kezdete
	move.l	ply_ptr1(a1),d0
	sub.l	d1,d0
	bcc.b	.do
	move.l	ply_bufsize(a1),d0	;vegeig!
	sub.l	d1,d0
.do	cmp.l	d7,d0
	bcs.b	.0
	move.l	d7,d0
.0	and.b	#$f8,d0
	sub.l	d0,d7
	add.l	d0,d6
	add.l	d0,d1	;bump pointer
	lsr.l	#3,d0
	subq.l	#1,d0
	bcs.b	.ok	;semmi!
	ENDM

PLAYERCONV_POST	MACRO
	cmp.l	ply_bufsize(a1),d1
	bcs.b	.2
	clr.l	d1
.2	move.l	d1,ply_ptr2(a1)	;!!!! nem jo!
	tst.l	d7
	bne.b	.meg
.ok	move.l	ply_bufeof(a1),d1
	beq.b	.3
	sub.l	d6,d1
	bcc.b	.4
	clr.l	d1
.4	move.l	d1,ply_bufeof(a1)
.3
	move.l	a6,d7
	move.l	excbase(a1),a6
	move.l	ply_sig(a1),d0
	move.l	ply_playproc(a1),a1
	jsr	_LVOSignal(a6)
	move.l	d7,a6

	move.l	d6,d0
	movem.l	(sp)+,d6-d7
	rts
	ENDM

playerhook	dc.l	0,0,.entry,0,0

.entry	PLAYERCONV_PRE
.1	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	move.b	(a0)+,(a5)
	dbf	d0,.1
	PLAYERCONV_POST

; ugyanez optimalizalva, word irassal

playeropthook	dc.l	0,0,.entry,0,0

.entry	PLAYERCONV_PRE
.1	move.w	(a0)+,(a5)
	move.w	(a0)+,(a5)
	move.w	(a0)+,(a5)
	move.w	(a0)+,(a5)
	dbf	d0,.1
	PLAYERCONV_POST


;----------< Sampler >-----------------------------------------------------------

; lfl_monitor	lowlevel task will read levels from codec
; sfl_histmon	enable histogram quantizing

sfl_start	equ	0
sfl_overrun	equ	1
sfl_go	equ	2
sfl_histmon	equ	4

saminit	lea	levhist1(a5),a0	;hisztogram bufferek
	move.l	a0,sam_hist(a5)
	lea	levhist2(a5),a0
	move.l	a0,sam_hist2(a5)
	clr.l	sam_hists(a5)

	;seti	gad_ftyp2(a5),#MUIA_Disabled,#TRUE

	bset	#lfl_monitor,ll_flags(a5)	;enable codec monitor
	rts

; mixer updatelese a sampler lapon, ha szukseges

sam_mixupdate	clr.l	d3
	move.b	mixtab+VMT_0l(a5),d3
	add.b	mixtab+VMT_0r(a5),d3
	lsr.b	d3
	cmp.b	sam_mx1(a5),d3
	beq.b	.0
          seti	gad_smix1(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	move.b	d3,sam_mx1(a5)
.0	move.b	mixtab+VMT_1l(a5),d3
	add.b	mixtab+VMT_1r(a5),d3
	lsr.b	d3
	cmp.b	sam_mx2(a5),d3
	beq.b	.1
          seti	gad_smix2(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	move.b	d3,sam_mx2(a5)
.1	move.b	mixtab+VMT_2l(a5),d3
	add.b	mixtab+VMT_2r(a5),d3
	lsr.b	d3
	cmp.b	sam_mx3(a5),d3
	beq.b	.2
          seti	gad_smix3(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	move.b	d3,sam_mx3(a5)
.2	move.b	mixtab+VMT_3l(a5),d3
	add.b	mixtab+VMT_3r(a5),d3
	lsr.b	d3
	cmp.b	sam_mx4(a5),d3
	beq.b	.3
          seti	gad_smix4(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	move.b	d3,sam_mx4(a5)
.3	rts

; sampler mixer hook
; mindket csatornat azonos szintre allitja, nema csatornat aktivalja

gui_smixhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1)+,d2	;level
	move.l	(a1)+,d3	;channel*2
	lea	.table(pc),a0
	move.w	(a0,d3.w),d0
	move.b	d2,(a5,d0.w,mixtab)	;mixtab beallitas
	move.b	d2,(a5,d0.w,mixtab+1)
	move.w	(a0,d3.w,.table1-.table),d0
	move.b	d2,(a5,d0.w)	;sam_mx? beallitas
	tst.l	d2
	beq.b	.0
	move.w	(a0,d3.w,.table2-.table),d0
	cmp.w	#3,(a5,d0.w,gui_modes)	;nema volt?
	bne.b	.0
	move.w	(a0,d3.w,.table3-.table),d0
	move.l	(a5,d0.w),d4
	;seti	d4,#MUIA_Cycle_Active,#0	;stereo
.0	move.l	vssbase(a5),a6
	move.l	main_handle(a5),a0
	st	d0
	jsr	_LVOvssRefreshMixer(a6)
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

.table	dc.w	VMT_0l,VMT_1l,VMT_2l,VMT_3l
.table1	dc.w	sam_mx1,sam_mx2,sam_mx3,sam_mx4
.table2	dc.w	cmode1,cmode2,cmode3,cmode4
.table3	dc.w	gad_mm1,gad_mm2,gad_mm3,gad_mm4

; input level monitor mod valtas

gui_levgrphook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	clr.l	d2
	move.b	sam_levmode(a5),d2
	addq.b	#1,d2
	cmp.b	#3,d2
	bne.b	.0
	clr.b	d2
.0	move.b	d2,sam_levmode(a5)
	seti	gad_levelgroup(a5),#MUIA_Group_ActivePage,d2
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; config level drop spd

gui_declhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1)+,d0
	move.b	d0,conftmp+conf_samdeclev(a5)
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

gui_samtyphook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1),d2
	;move.b	d2,sam_mode(a5)
	move.l	(pc,d2.w*4,.table),d3
	;seti	gad_ftyp2(a5),#MUIA_Cycle_Active,#0
	;seti	gad_ftyp2(a5),#MUIA_Cycle_Entries,d3
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

.table	dc.l	guisa_typ8
	dc.l	guisa_typ8
	dc.l	guisa_typ16
	dc.l	guisa_typ16
	dc.l	guisa_typ16
	dc.l	guisa_typ16

; prepare file

gad_sopenhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d0-a6,-(sp)
	move.l	var(pc),a5
	tst.l	samplerproc(a5)
	bne.w	.noproc
	lea	sam_vsstags(pc),a4
	clr.l	-(sp)	;query frequency
	geti	gad_sfreq(a5),#MUIA_Slider_Level,sp
	move.l	(sp)+,d0
	lea	freqtab,a0
	move.l	(a0,d0.w*4),sam_vssfreq-sam_vsstags(a4)
	clr.l	-(sp)	;query bits
	geti	gad_sftype(a5),#MUIA_Cycle_Active,sp
	move.l	(sp)+,d0
	asl.l	#2,d0
	move.l	(a4,d0.w,.sambittab-sam_vsstags),sam_vssbits-sam_vsstags(a4)
	move.l	conftmp+conf_flags(a5),d1
	btst	#cflg_levelhist,d1
	beq.b	.00
	add.w	#.samlevelhooks-.samhooktab,d0
	bset	#sfl_histmon,sam_flags(a5)
	bclr	#lfl_monitor,ll_flags(a5)
.00	move.l	(a4,d0.w,.samhooktab-sam_vsstags),sam_vsshook-sam_vsstags(a4)
	clr.l	-(sp)	;query filename
	geti	gad_sfname(a5),#MUIA_String_Contents,sp
	move.l	(sp)+,sam_filename(a5)

	move.l	dosbase(a5),a6	;open file
	move.l	sam_filename(a5),d1
	move.l	#MODE_NEWFILE,d2
	move.l	conftmp+conf_flags(a5),d3
	btst	#cflg_samappend,d3
	beq.b	.001
	subq.l	#MODE_NEWFILE-MODE_READWRITE,d2
.001	jsr	_LVOOpen(a6)
	move.l	d0,sam_file(a5)
	bne.b	.fileok
	bsr.w	doserror
	bra.w	.noproc
.fileok	btst	#cflg_samappend,d3	;ha append,
	beq.b	.010
	move.l	d0,d1	;a file vegere seekelunk
	clr.l	d2
	moveq	#OFFSET_END,d3
	jsr	_LVOSeek(a6)
.010	move.l	excbase(a5),a6
	moveq	#-1,d0
	jsr	_LVOAllocSignal(a6)
	move.l	d0,-(sp)
	tst.b	d0
	bmi.b	.baj
	clr.l	d1
	bset	d0,d1
	move.l	d1,subtasksig(a5)
	move.l	dosbase(a5),a6
	lea	.proctags(pc),a0
	move.l	a0,d1
	jsr	_LVOCreateNewProc(a6)
	move.l	excbase(a5),a6
	move.l	d0,samplerproc(a5)
	beq.b	.baj
	move.l	subtasksig(a5),d0	;megvarjuk
	jsr	_LVOWait(a6)
.baj	move.l	(sp)+,d0
	jsr	_LVOFreeSignal(a6)
	tst.l	sam_port(a5)	;dead or alive?
	beq.w	.close

	seti	gad_sopen(a5),#MUIA_Disabled,#TRUE
	seti	gad_sclose(a5),#MUIA_Disabled,#FALSE
	seti	gad_sstart(a5),#MUIA_Disabled,#FALSE
	seti	gad_sfreq(a5),#MUIA_Disabled,#TRUE
	seti	gad_sfname(a5),#MUIA_Disabled,#TRUE
	seti	gad_samfilebut(a5),#MUIA_Disabled,#TRUE
	seti	gad_sftype(a5),#MUIA_Disabled,#TRUE
	seti	gad_ftyp2(a5),#MUIA_Disabled,#TRUE
	;bset	#sfl_opened,sam_flags(a5)
	bra.b	.noproc

.close	move.l	dosbase(a5),a6
	move.l	sam_file(a5),d1
	jsr	_LVOClose(a6)
.noproc	movem.l	(sp)+,d0-a6
	rts

.proctags	dc.l	NP_Entry,samplerentry
	dc.l	NP_Name,smpname
	dc.l	NP_StackSize,8192
	dc.l	NP_Priority,0
	dc.l	NP_WindowPtr,-1
	dc.l	0

.samhooktab	dc.l	samh_8m	;normal hooks
	dc.l	samh_8s
	dc.l	samh_16m
	dc.l	samh_16mi
	dc.l	samh_16s
	dc.l	samh_16si
.samlevelhooks	dc.l	samh_l8m	;levelmeter hooks
	dc.l	samh_l8s
	dc.l	samh_l16m
	dc.l	samh_l16mi
	dc.l	samh_l16s
	dc.l	samh_l16si

.sambittab	dc.l	8
	dc.l	8
	dc.l	16
	dc.l	16
	dc.l	16
	dc.l	16

sam_vsstags	dc.l	VSS_freq
sam_vssfreq	dc.l	0
	dc.l	VSS_bits
sam_vssbits	dc.l	0
	dc.l	VSS_hook
sam_vsshook	dc.l	0
	dc.l	VSS_mode,1
	dc.l	VSS_channels,2
	;dc.l	VSS_maxbuf,254
	dc.l	TAG_DONE

doserror	lea	-maxdoserr(sp),sp
	move.l	dosbase(a5),a6
	jsr	_LVOIoErr(a6)
	move.l	d0,d1
	lea	guit_doserr(pc),a0
	move.l	a0,d2
	move.l	sp,d3
	move.l	#maxdoserr-1,d4
	jsr	_LVOFault(a6)
	move.l	sp,a0
	bsr.w	printFault
	lea	maxdoserr(sp),sp
	rts

; close file

gad_sclosehook	dc.l	0,0,.entry,0,0

.entry	movem.l	d0-a6,-(sp)
	move.l	var(pc),a5
	bclr	#sfl_histmon,sam_flags(a5)
	bset	#lfl_monitor,ll_flags(a5)
	seti	gad_sopen(a5),#MUIA_Disabled,#FALSE
	seti	gad_sclose(a5),#MUIA_Disabled,#TRUE
	seti	gad_sstart(a5),#MUIA_Disabled,#TRUE
	seti	gad_sstop(a5),#MUIA_Disabled,#TRUE
	seti	gad_sfreq(a5),#MUIA_Disabled,#FALSE
	seti	gad_sfname(a5),#MUIA_Disabled,#FALSE
	seti	gad_samfilebut(a5),#MUIA_Disabled,#FALSE
	seti	gad_sftype(a5),#MUIA_Disabled,#FALSE
	seti	gad_ftyp2(a5),#MUIA_Disabled,#FALSE
	seti	gad_samstatus(a5),#MUIA_Text_Contents,#guit_status
	move.l	sam_port(a5),d0
	beq.b	.noproc
	btst	#sfl_start,sam_flags(a5)
	beq.b	.0
	move.l	d0,a0
	moveq	#VSSpkt_Stop,d0
	move.l	main_port(a5),a1
	bsr.w	doPkt
.0	moveq	#VSSpkt_Kill,d0
	move.l	sam_port(a5),a0
	move.l	main_port(a5),a1
	bsr.w	doPkt
	move.l	dosbase(a5),a6
	move.l	sam_file(a5),d1
	jsr	_LVOClose(a6)
	clr.l	sam_file(a5)
	;bclr	#sfl_opened,sam_flags(a5)
.noproc	movem.l	(sp)+,d0-a6
	rts

gad_sstarthook	dc.l	0,0,.entry,0,0

.entry	movem.l	d0-a6,-(sp)
	move.l	var(pc),a5
	seti	gad_sstart(a5),#MUIA_Disabled,#TRUE
	seti	gad_sstop(a5),#MUIA_Disabled,#FALSE
	move.l	sam_port(a5),d0
	beq.b	.noproc
	move.l	d0,a0
	moveq	#VSSpkt_Sample,d0
	move.l	main_port(a5),a1
	bsr.w	doPkt
.noproc	movem.l	(sp)+,d0-a6
	rts

gad_sstophook	dc.l	0,0,.entry,0,0

.entry	movem.l	d0-a6,-(sp)
	move.l	var(pc),a5
	seti	gad_sstart(a5),#MUIA_Disabled,#FALSE
	seti	gad_sstop(a5),#MUIA_Disabled,#TRUE
	seti	gad_samstatus(a5),#MUIA_Text_Contents,#guit_status2
	move.l	sam_port(a5),d0
	beq.b	.noproc
	move.l	d0,a0
	moveq	#VSSpkt_Stop,d0
	move.l	main_port(a5),a1
	bsr.w	doPkt
.noproc	movem.l	(sp)+,d0-a6
	rts

samplerentry	move.l	var(pc),a5
	lea	d_samstart,a0	;debug
	move.l	a0,d1
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	move.l	excbase(a5),a6
	clr.l	d7	;no packet (finalizernek)
	moveq	#-1,d0
	jsr	_LVOAllocSignal(a6)
	move.b	d0,sam_signal(a5)
	bmi.w	.err2
	clr.l	d1
	bset	d0,d1
	move.l	d1,sam_sig(a5)
	jsr	_LVOCreateMsgPort(a6)
	move.l	d0,sam_port(a5)
	beq.w	.err2
	bsr.l	portsig
	move.l	d0,sam_portsig(a5)
	move.l	conftmp+conf_sambufsize(a5),d0  ;allocate buffer
	asl.l	#8,d0
	asl.l	#2,d0
	move.l	#MEMF_PUBLIC,d1
	move.l	d0,sam_bufsize(a5)
	move.l	d0,d2
	lsr.l	#sambufdiv,d2
	move.l	d2,sam_chunk(a5)
	jsr	_LVOAllocVec(a6)
	move.l	d0,sam_buffer(a5)
	beq.w	.err2
	lea	vssname,a1	;open vss.lib
	moveq	#vssver,d0
	jsr	_LVOOpenLibrary(a6)
	tst.l	d0
	beq.w	.err2
	move.l	vssbase(a5),a6	;alloc vsshandle
	lea	samplername,a0
	jsr	_LVOvssAllocHandle(a6)
	move.l	d0,sam_vhandle(a5)
	beq.w	.err
	move.l	d0,a0
	jsr	_LVOvssAllocAudio(a6)
	tst.l	d0
	bne.w	.err
	move.l	sam_vhandle(a5),a0
	lea	sam_vsstags(pc),a1
	jsr	_LVOvssInit(a6)
	tst.l	d0
	beq.w	.err3
	clr.l	sam_ptr1(a5)
	clr.l	sam_ptr2(a5)
	clr.l	sam_pos(a5)
	bclr	#sfl_go,sam_flags(a5)
	move.l	sam_vhandle(a5),a0
	jsr	_LVOvssStart(a6)
	move.l	excbase(a5),a6


	; setup actions


	move.l	mainproc(a5),a1	;we are alive!
	move.l	subtasksig(a5),d0
	jsr	_LVOSignal(a6)

.loop	move.l	sam_portsig(a5),d0	;wait for new messages
	or.l	sam_sig(a5),d0
	jsr	_LVOWait(a6)
	move.l	d0,sam_sigrcvd(a5)
	beq.b	.loop
	and.l	sam_sig(a5),d0
	bne.b	.writebuf
.nosig	move.l	excbase(a5),a6
	move.l	sam_sigrcvd(a5),d0
	and.l	sam_portsig(a5),d0
	beq.b	.loop
.port	move.l	sam_port(a5),a0	;process packets
	jsr	_LVOGetMsg(a6)
	tst.l	d0
	beq.b	.loop
	move.l	d0,a3
	move.l	pkt_cmd(a3),d0
	cmp.l	#VSSpkt_Kill,d0
	beq.w	.kill
	cmp.l	#VSSpkt_Sample,d0
	beq.b	.sample
	cmp.l	#VSSpkt_Stop,d0
	beq.b	.stop
	moveq	#VSSERR_BadPkt,d0
	bsr.l	panic
	illegal

.sample	bset	#sfl_start,sam_flags(a5)
	bset	#sfl_go,sam_flags(a5)
	bra.b	.done

.stop	bclr	#sfl_start,sam_flags(a5)
	bclr	#sfl_go,sam_flags(a5)
	bra.b	.done

; Write (ptr1): 0-nal tovabbmegy, nem tolt nullaig
; Read  (ptr2): nullaig olvas, nullanal megall

.writebuf	move.l	sam_ptr2(a5),d1
	move.l	d1,d2
	add.l	sam_buffer(a5),d2	;d2:olvasas kezdete
	move.l	sam_ptr1(a5),d3
	sub.l	d1,d3
	bcs.b	.veg
	cmp.l	sam_chunk(a5),d3
	bcs.b	.nosig
	bra.b	.do
.veg	move.l	sam_bufsize(a5),d3
	sub.l	d1,d3
.do	move.l	dosbase(a5),a6
	move.l	sam_file(a5),d1
	jsr	_LVOWrite(a6)
	tst.l	d0
	bmi.b	.iohiba
	add.l	d0,sam_pos(a5)
	add.l	sam_ptr2(a5),d0
	cmp.l	sam_bufsize(a5),d0
	bcs.b	.0
	clr.l	d0
.0	move.l	d0,sam_ptr2(a5)
	bra.b	.writebuf
.iohiba	bclr	#sfl_start,sam_flags(a5)
	bclr	#sfl_go,sam_flags(a5)
	bra.w	.nosig

.done	move.l	a3,a1	;reply packet, continue
	bsr.w	replyPkt
	bra.w	.port
.kill	move.l	a3,d7	;pkt_Kill
	move.l	vssbase(a5),a6
	move.l	sam_vhandle(a5),a0
	jsr	_LVOvssStop(a6)
.err3	move.l	sam_vhandle(a5),a0
	jsr	_LVOvssFreeAudio(a6)
.err	move.l	sam_vhandle(a5),a0
	jsr	_LVOvssFreeHandle(a6)
	clr.l	sam_vhandle(a5)
	move.l	excbase(a5),a6
	move.l	vssbase(a5),a1
	jsr	_LVOCloseLibrary(a6)
.err2	move.l	sam_buffer(a5),a1	;free buffer
	jsr	_LVOFreeVec(a6)
	clr.l	sam_buffer(a5)
	move.l	sam_port(a5),a0	;delete port
	jsr	_LVODeleteMsgPort(a6)
	clr.l	sam_port(a5)
	move.b	sam_signal(a5),d0
	jsr	_LVOFreeSignal(a6)
	lea	d_samex,a0	;debug
	move.l	a0,d1
	moveq	#VSSpkt_PrintDebug,d0
	move.l	cport(a5),a0
	sub.l	a1,a1
	bsr.w	sendPkt
	jsr	_LVOForbid(a6)
	clr.l	samplerproc(a5)
	tst.l	d7
	bne.b	.2
	move.l	mainproc(a5),a1	;we are dead!
	move.l	subtasksig(a5),d0
	jsr	_LVOSignal(a6)
	bra.b	.4
.2	move.l	d7,a1	;the killer msg :)
	bsr.w	replyPkt
.4	clr.l	d0
	rts


; Write (ptr1): 0-nal tovabbmegy, nem tolt nullaig
; Read  (ptr2): nullaig olvas, nullanal megall

; In D0:bytes_req Z:buffer_overrun

SAMH_PRE1	MACRO
	movem.l	d2-d3/d6-d7/a4/a6,-(sp)
	move.l	var(pc),a4
	beq.b	.noover
	bset	#sfl_overrun,sam_flags(a4)
.noover	clr.l	d6	;bytesdone
	clr.l	d7	;remaining
	move.w	d0,d7
	btst	#sfl_go,sam_flags(a4)
	beq.w	.3
	ENDM

; In D6:bytes_done D7:bytes_to_go

SAMH_PRE2	MACRO
.meg	move.l	sam_ptr1(a4),d1
	move.l	d1,a0
	add.l	sam_buffer(a4),a0	;a0:iras kezdete
	move.l	sam_ptr2(a4),d0
	sub.l	d1,d0
	bhi.b	.do
	move.l	sam_bufsize(a4),d0
	sub.l	d1,d0
	tst.l	sam_ptr2(a4)
	bne.b	.do2
.do	subq.l	#1,d0
.do2	cmp.l	d7,d0
	bcs.b	.1
	move.l	d7,d0
.1	and.w	#$fff0,d0
	beq.w	.ok
	move.l	d0,d2
	ENDM

; In D2:count  Out D0:bytes done  Scratch D1 D3

SAMH_POST1	MACRO
	move.l	sam_ptr1(a4),d1
	add.l	d0,d1
	cmp.l	sam_bufsize(a4),d1
	bcs.b	.2
	clr.l	d1
.2	move.l	d1,sam_ptr1(a4)
	add.l	d0,d6
	sub.l	d0,d7
	bne.w	.meg
.ok	move.l	excbase(a4),a6
	move.l	sam_sig(a4),d0
	move.l	samplerproc(a4),a1
	jsr	_LVOSignal(a6)
	move.l	d6,d0
	ENDM

SAMH_POST2	MACRO
.done	movem.l	(sp)+,d2-d3/d6-d7/a4/a6
	rts
.3	and.w	#$fff0,d7
	move.l	d7,d0
	beq.b	.done
	ENDM

SAMH_POST3	MACRO
	lsr.w	#2,d7
	subq.w	#1,d7
.4	tst.b	(a5)
	tst.b	(a5)
	tst.b	(a5)
	tst.b	(a5)
	dbf	d7,.4
	bra.b	.done
	ENDM

SAMH_PRE	MACRO
	SAMH_PRE1
	SAMH_PRE2
	ENDM

SAMH_PREMONO	MACRO
	SAMH_PRE1
	lsr.l	d7
	SAMH_PRE2
	ENDM

SAMH_POST	MACRO
	SAMH_POST1
	SAMH_POST2
	SAMH_POST3
	ENDM

SAMH_POSTMONO	MACRO
	SAMH_POST1
	asl.l	d0
	SAMH_POST2
	SAMH_POST3
	ENDM

SAMH_LPOST	MACRO
	SAMH_POST1
	SAMH_POST2
	ENDM

SAMH_LPOSTMONO	MACRO
	SAMH_POST1
	asl.l	d0
	SAMH_POST2
	ENDM

; 8M (In 8 Out 4)

samh_8m	dc.l	0,0,.entry,0,0

.entry	SAMH_PREMONO
	;move.w	#$0f00,$dff180
	lsr.l	d2
	subq.l	#1,d2
.0	move.b	(a5),d1
	move.b	(a5),d3
	asr.b	d1
	asr.b	d3
	add.b	d1,d3
	move.b	d3,(a0)+
	move.b	(a5),d1
	move.b	(a5),d3
	asr.b	d1
	asr.b	d3
	add.b	d1,d3
	move.b	d3,(a0)+
	dbf	d2,.0
	;move.w	#$0009,$dff180
	SAMH_POSTMONO

; 8S, 16S (In 8 Out 8)

samh_8s
samh_16s	dc.l	0,0,.entry,0,0

.entry	SAMH_PRE
	lsr.l	#3,d2
	subq.w	#1,d2
.0	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	move.b	(a5),(a0)+
	dbf	d2,.0
	SAMH_POST

; 16M (In 8 Out 4)

samh_16m	dc.l	0,0,.entry,0,0

.entry	SAMH_PREMONO
	lsr.l	#2,d2
	subq.w	#1,d2
.0	move.b	(a5),d1
	asl.w	#8,d1
	move.b	(a5),d1
	move.b	(a5),d3
	asl.w	#8,d3
	move.b	(a5),d3
	asr.w	d1
	asr.w	d3
	add.l	d1,d3
	move.w	d3,(a0)+
	move.b	(a5),d1
	asl.w	#8,d1
	move.b	(a5),d1
	move.b	(a5),d3
	asl.w	#8,d3
	move.b	(a5),d3
	asr.w	d1
	asr.w	d3
	add.l	d1,d3
	move.w	d3,(a0)+
	dbf	d2,.0
	SAMH_POSTMONO

; 16MI (In 8 Out 4)

samh_16mi	dc.l	0,0,.entry,0,0

.entry	SAMH_PREMONO
	lsr.l	#2,d2
	subq.w	#1,d2
.0	move.b	(a5),d1
	asl.w	#8,d1
	move.b	(a5),d1
	move.b	(a5),d3
	asl.w	#8,d3
	move.b	(a5),d3
	asr.w	d1
	asr.w	d3
	add.l	d1,d3
	rol.w	#8,d3
	move.w	d3,(a0)+
	move.b	(a5),d1
	asl.w	#8,d1
	move.b	(a5),d1
	move.b	(a5),d3
	asl.w	#8,d3
	move.b	(a5),d3
	asr.w	d1
	asr.w	d3
	add.l	d1,d3
	rol.w	#8,d3
	move.w	d3,(a0)+
	dbf	d2,.0
	SAMH_POSTMONO

; 16SI (In 8 Out 8)

samh_16si	dc.l	0,0,.entry,0,0

.entry	SAMH_PRE
	lsr.l	#3,d2
	subq.w	#1,d2
.0	move.b	(a5),1(a0)
	move.b	(a5),(a0)
	move.b	(a5),3(a0)
	move.b	(a5),2(a0)
	move.b	(a5),5(a0)
	move.b	(a5),4(a0)
	move.b	(a5),7(a0)
	move.b	(a5),6(a0)
	addq.w	#8,a0
	dbf	d2,.0
	SAMH_POST

; levelmetert is mukodteto (lassu) hookok

; 8M (In 8 Out 4)

samh_l8m	dc.l	0,0,.entry,0,0

.entry	SAMH_PREMONO
	add.l	d2,sam_hists(a4)
	add.l	d2,sam_hists(a4)
	lsr.l	d2
	subq.l	#1,d2
	move.l	sam_hist(a4),a1
	clr.w	d3
.0	move.b	(a5),d1
	move.b	(a5),d3
	asr.b	d1
	asr.b	d3
	add.b	d1,d3
	move.b	d3,(a0)+
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),d1
	move.b	(a5),d3
	asr.b	d1
	asr.b	d3
	add.b	d1,d3
	move.b	d3,(a0)+
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	dbf	d2,.0
	SAMH_LPOSTMONO
	add.l	d7,sam_hists(a4)
	add.l	d7,sam_hists(a4)
	lsr.l	#2,d7
	subq.w	#1,d7
	clr.w	d3
	move.l	sam_hist(a4),a1
.4	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	dbf	d7,.4
	bra.b	.done

; 8S (In 8 Out 8)

samh_l8s	dc.l	0,0,.entry,0,0

.entry	SAMH_PRE
	add.l	d2,sam_hists(a4)
	lsr.l	#2,d2
	subq.w	#1,d2
	clr.w	d3
	move.l	sam_hist(a4),a1
.0	move.b	(a5),d3
	move.b	d3,(a0)+
	add.w	#1,(a1,d3.w*2)
	move.b	(a5),d3
	move.b	d3,(a0)+
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),d3
	move.b	d3,(a0)+
	add.w	#1,(a1,d3.w*2)
	move.b	(a5),d3
	move.b	d3,(a0)+
	add.w	#1,(a1,d3.w*2,512)
	dbf	d2,.0
	SAMH_LPOST
	add.l	d7,sam_hists(a4)
	lsr.l	#2,d7
	subq.w	#1,d7
	clr.w	d3
	move.l	sam_hist(a4),a1
.4	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2,512)
	dbf	d7,.4
	bra.b	.done

; 16S (In 8 Out 8)

samh_l16s	dc.l	0,0,.entry,0,0

.entry	SAMH_PRE
	lsr.l	d2
	add.l	d2,sam_hists(a4)
	lsr.l	#2,d2
	subq.w	#1,d2
	clr.w	d3
	move.l	sam_hist(a4),a1
.0	move.b	(a5),d3
	move.b	d3,(a0)+
	add.w	#1,(a1,d3.w*2)
	move.b	(a5),(a0)+
	move.b	(a5),d3
	move.b	d3,(a0)+
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),(a0)+
	move.b	(a5),d3
	move.b	d3,(a0)+
	add.w	#1,(a1,d3.w*2)
	move.b	(a5),(a0)+
	move.b	(a5),d3
	move.b	d3,(a0)+
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),(a0)+
	dbf	d2,.0
	SAMH_LPOST
	lsr.l	d7
	add.l	d7,sam_hists(a4)
	lsr.l	#2,d7
	subq.w	#1,d7
	clr.w	d3
	move.l	sam_hist(a4),a1
.4	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	dbf	d7,.4
	bra.b	.done

; 16M (In 8 Out 4)

samh_l16m	dc.l	0,0,.entry,0,0

.entry	SAMH_PREMONO
	add.l	d2,sam_hists(a4)
	lsr.l	#2,d2
	subq.w	#1,d2
	move.l	sam_hist(a4),a1
.0	move.b	(a5),d1
	asl.w	#8,d1
	move.b	(a5),d1
	move.b	(a5),d3
	asl.w	#8,d3
	move.b	(a5),d3
	asr.w	d1
	asr.w	d3
	add.l	d1,d3
	move.w	d3,(a0)+
	lsr.w	#8,d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),d1
	asl.w	#8,d1
	move.b	(a5),d1
	move.b	(a5),d3
	asl.w	#8,d3
	move.b	(a5),d3
	asr.w	d1
	asr.w	d3
	add.l	d1,d3
	move.w	d3,(a0)+
	lsr.w	#8,d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	dbf	d2,.0
	SAMH_LPOSTMONO
	add.l	d7,sam_hists(a4)
	lsr.l	#3,d7
	subq.w	#1,d7
	clr.w	d3
	move.l	sam_hist(a4),a1
.4	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	dbf	d7,.4
	bra	.done

; 16MI (In 8 Out 4)

samh_l16mi	dc.l	0,0,.entry,0,0

.entry	SAMH_PREMONO
	add.l	d2,sam_hists(a4)
	lsr.l	#2,d2
	subq.w	#1,d2
	move.l	sam_hist(a4),a1
.0	move.b	(a5),d1
	asl.w	#8,d1
	move.b	(a5),d1
	move.b	(a5),d3
	asl.w	#8,d3
	move.b	(a5),d3
	asr.w	d1
	asr.w	d3
	add.l	d1,d3
	rol.w	#8,d3
	move.w	d3,(a0)+
	and.w	#$00ff,d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),d1
	asl.w	#8,d1
	move.b	(a5),d1
	move.b	(a5),d3
	asl.w	#8,d3
	move.b	(a5),d3
	asr.w	d1
	asr.w	d3
	add.l	d1,d3
	rol.w	#8,d3
	move.w	d3,(a0)+
	and.w	#$00ff,d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	dbf	d2,.0
	SAMH_LPOSTMONO
	add.l	d7,sam_hists(a4)
	lsr.l	#3,d7
	subq.w	#1,d7
	clr.w	d3
	move.l	sam_hist(a4),a1
.4	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	dbf	d7,.4
	bra	.done

; 16SI (In 8 Out 8)

samh_l16si	dc.l	0,0,.entry,0,0

.entry	SAMH_PRE
	lsr.l	d2
	add.l	d2,sam_hists(a4)
	lsr.l	#2,d2
	subq.w	#1,d2
	move.l	sam_hist(a4),a1
	clr.w	d3
.0	move.b	(a5),d3
	move.b	d3,1(a0)
	add.w	#1,(a1,d3.w*2)
	move.b	(a5),(a0)
	move.b	(a5),d3
	move.b	d3,3(a0)
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),2(a0)
	move.b	(a5),d3
	move.b	d3,5(a0)
	add.w	#1,(a1,d3.w*2)
	move.b	(a5),4(a0)
	move.b	(a5),d3
	move.b	d3,7(a0)
	add.w	#1,(a1,d3.w*2,512)
	move.b	(a5),6(a0)
	addq.w	#8,a0
	dbf	d2,.0
	SAMH_LPOST
	lsr.l	d7
	add.l	d7,sam_hists(a4)
	lsr.l	#2,d7
	subq.w	#1,d7
	clr.w	d3
	move.l	sam_hist(a4),a1
.4	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2)
	tst.b	(a5)
	move.b	(a5),d3
	add.w	#1,(a1,d3.w*2,512)
	tst.b	(a5)
	dbf	d7,.4
	bra	.done

; mixer gui update
; (levelmeterek, digi stream)

guiupdate	cmp.b	#page_sampler,gui_actpage(a5)
	bne.w	.3
	tst.b	sam_levmode(a5)
	beq.w	.2
	move.b	conftmp+conf_samdeclev(a5),d1	;level csokkentese
	move.b	sam_leftlev(a5),d0
	beq.b	.35
	sub.b	d1,d0
	bcc.b	.35
	clr.b	d0
.35	move.b	d0,sam_leftlev(a5)
	move.b	sam_rightlev(a5),d0
	beq.b	.36
	sub.b	d1,d0
	bcc.b	.36
	clr.b	d0
.36	move.b	d0,sam_rightlev(a5)
	btst	#lfl_monitor,ll_flags(a5)	;normal mode?
	bne.w	.55

	move.l	sam_hists(a5),d2	;eleg minta van mar?
	cmp.l	#256,d2
	bcs.w	.4	;nem
	move.l	sam_hist(a5),a2	;swap histogram buffers
	move.l	sam_hist2(a5),sam_hist(a5)
	move.l	a2,sam_hist2(a5)
	clr.l	sam_hists(a5)
	move.b	sam_levdiv(a5),d1
	bne.b	.37

	lea	256(a2),a2	;maximum kereses
	move.l	a2,a1
	moveq	#-1,d3
.66	tst.w	510(a2)	;Left & Right
	bne.b	.8
	IFD	quantizeneg
	tst.w	512(a1)
	bne.b	.8
	ENDIF
	tst.w	-(a2)
	bne.b	.77
	IFD	quantizeneg
	tst.w	(a1)+
	bne.b	.7
	ENDIF
	subq.b	#2,d3
	bcc.b	.66
	clr.l	d5
	clr.l	d6
	bra.w	.10
.77	IFD	quantizeneg
	addq.l	#2,a1
	ENDIF
.7	move.l	d3,d5	;Left OK, continue Right
	IFD	quantizeneg
	lea	512(a1),a1
	ENDIF
	lea	512(a2),a2
	bra.b	.71
.72	tst.w	-(a2)
	bne.b	.73
	IFD	quantizeneg
	tst.w	(a1)+
	bne.b	.73
	ENDIF
.71	subq.b	#2,d3
	bcc.b	.72
	clr.l	d3
.73	move.l	d3,d6
	bra.w	.10
.8	IFD	quantizeneg
	addq.l	#2,a1
	ENDIF
	move.l	d3,d6	;Right OK, continue Left
	bra.b	.81
.82	tst.w	-(a2)
	bne.b	.83
	IFD	quantizeneg
	tst.w	(a1)+
	bne.b	.83
	ENDIF
.81	subq.b	#2,d3
	bcc.b	.82
	clr.l	d3
.83	move.l	d3,d5
	bra.b	.10

.37	move.l	d2,d0	;calculate limit
	lsr.l	d1,d0
	sub.l	d0,d2
	subq.l	#2,d2
.38	IFD	quantizeneg	;quantize histograms
	lea	512(a2),a1
	lsr.l	d2
	ELSE
	lsr.l	#2,d2
	ENDIF
	clr.l	d0 ;lCnt
	clr.l	d1 ;rCnt
	clr.l	d3 ;counter
	clr.l	d4 ;scratch
.0	move.w	512(a2),d4	;Left & Right
	IFD	quantizeneg
	add.w	510(a1),d4
	ENDIF
	add.l	d4,d1
	move.w	(a2)+,d4
	IFD	quantizeneg
	add.w	-(a1),d4
	ENDIF
	add.l	d4,d0
	cmp.l	d1,d2
	bcs.b	.1	;Right done
	cmp.l	d0,d2
	bcs.b	.22	;Left done
	addq.b	#2,d3
	bne.b	.0
	moveq	#-1,d5
	move.l	d5,d6
	bra.b	.10
.1	move.l	d3,d6	;Right OK, continue Left
	bra.b	.6
.5	move.w	(a2)+,d4
	IFD	quantizeneg
	add.w	-(a1),d4
	ENDIF
	add.l	d4,d0
	cmp.l	d0,d2
	bcs.b	.9
.6	addq.b	#2,d3
	bne.b	.5
	moveq	#-1,d3
.9	move.l	d3,d5
	bra.b	.10
.22	move.l	d3,d5	;Left OK, continue Right
	IFD	quantizeneg
	lea	512(a1),a1
	ENDIF
	lea	512(a2),a2
	bra.b	.11
.12	move.w	(a2)+,d4
	IFD	quantizeneg
	add.w	-(a1),d4
	ENDIF
	add.l	d4,d1
	cmp.l	d1,d2
	bcs.b	.13
.11	addq.b	#2,d3
	bne.b	.12
	moveq	#-1,d3
.13	move.l	d3,d6

.10	move.l	sam_hist2(a5),a0	;clear histograms
	IFD	quantizeneg
	moveq	#31,d0
	ELSE
	moveq	#15,d0
	ENDIF
.15	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	dbf	d0,.15
	lsr.b	#2,d5
	lsr.b	#2,d6
	bra.b	.44

.55	move.b	sam_leftlev2(a5),d5	;lowlevel altal
	move.b	sam_rightlev2(a5),d6	;beolvasott ertekek

.44	move.b	sam_leftlev(a5),d3	;kiteres nagyobb?
	move.b	sam_rightlev(a5),d4
	cmp.b	d3,d5
	bcc.b	.33
	move.b	d3,d5
.33	cmp.b	d4,d6
	bcc.b	.34
	move.b	d4,d6
.34	move.b	d5,sam_leftlev(a5)
	move.b	d6,sam_rightlev(a5)
.4	clr.l	d3	;megjelenites
	move.b	sam_levmode(a5),d0
	subq.b	#1,d0
	bne.b	.gauge
	move.b	sam_leftlev(a5),d3
	seti	gad_leftlev(a5),#MUIA_Numeric_Value,d3
	move.b	sam_rightlev(a5),d3
	seti	gad_rightlev(a5),#MUIA_Numeric_Value,d3
	bra.b	.2
.gauge	move.b	sam_leftlev(a5),d3
	seti	gad_leftlev2(a5),#MUIA_Gauge_Current,d3
	move.b	sam_rightlev(a5),d3
	seti	gad_rightlev2(a5),#MUIA_Gauge_Current,d3

.2	btst	#sfl_start,sam_flags(a5)	;file progress
	beq.b	.3
	move.l	updatequantum(a5),d0
	sub.l	d0,sam_updatcnt(a5)
	bcc.b	.3
	move.l	#samplerguiupdat,sam_updatcnt(a5)
	lea	-20(sp),sp
	move.l	excbase(a5),a6
	lea	guit_bytes(pc),a0
	move.l	sam_pos(a5),d0
	lsr.l	#8,d0
	lsr.l	#2,d0
	move.l	d0,-(sp)
	move.l	sp,a1
	lea	.putchproc(pc),a2
	move.l	sp,a3
	jsr	_LVORawDoFmt(a6)
	move.l	sp,d3
	seti	gad_samstatus(a5),#MUIA_Text_Contents,d3
	lea	24(sp),sp
.3	rts
.putchproc	move.b	d0,(a3)+
	rts





; set AHI & MPEG options

; (ujra kene irni ezt a szart, de elobb ScaleToValue/ValueToScale patch)

gui_opthook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1)+,d2	;gadget
	move.l	(a1),d3	;level
	lea	conftmp+conf_ahibuftime(a5),a3
	tst.l	d2
	beq.w	.set	;ahibuf
	subq.l	#1,d2
	bne.b	.nompbuf
	asl.l	#8,d3	;mpbuf
	;move.l	d3,d4
	asl.l	#2,d3
	lea	conftmp+conf_mpbufsize(a5),a3
	bra.w	.set
.nompbuf	subq.l	#1,d2
	bne.b	.nompchunk
	; jelenleg nem kerulhet ide a vezerles
	asl.l	#7,d3	;chunk
	move.l	d3,d4
	asl.l	#2,d4
	cmp.l	conftmp+conf_mpbufsize(a5),d4
	bls.b	.chunkok
	lsr.l	#8,d4	;update mpbuf
	lsr.l	#2,d4
	seti	gad_mpbufsize(a5),#MUIA_Slider_Level,d4
.chunkok	lea	conftmp+conf_mpchunk(a5),a3
	bra.w	.set
.nompchunk	lea	conftmp+conf_mptimecorr(a5),a3
	subq.l	#1,d2	;mptimecorr
	bne.b	.notimecorr
	bra.w	.set
.notimecorr	subq.l	#1,d2
	bne.b	.noquantum
	lea	quantum(a5),a3	;quantum
	move.l	d3,d0
	mulu.w	#1000,d0
	move.l	d0,quantum2(a5)
	move.l	d3,d4
	asl.l	d4
	cmp.l	conftmp+conf_ahibuftime(a5),d4
	bls.b	.set
	seti	gad_ahibuf(a5),#MUIA_Slider_Level,d4
	bra.b	.set
.noquantum	subq.l	#1,d2
	bne.b	.nollpri
	move.l	excbase(a5),a6
	move.w	d3,conftmp+conf_pri(a5)
	jsr	_LVOForbid(a6)
	tst.l	lport(a5)
	beq.b	.perm
	move.l	a5,a1
.setpri	move.l	d3,d0
	jsr	_LVOSetTaskPri(a6)
.perm	jsr	_LVOPermit(a6)
	bra.b	.nop
.nollpri	subq.l	#1,d2
	bne.b	.nospri
	move.l	excbase(a5),a6
	move.w	d3,conftmp+conf_spri(a5)
	jsr	_LVOForbid(a6)
	tst.l	mport(a5)
	beq.b	.perm
	lea	bufmantask(a5),a1
	bra.b	.setpri
.nospri	lea	conftmp+conf_melbuftime(a5),a3
	subq.l	#1,d2
	beq.b	.set
	bra.b	.nop

.set	move.l	d3,(a3)
.nop	movem.l	(sp)+,d2-d7/a2-a6
	rts

; checkbox -> conf_flags hook

gui_markhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1)+,d2	;bit
	;move.l	(a1),d3	;value
	move.l	conftmp+conf_flags(a5),d0
	bclr	d2,d0
	tst.l	(a1)
	beq.b	.0
	bset	d2,d0
.0	move.l	d0,conftmp+conf_flags(a5)
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; set debug level

debughook	dc.l	0,0,debugentry,0,0

debugentry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1),d0
	move.b	d0,debuglevel(a5)
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; Set channel names

namehook	dc.l	0,0,nameentry,0,0

nameentry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1)+,d2	;channel
	move.l	(a1),d3	;name
	lea	gad_chname0(a5),a3
	lea	(a3,d2.w*4),a3
	seti	(a3),#MUIA_Text_Contents,d3
	moveq	#chnnamelength,d0
	move.l	d3,a0
	mulu.w	d0,d2
	lea	(a5,d2.w,cnames),a1
	move.l	excbase(a5),a6
	jsr	_LVOCopyMem(a6)
.0	movem.l	(sp)+,d2-d7/a2-a6
	rts

; Save configuration

saveconfhook	dc.l	0,0,saveconfentry,0,0

saveconfentry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	bsr.l	writeconf
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; Bezart ablakot CXCMD_APPEAR-ra ujra megnyitja

gui_brokerhook	dc.l	0,0,brokerhookentry,0,0

brokerhookentry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	cdtbase(a5),a6
	move.l	a1,d2
	move.l	a1,a0
	jsr	_LVOCxMsgType(a6)
	cmp.l	#CXM_COMMAND,d0
	bne.b	.ex
	move.l	d2,a0
	jsr	_LVOCxMsgID(a6)
	cmp.l	#CXCMD_APPEAR,d0
	bne.b	.noapp
	bsr.l	popup
	;bra	.ex
.noapp	;cmp.l	#CXCMD_DISAPPEAR,d0	;A MUI elintezi nekunk
	;bne	.ex
	;seti	gui_window(a5),#MUIA_Window_Open,#FALSE
.ex	movem.l	(sp)+,d2-d7/a2-a6
	rts

; Mixer slider's hook

gui_mixhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d3/a4-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1)+,d2	;chn
	move.l	(a1),d3	;level
	lea	mixtab+VMT_0l(a5),a4
	move.b	d3,(a4,d2.w)
	move.l	d2,d1
	eor.b	#1,d2
	and.b	#$fe,d1
	lea	gui_modes(a5),a0
	move.w	(a0,d1.w),d0
	beq.b	.0	;stereo
	cmp.b	#2,d0	;mono?
	bne.b	.1	;nem
	cmp.b	#8,d1	;digital?
	beq.b	.1
.0	move.b	d3,(a4,d2.w)	;masik oldalt is allitjuk
	lea	gad_s2(a5),a1
	move.l	(a1,d2.w*4),a1
	seti	a1,#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.1	add.b	(a4,d2.w),d3	;ket oldal atlagat a
	lsr.b	d3	;sampler mixerebe
	and.b	#$fe,d2
	lea	gad_smix1(a5),a1
	move.l	(a1,d2.w*2),a1
	seti	a1,#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	cmp.b	#8,d2	;player updatelese
	bne.b	.noplayer
	bsr.w	setplayvolbal
.noplayer	move.l	vssbase(a5),a6
	move.l	main_handle(a5),a0
	st	d0
	jsr	_LVOvssRefreshMixer(a6)
	movem.l	(sp)+,d2-d3/a4-a6
	rts

; left,right,remain -> volume,balance
;
; vol=max(l,r)
;
; max(l,r)<>0:	bal=50 ± (50*min(l,r)+remain)/max(l,r)  (+ ha l>r, - ha l<r)
; max(l,r)=0:	bal=remain
;
; ha vol=0, balance-t jelenleg egyaltalan nem allitja be
;
; Sajnos nem lehet tudni hogy a player vagy egy masik program (pl maga a
; mixer) allitotta a hangerot, masik program eseten a korrekcio hibat okoz!

setplayvolbal	clr.l	d2
	clr.l	d3
	move.b	mixtab+VMT_4l(a5),d2
	move.b	mixtab+VMT_4r(a5),d3
	cmp.b	d2,d3
	scs	d1
	bcc.b	.3
	exg	d2,d3
.3	mulu.w	#50,d2
	add.w	ply_volremain(a5),d2
	ext.l	d2
	tst.w	d3
	beq.b	.4
	divu.w	d3,d2
	cmp.w	#50,d2
	bcs.b	.6
	moveq	#50,d2
.6	ext.l	d2
	neg.w	d2
	add.w	#50,d2
	tst.b	d1
	beq.b	.4
	neg.l	d2
.4	move.b	d3,ply_volume(a5)
	beq.b	.1	;ha vol=0, bal-t nem kell
	move.b	d2,ply_balance(a5)
          seti	gad_balance(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d2
.1        seti	gad_playvol(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	tst.l	d3
	beq.b	.2
          seti	gad_volreset(a5),#MUIA_NoNotify,#TRUE,#MUIA_Selected,#FALSE
.2	rts

; Mixer mode cycle's hook

gui_modehook	dc.l	0,0,modehookentry,0,0

modehookentry	movem.l	d2-d3/a3/a5-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1)+,d2	;chn
	move.l	(a1),d3	;mode
	lea	gui_modes(a5),a0
	move.w	d3,(a0,d2.w*2)
	cmp.b	#4,d2
	bne.w	.inp

	moveq	#3,d0	;digital channel
	cmp.b	#2,d3
	bne.b	.d0
	clr.l	d0	;mute,clear
.d0	move.w	d0,mixtab+VMT_switch2(a5)
	tst.l	d3
	bne.b	.d1
	move.b	mixtab+VMT_4l(a5),d3	;masik csatornat is
	add.b	mixtab+VMT_4r(a5),d3
	lsr.b	#1,d3
	move.b	d3,mixtab+VMT_4l(a5)
	move.b	d3,mixtab+VMT_4r(a5)
	seti	gad_s0(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	seti	gad_s1(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
.d1	move.l	vssbase(a5),a6
	move.l	main_handle(a5),a0
	st	d0	;async
	jsr	_LVOvssRefreshMixer(a6)
	bra.w	.ok

.mode	dc.b	%1001,%1001,%1111,%0000

.inp	move.b	(.mode,pc,d3.w),d0
	move.w	mixtab+VMT_switch1(a5),d1
	asl.b	#2,d2
	ror.w	d2,d1
	and.b	#%11110000,d1
	or.b	d0,d1
	rol.w	d2,d1
	move.w	d1,mixtab+VMT_switch1(a5)
	lsr.b	#1,d2
	tst.l	d3
	beq.b	.i0
	cmp.b	#2,d3
	bne.b	.i1
.i0	lea	mixtab+VMT_0l(a5),a0	;masik csatornat is
	move.b	(a0,d2.w),d3
	add.b	(VMT_0r-VMT_0l,a0,d2.w),d3
	lsr.b	#1,d3
	move.b	d3,(a0,d2.w)
	move.b	d3,(VMT_0r-VMT_0l,a0,d2.w)
	lea	gad_s2(a5),a3
	move.l	(a3,d2.w*4),a1
	seti	a1,#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	move.l	(gad_s3-gad_s2,a3,d2.w*4),a1
	seti	a1,#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3

.i1	move.l	vssbase(a5),a6
	move.l	main_handle(a5),a0
	st	d0	;async
	jsr	_LVOvssRefreshMixer(a6)
.ok	movem.l	(sp)+,d2-d3/a3/a5-a6
	rts

; MPEG slider's hook

gui_mpeghook	dc.l	0,0,.entry,0,0

.table	dc.w	VMT_bassfreq,VMT_bassenhance
	dc.w	VMT_trebfreq,VMT_trebenhance
	dc.w	VMT_toneatten,VMT_channelmap

.table2	dc.w	1,0,1,0,0,0	;0:BYTE 1:WORD

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1)+,d2	;slider
	move.l	(a1),d3	;level

	move.w	(.table,pc,d2.w),d0
	lea	mixtab(a5),a0
	move.w	(.table2,pc,d2.w),d1
	beq.b	.0
	move.w	d3,(a0,d0.w)
	bra.b	.1
.0	move.b	d3,(a0,d0.w)
.1
	tst.l	d2
	bne.b	.nobassfreq
	cmp.w	VMT_trebfreq(a0),d3	;adjust trebfreq if
	bcs.w	.3	;neccessary
	add.w	#freqgadstep,d3
	move.w	d3,VMT_trebfreq(a0)

	; itt kell a notify, mert az allitja be a megfelelo gadgeteket
	; a player lapon!

	;seti	gad_ms3(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	seti	gad_ms3(a5),#MUIA_Slider_Level,d3
	;or.l	#VMTLF_trebfreql!VMTLF_trebfreqh,d4
	bra.b	.3
.nobassfreq	cmp.b	#4,d2
	bne.b	.3
	cmp.w	VMT_bassfreq(a0),d3	;adjust bassfreq if
	bhi.b	.3	;neccessary
	sub.w	#freqgadstep,d3
	move.w	d3,VMT_bassfreq(a0)
	;seti	gad_ms1(a5),#MUIA_NoNotify,#TRUE,#MUIA_Slider_Level,d3
	seti	gad_ms1(a5),#MUIA_Slider_Level,d3
	;or.l	#VMTLF_bassfreql!VMTLF_bassfreqh,d4
	bra.w	.3

	; toneatten kiszamitasa problemas, majd az user beallitja maganak

;	move.b	VMT_bassenhance(a0),d2
;	move.b	VMT_trebenhance(a0),d1
;	cmp.b	d2,d1
;	blt	.2
;	move.b	d1,d2
;.2
;	move.b	VMT_bassenhance(a0),d2
;	add.b	VMT_trebenhance(a0),d2
;	add.b	#8,d2
;	bpl.b	.2
;	clr.b	d2
;.2	lsr.b	d2
;	cmp.b	VMT_toneatten(a0),d2
;	ble	.3
;	move.b	d2,VMT_toneatten(a0)
;	seti	gad_ms5(a5),#MUIA_Slider_Level,d2
;	or.l	#VMTLF_toneatten,d3

	; a flagek mar nem erdekesek, mindent frissitunk ami valtozott

.3	move.l	main_handle(a5),a0
	move.l	vssbase(a5),a6
	st	d0
	jsr	_LVOvssRefreshMixer(a6)
	movem.l	(sp)+,d2-d7/a2-a6
	rts

; sampler freq formazas

guisa_disphook	dc.l	0,0,.entry,0,0

.entry	lea	freqtab,a0
	move.l	(a0,d0.w*4),d0
	rts

; player time slider formazas [min:sec]

gui_plydisphook	dc.l	0,0,.entry,0,0

.entry	divu.w	#60,d0
	swap	d0
	rts

; treble enhance/frequency formazas

gui_tformhook	dc.l	0,0,.entry,0,0

.entry	lea	guip_hzform(pc),a0
	btst	#plf2_trebfreq,ply_flags2(a5)
	bne.b	.ok
	tst.l	d0
	lea	guip_dbmform(pc),a0
	bmi.b	.ok
	lea	guip_dbzform(pc),a0
	beq.b	.ok
	lea	guip_dbpform(pc),a0
.ok	move.l	a0,d0
	rts

; bass enhance/frequency formazas

gui_bformhook	dc.l	0,0,.entry,0,0

.entry	lea	guip_hzform(pc),a0
	btst	#plf2_bassfreq,ply_flags2(a5)
	bne.b	.ok
	tst.l	d0
	lea	guip_dbmform(pc),a0
	bmi.b	.ok
	lea	guip_dbzform(pc),a0
	beq.b	.ok
	lea	guip_dbpform(pc),a0
.ok	move.l	a0,d0
	rts

; dB slider feliratok elojelfuggo formazasa

gui_tbformhook	dc.l	0,0,.entry,0,0

.entry	tst.l	d0
	lea	guip_dbmform(pc),a0
	bmi.b	.ok
	lea	guip_dbzform(pc),a0
	beq.b	.ok
	lea	guip_dbpform(pc),a0
.ok	move.l	a0,d0
	rts

; 'Options/General/GUI update delay'

gui_guiqhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1),d0
	move.w	d0,conftmp+conf_guiquantum(a5)
	mulu.w	#1000,d0
	move.l	d0,updatequantum(a5)
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

; 'Options/Sampler/Histogram limit'

gui_levdivhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1),d0
	move.b	d0,conftmp+conf_levdiv(a5)
	lea	.table(pc),a0
	move.b	(a0,d0.w),sam_levdiv(a5)
	movem.l	(sp)+,d2-d7/a2-a6
	rts

.table	dc.b	0,6,4,3

	even


;----------< VSS command shell >-------------------------------------------------

	STRUCTURE	histnode,MLN_SIZE
	LABEL	hn_size
	;command line

shell_length	equ	80	;max line length
shell_histlines	equ	100	;max lines
shell_coml	equ	20	;max command name length

; In A0:hook A1:message A2:object

shellhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	clr.l	shellhistnext(a5)
	move.l	(a1),a2
	tst.b	(a2)
	beq.w	.nocomm
	move.l	a2,a0
	moveq	#1,d2	;karakterek szamlalasa
.0	addq.l	#1,d2
	tst.b	(a0)+
	bne.b	.0
	move.l	d2,shellcs+CS_Length(a5)
	move.l	d2,d0
	add.w	#hn_size,d0
	move.l	pool1(a5),a0	;memoria foglalas
	move.l	excbase(a5),a6
	bsr.l	AllocVecPooled
	move.l	d0,a3
	tst.l	d0
	beq.w	.nocomm
	move.l	a2,a0
	lea	hn_size(a3),a1	;history-ba masolas
	move.l	a1,shellcs+CS_Buffer(a5)
.3	move.b	(a0)+,(a1)+
	bne.b	.3
	clr.b	(a1)
	move.b	#10,-1(a1)	;ReadArgs() bug miatt
	lea	shellhist(a5),a0	;listaba fuzes
	move.l	a3,a1
	jsr	_LVOAddTail(a6)
	move.w	shellhistlines(a5),d0
	addq.w	#1,d0
	cmp.w	#shell_histlines,d0	;elertuk a max sorszamot?
	bne.b	.1
	lea	shellhist(a5),a0	;igen, elso eltavolitasa
	jsr	_LVORemHead(a6)
	move.l	d0,a1
	move.l	pool1(a5),a0
	bsr.l	FreeVecPooled
	bra.b	.2
.1	move.w	d0,shellhistlines(a5)
.2	clr.l	shellcs+CS_CurChr(a5)

	lea	-shell_coml(sp),sp
	move.l	dosbase(a5),a6	;parse command
	move.l	sp,d1
	moveq	#shell_coml-1,d2
	lea	shellcs(a5),a0
	move.l	a0,d3
	jsr	_LVOReadItem(a6)
	subq.l	#ITEM_UNQUOTED,d0
	seq	d3
	bne.b	.bad
;	move.l	sp,a0	;vegere 0-t teszunk
;.7	move.b	(a0)+,d0
;	beq	.11
;	cmp.b	#10,d0	;LF
;	beq	.11
;	cmp.b	#13,d0	;CR
;	beq	.11
;	cmp.b	#32,d0	;spc
;	bne	.7
;.11	clr.b	-1(a0)
	lea	sh.comtable(pc),a1	;kereses a tablaban
	clr.l	d4
.nextcom	addq.l	#8,d4	;kovetkezo parancs
	move.l	sp,a0
	tst.b	(a1)
	beq.b	.bad	;vege, nem talaltuk meg
	sf	d2	;'*' flag
.nextchar	move.b	(a1)+,d1
	cmp.b	#'*',d1
	bne.b	.6
	st	d2
	bra.b	.nextchar
.6	bclr	#5,d1
	move.b	(a0)+,d0	;kovetkezo karakter
	bclr	#5,d0
	cmp.b	d0,d1
	bne.b	.diff	;nem egyezik
	tst.b	d0
	beq.b	.ok	;tovabb
	bra.b	.nextchar	;mind a ketto 0, megvan!
.diff	tst.b	d0	;inputnak nincs vege,
	bne.b	.nextc	;kovetkezo
	tst.b	d2	;rovidites?
	beq.b	.nextc	;nem, kovetkezo
	bra.b	.ok
.nextc	tst.b	(a1)+	;kovetkezo parancs
	bne.b	.nextc
	bra.b	.nextcom
.bad	clr.l	d4
.ok	lea	shell_coml(sp),sp
	tst.b	d3	;ures sor, ki sem irjuk
	beq.w	.hib	;historybol toroljuk
	lea	t_shellform(pc),a0	;sor kiirasa
	lea	shellcs+CS_Buffer(a5),a1
	bsr.w	printLine
	tst.l	d4	;megvan?
	bne.b	.parse
	lea	sht_err(pc),a0	;hiba kiirasa
	bsr.w	printLine
	bra.w	.nocomm	;historybol nem toroljuk
.parse	lea	sh.table(pc),a0	;parse args
	move.l	(a0,d4.w),d5
	bne.b	.arg
	lea	sht_none(pc),a1
	move.l	a1,d5
.arg	move.l	#DOS_RDARGS,d1
	clr.l	d2
	jsr	_LVOAllocDosObject(a6)
	move.l	d0,d3
	beq.w	.noarg
	move.l	d3,a1
	move.l	#RDAF_NOPROMPT,RDA_Flags(a1)
	clr.l	RDA_DAList(a1)
	clr.l	RDA_Buffer(a1)
	move.l	shellcs+CS_CurChr(a5),d0
	move.l	shellcs+CS_Length(a5),d1
	sub.l	d0,d1
	move.l	d1,RDA_Source+CS_Length(a1)
	move.l	shellcs+CS_Buffer(a5),d1
	add.l	d0,d1
	move.l	d1,RDA_Source+CS_Buffer(a1)
	clr.l	RDA_Source+CS_CurChr(a1)
	lea	sharg+sha_help(a5),a0
	move.l	a0,d2
	moveq	#shellargsize/4,d0
.10	clr.l	(a0)+
	dbf	d0,.10
	move.l	d5,d1
	jsr	_LVOReadArgs(a6)
	move.l	d0,d2
	beq.b	.bad1
	tst.l	sharg+sha_help(a5)	;kerdojel volt?
	beq.b	.nohelp
	move.l	d5,a0
	tst.b	3(a0)	;ures template?
	bne.b	.help
.nopar	lea	sht_chelpnopar(pc),a0
	bra.b	.b1
.help	lea	sht_chelp(pc),a0	;? help
	bra.b	.b1
.bad1	move.l	d5,a1	;hiba, bad template
	tst.b	3(a1)
	beq.b	.nopar
	lea	sht_badpar(pc),a0
.b1	addq.l	#4,d5
	move.l	d5,-(sp)
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#4,sp
	bra.b	.freearg
.nohelp	bsr.w	sh.call
.freearg	move.l	dosbase(a5),a6
	move.l	d2,d1
	jsr	_LVOFreeArgs(a6)
	move.l	#DOS_RDARGS,d1
	move.l	d3,d2
	jsr	_LVOFreeDosObject(a6)
.noarg	tst.b	shellchanged(a5)	;ha nem valtozott, utolso
	beq.b	.nocomm	;history line torlese
.hib	move.l	excbase(a5),a6
	lea	shellhist(a5),a0
	jsr	_LVORemTail(a6)
	move.l	d0,a1
	move.l	pool1(a5),a0
	bsr.l	FreeVecPooled
	subq.w	#1,shellhistlines(a5)
.nocomm

 seti gui_window(a5),#MUIA_Window_ActiveObject,gad_shellinput(a5)
 seti gad_shellinput(a5),#MUIA_String_Contents,#nothing
	movem.l	(sp)+,d2-d7/a2-a6
	rts

	txtstart

sh.comtable	dc.b	'h*elp',0
	dc.b	'q*uit',0
	dc.b	'hi*de',0
	dc.b	'w*riteprefs',0
	dc.b	'r*eadprefs',0
	dc.b	'addr*ess',0
	dc.b	'con*trol',0
	dc.b	'ser*ial',0
	dc.b	'mui*prefs',0
	dc.b	'cs',0
	dc.b	'sta',0
	dc.b	'mem*test',0
	dc.b	'mems*peed',0
	dc.b	'fl*ags',0
	dc.b	'o*rg',0
	dc.b	'ha*ndles',0
	dc.b	'no*tify',0
	dc.b	'i*conify',0
	dc.b	'il*evel',0
	dc.b	'clrb*uf',0
	dc.b	'sa*vebuf',0
	dc.b	'deb*uglevel',0
	dc.b	'de*emp',0
	dc.b	'lib*en',0
	dc.b	'mix*er',0
	dc.b	'dac*on',0
	dc.b	'adc*on',0
	dc.b	'br',0
	dc.b	'pac*kets',0
	dc.b	'chu*nk',0
	dc.b	'maxc*hunk',0
	dc.b	'fak*ecnt',0
	dc.b	'ir*init',0
	dc.b	'irs*top',0
	dc.b	0

	txtend
	even

sh.table	dc.l	0,sh_err	;error
	dc.l	0,sh_help	;help
	dc.l	0,sh_quit	;quit
	dc.l	0,hide	;hide
	dc.l	0,sh_write	;writeprefs
	dc.l	0,0	;readprefs
	dc.l	sht_addrarg,sh_addr	;address
	dc.l	sht_conarg,sh_con	;control
	dc.l	sht_serarg,sh_ser	;serial
	dc.l	0,sh_muip	;muiprefs
	dc.l	sht_csarg,sh_cs	;cs
	dc.l	sht_starg,sh_sta	;sta
	dc.l	0,sh_memtest	;test memory
	dc.l	0,sh_memspeed	;memory speed
	dc.l	sht_flag,sh_flags	;flag
	dc.l	0,sh_org	;org
	dc.l	0,sh_handles
	dc.l	0,sh_notify
	dc.l	0,sh_icon
	dc.l	sht_level,sh_level
	dc.l	0,sh_clrbuf
	dc.l	sht_saveb,sh_savebuf
	dc.l	sht_debug,sh_debug
	dc.l	sht_deemp,sh_deemp
	dc.l	sht_liben,sh_liben
	dc.l	sht_mixer,sh_mixer
	dc.l	sht_conarg,sh_dacon
	dc.l	sht_conarg,sh_adcon
	dc.l	0,sh_br
	dc.l	sht_pack,sh_packets
	dc.l	sht_maxchunk,sh_chunk
	dc.l	sht_maxchunk,sh_maxchunk
	dc.l	sht_fakecnt,sh_fakecnt
	dc.l	0,sh_irinit
	dc.l	0,sh_irstop

sh.call	lea	sh.table+4(pc),a0
	move.l	(a0,d4.w),d0
	movem.l	d2/d3/d5,-(sp)
	bne.b	.do
	lea	sht_notimpl(pc),a0
	bsr.w	printLine
	bra.b	.do1
.do	move.l	d0,a0
	jsr	(a0)
.do1	movem.l	(sp)+,d2/d3/d5
	rts

sh_iiccerr	lea	e_iicc,a0
	bra.w	printFault
sh_iicderr	lea	e_iicd,a0
	bra.w	printFault

sh_illq	lea	sht_illval(pc),a0
	bra.b	sh_pi
sh_ok	lea	sht_ok(pc),a0
	bra.b	sh_pi
sh_file	lea	sht_file(pc),a0
	bra.b	sh_pi
sh_badtempl	lea	sht_badtempl(pc),a0
	bra.b	sh_pi
sh_parsehex	lea	sht_parsehex(pc),a0
	bra.b	sh_pi
sh_err	lea	sht_err(pc),a0
	bra.b	sh_pi
sh_help	lea	sht_help(pc),a0
sh_pi	bra.w	printLine

sh_quit
 DoMethod gui_app(a5),#MUIM_Application_ReturnID,#MUIV_Application_ReturnID_Quit
	rts
sh_muip
 DoMethod gui_app(a5),#MUIM_Application_OpenConfigWindow,#0
 	rts

	txtstart

sht_none	dc.b	'?/S',0,0
sht_csarg	dc.b	'?/S,ADDRESS,DATA',0
sht_starg	dc.b	'?/S,ADDRESS/A,DATA',0
sht_addrarg	dc.b	'?/S,ADDRESS,F=FORCE/S',0
sht_conarg	dc.b	'?/S,CONREG',0
sht_serarg	dc.b	'?/S,SERREG',0
sht_flag	dc.b	'?/S,+/S,-/S,FLAGS',0
sht_saveb	dc.b	'?/S,FILE/A',0
sht_debug	dc.b	'?/S,DEBUGLEVEL/N',0
sht_level	dc.b	'?/S,C=CLEAR/S',0
sht_deemp	dc.b	'?/S,FREQ/N,D=DISABLE/S',0
sht_liben	dc.b	'?/S,E=ENABLE/S,D=DISABLE/S',0
sht_mixer	dc.b	'?/S,ADDRESS,DATA',0
sht_pack	dc.b	'?/S,PACKET/N,L=LIST/S',0
sht_maxchunk	dc.b	'?/S,BYTES/N',0
sht_fakecnt	dc.b	'?/S,SAMPLES/N',0

t_shellform	dc.b	$1b,'b>%s',0

sht_maxc	dc.b	'MPEGA Max chunk size is %ld bytes',0
sht_chunk	dc.b	'MPEGA Chunk size is %ld bytes',0
sht_fake	dc.b	'MPEGA_decode_frame() will return %ld samples',0
sht_pac	dc.b	'%ld/%ld free packets',0
sht_packmon	dc.b	'%2.2ld: %ls %ls',0
shtp_free	dc.b	'FREE',0
shtp_used	dc.b	'USED',0
sht_con	dc.b	'CON: %02.2lx  CON2: %02.2lx',0
sht_ser	dc.b	'SER: %02.2lx',0
sht_iicc	dc.b	'%02.2lx: %02.2lx',0
sht_addr	dc.b	'Board address: $%08.8lx',0
sht_addrn	dc.b	'Unknown, please set!',0
sht_csform	dc.b	'00: %02.2lx %02.2lx %02.2lx %02.2lx '
	dc.b	'%02.2lx %02.2lx %02.2lx',0
sht_memerr	dc.b	'Failed at $%04.4lx',0
sht_32k	dc.b	'32k O.K.',0
sht_flags	dc.b	'Flags: $%08.8lx',0
sht_org	dc.b	'Code: $%08.8lx  Data: $%08.8lx',0
sht_handhead	dc.b	'Handle             Task',0
sht_hand	dc.b	'%-16.16s   %s',0
sht_empty	dc.b	'« empty list »',0
sht_deb	dc.b	'Debuglevel %ld (%ls)',0
sht_levelf	dc.b	'Left: %ld, Right: %ld',0
sht_deemp1	dc.b	'De-emphasis: %ldHz',0
sht_deemp2	dc.b	'De-emphasis disabled',0
sht_deemp3	dc.b	'De-emphasis: $%02.2lx',0
sht_deempfr	dc.b	'Only 32kHz, 44.1kHz and 48kHz supported',0
sht_deempwar	dc.b	'Warning: not functional in CS4222 rev.A',0
sht_libe	dc.b	'Libraries enabled',0
sht_libd	dc.b	'Libraries disabled',0
sht_mixt	dc.b	'Name      LR LLev RLev LR',10
	dc.b	'Digital:  %2.2ls %3.3ld  %3.3ld  %2.2ls',10
	dc.b	'Chn.1:    %2.2ls %3.3ld  %3.3ld  %2.2ls',10
	dc.b	'Chn.2:    %2.2ls %3.3ld  %3.3ld  %2.2ls',10
	dc.b	'Chn.3:    %2.2ls %3.3ld  %3.3ld  %2.2ls',10
	dc.b	'Chn.4:    %2.2ls %3.3ld  %3.3ld  %2.2ls',0
sht_mix11	dc.b	'++',0
sht_mix10	dc.b	'+-',0
sht_mix01	dc.b	'-+',0
sht_mix00	dc.b	'--',0
sht_adconf	dc.b	'ADCON: %02.2lx',0
sht_daconf	dc.b	'DACON: %02.2lx',0
sht_bra	dc.b	'Average bitrate: %ldkbps',0
sht_speed	dc.b	'Read: %ld, Write: %ld',0

sht_err	dc.b	'Unrecognized command. Try help.',0
sht_help	dc.b	'Available commands:',10
	dc.b	'Help, Quit, HIde, ADDRess, MUIprefs, CS, STA,',10
	dc.b	'CONtrol, SERial, MEMtest, MEMSpeed, FLags, Org,',10
	dc.b	'HAndles, NOtify, Iconify, SAvebuf, CLRBuf,',10
	dc.b	'DEBuglevel, ILevel, DEemp, LIBen, MIXer, DACon,',10
	dc.b	'ADCon, BR, PACkets, CHUnk, MAXChunk, FAKecnt',10
	dc.b	'IRinit, IRStop, Writeprefs',10
	dc.b	'For a detailed command description,',10
	dc.b	'please read the driver documentations',0
sht_chelp	dc.b	'Parameters: %s',0
sht_chelpnopar	dc.b	'No parameters required',0
sht_file	dc.b	'File error',0
sht_ok	dc.b	'OK',0
sht_badpar	dc.b	'Parameters unsuitable for ''%ls''',0
sht_parsehex	dc.b	'Not a hexadecimal number',0
sht_notimpl	dc.b	'Command not implemented yet',0
sht_badtempl	dc.b	'Bad parameters',0
sht_illval	dc.b	'Illegal quantity',0
sht_startdate	dc.b	10,10,'Buffer saved on %s %s',10,10,0

pktn_Kill	dc.b	'Kill',0
pktn_PrintFault	dc.b	'PrintFault',0
pktn_PrintInfo	dc.b	'PrintInfo',0
pktn_PrintDebug	dc.b	'PrintDebug',0
pktn_OwnCard	dc.b	'OwnCard',0
pktn_DisownCard	dc.b	'DisownCard',0
pktn_InitCard	dc.b	'InitCard',0
pktn_SetFreq	dc.b	'SetFreq',0
pktn_RefreshMixer
	dc.b	'RefreshMixer',0
pktn_ReadCodecRegs
	dc.b	'ReadCodecRegs',0
pktn_IICRead	dc.b	'IICREad',0
pktn_IICWrite	dc.b	'IICWrite',0
pktn_Delay	dc.b	'Delay',0
pktn_LastSupported
	dc.b	'« NOP »',0
pktn_StartSlave	dc.b	'StartSlave',0
pktn_StopSlave	dc.b	'StopSlave',0
pktn_Play	dc.b	'Play',0
pktn_Sample	dc.b	'Sample',0
pktn_Stop	dc.b	'Stop',0
pktn_Pause	dc.b	'Pause',0
pktn_Seek	dc.b	'Seek',0
pktn_PlayEOF	dc.b	'PlayEOF',0

	txtend
	even

; Set/show board address

sh_addr	move.l	sharg+sha_arg(a5),d0
	beq.b	.query
	move.l	d0,a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	tst.l	sharg+sha_arg+4(a5)
	beq.b	.0
	move.l	d0,a0
	clr.b	_con(a0)	;reset CON
.0	move.l	d0,d2
	move.l	vssbase(a5),a6
	move.l	main_handle(a5),a0
	jsr	_LVOvssDisownCard(a6)
	lea	e_busy,a0
	tst.l	d0
	beq.w	sh_pi
	move.l	d2,d0
	move.l	main_handle(a5),a0
	jmp	_LVOvssOwnCard(a6)
.query	move.l	address(a5),d0
	bne.b	.1
	lea	sht_addrn(pc),a0
	bra.w	sh_pi
.1	move.l	d0,-(sp)
	move.l	sp,a1
	lea	sht_addr(pc),a0
	bsr.w	printLine
	addq.l	#4,sp
	rts

; Read/write codec registers

sh_cs	move.l	sharg+sha_arg(a5),d0
	beq.b	.dispall
	move.l	d0,a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	move.l	d0,d2
	move.l	sharg+sha_arg+4(a5),d1
	beq.b	.disp
	move.l	d1,a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	and.w	#$ff,d2
	or.w	#$2000,d2
	swap	d2
	asl.w	#8,d0
	or.l	d2,d0
	move.l	d0,d1
	moveq	#VSSpkt_IICWrite,d0
	move.l	lport(a5),a0
	move.l	main_handle(a5),a1
	move.l	vssh_port(a1),a1
	bsr.l	doPkt
	tst.l	d0
	bmi.w	sh_iiccerr
	rts
.disp	move.l	#$00ff2000,d1
	move.b	d0,d1
	swap	d1
	moveq	#VSSpkt_IICRead,d0
	move.l	lport(a5),a0
	move.l	main_handle(a5),a1
	move.l	vssh_port(a1),a1
	bsr.l	doPkt
	tst.l	d0
	bmi.w	sh_iiccerr
	move.l	d0,-(sp)
	move.l	d2,-(sp)
	move.l	sp,a1
	lea	sht_iicc(pc),a0
	bsr.w	printLine
	addq.l	#8,sp
	rts
.dispall	tst.l	sharg+sha_arg+4(a5)
	bne.w	sh_badtempl
	lea	-7*4(sp),sp
	moveq	#VSSpkt_ReadCodecRegs,d0
	move.l	sp,d1
	move.l	lport(a5),a0
	move.l	main_handle(a5),a1
	move.l	vssh_port(a1),a1
	bsr.l	doPkt
	tst.l	d0
	bne.b	.ok
	lea	sht_csform(pc),a0
	move.l	sp,a1
	bsr.w	printLine
.ok	lea	7*4(sp),sp
	rts

; Read/write DSP registers

sh_sta	move.l	sharg+sha_arg(a5),a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	move.l	d0,d2
	move.l	sharg+sha_arg+4(a5),d1
	beq.b	.disp
	move.l	d1,a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	and.w	#$ff,d2
	or.w	#$8600,d2
	swap	d2
	asl.w	#8,d0
	or.l	d2,d0
	move.l	d0,d1
	moveq	#VSSpkt_IICWrite,d0
	move.l	lport(a5),a0
	move.l	main_handle(a5),a1
	move.l	vssh_port(a1),a1
	bsr.l	doPkt
	tst.l	d0
	bmi.w	sh_iicderr
	rts
.disp	move.l	#$00ff8600,d1
	move.b	d0,d1
	swap	d1
	moveq	#VSSpkt_IICRead,d0
	move.l	lport(a5),a0
	move.l	main_handle(a5),a1
	move.l	vssh_port(a1),a1
	bsr.l	doPkt
	tst.l	d0
	bmi.w	sh_iicderr
	move.l	d0,-(sp)
	move.l	d2,-(sp)
	move.l	sp,a1
	lea	sht_iicc(pc),a0
	bsr.w	printLine
	addq.l	#8,sp
	rts

; Set/show control register

sh_con	move.l	sharg+sha_arg(a5),d0
	beq.b	.disp
	move.l	d0,a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	move.b	d0,d1
	and.b	#$20,d1	;INTENA-t nem engedjuk
	beq.b	.0	;bekapcsolni, ha con2-ben
	and.b	con2(a5),d1	;nincs bekapcsolva
	bne.b	.0	;(kulonben unexpected
	and.b	#~$20,d0	;interrupt -> fagyas)
.0	move.l	address(a5),a0
	move.b	d0,_con(a0)
	rts
.disp	clr.l	d0
	move.b	con2(a5),d0
	move.l	d0,-(sp)
	move.b	con(a5),d0
	move.l	d0,-(sp)
	move.l	sp,a1
	lea	sht_con(pc),a0
	bsr.w	printLine
	addq.l	#8,sp
	rts

; Set/show serial control register

sh_ser	move.l	sharg+sha_arg(a5),d0
	beq.b	.disp
	move.l	d0,a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	move.l	address(a5),a0
	move.b	d0,_ser(a0)
	move.b	d0,ser(a5)
	rts
.disp	clr.l	d0
	move.b	ser(a5),d0
	move.l	d0,-(sp)
	move.l	sp,a1
	lea	sht_ser(pc),a0
	bsr.w	printLine
	addq.l	#4,sp
	rts

; write config file

sh_write	bra.l	writeconf


; test memory

sh_memtest	move.l	vssbase(a5),a6
	move.l	address(a5),a4
	move.l	main_handle(a5),a0
	jsr	_LVOvssDisownCard(a6)
	lea	e_busy,a0
	tst.l	d0
	beq.w	sh_pi
	clr.b	_con(a4)
	move.b	#%10000000,_con(a4)
	lea	memtestdata,a1	;fill with pattern
	move.w	#$7fff,d0
.1	move.b	(a1)+,(a4)
	dbf	d0,.1
	clr.l	d2
	lea	memtestdata,a1	;check
	move.w	#$7fff,d0
.2	move.b	(a1)+,d1
	cmp.b	(a4),d1
	bne.b	.6
	addq.l	#1,d2
	dbf	d0,.2
	lea	sht_32k(pc),a0
	bra.b	.7
.6	lea	sht_memerr(pc),a0
	bra.w	.7
.7	move.l	d2,-(sp)
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#4,sp
	move.l	a4,d0
	move.l	main_handle(a5),a0
	jmp	_LVOvssOwnCard(a6)

; measure memory speed

speedcount	equ	50000

sh_memspeed	move.l	vssbase(a5),a6
	move.l	address(a5),a4
	move.l	main_handle(a5),a0
	jsr	_LVOvssDisownCard(a6)
	lea	e_busy,a0
	tst.l	d0
	beq.w	sh_pi
	clr.b	_con(a4)
	move.b	#%10000000,_con(a4)
	lea	-3*EV_SIZE(sp),sp
	move.l	excbase(a5),a6
	jsr	_LVOForbid(a6)
	move.l	#speedcount,d0	;wait for last interrupt
.2	nop
	dbf	d0,.2
	move.l	timbase(a5),a6	;iras meres
	move.l	sp,a0
	jsr	_LVOReadEClock(a6)
	move.l	#speedcount,d0
.0	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	clr.b	(a4)
	dbf	d0,.0
	lea	EV_SIZE(sp),a0	;olvasas meres
	jsr	_LVOReadEClock(a6)
	move.l	#speedcount,d0
.1	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	tst.b	(a4)
	dbf	d0,.1
	lea	2*EV_SIZE(sp),a0
	jsr	_LVOReadEClock(a6)
	move.l	excbase(a5),a6
	jsr	_LVOPermit(a6)
	move.l	4(sp),d1	;sebesseg szamitasa
	move.l	12(sp),d2
	move.l	20(sp),d3
	lea	3*EV_SIZE(sp),sp
	sub.l	d2,d3
	sub.l	d1,d2
	asl.l	#8,d0
	move.l	d0,d1
	divu.l	d2,d0
	divu.l	d3,d1
	movem.l	d0-d1,-(sp)	;kiiras
	lea	sht_speed(pc),a0
	move.l	sp,a1
	bsr	printLine
	addq.l	#8,sp
	move.l	vssbase(a5),a6	;kartya visszaadas
	move.l	a4,d0
	move.l	main_handle(a5),a0
	jmp	_LVOvssOwnCard(a6)


; Set/show flags

sh_flags	move.l	sharg+sha_arg+8(a5),d0	;flags
	beq.b	.disp
	move.l	d0,a0
	move.b	(a0),d0
	cmp.b	#'+',d0
	bne	.1
	tst.l	sharg+sha_arg(a5)	;+
	bne.w	sh_badtempl
	st	sharg+sha_arg(a5)	;+
	bra	.2
.1	cmp.b	#'-',d0
	bne	.0
	tst.l	sharg+sha_arg+4(a5)	;-
	bne.w	sh_badtempl
	st	sharg+sha_arg+4(a5)	;-
.2	addq.l	#1,a0
.0	bsr.w	parsehex
	beq.w	sh_parsehex
	move.l	conftmp+conf_flags(a5),d1
	tst.l	sharg+sha_arg(a5)	;+
	beq.b	.noor
	tst.l	sharg+sha_arg+4(a5)	;-
	bne.w	sh_badtempl
	or.l	d1,d0
	bra.b	.store
.noor	tst.l	sharg+sha_arg+4(a5)	;-
	beq.b	.store
	not.l	d0
	and.l	d1,d0
.store	move.l	d0,conftmp+conf_flags(a5)
	bsr	checkboxupd
.disp	move.l	conftmp+conf_flags(a5),d0
	move.l	d0,-(sp)
	move.l	sp,a1
	lea	sht_flags(pc),a0
	bsr.w	printLine
	addq.l	#4,sp
	rts

; Org

sh_org	lea	Main,a0
	move.l	a5,-(sp)
	move.l	a0,-(sp)
	lea	sht_org(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#8,sp
	rts

; Show handles

sh_handles	move.l	excbase(a5),a6
	lea	handsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	lea	handlist(a5),a2
	move.l	(a2),a2
	tst.l	(a2)
	bne.b	.1
	lea	sht_empty(pc),a0
	bsr.w	printLine
	bra.b	.ok
.1	lea	sht_handhead(pc),a0
	bsr.w	printLine
.0	move.l	(a2),d2
	beq.b	.ok
	move.l	vssh_task(a2),a0
	move.l	LN_NAME(a0),-(sp)
	move.l	LN_NAME(a2),-(sp)
	lea	sht_hand(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#8,sp
	move.l	d2,a2
	bra.b	.0
.ok	lea	handsem(a5),a0
	jmp	_LVOReleaseSemaphore(a6)

; Show mixer notify requests

sh_notify	move.l	excbase(a5),a6
	lea	notifysem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	lea	notifylist(a5),a2
	move.l	(a2),a2
	tst.l	(a2)
	bne.b	.1
	lea	sht_empty(pc),a0
	bsr.w	printLine
	bra.b	.ok
.1	lea	sht_handhead(pc),a0
	bsr.w	printLine
.0	move.l	(a2),d2
	beq.b	.ok
	move.l	vssn_sigtask(a2),a0
	move.l	LN_NAME(a0),-(sp)
	move.l	vssn_handle(a2),a0
	move.l	LN_NAME(a0),-(sp)
	lea	sht_hand(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#8,sp
	move.l	d2,a2
	bra.b	.0
.ok	lea	notifysem(a5),a0
	jmp	_LVOReleaseSemaphore(a6)

; Iconify

sh_icon	seti	gui_app(a5),#MUIA_Application_Iconified,#TRUE
	rts

; Clear shell buffer

sh_clrbuf	DoMethod	gad_shellist(a5),#MUIM_List_Clear
	clr.w	gui_shlines(a5)
	bra.w	sh_ok

; Save shell buffer

sh_savebuf	move.l	dosbase(a5),a6
	move.l	sharg+sha_arg(a5),d1
	move.l	#MODE_READWRITE,d2
	jsr	_LVOOpen(a6)
	move.l	d0,d7
	beq.w	.file
	move.l	d0,d1	;file vegere
	clr.l	d2
	moveq	#OFFSET_END,d3
	jsr	_LVOSeek(a6)
	; write separator, date etc...
	lea	-dat_SIZEOF-2*datelength(sp),sp
	move.l	sp,a2
	move.b	#FORMAT_INT,dat_Format(a2)
	clr.b	dat_Flags(a2)
	clr.l	dat_StrDay(a2)
	lea	dat_SIZEOF+datelength(a2),a0
	move.l	a0,dat_StrTime(a2)
	move.l	a0,-(sp)
	lea	-datelength(a0),a0
	move.l	a0,dat_StrDate(a2)
	move.l	a0,-(sp)
	move.l	a2,d1
	jsr	_LVODateStamp(a6)
	move.l	a2,d1
	jsr	_LVODateToStr(a6)
	tst.l	d0
	beq	.nostr
	move.l	d7,d1	;write
	lea	sht_startdate(pc),a0
	move.l	a0,d2
	move.l	sp,d3
	jsr	_LVOVFPrintf(a6)
.nostr	lea	dat_SIZEOF+2*datelength+8(sp),sp
	tst.l	d0
	bmi	.file
	clr.l	d6
	clr.l	-(sp)
	move.l	sp,a4
.loop	DoMethod	gad_shellist(a5),#MUIM_List_GetEntry,d6,a4
	addq.l	#1,d6
	move.l	dosbase(a5),a6
	move.l	d7,d1
	move.l	(sp),d2
	beq.b	.end
	jsr	_LVOFPuts(a6)
	tst.l	d0
	bne.b	.en
	move.l	d7,d1
	moveq	#10,d2
	jsr	_LVOFPutC(a6)
	cmp.b	#10,d0
	beq.b	.loop
.en	bsr.w	doserror
.end	move.l	d7,d1
	jsr	_LVOClose(a6)
	addq.l	#4,sp
	tst.l	d2
	bne.w	sh_file
	bra.w	sh_ok
.file	bsr.w	doserror
	bra.w	sh_file

; Set/show debuglevel

sh_debug	move.l	sharg+sha_arg(a5),d0
	beq.b	.disp
	move.l	d0,a0
	move.l	(a0),d3
	cmp.b	#debug_max,d3
	bhi.w	sh_illq
	move.b	d3,debuglevel(a5)
	seti	gad_debugl(a5),#MUIA_Cycle_Active,d3
	;rts
.disp	clr.l	d0
	move.b	debuglevel(a5),d0
	lea	gui_cycentries(pc),a0
	move.l	(a0,d0.w*4),-(sp)
	move.l	d0,-(sp)
	move.l	sp,a1
	lea	sht_deb(pc),a0
	bsr.w	printLine
	addq.l	#8,sp
	rts

; Show input levels

sh_level	moveq	#VSSpkt_IICRead,d0
	move.l	#$200600ff,d1
	move.l	lport(a5),a0
	move.l	main_handle(a5),a1
	move.l	vssh_port(a1),a1
	bsr.l	doPkt
	tst.l	d0
	bmi.w	sh_iiccerr
	clr.l	d1
	move.b	d0,d1
	lsr.b	#3,d1
	and.b	#7,d0
	and.b	#7,d1
	movem.l	d0-d1,-(sp)
	lea	sht_levelf(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#8,sp
	tst.l	sharg+sha_arg(a5)	;clear?
	beq.b	.0
	moveq	#VSSpkt_IICRead,d0
	move.l	#$200500ff,d1
	move.l	lport(a5),a0
	move.l	main_handle(a5),a1
	move.l	vssh_port(a1),a1
	bsr.l	doPkt
	tst.l	d0
	bmi.w	sh_iiccerr
.0	rts

; Set/show de-emphasis

sh_deemp	move.l	#$20050089,d1
	move.l	sharg+sha_arg+4(a5),d0
	bne.b	.0	;disable
	move.l	sharg+sha_arg(a5),d0
	beq.b	.disp
	move.l	d0,a0
	move.l	(a0),d0
	lea	freqtab,a0
.3	move.l	(a0)+,d2
	beq.b	.badfreq
	cmp.l	d0,d2
	bne.b	.3
	move.l	deemptab-freqtab-4(a0),d0
	move.b	d0,d1
	move.l	d1,-(sp)
	move.l	conftmp+conf_flags(a5),d0	;deemphasis flag?
	btst	#cflg_deemphasis,d0
	bne.b	.4
	lea	sht_deempwar(pc),a0
	bsr.w	printLine
.4	move.l	(sp)+,d1
.0	move.b	d1,deemp(a5)	;do it
	asl.w	#8,d1
	moveq	#VSSpkt_IICWrite,d0
	move.l	lport(a5),a0
	move.l	main_handle(a5),a1
	move.l	vssh_port(a1),a1
	bsr.l	doPkt
	tst.l	d0
	bmi.w	sh_iiccerr
	rts
.badfreq	lea	sht_deempfr(pc),a0
	bra.w	printLine
.disp	clr.l	d0
	move.b	deemp(a5),d0
	lsr.b	#3,d0
	and.b	#%00011100,d0
	lea	sht_deemp1(pc),a0
	move.l	.tab(pc,d0.w),d0
	bmi.b	.2
	bne.b	.1
	lea	sht_deemp2(pc),a0	;disabled
	bra.b	.1
.2	clr.l	d0	;$..
	move.b	deemp(a5),d0
	lea	sht_deemp3(pc),a0
.1	move.l	d0,-(sp)	;...Hz
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#4,sp
	rts

.tab	dc.l	-1,44100,48000,32000,0,-1,-1,-1

; Enable/disable/query library openings

sh_liben	tst.l	sharg+sha_arg(a5)
	bne.b	.en
	tst.l	sharg+sha_arg+4(a5)
	beq.b	.disp
	bclr	#flg_enable,flag(a5)
	bra.b	.disp
.en	tst.l	sharg+sha_arg+4(a5)
	bne.w	sh_badtempl
	bset	#flg_enable,flag(a5)
.disp	lea	sht_libe(pc),a0
	btst	#flg_enable,flag(a5)
	bne.b	.0
	lea	sht_libd(pc),a0
.0	bra.w	printLine

; Set/show mixer table

sh_mixer	move.l	sharg+sha_arg(a5),d0
	beq.b	.disp
	move.l	d0,a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	lea	sht_illval(pc),a0
	cmp.l	#VMT_SIZE,d0
	bcc.w	printLine
	move.l	d0,d2
	lea	(a5,d0.w,mixtab),a2
	move.l	sharg+sha_arg+4(a5),d0
	beq.b	.dispreg
	move.l	d0,a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	move.b	d0,(a2)	;uj ertek beiras
	move.l	vssbase(a5),a6	;mixer frissites
	move.l	main_handle(a5),a0
	;clr.l	d0
	st	d0
	jmp	_LVOvssRefreshMixer(a6)
.dispreg	move.b	(a2),d0	;regiszter lekerdezes
	move.l	d0,-(sp)
	move.l	d2,-(sp)
	lea	sht_iicc(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#8,sp
	rts
.spc	equ	20
.disp	moveq	#.spc-1,d0	;egesz tabla lekerdezese
.0	clr.l	-(sp)
	dbf	d0,.0
	lea	mixtab(a5),a2
	move.l	sp,a3
	lea	sht_mix00(pc),a0	;digital
	move.w	VMT_switch2(a2),d0
	btst	#0,d0
	beq.b	.1
	lea	sht_mix10(pc),a0
.1	move.l	a0,(a3)
	lea	sht_mix00(pc),a0
	move.w	VMT_switch2(a2),d0
	btst	#1,d0
	beq.b	.2
	lea	sht_mix01(pc),a0
.2	move.l	a0,12(a3)
	clr.l	d0
	move.b	VMT_4l(a2),d0
	move.l	d0,4(a3)
	move.b	VMT_4r(a2),d0
	move.l	d0,8(a3)
	move.w	VMT_switch1(a2),d1	;Chn.1 - Chn.4
	clr.l	d2
.chnloop	lea	16(a3),a3
	bsr.b	.chnbits
	move.l	a0,(a3)
	bsr.b	.chnbits
	move.l	a0,12(a3)
	clr.l	d0
	move.b	(a2,d2.w,VMT_0l),d0
	move.l	d0,4(a3)
	move.b	(a2,d2.w,VMT_0r),d0
	move.l	d0,8(a3)
	addq.w	#2,d2
	cmp.b	#8,d2
	bne.b	.chnloop
	lea	sht_mixt(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	lea	.spc*4(sp),sp
	rts
.chnbits	lea	sht_mix00(pc),a0	;bit -> +- konverzio
	move.b	d1,d0
	and.b	#3,d0
	beq.b	.3
	lea	sht_mix10(pc),a0
	subq.b	#1,d0
	beq.b	.3
	lea	sht_mix01(pc),a0
	subq.b	#1,d0
	beq.b	.3
	lea	sht_mix11(pc),a0
.3	lsr.w	#2,d1
	rts

; Set/show ADC control byte

sh_adcon	move.l	sharg+sha_arg(a5),d0
	beq.b	.disp
	move.l	d0,a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	move.b	d0,conftmp+conf_adcon(a5)
	rts
.disp	clr.l	d0
	move.b	conftmp+conf_adcon(a5),d0
	move.l	d0,-(sp)
	lea	sht_adconf(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#4,sp
	rts

; Set/show DAC control byte

sh_dacon	move.l	sharg+sha_arg(a5),d0
	beq.b	.disp
	move.l	d0,a0
	bsr.w	parsehex
	beq.w	sh_parsehex
	move.b	d0,conftmp+conf_dacon(a5)
	rts
.disp	clr.l	d0
	move.b	conftmp+conf_dacon(a5),d0
	move.l	d0,-(sp)
	lea	sht_daconf(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#4,sp
	rts

; Show average bitrate

sh_br	moveq	#VSSpkt_IICRead,d0
	move.l	#$866affff,d1
	move.l	lport(a5),a0
	move.l	main_handle(a5),a1
	move.l	vssh_port(a1),a1
	bsr.l	doPkt
	asl.l	#1,d0
	bmi.w	sh_iicderr
	move.l	d0,-(sp)
	lea	sht_bra(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#4,sp
	rts

; Packet monitor

sh_packets	tst.l	sharg+sha_arg+4(a5)
	bne.w	.list
	move.l	sharg+sha_arg(a5),d0
	beq.w	.free
	move.l	d0,a0
	move.l	(a0),d2
	cmp.l	#packetnum,d2
	bcc.w	sh_illq
	move.l	d2,d0
	mulu.w	#pkt_size,d0
	lea	(a5,d0.w,packets),a2
.dispac	lea	shtp_free(pc),a0
	tst.b	pkt_used(a2)
	beq.b	.2
	lea	shtp_used(pc),a0
.2	move.l	pkt_cmd(a2),d0
	lea	.type(pc),a1
	move.l	(a1,d0.w*4),-(sp)
	move.l	a0,-(sp)
	move.l	d2,-(sp)
	lea	sht_packmon(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	lea	12(sp),sp
	rts
.type	dc.l	pktn_Kill
	dc.l	pktn_PrintFault
	dc.l	pktn_PrintInfo
	dc.l	pktn_PrintDebug
	dc.l	pktn_OwnCard
	dc.l	pktn_DisownCard
	dc.l	pktn_InitCard
	dc.l	pktn_SetFreq
	dc.l	pktn_RefreshMixer
	dc.l	pktn_ReadCodecRegs
	dc.l	pktn_IICRead
	dc.l	pktn_IICWrite
	dc.l	pktn_Delay
	dc.l	pktn_LastSupported
	dc.l	pktn_StartSlave
	dc.l	pktn_StopSlave
	dc.l	pktn_Play
	dc.l	pktn_Sample
	dc.l	pktn_Stop
	dc.l	pktn_Pause
	dc.l	pktn_Seek
	dc.l	pktn_PlayEOF
.list	clr.l	d2
	lea	packets(a5),a2
.1	bsr.w	.dispac
	lea	pkt_size(a2),a2
	addq.l	#1,d2
	cmp.l	#packetnum,d2
	bne.b	.1
	rts
.free	move.l	excbase(a5),a6
	lea	pktsem(a5),a0
	jsr	_LVOObtainSemaphore(a6)
	lea	pktlist(a5),a0
	moveq	#-2,d2
.0	addq.l	#1,d2
	move.l	(a0),a0
	move.l	a0,d0
	bne.b	.0
	lea	pktsem(a5),a0
	jsr	_LVOReleaseSemaphore(a6)
	move.l	#packetnum,-(sp)
	move.l	d2,-(sp)
	lea	sht_pac(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#8,sp
	rts

; mpega max chunk

sh_maxchunk	move.l	sharg+sha_arg(a5),d0
	beq.b	.disp
	move.l	d0,a0
	move.l	(a0),d0
	beq.w	sh_illq
	bmi.w	sh_illq
	move.l	d0,conftmp+conf_mpmaxchunk(a5)
	move.l	d0,mpmaxchunk(a5)
	rts
.disp	move.l	conftmp+conf_mpmaxchunk(a5),-(sp)
	lea	sht_maxc(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#4,sp
	rts

; mpega chunk

sh_chunk	move.l	sharg+sha_arg(a5),d0
	beq.b	.disp
	move.l	d0,a0
	move.l	(a0),d0
	beq.w	sh_illq
	bmi.w	sh_illq
	move.l	d0,conftmp+conf_mpchunk(a5)
	move.l	d0,mpchunk(a5)
	rts
.disp	move.l	conftmp+conf_mpchunk(a5),-(sp)
	lea	sht_chunk(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#4,sp
	rts

; fakecnt

sh_fakecnt	move.l	sharg+sha_arg(a5),d0
	beq.b	.disp
	move.l	d0,a0
	move.l	(a0),d0
	beq.w	sh_illq
	cmp.l	#mpa_pcm_size,d0
	bhi.w	sh_illq
	move.l	d0,conftmp+conf_mpfakecnt(a5)

	IFD	mpusemagic
	move.l	excbase(a5),a6
	jsr	_LVOForbid(a6)
	move.l	stream(a5),d0
	beq.b	.0
	move.l	d0,a0
	cmp.l	#mpmagic,stream_magic(a0)
	bne.b	.0
	move.l	stream_extra(a0),a0	;buffert ujra torolni
	clr.w	strext_fillcnt(a0)	;kell!
.0	jsr	_LVOPermit(a6)
	ENDIF

.disp	move.l	conftmp+conf_mpfakecnt(a5),-(sp)
	lea	sht_fake(pc),a0
	move.l	sp,a1
	bsr.w	printLine
	addq.l	#4,sp
	rts

; irinit/irstop

sh_irinit	moveq	#VSSpkt_IrMode,d0
	st	d1
	move.l	lport(a5),a0
	sub.l	a1,a1
	bra.l	sendPkt

sh_irstop	moveq	#VSSpkt_IrMode,d0
	clr.l	d1
	move.l	lport(a5),a0
	sub.l	a1,a1
	bra.l	sendPkt



; In A0:hexstring  Out D0:bin, #-1 for error (and Z will be set) D1:last char

parsehex	movem.l	d2/d3,-(sp)
	clr.l	d0
	move.l	a0,d3
	moveq	#8,d2
.0	move.b	(a0)+,d1
	beq.b	.2
	cmp.b	#10,d1
	beq.b	.2
	cmp.b	#32,d1
	beq.b	.2
	sub.b	#48,d1	;< '0'
	bcs.b	.3	;hiba
	cmp.b	#9,d1	;<= '9'
	ble.b	.1	;szam
	bclr	#5,d1
	sub.b	#7,d1
	cmp.b	#10,d1	;< 'a'
	blt.b	.3	;hiba
	cmp.b	#15,d1	;> 'f'
	bgt.b	.3	;hiba
.1	asl.l	#4,d0
	or.b	d1,d0
	dbf	d2,.0
.3	move.l	d3,a0
	moveq	#-1,d0	;too many digits
	bra.b	.ok
.2	cmp.b	#8,d2	;first char bad
	beq.b	.3
	move.b	-(a0),d1	;ok, unread last
.ok	cmp.l	#-1,d0
	movem.l	(sp)+,d2/d3
	rts

; UP/DOWN mozog a history-ban
; (kilepeskor a hibakod nem erdekes)

shelledithook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	cmp.l	#SGH_KEY,(a1)
	bne.w	.0
	tst.w	sgw_Code(a2)
	beq.b	.rawkey
	sf	shellchanged(a5)
	bra.w	.0
.rawkey	move.l	sgw_IEvent(a2),a0
	move.w	ie_Code(a0),d0
	move.l	shellhistnext(a5),a0
	sub.b	#$4c,d0	;up?
	beq.b	.up
	subq.b	#1,d0	;down?
	bne.b	.0
	move.l	a0,d0
	beq.b	.3
	move.l	(a0),d0
	beq.b	.empty
	move.l	d0,a0
	tst.l	(a0)
	beq.b	.empty
	bra.b	.4
.up	move.l	a0,d0
	beq.b	.3
	move.l	LN_PRED(a0),d0
	beq.b	.empty
	move.l	d0,a0
	tst.l	LN_PRED(a0)
	beq.b	.empty
	bra.b	.4
.3	move.l	shellhist+LH_TAILPRED(a5),a0
	tst.l	LN_PRED(a0)
	beq.b	.empty
.4	move.l	(a0),a1	;utolso node?
	tst.l	(a1)	;(Ha igen akkor nem
	seq	shellchanged(a5)	;ismeteljuk meg)
	move.l	a0,shellhistnext(a5)
	lea	hn_size(a0),a0	;szoveg masolasa
	move.l	sgw_WorkBuffer(a2),a1
	moveq	#-2,d0
.6	addq.w	#1,d0
	move.b	(a0)+,(a1)+
	bne.b	.6
	move.w	d0,sgw_BufferPos(a2)	;cursor pos
	addq.w	#1,d0
	move.w	d0,sgw_NumChars(a2)	;string length
	clr.b	-2(a1)	;remove EOL
	bra.b	.ok
.empty	move.l	a0,shellhistnext(a5)
	move.l	sgw_WorkBuffer(a2),a0
	clr.b	(a0)
	clr.w	sgw_NumChars(a2)
	clr.w	sgw_BufferPos(a2)
.ok	;or.l	#SGA_USE,sgw_Actions(a2)
	moveq	#-1,d0	;return code
	bra.b	.1	;does not care
.0	clr.l	d0
.1	movem.l	(sp)+,d2-d7/a2-a6
	rts

; Aktualis lap szamat taroljuk gui_actpage1-be
; Ha a shell lap lett kivalasztva, ugras az utolso outputra

gui_reghook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	move.l	(a1)+,d3
	move.b	d3,gui_actpage1(a5)
	tst.b	gui_actpage(a5)
	bmi.b	.0
	move.b	d3,gui_actpage(a5)
.0	cmp.b	#page_shell,d3
	bne.b	.ok
	DoMethod	gad_shellist(a5),#MUIM_List_Jump,#MUIV_List_Jump_Active
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

; Ha ikonizalt allapotban vagyunk, vagy az ablak nincs nyitva,
; gui_actpage(a5) = -1
; Egyebkent az aktualis lap, gui_actpage1(a5)

; window open/close

gui_winhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	tst.l	(a1)
	seq	d0
	or.b	gui_actpage1(a5),d0
	move.b	d0,gui_actpage(a5)
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts

; application iconified

gui_iconhook	dc.l	0,0,.entry,0,0

.entry	movem.l	d2-d7/a2-a6,-(sp)
	move.l	var(pc),a5
	tst.l	(a1)
	sne	d0
	or.b	gui_actpage1(a5),d0
	move.b	d0,gui_actpage(a5)
.ok	movem.l	(sp)+,d2-d7/a2-a6
	rts



; strings for the gui engine

	txtstart

	include	VSS:Sources/include/mui_asm.i

; application

guit_author	dc.b	'Ferenc VALENTA',0
guit_base	testm
	dc.b	'VSS',0
guit_title	testm
	dc.b	'VSSDriver',0

guit_version	dc.b	'$VER: VSSDriver '
	version
	dc.b	'.'
	build
	dc.b	' ('
	;date
	%getdate	3
	dc.b	')',0
guit_copyright	dc.b	'©'
	copyright
	dc.b	' by VF',0
guit_desc	dc.b	'VF Sound System Driver/Controls',0

; window

guit_wintit	dc.b	'VSS Driver/Controls',0
guit_screentit	dc.b	'VSS Driver/Controls V'
	version
	dc.b	'  ©'
	copyright
	dc.b	' by Valenta Ferenc',0

; info

guit_info	dc.b	'VF Sound System driver software',10
	dc.b	'©'
	copyright
	dc.b	' by Valenta Ferenc',10
	dc.b	'Version '
	version
	dc.b	' Build '
	build
	dc.b	' ('
	;date
	%getdate	3
	dc.b	')',10
	dc.b	'Compilation time: '
	%gettime
	dc.b	0

guit_aboutmui	dc.b	'About',10
	dc.b	'MUI',0

guit_info3	dc.b	'Ideas, bug reports are always welcome',10
	dc.b	'vf@elte.hu    '
	dc.b	'http://ludens.elte.hu/~vf/'
	IFD	testmode
	dc.b	10,'*** INTERNAL TEST VERSION, DO NOT SPREAD ***'
	ENDIF
	dc.b	0

guit_info4	dc.b	10,10,10,10,10,10,10,10,10,10,10,10,0
guit_info5	dc.b	10,10,'Greetings will fly to...',10,10,0

guit_info2	dc.b	'Balogh Zoltán (Docky)',10
	dc.b	'Botlik Mihály [VMPRO]',10
	dc.b	'Carl Alberty [Crystal]',10
	dc.b	'Cziglédszky László (Nemtom)',10
	dc.b	'Dienes Péter (PDP)',10
	dc.b	'Ferge László [STM]',10
	dc.b	'Gothárd József',10
	dc.b	'Kárpáti Szilvi',10
	dc.b	'Martin Blom',10
	dc.b	'Qxy',10
	dc.b	'Stefan Stuntz',10
	dc.b	'Stuart Dimond [Crystal]',10
	dc.b	'Zelenyánszky Anna',0

guit_info22	dc.b	'Barna Béla [Spoerle]',10
	dc.b	'Bujdosó Artur (Artlace)',10
	dc.b	'Chip',10
	dc.b	'Demeter Noémi (Pluto)',10
	dc.b	'Fazekas Csaba (Enc0re)',10
	dc.b	'Füzék Péter',10
	dc.b	'Kankai Zoltán (MrZ)',10
	dc.b	'Marco Veneri [STM]',10
	dc.b	'Miklós Árpád (Alan)',10
	dc.b	'Schmiedl Tibor (Cyx)',10
	dc.b	'Stephane Tavenard',10
	dc.b	'Thorsten Hansen [KatoDev]',10,0

guit_info6	dc.b	10,'...and all the forgotten ones'
guit_info8	dc.b	10,10,0

guit_info7	dc.b	$1b,'bVSSDriver',$1b,'n uses the ',$1b,'bMUI',$1b,'n'
	dc.b	' object library.',10
	dc.b	'MUI is ©1992-98 by Stefan Stuntz',10,10,10
	dc.b	'This program is freely distributable, as long as ',10
	dc.b	'all files are included and no profit is made.',10
	dc.b	'Parts of this product may not be included in',10
	dc.b	'commercial software, and it may not be modified',10
	dc.b	'without my prior written permission.',10
	dc.b	'No warranty, use at your own risk!'
	dc.b	10,10,10
	dc.b	'Written in pure, highly optimized assembly',10
	dc.b	'Compiled with Asm-Pro V1.16i',10,0

guit_info9	dc.b	10,$1b,'iRULES FOREVER!',10,0

; shell

i_welc	dc.b	'««« VSS Command Shell V'
	version
	dc.b	' »»»',10
	dc.b	'©'
	copyright
	dc.b	' Valenta Ferenc <vf@elte.hu>',10
	dc.b	$1b,'b',$1b,'u'
	;dc.b	$1b,'3'
	dc.b	'Type ''help'' for help.',10
	dc.b	'Code: $%08.8lx  Data: $%08.8lx',0

; mixer

guit_center	dc.b	$1b,'c',0

guit_chn1	dc.b	'In1',0
guit_chn2	dc.b	'In2',0
guit_chn3	dc.b	'In3',0
guit_chn4	dc.b	'In4',0
guit_chn5	dc.b	'Chn5',0
guit_chnt	dc.b	'12345678',0

guit_mm_stereo	dc.b	'Stereo',0
guit_mm_mono	dc.b	'Mono',0
guit_mm_mute	dc.b	'Mute',0
guit_mm_diff	dc.b	'Diff',0

; mpeg

guit_mp1	dc.b	'Bass freq',0
guit_mp2	dc.b	'Bass enhance',0
guit_mp3	dc.b	'Treble freq',0
guit_mp4	dc.b	'Treble enhance',0
guit_mp5	dc.b	'Attenuation',0
guit_mp6	dc.b	'Stereo width',0

; player

gent_Blues	dc.b	'Blues',0
gent_ClassRock	dc.b	'Classic Rock',0
gent_Country	dc.b	'Country',0
gent_Dance	dc.b	'Dance',0
gent_Disco	dc.b	'Disco',0
gent_Funk	dc.b	'Funk',0
gent_Grunge	dc.b	'Grunge',0
gent_HipHop	dc.b	'Hip-Hop',0
gent_Jazz	dc.b	'Jazz',0
gent_Metal	dc.b	'Metal',0
gent_NewAge	dc.b	'New Age',0
gent_Oldies	dc.b	'Oldies',0
gent_Other	dc.b	'Other',0
gent_Pop	dc.b	'Pop',0
gent_RB	dc.b	'R&B',0
gent_Rap	dc.b	'Rap',0
gent_Reggae	dc.b	'Reggae',0
gent_Rock	dc.b	'Rock',0
gent_Techno	dc.b	'Techno',0
gent_Industrial	dc.b	'Industrial',0
gent_Alter	dc.b	'Alternative',0
gent_Ska	dc.b	'Ska',0
gent_DeathMetal	dc.b	'Death Metal',0
gent_Pranks	dc.b	'Pranks',0
gent_Soundtrack	dc.b	'Soundtrack',0
gent_EuroTechno	dc.b	'Euro-Techno',0
gent_Ambient	dc.b	'Ambient',0
gent_TripHop	dc.b	'Trip-Hop',0
gent_Vocal	dc.b	'Vocal',0
gent_JazzFunk	dc.b	'Jazz+Funk',0
gent_Fusion	dc.b	'Fusion',0
gent_Trance	dc.b	'Trance',0
gent_Classical	dc.b	'Classical',0
gent_Instr	dc.b	'Instrumental',0
gent_Acid	dc.b	'Acid',0
gent_House	dc.b	'House',0
gent_Game	dc.b	'Game',0
gent_SoundClip	dc.b	'Sound Clip',0
gent_Gospel	dc.b	'Gospel',0
gent_Noise	dc.b	'Noise',0
gent_AlternRock	dc.b	'AlternRock',0
gent_Bass	dc.b	'Bass',0
gent_Soul	dc.b	'Soul',0
gent_Punk	dc.b	'Punk',0
gent_Space	dc.b	'Space',0
gent_Meditative	dc.b	'Meditative',0
gent_InstrPop	dc.b	'Instrumental Pop',0
gent_InstrRock	dc.b	'Instrumental Rock',0
gent_Ethnic	dc.b	'Ethnic',0
gent_Gothic	dc.b	'Gothic',0
gent_Darkwave	dc.b	'Darkwave',0
gent_TechIndust	dc.b	'Techno-Industrial',0
gent_Electronic	dc.b	'Electronic',0
gent_PopFolk	dc.b	'Pop-Folk',0
gent_Eurodance	dc.b	'Eurodance',0
gent_Dream	dc.b	'Dream',0
gent_SouthRock	dc.b	'Southern Rock',0
gent_Comedy	dc.b	'Comedy',0
gent_Cult	dc.b	'Cult',0
gent_Gangsta	dc.b	'Gangsta',0
gent_Top40	dc.b	'Top 40',0
gent_ChristRap	dc.b	'Christian Rap',0
gent_PopFunk	dc.b	'Pop/Funk',0
gent_Jungle	dc.b	'Jungle',0
gent_NativeAm	dc.b	'Native American',0
gent_Cabaret	dc.b	'Cabaret',0
gent_NewWave	dc.b	'New Wave',0
gent_Psycha	dc.b	'Psychadelic',0
gent_Rave	dc.b	'Rave',0
gent_Showtunes	dc.b	'Showtunes',0
gent_Trailer	dc.b	'Trailer',0
gent_LoFi	dc.b	'Lo-Fi',0
gent_Tribal	dc.b	'Tribal',0
gent_AcidPunk	dc.b	'Acid Punk',0
gent_AcidJazz	dc.b	'Acid Jazz',0
gent_Polka	dc.b	'Polka',0
gent_Retro	dc.b	'Retro',0
gent_Musical	dc.b	'Musical',0
gent_RockRoll	dc.b	'Rock & Roll',0
gent_HardRock	dc.b	'Hard Rock',0
gent_Unknown	dc.b	'Unknown',0

guip_playform	dc.b	'D=8,',0

guip_form	dc.b	'%02.2d:%02.2d',0

guip_dbmform	dc.b	'%lddB',0
guip_dbzform	dc.b	' %lddB',0
guip_dbpform	dc.b	'+%lddB',0
guip_hzform	dc.b	'%ldHz',0

guip_kform	dc.b	'%ldk',0
guip_msform	dc.b	'%ldms',0

guit_text	;dc.b	'MPEG Audio Player V'

	dc.b	'n'
	dc.b	$1b,'uE'
	dc.b	$1b,'n',$1b,'bt'
	dc.b	$1b,'iU'
	dc.b	$1b,'n',$1b,'uD'
	dc.b	$1b,'n',$1b,'im'
	dc.b	$1b,'n',$1b,'bE'
	dc.b	$1b,'n',$1b,'ig'
	dc.b	$1b,'n',$1b,'bM'
	dc.b	$1b,'n',$1b,'uI'
	dc.b	$1b,'n',$1b,'ie'
	dc.b	$1b,'n',$1b,'bz'
	dc.b	$1b,'n V'

	version
	dc.b	10,10
	dc.b	'Drop files here or use the eject button!',0

guit_status	dc.b	'READY.',0
guit_status1	dc.b	'Play',0
guit_status2	dc.b	'Paused',0
guit_status3	dc.b	'Seek',0
guit_status4	dc.b	'Jump',0

guit_bytes	dc.b	'%ldk',0


guit_playfile	dc.b	'Load audio/playlist files...',0
guit_savefile	dc.b	'Save playlist...',0
guit_pattxt	dc.b	'#?.plst',0
;guit_okeject	dc.b	'Lo__ad',0
;guit_oksave	dc.b	'-Save',0

; sampler

guit_samc1	dc.b	'Left',0
guit_samc2	dc.b	'Right',0

guit_mondis	dc.b	'DISABLED',10
	dc.b	'« Click »',0

guit_levmode	dc.b	'Histogram limit:',0

guit_inmon	dc.b	'Input level',0
guit_reso	dc.b	'Resolution',0
guit_control	dc.b	'Control',0
guit_file	dc.b	'Output file',0
guit_sstat	dc.b	'Status',0

guit_open	dc.b	'Prepare',0
guit_close	dc.b	'Close',0

guit_sabuf	dc.b	'Sample buffer:',0
guit_declev	dc.b	'Level drop spd:',0
guit_levhist	dc.b	'Create histograms:',0
guit_samappend	dc.b	'Append file:',0

guit_start	dc.b	'Start',0
guit_stop	dc.b	'Stop/pause',0

guit_filesel	dc.b	'Select output file...',0

; options

guit_ahivol
guit_melvol
guit_mpvol	dc.b	'Accept volume:',0
guit_mpprayer	dc.b	$1b,'iPrayer2 only',0
guit_mpsupport	dc.b	'Mpega.lib support:',0

guit_const	dc.b	'««« Under construction »»»',10
	dc.b	'Come back later! ;)',0

;guit_ahi	dc.b	'AHI driver',0
;guit_vss	dc.b	'VSS settings',0
;guit_melody	dc.b	'Melody emulation',0
;guit_mpega	dc.b	'MPEGA emulation',0

guit_save	dc.b	'Save prefs',0
guit_default	dc.b	'Load defaults',0
guit_unload	dc.b	'Unload driver',0
guit_hide	dc.b	'Hide GUI',0

guit_opt1_1	dc.b	'Max buffer:',0
guit_opt1_2	dc.b	'Quantum:',0
guit_opt1_3	dc.b	'Lowlevel priority:',0
guit_opt1_4	dc.b	'BufMan priority:',0
guit_opt1_5	dc.b	'Interrupt mixing:',0
guit_opt1_6	dc.b	'Interrupt playing:',0
guit_opt1_7	dc.b	'Delayed flush:',0
guit_opt1_8	dc.b	'Pass on used str:',0
guit_opt1_9	dc.b	'Enable AHI:',0
guit_opt1_10	dc.b	'Enable Melody:',0
guit_opt1_11	dc.b	'Enable Mpega:',0
guit_opt1_12	dc.b	'Load mpega.lib:',0
guit_opt1_13	dc.b	'Load in RAM:',0
guit_opt1_14	dc.b	'Enable de-emphasis:',0
guit_opt1_15	dc.b	$1b,'iWARNING!',0
guit_opt2_1	dc.b	'Buffer:',0
guit_opt2_3	dc.b	'Time corr:',0

guit_opt3_1	dc.b	'Debuglevel:',0

guit_opt3_3	dc.b	'Digital name:',0
guit_opt3_4	dc.b	'Input 1 name:',0
guit_opt3_5	dc.b	'Input 2 name:',0
guit_opt3_6	dc.b	'Input 3 name:',0
guit_opt3_7	dc.b	'Input 4 name:',0
guit_opt3_10	dc.b	'GUI update delay:',0

; misc

guit_doserr	dc.b	'*** DOS Error',0

guit_playinfo	;dc.b	'%s / %s (%s)',10
	dc.b	10,'MPEG%ld-%s %ldkbps %ldHz %s',10,0
	even

guia_mpegmode	dc.l	.0,.1,.2,.3

.0	dc.b	'Stereo',0
.1	dc.b	'J-stereo',0
.2	dc.b	'2-channel',0
.3	dc.b	'Mono',0
	even

guia_mpeglayer	dc.l	.0,.1,.2

.0	dc.b	'I',0
.1	dc.b	'II',0
.2	dc.b	'III',0
	even

guia_regtitles	dc.l	.0
	dc.l	.1
	dc.l	.2
	dc.l	.4
	dc.l	.5
	dc.l	.6
	dc.l	0

.0	dc.b	'Info',0
.1	dc.b	'Shell',0
.2	dc.b	'Mixer',0
.4	dc.b	'Player',0
.5	dc.b	'Sampler',0
.6	dc.b	'Options',0
	even

guia_mixmod2	dc.l	guit_mm_stereo
	dc.l	guit_mm_diff
	dc.l	guit_mm_mute
nothing	dc.l	0

guia_mixmode	dc.l	guit_mm_stereo
	dc.l	guit_mm_diff
	dc.l	guit_mm_mono
	dc.l	guit_mm_mute
	dc.l	0

guisa_filetype	dc.l	.0
	dc.l	.1
	dc.l	.2
	dc.l	.3
	dc.l	.4
	dc.l	.5
	dc.l	0

.0	dc.b	'8bit Mono  ',0
.1	dc.b	'8bit Stereo',0
.2	dc.b	'16b Mono   ',0
.3	dc.b	'16b M Intel',0
.4	dc.b	'16b Stereo ',0
.5	dc.b	'16b S Intel',0
	even

guisa_typ16	dc.l	guitftyp1
	dc.l	guitftyp2
	dc.l	guitftyp3
	dc.l	0

guisa_typ8	dc.l	guitftyp1
	dc.l	0

guitftyp1	dc.b	'RAW',0
guitftyp2	dc.b	'AIFF',0
guitftyp3	dc.b	'WAV',0
	even

gui_cycentries	dc.l	.0
	dc.l	.1
	dc.l	.2
	dc.l	.3
	dc.l	0

.0	dc.b	'error',0
.1	dc.b	'info',0
.2	dc.b	'simple debug',0
.3	dc.b	'detail debug',0
	even

guia_regtitles2	dc.l	.0	;options
	dc.l	.1
	dc.l	.2
	dc.l	.3
	dc.l	.4
	dc.l	.7
	dc.l	.6
	dc.l	.8
	dc.l	.9
	dc.l	.5
	dc.l	0

.0	dc.b	'General',0
.1	dc.b	'VSS',0
.2	dc.b	'AHI',0
.3	dc.b	'Melody',0
.4	dc.b	'Mpega',0
.7	dc.b	'Player',0
.6	dc.b	'Sampler',0
.8	dc.b	'MP3 DSP',0
.9	dc.b	'IR remote',0
.5	dc.b	'Expansion',0

guit_opt1	dc.b	'General options',0
guit_opt2	dc.b	'VSS options',0
guit_opt3	dc.b	'AHI options',0
guit_opt4	dc.b	'Melody options',0
guit_opt7	dc.b	'Player options',0
guit_opt6	dc.b	'Sampler options',0
guit_opt8	dc.b	'MP3 DSP options',0
guit_opt5	dc.b	'Mpega options',0
guit_opt9	dc.b	'IR remote control options',0
	even


guisa_levmode	dc.l	.0
	dc.l	.1
	dc.l	.2
	dc.l	.3
	dc.l	0

.0	dc.b	'Maximum',0
.1	dc.b	'98%',0	;64
.2	dc.b	'94%',0	;16
.3	dc.b	'87%',0	;4
	even

guia_regtitles3	dc.l	.0	;slot
	dc.l	.1
	dc.l	.2
	dc.l	.3
	dc.l	0

.0	dc.b	'Slot #1',0
.1	dc.b	'Slot #2',0
.2	dc.b	'Slot #3',0
.3	dc.b	'Slot #4',0

	txtend
	even

; paletta az ikonokhoz, kb MagicWB szinek

gui_logocolors	dc.l	$99999999,$99999999,$99999999	;szurke (hatter)
	dc.l	$00000000,$00000000,$00000000	;fekete
	dc.l	$ffffffff,$ffffffff,$ffffffff	;feher
	dc.l	$55555555,$77777777,$aaaaaaaa	;kek
	dc.l	$77777777,$77777777,$77777777	;sotetszurke
	dc.l	$bbbbbbbb,$bbbbbbbb,$bbbbbbbb	;vilagosszurke
	dc.l	$aaaaaaaa,$88888888,$77777777	;barna
	dc.l	$ffffffff,$00000000,$00000000	;piros

;	dc.l	$99999999,$99999999,$99999999	;szurke (hatter)
;	dc.l	$00000000,$00000000,$00000000	;fekete
;	dc.l	$f0f0f0f0,$f0f0f0f0,$f0f0f0f0	;feher
;	dc.l	$55555555,$77777777,$aaaaaaaa	;kek
;	dc.l	$88888888,$88888888,$88888888	;sotetszurke
;	dc.l	$aaaaaaaa,$aaaaaaaa,$aaaaaaaa	;vilagosszurke
;	;dc.l	$aaaaaaaa,$99999999,$88888888	;vilagosbarna
;	dc.l	$aaaaaaaa,$99999999,$88888888	;barna
;	dc.l	$eeeeeeee,$aaaaaaaa,$99999999	;rozsaszin
;	;dc.l	$ffffffff,$00000000,$00000000	;piros (teszt)

genretab	dc.l	gent_Blues	
	dc.l	gent_ClassRock	
	dc.l	gent_Country	
	dc.l	gent_Dance	
	dc.l	gent_Disco	
	dc.l	gent_Funk	
	dc.l	gent_Grunge	
	dc.l	gent_HipHop	
	dc.l	gent_Jazz	
	dc.l	gent_Metal	
	dc.l	gent_NewAge	
	dc.l	gent_Oldies	
	dc.l	gent_Other	
	dc.l	gent_Pop	
	dc.l	gent_RB	
	dc.l	gent_Rap	
	dc.l	gent_Reggae	
	dc.l	gent_Rock	
	dc.l	gent_Techno	
	dc.l	gent_Industrial	
	dc.l	gent_Alter	
	dc.l	gent_Ska	
	dc.l	gent_DeathMetal	
	dc.l	gent_Pranks	
	dc.l	gent_Soundtrack	
	dc.l	gent_EuroTechno	
	dc.l	gent_Ambient	
	dc.l	gent_TripHop	
	dc.l	gent_Vocal	
	dc.l	gent_JazzFunk	
	dc.l	gent_Fusion	
	dc.l	gent_Trance	
	dc.l	gent_Classical	
	dc.l	gent_Instr	
	dc.l	gent_Acid	
	dc.l	gent_House	
	dc.l	gent_Game	
	dc.l	gent_SoundClip	
	dc.l	gent_Gospel	
	dc.l	gent_Noise	
	dc.l	gent_AlternRock	
	dc.l	gent_Bass	
	dc.l	gent_Soul	
	dc.l	gent_Punk	
	dc.l	gent_Space	
	dc.l	gent_Meditative	
	dc.l	gent_InstrPop	
	dc.l	gent_InstrRock	
	dc.l	gent_Ethnic	
	dc.l	gent_Gothic	
	dc.l	gent_Darkwave	
	dc.l	gent_TechIndust	
	dc.l	gent_Electronic	
	dc.l	gent_PopFolk	
	dc.l	gent_Eurodance	
	dc.l	gent_Dream	
	dc.l	gent_SouthRock	
	dc.l	gent_Comedy	
	dc.l	gent_Cult	
	dc.l	gent_Gangsta	
	dc.l	gent_Top40	
	dc.l	gent_ChristRap	
	dc.l	gent_PopFunk	
	dc.l	gent_Jungle	
	dc.l	gent_NativeAm	
	dc.l	gent_Cabaret	
	dc.l	gent_NewWave	
	dc.l	gent_Psycha	
	dc.l	gent_Rave	
	dc.l	gent_Showtunes	
	dc.l	gent_Trailer	
	dc.l	gent_LoFi	
	dc.l	gent_Tribal	
	dc.l	gent_AcidPunk	
	dc.l	gent_AcidJazz	
	dc.l	gent_Polka	
	dc.l	gent_Retro	
	dc.l	gent_Musical	
	dc.l	gent_RockRoll	
	dc.l	gent_HardRock	
	dc.l	gent_Unknown	

muistart

guim_notimpl	Childi BodychunkObject	; "not implemented" page
	 MUIT MUIA_Bodychunk_Body,im_work
	 MUIT MUIA_Bodychunk_Depth,3
	 ;MUIT MUIA_Bodychunk_Compression,1
	 ;MUIT MUIA_Bodychunk_Masking,0
	 MUIT MUIA_FixWidth,53
	 MUIT MUIA_FixHeight,47
	 MUIT MUIA_Bitmap_Width,53
	 MUIT MUIA_Bitmap_Height,47
	 MUIT MUIA_Bitmap_Transparent,0
	 MUIT MUIA_Bitmap_Precision,PRECISION_IMAGE
	 MUIT MUIA_Bitmap_SourceColors,gui_logocolors
	Endi
	Childi TextObject
	 MUIT MUIA_Frame,MUIV_Frame_None
	 MUIT MUIA_Text_Contents,guit_const
	 MUIT MUIA_Text_PreParse,guit_center
	Endi
	rts

initgui	move.l	dosbase(a5),a6	;load icon
	jsr	_LVOGetProgramDir(a6)
	move.l	d0,d1
	jsr	_LVOCurrentDir(a6)
	move.l	d0,-(sp)
	move.l	icobase(a5),a6
	lea	progname,a0
	jsr	_LVOGetDiskObject(a6)
	move.l	d0,gui_icon(a5)
	move.l	dosbase(a5),a6
	move.l	(sp)+,d1
	jsr	_LVOCurrentDir(a6)

	move.l	excbase(a5),a6	;tag memory
	move.l	TAG_SPACE_LONG,d0
	moveq	#MEMF_ANY,d1
	jsr	_LVOAllocVec(a6)
	move.l	d0,gui_tagspace(a5)
	beq.b	.err

	move.l	muibase(a5),a6	;create our private class
	sub.l	a0,a0	;base: 0
	lea	MUIC_Slider_s(pc),a1	;supername
	sub.l	a2,a2	;supermcc: 0
	lea	sliderdisp(pc),a3	;dispfunc
	moveq	#od_size,d0	;datasize
	jsr	_LVOMUI_CreateCustom(a6)
	move.l	d0,gui_myslidermcc(a5)
	beq.b	.err2

	sub.l	a0,a0	;base: 0
	lea	MUIC_Group_s(pc),a1	;supername
	sub.l	a2,a2	;supermcc: 0
	lea	bodydisp(pc),a3	;dispfunc
	moveq	#bo_size,d0	;datasize
	jsr	_LVOMUI_CreateCustom(a6)
	move.l	d0,gui_mybodymcc(a5)
	bne.b	.ok

	move.l	gui_myslidermcc(a5),a0	;hiba! class torlese
	jsr	_LVOMUI_DeleteCustom(a6)
	clr.l	gui_myslidermcc(a5)
.err2	move.l	excbase(a5),a6
	move.l	gui_tagspace(a5),a1
	clr.l	gui_tagspace(a5)
	jsr	_LVOFreeVec(a6)
.err	clr.l	d0
	rts

.ok	move.l	gui_myslidermcc(a5),a0
	move.l	mcc_Class(a0),gui_mysliderclass(a5)
	move.l	gui_mybodymcc(a5),a0
	move.l	mcc_Class(a0),gui_mybodyclass(a5)

	move.l	gui_tagspace(a5),a4

; gui description

 ApplicationObject
  MUIT MUIA_Application_Author,guit_author
  MUIT MUIA_Application_Base,guit_base
  MUIT MUIA_Application_Title,guit_title
  MUIT MUIA_Application_Version,guit_version
  MUIT MUIA_Application_Copyright,guit_copyright
  MUIT MUIA_Application_Description,guit_desc
  MUIT MUIA_Application_SingleTask,TRUE
  MUIT MUIA_Application_DiskObject
  MUIT2 gui_icon(a5)
  MUIT MUIA_Application_BrokerHook,gui_brokerhook
  SubWindowi
  WindowObject
   MUIT MUIA_Window_Open,FALSE
   MUIT MUIA_Window_Title,guit_wintit
   MUIT MUIA_Window_ID,"0win"
   MUIT MUIA_Window_AppWindow,TRUE
   MUIT MUIA_Window_NoMenus,TRUE
   MUIT MUIA_Window_ScreenTitle,guit_screentit
   ;MUIT MUIA_Window_SizeGadget,FALSE
   ;MUIT MUIA_Window_Width,99;MUIV_Window_Width_MinMax
   WindowContentsi
   RegisterObject
    MUIT MUIA_Register_Titles,guia_regtitles
    ;MUIT MUIA_Group_SameWidth,TRUE
    MUIT MUIA_CycleChain,1
    MUIT MUIA_Group_ActivePage
    clr.l   d2
    move.b  startpage,d2
    MUIT2 d2

; info page

    Childi GroupObject
     Childi HGroup
      ;MUIT MUIA_Frame,MUIV_Frame_Button
      Childi BodychunkObject	;VSS logo
       MUIT MUIA_Bodychunk_Body,im_vsslogo
       MUIT MUIA_Bodychunk_Depth,3
       ;MUIT MUIA_Bodychunk_Compression,1
       ;MUIT MUIA_Bodychunk_Masking,0
       MUIT MUIA_FixWidth,55
       MUIT MUIA_FixHeight,31
       MUIT MUIA_Bitmap_Width,55
       MUIT MUIA_Bitmap_Height,31
       MUIT MUIA_Bitmap_Precision,PRECISION_IMAGE
       MUIT MUIA_Bitmap_SourceColors,gui_logocolors
      Endi
      Childi TextObject
       MUIT MUIA_Frame,MUIV_Frame_None
       MUIT MUIA_Text_Contents,guit_info
      Endi
      Childi HSpace
      Childi TextObject
       MUIT MUIA_Frame,MUIV_Frame_None
       MUIT MUIA_Text_PreParse,guit_center
       MUIT MUIA_Text_Contents,guit_aboutmui
      Endi
      Childi BodychunkObject	;MUI logo
       MUIT MUIA_Bodychunk_Body,im_muilogo
       MUIT MUIA_Bodychunk_Depth,3
       ;MUIT MUIA_Bodychunk_Compression,1
       ;MUIT MUIA_Bodychunk_Masking,0
       MUIT MUIA_FixWidth,62
       MUIT MUIA_FixHeight,32
       MUIT MUIA_Bitmap_Width,62
       MUIT MUIA_Bitmap_Height,32
       MUIT MUIA_Bitmap_Transparent,0
       MUIT MUIA_Bitmap_Precision,PRECISION_IMAGE
       MUIT MUIA_Bitmap_SourceColors,gui_logocolors
       MUIT MUIA_InputMode,MUIV_InputMode_RelVerify
       MUIT MUIA_ShowSelState,FALSE
       MUIT MUIA_CycleChain,1
      Endi
      is gad_aboutm(a5)
     Endi
     Childi RectangleObject
      MUIT MUIA_Rectangle_HBar,TRUE
      MUIT MUIA_FixHeight,8
     Endi
     Childi TextObject
      MUIT MUIA_Frame,MUIV_Frame_None
      MUIT MUIA_Text_Contents,guit_info3
     Endi
     Childi RectangleObject
      MUIT MUIA_Rectangle_HBar,TRUE
      MUIT MUIA_FixHeight,8
     Endi
     Childi ScrollgroupObject	;scroll text
      MUIT MUIA_Scrollgroup_FreeHoriz,FALSE
      MUIT MUIA_Scrollgroup_Contents
      VirtgroupObject
       Childi HGroup
        Childi TextObject
         MUIT MUIA_Frame,MUIV_Frame_None
         MUIT MUIA_Text_Contents,guit_info4
         MUIT MUIA_Text_PreParse,guit_center
        Endi
        Childi GroupObject
         Childi BodychunkObject
          MUIT MUIA_Bodychunk_Body,im_amiga
          MUIT MUIA_Bodychunk_Depth,3
          ;MUIT MUIA_Bodychunk_Compression,1
          ;MUIT MUIA_Bodychunk_Masking,0
          MUIT MUIA_FixWidth,87
          MUIT MUIA_FixHeight,24
          MUIT MUIA_Bitmap_Width,87
          MUIT MUIA_Bitmap_Height,24
          MUIT MUIA_Bitmap_Transparent,0
          MUIT MUIA_Bitmap_Precision,PRECISION_IMAGE
          MUIT MUIA_Bitmap_SourceColors,gui_logocolors
         Endi
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_info9
          MUIT MUIA_Text_PreParse,guit_center
         Endi
        Endi
        Childi TextObject
         MUIT MUIA_Frame,MUIV_Frame_None
         MUIT MUIA_Text_Contents,guit_info4
         MUIT MUIA_Text_PreParse,guit_center
        Endi
       Endi
       Childi TextObject		;third party soft
        MUIT MUIA_Frame,MUIV_Frame_None
        MUIT MUIA_Text_Contents,guit_info7
        MUIT MUIA_Text_PreParse,guit_center
       Endi
       Childi HGroup
        Childi HSpace
        Childi BodychunkObject	;AsmPro logo
         MUIT MUIA_Bodychunk_Body,im_asmpro
         MUIT MUIA_Bodychunk_Depth,3
         ;MUIT MUIA_Bodychunk_Compression,1
         ;MUIT MUIA_Bodychunk_Masking,0
         MUIT MUIA_FixWidth,97
         MUIT MUIA_FixHeight,25
         MUIT MUIA_Bitmap_Width,97
         MUIT MUIA_Bitmap_Height,25
         MUIT MUIA_Bitmap_Transparent,0
         MUIT MUIA_Bitmap_Precision,PRECISION_IMAGE
         MUIT MUIA_Bitmap_SourceColors,gui_logocolors
        Endi
        Childi HSpace
       Endi
       Childi TextObject		;greetings...
        MUIT MUIA_Frame,MUIV_Frame_None
        MUIT MUIA_Text_Contents,guit_info5
        MUIT MUIA_Text_PreParse,guit_center
       Endi
       Childi HGroup
        Childi TextObject
         MUIT MUIA_Frame,MUIV_Frame_None
         MUIT MUIA_Text_Contents,guit_info2
         ;MUIT MUIA_Text_PreParse,guit_center
        Endi
        Childi TextObject
         MUIT MUIA_Frame,MUIV_Frame_None
         MUIT MUIA_Text_Contents,guit_info22
         ;MUIT MUIA_Text_PreParse,guit_center
        Endi
       Endi
       Childi TextObject
        MUIT MUIA_Frame,MUIV_Frame_None
        MUIT MUIA_Text_Contents,guit_info6
        MUIT MUIA_Text_PreParse,guit_center
       Endi
       Childi HGroup
        Childi HSpace
        Childi BodychunkObject	;intel outside logo
         MUIT MUIA_Bodychunk_Body,im_intelout
         MUIT MUIA_Bodychunk_Depth,3
         MUIT MUIA_Bodychunk_Compression,1
         ;MUIT MUIA_Bodychunk_Masking,0
         MUIT MUIA_FixWidth,139
         MUIT MUIA_FixHeight,97
         MUIT MUIA_Bitmap_Width,139
         MUIT MUIA_Bitmap_Height,97
         MUIT MUIA_Bitmap_Transparent,0
         MUIT MUIA_Bitmap_Precision,PRECISION_IMAGE
         MUIT MUIA_Bitmap_SourceColors,gui_logocolors
        Endi
        Childi HSpace
       Endi
       Childi TextObject
        MUIT MUIA_Frame,MUIV_Frame_None
        MUIT MUIA_Text_Contents,guit_info8
       Endi
      Endi
     Endi
    Endi

; shell page

    Childi GroupObject
     Childi ListviewObject
      MUIT MUIA_Listview_Input,FALSE
      MUIT MUIA_Listview_List
      ListObject
       MUIT MUIA_Frame,MUIV_Frame_InputList
       MUIT MUIA_List_AutoVisible,TRUE
       MUIT MUIA_List_ConstructHook,MUIV_List_ConstructHookString
       MUIT MUIA_List_DestructHook,MUIV_List_DestructHookString
      Endi
      is gad_shellist(a5)
     Endi
     Childi StringObject
      MUIT MUIA_Frame,MUIV_Frame_String
      MUIT MUIA_ControlChar,13
      MUIT MUIA_CycleChain,1
      MUIT MUIA_String_EditHook,shelledithook
      ;MUIT MUIA_String_LonelyEditHook,TRUE
     Endi
     is gad_shellinput(a5)
    Endi

; mixer page

    Childi HGroup
     Childi GroupObject
      Childi TextObject
       MUIT MUIA_InputMode,MUIV_InputMode_RelVerify
       MUIT MUIA_ShowSelState,FALSE
       MUIT MUIA_Frame,MUIV_Frame_None
       MUIT MUIA_Text_Contents,guit_chnt
       MUIT MUIA_Text_SetMin,FALSE
       MUIT MUIA_Text_PreParse,guit_center
      Endi
      is gad_chname0(a5)
      Childi HGroup
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,50
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_ControlChar,"1"
        MUIT MUIA_CycleChain,1
       Endi
       is gad_s0(a5)
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,50
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_CycleChain,1
       Endi
       is gad_s1(a5)
      Endi
      Childi CycleObject
       MUIT MUIA_Cycle_Entries,guia_mixmod2
       MUIT MUIA_CycleChain,1
      Endi
      is gad_mm0(a5)
     Endi
     Childi HSpace
     Childi GroupObject
      Childi TextObject
       MUIT MUIA_InputMode,MUIV_InputMode_RelVerify
       MUIT MUIA_ShowSelState,FALSE
       MUIT MUIA_Frame,MUIV_Frame_None
       MUIT MUIA_Text_Contents,guit_chnt
       MUIT MUIA_Text_SetMin,FALSE
       MUIT MUIA_Text_PreParse,guit_center
      Endi
      is gad_chname1(a5)
      Childi HGroup
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,50
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_ControlChar,"2"
        MUIT MUIA_CycleChain,1
       Endi
       is gad_s2(a5)
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,50
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_CycleChain,1
       Endi
       is gad_s3(a5)
      Endi
      Childi CycleObject
       MUIT MUIA_Cycle_Entries,guia_mixmode
       MUIT MUIA_CycleChain,1
      Endi
      is gad_mm1(a5)
     Endi
     Childi HSpace
     Childi GroupObject
      Childi TextObject
       MUIT MUIA_InputMode,MUIV_InputMode_RelVerify
       MUIT MUIA_ShowSelState,FALSE
       MUIT MUIA_Frame,MUIV_Frame_None
       MUIT MUIA_Text_Contents,guit_chnt
       MUIT MUIA_Text_SetMin,FALSE
       MUIT MUIA_Text_PreParse,guit_center
      Endi
      is gad_chname2(a5)
      Childi HGroup
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,50
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_ControlChar,"3"
        MUIT MUIA_CycleChain,1
       Endi
       is gad_s4(a5)
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,50
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_CycleChain,1
       Endi
       is gad_s5(a5)
      Endi
      Childi CycleObject
       MUIT MUIA_Cycle_Entries,guia_mixmode
       MUIT MUIA_CycleChain,1
      Endi
      is gad_mm2(a5)
     Endi
     Childi HSpace
     Childi GroupObject
      Childi TextObject
       MUIT MUIA_InputMode,MUIV_InputMode_RelVerify
       MUIT MUIA_ShowSelState,FALSE
       MUIT MUIA_Frame,MUIV_Frame_None
       MUIT MUIA_Text_Contents,guit_chnt
       MUIT MUIA_Text_SetMin,FALSE
       MUIT MUIA_Text_PreParse,guit_center
      Endi
      is gad_chname3(a5)
      Childi HGroup
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,50
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_ControlChar,"4"
        MUIT MUIA_CycleChain,1
       Endi
       is gad_s6(a5)
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,50
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_CycleChain,1
       Endi
       is gad_s7(a5)
      Endi
      Childi CycleObject
       MUIT MUIA_Cycle_Entries,guia_mixmode
       MUIT MUIA_CycleChain,1
      Endi
      is gad_mm3(a5)
     Endi
     Childi HSpace
     Childi GroupObject
      Childi TextObject
       MUIT MUIA_InputMode,MUIV_InputMode_RelVerify
       MUIT MUIA_ShowSelState,FALSE
       MUIT MUIA_Frame,MUIV_Frame_None
       MUIT MUIA_Text_Contents,guit_chnt
       MUIT MUIA_Text_SetMin,FALSE
       MUIT MUIA_Text_PreParse,guit_center
      Endi
      is gad_chname4(a5)
      Childi HGroup
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,50
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_ControlChar,"5"
        MUIT MUIA_CycleChain,1
       Endi
       is gad_s8(a5)
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,50
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_CycleChain,1
       Endi
       is gad_s9(a5)
      Endi
      Childi CycleObject
       MUIT MUIA_Cycle_Entries,guia_mixmode
       MUIT MUIA_CycleChain,1
      Endi
      is gad_mm4(a5)
     Endi
    Endi

; player page

    Childi GroupObject
     Childi ListviewObject
      MUIT MUIA_Listview_MultiSelect,MUIV_Listview_MultiSelect_None
      MUIT MUIA_Listview_DragType,MUIV_Listview_DragType_Immediate
      MUIT MUIA_CycleChain,1
      MUIT MUIA_Listview_List
      ListObject
       MUIT MUIA_Frame,MUIV_Frame_InputList
       MUIT MUIA_List_AutoVisible,TRUE
       MUIT MUIA_List_Format,guip_playform
       MUIT MUIA_List_DragSortable,TRUE
       MUIT MUIA_List_ConstructHook,gui_playconst
       MUIT MUIA_List_DestructHook,gui_playdestr
       MUIT MUIA_List_DisplayHook,gui_playdisp
      Endi
      is gad_playlist(a5)
     Endi
     Childi TextObject		;status
      MUIT MUIA_Frame,MUIV_Frame_Text
      MUIT MUIA_Text_Contents,guit_text
      MUIT MUIA_Text_SetMin,TRUE
      ;MUIT MUIA_Text_SetVMax,FALSE
     Endi
     is gad_playinfo(a5)
     Childi GroupObject
      MUIT MUIA_Group_VertSpacing,0
      Childi MySliderObject	;time slider
       MUIT MUIA_Slider_Min,0
       MUIT MUIA_Slider_Max,100
       MUIT MUIA_Slider_Level,0
       MUIT MUIA_InputMode,MUIV_InputMode_RelVerify
       MUIT MUIA_ShowSelState,FALSE
       MUIT MUIA_My_Form,guip_form
       MUIT MUIA_My_DispHook,gui_plydisphook
       MUIT MUIA_My_Divider,10
       MUIT MUIA_My_Flags,1<<slcf_nodragdiv
       MUIT MUIA_CycleChain,1
      MyEndi
      is gad_playtime(a5)
      Childi HGroup
       MUIT MUIA_Group_Spacing,0
       Childi GroupObject
        MUIT MUIA_Group_Spacing,0
        MUIT MUIA_Group_Columns,2
        Childi MyBodyObject        	;mute/unmute
         MUIT MUIA_My_Im1,im_speaker1
         MUIT MUIA_My_Im2,im_speaker2
         MUIT MUIA_My_InputMode,MUIV_InputMode_Toggle
        MyEndi
        is gad_volreset(a5)
        Childi SliderObject	;volume
         MUIT MUIA_Slider_Min,0
         MUIT MUIA_Slider_Max,100
         MUIT MUIA_Slider_Level,0
         MUIT MUIA_CycleChain,1
        Endi
        is gad_playvol(a5)
        Childi MyImage,im_balance1,im_balance2	;balance reset
        is gad_balreset(a5)
        Childi SliderObject	;balance
         MUIT MUIA_Slider_Min,-50
         MUIT MUIA_Slider_Max,50
         MUIT MUIA_Slider_Level,0
         MUIT MUIA_CycleChain,1
        Endi
        is gad_balance(a5)
       Endi
       Childi RectangleObject
        MUIT MUIA_Rectangle_VBar,TRUE
        MUIT MUIA_FixWidth,8
       Endi
       Childi GroupObject
        MUIT MUIA_Group_Spacing,0
        MUIT MUIA_Group_Columns,2
        Childi MyBodyObject
         MUIT MUIA_My_Im1,im_treble1
         MUIT MUIA_My_Im2,im_treble2
         MUIT MUIA_My_InputMode,MUIV_InputMode_Toggle
        MyEndi
        is gad_trebswitch(a5)
        Childi MySliderObject	;treble
         MUIT MUIA_Slider_Min,-18
         MUIT MUIA_Slider_Max,18
         MUIT MUIA_Slider_Level,0
         MUIT MUIA_My_FormHook,gui_tformhook
         MUIT MUIA_CycleChain,1
        MyEndi
        is gad_playtreble(a5)
        Childi MyBodyObject
         MUIT MUIA_My_Im1,im_bass1
         MUIT MUIA_My_Im2,im_bass2
         MUIT MUIA_My_InputMode,MUIV_InputMode_Toggle
        MyEndi
        is gad_bassswitch(a5)
        Childi MySliderObject	;bass
         MUIT MUIA_Slider_Min,-18
         MUIT MUIA_Slider_Max,18
         MUIT MUIA_Slider_Level,0
         MUIT MUIA_My_FormHook,gui_bformhook
         MUIT MUIA_CycleChain,1
        MyEndi
        is gad_playbass(a5)
       Endi
      Endi
      Childi HGroup
       MUIT MUIA_Group_HorizSpacing,0
       Childi TextObject
        MUIT MUIA_Frame,MUIV_Frame_Text
        MUIT MUIA_Text_Contents,guit_status
       Endi
       is gad_playstatus(a5)
       Childi MyImage,im_rand1,im_rand2
       is gad_random(a5)
       Childi MyBodyObject
        MUIT MUIA_My_Im1,im_intro1
        MUIT MUIA_My_Im2,im_intro2
        MUIT MUIA_My_InputMode,MUIV_InputMode_Toggle
       MyEndi
       is gad_intro(a5)
       Childi MyBodyObject
        MUIT MUIA_My_Im1,im_loop1
        MUIT MUIA_My_Im2,im_loop2
        MUIT MUIA_My_InputMode,MUIV_InputMode_Toggle
       MyEndi
       is gad_loop(a5)
       Childi MyImage,im_clear1,im_clear2
       is gad_clearlist(a5)
       Childi MyImage,im_prev1,im_prev2
       is gad_prev(a5)
       Childi MyImage,im_back1,im_back2
       is gad_backw(a5)
       Childi MyImage,im_forw1,im_forw2
       is gad_forw(a5)
       Childi MyImage,im_next1,im_next2
       is gad_next(a5)
       Childi MyBodyObject
        MUIT MUIA_My_Im1,im_pause1
        MUIT MUIA_My_Im2,im_pause2
        MUIT MUIA_My_InputMode,MUIV_InputMode_Toggle
       MyEndi
       is gad_pause(a5)
       Childi MyImage,im_play1,im_play2
       is gad_play(a5)
       Childi MyImage,im_stop1,im_stop2
       is gad_stop(a5)
       ;Childi MyBodyObject
       ; MUIT MUIA_My_Im1,im_dolby1
       ; MUIT MUIA_My_Im2,im_dolby2
       ; MUIT MUIA_My_InputMode,MUIV_InputMode_Toggle
       ;MyEndi
       Childi MyImage,im_close1,im_close2
       is gad_closefile(a5)
       Childi MyImage,im_edit1,im_edit2
       is gad_tagedit(a5)
       Childi PopaslObject	;save playlist
        MUIT MUIA_Popstring_String
        StringObject
         MUIT MUIA_ShowMe,FALSE
         MUIT MUIA_String_MaxLen,maxpathlen
        Endi
        is gad_savefile(a5)
        MUIT MUIA_Popstring_Button
        MyImage im_rec1,im_rec2
        MUIT MUIA_Popasl_Type,ASL_FileRequest
        MUIT MUIA_Popasl_StopHook,gui_savelhook
        MUIT ASLFR_TitleText,guit_savefile
        MUIT ASLFR_DoSaveMode,TRUE
        MUIT ASLFR_DoPatterns,TRUE
        MUIT ASLFR_InitialPattern,guit_pattxt
        ;MUIT ASLFR_PositiveText,guit_oksave
       Endi
       is gad_savelist(a5)
       Childi MyImage,im_cdda1,im_cdda2
       is gad_cdda(a5)
       Childi PopaslObject	;eject
        MUIT MUIA_Popstring_String
        StringObject
         MUIT MUIA_ShowMe,FALSE
         MUIT MUIA_String_MaxLen,maxpathlen
        Endi
        is gad_playfile(a5)
        MUIT MUIA_Popstring_Button
        MyImage im_eject1,im_eject2
        MUIT MUIA_Popasl_StopHook,gui_ejecthook
        MUIT MUIA_Popasl_Type,ASL_FileRequest
        MUIT ASLFR_TitleText,guit_playfile
        MUIT ASLFR_DoMultiSelect,TRUE
        MUIT ASLFR_DoPatterns,TRUE
        ;MUIT ASLFR_PositiveText,guit_okeject
       Endi
      Endi
     Endi
    Endi

; sampler page

    Childi GroupObject
     Childi HGroup
      ;MUIT MUIA_FrameTitle,guit_control
      ;MUIT MUIA_Frame,MUIV_Frame_Group
      Childi GroupObject
       Childi GroupObject		;input level monitor
        MUIT MUIA_FrameTitle,guit_inmon
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_Group_PageMode,TRUE
        MUIT MUIA_Group_ActivePage,0
        ;MUIT MUIA_Group_Forward,FALSE	;csak OM_SET-ben hatasos!
        MUIT MUIA_InputMode,MUIV_InputMode_RelVerify
        MUIT MUIA_ShowSelState,FALSE
        MUIT MUIA_CycleChain,1
        Childi TextObject
         MUIT MUIA_Text_Contents,guit_mondis
         ;MUIT MUIA_Frame,MUIV_Frame_Text
         MUIT MUIA_Text_SetVMax,FALSE
         MUIT MUIA_ShowSelState,FALSE
         MUIT MUIA_Text_PreParse,guit_center
        Endi
        Childi HGroup
         MUIT MUIA_ShowSelState,FALSE
         Childi HSpace
         Childi LevelmeterObject
          MUIT MUIA_Levelmeter_Label,guit_samc1
          MUIT MUIA_Numeric_Min,0
          MUIT MUIA_Numeric_Max,63
          MUIT MUIA_ShowSelState,FALSE
         Endi
         is gad_leftlev(a5)
         Childi LevelmeterObject
          MUIT MUIA_Levelmeter_Label,guit_samc2
          MUIT MUIA_Numeric_Min,0
          MUIT MUIA_Numeric_Max,63
          MUIT MUIA_ShowSelState,FALSE
         Endi
         is gad_rightlev(a5)
         Childi HSpace
        Endi
        Childi GroupObject
         MUIT MUIA_ShowSelState,FALSE
         Childi GaugeObject
          MUIT MUIA_Frame,MUIV_Frame_Gauge
          MUIT MUIA_Gauge_Horiz,TRUE
          MUIT MUIA_Gauge_InfoText,guit_samc1
          MUIT MUIA_Gauge_Max,63
          MUIT MUIA_ShowSelState,FALSE
         Endi
         is gad_leftlev2(a5)
         Childi GaugeObject
          MUIT MUIA_Frame,MUIV_Frame_Gauge
          MUIT MUIA_Gauge_Horiz,TRUE
          MUIT MUIA_Gauge_InfoText,guit_samc2
          MUIT MUIA_Gauge_Max,63
          MUIT MUIA_ShowSelState,FALSE
         Endi
         is gad_rightlev2(a5)
        Endi
       Endi
       is gad_levelgroup(a5)
       ;Childi VSpace
       Childi GroupObject		;resolution
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_FrameTitle,guit_reso
        Childi VSpace
        Childi MySliderObject
         MUIT MUIA_Slider_Min,0
         MUIT MUIA_Slider_Max,8
         MUIT MUIA_Slider_Level,7
         MUIT MUIA_My_DispHook,guisa_disphook
         MUIT MUIA_My_Form,guip_hzform
         MUIT MUIA_CycleChain,1
        MyEndi
        is gad_sfreq(a5)
        ;Childi VSpace
        Childi CycleObject
         MUIT MUIA_Cycle_Entries,guisa_filetype
         MUIT MUIA_Cycle_Active,4
         MUIT MUIA_CycleChain,1
        Endi
        is gad_sftype(a5)
        Childi VSpace
       Endi
      Endi
      Childi HSpace
      Childi GroupObject		;mixer
       Childi TextObject
        MUIT MUIA_Text_Contents,guit_chn1
        MUIT MUIA_Text_PreParse,guit_center
        MUIT MUIA_Frame,MUIV_Frame_None
       Endi
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,0
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_CycleChain,1
       Endi
       is gad_smix1(a5)
      Endi
      Childi GroupObject
       Childi TextObject
        MUIT MUIA_Text_Contents,guit_chn2
        MUIT MUIA_Text_PreParse,guit_center
        MUIT MUIA_Frame,MUIV_Frame_None
       Endi
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,0
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_CycleChain,1
       Endi
       is gad_smix2(a5)
      Endi
      Childi GroupObject
       Childi TextObject
        MUIT MUIA_Text_Contents,guit_chn3
        MUIT MUIA_Text_PreParse,guit_center
        MUIT MUIA_Frame,MUIV_Frame_None
       Endi
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,0
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_CycleChain,1
       Endi
       is gad_smix3(a5)
      Endi
      Childi GroupObject
       Childi TextObject
        MUIT MUIA_Text_Contents,guit_chn4
        MUIT MUIA_Text_PreParse,guit_center
        MUIT MUIA_Frame,MUIV_Frame_None
       Endi
       Childi SliderObject
        MUIT MUIA_Group_Horiz,FALSE
        MUIT MUIA_Slider_Min,0
        MUIT MUIA_Slider_Max,100
        MUIT MUIA_Slider_Level,0
        MUIT MUIA_Slider_Reverse,TRUE
        MUIT MUIA_CycleChain,1
       Endi
       is gad_smix4(a5)
      Endi
      Childi HSpace
      Childi GroupObject
       Childi GroupObject		;control
        MUIT MUIA_FrameTitle,guit_control
        MUIT MUIA_Frame,MUIV_Frame_Group
        Childi VSpace
        Childi KeyButton,guit_open,"p"
        is gad_sopen(a5)
        Childi KeyButton,guit_close,"c"
        is gad_sclose(a5)
        Childi VSpace
        Childi KeyButton,guit_start,"s"
        is gad_sstart(a5)
        Childi KeyButton,guit_stop,"t"
        is gad_sstop(a5)
        Childi VSpace
       Endi
       Childi GroupObject		;status
        MUIT MUIA_FrameTitle,guit_sstat
        MUIT MUIA_Frame,MUIV_Frame_Group
        ;MUIT MUIA_Group_Spacing,0
        Childi VSpace
        Childi TextObject
         MUIT MUIA_Frame,MUIV_Frame_None
         MUIT MUIA_Text_Contents,guit_status
         ;MUIT MUIA_Text_SetVMax,FALSE
         ;MUIT MUIA_Text_SetMin,FALSE
         MUIT MUIA_Text_PreParse,guit_center
        Endi
        is gad_samstatus(a5)
        Childi VSpace
       Endi
      Endi
     Endi
     ;Childi VSpace
     Childi HGroup		;file
      MUIT MUIA_FrameTitle,guit_file
      MUIT MUIA_Frame,MUIV_Frame_Group
      Childi PopaslObject
       MUIT MUIA_Popstring_String
       StringObject
        MUIT MUIA_Frame,MUIV_Frame_String
        MUIT MUIA_ControlChar,13
        MUIT MUIA_CycleChain,1
       Endi
       is gad_sfname(a5)
       MUIT MUIA_Popstring_Button
       PopButton MUII_PopFile
       is gad_samfilebut(a5)
       MUIT MUIA_Popasl_Type,ASL_FileRequest
       MUIT ASLFR_TitleText,guit_filesel
       MUIT ASLFR_DoSaveMode,TRUE
      Endi
      Childi CycleObject
       MUIT MUIA_Cycle_Entries,guisa_typ16
       MUIT MUIA_Cycle_Active,0
       MUIT MUIA_CycleChain,1
       MUIT MUIA_Weight,20
      Endi
      is gad_ftyp2(a5)
     Endi
    Endi

; options page

    Childi GroupObject
     Childi HGroup
      Childi ListviewObject
       MUIT MUIA_Listview_ScrollerPos,MUIV_Listview_ScrollerPos_Right
       MUIT MUIA_CycleChain,1
       MUIT MUIA_Listview_List
       ListObject
        MUIT MUIA_Frame,MUIV_Frame_InputList
        MUIT MUIA_List_AdjustWidth,TRUE
        MUIT MUIA_List_AutoVisible,TRUE
        MUIT MUIA_List_Active,0;MUIV_List_ActiveTop
        MUIT MUIA_List_SourceArray,guia_regtitles2
        ;MUIT MUIA_List_ConstructHook,MUIV_List_ConstructHookString
        ;MUIT MUIA_List_DestructHook,MUIV_List_DestructHookString
       Endi
       is gad_optlist(a5)
      Endi
      Childi GroupObject
       ;MUIT MUIA_Register_Titles,guia_regtitles2
       ;MUIT MUIA_CycleChain,1
       MUIT MUIA_Group_PageMode,TRUE
       MUIT MUIA_Group_ActivePage,0

  ; general options

       Childi GroupObject
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_FrameTitle,guit_opt1
        Childi VSpace
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt3_1
         Endi
         Childi CycleObject
          MUIT MUIA_Cycle_Entries,gui_cycentries
          MUIT MUIA_CycleChain,1
         Endi
         is gad_debugl(a5)
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt3_3
         Endi
         Childi StringObject
          MUIT MUIA_Frame,MUIV_Frame_String
          MUIT MUIA_String_MaxLen,chnnamelength
          MUIT MUIA_ControlChar,13
          MUIT MUIA_String_AdvanceOnCR,TRUE
          MUIT MUIA_CycleChain,1
         Endi
         is gad_name0(a5)
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt3_4
         Endi
         Childi StringObject
          MUIT MUIA_Frame,MUIV_Frame_String
          MUIT MUIA_String_MaxLen,chnnamelength
          MUIT MUIA_String_AdvanceOnCR,TRUE
          MUIT MUIA_CycleChain,1
         Endi
         is gad_name1(a5)
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt3_5
         Endi
         Childi StringObject
          MUIT MUIA_Frame,MUIV_Frame_String
          MUIT MUIA_String_MaxLen,chnnamelength
          MUIT MUIA_String_AdvanceOnCR,TRUE
          MUIT MUIA_CycleChain,1
         Endi
         is gad_name2(a5)
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt3_6
         Endi
         Childi StringObject
          MUIT MUIA_Frame,MUIV_Frame_String
          MUIT MUIA_String_MaxLen,chnnamelength
          MUIT MUIA_String_AdvanceOnCR,TRUE
          MUIT MUIA_CycleChain,1
         Endi
         is gad_name3(a5)
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt3_7
         Endi
         Childi StringObject
          MUIT MUIA_Frame,MUIV_Frame_String
          MUIT MUIA_String_MaxLen,chnnamelength
          MUIT MUIA_String_AdvanceOnCR,TRUE
          MUIT MUIA_CycleChain,1
         Endi
         is gad_name4(a5)
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt3_10
         Endi
         Childi MySliderObject
          MUIT MUIA_Slider_Min,30
          MUIT MUIA_Slider_Max,200
          MUIT MUIA_My_Form,guip_msform
          MUIT MUIA_My_Divider,10
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_guiquantum(a5)
        Endi
        Childi VSpace
       Endi

  ; vss options

       Childi GroupObject
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_FrameTitle,guit_opt2
        MUIT MUIA_Group_Columns,2
        Childi VSpace
        Childi VSpace
        Childi TextObject		;pri
         MUIT MUIA_Frame,MUIV_Frame_None
         MUIT MUIA_Text_Contents,guit_opt1_3
        Endi
        Childi SliderObject
         MUIT MUIA_Slider_Min,-10
         MUIT MUIA_Slider_Max,30
         MUIT MUIA_CycleChain,1
        Endi
        is gad_llpri(a5)
        Childi TextObject		;pri
         MUIT MUIA_Frame,MUIV_Frame_None
         MUIT MUIA_Text_Contents,guit_opt1_4
        Endi
        Childi SliderObject
         MUIT MUIA_Slider_Min,-10
         MUIT MUIA_Slider_Max,30
         MUIT MUIA_CycleChain,1
        Endi
        is gad_spri(a5)
        Childi TextObject		;quantum
         MUIT MUIA_Frame,MUIV_Frame_None
         MUIT MUIA_Text_Contents,guit_opt1_2
        Endi
        Childi MySliderObject
         MUIT MUIA_Slider_Min,1
         MUIT MUIA_Slider_Max,100
         MUIT MUIA_My_Form,guip_msform
         MUIT MUIA_CycleChain,1
        MyEndi
        is gad_quantum(a5)
        Childi TextObject		;de-emphasis
         MUIT MUIA_Frame,MUIV_Frame_None
         MUIT MUIA_Text_Contents,guit_opt1_14
        Endi
        Childi HGroup
         Childi CheckMark,FALSE
         is gad_endeemp(a5)
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_15
          MUIT MUIA_Text_PreParse,guit_center
         Endi
        Endi
        Childi VSpace
        Childi VSpace
       Endi

  ; ahi options

       Childi GroupObject
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_FrameTitle,guit_opt3
        Childi VSpace
        Childi HGroup		;enable AHI
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_9
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_ahiena(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_1
         Endi
         Childi MySliderObject
          MUIT MUIA_Slider_Min,10
          MUIT MUIA_Slider_Max,500
          MUIT MUIA_My_Form,guip_msform
          MUIT MUIA_My_Divider,10
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_ahibuf(a5)
        Endi
        Childi HGroup		;mpeg support
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_mpsupport
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_ahimpsupp(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup		;accept volume
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_ahivol
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_ahivol(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup		;mixing
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_5
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_ahiint(a5)
          Childi HSpace
         Endi
        Endi
        Childi VSpace
       Endi

  ; melody options

       Childi GroupObject
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_FrameTitle,guit_opt4
        Childi VSpace
        Childi HGroup		;enable melody
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_10
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_melena(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_1
         Endi
         Childi MySliderObject
          MUIT MUIA_Slider_Min,4
          MUIT MUIA_Slider_Max,32
          MUIT MUIA_My_Form,guip_kform
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_melbuf(a5)
        Endi
        Childi HGroup		;delayed flush
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_7
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_delflush(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup		;accept volume
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_melvol
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_melvol(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup		;play int
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_6
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_melint(a5)
          Childi HSpace
         Endi
        Endi
        Childi VSpace
       Endi

  ; mpega options

       Childi GroupObject
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_FrameTitle,guit_opt5
        Childi VSpace
        Childi HGroup		;enable mpega
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_11
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_mpaena(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt2_1
         Endi
         Childi MySliderObject
          MUIT MUIA_Slider_Min,8
          MUIT MUIA_Slider_Max,256
          MUIT MUIA_My_Form,guip_kform
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_mpbufsize(a5)
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt2_3
         Endi
         Childi MySliderObject
          MUIT MUIA_Slider_Min,-1000
          MUIT MUIA_Slider_Max,2000
          MUIT MUIA_My_Form,guip_msform
          MUIT MUIA_My_Divider,20
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_mptimecorr(a5)
        Endi
        Childi HGroup		;load
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_12
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_mpload(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup		;activate
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_8
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_mpassw(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup		;load in ram
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_13
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_mploadram(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup		;accept volume
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_mpvol
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_mpavol(a5)
          Childi TextObject
           MUIT MUIA_Frame,MUIV_Frame_None
           MUIT MUIA_Text_Contents,guit_mpprayer
           MUIT MUIA_Text_PreParse,guit_center
          Endi
         Endi
        Endi
        Childi HGroup		;play int
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_opt1_6
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_mpaint(a5)
          Childi HSpace
         Endi
        Endi
        Childi VSpace
       Endi

  ; player options

       Childi HGroup
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_FrameTitle,guit_opt7
        Childi HSpace
        Childi GroupObject
         Childi VSpace

         bsr.w guim_notimpl

         Childi VSpace
        Endi
        Childi HSpace
       Endi

  ; sampler options

       Childi GroupObject
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_FrameTitle,guit_opt6
        Childi VSpace
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_sabuf
         Endi
         Childi MySliderObject
          MUIT MUIA_Slider_Min,8
          MUIT MUIA_Slider_Max,256
          MUIT MUIA_My_Form,guip_kform
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_sbuf(a5)
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_declev
         Endi
         Childi SliderObject
          MUIT MUIA_Slider_Min,0
          MUIT MUIA_Slider_Max,16
          MUIT MUIA_Slider_Level,4
          MUIT MUIA_CycleChain,1
         Endi
         is gad_samdeclev(a5)
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_levhist
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_histogram(a5)
          Childi HSpace
         Endi
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_levmode
         Endi
         Childi CycleObject
          MUIT MUIA_Cycle_Entries,guisa_levmode
          MUIT MUIA_Cycle_Active,0
          MUIT MUIA_CycleChain,1
         Endi
         is gad_levmode(a5)
        Endi
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_samappend
         Endi
         Childi HGroup
          Childi CheckMark,FALSE
          is gad_samappend(a5)
          Childi HSpace
         Endi
        Endi
        Childi VSpace
       Endi

; mp3 dsp options

       Childi GroupObject
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_FrameTitle,guit_opt8
        Childi VSpace
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_mp3
         Endi
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_mp4
         Endi
        Endi
        Childi HGroup
         Childi MySliderObject
          MUIT MUIA_Slider_Min,mp3dsp_trebmin
          MUIT MUIA_Slider_Max,mp3dsp_trebmax
          MUIT MUIA_My_Form,guip_hzform
          MUIT MUIA_My_Divider,freqgadstep
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_ms3(a5)
         Childi MySliderObject
          MUIT MUIA_Slider_Min,-18
          MUIT MUIA_Slider_Max,18
          MUIT MUIA_My_FormHook,gui_tbformhook
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_ms4(a5)
        Endi
        Childi VSpace
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_mp1
         Endi
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_mp2
         Endi
        Endi
        Childi HGroup
         Childi MySliderObject
          MUIT MUIA_Slider_Min,mp3dsp_bassmin
          MUIT MUIA_Slider_Max,mp3dsp_bassmax
          MUIT MUIA_My_Form,guip_hzform
          MUIT MUIA_My_Divider,freqgadstep
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_ms1(a5)
         Childi MySliderObject
          MUIT MUIA_Slider_Min,-18
          MUIT MUIA_Slider_Max,18
          MUIT MUIA_My_FormHook,gui_tbformhook
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_ms2(a5)
        Endi
        Childi VSpace
        Childi HGroup
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_mp5
         Endi
         Childi TextObject
          MUIT MUIA_Frame,MUIV_Frame_None
          MUIT MUIA_Text_Contents,guit_mp6
         Endi
        Endi
        Childi HGroup
         Childi MySliderObject
          MUIT MUIA_Slider_Min,0
          MUIT MUIA_Slider_Max,18
          MUIT MUIA_My_Form,guip_dbmform
          MUIT MUIA_CycleChain,1
         MyEndi
         is gad_ms5(a5)
         Childi SliderObject
          MUIT MUIA_Slider_Min,-50
          MUIT MUIA_Slider_Max,50
          MUIT MUIA_CycleChain,1
         Endi
         is gad_ms6(a5)
        Endi
        Childi VSpace
       Endi

  ; infra

       Childi HGroup
        MUIT MUIA_Frame,MUIV_Frame_Group
        MUIT MUIA_FrameTitle,guit_opt9
        Childi HSpace
        Childi GroupObject
         Childi VSpace

         bsr.w guim_notimpl

         Childi VSpace
        Endi
        Childi HSpace
       Endi

  ; expansion

       Childi RegisterObject
        ;MUIT MUIA_Disabled,TRUE
        MUIT MUIA_Register_Titles,guia_regtitles3
        MUIT MUIA_CycleChain,1
        Childi GroupObject		;slot 1
         bsr.w guim_notimpl
        Endi
        Childi GroupObject		;slot 2
         bsr.w guim_notimpl
        Endi
        Childi GroupObject		;slot 3
         bsr.w guim_notimpl
        Endi
        Childi GroupObject		;slot 4
         bsr.w guim_notimpl
        Endi
       Endi

  ; end of option pages

      Endi
      is gad_optreg(a5)
     Endi

  ; buttons

     Childi HGroup
      Childi KeyButton,guit_save,"s"
      is gad_saveconf(a5)
      Childi HSpace
      Childi KeyButton,guit_default,"d"
      is gad_default(a5)
      Childi HSpace
      Childi KeyButton,guit_unload,"u"
      is gad_unload(a5)
      Childi HSpace
      Childi KeyButton,guit_hide,"h"
      is gad_hide(a5)
     Endi
    Endi

; end of pages

   Endi
   is gui_register(a5)
  Endi
  is gui_window(a5)
  MUIT MUIA_Application_DropObject
  MUIT2 gui_window(a5)
 Endi
 is gui_app(a5)

	tst.l	d0
	beq.w	killgui


; Close gadget = hide window
 DoMethod gui_window(a5),#MUIM_Notify,#MUIA_Window_CloseRequest,#TRUE,gui_window(a5),#3,#MUIM_Set,#MUIA_Window_Open,#FALSE

; Info page (about MUI gadget)
 DoMethod gad_aboutm(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gui_app(a5),#2,#MUIM_Application_AboutMUI,gui_window(a5)

; Options listview a groupot beallitja
 DoMethod gad_optlist(a5),#MUIM_Notify,#MUIA_List_Active,#MUIV_EveryTime,gad_optreg(a5),#3,#MUIM_Set,#MUIA_Group_ActivePage,#MUIV_TriggerValue

; SaveConf/Unload/Hide buttons
 DoMethod gad_unload(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gui_app(a5),#2,#MUIM_Application_ReturnID,#MUIV_Application_ReturnID_Quit
 DoMethod gad_saveconf(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_saveconf(a5),#2,#MUIM_CallHook,#saveconfhook
 DoMethod gad_hide(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gui_window(a5),#3,#MUIM_Set,#MUIA_Window_Open,#FALSE

; Channel name hooks
 DoMethod gad_name0(a5),#MUIM_Notify,#MUIA_String_Acknowledge,#MUIV_EveryTime,gad_name0(a5),#4,#MUIM_CallHook,#namehook,#0,#MUIV_TriggerValue
 DoMethod gad_name1(a5),#MUIM_Notify,#MUIA_String_Acknowledge,#MUIV_EveryTime,gad_name1(a5),#4,#MUIM_CallHook,#namehook,#1,#MUIV_TriggerValue
 DoMethod gad_name2(a5),#MUIM_Notify,#MUIA_String_Acknowledge,#MUIV_EveryTime,gad_name2(a5),#4,#MUIM_CallHook,#namehook,#2,#MUIV_TriggerValue
 DoMethod gad_name3(a5),#MUIM_Notify,#MUIA_String_Acknowledge,#MUIV_EveryTime,gad_name3(a5),#4,#MUIM_CallHook,#namehook,#3,#MUIV_TriggerValue
 DoMethod gad_name4(a5),#MUIM_Notify,#MUIA_String_Acknowledge,#MUIV_EveryTime,gad_name4(a5),#4,#MUIM_CallHook,#namehook,#4,#MUIV_TriggerValue

; Debuglevel button
 DoMethod gad_debugl(a5),#MUIM_Notify,#MUIA_Cycle_Active,#MUIV_EveryTime,gad_debugl(a5),#3,#MUIM_CallHook,#debughook,#MUIV_TriggerValue

; AHI & MPEG options
 DoMethod gad_ahibuf(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ahibuf(a5),#4,#MUIM_CallHook,#gui_opthook,#0,#MUIV_TriggerValue
 DoMethod gad_melbuf(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_melbuf(a5),#4,#MUIM_CallHook,#gui_opthook,#7,#MUIV_TriggerValue
 DoMethod gad_quantum(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_quantum(a5),#4,#MUIM_CallHook,#gui_opthook,#4,#MUIV_TriggerValue
 DoMethod gad_llpri(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_llpri(a5),#4,#MUIM_CallHook,#gui_opthook,#5,#MUIV_TriggerValue
 DoMethod gad_spri(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_spri(a5),#4,#MUIM_CallHook,#gui_opthook,#6,#MUIV_TriggerValue
 DoMethod gad_mpbufsize(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_mpbufsize(a5),#4,#MUIM_CallHook,#gui_opthook,#1,#MUIV_TriggerValue
 DoMethod gad_mptimecorr(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_mptimecorr(a5),#4,#MUIM_CallHook,#gui_opthook,#3,#MUIV_TriggerValue

; Play interrupt/volume accept checkboxes
 DoMethod gad_ahiint(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_ahiint(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_ahiint,#MUIV_TriggerValue
 DoMethod gad_melint(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_melint(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_melint,#MUIV_TriggerValue
 DoMethod gad_mpaint(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_mpaint(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_mpaint,#MUIV_TriggerValue
 DoMethod gad_ahivol(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_ahivol(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_ahivol,#MUIV_TriggerValue
 DoMethod gad_melvol(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_melvol(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_melvol,#MUIV_TriggerValue
 DoMethod gad_mpavol(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_mpavol(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_mpavol,#MUIV_TriggerValue
 DoMethod gad_ahiena(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_ahiena(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_ahiena,#MUIV_TriggerValue
 DoMethod gad_melena(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_melena(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_melena,#MUIV_TriggerValue
 DoMethod gad_mpaena(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_mpaena(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_mpaena,#MUIV_TriggerValue
 DoMethod gad_mpassw(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_mpassw(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_nostrsw,#MUIV_TriggerValue
 DoMethod gad_mpload(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_mpload(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_mploadlib,#MUIV_TriggerValue
 DoMethod gad_mploadram(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_mploadram(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_mpaloadram,#MUIV_TriggerValue
 DoMethod gad_histogram(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_histogram(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_levelhist,#MUIV_TriggerValue
 DoMethod gad_endeemp(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_endeemp(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_deemphasis,#MUIV_TriggerValue
 DoMethod gad_samappend(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_samappend(a5),#4,#MUIM_CallHook,#gui_markhook,#cflg_samappend,#MUIV_TriggerValue

; Mixer slider hooks
 DoMethod gad_s0(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_s0(a5),#4,#MUIM_CallHook,#gui_mixhook,#8,#MUIV_TriggerValue
 DoMethod gad_s1(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_s1(a5),#4,#MUIM_CallHook,#gui_mixhook,#9,#MUIV_TriggerValue
 DoMethod gad_s2(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_s2(a5),#4,#MUIM_CallHook,#gui_mixhook,#0,#MUIV_TriggerValue
 DoMethod gad_s3(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_s3(a5),#4,#MUIM_CallHook,#gui_mixhook,#1,#MUIV_TriggerValue
 DoMethod gad_s4(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_s4(a5),#4,#MUIM_CallHook,#gui_mixhook,#2,#MUIV_TriggerValue
 DoMethod gad_s5(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_s5(a5),#4,#MUIM_CallHook,#gui_mixhook,#3,#MUIV_TriggerValue
 DoMethod gad_s6(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_s6(a5),#4,#MUIM_CallHook,#gui_mixhook,#4,#MUIV_TriggerValue
 DoMethod gad_s7(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_s7(a5),#4,#MUIM_CallHook,#gui_mixhook,#5,#MUIV_TriggerValue
 DoMethod gad_s8(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_s8(a5),#4,#MUIM_CallHook,#gui_mixhook,#6,#MUIV_TriggerValue
 DoMethod gad_s9(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_s9(a5),#4,#MUIM_CallHook,#gui_mixhook,#7,#MUIV_TriggerValue
; Mixer mode select hooks
 DoMethod gad_mm0(a5),#MUIM_Notify,#MUIA_Cycle_Active,#MUIV_EveryTime,gad_mm0(a5),#4,#MUIM_CallHook,#gui_modehook,#4,#MUIV_TriggerValue
 DoMethod gad_mm1(a5),#MUIM_Notify,#MUIA_Cycle_Active,#MUIV_EveryTime,gad_mm1(a5),#4,#MUIM_CallHook,#gui_modehook,#0,#MUIV_TriggerValue
 DoMethod gad_mm2(a5),#MUIM_Notify,#MUIA_Cycle_Active,#MUIV_EveryTime,gad_mm2(a5),#4,#MUIM_CallHook,#gui_modehook,#1,#MUIV_TriggerValue
 DoMethod gad_mm3(a5),#MUIM_Notify,#MUIA_Cycle_Active,#MUIV_EveryTime,gad_mm3(a5),#4,#MUIM_CallHook,#gui_modehook,#2,#MUIV_TriggerValue
 DoMethod gad_mm4(a5),#MUIM_Notify,#MUIA_Cycle_Active,#MUIV_EveryTime,gad_mm4(a5),#4,#MUIM_CallHook,#gui_modehook,#3,#MUIV_TriggerValue
; Channel name = mute
 DoMethod gad_chname0(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_mm0(a5),#3,#MUIM_Set,#MUIA_Cycle_Active,#2
 DoMethod gad_chname1(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_mm1(a5),#3,#MUIM_Set,#MUIA_Cycle_Active,#3
 DoMethod gad_chname2(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_mm2(a5),#3,#MUIM_Set,#MUIA_Cycle_Active,#3
 DoMethod gad_chname3(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_mm3(a5),#3,#MUIM_Set,#MUIA_Cycle_Active,#3
 DoMethod gad_chname4(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_mm4(a5),#3,#MUIM_Set,#MUIA_Cycle_Active,#3

; Mpeg sliders (options page)
 DoMethod gad_ms1(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ms1(a5),#4,#MUIM_CallHook,#gui_mpeghook,#0,#MUIV_TriggerValue
 DoMethod gad_ms2(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ms2(a5),#4,#MUIM_CallHook,#gui_mpeghook,#2,#MUIV_TriggerValue
 DoMethod gad_ms3(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ms3(a5),#4,#MUIM_CallHook,#gui_mpeghook,#4,#MUIV_TriggerValue
 DoMethod gad_ms4(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ms4(a5),#4,#MUIM_CallHook,#gui_mpeghook,#6,#MUIV_TriggerValue
 DoMethod gad_ms5(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ms5(a5),#4,#MUIM_CallHook,#gui_mpeghook,#8,#MUIV_TriggerValue
 DoMethod gad_ms6(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ms6(a5),#4,#MUIM_CallHook,#gui_mpeghook,#10,#MUIV_TriggerValue

; Shell page
 DoMethod gad_shellinput(a5),#MUIM_Notify,#MUIA_String_Acknowledge,#MUIV_EveryTime,gad_shellinput(a5),#3,#MUIM_CallHook,#shellhook,#MUIV_TriggerValue
 DoMethod gui_window(a5),#MUIM_Notify,#MUIA_Window_Activate,#TRUE,gui_window(a5),#3,#MUIM_Set,#MUIA_Window_ActiveObject,gad_shellinput(a5)

; Main register pages
 DoMethod gui_register(a5),#MUIM_Notify,#MUIA_Group_ActivePage,#MUIV_EveryTime,gui_register(a5),#3,#MUIM_CallHook,#gui_reghook,#MUIV_TriggerValue

; Window open/iconify
 DoMethod gui_window(a5),#MUIM_Notify,#MUIA_Window_Open,#MUIV_EveryTime,gui_window(a5),#3,#MUIM_CallHook,#gui_winhook,#MUIV_TriggerValue
 DoMethod gui_app(a5),#MUIM_Notify,#MUIA_Application_Iconified,#MUIV_EveryTime,gui_app(a5),#3,#MUIM_CallHook,#gui_iconhook,#MUIV_TriggerValue

; Sampler page
 DoMethod gad_sopen(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_sopen(a5),#2,#MUIM_CallHook,#gad_sopenhook
 DoMethod gad_sclose(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_sclose(a5),#2,#MUIM_CallHook,#gad_sclosehook
 DoMethod gad_sstart(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_sstart(a5),#2,#MUIM_CallHook,#gad_sstarthook
 DoMethod gad_sstop(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_sstop(a5),#2,#MUIM_CallHook,#gad_sstophook
 DoMethod gad_samdeclev(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_samdeclev(a5),#3,#MUIM_CallHook,#gui_declhook,#MUIV_TriggerValue
 DoMethod gad_levelgroup(a5),#MUIM_Notify,#MUIA_Pressed,#TRUE,gad_levelgroup(a5),#2,#MUIM_CallHook,#gui_levgrphook
 DoMethod gad_sftype(a5),#MUIM_Notify,#MUIA_Cycle_Active,#MUIV_EveryTime,gad_sftype(a5),#3,#MUIM_CallHook,#gui_samtyphook,#MUIV_TriggerValue

 DoMethod gad_smix1(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_smix1(a5),#4,#MUIM_CallHook,#gui_smixhook,#MUIV_TriggerValue,#0
 DoMethod gad_smix2(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_smix2(a5),#4,#MUIM_CallHook,#gui_smixhook,#MUIV_TriggerValue,#2
 DoMethod gad_smix3(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_smix3(a5),#4,#MUIM_CallHook,#gui_smixhook,#MUIV_TriggerValue,#4
 DoMethod gad_smix4(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_smix4(a5),#4,#MUIM_CallHook,#gui_smixhook,#MUIV_TriggerValue,#6
	lea	conftmp+conf_sambufsize(a5),a3
 DoMethod gad_sbuf(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_sbuf(a5),#3,#MUIM_WriteLong,#MUIV_TriggerValue,a3
 DoMethod gad_guiquantum(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_guiquantum(a5),#3,#MUIM_CallHook,#gui_guiqhook,#MUIV_TriggerValue
 DoMethod gad_levmode(a5),#MUIM_Notify,#MUIA_Cycle_Active,#MUIV_EveryTime,gad_levmode(a5),#3,#MUIM_CallHook,#gui_levdivhook,#MUIV_TriggerValue

; Player page
 DoMethod gad_play(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_play(a5),#2,#MUIM_CallHook,#gui_playhook
 DoMethod gad_stop(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_stop(a5),#2,#MUIM_CallHook,#gui_stophook
 DoMethod gad_playlist(a5),#MUIM_Notify,#MUIA_List_Active,#MUIV_EveryTime,gad_playlist(a5),#3,#MUIM_CallHook,#gui_playselhook,#MUIV_TriggerValue
 DoMethod gad_prev(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_prev(a5),#2,#MUIM_CallHook,#gui_prevhook
 DoMethod gad_next(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_next(a5),#2,#MUIM_CallHook,#gui_nexthook
 DoMethod gad_loop(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_loop(a5),#3,#MUIM_CallHook,#gui_loophook,#MUIV_TriggerValue
 DoMethod gad_playtime(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_playtime(a5),#3,#MUIM_CallHook,#play_seekhook,#MUIV_TriggerValue
 ;DoMethod gad_playtime(a5),#MUIM_Notify,#MUIA_Prop_Release,#MUIV_EveryTime,gad_playtime(a5),#3,#MUIM_CallHook,#play_seekhook,#MUIV_TriggerValue
 DoMethod gad_pause(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_pause(a5),#3,#MUIM_CallHook,#play_pausehook,#MUIV_TriggerValue
 DoMethod gad_forw(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_forw(a5),#3,#MUIM_CallHook,#play_rseekhook,#seekbytes
 DoMethod gad_backw(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_backw(a5),#3,#MUIM_CallHook,#play_rseekhook,#-seekbytes
 DoMethod gad_clearlist(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_playlist(a5),#2,#MUIM_CallHook,#gui_clearhook
 DoMethod gad_closefile(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_playlist(a5),#2,#MUIM_CallHook,#gui_closehook
; volume/balance/bass/treble adjust
 DoMethod gad_balreset(a5),#MUIM_Notify,#MUIA_Pressed,#FALSE,gad_balance(a5),#3,#MUIM_Set,#MUIA_Slider_Level,#0
 DoMethod gad_volreset(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_volreset(a5),#3,#MUIM_CallHook,#play_vreshook,#MUIV_TriggerValue
 DoMethod gad_balance(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_balance(a5),#3,#MUIM_CallHook,#play_balhook,#MUIV_TriggerValue
 DoMethod gad_playvol(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_playvol(a5),#3,#MUIM_CallHook,#play_mixhook,#MUIV_TriggerValue
 DoMethod gad_playtreble(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_playtreble(a5),#3,#MUIM_CallHook,#play_trebhook,#MUIV_TriggerValue
 DoMethod gad_playbass(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_playbass(a5),#3,#MUIM_CallHook,#play_basshook,#MUIV_TriggerValue
 DoMethod gad_trebswitch(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_trebswitch(a5),#3,#MUIM_CallHook,#play_trebswhook,#MUIV_TriggerValue
 DoMethod gad_bassswitch(a5),#MUIM_Notify,#MUIA_Selected,#MUIV_EveryTime,gad_bassswitch(a5),#3,#MUIM_CallHook,#play_bassswhook,#MUIV_TriggerValue
 DoMethod gad_ms4(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ms4(a5),#3,#MUIM_CallHook,#play_trebhook2,#MUIV_TriggerValue
 DoMethod gad_ms2(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ms2(a5),#3,#MUIM_CallHook,#play_basshook2,#MUIV_TriggerValue
 DoMethod gad_ms3(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ms3(a5),#3,#MUIM_CallHook,#play_trebhook3,#MUIV_TriggerValue
 DoMethod gad_ms1(a5),#MUIM_Notify,#MUIA_Slider_Level,#MUIV_EveryTime,gad_ms1(a5),#3,#MUIM_CallHook,#play_basshook3,#MUIV_TriggerValue

; AppMessages -> insert files into the playlist
 DoMethod gui_window(a5),#MUIM_Notify,#MUIA_AppMessage,#MUIV_EveryTime,gui_window(a5),#3,#MUIM_CallHook,#gui_apphook,#MUIV_TriggerValue


muiend	lea	i_welc,a0	;print welcome txt
	pea	(a5)
	pea	Main
	move.l	sp,a1
	bsr.l	printLine
	addq.l	#8,sp
	move.l	gui_app(a5),d0
	rts

popup	seti	gui_window(a5),#MUIA_Window_Open,#TRUE
	seti	gui_app(a5),#MUIA_Application_Iconified,#FALSE
	DoMethod	gui_window(a5),#MUIM_Window_ToFront
	DoMethod	gui_window(a5),#MUIM_Window_ScreenToFront
	rts

hide	seti	gui_window(a5),#MUIA_Window_Open,#FALSE
	rts

killgui	move.l	muibase(a5),a6
	move.l	gui_app(a5),d0
	beq.b	.1
	move.l	d0,a0
	jsr	_LVOMUI_DisposeObject(a6)
	clr.l	gui_app(a5)
.1	move.l	gui_myslidermcc(a5),d0
	beq.b	.4
	move.l	d0,a0
	jsr	_LVOMUI_DeleteCustom(a6)
	clr.l	gui_myslidermcc(a5)
.4	move.l	gui_mybodymcc(a5),d0
	beq.b	.5
	move.l	d0,a0
	jsr	_LVOMUI_DeleteCustom(a6)
	clr.l	gui_mybodymcc(a5)
.5	move.l	icobase(a5),a6
	move.l	gui_icon(a5),d0
	beq.b	.2
	move.l	d0,a0
	jsr	_LVOFreeDiskObject(a6)
.2	move.l	excbase(a5),a6
	move.l	gui_tagspace(a5),d0
	beq.b	.0
	move.l	d0,a1
	jsr	_LVOFreeVec(a6)
	clr.l	gui_tagspace(a5)
.0	rts

TAG_SPACE_LONG	dc.l	TAG_SPACE

; gfx data - logos

gfxstart

im_vsslogo	incbin	VSS:Sources/gfx/VSSlogo.body
im_muilogo	incbin	VSS:Sources/gfx/MUIlogo.body
im_intelout	incbin	VSS:Sources/gfx/Intelout.body
im_amiga	incbin	VSS:Sources/gfx/Amiga.body
im_work	incbin	VSS:Sources/gfx/Work.body
im_asmpro	incbin	VSS:Sources/gfx/AsmPro.body

; button images

im_clear1	incbin	VSS:Sources/gfx/Clear1.body
im_clear2	incbin	VSS:Sources/gfx/Clear2.body
im_close1	incbin	VSS:Sources/gfx/Close1.body
im_close2	incbin	VSS:Sources/gfx/Close2.body
im_edit1	incbin	VSS:Sources/gfx/Edit1.body
im_edit2	incbin	VSS:Sources/gfx/Edit2.body

im_play1	incbin	VSS:Sources/gfx/Play1.body
im_play2	incbin	VSS:Sources/gfx/Play2.body
im_stop1	incbin	VSS:Sources/gfx/Stop1.body
im_stop2	incbin	VSS:Sources/gfx/Stop2.body
im_cdda1	incbin	VSS:Sources/gfx/Cdda1.body
im_cdda2	incbin	VSS:Sources/gfx/Cdda2.body
im_eject1	incbin	VSS:Sources/gfx/Eject1.body
im_eject2	incbin	VSS:Sources/gfx/Eject2.body
im_rec1	incbin	VSS:Sources/gfx/Record1.body
im_rec2	incbin	VSS:Sources/gfx/Record2.body
;im_expand1	incbin	VSS:Sources/gfx/Expand1.body
;im_expand2	incbin	VSS:Sources/gfx/Expand2.body
;im_dolby1	incbin	VSS:Sources/gfx/Dolby1.body
;im_dolby2	incbin	VSS:Sources/gfx/Dolby2.body
im_pause1	incbin	VSS:Sources/gfx/Pause1.body
im_pause2	incbin	VSS:Sources/gfx/Pause2.body
im_next1	incbin	VSS:Sources/gfx/Next1.body
im_next2	incbin	VSS:Sources/gfx/Next2.body
im_forw1	incbin	VSS:Sources/gfx/Forward1.body
im_forw2	incbin	VSS:Sources/gfx/Forward2.body
im_back1	incbin	VSS:Sources/gfx/Backward1.body
im_back2	incbin	VSS:Sources/gfx/Backward2.body
im_prev1	incbin	VSS:Sources/gfx/Prev1.body
im_prev2	incbin	VSS:Sources/gfx/Prev2.body
;im_opt1	incbin	VSS:Sources/gfx/Option1.body
;im_opt2	incbin	VSS:Sources/gfx/Option2.body
im_rand1	incbin	VSS:Sources/gfx/Random1.body
im_rand2	incbin	VSS:Sources/gfx/Random2.body
im_intro1	incbin	VSS:Sources/gfx/Intro1.body
im_intro2	incbin	VSS:Sources/gfx/Intro2.body
im_loop1	incbin	VSS:Sources/gfx/Loop1.body
im_loop2	incbin	VSS:Sources/gfx/Loop2.body

im_speaker1	incbin	VSS:Sources/gfx/Speaker1.body
im_speaker2	incbin	VSS:Sources/gfx/Speaker2.body
im_balance1	incbin	VSS:Sources/gfx/Balance1.body
im_balance2	incbin	VSS:Sources/gfx/Balance2.body
im_bass1	incbin	VSS:Sources/gfx/Bass1.body
im_bass2	incbin	VSS:Sources/gfx/Bass2.body
im_treble1	incbin	VSS:Sources/gfx/Treble1.body
im_treble2	incbin	VSS:Sources/gfx/Treble2.body

gfxend	even


; Bump build counter

	auto	zf VSS:Sources/_build.cnt\
	auto	wb VSS:Sources/_build.cnt\
	auto	Start\
	auto	Start+buc\

; Update version

verc	equ	vssver*1000+vssrev

	auto	zf VSS:Sources/_version.cnt\
	auto	wb VSS:Sources/_version.cnt\
	auto	Start\
	auto	Start+verc\

; Automatikusan elmenti a forrast es az objectet

	auto	zf VSS:Sources/vss.asm\
	auto	w VSS:Sources/vss.asm\

	printt	'-----------------------------------------------------'
	printt	'MUI tag memory:'
	printv	TAG_SPACE
	printt	'Variable area:'
	printv	var_size
	printt	''
	printt	'MUI macro section:'
	printv	muiend-muistart
	printt	'GFX binary includes:'
	printv	gfxend-gfxstart
	printt	'Text bytes:'
	printv	txtsize
	printt	''

	IFD	mpusemagic
	printt	'Stream magic number check       ENABLED'
	;auto	zf VSS:VSSm\
	;auto	wo VSS:VSSm\
	auto	zf VSS:VSS\
	auto	wo VSS:VSS\
	ELSE
	auto	zf VSS:VSS\
	auto	wo VSS:VSS\
	printt	'Stream magic number check       DISABLED'
	ENDIF

	IFD	checkptr
	printt	'Ptr2 read check                 ENABLED'
	ELSE
	printt	'Ptr2 read check                 DISABLED'
	ENDIF

	IFD	makecolor
	printt	'BG color timing monitor         ENABLED'
	ELSE
	printt	'BG color timing monitor         DISABLED'
	ENDIF

	IFD	quantizeneg
	printt	'Quantize neg histogram          ENABLED'
	ELSE
	printt	'Quantize neg histogram          DISABLED'
	ENDIF

	IFD	melodyport2
	printt	'Process QUICK melody requests   ENABLED'
	ELSE
	printt	'Process QUICK melody requests   DISABLED'
	ENDIF

	IFD	dragsetcurdir
	printt	'Drag`n drop sets current dir    ENABLED'
	ELSE
	printt	'Drag`n drop sets current dir    DISABLED'
	ENDIF

	IFD	playlistquiet
	printt	'Quiet playlist                  ENABLED'
	ELSE
	printt	'Quiet playlist                  DISABLED'
	ENDIF

	printt	'-----------------------------------------------------'

	txtlast

; uj flag bevezetese
; - cflg_, gad_, guit_ definialas
; - checkmark gadget a makrok koze
; - domethod notify (gui_markhook)
; - default ertek beallitas checkboxupd

; Hibasan kezelt fajlok

; SubBassMonsters/Ra se rants!
; Agasvar csillagasz tabor
; Roy&Adam

; kn0wn bugz:

; player neha file elejen leall, neha EOF-ot jelez, altalaban nem
; pause modban fajlt valtva a pause gomb benyomva marad, de a lejatszas ujraindul
; mpega emulacio nem zarja be a vss.library-t
; melody fagy
; lib/dev enable
; shell-ben nem muxik a ?help ha /A argumentum is van a template-ben
; ha a sampler inditasa kozben hiba tortenik, a codec levelmeter nem indul ujra
; channel nev gadgeteket le kell enterezni, kulonben nincs hatasa
; vss.lib-et valami nem zarja be, emiatt neha nem lehet kilepni

; fakecnt opcionalisan a valodi erteket kene hogy adja vissza
; shell mixer parameterezese ertelmesebb kene hogy legyen
; kill ahi/melody/mpega/egyeb
; config atdizajnolasa valami listree-szeru classal

;---------------------------< END OF FILE >--------------------------------------
