**********************************************************
*                                                        *
*  Protracker Version 1.2 - Release A                    *
*  Amiga Freelancers 1991                                *
*                                                        *
*  Based on Soundtracker V2.5 / Noisetracker x.x)        *
*  Coding by Lars Hamre                                  *
*  with some "Resource" help from Anders Hamre           *
*  Thanks to Rune, and a hi there to Sven, Morten,       *
*  Per and Torgeir.                                      *
*                                                        *
*--------------------------------------------------------*
*  First Update   1.0 : 04/05-1990 First version         *
* Middle Update   1.0 : 13/08-1990 -                     *
*   Last Update   1.0 : 25/08-1990 -                     *
* Final Version   1.0 : 26/08-1990 Released to Crusaders *
* Rev. B Update 1 1.0B: 29/08-1990 -                     *
* Rev. B Update 2 1.0B: 06/09-1990 Released to Crusaders *
* Rev. C Update 1 1.0C: 16/10-1990 -                     *
* Rev. C Update 2 1.0C: 18/10-1990 -                     *
* Start V1.1A     1.1A: 23/10-1990 Second version        *
* Rev. C Update 3 1.0C: 06/11-1990 Released to Crusaders *
* Rev. A Update 1 1.1A: 15/11-1990 InputEvent Handler    *
* Rev. A Update 2 1.1A: 21/11-1990 RunBack Hunk, KbdBuf. *
* Rev. A Update 3 1.1A: 02/12-1990 iffld,loopspr,scopefix*
* Rev. A Update 4 1.1A: 05/12-1990 virusattack,dirbrowse *
* Rev. A Update 5 1.1A: 20/12-1990 Testversion to Awesome*
* Rev. A Update 6 1.1A: 27/12-1990 Party-release (w/bugs)*
* Rev. B Update 1 1.1B: 01/01-1991 Bugfixing...          *
* Rev. B Update 2 1.1B: 05/01-1991 ---                   *
* Rev. B Update 3 1.1B: 08/01-1991 ScopeGfx, loadbug.    *
* Start V2.0A     2.0A: 12/02-1991 Wow! a new version... *
* Start V1.2A     1.2A: 13/07-1991 Kick 37.175 version   *
**********************************************************

; FileFormat
sd_songname	EQU	0
sd_sampleinfo	EQU	20
sd_numofpatt	EQU	950
sd_pattpos	EQU	952
sd_mahokakt	EQU	1080
sd_patterndata	EQU	1084

; audXtemp offsets
n_note		EQU	0  ; W
n_cmd		EQU	2  ; W
n_cmdlo		EQU	3  ; low B of cmd
n_start		EQU	4  ; L
n_length	EQU	8  ; W
n_loopstart	EQU	10 ; L
n_replen	EQU	14 ; W
n_period	EQU	16 ; W
n_finetune	EQU	18 ; B
n_volume	EQU	19 ; B
n_dmabit	EQU	20 ; W
n_toneportdirec	EQU	22 ; B
n_toneportspeed	EQU	23 ; B
n_wantedperiod	EQU	24 ; W
n_vibratocmd	EQU	26 ; B
n_vibratopos	EQU	27 ; B
n_tremolocmd	EQU	28 ; B
n_tremolopos	EQU	29 ; B
n_wavecontrol	EQU	30 ; B
n_glissfunk	EQU	31 ; B
n_sampleoffset	EQU	32 ; B
n_pattpos	EQU	33 ; B
n_loopcount	EQU	34 ; B
n_funkoffset	EQU	35 ; B
n_wavestart	EQU	36 ; L
n_reallength	EQU	40 ; W
n_trigger	EQU	42 ; B
n_samplenum	EQU	43 ; B

; Exec Library Offsets
LVOFindTask	EQU	-294
LVOFindName	EQU	-276
LVOForbid	EQU	-132
LVOPermit	EQU	-138
LVOAddPort	EQU	-354
LVORemPort	EQU	-360
LVOWaitPort	EQU	-384
LVOOpenLibrary	EQU	-552
LVOCloseLibrary	EQU	-414
LVOOpenDevice	EQU	-444
LVOCloseDevice	EQU	-450
LVODoIO		EQU	-456
LVOSendIO	EQU	-462
LVOGetMsg	EQU	-372
LVOReplyMsg	EQU	-378
LVOAllocMem	EQU	-198
LVOAllocAbs	EQU	-204
LVOFreeMem	EQU	-210
LVOAvailMem	EQU	-216
LVOAddIntServer	EQU	-168
LVORemIntServer	EQU	-174
LVOOpenResource	EQU	-498
LVOSetFunction	EQU	-420
LVOSetIntVector	EQU	-162
LVORemDevice	EQU	-438

; CIA Resource Offsets
AddICRVector	EQU	-6
RemICRVector	EQU	-12

; DOS Library Offsets
LVOOpen		EQU	-30
LVOClose	EQU	-36
LVORead		EQU	-42
LVOWrite	EQU	-48
LVODeleteFile	EQU	-72
LVORename	EQU	-78
LVOLock		EQU	-84
LVOUnLock	EQU	-90
LVOExamine	EQU	-102
LVOExNext	EQU	-108
LVOInfo		EQU	-114
LVOIoErr	EQU	-132
LVOSeek		EQU	-66
LVOCreateDir	EQU	-120
LVOCurrentDir	EQU	-126
LVODateStamp	EQU	-192
LVODelay	EQU	-198
LVOExecute	EQU	-222
LVOUnLoadSeg	EQU	-156


; Intuition Library Offsets
LVOAutoRequest	  EQU	-348
LVODisplayAlert   EQU	-90	  
LVOOpenWorkbench  EQU	-210
LVOCloseWorkbench EQU	-78
LVOWBenchToFront  EQU	-342

; Graphics Library Offsets
LVOBltBitMap	EQU	-30
LVOWaitBlit	EQU	-228
LVOOwnBlitter	EQU	-456
LVODisownBlitter EQU	-462

; DateStamp
ofib_DateStamp	EQU	$84
ds_Days		EQU	$00
ds_Minute	EQU	$04
ds_Tick		EQU	$08

; Memory Alloc Flags
MEMF_PUBLIC	EQU	$0001
MEMF_CHIP	EQU	$0002
MEMF_FAST	EQU	$0004
MEMF_CLEAR	EQU	$10000
MEMF_LARGEST	EQU	$20000

; IO Block Offsets
IO_COMMAND	EQU	$1C
IO_FLAGS	EQU	$1E
IO_ACTUAL	EQU	$20
IO_LENGTH	EQU	$24
IO_DATA		EQU	$28
IO_OFFSET	EQU	$2C

; Device Commands 
CMD_READ	EQU	$2
CMD_WRITE	EQU	$3
CMD_UPDATE	EQU	$4
TD_MOTOR	EQU	$9
TD_FORMAT	EQU	$B
TD_CHANGESTATE	EQU	$E
TD_PROTSTATUS	EQU	$F

	INCDIR	"dh0:devpac/include/"
	INCLUDE	"exec/execbase.i"


DirNameLength		EQU	30
ConfigFileSize		EQU	1024
KeyBufSize		EQU	20

;**************************************************************************

; RunBack Hunk for Protracker
; Written by Lars Hamre/Amiga Freelancers 1990
; NOTE: This version will not pass arguments or WBMsg to the new Process!

	SECTION	ptrunback,CODE

rb_HunkStart
	SUB.L	A1,A1
	MOVE.L	4.W,A6
	JSR	-294(A6)	; FindTask
	MOVE.L	D0,A4
	MOVEQ	#0,D0
	TST.L	$AC(A4)		; pr_CLI
	BNE.S	rb_FromCLI
	LEA	$5C(A4),A0	; pr_MsgPort
	JSR	-384(A6)	; WaitPort
	LEA	$5C(A4),A0
	JSR	-372(A6)	; GetMsg
rb_FromCLI
	MOVE.L	D0,D7		; D7 = WBmsg
	LEA	rb_DOSname(PC),A1
	MOVEQ	#0,D0
	JSR	-552(A6)	; OpenLibrary
	TST.L	D0
	BEQ.S	rb_dexit
	MOVE.L	D0,A5
	EXG	A5,A6		; A6=DOS
	MOVE.L	$98(A4),D1	; pr_CurrentDir
	JSR	-96(A6)		; DupLock
	MOVE.L	D0,rb_CurrentDir
	EXG	A5,A6 ; A6=exec
	JSR	-132(A6) ; Forbid
	MOVE.L	#rb_ProgName,D1
	MOVEQ.L	#0,D2
	MOVE.B	9(A4),D2	; priority
	LEA	rb_HunkStart-4(PC),A0
	MOVE.L	(A0),D3		; Ptr to next segment
	CLR.L	(A0)		; Unlink next segment
	MOVE.L	#4000,D4
	EXG	A5,A6		; A6=DOS
	JSR	-138(A6)	; CreateProc
	EXG	A5,A6		; A6=exec
	JSR	-138(A6)	; Permit
	MOVE.L	A5,A1
	JSR	-414(A6)	; CloseLibrary
rb_dexit
	TST.L	D7
	BEQ.S	rb_exit
	JSR	-132(A6)	; Forbid
	MOVE.L	D7,A1
	JSR	-378(A6)	; ReplyMsg
rb_exit	MOVEQ	#0,D0
	RTS
 
rb_DOSname	dc.b	"dos.library",0

; End of runback hunk

;******************** CODE HUNK *********************

	SECTION ptcode,CODE

START	MOVE.L	4.W,A6
	SUB.L	A1,A1
	JSR	LVOFindTask(A6)
	MOVE.L	D0,PTProcess
	BSR.S	Main
	MOVE.L	4.W,A6
	JSR	LVOForbid(A6)
	MOVE.L	DOSbase,A6
	MOVE.L	rb_CurrentDir(PC),D1
	JSR	LVOUnLock(A6)
	LEA	START-4(PC),A0
	MOVE.L	A0,D1
	LSR.L	#2,D1
	JSR	LVOUnLoadSeg(A6)
	MOVEQ	#0,D0
	RTS

Main	MOVE.L	SP,StackSave
	BSR	OpenLotsOfThings
	BSR	SetVBInt
	BSR	SetMusicInt
	BSR	SetInputHandler
	BSR	SetAutoReq
	BSR	SetPatternPos
	BSR	SetNormalPtrCol
	BSR	StorePtrCol
	BSR	RedrawToggles
	BSR	DoShowFreeMem
	BSR	SetTempo
	BSR	SetCopList
	BSR	CheckInitError
	LEA	VersionText(PC),A0
	JSR	ShowStatusText
MainLoop
	BSR	DoKeyBuffer
	BSR	CheckTransKeys
	BSR	CheckCtrlKeys
	BSR	CheckAltKeys
	BSR	CheckKeyboard
	BSR	CheckF1_F2
	BSR	CheckF3_F5
	BSR	CheckF6_F10
	BSR	CheckPlayKeys
	BSR	CheckHelpKey
	BSR	ArrowKeys2
	BSR	ShowTimer
	BSR	ShowFreeMem
	BSR	CheckBlockPos
	BSR	CheckSampleLength
	BSR	CheckRedraw
	JSR	CheckMIDIin
	BTST	#6,$BFE001
	BNE.S	MainLoop    ; Loop if not pressed
	TST.W	CurrScreen
	BNE.S	MainSkip
	MOVE.W	#1,notogfl
	BSR	DisplayMainScreen
	JSR	ShowAllRight
	BSR	WaitForButtonUp
MainSkip
	BSR	ArrowKeys
	BRA	CheckGadgets

notogfl		dc.w 0
PTProcess	dc.l 0
rb_CurrentDir	dc.l 0
rb_Progname	dc.b "PT1.2A",0
VersionText	dc.b 'V1.2 - RELEASE A',0

CheckInitError
	MOVE.B	InitError,D0
	BEQ	Return1
	BTST	#0,D0
	BEQ.S	cieskp1
	JSR	PLSTMemErr
cieskp1	MOVE.B	InitError,D0
	BTST	#1,D0
	BEQ.S	cieskp2
	JSR	PLSTOpenErr
	JSR	FreePLST
cieskp2	MOVE.B	InitError,D0
	BTST	#2,D0
	BEQ.S	cieskp3
	BSR	ConfigErr
cieskp3	MOVE.B	InitError,D0
	BTST	#3,D0
	BEQ	Return1
	BSR	ConfigErr2
	BSR	SetNormalPtrCol
	BRA	StorePtrCol

CheckRedraw
	TST.B	UpdateTempo
	BEQ.S	chkredr
	CLR.B	UpdateTempo
	BSR	SetTempo
chkredr	CMP.L	#'patp',RunMode
	BNE	Return1
	JSR	ShowPosition
	TST.B	PattRfsh
	BEQ	Return1
	MOVE.L	PattRfshNum,PatternNumber
	JMP	RedrawPattern

;---- Cleanup upon exit from PT ----

ExitCleanup
	JSR	StopIt
	SF	EdEnable
	BSR	EscPressed
	BSR	EscPressed
	BSR	ResetVBInt
	BSR	ResetMusicInt
	BSR	ResetInputHandler
	BSR	ResetAutoReq
	BSR	ResetCopList
	JSR	CloseMIDI
	JSR	FreeCopyBuf
errorexit1
errorexit2
	MOVE.L	StackSave,SP
	MOVE.L	4.W,A6
	MOVE.L	GfxBase,A1
	JSR	LVOCloseLibrary(A6)
	MOVE.L	IntuitionBase,A1
	JSR	LVOCloseLibrary(A6)
	MOVE.L	DOSbase,A1
	JSR	LVOCloseLibrary(A6)

	MOVE.L	TextBplPtr,D1
	BEQ.S	exex1
	MOVE.L	D1,A1
	MOVE.L	#27760,D0
	JSR	LVOFreeMem(A6)
exex1	MOVE.L	SongDataPtr,D1
	BEQ.S	exex2
	MOVE.L	D1,A1
	MOVE.L	#70716,D0
	JSR	LVOFreeMem(A6)
exex2	BSR	FreeDirMem
	BSR	GiveBackInstrMem
	JSR	FreePLST
	JSR	TurnOffVoices
	BCLR	#1,$BFE001
	MOVE.B	LEDStatus,D0
	AND.B	#2,D0
	OR.B	D0,$BFE001 ; Restore LED Status
	MOVEQ	#0,D0
Return1	RTS

;---- Open Lots Of Things ----

OpenLotsOfThings
	MOVE.B	$BFE001,LEDStatus
	BSET	#1,$BFE001
	JSR	TurnOffVoices
	MOVE.L	4.W,A6
	LEA	DOSname(PC),A1		; Open DOS library
	MOVEQ	#0,D0
	JSR	LVOOpenLibrary(A6)
	MOVE.L	D0,DOSbase
	LEA	GraphicsName(PC),A1	; Open graphics library
	MOVEQ	#0,D0
	JSR	LVOOpenLibrary(A6)
	MOVE.L	D0,GfxBase
	LEA	IntuitionName(PC),A1	; Open Intuition library
	MOVEQ	#0,D0
	JSR	LVOOpenLibrary(A6)
	MOVE.L	D0,IntuitionBase

	MOVE.L	DOSbase,A6
	MOVE.L	rb_CurrentDir(PC),D1
	JSR	LVOCurrentDir(A6)

	MOVE.L	#27760,D0		; Alloc 27k CHIP textbpl
	MOVE.L	#MEMF_CHIP!MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,TextBplPtr
	BEQ	errorexit1
	MOVE.W	D0,NoteBplptrLow	; set low word
	SWAP	D0
	MOVE.W	D0,NoteBplptrHigh	; set high word

	MOVE.L	#70716,D0		; Alloc 70k patternmemory
	MOVE.L	#MEMF_PUBLIC!MEMF_CLEAR,D1
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,SongDataPtr
	BEQ	errorexit2

	MOVE.L	D0,A0
	LEA	$0014(A0),A0
	MOVEQ	#30,D0
alotloop1
	MOVE.W	#1,$001C(A0)
	LEA	$001E(A0),A0
	DBRA	D0,alotloop1
	MOVE.L	SongDataPtr,A0
	MOVE.W	#$017F,sd_numofpatt(A0)
	MOVE.L	#'M.K.',sd_mahokakt(A0)	; M.K. again...

	MOVEQ	#6,D0
	MOVE.L	D0,CurrSpeed
	MOVE.W	#1,PEDpos
	MOVE.L	#ModulesPath2,PathPtr
	MOVE.L	#CopList,CopListPtr
	MOVE.L	#CopListBitplanes,CopListBplPtr
	MOVE.L	#CopListColors,CopListColorPtr
	MOVE.L	#CopListBpl4,CopListBpl4Ptr
	MOVE.L	#KbdTransTable2,KeyTransTabPtr
	MOVE.L	#NoteNames1,NoteNamesPtr
	MOVE.L	#VUMeterColors,TheRightColors

	MOVE.L	#BitplaneData,D0 ; Set pointers to bitplane data
	LEA	CopListBitplanes,A1
	MOVE.W	D0,6(A1)
	SWAP	D0
	MOVE.W	D0,2(A1)
	SWAP	D0
	ADD.L	#10240,D0
	MOVE.W	D0,14(A1)
	SWAP	D0
	MOVE.W	D0,10(A1)
	BSR	SetDefSpritePtrs
	BSR	UpdateCursorPos
	JSR	RedrawPattern
	JSR	ShowPosition
	JSR	ShowSongLength
	MOVE.W	#1,InsNum
	JSR	ShowSampleInfo

	LEA	TopMenusPos,A0
	LEA	TopMenusBuffer,A1
	MOVEQ	#43,D0
stmloop	MOVEQ	#24,D1
stmloop2
	MOVE.B	(A0)+,(A1)+
	MOVE.B	$27FF(A0),$044B(A1)
	DBRA	D1,stmloop2
	LEA	15(A0),A0
	DBRA	D0,stmloop

	BSR	DoResetAll
	BSR	cfgupda
	BSR	DoLoadConfig
	MOVE.L	#NoteNames1,NoteNamesPtr
	TST.B	Accidental
	BEQ	.skip
	MOVE.L	#NoteNames2,NoteNamesPtr
.skip	JSR	DoLoadPLST
ChangeCopList
	TST.W	SamScrEnable
	BNE	Return1
	BSR	SetupAnaCols
SetupVUCols
	TST.W	SamScrEnable
	BNE	Return1
	LEA	CopListMark2,A0	; VUmeter coloring
	MOVE.W	#$BA07,D5	; Start position
	LEA	VUmeterColors,A5
	MOVEQ	#47,D7	; Change 48 lines
alotlp4	MOVE.W	D5,(A0)+
	MOVE.W	#$FFFE,(A0)+
	LEA	VUmeterColRegs(PC),A1
	MOVEQ	#1,D6
alotlp5	MOVE.W	(A5),D4
	MOVE.W	(A1)+,(A0)+
	MOVE.W	#3,FadeX
	MOVE.W	D4,D0
	BSR	FadeCol
	MOVE.W	D0,(A0)+
	MOVE.W	(A1)+,(A0)+
	MOVE.W	D4,(A0)+
	MOVE.W	(A1)+,(A0)+
	MOVE.W	#-3,FadeX
	MOVE.W	D4,D0
	BSR	FadeCol
	MOVE.W	D0,(A0)+
	DBRA	D6,alotlp5
	ADDQ.W	#2,A5
	ADD.W	#$0100,D5	; Next line...
	DBRA	D7,alotlp4
	RTS

VUmeterColRegs
	dc.w	$01B2,$01B4,$01B6,$01BA,$01BC,$01BE

CopListBpl4Ptr	dc.l	0
CopListPtr	dc.l	0
CopListBplPtr	dc.l	0
CopListColorPtr	dc.l	0
DOSname		dc.b	'dos.library',0
IntuitionName	dc.b	'intuition.library',0
GraphicsName	dc.b	'graphics.library',0,0

SetupAnaCols
	LEA	CopListAnalyzer,A5
	MOVE.W	ColorTable+12,D2
	MOVEQ	#39,D0		; Change col 40 lines
	MOVE.W	#$687D,D1	; Start pos
sacloop	MOVE.W	D1,(A5)+	; Set wait
	MOVE.W	#$FFFE,(A5)+
	MOVE.W	#$018C,(A5)+	; Set analyzer color
	ADDQ.L	#2,A5
	ADD.W	#$0060,D1	; Move x-pos
	MOVE.W	D1,(A5)+	; Wait
	MOVE.W	#$FFFE,(A5)+
	MOVE.W	#$018C,(A5)+	; Set text color
	MOVE.W	D2,(A5)+
	ADD.W	#$0100,D1	; Next line...
	SUB.W	#$0060,D1
	DBRA	D0,sacloop
	TST.B	DisableAnalyzer
	BNE.S	ClearAnalyzerColors
SetAnalyzerColors
	LEA	CopListAnalyzer+6,A1
	LEA	AnalyzerColors,A0
	MOVEQ	#39,D0	; 40 lines
sanclop	MOVE.W	(A0)+,(A1)
	LEA	16(A1),A1
	DBRA	D0,sanclop
	RTS

ClearAnalyzerColors
	LEA	CopListAnalyzer+6,A0
	MOVE.W	ColorTable+12,D1
	MOVEQ	#39,D0	; 40 lines.
cacloop	MOVE.W	D1,(A0)
	LEA	16(A0),A0
	DBRA	D0,cacloop
	RTS

SetSamSpritePtrs
	MOVE.L	#LoopSpriteData1,Ch1SpritePtr
	MOVE.L	#LoopSpriteData2,Ch2SpritePtr
	MOVE.L	#PlayPosSpriteData,Ch3SpritePtr
	MOVE.L	#NoSpriteData,Ch4SpritePtr
	BRA.S	sdsp2
SetDefSpritePtrs
	MOVE.L	#VUSpriteData1,Ch1SpritePtr
	MOVE.L	#VUSpriteData2,Ch2SpritePtr
	MOVE.L	#VUSpriteData3,Ch3SpritePtr
	MOVE.L	#VUSpriteData4,Ch4SpritePtr
sdsp2	MOVE.L	#CursorSpriteData,CursorPosPtr
	MOVE.L	#PointerSpriteData,PointerSpritePtr
	MOVE.L	#LineCurSpriteData,LineCurPosPtr
	MOVE.L	#NoSpriteData,NoSpritePtr
	MOVE.L	PointerSpritePtr,SpritePtrsPtr

	LEA	SpritePtrsPtr,A0	; Set pointers to spritedata
	LEA	CopList,A1
	MOVEQ	#15,D2
alotloop2
	MOVE.W	(A0)+,2(A1)
	ADDQ.L	#4,A1
	DBRA	D2,alotloop2
	RTS

PointerSpritePtr
	dc.l	0

;---- AutoRequest() ----

SetAutoReq
	MOVE.L	4.W,A6
	JSR	LVOForbid(A6)
	MOVE.L	IntuitionBase,A1
	MOVE.L	#NewAutoRequest,D0
	MOVE.L	#LVOAutoRequest,A0
	JSR	LVOSetFunction(A6)
	MOVE.L	D0,AutoReqSave
	MOVE.L	D0,xyzza+2
	JSR	LVOPermit(A6)
	RTS

ResetAutoReq
	MOVE.L	4.W,A6
	MOVE.L	IntuitionBase,A1
	MOVE.L	AutoReqSave(PC),D0
	MOVE.L	#LVOAutoRequest,A0
	JSR	LVOSetFunction(A6)
	RTS

NewAutoRequest
	TST.W	MountFlag
	BNE	PositiveReturn
	TST.B	AutoReqRunning
	BNE.S	xyzza
	BSET 	#0,AutoReqRunning
	MOVE.L	A0,-(SP)
	MOVE.L	GfxBase,A0
	MOVE.L	38(A0),$DFF080
	MOVE.L	(SP)+,A0
xyzzy	JSR	$0
	MOVE.L	#CopList,$DFF080
	BCLR	#0,AutoReqRunning
	RTS
xyzza	JMP	$0

AutoReqSave	EQU xyzzy+2

;---- Special Uffda function for Intuition version 37+ -----

TestUffda
	MOVE.L	IntuitionBase,A6
	MOVE.L	60(A6),D0 ; FirstScreen
	BEQ	Return1
.wbloop	MOVE.L	D0,A0
	MOVE.W	20(A0),D0 ; Flags
	AND.W	#$000F,D0
	CMP.W	#1,D0
	BEQ.S	.wbfound
	MOVE.L	(A0),D0
	BEQ	Return1
	BRA.S	.wbloop

.wbfound
	MOVE.L	PTProcess,D1
	MOVE.L	4(A0),D0  ; FirstWindow
	BEQ	Return1
.loop	MOVE.L	D0,A0
	MOVE.L	86(A0),D0 ; UserPort
	BEQ	Return1
	MOVE.L	D0,A1
	CMP.L	16(A1),D1 ; SigTask
	BEQ.S	.foundwin
	MOVE.L	(A0),D0
	BEQ	.notfoundwin
	BRA.S	.loop

.foundwin
	BTST	#1,AutoReqRunning ; Det er noe der!
	BNE	Return1
	BSET	#1,AutoReqRunning
	BRA	ResetCopList

.notfoundwin
	BTST	#1,AutoReqRunning ; Ikke noe der!
	BEQ	Return1
	BCLR	#1,AutoReqRunning
	BRA	SetCopList

;---- DisplayAlert() ----

SetDispAlert
	MOVE.L	IntuitionBase,A0
	MOVE.L	LVODisplayAlert+2(A0),DispAlertSave
	MOVE.L	#NewDispAlert,LVODisplayAlert+2(A0)
	RTS

ResetDispAlert
	MOVE.L	IntuitionBase,A0
	MOVE.L	DispAlertSave,LVODisplayAlert+2(A0)
	RTS

NewDispAlert
	MOVEM.L	D0-D7/A0-A6,-(SP)
	LEA	SoftFailText(PC),A1
	ADDQ.L	#3,A0
	MOVEQ	#16,D0
ndaloop	CMP.B	(A1)+,(A0)+
	BNE.S	ndaexit
	DBRA	D0,ndaloop
	BSR	FindAdr
	TST.L	D0
	BEQ.S	ndaexit
	MOVE.L	D0,A1
	MOVE.L	SongDataPtr,A0
	MOVE.L	#70716,D0
ndalop2	MOVE.B	(A0)+,D1
	MOVE.W	D1,$DFF180
	MOVE.B	D1,(A1)+
	SUBQ.L	#1,D0
	BNE.S	ndalop2
ndaexit	MOVEM.L	(SP)+,D0-D7/A0-A6
xyzzy2	JMP	$FC0000
		
DispAlertSave EQU xyzzy2+2
SoftFailText	dc.b	"Software Failure.",0

;---- Copper List ----

SetCopList
	MOVE.L	#CopList,$DFF080
	RTS

ResetCopList
	MOVE.L	GfxBase,A0
	MOVE.L	38(A0),$DFF080
	RTS

;---- Vertical Blank Interrupt ----

SetVBInt
	MOVEQ	#5,D0
	LEA	VBIntServer,A1
	MOVE.L	4.W,A6
	JSR	LVOAddIntServer(A6)
	RTS

ResetVBInt
	MOVEQ	#5,D0
	LEA	VBIntServer,A1
	MOVE.L	4.W,A6
	JSR	LVORemIntServer(A6)
	RTS

vbint	MOVEM.L	D0-D7/A0-A6,-(SP)
	BSR	UpdatePointerPos
	BSR	VUmeters
	BSR	Scope
	BSR	SpecAnaInt
	BSR	ArrowKeys
	BSR	CheckKeyRepeat
	BSR	UpdateTicks
	BSR	TestUffda
	MOVEM.L	(SP)+,D0-D7/A0-A6
	RTS

VBIntServer
	dc.l 0,0
	dc.b 2,0 ; type, priority
	dc.l vbintname
	dc.l 0,vbint

vbintname
	dc.b "Protracker VBlank",0

;---- Music Interrupt ----

SetMusicInt
	TST.B	IntMode
	BNE.S	SetCIAInt
	MOVEQ	#5,D0
	LEA	MusicIntServer,A1
	MOVE.L	4.W,A6
	JSR	LVOAddIntServer(A6)
	RTS

ResetMusicInt
	TST.B	IntMode
	BNE	ResetCIAInt
	MOVEQ	#5,D0
	LEA	MusicIntServer,A1
	MOVE.L	4.W,A6
	JSR	LVORemIntServer(A6)
	RTS

MusicIntServer
	dc.l 0,0
	dc.b 2,1 ; type, priority
	dc.l musintname
	dc.l 0,IntMusic

musintname	dc.b "Protracker MusicInt",0

;---- CIA Interrupt ----

ciatalo = $400
ciatahi = $500
ciatblo = $600
ciatbhi = $700
ciacra  = $E00
ciacrb  = $F00

SetCIAInt
	MOVEQ	#2,D6
	LEA	$BFD000,A5
	MOVE.B	#'b',CIAAname+3
SetCIALoop
	MOVEQ	#0,D0
	LEA	CIAAname(PC),A1
	MOVE.L	4.W,A6
	JSR	LVOOpenResource(A6)
	MOVE.L	D0,CIAAbase
	BEQ	Return1
	MOVE.L	D0,A6

	MOVE.L	GfxBase,A0
	MOVE.W	206(A0),D0	; DisplayFlags
	BTST	#2,D0		; PAL?
	BEQ.S	WasNTSC
	MOVE.L	#1773447,D7 ; PAL
	BRA.S	sciask
WasNTSC	MOVE.L	#1789773,D7 ; NTSC
sciask	MOVE.L	D7,TimerValue
	DIVU	#125,D7 ; Default to normal 50 Hz timer

	CMP.W	#2,D6
	BEQ.S	TryTimerA
TryTimerB
	LEA	MusicIntServer(PC),A1
	MOVEQ	#1,D0	; Bit 1: Timer B
	JSR	AddICRVector(A6)
	MOVE.L	#1,TimerFlag
	TST.L	D0
	BNE.S	CIAError
	MOVE.L	A5,CIAAaddr
	MOVE.B	D7,ciatblo(A5)
	LSR.W	#8,D7
	MOVE.B	D7,ciatbhi(A5)
	BSET	#0,ciacrb(A5)
	BRA	SetTempo

TryTimerA
	LEA	MusicIntServer(PC),A1
	MOVEQ	#0,D0	; Bit 0: Timer A
	JSR	AddICRVector(A6)
	CLR.L	TimerFlag
	TST.L	D0
	BNE.S	CIAError
	MOVE.L	A5,CIAAaddr
	MOVE.B	D7,ciatalo(A5)
	LSR.W	#8,D7
	MOVE.B	D7,ciatahi(A5)
	BSET	#0,ciacra(A5)
	BRA	SetTempo

CIAError
	MOVE.B	#'a',CIAAname+3
	LEA	$BFE001,A5
	SUBQ.W	#1,D6
	BNE	SetCIALoop
	CLR.L	CIAAbase
	RTS

ResetCIAInt
	MOVE.L	CIAAbase(PC),D0
	BEQ	Return1
	CLR.L	CIAAbase
	MOVE.L	D0,A6
	MOVE.L	CIAAaddr(PC),A5
	TST.L	TimerFlag
	BEQ.S	ResTimerA

	BCLR	#0,ciacrb(A5)
	MOVEQ	#1,D0
	BRA.S	RemInt

ResTimerA
	BCLR	#0,ciacra(A5)
	MOVEQ	#0,D0
RemInt	LEA	MusicIntServer(PC),A1
	MOVEQ	#0,d0
	JSR	RemICRVector(A6)
	RTS

CIAAname	dc.b 'ciaa.resource',0
CIAAbase	dc.l 0
TimerFlag	dc.l 0
TimerValue	dc.l 0

;---- Tempo ----

TempoGadg
	CMP.W	#60,D0
	BHS	Return1
	CMP.W	#44,D0
	BHS.S	TemDown
TemUp	MOVE.W	RealTempo(PC),D0
	ADDQ.W	#1,D0
	BTST	#2,$DFF016
	BNE.S	teupsk
	ADDQ.W	#8,D0
	ADDQ.W	#1,D0
teupsk	CMP.W	#255,D0
	BLS.S	teposk
	MOVE.W	#255,D0
teposk	MOVE.W	D0,RealTempo
	BSR	SetTempo
	BRA	Wait_4000

TemDown	MOVE.W	RealTempo(PC),D0
	SUBQ.W	#1,D0
	BTST	#2,$DFF016
	BNE.S	tednsk
	SUBQ.W	#8,D0
	SUBQ.W	#1,D0
tednsk	CMP.W	#32,D0
	BHS.S	teposk
	MOVE.W	#32,D0
	BRA.S	teposk


ChangeTempo
	CMP.W	#94,D0
	BHS.S	TempoDown
	CMP.W	#80,D0
	BHS.S	TempoUp
	RTS

TempoUp	MOVE.W	Tempo,D0
	ADDQ.W	#1,D0
	BTST	#2,$DFF016
	BNE.S	temupsk
	ADDQ.W	#8,D0
	ADDQ.W	#1,D0
temupsk	CMP.W	#255,D0
	BLS.S	temposk
	MOVE.W	#255,D0
temposk	MOVE.W	D0,Tempo
	MOVE.W	D0,RealTempo
	BSR.S	ShowTempo
	BSR.S	SetTempo
	BRA	Wait_4000

TempoDown
	MOVE.W	Tempo,D0
	SUBQ.W	#1,D0
	BTST	#2,$DFF016
	BNE.S	temdnsk
	SUBQ.W	#8,D0
	SUBQ.W	#1,D0
temdnsk	CMP.W	#32,D0
	BHS.S	temposk
	MOVE.W	#32,D0
	BRA.S	temposk

ShowTempo
	MOVE.W	#607,TextOffset
	MOVE.W	RealTempo(PC),WordNumber
	JMP	Print3DecDigits

SetTempo
	MOVEQ	#125,D0
	MOVE.L	CIAAbase(PC),D1
	BEQ.S	setesk3
	MOVE.W	RealTempo(PC),D0
	CMP.W	#32,D0
	BHS.S	setemsk
	MOVEQ	#32,D0
setemsk	MOVE.W	D0,RealTempo
setesk3	TST.W	SamScrEnable
	BNE.S	setesk2
	MOVE.W	#4964,TextOffset
	MOVE.W	D0,WordNumber
	JSR	Print3DecDigits
setesk2	MOVE.L	CIAAbase(PC),D0
	BEQ	Return1
	MOVE.W	RealTempo(PC),D0
	MOVE.L	TimerValue(PC),D1
	DIVU	D0,D1
	MOVE.L	CIAAaddr(PC),A5
	MOVE.L	TimerFlag(PC),D0
	BEQ.S	SetTemA
	MOVE.B	D1,ciatblo(A5)	;and set the CIA timer
	LSR.W	#8,D1
	MOVE.B	D1,ciatbhi(A5)
	RTS

SetTemA	MOVE.B	D1,ciatalo(A5)
	LSR.W	#8,D1
	MOVE.B	D1,ciatahi(A5)
	RTS

RealTempo	dc.w 125
CIAAaddr	dc.l 0

;---- Input Event Handler ----

SetInputHandler
	MOVE.L	4.W,A6
	LEA	InpEvPort,A1
	JSR	LVOAddPort(A6)
	LEA	InputDevName,A0
	MOVEQ	#0,D0
	LEA	InpEvIOReq,A1
	MOVEQ	#0,D1
	MOVE.L	#InpEvPort,14(A1)
	JSR	LVOOpenDevice(A6)
	LEA	InpEvIOReq,A1
	MOVE.W	#9,IO_COMMAND(A1) ; IND_ADDHANDLER
	MOVE.B	#1,IO_FLAGS(A1)
	MOVE.L	#InpEvStuff,IO_DATA(A1)
	JSR	LVOSendIO(A6)
	RTS

ResetInputHandler
	MOVE.L	4.W,A6
	LEA	InpEvIOReq,A1
	MOVE.W	#10,IO_COMMAND(A1) ; IND_REMHANDLER
	MOVE.B	#1,IO_FLAGS(A1)
	MOVE.L	#InpEvStuff,IO_DATA(A1)
	JSR	LVOSendIO(A6)
	LEA	InpEvIOReq,A1
	JSR	LVOCloseDevice(A6)
	LEA	InpEvPort,A1
	JSR	LVORemPort(A6)
	RTS


InpEvStuff
	dc.l	0,0
	dc.b	2,52 ; type, priority
	dc.l	inpevname
	dc.l	0,InputHandler

inpevname
	dc.b "Protracker InputHandler",0

InputHandler ; A0-InputEvent, A1-Data Area
	MOVEM.L	D1/A0-A3,-(SP)
	TST.B	AutoReqRunning
	BNE.S	inphend
	SUB.L	A2,A2
	MOVE.L	A0,A1
	MOVE.B	4(A1),D0 ; ie_Class
	CMP.B	#1,D0    ; RAWKEY
	BEQ.S	InpRawkey
	CMP.B	#2,D0    ; RAWMOUSE
	BEQ.S	InpRawmouse
	MOVE.L	A1,A2
InpNext	MOVE.L	(A1),A1
	MOVE.L	A1,D0
	BNE.S	InputHandler
inphend	MOVE.L	A0,D0
	MOVEM.L	(SP)+,D1/A0-A3
	RTS

InpRawkey
	BSR	InpUnchain
	MOVE.W	6(A1),D0
	BSR	ProcessRawkey
	BRA	InpNext

InpRawmouse
	BSR	InpUnchain
	MOVE.W	MouseX3,D0
	MOVE.W	MouseY3,D1
	ADD.W	10(A1),D0
	ADD.W	12(A1),D1
	TST.W	D0
	BPL.S	irskip1
	MOVEQ	#0,D0
irskip1	TST.W	D1
	BPL.S	irskip2
	MOVEQ	#0,D1
irskip2	CMP.W	#640,D0
	BLO.S	irskip3
	MOVE.W	#639,D0
irskip3	CMP.W	#512,D1
	BLO.S	irskip4
	MOVE.W	#511,D1
irskip4	MOVE.W	D0,MouseX3
	MOVE.W	D1,MouseY3
	LSR.W	#1,D0
	LSR.W	#1,D1
	MOVE.W	D0,MouseX
	MOVE.W	D1,MouseY
	BRA.S	InpNext

InpUnchain
	MOVE.L	A2,D0
	BNE.S	InpUnc2
	MOVE.L	(A1),A0
	RTS
InpUnc2	MOVE.L	(A1),(A2)
	RTS

;---- Process rawkey code from the keyboard ----

ProcessRawkey
	CMP.B	LastRawkey,D0
	BEQ	Return1
	MOVE.B	D0,LastRawkey
	CMP.B	#67+128,D0
	BEQ	LowerInstr2
	CMP.B	#96,D0
	BEQ	ShiftOn
	CMP.B	#97,D0
	BEQ	ShiftOn2
	CMP.B	#100,D0
	BEQ	AltOn
	CMP.B	#101,D0
	BEQ	AltOn2
	CMP.B	#99,D0
	BEQ	CtrlOn
	CMP.B	#102,D0
	BEQ	LeftAmigaOn
	CMP.B	#99+128,D0
	BEQ	CtrlOff
	CMP.B	#100+128,D0
	BEQ	AltOff
	CMP.B	#96+128,D0
	BEQ.S	ShiftOff
	CMP.B	#101+128,D0
	BEQ	AltOff
	CMP.B	#97+128,D0
	BEQ.S	ShiftOff
	CMP.B	#102+128,D0
	BEQ	LeftAmigaOff
	CMP.B	#98,D0
	BEQ.S	KeyRepOn
	CMP.B	#98+128,D0
	BEQ.S	KeyRepOff
kbintSetKey
	TST.B	D0
	BNE.S	kbintDoSet
	MOVE.B	#127,D0
kbintDoSet
	MOVE.W	KeyBufPos(PC),D1
	CMP.W	#KeyBufSize,D1
	BHS	Return1
	LEA	KeyBuffer(PC),A3
	MOVE.B	D0,(A3,D1.W)
	ADDQ.W	#1,KeyBufPos
	RTS

LowerInstr2
	CLR.W	HiLowInstr
	BRA.S	kbintsetkey

KeyRepOn
	ST	KeyRepeat
	RTS

KeyRepOff
	SF	KeyRepeat
	RTS

ShiftOn	MOVE.W	#1,ShiftKeyStatus
	RTS

ShiftOn2
	MOVE.W	#1,ShiftKeyStatus
	BRA.S	kbintsetkey
 
ShiftOff
	CLR.W	ShiftKeyStatus
	RTS
 
AltOn	MOVE.W	#1,AltKeyStatus
	RTS

AltOn2	MOVE.W	#1,AltKeyStatus
	BRA.S	kbintsetkey
 
AltOff	CLR.W	AltKeyStatus
	RTS

CtrlOn	MOVE.W	#1,CtrlKeyStatus
	RTS
 
CtrlOff	CLR.W	CtrlKeyStatus
	RTS

LeftAmigaOn
	MOVE.W	#1,LeftAmigaStatus
	RTS
 
LeftAmigaOff
	CLR.W	LeftAmigaStatus
	RTS

DoKeyBuffer
	MOVE.W	KeyBufPos(PC),D0
	BEQ	Return1
	SUBQ.W	#1,D0
	LEA	KeyBuffer(PC),A0
	MOVE.B	(A0,D0.W),D1
	MOVE.W	D0,KeyBufPos
	MOVE.B	D1,RawKeyCode
	MOVE.B	D1,SaveKey
	MOVE.W	KeyRepDelay,KeyRepCounter
	BTST	#7,D1
	BEQ.S	dkbend
	CLR.W	KeyRepCounter
dkbend	RTS

KeyBufPos	dc.w	0
KeyBuffer	dcb.b	KeyBufSize,0
ShiftKeyStatus	dc.w	0
AltKeyStatus	dc.w	0
CtrlKeyStatus	dc.w	0
LeftAmigaStatus	dc.w	0
LastRawkey	dc.b	255,0
SaveKey		dc.b 0
KeyRepeat	dc.b 0
KeyRepCounter	dc.w 0

;---- Key repeat ----

CheckKeyRepeat
	TST.B	KeyRepeat
	BEQ	Return1
	MOVE.W	KeyRepCounter(PC),D0
	BEQ	Return1
	SUBQ.W	#1,D0
	BEQ.S	RepDown
	MOVE.W	D0,KeyRepCounter
	RTS

RepDown	MOVE.B	SaveKey(PC),RawKeyCode
	MOVE.W	KeyRepSpeed,KeyRepCounter
	RTS

;---- Update Mousepointer Position ----

UpdatePointerPos
	MOVE.W	MouseX,D0
	ADDQ.W	#3,D0
	MOVE.W	MouseY,D1
	MOVEQ	#16,D2
	MOVE.L	PointerSpritePtr,A0
	JMP	SetSpritePos

;---- Timer ----

UpdateTicks
	CMP.L	#'patp',RunMode
	BNE	Return1
	ADDQ.W	#1,PatternTicks
	MOVE.W	PatternTicks(PC),D0
	MOVE.L	4.W,A0
	CMP.B	$0212(A0),D0
	BNE	Return1
	CLR.W	PatternTicks
	ADDQ.L	#1,TimerTicks
	RTS	
 
ShowTimer
	CMP.W	#4,CurrScreen
	BEQ	Return1
	MOVE.L	TimerTicks(PC),D0
	DIVU	#60,D0
	MOVE.W	D0,WordNumber
	SWAP	D0
	MOVE.W	D0,D7
	MOVE.W	#4154,TextOffset
	JSR	Print2DecDigits
	MOVE.W	D7,WordNumber
	ADDQ.W	#1,TextOffset
	JMP	Print2DecDigits

PatternTicks	dc.w	0
TimerTicks	dc.l	0

;---- Spectrum Analyzer ----

SpecAnaInt
	CMP.W	#1,CurrScreen
	BNE	Return1
	TST.B	DisableAnalyzer
	BNE	Return1
.loop	TST.B	AnaDrawFlag
	BNE	Return1
	ST	AnaDrawFlag
	MOVEQ	#40,D5
	MOVEQ	#126,D6
	LEA	AnalyzerHeights,A0
	LEA	AnalyzerOpplegg,A1
	LEA	AnalyzerOffsets(PC),A2
	MOVE.L	TextBplPtr,A3
	LEA	1976(A3),A3
	MOVEQ	#$16,D7
spanlab1
	MOVE.W	(A0)+,D0
	CMP.W	#36,D0
	BLO.S	spanskip2
	MOVE.W	#36,D0
spanskip2
	MOVE.W	(A1)+,D1
	CMP.W	D0,D1
	BEQ.S	spanskip3
	BLO.S	span_r2
	SUB.W	D0,D1
	SUBQ.W	#1,D1
	ADD.W	D0,D0
	MOVE.W	0(A2,D0.W),D0
spanloop
	CLR.B	0(A3,D0.W)
	SUB.W	D5,D0
	DBRA	D1,spanloop
	BRA.S	spanskip3
 
span_r2	SUB.W	D1,D0
	SUBQ.W	#1,D0
	LSL.W	#1,D1
	MOVE.W	0(A2,D1.W),D1
spanloop2	MOVE.B	D6,0(A3,D1.W)
	SUB.W	D5,D1
	DBRA	D0,spanloop2
spanskip3
	ADDQ.L	#1,A3
	DBRA	D7,spanlab1
	LEA	AnalyzerHeights,A0
	LEA	AnalyzerOpplegg,A1
	MOVEQ	#22,D7
spanloop3	MOVE.W	(A0),D0
	MOVE.W	D0,(A1)+
	BEQ.S	spanskip4
	SUBQ.W	#1,D0
spanskip4
	MOVE.W	D0,(A0)+
	DBRA	D7,spanloop3
	SF	AnaDrawFlag
	RTS

ClearAnaHeights
	LEA	AnalyzerOpplegg,A0
	LEA	AnalyzerHeights,A1
	MOVEQ	#22,D7
	MOVEQ	#0,D0
cahloop	MOVE.W	D0,(A0)+
	MOVE.W	D0,(A1)+
	DBRA	D7,cahloop
	RTS

PlayNoteAnalyze		; Called by keyboard play
	MOVEM.L	D0-D3/A0,-(SP)
	MOVEQ	#0,D2
	MOVE.W	2(A6),D2
	BRA.S	SpecAna2

SpectrumAnalyzer	; Called by playroutine
	TST.W	(A4)
	BEQ	Return1
	MOVEM.L	D0-D3/A0,-(SP)
	MOVEQ	#0,D2
	MOVE.B	n_volume(A6),D2	; Get channel volume
SpecAna2
	TST.B	AnaDrawFlag
	BNE	ohno
	ST	AnaDrawFlag
	BSR	SetAnalyzerVolumes
	BEQ.S	saend
	LSL.W	#8,D2
	DIVU	#682,D2
	MOVE.W	D2,D3
	LSR.W	#1,D3
	LEA	AnalyzerHeights,A0
	SUB.W	#113,D0	; Subtract 113 (highest rate)
	MOVE.W	#743,D1
	SUB.W	D0,D1	; Invert range 0-743
	MULU	D1,D1	; 0 - 743^2
	DIVU	#25093,D1
	MOVE.W	D1,D0
	CMP.W	#46,D0
	BLO.S	saskip
	MOVEQ	#45,D0
saskip	LSL.W	#1,D0
	ADD.W	D2,(A0,D0.W)
	CMP.W	#36,(A0,D0.W)
	BLO.S	saskip2
	MOVE.W	#36,(A0,D0.W)
saskip2	TST.W	D0
	BEQ.S	saskip3
	ADD.W	D3,-2(A0,D0.W)
	CMP.W	#36,-2(A0,D0.W)
	BLO.S	saskip3
	MOVE.W	#36,-2(A0,D0.W)
saskip3	CMP.W	#44,D0
	BEQ.S	saend
	ADD.W	D3,2(A0,D0.W)
	CMP.W	#36,2(A0,D0.W)
	BLO.S	saend
	MOVE.W	#36,2(A0,D0.W)
saend	SF	AnaDrawFlag
ohno	MOVEM.L	(SP)+,D0-D3/A0
	RTS

AnalyzerOffsets
	dc.w $0730,$0708,$06E0,$06B8,$0690,$0668,$0640,$0618
	dc.w $05F0,$05C8,$05A0,$0578,$0550,$0528,$0500,$04D8
	dc.w $04B0,$0488,$0460,$0438,$0410,$03E8,$03C0,$0398
	dc.w $0370,$0348,$0320,$02F8,$02D0,$02A8,$0280,$0258
	dc.w $0230,$0208,$01E0,$01B8,$0190,$0168,$0140,$0118
	dc.w $00F0

AnaDrawFlag	dc.b 0,0

;---- Scope ----

ns_sampleptr =  0
ns_endptr    =  4
ns_repeatptr =  8
ns_rependptr = 12
ns_period    = 16
ns_volume    = 18

Scope	LEA	audchan1temp,A0
	LEA	ScopeInfo,A2
	LEA	ScopeSamInfo,A1
	LEA	BlankSample,A3
	MOVEQ.L	#3,D6
ScoLoop	MOVE.W	(A0),D0
	AND.W	#$0FFF,D0
	OR.W	n_period(A0),D0
	BEQ	ScoSampleEnd ; end if no note & no period

	MOVE.W	n_period(A0),ns_period(A2)
	MOVE.B	n_volume(A0),ns_volume(A2)


	TST.B	n_trigger(A0)
	BEQ	ScoContinue
ScoRetrig
	SF	n_trigger(A0)
	BSR	SetScope
	MOVEQ	#0,D0
	MOVE.B	n_samplenum(A0),D0
	SUBQ.W	#1,D0
	LSL.W	#4,D0
	MOVE.L	ns_sampleptr(A1,D0.W),ns_sampleptr(A2)
	MOVE.L	ns_endptr(A1,D0.W),ns_endptr(A2)
	MOVE.L	ns_repeatptr(A1,D0.W),ns_repeatptr(A2)
	MOVE.L	ns_rependptr(A1,D0.W),ns_rependptr(A2)
	MOVE.L	ns_sampleptr(A2),D0
	CMP.L	A3,D0 ; at end of sample...
	BEQ.S	ScoNextChan
	BRA	ScoChk
ScoContinue
	MOVE.L	ns_sampleptr(A2),D0
	CMP.L	A3,D0 ; at end of sample...
	BEQ.S	ScoNextChan
	MOVEQ.L	#0,D1
	MOVE.W	ns_period(A2),D1
	LSR.W	#1,D1
	BEQ.S	ScoNextChan
	MOVE.L	#35469,D2
	DIVU	D1,D2
	EXT.L	D2
	ADD.L	D2,D0
ScoChk	CMP.L	ns_endptr(A2),D0
	BLO.S	ScoUpdatePtr
	TST.L	ns_repeatptr(A2)
	BNE.S	ScoSamLoop
ScoSampleEnd
	MOVE.L	A3,D0
	BRA.S	ScoUpdatePtr

ScoSamLoop
	SUB.L	ns_endptr(A2),D0
	ADD.L	ns_repeatptr(A2),D0
	MOVE.L	ns_rependptr(A2),ns_endptr(A2)
	CMP.L	ns_endptr(A2),D0
	BHS	ScoSamLoop
ScoUpdatePtr
	MOVE.L	D0,ns_sampleptr(A2)
ScoNextChan
	ADD.L	#20,A2
	ADD.L	#44,A0
	DBRA	D6,ScoLoop

; now draw channels
	TST.B	ScopeEnable
	BEQ.S	clsnot
	CMP.W	#1,CurrScreen
	BNE.S	clsnot
	TST.B	EdEnable
	BNE.S	clsnot

	MOVE.L	TextBplPtr,A0
	ADD.L	#2256,A0
	MOVEQ	#0,D0
	MOVEQ	#32,D1
clscop1	MOVEQ	#5,D2
clscop2	MOVE.L	D0,(A0)+
	DBRA	D2,clscop2
	LEA	16(A0),A0
	DBRA	D1,clscop1

clsnot	MOVEQ	#-1,D4
	TST.W	SamScrEnable
	BEQ.S	ScoNClr
	MOVEQ	#0,D4
ScoNClr	MOVEQ	#0,D7
	MOVE.W	$DFF002,D6 ; dmaconr
	MOVEQ	#0,D5
	MOVE.L	#72*40+16,A1
	LEA	xBlankSample,A2
	BTST	#0,D6
	BEQ.S	ScoSkp1
	LEA	ScopeInfo,A2
	MOVE.B	ns_volume(A2),D5
ScoSkp1	BSR.S	ScoDraw

	MOVEQ	#0,D5
	MOVE.L	#72*40+22,A1
	LEA	xBlankSample,A2
	BTST	#1,D6
	BEQ.S	ScoSkp2
	LEA	ScopeInfo+20,A2
	MOVE.B	ns_volume(A2),D5
ScoSkp2	BSR.S	ScoDraw

	MOVEQ	#0,D5
	MOVE.L	#72*40+28,A1
	LEA	xBlankSample,A2
	BTST	#2,D6
	BEQ.S	ScoSkp3
	LEA	ScopeInfo+40,A2
	MOVE.B	ns_volume(A2),D5
ScoSkp3	BSR.S	ScoDraw

	MOVEQ	#0,D5
	MOVE.L	#72*40+34,A1
	LEA	xBlankSample,A2
	BTST	#3,D6
	BEQ.S	ScoSkp4
	LEA	ScopeInfo+60,A2
	MOVE.B	ns_volume(A2),D5
ScoSkp4	BSR	ScoDraw
	TST.L	D7
	BEQ	sdloscr
ScoRTS	RTS

ScoDraw	TST.B	ScopeEnable
	BEQ.S	sdlpos
	CMP.W	#1,CurrScreen
	BNE.S	sdlpos
	TST.B	EdEnable
	BNE.S	sdlpos
	LSR.W	#1,D5
	CMP.W	#32,D5
	BLS.S	sdsk1
	MOVEQ	#32,D5
sdsk1	MOVE.L	(A2),A0
	ADD.L	TextBplPtr,A1
	MOVEQ	#4,D2
sdlp1	MOVEQ	#7,D3
sdlp2	MOVE.B	(A0)+,D0	; get byte
	EXT.W	D0		; extend to word
	NEG.W	D0		; negate
	MULS	D5,D0		; multiply by volume
	ASR.W	#8,D0		; shift down
	MOVE.W	D0,D1
	ASL.W	#5,D0		; * 32
	ASL.W	#3,D1		; * 8
	ADD.W	D1,D0		; (32+8) = * 40
	BSET	D3,(A1,D0.W)	; set a bit
	DBRA	D3,sdlp2
	ADDQ.L	#1,A1		; yes
sdlskip	DBRA	D2,sdlp1

sdlpos	TST.B	D4
	BNE.S	ScoRTS
	LEA	xBlankSample(PC),A0
	CMP.L	A0,A2
	BEQ.S	sdloscr
	MOVE.L	(A2),D1
	MOVE.L	SamDrawStart(PC),D0
	CMP.L	D0,D1
	BLS.S	ScoRTS
	CMP.L	SamDrawEnd(PC),D1
	BHS.S	ScoRTS
	SUB.L	D0,D1
	MULU	#314,D1
	MOVE.L	SamDisplay,D0
	BEQ.S	ScoRTS
	DIVU	D0,D1
	ST	D4
	ST	D7
	MOVE.L	D1,D0
	ADDQ.W	#6,D0
	MOVE.W	#139,D1
sdlpspr	MOVEQ	#64,D2
	LEA	PlayPosSpriteData,A0
	JMP	SetSpritePos

sdloscr	MOVEQ	#0,D0
	MOVE.W	#270,D1
	BRA.S	sdlpspr

SetScope
	MOVEQ	#0,D1
	MOVE.B	n_samplenum(A0),D1
	SUBQ.W	#1,D1
	LSL.W	#4,D1
	LEA	ScopeSamInfo,A4
	LEA	(A4,D1.W),A4

	MOVE.L	n_start(A0),D0
	MOVE.L	D0,ns_sampleptr(A4)
	MOVEQ	#0,D1
	MOVE.W	n_length(A0),D1
	ADD.L	D1,D0
	ADD.L	D1,D0
	MOVE.L	D0,ns_endptr(A4)

	MOVE.L	n_loopstart(A0),D0
	MOVE.L	D0,ns_repeatptr(A4)
	MOVEQ	#0,D1
	MOVE.W	n_replen(A0),D1
	CMP.W	#1,D1
	BEQ.S	sconorep
	ADD.L	D1,D0
	ADD.L	D1,D0
	MOVE.L	D0,ns_rependptr(A4)
	BRA	Return1
sconorep
	CLR.L	ns_repeatptr(A4)
	RTS

ToggleAnaScope
	TST.W	notogfl
	BNE	return1
	BSR	WaitForButtonUp
	MOVEQ	#0,D4
	TST.B	AnaScopFlag
	BNE.S	tasana
	ST	AnaScopFlag
	BRA	RedrawAnaScope
tasana	SF	AnaScopFlag
	BRA	RedrawAnaScope

ScopeEnable	dc.b 0
AnaScopFlag	dc.b 1
xBlankSample	dc.l BlankSample
SamDrawStart	dc.l 0
SamDrawEnd	dc.l 0

;---- Show Free/Tune Memory ----

DoShowFreeMem
	MOVE.W	#1,UpdateFreeMem
ShowFreeMem
	MOVEM.L	D0-D7/A0-A6,-(SP)
	TST.W	UpdateFreeMem
	BEQ.S	sfmskp
	CLR.W	UpdateFreeMem
	CLR.L	FreeMemory
	CLR.L	TuneMemory
sfmskp	BSR	ShowTuneMem
	MOVEQ	#MEMF_CHIP,D1
	TST.B	ShowPublicFlag
	BEQ	sfmskp2
	MOVEQ	#MEMF_PUBLIC,D1
sfmskp2	MOVE.L	4.W,A6
	JSR	LVOAvailMem(A6)
	CMP.L	FreeMemory(PC),D0
	BEQ.S	fremend
	MOVE.L	D0,FreeMemory
	TST.B	ShowDecFlag
	BNE.S	shfrdec
	MOVE.L	D0,D6
	MOVE.W	#5273,TextOffset
	SWAP	D0
	AND.W	#$FF,D0
	MOVE.W	D0,WordNumber
	JSR	PrintHexByte
	MOVE.W	D6,WordNumber
	JSR	PrintHexWord
fremend	MOVEM.L	(SP)+,D0-D7/A0-A6
	RTS

shfrdec	MOVE.L	FreeMemory(PC),D0
	MOVE.W	#5273,TextOffset
	JSR	Print6DecDigits
	BRA.S	fremend

ShowTuneMem
	MOVE.L	SongDataPtr,A0
	LEA	42(A0),A0
	MOVE.W	TuneUp(PC),D7
	SUBQ.W	#1,D7
	MOVE.W	#31,TuneUp
	MOVEQ	#0,D0	; Zero length
stumeloop
	MOVEQ	#0,D1
	MOVE.W	(A0),D1
	ASL.W	#1,D1
	ADD.L	D1,D0	; Add samplelength
	ADD.L	#30,A0
	DBRA	D7,stumeloop
	ADD.L	#1084,D0	; Add 1084 to length
	MOVE.L	SongDataPtr,A0
	MOVEQ	#127,D7
	ADD.L	#952,A0
	MOVEQ	#0,D6
stumeloop2
	MOVE.B	(A0)+,D5
	CMP.B	D5,D6
	BHI.S	stumeskip
	MOVE.B	D5,D6
stumeskip
	DBRA	D7,stumeloop2
	ADDQ.W	#1,D6
	ASL.L	#8,D6
	ASL.L	#2,D6
	ADD.L	D6,D0	; Add 1024 x Number of patterns
	CMP.L	TuneMemory(PC),D0
	BEQ	Return1
	MOVE.L	D0,TuneMemory
	TST.B	ShowDecFlag
	BNE.S	shtudec
	MOVE.L	D0,D6
	MOVE.W	#4993,TextOffset
	SWAP	D0
	AND.W	#$FF,D0
	MOVE.W	D0,WordNumber
	JSR	PrintHexByte
	MOVE.W	D6,WordNumber
	JMP	PrintHexWord

shtudec	MOVE.L	TuneMemory(PC),D0
	MOVE.W	#4993,TextOffset
	JMP	Print6DecDigits

UpdateFreeMem	dc.w	0
FreeMemory	dc.l	0
TuneMemory	dc.l	0
TuneUp		dc.w	31

;---- Audio Channel Toggles ----

CheckToggle
	MOVE.W	MouseY2,D0
	CMP.W	#1,CurrScreen
	BNE	Return1
	CMP.W	#44,D0
	BHS	Return1
DoToggleMute
	CMP.W	#34,D0
	BHS.S	ToggleCh4
	CMP.W	#23,D0
	BHS.S	ToggleCh3
	CMP.W	#12,D0
	BHS.S	ToggleCh2
ToggleCh1
	LEA	audchan1toggle(PC),A0
	BRA.S	TogCh
ToggleCh2
	LEA	audchan2toggle(PC),A0
	BRA.S	TogCh
ToggleCh3
	LEA	audchan3toggle(PC),A0
	BRA.S	TogCh
ToggleCh4
	LEA	audchan4toggle(PC),A0
TogCh	CLR.B	RawKeyCode
	EOR.W	#1,(A0)
	BSR	WaitForButtonUp
	CLR.W	D0
	MOVE.W	audchan4toggle(PC),D0
	LSL.B	#1,D0
	OR.W	audchan3toggle(PC),D0
	LSL.B	#1,D0
	OR.W	audchan2toggle(PC),D0
	LSL.B	#1,D0
	OR.W	audchan1toggle(PC),D0
	MOVE.W	D0,ActiveChannels
	BSR.S	RedrawToggles
	BSR	Wait_4000
	BRA	Wait_4000

RedrawToggles
	CMP.W	#1,CurrScreen
	BNE	Return1
	LEA	audchan1toggle(PC),A0
	BSR.S	RedrawSingleTogg
	LEA	audchan2toggle(PC),A0
	BSR.S	RedrawSingleTogg
	LEA	audchan3toggle(PC),A0
	BSR.S	RedrawSingleTogg
	LEA	audchan4toggle(PC),A0
RedrawSingleTogg
	LEA	BitplaneData,A1
	MOVEQ	#0,D0
	MOVE.W	2(A0),D0
	ADD.L	D0,A1
	MOVEQ	#0,D0
	MOVE.W	4(A0),D0
	LEA	ToggleONdata,A2
	TST.W	(A0)
	BNE.S	rtskip
	LEA	ToggleOFFdata,A2
rtskip	ADD.L	D0,A2
	MOVEQ	#10,D4
rtloop2	MOVE.W	88(A2),10240(A1)
	MOVE.W	(A2)+,(A1)
	LEA	40(A1),A1
	DBRA	D4,rtloop2
	RTS

rtdoit	MOVEQ	#10,D4
rtloop	MOVE.W	132(A2),10240(A1)
	MOVE.W	(A2)+,(A1)
	LEA	40(A1),A1
	DBRA	D4,rtloop
	RTS

;---- VU Meters ----

VUmeters
	LEA	audchan1temp,A1
	LEA	VUSpriteData1,A2
	MOVE.W	audchan1toggle(PC),D0
	BSR.S	OneVUmeter
	LEA	audchan2temp,A1
	LEA	VUSpriteData2,A2
	MOVE.W	audchan2toggle(PC),D0
	BSR.S	OneVUmeter
	LEA	audchan3temp,A1
	LEA	VUSpriteData3,A2
	MOVE.W	audchan3toggle,D0
	BSR.S	OneVUmeter
	LEA	audchan4temp,A1
	LEA	VUSpriteData4,A2
	MOVE.W	audchan4toggle,D0
OneVUmeter
	TST.L	RunMode
	BEQ.S	VUmeterDown
	TST.W	(A1)
	BEQ.S	VUmeterDown
	TST.W	D0
	BEQ.S	VUmeterDown
	TST.L	Counter
	BNE.S	VUmeterDown
	MOVEQ	#0,D0	; New note
	MOVE.B	n_volume(A1),D0	; Get volume
	CMP.W	#$40,D0	; Higher than $40 ?
	BLS.S	iccskip	; No, skip
	MOVEQ	#$40,D0	; Yes, set to $40
iccskip	LEA	VUmeterHeights(PC),A3
	MOVE.B	#233,D7		; Set to 233
	SUB.B	0(A3,D0.W),D7	; Subtract to get new height
	MOVE.B	D7,(A2)
	RTS

VUmeterDown
	CMP.B	#233,(A2)
	BEQ	Return1
	ADDQ.B	#1,(A2)
	RTS	

audchan1toggle	dc.w	1,78,0
Chan1Volume	dc.w	0
audchan2toggle	dc.w	1,518,$16
Chan2Volume	dc.w	0
audchan3toggle	dc.w	1,958,$2C
Chan3Volume	dc.w	0
audchan4toggle	dc.w	1,1398,$42
Chan4Volume	dc.w	0
ActiveChannels	dc.w	15

VUmeterHeights
	dc.b	0,0,1,2,2,3,4,5,5,6,7,8,8,9,10,11
	dc.b	11,12,13,14,14,15,16,17,17,18,19,20,20,21,22,23
	dc.b	23,24,25,26,26,27,28,29,29,30,31,32,32,33,34,35
	dc.b	35,36,37,38,38,39,40,41,41,42,43,44,44,45,46,47
	dc.b	47,0

SetAnalyzerVolumes
	MOVEM.L	D0-D3/A0,-(SP)
	LSL.W	#8,D2
	DIVU	#640,D2
	CMP.W	#$F0A0,A5
	BNE.S	savCh2
	TST.W	audchan1toggle
	BEQ.S	savCh2
	CMP.W	Chan1Volume,D2 ; volume
	BLO.S	savCh2
	MOVE.W	D2,Chan1Volume
savCh2	CMP.W	#$F0B0,A5
	BNE.S	savCh3
	TST.W	audchan2toggle
	BEQ.S	savCh3
	CMP.W	Chan2Volume,D2
	BLO.S	savCh3
	MOVE.W	D2,Chan2Volume
savCh3	CMP.W	#$F0C0,A5
	BNE.S	savCh4
	TST.W	audchan3toggle
	BEQ.S	savCh4
	CMP.W	Chan3Volume,D2
	BLO.S	savCh4
	MOVE.W	D2,Chan3Volume
savCh4	CMP.W	#$F0D0,A5
	BNE.S	savNone
	TST.W	audchan4toggle
	BEQ.S	savNone
	CMP.W	Chan4Volume,D2
	BLO.S	savNone
	MOVE.W	D2,Chan4Volume
savNone	MOVEM.L	(SP)+,D0-D3/A0
	MOVEQ	#1,D4
	RTS

;---- Disk Op. ----

DiskOp	CLR.B	RawKeyCode
	MOVE.W	CurrScreen,D0
	CMP.W	#3,D0
	BEQ	ExitFromDir
	CMP.W	#1,D0
	BNE	Return1
ShowDirScreen
	BSR	WaitForButtonUp
	MOVE.W	#3,CurrScreen
	ST	DisableAnalyzer
	ST	NoSampleInfo
	JSR	ClearAnalyzerColors
	BSR	Clear100Lines
	BSR.S	SwapDirScreen
	BEQ	DisplayMainAll
	BSR	ShowDiskSpace
	BSR	ShowModPackMode
	BSR	ShowPackMode
	BSR	ShowRAWIFFmode
DoAutoDir
	TST.B	AutoDirFlag
	BEQ	SelectPath
	MOVE.W	DirPathNum,D0 
	BEQ	LoadModuleGadg
	CMP.W	#1,D0
	BEQ	LoadSongGadg
	CMP.W	#2,D0
	BEQ	LoadSampleGadg
	BRA	SelectModules
SelectPath
	MOVE.W	DirPathNum,D0 
	BEQ	SelectModules
	CMP.W	#1,D0
	BEQ	SelectSongs
	BRA	SelectSamples

SwapDirScreen
	MOVE.L	DecompMemPtr,D0
	BEQ.S	sdirs2
	MOVE.L	D0,A1
	BSR.S	ssets3
	BRA	FreeDecompMem
sdirs2	LEA	DirScreenData,A0
	MOVE.L	#DirScreenSize,D0
	BSR	Decompact
	BEQ	Return1
ssets3	LEA	BitplaneData,A0
	MOVEQ	#1,D2
sxloop1	MOVE.W	#999,D0
sxloop2	MOVE.L	(A0),D1
	MOVE.L	(A1),(A0)+
	MOVE.L	D1,(A1)+
	DBRA	D0,sxloop2
	LEA	6240(A0),A0
	DBRA	D2,sxloop1
	RTS

CheckDirGadgets
	MOVEM.W	MouseX2,D0/D1
	CMP.W	#44,D1
	BHI	CheckDirGadgets2
	CMP.W	#33,D1
	BLS.S	ExtrasMenu
	CMP.W	#11,D0
	BLO	DirBrowseGadg
	CMP.W	#187,D0
	BLO	DirPathGadg
	CMP.W	#216,D0
	BLO	ParentDirGadg
	CMP.W	#307,D0
	BLO	ShowFreeDiskGadg
	BRA	CheckDirGadgets2

ExtrasMenu
	CMP.W	#94,D0
	BHS	ToggleMenu
	CMP.W	#22,D1
	BHI.S	DeleteFileGadg
	CMP.W	#11,D1
	BHI.S	RenameFileGadg
	BRA	DiskFormatGadg

RenameFileGadg
	BSR	StorePtrCol
	BSR	WaitForButtonUp
	BSR	ClearFileNames
	MOVE.L	PathPtr,A4
	BSR	ShowDirPath
	LEA	FileNamesPtr(PC),A5
	BSR	HasDiskChanged
	BEQ.S	RenameFileDirOk
	BSR	ClearDirTotal
	BSR	DirDisk
	BNE	RestorePtrCol

RenameFileDirOk
	MOVE.W	FileNameScrollPos,D0
	BSR	RedrawFileNames
	MOVE.W	#10,Action
	LEA	SelectFileText,A0
	JSR	ShowStatusText
	BRA	RestorePtrCol

DeleteFileGadg
	MOVE.W	DirPathNum,D0
	BEQ	DeleteModuleGadg
	CMP.W	#1,D0
	BEQ	DeleteSongGadg
	CMP.W	#2,D0
	BEQ	DeleteSampleGadg
	RTS

ToggleMenu
	CMP.W	#146,D0
	BHS	SelectMenu
	CMP.W	#22,D1
	BHI	ToggleIFFSave
	CMP.W	#11,D1
	BHI.S	ToggleSongPack
ToggleModPack
	CLR.B	ModPackMode
	BSR	ShowNotImpl
ShowModPackMode
	LEA	ToggleOFFText(PC),A0
	TST.B	ModPackMode
	BEQ.S	smpmskp
	LEA	ToggleONText2(PC),A0
smpmskp	MOVEQ	#3,D0
	MOVE.W	#175,D1
	JSR	ShowText3
	BRA	WaitForButtonUp

ShowNotImpl
	LEA	NotImplText(PC),A0
	JSR	ShowStatusText
	BRA	SetErrorPtrCol

NotImplText	dc.b	'Not implemented',0

ToggleSongPack
	EOR.B	#1,PackMode
ShowPackMode
	LEA	ToggleOFFText(PC),A0
	TST.B	PackMode
	BEQ.S	spsskip
	LEA	ToggleONText2(PC),A0
spsskip	MOVEQ	#3,D0
	MOVE.W	#615,D1
	JSR	ShowText3
	BRA	WaitForButtonUp

ToggleIFFSave
	EOR.B	#1,RAWIFFMode
ShowRAWIFFMode
	LEA	RAWText(PC),A0
	TST.B	RAWIFFMode
	BEQ.S	sraffu
	LEA	IFFText(PC),A0
sraffu	MOVEQ	#3,D0
	MOVE.W	#1055,D1
	JSR	ShowText3
	BRA	WaitForButtonUp

RAWText	dc.b "RAW",0
IFFText	dc.b "IFF",0

SelectMenu
	CMP.W	#156,D0
	BHS	LoadMenu
	CMP.W	#22,D1
	BHI	SelectSamples
	CMP.W	#11,D1
	BHI	SelectSongs
	BRA	SelectModules

LoadMenu
	CMP.W	#238,D0
	BHS.S	SaveMenu
	CMP.W	#22,D1
	BHI	LoadSampleGadg
	CMP.W	#11,D1
	BHI	LoadSongGadg
	BRA	LoadModuleGadg

SaveMenu
	CMP.W	#22,D1
	BHI	SaveSampleGadg
	CMP.W	#11,D1
	BHI	SaveSongGadg
	BRA	SaveModuleGadg

CheckDirGadgets2
	MOVEM.W	MouseX2,D0/D1
	CMP.W	#3,CurrScreen
	BNE	Return1
	CMP.W	#307,D0
	BLO	FileNamePressed
	CMP.W	#44,D1
	BLS	FileNameOneUp
	CMP.W	#89,D1
	BLO.S	ExitFromDir
	BRA	FileNameOneDown

ExitFromDir
	BSR	WaitForButtonUp
	CLR.B	RawKeyCode
	BSR	SwapDirScreen
	BSR	Clear100Lines
DisplayMainAll
	BSR	DisplayMainScreen
	JSR	ShowSongLength
	JSR	ShowSampleInfo
	JSR	ShowSongName
	JSR	Show_MS
	TST.W	LoadInProgress
	BNE.S	dimaskp
	JSR	ShowAllRight
dimaskp	JMP	ShowPosition

DirBrowseGadg
	MOVEQ	#0,D3
	MOVE.W	DirPathNum,D3
	LEA	dpnum(PC),A0
	ADD.L	D3,A0
	MOVEQ	#0,D0
	MOVE.B	(A0),D0
	ADDQ.B	#1,D0
	CMP.B	#7,D0
	BLO.S	dbgskip
	MOVEQ	#0,D0
dbgskip	MOVE.B	D0,(A0)
	MULU	#5,D0
	MOVE.L	PathPtr,A0
	MOVEQ	#63,D1
	MOVEQ	#0,D2
dbglop1	MOVE.B	D2,(A0)+
	DBRA	D1,dbglop1
	LEA	dirpaths(PC),A0
	LEA	-5(A0,D0.W),A0
	TST.W	D0
	BNE.S	dbgskp2
	LEA	ModulesPath,A0
	TST.W	D3
	BEQ.S	dbgskp2
	LEA	SongsPath,A0
	CMP.W	#1,D3
	BEQ.S	dbgskp2
	LEA	SamplePath,A0
dbgskp2	MOVE.L	PathPtr,A1
dbglop2	MOVE.B	(A0)+,(A1)+
	BNE.S	dbglop2
	BSR	ShowDirPath
	MOVE.W	#3,WaitTime
	BRA	WaitALittle

dirpaths
	dc.b "DF0:",0,"DF1:",0,"DH0:",0,"DH1:",0,"RAM:",0,"ST-00:",0
dpnum	dc.b 0,0,0,0
DirPathNum
	dc.w 3

SelectModules
	MOVEQ	#0,D0
	BRA.S	ChangePath
SelectSongs
	MOVEQ	#1,D0
	BRA.S	ChangePath
SelectSamples
	MOVEQ	#2,D0
ChangePath
	MOVE.L	TextBplPtr,A0
	LEA	178(A0),A0
	MOVEQ	#0,D2
	MOVEQ	#26,D1
chpalop	MOVE.W	D2,(A0)
	LEA	40(A0),A0
	DBRA	D1,chpalop
	LEA	ModulesPath2,A4
	MOVE.W	#178,D1
	MOVE.W	D0,DirPathNum
	BEQ.S	chpaski
	LEA	SongsPath2,A4
	MOVE.W	#618,D1
	CMP.W	#1,D0
	BEQ.S	chpaski
	LEA	SamplePath2,A4
	MOVE.W	#1058,D1
chpaski	MOVE.L	TextBplPtr,A0
	LEA	(A0,D1.W),A0
	MOVE.W	#$0100,(A0)
	MOVE.W	#$0F80,40(A0)
	MOVE.W	#$0FC0,80(A0)
	MOVE.W	#$0F80,120(A0)
	MOVE.W	#$0100,160(A0)
	MOVE.L	A4,PathPtr
	BSR	ShowDirPath
	BRA	WaitForButtonUp

;---- Song Gadgets ----

LoadSongGadg
	BSR	StorePtrCol
	BSR	WaitForButtonUp
	BSR	ClearFileNames
	BSR	SelectSongs
	LEA	FileNamesPtr(PC),A5
	BSR	HasDiskChanged
	BEQ	LoadSongDirOk
	BSR	ClearDirTotal
	BSR	DirDisk
	BNE	RestorePtrCol
LoadSongDirOk
	MOVE.W	FileNameScrollPos,D0
	BSR	RedrawFileNames
	MOVE.W	#1,Action
	LEA	SelectSongText,A0
	JSR	ShowStatusText
	BRA	RestorePtrCol

SaveSongGadg
	BSR	SelectSongs
	LEA	SaveSongText,A0
	BSR	AreYouSure
	BNE	Return1
	BSR	RestorePtrCol
	BSR	ClearFileNames
	BSR	StopIt
	JSR	SaveSong
	MOVE.W	#1,Action
	RTS
 
DeleteSongGadg
	BSR	WaitForButtonUp
	BSR	ClearFileNames
	BSR	SelectSongs
	LEA	FileNamesPtr(PC),A5
	BSR	HasDiskChanged
	BEQ.S	DeleteSongDirOk
	BSR	ClearDirTotal
	BSR	DirDisk
	BEQ.S	DeleteSongDirOk
	BRA	RestorePtrCol

DeleteSongDirOk
	MOVE.W	FileNameScrollPos,D0
	BSR	RedrawFileNames
	MOVE.W	#2,Action
	LEA	SelectSongText,A0
	JSR	ShowStatusText
	BRA	SetDeletePtrCol

;---- Module Gadgets ----

LoadModuleGadg
	BSR	StorePtrCol
	BSR	WaitForButtonUp
	BSR	ClearFileNames
	BSR	SelectModules
	LEA	FileNamesPtr(PC),A5
	BSR	HasDiskChanged
	BEQ.S	LoadModDirOk
	BSR	ClearDirTotal
	BSR	DirDisk
	BEQ.S	LoadModDirOk
	BRA	RestorePtrCol

LoadModDirOk
	MOVE.W	FileNameScrollPos,D0
	BSR	RedrawFileNames
	MOVE.W	#3,Action
	LEA	SelectModuleText,A0
	JSR	ShowStatusText
	BRA	RestorePtrCol

SaveModuleGadg
	BSR	SelectModules
	LEA	SaveModuleText,A0
	BSR	AreYouSure
	BNE	Return1
	JSR	SaveModule
	MOVE.W	#3,Action
	JMP	ShowAllRight

DeleteModuleGadg
	BSR	WaitForButtonUp
	BSR	ClearFileNames
	BSR	SelectModules
	LEA	FileNamesPtr(PC),A5
	BSR	HasDiskChanged
	BEQ.S	DeleteModDirOk
	BSR	ClearDirTotal
	BSR	DirDisk
	BEQ.S	DeleteModDirOk
	BRA	RestorePtrCol

DeleteModDirOk
	MOVE.W	FileNameScrollPos(PC),D0
	BSR	RedrawFileNames
	MOVE.W	#4,Action
	LEA	SelectModuleText,A0
	JSR	ShowStatusText
	BRA	SetDeletePtrCol

;---- Sample Gadgets ----

LoadSampleGadg
	BSR	StorePtrCol
	BSR	WaitForButtonUp
	BSR	ClearFileNames
	BSR	SelectSamples
	LEA	FileNamesPtr(PC),A5
	BSR	HasDiskChanged
	BEQ.S	LoadSampleDirOk
	BSR	ClearDirTotal
	BSR	DirDisk
	BEQ.S	LoadSampleDirOk
	BRA	RestorePtrCol

LoadSampleDirOk
	MOVE.W	FileNameScrollPos,D0
	BSR	RedrawFileNames
	MOVE.W	#5,Action
	LEA	SelectSampleText,A0
	JSR	ShowStatusText
	BRA	RestorePtrCol

SaveSampleGadg
	BSR	SelectSamples
	CLR.B	RawKeyCode
	MOVE.W	InsNum,D0
	BEQ	NotSampleNull
	LEA	SaveSampleText,A0
	BSR	AreYouSure
	BNE	Return1
dosavesample
	BSR	StorePtrCol
	BSR	CreateSampleName
	MOVE.L	DiskDataLength,D1
	BEQ	Return1
	MOVEQ	#0,D1
	MOVE.W	InsNum,D1
	LSL.L	#2,D1
	LEA	SamplePtrs,A0
	MOVE.L	0(A0,D1.W),DiskDataPtr

	BSR	SetDiskPtrCol
	MOVE.L	DOSbase,A6
	MOVE.L	FileNamePtr,D1
	MOVE.L	#1006,D2
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BNE	SaveSample
	JSR	CantOpenFile
	BRA	ErrorRestoreCol

SaveSample
	LEA	SavingSampleText,A0
	JSR	ShowStatusText
	TST.B	RAWIFFMode
	BEQ	savesam
	MOVE.L	FileHandle,D1
	MOVE.L	#IFFFORM,D2
	MOVEQ	#IFFEND-IFFFORM,D3
	JSR	LVOWrite(A6)
savesam	MOVE.L	FileHandle,D1
	MOVE.L	DiskDataPtr,D2
	MOVE.L	DiskDataLength,D3
	JSR	LVOWrite(A6)

	MOVE.L	FileHandle,D1
	JSR	LVOClose(A6)
	CLR.L	FileHandle
	MOVE.W	#5,Action
	JSR	ShowAllRight
	BRA	RestorePtrCol

IFFFORM	dc.b	"FORM"
	dc.l	0
	dc.b	"8SVX"
IFFVHDR	dc.b	"VHDR"
	dc.l	20
	dc.l	0,0,32	; oneshot, repeat, hisamples
	dc.w	16726	; This is really NTSC (16574 for PAL!!!)
	dc.b	1,0	; octaves, compression
	dc.l	$10000	; volume
IFFNAME	dc.b	"NAME"
	dc.l	24
	dcb.b	24,0
	dc.b	"ANNO"
	dc.l	16
	dc.b	"Protracker 1.2A",0
IFFBODY	dc.b	"BODY"
	dc.l	0
IFFEND


;---- Delete Sample ----

DeleteSampleGadg
	BSR	StorePtrCol
	BSR	WaitForButtonUp
	BSR	ClearFileNames
	BSR	SelectSamples
	LEA	FileNamesPtr(PC),A5
	BSR	HasDiskChanged
	BEQ.S	DeleteSamDirOk
	BSR	ClearDirTotal
	BSR	DirDisk
	BEQ.S	DeleteSamDirOk
	BRA	RestorePtrCol

DeleteSamDirOk
	MOVE.W	FileNameScrollPos(PC),D0
	BSR	RedrawFileNames
	MOVE.W	#6,Action
	LEA	SelectSampleText,A0
	JSR	ShowStatusText
	BRA	SetDeletePtrCol

;---- Directory Path Gadget ----

DirPathGadg
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVE.L	PathPtr,A6
	MOVE.L	A6,ShowTextPtr
	MOVE.L	A6,TextEndPtr
	ADD.L	#63,TextEndPtr
	MOVE.W	#19,TextLength
	MOVE.W	#1484,A4
	BSR	GetTextLine
	BRA	RestorePtrCol


CopyPath
	LEA	FileName,A1
	TST.B	(A0)		; If no path
	BEQ	Return1
cploop	MOVE.B	(A0)+,(A1)+	; Copy path to filename
	BNE.S	cploop
	CMP.B	#':',-2(A1)	; If ending with ':' it's ok
	BEQ.S	PathCharBack
	CMP.B	#'/',-2(A1)	; If ending with '/' it's ok
	BEQ.S	PathCharBack
	MOVE.B	#'/',-1(A1)	; Add '/' to end path
	RTS

PathCharBack
	SUBQ.L	#1,A1
	RTS

ShowDirPath
	CMP.W	#3,CurrScreen
	BNE	Return1
	MOVEM.L	D0-D7/A0-A6,-(SP)
	MOVE.L	PathPtr,A0
	MOVEQ	#19,D0
	MOVE.W	#1484,D1
	JSR	ShowText3
	MOVEM.L	(SP)+,D0-D7/A0-A6
	RTS

;---- File List Gadgets ----

FileNameOneUp
	TST.W	Action
	BEQ	Return1
	LEA	FileNamesPtr(PC),A5
	MOVE.W	FileNameScrollPos(PC),D0
	BEQ	Return1
	SUBQ.W	#1,D0
	BTST	#2,$DFF016
	BNE	RedrawFileNames
	SUBQ.W	#3,D0
	BPL	RedrawFileNames
	MOVEQ	#0,D0
	BRA	RedrawFileNames

FileNameOneDown
	TST.W	Action
	BEQ	Return1
	LEA	FileNamesPtr(PC),A5
	MOVE.W	FileNameScrollPos(PC),D0
	ADDQ.W	#1,D0
	BTST	#2,$DFF016
	BNE.S	fnod2
	ADDQ.W	#3,D0
fnod2	MOVE.W	16(A5),D1
	SUBQ.W	#8,D1
	BMI	Return1
	CMP.W	D1,D0
	BLS	RedrawFileNames
	MOVE.W	D1,D0
	BRA	RedrawFileNames

;---- Clicked on a filename ----

FileNamePressed
	SUB.W	#44,D1
	MOVE.W	#1,UpdateFreeMem
	TST.W	Action
	BEQ	Return1
	CMP.W	#3,D1
	BLO	Return1
	CMP.W	#50,D1
	BHI	Return1
	SUBQ.W	#3,D1
	AND.L	#$FFFF,D1
	DIVU	#6,D1
	MOVE.W	D1,FileNameScrollPos+2
	LEA	FileNamesPtr(PC),A5
	MOVE.W	FileNameScrollPos(PC),D0
	ADD.W	D1,D0
	CMP.W	16(A5),D0
	BHS	Return1
	MULU	#36,D0
	ADD.L	(A5),D0
	MOVE.L	D0,A0
	TST.L	32(A0)
	BMI.S	AddDirectory
	MOVEQ	#DirNameLength-1,D0
	LEA	DirInputName,A1
fnploop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,fnploop
	MOVE.W	Action,D6
	CMP.W	#1,D6
	BEQ	LoadSong
	CMP.W	#2,D6
	BEQ	DeleteSong
	CMP.W	#3,D6
	BEQ	xLoadModule
	CMP.W	#4,D6
	BEQ	DeleteModule
	CMP.W	#5,D6
	BEQ	LoadSample
	CMP.W	#6,D6
	BEQ	DeleteSample
	CMP.W	#10,D6
	BEQ	RenameFile
	RTS

xLoadModule	JMP	LoadModule

AddDirectory
	MOVE.L	A0,-(SP)
	MOVE.L	PathPtr,A0
	BSR	CopyPath
	MOVE.L	(SP)+,A0
	MOVEQ	#DirNameLength-1,D0
addplop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,addplop
	LEA	FileName,A0
	MOVE.L	PathPtr,A1
	MOVEQ	#62,D0
addplp2	MOVE.B	(A0)+,(A1)+
	DBRA	D0,addplp2
addpdir	BSR	ShowDirPath
	MOVE.W	Action,D6
	CMP.W	#1,D6
	BEQ	LoadSongGadg
	CMP.W	#2,D6
	BEQ	DeleteSongGadg
	CMP.W	#3,D6
	BEQ	LoadModuleGadg
	CMP.W	#4,D6
	BEQ	DeleteModuleGadg
	CMP.W	#5,D6
	BEQ	LoadSampleGadg
	CMP.W	#6,D6
	BEQ	DeleteSampleGadg
	CMP.W	#10,D6
	BEQ	RenameFileGadg
	RTS

ParentDirGadg
	BSR	WaitForButtonUp
	MOVE.L	PathPtr,A0
	MOVE.L	A0,A1
pdgloop	TST.B	(A1)+
	BNE.S	pdgloop
	SUBQ.L	#1,A1
	CMP.L	A0,A1
	BLS	Return1
	SUBQ.L	#1,A1
	CMP.B	#'/',(A1)
	BNE.S	pdgskp1
	CLR.B	(A1)
pdgskp1	CMP.B	#':',(A1)
	BEQ.S	addpdir
	CMP.B	#'/',(A1)
	BEQ	pdgslsh
	CLR.B	(A1)
	CMP.L	A0,A1
	BLS.S	addpdir
	SUBQ.L	#1,A1
	BRA.S	pdgskp1

pdgslsh	CLR.B	(A1)
	BRA	addpdir

FileNamesPtr
	dc.l	0  ; A5+ 0
	dc.l	0  ;   + 4
	dc.l	0  ;   + 8
	dc.l	0  ;   +12
	dc.w	0  ;   +16
	dc.w	24 ;   +18
FileNameScrollPos
	dc.w	0
	dc.w	0

;---- Has Disk Changed ----

HasDiskChanged
	MOVE.L	DOSbase,A6
	MOVE.L	A4,D1
	MOVEQ	#-2,D2
	JSR	LVOLock(A6)
	MOVE.L	D0,FileLock
	BEQ.S	ExamineError
	MOVE.L	FileLock,D1
	MOVE.L	#FileInfoBlock,D2
	JSR	LVOExamine(A6)
	TST.L	D0
	BEQ.S	ExamineError
	LEA	FileInfoBlock,A0
	MOVE.L	ofib_DateStamp+ds_Days(A0),D0
	CMP.L	4(A5),D0
	BNE.S	DiskChanged
	MOVE.L	ofib_DateStamp+ds_Minute(A0),D0
	CMP.L	8(A5),D0
	BNE.S	DiskChanged
	MOVE.L	ofib_DateStamp+ds_Tick(A0),D0
	CMP.L	12(A5),D0
	BNE.S	DiskChanged
	MOVE.L	FileLock,D1
	JSR	LVOUnLock(A6)
	MOVEQ	#0,D0
	RTS	
 
DiskChanged
	MOVE.L	ofib_DateStamp+ds_Days(A0),4(A5)
	MOVE.L	ofib_DateStamp+ds_Minute(A0),8(A5)
	MOVE.L	ofib_DateStamp+ds_Tick(A0),12(A5)
	MOVE.L	FileLock,D1
	JSR	LVOUnLock(A6)
	MOVEQ	#-1,D0
	RTS

ExamineError
	CLR.L	4(A5)
	MOVEQ	#-1,D0
	RTS

ShowFreeDiskGadg
	BSR	WaitForButtonUp
	BSR.S	LockAndGetInfo
	BSR	DirDiskUnlock
	JMP	ShowAllRight

LockAndGetInfo
	BSR	StorePtrCol
	BSR	SetDiskPtrCol
	CLR.W	FileNameScrollPos
	MOVE.L	DOSbase,A6
	MOVE.L	A4,D1	; Dir-path in A4
	MOVEQ	#-2,D2
	JSR	LVOLock(A6)
	MOVE.L	D0,FileLock
	BEQ	DirDiskError
	LEA	ReadingDirText,A0
	JSR	ShowStatusText
	MOVE.L	FileLock,D1
	MOVE.L	#InfoData,D2
	JSR	LVOInfo(A6)
	LEA	InfoData,A0
	MOVE.L	12(A0),D0 ; id_NumBlocks
	SUB.L	16(A0),D0 ; id_NumBlocksUsed
	MULU	22(A0),D0 ; id_BytesPerBlock + 2
	MOVE.L	D0,FreeDiskSpace
ShowDiskSpace
	MOVE.W	#1510,TextOffset
	MOVE.L	FreeDiskSpace,D7
	TST.B	ShowDecFlag
	BNE.S	sdsdec
	SWAP	D7
	MOVE.W	D7,WordNumber
	JSR	PrintHexWord
	SWAP	D7
	MOVE.W	D7,WordNumber
	JSR	PrintHexWord
	MOVEQ	#0,D0
	RTS

sdsdec	DIVU	#10000,D7
	MOVE.W	D7,WordNumber
	JSR	Print4DecDigits
	SWAP	D7
	MOVE.W	D7,WordNumber
	JSR	Print4DecDigits
	MOVEQ	#0,D0
	RTS

;---- Get Disk Directory ----

AllocDirMem
	ADD.W	#50,DirEntries
	MOVE.W	DirEntries,D0
	MULU	#36,D0
	MOVE.L	DirAllocSize(PC),D6
	MOVE.L	D0,DirAllocSize
	MOVE.L	#MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,D7
	BEQ.S	baehsj

	MOVE.L	FileNamesPtr(PC),D1
	MOVE.L	D7,FileNamesPtr
	TST.L	D1
	BEQ	Return1
	TST.L	D6
	BEQ	Return1
	MOVE.L	D1,A0
	MOVE.L	D7,A1
	MOVE.L	D6,D0
admloop	MOVE.B	(A0)+,(A1)+
	SUBQ.L	#1,D6
	BNE.S	admloop
	MOVE.L	D1,A1
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	RTS

FreeDirMem
	MOVE.L	FileNamesPtr(PC),D1
	BEQ	Return1
	MOVE.L	D1,A1
	MOVE.L	DirAllocSize(PC),D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	CLR.L	FileNamesPtr
	CLR.W	DirAllocSize
	CLR.W	DirEntries
	RTS

baehsj	BSR	OutOfMemErr
	MOVEQ	#-1,D0
	RTS

DirAllocSize	dc.l	0
DirEntries	dc.w	0

DirDisk	BSR	FreeDirMem
	BSR	AllocDirMem
	BSR	LockAndGetInfo
	BNE	Return1
	MOVE.L	FileLock,D1
	MOVE.L	#FileInfoBlock,D2
	JSR	LVOExamine(A6)
	TST.L	D0
	BEQ	DirDiskError
ddloop1	MOVE.L	FileLock,D1
	MOVE.L	#FileInfoBlock,D2
	MOVE.L	DOSBase,A6
	JSR	LVOExNext(A6)
	TST.L	D0
	BEQ	DirDiskUnlock
	BTST	#2,$DFF016
	BEQ.S	AbortDir
	BSR	NewDirEntry
	BRA.S	ddloop1

AbortDir
	CLR.L	4(A5)
	LEA	DirAbortedText(PC),A0
	JSR	ShowStatusText
	BSR.S	DirDiskUnlock
	BSR	WaitALittle
	JSR	ShowAllRight
	MOVEQ	#0,D0
	RTS

DirDiskUnlock
	MOVE.L	FileLock,D1
	JSR	LVOUnLock(A6)
	BSR	RestorePtrCol
	MOVEQ	#0,D0
	RTS

DirAbortedText	dc.b 'dir aborted !',0

DirDiskError
	TST.L	FileLock
	BEQ.S	ddeskip
	MOVE.L	FileLock,D1
	JSR	LVOUnLock(A6)
ddeskip	BSR	RestorePtrCol
	LEA	CantFindDirText,A0
	JSR	ShowStatusText
	BSR	SetErrorPtrCol
	MOVEQ	#-1,D0
	RTS

ClearDirTotal
	CLR.W	16(A5)
	RTS	

CantFindDirText	dc.b "can't find dir !",0,0

 
NewDirEntry
	LEA	FIB_FileName,A0
	TST.B	ShowDirsFlag
	BNE.S	ndeok1
	TST.L	FIB_EntryType
	BPL	Return1
ndeok1	TST.L	FIB_EntryType
	BPL	ndeok2
	TST.B	ModOnlyFlag
	BEQ.S	ndeok2
	TST.W	DirPathNum
	BNE.S	ndeok2
	MOVE.L	(A0),D0
	AND.L	#$CFCFCFCF,D0
	CMP.L	#$4D4F440E,D0 ; MOD.
	BNE	Return1
ndeok2	MOVE.W	16(A5),D0
	CMP.W	DirEntries,D0
	BLO.S	ndeok3
	MOVE.L	A0,-(SP)
	BSR	AllocDirMem
	MOVE.L	(SP)+,A0
ndeok3	MOVE.W	16(A5),D6
	BEQ.S	ndeadd1		; If first entry
	SUBQ.W	#1,D6
	MOVE.L	(A5),A1
ndeloopname
	MOVEQ	#0,D2
	MOVE.L	FIB_EntryType,D0
	MOVE.L	32(A1),D1
	TST.L	D0
	BPL.S	ndesfil	; if directory, all is well
	TST.L	D1
	BMI.S	ndenext	; was file, so skip if directory
	BRA.S	ndelopc
ndesfil	TST.L	D1	; if file
	BPL.S	ndeinse
ndelopc	MOVE.B	(A0,D2.W),D0 ; Get a character
	BEQ.S	ndeinse
	CMP.B	#96,D0  ; Lowercase?
	BLO.S	ndeskp1
	SUB.B	#32,D0	; Switch to upper
ndeskp1	MOVE.B	(A1,D2.W),D1
	BEQ.S	ndenext
	CMP.B	#96,D1
	BLO.S	ndeskp2
	SUB.B	#32,D1
ndeskp2	CMP.B	D0,D1
	BHI.S	ndeinse
	BNE.S	ndenext
	ADDQ.W	#1,D2
	BRA.S	ndelopc
ndenext	LEA	36(A1),A1	; next entry
	DBRA	D6,ndeloopname	; loop entries
	MOVE.L	(A5),A1
	MOVE.W	16(A5),D0
	MULU	#36,D0
	ADD.W	D0,A1
	BRA.S	ndeadd2

ndeinse	MOVE.L	(A5),A2
	MOVE.W	16(A5),D0
	MULU	#36,D0
	ADD.W	D0,A2
	MOVE.L	A2,A3
	LEA	36(A3),A3
nde3loop
	MOVE.W	-(A2),-(A3)
	CMP.L	A2,A1
	BNE.S	nde3loop
	BRA.S	ndeadd2
 
ndeadd1	MOVE.L	(A5),A1            ; Put new filename into list
ndeadd2	LEA	FIB_FileName,A0
	MOVE.L	A1,A3
	MOVEQ	#35,D0             ; Clear old filename
nde4loop
	CLR.B	(A3)+
	DBRA	D0,nde4loop
	MOVE.W	FIB_DateStamp+ds_Days+2,30(A1)
	MOVEQ	#-1,D0
	TST.L	FIB_EntryType
	BPL	ndefskp
	MOVE.L	FIB_FileSize,D0
ndefskp	MOVE.L	D0,32(A1)
	MOVEQ	#29,D0             ; Copy new filename
nde4loop2
	MOVE.B	(A0)+,D1
	MOVE.B	D1,(A1)+
	TST.B	D1
	BEQ.S	nde4skip
	DBRA	D0,nde4loop2
nde4skip
	ADDQ.W	#1,16(A5)	; Files + 1
	RTS	
 
RedrawFileNames
	MOVE.W	D0,-(SP)
	BSR	ShowDirPath
	MOVE.W	(SP)+,D0
	MOVE.W	D0,FileNameScrollPos
	TST.W	16(A5)
	BEQ	Return1
	MOVE.L	#1881,A6
	MOVE.W	A6,TextOffset
	MOVE.L	(A5),D6
	MULU	#36,D0
	ADD.L	D0,D6
	MOVE.W	16(A5),D0
	SUB.W	FileNameScrollPos,D0
	CMP.W	#8,D0
	BHS.S	ShowFileNames
	SUBQ.W	#1,D0
	MOVE.W	D0,D7
	BRA.S	sfnloop
 
ShowFileNames
	MOVE.W	#$91,D0
	BSR	WaitForVBlank
	MOVEQ	#7,D7
sfnloop	MOVE.W	A6,TextOffset
	MOVE.L	D6,A0
	MOVEQ	#0,D0
	MOVE.W	30(A0),D0
	BSR	CalculateDate
	MOVE.W	TheDay(PC),WordNumber	
	JSR	Print2DecDigits
	MOVE.W	TheMonth(PC),WordNumber	
	JSR	Print2DecDigits
	MOVE.W	TheYear(PC),WordNumber	
	JSR	Print2DecDigits
	ADDQ.W	#1,TextOffset
	MOVE.L	D6,ShowTextPtr
	MOVE.W	#24,TextLength
	TST.W	DirPathNum
	BNE.S	sfnskip
	TST.B	ModOnlyFlag
	BEQ.S	sfnskip
	MOVE.L	D6,A0
	CMP.L	#"mod.",(A0)
	BNE.S	sfnskip
	ADDQ.L	#4,ShowTextPtr
sfnskip	JSR	SpaceShowText
	LEA	32(A6),A6
	MOVE.W	A6,TextOffset
	MOVE.L	D6,A0
	MOVE.L	32(A0),D1
	BMI.S	sfndir
	TST.B	ShowDecFlag
	BNE.S	sfndec
	SWAP	D1
	AND.W	#$000F,D1
	BSR	ShowOneDigit
	MOVE.L	D6,A0
	MOVE.W	34(A0),WordNumber
	JSR	PrintHexWord
sfnend	ADD.L	#36,D6
	LEA	208(A6),A6
	DBRA	D7,sfnloop
	RTS

sfndec	MOVE.L	D1,D0
	SUBQ.W	#1,TextOffset
	JSR	Print6DecDigits
	BRA.S	sfnend

sfndir	MOVE.L	#DirText,ShowTextPtr
	SUBQ.W	#1,TextOffset
	MOVE.W	#6,TextLength
	JSR	ShowText
	BRA.S	sfnend

ClrFileText	dc.b "    "

CalculateDate
	DIVU	#1461,D0
	LSL.W	#2,D0
	MOVE.W	D0,TheYear
	CLR.W	D0
	SWAP	D0
	CMP.W	#789,D0
	BEQ.S	cadleap
	BLO.S	cadskp2
	SUBQ.W	#1,D0
cadskp2	DIVU	#365,D0
	MOVE.L	D0,D1
	SWAP	D1
	CMP.W	#4,D0
	BLO.S	cadskip
	SUBQ.W	#1,D0
cadskip	ADD.W	D0,TheYear
	LEA	MonthTable(PC),A1
	MOVEQ	#24,D0
cadloop	SUBQ.W	#2,D0
	MOVE.W	(A1,D0.W),D2
	CMP.W	D2,D1
	BHS.S	cadskp3
	TST.W	D0
	BNE.S	cadloop
cadskp3	LSR.W	#1,D0
	ADDQ.W	#1,D0
	MOVE.W	D0,TheMonth
	SUB.W	D2,D1
	ADDQ.W	#1,D1
	MOVE.W	D1,TheDay
cadend	MOVEQ	#0,D0
	MOVE.W	TheYear(PC),D0
	ADD.W	#78,D0
	DIVU	#100,D0
	SWAP	D0
	MOVE.W	D0,TheYear
	RTS
cadleap	ADD.W	#2,TheYear
	MOVE.W	#2,TheMonth
	MOVE.W	#29,TheDay
	BRA	cadend

TheYear		dc.w 0
TheMonth	dc.w 0
TheDay		dc.w 0
MonthTable	dc.w 0,31,59,90,120,151,181,212,243,273,304,334
DirText		dc.b " (DIR)"

;---- Play Song ----

PlaySong
	MOVEQ	#0,D0
SongFrom
	BSR	StopIt
	BTST	#2,$DFF016
	BNE.S	sofr1
	MOVE.W	PlayFromPos,D0
sofr1	MOVE.W	D0,ScrPattPos
	LSL.W	#4,D0
	AND.L	#$FFFF,D0
	MOVE.L	D0,PatternPosition
wfbu1	BTST	#6,$BFE001
	BEQ.S	wfbu1
	CLR.B	RawKeyCode
	CLR.B	SaveKey
	BSR	SetPlayPtrCol
	CLR.W	PatternTicks
	CLR.L	TimerTicks
	MOVE.L	#'patp',RunMode
	CLR.W	DidQuantize
	MOVE.L	#-1,LongFFFF
	BSR	SetScrPatternPos
SetPlayPosition
	MOVE.L	CurrPos,D0
	MOVE.L	SongDataPtr,A0
	CMP.B	sd_numofpatt(A0),D0
	BHS.S	SongPosToZero
	MOVE.L	CurrPos,SongPosition
	RTS

SongPosToZero
	CLR.L	SongPosition
	CLR.L	CurrPos
	RTS

;---- Play Pattern ----

PlayPattern
	MOVEQ	#0,D0
PattFrom
	BSR	StopIt
	BTST	#2,$DFF016
	BNE.S	pafr1
	MOVE.W	PlayFromPos,D0
pafr1	MOVE.W	D0,ScrPattPos
	LSL.W	#4,D0
	AND.L	#$FFF,D0
	MOVE.L	D0,PatternPosition
wfbu2	BTST	#6,$BFE001
	BEQ.S	wfbu2
	CLR.B	RawKeyCode
	CLR.B	SaveKey
	MOVE.L	#'patt',RunMode
	BSR	SetPlayPtrCol
ResetPosition
	MOVE.L	PatternNumber,SongPosition
	RTS	

;---- Record Pattern/Song ----

RecordPattern
	MOVEQ	#0,D0
RecordFrom
	TST.W	SamScrEnable
	BNE	Return1
	BSR	StopIt
	BTST	#2,$DFF016
	BNE.S	refr1
	MOVE.W	PlayFromPos,D0
refr1	MOVE.W	D0,ScrPattPos
	LSL.W	#4,D0
	AND.L	#$FFF,D0
	MOVE.L	D0,PatternPosition
wfbu3	BTST	#6,$BFE001
	BEQ.S	wfbu3
	BSR	SetEditPtrCol
	CLR.B	RawKeyCode
	CLR.B	SaveKey
	BSR	SaveUndo
	MOVE.L	#'edit',EditMode
	MOVE.L	#'patt',RunMode
	TST.B	RecordMode
	BEQ.S	ResetPosition
	MOVE.L	#'patp',RunMode
	BRA	SetPlayPosition

;---- Show Main Screen ----

DisplayMainScreen
	CLR.W	BlockMarkFlag
	MOVE.W	#1,CurrScreen
	SF	NoSampleInfo
	TST.W	LoadInProgress
	BNE.S	dmsskp3
	BSR	SetNormalPtrCol
	TST.W	RunMode
	BEQ.S	dmsskip
	BSR	SetPlayPtrCol
dmsskip	TST.L	EditMode
	BEQ.S	dmsskp2
	BSR	SetEditPtrCol
dmsskp2	BSR	StorePtrCol
dmsskp3	ST	DisableAnalyzer
	BSR	ClearAnaHeights
	BSR	ClearRightArea
	LEA	TopMenusPos,A0
	LEA	TopMenusBuffer,A1
	MOVEQ	#43,D0
cgloop4	MOVEQ	#24,D1
cgloop5	MOVE.B	(A1)+,(A0)+
	MOVE.B	1099(A1),10239(A0)
	DBRA	D1,cgloop5
	LEA	15(A0),A0
	DBRA	D0,cgloop4
	BSR	RedrawToggles
	TST.B	EdEnable
	BNE	EditOp
	MOVEQ	#0,D4
RedrawAnaScope
	MOVE.W	#145,D0
	BSR	WaitForVBlank
	SF	ScopeEnable
	ST	DisableAnalyzer
	BSR	ClearRightArea
	LEA	SpectrumAnaData,A0
	MOVE.L	#SpectrumAnaSize,D0
	TST.B	AnaScopFlag
	BEQ.S	cgjojo
	LEA	ScopeData,A0
	MOVE.L	#ScopeSize,D0

cgjojo	BSR	Decompact
	BEQ	cgjaja
	LEA	SpectrumAnaPos,A0
	MOVEQ	#1,D7
cgloop1	MOVEQ	#54,D6	; 55 lines in picture.
cgloop2	MOVEQ	#24,D5	; 25 bytes(x8)
cgloop3	MOVE.B	(A1)+,(A0)+
	DBRA	D5,cgloop3
	ADDQ.L	#1,A1
	LEA	15(A0),A0
	DBRA	D6,cgloop2
	LEA	$1F68(A0),A0
	DBRA	D7,cgloop1
	BSR	FreeDecompMem
cgjaja	TST.L	D4
	BNE	Return1
	TST.B	AnaScopFlag
	BNE	cgscope
	BSR	ClearAnaHeights
	BSR	ClearRightArea
	SF	DisableAnalyzer
	BRA	SetAnalyzerColors
cgscope	ST	ScopeEnable
	BRA	ClearAnalyzerColors
	
;---- Clear Areas ----

ClearFileNames	MOVE.W	#145,D0
	BSR	WaitForVBlank
	MOVE.L	TextBplPtr,A1
	LEA	1800(A1),A1
	MOVE.W	#549,D0
	MOVEQ	#0,D1
cfnloop	MOVE.L	D1,(A1)+
	DBRA	D0,cfnloop
	RTS

ClearRightArea
	MOVE.L	TextBplPtr,A0
	LEA	55(A0),A0
	MOVEQ	#0,D2
	MOVEQ	#98,D0
cnblloop1
	MOVEQ	#24,D1
cnblloop2
	MOVE.B	D2,(A0)+
	DBRA	D1,cnblloop2
	LEA	15(A0),A0
	DBRA	D0,cnblloop1
	RTS

Clear100Lines
	MOVE.L	TextBplPtr,A0
	MOVE.W	#999,D0
	MOVEQ	#0,D1
chlloop	MOVE.L	D1,(A0)+
	DBRA	D0,chlloop
	RTS

;---- Are You Sure Requester ----

AreYouSure
	MOVE.B	DisableAnalyzer,SaveDA
	MOVE.B	ScopeEnable,SaveScope
	SF	ScopeEnable
	JSR	ShowStatusText
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	BSR	Wait_4000
	CMP	#1,CurrScreen
	BNE.S	aysskip
	TST.B	DisableAnalyzer
	BNE.S	aysskip
	ST 	DisableAnalyzer
	BSR	ClearAnaHeights
	BSR	ClearRightArea
aysskip	LEA	SureBoxData,A1
	BSR	SwapBoxMem
	BSR	WaitForButtonUp
	BSR	Wait_4000
	CLR.B	RawKeyCode
surebuttoncheck
	BTST	#2,$DFF016
	BEQ.S	SureAnswerNo
	BSR	DoKeyBuffer
	MOVE.B	RawKeyCode,D0
	CMP.B	#21,D0 ; Pressed Y
	BEQ	SureAnswerYes
	CMP.B	#68,D0 ; Pressed Return
	BEQ.S	SureAnswerYes
	CMP.B	#54,D0 ; Pressed N
	BEQ.S	SureAnswerNo
	CMP.B	#69,D0 ; Pressed Esc
	BEQ.S	SureAnswerNo
	BTST	#6,$BFE001	; Left Mousebutton
	BNE.S	surebuttoncheck
	MOVEM.W	MouseX,D0/D1
	CMP.W	#$AB,D0
	BLO.S	surebuttoncheck
	CMP.W	#$FC,D0
	BHI.S	surebuttoncheck
	CMP.W	#$48,D1
	BLO.S	surebuttoncheck
	CMP.W	#$52,D1
	BHI.S	surebuttoncheck
	CMP.W	#$C5,D0
	BLO.S	SureAnswerYes
	CMP.W	#$EA,D0
	BLO.S	surebuttoncheck
SureAnswerNo
	LEA	SureBoxData,A1
	BSR.S	SwapBoxMem
	JSR	ShowAllRight
	BSR	ClearAnaHeights
	MOVE.B	SaveDA,DisableAnalyzer
	MOVE.B	SaveScope,ScopeEnable
	BSR	ErrorRestoreCol
	BSR	WaitForButtonUp
	BSR	Wait_4000
	CLR.B	RawKeyCode
	MOVEQ	#-1,D0
	RTS

SureAnswerYes
	LEA	SureBoxData,A1
	BSR.S	SwapBoxMem
	JSR	ShowAllRight
	BSR	ClearAnaHeights
	MOVE.B	SaveDA,DisableAnalyzer
	MOVE.B	SaveScope,ScopeEnable
	BSR	RestorePtrCol
	BSR	WaitForButtonUp
	BSR	Wait_4000
	CLR.B	RawKeyCode
	MOVEQ	#0,D0
	RTS

SwapBoxMem
	LEA	SureBoxPos,A0
	MOVEQ	#38,D0
ssbmloop1
	MOVEQ	#12,D1
ssbmloop2
	MOVE.B	10240(A0),D2
	MOVE.B	546(A1),10240(A0)
	MOVE.B	D2,546(A1)
	MOVE.B	(A0),D2
	MOVE.B	(A1),(A0)+
	MOVE.B	D2,(A1)+
	DBRA	D1,ssbmloop2
	LEA	$001B(A0),A0
	ADDQ.L	#1,A1
	DBRA	D0,ssbmloop1
	MOVE.L	TextBplPtr,A0	; Swap text...
	LEA	2100(A0),A0
	LEA	TextDataBuffer,A1
	MOVEQ	#38,D0
ssbmloop3
	MOVEQ	#12,D1
ssbmloop4
	MOVE.B	(A0),D2
	MOVE.B	(A1),(A0)+
	MOVE.B	D2,(A1)+
	DBRA	D1,ssbmloop4
	LEA	$001B(A0),A0
	ADDQ.L	#1,A1
	DBRA	D0,ssbmloop3
	RTS	
 
WaitForButtonUp
	BTST	#6,$BFE001	; Check Left Mousebutton
	BEQ.S	WaitForButtonUp
	BTST	#2,$DFF016	; Check Right Mousebutton
	BEQ.S	WaitForButtonUp
	MOVE.W	#$91,D0
WaitForVBlank
	MOVE.L	$DFF004,D1
	LSR.L	#8,D1
	AND.W	#$1FF,D1
	CMP.W	D1,D0
	BNE.S	WaitForVBlank
	RTS

;---- Set Pointercolors ----

SetDeletePtrCol
	MOVE.L	A6,-(SP)
	MOVE.L	CopListColorPtr,A6
	MOVE.W	#$0AA,2(A6)
	MOVE.W	#$077,6(A6)
	MOVE.W	#$044,10(A6)
	MOVE.L	(SP)+,A6
	RTS	
 
SetNormalPtrCol
	MOVE.L	A6,-(SP)
	MOVE.L	CopListColorPtr,A6
	MOVE.W	#$AAA,2(A6)
	MOVE.W	#$777,6(A6)
	MOVE.W	#$444,10(A6)
	MOVE.L	(SP)+,A6
	RTS	
 
SetDiskPtrCol
	MOVE.L	A6,-(SP)
	MOVE.L	CopListColorPtr,A6
	MOVE.W	#$0A0,2(A6)
	MOVE.W	#$070,6(A6)
	MOVE.W	#$040,10(A6)
	MOVE.L	(SP)+,A6
	RTS	
 
SetPlayPtrCol
	MOVE.L	A6,-(SP)
	MOVE.L	CopListColorPtr,A6
	MOVE.W	#$AA0,2(A6)
	MOVE.W	#$770,6(A6)
	MOVE.W	#$440,10(A6)
	MOVE.L	(SP)+,A6
	RTS	
 
SetEditPtrCol
	MOVE.L	A6,-(SP)
	MOVE.L	CopListColorPtr,A6
	MOVE.W	#$05B,2(A6)
	MOVE.W	#$049,6(A6)
	MOVE.W	#$006,10(A6)
	MOVE.L	(SP)+,A6
	RTS	
 
SetWaitPtrCol
	MOVE.L	A6,-(SP)
	MOVE.L	CopListColorPtr,A6
	MOVE.W	#$A5A,2(A6)
	MOVE.W	#$727,6(A6)
	MOVE.W	#$404,10(A6)
	MOVE.L	(SP)+,A6
	RTS	
 
SetErrorPtrCol
	MOVE.L	A6,-(SP)
	MOVE.L	CopListColorPtr,A6
	MOVE.W	#$C00,2(A6)
	MOVE.W	#$900,6(A6)
	MOVE.W	#$700,10(A6)
	MOVE.L	(SP)+,A6
	MOVE.W	#1,UpdateFreeMem
	BSR	WaitALittle
	JSR	ShowAllRight
	BSR	SetNormalPtrCol
	MOVEQ	#0,D0
	RTS

WaitALittle
	MOVE.W	WaitTime(PC),D1
errorwaitloop
	BSR	Wait_4000
	DBRA	D1,errorwaitloop
	MOVE.W	#23,WaitTime
	RTS

WaitTime dc.w 23

StorePtrCol
	MOVE.L	A6,-(SP)
	MOVE.L	CopListColorPtr,A6
	MOVE.W	2(A6),PointerCol1Save
	MOVE.W	6(A6),PointerCol2Save
	MOVE.W	10(A6),PointerCol3Save
	MOVE.L	(SP)+,A6
	RTS

ErrorRestoreCol
	BSR	SetErrorPtrCol
RestorePtrCol
	MOVE.L	A6,-(SP)
	MOVE.W	#1,UpdateFreeMem
	MOVE.L	CopListColorPtr,A6
	MOVE.W	PointerCol1Save,2(A6)
	MOVE.W	PointerCol2Save,6(A6)
	MOVE.W	PointerCol3Save,10(A6)
	MOVE.L	(SP)+,A6
	RTS


;---- Check special keys ----

CheckPlayKeys
	MOVE.B	RawKeyCode,D0
	CMP.B	#101,D0
	BEQ	PlaySong
	CMP.B	#103,D0
	BEQ	PlayPattern
	CMP.B	#64,D0
	BNE.S	cpkskip
	TST.L	RunMode	
	BNE	StopIt
	TST.L	EditMode
	BNE	StopIt	
	BRA	Edit
cpkskip	CMP.B	#97,D0
	BEQ	RecordPattern
	CMP.B	#69,D0
	BEQ	EscPressed
	CMP.B	#66,D0
	BEQ	TabulateCursor
	CMP.B	#127,D0
	BEQ	GotoCLI
	CMP.B	#48,D0
	BEQ	TurnOffVoices
	CMP.B	#60,D0
	BEQ	KillSample
	CMP.B	#13,D0
	BEQ	togglepnote

	CMP.B	#67,D0
	BEQ.S	HigherInstr
	CMP.B	#67+128,D0
	BEQ.S	LowerInstr
	MOVE.W	HiLowInstr,D1
	LEA	kpinstable(PC),A0
	MOVEQ	#0,D2
kpinsloop
	CMP.B	(A0,D2.W),D0
	BEQ.S	kpinsfound
	ADDQ.W	#1,D2
	CMP.W	#16,D2
	BLO.S	kpinsloop
	RTS

kpinsfound
	ADD.W	D2,D1
	BEQ.S	insnull
	BRA.S	redrsa2

LowerInstr
	CLR.W	HiLowInstr
	RTS
HigherInstr
	MOVE.W	#16,HiLowInstr
	MOVE.W	#16,D1
	BRA.S	redrsa2

redrsa3	MOVE.W	D1,InsNum
redrsam	CLR.B	RawKeyCode
	CLR.L	SavSamInf
	JSR	ShowSampleInfo
	JMP	RedrawSample

insnull	TST.W	InsNum
	BEQ.S	insnul2
	MOVE.W	InsNum,LastInsNum
	CLR.W	InsNum
	MOVEQ	#0,D1
	BRA.S	redrsa2

insnul2	JMP	ShowSampleInfo

kpinstable
	dc.b 15,90,91,92
	dc.b 93,61,62,63,74
	dc.b 45,46,47,94,29,30
	dc.b 31

redrsa2	TST.B	pnoteflag
	BEQ.S	redrsa3
	CLR.B	RawKeyCode
	TST.W	AltKeyStatus
	BNE.S	setpnote
	MOVE.W	D1,InsNum
	JSR	ShowSampleInfo
	MOVE.W	InsNum,D0
	ADD.W	D0,D0
	MOVE.W	pnotetable(PC,D0.W),D0
	MOVEQ	#-1,D2
	BSR	playtheinstr
	JMP	RedrawSample

pnotetable
	dc.w 24,24,24,24,24,24,24,24,24,24
	dc.w 24,24,24,24,24,24,24,24,24,24
	dc.w 24,24,24,24,24,24,24,24,24,24
	dc.w 24,24

setpnote
	ADD.W	D1,D1
	LEA	pnotetable(PC,D1.W),A0
	MOVE.L	A0,SplitAddress
	MOVE.W	#4,SamNoteType
	LEA	selnotetext(PC),A0
	JMP	ShowStatusText

selnotetext	dc.b "select note",0

togglepnote
	CLR.B	RawKeyCode
	MOVEQ	#0,D0
	MOVE.B	pnoteflag,D0
	ADDQ.B	#1,D0
	CMP.B	#3,D0
	BLO.S	tpnskp
	MOVEQ	#0,D0
tpnskp	MOVE.B	D0,pnoteflag
	LEA	pnotechar(PC,D0.W),A0
	MOVEQ	#1,D0
	MOVE.W	#5159,D1
	JMP	ShowText3

pnotechar	dc.b 32,128,129
pnoteflag	dc.b 0

;---- Check transpose keys ----

CheckTransKeys
	TST.W	LeftAmigaStatus
	BEQ	Return1
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	MOVE.B	RawKeyCode,D1
	CLR.B	RawKeyCode
	MOVE.B	SampleAllFlag,-(SP)
	BSR.S	ctksub
	MOVE.B	(SP)+,SampleAllFlag
	RTS

ctksub	MOVEQ	#0,D0
	CLR.B	SampleAllFlag
	CMP.B	#1,D1
	BEQ	NoteUp
	CMP.B	#16,D1
	BEQ	NoteDown
	CMP.B	#32,D1
	BEQ	OctaveUp
	CMP.B	#49,D1
	BEQ	OctaveDown

	MOVE.W	#300,D0
	CMP.B	#2,D1
	BEQ	NoteUp
	CMP.B	#17,D1
	BEQ	NoteDown
	CMP.B	#33,D1
	BEQ	OctaveUp
	CMP.B	#50,D1
	BEQ	OctaveDown

	MOVEQ	#0,D0
	MOVE.B	#1,SampleAllFlag
	CMP.B	#3,D1
	BEQ	NoteUp
	CMP.B	#18,D1
	BEQ	NoteDown
	CMP.B	#34,D1
	BEQ	OctaveUp
	CMP.B	#51,D1
	BEQ	OctaveDown
	
	MOVE.W	#300,D0
	CMP.B	#4,D1
	BEQ	NoteUp
	CMP.B	#19,D1
	BEQ	NoteDown
	CMP.B	#35,D1
	BEQ	OctaveUp
	CMP.B	#52,D1
	BEQ	OctaveDown
	RTS

;---- Check control keys ----

CheckCtrlKeys
	TST.W	CtrlKeyStatus
	BEQ	Return1
	MOVEQ	#0,D0
	MOVE.B	RawKeyCode,D0
	CMP.B	#35,D0 ; F
	BEQ	ToggleFilter
	CMP.B	#33,D0 ; S
	BEQ	ToggleSplit
	CMP.B	#55,D0 ; M
	BEQ	ToggleMultiMode
	CMP.B	#16,D0 ; Q
	BEQ	UnmuteAll
	CMP.B	#32,D0 ; A
	BEQ	ToggleMute
	CMP.B	#49,D0 ; Z
	BEQ	RestoreEffects
	CMP.B	#19,D0 ; R
	BEQ	RestoreFKeyPos
	CMP.B	#20,D0 ; T
	BEQ	SwapTrack

	CMP.B	#53,D0 ; B
	BEQ	BeginBlock
	CMP.B	#51,D0 ; C
	BEQ	CopyBlock
	CMP.B	#50,D0 ; X
	BEQ	CutBlock
	CMP.B	#34,D0 ; D
	BEQ	DeleteBlock
	CMP.B	#25,D0 ; P
	BEQ	PasteBlock
	CMP.B	#23,D0 ; I
	BEQ	InsertBlock
	CMP.B	#38,D0 ; J
	BEQ	JoinPasteBlock
	CMP.B	#54,D0 ; N
	BEQ	Re_MarkBlock
	CMP.B	#21,D0 ; Y
	BEQ	BackwardsBlock
	CMP.B	#17,D0 ; W
	BEQ	PolyphonizeBlock

	CMP.B	#18,D0 ; E
	BEQ	ExpandTrack
	CMP.B	#24,D0 ; O
	BEQ	ContractTrack
	CMP.B	#37,D0 ; H
	BEQ	TransBlockUp
	CMP.B	#40,D0 ; L
	BEQ	TransBlockDown
	CMP.B	#39,D0 ; K
	BEQ	KillToEndOfTrack
	CMP.B	#22,D0 ; U
	BEQ	UndoLastChange

	CMP.B	#68,D0 ; CR
	BEQ	InsCmdTrack
	CMP.B	#65,D0 ; Del
	BEQ	DelCmdTrack
	CMP.B	#36,D0 ; G
	BEQ	BoostAll
	CMP.B	#52,D0 ; V
	BEQ	FilterAll

	CMP.B	#1,D0
	BLO	Return1
	CMP.B	#10,D0
	BHI	Return1
	BNE.S	cckskip
	MOVEQ	#0,D0
cckskip	MOVE.W	D0,EditMoveAdd
	CLR.B	RawKeyCode
	ADD.B	#'0',D0
	LEA	ematext(PC),A0
	MOVE.B	D0,11(A0)
	JSR	ShowStatusText
	MOVE.W	#10,WaitTime
	BSR	WaitALittle
	JMP	ShowAllRight

ematext	dc.b "editskip = 0",0,0

;---- Check alt keys ----

CheckAltKeys
	TST.W	AltKeyStatus
	BEQ	Return1
	MOVEQ	#0,D0
	MOVE.B	RawKeyCode,D0
	CMP.B	#49,D0 ; Z
	BEQ	ToggleCh1
	CMP.B	#50,D0 ; X
	BEQ	ToggleCh2
	CMP.B	#51,D0 ; C
	BEQ	ToggleCh3
	CMP.B	#52,D0 ; V
	BEQ	ToggleCh4
	CMP.B	#53,D0 ; B
	BEQ	Boost
	CMP.B	#35,D0 ; F
	BEQ	Filter
	CMP.B	#20,D0 ; T
	BEQ.S	xTuningTone
	CMP.B	#33,D0 ; S
	BEQ.S	xSamplerScreen
	CMP.B	#19,D0 ; R
	BEQ.S	xResample
	CMP.B	#18,D0 ; E
	BEQ	DoEditOp
	CMP.B	#23,D0 ; I
	BEQ.S	AutoInsert
	CMP.B	#34,D0 ; D
	BEQ	DiskOp
	CMP.B	#39,D0 ; K
	BEQ	KillInstrTrack
;	CMP.B	#17,D0 ; W
;	BEQ.S	WaitForNote
	CMP.B	#32,D0 ; A
	BEQ.S	xSampler
	CMP.B	#16,D0 ; Q
	BEQ	ChkQuit
	CMP.B	#21,D0 ; Y
	BEQ	SaveAllSamples

;	CMP.B	#24,D0 ; O
;	CMP.B	#55,D0 ; M
;	CMP.B	#25,D0 ; P
;	CMP.B	#38,D0 ; J
;	CMP.B	#54,D0 ; N
;	CMP.B	#37,D0 ; H
;	CMP.B	#40,D0 ; L
;	CMP.B	#22,D0 ; U
;	CMP.B	#36,D0 ; G
	RTS

xTuningTone	JMP	TuningTone
xSamplerScreen	JMP	SamplerScreen
xResample	JMP	Resample
xSampler	JMP	Sampler

AutoInsert
	CLR.B	RawKeyCode
	EOR.B	#1,AutoInsFlag
ShowAutoInsert
	CMP.W	#4,CurrScreen
	BEQ	Return1
	MOVE.B	#' ',D0
	TST.B	AutoInsFlag
	BEQ.S	auins2
	MOVE.B	#'I',D0
auins2	MOVE.B	D0,AutoInsText
	MOVE.W	#4561,D1
	MOVEQ	#1,D0
	LEA	AutoInsText(PC),A0
	JMP	ShowText3

AutoInsText	dc.b 0
AutoInsFlag	dc.b 0

SaveAllSamples
	LEA	SaveAllSamplesText,A0
	BSR	AreYouSure
	BNE	SetErrorPtrCol
	MOVE.W	InsNum,SaveInstr
	MOVE.W	#1,InsNum
.loop	JSR	ShowSampleInfo
	BSR	dosavesample
	ADDQ.W	#1,InsNum
	CMP.W	#32,InsNum
	BLO.S	.loop
	MOVE.W	SaveInstr,InsNum
	JMP	ShowSampleInfo

SaveAllSamplesText
	dc.b	"save all samples?",0
	even

;---- Jump between channels ----

TabulateCursor
	CLR.B	RawKeyCode
	TST.W	ShiftKeyStatus
	BNE.S	TabCurRight
	MOVE.W	PattCurPos,D0
	DIVU	#6,D0
	ADDQ.W	#1,D0
	CMP.W	#4,D0
	BNE.S	tacskip
	MOVEQ	#0,D0
tacskip	MULU	#6,D0
	MOVE.W	D0,PattCurPos
	BRA	UpdateCursorPos

TabCurRight
	MOVE.W	PattCurPos,D0
	ADDQ.W	#5,D0
	DIVU	#6,D0
	SUBQ.W	#1,D0
	BPL.S	tacskip
	MOVEQ	#3,D0
	BRA.S	tacskip

;---- Escape was pressed ----

EscPressed
	CLR.B	RawKeyCode
	CLR.B	SaveKey
	MOVE.W	CurrScreen,D0
	CMP.W	#2,D0
	BLO.S	BotExit
TopExit	MOVE.W	CurrScreen,D0
	CMP.W	#2,D0
	BEQ	ExitHelpScreen
	CMP.W	#3,D0
	BEQ	ExitFromDir
	CMP.W	#4,D0
	BEQ.S	pedexit
	CMP.W	#5,D0
	BEQ	ExitSetup
	CMP.W	#6,D0
	BEQ.S	plstexit
	CMP.W	#7,D0
	BEQ	ExitSetup
	RTS

BotExit	TST.W	SamScrEnable
	BNE.S	samplerexit
	TST.W	BlockMarkFlag
	BNE	blkunma
	CMP.W	#1,CurrScreen
	BNE	Return1
	TST.B	EdEnable
	BNE	ExitEditOp
	RTS

samplerexit
	 JMP	ExitFromSam
pedexit	 JMP	PED_Exit
plstexit JMP	ExitPLST

GotoCLI	CLR.B	RawKeyCode
	CLR.B	SaveKey
	TST.L	RunMode
	BNE.S	cliskp1
	BSR	StopIt
	BSR	ResetMusicInt
cliskp1	BSR	ResetVBInt
	BSR	ResetInputHandler
	BSR	ResetAutoReq
	BSR	ResetCopList
	MOVE.L	ExtCmdAddress,D1
	BEQ.S	gcliskp
	CLR.L	ExtCmdAddress
	MOVE.L	ExtCmdWindow,D2
	MOVEQ	#0,D3
	MOVE.L	DOSBase,A6
	JSR	LVOExecute(A6)
gcliskp	SUB.L	A0,A0
	LEA	BackToProText(PC),A1
	LEA	OKGadgetText(PC),A2
	MOVE.L	A2,A3
	MOVEQ	#0,D0
	MOVEQ	#0,D1
	MOVE.W	#196,D2
	MOVEQ	#50,D3
	MOVE.L	IntuitionBase,A6
	JSR	LVOAutoRequest(A6)
	ADDQ.B	#1,LastRawkey
	TST.L	RunMode
	BNE.S	cliskp2
	BSR	SetMusicInt
cliskp2	BSR	SetVBInt
	BSR	SetInputHandler
	BSR	SetAutoReq
	BRA	SetCopList

BackToProText
	dc.b 0,1,1,0
	dc.w 14,6
	dc.l 0,btptext,0
btptext dc.b 'Back to Protracker',0,0

OKGadgetText
	dc.b 0,1,1,0
	dc.w 6,3
	dc.l 0,okgtext,0
okgtext dc.b 'OK',0,0

;---- Check Help Key ----

HelpSelectText	dc.b	'* help selected *',0
PLSTSelectText	dc.b	'* plst selected *',0
PLSTHelpFlag	dc.b	0,0 ; free 0

CheckHelpKey
	CMP.B	#95,RawKeyCode	; Help Key pressed ?
	BNE	Return1
	CLR.B	RawKeyCode
	TST.W	ShiftKeyStatus
	BEQ.S	realhlp
	LEA	HelpSelectText,A0
	EOR.B	#1,PLSTHelpFlag
	BEQ.S	chksel
	LEA	PLSTSelectText,A0
chksel	BSR	ShowStatusText
	BSR	WaitALittle
	BRA	ShowAllRight

gtoPLST	JMP	PLST

realhlp	TST.B	PLSTHelpFlag
	BNE.S	gtoPLST
	CMP.W	#2,CurrScreen
	BEQ	ExitHelpScreen
	BSR	TopExit
	MOVE.W	CurrScreen,SaveCurrScreen
	SF	ScopeEnable
	ST 	DisableAnalyzer
	ST	NoSampleInfo
	BSR	ClearAnalyzerColors
	BSR	SwapHelpScreen
	BEQ	exithlp
	BSR	Clear100Lines
	TST.L	HelpTextIndex+4
	BNE.S	chkskip
	BSR	GetHelpIndex
chkskip	MOVE.L	#HelpFileName,D1
	MOVE.L	#1005,D2
	MOVE.L	DOSBase,A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BNE	ShowHelpPage
	LEA	PTPath,A0
	BSR	CopyPath
	LEA	HelpFileName(PC),A0
	MOVEQ	#7,D0
hefilop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,hefilop
	MOVE.L	#FileName,D1
	MOVE.L	#1005,D2
	MOVE.L	DOSBase,A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BEQ	HelpFileError
	BRA.S	ShowHelpPage

ExitHelpScreen
	MOVE.L	FileHandle,D1
	BEQ.S	ehsskip
	MOVE.L	DOSBase,A6
	JSR	LVOClose(A6)
ehsskip	BSR	Clear100Lines
	BSR.S	SwapHelpScreen
exithlp	MOVE.W	SaveCurrScreen,CurrScreen
 	BRA	DisplayMainAll

SwapHelpScreen
	MOVE.L	DecompMemPtr,D0
	BEQ.S	shelps2
	MOVE.L	D0,A1
	BSR	ssets3
	BRA	FreeDecompMem
shelps2	LEA	HelpScreenData,A0
	MOVE.L	#HelpScreenSize,D0
	BSR	Decompact
	BEQ	Return1
	BRA	ssets3

ShowHelpPage
	MOVE.W	#2,CurrScreen
	MOVE.W	HelpPage,D0
	AND.W	#$00FF,D0
	LSL.W	#2,D0
	LEA	HelpTextIndex,A0
	MOVE.L	FileHandle,D1
	BEQ	Return1
	MOVE.L	(A0,D0.W),D2
	ADD.L	#1024,D2
	MOVEQ	#-1,D3
	MOVE.L	DOSBase,A6
	JSR	LVOSeek(A6)
	MOVE.L	FileHandle,D1
	MOVE.L	#HelpTextData,D2
	MOVE.L	#656,D3
	JSR	LVORead(A6)
	BSR	Clear100Lines
	LEA	HelpTextData+16,A6
	MOVEQ	#120,D6
	MOVEQ	#15,D7
shploop	MOVEQ	#0,D0
	MOVE.L	A6,A1
shplop2	CMP.B	#10,(A1)+
	BEQ.S	ShowHelpLine
	TST.B	-1(A1)
	BEQ.S	ShowHelpLine
	ADDQ.W	#1,D0
	BRA.S	shplop2
ShowHelpLine
	MOVE.L	A6,ShowTextPtr
	MOVE.L	A1,A6
	MOVE.W	D6,TextOffset
	MOVE.W	D0,TextLength
	BEQ.S	shlskip
	JSR	ShowText
shlskip	ADD.W	#240,D6
	DBRA	D7,shploop
	BSR	Wait_4000
	BRA	Wait_4000

HelpPage	dc.w	1

HelpUp	LEA	HelpTextData+7,A0
	BRA.S	HelpMove
HelpDown
	LEA	HelpTextData+10,A0
	BRA.S	HelpMove
HelpLeft
	LEA	HelpTextData+4,A0
	BRA.S	HelpMove
HelpRight
	LEA	HelpTextData+13,A0
HelpMove
	CLR.B	RawKeyCode
	MOVEQ	#0,D0
	JSR	HexToInteger2
	TST.W	D0
	BEQ	Return1
	AND.W	#$00FF,D0
	MOVE.W	D0,HelpPage
	BRA	ShowHelpPage

GetHelpIndex
	MOVE.L	LaHeTx,D0
	CMP.L	HelpTextIndex,D0
	BEQ	Return1
	MOVE.L	#HelpFileName,D1
	MOVE.L	#1005,D2
	MOVE.L	DOSBase,A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BNE.S	gehein
	LEA	PTPath,A0
	BSR	CopyPath
	LEA	HelpFileName(PC),A0
	MOVEQ	#7,D0
hefilp2	MOVE.B	(A0)+,(A1)+
	DBRA	D0,hefilp2
	MOVE.L	#FileName,D1
	MOVE.L	#1005,D2
	MOVE.L	DOSBase,A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BEQ.S	HelpFileError
gehein	MOVE.L	D0,D1
	MOVE.L	#HelpTextIndex,D2
	MOVE.L	#256*4,D3
	JSR	LVORead(A6)
	MOVE.L	FileHandle,D1
	JSR	LVOClose(A6)
	MOVE.L	HelpTextIndex,LaHeTx
	RTS

HelpFileError
	MOVE.W	#2,CurrScreen
	LEA	NoHelpText(PC),A0
	MOVE.W	#1887,D1
	MOVEQ	#24,D0
	JMP	ShowText3

NoHelpText	dc.b 'Unable to open helpfile!'
HelpFileName	dc.b 'PT.help',0


;---- Check Function Keys F6-F10 ----

CheckF6_F10
	CMP.B	#85,RawKeyCode
	BNE.S	CheckF7
	CLR.B	RawKeyCode
	TST.W	ShiftKeyStatus
	BEQ.S	cf6skip
	MOVE.W	ScrPattPos,F6pos
ShowPosSetText
	LEA	PosSetText,A0
	BSR	ShowStatusText
	MOVEQ	#8,D2
spsloop	BSR	Wait_4000
	DBRA	D2,spsloop
	BRA	ShowAllRight	
cf6skip	MOVE.W	F6pos,D0
chkalt	TST.W	AltKeyStatus
	BNE	PattFrom
	TST.W	CtrlKeyStatus
	BNE	RecordFrom
	TST.W	LeftAmigaStatus
	BNE	SongFrom
	TST.W	RunMode
	BNE	Return1
	MOVE.W	D0,ScrPattPos
	BRA	SetScrPatternPos
 
CheckF7	CMP.B	#86,RawKeyCode
	BNE.S	CheckF8
	CLR.B	RawKeyCode
	TST.W	ShiftKeyStatus
	BEQ.S	cf7skip
	MOVE.W	ScrPattPos,F7pos
	BRA.S	ShowPosSetText	
cf7skip	MOVE.W	F7pos,D0
	BRA.S	chkalt

CheckF8	CMP.B	#87,RawKeyCode
	BNE.S	CheckF9
	CLR.B	RawKeyCode
	TST.W	ShiftKeyStatus
	BEQ.S	cf8skip
	MOVE.W	ScrPattPos,F8pos
	BRA	ShowPosSetText	
cf8skip	MOVE.W	F8pos,D0
	BRA	chkalt

CheckF9	CMP.B	#88,RawKeyCode
	BNE.S	CheckF10
	CLR.B	RawKeyCode
	TST.W	ShiftKeyStatus
	BEQ.S	cf9skip
	MOVE.W	ScrPattPos,F9pos
	BRA	ShowPosSetText
cf9skip	MOVE.W	F9pos,D0
	BRA	chkalt

CheckF10
	CMP.B	#89,RawKeyCode
	BNE	Return1
	CLR.B	RawKeyCode
	TST.W	ShiftKeyStatus
	BEQ.S	cf10skip
	MOVE.W	ScrPattPos,F10pos
	BRA	ShowPosSetText	
cf10skip
	MOVE.W	F10pos,D0
	BRA	chkalt

F6pos	dc.w  0
F7pos	dc.w 16
F8pos	dc.w 32
F9pos	dc.w 48
F10pos	dc.w 63

;---- Check Function Keys F3-F5 ----

CheckF3_F5
	MOVEQ	#0,D0
	MOVE.L	D0,A0
	TST.W	ShiftKeyStatus
	BNE	CCP4
	TST.W	CtrlKeyStatus
	BNE	CCP4
	TST.W	AltKeyStatus
	BNE	CutCopPas
	CMP.B	#68,RawKeyCode
	BEQ.S	StepPlayForward
	CMP.B	#65,RawKeyCode
	BEQ.S	StepPlayBackward
	TST.W	SamScrEnable
	BEQ	Return1
	CMP.B	#82,RawKeyCode
	BEQ.S	xSamCut
	CMP.B	#83,RawKeyCode
	BEQ.S	xSamCop
	CMP.B	#84,RawKeyCode
	BEQ.S	xSamPas
	RTS

xSamCut	JMP	SamCut
xSamCop	JMP	SamCopy
xSamPas	JMP	SamPaste

StepPlayForward
	MOVE.W	#1,StepPlayEnable
	BSR	DoStopIt
	MOVE.W	ScrPattPos,D0
	BRA	pafr1

StepPlayBackward
	MOVE.W	#1,StepPlayEnable
	BSR	DoStopIt
	MOVE.W	ScrPattPos,D0
	SUBQ.W	#1,D0
	AND.W	#63,D0
	BRA	pafr1

StepPlayEnable	dc.w	0

CCP4	MOVEQ	#0,D0
	MOVE.W	PattCurPos,D0
	DIVU	#6,D0
	AND.L	#3,D0
	LSL.L	#2,D0
	MOVE.L	D0,A0
CutCopPas
	ADD.L	SongDataPtr,A0
	LEA	sd_patterndata(A0),A0
	MOVE.L	PatternNumber,D0
	LSL.L	#8,D0
	LSL.L	#2,D0
	ADD.L	D0,A0
	TST.W	AltKeyStatus
	BNE.S	CutCopPasPatt
	TST.W	CtrlKeyStatus
	BNE.S	CutCopPasCmds
CutCopPasTrack	
	CMP.B	#82,RawKeyCode
	BEQ	CutTrack
	CMP.B	#83,RawKeyCode
	BEQ	CopyTrack
	CMP.B	#84,RawKeyCode
	BEQ	PasteTrack
	CMP.B	#68,RawKeyCode
	BEQ	InsNoteTrack
	CMP.B	#65,RawKeyCode
	BEQ	DelNoteTrack
	RTS

CutCopPasPatt
	CMP.B	#82,RawKeyCode
	BEQ.S	CutPattern
	CMP.B	#83,RawKeyCode
	BEQ.S	CopyPattern
	CMP.B	#84,RawKeyCode
	BEQ	PastePattern
	CMP.B	#68,RawKeyCode
	BEQ	InsNotePattern
	CMP.B	#65,RawKeyCode
	BEQ	DelNotePattern
	RTS

CutCopPasCmds	
	CMP.B	#82,RawKeyCode
	BEQ	CutCmds
	CMP.B	#83,RawKeyCode
	BEQ	CopyCmds
	CMP.B	#84,RawKeyCode
	BEQ	PasteCmds
	RTS

CutPattern
	BSR	SaveUndo
	LEA	PatternBuffer,A1
	MOVE.W	#255,D0
	MOVEQ	#0,D1
cupaloop
	MOVE.L	(A0),(A1)+
	MOVE.L	D1,(A0)+
	DBRA	D0,cupaloop
	CLR.B	RawKeyCode
	BRA	RedrawPattern
 
CopyPattern
	LEA	PatternBuffer,A1
	MOVE.W	#255,D0
copaloop
	MOVE.L	(A0)+,(A1)+
	DBRA	D0,copaloop
	CLR.B	RawKeyCode
	RTS	
 
PastePattern
	BSR	SaveUndo
	LEA	PatternBuffer,A1
	MOVE.W	#255,D0
papaloop
	MOVE.L	(A1)+,(A0)+
	DBRA	D0,papaloop
	CLR.B	RawKeyCode
	BRA	RedrawPattern


CutTrack
	BSR	SaveUndo
	LEA	TrackBuffer,A1
	MOVEQ	#63,D0
	MOVEQ	#0,D1
cutrloop
	MOVE.L	(A0),(A1)+
	MOVE.L	D1,(A0)
	LEA	16(A0),A0
	DBRA	D0,cutrloop
	CLR.B	RawKeyCode
	BRA	RedrawPattern

CopyTrack
	LEA	TrackBuffer,A1
	MOVEQ	#63,D0
cotrloop
	MOVE.L	(A0),(A1)+
	LEA	16(A0),A0
	DBRA	D0,cotrloop
	CLR.B	RawKeyCode
	RTS

PasteTrack
	BSR	SaveUndo
	LEA	TrackBuffer,A1
	MOVEQ	#63,D0
patrloop
	MOVE.L	(A1)+,(A0)
	LEA	16(A0),A0
	DBRA	D0,patrloop
	CLR.B	RawKeyCode
	BRA	RedrawPattern

InsNotePattern
	BSR	SaveUndo
	MOVEQ	#0,D0
	BSR	GetPositionPtr
	BSR.S	inotr
	MOVEQ	#6,D0
	BSR	GetPositionPtr
	BSR.S	inotr
	MOVEQ	#12,D0
	BSR	GetPositionPtr
	BSR.S	inotr
	MOVEQ	#18,D0
	BSR	GetPositionPtr
	BSR.S	inotr
	BRA.S	intskip
InsNoteTrack
	BSR	SaveUndo
	BSR.S	inotr
intskip	CLR.B	RawKeyCode
	ADD.W	D2,ScrPattPos
	AND.W	#$3F,ScrPattPos
	BSR	SetScrPatternPos
	BRA	RedrawPattern

inotr	MOVE.W	ScrPattPos,D1
	LSL.W	#4,D1
	CMP.W	#63*16,D1
	BEQ.S	inoskip
	MOVE.W	#992,D0
intloop	MOVE.L	(A0,D0.W),16(A0,D0.W)
	SUB.W	#16,D0
	CMP.W	D1,D0
	BGE.S	intloop
inoskip	MOVEQ	#1,D2
	CLR.L	(A0,D1.W)
	RTS

InsCmdTrack
	BSR	SaveUndo
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	BSR.S	icmtr
	BRA.S	intskip
icmtr	MOVE.W	ScrPattPos,D1
	LSL.W	#4,D1
	CMP.W	#63*16,D1
	BEQ.S	icmskip
	MOVE.W	#992,D0
icmloop	MOVE.W	2(A0,D0.W),D2
	AND.W	#$0FFF,D2
	AND.W	#$F000,18(A0,D0.W)
	OR.W	D2,18(A0,D0.W)
	SUB.W	#16,D0
	CMP.W	D1,D0
	BGE.S	icmloop
icmskip	MOVEQ	#1,D2
	AND.W	#$F000,2(A0,D1.W)
	RTS

DelNotePattern
	BSR	SaveUndo
	MOVEQ	#0,D0
	BSR	GetPositionPtr
	BSR.S	dnt
	MOVEQ	#6,D0
	BSR	GetPositionPtr
	BSR.S	dnt
	MOVEQ	#12,D0
	BSR	GetPositionPtr
	BSR.S	dnt
	MOVEQ	#18,D0
	BSR	GetPositionPtr
	BSR.S	dnt
	BRA	intskip
DelNoteTrack
	BSR	SaveUndo
	BSR.S	dnt
	BRA	intskip

dnt	MOVE.W	ScrPattPos,D0
	BEQ	Return1
	SUBQ.W	#1,D0
	LSL.W	#4,D0
dntloop	MOVE.L	16(A0,D0.W),(A0,D0.W)
	ADD.W	#16,D0
	CMP.W	#1024,D0
	BLO.S	dntloop
	MOVE.W	#1008,D1
	MOVEQ	#-1,D2
	CLR.L	(A0,D1.W)
	RTS

DelCmdTrack
	BSR	SaveUndo
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	BSR.S	dct
	BRA	intskip
dct	MOVE.W	ScrPattPos,D0
	BEQ	Return1
	SUBQ.W	#1,D0
	LSL.W	#4,D0
dctloop	MOVE.W	18(A0,D0.W),D2
	AND.W	#$0FFF,D2
	AND.W	#$F000,2(A0,D0.W)
	OR.W	D2,2(A0,D0.W)
	ADD.W	#16,D0
	CMP.W	#1024,D0
	BLO.S	dctloop
	MOVE.W	#1008,D1
	MOVEQ	#-1,D2
	AND.W	#$F000,2(A0,D1.W)
	RTS

CutCmds	BSR	SaveUndo
	LEA	CmdsBuffer,A1
	CLR.W	D0
cucmloop
	MOVE.L	0(A0,D0.W),(A1)+
	AND.L	#$FFFFF000,(A0,D0.W)
	ADD.W	#16,D0
	CMP.W	#1024,D0
	BNE.S	cucmloop
	CLR.B	RawKeyCode
	BRA	RedrawPattern
 
CopyCmds
	LEA	CmdsBuffer,A1
	CLR.W	D0
cocmloop
	MOVE.L	0(A0,D0.W),(A1)+
	ADD.W	#16,D0
	CMP.W	#1024,D0
	BNE.S	cocmloop
	CLR.B	RawKeyCode
	RTS	
 
PasteCmds
	BSR	SaveUndo
	LEA	CmdsBuffer,A1
	CLR.W	D0
pacmloop
	MOVE.L	0(A0,D0.W),D1
	AND.L	#$FFFFF000,D1
	MOVE.L	(A1)+,D2
	AND.L	#$00000FFF,D2
	OR.L	D2,D1
	MOVE.L	D1,(A0,D0.W)
	ADD.W	#16,D0
	CMP.W	#1024,D0
	BNE.S	pacmloop
	CLR.B	RawKeyCode
	BRA	RedrawPattern

;---- Swap Tracks ----

SwapTrack
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	LEA	SwapTrackText,A0
	BSR	ShowStatusText
swtloop	BSR	GetHexKey
	TST.B	D1
	BEQ.S	swtabor
	CMP.B	#4,D1
	BHI.S	swtloop
	BSR	SaveUndo
	SUBQ.L	#1,D1
	LSL.L	#2,D1
	MOVE.L	D1,D0
	MOVE.L	SongDataPtr,A0
	LEA	sd_patterndata(A0),A0
	MOVE.L	PatternNumber,D1
	LSL.L	#8,D1
	LSL.L	#2,D1
	ADD.L	D1,A0
	MOVE.L	A0,A1
	ADD.L	D0,A0
	MOVE.W	PattCurPos,D0
	DIVU	#6,D0
	AND.L	#$F,D0
	LSL.L	#2,D0
	ADD.L	D0,A1
	MOVEQ	#63,D1
swtloop2
	MOVE.L	(A0),D0
	MOVE.L	(A1),(A0)
	MOVE.L	D0,(A1)
	LEA	16(A0),A0
	LEA	16(A1),A1
	DBRA	D1,swtloop2
	BSR	RedrawPattern
swtabor	BSR	ShowAllRight
	BRA	RestorePtrCol

SwapTrackText	dc.b 'Swap (1/2/3/4) ?',0,0

;---- Block Commands ----

BlockMarkText	dc.b 'mark block 00-00',0,0
BlockErrorText	dc.b 'no block marked !',0
BufEmptyText	dc.b 'buffer is empty !',0
BlockFromPos	dc.w 0
BlockToPos	dc.w 0
BlockMarkFlag	dc.w 0
BlockBufferFlag	dc.w 0
JoinPasteFlag	dc.w 0
PolyPasteFlag	dc.w 0
BuffFromPos	dc.w 0
BuffToPos	dc.w 0

BeginBlock
	CLR.B	RawKeyCode
	TST.W	BlockMarkFlag
	BEQ.S	beblskp
blkunma	CLR.W	BlockMarkFlag
	BRA	ShowAllRight
beblskp	MOVE.W	#1,BlockMarkFlag
	MOVE.W	ScrPattPos,BlockFromPos
	MOVE.W	ScrPattPos,BlockToPos
ShowBlockPos
	MOVE.W	BlockFromPos,D0
	MOVE.W	BlockToPos,D1
	CMP.W	D0,D1
	BHS.S	sbpskip
	EXG	D0,D1
sbpskip	LEA	BlockMarkText+12(PC),A0
	BSR	IntTo2DecAscii
	LEA	BlockMarkText+15(PC),A0
	MOVE.W	D1,D0
	BSR	IntTo2DecAscii
	LEA	BlockMarkText(PC),A0
	BRA	ShowStatusText

Re_MarkBlock
	CLR.B	RawKeyCode
	MOVE.W	#1,BlockMarkFlag
	MOVE.W	BlockToPos,ScrPattPos
	BSR	SetScrPatternPos
	BRA.S	ShowBlockPos

CheckBlockPos
	TST.W	BlockMarkFlag
	BEQ	Return1
	MOVE.W	ScrPattPos,D0
	CMP.W	BlockToPos,D0
	BEQ	Return1
	MOVE.W	D0,BlockToPos
	BRA.S	ShowBlockPos

IntTo2DecAscii
	AND.L	#$FF,D0
	DIVU	#10,D0
	ADD.B	#'0',D0
	MOVE.B	D0,-1(A0)
	SWAP	D0
	ADD.B	#'0',D0
	MOVE.B	D0,(A0)
	RTS

CutBlock
	CLR.B	RawKeyCode
	TST.W	BlockMarkFlag
	BEQ	BlockError
	BSR.S	cobldo
	MOVE.W	#1,BlockMarkFlag
	BRA	ClearBlock

CopyBlock
	CLR.B	RawKeyCode
	TST.W	BlockMarkFlag
	BEQ	BlockError
cobldo	CLR.W	BlockMarkFlag
	MOVE.W	#1,BlockBufferFlag
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	LEA	BlockBuffer,A1
	MOVEQ	#63,D0
cobllop	MOVE.L	(A0),(A1)
	LEA	16(A0),A0
	LEA	16(A1),A1
	DBRA	D0,cobllop
	MOVE.W	BlockFromPos,D0
	MOVE.W	BlockToPos,D1
	CMP.W	D0,D1
	BHS.S	coblskp
	EXG	D0,D1
coblskp	MOVE.W	D0,BuffFromPos
	MOVE.W	D1,BuffToPos
	BRA	ShowAllRight


PasteBlock
	CLR.B	RawKeyCode
	TST.W	BlockBufferFlag
	BEQ	BufferError
	BSR	SaveUndo
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	LEA	BlockBuffer,A1
	MOVE.W	BuffFromPos,D0
	MOVE.W	BuffToPos,D1
	MOVE.W	ScrPattPos,D2
	LSL.W	#4,D0
	LSL.W	#4,D1
	LSL.W	#4,D2
pabllop	MOVE.L	(A1,D0.W),D3
	TST.W	JoinPasteFlag
	BEQ.S	pablskp
	MOVE.L	D3,D4
	AND.L	#$FFFFF000,D4
	BNE.S	pablskp
	MOVE.L	(A0,D2.W),D4
	AND.L	#$FFFFF000,D4
	AND.L	#$00000FFF,D3
	OR.L	D4,D3
pablskp	MOVE.L	D3,(A0,D2.W)
	CMP.W	D0,D1
	BEQ.S	pablend
	CMP.W	#63*16,D0
	BEQ.S	pablend
	CMP.W	#63*16,D2
	BEQ.S	pablend
	ADD.W	#16,D0
	ADD.W	#16,D2
	TST.W	PolyPasteFlag
	BEQ.S	pabllop
	MOVEM.L	A1/D0-D2,-(SP)
	BSR	GotoNextMulti
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	MOVEM.L	(SP)+,A1/D0-D2
	BRA.S	pabllop
pablend	CLR.W	JoinPasteFlag
	CLR.W	PolyPasteFlag
	BSR	ShowAllRight
	BSR	RedrawPattern
	TST.W	ShiftKeyStatus
	BNE	Return1
	MOVE.W	BuffToPos,D0
	SUB.W	BuffFromPos,D0
	ADDQ.W	#1,D0
	ADD.W	ScrPattPos,D0
	CMP.W	#63,D0
	BLS.S	pablset
	MOVEQ	#0,D0
pablset	MOVE.W	D0,ScrPattPos
	BRA	SetScrPatternPos

PolyphonizeBlock
	MOVE.W	#1,PolyPasteFlag
JoinPasteBlock
	MOVE.W	#1,JoinPasteFlag
	BRA	PasteBlock

InsertBlock
	CLR.B	RawKeyCode
	TST.W	BlockBufferFlag
	BEQ	BufferError
	CMP.W	#63,ScrPattPos
	BEQ	PasteBlock
	MOVE.W	BuffToPos,D0
	SUB.W	BuffFromPos,D0
inbllop	MOVE.L	D0,-(SP)
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	BSR	inotr
	MOVE.L	(SP)+,D0
	DBRA	D0,inbllop
	BRA	PasteBlock

DeleteBlock
	CLR.B	RawKeyCode
	TST.W	BlockMarkFlag
	BEQ	BlockError
	BSR	SaveUndo
	MOVE.W	BlockFromPos,D0
	MOVE.W	BlockToPos,D1
	CMP.W	D0,D1
	BHS.S	deblskp
	EXG	D0,D1
deblskp	CMP.W	#63,D1
	BEQ.S	ClearBlock
	CLR.W	BlockMarkFlag
	MOVE.W	D0,ScrPattPos
	ADDQ.W	#1,ScrPattPos
	SUB.W	D0,D1
	MOVE.W	D1,D0
debllop	MOVE.L	D0,-(SP)
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	BSR	dnt
	MOVE.L	(SP)+,D0
	DBRA	D0,debllop
	SUBQ.W	#1,ScrPattPos
	BSR	ShowAllRight
	BRA	RedrawPattern

ClearBlock
	TST.W	BlockMarkFlag
	BEQ	BlockError
	BSR	SaveUndo
	CLR.W	BlockMarkFlag
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	MOVE.W	BlockFromPos,D0
	MOVE.W	BlockToPos,D1
	CMP.W	D0,D1
	BHS.S	clblskp
	EXG	D0,D1
clblskp	LSL.W	#4,D0
	LSL.W	#4,D1
	MOVEQ	#0,D2
clbllop	MOVE.L	D2,(A0,D0.W)
	CMP.W	D0,D1
	BEQ.S	clblend
	ADD.W	#16,D0
	BRA.S	clbllop
clblend	BSR	ShowAllRight
	BRA	RedrawPattern

BackwardsBlock
	CLR.B	RawKeyCode
	TST.W	BlockMarkFlag
	BEQ	BlockError
	BSR	SaveUndo
	CLR.W	BlockMarkFlag
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	MOVE.W	BlockFromPos,D0
	MOVE.W	BlockToPos,D1
	CMP.W	D0,D1
	BHS.S	bablskp
	EXG	D0,D1
bablskp	LSL.W	#4,D0
	LSL.W	#4,D1
babllop	MOVE.L	(A0,D0.W),D2
	MOVE.L	(A0,D1.W),(A0,D0.W)
	MOVE.L	D2,(A0,D1.W)
	CMP.W	D1,D0
	BHS.S	bablend
	ADD.W	#16,D0
	SUB.W	#16,D1
	CMP.W	D1,D0
	BHS.S	bablend
	BRA.S	babllop
bablend	BSR	ShowAllRight
	BRA	RedrawPattern

TransBlockUp
	SF	trblflag
	BRA.S	trbldo
TransBlockDown
	ST	trblflag
trbldo	CLR.B	RawKeyCode
	TST.W	BlockMarkFlag
	BEQ.S	BlockError
	BSR	SaveUndo
	MOVE.W	#2,NoteShift
	MOVE.W	BlockFromPos,D0
	MOVE.W	BlockToPos,D1
	CMP.W	D0,D1
	BHS.S	trblskp
	EXG	D0,D1
trblskp	MOVE.W	D0,D5
	LSL.W	#4,D5
	SUB.W	D0,D1
	MOVE.W	D1,D6
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	LEA	(A0,D5.W),A3
	MOVEQ	#0,D3
	MOVE.B	SampleAllFlag,sampallsave
	ST	SampleAllFlag
	TST.B	trblflag
	BEQ.S	trblup
	BSR	sandlo2
	MOVE.B	sampallsave,SampleAllFlag
	BRA	RedrawPattern
trblup	BSR	sanulo2
	MOVE.B	sampallsave,SampleAllFlag
	BRA	RedrawPattern

trblflag	dc.b 0
sampallsave	dc.b 0

BlockError
	LEA	BlockErrorText,A0
	BSR	ShowStatusText
	BRA	SetErrorPtrCol

BufferError
	LEA	BufEmptyText,A0
	BSR	ShowStatusText
	BRA	SetErrorPtrCol

ExpandTrack
	CLR.B	RawKeyCode
	BSR	SaveUndo
	MOVE.W	ScrPattPos,PosSave
	ADDQ.W	#1,ScrPattPos
	CMP.W	#64,ScrPattPos
	BHS.S	extrend
extrlop	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	BSR	inotr
	ADDQ.W	#2,ScrPattPos
	CMP.W	#64,ScrPattPos
	BLO.S	extrlop
extrend	MOVE.W	PosSave,ScrPattPos
	BRA	RedrawPattern

ContractTrack
	CLR.B	RawKeyCode
	BSR	SaveUndo
	MOVE.W	ScrPattPos,PosSave
	ADDQ.W	#1,ScrPattPos
	CMP.W	#64,ScrPattPos
	BHS.S	cotrend
cotrlop	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	BSR	dnt
	ADDQ.W	#1,ScrPattPos
	CMP.W	#64,ScrPattPos
	BLO.S	cotrlop
cotrend	MOVE.W	PosSave,ScrPattPos
	BRA	RedrawPattern

PosSave	dc.w 0


KillToEndOfTrack
	CLR.B	RawKeyCode
	BSR	SaveUndo
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	MOVE.W	ScrPattPos,D0
	MOVE.W	D0,D1
	MULU	#16,D1
	LEA	(A0,D1.W),A0
	TST.W	ShiftKeyStatus
	BNE.S	KillToStart
kteot	CLR.L	(A0)
	LEA	16(A0),A0
	ADDQ.W	#1,D0
	CMP.W	#64,D0
	BLO.S	kteot
	BRA	RedrawPattern

KillToStart
	CLR.L	(A0)
	LEA	-16(A0),A0
	TST.W	D0
	BEQ	RedrawPattern
	SUBQ.W	#1,D0
	BRA.S	KillToStart

UndoLastChange
	CLR.B	RawKeyCode
	MOVEQ	#0,D0
	BSR	GetPositionPtr
	LEA	UndoBuffer,A1
	MOVE.W	#255,D0
unlalop	MOVE.L	(A1),D1
	MOVE.L	(A0),(A1)+
	MOVE.L	D1,(A0)+
	DBRA	D0,unlalop
	BRA	RedrawPattern

SaveUndo
	MOVEM.L	D0/A0-A1,-(SP)
	MOVEQ	#0,D0
	BSR	GetPositionPtr
	LEA	UndoBuffer,A1
	MOVE.W	#255,D0
saunlop	MOVE.L	(A0)+,(A1)+
	DBRA	D0,saunlop
	MOVEM.L	(SP)+,D0/A0-A1
	RTS

BoostAll
	LEA	BoostAllText,A0
	BSR	AreYouSure
	BNE	SetErrorPtrCol
	MOVE.W	InsNum,SaveInstr
	MOVE.W	#1,InsNum
boallop	BSR	ShowSampleInfo
	BSR	Boost2
	ADDQ.W	#1,InsNum
	CMP.W	#32,InsNum
	BLO.S	boallop
	MOVE.W	SaveInstr,InsNum
	BRA	ShowSampleInfo

FilterAll
	LEA	FilterAllText,A0
	BSR	AreYouSure
	BNE	SetErrorPtrCol
	MOVE.W	InsNum,SaveInstr
	MOVE.W	#1,InsNum
fiallop	BSR	ShowSampleInfo
	BSR	Filter2
	ADDQ.W	#1,InsNum
	CMP.W	#32,InsNum
	BLO.S	fiallop
	MOVE.W	SaveInstr,InsNum
	BRA	ShowSampleInfo

BoostAllText	dc.b 'boost all samples',0
FilterAllText	dc.b 'filter all sampls',0
SaveInstr	dc.w 0

;---- Check Function Keys F1-F2 ----

CheckF1_F2
	CMP.B	#80,RawKeyCode
	BEQ.S	SetOctaveLow
	CMP.B	#81,RawKeyCode
	BEQ.S	SetOctaveHigh
	RTS	
 
SetOctaveLow
	MOVE.L	#KbdTransTable1,KeyTransTabPtr
	CLR.B	RawKeyCode
	RTS	
 
SetOctaveHigh
	MOVE.L	#KbdTransTable2,KeyTransTabPtr
	CLR.B	RawKeyCode
	RTS	


;---- Get Hex Key ----

GetHexByte
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	CLR.B	RawKeyCode
	MOVEQ	#0,D0
	MOVE.W	TextOffset,D0
	DIVU	#40,D0
	ADDQ.W	#6,D0
	MOVE.W	D0,LineCurY
	SWAP	D0
	LSL.W	#3,D0
	ADDQ.W	#4,D0
	MOVE.W	D0,LineCurX
	BSR	UpdateLineCurPos
	BSR.S	GetHexKey
	MOVE.L	D1,D0
	MOVE.B	D1,D6
	LSL.B	#4,D6
	BSR	PrintHexDigit
	ADD.W	#8,LineCurX
	BSR	UpdateLineCurPos
	BSR.S	GetHexKey
	MOVE.L	D1,D0
	OR.B	D1,D6
	BSR	PrintHexDigit
	CLR.W	LineCurX
	MOVE.W	#270,LineCurY
	BSR	UpdateLineCurPos
	BSR	RestorePtrCol
	MOVEQ	#0,D0
	MOVE.B	D6,D0
	RTS

GetHexKey
	CLR.B	MixChar
	MOVEQ	#0,D0
	MOVEQ	#0,D1
	BTST	#2,$DFF016
	BEQ.S	ghkreturn
	BSR	DoKeyBuffer
	MOVE.B	RawKeyCode,D0
	MOVE.B	D0,MixChar
	BEQ.S	GetHexKey
	CMP.B	#68,D0 ; CR
	BEQ.S	ghkreturn
	CMP.B	#69,D0 ; Esc
	BEQ.S	ghkreturn
	CMP.B	#79,D0 ; <--
	BEQ.S	ghkleft
	CMP.B	#78,D0 ; -->
	BEQ.S	ghkright
	BSR.S	CheckHexKey
	CMP.B	#16,D1
	BEQ.S	GetHexKey
	CLR.B	RawKeyCode
ghkreturn
	MOVEQ	#0,D0
	RTS

ghkleft	MOVEQ	#-1,D0
	RTS

ghkright
	MOVEQ	#1,D0
	RTS

CheckHexKey
	LEA	RawKeyHexTable,A0
	MOVEQ	#0,D1
chxloop	CMP.B	(A0)+,D0
	BEQ	Return1
	ADDQ.B	#1,D1
	CMP.B	#16,D1
	BNE.S	chxloop
	RTS

;---- Enter Edit Commands (effects) ----

EditCommand
	TST.L	EditMode
	BEQ	Return1
	MOVEQ	#0,D0
	MOVE.B	RawKeyCode,D0
	BSR.S	CheckHexKey
	CMP.B	#16,D1
	BNE.S	DoEditCommand
	RTS

DoEditCommand
	CMP.W	#1,PattCurPos
	BNE.S	ChkPos2
	CMP.W	#1,D1
	BHI	Return1
	MOVE.L	#$FFF,D2
	CLR.W	CmdOffset
	LSL.W	#4,D1
	LSL.W	#8,D1
	BRA	UpdateCommand

ChkPos2	CMP.W	#2,PattCurPos
	BNE.S	ChkPos3
	MOVE.L	#$FFF,D2
	MOVE.W	#2,CmdOffset
	LSL.W	#4,D1
	LSL.W	#8,D1
	BRA	UpdateCommand

ChkPos3	CMP.W	#3,PattCurPos
	BNE.S	ChkPos4
	MOVE.L	#$F0FF,D2
	MOVE.W	#2,CmdOffset
	LSL.W	#8,D1
	BRA	UpdateCommand

ChkPos4	CMP.W	#4,PattCurPos
	BNE.S	ChkPos5
	MOVE.L	#$FF0F,D2
	MOVE.W	#2,CmdOffset
	LSL.W	#4,D1
	BRA	UpdateCommand

ChkPos5	CMP.W	#5,PattCurPos
	BNE.S	ChkPos7
	MOVE.L	#$FFF0,D2
	MOVE.W	#2,CmdOffset
	BRA	UpdateCommand

ChkPos7	CMP.W	#7,PattCurPos
	BNE.S	ChkPos8
	CMP.W	#1,D1
	BHI	Return1
	MOVE.L	#$FFF,D2
	MOVE.W	#4,CmdOffset
	LSL.W	#4,D1
	LSL.W	#8,D1
	BRA	UpdateCommand

ChkPos8	CMP.W	#8,PattCurPos
	BNE.S	ChkPos9
	MOVE.L	#$FFF,D2
	MOVE.W	#6,CmdOffset
	LSL.W	#4,D1
	LSL.W	#8,D1
	BRA	UpdateCommand

ChkPos9	CMP.W	#9,PattCurPos
	BNE.S	ChkPos10
	MOVE.L	#$F0FF,D2
	MOVE.W	#6,CmdOffset
	LSL.W	#8,D1
	BRA	UpdateCommand

ChkPos10
	CMP.W	#10,PattCurPos
	BNE.S	ChkPos11
	MOVE.L	#$FF0F,D2
	MOVE.W	#6,CmdOffset
	LSL.W	#4,D1
	BRA	UpdateCommand

ChkPos11
	CMP.W	#11,PattCurPos
	BNE.S	ChkPos13
	MOVE.L	#$FFF0,D2
	MOVE.W	#6,CmdOffset
	BRA	UpdateCommand

ChkPos13
	CMP.W	#13,PattCurPos
	BNE.S	ChkPos14
	CMP.W	#1,D1
	BHI	Return1
	MOVE.L	#$FFF,D2
	MOVE.W	#8,CmdOffset
	LSL.W	#4,D1
	LSL.W	#8,D1
	BRA	UpdateCommand

ChkPos14
	CMP.W	#14,PattCurPos
	BNE.S	ChkPos15
	MOVE.L	#$FFF,D2
	MOVE.W	#10,CmdOffset
	LSL.W	#4,D1
	LSL.W	#8,D1
	BRA	UpdateCommand

ChkPos15
	CMP.W	#15,PattCurPos
	BNE.S	ChkPos16
	MOVE.L	#$F0FF,D2
	MOVE.W	#10,CmdOffset
	LSL.W	#8,D1
	BRA	UpdateCommand

ChkPos16
	CMP.W	#16,PattCurPos
	BNE.S	ChkPos17
	MOVE.L	#$FF0F,D2
	MOVE.W	#10,CmdOffset
	LSL.W	#4,D1
	BRA	UpdateCommand

ChkPos17
	CMP.W	#17,PattCurPos
	BNE.S	ChkPos19
	MOVE.L	#$FFF0,D2
	MOVE.W	#10,CmdOffset
	BRA	UpdateCommand

ChkPos19
	CMP.W	#19,PattCurPos
	BNE.S	ChkPos20
	CMP.W	#1,D1
	BHI	Return1
	MOVE.L	#$FFF,D2
	MOVE.W	#12,CmdOffset
	LSL.W	#4,D1
	LSL.W	#8,D1
	BRA.S	UpdateCommand

ChkPos20
	CMP.W	#20,PattCurPos
	BNE.S	ChkPos21
	MOVE.L	#$FFF,D2
	MOVE.W	#14,CmdOffset
	LSL.W	#4,D1
	LSL.W	#8,D1
	BRA.S	UpdateCommand

ChkPos21
	CMP.W	#21,PattCurPos
	BNE.S	ChkPos22
	MOVE.L	#$F0FF,D2
	MOVE.W	#14,CmdOffset
	LSL.W	#8,D1
	BRA.S	UpdateCommand

ChkPos22
	CMP.W	#22,PattCurPos
	BNE.S	MustBePos23
	MOVE.L	#$FF0F,D2
	MOVE.W	#14,CmdOffset
	LSL.W	#4,D1
	BRA.S	UpdateCommand

MustBePos23
	MOVE.L	#$FFF0,D2
	MOVE.W	#14,CmdOffset
UpdateCommand
	MOVE.L	SongDataPtr,A0
	LEA	sd_patterndata(A0),A0
	MOVE.L	PatternNumber,D0
	LSL.L	#8,D0
	LSL.L	#2,D0
	ADD.L	D0,A0
	MOVEQ	#0,D0
	MOVE.W	ScrPattPos,D0
	LSL.W	#4,D0
	EXT.L	D0
	ADD.L	D0,A0
	ADD.W	CmdOffset,A0
	AND.W	D2,(A0)
	ADD.W	D1,(A0)
	MOVE.W	(A0),WordNumber
	MOVEQ	#0,D0
	MOVE.W	ScrPattPos,D0
	MULU	#7*40,D0
	MOVE.W	PattCurPos,D1
	DIVU	#6,D1
	MULU	#9,D1
	ADD.L	D1,D0
	ADD.W	#7528,D0
	MOVE.W	D0,TextOffset
	CMP.W	#0,CmdOffset
	BEQ.S	ShowInstrNibble
	CMP.W	#4,CmdOffset
	BEQ.S	ShowInstrNibble
	CMP.W	#8,CmdOffset
	BEQ.S	ShowInstrNibble
	CMP.W	#12,CmdOffset
	BEQ.S	ShowInstrNibble
	BSR	PrintHexWord
dscend	BSR	EditMoveDown
	CLR.B	RawKeyCode
	RTS

ShowInstrNibble
	SUBQ.W	#1,TextOffset
	MOVE.W	#1,TextLength
	MOVEQ	#0,D0
	MOVE.W	(A0),D0
	AND.W	#$F000,D0
	BNE.S	sinokok
	TST.B	BlankZeroFlag
	BEQ.S	sinokok	
	MOVE.L	#BlankInsText,D0
	BRA.S	sinprt
sinokok	LSR.W	#4,D0
	LSR.W	#7,D0
	ADD.L	#FastHexTable+1,D0
sinprt	MOVE.L	D0,ShowTextPtr
	BSR	ShowText
	BRA.S	dscend

;---- Store/Insert Effect Commands ----

CheckStoreEffect
	MOVEQ	#0,D0
	MOVE.B	RawKeyCode,D0
	BEQ	Return1
	CMP.B	#70,D0 ; Del
	BEQ	CheckKeyboard2
	CMP.B	#10,D0
	BHI	Return1
	SUBQ.B	#1,D0
	ADD.B	D0,D0
	MOVE.L	D0,D7
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	MOVE.W	ScrPattPos,D0
	LSL.W	#4,D0
	LEA	(A0,D0.W),A0
	MOVE.W	2(A0),D0
	AND.W	#$0FFF,D0
	LEA	EffectMacros,A0
	MOVE.W	D0,(A0,D7.W)
	LEA	CommandStoredText,A0
	BSR	ShowStatusText
	BSR	WaitALittle
	BRA	ShowAllRight

CommandStoredText dc.b 'command stored!',0

CheckInsertEffect
	CLR.W	InsEfx
	MOVEQ	#0,D0
	MOVE.B	RawKeyCode,D0
	BEQ	Return1
	CMP.B	#70,D0 ; Del
	BEQ	CheckKeyboard2
	CMP.B	#11,D0 ; -
	BEQ	DecreaseEffect
	CMP.B	#12,D0 ; = (+)
	BEQ.S	IncreaseEffect
	CMP.B	#13,D0 ; \
	BEQ.S	CopyEffect
	CMP.B	#10,D0
	BHI	Return1
	SUBQ.B	#1,D0
	ADD.B	D0,D0
	LEA	EffectMacros,A0
	MOVE.W	(A0,D0.W),InsEfx
DoInsEffect
	MOVE.B	#70,RawKeyCode
	BRA	CheckNoteKeys

GetLastEffect
	MOVE.W	PattCurPos,D0
	BSR	GetPositionPtr
	MOVE.W	ScrPattPos,D0
	SUBQ.W	#1,D0
	AND.W	#$3F,D0
	LSL.W	#4,D0
	LEA	(A0,D0.W),A0
	MOVE.W	2(A0),D0
	AND.W	#$0FFF,D0
	RTS

CopyEffect
	BSR.S	GetLastEffect
	MOVE.W	D0,InsEfx
	BRA.S	DoInsEffect

IncreaseEffect
	BSR.S	GetLastEffect
	MOVE.W	D0,D1
	AND.W	#$0F00,D1
	CMP.W	#$0E00,D1
	BEQ.S	IncECom
	ADDQ.B	#1,D0
	MOVE.W	D0,InsEfx
	BRA.S	DoInsEffect

IncECom	MOVE.W	D0,D1
	ADDQ.B	#1,D1
	AND.B	#$0F,D1
	AND.W	#$0FF0,D0
	OR.B	D1,D0
	MOVE.W	D0,InsEfx
	BRA.S	DoInsEffect

DecreaseEffect
	BSR.S	GetLastEffect
	MOVE.W	D0,D1
	AND.W	#$0F00,D1
	CMP.W	#$0E00,D1
	BEQ.S	DecECom
	SUBQ.B	#1,D0
	MOVE.W	D0,InsEfx
	BRA	DoInsEffect

DecECom	MOVE.W	D0,D1
	SUBQ.B	#1,D1
	AND.B	#$0F,D1
	AND.W	#$0FF0,D0
	OR.B	D1,D0
	MOVE.W	D0,InsEfx
	BRA	DoInsEffect

InsEfx	dc.w 0

;---- Check Keyboard for notekeys ----

CheckKeyboard
	TST.B	RawKeyCode
	BEQ	Return1
	TST.W	LeftAmigaStatus
	BNE	Return1
	TST.W	CtrlKeyStatus
	BNE	Return1
	TST.W	ShiftKeyStatus
	BNE	CheckStoreEffect
	TST.W	AltKeyStatus
	BNE	CheckInsertEffect
CheckKeyboard2
	MOVE.W	PattCurPos,D0
	BEQ.S	CheckNoteKeys
	CMP.W	#6,D0
	BEQ.S	CheckNoteKeys
	CMP.W	#12,D0
	BEQ.S	CheckNoteKeys
	CMP.W	#18,D0
	BEQ.S	CheckNoteKeys
	TST.L	EditMode
	BNE	EditCommand
	RTS	
 
CheckNoteKeys
	LEA	RawKeyScaleTable,A0
	MOVE.B	RawKeyCode,D1
	MOVEQ	#38,D0
cnkloop	CMP.B	0(A0,D0.W),D1
	BEQ.S	NoteKeyPressed
	DBRA	D0,cnkloop
	RTS

NoteKeyPressed
	CLR.B	RawKeyCode
	MOVE.L	KeyTransTabPtr,A1
	MOVE.B	(A1,D0.W),D0
	CMP.W	#36,D0
	BHS.S	nkpskip
	MOVE.L	SplitAddress,D1
	BEQ.S	nkpskip
	CLR.L	SplitAddress
	MOVE.L	D1,A0
	MOVE.B	D0,(A0)
	MOVE.W	SamNoteType,D1
	BEQ	ShowSplit
	CLR.W	SamNoteType
	AND.W	#$FF,D0
	MOVE.W	D0,(A0)
	CMP.W	#1,D1
	BEQ.S	loclab1
	CMP.W	#3,D1
	BEQ	ShowS2TuneNote
	CMP.W	#4,D1
	BEQ	ShowAllRight
	JMP	ShowResamNote
loclab1	JMP	ShowSamNote
nkpskip	MOVE.W	InsNum,PlayInsNum
	TST.B	SplitFlag
	BEQ.S	nkpskip2
	LEA	SplitInstrTable,A1
	MOVE.B	(A1,D0.W),D1
	BEQ.S	nkpskip3
	MOVE.B	D1,PlayInsNum+1
nkpskip3
	LEA	SplitTransTable,A1
	MOVE.B	(A1,D0.W),D0
nkpskip2
	MOVEQ	#0,D2
playtheinstr
	LEA	PeriodTable,A1 ; note in d0 here
	MOVE.L	D0,D4
	LSL.W	#1,D0
	MOVEQ	#0,D3
	MOVE.W	(A1,D0.W),D3

	MOVE.L	SongDataPtr,A0 ; This fixes finetune...
	LEA	14(A0),A0
	MOVE.W	InsNum,D1
	BNE.S	nkpskipit
	MOVE.W	LastInsNum,D1
nkpskipit
	MULU	#30,D1
	ADD.L	D1,A0
	MOVEQ	#0,D1
	MOVE.B	(A0),D1 ; get finetune
	MULU	#37*2,D1
	ADD.L	D1,A1
	MOVE.W	(A1,D0.W),CurrentPlayNote
	TST.L	D2
	BEQ.S	nkpnrml
	CMP.B	#2,pnoteflag
	BNE	antpskip

nkpnrml	TST.L	EditMode
	BEQ	antpskip
AddNoteToPattern
	MOVE.L	SongDataPtr,A0
	LEA	sd_patterndata(A0),A0	; Find first pattern
	MOVE.L	PatternNumber,D0
	LSL.L	#8,D0
	LSL.L	#2,D0
	ADD.L	D0,A0	; Find current pattern
	MOVEQ	#0,D0
	MOVE.W	ScrPattPos,D0
	BSR	QuantizeCheck
	LSL.W	#4,D0
	EXT.L	D0
	ADD.L	D0,A0	; Find current pos
	MOVE.W	PattCurPos,D0
	DIVU	#6,D0
	LSL.W	#2,D0
	EXT.L	D0
	ADD.L	D0,A0	; Find current channel
	TST.W	AltKeyStatus
	BEQ.S	antpsks
	AND.L	#$FFFFF000,(A0)
	MOVE.W	InsEfx,D0
	OR.W	D0,2(A0)
	BRA.S	antp3
antpsks	TST.W	ShiftKeyStatus
	BEQ.S	antpskip2
	CLR.L	(A0)
antpskip2
	MOVE.W	D3,(A0)	; Put note into pattern
	BEQ.S	antp2
	TST.B	AutoInsFlag
	BEQ.S	antp4
	MOVE.W	VolumeEfx,2(A0)
antp4	MOVEQ	#0,D0
	MOVE.W	PlayInsNum,D0
	LSL.W	#4,D0
	AND.B	#$F,2(A0)
	ADD.B	D0,2(A0)
	LSL.W	#4,D0
	AND.W	#$F000,D0
	OR.W	D0,(A0)
	BRA.S	antp3

antp2	AND.W	#$0FFF,2(A0)
antp3	MOVE.W	2(A0),CurrCmds
	MOVE.L	NoteNamesPtr,A0
	LSL.W	#2,D4
	EXT.L	D4
	ADD.L	D4,A0
	MOVE.L	A0,ShowTextPtr
	MOVE.W	#3,TextLength
	MOVEQ	#0,D0
	MOVE.W	ScrPattPos,D0
	BSR	QuantizeCheck
	MULU	#7*40,D0
	MOVEQ	#0,D1
	MOVE.W	PattCurPos,D1
	DIVU	#6,D1
	MULU	#9,D1
	ADD.L	D1,D0
	ADD.W	#7524,D0
	MOVE.W	D0,TextOffset
	TST.W	AltKeyStatus
	BEQ.S	antpnot
	ADDQ.W	#4,D0
	MOVE.W	D0,TextOffset
	BRA.S	antpalt
antpnot	BSR	ShowText	; Show notename
	BSR	PrintHiInstrNum
antpalt
	MOVE.W	CurrCmds,WordNumber
	BSR	PrintHexWord
	BSR	EditMoveDown
antpskip
	TST.W	DidQuantize
	BNE.S	testmul
	TST.W	CurrentPlayNote
	BNE	PlayNote
testmul	TST.B	MultiFlag
	BEQ	Return1
GotoNextMulti
	MOVEQ	#0,D0
	MOVE.W	PattCurPos,D0
	DIVU	#6,D0
	LEA	MultiModeNext,A0
	MOVE.B	(A0,D0.W),D0
	SUBQ.W	#1,D0
	AND.W	#3,D0
	MULU	#6,D0
	MOVE.W	D0,PattCurPos
	BRA	UpdateCursorPos

QuantizeCheck
	TST.L	RunMode
	BEQ.S	qcend
	MOVEQ	#0,D1
	MOVE.B	QuantizeValue,D1
	BEQ.S	qcend
	CMP.W	#1,D1
	BEQ.S	QuanOne
	MOVE.W	#1,DidQuantize
	MOVE.L	D1,D2
	LSR.W	#1,D2
	ADD.W	D0,D2
	AND.W	#$003F,D2
	DIVU	D1,D2
	MULU	D1,D2
	CMP.W	D0,D2
	BHI.S	qcskip
	CLR.W	DidQuantize
qcskip	MOVE.W	D2,D0
	RTS

QuanOne	MOVE.L	CurrSpeed,D1
	LSR.L	#1,D1
	CMP.L	Counter,D1
	BLS.S	QuantToNextNote
qcend	CLR.W	DidQuantize
	RTS	
 
QuantToNextNote
	ADDQ.W	#1,D0
	AND.W	#$003F,D0
	MOVE.W	#1,DidQuantize
	RTS

PlayNote
	CMP.W	#18,PattCurPos
	BNE.S	ChkChan3
	LEA	$DFF0D0,A5
	LEA	audchan4temp,A4
	BRA.S	DoPlayNote

ChkChan3
	CMP.W	#12,PattCurPos
	BNE.S	ChkChan2
	LEA	$DFF0C0,A5
	LEA	audchan3temp,A4
	BRA.S	DoPlayNote

ChkChan2
	CMP.W	#6,PattCurPos
	BNE.S	ChkChan1
	LEA	$DFF0B0,A5
	LEA	audchan2temp,A4
	BRA.S	DoPlayNote

ChkChan1
	TST.W	PattCurPos
	BNE	Return1
	LEA	$DFF0A0,A5
	LEA	audchan1temp,A4
DoPlayNote
	MOVE.L	A5,NoteAddr
	LEA	SampleInfo,A6
	MOVE.W	PlayInsNum,D0
	BEQ.S	dpnplay
	LSL.L	#2,D0
	LEA	SamplePtrs,A0
	LEA	SampleInfo2,A6
	MOVE.L	0(A0,D0.W),si_pointer2
	MOVE.L	SongDataPtr,A0
	LEA	-10(A0),A0
	MOVE.W	PlayInsNum,D0
	MOVE.B	D0,PlayInsNum2
	MULU	#30,D0
	ADD.L	D0,A0
	MOVE.L	22(A0),SampleInfo2
	MOVE.L	26(A0),si_long2
dpnplay	MOVE.B	PlayInsNum2,n_samplenum(A4)
	MOVE.W	4(A6),D0 ; repeat
	BNE.S	dpn2
	MOVE.W	(A6),D0  ; length
	BRA.S	dpn3
dpn2	ADD.W	6(A6),D0 ; add replen
dpn3	MOVEQ	#0,D1
	MOVE.B	3(A6),D1
	MOVE.W	D1,8(A5)		; Set volume
	MOVE.B	D1,n_volume(A4)
	MOVE.W	CurrentPlayNote,6(A5)	; Set period
	MOVE.W	CurrentPlayNote,n_period(A4)
	MOVE.W	n_dmabit(A4),$DFF096	; Channel DMA off
	MOVE.L	8(A6),D1
	ADD.L	StartOfs,D1
	MOVE.L	D1,(A5)		; Set sampledata pointer
	CLR.L	StartOfs
	MOVE.L	D1,n_start(A4)
	MOVE.W	D0,4(A5)		; Set length
	MOVE.W	D0,n_length(A4)
	BNE.S	dpnnz
	MOVEQ	#1,D0
	MOVE.W	D0,4(A5)
	MOVE.W	D0,n_length(A4)
dpnnz	MOVE.W	CurrentPlayNote,D0
	BSR	PlayNoteAnalyze
	MOVE.W	DMAWait,D0
dpnolp1	DBRA	D0,dpnolp1
	MOVE.W	n_dmabit(A4),D0
	AND.W	ActiveChannels,D0
	OR.W	#$8000,D0
	MOVE.W	D0,$DFF096	; Turn DMA back on
	MOVE.W	DMAWait,D0
dpnolp2	DBRA	D0,dpnolp2
	MOVEQ	#0,D1
	MOVE.W	4(A6),D1 ; repeat*2
	ADD.L	D1,D1
	ADD.L	8(A6),D1 ; + startptr
	MOVE.L	D1,(A5)	; Set loop pointer
	MOVE.L	D1,n_loopstart(A4)
	MOVE.W	6(A6),4(A5)	; Set loop length
	MOVE.W	6(A6),n_replen(A4)
	ST	n_trigger(A4)
	BRA	testmul

PlayInsNum2	dc.b 0,0

;---- Check Cursor Arrow Keys ----
 
ArrowKeys
	CMP.B	#4,EnterTextFlag
	BEQ	Return1
	MOVE.B	RawKeyCode,D0
	TST.W	GetLineFlag
	BNE.S	arkeskip
	TST.W	ShiftKeyStatus
	BNE.S	arkeskip
	TST.W	AltKeyStatus
	BNE.S	arkeskip
	CMP.W	#2,CurrScreen
	BEQ.S	arkeskip
	CMP.B	#78,D0
	BEQ	RightArrow
	CMP.B	#79,D0
	BEQ	LeftArrow
arkeskip
	CMP.B	#76,D0
	BEQ.S	UpArrow
	CMP.B	#77,D0
	BEQ	DownArrow
	CLR.W	ArrowPressed
	RTS	

ArrowPressed	dc.w	0
ArrowRepCounter	dc.w	0

UpArrow	TST.L	RunMode
	BNE	Return1
	CMP.W	#2,CurrScreen
	BEQ	Return1
	CMP.W	#3,CurrScreen
	BEQ	Return1
	CMP.W	#4,CurrScreen
	BEQ	Return1
	CMP.W	#6,CurrScreen
	BEQ	Return1
	TST.W	ArrowPressed
	BEQ.S	MoveOneUp
	ADDQ.W	#1,ArrowRepCounter
	TST.W	AltKeyStatus
	BNE.S	AltUpArrow
	TST.W	ShiftKeyStatus
	BNE.S	ShiftUpArrow
	CMP.W	#6,ArrowRepCounter
	BPL.S	MoveOneUp
	RTS

ShiftUpArrow
	CMP.W	#3,ArrowRepCounter
	BPL.S	MoveOneUp
	RTS

AltUpArrow
	CMP.W	#1,ArrowRepCounter
	BPL.S	MoveOneUp
	RTS

MoveOneUp
	CLR.W	ArrowRepCounter
	MOVE.W	#$FFFF,ArrowPressed
	CMP.L	#'patt',RunMode
	BEQ	Return1
	SUBQ.W	#1,ScrPattPos
	AND.W	#$003F,ScrPattPos
	BRA	SetScrPatternPos

DownArrow
	TST.L	RunMode
	BNE	Return1
	CMP.W	#2,CurrScreen
	BEQ	Return1
	CMP.W	#3,CurrScreen
	BEQ	Return1
	CMP.W	#4,CurrScreen
	BEQ	Return1
	CMP.W	#6,CurrScreen
	BEQ	Return1
	TST.W	ArrowPressed
	BEQ.S	MoveOneDown
	ADDQ.W	#1,ArrowRepCounter
	TST.W	AltKeyStatus
	BNE.S	AltDownArrow
	TST.W	ShiftKeyStatus
	BNE.S	ShiftDownArrow
	CMP.W	#6,ArrowRepCounter
	BPL.S	MoveOneDown
	RTS

ShiftDownArrow
	CMP.W	#3,ArrowRepCounter
	BPL.S	MoveOneDown
	RTS	
 
AltDownArrow
	CMP.W	#1,ArrowRepCounter
	BPL.S	MoveOneDown
	RTS	
 
MoveOneDown
	CLR.W	ArrowRepCounter
	MOVE.W	#$FFFF,ArrowPressed
	TST.L	RunMode
	BNE	Return1
	ADDQ.W	#1,ScrPattPos
	AND.W	#$003F,ScrPattPos
	BRA	SetScrPatternPos

EditMoveDown
	TST.L	RunMode
	BNE	Return1
	MOVE.W	EditMoveAdd,D0
	ADD.W	D0,ScrPattPos
	AND.W	#$003F,ScrPattPos
	BRA	SetScrPatternPos

EditMoveAdd	dc.w 1

RightArrow
	TST.W	ArrowPressed
	BEQ.S	MoveOneRight
	ADDQ.W	#1,ArrowRepCounter
	CMP.W	#6,ArrowRepCounter
	BPL.S	MoveOneRight
	RTS	
 
PatternOneUp
	ADDQ.L	#1,PatternNumber
	AND.L	#$3F,PatternNumber
	BSR	Wait_4000
	BRA	RedrawPattern

MoveOneRight
	CLR.W	ArrowRepCounter
	MOVE.W	#$FFFF,ArrowPressed
	ADDQ.W	#1,PattCurPos
	CMP.W	#24,PattCurPos
	BMI.S	morskip
	CLR.W	PattCurPos
morskip	BRA.S	UpdateCursorPos
 
LeftArrow
	TST.W	ArrowPressed
	BEQ.S	MoveOneLeft
	ADDQ.W	#1,ArrowRepCounter
	CMP.W	#6,ArrowRepCounter
	BPL.S	MoveOneLeft
	RTS	
 
PatternOneDown
	SUBQ.L	#1,PatternNumber
	AND.L	#$3F,PatternNumber
	BSR	Wait_4000
	BRA	RedrawPattern

MoveOneLeft
	CLR.W	ArrowRepCounter
	MOVE.W	#$FFFF,ArrowPressed
	SUBQ.W	#1,PattCurPos
	TST.W	PattCurPos
	BPL	UpdateCursorPos
	MOVE.W	#23,PattCurPos
UpdateCursorPos
	TST.W	SamScrEnable
	BNE	Return1
	MOVE.W	PattCurPos,D0
	LEA	CursorPosTable,A0
	MOVE.B	0(A0,D0.W),D0
	LSL.W	#3,D0
	ADD.W	#9,D0
	MOVE.W	#$BD,D1
	MOVEQ	#14,D2 ; 14 lines
	LEA	CursorSpriteData,A0
	BRA	SetSpritePos

ArrowKeys2
	MOVE.B	RawKeyCode,D0
	CMP.B	#76,D0
	BEQ.S	UpArrow2
	CMP.B	#77,D0
	BEQ.S	DownArrow2
	CMP.B	#79,D0
	BEQ.S	LeftArrow2
	CMP.B	#78,D0
	BEQ	RightArrow2
	RTS

UpArrow2
	CMP.W	#2,CurrScreen
	BEQ	HelpUp
	CMP.W	#3,CurrScreen
	BEQ	FilenameOneUp
	CMP.W	#4,CurrScreen
	BEQ	PED_OneUp
	CMP.W	#6,CurrScreen
	BEQ	PLSTOneUp
	RTS

DownArrow2
	CMP.W	#2,CurrScreen
	BEQ	HelpDown
	CMP.W	#3,CurrScreen
	BEQ	FilenameOneDown
	CMP.W	#4,CurrScreen
	BEQ	PED_OneDown
	CMP.W	#6,CurrScreen
	BEQ	PLSTOneDown
	RTS

LeftArrow2
	TST.W	ShiftKeyStatus
	BNE	PositionDown
	TST.W	AltKeyStatus
	BNE	PatternOneDown
	TST.W	CtrlKeyStatus
	BNE	SampleNumDown
	CMP.W	#2,CurrScreen
	BEQ	HelpLeft
	RTS

RightArrow2
	TST.W	ShiftKeyStatus
	BNE	PositionUp
	TST.W	AltKeyStatus
	BNE	PatternOneUp
	TST.W	CtrlKeyStatus
	BNE	SampleNumUp
	CMP.W	#2,CurrScreen
	BEQ	HelpRight
	RTS

;---- Update Line Cursor Position ----

UpdateLineCurPos
	MOVE.W	LineCurX,D0
	MOVE.W	LineCurY,D1
	SUBQ.W	#1,D0
	MOVEQ	#2,D2
	LEA	LineCurSpriteData,A0
	BRA	SetSpritePos

;---- Check Gadgets ----

CheckGadgets
	BSR	UpdatePointerPos
	BSR.S	CheckGadgets2
	CLR.W	notogfl
	CMP.W	#$FFFF,GadgRepeat
	BEQ.S	CgRepeat
	MOVEQ	#0,D0
cgloop	ADDQ.L	#1,D0
	TST.B	HHFlag
	BEQ.S	cgnorma
	CMP.L	#40000*5,D0
	BEQ.S	CgRepeat
	BRA.S	cgnorm2
cgnorma	CMP.L	#40000,D0
	BEQ.S	CgRepeat
cgnorm2	BTST	#6,$BFE001
	BEQ.S	cgloop
	CLR.W	UpOrDown
	BRA	MainLoop
 
CgRepeat
	TST.B	HHFlag
	BEQ.S	cgrepe2
	BSR	Wait_4000
cgrepe2	MOVE.W	#$FFFF,GadgRepeat
	BTST	#6,$BFE001
	BEQ.S	CheckGadgets
	CLR.W	GadgRepeat
	CLR.W	UpOrDown
	BRA	MainLoop
 
CheckGadgets2
	MOVE.W	MouseX,MouseX2
	BNE.S	cgskip
	TST.W	MouseY
	BNE.S	cgskip
ChkQuit	LEA	QuitPTText,A0
	BSR	AreYouSure
	BEQ	ExitCleanup	; Quit PT!
	RTS
cgskip	MOVE.W	MouseY,MouseY2
	CMP.W	#4,CurrScreen
	BEQ	CheckPresEdGadgs
	MOVE.W	MouseX2,D0
	MOVE.W	MouseY2,D1
	CMP.W	#122,D1
	BHS	WantedPattGadg
	CMP.W	#111,D1
	BHS	CheckSmplNamOrLoad
	CMP.W	#100,D1
	BHS	TypeInSongName
	CMP.W	#120,D0
	BLO.S	cgskip2
	CMP.W	#6,CurrScreen
	BEQ	CheckPLSTGadgs
cgskip2	CMP.W	#2,CurrScreen
	BEQ	Return1
	CMP.W	#3,CurrScreen	; Screen 3 is DirScreen...
	BEQ	CheckDirGadgets
	CMP.W	#5,CurrScreen
	BEQ	CheckSetupGadgs
	CMP.W	#7,CurrScreen
	BEQ	CheckSetup2Gadgs
	CMP.W	#45,D1
	BHS.S	cgskip3
	CMP.W	#306,D0
	BHS	CheckToggle
	CMP.W	#244,D0
	BHS.S	MainMenu3
	CMP.W	#181,D0
	BHS.S	MainMenu2
	CMP.W	#120,D0
	BHS	MainMenu1
cgskip3	CMP.W	#120,D0
	BHS.S	cgskip4
	CMP.W	#109,D0
	BHS	DownGadgets
	CMP.W	#98,D0
	BHS	UpGadgets
	CMP.W	#62,D0
	BHS	EnterNumGadg
	BRA	PosInsDelGadgs
cgskip4	TST.B	EdEnable
	BNE	CheckEditOpGadgs
	BRA	ToggleAnaScope

MainMenu3
	MOVE.W	MouseY2,D0
	CMP.W	#34,D0
	BHS	DPMGFIH
	CMP.W	#23,D0
	BHS	Setup
	CMP.W	#12,D0
	BHS	PresetEditor
	TST.W	D0
	BHS	PLST
	RTS	

DPMGFIH	JMP	SamplerScreen

MainMenu2
	MOVE.W	MouseY2,D0
	CMP.W	#3,CurrScreen
	BNE	mm2skip
	CMP.W	#44,D0
	BHS	CheckDirGadgets2
mm2skip	CMP.W	#44,D0
	BHS	Return1
	CMP.W	#34,D0
	BHS	ShowDirScreen
	CMP.W	#23,D0
	BHS	DoEditOp
	CMP.W	#12,D0
	BHS	ClearAll
	TST.W	D0
	BHS	StopIt
	RTS	
 
MainMenu1
	MOVE.W	MouseY2,D0
	CMP.W	#3,CurrScreen
	BNE.S	mm1skip
	CMP.W	#44,D0
	BHS	CheckDirGadgets2
mm1skip	CMP.W	#44,D0
	BHS	Return1
	CMP.W	#34,D0
	BHS	RecordPattern
	CMP.W	#23,D0
	BHS	Edit
	CMP.W	#12,D0
	BHS	PlayPattern
	TST.W	D0
	BHS	PlaySong
	RTS


;---- Disk Format ----

DiskFormatGadg
	BSR	WaitForButtonUp
	BSR	Wait_4000
	BSR	ClearFileNames
	BSR	SwapFormatBox
	BSR	WaitForButtonUp
	LEA	AreYouSureText,A0
	BSR	ShowStatusText
fmtbuttonchk
	BSR	DoKeyBuffer
	MOVE.B	RawKeyCode,D0
	CMP.B	#69,D0 ; ESC
	BEQ.S	fmtbend
	BTST	#6,$BFE001
	BNE.S	fmtbuttonchk
	MOVEM.W	MouseX,D0/D1
	CMP.W	#89,D0
	BLO.S	fmtbuttonchk
	CMP.W	#212,D0
	BHI.S	fmtbuttonchk
	CMP.W	#72,D1
	BLO.S	fmtbuttonchk
	CMP.W	#82,D1
	BHI.S	fmtbuttonchk
	CMP.W	#136,D0
	BLO.S	DiskFormat
	CMP.W	#166,D0
	BLO.S	fmtbuttonchk
fmtbend	BSR.S	SwapFormatBox
	BRA	SetErrorPtrCol

SwapFormatBox
	LEA	FormatBoxData,A1
	LEA	FormatBoxPos,A0
	MOVEQ	#38,D0
sfbloop1
	MOVEQ	#17,D1
sfbloop2
	MOVE.B	10240(A0),D2
	MOVE.B	702(A1),10240(A0)
	MOVE.B	D2,702(A1)
	MOVE.B	(A0),D2
	MOVE.B	(A1),(A0)+
	MOVE.B	D2,(A1)+
	DBRA	D1,sfbloop2
	LEA	$0016(A0),A0
	DBRA	D0,sfbloop1
	RTS

DiskFormat
	BSR	SwapFormatBox
	BSR	ShowAllRight
	BSR	ClearFileNames
	LEA	PlsEntNamText,A0
	MOVE.W	#2570,D1
	MOVEQ	#17,D0
	BSR	ShowText3
	LEA	OfVolumeText,A0
	MOVE.B	#'_',15(A0)
	MOVE.B	#'_',16(A0)
	MOVE.W	#2810,D1
	MOVEQ	#17,D0
	BSR	ShowText3
	BSR	SetWaitPtrCol
	LEA	DiskNumText1,A6
	MOVEQ	#1,D0
df_loop	CLR.B	0(A6,D0.W)
	DBRA	D0,df_loop
	MOVE.L	A6,TextEndPtr
	MOVE.L	A6,ShowTextPtr
	ADDQ.L	#2,TextEndPtr
	MOVE.B	#1,EnterTextFlag
	MOVE.W	#2825,A4
	MOVE.W	#2,TextLength
	BSR	GetTextLine
	CLR.B	EnterTextFlag
	CLR.L	TextEndPtr
	BSR	ClearFileNames
	MOVE.L	4.W,A6
	MOVE.L	#5632,D0
	MOVEQ	#MEMF_CHIP,D1
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,FormatDataPtr
	BEQ	df_memerr
	BSR	DoShowFreeMem
	SUB.L	A1,A1
	JSR	LVOFindTask(A6)
	MOVE.L	D0,ProcessPtr
	LEA	TrackdiskPort,A1
	JSR	LVOAddPort(A6)
	LEA	TrackdiskIOExtTD,A1
	MOVEQ	#0,D0
	MOVEQ	#0,D1
	LEA	TrackdiskName,A0
	JSR	LVOOpenDevice(A6)
	MOVE.L	#TrackdiskPort,TDPortPtr
	CLR.W	CylinderNumber
	LEA	TrackdiskIOExtTD,A1
	MOVE.W	#TD_CHANGESTATE,IO_COMMAND(A1)
	JSR	LVODoIO(A6)	; Check if disk in drive
	TST.L	IO_ACTUAL(A1)
	BEQ.S	df_indrive
	LEA	NoDiskInDriveText,A0
	BSR	ShowStatusText
	MOVE.W	#50,WaitTime
	BSR	SetErrorPtrCol
	BRA	df_cleanup

df_indrive
	LEA	TrackdiskIOExtTD,A1
	MOVE.W	#TD_PROTSTATUS,IO_COMMAND(A1)
	JSR	LVODoIO(A6)	; Check write protect
	TST.L	IO_ACTUAL(A1)
	BEQ.S	df_noprot
	LEA	WriteProtectedText,A0
	BSR	ShowStatusText
	MOVE.W	#50,WaitTime
	BSR	SetErrorPtrCol
	BRA	df_cleanup

df_noprot
	LEA	DiskFormatText,A0
	MOVE.W	#2581-7,D1
	MOVEQ	#11,D0
	BSR	ShowText3
	LEA	InProgressText,A0
	MOVE.W	#2821-7,D1
	MOVEQ	#11,D0
	BSR	ShowText3
	CLR.L	FmtDiskOffset
	MOVE.L	FormatDataPtr,FmtDataPtr
	MOVE.L	#$00001600,FmtDataSize
	BSR	ClearCylinder
	BSR	SetDiskPtrCol
df_formatloop
	LEA	FormattingCylText,A0
	BSR	ShowStatusText
	MOVE.W	CylinderNumber,D0
	LSR.W	#1,D0
	MOVE.W	D0,WordNumber
	BSR	Print2DecDigits
	MOVE.L	4.W,A6
	LEA	TrackdiskIOExtTD,A1
	MOVE.W	#TD_FORMAT,IO_COMMAND(A1)
	JSR	LVODoIO(A6)	; Format cylinder
	LEA	VerifyingText,A0
	BSR	ShowStatusText
	MOVE.W	CylinderNumber,D0
	LSR.W	#1,D0
	MOVE.W	D0,WordNumber
	BSR	Print2DecDigits
	MOVE.L	4.W,A6
	LEA	TrackdiskIOExtTD,A1
	MOVE.W	#CMD_READ,IO_COMMAND(A1)
	JSR	LVODoIO(A6)	; Read cylinder to verify
	TST.B	Fmt_hmmm
	BEQ.S	df_verifyok
	LEA	VerifyErrText(PC),A0
	BSR	ShowStatusText
	MOVE.W	#50,WaitTime
	BSR	SetErrorPtrCol
	BRA	df_cleanup

VerifyErrText	dc.b "Verify error!",0

df_verifyok
	ADD.L	#$1600,FmtDiskOffset
	ADDQ.W	#1,CylinderNumber
	CMP.W	#160,CylinderNumber
	BNE	df_formatloop

	LEA	InitDiskText,A0
	BSR	ShowStatusText
	BSR	ClearCylinder
	MOVE.L	FormatDataPtr,A2
	LEA	BootblockData(PC),A0
	MOVEQ	#12,D0
.loop	MOVE.L	(A0)+,(A2)+
	DBRA	D0,.loop
	LEA	TrackdiskIOExtTD,A1
	CLR.L	IO_OFFSET(A1)
	MOVE.L	#512,IO_LENGTH(A1)
	MOVE.W	#CMD_WRITE,IO_COMMAND(A1)
	MOVE.L	4.W,A6
	JSR	LVODoIO(A6)	; Write bootblock
	MOVE.L	#DateStamp,D1
	MOVE.L	DOSbase,A6
	JSR	LVODateStamp(A6)
	MOVE.L	4.W,A6
	BSR	ClearCylinder
	MOVE.L	FormatDataPtr,A2
	MOVE.W	#2,2(A2)
	MOVE.W	#$0048,14(A2)
	MOVE.L	#$FFFFFFFF,$0138(A2)
	MOVE.W	#$0371,$013E(A2)
	MOVE.L	#$0553542D,$01B0(A2)
	MOVE.W	DiskNumText1,$01B4(A2)
	MOVE.W	#$06F7,$01E2(A2)
	MOVE.W	#1,$01FE(A2)
	LEA	DateStamp,A0
	MOVE.L	(A0),D0
	MOVE.L	D0,$01A4(A2)
	MOVE.L	D0,$01E4(A2)
	MOVE.L	4(A0),D0
	MOVE.L	D0,$01A8(A2)
	MOVE.L	D0,$01E8(A2)
	MOVE.L	8(A0),D0
	MOVE.L	D0,$01AC(A2)
	MOVE.L	D0,$01EC(A2)
	MOVE.L	A2,A3
	MOVE.L	A3,A4
	MOVEQ	#$7F,D1
	MOVEQ	#0,D0
	MOVE.L	D0,$0014(A4)
df_loop2
	SUB.L	(A3)+,D0
	DBRA	D1,df_loop2
	MOVE.L	D0,$0014(A4)
	MOVE.L	#$C000C037,$0200(A2)
	LEA	$0204(A2),A3
	MOVEQ	#$36,D0
df_loop3
	MOVE.L	#$FFFFFFFF,(A3)+
	DBRA	D0,df_loop3
	MOVE.W	#$3FFF,$0272(A2)
	MOVE.W	#$3FFF,$02DC(A2)
	LEA	TrackdiskIOExtTD,A1
	MOVE.L	#$0006E000,IO_OFFSET(A1)
	MOVE.L	#1024,IO_LENGTH(A1)
	MOVE.W	#CMD_WRITE,IO_COMMAND(A1)
	JSR	LVODoIO(A6)
	LEA	TrackdiskIOExtTD,A1
	MOVE.W	#CMD_UPDATE,IO_COMMAND(A1)
	JSR	LVODoIO(A6)	; Flush write buffer
df_cleanup
	LEA	TrackdiskIOExtTD,A1
	MOVE.W	#TD_MOTOR,IO_COMMAND(A1)
	CLR.L	IO_LENGTH(A1)
	JSR	LVODoIO(A6)	; Turn off the motor
	MOVE.L	Fmt_tja,A0
	MOVE.L	$0024(A0),A0
	OR.B	#2,$0040(A0)
	LEA	TrackdiskIOExtTD,A1
	MOVE.L	4.W,A6
	JSR	LVOCloseDevice(A6)
	LEA	TrackdiskPort,A1
	JSR	LVORemPort(A6)
	MOVE.L	FormatDataPtr,A1
	MOVE.L	#5632,D0
	JSR	LVOFreeMem(A6)
	CMP.W	#160,CylinderNumber
	BNE.S	df_skip2
	CMP.W	#'00',DiskNumText1
	BNE.S	df_skip2

	MOVE.L	DOSbase,A6
	MOVE.L	#150,D1	; Wait 3 sec
	JSR	LVODelay(A6)

	MOVE.L	#LockNameDF0,D1
	MOVE.L	#-2,D2
	JSR	LVOLock(A6)
	MOVE.L	D0,D6
	BEQ	df_skip3
	MOVE.L	D0,D1
	JSR	LVOCurrentDir(A6)
	MOVE.L	D0,D7
	MOVE.L	D6,D1
	JSR	LVOUnLock(A6)

	MOVE.L	#FmtSongDirName,D1
	JSR	LVOCreateDir(A6)
	MOVE.L	D0,D1
	BEQ.S	.err1
	JSR	LVOUnLock(A6)
.err1	MOVE.L	#FmtModuleDirName,D1
	JSR	LVOCreateDir(A6)
	MOVE.L	D0,D1
	BEQ.S	df_skip2
	JSR	LVOUnLock(A6)
df_skip2
	MOVE.L	D7,D1
	JSR	LVOCurrentDir(A6)
df_skip3
	BSR	SetNormalPtrCol
	BSR	ShowAllRight
	BSR	ClearFileNames
	MOVE.W	#1,UpdateFreeMem
	RTS	

df_memerr
	BSR	OutOfMemErr
	BRA.S	df_skip2

ClearCylinder
	MOVE.L	FormatDataPtr,A0
	MOVE.W	#1407,D0
	MOVEQ	#0,D1
ccloop	MOVE.L	D1,(A0)+
	DBRA	D0,ccloop
	RTS	

BootblockData
	dc.l	$444F5300,$c0200f19,$00000370,$43fa0018
	dc.l	$4eaeffa0,$4a80670a,$20402068,$00167000
	dc.l	$4e7570ff,$60fa646f,$732e6c69,$62726172
	dc.l	$79000000

LockNameDF0	dc.b	"df0:",0
	even


;---- Clear All Data ----

ClearAll
	ADD.L	#1,LaHeTx
	MOVE.B	DisableAnalyzer,SaveDA
	MOVE.B	ScopeEnable,SaveScope
	SF	ScopeEnable
	LEA	PleaseSelectText,A0
	BSR	ShowStatusText
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	BSR	Wait_4000
	CMP	#1,CurrScreen
	BNE.S	clbskip
	TST.B	DisableAnalyzer
	BNE.S	clbskip
	ST 	DisableAnalyzer
	BSR	ClearAnaHeights
	BSR	ClearRightArea
clbskip	LEA	ClearBoxData,A1
	BSR	SwapBoxMem
	BSR	WaitForButtonUp
	BSR	Wait_4000
clearbuttoncheck
	BTST	#2,$DFF016
	BEQ	ClrCancel
	BSR	DoKeyBuffer
	MOVE.B	RawKeyCode,D0
	CMP.B	#33,D0 ; Pressed S
	BEQ	ClrSamples
	CMP.B	#24,D0 ; Pressed O
	BEQ	ClrSong
	CMP.B	#32,D0 ; Pressed A
	BEQ	ClrAll
	CMP.B	#51,D0 ; Pressed C
	BEQ	ClrCancel
	CMP.B	#69,D0 ; Pressed Esc
	BEQ	ClrCancel
	BTST	#6,$BFE001	; Left Mousebutton
	BNE.S	clearbuttoncheck
	MOVEM.W	MouseX,D0/D1
	CMP.W	#166,D0
	BLO.S	clearbuttoncheck
	CMP.W	#257,D0
	BHI.S	clearbuttoncheck
	CMP.W	#58,D1
	BLO.S	clearbuttoncheck
	CMP.W	#84,D1
	BHI.S	clearbuttoncheck
	CMP.W	#204,D0
	BHS.S	samporcancel
	CMP.W	#198,D0
	BLS.S	songorall
	BRA.S	clearbuttoncheck

songorall
	CMP.W	#74,D1
	BHS.S	ClrAll
	CMP.W	#68,D1
	BLS	ClrSong
	BRA	clearbuttoncheck

samporcancel
	CMP.W	#74,D1
	BHS.S	ClrCancel
	CMP.W	#68,D1
	BLS.S	ClrSamples
	BRA	clearbuttoncheck

RemoveClearBox
	LEA	ClearBoxData,A1
	BSR	SwapBoxMem
	BSR	ShowAllRight
	BSR	ClearAnaHeights
	MOVE.B	SaveDA,DisableAnalyzer
	MOVE.B	SaveScope,ScopeEnable
	BSR	RestorePtrCol
	CLR.B	RawKeyCode
	RTS

ClrCancel
	BSR.S	RemoveClearBox
	BRA	SetErrorPtrCol

ClrAll	BSR	DoClearSong
	BSR	ClrSampleInfo
	BSR	SetNormalPtrCol
	BSR.S	RemoveClearBox
	BRA	DisplayMainAll

ClrSamples
	BSR.S	RemoveClearBox
	BSR	StopIt
	BSR	ClrSampleInfo
	BSR	SetNormalPtrCol
	SF	EdEnable
	MOVE.W	#1,InsNum
	BRA	DisplayMainAll

ClrSong	BSR.S	RemoveClearBox
	BSR	DoClearSong
	BSR	SetNormalPtrCol
	BRA	DisplayMainAll

ClrSampleInfo
	BSR	GiveBackInstrMem
	MOVE.L	SongDataPtr,A0
	LEA	sd_sampleinfo(A0),A0
	MOVE.W	#31*30-1,D0
csiloop	CLR.B	(A0)+
	DBRA	D0,csiloop
	MOVE.L	SongDataPtr,A1
	LEA	sd_sampleinfo(A1),A1
	MOVEQ	#30,D0
caloop2	MOVE.W	#1,28(A1)
	LEA	30(A1),A1
	DBRA	D0,caloop2
	LEA	SampleLengthAdd(PC),A3
	MOVEQ	#31,D0
csilop2	CLR.W	(A3)+
	DBRA	D0,csilop2
	JMP	RedrawSample

DoClearSong
	BSR	StopIt
	CLR.B	RawKeyCode
	MOVE.L	SongDataPtr,A0
	MOVE.L	A0,A1
	MOVEQ	#19,D0
docla2x	CLR.B	(A0)+
	DBRA	D0,docla2x
	MOVE.L	A1,A2
	ADD.L	#70716,A2
	ADD.L	#950,A1
	MOVEQ	#0,D0
caloop	MOVE.W	D0,(A1)+
	CMP.L	A1,A2
	BNE.S	caloop
	MOVE.L	SongDataPtr,A0
	MOVE.W	#$017F,sd_numofpatt(A0)
	MOVE.L	#'M.K.',sd_mahokakt(A0)  ; M.K. all the way...
	SF	EdEnable
	CLR.L	CurrPos
	CLR.L	SongPosition
	CLR.L	PatternPosition
	CLR.L	PatternPtr
	CLR.L	TimerTicks
	CLR.W	BlockMarkFlag
	CLR.B	MetroChannel
	MOVE.W	#1,EditMoveAdd
	MOVEQ	#0,D0
	MOVE.W	DefaultSpeed,D0
	MOVE.L	D0,CurrSpeed
	MOVE.W	Tempo,RealTempo
	BSR	SetTempo
	BSR	RestoreEffects2
	BSR	RestoreFKeyPos2
	BSR	UnmuteAll
	MOVE.W	#1,InsNum
	MOVE.L	#6,CurrSpeed
	CLR.L	PatternNumber
	CLR.W	ScrPattPos
	BSR	SetScrPatternPos
	BRA	RedrawPattern

UnmuteAll
	MOVE.W	#1,audchan1toggle
	MOVE.W	#1,audchan2toggle
	MOVE.W	#1,audchan3toggle
	MOVE.W	#1,audchan4toggle
	MOVE.W	#$000F,ActiveChannels
	BRA	RedrawToggles

ToggleMute
	CLR.B	RawKeyCode
	TST.W	ShiftKeyStatus
	BEQ	tomuskp
	CLR.W	audchan1toggle
	CLR.W	audchan2toggle
	CLR.W	audchan3toggle
	CLR.W	audchan4toggle
tomuskp	MOVEQ	#0,D0
	MOVE.W	PattCurPos,D0
	DIVU	#6,D0
	MULU	#11,D0
	ADDQ.W	#4,D0
	BRA	DoToggleMute

RestoreEffects
	MOVEQ	#0,D0
	MOVE.W	DefaultSpeed,D0
	MOVE.L	D0,CurrSpeed
	MOVE.W	Tempo,RealTempo
	BSR	SetTempo
	BSR.S	RestoreEffects2
	CLR.B	RawKeyCode
	LEA	EfxRestoredText(PC),A0
	BSR	ShowStatusText
	BSR	WaitALittle
	BRA	ShowAllRight

RestoreEffects2
	LEA	audchan1temp,A0
	BSR.S	reefsub
	LEA	audchan2temp,A0
	BSR.S	reefsub
	LEA	audchan3temp,A0
	BSR.S	reefsub
	LEA	audchan4temp,A0
reefsub	CLR.B	n_wavecontrol(A0)
	CLR.B	n_glissfunk(A0)
	CLR.B	n_finetune(A0)
	CLR.B	n_loopcount(A0)
	RTS

RestoreFKeyPos
	CLR.B	RawKeyCode
	LEA	PosRestoredText(PC),A0
	BSR	ShowStatusText
	BSR	WaitALittle
	BSR	ShowAllRight
RestoreFKeyPos2
	MOVE.W	#00,F6pos
	MOVE.W	#16,F7pos
	MOVE.W	#32,F8pos
	MOVE.W	#48,F9pos
	MOVE.W	#63,F10pos
	RTS

EfxRestoredText	dc.b 'efx restored !',0
PosRestoredText	dc.b 'pos restored !',0


GiveBackInstrMem
	MOVEQ	#1,D7
	MOVE.L	4.W,A6
gbimloop
	MOVE.W	D7,D2
	LSL.W	#2,D2
	LEA	SamplePtrs,A0
	MOVE.L	0(A0,D2.W),D1
	BEQ.S	gbimskip
	MOVE.L	124(A0,D2.W),D0
	CLR.L	0(A0,D2.W)
	CLR.L	124(A0,D2.W)
	MOVE.L	D1,A1
	JSR	LVOFreeMem(A6)
gbimskip
	ADDQ.W	#1,D7
	CMP.W	#32,D7
	BNE.S	gbimloop
	MOVE.W	#1,UpdateFreeMem
	JMP	FreeCopyBuf

;---- Setup ----

Setup	CMP.W	#7,LastSetupScreen
	BEQ	Setup2
	CLR.W	LastSetupScreen
	BSR	WaitForButtonUp
	MOVE.W	#5,CurrScreen
	ST	DisableAnalyzer
	ST	NoSampleInfo
	BSR	Clear100Lines
	BSR	ShowSetupScreen
	BEQ	ExitSetup
RefreshSetup
	BSR	SetScreenColors
	BSR	Show_MS
	CMP.W	#5,CurrScreen
	BNE	Return1
	BSR	MarkColor
	BSR	ShowSetupToggles
	BSR	ShowSplit
	BSR	ShowPrintPath
	BSR	ShowKeyRepeat
	BSR	ShowExtCommand
	BSR	ShowMultiSetup
	BSR	ShowConfigNumber
	BRA	ShowAccidental ; Always last (redraws pattern) !

ShowSetupScreen
	MOVE.L	SetupMemPtr,D0
	BNE	DecompactSetup
	BSR.S	SaveMainPic
	TST.L	SetupMemPtr
	BEQ	Return1
	BRA	DecompactSetup

SaveMainPic
	MOVE.L	#8000,D0
	MOVEQ	#0,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,SetupMemPtr
	BEQ	OutOfMemErr
	MOVE.L	D0,A1
	LEA	BitplaneData,A0
	MOVEQ	#1,D2
sssloop1
	MOVE.W	#999,D0
sssloop2
	MOVE.L	(A0)+,(A1)+
	DBRA	D0,sssloop2
	LEA	6240(A0),A0
	DBRA	D2,sssloop1
	RTS	

RestoreMainPic
	MOVE.L	SetupMemPtr,D0
	BEQ	Return1
	MOVE.L	D0,A1
	LEA	BitplaneData,A0
	MOVEQ	#1,D2
ssxloop1
	MOVE.W	#999,D0
ssxloop2
	MOVE.L	(A1)+,(A0)+
	DBRA	D0,ssxloop2
	LEA	6240(A0),A0
	DBRA	D2,ssxloop1
	MOVE.L	SetupMemPtr,A1
	CLR.L	SetupMemPtr
	MOVE.L	#8000,D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	RTS	

SetupMemPtr	dc.l 0

DecompactSetup2
	LEA	Setup2Data+4,A0
	MOVE.L	#Setup2Size-4-8,D0
	BRA.S	decoset

DecompactSetup
	LEA	SetupScreenData+4,A0
	MOVE.L	#SetupScreenSize-4-8,D0
decoset	LEA	BitplaneData,A1
	MOVE.L	A1,A2
	LEA	4000(A2),A2
dcsloop	MOVE.B	(A0)+,D1
	CMP.B	#181,D1
	BEQ.S	DecodeIt2
	MOVE.B	D1,(A1)+
	CMP.L	A2,A1
	BLO.S	dcslop2
	LEA	6240(A1),A1
	MOVE.L	#$FFFFFFFF,A2
dcslop2	SUBQ.L	#1,D0
	CMP.L	#0,D0
	BGT.S	dcsloop
	MOVEQ	#-1,D0
	RTS

DecodeIt2
	MOVEQ	#0,D1
	MOVE.B	(A0)+,D1
	MOVE.B	(A0)+,D2
dcdlop2	MOVE.B	D2,(A1)+
	CMP.L	A2,A1
	BLO.S	dcdskp2
	LEA	6240(A1),A1
	MOVE.L	#$FFFFFFFF,A2
dcdskp2	DBRA	D1,dcdlop2
	SUBQ.L	#2,D0
	BRA.S	dcslop2

; Gadgets

CheckSetupGadgs
	TST.L	SplitAddress
	BEQ.S	csgskip
	CLR.L	SplitAddress
	BSR	ShowSplit
csgskip	MOVEM.W	MouseX2,D0/D1
	CMP.W	#210,D0
	BHS	SetupMenu3
	CMP.W	#108,D0
	BHS.S	SetupMenu2
;---- Menu 1 ----
	CMP.W	#11,D1
	BLS	LoadConfig
	CMP.W	#22,D1
	BLS	SaveConfig
	CMP.W	#33,D1
	BLS	ResetAll
	CMP.W	#44,D1
	BLS	ExtCommand
	CMP.W	#55,D1
	BLS	MultiSetup
	CMP.W	#66,D1
	BLS	SetRed
	CMP.W	#77,D1
	BLS	SetGreen
	CMP.W	#88,D1
	BLS	SetBlue
	CMP.W	#99,D1
	BLS	ColorGadgets
	RTS

SetupMenu2
	CMP.W	#11,D1
	BLS	Return1
	CMP.W	#55,D1
	BLS	SetSplit
	CMP.W	#66,D1
	BLS	SetKeyRepeat
	CMP.W	#77,D1
	BLS	ToggleAccidental
	CMP.W	#88,D1
	BLS	PrintSong
	CMP.W	#99,D1
	BLS	EnterPrintPath
	RTS

SetupMenu3
	CMP.W	#11,D1
	BLS.S	ExitOrClear
	CMP.W	#22,D1
	BLS	ToggleSplit
	CMP.W	#33,D1
	BLS	ToggleFilter
	CMP.W	#44,D1
	BLS	ToggleTransDel
	CMP.W	#55,D1
	BLS	ToggleShowDec
	CMP.W	#66,D1
	BLS	ToggleAutoDir
	CMP.W	#77,D1
	BLS	ToggleAutoExit
	CMP.W	#88,D1
	BLS	ToggleModOnly
	CMP.W	#99,D1
	BLS	ToggleMIDI
	RTS

ExitOrClear
	CMP.W	#265,D0
	BLS	ClearSplit
	CMP.W	#306,D0
	BHS	Setup2
ExitSetup
	BSR	WaitForButtonUp
	MOVE.W	CurrScreen,LastSetupScreen
	CLR.B	RawKeyCode
	CLR.L	SplitAddress
	BSR	RestoreMainPic
	SF	NoSampleInfo
	BSR	SetupVUCols
	BSR	SetupAnaCols
	BSR	Clear100Lines
	BRA	DisplayMainAll

LastSetupScreen	dc.w 0

ToggleSplit
	EOR.B	#1,SplitFlag
	BRA	ShowSetupToggles

ToggleFilter
	BCHG	#1,$BFE001
	BRA	ShowSetupToggles

ToggleTransDel
	EOR.B	#1,TransDelFlag
	BRA.S	ShowSetupToggles

ToggleShowDec
	EOR.B	#1,ShowDecFlag
	MOVE.W	#1,UpdateFreeMem
	BRA.S	ShowSetupToggles

ToggleAutoDir
	EOR.B	#1,AutoDirFlag
	BRA.S	ShowSetupToggles

ToggleAutoExit
	EOR.B	#1,AutoExitFlag
	BRA.S	ShowSetupToggles

ToggleModOnly
	EOR.B	#1,ModOnlyFlag
	LEA	FileNamesPtr(PC),A0
	CLR.L	4(A0)
	BRA.S	ShowSetupToggles

ToggleMIDI
	EOR.B	#1,MIDIFlag
	BSR.S	ShowSetupToggles
tstmidi	TST.B	MIDIFlag
	BNE	OpenMIDI
	BRA	CloseMIDI

ClearSplit
	BSR	WaitForButtonUp
	LEA	ClearSplitText,A0
	BSR	AreYouSure
	BNE	Return1
	LEA	SplitData,A0
	MOVEQ	#15,D0
clsploop
	CLR.B	(A0)+
	DBRA	D0,clsploop
	BRA	ShowSplit

ShowSetupToggles
	CLR.B	RawKeyCode
	BSR	Show_MS
	CMP.W	#5,CurrScreen
	BNE	Return1
	MOVE.B	$BFE001,D0
	LSR.B	#1,D0
	AND.B	#1,D0
	EOR.B	#1,D0
	MOVE.B	D0,FilterFlag
	LEA	SplitFlag,A4
	MOVE.W	#3,TextLength
	MOVEQ	#7,D7
	MOVE.W	#636,D6
sstloop
	MOVE.W	D6,TextOffset
	LEA	ToggleOFFText(PC),A0
	TST.B	(A4)+
	BEQ.S	sstskip
	LEA	ToggleONText(PC),A0
sstskip
	BSR	ShowText2
	ADD.W	#440,D6
	DBRA	D7,sstloop
	BRA	WaitForButtonUp

ToggleONText2	dc.b ' '
ToggleONText	dc.b 'on '
ToggleOFFText	dc.b 'off '

ShowSplit
	BSR	CalculateSplit
	CMP.W	#5,CurrScreen
	BNE	Return1
	LEA	SplitData,A3
	MOVE.L	NoteNamesPtr,A4
	MOVEQ	#0,D5
	MOVE.W	#614,D6
	CLR.W	WordNumber
shsploop
	MOVE.W	D6,TextOffset
	MOVE.B	(A3,D5.W),WordNumber+1 ; instr
	BSR	PrintHexByte
	ADDQ.W	#4,D6
	MOVE.W	D6,D1
	MOVEQ	#0,D0
	MOVE.B	1(A3,D5.W),D0 ; note
	LSL.W	#2,D0
	LEA	(A4,D0.W),A0
	MOVEQ	#4,D0
	BSR	ShowText3
	ADDQ.L	#5,D6
	MOVE.W	D6,D1
	MOVEQ	#0,D0
	MOVE.B	2(A3,D5.W),D0 ; trans
	LSL.W	#2,D0
	LEA	(A4,D0.W),A0
	MOVEQ	#4,D0
	BSR	ShowText3
	ADD.W	#431,D6
	ADDQ.W	#4,D5
	CMP.W	#16,D5
	BNE.S	shsploop
	RTS

CalculateSplit
	LEA	SplitTransTable,A0
	LEA	SplitInstrTable,A1
	LEA	SplitData,A2
	MOVEQ	#0,D0
casploop
	MOVE.B	D0,(A0,D0.W)
	CLR.B	(A1,D0.W)
	ADDQ.W	#1,D0
	CMP.W	#37,D0
	BLS.S	casploop

	MOVE.B	1(A2),-(SP)
	MOVE.B	1+4(A2),-(SP)
	MOVE.B	1+8(A2),-(SP)
	MOVE.B	1+12(A2),-(SP) ; note

	MOVEQ	#3,D4
caspbigloop
	MOVEQ	#127,D0
	MOVEQ	#0,D1
	MOVEQ	#0,D2
casploop2
	CMP.B	1(A2,D1.W),D0 ; note
	BLS.S	caspskip
	MOVE.L	D1,D2
	MOVE.B	1(A2,D1.W),D0 ; note
caspskip
	ADDQ.W	#4,D1
	CMP.W	#16,D1
	BNE.S	casploop2

	MOVEQ	#0,D0
	MOVE.B	1(A2,D2.W),D0 ; note
	MOVE.B	#127,1(A2,D2.W) ; note
	MOVE.B	2(A2,D2.W),D1 ; trans
casploop3
	MOVE.B	D1,(A0,D0.W)
	MOVE.B	(A2,D2.W),(A1,D0.W) ; instr
	ADDQ.W	#1,D1
	CMP.W	#36,D1
	BLO.S	caspskip2
	MOVEQ	#35,D1
caspskip2
	ADDQ.W	#1,D0
	CMP.W	#36,D0
	BLO.S	casploop3
	DBRA	D4,caspbigloop
	
	MOVE.B	(SP)+,1+12(A2)
	MOVE.B	(SP)+,1+8(A2)
	MOVE.B	(SP)+,1+4(A2)
	MOVE.B	(SP)+,1(A2) ; note
	RTS

SetSplit
	LEA	SplitData,A2
	SUBQ.W	#1,D1
	DIVU	#11,D1
	SUBQ.W	#1,D1
	MOVE.W	D1,D7
	LSL.W	#2,D7
	MULU	#440,D1
	ADD.W	#600,D1
	CMP.W	#176,D0
	BHS.S	SetSplitTranspose
	CMP.W	#136,D0
	BHS.S	SetSplitNote
	ADD.W	#14,D1
	MOVE.W	D1,TextOffset
	BSR	GetHexByte
	CMP.B	#$1F,D0
	BLS.S	setskip
	MOVE.B	#$1F,D0
setskip	MOVE.B	D0,(A2,D7.W) ; instr
	BRA	ShowSplit

SetSplitNote
	MOVE.L	D1,-(SP)
	BSR	ShowSplit
	MOVE.L	(SP)+,D1
	ADD.W	#18,D1
	MOVEQ	#3,D0
	LEA	SpcNoteText,A0
	BSR	ShowText3
	LEA	1(A2,D7.W),A0 ; note
	MOVE.L	A0,SplitAddress
	BRA	WaitForButtonUp

SetSplitTranspose
	MOVE.L	D1,-(SP)
	BSR	ShowSplit
	MOVE.L	(SP)+,D1
	ADD.W	#23,D1
	MOVEQ	#3,D0
	LEA	SpcNoteText,A0
	BSR	ShowText3
	LEA	2(A2,D7.W),A0 ; trans
	MOVE.L	A0,SplitAddress
	BRA	WaitForButtonUp

SetKeyRepeat
	CMP.W	#188,D0
	BHS.S	skrep2
	MOVE.W	#2381,TextOffset
	BSR	GetHexByte
	MOVE.B	D0,KeyRepDelay+1
	BRA.S	ShowKeyRepeat
skrep2	MOVE.W	#2384,TextOffset
	BSR	GetHexByte
	MOVE.B	D0,KeyRepSpeed+1
ShowKeyRepeat
	MOVE.W	#2381,TextOffset
	MOVE.W	KeyRepDelay,WordNumber
	BSR	PrintHexByte
	ADDQ.W	#1,TextOffset
	MOVE.W	KeyRepSpeed,WordNumber
	BRA	PrintHexByte

ToggleAccidental
	MOVE.L	#NoteNames1,NoteNamesPtr
	EOR.B	#1,Accidental
	BEQ.S	ShowAccidental
	MOVE.L	#NoteNames2,NoteNamesPtr
ShowAccidental
	LEA	AccidentalText(PC),A0
	TST.B	Accidental
	BEQ.S	shacskp
	LEA	AccidentalText+1(PC),A0
shacskp	MOVEQ	#1,D0
	MOVE.W	#2824,D1
	BSR	ShowText3
	BRA	RedrawPattern

AccidentalText	dc.b '#Ą'

LoadConfig
	CMP.W	#84,D0
	BHS	SetConfigNumber
	LEA	LoadConfigText,A0
	BSR	AreYouSure
	BNE	Return1
	BSR	WaitForButtonUp
	LEA	LoadingCfgText(PC),A0
	BSR	ShowStatusText
DoLoadConfig
	BSR	StorePtrCol
	BSR	PutConfigNumber
	MOVE.L	#Configname,D1
	MOVE.L	#1005,D2
	MOVE.L	DOSbase,A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,D7
	BNE.S	cfgopok
	LEA	PTPath,A0
	BSR	CopyPath
	LEA	ConfigName(PC),A0
	MOVEQ	#12,D0
cfgnlop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,cfgnlop
	MOVE.L	#FileName,D1
	MOVE.L	#1005,D2
	MOVE.L	DOSbase,A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,D7
	BEQ.S	ConfigErr
cfgopok	BSR	SetDiskPtrCol
	MOVE.L	D7,D1
	CLR.L	ConfigID
	MOVE.L	#ConfigID,D2
	MOVE.L	#4,D3
	JSR	LVORead(A6)
	CMP.L	#"PT1.",ConfigID
	BNE.S	cfgerr2
	LEA	SetupData+4,A0
	MOVE.L	D7,D1
	MOVE.L	A0,D2
	MOVE.L	#ConfigFileSize-4,D3
	JSR	LVORead(A6)
lcfgend	MOVE.L	D7,D1
	JSR	LVOClose(A6)
	BSR	ShowAllRight
	BSR	RestorePtrCol
cfgupda	BSR	CopyCfgData
	BSR	tstmidi
	BSR	ChangeCopList
	BRA	RefreshSetup

ConfigErr
	BSET	#2,InitError
	LEA	FileNotFoundText(PC),A0
cferr	BSR	ShowStatusText
	MOVE.W	#50,WaitTime
	BRA	ErrorRestoreCol

cfgerr2	BSR.S	ConfigErr2
	BRA.S	lcfgend

ConfigErr2
	BSET	#3,InitError
	LEA	NotAConfFileText(PC),A0
	BRA.S	cferr

ConfigErr3
	LEA	CantCreateFiText(PC),A0
	BRA.S	cferr

FileNotFoundText dc.b "config not found!",0
NotAConfFileText dc.b "not a config file",0
CantCreateFiText dc.b "can't create file",0

SaveConfig
	CMP.W	#84,D0
	BHS	SetConfigNumber
	LEA	SaveConfigText,A0
	BSR	AreYouSure
	BNE	Return1
	BSR	StorePtrCol
	BSR	SetDiskPtrCol
	BSR	PutConfigNumber
	LEA	SavingCfgText(PC),A0
	BSR	ShowStatusText
	MOVE.L	DOSbase,A6
	LEA	PTPath,A0
	BSR	CopyPath
	LEA	ConfigName(PC),A0
	MOVEQ	#12,D0
cfgllop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,cfgllop
	MOVE.L	#FileName,D1
	MOVE.L	#1006,D2
	JSR	LVOOpen(A6)
	MOVE.L	D0,D7
	BEQ	ConfigErr3
	MOVE.L	D0,D1
	MOVE.L	#SetupData,D2
	MOVE.W	#ConfigFileSize,D3
	JSR	LVOWrite(A6)
	MOVE.L	D7,D1
	JSR	LVOClose(A6)
	BSR	ShowAllRight
	BRA	RestorePtrCol

SetConfigNumber
	MOVE.W	#611,TextOffset
	BSR	GetHexByte
	MOVE.W	D0,ConfigNumber
ShowConfigNumber
	MOVE.W	#611,TextOffset
	MOVE.W	ConfigNumber(PC),WordNumber
	BRA	PrintHexByte

PutConfigNumber
	MOVE.W	ConfigNumber(PC),D0
	LEA	ConfigName+12(PC),A0
	BRA	IntToHex2

ConfigName	dc.b 'PT.config-00',0,0
ConfigNumber	dc.w 0
LoadingCfgText	dc.b 'loading config',0
SavingCfgText	dc.b 'saving config',0,0
ConfigID	dc.l 0

ResetAll
	LEA	ResetAllText,A0
	BSR	AreYouSure
	BNE	Return1
DoResetAll
	LEA	DefaultSetupData,A0
	LEA	SetupData,A1
	MOVE.W	#ConfigFileSize-1,D0
rsaloop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,rsaloop
	BRA	RefreshSetup

ExtCommand
	CMP.W	#11,D0
	BLO.S	excolab
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	LEA	ExtCommands,A6
	MOVE.W	ExtCmdNumber,D1
	MULU	#32,D1
	ADD.L	D1,A6
	MOVE.L	A6,ShowTextPtr
	MOVE.L	A6,TextEndPtr
	ADD.L	#31,TextEndPtr
	MOVE.W	#11,TextLength
	MOVE.W	#1482,A4
	BSR	GetTextLine
	BRA	RestorePtrCol

ShowExtCommand
	LEA	ExtCommands,A0
	MOVE.W	ExtCmdNumber,D1
	MULU	#32,D1
	ADD.L	D1,A0
	MOVEQ	#11,D0
	MOVE.W	#1482,D1
	BRA	ShowText3

excolab	BTST	#2,$DFF016
	BEQ.S	excorun
	ADDQ.W	#1,ExtCmdNumber
	AND.W	#7,ExtCmdNumber
	BSR.S	ShowExtCommand
	BSR	Wait_4000
	BSR	Wait_4000
	BSR	Wait_4000
	BRA	Wait_4000

excorun	LEA	ExtCommands,A0
	MOVE.W	ExtCmdNumber,D1
	MULU	#32,D1
	ADD.L	D1,A0
	TST.B	(A0)
	BEQ	Return1
	MOVE.L	A0,ExtCmdAddress
	MOVE.L	IntuitionBase,A6
	JSR	LVOOpenWorkbench(A6)
	JSR	LVOWBenchToFront(A6)
	MOVE.L	#ExtCmdConsole,D1
	MOVE.L	#1005,D2
	MOVE.L	DOSBase,A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,ExtCmdWindow
	BEQ.S	winderr
	LSL.L	#2,D0
	MOVE.L	D0,A0
	MOVE.L	PTProcess,A1
	MOVE.L	8(A0),$A4(A1)
	BSR	GotoCLI
	MOVE.L	ExtCmdWindow,D1
	BEQ	Return1
	MOVE.L	DOSBase,A6
	JSR	LVOClose(A6)
	CLR.L	ExtCmdWindow
	RTS

winderr	LEA	ConsoleErrText,A0
	BSR	ShowStatusText
	BRA	SetErrorPtrCol

ExtCmdNumber	dc.w 0
ExtCmdAddress	dc.l 0
ExtCmdWindow	dc.l 0
ExtCmdConsole	dc.b "CON:0/0/640/150/Protracker Output Window",0
ConsoleErrText	dc.b "Can't open window",0,0

MultiSetup
	CMP.W	#44,D0
	BLO	Return1
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVE.W	#1,GetLineFlag
	SUB.W	#44,D0
	LSR.W	#4,D0
	MOVE.B	D0,musepos
museset	MOVEQ	#0,D0
	MOVE.B	musepos,D0
	MOVE.L	D0,D1
	LSL.W	#4,D1
	MOVE.W	#52,LineCurX
	MOVE.W	#53,LineCurY
	ADD.W	D1,LineCurX
	LEA	MultiModeNext,A1
	LEA	(A1,D0.W),A1
	BSR	UpdateLineCurPos
muselop	BSR	GetHexKey
	TST.B	D1
	BEQ.S	musenul
	CMP.B	#4,D1
	BHI.S	muselop
	MOVE.B	D1,(A1)
	BSR	ShowMultiSetup
	MOVEQ	#1,D0
musenul	TST.B	D0
	BEQ.S	museabo
	ADD.B	D0,musepos
	AND.B	#3,musepos
	BSR	Wait_4000
	BSR	Wait_4000
	BSR	Wait_4000
	BRA.S	museset

museabo	BSR	RestorePtrCol
	CLR.W	GetLineFlag
	MOVE.W	#0,LineCurX
	MOVE.W	#270,LineCurY
	BSR	UpdateLineCurPos
ShowMultiSetup
	MOVE.W	#1926,TextOffset
	MOVE.B	MultiModeNext,D0
	BSR	PrintHexDigit
	MOVE.W	#1928,TextOffset
	MOVE.B	MultiModeNext+1,D0
	BSR	PrintHexDigit
	MOVE.W	#1930,TextOffset
	MOVE.B	MultiModeNext+2,D0
	BSR	PrintHexDigit
	MOVE.W	#1932,TextOffset
	MOVE.B	MultiModeNext+3,D0
	BRA	PrintHexDigit

musepos	dc.b 0,0

SetRed	CMP.W	#82,D0
	BHS	SelectColor
setr2	BSR	GetColPos
	AND.W	#$0F00,D2
	LSR.W	#8,D2
	CMP.B	D2,D0
	BEQ.S	setrskp
	AND.W	#$00FF,D1
	LSL.W	#8,D0
	OR.W	D0,D1
	MOVE.W	D1,(A0)
	BSR	ShowColSliders
	BSR	SetScreenColors
setrskp	BTST	#6,$BFE001
	BEQ.S	setr2
	RTS

SetGreen
	CMP.W	#82,D0
	BHS	SelectColor
setg2	BSR.S	GetColPos
	AND.W	#$00F0,D2
	LSR.W	#4,D2
	CMP.B	D2,D0
	BEQ.S	setgskp
	AND.W	#$0F0F,D1
	LSL.W	#4,D0
	OR.W	D0,D1
	MOVE.W	D1,(A0)
	BSR	ShowColSliders
	BSR	SetScreenColors
setgskp	BTST	#6,$BFE001
	BEQ.S	setg2
	RTS

SetBlue	CMP.W	#82,D0
	BHS	SelectColor
setb2	BSR.S	GetColPos
	AND.W	#$000F,D2
	CMP.B	D2,D0
	BEQ.S	setbskp
	AND.W	#$0FF0,D1
	OR.W	D0,D1
	MOVE.W	D1,(A0)
	BSR	ShowColSliders
	BSR	SetScreenColors
setbskp	BTST	#6,$BFE001
	BEQ.S	setb2
	RTS

GetColPos
	MOVEQ	#0,D0
	MOVE.W	MouseX,D0
	CMP.W	#26,D0
	BHS.S	gcpskp2
	MOVEQ	#0,D0
	BRA.S	gcpskip
gcpskp2	SUB.W	#26,D0
	DIVU	#3,D0
	AND.L	#$FF,D0
	CMP.W	#15,D0
	BLS.S	gcpskip
	MOVEQ	#15,D0
gcpskip	CMP.W	#7,CurrScreen
	BEQ	GetColAddr
	LEA	ColorTable,A0
	MOVE.W	CurrColor,D1
	ADD.W	D1,D1
	LEA	(A0,D1.W),A0
	MOVE.W	(A0),D1
	MOVE.W	D1,D2
	RTS

ShowColSliders
	LEA	ColSliders(PC),A2
	MOVE.L	TextBplPtr,A1
	ADD.L	#3282,A1
	BSR.S	gcpskip
	AND.W	#$000F,D1
	BSR.S	ShowOneSlider
	MOVE.L	TextBplPtr,A1
	ADD.L	#2842,A1
	BSR.S	gcpskip
	AND.W	#$00F0,D1
	LSR.W	#4,D1
	BSR.S	ShowOneSlider
	MOVE.L	TextBplPtr,A1
	ADD.L	#2402,A1
	BSR.S	gcpskip
	AND.W	#$0F00,D1
	LSR.W	#8,D1
ShowOneSlider
	CLR.L	(A1)
	CLR.L	4(A1)
	CLR.L	40(A1)
	CLR.L	44(A1)
	CLR.L	80(A1)
	CLR.L	84(A1)
	MOVE.W	D1,D3
	ADD.W	D3,D3
	ADD.W	D3,D3
	MOVE.W	2(A2,D3.W),D4
	MOVE.B	0(A2,D3.W),00(A1,D4.W)
	MOVE.B	1(A2,D3.W),01(A1,D4.W)
	MOVE.B	0(A2,D3.W),40(A1,D4.W)
	MOVE.B	1(A2,D3.W),41(A1,D4.W)
	MOVE.B	0(A2,D3.W),80(A1,D4.W)
	MOVE.B	1(A2,D3.W),81(A1,D4.W)
	RTS

ColSliders
	dc.w %0000000001111100,0
	dc.w %0000111110000000,1
	dc.w %0000000111110000,1
	dc.w %0000000000111110,1
	dc.w %0000011111000000,2
	dc.w %0000000011111000,2
	dc.w %0000000000011111,2
	dc.w %0000001111100000,3
	dc.w %0000000001111100,3
	dc.w %0000111110000000,4
	dc.w %0000000111110000,4
	dc.w %0000000000111110,4
	dc.w %0000011111000000,5
	dc.w %0000000011111000,5
	dc.w %0000000000011111,5
	dc.w %0000001111100000,6

SelectColor
	CMP.W	#84,D1
	BHS	Return1
	MOVE.L	TextBplPtr,A0
	LEA	2410(A0),A0
	MOVEQ	#24,D2
slcloop	CLR.L	(A0)
	LEA	40(A0),A0
	DBRA	D2,slcloop

	SUB.W	#82,D0
	DIVU	#13,D0
	SUB.W	#60,D1
	DIVU	#6,D1
	MOVE.W	D0,D2
	LSL.W	#2,D2
	ADD.W	D1,D2
	MOVE.W	D2,CurrColor
MarkColor
	BSR	gcpskip
	MOVE.L	A0,UndoColAddr
	MOVE.W	D1,UndoCol
	BSR	ShowColSliders
	BSR.S	BlockColors
	MOVE.W	CurrColor,D0
	MOVE.W	D0,D1
	AND.W	#$0003,D1
	MULU	#6*40,D1
	MOVE.L	TextBplPtr,A0
	ADD.L	D1,A0
	LEA	2410(A0),A0
	BTST	#2,D0
	BNE.S	slcskip
	MOVE.W	#$3FF0,(A0)
	MOVE.W	#$3FF0,240(A0)
	MOVEQ	#4,D0
slclop2	LEA	40(A0),A0
	MOVE.W	#$2010,(A0)
	DBRA	D0,slclop2
	BRA	WaitForButtonUp

slcskip	MOVE.L	#$0001FF80,(A0)
	MOVE.L	#$0001FF80,240(A0)
	MOVEQ	#4,D0
slclop3	LEA	40(A0),A0
	MOVE.L	#$0001FF80,(A0)
	DBRA	D0,slclop3
	BRA	WaitForButtonUp

BlockColors
	MOVE.L	TextBplPtr,A0
	LEA	2452(A0),A0
	MOVEQ	#3,D1
suploop2
	MOVEQ	#4,D0
suploop3
	MOVE.B	#$FF,(A0)
	ADD.L	#40,A0
	DBRA	D0,suploop3
	ADD.L	#40,A0
	DBRA	D1,suploop2
	RTS

ColorGadgets
	CMP.W	#79,D0
	BHS	SetDefaultCol
	CMP.W	#33,D0
	BHS.S	CancelCol
	MOVE.L	UndoColAddr,A0
	MOVE.W	UndoCol,D0
	MOVE.W	(A0),UndoCol
	MOVE.W	D0,(A0)
	BSR	ShowColSliders
	BRA	SetScreenColors

CancelCol
	LEA	CanCols(PC),A0
	LEA	ColorTable,A1
	MOVEQ	#7,D0
cacolop	MOVE.W	(A0)+,(A1)+
	DBRA	D0,cacolop
	BSR	ShowColSliders
	BRA	SetScreenColors

CopyCfgData
	LEA	ColorTable,A0
	LEA	CanCols(PC),A1
	MOVEQ	#7,D0
cocclop	MOVE.W	(A0)+,(A1)+
	DBRA	D0,cocclop
	LEA	ModulesPath2,A0
	MOVE.W	#195,D0
cocclp2	CLR.B	(A0)+
	DBRA	D0,cocclp2
	LEA	ModulesPath,A0
	LEA	ModulesPath2,A1
	BSR.S	cocclp3
	LEA	SongsPath,A0
	LEA	SongsPath2,A1
	BSR.S	cocclp3
	LEA	SamplePath,A0
	LEA	SamplePath2,A1
cocclp3	MOVE.B	(A0)+,(A1)+
	BNE.S	cocclp3
	LEA	VUMeterColors,A0
	LEA	SaveColors,A1
	MOVEQ	#40+48-1,D0
cocclp4	MOVE.W	(A0)+,(A1)+
	DBRA	D0,cocclp4
	RTS

UndoCol		dc.w 0
UndoColAddr	dc.l 0
CanCols		dc.w 0,0,0,0,0,0,0,0

SetDefaultCol
	LEA	DefCol,A0
	LEA	ColorTable,A1
	MOVEQ	#7,D0
sdcloop	MOVE.W	(A0)+,(A1)+
	DBRA	D0,sdcloop
	BSR	ShowColSliders
SetScreenColors
	BSR	SetupAnaCols
SetScreenColors2
	LEA	ColorTable,A0
	LEA	CopCol0,A1
	MOVE.W	(A0),(A1)
	MOVE.W	2(A0),4(A1)
	MOVE.W	4(A0),8(A1)
	MOVE.W	6(A0),12(A1)
	MOVE.W	8(A0),16(A1)
	MOVE.W	10(A0),20(A1)
	MOVE.W	12(A0),24(A1)
	MOVE.W	14(A0),28(A1)
	MOVE.W	14(A0),D0
	TST.W	SamScrEnable
	BEQ.S	sscnosc
	MOVE.W	8(A0),D0
sscnosc	MOVE.W	D0,NoteCol
	MOVE.W	10(A0),D0
	MOVE.W	D0,D4
	MOVE.W	#3,FadeX
	BSR.S	FadeCol
	MOVE.W	D0,32(A1)
	MOVE.W	D4,36(A1)
	MOVE.W	D4,D0
	MOVE.W	#-3,FadeX
	BSR.S	FadeCol
	MOVE.W	D0,40(A1)
	RTS

FadeCol	MOVE.W	D0,D1
	MOVE.W	D0,D2
	MOVE.W	D0,D3
	LSR.W	#8,D1
	ADD.W	FadeX(PC),D1
	BPL.S	redskp
	MOVEQ	#0,D1
redskp	CMP.W	#15,D1
	BLS.S	redskp2
	MOVEQ	#15,D1
redskp2	AND.W	#$00F0,D2
	LSR.W	#4,D2
	ADD.W	FadeX(PC),D2
	BPL.S	grnskp
	MOVEQ	#0,D2
grnskp	CMP.W	#15,D2
	BLS.S	grnskp2
	MOVEQ	#15,D2
grnskp2	AND.W	#$000F,D3
	ADD.W	FadeX(PC),D3
	BPL.S	bluskp
	MOVEQ	#0,D3
bluskp	CMP.W	#15,D3
	BLS.S	bluskp2
	MOVEQ	#15,D3
bluskp2	MOVE.W	D3,D0
	LSL.W	#4,D2
	OR.W	D2,D0
	LSL.W	#8,D1
	OR.W	D1,D0
	RTS

CurrColor	dc.w	0
FadeX		dc.w	-3

EnterPrintPath
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	LEA	PrintPath,A6
	MOVE.L	A6,ShowTextPtr
	MOVE.L	A6,TextEndPtr
	ADD.L	#31,TextEndPtr
	MOVE.W	#12,TextLength
	MOVE.W	#3694,A4
	BSR	GetTextLine
	BRA	RestorePtrCol

ShowPrintPath
	LEA	PrintPath,A0
	MOVE.W	#3694,D1
	MOVEQ	#12,D0
	BRA	ShowText3

; Print Song

PrintSong
	LEA	PrintSongText,A0
	BSR	AreYouSure
	BNE	Return1
	BSR	StorePtrCol
	MOVE.L	#PrintPath,D1
	MOVE.L	#1006,D2
	MOVE.L	DOSbase,A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BEQ	CantOpenFile
	BSR	SetDiskPtrCol
	LEA	PrintingSongText,A0
	BSR	ShowStatusText
	MOVE.L	FileHandle,D1
	MOVE.L	#SongDumpText,D2
	MOVEQ	#68,D3
	MOVE.L	DOSbase,A6
	JSR	LVOWrite(A6)
	MOVE.L	FileHandle,D1
	MOVE.L	SongDataPtr,D2
	MOVEQ	#20,D3
	JSR	LVOWrite(A6)
	BSR	PrintCRLF
	BSR	PrintCRLF
	BSR	PrintSong2
	BSR	PrintCRLF
	BSR	PrintSong4
	BSR	PrintFormFeed
	MOVE.L	SongDataPtr,A0
	MOVEQ	#0,D0
	MOVE.B	sd_numofpatt(A0),D0
	LEA	sd_pattpos(A0),A0
	MOVEQ	#0,D7
ps_loop	CMP.B	(A0,D0.W),D7
	BGT.S	ps_skip
	MOVE.B	(A0,D0.W),D7
ps_skip	SUBQ.W	#1,D0
	BPL.S	ps_loop
	MOVEQ	#0,D1
ps_loop2
	MOVEM.L	D1-D7/A0-A6,-(SP)
	BSR	PrintPattern
	MOVEM.L	(SP)+,D1-D7/A0-A6
	TST.L	D0
	BNE.S	ps_skip2
	MOVEM.L	D1/D7,-(SP)
	BSR	PrintFormFeed
	MOVEM.L	(SP)+,D1/D7
	ADDQ.W	#1,D1
	CMP.W	D7,D1
	BLO.S	ps_loop2
ps_skip2
	MOVE.L	DOSbase,A6
	MOVE.L	FileHandle,D1
	JSR	LVOClose(A6)
	BSR	ShowAllRight
	BRA	RestorePtrCol

PrintSong2
	MOVEQ	#1,D7
ps2_2	MOVE.L	D7,D0
	LSR.B	#4,D0
	CMP.B	#9,D0
	BLS.S	spujk
	ADDQ.B	#7,D0
spujk	ADD.B	#'0',D0
	MOVE.B	D0,PattXText1
	MOVE.B	D7,D0
	AND.B	#$0F,D0
	CMP.B	#9,D0
	BLS.S	spujk2
	ADDQ.B	#7,D0
spujk2	ADD.B	#'0',D0
	MOVE.B	D0,PattXText2

	MOVE.L	SongDataPtr,A0
	MOVE.W	D7,D0
	MULU	#30,D0
	LEA	-10(A0,D0.W),A0
	LEA	PpText,A1
	MOVEQ	#21,D0
ps2_loop
	MOVE.B	#' ',(A1)+
	DBRA	D0,ps2_loop
	LEA	PpText,A1

	MOVE.L	A0,SavIt
ps2_loop2
	MOVE.B	(A0)+,D0
	BEQ	PrintSong3
	MOVE.B	D0,(A1)+
	BRA.S	ps2_loop2

SavIt	dc.l	0

PrintSong3
	MOVE.L	D7,-(SP) ; pattnum
	MOVE.L	SavIt(PC),A0
	MOVE.W	22(A0),D0
	ADD.W	D0,D0
	LEA	Prafs+2+4,A0
	BSR	IntToHexASCII
	MOVE.L	SavIt(PC),A0
	MOVE.W	26(A0),D0
	ADD.W	D0,D0
	LEA	Prafs+8+4,A0
	BSR	IntToHexASCII
	MOVE.L	SavIt(PC),A0
	MOVE.W	28(A0),D0
	ADD.W	D0,D0
	LEA	Prafs+14+4,A0
	BSR	IntToHexASCII
	MOVE.L	DOSbase,A6
	MOVE.L	FileHandle,D1
	MOVE.L	#PtotText,D2
	MOVEQ	#52,D3
	JSR	LVOWrite(A6)
	MOVE.L	(SP)+,D7
	ADDQ.W	#1,D7
	CMP.W	#$0020,D7
	BNE	ps2_2
	RTS

PrintSong4
	MOVE.L	SongDataPtr,A0
	MOVEQ	#0,D7
	MOVE.B	sd_numofpatt(A0),D7
	LEA	sd_pattpos(A0),A0
	MOVEQ	#0,D5
ps4_loop1
	MOVEQ	#0,D6
ps4_loop2
	MOVEQ	#0,D0
	MOVE.B	(A0)+,D0
	MOVE.W	D0,D1
	LSR.W	#4,D1
	ADD.B	#$30,D1
	CMP.B	#$39,D1
	BLS.S	ps4_skip
	ADDQ.B	#7,D1
ps4_skip
	AND.B	#15,D0
	ADD.B	#$30,D0
	CMP.B	#$39,D0
	BLS.S	ps4_skip2
	ADDQ.B	#7,D0
ps4_skip2
	LEA	PnText,A1
	MOVE.B	D1,(A1)+
	MOVE.B	D0,(A1)
	MOVEM.L	D5-D7/A0,-(SP)
	MOVE.L	DOSbase,A6
	MOVE.L	FileHandle,D1
	MOVE.L	#PnText,D2
	MOVEQ	#4,D3
	JSR	LVOWrite(A6)
	MOVEM.L	(SP)+,D5-D7/A0
	ADDQ.W	#1,D5
	CMP.W	D5,D7
	BEQ	Return1
	ADDQ.W	#1,D6
	CMP.W	#$0010,D6
	BNE.S	ps4_loop2
	MOVEM.L	D5-D7/A0,-(SP)
	BSR.S	PrintCRLF
	MOVEM.L	(SP)+,D5-D7/A0
	BRA.S	ps4_loop1

PrintCRLF
	MOVE.L	DOSbase,A6
	MOVE.L	FileHandle,D1
	MOVE.L	#CRLF_Text,D2
	MOVEQ	#2,D3
	JSR	LVOWrite(A6)
	RTS

PrintFormFeed
	MOVE.L	DOSbase,A6
	MOVE.L	FileHandle,D1
	MOVE.L	#FF_Text,D2
	MOVEQ	#1,D3
	JSR	LVOWrite(A6)
	RTS

PrintPattern
	MOVE.L	D1,D6 ; D1=pattern number
	DIVU	#10,D6
	ADD.B	#'0',D6
	MOVE.B	D6,PattNumText1
	SWAP	D6
	ADD.B	#'0',D6
	MOVE.B	D6,PattNumText2
	MOVEM.L	D0-D7/A0-A6,-(SP)
	MOVE.L	FileHandle,D1
	MOVE.L	#PatternNumText,D2
	MOVEQ	#18,D3
	MOVE.L	DOSbase,A6
	JSR	LVOWrite(A6)
	MOVEM.L	(SP)+,D0-D7/A0-A6

	MOVE.L	D1,D6
	MOVE.L	SongDataPtr,A6
	LEA	sd_patterndata(A6),A6
	LSL.L	#8,D6
	LSL.L	#2,D6
	ADD.L	D6,A6
	CLR.L	PPattPos
	MOVEQ	#0,D6
pp_posloop
	MOVEQ	#0,D7
	MOVE.W	#2,TextLength
	MOVE.L	PPattPos,D1
	LEA	PattPosText,A5
	DIVU	#10,D1
	ADD.B	#'0',D1
	MOVE.B	D1,(A5)+
	CLR.W	D1
	SWAP	D1
	ADD.B	#'0',D1
	MOVE.B	D1,(A5)+
	ADDQ.L	#5,A5
pp_noteloop
	MOVEQ	#0,D0
	MOVEQ	#0,D1
	MOVE.W	#3,TextLength
	MOVE.W	(A6),D1
	AND.W	#$0FFF,D1
	LEA	PeriodTable,A0
pp_findloop
	CMP.W	0(A0,D0.L),D1
	BEQ	PrintNote
	ADDQ.L	#2,D0
	BRA.S	pp_findloop

PrintNote
	ADD.L	D0,D0
	ADD.L	NoteNamesPtr,D0
	MOVE.L	D0,A0
	MOVE.L	(A0),(A5)+
	CMP.B	#'Ą',-3(A5)
	BNE.S	prnoxyz
	MOVE.B	#'b',-3(A5)
prnoxyz	ADDQ.L	#1,A5
	MOVEQ	#0,D0
	MOVE.W	(A6),D0
	AND.W	#$F000,D0
	LSR.W	#8,D0
	LSL.L	#1,D0
	LEA	FastHexTable,A0
	ADD.L	D0,A0
	MOVE.B	(A0),(A5)+
	MOVEQ	#0,D0
	MOVE.B	2(A6),D0
	LSL.L	#1,D0
	LEA	FastHexTable,A0
	ADD.L	D0,A0
	MOVE.W	(A0),(A5)+
	MOVEQ	#0,D0
	MOVE.B	3(A6),D0
	LSL.L	#1,D0
	LEA	FastHexTable,A0
	ADD.L	D0,A0
	MOVE.W	(A0),(A5)+
	ADDQ.L	#4,A5
	ADDQ.L	#4,A6
	ADDQ.L	#1,D7
	CMP.L	#4,D7
	BNE	pp_noteloop
	ADDQ.L	#1,PPattPos
	MOVEM.L	D0-D7/A0-A6,-(SP)
	MOVE.L	DOSbase,A6
	MOVE.L	FileHandle,D1
	MOVE.L	#PnText2,D2
	MOVEQ	#68,D3
	JSR	LVOWrite(A6)
	BSR	PrintCRLF
	MOVEM.L	(SP)+,D0-D7/A0-A6
	BTST	#2,$DFF016
	BEQ.S	NegativeReturn
	ADDQ.L	#1,D6
	CMP.L	#64,D6
	BNE	pp_posloop
	MOVEQ	#0,D0
	RTS

NegativeReturn
	MOVEQ	#-1,D0
	RTS

PositiveReturn
	MOVEQ	#0,D0
	RTS

;---- Setup2 ----

Setup2	BSR	WaitForButtonUp
	MOVE.W	#7,CurrScreen
	CLR.W	LastSetupScreen
	ST	DisableAnalyzer
	ST	NoSampleInfo
	BSR	Clear100Lines
	MOVE.L	SetupMemPtr,D0
	BNE	set2skp
	BSR	SaveMainPic
	BEQ	ExitSetup
set2skp	BSR	DecompactSetup2
	BSR	SetScreenColors
refrsh2	BSR	ShowIntMode
	BSR	ShowTempo
	BSR	ShowSpeed
	BSR	ShowColEdit
	BSR	ShowRainbow
	BSR	GetColPos
	BSR	ShowColSliders
	BSR	ShowS2Modules
	BSR	ShowS2Songs
	BSR	ShowS2Samples
	BSR	ShowS2PTPath
	BSR	ShowS2MaxPLST
	BSR	ShowS2DMAWait
	BSR	ShowS2TuneNote
	BSR	ShowS2SalvAddr
	BRA	ShowS2T

CheckSetup2Gadgs
	MOVEM.W	MouseX2,D0/D1
	CMP.W	#210,D0
	BHS	Setup2Menu3
	CMP.W	#108,D0
	BHS.S	Setup2Menu2
;---- Menu 1 ----
	CMP.W	#11,D1
	BLS	ToggleIntMode
	CMP.W	#22,D1
	BLS	ChangeTempo
	CMP.W	#33,D1
	BLS	ChangeSpeed
	CMP.W	#44,D1
	BLS	ToggleColEdit
	CMP.W	#55,D1
	BLS	RotOrSpread
	CMP.W	#66,D1
	BLS	SetRed2
	CMP.W	#77,D1
	BLS	SetGreen2
	CMP.W	#88,D1
	BLS	SetBlue2
	CMP.W	#99,D1
	BLS	ColorGadgets2
	RTS

Setup2Menu2
	CMP.W	#11,D1
	BLS	Set2ModPath
	CMP.W	#22,D1
	BLS	Set2SongPath
	CMP.W	#33,D1
	BLS	Set2SamPath
	CMP.W	#44,D1
	BLS	Set2PTPath
	CMP.W	#55,D1
	BLS	SetS2MaxPLST
	CMP.W	#66,D1
	BLS	SetS2DMAWait
	CMP.W	#77,D1
	BLS	SetS2TuneNote
	CMP.W	#88,D1
	BLS	SetS2SalvAddr
	CMP.W	#99,D1
	BLS	RecoverSong
	RTS

Setup2Menu3
	CMP.W	#11,D1
	BLS	ExitOrDefault
	CMP.W	#22,D1
	BLS	ToggleOverride
	CMP.W	#33,D1
	BLS	ToggleNosamples
	CMP.W	#44,D1
	BLS	ToggleBlankZero
	CMP.W	#55,D1
	BLS	ToggleShowDirs
	CMP.W	#66,D1
	BLS	ToggleShowPublic
	CMP.W	#77,D1
	BLS	ToggleCutToBuf
	CMP.W	#88,D1
	BLS	ToggleIFFLoop
	CMP.W	#99,D1
	BLS	ToggleHH
	RTS

ExitOrDefault
	CMP.W	#265,D0
	BLS.S	SetS2Default
	CMP.W	#306,D0
	BHS	Setup
	BRA	ExitSetup

SetS2Default
	LEA	SetDefaultsText(PC),A0
	BSR	AreYouSure
	BNE	Return1
	LEA	DefaultSetupData,A0
	LEA	SongsPath-SetupData(A0),A2
	LEA	SongsPath,A1
	MOVEQ	#95,D0
ss2dela	MOVE.B	(A2)+,(A1)+
	DBRA	D0,ss2dela
	LEA	PTPath-SetupData(A0),A2
	LEA	PTPath,A1
	MOVEQ	#31,D0
ss2delb	MOVE.B	(A2)+,(A1)+
	DBRA	D0,ss2delb
	LEA	MaxPLSTEntries-SetupData(A0),A2
	MOVE.W	(A2),MaxPLSTEntries
	LEA	DMAWait-SetupData(A0),A2
	MOVE.W	(A2),DMAWait
	LEA	TuneNote-SetupData(A0),A2
	MOVE.L	(A2),TuneNote
	LEA	SalvageAddress-SetupData(A0),A2
	LEA	SalvageAddress,A1
	MOVE.L	(A2)+,(A1)+
	MOVE.L	(A2),(A1)
	BRA	refrsh2

SetDefaultsText	dc.b "Set defaults?",0

ToggleIntMode
	BSR	ResetMusicInt
	EOR.B	#1,IntMode
	BSR	SetMusicInt
	BSR	SetTempo
	BSR	WaitForButtonUp
ShowIntMode
	LEA	VBlankText(PC),A0
	TST.B	IntMode
	BEQ.S	simskip
	LEA	CIAText(PC),A0
simskip	MOVEQ	#6,D0
	MOVE.W	#167,D1
	BRA	ShowText3

VBlankText	dc.b "VBLANK"
CIAText		dc.b " CIA  "

ChangeSpeed
	MOVEQ	#0,D1
	CMP.W	#94,D0
	BHS.S	SpeedDown
	CMP.W	#80,D0
	BHS.S	SpeedUp
	RTS

SpeedUp	MOVE.W	DefaultSpeed,D1
	ADDQ.B	#1,D1
	CMP.B	#$FF,D1
	BLS.S	spedup2
	MOVE.W	#$FF,D1
spedup2	MOVE.W	D1,DefaultSpeed
	MOVE.L	D1,CurrSpeed
	BSR.S	ShowSpeed
	BSR	Wait_4000
	BRA	Wait_4000
ShowSpeed
	MOVE.W	#608+440,TextOffset
	MOVE.W	DefaultSpeed,WordNumber
	JMP	PrintHexByte

SpeedDown
	MOVE.W	DefaultSpeed,D1
	SUB.B	#1,D1
	CMP.B	#1,D1
	BHS.S	spedup2
	MOVEQ	#1,D1
	BRA.S	spedup2


ToggleOverride
	EOR.B	#1,OverrideFlag
	BRA.S	ShowS2T
ToggleNosamples
	EOR.B	#1,NosamplesFlag
	BRA.S	ShowS2T
ToggleBlankZero
	EOR.B	#1,BlankZeroFlag
	BSR.S	ShowS2T
	BRA	RedrawPattern
ToggleShowDirs
	EOR.B	#1,ShowDirsFlag
	BRA.S	ShowS2T
ToggleShowPublic
	EOR.B	#1,ShowPublicFlag
	MOVE.W	#1,UpdateFreeMem
	BRA.S	ShowS2T
ToggleCutToBuf
	EOR.B	#1,CutToBufFlag
	BRA.S	ShowS2T
ToggleIFFLoop
	EOR.B	#1,IFFLoopFlag
	BRA.S	ShowS2T
ToggleHH
	EOR.B	#1,HHFlag

ShowS2T	CLR.B	RawKeyCode
	CMP.W	#7,CurrScreen
	BNE	Return1
	LEA	OverrideFlag,A4
	MOVE.W	#3,TextLength
	MOVEQ	#7,D7
	MOVE.W	#636,D6
sstloop2
	MOVE.W	D6,TextOffset
	LEA	ToggleOFFText,A0
	TST.B	(A4)+
	BEQ.S	sstskip2
	LEA	ToggleONText,A0
sstskip2
	BSR	ShowText2
	ADD.W	#440,D6
	DBRA	D7,sstloop2
	BRA	WaitForButtonUp

ShowS2Modules
	MOVE.W	#177,D1
	LEA	ModulesPath,A0
s2path	MOVEQ	#9,D0
	BRA	ShowText3

ShowS2Songs
	MOVE.W	#177+440,D1
	LEA	SongsPath,A0
	BRA.S	s2path

ShowS2Samples
	MOVE.W	#177+880,D1
	LEA	SamplePath,A0
	BRA.S	s2path

ShowS2PTPath
	MOVE.W	#177+1320,D1
	LEA	PTPath,A0
	BRA.S	s2path


Set2ModPath
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	LEA	ModulesPath,A6
	MOVE.L	A6,ShowTextPtr
	MOVE.L	A6,TextEndPtr
	ADD.L	#31,TextEndPtr
	MOVE.W	#9,TextLength
	MOVE.W	#177,A4
	BSR	GetTextLine
	BRA	RestorePtrCol

Set2SongPath
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	LEA	SongsPath,A6
	MOVE.L	A6,ShowTextPtr
	MOVE.L	A6,TextEndPtr
	ADD.L	#31,TextEndPtr
	MOVE.W	#9,TextLength
	MOVE.W	#177+440,A4
	BSR	GetTextLine
	BRA	RestorePtrCol

Set2SamPath
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	LEA	SamplePath,A6
	MOVE.L	A6,ShowTextPtr
	MOVE.L	A6,TextEndPtr
	ADD.L	#31,TextEndPtr
	MOVE.W	#9,TextLength
	MOVE.W	#177+880,A4
	BSR	GetTextLine
	BRA	RestorePtrCol

Set2PTPath
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	LEA	PTPath,A6
	MOVE.L	A6,ShowTextPtr
	MOVE.L	A6,TextEndPtr
	ADD.L	#31,TextEndPtr
	MOVE.W	#9,TextLength
	MOVE.W	#177+1320,A4
	BSR	GetTextLine
	BRA	RestorePtrCol

SetS2MaxPLST
	CMP.W	#199,D0
	BHS.S	S2plstd
	CMP.W	#188,D0
	BHS.S	S2PLSTu
	RTS
S2PLSTu	MOVE.W	MaxPLSTEntries,D0
	ADD.W	#100,D0
	CMP.W	#9999,D0
	BLS.S	s2pu
	MOVE.W	#9999,D0
s2pu	MOVE.W	D0,MaxPLSTEntries
	BSR.S	ShowS2MaxPLST
	BRA	Wait_4000

S2PLSTd	MOVEQ	#0,D0
	MOVE.W	MaxPLSTEntries,D0
	ADDQ.W	#1,D0
	DIVU	#100,D0
	SUBQ.W	#1,D0
	BPL.S	s2pd
	MOVEQ	#0,D0
	BRA.S	s2pu
s2pd	MULU	#100,D0
	BRA.S	s2pu

ShowS2MaxPLST
	MOVE.W	MaxPLSTEntries,WordNumber
	MOVE.W	#1939,TextOffset
	BRA	Print4DecDigits

SetS2DMAWait
	CMP.W	#199,D0
	BHS.S	S2dwd
	CMP.W	#188,D0
	BHS.S	S2dwu
	RTS
S2dwu	MOVE.W	DMAWait,D0
	ADDQ.W	#1,D0
	BTST	#2,$DFF016
	BNE.S	s2npu
	ADD.W	#9,D0
s2npu	CMP.W	#9999,D0
	BLS.S	s2du
	MOVE.W	#9999,D0
s2du	MOVE.W	D0,DMAWait
	BSR.S	ShowS2DMAWait
	BRA	Wait_4000

S2dwd	MOVE.W	DMAWait,D0
	SUBQ.W	#1,D0
	BTST	#2,$DFF016
	BNE.S	s2npd
	SUB.W	#9,D0
s2npd	TST.W	D0
	BPL.S	s2du
	MOVEQ	#0,D0
	BRA.S	s2du

ShowS2DMAWait
	MOVE.W	DMAWAit,WordNumber
	MOVE.W	#2379,TextOffset
	BRA	Print4DecDigits

SetS2TuneNote
	CMP.W	#187,D0
	BHS.S	ss2tvol
	MOVE.W	#2820,D1
	MOVEQ	#3,D0
	LEA	SpcNoteText,A0
	BSR	ShowText3
	MOVE.W	#3,SamNoteType
	MOVE.L	#TuneNote,SplitAddress
	BRA	WaitForButtonUp

ShowS2TuneNote
	CMP.W	#7,CurrScreen
	BNE	Return1
	MOVE.L	NoteNamesPtr,A4
	MOVE.W	TuneNote,D0
	LSL.W	#2,D0
	LEA	(A4,D0.W),A0
	MOVEQ	#4,D0
	MOVE.W	#2820,D1
	BSR	ShowText3
	MOVE.W	TToneVol,WordNumber
	MOVE.W	#2824,TextOffset
	BRA	PrintHexByte

ss2tvol	MOVE.W	#2824,TextOffset
	BSR	GetHexByte
	MOVE.W	D0,TToneVol
	BRA.S	ShowS2TuneNote

SetS2SalvAddr
	BRA	ShowNotImpl
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	LEA	SalvageAddress,A6
	MOVE.L	A6,ShowTextPtr
	MOVE.L	A6,TextEndPtr
	ADD.L	#6,TextEndPtr
	MOVE.W	#6,TextLength
	MOVE.W	#3260,A4
	MOVE.B	#5,EnterTextFlag
	BSR	GetTextLine
	CLR.B	EnterTextFlag
	BSR	RestorePtrCol
ShowS2SalvAddr
	LEA	SalvageAddress,A0
	MOVEQ	#6,D0
	MOVE.W	#3260,D1
	BRA	ShowText3

RecoverSong
	BRA	ShowNotImpl
	LEA	RecoverSongText(PC),A0
	BSR	AreYouSure
	BNE	Return1
	BSR	DoClearSong
	BSR	ClrSampleInfo
	BSR.S	FindAdr
	MOVE.L	D0,A0
	MOVE.L	SongDataPtr,A1
	MOVE.L	#70716,D0
recloop	MOVE.B	(A0)+,(A1)+
	SUBQ.L	#1,D0
	BNE.S	recloop	
	BRA	RedrawPattern

FindAdr	LEA	SalvageAddress+6,A0
	BSR	HexToInteger
	MOVE.W	D0,D3
	MOVEQ	#0,D0
	BSR	HexToInteger2
	SWAP	D0
	OR.W	D3,D0
	RTS

RecoverSongText	dc.b	"Recover Song?",0

ToggleColEdit
	CMP.W	#79,D0
	BHS	SelectColor2
	CLR.W	SpreadFlag
	LEA	VUMeterColors,A0
	MOVEQ	#48,D0
	CMP.L	TheRightColors(PC),A0
	BNE.S	tced2
	LEA	AnalyzerColors,A0
	MOVEQ	#36,D0
tced2	MOVE.L	A0,TheRightColors
	MOVE.W	D0,ColorsMax
	CMP.W	RainbowPos(PC),D0
	BHI.S	tced3
	CLR.W	RainbowPos
tced3	BSR	ShowRainbow
	BSR.S	ShowColEdit
	BRA	WaitForButtonUp
ShowColEdit
	LEA	cedtxt1(PC),A0
	LEA	VUMeterColors,A1
	CMP.L	TheRightColors(PC),A1
	BEQ.S	shcoed
	LEA	cedtxt2(PC),A0
shcoed	MOVEQ	#6,D0
	MOVE.W	#1483,D1
	BRA	ShowText3

cedtxt1	dc.b "VU-MTR"
cedtxt2 dc.b "ANALYZ"

RotOrSpread
	CMP.W	#16,D0
	BLO.S	RotColUp
	CMP.W	#32,D0
	BLO.S	RotColDown
	CMP.W	#79,D0
	BHS	SelectColor2
	BRA	SpreadColors

RotColUp
	CLR.W	SpreadFlag
	MOVE.L	TheRightColors(PC),A0
	MOVE.W	(A0),D0
	MOVE.W	ColorsMax(PC),D1
	SUBQ.W	#2,D1
rocoup1	MOVE.W	2(A0),(A0)+
	DBRA	D1,rocoup1
	MOVE.W	D0,(A0)
rocoup2	BSR	GetColPos
	BSR	ShowColSliders
	BSR	ShowRainbow
	BRA	SetupVUCols

RotColDown
	CLR.W	SpreadFlag
	MOVE.L	TheRightColors(PC),A0
	MOVE.W	ColorsMax(PC),D1
	ADD.W	D1,A0
	ADD.W	D1,A0
	MOVE.W	-(A0),D0
	SUBQ.W	#2,D1
rocodn1	MOVE.W	-2(A0),(A0)
	SUBQ.L	#2,A0
	DBRA	D1,rocodn1
	MOVE.W	D0,(A0)
	BRA.S	rocoup2

SpreadColors
	MOVE.W	RainbowPos,SpreadFrom
	MOVE.W	#1,SpreadFlag
	RTS

SpreadFrom	dc.w 0
SpreadFlag	dc.w 0

ColorGadgets2
	CLR.W	SpreadFlag
	CMP.W	#79,D0
	BHS.S	SetDefaultCol2
	CMP.W	#33,D0
	BHS.S	CancelCol2
	MOVE.L	UndoColAddr,A0
	MOVE.W	UndoCol,D1
	MOVE.W	(A0),UndoCol
	MOVE.W	D1,(A0)
	BRA	rocoup2

SetDefaultCol2
	MOVE.L	TheRightColors(PC),A0
	MOVE.L	A0,A1
	SUB.L	#SetupData,A1
	ADD.L	#DefaultSetupData,A1
sedeco4	MOVE.W	ColorsMax,D0
	BRA.S	sedeco3
sedeco2	MOVE.W	(A1)+,(A0)+
sedeco3	DBRA	D0,sedeco2
	BRA	rocoup2

CancelCol2
	MOVE.L	TheRightColors(PC),A0
	MOVE.L	A0,A1
	SUB.L	#VUMeterColors,A1
	ADD.L	#SaveColors,A1
	BRA.S	sedeco4

GetColAddr
	MOVE.L	TheRightColors(PC),A0
	MOVE.W	RainbowPos,D1
	ADD.W	D1,D1
	LEA	(A0,D1.W),A0
	MOVE.W	(A0),D1
	MOVE.W	D1,D2
	RTS

SetRed2	CMP.W	#79,D0
	BHS	SelectColor2
set2r2	BSR	GetColPos
	AND.W	#$0F00,D2
	LSR.W	#8,D2
	CMP.B	D2,D0
	BEQ.S	setrsk2
	AND.W	#$00FF,D1
	LSL.W	#8,D0
	OR.W	D0,D1
	MOVE.W	D1,(A0)
	BSR	ShowColSliders
	BSR	ShowRainbow
	BSR	SetupVUCols
setrsk2	BTST	#6,$BFE001
	BEQ.S	set2r2
	RTS

SetGreen2
	CMP.W	#79,D0
	BHS	SelectColor2
set2g2	BSR	GetColPos
	AND.W	#$00F0,D2
	LSR.W	#4,D2
	CMP.B	D2,D0
	BEQ.S	setgsk2
	AND.W	#$0F0F,D1
	LSL.W	#4,D0
	OR.W	D0,D1
	MOVE.W	D1,(A0)
	BSR	ShowColSliders
	BSR	ShowRainbow
	BSR	SetupVUCols
setgsk2	BTST	#6,$BFE001
	BEQ.S	set2g2
	RTS

SetBlue2
	CMP.W	#79,D0
	BHS.S	SelectColor2
set2b2	BSR	GetColPos
	AND.W	#$000F,D2
	CMP.B	D2,D0
	BEQ.S	setbsk2
	AND.W	#$0FF0,D1
	OR.W	D0,D1
	MOVE.W	D1,(A0)
	BSR	ShowColSliders
	BSR	ShowRainbow
	BSR	SetupVUCols
setbsk2	BTST	#6,$BFE001
	BEQ.S	set2b2
	RTS

SelectColor2
	MOVEQ	#0,D7
setcolp	BTST	#6,$BFE001
	BNE.S	ChkSpread
	MOVE.W	ColorsMax(PC),D0
	MOVE.W	MouseY,D1
	CMP.W	D7,D1
	BEQ.S	setcolp
	MOVE.W	D1,D7
	CMP.W	#37,D1
	BHS.S	setcoly
	MOVEQ	#37,D1
setcoly	SUB.W	#37,D1
	CMP.W	D0,D1
	BLT.S	setcol2
	MOVE.W	D0,D1
	SUBQ.W	#1,D1
setcol2	MOVE.W	D1,RainbowPos
	BSR	ShowRainbow
	BSR	GetColPos
	MOVE.L	A0,UndoColAddr
	MOVE.W	D1,UndoCol
	BSR	ShowColSliders
	BRA	setcolp

ChkSpread
	TST.W	SpreadFlag
	BEQ	Return1
	CLR.W	SpreadFlag
	BSR	GetColPos
	MOVE.W	SpreadFrom,D0
	MOVE.W	RainbowPos,D1
	CMP.W	D1,D0
	BLO.S	chkspr2
	EXG	D0,D1
chkspr2	MOVE.W	D1,D4
	SUB.W	D0,D4
	CMP.W	#1,D4
	BLS	Return1
	MOVE.L	D4,A2
	ADD.W	D4,D4
	MOVE.L	TheRightColors(PC),A0
	MOVE.L	A0,A1
	ADD.W	D0,D0
	ADD.W	D1,D1
	LEA	(A0,D0.W),A0	; 1st col
	MOVE.W	(A0),D2
	LEA	(A1,D1.W),A1
	MOVE.W	(A1),D3		; 2nd col
	MOVEQ	#0,D5

sprdlop	MOVE.W	D2,D0 ; red
	LSR.W	#8,D0
	MOVE.W	D3,D1
	LSR.W	#8,D1
	BSR	ColCrossfade
	LSL.W	#8,D0
	MOVE.W	D0,D7
	MOVE.W	D2,D0 ; green
	LSR.W	#4,D0
	AND.W	#$000F,D0
	MOVE.W	D3,D1
	LSR.W	#4,D1
	AND.W	#$000F,D1
	BSR	ColCrossfade
	LSL.W	#4,D0
	OR.W	D0,D7
	MOVE.W	D2,D0 ; blue
	AND.W	#$000F,D0
	MOVE.W	D3,D1
	AND.W	#$000F,D1
	BSR	ColCrossfade
	OR.W	D0,D7
	ADDQ.W	#2,D5
	MOVE.W	D7,(A0)+
	CMP.L	A1,A0
	BLO	sprdlop
	BRA	rocoup2

ColCrossfade
	MOVE.W	D4,D6
	SUB.W	D5,D6
	MULU	D6,D0
	ADD.L	A2,D0
	MULU	D5,D1
	ADD.W	D1,D0
	DIVU	D4,D0
	CMP.W	#15,D0
	BLS	Return1
	MOVEQ	#15,D0
	RTS

ShowRainbow
	MOVE.L	TextBplPtr,A0
	LEA	1490(A0),A0
	MOVEQ	#0,D1
	MOVE.L	#$00FFE000,D2
	MOVE.W	RainbowPos,D3
rainlop	MOVE.L	D2,D0
	CMP.W	D3,D1
	BNE.S	rainsk1
	MOVE.L	#$1EFFEF00,D0
rainsk1	MOVE.L	D0,(A0)
	LEA	40(A0),A0
	ADDQ.W	#1,D1
	CMP.W	#48,D1
	BLO.S	rainlop
	LEA	CopListAnalyzer,A0
	MOVE.L	TheRightColors(PC),A1
	MOVE.W	#$5107,D0
	MOVEQ	#47,D1
	MOVEQ	#0,D2
rainlp2	MOVE.W	D0,(A0)+
	MOVE.W	#$FFFE,(A0)+
	MOVE.W	#$018E,(A0)+
	CMP.W	ColorsMax(PC),D2
	BLO.S	rainsk2
	CLR.W	(A0)+
	BRA.S	rainsk3
rainsk2	MOVE.W	(A1)+,(A0)+
rainsk3	ADD.W	#$0100,D0
	ADDQ.W	#1,D2
	DBRA	D1,rainlp2
	MOVEQ	#63,D0
rainlp3	MOVE.L	#$01B80000,(A0)+
	DBRA	D0,rainlp3
	RTS

RainbowPos	dc.w	0
TheRightColors	dc.l	0
ColorsMax	dc.w	48

;---- PT Decompacter ----

Decompact
	MOVE.L	A0,CompPtr
	MOVE.L	D0,CompLen
	BSR.S	FreeDecompMem
	MOVE.L	CompPtr(PC),A0
	MOVE.L	(A0),D0
	MOVE.L	D0,DecompMemSize
	MOVEQ	#0,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,DecompMemPtr
	BEQ	OutOfMemErr
	MOVE.L	D0,A1
	MOVE.L	CompPtr(PC),A0
	MOVE.L	CompLen(PC),D0
	ADDQ.L	#4,A0
	SUBQ.L	#4,D0
dcmloop	MOVE.B	(A0)+,D1
	CMP.B	#181,D1
	BEQ.S	DecodeIt
	MOVE.B	D1,(A1)+
decom2	SUBQ.L	#1,D0
	CMP.L	#0,D0
	BGT.S	dcmloop
	MOVE.L	DecompMemPtr,A1
	MOVEQ	#-1,D0
	RTS

DecodeIt
	MOVEQ	#0,D1
	MOVE.B	(A0)+,D1
	MOVE.B	(A0)+,D2
dcdloop	MOVE.B	D2,(A1)+
	DBRA	D1,dcdloop
	SUBQ.L	#2,D0
	BRA.S	decom2

FreeDecompMem
	MOVE.L	DecompMemPtr,D0
	BEQ	Return1
	MOVE.L	D0,A1
	MOVE.L	DecompMemSize,D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)	
	CLR.L	DecompMemPtr
	RTS

CompPtr	dc.l	0
CompLen	dc.l	0

;---- Position Insert/Delete Gadgets ----

PosInsDelGadgs
	MOVE.W	MouseY2,D0
	CMP.W	#11,D0
	BHS	Return1
	MOVE.W	MouseX2,D0
	CMP.W	#62,D0
	BHS	Return1
	CMP.W	#51,D0
	BHS.S	PosDelete
	CMP.W	#40,D0
	BHS.S	PosInsert
	RTS

PosInsert
	MOVE.L	SongDataPtr,A0
	LEA	sd_pattpos(A0),A0
	ADD.L	#126,A0
	MOVEQ	#127,D0
	MOVE.L	CurrPos,D1
	AND.L	#127,D1
posinloop
	MOVE.B	(A0),1(A0)
	SUBQ.L	#1,A0
	SUBQ.L	#1,D0
	CMP.L	D1,D0
	BHI.S	posinloop
	MOVE.B	PatternNumber+3,1(A0)
	BSR	PositionUp
	CLR.W	UpOrDown
	BSR	SongLengthGadg
	BRA	WaitForButtonUp

PosDelete
	MOVE.L	SongDataPtr,A0
	LEA	sd_pattpos(A0),A0
	MOVE.L	CurrPos,D0
	AND.L	#127,D0
	ADD.L	D0,A0
posdeloop
	MOVE.B	1(A0),(A0)
	ADDQ.L	#1,A0
	ADDQ.L	#1,D0
	CMP.L	#127,D0
	BLS.S	posdeloop
	CLR.B	-1(A0)
	BSR	ShowPosition
	MOVE.W	#-1,UpOrDown
	BSR	SongLengthGadg
	BRA	WaitForButtonUp

;---- Enter number gadg ----

EnterNumGadg
	MOVE.W	MouseY2,D1
	CMP.W	#11,D1
	BLO	EnterPosGadg
	CMP.W	#22,D1
	BLO	EnterPattGadg
	CMP.W	#33,D1
	BLO	EnterLenGadg
	RTS

EnterPosGadg
	CLR.B	RawKeyCode
	MOVE.W	#76,LineCurX
	MOVE.W	#10,LineCurY
	MOVE.W	#169,TextOffset
	BSR	GetDec3Dig
	TST.W	AbortDecFlag
	BNE	pogskip
	CMP.W	#127,D0
	BLS.S	eposgok
	MOVEQ	#127,D0
eposgok	MOVE.L	D0,CurrPos
	BRA	pogskip

EnterPattGadg
	CLR.B	RawKeyCode
	MOVE.W	#84,LineCurX
	MOVE.W	#21,LineCurY
	MOVE.W	#610,TextOffset
	BSR	GetDecByte
	TST.W	AbortDecFlag
	BNE	pogskip
	CMP.B	#63,D0
	BLS.S	epgok
	MOVEQ	#63,D0
epgok	MOVE.L	SongDataPtr,A0
	LEA	sd_pattpos(A0),A0
	ADD.L	CurrPos,A0
	MOVE.B	D0,(A0)
	BRA	pogskip

EnterLenGadg
	CLR.B	RawKeyCode
	MOVE.W	#76,LineCurX
	MOVE.W	#32,LineCurY
	MOVE.W	#1049,TextOffset
	BSR	GetDec3Dig
	TST.W	AbortDecFlag
	BNE	ShowSongLength
	CMP.W	#128,D0
	BLS.S	elengok
	MOVE.B	#128,D0
elengok	MOVE.L	SongDataPtr,A0
	LEA	sd_numofpatt(A0),A0
	MOVE.B	D0,(A0)
	BRA	ShowSongLength

GetDec3Dig
	MOVE.W	#1,AbortDecFlag
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	BSR	UpdateLineCurPos
gd3loop	BSR	GetKey0_9
	CMP.W	#68,D1
	BEQ.S	gd3exit
	CMP.W	#2,D1
	BHI.S	gd3loop
	MOVE.W	D1,D0
	MULU	#100,D0
	MOVE.W	D0,gd3temp
	BSR	ShowOneDigit
	ADDQ.W	#8,LineCurX
	BSR	UpdateLineCurPos
	BSR	GetKey0_9
	CMP.B	#68,D1
	BEQ.S	gd3exit
	MOVE.W	D1,D0
	MULU	#10,D0
	ADD.W	D0,gd3temp
	BSR	ShowOneDigit
	ADDQ.W	#8,LineCurX
	BSR	UpdateLineCurPos
	BSR	GetKey0_9
	CMP.B	#68,D1
	BEQ.S	gd3exit
	ADD.W	D1,gd3temp
	CLR.W	AbortDecFlag
gd3exit	CLR.W	LineCurX
	MOVE.W	#270,LineCurY
	BSR	UpdateLineCurPos
	BSR	RestorePtrCol
	MOVEQ	#0,D0
	MOVE.W	gd3temp(PC),D0
	RTS

gd3temp	dc.w	0

;----  Up/Down Gadgets ----

DownGadgets
	MOVE.W	#1,UpdateFreeMem
	MOVE.W	#-1,UpOrDown
	BRA.S	ug2

UpGadgets
	MOVE.W	#1,UpdateFreeMem
	CLR.W	UpOrDown
ug2	MOVE.W	MouseY2,D0
	CMP.W	#100,D0
	BHS	Return1
	CMP.W	#89,D0
	BHS	RepLenGadg
	CMP.W	#78,D0
	BHS	RepeatGadg
	CMP.W	#67,D0
	BHS	SampleLengthGadg
	CMP.W	#56,D0
	BHS	VolumeGadg
	CMP.W	#45,D0
	BHS	SampleNumGadg
	CMP.W	#34,D0
	BHS	FineTuneGadg
	CMP.W	#23,D0
	BHS	SongLengthGadg
	CMP.W	#12,D0
	BHS	PatternGadg
	TST.W	MouseY2
	BHS.S	PositionGadg
	RTS

PositionGadg
	TST.W	UpOrDown
	BMI.S	PositionDown
PositionUp
	ADDQ.L	#1,CurrPos
	BTST	#2,$DFF016
	BNE.S	pogskp2
	ADD.L	#9,CurrPos
pogskp2	CMP.L	#127,CurrPos
	BLS.S	pogskip
	MOVE.L	#127,CurrPos
pogskip	MOVE.L	CurrPos,SongPosition
	BSR	ShowPosition
	BSR	Wait_4000
	BSR	Wait_4000
	BRA	Wait_4000

PositionDown
	SUBQ.L	#1,CurrPos
	BTST	#2,$DFF016
	BNE.S	pogskp3
	SUB.L	#9,CurrPos
pogskp3	TST.L	CurrPos
	BPL.S	pogskip
	CLR.L	CurrPos
	BRA.S	pogskip

PatternGadg
	MOVE.L	SongDataPtr,A0
	LEA	sd_pattpos(A0),A0
	TST.W	UpOrDown
	BMI.S	PatternDown
PatternUp
	ADD.L	CurrPos,A0
	ADDQ.B	#1,(A0)
	BTST	#2,$DFF016
	BNE.S	pagaskp
	ADD.B	#9,(A0)
pagaskp	CMP.B	#63,(A0)
	BLS.S	pogskip
	MOVE.B	#63,(A0)
	BRA.S	pogskip
 
PatternDown
	ADD.L	CurrPos,A0
	SUBQ.B	#1,(A0)
	BTST	#2,$DFF016
	BNE.S	padoskp
	SUB.B	#9,(A0)
padoskp	TST.B	(A0)
	BPL	pogskip
	CLR.B	(A0)
	BRA	pogskip


SongLengthGadg
	MOVE.L	SongDataPtr,A0
	LEA	sd_numofpatt(A0),A0
	TST.W	UpOrDown
	BMI.S	SongLengthDown
SongLengthUp
	ADDQ.B	#1,(A0)
	BTST	#2,$DFF016
	BNE.S	slupskp
	ADD.B	#9,(A0)
slupskp	CMP.B	#127,(A0)
	BMI.S	solgskip
	MOVE.B	#127,(A0)
solgskip
	BSR	ShowSongLength
	BSR	Wait_4000
	BSR	Wait_4000
	BRA	Wait_4000
 
SongLengthDown
	SUBQ.B	#1,(A0)
	BTST	#2,$DFF016
	BNE.S	sldoskp
	SUB.B	#9,(A0)
sldoskp	CMP.B	#1,(A0)
	BGE.S	solgskip
	MOVE.B	#1,(A0)
	BRA.S	solgskip


SampleNumGadg
	BTST	#2,$DFF016	; Check Right Mousebutton
	BNE.S	SampleNum2
	TST.W	InsNum
	BEQ	ShowSampleInfo
	MOVE.W	InsNum,LastInsNum
	CLR.W	InsNum
	BRA	ShowSampleInfo
 
SampleNum2
	TST.W	UpOrDown
	BMI.S	SampleNumDown
SampleNumUp
	ADDQ.W	#1,InsNum
	CMP.W	#31,InsNum
	BMI.S	snuskip
	MOVE.W	#31,InsNum
snuskip	BSR	redrsam
	BSR	Wait_4000
	BRA	Wait_4000

SampleNumDown
	TST.W	InsNum
	BEQ.S	snuskip
	SUBQ.W	#1,InsNum
	CMP.W	#1,InsNum
	BPL.S	snuskip
	MOVE.W	#1,InsNum
	BRA.S	snuskip

FineTuneGadg
	MOVE.L	SongDataPtr,A0
	LEA	12(A0),A0
	MOVE.W	InsNum,D0
	BEQ	Return1
	MULU	#30,D0
	ADD.L	D0,A0
	BTST	#2,$DFF016
	BNE.S	ftgskip
	CLR.B	2(A0)
	BRA.S	ftuskip
ftgskip	TST.W	UpOrDown
	BMI.S	FineTuneDown
FineTuneUp
	AND.B	#$0F,2(A0)
	CMP.B	#7,2(A0)
	BEQ.S	ftuskip
	ADDQ.B	#1,2(A0)
	AND.B	#$0F,2(A0)
ftuskip	BSR	ShowSampleInfo
	BSR	Wait_4000
	BRA	Wait_4000

FineTuneDown
	AND.B	#$0F,2(A0)
	CMP.B	#8,2(A0)
	BEQ.S	ftuskip
	SUBQ.B	#1,2(A0)
	AND.B	#$0F,2(A0)
	BRA.S	ftuskip


VolumeGadg
	MOVE.L	SongDataPtr,A0
	LEA	12(A0),A0
	MOVE.W	InsNum,D0
	BEQ	Return1
	MULU	#30,D0
	ADD.L	D0,A0
	TST.W	UpOrDown
	BMI.S	VolumeDown
VolumeUp
	ADDQ.B	#1,3(A0)
	BTST	#2,$DFF016
	BNE.S	voupskp
	ADD.B	#15,3(A0)
voupskp	CMP.B	#$40,3(A0)
	BLS.S	ftuskip
	MOVE.B	#$40,3(A0)
	BRA.S	ftuskip

VolumeDown
	SUBQ.B	#1,3(A0)
	BTST	#2,$DFF016
	BNE.S	vodoskp
	SUB.B	#15,3(A0)
vodoskp	TST.B	3(A0)
	BPL	ftuskip
	CLR.B	3(A0)
	BRA	ftuskip

SampleLengthGadg
	MOVE.W	#1,SampleLengthFlag
	MOVE.L	SongDataPtr,A0
	LEA	12(A0),A0
	MOVEQ	#0,D0
	MOVE.W	InsNum,D0
	BEQ	ShowSampleInfo
	MULU	#30,D0
	ADD.L	D0,A0
	TST.W	UpOrDown
	BMI.S	SampleLengthDown
SampleLengthUp
	ADDQ.W	#1,(A0)
	BTST	#2,$DFF016
	BNE.S	sluskip
	CMP.W	#$7FF0,(A0)
	BHS.S	sluskip
	ADDQ.W	#7,(A0)
sluskip	CMP.W	#$7FFF,(A0)
	BLO	ShowSampleInfo
	MOVE.W	#$7FFF,(A0)
	BRA	ShowSampleInfo
 
SampleLengthDown
	MOVE.W	4(A0),D0
	ADD.W	6(A0),D0
	SUBQ.W	#1,(A0)
	BTST	#2,$DFF016
	BNE.S	sldskip
	SUBQ.W	#7,(A0)
sldskip	BMI.S	sldskip2
	CMP.W	(A0),D0
	BLS	ShowSampleInfo
	MOVE.W	D0,(A0)
	BRA	ShowSampleInfo
sldskip2
	CLR.W	(A0)
	BRA	ShowSampleInfo

SampleLengthFlag dc.w 0

CheckSampleLength
	TST.W	SampleLengthFlag
	BEQ	Return1
	CLR.W	SampleLengthFlag
	MOVEQ	#0,D0
	MOVE.W	InsNum,D0
	BEQ	Return1
	MOVE.L	SongDataPtr,A0
	LEA	12(A0),A0
	MOVE.L	D0,D1
	LSL.W	#2,D1
	MULU	#30,D0
	ADD.L	D0,A0
	LEA	SamplePtrs,A1
	LEA	(A1,D1.W),A1
	MOVE.L	A0,PlaySamPtr
	MOVE.L	A1,RealSamPtr
	MOVE.L	124(A1),D0
	LSR.L	#1,D0
	MOVE.W	(A0),D1
	CMP.W	D0,D1
	BHI.S	ItsTooMuch
	RTS

ItsTooMuch
	LEA	AddWorkSpaceText(PC),A0
	BSR	AreYouSure
	BNE.S	RestoreLength
	BSR	TurnOffVoices
	MOVE.L	PlaySamPtr,A0
	MOVEQ	#0,D0
	MOVE.W	(A0),D0
	LSL.L	#1,D0
	MOVE.L	D0,SamAllocLen
	MOVE.L	#MEMF_CHIP!MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,SamAllocPtr
	BEQ.S	RestoreLength
	MOVE.L	D0,A1
	MOVE.L	RealSamPtr,A0
	MOVE.L	(A0),D0
	BEQ.S	nosamth
	MOVE.L	D0,A2
	MOVE.L	124(A0),D1
	BEQ.S	nosamth
	SUBQ.L	#1,D1
cpsalop	MOVE.B	(A2)+,(A1)+
	DBRA	D1,cpsalop
	MOVE.L	(A0),A1
	MOVE.L	124(A0),D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
nosamth	MOVE.L	RealSamPtr,A0
	MOVE.L	SamAllocPtr,(A0)
	MOVE.L	SamAllocLen,124(A0)
	BSR	ShowSampleInfo
	BSR	RedrawSample
	BRA	WaitForButtonUp

RestoreLength
	MOVE.L	PlaySamPtr,A0
	MOVE.L	RealSamPtr,A1
	MOVE.L	124(A1),D0
	LSR.L	#1,D0
	MOVE.W	D0,(A0)
	BRA	ShowSampleInfo

AddWorkSpaceText dc.b 'add workspace ?',0
PlaySamPtr  dc.l 0
RealSamPtr  dc.l 0
SamAllocPtr dc.l 0
SamAllocLen dc.l 0

RepeatGadg
	MOVE.L	SongDataPtr,A0
	LEA	12(A0),A0
	MOVEQ	#0,D0
	MOVE.W	InsNum,D0
	BEQ	ShowSampleInfo
	MULU	#30,D0
	ADD.L	D0,A0
	TST.W	UpOrDown
	BMI.S	RepeatDown
RepeatUp
	ADDQ.W	#1,4(A0)
	BTST	#2,$DFF016
	BNE.S	ruskip
	ADDQ.W	#7,4(A0)
ruskip	MOVE.W	(A0),D0		;   Length
	BEQ.S	ruskip2
	SUB.W	6(A0),D0	; - RepLen
	CMP.W	4(A0),D0
	BHI.S	ruskip2
	MOVE.W	D0,4(A0)
ruskip2	BSR	ShowSampleInfo
	BSR	UpdateRepeats
	BRA	SetLoopSprites2

RepeatDown
	SUBQ.W	#1,4(A0)
	BTST	#2,$DFF016
	BNE.S	rdskip
	SUBQ.W	#7,4(A0)
rdskip	TST.W	4(A0)
	BPL.S	ruskip2
	CLR.W	4(A0)
	BRA.S	ruskip2


RepLenGadg
	MOVE.L	SongDataPtr,A0
	LEA	12(A0),A0
	MOVEQ	#0,D0
	MOVE.W	InsNum,D0
	BEQ	ShowSampleInfo
	MULU	#30,D0
	ADD.L	D0,A0
	TST.W	UpOrDown
	BMI.S	RepLenDown
RepLenUp
	ADDQ.W	#1,6(A0)
	BTST	#2,$DFF016	; Check Right Mousebutton
	BNE.S	rluskip
	ADDQ.W	#7,6(A0)
rluskip	MOVE.W	(A0),D0
	BEQ.S	ruskip2
	SUB.W	4(A0),D0
	CMP.W	6(A0),D0
	BHI.S	ruskip2
	BRA.S	rldskip
 
RepLenDown
	MOVEQ	#0,D0
	MOVE.W	6(A0),D0
	SUBQ.L	#1,D0
	BTST	#2,$DFF016
	BNE.S	rldskip
	SUBQ.L	#7,D0
rldskip	CMP.L	#1,D0
	BGE.S	rldskp2
	MOVEQ	#1,D0
rldskp2	MOVE.W	D0,6(A0)
	BRA	ruskip2

Wait_4000
	MOVE.W	#$4000,D0
	TST.B	HHFlag
	BEQ.S	wl_loop
	MOVE.W	#$FFFF,D0
wl_lop1	DBRA	D0,wl_lop1
	MOVE.W	#$A000,D0
wl_loop	DBRA	D0,wl_loop
	RTS

UpdateRepeats
	LEA	audchan1temp,A1
	LEA	$DFF0A0,A2
	LEA	ScopeInfo,A3
	BSR.S	upre2
	LEA	audchan2temp,A1
	LEA	$DFF0B0,A2
	LEA	ScopeInfo+20,A3
	BSR.S	upre2
	LEA	audchan3temp,A1
	LEA	$DFF0C0,A2
	LEA	ScopeInfo+40,A3
	BSR.S	upre2
	LEA	audchan4temp,A1
	LEA	$DFF0D0,A2
	LEA	ScopeInfo+60,A3
upre2	MOVE.W	InsNum,D0
	CMP.B	n_samplenum(A1),D0
	BNE	Return1
	MOVE.L	n_start(A1),A1
	MOVEQ	#0,D0
	MOVE.W	4(A0),D0	; repeat
	ADD.L	D0,A1
	ADD.L	D0,A1
	MOVE.L	A1,(A2)
	MOVE.W	6(A0),D0
	MOVE.W	D0,4(A2)	; replen
	MOVE.L	A1,ns_repeatptr(A3)
	ADD.L	D0,A1
	ADD.L	D0,A1
	MOVE.L	A1,ns_rependptr(A3)
	RTS

SetPatternPos
	MOVE.L	PatternPosition,D0
	LSR.L	#4,D0
	BRA.S	ssppskip
 
SetScrPatternPos
	MOVE.W	ScrPattPos,D0
ssppskip
	TST.W	SamScrEnable
	BNE	Return1
	MOVE.W	D0,PlayFromPos
	MULU	#7*40,D0
	MOVE.L	TextBplPtr,A0
	LEA	$15B8(A0),A0
	ADD.L	A0,D0
	MOVE.L	CopListBpl4Ptr(PC),A1
	MOVE.W	D0,6(A1)
	SWAP	D0
	MOVE.W	D0,2(A1)
	RTS

PlayFromPos	dc.w 0

WantedPattGadg
	TST.W	SamScrEnable
	BNE	CheckSamGadgets
TypeInWantedPatt
	CMP.W	#138,D1
	BHI.S	tiwp2
	CMP.W	#25,D0
	BHI	TempoGadg
tiwp2	CMP.L	#'patp',RunMode	; not if a song is playing... 
	BEQ	Return1
	CLR.B	RawKeyCode
	MOVE.W	#12,LineCurX
	MOVE.W	#$0086,LineCurY
	MOVE.W	#5121,TextOffset
	BSR	GetDecByte
	TST.W	AbortDecFlag
	BNE.S	twexit
	MOVE.B	D0,PatternNumber+3
	CMP.L	#63,PatternNumber
	BLS.S	twexit
	MOVE.L	#63,PatternNumber
twexit	BRA	RedrawPattern

GetKey0_9
	BTST	#2,$DFF016
	BEQ.S	gk_ret
	MOVEQ	#0,D0
	BSR	DoKeyBuffer
	MOVE.B	RawKeyCode,D0
	BEQ.S	GetKey0_9
	CLR.B	RawKeyCode
	CMP.B	#68,D0
	BEQ.S	gk_ret
	CMP.B	#69,D0
	BEQ.S	gk_ret
	CMP.B	#10,D0
	BEQ.S	gk_end
	BHI.S	GetKey0_9
	CMP.B	#1,D0
	BLO.S	GetKey0_9
	MOVE.L	D0,D1
	RTS

gk_end	MOVEQ	#0,D1
	RTS

gk_ret	MOVEQ	#68,D1
	RTS

ShowOneDigit
	ADD.B	#'0',D1
	MOVE.B	D1,NumberText
	CLR.W	D1
	SWAP	D1
	MOVE.W	#1,TextLength
	MOVE.L	#NumberText,ShowTextPtr
	BSR	ShowText
	CLR.L	NumberText
	CLR.W	WordNumber
	RTS

;---- Get Text Line ----

GetTextLine
	MOVEQ	#0,D0
	MOVE.W	A4,D0
	DIVU	#40,D0
	ADDQ.W	#5,D0
	MOVE.W	D0,LineCurY
	SWAP	D0
	LSL.W	#3,D0
	ADDQ.W	#4,D0
	MOVE.W	D0,LineCurX
	BSR	UpdateLineCurPos
	MOVE.L	ShowTextPtr,DSTPtr
	CLR.L	DSTOffset
	CLR.L	DSTPos
	MOVE.W	#1,GetLineFlag
	MOVE.L	A4,A5
	MOVE.W	LineCurX,D5
	CLR.B	RawKeyCode
	BSR	UpdateText
WaitForKey
	BTST	#2,$DFF016
	BEQ	AbortGetLine
	BTST	#6,$BFE001
	BEQ	LineClicked
	BSR	DoKeyBuffer
	MOVEQ	#0,D1
	MOVE.B	RawKeyCode,D1
	BEQ.S	WaitForKey
	CMP.B	#78,D1
	BEQ	MoveCharRight
	CMP.B	#79,D1
	BEQ	MoveCharLeft
	CMP.B	#70,D1
	BEQ	DeleteChar
	CMP.B	#65,D1
	BEQ	BackspaceChar
	CMP.B	#68,D1
	BEQ	GetLineReturn
	CMP.B	#69,D1
	BEQ	GetLineReturn
	BTST	#7,D1
	BNE.S	WaitForKey
	LEA	UnshiftedKeymap,A4
	TST.W	ShiftKeyStatus
	BEQ.S	gtlskip
	LEA	ShiftedKeymap,A4
gtlskip	AND.W	#$007F,D1
	CMP.B	#64,D1
	BHI.S	WaitForKey
	MOVE.B	(A4,D1.W),D1
	BEQ.S	WaitForKey
	TST.B	EnterTextFlag
	BEQ.S	TextLineKey
	CMP.B	#'0',D1
	BLO	WaitForKey
	CMP.B	#'f',D1
	BHI	WaitForKey
	CMP.B	#'a',D1
	BHS.S	TextLineKey
	CMP.B	#'9',D1
	BHI	WaitForKey
TextLineKey
	CMP.L	TextEndPtr,A6
	BEQ	WaitForKey
	MOVE.L	TextEndPtr,A4
tlkloop	MOVE.B	-(A4),1(A4)
	CMP.L	A4,A6
	BNE.S	tlkloop
	MOVE.L	TextEndPtr,A4
	CLR.B	(A4)
	MOVE.B	D1,(A6)+
	BSR	PosMoveRight
	BSR	UpdateText
	CLR.B	RawKeyCode
	BRA	WaitForKey

LineClicked
	MOVE.W	MouseY,D1
	SUB.W	LineCurY,D1
	CMP.W	#2,D1
	BGT	GetLineReturn
	CMP.W	#-8,D1
	BLT	GetLineReturn
	MOVE.W	MouseX,D1
	SUB.W	LineCurX,D1
	ADDQ.W	#4,D1
	ASR.W	#3,D1
	BEQ	WaitForKey
	BPL.S	linclri

	CMP.L	DSTPtr(PC),A6
	BEQ	WaitForKey
	SUBQ.L	#1,A6
	BSR	PosMoveLeft
upwake2	BSR	UpdateText
	BRA	WaitForKey

linclri	CMP.L	TextEndPtr,A6
	BEQ	WaitForKey
	TST.B	(A6)
	BEQ	WaitForKey
	ADDQ.L	#1,A6
	BSR	PosMoveRight
	BRA.S	upwake2

PosMoveRight
	MOVE.L	DSTPos,D0
	MOVEQ	#0,D1
	MOVE.W	TextLength,D1
	TST.B	EnterTextFlag
	BNE.S	pmrskip
	SUBQ.W	#1,D1
pmrskip	CMP.L	D1,D0
	BLO.S	posrok
	ADDQ.L	#1,DSTOffset
	BRA	UpdateLineCurPos
posrok	ADDQ.L	#1,DSTPos
	ADDQ.W	#8,LineCurX
	BRA	UpdateLineCurPos

PosMoveLeft
	TST.L	DSTPos
	BNE.S	poslok
	SUBQ.L	#1,DSTOffset
	BRA	UpdateLineCurPos	
poslok	SUBQ.L	#1,DSTPos
	SUBQ.W	#8,LineCurX
	BRA	UpdateLineCurPos

BackspaceChar
	CMP.L	DSTPtr(PC),A6
	BEQ	WaitForKey
	SUBQ.L	#1,A6
	MOVE.L	A6,A4
dobaloop
	MOVE.B	1(A4),(A4)+
	CMP.L	TextEndPtr,A4
	BNE.S	dobaloop
	BSR	PosMoveLeft
upwake	BSR	UpdateText
	BSR	Wait_4000
	BSR	Wait_4000
	BSR	Wait_4000
	BRA	WaitForKey
 
DeleteChar
	MOVE.L	A6,A4
dechloop
	MOVE.B	1(A4),(A4)+
	CMP.L	TextEndPtr,A4
	BLO.S	dechloop
	BRA.S	upwake
 
MoveCharRight
	CMP.L	TextEndPtr,A6
	BEQ	WaitForKey
	TST.B	(A6)
	BEQ	WaitForKey
	ADDQ.L	#1,A6
	BSR	PosMoveRight
	BRA.S	upwake

MoveCharLeft
	CMP.L	DSTPtr(PC),A6
	BEQ	WaitForKey
	SUBQ.L	#1,A6
	BSR	PosMoveLeft
	BRA.S	upwake

GetLineReturn
	MOVE.L	DSTPtr,A6
	CMP.B	#1,EnterTextFlag
	BNE.S	gtl_rskip
	TST.B	DiskNumText2
	BEQ	WaitForKey
gtl_rskip
	CMP.B	#3,EnterTextFlag
	BNE.S	gtl_rskip2
	TST.B	SndDiskNum1
	BEQ	WaitForKey
gtl_rskip2
	MOVE.L	A6,A4
dlrloop	TST.B	(A4)+
	BNE.S	dlrloop
	SUBQ.L	#1,A4
dlrloop2
	CMP.L	TextEndPtr,A4
	BHS.S	dlrexit
	CLR.B	(A4)+
	BRA.S	dlrloop2

dlrexit	CLR.W	LineCurX
	MOVE.W	#270,LineCurY
	BSR	UpdateLineCurPos
	CLR.W	GetLineFlag
	MOVE.B	RawKeyCode,MixChar
	CLR.B	RawKeyCode
	CLR.L	DSTOffset
	BSR.S	UpdateText
	BRA	WaitForButtonUp

AbortGetLine
	MOVE.L	DSTPtr,A6
	MOVE.L	A6,A4
clliloop
	CLR.B	(A4)+
	CMP.L	TextEndPtr,A4
	BNE.S	clliloop
	BSR.S	UpdateText
	BRA	GetLineReturn
 
UpdateText
	MOVE.W	A5,TextOffset
	MOVE.L	DSTPtr,A0
	ADD.L	DSTOffset,A0
	BRA	ShowText2

DSTPtr		dc.l	0
DSTPos		dc.l	0
DSTOffset	dc.l	0

;----

TypeInSongName
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	CLR.L	EditMode
	MOVE.L	SongDataPtr,A6
	MOVE.L	A6,TextEndPtr
	MOVE.L	A6,ShowTextPtr
	ADD.L	#19,TextEndPtr
	MOVE.W	#20,TextLength
	MOVE.W	#4133,A4
	BSR	GetTextLine
	CLR.L	TextEndPtr
	BRA	RestorePtrCol

CheckSmplNamOrLoad
	CMP.W	#287,MouseX2
	BHS	LoadNamedSample
TypeInSampleName
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	CLR.L	EditMode
	MOVE.L	SongDataPtr,A6
	LEA	-10(A6),A6
	MOVE.W	InsNum,D7
	BNE.S	tisnskip
	MOVE.W	LastInsNum,D7
tisnskip
	MULU	#30,D7
	ADD.L	D7,A6
	MOVE.L	A6,TextEndPtr
	MOVE.L	A6,ShowTextPtr
	ADD.L	#21,TextEndPtr
	MOVE.W	#22,TextLength
	MOVE.W	#4573,A4
	BSR	GetTextLine
	CLR.L	TextEndPtr
	BRA	RestorePtrCol

LoadSample
	TST.W	InsNum
	BEQ	NotSampleNull
	BSR	StorePtrCol
	LEA	SamplePath2,A0
	BSR	CopyPath
	LEA	DirInputName,A0
	MOVEQ	#DirNameLength-1,D0
lsloop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,lsloop
	MOVE.L	SongDataPtr,A0
	MOVE.W	InsNum,D0
	MULU	#30,D0
	LEA	-10(A0),A0
	ADD.L	D0,A0
	LEA	DirInputName,A1
	MOVEQ	#21,D0
lsloop2	MOVE.B	(A1)+,(A0)+
	DBRA	D0,lsloop2
	BSR	SetDiskPtrCol
	MOVE.L	#FileName,D1
	BSR	ExamineAndAlloc
	BEQ	ErrorRestoreCol
	MOVE.L	#FileName,D1
	BRA	lnssec2

CheckForIFF2 ; load loop
	MOVEQ	#-1,D2
	BRA.S	ciff2
CheckForIFF
	MOVEQ	#0,D2
ciff2	MOVEQ	#0,D1
	CMP.L	#'FORM',(A0)
	BNE.S	wiskip
	CMP.L	#'8SVX',8(A0)
	BNE.S	wiskip
	MOVE.L	A0,A2
	MOVE.L	A1,D0
	ADD.L	A0,A1
	TST.L	D2
	BEQ.S	cfiloop
	BSR.S	CheckIFFLoop
cfiloop	CMP.L	#'BODY',(A0)
	BEQ.S	wasiff
	ADDQ.L	#2,A0
	CMP.L	A1,A0
	BLS.S	cfiloop
	RTS

WasIFF	ADDQ.L	#8,A0
	ADD.L	A2,D0
	SUB.L	A0,D0
wiloop	MOVE.B	(A0)+,(A2)+
	CMP.L	A1,A0
	BLS.S	wiloop
	MOVE.L	SampleInstrSave(PC),A3
	LSR.W	#1,D0
	MOVE.W	22(A3),D1
	SUB.W	D0,D1
	MOVE.W	D0,22(A3)
wiskip	LEA	SampleLengthAdd(PC),A3
	MOVE.W	InsNum,D0
	LSL.W	#1,D0
	MOVE.W	D1,(A3,D0.W)
	RTS

CheckIFFLoop
	TST.B	IFFLoopFlag
	BEQ	Return2
	MOVEM.L	A0/D0,-(SP)
cilloop	CMP.L	#'VHDR',(A0)
	BEQ.S	wasvhdr
	ADDQ.L	#2,A0
	CMP.L	A1,A0
	BLS.S	cilloop
cilend	MOVEM.L	(SP)+,A0/D0
	RTS

wasvhdr	MOVE.L	SampleInstrSave(PC),A3
	MOVE.L	12(A0),D0
	BEQ.S	cilend
	LSR.W	#1,D0
	MOVE.W	D0,28(A3)
	MOVE.L	8(A0),D0
	BNE.S	wsvhdr2
	MOVEQ	#2,D0
wsvhdr2	LSR.W	#1,D0
	MOVE.W	D0,26(A3)
	BRA.S	cilend


SampleInstrSave	dc.l	0
SampleLengthAdd	dcb.w	32,0

ExamineAndAlloc
	MOVEQ	#-2,D2
	MOVE.L	DOSBase,A6
	JSR	LVOLock(A6)
	MOVE.L	D0,FileLock
	BEQ	CantFindFile
	MOVE.L	D0,D1
	MOVE.L	#FileInfoBlock,D2
	JSR	LVOExamine(A6)
	TST.L	FIB_EntryType
	BPL	CantExamFile
	MOVE.L	FileLock,D1
	JSR	LVOUnLock(A6)
	MOVE.L	FIB_FileSize,D0
	BEQ	FileIsEmpty
	BSR	TurnOffVoices
	BSR	FreeSample
	MOVE.L	FIB_FileSize,D0
	CMP.L	#$FFFE,D0
	BLS.S	exalloc
	MOVE.L	#$FFFE,D0
exalloc	LEA	SamplePtrs,A4
	MOVE.W	InsNum,D1
	LSL.W	#2,D1
	ADD.W	D1,A4
	MOVE.L	D0,124(A4)
	MOVE.L	D0,DiskDataLength
	MOVE.L	4.W,A6
	MOVE.L	#MEMF_CHIP!MEMF_CLEAR,D1
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,DiskDataPtr
	MOVE.L	D0,(A4)
	BEQ	OutOfMemErr
	LEA	LoadingSampleText(PC),A0
	BSR	ShowStatusText
	MOVE.L	SongDataPtr,A0
	MOVE.W	InsNum,D0
	MULU	#30,D0
	LEA	-10(A0),A0
	ADD.L	D0,A0
	MOVE.L	A0,SampleInstrSave
	MOVE.L	DiskDataLength,D0
	LSR.L	#1,D0
	MOVE.W	D0,22(A0)
	MOVE.L	#$00400000,24(A0)
	MOVE.W	#1,28(A0)
	BSR	ShowSampleInfo
	MOVEQ	#-1,D0
	RTS

LoadNamedSample
	TST.W	InsNum
	BEQ	NotSampleNull
	BSR	StorePtrCol
	BSR	CreateSampleName
	BSR	SetDiskPtrCol
	MOVE.L	FileNamePtr,D1
	BSR	ExamineAndAlloc
	BEQ	Return2
	MOVE.L	FileNamePtr,D1
lnssec2	MOVE.L	DOSbase(PC),A6
	MOVE.L	#1005,D2
	JSR	LVOOpen(A6)
	MOVE.L	D0,D7
	BEQ	CantOpenFile
	MOVE.L	D0,D1
	MOVE.L	DiskDataPtr,D2
	MOVE.L	DiskDataLength,D3
	JSR	LVORead(A6)
	MOVE.L	D7,D1
	JSR	LVOClose(A6)
	MOVE.L	DiskDataPtr,A0
	MOVE.L	DiskDataLength,A1
	BSR	CheckForIFF2
	BSR	ValidateLoops
	BSR	ShowSampleInfo
	MOVE.L	DiskDataPtr,A0
	CLR.W	(A0)
	BSR	RedrawSample
	CLR.L	SavSamInf
	BSR	ShowAllRight
	BRA	RestorePtrCol

CreateSampleName
	LEA	SampleFileName,A0
	MOVEQ	#27,D0
csnloop	CLR.B	(A0)+
	DBRA	D0,csnloop
	MOVE.L	SongDataPtr,A0
	MOVE.W	InsNum,D0
	MULU	#30,D0
	LEA	-10(A0),A0
	ADD.L	D0,A0
	MOVE.L	A0,SampleInstrSave
	MOVEQ	#0,D0
	MOVE.W	22(A0),D0
	ADD.L	D0,D0
	MOVE.L	D0,DiskDataLength
	MOVE.L	D0,IFFBODY+4
	ADD.L	#IFFEND-IFFFORM-8,D0
	MOVE.L	D0,IFFFORM+4

	MOVEQ	#0,D0
	MOVE.W	22(A0),D0
	ADD.L	D0,D0
	MOVEQ	#0,D1
	MOVE.W	28(A0),D1
	CMP.W	#1,D1
	BLS.S	csnskp2
	ADD.L	D1,D1
	MOVEQ	#0,D0
	MOVE.W	26(A0),D0
	ADD.L	D0,D0
	BRA.S	csnskp3
csnskp2	MOVEQ	#0,D1
csnskp3	MOVE.L	D0,IFFVHDR+8
	MOVE.L	D1,IFFVHDR+12

	LEA	SampleFileName,A1
	LEA	IFFNAME+8,A2
	MOVEQ	#21,D0
csnloop2
	MOVE.B	(A0),(A1)+
	MOVE.B	(A0)+,(A2)+
	DBRA	D0,csnloop2
	MOVE.L	#SampleFileName,D1
	MOVE.L	D1,FileNamePtr
	BSR.S	FindColon
	BEQ.S	CheckOverride
	LEA	SamplePath2,A0
	BSR	CopyPath
	MOVE.L	#SampleFileName,A0
csnloop3
	MOVE.B	(A0)+,(A1)+
	BNE.S	csnloop3
	MOVE.L	#FileName,FileNamePtr
	RTS

FindColon
	MOVE.L	D1,A0
fcloop	MOVE.B	(A0)+,D0
	BEQ.S	FindColonFail
	CMP.B	#':',D0
	BEQ.S	FindColonSuccess
	BRA.S	fcloop

FindColonFail
	MOVEQ	#-1,D0
	RTS

FindColonSuccess
	MOVEQ	#0,D0
	RTS

CheckOverride
	TST.B	OverrideFlag
	BEQ	Return2
	MOVE.L	#SampleFileName,A0
	MOVE.L	A0,A1
	LEA	21(A1),A1
	MOVE.L	A1,A2
chkovlp	MOVE.B	-(A1),D0
	CMP.B	#':',D0
	BEQ.S	chkovok
	CMP.B	#'/',D0
	BEQ.S	chkovok
	MOVE.L	A1,A2
	CMP.L	A0,A1
	BHI.S	chkovlp
chkovok	LEA	SamplePath2,A0
	BSR	CopyPath
chkovl2	MOVE.B	(A2)+,(A1)+
	BNE.S	chkovl2
	MOVE.L	#FileName,FileNamePtr
	RTS

ValidateLoops
	MOVE.L	SongDataPtr,A0
	LEA	20(A0),A0
	MOVEQ	#30,D0
valolop	MOVE.W	22(A0),D1
	MOVE.W	26(A0),D2
	MOVE.W	28(A0),D3
	CMP.W	D1,D2
	BHS.S	valosk1
	ADD.W	D2,D3
	CMP.W	D1,D3
	BHI.S	valosk2
valoque	TST.W	28(A0)
	BNE.S	valosk3
	MOVE.W	#1,28(A0)
valosk3	LEA	30(A0),A0
	DBRA	D0,valolop
	RTS
valosk2	SUB.W	D2,D1
	MOVE.W	D1,28(A0)
	BRA.S	valoque
valosk1	MOVEQ	#1,D1
	MOVE.L	D1,26(A0)
	BRA.S	valoque

LoadPreset
	CLR.B	RawKeyCode
	TST.W	InsNum
	BEQ	NotSampleNull
	BSR	TurnOffVoices
	BSR	FreeSample
	BSR	CreateSampleName
	MOVE.L	FileNamePtr,A0
	TST.B	(A0)
	BEQ	Return2
	MOVE.L	DiskDataLength(PC),D0
	BEQ	Return2
	MOVE.L	DiskDataLength(PC),D0
	BSR	AllocSample
	MOVE.L	DiskDataPtr(PC),D0
	BEQ.S	loprerr
	BSR	ShowSampleInfo
	BSR	DoLoadData
	BSR	ShowAllRight
	MOVE.L	DiskDataPtr(PC),A0
	MOVE.L	DiskDataLength(PC),A1
	BSR	CheckForIFF
	BSR	ValidateLoops
	BSR	ShowSampleInfo
	MOVE.L	DiskDataPtr(PC),A0
	CLR.W	(A0)
	BSR	RedrawSample
	MOVE.W	#1,UpdateFreeMem
	RTS

loprerr	BSR	StorePtrCol
	BSR	OutOfMemErr
	BSR	RestorePtrCol
	BRA	RedrawSample

AllocSample
	MOVE.L	D0,-(SP)
	MOVE.L	#MEMF_CHIP!MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,DiskDataPtr
	LEA	SamplePtrs(PC),A0
	MOVE.W	InsNum(PC),D1
	LSL.W	#2,D1
	LEA	(A0,D1.W),A0
	MOVE.L	DiskDataPtr(PC),(A0)
	MOVE.L	(SP)+,124(A0)
	RTS

FreeSample
	LEA	SamplePtrs(PC),A0
	MOVE.W	InsNum(PC),D0
	BEQ	Return2
	LSL.W	#2,D0
	LEA	(A0,D0.W),A0
	MOVE.L	(A0),D1
	BEQ	Return2
	CLR.L	(A0)
	MOVE.L	124(A0),D0
	CLR.L	124(A0)
	MOVE.L	D1,A1
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	RTS

NotSampleNull
	LEA	NotSample0Text(PC),A0
	BSR	ShowStatusText
	BRA	SetErrorPtrCol

NotSample0Text	dc.b 'not sample 0 !',0,0

;----

DeleteSong
	LEA	DeleteSongText,A0
	BSR	AreYouSure
	BNE	Return2
	LEA	DeletingSongText(PC),A0
	BSR	ShowStatusText
	LEA	SongsPath2,A0
	BSR	CopyPath
	LEA	DirInputName,A0
	MOVEQ	#DirNameLength-1,D0
dsloop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,dsloop
	MOVE.W	#1,Action
Delete3	MOVE.L	#FileName,FileNamePtr
	MOVE.L	DOSbase(PC),A6
	MOVE.L	FileNamePtr(PC),D1
	MOVE.L	D1,A0
	JSR	LVODeleteFile(A6)
	BSR	ClearFileNames
	BSR	ShowAllRight
	BSR	SetNormalPtrCol
	BSR	StorePtrCol
	BRA	DoAutoDir
	
DeleteModule
	LEA	DeleteModuleText,A0
	BSR	AreYouSure
	BNE	Return2
	LEA	DeletingModuleText(PC),A0
	BSR	ShowStatusText
	LEA	ModulesPath2,A0
	BSR	CopyPath
	LEA	DirInputName,A0
	MOVEQ	#DirNameLength-1,D0
dmdloop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,dmdloop
	MOVE.W	#3,Action
	BRA.S	Delete3

DeleteSample
	LEA	DeleteSampleText,A0
	BSR	AreYouSure
	BNE	Return2
	LEA	DeletingSampleText(PC),A0
	BSR	ShowStatusText
	LEA	SamplePath2,A0
	BSR	CopyPath
	LEA	DirInputName,A0
	MOVEQ	#DirNameLength-1,D0
dsaloop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,dsaloop
	MOVE.W	#5,Action
	BRA	Delete3

RenameFile
	LEA	RenamingFileText(PC),A0
	BSR	ShowStatusText
	LEA	DirInputName,A0
	LEA	NewInputName,A1
	MOVEQ	#23,D0
rnfloop	MOVE.B	(A0)+,(A1)+
	BNE.S	rnfskip
	SUBQ.L	#1,A0
rnfskip	DBRA	D0,rnfloop
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	LEA	NewInputName,A6
	MOVE.L	A6,ShowTextPtr
	MOVE.L	A6,TextEndPtr
	ADD.L	#23,TextEndPtr
	MOVE.W	#24,TextLength
	MOVE.W	FileNameScrollPos+2,D0
	MULU	#240,D0
	MOVE.W	#1888,A4
	ADD.W	D0,A4
	BSR	GetTextLine
	TST.B	NewInputName
	BEQ.S	rnfend
	CMP.B	#69,MixChar
	BEQ.S	rnfend

	MOVE.L	PathPtr,A0
	BSR	CopyPath
	LEA	NewInputName,A0
	MOVEQ	#23,D0
rnfloop2
	MOVE.B	(A0)+,(A1)+
	DBRA	D0,rnfloop2

	LEA	FileName,A0
	LEA	NewFileName,A1
rnfloop3
	MOVE.B	(A0)+,(A1)+
	BNE.S	rnfloop3

	MOVE.L	PathPtr,A0
	BSR	CopyPath
	LEA	DirInputName,A0
	MOVEQ	#DirNameLength-1,D0
rnfloop4
	MOVE.B	(A0)+,(A1)+
	DBRA	D0,rnfloop4

	MOVE.L	#FileName,D1
	MOVE.L	#NewFileName,D2
	MOVE.L	DOSbase(PC),A6
	JSR	LVORename(A6)

rnfend	BSR	ClearFileNames
	CLR.W	Action
	BSR	RestorePtrCol
	BRA	ShowAllRight

LoadSong
	MOVE.W	#1,LoadInProgress
	BSR	DoClearSong
	BSR	ClrSampleInfo
	LEA	SongsPath2,A0
	BSR	CopyPath
	LEA	DirInputName,A0
	MOVEQ	#DirNameLength-1,D0
losoloop2
	MOVE.B	(A0)+,(A1)+
	DBRA	D0,losoloop2
	MOVE.L	SongDataPtr(PC),DiskDataPtr
	MOVE.L	#FileName,FileNamePtr
	MOVE.L	#70716,DiskDataLength
	LEA	LoadingSongText(PC),A0
	BSR	ShowStatusText
	BSR	DoLoadData
	BEQ	lososkip3
	MOVE.L	SongDataPtr(PC),A0
	CMP.L	#'PACK',(A0)
	BNE.S	lososkip
	MOVE.L	4(A0),CrunchedSongLength
	MOVE.L	8(A0),RealSongLength
	MOVE.L	SongDataPtr(PC),D0
	ADD.L	#70716,D0
	SUB.L	RealSongLength(PC),D0
	MOVE.L	D0,EndOfSongPtr
	LEA	DecrunchingText(PC),A0
	BSR	ShowStatusText
	BSR	Decruncher
	BSR	ShowAllRight
lososkip
	MOVE.L	SongDataPtr(PC),A0
	CMP.L	#'M.K.',sd_mahokakt(A0)	;  M.K. again...
	BEQ.S	lososkip2	
	BSR	MahoneyAndKaktus ; Was 16 instr, so move up...
lososkip2
	LEA	LoadingSongText(PC),A0
	BSR	ShowStatusText
	BSR	CheckAbort
	BEQ.S	lososkip3
	TST.B	AutoExitFlag
	BEQ.S	NoSongAutoExit
	BSR	ExitFromDir
NoSongAutoExit
	CLR.L	PatternNumber
	CLR.L	CurrPos
	BSR	RedrawPattern
	CLR.W	ScrPattPos
	BSR	SetScrPatternPos
	BSR	SortDisks
	LEA	SampleSortList(PC),A0
	MOVEQ	#30,D0
losoloop3
	TST.B	NosamplesFlag
	BNE.S	lososkip3
	BSR	CheckAbort
	BEQ.S	lososkip3
	MOVE.W	InsNum(PC),TuneUp
	BSR	DoShowFreeMem
	MOVE.L	(A0)+,D1
	MOVE.W	D1,InsNum
	MOVEM.L	D0/D1/A0,-(SP)
	BSR	LoadPreset
	BSR	ShowSampleInfo
	MOVEM.L	(SP)+,D0/D1/A0
	DBRA	D0,losoloop3
lososkip3
	MOVE.W	#1,InsNum
	MOVE.L	#6,CurrSpeed
	CLR.W	LoadInProgress
	BSR	ShowAllRight
	BSR	SetNormalPtrCol
	BSR	DoShowFreeMem
	BSR	CheckInstrLengths
	BSR	ShowSampleInfo
	BRA	RedrawSample

SortDisks
	MOVEM.L	D0-D4/A0-A1,-(SP)
	MOVE.L	SongDataPtr(PC),A0
	LEA	23(A0),A0
	LEA	SampleSortList(PC),A1
	MOVEQ	#1,D0
losoloop4
	MOVE.B	(A0)+,(A1)+	; ST-(0)1
	MOVE.B	(A0),(A1)+	; ST-0(1)
	MOVE.W	D0,(A1)+	; insnum
	LEA	29(A0),A0
	ADDQ.L	#1,D0
	CMP.L	#32,D0
	BLO.S	losoloop4
losoloop5
	CLR.W	MoreInstrFlag
	LEA	SampleSortList(PC),A0
	MOVEQ	#29,D2
losoloop6
	MOVE.W	(A0),D0
	MOVE.W	4(A0),D1
	CMP.W	D0,D1 ; if next disk greater
	BHS.S	loso2_2
	MOVE.W	#1,MoreInstrFlag
	MOVE.L	(A0),D3 ; swap disks
	MOVE.L	4(A0),D4
	MOVE.L	D4,(A0)
	MOVE.L	D3,4(A0)
loso2_2	ADDQ.L	#4,A0
	DBRA	D2,losoloop6
	TST.W	MoreInstrFlag
	BNE.S	losoloop5
	LEA	SampleSortList(PC),A0
	MOVEQ	#30,D0
losoloop7
	CLR.W	(A0)
	ADDQ.L	#4,A0
	DBRA	D0,losoloop7
	MOVEM.L	(SP)+,D0-D4/A0-A1
	RTS

MahoneyAndKaktus
	LEA	Loadas31text(PC),A0
	BSR	AreYouSure
	BEQ.S	putmk
	MOVE.L	SongDataPtr(PC),A0	; M.K. 32 samples format!
	LEA	466(A0),A1
	ADD.L	#66006,A0
makloop	MOVE.L	(A0),484(A0)
	CLR.L	(A0)
	SUBQ.L	#4,A0
	CMP.L	A0,A1
	BNE.S	makloop
	MOVE.L	SongDataPtr(PC),A0
	LEA	sd_mahokakt(A0),A1
	LEA	sd_numofpatt(A0),A0
makloop2
	MOVE.W	4(A0),(A0)+
	CMP.L	A0,A1
	BNE.S	makloop2
	MOVE.L	#'M.K.',(A0)	; M.K. again!
	MOVEQ	#0,D0
	RTS

putmk	MOVE.L	SongDataPtr(PC),A0
	MOVE.L	#'M.K.',sd_mahokakt(A0)	;  put M.K.
	MOVEQ	#-1,D0
	RTS

Loadas31Text	dc.b	"Load as 31 instr?",0

CheckInstrLengths
	MOVE.L	SongDataPtr(PC),A0
	ADD.L	#20,A0
	MOVEQ	#30,D1
xilloop	MOVE.W	26(A0),D0
	ADD.W	28(A0),D0
	CMP.W	22(A0),D0
	BLS.S	xilSkip
	MOVE.W	26(A0),D0
	LSR.W	#1,D0	
	MOVE.W	D0,26(A0)
xilskip	ADD.L	#30,A0
	DBRA	D1,xilloop
	RTS	

CheckAbort
	BTST	#2,$DFF016
	BNE	Return2
	MOVEM.L	D0-D7/A0-A6,-(SP)
	LEA	AbortLoadText,A0
	BSR	AreYouSure
	BNE.S	chabno
	MOVEM.L	(SP)+,D0-D7/A0-A6
	MOVEQ	#0,D7
	RTS

chabno	MOVEM.L	(SP)+,D0-D7/A0-A6
	MOVEQ	#-1,D7
	RTS

StopIt	BSR.S	DoStopIt
TurnOffVoices
	MOVEM.L	A0-A1,-(SP)
	MOVE.W	#$000F,$DFF096
	CLR.W	$DFF0A8
	CLR.W	$DFF0B8
	CLR.W	$DFF0C8
	CLR.W	$DFF0D8
	CLR.B	RawKeyCode
	LEA	ScopeInfo,A0
	LEA	BlankSample,A1
	MOVE.L	A1,(A0)
	MOVE.L	A1,20(A0)
	MOVE.L	A1,40(A0)
	MOVE.L	A1,60(A0)
	MOVEM.L	(SP)+,A0-A1
	RTS

DoStopIt
	BSR	SetNormalPtrCol
	CLR.L	EditMode
	CLR.L	RunMode
	CLR.B	PattDelayTime
	CLR.B	PattDelayTime2
	BRA	RestoreEffects2

UsePreset
	BSR	StorePtrCol
	TST.L	PLSTmem
	BEQ.S	upend
	TST.W	InsNum
	BEQ.S	upend
	CLR.B	RawKeyCode
	MOVE.W	CurrentPreset(PC),D0
	SUBQ.W	#1,D0
	MULU	#30,D0
	MOVE.L	PLSTmem(PC),A0
	ADD.L	D0,A0
	MOVE.W	InsNum(PC),D0
	MULU	#30,D0
	MOVE.L	SongDataPtr(PC),A1
	LEA	-10(A1,D0.W),A1
	MOVE.L	A1,A2
	MOVEQ	#29,D0
uploop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,uploop
	MOVE.L	(A2),D0
	AND.L	#$DFDFFFFF,D0
	CMP.L	#$53540000,D0 ;ST__
	BNE.S	upok
	CLR.W	(A2)
	CLR.L	22(A2)
	MOVE.L	#$00000001,26(A2)
upok	BSR	LoadPreset
upend	BSR	ShowSampleInfo
	BRA	RestorePtrCol

;---- Edit ----

Edit	TST.W	SamScrEnable
	BNE	Return2	
	BSR	StopIt
	CLR.B	RawKeyCode
	BSR	SetEditPtrCol
	BSR	SetScrPatternPos
	MOVE.L	#'edit',EditMode
	BRA	WaitForButtonUp

;---- Edit Op. ----

DoEditOp
	CLR.B	RawKeyCode
	CMP.W	#1,CurrScreen
	BNE	Return2
	TST.B	EdEnable
	BEQ.S	EditOp
	ADDQ.B	#1,EdScreen
	CMP.B	#4,EdScreen
	BLO.S	EditOp
	MOVE.B	#1,EdScreen
EditOp	BSR	WaitForButtonUp
	ST	EdEnable
	ST	DisableAnalyzer
	JSR	ClearAnalyzerColors
	BSR	ClearRightArea
	BRA	DrawEditMenu

EdEnable	dc.b	0
EdScreen	dc.b	1

DrawEditMenu
	CMP.B	#1,EdScreen
	BNE.S	demskip
	MOVE.L	#EditOpText1,ShowTextPtr
	LEA	Edit1Data,A0
	MOVE.L	#Edit1Size,D0
	BRA.S	demit
demskip
	CMP.B	#2,EdScreen
	BNE.S	demskip2
	MOVE.L	#EditOpText2,ShowTextPtr
	LEA	Edit2Data,A0
	MOVE.L	#Edit2Size,D0
	BRA.S	demit
demskip2
	CMP.B	#3,EdScreen
	BNE	Return2
	MOVE.L	#EditOpText3,ShowTextPtr
	LEA	Edit3Data,A0
	MOVE.L	#Edit3Size,D0
demit	BSR	Decompact
	BEQ	ExitEditOp
	LEA	SpectrumAnaPos,A0
	MOVEQ	#54,D0
demloop1
	MOVEQ	#24,D1
demloop2
	MOVE.B	1430(A1),10240(A0)
	MOVE.B	(A1)+,(A0)+
	DBRA	D1,demloop2
	LEA	15(A0),A0
	ADDQ.L	#1,A1
	DBRA	D0,demloop1
	BSR	FreeDecompMem
	MOVE.W	#1936,TextOffset
	MOVE.W	#22,TextLength
	BSR	ShowText
	CMP.B	#1,EdScreen
	BEQ	ShowSampleAll
	CMP.B	#2,EdScreen
	BNE.S	demskip3
	BSR	ShowTrackPatt
	BSR	ShowFrom
	BSR	ShowTo
	BSR	ShowRecordMode
	BSR	ShowQuantize
	BSR	ShowMetronome
	BRA	ShowMultiMode
demskip3
	CMP.B	#3,EdScreen
	BNE	Return2
	BSR	ShowHalfClip
	BSR	ShowPos
	BSR	ShowMod
	BRA	ShowVol

EditOpText1	dc.b	'  track      pattern  '
EditOpText2	dc.b	'  record     samples  '
EditOpText3	dc.b	'    sample editor     '

CheckEditOpGadgs
	MOVE.W	PattCurPos(PC),D0
	BSR.S	GetPositionPtr
	MOVEM.W	MouseX2,D0/D1
	CMP.W	#306,D0
	BHS.S	CheckEdSwap
	CMP.B	#1,EdScreen
	BEQ	CheckEdGadg1
	CMP.B	#2,EdScreen
	BEQ	CheckEdGadg2
	CMP.B	#3,EdScreen
	BEQ	CheckEdGadg3
	RTS

GetPositionPtr
	MOVE.L	SongDataPtr(PC),A0
	LEA	sd_patterndata(A0),A0
	MOVE.L	PatternNumber(PC),D2
	LSL.L	#8,D2
	LSL.L	#2,D2
	ADD.L	D2,A0
	MOVEQ	#0,D2
	MOVE.W	D0,D2
	DIVU	#6,D2
	LSL.W	#2,D2
	ADD.W	D2,A0
	RTS

CheckEdSwap
	CMP.W	#55,D1
	BLS	Return2
	MOVEQ	#1,D2
	CMP.W	#66,D1
	BLS.S	SetEditOpScreen
	MOVEQ	#2,D2
	CMP.W	#77,D1
	BLS.S	SetEditOpScreen
	MOVEQ	#3,D2
	CMP.W	#88,D1
	BLS.S	SetEditOpScreen
	CMP.W	#99,D1
	BLS.S	ExitEditOp
	RTS

SetEditOpScreen
	MOVE.B	D2,EdScreen
	BRA	EditOp

ExitEditOp
	SF	EdEnable
	CLR.B	RawKeyCode
	BRA	DisplayMainScreen

CheckEdGadg1
	CMP.W	#55,D1
	BLS	ToggleSampleAll
	CMP.W	#66,D1
	BLS	NoteUp
	CMP.W	#77,D1
	BLS	NoteDown
	CMP.W	#88,D1
	BLS	OctaveUp
	CMP.W	#99,D1
	BLS	OctaveDown
	RTS

CheckEdGadg2
	CMP.W	#55,D1
	BLS	ToggleTrackPatt
	CMP.W	#213,D0
	BLS.S	ceg2left
	CMP.W	#66,D1
	BLS	DeleteOrKill
	CMP.W	#77,D1
	BLS	ExchangeOrCopy
	CMP.W	#88,D1
	BLS	SetSampleFrom
	CMP.W	#99,D1
	BLS	SetSampleTo
	RTS

ceg2left
	CMP.W	#66,D1
	BLS	ToggleRecordMode
	CMP.W	#77,D1
	BLS	SetQuantize
	CMP.W	#88,D1
	BLS	SetMetronome
	CMP.W	#99,D1
	BLS	ToggleMultiMode
	RTS

CheckEdGadg3
	MOVE.L	SongDataPtr(PC),A5
	LEA	-10(A5),A5
	MOVE.W	InsNum(PC),D2
	BNE.S	ceg3skip
	MOVE.W	LastInsNum,D2
ceg3skip
	MULU	#30,D2
	ADD.L	D2,A5
	CMP.W	#55,D1
	BLS	ToggleHalfClip
	CMP.W	#213,D0
	BLS.S	ceg3mid
	CMP.W	#66,D1
	BLS	SetSamplePos
	CMP.W	#77,D1
	BLS	SetModSpeed
	CMP.W	#88,D1
	BLS	CutBeg
	CMP.W	#99,D1
	BLS	ChangeVolume
	RTS

ceg3mid	CMP.W	#165,D0
	BLS.S	ceg3left
	CMP.W	#66,D1
	BLS	Echo
	CMP.W	#77,D1
	BLS	Filter
	CMP.W	#88,D1
	BLS	Backwards
	CMP.W	#99,D1
	BLS	DownSample
	RTS

ceg3left
	CMP.W	#66,D1
	BLS	Mix
	CMP.W	#77,D1
	BLS	Boost
	CMP.W	#88,D1
	BLS	XFade
	CMP.W	#99,D1
	BLS	UpSample
	RTS

ToggleSampleAll
	BSR	WaitForButtonUp
	EOR.B	#1,SampleAllFlag
ShowSampleAll
	LEA	BitplaneData+1838,A1
	LEA	S_BoxData,A2
	TST.B	SampleAllFlag
	BEQ	rtdoit
	LEA	A_BoxData,A2
	BRA	rtdoit

OctaveUp
	MOVE.W	#24,NoteShift
	BRA.S	nup2

NoteUp	MOVE.W	#2,NoteShift
nup2	BSR	SaveUndo
	CMP.W	#214,D0
	BLS.S	nup3
	MOVEQ	#0,D0
	BSR	GetPositionPtr
	BSR.S	SampleNoteUp
	MOVEQ	#6,D0
	BSR	GetPositionPtr
	BSR.S	SampleNoteUp
	MOVEQ	#12,D0
	BSR	GetPositionPtr
	BSR.S	SampleNoteUp
	MOVEQ	#18,D0
	BSR	GetPositionPtr
nup3	BSR.S	SampleNoteUp
	BRA	RedrawPattern

SampleNoteUp
	MOVEQ	#63,D6
	MOVE.L	A0,A3
	MOVE.W	InsNum(PC),D3
	TST.B	SampleAllFlag
	BEQ.S	sanulo2
	MOVEQ	#0,D3
sanulo2	MOVE.L	D6,D0
	MOVE.L	A3,A0
sanuloop
	MOVE.B	2(A0),D1
	LSR.B	#4,D1
	MOVE.B	(A0),D2
	AND.B	#$F0,D2
	OR.B	D2,D1
	CMP.B	D3,D1
	BNE.S	sanuskip
	MOVE.W	(A0),D1
	MOVE.W	D1,D2
	AND.W	#$F000,D2
	AND.W	#$0FFF,D1
	BEQ.S	sanuskip
	BSR	CheckPeriod
	BNE.S	sanuskip
	ADD.W	NoteShift(PC),D5
	CMP.W	#$0048,D5
	BLO.S	sanuok
	TST.B	TransDelFlag
	BEQ.S	sanuskip
	AND.L	#$00000FFF,(A0)
	BRA.S	sanuskip
sanuok	MOVE.W	0(A2,D5.W),D1
	OR.W	D2,D1
	MOVE.W	D1,(A0)
sanuskip
	LEA	16(A0),A0
	DBRA	D0,sanuloop
	TST.B	SampleAllFlag
	BEQ	Return2
	ADDQ.W	#1,D3
	CMP.W	#32,D3
	BLO.S	sanulo2
	RTS

OctaveDown
	MOVE.W	#24,NoteShift
	BRA.S	ndown2

NoteDown
	MOVE.W	#2,NoteShift
ndown2	BSR	SaveUndo
	CMP.W	#214,D0
	BLS.S	ndown3
	MOVEQ	#0,D0
	BSR	GetPositionPtr
	BSR.S	SampleNoteDown
	MOVEQ	#6,D0
	BSR	GetPositionPtr
	BSR.S	SampleNoteDown
	MOVEQ	#12,D0
	BSR	GetPositionPtr
	BSR.S	SampleNoteDown
	MOVEQ	#18,D0
	BSR	GetPositionPtr
ndown3	BSR.S	SampleNoteDown
	BRA	RedrawPattern

SampleNoteDown
	MOVEQ	#63,D6
	MOVE.L	A0,A3
	MOVE.W	InsNum(PC),D3
	TST.B	SampleAllFlag
	BEQ.S	sandlo2
	MOVEQ	#0,D3
sandlo2	MOVE.W	D6,D0
	MOVE.L	A3,A0
sandloop
	MOVE.B	2(A0),D1
	LSR.B	#4,D1
	MOVE.B	(A0),D2
	AND.B	#$F0,D2
	OR.B	D2,D1
	CMP.B	D1,D3
	BNE.S	sandskip
	MOVE.W	(A0),D1
	MOVE.W	D1,D2
	AND.W	#$F000,D2
	AND.W	#$0FFF,D1
	BEQ.S	sandskip
	BSR.S	CheckPeriod
	BNE.S	sandskip
	SUB.W	NoteShift(PC),D5
	BPL.S	sandok
	TST.B	TransDelFlag
	BEQ.S	sandskip
	AND.L	#$00000FFF,(A0)
	BRA.S	sandskip
sandok	MOVE.W	0(A2,D5.W),D1
	OR.W	D2,D1
	MOVE.W	D1,(A0)
sandskip
	LEA	16(A0),A0
	DBRA	D0,sandloop
	TST.B	SampleAllFlag
	BEQ	Return2
	ADDQ.W	#1,D3
	CMP.W	#$20,D3
	BLO.S	sandlo2
	RTS

NoteShift
	dc.w	0

CheckPeriod
	LEA	PeriodTable(PC),A2
	MOVEQ	#-2,D5
chpeloop
	ADDQ.L	#2,D5
	MOVE.W	0(A2,D5.W),D4
	BEQ.S	PeriodNotOk
	CMP.W	D4,D1
	BEQ.S	PeriodOk
	BRA.S	chpeloop

PeriodOk
	MOVEQ	#0,D4
	RTS

PeriodNotOk
	MOVEQ	#-1,D4
	RTS

ToggleTrackPatt
	BSR	WaitForButtonUp
	ADDQ.B	#1,TrackPattFlag
	CMP.B	#3,TrackPattFlag
	BLO.S	ShowTrackPatt
	CLR.B	TrackPattFlag
ShowTrackPatt
	LEA	BitplaneData+1838,A1
	LEA	T_BoxData,A2
	TST.B	TrackPattFlag
	BEQ	rtdoit
	LEA	P_BoxData,A2
	CMP.B	#1,TrackPattFlag
	BEQ	rtdoit
	LEA	S_BoxData,A2
	BRA	rtdoit

KillInstrTrack
	BSR	SaveUndo
	MOVE.W	PattCurPos(PC),D0
	BSR	GetPositionPtr
	BRA.S	dst2

DeleteOrKill
	CMP.W	#260,D0
	BHS	KillSample
DelSampleTrack
	BSR	SaveUndo
	TST.B	TrackPattFlag
	BEQ.S	dst2
	MOVEQ	#0,D0
	BSR	GetPositionPtr
	BSR.S	dstdoit
	MOVEQ	#6,D0
	BSR	GetPositionPtr
	BSR.S	dstdoit
	MOVEQ	#12,D0
	BSR	GetPositionPtr
	BSR.S	dstdoit
	MOVEQ	#18,D0
	BSR	GetPositionPtr
dst2	BSR.S	dstdoit
	BRA	RedrawPattern

dstdoit	CLR.B	RawKeyCode
	MOVEQ	#63,D0
	MOVE.W	InsNum(PC),D3
	BEQ	NotSampleNull
ksloop	MOVE.B	2(A0),D1
	LSR.B	#4,D1
	MOVE.B	(A0),D2
	AND.B	#$F0,D2
	OR.B	D2,D1
	CMP.B	D1,D3
	BNE.S	ksskip
	CLR.L	(A0)
ksskip	LEA	16(A0),A0
	DBRA	D0,ksloop
	RTS

KillSample
	LEA	KillSampleText,A0
	BSR	AreYouSure
	BNE	Return2
Destroy	BSR	StorePtrCol
	MOVE.W	InsNum(PC),D0
	BEQ	ErrorRestoreCol
	BSR	TurnOffVoices
	BSR	FreeSample
	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	InsNum(PC),D0
	MULU	#30,D0
	LEA	-10(A0),A0
	ADD.L	D0,A0
	MOVE.L	A0,A1
	MOVEQ	#29,D0
kisalop	CLR.B	(A0)+
	DBRA	D0,kisalop
	MOVE.W	#1,28(A1)
	BSR	ShowSampleInfo
	BSR	RedrawSample
	BRA	RestorePtrCol

ExchangeOrCopy
	MOVEQ	#-1,D4
	CMP.W	#260,D0
	BHS.S	CopySampleTrack
ExchSampleTrack
	CMP.B	#2,TrackPattFlag
	BEQ	ExchSamples
	MOVEQ	#0,D4
	BRA.S	mstskip
CopySampleTrack
	CMP.B	#2,TrackPattFlag
	BEQ	CopySamples
mstskip	TST.B	TrackPattFlag
	BEQ.S	mst2
	MOVEQ	#0,D0
	BSR	GetPositionPtr
	BSR.S	mstdoit
	MOVEQ	#6,D0
	BSR	GetPositionPtr
	BSR.S	mstdoit
	MOVEQ	#12,D0
	BSR	GetPositionPtr
	BSR.S	mstdoit
	MOVEQ	#18,D0
	BSR	GetPositionPtr
mst2	BSR.S	mstdoit
	BRA	RedrawPattern

mstdoit	MOVEQ	#63,D0
esloop	MOVE.B	2(A0),D1
	LSR.B	#4,D1
	MOVE.B	(A0),D2
	AND.B	#$F0,D2
	OR.B	D2,D1
	CMP.B	SampleFrom(PC),D1
	BNE.S	esskip2
	AND.L	#$0FFF0FFF,(A0)
	MOVE.B	SampleTo(PC),D2
	MOVE.B	D2,D3
	AND.B	#$F0,D2
	OR.B	D2,(A0)
	LSL.B	#4,D3
	OR.B	D3,2(A0)
	BRA.S	esskip3
esskip2	TST.B	D4
	BNE.S	esskip3
	CMP.B	SampleTo(PC),D1
	BNE.S	esskip3
	AND.L	#$0FFF0FFF,(A0)
	MOVE.B	SampleFrom(PC),D2
	MOVE.B	D2,D3
	AND.B	#$F0,D2
	OR.B	D2,(A0)
	LSL.B	#4,D3
	OR.B	D3,2(A0)
esskip3	LEA	16(A0),A0
	DBRA	D0,esloop
	RTS

ExchSamples
	BSR	StorePtrCol
	MOVEQ	#0,D0
	MOVE.B	SampleFrom(PC),D0
	BEQ	ErrorRestoreCol
	MOVEQ	#0,D1
	MOVE.B	SampleTo(PC),D1
	BEQ	ErrorRestoreCol
	MOVE.W	D0,D2
	MOVE.W	D1,D3
	LEA	SampleLengthAdd+2,A2
	LSL.W	#1,D2
	LSL.W	#1,D3
	LEA	(A2,D2.W),A0
	LEA	(A2,D3.W),A1
	MOVE.W	(A0),D4
	MOVE.W	(A1),(A0)
	MOVE.W	D4,(A1)
	LEA	SamplePtrs,A2
	LSL.W	#1,D2
	LSL.W	#1,D3
	LEA	(A2,D2.W),A0
	LEA	(A2,D3.W),A1
	MOVE.L	(A0),D4
	MOVE.L	(A1),(A0)
	MOVE.L	D4,(A1)
	MOVE.L	124(A0),D4
	MOVE.L	124(A1),124(A0)
	MOVE.L	D4,124(A1)
	SUBQ.W	#1,D0
	SUBQ.W	#1,D1
	MULU	#30,D0
	MULU	#30,D1
	MOVE.L	SongDataPtr(PC),A2
	LEA	20(A2),A2
	LEA	(A2,D0.W),A0
	LEA	(A2,D1.W),A1
	MOVEQ	#29,D0
exsalop	MOVE.B	(A0),D1
	MOVE.B	(A1),(A0)+
	MOVE.B	D1,(A1)+
	DBRA	D0,exsalop
	BSR	ShowSampleInfo
	BSR	RedrawSample
	BRA	RestorePtrCol

CopySamples
	BSR	StorePtrCol
	MOVEQ	#0,D0
	MOVE.B	SampleFrom(PC),D0
	BEQ	ErrorRestoreCol
	MOVEQ	#0,D1
	MOVE.B	SampleTo(PC),D1
	BEQ	ErrorRestoreCol
	LEA	SamplePtrs(PC),A2
	LSL.W	#2,D0
	LSL.W	#2,D1
	LEA	(A2,D0.W),A3
	LEA	(A2,D1.W),A4
	MOVE.L	(A3),D0
	BEQ	ErrorRestoreCol
	MOVE.L	124(A3),D0
	MOVEQ	#MEMF_CHIP,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	TST.L	D0
	BEQ	OutOfMemErr
	MOVE.L	D0,A5
	MOVEQ	#0,D0
	MOVE.B	SampleTo(PC),D0
	MOVE.W	D0,InsNum
	BSR	Destroy
	MOVE.L	A5,(A4)
	MOVE.L	124(A3),D0
	MOVE.L	D0,124(A4)
	MOVE.L	(A3),A0
cosalp2	MOVE.B	(A0)+,(A5)+
	SUBQ.L	#1,D0
	BNE.S	cosalp2
	MOVEQ	#0,D0
	MOVEQ	#0,D1
	MOVE.B	SampleFrom(PC),D0
	MOVE.B	SampleTo(PC),D1
	MOVE.W	D1,InsNum
	MULU	#30,D0
	MULU	#30,D1
	MOVE.L	SongDataPtr(PC),A2
	LEA	-10(A2),A2
	LEA	(A2,D0.W),A0
	LEA	(A2,D1.W),A1
	MOVEQ	#29,D0
cosalop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,cosalop
	BSR	ShowSampleInfo
	BSR	RedrawSample
	BRA	RestorePtrCol

SetSampleFrom
	MOVE.W	InsNum(PC),D2
	CMP.W	#283,D0
	BLS.S	sesfskip
	MOVE.B	SampleFrom(PC),D2
	CMP.W	#295,D0
	BLS.S	sesfup
	SUBQ.B	#1,D2
	BPL.S	sesfskip
	MOVEQ	#0,D2
sesfskip
	MOVE.B	D2,SampleFrom
	BSR	Wait_4000
	BRA.S	ShowFrom
sesfup	ADDQ.B	#1,D2
	CMP.B	#$1F,D2
	BLS.S	sesfskip
	MOVE.B	#$1F,SampleFrom
ShowFrom
	MOVE.W	#3273,TextOffset
	CLR.W	WordNumber
	MOVE.B	SampleFrom(PC),WordNumber+1
	BRA	PrintHexByte

SetSampleTo
	MOVE.W	InsNum(PC),D2
	CMP.W	#283,D0
	BLS.S	sestskip
	MOVE.B	SampleTo(PC),D2
	CMP.W	#295,D0
	BLS.S	sestup
	SUBQ.B	#1,D2
	BPL.S	sestskip
	MOVEQ	#0,D2
sestskip
	MOVE.B	D2,SampleTo
	BSR	Wait_4000
	BRA.S	ShowTo
sestup	ADDQ.B	#1,D2
	CMP.B	#$1F,D2
	BLS.S	sestskip
	MOVE.B	#$1F,SampleTo
ShowTo	MOVE.W	#3713,TextOffset
	CLR.W	WordNumber
	MOVE.B	SampleTo(PC),WordNumber+1
	BRA	PrintHexByte

SampleFrom	dc.b 0
SampleTo	dc.b 0

ToggleRecordMode
	BSR	WaitForButtonUp
	EOR.B	#1,RecordMode
ShowRecordMode
	MOVE.W	#2382,D1
	LEA	PattText,A0
	TST.B	RecordMode
	BEQ.S	srmskip
	LEA	SongText,A0
srmskip	MOVEQ	#4,D0
	BRA	ShowText3

PattText	dc.b 'patt'
SongText	dc.b 'song'
RecordMode	dc.b 0
QuantizeValue	dc.b 1
MetroSpeed	dc.b 4
MetroChannel	dc.b 0
MultiFlag	dc.b 0
SampleAllFlag	dc.b 0
TrackPattFlag	dc.b 0
ClipHalveFlag	dc.b 0
AbortDecFlag	dc.w 0

GetDecByte
	MOVE.W	#1,AbortDecFlag
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	BSR	UpdateLineCurPos
	BSR	GetKey0_9
	CMP.B	#68,D1
	BEQ.S	gdbexit
	MOVE.B	D1,D0
	MULU	#10,D0
	MOVE.B	D0,GetDecTemp
	BSR	ShowOneDigit
	ADDQ.W	#8,LineCurX
	BSR	UpdateLineCurPos
	BSR	GetKey0_9
	CMP.B	#68,D1
	BEQ.S	gdbexit
	ADD.B	D1,GetDecTemp
	CLR.W	AbortDecFlag
gdbexit	CLR.W	LineCurX
	MOVE.W	#270,LineCurY
	BSR	UpdateLineCurPos
	BSR	RestorePtrCol
	MOVE.B	GetDecTemp,D0
	RTS

SetQuantize
	CLR.B	RawKeyCode
	MOVE.W	#196,LineCurX
	MOVE.W	#76,LineCurY
	MOVE.W	#2824,TextOffset
	BSR	GetDecByte
	TST.W	AbortDecFlag
	BNE	ShowQuantize
	CMP.B	#63,D0
	BLS.S	tqskip
	MOVEQ.B	#63,D0
tqskip	MOVE.B	D0,QuantizeValue
ShowQuantize
	MOVE.W	#2824,TextOffset
	CLR.W	WordNumber
	MOVE.B	QuantizeValue,WordNumber+1
	BRA	Print2DecDigits

SetMetronome
	CLR.B	RawKeyCode
	CMP.W	#188,D0
	BHS.S	smchan
	MOVE.W	#3261,TextOffset
	MOVE.W	#172,LineCurX
	MOVE.W	#87,LineCurY
	BSR	GetDecByte
	TST.W	AbortDecFlag
	BNE	ShowMetronome
	CMP.B	#64,D0
	BLS.S	smexit
	MOVEQ.B	#64,D0
smexit	MOVE.B	D0,MetroSpeed
	BRA.S	ShowMetronome

smchan	MOVE.W	#3264,TextOffset
	BSR	GetHexByte
	CMP.W	#4,D0
	BLS.S	smexit2
	MOVEQ	#4,D0
smexit2	MOVE.B	D0,MetroChannel
ShowMetronome
	MOVE.W	#3261,TextOffset
	CLR.W	WordNumber
	MOVE.B	MetroSpeed,WordNumber+1
	BSR	Print2DecDigits
	MOVE.W	#3264,TextOffset
	CLR.W	WordNumber
	MOVE.B	MetroChannel,WordNumber+1
	BRA	Print2DecDigits

ToggleMultiMode
	BSR	WaitForButtonUp
	CLR.B	RawKeyCode
	EOR.B	#1,MultiFlag
ShowMultiMode
	BSR.S	Show_MS
	CMP.W	#1,CurrScreen
	BNE	Return2
	CMP.B	#2,EdScreen
	BNE	Return2
	TST.B	EdEnable
	BEQ	Return2
	MOVE.W	#3700,D1
	LEA	SingleText(PC),A0
	TST.B	MultiFlag
	BEQ.S	smmskip
	LEA	MultiText(PC),A0
smmskip	MOVEQ	#6,D0
	BRA	ShowText3

SingleText	dc.b 'single'
MultiText	dc.b ' multi'

Show_MS	CMP.W	#4,CurrScreen
	BEQ	Return2
	MOVE.B	#' ',D0
	TST.B	MultiFlag
	BEQ.S	smsskp1
	MOVE.B	#'M',D0
smsskp1	MOVE.B	#' ',D1
	TST.B	SplitFlag
	BEQ.S	smsskp2
	MOVE.B	#'S',D1
smsskp2	MOVE.B	D0,mstext
	MOVE.B	D1,mstext+1
	MOVE.W	#4121,D1
	MOVEQ	#2,D0
	LEA	mstext(PC),A0
	BSR	ShowText3
	BRA	ShowAutoInsert

mstext	dc.b '__'

;---- Edit Op. 3 ----

ToggleHalfClip
	BSR	WaitForButtonUp
	EOR.B	#1,HalfClipFlag
ShowHalfClip
	LEA	BitplaneData+1838,A1
	LEA	H_BoxData,A2
	TST.B	HalfClipFlag
	BEQ.S	ogaoga
	LEA	C_BoxData,A2
ogaoga	JMP	rtdoit

SetSamplePos
	MOVE.L	SamplePos(PC),D2
	CMP.W	#283,D0
	BLS	Return2
	CMP.W	#295,D0
	BLS.S	shpoup
	SUBQ.L	#1,D2
	BTST	#2,$DFF016
	BNE.S	shposkip
	SUB.L	#15,D2
shposkip
	BPL.S	shposkip2
	MOVEQ	#0,D2
shposkip2
	MOVE.L	D2,SamplePos
	BRA.S	ShowPos
shpoup	ADDQ.L	#1,D2
	BTST	#2,$DFF016
	BNE.S	shposkip3
	ADD.L	#15,D2
shposkip3
	MOVEQ	#0,D3
	MOVE.W	22(A5),D3
	LSL.L	#1,D3
	CMP.L	D3,D2
	BLS.S	shposkip2
	MOVE.L	D3,SamplePos
ShowPos	CMP.W	#1,CurrScreen
	BNE	Return2
	TST.B	EdEnable
	BEQ	Return2
	CMP.B	#3,EdScreen
	BNE	Return2
	MOVE.W	#2391,TextOffset
	MOVE.W	SamplePos+2(PC),WordNumber
	BRA	PrintHexWord

SetModSpeed
	CMP.W	#243,D0
	BLS	DoMod
	MOVEQ	#0,D2
	BTST	#2,$DFF016
	BEQ.S	semoskp
	MOVE.B	ModSpeed,D2
	CMP.W	#283,D0
	BLS	Return2
	CMP.W	#295,D0
	BLS.S	semoup
	SUB.B	#1,D2
	CMP.B	#127,D2
	BNE.S	semoskp
	MOVE.B	#128,D2
	BRA.S	semoskp
semoup	ADD.B	#1,D2
	CMP.B	#128,D2
	BNE.S	semoskp
	MOVEQ	#127,D2
semoskp	MOVE.B	D2,ModSpeed
	BSR	Wait_4000
ShowMod	LEA	PlusMinusText(PC),A0
	MOVEQ	#0,D6
	MOVE.B	ModSpeed,D6
	BPL.S	shmoskp
	NEG.B	D6
	ADDQ.L	#1,A0
shmoskp	MOVEQ	#1,D0
	MOVE.W	#2831,D1
	BSR	ShowText3
	MOVE.W	D6,WordNumber
	BRA	Print3DecDigits

PlusMinusText	dc.b ' -'

DoMod	BSR	WaitForButtonUp
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVEQ	#0,D2
	MOVE.W	SampleInfo(PC),D2
	BEQ	ErrorRestoreCol
	LSL.W	#1,D2
	SUBQ.W	#1,D2
	MOVE.L	si_pointer,D0
	BEQ	ErrorRestoreCol
	BSR	AllocBuffer
	MOVE.L	D0,A2
	MOVE.L	D0,A3
	MOVE.L	A1,A4
	ADD.L	D2,A3
	CLR.L	ModOffset
	CLR.L	ModPos
dmoloop	CMP.L	A3,A2
	BHS.S	dmoskip
	MOVE.B	(A1),(A2)+
	BSR.S	UpdateMod
	BRA.S	dmoloop
dmoskip	MOVE.L	si_pointer(PC),A1
	CLR.W	(A1)
	BSR	FreeBuffer
	BSR	RestorePtrCol
	BRA	DisplaySample

UpdateMod
	MOVE.B	ModSpeed(PC),D0
	BEQ.S	upmplus
	EXT.W	D0
	EXT.L	D0
	MOVE.L	ModPos(PC),D1
	ADD.L	D0,D1
	MOVE.L	D1,ModPos
	ASR.L	#8,D1
	ASR.L	#4,D1
	MOVE.L	D1,D3
	AND.W	#$1F,D1	
	LEA	VibratoTable,A0
	MOVEQ	#0,D0
	MOVE.B	(A0,D1.W),D0
	LSR.B	#2,D0
	MOVE.L	ModOffset(PC),D1
	BTST	#5,D3
	BEQ.S	upmskip
	SUB.L	D0,D1
	BRA.S	upmskp2
upmskip	ADD.L	D0,D1
upmskp2	ADD.L	#$800,D1
	MOVE.L	D1,ModOffset
	ASR.L	#8,D1
	ASR.L	#3,D1
	BPL.S	upmskp3
	MOVEQ	#0,D1
upmskp3	CMP.L	D2,D1
	BLO.S	upmskp4
	MOVE.L	D2,D1
upmskp4	MOVE.L	A4,A1
	ADD.L	D1,A1
	RTS
upmplus	ADDQ.L	#1,A1
	RTS

CutBeg	CMP.W	#283,D0
	BHI	FadeDown
	CMP.W	#259,D0
	BHI	FadeUp
	BSR	WaitForButtonUp
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVEQ	#0,D3
	MOVE.W	SampleInfo(PC),D3
	BEQ	ErrorRestoreCol
	LSL.W	#1,D3
	SUBQ.W	#1,D3
	MOVE.L	si_pointer,D0
	BEQ	ErrorRestoreCol
	MOVE.L	D0,A1
	MOVE.L	D0,A2
	MOVE.L	D0,A3
	ADD.L	SamplePos(PC),A2
	ADD.L	D3,A3
cbeloop	CMP.L	A3,A2
	BHS.S	cbeskip
	MOVE.B	(A2),D0
	CLR.B	(A2)+
	MOVE.B	D0,(A1)+
	BRA.S	cbeloop	
cbeskip	MOVE.L	si_pointer(PC),A2
	CLR.W	(A2)
	SUB.L	A2,A1
	MOVE.L	A1,D0
	LSR.L	#1,D0
	ADDQ.L	#1,D0
	AND.W	#$7FFF,D0
	MOVE.W	D0,22(A5)
	BSR	ShowSampleInfo
	BSR	RestorePtrCol
	BRA	DisplaySample

FadeUp	BSR	WaitForButtonUp
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVEQ	#0,D3
	MOVE.W	SampleInfo(PC),D3
	BEQ	ErrorRestoreCol
	MOVEQ	#0,D1
	MOVE.L	SamplePos(PC),D2
	BEQ	ErrorRestoreCol
	MOVE.L	si_pointer,D0
	BEQ	ErrorRestoreCol
	MOVE.L	D0,A0
	LSR.W	#1,D2
	BEQ	ErrorRestoreCol
	MOVEQ	#0,D4
fuloop	MOVE.B	(A0),D0
	EXT.W	D0
	MULS	D1,D0
	DIVS	D2,D0
	MOVE.B	D0,(A0)+
	ADDQ.W	#1,D4
	MOVE.W	D4,D1
	LSR.W	#1,D1
	CMP.W	D2,D1
	BLO.S	fuloop
	BSR	RestorePtrCol
	BRA	DisplaySample

FadeDown
	BSR	WaitForButtonUp
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVEQ	#0,D3
	MOVE.W	SampleInfo(PC),D3
	BEQ	ErrorRestoreCol
	LSL.L	#1,D3
	SUBQ.L	#1,D3 ;Length-1
	MOVE.L	SamplePos(PC),D2
	CMP.L	D3,D2
	BHS	ErrorRestoreCol
	MOVE.L	D3,D4 ; Copy length to D4
	SUB.L	D2,D3 ; Length-pos
	MOVE.L	D3,D2 ; Copy offset to D2
	MOVE.L	si_pointer,D0
	BEQ	ErrorRestoreCol
	MOVE.L	D0,A0
	LEA	(A0,D4.L),A0 ;Start at end of sample
	LSR.W	#1,D2
	BEQ	ErrorRestoreCol
	MOVEQ	#0,D1
	MOVEQ	#0,D4
fdloop	MOVE.B	(A0),D0
	EXT.W	D0
	MULS	D1,D0
	DIVS	D2,D0
	MOVE.B	D0,(A0)
	SUBQ.L	#1,A0
	ADDQ.W	#1,D4
	MOVE.W	D4,D1
	LSR.W	#1,D1
	CMP.W	D2,D1
	BLO.S	fdloop
	BSR	RestorePtrCol
	BRA	DisplaySample

ChangeVolume
	MOVE.W	SampleVol(PC),D2
	CMP.W	#243,D0
	BLS	DoChangeVol
	CMP.W	#283,D0
	BLS	Return2
	CMP.W	#295,D0
	BLS	shvoup
	SUBQ.W	#1,D2
	BTST	#2,$DFF016
	BNE.S	shvoskip
	SUB.W	#9,D2
shvoskip
	BPL.S	shvoskip2
	MOVEQ	#0,D2
shvoskip2
	MOVE.W	D2,SampleVol
	BSR	Wait_4000
	BRA.S	ShowVol
shvoup	ADDQ.L	#1,D2
	BTST	#2,$DFF016
	BNE.S	shvoskip3
	ADD.W	#9,D2
shvoskip3
	CMP.W	#999,D2
	BLS.S	shvoskip2
	MOVE.W	#999,SampleVol
	BSR	Wait_4000
ShowVol	MOVE.W	#3711,TextOffset
	MOVE.W	SampleVol(PC),WordNumber
	BSR	Print3DecDigits
	LEA	PercentText(PC),A0
	MOVE.W	#1,TextLength
	BRA	ShowText2

PercentText	dc.b '%',0

DoChangeVol
	BSR	WaitForButtonUp
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVEQ	#0,D3
	MOVE.W	SampleInfo(PC),D3
	BEQ	ErrorRestoreCol
	LSL.W	#1,D3
	SUBQ.W	#1,D3
	MOVE.L	si_pointer,D0
	BEQ	ErrorRestoreCol
	MOVE.L	D0,A1
	MOVE.W	SampleVol(PC),D0
dcvloop	MOVE.B	(A1),D1
	EXT.W	D1
	MULS	D0,D1
	DIVS	#100,D1
	CMP.W	#127,D1
	BLE.S	dcvskip
	MOVEQ	#127,D1
dcvskip	CMP.W	#-128,D1
	BGE.S	dcvskp2
	MOVEQ	#-128,D1
dcvskp2	MOVE.B	D1,(A1)+
	DBRA	D3,dcvloop
	MOVE.L	si_pointer(PC),A1
	CLR.W	(A1)
	BSR	RestorePtrCol
	BRA	DisplaySample

Mix	BTST	#2,$DFF016
	BEQ	OldMix
	BSR	WaitForButtonUp
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVE.W	#1,GetLineFlag
	MOVE.W	#4,MixCurPos
mixlopx	BSR	DisplayMix
	BSR	GetHexKey
	TST.B	D0
	BNE.S	mixnzro
	CMP.B	#68,MixChar
	BEQ	Mix2
	CMP.B	#69,MixChar
	BEQ	EndMix
	BTST	#2,$DFF016
	BEQ	EndMix
mixpchr	LEA	MixText(PC),A0
	LEA	FastHexTable+1,A1
	LSL.W	#1,D1
	MOVE.W	MixCurPos(PC),D0
	MOVE.B	(A1,D1.W),(A0,D0.W)
MixMoveRight
	MOVE.W	MixCurPos,D0
	ADDQ.W	#1,D0
	CMP.W	#6,D0
	BEQ.S	mmrp1
	CMP.W	#9,D0
	BEQ.S	mmrp2
	CMP.W	#15,D0
	BHS.S	mmrp3
mmrok	MOVE.W	D0,MixCurPos
	BRA	mixlopx
mmrp1	MOVEQ	#7,D0
	BRA.S	mmrok
mmrp2	MOVEQ	#13,D0
	BRA.S	mmrok
mmrp3	MOVEQ	#14,D0
	BRA.S	mmrok

mixnzro	CMP.B	#1,D0
	BEQ.S	MixMoveRight
MixMoveLeft
	MOVE.W	MixCurPos,D0
	SUBQ.W	#1,D0
	CMP.W	#4,D0
	BLO.S	mmlp1
	CMP.W	#6,D0
	BEQ.S	mmlp2
	CMP.W	#12,D0
	BEQ.S	mmlp3
	BRA.S	mmrok
mmlp1	MOVEQ	#4,D0
	BRA.S	mmrok
mmlp2	MOVEQ	#5,D0
	BRA.S	mmrok
mmlp3	MOVEQ	#8,D0
	BRA.S	mmrok

DisplayMix
	MOVE.W	#53,LineCurY
	MOVE.W	MixCurPos,D0
	LSL.W	#3,D0
	ADD.W	#132,D0
	MOVE.W	D0,LineCurX
	BSR	UpdateLineCurPos
	LEA	MixText(PC),A0
	MOVE.W	#1936,D1
	MOVEQ	#22,D0
	BSR	ShowText3
	BSR	Wait_4000
	BSR	Wait_4000
	BRA	Wait_4000

MixText	dc.b 'mix 01+02 to 03       '
MixChar	dc.b 0,0
MixCurPos dc.w 0
ToSam		dc.w 0
FromPtr1	dc.l 0
FromPtr2	dc.l 0
ToPtr		dc.l 0
MixPtr		dc.l 0
MixLength	dc.l 0

EndMix	CLR.B	RawKeyCode
	BSR	RestoreMix
	BRA	RestorePtrCol

RestoreMix
	CLR.W	GetLineFlag
	MOVE.W	#270,LineCurY
	CLR.W	LineCurX
	BSR	UpdateLineCurPos
	LEA	EditOpText3(PC),A0
	MOVE.W	#1936,D1
	MOVEQ	#22,D0
	BRA	ShowText3

Mix2	BSR	RestoreMix
	BSR	TurnOffVoices
	LEA	SamplePtrs,A2
	MOVEQ	#0,D0
	LEA	MixText+4(PC),A0
	BSR	HexToInteger2
	TST.W	D0
	BEQ	SamOutOfRange
	CMP.W	#$1F,D0
	BHI	SamOutOfRange
	LSL.W	#2,D0
	LEA	(A2,D0.W),A3
	MOVE.L	A3,FromPtr1

	MOVEQ	#0,D0
	LEA	MixText+7(PC),A0
	BSR	HexToInteger2
	TST.W	D0
	BEQ	SamOutOfRange
	CMP.W	#$1F,D0
	BHI	SamOutOfRange
	LSL.W	#2,D0
	LEA	(A2,D0.W),A3
	MOVE.L	A3,FromPtr2

	MOVEQ	#0,D0
	LEA	MixText+13(PC),A0
	BSR	HexToInteger2
	MOVE.W	D0,ToSam
	BEQ	SamOutOfRange
	CMP.W	#$1F,D0
	BHI	SamOutOfRange
	LSL.W	#2,D0
	LEA	(A2,D0.W),A3
	MOVE.L	A3,ToPtr

	MOVE.L	FromPtr1,A1
	MOVE.L	FromPtr2,A2
	MOVE.L	124(A1),D1
	MOVE.L	124(A2),D2
	CMP.L	D1,D2
	BLO.S	mixnswp
	EXG	D1,D2
mixnswp	TST.L	D1
	BEQ	SamEmptyError ; Both samples had length=0
	MOVE.L	D1,D0
	AND.L	#$FFFF,D0
	MOVE.L	D0,MixLength
	MOVE.L	#MEMF_CHIP!MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,MixPtr
	BEQ	SamMemError ; No memory for new sample...

	LEA	MixingText(PC),A0
	BSR	ShowStatusText
	MOVE.L	FromPtr1(PC),A1
	MOVE.L	124(A1),D1
	MOVE.L	(A1),A1
	MOVE.L	FromPtr2(PC),A2
	MOVE.L	124(A2),D2
	MOVE.L	(A2),A2
	MOVE.L	MixPtr(PC),A3
	MOVE.L	MixLength,D3
	TST.W	D1
	BEQ.S	mixnd1
	SUBQ.W	#1,D1
mixnd1	TST.W	D2
	BEQ.S	mixnd2
	SUBQ.W	#1,D2
mixnd2	SUBQ.W	#1,D3

mixlop2	MOVE.B	(A1),D4
	EXT.W	D4
	MOVE.B	(A2),D0
	EXT.W	D0
	ADD.W	D4,D0
	TST.B	HalfClipFlag
	BEQ	mixhalf
	CMP.W	#127,D0
	BLE.S	mixnbig
	MOVEQ	#127,D0
mixnbig	CMP.W	#-128,D0
	BGE	mixokok
	MOVEQ	#-128,D0
	BRA	mixokok
mixhalf	ASR.W	#1,D0
mixokok	MOVE.B	D0,(A3)+

	TST.W	D1
	BEQ.S	zerd1
	ADDQ.L	#1,A1
	SUBQ.W	#1,D1
	BNE.S	zerd1
	SUB.L	A1,A1

zerd1	TST.W	D2
	BEQ.S	zerd2
	ADDQ.L	#1,A2
	SUBQ.W	#1,D2
	BNE.S	zerd2
	SUB.L	A2,A2

zerd2	DBRA	D3,mixlop2
	MOVE.W	ToSam(PC),InsNum
	BSR	FreeSample
	MOVE.L	ToPtr(PC),A0
	MOVE.L	MixPtr(PC),A1
	CLR.W	(A1)
	MOVE.L	A1,(A0)
	MOVE.L	MixLength(PC),124(A0)
	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	ToSam(PC),D0
	SUBQ.W	#1,D0
	MULU	#30,D0
	LEA	20(A0,D0.W),A0
	MOVE.L	MixLength(PC),D0
	LSR.W	#1,D0
	MOVE.W	D0,22(A0)
	MOVE.W	#$0040,24(A0)
	MOVE.W	#$0000,26(A0)
	MOVE.W	#$0001,28(A0)
	BSR	RestorePtrCol
	BSR	ShowAllRight
	BSR	ShowSampleInfo
	BRA	DisplaySample

SamOutOfRange
	LEA	mixerrtext1(PC),A0
	BSR	ShowStatusText
	BRA	ErrorRestoreCol
SamEmptyError
	LEA	mixerrtext2(PC),A0
	BSR	ShowStatusText
	BRA	ErrorRestoreCol
SamMemError
	LEA	mixerrtext3(PC),A0
	BSR	ShowStatusText
	BRA	ErrorRestoreCol
OutOfMemErr
	LEA	mixerrtext3(PC),A0
	BSR	ShowStatusText
	BSR	SetErrorPtrCol
	MOVEQ	#0,D0
	RTS

mixerrtext1	dc.b 'not range 01-1F !',0
mixerrtext2	dc.b 'empty samples !!!',0
mixerrtext3	dc.b 'out of memory !!!',0
mixingtext	dc.b 'mixing samples...',0

OldMix	MOVE.L	SamplePos(PC),FlangePos
DoMix	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVEQ	#0,D2
	MOVE.W	SampleInfo(PC),D2 ; 22(A0)
	BEQ	Return2
	LSL.W	#1,D2
	MOVE.L	si_pointer(PC),D0
	BEQ	ErrorRestoreCol
	BSR	AllocBuffer
	MOVE.L	D0,A2
	MOVE.L	D0,A3
	MOVE.L	A1,A4
	ADD.L	D2,A3
	ADD.L	FlangePos(PC),A2
	CLR.L	ModOffset
	CLR.L	ModPos
	MOVEQ	#-1,D7
mixloop	ADDQ.L	#1,D7
	CMP.L	A3,A2
	BHS	mixskip
	MOVE.L	MarkStartOfs(PC),D0
	BMI.S	omxget
	MOVE.L	MarkEndOfs(PC),D1
	CMP.L	D0,D1
	BEQ.S	omxget
	CMP.L	D0,D7
	BLO.S	omxok2
	CMP.L	D1,D7
	BHI.S	omxok2
omxget	MOVE.B	(A2),D0
	EXT.W	D0
	MOVE.B	(A1),D1
	EXT.W	D1
	ADD.W	D1,D0
	TST.B	HalfClipFlag
	BEQ.S	omxhalf
	CMP.W	#127,D0
	BLE.S	omxnbig
	MOVEQ	#127,D0
omxnbig	CMP.W	#-128,D0
	BGE.S	omxokok
	MOVEQ	#-128,D0
	BRA.S	omxokok
omxhalf	ASR.W	#1,D0
omxokok	MOVE.B	D0,(A2)+
	BSR	UpdateMod
	BRA.S	mixloop
omxok2	ADDQ.L	#1,A2
	BSR	UpdateMod
	BRA.S	mixloop

mixskip	MOVE.L	si_pointer(PC),A1
	CLR.W	(A1)
	BSR	FreeBuffer
	BSR	RestorePtrCol
	BSR	WaitForButtonUp
	TST.W	DispOrRedraw
	BEQ	DisplaySample
	CLR.W	DispOrRedraw
	BRA	RedrawSample

DispOrRedraw	dc.w 0

AllocBuffer
	MOVE.L	D0,D7
	MOVE.L	D2,D0
	MOVE.L	D2,BufMemSize
	MOVE.L	#MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,BufMemPtr
	BEQ	OutOfMemErr
	MOVE.L	D7,A0
	MOVE.L	D0,A1
	MOVE.L	BufMemSize(PC),D0
	SUBQ.L	#1,D0
albloop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,albloop
	MOVE.L	BufMemPtr(PC),A1
	MOVE.L	D7,D0
	RTS

FreeBuffer
	MOVE.L	BufMemPtr(PC),D0
	BEQ	Return2
	MOVE.L	D0,A1
	MOVE.L	BufMemSize(PC),D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	CLR.L	BufMemPtr
	RTS

Echo	MOVE.L	SamplePos(PC),FlangePos
	BSR	WaitForButtonUp
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVEQ	#0,D2
	MOVE.W	SampleInfo(PC),D2 ; 22(A0)
	BEQ	ErrorRestoreCol
	LSL.W	#1,D2
	MOVE.L	si_pointer(PC),D0
	BEQ	ErrorRestoreCol
	MOVE.L	D0,A1
	MOVE.L	D0,A2
	MOVE.L	D0,A3
	MOVE.L	D0,A4
	ADD.L	D2,A3
	ADD.L	FlangePos(PC),A2
	CLR.L	ModOffset
	CLR.L	ModPos
flaloop	CMP.L	A3,A2
	BHS	flaskip
	MOVE.B	(A2),D0
	EXT.W	D0
	MOVE.B	(A1),D1
	EXT.W	D1
	ADD.W	D1,D0
	ASR.W	#1,D0
	MOVE.B	D0,(A2)+
	BSR	UpdateMod
	BRA	flaloop
flaskip	MOVE.L	si_pointer(PC),A1
	CLR.W	(A1)
	BSR	RestorePtrCol
	TST.B	HalfClipFlag
	BEQ	DisplaySample
	MOVE.W	SampleVol,-(SP)
	MOVE.W	#200,SampleVol
	BSR	DoChangeVol
	MOVE.W	(SP)+,SampleVol
	BRA	DisplaySample

Filter2	MOVE.W	#1,DispOrRedraw
Filter	CLR.B	RawKeyCode
	LEA	FilteringText,A0
	BSR	ShowStatusText
	MOVE.L	#1,FlangePos
	MOVE.B	ModSpeed(PC),-(SP)
	MOVE.B	HalfClipFlag,-(SP)
	CLR.B	ModSpeed
	CLR.B	HalfClipFlag
	BSR	DoMix
	MOVE.B	(SP)+,HalfClipFlag
	MOVE.B	(SP)+,ModSpeed
	BRA	ShowAllRight

FilteringText	dc.b 'filtering',0

Boost	BSR.S	Boost3
	BRA	DisplaySample
Boost2	BSR.S	Boost3
	BRA	RedrawSample
Boost3	CLR.B	RawKeyCode
	BSR	WaitForButtonUp
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	LEA	BoostingText,A0
	BSR	ShowStatusText
	MOVEQ	#0,D3
	MOVE.W	SampleInfo(PC),D3
	BEQ	booend
	LSL.W	#1,D3
	SUBQ.W	#1,D3
	MOVE.L	si_pointer(PC),D0
	BEQ	booend
	MOVE.L	D0,A1
	MOVE.L	MarkStartOfs(PC),D0
	BMI.S	booskp1
	MOVE.L	MarkEndOfs(PC),D1
	SUB.L	D0,D1
	BEQ.S	booskp1
	MOVE.L	D1,D3
	MOVE.L	SamStart(PC),A1
	ADD.L	D0,A1
booskp1	MOVEQ	#0,D0
booloop	MOVE.B	(A1),D1
	EXT.W	D1
	MOVE.W	D1,D2
	SUB.W	D0,D1
	MOVE.W	D2,D0
	TST.W	D1
	BMI	booneg
	ASR.W	#2,D1
	ADD.W	D1,D2
	BRA	booskip
booneg	NEG.W	D1
	ASR.W	#2,D1
	SUB.W	D1,D2
booskip	CMP.W	#127,D2
	BLE	booskp2
	MOVEQ	#127,D2
booskp2	CMP.W	#-128,D2
	BGE	booskp3
	MOVEQ	#-128,D2
booskp3	MOVE.B	D2,(A1)+
	DBRA	D3,booloop
	MOVE.L	si_pointer(PC),A1
	CLR.W	(A1)
booend	BSR	ShowAllRight
	BRA	RestorePtrCol
	
BoostingText	dc.b 'boosting',0,0

XFade	BSR	WaitForButtonUp
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVEQ	#0,D2
	MOVE.W	SampleInfo(PC),D2 ; 22(A0)
	BEQ	ErrorRestoreCol
	ADD.W	D2,D2
	MOVE.L	si_pointer(PC),D0
	BEQ	ErrorRestoreCol
	MOVE.L	D0,A1
	MOVE.L	D0,A2
	ADD.L	D2,A2
xfaloop	MOVE.B	(A1),D0
	EXT.W	D0
	MOVE.B	-(A2),D1
	EXT.W	D1
	ADD.W	D1,D0
	TST.B	HalfClipFlag
	BEQ	xfahalf
	CMP.W	#127,D0
	BLE	xfanbig
	MOVEQ	#127,D0
xfanbig	CMP.W	#-128,D0
	BGE	xfaokok
	MOVEQ	#-128,D0
	BRA	xfaokok
xfahalf	ASR.W	#1,D0
xfaokok	MOVE.B	D0,(A1)+
	MOVE.B	D0,(A2)
	CMP.L	A2,A1
	BLO.S	xfaloop
	MOVE.L	si_pointer(PC),A1
	CLR.W	(A1)
	BSR	RestorePtrCol
	BRA	DisplaySample

Backwards
	BSR	WaitForButtonUp
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVEQ	#0,D2
	MOVE.W	22(A5),D2
	BEQ	ErrorRestoreCol
	MOVE.L	si_pointer(PC),D0
	BEQ	ErrorRestoreCol
	MOVE.L	D0,A1
	MOVE.L	D0,A2
	ADD.L	D2,D2
	ADD.L	D2,A2
	MOVE.L	MarkStartOfs(PC),D0
	BMI.S	bacloop
	MOVE.L	MarkEndOfs(PC),D1
	SUB.L	D0,D1
	BEQ.S	bacloop
	MOVE.L	SamStart(PC),A1
	ADD.L	D0,A1
	MOVE.L	A1,A2
	ADD.L	D1,A2
bacloop	MOVE.B	(A1),D0
	MOVE.B	-(A2),(A1)+
	MOVE.B	D0,(A2)
	CMP.L	A2,A1
	BLO.S	bacloop
	MOVE.L	si_pointer(PC),A1
	CLR.W	(A1)
	BSR	RestorePtrCol
	BRA	DisplaySample

Upsample
	BSR	WaitForButtonUp
	LEA	UpsampleText,A0
	BSR	AreYouSure
	BNE	Return2
	BSR	TurnOffVoices
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVE.W	InsNum(PC),D0
	LSL.W	#2,D0
	LEA	SamplePtrs(PC),A0
	LEA	(A0,D0.W),A0
	MOVE.L	124(A0),D3
	CMP.L	#2,D3
	BLS	ErrorRestoreCol
	MOVE.L	(A0),D0
	BEQ	ErrorRestoreCol
	MOVE.L	D0,A2
	MOVE.L	D0,A4
	MOVE.L	D3,D4
	LSR.L	#1,D3
	BCLR	#0,D3
	MOVE.L	D3,D0
	MOVEQ	#MEMF_CHIP,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	TST.L	D0
	BEQ.S	upserro
	MOVE.L	D0,A3
	MOVE.L	D0,D2
	MOVE.L	D3,D5
	SUBQ.L	#1,D3
upsloop	MOVE.B	(A2)+,(A3)+
	ADDQ.L	#1,A2
	DBRA	D3,upsloop
	MOVE.L	A4,A1
	MOVE.L	D4,D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	MOVE.W	InsNum(PC),D0
	LSL.W	#2,D0
	LEA	SamplePtrs(PC),A0
	LEA	(A0,D0.W),A0
	MOVE.L	D2,(A0)
	MOVE.L	D5,124(A0)
	MOVE.L	D2,A0
	CLR.W	(A0)
	MOVE.W	22(A5),D0
	LSR.W	#1,D0
	MOVE.W	D0,22(A5)
	MOVE.W	26(A5),D0
	LSR.W	#1,D0
	MOVE.W	D0,26(A5)
	MOVE.W	28(A5),D0
	LSR.W	#1,D0
	BNE	upsskip2
	MOVEQ	#1,D0
upsskip2
	MOVE.W	D0,28(A5)
	BSR	ShowSampleInfo
	BSR	WaitForButtonUp
	BSR	RestorePtrCol
	BRA	RedrawSample

upserro	BSR	RestorePtrCol
	BRA	OutOfMemErr

DownSample
	BSR	WaitForButtonUp
	TST.W	InsNum
	BEQ	NotSampleNull
	LEA	DownSampleText,A0
	BSR	AreYouSure
	BNE	Return2
	BSR	TurnOffVoices
	BSR	StorePtrCol
	BSR	SetWaitPtrCol
	MOVE.L	si_pointer(PC),D0
	BEQ	ErrorRestoreCol
	MOVEQ	#0,D0
	MOVE.W	SampleInfo(PC),D0
	BEQ	ErrorRestoreCol
	LSL.L	#1,D0
	CMP.L	#$FFFE,D0
	BHI	ErrorRestoreCol
	LSL.L	#1,D0
	CMP.L	#$FFFE,D0
	BLS	dnsskip
	MOVE.L	#$FFFE,D0
dnsskip	MOVE.L	D0,BufMemSize
	MOVEQ	#MEMF_CHIP,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,BufMemPtr
	BEQ	SamMemError
	MOVE.L	si_pointer(PC),A1
	MOVE.L	D0,A2
	MOVE.L	BufMemSize,D3
	LSR.L	#1,D3
	SUBQ.L	#1,D3
dnsloop	MOVE.B	(A1)+,D0
	MOVE.B	D0,(A2)+
	MOVE.B	D0,(A2)+
	DBRA	D3,dnsloop
	BSR	FreeSample
	LEA	SamplePtrs,A0
	MOVE.W	InsNum(PC),D0
	LSL.W	#2,D0
	MOVE.L	BufMemPtr,(A0,D0.W)
	MOVE.L	BufMemSize,124(A0,D0.W)
	MOVE.W	22(A5),D0
	LSL.W	#1,D0
	CMP.W	#$7FFF,D0
	BLS	dnsok1
	MOVE.W	#$7FFF,D0
dnsok1	MOVE.W	D0,22(A5)
	MOVE.W	26(A5),D0
	LSL.W	#1,D0
	CMP.W	#$7FFF,D0
	BLS	dnsok2
	MOVE.W	#$7FFF,D0
dnsok2	MOVE.W	D0,26(A5)
	MOVE.W	28(A5),D0
	CMP.W	#1,D0
	BEQ	dnsok3
	LSL.W	#1,D0
	CMP.W	#$7FFF,D0
	BLS	dnsok3
	MOVE.W	#$7FFF,D0
dnsok3	MOVE.W	D0,28(A5)
	BSR	ShowSampleInfo
	BSR	WaitForButtonUp
	BSR	RestorePtrCol
	BRA	RedrawSample

SamplePos	dc.l	0
SampleVol	dc.w	100
FlangePos	dc.l	0
ModSpeed	dc.b	0
HalfClipFlag	dc.b	0
ModPos		dc.l	0
ModOffset	dc.l	0
BufMemPtr	dc.l	0
BufMemSize	dc.l	0

;---- Save Song ----

SaveSong
	BSR	StopIt
	CLR.B	RawKeyCode
	MOVE.L	SongDataPtr(PC),A0
	LEA	SampleLengthAdd(PC),A1
	MOVEQ	#0,D0
	MOVEQ	#2,D1
sadloop	MOVE.W	(A1,D1.W),D2
	ADD.W	D2,42(A0,D0.W)
	ADD.W	#30,D0
	ADDQ.W	#2,D1
	CMP.W	#62,D1
	BNE	sadloop

	LEA	sd_pattpos(A0),A0
	MOVEQ	#0,D0
	MOVEQ	#0,D1
	MOVEQ	#0,D2
	CLR.W	HighPattNum
ssloop	MOVE.B	0(A0,D0.W),D1
	ADDQ.W	#1,D0
	CMP.W	#128,D0
	BHI	DoSaveSong
	MOVE.W	HighPattNum(PC),D2
	CMP.W	D2,D1
	BLS	ssloop
	MOVE.W	D1,HighPattNum
	BRA	ssloop

DoSaveSong
	LEA	SongsPath2,A0
	JSR	CopyPath
	MOVE.L	SongDataPtr(PC),A0
	MOVEQ	#19,D0
dssloop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,dssloop
	MOVE.L	#FileName,FileNamePtr
	MOVE.L	SongDataPtr(PC),DiskDataPtr
	MOVE.L	#1084,DiskDataLength
	MOVEQ	#0,D0
	MOVE.W	HighPattNum,D0
	ADDQ.L	#1,D0
	LSL.L	#8,D0
	LSL.L	#2,D0
	ADD.L	D0,DiskDataLength
	MOVE.B	PackMode,D0
	BTST	#0,D0
	BEQ	dssnocr
	LEA	CrunchingText(PC),A0
	BSR	ShowStatusText
	BSR	Cruncher
dssnocr	BSR	DoSaveData
	MOVE.B	PackMode,D0
	BTST	#0,D0
	BEQ	dssnoc2
	LEA	DecrunchingText(PC),A0
	BSR	ShowStatusText
	BSR	Decruncher
dssnoc2	MOVE.L	SongDataPtr(PC),A0
	LEA	SampleLengthAdd(PC),A1
	MOVEQ	#0,D0
	MOVEQ	#2,D1
ssuloop	MOVE.W	(A1,D1.W),D2
	SUB.W	D2,42(A0,D0.W)
	ADD.W	#30,D0
	ADD.W	#2,D1
	CMP.W	#62,D1
	BNE	ssuloop
	BSR	ShowAllRight
	BRA	SetNormalPtrCol

Cruncher
	BSR	SetNormalPtrCol
	MOVE.L	SongDataPtr(PC),A0
	MOVE.L	A0,A1
	LEA	12(A1),A1
	MOVE.L	A1,SongPlus12Ptr
	MOVE.L	A0,A1
	ADD.L	#70716,A1	; hoeh...
	MOVE.L	A1,SongPlus70kPtr
	MOVE.L	A0,A1
	ADD.L	#70716,A1
	SUB.L	DiskDataLength(PC),A1
	MOVE.L	A1,EndOfSongPtr
	MOVE.L	DiskDataLength(PC),RealSongLength
	MOVE.L	SongDataPtr(PC),A0
	ADD.L	DiskDataLength(PC),A0
	MOVE.L	SongDataPtr(PC),A1
	ADD.L	#70716,A1
	MOVE.L	DiskDataLength(PC),D0
cloop	MOVE.B	-(A0),-(A1)
	SUBQ.L	#1,D0
	BNE	cloop
	BSR	DoCrunch
	SUB.L	SongPlus12Ptr(PC),A2
	MOVE.L	A2,D0
	MOVE.L	D0,CrunchedSongLength
	MOVE.L	D0,D1
	ADD.L	SongPlus12Ptr(PC),D1
	MOVE.L	D1,DontKnowWhat
	MOVE.L	SongDataPtr(PC),A0
	MOVE.L	#'PACK',(A0)
	MOVE.L	CrunchedSongLength,4(A0)
	MOVE.L	RealSongLength(PC),8(A0)
	MOVE.L	SongDataPtr(PC),DiskDataPtr
	MOVE.L	CrunchedSongLength(PC),D0
	ADD.L	#12,D0
	MOVE.L	D0,DiskDataLength
	RTS	
 
EndOfSongPtr		dc.l	0
SongPlus70kPtr		dc.l	0
RealSongLength		dc.l	0
SongPlus12Ptr		dc.l	0
DontKnowWhat		dc.l	0
CrunchedSongLength	dc.l	0

DoCrunch
	MOVE.L	EndOfSongPtr(PC),A0
	MOVE.L	SongPlus70kPtr(PC),A1
	MOVE.L	SongPlus12Ptr(PC),A2
	MOVEQ	#1,D2
	CLR.W	D1
dcloop	BSR	DoCrunch2
	TST.B	D0
	BEQ	dcskip
	ADDQ.W	#1,D1
	CMP.W	#$0108,D1
	BNE	dcskip
	BSR	DoCrunch6
dcskip	CMP.L	A0,A1
	BGT	dcloop
	BSR	DoCrunch6
	BRA	DoCrunch8
 
DoCrunch2
	MOVE.L	A0,A3
	LEA	127(A3),A3
	CMP.L	A1,A3
	BLE	dc2skip
	MOVE.L	A1,A3
dc2skip	MOVEQ	#1,D5
	MOVE.L	A0,A5
	ADDQ.L	#1,A5
dc2_1	MOVE.B	(A0),D3
	MOVE.B	1(A0),D4
	MOVE.W	D3,$DFF1A2
dc2loop	CMP.B	(A5)+,D3
	BNE	dc2skip2
	CMP.B	(A5),D4
	BEQ	DoCrunch3
dc2skip2
	CMP.L	A5,A3
	BGT	dc2loop
	BRA	dc4_3
 
DoCrunch3
	SUBQ.L	#1,A5
	MOVE.L	A0,A4
dc3loop	MOVE.B	(A4)+,D3
	CMP.B	(A5)+,D3
	BNE	dc3skip
	CMP.L	A5,A3
	BGT	dc3loop
dc3skip	MOVE.L	A4,D3
	SUB.L	A0,D3
	SUBQ.L	#1,D3
	CMP.L	D3,D5
	BGE	dc4_2
	MOVE.L	A5,D4
	SUB.L	A0,D4
	SUB.L	D3,D4
	SUBQ.L	#1,D4
	CMP.L	#4,D3
	BLE	DoCrunch4
	MOVEQ	#6,D6
	CMP.L	#$00000101,D3
	BLT	dc3skip2
	MOVE.W	#$0100,D3
dc3skip2
	BRA	dc4_1
 
DoCrunch4
	MOVE.W	D3,D6
	SUBQ.W	#2,D6
	LSL.W	#1,D6
dc4_1	LEA	CrunchData3(PC),A6
	CMP.W	0(A6,D6.W),D4
	BGE	dc4_2
	MOVE.L	D3,D5
	MOVE.L	D4,CrunchData1
	MOVE.B	D6,CrunchData2
dc4_2	CMP.L	A5,A3
	BGT	dc2_1
dc4_3	CMP.L	#1,D5
	BEQ	DoCrunch5
	BSR	DoCrunch6
	MOVE.B	CrunchData2(PC),D6
	MOVE.L	CrunchData1(PC),D3
	MOVE.W	8(A6,D6.W),D0
	BSR	DoCrunch7
	MOVE.W	$0010(A6,D6.W),D0
	BEQ	dc4skip
	MOVE.L	D5,D3
	SUBQ.W	#1,D3
	BSR	DoCrunch7
dc4skip	MOVE.W	$0018(A6,D6.W),D0
	MOVE.W	$0020(A6,D6.W),D3
	BSR	DoCrunch7
	ADDQ.W	#1,$0028(A6,D6.W)
	ADD.L	D5,A0
	CLR.B	D0
	RTS	
 
DoCrunch5
	MOVE.B	(A0)+,D3
	MOVEQ	#8,D0
	BSR	DoCrunch7
	MOVEQ	#1,D0
	RTS	
 
CrunchData1	dc.l	0
CrunchData2	dc.b	0,0
CrunchData3	dc.w	$0100,$0200,$0400,$1000,8,9,10,8
		dc.w	0,0,0,8,2,3,3,3,1,4,5,6,0,0,0,0
CrunchData4	dc.w	0
CrunchData5	dc.w	0

DoCrunch6
	TST.W	D1
	BEQ	Return2
	MOVE.W	D1,D3
	CLR.W	D1
	CMP.W	#9,D3
	BGE	dc6_2
	ADDQ.W	#1,CrunchData4
	SUBQ.W	#1,D3
	MOVEQ	#5,D0
	BRA	DoCrunch7
 
dc6_2	ADDQ.W	#1,CrunchData5
	SUB.W	#9,D3
	OR.W	#$0700,D3
	MOVEQ	#11,D0
DoCrunch7
	SUBQ.W	#1,D0
dc7loop	LSR.L	#1,D3
	ROXL.L	#1,D2
	BCS	dc8_2
	DBRA	D0,dc7loop
	RTS	
 
DoCrunch8
	CLR.W	D0
dc8_2	MOVE.L	D2,(A2)+
	MOVEQ	#1,D2
	DBRA	D0,dc7loop
	RTS


Decruncher
	MOVE.L	SongDataPtr(PC),A0
	LEA	12(A0),A0
	MOVE.L	EndOfSongPtr(PC),A1
	MOVE.L	CrunchedSongLength(PC),D0
	MOVE.L	RealSongLength(PC),D1
	BSR	DoDecrunch
	MOVE.L	EndOfSongPtr(PC),A0
	MOVE.L	SongDataPtr(PC),A1
	MOVE.L	RealSongLength(PC),D0
ddcloop	MOVE.B	(A0)+,(A1)+
	SUBQ.L	#1,D0
	BNE	ddcloop
	MOVE.L	SongDataPtr(PC),A0
	ADD.L	#$0001143C,A0
	MOVE.L	SongDataPtr(PC),A1
	ADD.L	RealSongLength(PC),A1
	SUB.L	A1,A0
	MOVE.L	A0,D0
ddcloop2
	CLR.B	(A1)+
	SUBQ.L	#1,D0
	BNE	ddcloop2
	RTS	

DoDecrunch
	ADD.L	D0,A0
	MOVE.L	D1,A2
	ADD.L	A1,A2
	MOVE.L	-(A0),D0
dec_1	LSR.L	#1,D0
	BNE.S	decskip
	BSR.S	dec5
decskip	BCS.S	dec3
	MOVEQ	#8,D1
	MOVEQ	#1,D3
	LSR.L	#1,D0
	BNE.S	decskip2
	BSR.S	dec5
decskip2
	BCS.S	dec4_1
	MOVEQ	#3,D1
	CLR.W	D4
dec_2	BSR.S	dec6
	MOVE.W	D2,D3
	ADD.W	D4,D3
decloop1
	MOVEQ	#7,D1
decloop2
	LSR.L	#1,D0
	BNE.S	decskip3
	BSR.S	dec5
decskip3
	ROXL.L	#1,D2
	DBRA	D1,decloop2
	MOVE.B	D2,-(A2)
	DBRA	D3,decloop1
	BRA.S	dec4_3
 
dec2	MOVEQ	#8,D1
	MOVEQ	#8,D4
	BRA.S	dec_2
 
dec3	MOVEQ	#2,D1
	BSR.S	dec6
	CMP.B	#2,D2
	BLT.S	dec4
	CMP.B	#3,D2
	BEQ.S	dec2
	MOVEQ	#8,D1
	BSR.S	dec6
	MOVE.W	D2,D3
	MOVE.W	#8,D1
	BRA.S	dec4_1
 
dec4	MOVE.W	#9,D1
	ADD.W	D2,D1
	ADDQ.W	#2,D2
	MOVE.W	D2,D3
dec4_1	BSR.S	dec6
dec4_2	SUBQ.W	#1,A2
	MOVE.B	0(A2,D2.W),(A2)
	DBRA	D3,dec4_2
dec4_3	CMP.L	A2,A1
	BLT.S	dec_1
	RTS	
 
dec5	MOVE.L	-(A0),D0
	MOVE.W	D0,$DFF1A2
	MOVE	#$10,CCR
	ROXR.L	#1,D0
	RTS	
 
dec6	SUBQ.W	#1,D1
	CLR.W	D2
dec6loop
	LSR.L	#1,D0
	BNE.S	dec6skip
	MOVE.L	-(A0),D0
	MOVE.W	D0,$DFF1A2
	MOVE	#$10,CCR
	ROXR.L	#1,D0
dec6skip
	ROXL.L	#1,D2
	DBRA	D1,dec6loop
	RTS	
 
LoadModule
	MOVE.W	#1,LoadInProgress
	BSR	DoClearSong
	BSR	ClrSampleInfo
	JSR	SetDiskPtrCol
	LEA	ModulesPath2,A0
	JSR	CopyPath
	LEA	DirInputName,A0
	MOVEQ	#DirNameLength-1,D0
lmloop2	MOVE.B	(A0)+,(A1)+
	DBRA	D0,lmloop2
	LEA	LoadingModuleText(PC),A0
	BSR	ShowStatusText
	MOVE.L	DOSbase(PC),A6
	MOVE.L	#FileName,D1
	MOVE.L	#1005,D2
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BEQ	CantOpenFile
	MOVE.L	D0,D1
	MOVE.L	SongDataPtr(PC),D2
	MOVE.L	#1084,D3
	JSR	LVORead(A6)
	MOVE.L	SongDataPtr(PC),A0
	CMP.L	#'PP20',(A0)
	BEQ	PowerPacked
	MOVE.B	#127,sd_numofpatt+1(A0) ; Set maxpatt to 127
	CMP.L	#'M.K.',sd_mahokakt(A0)	; M.K. again...
	BEQ	lmskip
	BSR	MahoneyAndKaktus
	BNE	lmskip
	MOVE.L	FileHandle(PC),D1
	MOVE.L	#600,D2
	MOVEQ	#-1,D3
	JSR	LVOSeek(A6)
lmskip	LEA	LoadingModuleText(PC),A0
	BSR	ShowStatusText
	MOVE.L	SongDataPtr(PC),A0
	LEA	sd_pattpos(A0),A0
	MOVEQ	#0,D0
	MOVE.B	-1(A0),D0
	MOVEQ	#0,D3
lmloop3	CMP.B	(A0)+,D3
	BHI	lmskip2
	MOVE.B	-1(A0),D3
lmskip2	DBRA	D0,lmloop3
	ADDQ.W	#1,D3
	MULU	#1024,D3
	MOVE.L	FileHandle(PC),D1
	MOVE.L	SongDataPtr(PC),D2
	ADD.L	#1084,D2
	MOVE.L	DOSBase,A6
	JSR	LVORead(A6)
	CLR.L	PatternNumber
	CLR.L	CurrPos
	BSR	RedrawPattern
	CLR.W	ScrPattPos
	BSR	SetScrPatternPos
	MOVE.W	#1,InsNum
	BSR	CheckAbort
	BEQ	rmiend
	TST.B	AutoExitFlag
	BEQ	readinstrloop
	JSR	ExitFromDir
readinstrloop
	TST.B	NosamplesFlag
	BNE	rmiend
	BSR	CheckAbort
	BEQ	rmiend
	BSR	ShowSampleInfo
	MOVE.W	InsNum(PC),TuneUp
	JSR	DoShowFreeMem
	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	InsNum(PC),D7
	MULU	#30,D7
	MOVEQ	#0,D0
	MOVE.W	12(A0,D7.W),D0
	BEQ	rminext
	ADD.L	D0,D0
	MOVE.L	4.W,A6
	MOVE.L	#MEMF_CHIP!MEMF_CLEAR,D1
	MOVE.L	D0,-(SP)
	JSR	LVOAllocMem(A6)
	MOVE.L	(SP)+,D6
	TST.L	D0
	BNE	ReadModInstrument
	BSR	OutOfMemErr
	MOVE.L	DOSbase(PC),A6
	MOVE.L	FileHandle(PC),D1
	MOVE.L	D6,D2
	MOVEQ	#0,D3
	BRA	rminext
 
ReadModInstrument
	MOVE.W	InsNum(PC),D7
	LSL.W	#2,D7
	LEA	SamplePtrs(PC),A0
	MOVE.L	D0,0(A0,D7.W)
	MOVE.L	D6,124(A0,D7.W)
	MOVE.L	DOSbase(PC),A6
	MOVE.L	FileHandle(PC),D1
	MOVE.L	D0,D2
	MOVE.L	D6,D3
	JSR	LVORead(A6)
	BSR	RedrawSample
rminext	ADDQ.W	#1,InsNum
	CMP.W	#32,InsNum
	BNE	readinstrloop
rmiend	MOVE.L	FileHandle(PC),D1
	MOVE.L	DOSbase(PC),A6
	JSR	LVOClose(A6)
	MOVE.W	#1,InsNum
	MOVE.L	#6,CurrSpeed
	CLR.W	LoadInProgress
	BSR	ShowAllRight
	JSR	SetNormalPtrCol
	BSR	CheckInstrLengths
	BSR	ShowSampleInfo
	BSR	RedrawSample
	JMP	DoShowFreeMem

PowerPacked
	LEA	PowerPackedText,A0
	BSR	ShowStatusText
	JSR	SetErrorPtrCol
	BSR	DoClearSong
	BSR	ClrSampleInfo
	BRA	rmiend

PowerPackedText	dc.b "Powerpacker mod.!",0



SaveModule
	JSR	StorePtrCol
	JSR	SetDiskPtrCol
	MOVE.L	SongDataPtr(PC),A0
	LEA	sd_pattpos(A0),A0
	MOVEQ	#0,D0
	MOVEQ	#0,D1
	MOVEQ	#0,D2
	CLR.W	HighPattNum
smloop	MOVE.B	0(A0,D0.W),D1
	ADDQ.W	#1,D0
	CMP.W	#128,D0
	BHI	DoSaveModule
	MOVE.W	HighPattNum(PC),D2
	CMP.W	D2,D1
	BLS	smloop
	MOVE.W	D1,HighPattNum
	BRA	smloop
 
DoSaveModule
	LEA	ModulesPath2,A0
	JSR	CopyPath
	MOVE.B	#'m',(A1)+
	MOVE.B	#'o',(A1)+
	MOVE.B	#'d',(A1)+
	MOVE.B	#'.',(A1)+
	MOVE.L	SongDataPtr(PC),A0
	MOVEQ	#19,D0
dsmloop2
	MOVE.B	(A0)+,(A1)+
	DBRA	D0,dsmloop2
	MOVE.L	SongDataPtr(PC),A0
	MOVE.L	A0,DiskDataPtr
	MOVE.L	#1084,DiskDataLength
	MOVEQ	#0,D0
	MOVE.W	HighPattNum(PC),D0
	ADDQ.L	#1,D0
	LSL.L	#8,D0
	LSL.L	#2,D0
	ADD.L	D0,DiskDataLength	; Add 1024 x NumOfPatt
	MOVE.B	#127,sd_numofpatt+1(A0) ; Set maxpatt to 127
	MOVE.L	#'M.K.',sd_mahokakt(A0)	; M.K. again...
	LEA	SavingModuleText(PC),A0
	BSR	ShowStatusText
	BTST	#2,$DFF016
	BEQ	CantOpenFile
	BSR	OpenModForWrite
	BNE	CantOpenFile
	BSR	WriteModuleData
	BNE	WriteModError
	BTST	#2,$DFF016
	BEQ	WriteModError
	MOVE.W	InsNum(PC),SaveInstrNum
	MOVEQ	#1,D6
saveinstrloop
	MOVE.W	D6,InsNum
	BSR	ShowSampleInfo
	BSR	WriteInstrument
	TST.L	D0
	BNE.S	WriteModError
	ADDQ.B	#1,D6
	CMP.B	#32,D6
	BNE	saveinstrloop
	BSR	CloseWriteMod
	MOVE.W	SaveInstrNum(PC),InsNum
	BSR	ShowSampleInfo
	BSR	ShowAllRight
	JMP	RestorePtrCol

SaveInstrNum	dc.w 0

WriteModError
	BSR	CloseWriteMod
	BRA	CantSaveFile

WriteInstrument
	MOVE.W	D6,D0
	LSL.W	#2,D0
	LEA	SamplePtrs(PC),A0
	MOVE.L	0(A0,D0.W),D1
	BEQ	Return2
	MOVE.L	124(A0,D0.W),D0
	BEQ	Return2
	MOVE.L	D1,DiskDataPtr
	MOVE.L	SongDataPtr(PC),A0
	LEA	12(A0),A0
	MOVE.W	D6,D0
	MULU	#30,D0
	ADD.L	D0,A0
	MOVEQ	#0,D0
	MOVE.W	(A0),D0
	LSL.L	#1,D0
	MOVE.L	D0,DiskDataLength
	BNE.S	WriteModuleData
	RTS

OpenModForWrite
	MOVE.L	DOSbase(PC),A6
	MOVE.L	#FileName,D1
	MOVE.L	#1006,D2
	JSR	LVOOpen(A6)
	MOVE.L	D0,D7
	BEQ.S	wmfailed
	MOVEQ	#0,D0
	RTS

WriteModuleData
	MOVE.L	DOSbase(PC),A6
	MOVE.L	D7,D1
	MOVE.L	DiskDataPtr(PC),D2
	MOVE.L	DiskDataLength(PC),D3
	BEQ	.okok
	JSR	LVOWrite(A6)
	CMP.L	DiskDataLength(PC),D3
	BNE.S	wmfailed
;	JSR	LVOIoErr(A6)
;	TST.L	D0
;	BNE.S	wmfailed
	MOVEQ	#0,D0
.okok	RTS

wmfailed
	MOVEQ	#-1,D0
	RTS	

CloseWriteMod
	MOVE.L	DOSbase(PC),A6
	MOVE.L	D7,D1
	JMP	LVOClose(A6)

;---- Load PLST ----

LoadPLST
	LEA	LoadPLSTText,A0
	JSR	AreYouSure
	BNE	Return2
	JSR	WaitForButtonUp
	LEA	LoadingPLSTText(PC),A0
	BSR	ShowStatusText
DoLoadPLST
	JSR	StorePtrCol
	LEA	PTPath,A0
	JSR	CopyPath
	LEA	PLSTName(PC),A0
	MOVEQ	#4,D0
dlploop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,dlploop
	MOVE.L	#FileName,D1
	MOVE.L	#1005,D2
	MOVE.L	DOSbase(PC),A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,D7
	BEQ	PLSTOpenErr
	JSR	SetDiskPtrCol
	CLR.L	PresetTotal
	BSR.S	AllocPLST
	MOVE.L	PLSTmem(PC),D2
	BEQ	PLSTMemErr2
	MOVE.L	D7,D1
	MOVE.L	PLSTallocsize,D3
	MOVE.L	DOSBase(PC),A6
	JSR	LVORead(A6)
	MOVE.L	D0,MaxPLSTOffset
	DIVU	#30,D0
	MOVE.W	D0,PresetTotal
cloplst	MOVE.L	D7,D1
	MOVE.L	DOSbase(PC),A6
	JSR	LVOClose(A6)
;	BSR	PLSTCheckNum
;	BSR	ShowAllRight
	JMP	RestorePtrCol

PLSTname	dc.b	'PLST',0,0

AllocPLST
	BSR.S	FreePLST
	MOVE.W	MaxPLSTEntries,D0
	BEQ	Return2
	MULU	#30,D0
	MOVE.L	D0,PLSTAllocSize
	MOVE.L	#MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,PLSTmem
	MOVE.W	MaxPLSTEntries,MaxPLSTEntries2
	RTS

FreePLST
	CLR.W	MaxPLSTEntries2
	CLR.L	MaxPLSTOffset
	CLR.W	PresetTotal
	MOVE.L	PLSTmem,D0
	BEQ	Return2
	MOVE.L	D0,A1
	MOVE.L	PLSTAllocSize,D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	CLR.L	PLSTmem
	RTS

PLSTMemErr2
	BSR.S	PLSTMemErr
	BRA	cloplst

PLSTMemErr
	TST.W	MaxPLSTEntries
	BEQ.S	reptrco
	BSET	#0,InitError
	LEA	PLSTMemText,A0
plster	BSR.S	ShowStatusText
	MOVE.W	#50,WaitTime
	JMP	ErrorRestoreCol
reptrco	JMP	RestorePtrCol
PLSTOpenErr
	TST.W	MaxPLSTEntries
	BEQ.S	reptrco
	BSET	#1,InitError
	LEA	PLSTOpenText,A0
	BRA.S	plster

PLSTMemText	dc.b 'no mem for plst !',0
PLSTOpenText	dc.b 'plst not found ! ',0
InitError	dc.b 0,0


ShowAllRight
	MOVEM.L	D0-D7/A0-A6,-(SP)
	LEA	AllRightText(PC),A0
	BSR.S	ShowStatusText
	MOVEM.L	(SP)+,D0-D7/A0-A6
	RTS	
 
ShowStatusText
	MOVE.L	TextBplPtr(PC),A1
	ADD.L	#5131,A1
	MOVEQ	#4,D0
stloop1	MOVEQ	#16,D1
stloop2	CLR.B	(A1)+
	DBRA	D1,stloop2
	LEA	23(A1),A1
	DBRA	D0,stloop1
	MOVE.L	A0,A1
	MOVE.W	#5131,D1
	MOVEQ	#-1,D0
stloop3	ADDQ.W	#1,D0
	TST.B	(A1)+
	BNE.S	stloop3
	BRA	ShowText3

;---- Redraw Pattern ----

RedrawPattern
	TST.W	SamScrEnable
	BNE	Return2
	SF	PattRfsh
	MOVE.W	#5121,TextOffset
	MOVE.W	PatternNumber+2(PC),WordNumber
	BSR	Print2DecDigits ; Print PatternNumber
	MOVE.L	SongDataPtr(PC),A6
	LEA	$043C(A6),A6
	MOVE.L	PatternNumber,D6
	LSL.L	#8,D6
	LSL.L	#2,D6
	ADD.L	D6,A6
	MOVE.W	#7521,TextOffset
	CLR.L	PPattPos
	LEA	PeriodTable(PC),A2
	LEA	RedrawBuffer(PC),A3
	LEA	FastHexTable(PC),A4
	MOVEQ	#'0',D4
	MOVE.B	BlankZeroFlag,D5
	MOVEQ	#63,D6
rpnxpos	MOVEQ	#3,D7
	MOVE.W	PPattPos+2(PC),WordNumber
	BSR	Print2DecDigits ; Print PatternPosition
	ADDQ.W	#1,TextOffset
	ADDQ.L	#1,PPattPos
rploop	MOVEQ	#0,D0
	MOVEQ	#0,D1
	MOVE.W	(A6),D1
	AND.W	#$0FFF,D1
	BNE.S	rpfind
	MOVE.L	#'--- ',(A3)
	BRA.S	rpskip
rpfind	MOVEQ	#36,D2
rpfindloop
	CMP.W	(A2,D0.W),D1
	BEQ.S	rpfound
	ADDQ.L	#2,D0
	DBRA	D2,rpfindloop
	MOVE.L	#'??? ',(A3)
	BRA.S	rpskip
rpfound	LSL.L	#1,D0
	ADD.L	NoteNamesPtr(PC),D0
	MOVE.L	D0,A0
	MOVE.L	(A0),(A3)
rpskip	MOVE.B	(A6)+,D0
	LSR.B	#4,D0
	ADD.B	#'0',D0
	MOVE.B	D0,3(A3)
	MOVE.B	(A6)+,D0
	MOVEQ	#0,D0
	MOVE.B	(A6)+,D0
	ADD.W	D0,D0
	MOVE.W	(A4,D0.W),4(A3)
	MOVEQ	#0,D0
	MOVE.B	(A6)+,D0
	ADD.W	D0,D0
	MOVE.W	(A4,D0.W),6(A3)

	TST.B	D5
	BEQ.S	rpskp3
	CMP.B	3(A3),D4
	BNE.S	rpskp3
	MOVE.B	#' ',3(A3)
rpskp3	MOVE.W	#9,TextLength
	MOVE.L	A3,ShowTextPtr
	BSR	ShowText
	DBRA	D7,rploop  ; Next Channel
	ADD.W	#241,TextOffset
	DBRA	D6,rpnxpos ; Next PattPos
	RTS

RedrawBuffer	dc.b '---00000 ',0


ShowPosition
	MOVE.L	SongDataPtr(PC),A0
	LEA	952(A0),A0
	ADD.L	CurrPos(PC),A0
	MOVE.B	(A0),WordNumber+1
	MOVE.B	(A0),PattRfshNum+3
	MOVEQ	#0,D1
	MOVE.B	(A0),D1
	CMP.B	LongFFFF+3(PC),D1
	BEQ.S	spskip2
	ST	PattRfsh
spskip2	MOVE.W	#608,TextOffset
	MOVE.B	(A0),LongFFFF+3
	CMP.W	#0,CurrScreen
	BEQ.S	spokok
	CMP.W	#1,CurrScreen
	BEQ.S	spokok
	CMP.W	#6,CurrScreen
	BEQ.S	spokok
	RTS
spokok	BSR	Print4DecDigits
	MOVE.W	CurrPos+2,WordNumber
	MOVE.W	#168,TextOffset
	BRA	Print4DecDigits


ShowSongLength
	CMP.W	#2,CurrScreen
	BEQ.S	ShowSongName
	CMP.W	#3,CurrScreen
	BEQ.S	ShowSongName
	CMP.W	#4,CurrScreen
	BEQ	Return2
	CMP.W	#5,CurrScreen
	BEQ.S	ShowSongName
	MOVE.L	SongDataPtr(PC),A0
	LEA	sd_numofpatt(A0),A0
	MOVE.L	A0,CurrCmds
	CLR.W	WordNumber
	MOVE.B	(A0),WordNumber+1
	MOVE.W	#1048,TextOffset
	BSR	Print4DecDigits
	MOVE.L	CurrCmds(PC),A0
ShowSongName
	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	#4133,D1
	MOVEQ	#20,D0
	BRA	ShowText3


ShowSampleInfo	
	MOVEQ	#0,D0
	MOVE.W	InsNum(PC),D0
	MOVE.W	D0,PlayInsNum
	BNE.S	ssiskipit
	MOVE.W	LastInsNum(PC),D0
ssiskipit
	MOVE.W	D0,D1
	LSL.L	#2,D0
	LEA	SamplePtrs(PC),A0
	MOVE.L	(A0,D0.W),si_pointer
	MOVE.L	SongDataPtr(PC),A0
	LEA	-10(A0),A0
	MOVE.W	D1,D0
	MULU	#30,D0
	ADD.L	D0,A0
	MOVE.L	A0,CurrCmds
	MOVE.L	22(A0),SampleInfo
	MOVE.L	26(A0),si_long
	TST.B	NoSampleInfo
	BNE	ssiskip

	MOVE.L	A0,-(SP)
	MOVE.B	#' ',FineTuneSign
	MOVE.B	24(A0),D0
	BEQ.S	dopfitu
	MOVE.B	#'+',FineTuneSign
	BTST	#3,D0
	BEQ.S	dopfitu
	MOVE.B	#'-',FineTuneSign
	MOVEQ	#16,D1
	SUB.B	D0,D1
	MOVE.B	D1,D0
dopfitu	MOVE.W	#1491,TextOffset
	BSR	PrintHexDigit  ; FineTune
	LEA	FineTuneSign(PC),A0
	MOVE.W	#1490,D1
	MOVEQ	#1,D0
	BSR	ShowText3  ; FineTuneSign
	MOVE.L	(SP)+,A0

	CLR.W	WordNumber
	MOVE.B	25(A0),WordNumber+1
	MOVE.W	WordNumber,VolumeEfx
	OR.W	#$0C00,VolumeEfx
	MOVE.W	#2368,TextOffset
	BSR	PrintHexWord  ; Volume

	MOVEQ	#0,D0
	MOVE.W	SampleInfo(PC),D0
	LSL.L	#1,D0
	MOVE.W	D0,WordNumber
	MOVE.L	SamplePos,D2
	CMP.L	D0,D2
	BLS.S	ShowLen
	MOVE.L	D0,SamplePos
	BSR	ShowPos
	MOVEQ	#0,D0
	MOVE.W	SampleInfo(PC),D0
	LSL.L	#1,D0
	MOVE.W	D0,WordNumber
ShowLen	MOVE.W	#2808,TextOffset
	BSR	PrintHexWord  ; Length

	MOVE.W	si_long,D0
	ADD.W	D0,D0
	MOVE.W	D0,WordNumber
	MOVE.W	#3248,TextOffset
	BSR	PrintHexWord  ; Repeat

	MOVE.W	si_long+2,D0
	ADD.W	D0,D0
	MOVE.W	D0,WordNumber
	MOVE.W	#3688,TextOffset
	BSR	PrintHexWord  ; RepLen

	BSR.S	ssiinst

ssiskip	TST.B	NoSampleInfo
	BEQ.S	ssiskp2
	CMP.W	#5,CurrScreen
	BEQ.S	ssiskp2
	CMP.W	#3,CurrScreen
	BNE	Return2
ssiskp2	MOVE.L	CurrCmds(PC),A0
	MOVE.W	#4573,D1
	MOVEQ	#22,D0
	BRA	ShowText3  ; SampleName

ssiinst	TST.B	NoSampleInfo
	BNE	Return2
	MOVE.W	InsNum(PC),WordNumber
	MOVE.W	#1928,TextOffset
	BRA	PrintHexWord  ; SampleNumber

FineTuneSign	dc.b ' ',0
VolumeEfx	dc.w 0

;---- Print Decimal Digits ----
 
Print2DecDigits
	MOVE.W	#2,TextLength
	MOVEQ	#0,D1
	MOVE.W	WordNumber(PC),D1
	LEA	NumberText(PC),A0
	BRA.S	pdig
 
Print3DecDigits
	MOVE.W	#3,TextLength
	MOVEQ	#0,D1
	MOVE.W	WordNumber(PC),D1
	LEA	NumberText(PC),A0
	BRA.S	pdig100

Print4DecDigits
	MOVE.W	#4,TextLength
	MOVEQ	#0,D1
	MOVE.W	WordNumber(PC),D1
	LEA	NumberText(PC),A0
	DIVU	#1000,D1
	BSR.S	DoOneDigit
pdig100	DIVU	#100,D1
	BSR.S	DoOneDigit
pdig	DIVU	#10,D1
	BSR.S	DoOneDigit
	BSR.S	DoOneDigit
	MOVE.L	#NumberText,A0
	BRA	ShowText2

DoOneDigit
	ADD.B	#'0',D1
	MOVE.B	D1,(A0)+
	CLR.W	D1
	SWAP	D1
	RTS

Print6DecDigits
	LEA	NumberText,A0
	MOVE.L	D0,D1
	CMP.L	#1000000,D1
	BHS	toobig

	SWAP	D1
	CLR.W	D1
	SWAP	D1
	DIVU	#10,D1
	SWAP	D1
	ADD.B	#'0',D1
	MOVE.B	D1,5(A0)
	CMP.L	#10,D0
	BLO.S	p6zero1

	DIVU	#100,D0
	MOVE.L	D0,D1
	CLR.W	D0
	SWAP	D0
	DIVU	#10,D0
	ADD.B	#'0',D0
	MOVE.B	D0,4(A0)
	AND.L	#$FFFF,D1
	BEQ.S	p6zero2
	DIVU	#10,D1
	SWAP	D1
	ADD.B	#'0',D1
	MOVE.B	D1,3(A0)
	CLR.W	D1
	SWAP	D1
	TST.W	D1
	BEQ.S	p6zero3
	DIVU	#10,D1
	SWAP	D1
	ADD.B	#'0',D1
	MOVE.B	D1,2(A0)
	CLR.W	D1
	SWAP	D1
	TST.W	D1
	BEQ.S	p6zero4
	DIVU	#10,D1
	SWAP	D1
	ADD.B	#'0',D1
	MOVE.B	D1,1(A0)
	CLR.W	D1
	SWAP	D1
	TST.W	D1
	BEQ.S	p6zero5
	DIVU	#10,D1
	SWAP	D1
	ADD.B	#'0',D1
	MOVE.B	D1,(A0)
	BRA.S	p6end

p6zero1	MOVE.B	#' ',4(A0)
p6zero2	MOVE.B	#' ',3(A0)
p6zero3	MOVE.B	#' ',2(A0)
p6zero4	MOVE.B	#' ',1(A0)
p6zero5	MOVE.B	#' ',0(A0)
p6end	MOVE.W	#6,TextLength
	BRA	ShowText2

toobig	MOVE.B	#' ',D0
	BSR.S	toobig2
	DIVU	#1000,D0
	MOVE.W	D0,WordNumber
	BSR	Print4DecDigits
	MOVE.B	#'K',D0
toobig2	LEA	NumberText(PC),A0
	MOVE.B	D0,(A0)
	MOVE.W	#1,TextLength
	BRA	ShowText2

NumberText	dcb.b	6,0

;---- Print Hex Digits ----

PrintHiInstrNum
	MOVEQ	#0,D0
	TST.W	CurrentPlayNote
	BEQ.S	phin2
	MOVE.W	InsNum(PC),D0
	LSR.W	#4,D0
	BNE.S	PrintHexDigit
phin2	TST.B	BlankZeroFlag
	BEQ.S	PrintHexDigit
	LEA	BlankInsText(PC),A0
	BRA.S	phd2
PrintHexDigit
	AND.L	#$0F,D0
	ADD.L	D0,D0
	LEA	FastHexTable+1(PC),A0
	ADD.L	D0,A0
phd2	MOVE.W	#1,TextLength
	BRA.S	ShowText2

BlankInsText	dc.b " ",0

PrintHexWord
	LEA	WordNumber(PC),A0
	MOVEQ	#0,D0
	MOVE.B	(A0),D0
	LSL.L	#1,D0
	LEA	FastHexTable(PC),A0
	ADD.L	D0,A0
	MOVE.W	#2,TextLength
	BSR.S	ShowText2
PrintHexByte
	LEA	WordNumber(PC),A0
	MOVEQ	#0,D0
	MOVE.B	1(A0),D0
	LSL.L	#1,D0
	MOVE.L	#FastHexTable,A0
	ADD.L	D0,A0
	MOVE.W	#2,TextLength
	BSR.S	ShowText2
	CLR.W	WordNumber
	RTS	

;---- Text Output Routines ----

ShowText3
	MOVE.W	D0,TextLength
	MOVE.W	D1,TextOffset
ShowText2
	MOVE.L	A0,ShowTextPtr
ShowText
	MOVEM.L	A2-A4,-(SP)
	LEA	TextTable(PC),A3
	LEA	FontData,A4
	MOVE.W	TextLength(PC),D0
	MOVE.L	TextBplPtr(PC),A1
	MOVE.W	TextOffset(PC),D1
	LEA	(A1,D1.W),A1
	ADD.W	D0,TextOffset
	MOVE.L	ShowTextPtr(PC),A0
	BRA.S	dstlope
dstloop	MOVEQ	#0,D1
	MOVE.B	(A0)+,D1
	BNE.S	dstskip
	MOVE.B	ShowZeroFlag(PC),D1
dstskip	MOVE.B	(A3,D1.W),D1
	LSL.W	#3,D1
	MOVE.L	A4,A2
	ADD.L	D1,A2
	MOVE.B	(A2)+,(A1)+
	MOVE.B	(A2)+,40-1(A1)
	MOVE.B	(A2)+,80-1(A1)
	MOVE.B	(A2)+,120-1(A1)
	MOVE.B	(A2),160-1(A1)
dstlope	DBRA	D0,dstloop
	MOVEM.L	(SP)+,A2-A4
	RTS	

SpaceShowText
	MOVE.W	ShowZeroFlag(PC),-(SP)
	MOVE.B	#' ',ShowZeroFlag
	BSR.S	ShowText
	MOVE.W	(SP)+,ShowZeroFlag
	RTS	
 
ShowZeroFlag	dc.b	95,0

TextTable
	dc.b 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ; 0
	dc.b 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.b 00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15 ; 32
	dc.b 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
	dc.b 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47 ; 64
	dc.b 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63
	dc.b 69,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47 ; 96
	dc.b 48,49,50,51,52,53,54,55,56,57,58,65,66,67,68,63
	dc.b 69,70,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ; 128
	dc.b 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.b 0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ; 160
	dc.b 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.b 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ; 192
	dc.b 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.b 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ; 224
	dc.b 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0


;---- Set Sprite Position ----

SetSpritePos
	ADD.W	#$002C,D1
	ADD.W	D1,D2
	ROL.W	#7,D2
	ASL.W	#1,D2
	BCC.S	sppskip
	OR.W	#2,D2
sppskip	ROL.W	#7,D1
	ASL.W	#1,D1
	BCC.S	sppskip2
	OR.W	#4,D2
sppskip2
	ADD.W	#$007D,D0
	ASR.W	#1,D0
	BCC.S	sppskip3
	OR.W	#1,D2
sppskip3
	OR.W	D0,D1
	MOVE.W	D1,(A0)
	MOVE.W	D2,2(A0)
	RTS	
 

;---- DoLoad / DoSave Data ----

DoLoadData
	TST.L	DiskDataLength
	BEQ.S	dlsend
	TST.L	DiskDataPtr
	BEQ.S	dlsend
	JSR	SetDiskPtrCol
	LEA	LoadingText(PC),A0
	BSR	ShowStatusText
	MOVE.L	DOSbase(PC),A6
	MOVE.L	FileNamePtr(PC),D1
	MOVE.L	#1005,D2
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BEQ.S	CantOpenFile
	MOVE.L	FileHandle(PC),D1
	MOVE.L	DiskDataPtr(PC),D2
	MOVE.L	DiskDataLength(PC),D3
	JSR	LVORead(A6)
	MOVE.L	FileHandle(PC),D1
	JSR	LVOClose(A6)
dlsend	CLR.L	FileHandle
	JSR	SetNormalPtrCol
 	MOVEQ	#-1,D0
	RTS

CantOpenFile
	LEA	CantOpenFileText(PC),A0
caopfil	BSR	ShowStatusText
	JSR	SetErrorPtrCol
 	MOVEQ	#0,D0
	RTS

CantSaveFile
	LEA	CantSaveFileText(PC),A0
	BRA.S	caopfil
CantExamFile
	LEA	CantExamFileText(PC),A0
	BRA.S	caopfil
CantFindFile
	LEA	CantFindFileText(PC),A0
	BRA.S	caopfil
FileIsEmpty
	LEA	FileIsEmptyText(PC),A0
	BRA.S	caopfil

CantOpenFileText	dc.b "can't open file !",0
CantSaveFileText	dc.b "can't save file !",0
CantExamFileText	dc.b "examine error !",0
CantFindFileText	dc.b "can't find file !",0
FileIsEmptyText		dc.b "file is empty !",0

DoSaveData
	JSR	SetDiskPtrCol
	LEA	SavingText(PC),A0
	BSR	ShowStatusText
	MOVE.L	DOSbase(PC),A6
	MOVE.L	FileNamePtr(PC),D1
	MOVE.L	#1006,D2
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BEQ	CantOpenFile
	MOVE.L	FileHandle(PC),D1
	MOVE.L	DiskDataPtr(PC),D2
	MOVE.L	DiskDataLength(PC),D3
	JSR	LVOWrite(A6)
	MOVE.L	FileHandle(PC),D1
	JSR	LVOClose(A6)
	CLR.L	FileHandle
	JMP	SetNormalPtrCol

;---- PLST ----

PLST	CLR.B	RawKeyCode
	CMP.W	#6,CurrScreen
	BEQ	ExitPLST
	CMP.W	#1,CurrScreen
	BNE	Return2
	JSR	WaitForButtonUp
	MOVE.W	#6,CurrScreen
	JSR	ClearRightArea
	JSR	ClearAnalyzerColors
	BSR.S	DrawPLSTScreen
	BEQ	ExitPLST
	BSR	RedrawPLST
PLST_rts
	RTS

DrawPLSTScreen
	LEA	PLSTData,A0
	MOVE.L	#PLSTSize,D0
	BSR	Decompact
	BEQ	Return2
	LEA	TopMenusPos,A0
	MOVEQ	#98,D0
dplstloop1
	MOVEQ	#24,D1
dplstloop2
	MOVE.B	2574(A1),10240(A0)
	MOVE.B	(A1)+,(A0)+
	DBRA	D1,dplstloop2
	LEA	15(A0),A0
	ADDQ.L	#1,A1
	DBRA	D0,dplstloop1
	BSR	FreeDecompMem
	MOVEQ	#-1,D0
	RTS

ShowDiskNames
	MOVE.W	#616,TextOffset
	MOVE.W	#5,TextLength
	MOVE.L	#STText1,A0
	BSR	ShowText2
	ADDQ.W	#1,TextOffset
	MOVE.L	#STText2,A0
	BSR	ShowText2
	ADDQ.W	#1,TextOffset
	MOVE.L	#STText3,A0
	BRA	ShowText2

PLSTCheckNum
	TST.L	PLSTmem
	BEQ	NoPLST
	TST.L	MaxPLSTOffset
	BEQ	NoPLST
	MOVE.L	PLSTmem(PC),A0
	MOVE.W	PresetTotal(PC),D7
	SUBQ.W	#1,D7
	TST.B	STText1Number
	BNE	plstsskip
	TST.B	STText2Number
	BNE	plstsskip
	TST.B	STText3Number
	BEQ	PLSTMarkAll
plstsskip
	MOVE.L	STText1Number-1(PC),A3
	MOVE.L	STText2Number-1(PC),A4
	MOVE.L	STText3Number-1(PC),A5
	MOVEQ	#0,D6
PLSTmarkloop
	MOVE.W	#'st',(A0)	; Set lowercase 'st'
	MOVE.B	3(A0),D0
	CMP.B	#'a',D0
	BLO.S	pmlskp1
	SUB.B	#32,D0
pmlskp1	MOVE.B	D0,3(A0)
	MOVE.B	4(A0),D0
	CMP.B	#'a',D0
	BLO.S	pmlskp2
	SUB.B	#32,D0
pmlskp2	MOVE.B	D0,4(A0)
	MOVE.L	2(A0),D0	; Get number ('-01:' etc)
PLSTchk1
	CMP.L	A3,D0
	BNE	PLSTchk2
	MOVE.W	#'ST',(A0)
	ADDQ.W	#1,D6
	BRA	PLSTmarknext

PLSTchk2
	CMP.L	A4,D0
	BNE	PLSTchk3
	MOVE.W	#'ST',(A0)
	ADDQ.W	#1,D6
	BRA	PLSTmarknext

PLSTchk3
	CMP.L	A5,D0
	BNE	PLSTmarknext
	MOVE.W	#'ST',(A0)
	ADDQ.W	#1,D6
PLSTmarknext
	ADD.L	#30,A0
	DBRA	D7,PLSTmarkloop
	MOVE.W	D6,PresetMarkTotal
	CLR.W	PLSTpos
	CLR.L	PLSTOffset
	RTS

PLSTMarkAll
	MOVE.W	#'ST',(A0)	; Set uppercase 'ST'
	LEA	30(A0),A0
	DBRA	D7,PLSTMarkAll
	MOVE.W	PresetTotal(PC),PresetMarkTotal
	CLR.W	PLSTpos
	CLR.L	PLSTOffset
	RTS

NoPLST	CLR.W	PresetMarkTotal
	CLR.W	PLSTpos
	CLR.L	PLSTOffset
	RTS

RedrawPLST
	MOVE.W	PresetMarkTotal(PC),WordNumber
	MOVE.W	#189,TextOffset
	BSR	Print4DecDigits
	BSR	ShowDiskNames
	TST.L	PLSTmem
	BEQ	PLST_rts
	TST.L	MaxPLSTOffset
	BEQ	PLST_rts

	MOVE.L	PLSTOffset(PC),D6
	MOVE.L	PLSTmem(PC),A6
	MOVE.W	#976,A5 ; TextOffset
	LEA	PLSTOffset(PC),A4
	MOVEQ	#11,D7	; Number of lines to print
	TST.L	D6
	BMI	EndOfPLST
dtplstloop
	CMP.W	#'ST',(A6,D6.L)	; Check for 'ST'
	BNE	PLSTNext
	MOVE.L	D6,(A4)+
	MOVE.W	A5,TextOffset
	MOVE.W	#19,TextLength
	LEA	(A6,D6.L),A3
	ADDQ.L	#3,A3
	MOVE.L	A3,ShowTextPtr
	MOVEM.L	D0-D7/A0-A6,-(SP)
	BSR	SpaceShowText
	MOVEM.L	(SP)+,D0-D7/A0-A6
	MOVE.W	22(A6,D6.L),WordNumber
	MOVE.W	WordNumber,D0
	LSL.W	#1,D0
	MOVE.W	D0,WordNumber
	MOVEM.L	D0-D7/A0-A6,-(SP)
	BSR	PrintHexWord
	MOVEM.L	(SP)+,D0-D7/A0-A6
	ADD.L	#30,D6
	CMP.L	MaxPLSTOffset(PC),D6
	BHI	EndOfPLST
	ADD.W	#240,A5	; Next Screen position
	DBRA	D7,dtplstloop
	RTS

PLSTNext
	ADD.L	#30,D6
	CMP.L	MaxPLSTOffset(PC),D6
	BHI	EndOfPLST
	BRA	dtplstloop

EndOfPLST
	MOVE.L	#$FFFFFFFF,(A4)+
	MOVE.W	A5,TextOffset
	MOVE.W	#23,TextLength
	MOVE.L	#EmptyLineText,ShowTextPtr
	MOVEM.L	D0-D7/A0-A6,-(SP)
	BSR	SpaceShowText
	MOVEM.L	(SP)+,D0-D7/A0-A6
	ADD.W	#240,A5
	DBRA	D7,EndOfPLST
	RTS

TypeInDisk1
	LEA	STText1Number(PC),A6
	MOVE.W	#156,LineCurX
	BRA	DoTypeInDisk

TypeInDisk2
	LEA	STText2Number(PC),A6
	MOVE.W	#204,LineCurX
	BRA	DoTypeInDisk

TypeInDisk3
	LEA	STText3Number(PC),A6
	MOVE.W	#252,LineCurX
DoTypeInDisk
	CLR.B	(A6)
	CLR.B	1(A6)
	JSR	StorePtrCol
	JSR	SetWaitPtrCol
	BSR	ShowDiskNames
	MOVE.W	#20,LineCurY
	BSR	UpdateLineCurPos
	BSR	GetHexKey
	TST.B	RawKeyCode
	BNE	ClearDiskNum
	BTST	#2,$DFF016
	BEQ	ClearDiskNum
	ADD.W	D1,D1
	LEA	FastHexTable+1(PC),A0
	MOVE.B	(A0,D1.W),(A6)
	ADDQ.W	#8,LineCurX
	BSR	ShowDiskNames
	BSR	UpdateLineCurPos
	BSR	GetHexKey
	TST.B	RawKeyCode
	BNE	ClearDiskNum
	BTST	#2,$DFF016
	BEQ	ClearDiskNum
	ADD.W	D1,D1
	LEA	FastHexTable+1(PC),A0
	MOVE.B	(A0,D1.W),1(A6)
	JSR	RestorePtrCol
	BSR	PLSTCheckNum
	BSR	RedrawPLST
	CLR.W	LineCurX
	MOVE.W	#270,LineCurY
	BRA	UpdateLineCurPos

ClearAllDisks
	BSR	DoClearDisks
	BSR	PLSTCheckNum
	BRA	RedrawPLST

DoClearDisks
	MOVEQ	#0,D0
	MOVE.B	D0,STText1Number
	MOVE.B	D0,STText1Number+1
	MOVE.B	D0,STText2Number
	MOVE.B	D0,STText2Number+1
	MOVE.B	D0,STText3Number
	MOVE.B	D0,STText3Number+1
	RTS

MountList
	JSR	StorePtrCol
	JSR	SetDiskPtrCol
	BSR	DoClearDisks
	MOVE.W	#1,MountFlag
	LEA	df0text(PC),A4
	BSR	DoMountList
	LEA	STText1(PC),A0
	BSR	CheckMountName
	LEA	df1text(PC),A4
	BSR	DoMountList
	LEA	STText2(PC),A0
	BSR	CheckMountName
	LEA	df2text(PC),A4
	BSR	DoMountList
	LEA	STText3(PC),A0
	BSR	CheckMountName
	CLR.W	MountFlag
	JSR	RestorePtrCol
	BRA	cdisknum2

DoMountList	
	CLR.L	FIB_Filename
	CLR.L	FIB_Filename+4
	CLR.L	FIB_Filename+8
	MOVE.L	DOSBase(PC),A6
	MOVE.L	A4,D1
	MOVEQ	#-2,D2
	JSR	LVOLock(A6)
	MOVE.L	D0,FileLock
	BEQ	MountError
	MOVE.L	FileLock(PC),D1
	MOVE.L	#FileInfoBlock,D2
	JSR	LVOExamine(A6)
	TST.L	D0
	BEQ	MountError
	MOVE.L	FileLock(PC),D1
	JSR	LVOUnLock(A6)
MountError
	MOVEQ	#-1,D0
	RTS

CheckMountName
	MOVE.W	FIB_Filename,D0
	BEQ	MountError
	MOVE.W	#'ST',D1	; Check for ST
	AND.W	#$1F1F,D0
	AND.W	#$1F1F,D1
	CMP.W	D0,D1
	BNE	ClearDiskNum
	MOVE.B	FIB_Filename+3,D0
	LSL.W	#8,D0
	MOVE.B	FIB_Filename+4,D0
	CMP.W	#'00',D0
	BEQ	ClearDiskNum
	MOVE.B	D0,4(A0)	; Put disk number into ST-xx
	LSR.W	#8,D0
	MOVE.B	D0,3(A0)
	MOVEQ	#0,D0
	RTS

ClearDiskNum
	CLR.B	RawKeyCode
	JSR	RestorePtrCol
	CLR.B	(A6)
	CLR.B	1(A6)
	CLR.W	LineCurX
	MOVE.W	#270,LineCurY
	BSR	UpdateLineCurPos
cdisknum2
	BSR	PLSTCheckNum
	BRA	RedrawPLST

df0text	dc.b 'DF0:',0,0
df1text	dc.b 'DF1:',0,0
df2text	dc.b 'DF2:',0,0

CheckPLSTGadgs
	MOVEM.W	MouseX2(PC),D0/D1
	CMP.W	#120,D0
	BLO	cplstend
	CMP.W	#23,D1
	BHS	PLSTPressed
	CMP.W	#12,D1
	BHS	PLSTLine2
PLSTLine1
	CMP.W	#268,D0
	BHS	ExitPLST
	CMP.W	#168,D0
	BHS	cplstend
	CMP.W	#120,D0
	BHS	ClearAllDisks
	RTS

PLSTLine2
	CMP.W	#268,D0
	BHS	MountList
	CMP.W	#218,D0
	BHS	TypeInDisk3
	CMP.W	#168,D0
	BHS	TypeInDisk2
	BRA	TypeInDisk1
cplstend
	RTS

ExitPLST
	JSR	WaitForButtonUp
	CLR.B	RawKeyCode
	JSR	ClearRightArea
	JMP	DisplayMainScreen

PLSTOneUp
	TST.W	PresetMarkTotal
	BEQ	Return2
	BTST	#6,$BFE001
	BEQ	PLST_rts
	MOVE.W	PLSTpos,D0
	MOVE.W	D0,D2
	SUBQ.W	#1,D0
	TST.W	ShiftKeyStatus
	BNE.S	pouski2
	BTST	#2,$DFF016
	BNE	pouskip
pouski2	SUB.W	#9,D0
pouskip	TST.W	D0
	BPL	DoPLSTUpDown
	CLR.W	D0
	BRA	DoPLSTUpDown

PLSTOneDown
	TST.W	PresetMarkTotal
	BEQ	Return2
	BTST	#6,$BFE001
	BEQ	PLST_rts
	MOVE.W	PLSTpos(PC),D0
	MOVE.W	D0,D2
	ADDQ.W	#1,D0
	TST.W	ShiftKeyStatus
	BNE.S	podski2
	BTST	#2,$DFF016
	BNE	podskip
podski2	ADD.W	#9,D0
podskip	MOVE.W	PresetMarkTotal(PC),D1
	SUB.W	#12,D1
	CMP.W	D0,D1
	BHS	DoPLSTUpDown
	MOVE.W	D1,D0
DoPLSTUpDown
	BSR	UpdateOffset
	BRA	RedrawPLST

UpdateOffset
	MOVE.W	PLSTpos(PC),D1
	MOVE.W	D0,PLSTpos
	CMP.W	D0,D1
	BEQ	Return2
	TST.W	D0
	BEQ	ZeroPLSTOffset
	SUBQ.W	#1,D0
	MOVE.L	PLSTmem(PC),A0
	MOVEQ	#0,D6
cpoloop	CMP.W	#'ST',(A0,D6.L)	; Check for 'ST'
	BEQ	NextPLSTOffset
	ADD.L	#30,D6
	BRA	cpoloop
NextPLSTOffset
	ADD.L	#30,D6
	DBRA	D0,cpoloop
	MOVE.L	D6,PLSTOffset
	RTS

ZeroPLSTOffset
	CLR.L	PLSTOffset
	RTS

PLSTPressed
	CMP.W	#24,D1
	BLO	Return2
	CMP.W	#95,D1
	BHI	Return2
	SUB.W	#24,D1
	AND.L	#$0000FFFF,D1
	DIVU	#6,D1
	LSL.W	#2,D1
	LEA	PLSTOffset(PC),A0
	MOVE.L	(A0,D1.W),D1
	BMI	Return2
	DIVU	#30,D1
	ADDQ.W	#1,D1
	MOVE.W	D1,CurrentPreset
	JSR	WaitForButtonUp
	BRA	UsePreset

;---- Preset Editor/PED ----

PresetEditor
	JSR	WaitForButtonUp
	MOVE.W	#4,CurrScreen
	ST 	DisableAnalyzer
	ST	NoSampleInfo
	BSR	SwapPresEdScreen
	BEQ	pedexi2
PED_Refresh
	CLR.W	PED_Action
	MOVE.L	TextBplPtr(PC),A0
	MOVE.W	#1219,D0
pedloop	CLR.L	(A0)+
	DBRA	D0,pedloop
	JSR	SetNormalPtrCol
	JSR	ClearAnalyzerColors
	BSR	ShowPEDnumbers
	BRA	ShowPresetNames

SwapPresEdScreen
	MOVE.L	DecompMemPtr,D0
	BEQ	speds2
	MOVE.L	D0,A1
	BSR	speds3
	BRA	FreeDecompMem
speds2	LEA	PresetEdData,A0
	MOVE.L	#PresetEdSize,D0
	BSR	Decompact
	BEQ	Return2
speds3	LEA	BitplaneData,A0
	MOVEQ	#1,D2
spesloop1
	MOVE.W	#1219,D0
spesloop2
	MOVE.L	(A0),D1
	MOVE.L	(A1),(A0)+
	MOVE.L	D1,(A1)+
	DBRA	D0,spesloop2
	LEA	$14F0(A0),A0
	DBRA	D2,spesloop1
	MOVEQ	#-1,D0
	RTS

CheckPresEdGadgs
	MOVEM.W	MouseX2(PC),D0/D1
	CMP.W	#45,D1
	BHS	CheckPEDnames
	CLR.W	PED_Action
	JSR	SetNormalPtrCol
	CMP.W	#307,D0
	BHS	PED_GotoPLST
PED_Menu1
	CMP.W	#101,D0
	BHS	PED_Menu2
	CMP.W	#34,D1
	BHS	PED_DeleteDisk
	CMP.W	#23,D1
	BHS	PED_Delete
	CMP.W	#12,D1
	BHS	PED_Insert
	BRA	PED_AddPathGadg

PED_Menu2	
	CMP.W	#209,D0
	BHS	PED_Menu3
	CMP.W	#34,D1
	BHS	PED_ClearPLST
 	CMP.W	#23,D1
	BHS	Return2
 	CMP.W	#12,D1
	BHS	PED_Disk
	BRA	PED_EnterPath

PED_Menu3	
	CMP.W	#34,D1
	BHS	PED_Print
	CMP.W	#23,D1
	BHS	WritePLST
	CMP.W	#12,D1
	BHS.S	xLoadPLST
	BRA	PED_EnterPath

xLoadPLST
	BSR	LoadPLST
	MOVE.W	#1,PEDPos
	BRA	PED_Refresh

PED_GotoPLST
	BSR	PED_Exit
	BRA	PLST

PED_Exit
	JSR	WaitForButtonUp
	BSR	PLSTCheckNum
	BSR	SwapPresEdScreen
	MOVE.L	TextBplPtr(PC),A0
	MOVE.W	#1219,D0
pedeloop	CLR.L	(A0)+
	DBRA	D0,pedeloop
	CLR.B	RawKeyCode
pedexi2	JSR	ClearAnalyzerColors
	JMP	DisplayMainAll

PED_AddPathGadg
	TST.L	PLSTmem
	BNE	pedawok
	BSR	AllocPLST
	TST.L	PLSTmem
	BEQ	PLSTMemErr
pedawok	MOVE.L	DOSbase(PC),A6
	MOVE.L	#PEdDefaultPath,D1
	MOVEQ	#-2,D2
	JSR	LVOLock(A6)
	MOVE.L	D0,FileLock
	BEQ	UnlockReadPath
	JSR	SetDiskPtrCol
	LEA	AddingPathText(PC),A0
	BSR	ShowStatusText
	MOVE.L	DOSbase(PC),A6
	MOVE.L	FileLock(PC),D1
	MOVE.L	#FileInfoBlock,D2
	JSR	LVOExamine(A6)
	TST.L	D0
	BPL	UnlockReadPath
	TST.L	FIB_EntryType
	BPL	CheckPathDirName
	BSR	AddPreset
	BRA	IsPLSTfull

CheckPathDirName
	CMP.B	#'-',FIB_FileName+2
	BNE	IsPLSTfull
	MOVE.B	FIB_FileName+3,SndDiskNum0
	MOVE.B	FIB_FileName+4,SndDiskNum1
	BSR	ShowPEDnumbers
IsPLSTfull
	MOVE.W	PresetTotal(PC),D0
	CMP.W	MaxPLSTEntries,D0
	BLO	ReadPathNext
	BSR	PLSTisFull
	BRA	ReadPathEnd

ReadPathNext
	MOVE.L	DOSbase,A6
	MOVE.L	FileLock(PC),D1
	MOVE.L	#FileInfoBlock,D2
	JSR	LVOExNext(A6)
	TST.L	D0
	BPL	ReadPathEnd
	BTST	#2,$DFF016
	BEQ	ReadPathEnd
	TST.L	FIB_EntryType
	BPL	IsPLSTfull
	CMP.L	#'.inf',FIB_FileName
	BEQ	IsPLSTfull
	CMP.L	#'.inf',FIB_FileName+4
	BEQ	IsPLSTfull
;	LEA	FIB_FileName,A0
;	MOVEQ	#28,D0
;repalop	CMP.B	#'.',(A0)+
;	BEQ	CouldBeInfo
;	DBRA	D0,repalop
rpnskip	CLR.W	PresetRepeat
	MOVE.W	#1,PresetReplen
;	TST.B	IFFLoopFlag	; name is ok, test for IFF
	BRA	rpnskp2
;	LEA	PEDDefaultPath,A0
	JSR	CopyPath
	LEA	FIB_FileName,A0
rpncpfn	MOVE.B	(A0)+,(A1)+
	BNE.S	rpncpfn
	MOVE.L	#FileName,D1
	MOVE.L	#1005,D2
	MOVE.L	DOSbase(PC),A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BEQ.S	rpnskp2
	MOVE.L	D0,D1
	LEA	chkiffbuffer(PC),A2
	CLR.L	(A2)
	MOVE.L	A2,D2
	MOVEQ	#12,D3
	JSR	LVORead(A6)
	CMP.L	#"FORM",(A2)
	BNE.S	rpnclse
	CMP.L	#"8SVX",8(A2)
	BNE.S	rpnclse
rpnvhdr	MOVE.L	FileHandle(PC),D1
	MOVE.L	A2,D2
	MOVEQ	#4,D3
	JSR	LVORead(A6)
	TST.L	D0
	BEQ.S	rpnclse
	CMP.L	#"VHDR",(A2)
	BNE.S	rpnvhdr
	MOVE.L	FileHandle(PC),D1
	MOVE.L	A2,D2
	MOVEQ	#12,D3
	JSR	LVORead(A6)
	MOVE.L	8(A2),D0
	BEQ.S	rpnclse
	LSR.W	#1,D0
	MOVE.W	D0,PresetReplen
	MOVE.L	4(A2),D0
	LSR.W	#1,D0
	MOVE.W	D0,PresetRepeat
rpnclse	MOVE.L	FileHandle(PC),D1
	JSR	LVOClose(A6)
rpnskp2	BSR	AddPreset
	BSR	ShowPEDnumbers
	BRA	IsPLSTfull

chkiffbuffer
	dc.l	0,0,0

CouldBeInfo
	CMP.B	#'i',(A0)+
	BNE	rpnskip
	CMP.B	#'n',(A0)+
	BNE	rpnskip
	CMP.B	#'f',(A0)+
	BNE	rpnskip
	CMP.B	#'o',(A0)+
	BNE	rpnskip
	CMP.B	#0,(A0)+
	BNE	rpnskip
	BRA	IsPLSTFull

ReadPathEnd
	MOVE.L	DOSbase(PC),A6
	MOVE.L	FileLock(PC),D1
	BEQ	rpeskip
	JSR	LVOUnLock(A6)
rpeskip	JSR	SetNormalPtrCol
	BSR	ShowPresetNames
	BRA	ShowAllRight

UnlockReadPath
	MOVE.L	DOSbase(PC),A6
	MOVE.L	FileLock(PC),D1
	BEQ	urpend
	JSR	LVOUnLock(A6)
urpend	JMP	SetErrorPtrCol

AddPreset
	LEA	PEdDefaultVol(PC),A0
	LEA	PresetName(PC),A1
	MOVEQ	#21,D0
.loop	CLR.B	(A1)+
	DBRA	D0,.loop
	LEA	PresetName(PC),A1
	MOVEQ	#5,D0	; Disk ST-XX:
aploop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,aploop

	LEA	FIB_FileName,A0
	MOVEQ	#14,D0	; Name 16 letters.
aploop2	MOVE.B	(A0)+,D1
	BEQ	.skip
	MOVE.B	D1,(A1)+
	DBRA	D0,aploop2
.skip	CLR.B	(A1)

	MOVE.L	FIB_FileSize,D0
	CMP.L	#$FFFE,D0
	BLS.S	apskip2
	MOVE.W	#$FFFE,D0
apskip2	LSR.W	#1,D0
	MOVE.W	D0,PresetLength
	CLR.W	PresetFineTune
	LEA	PresetName(PC),A6
	BSR	PED_CheckAdd
	ADDQ.W	#1,PresetTotal
	ADD.L	#30,MaxPLSTOffset
	RTS

PED_Insert
	TST.L	PLSTmem
	BNE	pediwok
	BSR	AllocPLST
	TST.L	PLSTmem
	BEQ	PLSTMemErr
pediwok	MOVE.W	PresetTotal(PC),D0
	CMP.W	MaxPLSTEntries2,D0
	BHS	PLSTisFull
	LEA	InsertPsetText(PC),A0
	LEA	PresetName(PC),A1
	MOVEQ	#39,D0
pediloop
	MOVE.B	(A0)+,(A1)+
	DBRA	D0,pediloop
PossibleEdit
	JSR	StorePtrCol
	JSR	SetWaitPtrCol
	MOVE.L	TextBplPtr(PC),A0
	LEA	2320(A0),A0
	MOVEQ	#59,D0
pediloop2
	CLR.L	(A0)+
	DBRA	D0,pediloop2
	LEA	EnterDataText(PC),A0
	BSR	ShowStatusText
	MOVE.W	#63,LineCurY
	LEA	PresetName(PC),A6
	MOVEQ	#3,D7
	MOVE.B	#4,EnterTextFlag
ShowPsetText
	LEA	PresetName+3(PC),A0
	MOVEQ	#37,D0
	MOVE.W	#2321,D1
	BSR	ShowText3
	MOVE.W	D7,D0
	SUBQ.W	#3,D0
	ASL.W	#3,D0
	ADD.W	#12,D0
	MOVE.W	D0,LineCurX
	BSR	UpdateLineCurPos
pediwaitkey
	JSR	DoKeyBuffer
	MOVE.B	RawKeyCode(PC),D0
	BEQ	pediwaitkey
	CLR.B	RawKeyCode
	BTST	#7,D0
	BNE	pediwaitkey
	AND.W	#$FF,D0
	CMP.B	#69,D0
	BEQ	PED_ESCkey
	CMP.B	#65,D0
	BEQ	PED_BkspaceKey
	CMP.B	#68,D0
	BEQ	PED_ReturnKey
	CMP.B	#79,D0
	BEQ	PED_LeftArrowKey
	CMP.B	#78,D0
	BEQ	PED_RightArrowKey
	CMP.B	#64,D0
	BHI	pediwaitkey
	CMP.W	#40,D7
	BEQ	pediwaitkey
	CMP.W	#22,D7
	BEQ	pediwaitkey
	LEA	UnshiftedKeymap(PC),A0
	TST.W	ShiftKeyStatus
	BEQ	ShiftKeySkip
	LEA	ShiftedKeymap(PC),A0
ShiftKeySkip
	MOVE.B	0(A0,D0.W),D1
	BEQ	pediwaitkey

	CMP.W	#6,D7
	BLO.S	hexchk2
	CMP.W	#25,D7
	BLO	PED_PrintChar
hexchk2	CMP.B	#'0',D1
	BLO	pediwaitkey
	CMP.B	#'f',D1
	BHI	pediwaitkey
	CMP.B	#'9',D1
	BLS	PED_PrintChar
	CMP.B	#'a',D1
	BHS	PED_PrintChar
	BRA	pediwaitkey

PED_PrintChar
	MOVE.B	D1,(A6,D7.W)
	BRA	PED_RightArrowKey

PED_ReturnKey
	CMP.B	#' ',PsetNameText
	BEQ	PED_ESCkey
	LEA	PsetVolText(PC),A0
	LEA	fitutexttab+32(PC),A1
	MOVE.B	(A0)+,D1
	LSL.W	#8,D1
	MOVE.B	(A0),D1
	MOVEQ	#15,D0
vofloop	CMP.W	-(A1),D1
	BEQ.S	vofound
	DBRA	D0,vofloop
	MOVEQ	#0,D0
vofound	MOVE.W	D0,PresetFineTune
	LEA	PsetLenText(PC),A0
	BSR	HexToInteger
	LSR.W	#1,D0
	MOVE.W	D0,PresetLength
	LEA	PsetRepeatText(PC),A0
	BSR	HexToInteger
	LSR.W	#1,D0
	MOVE.W	D0,PresetRepeat
	LEA	PsetReplenText(PC),A0
	BSR	HexToInteger
	LSR.W	#1,D0
	MOVE.W	D0,PresetReplen
	BSR	PED_CheckAdd
	ADDQ.W	#1,PresetTotal
PED_ESCkey
	CLR.W	LineCurX
	MOVE.W	#270,LineCurY
	BSR	UpdateLineCurPos
	BSR	ShowPresetNames
	BSR	ShowPEDnumbers
	BSR	ShowAllRight
	CLR.B	EnterTextFlag
	JMP	RestorePtrCol

PED_BkspaceKey
	CMP.W	#23,D7
	BHS	pedbsend
	CMP.W	#6,D7
	BLS	pedbsend
	SUBQ.W	#1,D7
	MOVE.B	#' ',(A6,D7.W)
pedbsend
	BRA	ShowPsetText

PED_LeftArrowKey
	SUBQ.W	#1,D7
	CMP.W	#2,D7
	BLS	pedlakskip2
	CMP.W	#5,D7
	BEQ	pedlakskip
	CMP.W	#22,D7
	BEQ	pedlakskip
	CMP.W	#25,D7
	BEQ	pedlakskip
	CMP.W	#30,D7
	BEQ	pedlakskip
	CMP.W	#35,D7
	BEQ	pedlakskip
	BRA	ShowPsetText

pedlakskip
	SUBQ.W	#1,D7
	BRA	ShowPsetText

pedlakskip2
	MOVEQ	#3,D7
	BRA	ShowPsetText

PED_RightArrowKey
	ADDQ.W	#1,D7
	CMP.W	#5,D7
	BEQ	pedrakskip
	CMP.W	#22,D7
	BEQ	pedrakskip
	CMP.W	#25,D7
	BEQ	pedrakskip
	CMP.W	#30,D7
	BEQ	pedrakskip
	CMP.W	#35,D7
	BEQ	pedrakskip
	CMP.W	#40,D7
	BHS	pedrakskip2
	BRA	ShowPsetText

pedrakskip
	ADDQ.W	#1,D7
	BRA	ShowPsetText

pedrakskip2
	MOVEQ	#39,D7
	BRA	ShowPsetText

PED_CheckAdd
	MOVEQ	#23,D0
pedcaloop
	SUBQ.W	#1,D0
	CMP.B	#' ',0(A6,D0.W)
	BEQ	pedcaloop
	CLR.B	1(A6,D0.W)
	MOVE.L	PLSTmem(PC),A5
	LEA	30(A5),A5
pedccnextloop
	MOVEQ	#6,D0
	TST.B	6(A5)
	BEQ	PED_AddPreset
PED_ConvertLoop
	MOVE.B	0(A5,D0.W),D2
	BNE	PED_ConvertCase
	TST.B	0(A6,D0.W)
	BNE	PED_ConvertCase
	CLR.B	6(A5)
	SUBQ.W	#1,PresetTotal
	BRA	PED_AddPreset

PED_ConvertCase
	CMP.B	#'A',D2
	BLO	pedccskip
	CMP.B	#'Z',D2
	BHI	pedccskip
	ADD.B	#32,D2
pedccskip
	MOVE.B	0(A6,D0.W),D1
	CMP.B	#' ',D1
	BEQ	pedccskip3
	CMP.B	#'A',D1
	BLO	pedccskip2
	CMP.B	#'Z',D1
	BHI	pedccskip2
	ADD.B	#32,D1
pedccskip2
	CMP.B	D2,D1
	BEQ	pedccnext
	BHI	pedccskip3
	BRA	PED_AddPreset

pedccnext
	ADDQ.W	#1,D0
	CMP.W	#22,D0
	BNE	PED_ConvertLoop
pedccskip3
	LEA	30(A5),A5
	BRA	pedccnextloop

PED_AddPreset
	TST.B	6(A5)
	BEQ	pedapskip
	MOVE.L	PLSTmem(PC),A1
	MOVE.W	PresetTotal(PC),D0
	CMP.W	#1,D0
	BEQ	pedapskip
	MULU	#30,D0
	ADD.L	D0,A1
	LEA	30(A1),A1
	CLR.B	31(A1)
pedaploop
	MOVE.W	(A1),30(A1)
	SUBQ.L	#2,A1
	CMP.L	A1,A5
	BLS	pedaploop
pedapskip
	MOVEQ	#21,D0
pedaploop2
	MOVE.B	0(A6,D0.W),D1
	CMP.B	#'A',D1
	BLO	pedapskip2
	CMP.B	#'Z',D1
	BHI	pedapskip2
	ADD.B	#32,D1
pedapskip2
	CMP.B	#' ',D1
	BNE	pedapskip3
	MOVEQ	#0,D1
pedapskip3
	MOVE.B	D1,0(A5,D0.W)
	DBRA	D0,pedaploop2
	MOVE.W	PresetLength(PC),22(A5)
	MOVE.B	PresetFineTune+1(PC),24(A5)
	MOVE.B	#$40,25(A5)
	MOVE.W	PresetRepeat(PC),26(A5)
	MOVE.W	PresetReplen(PC),28(A5)
	RTS

HexToInteger
	MOVEQ	#0,D0
	BSR	Single_hti
	LSL.W	#8,D1
	LSL.W	#4,D1
	OR.W	D1,D0
	BSR	Single_hti
	LSL.W	#8,D1
	OR.W	D1,D0
HexToInteger2
	BSR	Single_hti
	LSL.W	#4,D1
	OR.W	D1,D0
	BSR	Single_hti
	OR.W	D1,D0
	RTS

Single_hti
	MOVEQ	#0,D1
	MOVE.B	(A0)+,D1
	CMP.B	#$60,D1
	BLO	shtiskip
	SUB.B	#$20,D1
shtiskip
	SUB.B	#$30,D1
	CMP.B	#9,D1
	BLS	Return2
	SUBQ.B	#7,D1
	RTS

PED_Delete
	JSR	SetDeletePtrCol
	MOVE.W	#1,PED_Action
	LEA	SelectEntryText(PC),A0
	BRA	ShowStatusText

PED_ClearPLST
	LEA	ClearPLSTText,A0
	JSR	AreYouSure
	BNE	Return2
	BSR	FreePLST
	BRA	PED_Refresh

ClearPLSTText	dc.b 'clear plst ?',0,0

PED_Print
	LEA	PrintPLSTText,A0
	JSR	AreYouSure
	BNE	Return2
	JSR	StorePtrCol
	MOVE.L	DOSbase(PC),A6
	MOVE.L	#PrintPath,D1
	MOVE.L	#1006,D2
	JSR	LVOOpen(A6)
	MOVE.L	D0,FileHandle
	BEQ	CantOpenFile
	JSR	SetDiskPtrCol
	MOVE.L	D0,D1
	MOVE.L	#PsetPLSTtext,D2
	MOVEQ	#56,D3
	JSR	LVOWrite(A6)
	LEA	PrintingPLSTText(PC),A0
	BSR	ShowStatusText
	CLR.W	PsetNumTemp
pedpmainloop	
	MOVE.W	PsetNumTemp(PC),D0
	LEA	PsetPrtNumText(PC),A0
	BSR	IntToDecASCII
	MOVE.L	PLSTmem(PC),A0
	MOVE.W	PsetNumTemp(PC),D0
	MULU	#30,D0
	ADD.L	D0,A0
	MOVE.L	A0,PsetPtrTemp
	LEA	PsetPrtNameText(PC),A1
	MOVE.L	A1,A2
	MOVEQ	#19,D1
FillSpaceLoop
	MOVE.B	#' ',(A2)+
	DBRA	D1,FillSpaceLoop
	MOVEQ	#19,D1
pedploop
	TST.B	(A0)
	BEQ	pedpskip
	MOVE.B	(A0)+,(A1)+
	DBRA	D1,pedploop
pedpskip
	MOVE.L	PsetPtrTemp(PC),A1
	MOVE.W	22(A1),D0
	LEA	PsetPrtLenText(PC),A0
	BSR	IntToHexASCII
	MOVE.L	PsetPtrTemp(PC),A1
	MOVE.W	26(A1),D0
	LEA	PsetPrtRepeatText(PC),A0
	BSR	IntToHexASCII
	MOVE.L	PsetPtrTemp(PC),A1
	MOVE.W	28(A1),D0
	ADD.W	D0,D0
	LEA	PsetPrtRepLenText(PC),A0
	BSR	IntToHexASCII
	MOVE.L	FileHandle(PC),D1
	MOVE.L	#PsetPrtNumText,D2
	MOVEQ	#53,D3
	JSR	LVOWrite(A6)
	BTST	#2,$DFF016
	BEQ	AbortPLSTPrint
	ADDQ.W	#1,PsetNumTemp
	MOVE.W	PsetNumTemp(PC),D0
	CMP.W	PresetTotal(PC),D0
	BNE	pedpmainloop
	BRA	pedpend

AbortPLSTPrint
	LEA	OprAbortedText(PC),A0
	BSR	ShowStatusText
	JSR	SetErrorPtrCol
pedpend	MOVE.L	FileHandle(PC),D1
	JSR	LVOClose(A6)
	BSR	ShowAllRight
	JMP	RestorePtrCol

IntToDecASCII
	MOVEQ	#3,D3
	MOVE.L	#1000,D2
itdloop	EXT.L	D0
	DIVU	D2,D0
	ADD.B	#'0',D0
	MOVE.B	D0,(A0)+
	DIVU	#10,D2
	SWAP	D0
	DBRA	D3,itdloop
	RTS

IntToHex2
	MOVEQ	#1,D2
	BRA	ithaloop

IntToHexASCII
	MOVEQ	#3,D2
ithaloop
	MOVE.W	D0,D1
	AND.B	#15,D1
	CMP.B	#10,D1
	BLO	ithaskip
	ADDQ.B	#7,D1
ithaskip
	ADD.B	#'0',D1
	MOVE.B	D1,-(A0)
	ROR.W	#4,D0
	DBRA	D2,ithaloop
	RTS

WritePLST
	LEA	SavePLSTText,A0
	JSR	AreYouSure
	BNE	Return2
	JSR	StorePtrCol
	JSR	SetDiskPtrCol
	LEA	SavingPLSTText(PC),A0
	BSR	ShowStatusText
	LEA	PTPath,A0
	JSR	CopyPath
	LEA	PLSTName(PC),A0
	MOVEQ	#4,D0
dsploop	MOVE.B	(A0)+,(A1)+
	DBRA	D0,dsploop
	MOVE.L	#FileName,D1
	MOVE.L	#1006,D2
	MOVE.L	DOSbase(PC),A6
	JSR	LVOOpen(A6)
	MOVE.L	D0,D7
	BEQ	CantOpenFile
	MOVE.L	D0,D1
	MOVE.L	PLSTmem(PC),D2
	MOVE.W	PresetTotal(PC),D3
	MULU	#30,D3
	JSR	LVOWrite(A6)
	MOVE.L	D7,D1
	JSR	LVOClose(A6)
	BSR	ShowAllRight
	JMP	RestorePtrCol

CheckPEDnames
	CMP.W	#307,D0
	BLO	PED_PsetHit
	CLR.W	PED_Action
	JSR	SetNormalPtrCol
	CMP.W	#122,D1
	BHS	Return2
	CMP.W	#111,D1
	BHS	PED_Bottom	
	CMP.W	#100,D1
	BHS.S	PED_OneDown
	CMP.W	#67,D1
	BHS	PED_Exit
	CMP.W	#56,D1
	BHS.S	PED_OneUp
	BRA.S	PED_Top

PED_OneUp	
	SUBQ.W	#1,PEDpos
	TST.W	ShiftKeyStatus
	BNE.S	poup2
	BTST	#2,$DFF016
	BNE.S	pedouskip
poup2	SUBQ.W	#7,PEDpos
pedouskip	
	CMP.W	#1,PEDpos
	BGE	ShowPresetNames
PED_Top	MOVE.W	#1,PEDpos
	BRA	ShowPresetNames

PED_OneDown	
	CMP.W	#9,PresetTotal
	BLO	ShowPresetNames
	ADDQ.W	#1,PEDpos
	TST.W	ShiftKeyStatus
	BNE.S	podn2
	BTST	#2,$DFF016
	BNE.S	pedodskip
podn2	ADDQ.W	#7,PEDpos
pedodskip	
	MOVE.W	PresetTotal,D0
	SUB.W	#10,D0
	CMP.W	PEDpos,D0
	BHS	ShowPresetNames
pedodsx	MOVE.W	D0,PEDpos
	BRA	ShowPresetNames

PED_Bottom
	MOVE.W	PresetTotal(PC),D0
	SUB.W	#11,D0
	BMI.S	PED_Top	
	ADDQ.W	#1,D0
	BRA.S	pedodsx

ShowPresetNames
	MOVE.W	#2321,D6
	MOVEQ	#9,D7
	MOVE.L	PLSTmem(PC),D0
	BEQ	Return2
	MOVE.L	D0,A5
	MOVE.W	PEDpos(PC),D0
	MULU	#30,D0
	ADD.L	D0,A5
spndploop
	TST.B	(A5)
	BEQ	Return2
	LEA	PresetNameText,A1
	MOVEQ	#21,D0
spnloop	MOVE.B	#' ',(A1)+
	DBRA	D0,spnloop
	MOVE.L	A5,A0
	ADDQ.L	#3,A0
	LEA	-22(A1),A1
spnloop2
	MOVE.B	(A0)+,D0
	BEQ.S	DisplayPreset
	MOVE.B	D0,(A1)+
	BRA.S	spnloop2

fitutexttab
	dc.b " 0+1+2+3+4+5+6+7-8-7-6-5-4-3-2-1"

DisplayPreset
	MOVEQ	#19,D0
	LEA	PresetNameText,A0
	MOVE.W	D6,D1
	BSR	ShowText3
	ADD.W	#20,D6
	MOVE.W	D6,TextOffset
	MOVEQ	#0,D0
	MOVE.B	24(A5),D0
	AND.B	#$0F,D0
	ADD.W	D0,D0
	LEA	fitutexttab(PC,D0.W),A0
	MOVE.W	#2,TextLength
	BSR	ShowText2
	MOVE.W	22(A5),D0
	ADD.W	D0,D0
	MOVE.W	D0,WordNumber
	ADDQ.W	#1,TextOffset
	BSR	PrintHexWord
	MOVE.W	26(A5),D0
	ADD.W	D0,D0
	MOVE.W	D0,WordNumber
	ADDQ.W	#1,TextOffset
	BSR	PrintHexWord
	MOVE.W	28(A5),D0
	ADD.W	D0,D0
	MOVE.W	D0,WordNumber
	ADDQ.W	#1,TextOffset
	BSR	PrintHexWord
	ADD.W	#220,D6   ; 218
	LEA	30(A5),A5
	DBRA	D7,spndploop
	RTS

PED_EnterPath
	JSR	StorePtrCol
	JSR	SetWaitPtrCol
	CLR.L	EditMode
	LEA	PEdDefaultPath(PC),A6
	BSR	UpdateLineCurPos
	MOVE.L	A6,TextEndPtr
	MOVE.L	A6,ShowTextPtr
	ADD.L	#31,TextEndPtr
	MOVE.W	#20,TextLength
	MOVE.L	#178,A4
	BSR	GetTextLine
	CLR.L	TextEndPtr
	JMP	RestorePtrCol

PED_Disk
	JSR	StorePtrCol
	JSR	SetWaitPtrCol
	CLR.L	EditMode
	CLR.B	SndDiskNum0
	CLR.B	SndDiskNum1
	LEA	SndDiskNum0(PC),A6
	MOVE.L	A6,TextEndPtr
	MOVE.L	A6,ShowTextPtr
	ADDQ.L	#2,TextEndPtr
	MOVE.W	#2,TextLength
	MOVE.L	#621,A4
	MOVE.B	#3,EnterTextFlag
	BSR	GetTextLine
	LEA	SndDiskNum0(PC),A6
	MOVE.B	#':',2(A6)
	CLR.B	EnterTextFlag
	CLR.L	TextEndPtr
	JMP	RestorePtrCol

ShowPEDnumbers
	MOVE.W	PresetTotal,WordNumber
	MOVE.W	#1061,TextOffset
	BSR	Print4DecDigits
	LEA	PEdDefaultPath,A0
	MOVE.W	#178,D1
	MOVEQ	#20,D0
	BSR	ShowText3
	LEA	PEdDefaultVol,A0
	MOVE.W	#618,D1
	MOVEQ	#6,D0
	BRA	ShowText3

PED_PsetHit
	CMP.W	#1,PresetTotal
	BEQ.S	pedphend
	MOVE.W	MouseY2(PC),D0
	CMP.W	#59,D0
	BLO.S	pedphend
	CMP.W	#119,D0
	BHS.S	pedphend
	SUB.W	#59,D0
	DIVU	#6,D0
	MOVE.L	D0,D1
	SWAP	D1
	CMP.W	#5,D1
	BEQ.S	pedphend
	MOVE.W	D0,D2
	ADD.W	PEDpos(PC),D2
	CMP.W	PresetTotal(PC),D2
	BHI.S	pedphend
	MULU	#30,D0
	MOVE.L	PLSTmem(PC),A5
	ADD.L	D0,A5
	MOVE.W	PEDpos(PC),D0
	MULU	#30,D0
	ADD.L	D0,A5
	TST.W	PED_Action
	BEQ.S	PED_CopyName
	CMP.W	#1,PED_Action
	BEQ	PED_DoDelete
pedphend
	RTS

PED_CopyName
	LEA	PresetName(PC),A0
	MOVEQ	#21,D0
pedcnloop
	MOVE.B	(A5)+,D1
	BNE	pedcnskip
	MOVE.B	#' ',D1
pedcnskip
	MOVE.B	D1,(A0)+
	DBRA	D0,pedcnloop
	MOVEQ	#0,D0
	MOVE.B	2(A5),D0
	AND.B	#$0F,D0
	ADD.W	D0,D0
	LEA	fitutexttab(PC),A1
	LEA	(A1,D0.W),A1
	LEA	PsetVolText(PC),A0
	MOVE.B	#' ',-1(A0)
	MOVE.B	(A1)+,(A0)+
	MOVE.B	(A1),(A0)
	MOVE.W	(A5),D0
	ADD.W	D0,D0
	LEA	6(A0),A0
	BSR	IntToHexASCII
	MOVE.W	4(A5),D0
	ADD.W	D0,D0
	LEA	9(A0),A0
	BSR	IntToHexASCII
	LEA	9(A0),A0
	MOVE.W	6(A5),D0
	ADD.W	D0,D0
	BSR	IntToHexASCII
	BRA	PossibleEdit

PED_DoDelete
	CLR.W	PED_Action
	LEA	DeletePresetText,A0
	JSR	AreYouSure
	BNE	Return2
	MOVE.L	PLSTmem(PC),A1
	MOVE.W	PresetTotal(PC),D0
	MULU	#30,D0
	ADD.L	D0,A1
pedddloop
	MOVE.W	30(A5),(A5)
	ADDQ.L	#2,A5
	CMP.L	A5,A1
	BHI.S	pedddloop
	CLR.B	(A5)
	SUBQ.W	#1,PresetTotal
	SUB.L	#30,MaxPLSTOffset
	MOVE.W	PEDpos(PC),D0
	ADD.W	#9,D0
	CMP.W	PresetTotal(PC),D0
	BHI.S	pedddskip
	SUBQ.W	#1,PEDpos
pedddskip	
	BSR	ShowPEDnumbers
	BSR	ShowPresetNames
	JSR	SetNormalPtrCol
	JMP	StorePtrCol

sepc	JMP	SetErrorPtrCol

PLSTisFull
	LEA	PLSTFullText(PC),A0
	BSR	ShowStatusText
	JMP	SetErrorPtrCol

PED_DeleteDisk
	LEA	DelDiskText(PC),A0
	MOVE.B	snddisknum0(PC),10(A0)
	MOVE.B	snddisknum1(PC),11(A0)
	JSR	AreYouSure
	BNE	Return2
	JSR	StorePtrCol
	JSR	SetWaitPtrCol
	MOVE.L	PLSTmem(PC),A0
	MOVE.L	A0,A1
	MOVE.W	PresetTotal(PC),D0
	MULU	#30,D0
	ADD.L	D0,A1
	MOVE.L	snddisknum0-1(PC),D1
	MOVE.L	#$FF5F5FFF,D2
	AND.L	D2,D1
peddslo	MOVE.L	2(A0),D0
	AND.L	D2,D0
	CMP.L	D1,D0
	BNE.S	peddsno
	MOVE.L	A0,A2
peddslp	MOVE.W	30(A0),(A0)+
	CMP.L	A1,A0
	BLO.S	peddslp
	SUBQ.W	#1,PresetTotal
	SUB.L	#30,MaxPLSTOffset
	MOVE.L	A2,A0
	BRA.S	peddsn1
peddsno	LEA	30(A0),A0
peddsn1	CMP.L	A1,A0
	BLO.S	peddslo
	MOVE.W	#1,PEDpos
	BSR	ShowPEDnumbers
	BSR	ShowPresetNames
	JMP	RestorePtrCol

DelDiskText	dc.b "Delete ST-.. ?",0,0

;************  MIDI Routines  ************

*  Apollon MIDI Routines V0.2               *
*  V0.2 04/07-1991 First version            *

_RVOAllocMiscResource =  -6
_RVOFreeMiscResource  = -12

OpenMIDI
	TST.L	MIDIinBuffer
	BNE.S	omidisk
	MOVE.L	#256,D0
	MOVE.L	#MEMF_PUBLIC!MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,MIDIinBuffer
	BEQ	Return2
omidisk	CLR.B	MIDIinTo
	CLR.B	MIDIinFrom

	BSR.S	.GetSer2		;got the port?
	BEQ.S	.end			;yes
	MOVE.L	4.W,A6			;no..try to flush serial.device:
	JSR	LVOForbid(A6)
	LEA	DeviceList(A6),A0	;ExecBase->DeviceList
	LEA	SerDevName(PC),a1	;"serial.device"
	JSR	LVOFindName(A6)
	TST.L	D0
	BEQ.S	.notfnd			;no serial.device!!
	MOVE.L	D0,A1
	JSR	LVORemDevice(A6)
.notfnd	JSR	LVOPermit(A6)
	BSR.S	.GetSer2		;now try it again...
.end	RTS

.GetSer2
	MOVE.L	4.W,A6
	MOVEQ	#0,D0
	LEA	MiscResName(PC),a1
	JSR	LVOOpenResource(A6)
	MOVE.L	D0,MiscResBase
	BEQ	.gs_err
	MOVE.L	D0,A6
	LEA	rb_ProgName,A1
	MOVEQ	#0,D0		;serial port
	JSR	_RVOAllocMiscResource(A6)
	TST.L	D0
	BNE.S	.gs_err
	ST.B	SerPortAlloc
	CLR.W	PrevBits
	MOVE.W	$DFF01C,D0
	BTST	#0,D0
	SNE.B	PrevBits
	BTST	#11,D0
	SNE.B	PrevBits+1
	MOVEQ	#0,D0		;TBE
	LEA	MIDIOutInterrupt(PC),A1
	MOVE.L	4.W,A6
	JSR	LVOSetIntVector(A6)
	MOVE.L	D0,PrevTBE
	MOVEQ	#11,D0		;RBF
	LEA	MIDIInInterrupt(pc),A1
	JSR	LVOSetIntVector(A6)
	MOVE.L	D0,PrevRBF
	MOVE.W	#114,$DFF032	;set baud rate 114/31250 (SERPER)
	MOVE.W	#$8801,$DFF09A	;RBF & TBE on!!

	MOVE.W	#30000,D0
.lolo	MOVE.W	#$0F0,$DFF180
	DBRA	D0,.lolo
	MOVEQ	#0,D0
	RTS

.gs_err	MOVE.W	#30000,D0
.lplp	MOVE.W	#$F00,$DFF180
	DBRA	D0,.lplp
	MOVEQ	#-1,D0
	RTS


;----- Close MIDI and release serial port -----

CloseMIDI
	MOVE.L	MIDIinBuffer(PC),D0
	BEQ.S	clmskip
	MOVE.L	D0,A1
	MOVE.L	#256,D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	CLR.L	MIDIinBuffer
clmskip
	TST.L	MiscResBase
	BEQ.S	.fs_end
	TST.B	SerPortAlloc
	BEQ.S	.fs_end
	MOVE.W	#$0801,$dff09a	;disable RBF & TBE
	MOVE.L	PrevTBE(PC),a1
	MOVEQ	#0,D0
	MOVE.L	4.W,A6
	JSR	LVOSetIntVector(A6)
	MOVE.L	PrevRBF(PC),a1
	MOVEQ	#11,D0
	JSR	LVOSetIntVector(A6)
	MOVE.W	#$8000,D0
	TST.B	PrevBits
	BEQ.S	.fs1
	BSET	#0,D0
.fs1	TST.B	PrevBits+1
	BEQ.S	.fs2
	BSET	#11,D0
.fs2	MOVE.W	D0,$DFF09A	;set RBF & TBE to their prev. values
	MOVE.L	MiscResBase(PC),A6
	MOVEQ	#0,D0		;serial port
	JSR	_RVOFreeMiscResource(A6)
	CLR.B	SerPortAlloc
	CLR.B	lastcmdbyte
.fs_end	RTS

mintflag dc.w 0


;----- Every time we receive a MIDI byte -----

MIDIInIntHandler
	MOVE.W	$DFF018,D0 ; read from serdatr
	MOVE.W	#$0800,$DFF09C ;intreq

	MOVE.W	#$F00,$DFF180

	MOVEM.L	D1-D2,-(SP)
gmiskip	MOVEQ	#0,D1
	MOVE.B	4(A1),D1 ; in to
	MOVE.L	D1,D2
	ADDQ.B	#1,D2
	CMP.B	5(A1),D2 ; in from
	BEQ	gmiexit	; Buffer overflow
	MOVE.L	(A1),A0 ; midi in buffer
	MOVE.B	D0,(A0,D1.W)
	MOVE.B	D2,4(A1); MIDIinTo
gmiexit	MOVEM.L	(SP)+,D1-D2
	RTS


;----- MIDI Transmit Buffer Empty Interrupt (Output) -----

MIDIOutIntHandler
	MOVE.W	#$4000,intena(A0)	;disable int.
	ADDQ.B	#1,IDNestCnt(A6)	;turn off multitasking
	MOVE.W	#1,intreq(A0)		;clear intreq bit
	MOVE.B	bytesinbuff(PC),D0
	BEQ.S	exsih			;buffer empty
	MOVE.L	4(A1),A5		;get buffer read pointer
	MOVE.W	#$100,D1		;Stop bit
	MOVE.B	(A5),D1			;get byte
	MOVE.W	D1,$030(A0)		;and push it out!!
	ADDQ.L	#1,A5			;add 1
	CMP.L	A1,A5			;shall we reset ptr??
	BNE.S	nbufptr			;not yet..
	LEA	sendbuffer(PC),A5
nbufptr	SUBQ.B	#1,D0			;one less bytes in buffer
	MOVE.B	D0,bytesinbuff		;remember it
	MOVE.L	A5,4(A1)		;push new read pointer back
exsih	SUBQ.B	#1,IDNestCnt(A6)	;multitasking back on
	BGE.S	exsih0
	MOVE.W	#$C000,intena(A0)
exsih0	RTS

;----- Send data to MIDI Out or to output buffer -----
; A0=ptr to data, D0=length

AddMIDIData
	TST.B	SerPortAlloc
	BEQ.S	retamd
	MOVE.L	A2,-(SP)
	MOVE.L	4.W,A6
	MOVE.W	#$4000,$DFF09A	;Disable interrupts
	ADDQ.B	#1,IDNestCnt(A6)
	MOVE.B	bytesinbuff(pc),D1
	BNE.S	noTBEreq
	MOVE.W	#$8001,$DFF09C	;request TBE
noTBEreq
	LEA	buffptr(PC),A2	;end of buffer (ptr)
	MOVE.L	(A2),A1		;buffer pointer
adddataloop
	MOVE.B	(A0)+,D1	;get byte
	BPL.S	norscheck	;this isn't a status byte
	CMP.B	#$EF,D1		;forget system messages
	BHI.S	norscheck
	CMP.B	lastcmdbyte(PC),D1 ;same as previos status byte??
	BEQ.S	samesb		;yes, skip
	MOVE.B	D1,lastcmdbyte	;no, don't skip but remember!!
norscheck
	MOVE.B	D1,(A1)+	;push it to midi send buffer
	ADDQ.B	#1,bytesinbuff
samesb	CMP.L	A2,A1	;end of buffer??
	BNE.S	noresbuffptr	;no, no!!
	LEA	sendbuffer(pc),a1 ;better reset it to avoid trashing
noresbuffptr
	SUBQ.B	#1,D0
	BNE.S	adddataloop
	MOVE.L	A1,(A2)		;push new buffer ptr back
overflow
	SUBQ.B	#1,IDNestCnt(A6)
	BGE.S	retamd1
	MOVE.W	#$C000,$DFF09A	;enable interrupts again
retamd1	MOVE.L	(SP)+,A2
retamd	RTS

PrevTBE		dc.l	0
PrevRBF		dc.l	0		
PrevBits	dc.b	0,0

MIDIOutInterrupt
	dc.l	0,0
	dc.b	2,0
	dc.l	MIDIOutName,buffptr,MIDIOutIntHandler

MIDIInInterrupt
	dc.l	0,0
	dc.b	2,0
	dc.l	MIDIInName,MIDIinBuffer,MIDIInIntHandler

buffptr		dc.l	sendbuffer
readbuffptr	dc.l	sendbuffer
sendbuffer	ds.b	128

SerPortAlloc	dc.b	0
bytesinbuff	dc.b	0
MiscResBase	dc.l	0
lastcmdbyte	dc.b	0
		even

MIDIOutName	dc.b	'PT MIDI Out',0
MIDIInName	dc.b	'PT MIDI In',0
MiscResName	dc.b	'misc.resource',0
SerDevName	dc.b	'serial.device',0
		even


;----- read from input buffer -----

MIDIin	MOVE.B	MIDIinFrom(PC),D0
	CMP.B	MIDIinTo(PC),D0
	BNE	migetbyt
	MOVEQ	#2,D1
	RTS
migetbyt
	MOVE.L	MIDIinBuffer(PC),A0
	MOVE.L	A0,$7FFF0
	MOVE.B	(A0,D0.W),D0
	ADDQ.B	#1,MIDIinFrom
	MOVEQ	#0,D1
	RTS

CheckMIDIin
	TST.B	MIDIFlag
	BEQ	Return2
	BSR	MIDIin
	TST.L	D1
	BEQ	mic_ok
	MOVE.B	#1,MIDIError
	RTS

mic_ok	CLR.B	MIDIError
	TST.B	D0
	BPL	mic_databyte
;statusbyte here
	CMP.B	#$F0,D0
	BHS	MIDISysMessage
	MOVE.B	D0,MIDIRunStatus
	MOVE.B	D0,D1
	AND.B	#$F0,D1
	MOVE.B	D1,MIDIRunCommand
	AND.B	#$0F,D0
	MOVE.B	D0,MIDIRunChannel
	CLR.B	MIDIByteCount
	RTS

mic_databyte
	MOVE.B	MIDIRunCommand(PC),D1
	CMP.B	#$80,D1
	BEQ	M_NoteOff
	CMP.B	#$90,D1
	BEQ	M_NoteOn
	CMP.B	#$A0,D1
	BEQ	M_PolyTouch
	CMP.B	#$B0,D1
	BEQ	M_Control
	CMP.B	#$C0,D1
	BEQ	M_ProgChange
	CMP.B	#$D0,D1
	BEQ	M_MonoTouch
	CMP.B	#$E0,D1
	BEQ	M_PitchBend
	CMP.B	#$F0,D1
	BEQ	M_SysExData
	RTS

M_NoteOff
	TST.B	MIDIByteCount
	BNE	mnf_veloc
	MOVE.B	D0,MIDINote
	ADD.B	#1,MIDIByteCount
	RTS
mnf_veloc
	CLR.B	MIDIByteCount
	MOVE.B	D0,MIDIVelocity
;	BSR	MidiPlay
	RTS

M_NoteOn
	MOVE.W	#$F0F,$DFF180
	TST.B	MIDIByteCount
	BNE	mno_veloc
	MOVE.B	D0,MIDINote
	ADD.B	#1,MIDIByteCount
	RTS
mno_veloc
	CLR.B	MIDIByteCount
	MOVE.B	D0,MIDIVelocity
	BEQ	mnf_veloc
;* MidiPlay *
	MOVE.B	MIDINote,D0
	CMP.B	MIDIinTrans,D0
	BLO	miplskip
	SUB.B	MIDIinTrans(PC),D0
	CMP.B	#36,D0
	BLS	J_nkp
	RTS
miplskip
	CMP.B	XMIDI_Play,D0
	BEQ	J_PlaySong
	CMP.B	XMIDI_Pattern,D0
	BEQ	J_PlayPattern
	CMP.B	XMIDI_Edit,D0
	BEQ	J_Edit
	CMP.B	XMIDI_Record,D0
	BEQ	J_RecordPattern
	CMP.B	XMIDI_Stop,D0
	BEQ	J_StopIt
	RTS

J_nkp	JMP	nkpskip
J_PlaySong
	JMP	PlaySong
J_PlayPattern
	JMP	PlayPattern
J_Edit	JMP	Edit
J_RecordPattern
	JMP	RecordPattern
J_StopIt
	JMP	StopIt

MIDIinTrans	dc.b 60,0

XMIDI_Play	dc.b 40 ; E
XMIDI_Pattern	dc.b 38 ; D
XMIDI_Edit	dc.b 43 ; G
XMIDI_Record	dc.b 41 ; F
XMIDI_Stop	dc.b 36 ; C
		dc.b 0 ; free 0

M_PolyTouch
	TST.B	MIDIByteCount
	BNE	mpt_touch
	MOVE.B	D0,MIDINote
	ADD.B	#1,MIDIByteCount
	RTS
mpt_touch
	CLR.B	MIDIByteCount
	MOVE.B	D0,MIDITouch
	RTS

M_Control
	TST.B	MIDIByteCount
	BNE	mc_value
	MOVE.B	D0,MIDIController
	ADD.B	#1,MIDIByteCount
	RTS
mc_value
	CLR.B	MIDIByteCount
	MOVE.B	D0,MIDIlsb
	RTS

M_ProgChange
	MOVE.B	D0,MIDIProgram
	RTS
;	ADDQ.B	#1,D0
;	AND.B	#$1F,D0
;	BNE	mpchskip
;	TST.W	InsNum
;	BEQ	mpchskip
;	MOVE.W	InsNum(PC),LastInsNum
;mpchskip
;	MOVE.B	D0,InsNum+1
;	BRA	ShowSampleInfo

M_MonoTouch
	MOVE.B	D0,MIDITouch
	RTS

M_PitchBend
	TST.B	MIDIByteCount
	BNE	mp_msb
	MOVE.B	D0,MIDIlsb
	ADD.B	#1,MIDIByteCount
	RTS
mp_msb	CLR.B	MIDIByteCount
	MOVE.B	D0,MIDImsb
	EXT.W	D0
	SUB.W	#128,D0
	MOVE.W	CurrentPlayNote,D1
	SUB.W	D0,D1
	CMP.W	#113,D1
	BLS.S	mp_2
	MOVE.W	#113,D1
mp_2	MOVE.L	NoteAddr,A0
	MOVE.W	D1,6(A0)
	RTS

NoteAddr	dc.l 0

M_rts	RTS

MIDISysMessage
	CMP.B	#$F0,D0
	BEQ	M_SysEx		; System Exclusive
	CMP.B	#$F1,D0
	BEQ	M_rts		; Quarter Frame (MIDI Time Code)
	CMP.B	#$F2,D0
	BEQ	M_SongPos	; Song Position Pointer
	CMP.B	#$F3,D0
	BEQ	M_SongSelect	; Song Select
	CMP.B	#$F4,D0
	BEQ	M_rts		; -Reserved-
	CMP.B	#$F5,D0
	BEQ	M_rts		; -Reserved-
	CMP.B	#$F6,D0
	BEQ	M_rts		; -Reserved-
	CMP.B	#$F7,D0
	BEQ	M_EOX		; End of System Exclusive
	CMP.B	#$F8,D0
	BEQ	M_rts		; MIDI Timing Clock
	CMP.B	#$F9,D0
	BEQ	M_rts		; -Reserved-
	CMP.B	#$FA,D0
	BEQ	M_Start		; Start Message
	CMP.B	#$FB,D0
	BEQ	M_Continue	; Continue Message
	CMP.B	#$FC,D0
	BEQ	M_Stop		; Stop Message
	CMP.B	#$FD,D0
	BEQ	M_rts		; --- Reserved ---
	CMP.B	#$FE,D0
	BEQ	M_rts		; Active Sensing (Each 300ms if on)
	CMP.B	#$FF,D0
	BRA	M_rts		; System Reset Message

M_SysEx		RTS
M_SysExData	RTS
M_SongPos	RTS
M_SongSelect	RTS
M_EOX		RTS

M_Start		JMP	PlaySong
M_Continue	RTS
M_Stop		JMP	StopIt

MIDIinBuffer	dc.l 0
MIDIinTo	dc.b 0
MIDIinFrom	dc.b 0

MIDIRunStatus	dc.b $80
MIDIRunChannel	dc.b 0
MIDIRunCommand	dc.b $08
MIDIByteCount	dc.b 0

MIDINote	dc.b 0
MIDIVelocity	dc.b 0
MIDITouch	dc.b 0
MIDIController	dc.b 0
MIDImsb		dc.b 0	
MIDIlsb		dc.b 0
MIDIProgram	dc.b 0
MIDIError	dc.b 0


;---- Sampler Screen ----

SamplerScreen
	CLR.B	RawKeyCode
	JSR	WaitForButtonUp
	TST.W	SamScrEnable
	BNE	ExitFromSam
	MOVE.W	#1,SamScrEnable
	MOVE.L	EditMode(PC),SaveEditMode
	CLR.L	EditMode

	MOVE.L	TextBplPtr(PC),A0
	LEA	$15B8(A0),A0
	MOVE.L	A0,D0
	LEA	CopList2Bpl4Ptr,A1
	MOVE.W	D0,6(A1)
	SWAP	D0
	MOVE.W	D0,2(A1)

	MOVEQ	#0,D0
	MOVE.W	#270,D1
	MOVEQ	#14,D2
	LEA	CursorSpriteData,A0
	BSR	SetSpritePos
	JSR	SetSamSpritePtrs
	MOVE.W	CopCol0+16,CopList2+18
	BSR	SwapSamScreen
	BEQ	exisam2
	BSR	ClearSamScr
	JSR	DoShowFreeMem
	BSR	ShowSamNote
	BSR	ShowResamNote
	BRA	RedrawSample

ExitFromSam
	JSR	WaitForButtonUp
	MOVE.L	SamMemPtr,D0
	BEQ	exisam2
	MOVE.L	D0,A1
	BSR	Bjarne
	BSR.S	FreeDecompMem2
exisam2	JSR	SetDefSpritePtrs
	CLR.B	RawKeyCode
	CLR.W	SamScrEnable
	MOVEQ	#-1,D0
	MOVE.L	D0,MarkStartOfs
	MOVE.L	SaveEditMode(PC),EditMode
	BSR	SetScreenColors2
	JSR	SetupVUCols
	BSR	SetScrPatternPos
	BSR	ClearSamScr
	JSR	UpdateCursorPos
	JSR	SetTempo
	BRA	RedrawPattern

FreeDecompMem2
	MOVE.L	SamMemPtr,D0
	BEQ	Return2
	MOVE.L	D0,A1
	MOVE.L	SamMemSize,D0
	CLR.L	SamMemPtr
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	RTS

Decompact2
	MOVE.L	A0,CompPtr
	MOVE.L	D0,CompLen
	BSR.S	FreeDecompMem2
	MOVE.L	CompPtr(PC),A0
	MOVE.L	(A0),D0
	MOVE.L	D0,SamMemSize
	MOVEQ	#0,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,SamMemPtr
	BEQ	OutOfMemErr
	MOVE.L	D0,A1
	MOVE.L	CompPtr(PC),A0
	MOVE.L	CompLen(PC),D0
	ADDQ.L	#4,A0
	SUBQ.L	#4,D0
dcmlop3	MOVE.B	(A0)+,D1
	CMP.B	#181,D1
	BEQ.S	DecodeIt3
	MOVE.B	D1,(A1)+
decom3	SUBQ.L	#1,D0
	CMP.L	#0,D0
	BGT.S	dcmlop3
	MOVE.L	SamMemPtr(PC),A1
	MOVEQ	#-1,D0
	RTS

DecodeIt3
	MOVEQ	#0,D1
	MOVE.B	(A0)+,D1
	MOVE.B	(A0)+,D2
dcdlop3	MOVE.B	D2,(A1)+
	DBRA	D1,dcdlop3
	SUBQ.L	#2,D0
	BRA.S	decom3

SwapSamScreen
	LEA	SampScreenData,A0
	MOVE.L	#SampScreenSize,D0
	BSR	Decompact2
	BEQ	Return2

bjasize=134*10
Bjarne	LEA	SamScrPos,A0
	MOVEQ	#1,D2
BjaLop1	MOVE.W	#bjasize-1,D1 ; 134
BjaLop2	MOVE.L	(A0),D0
	MOVE.L	(A1),(A0)+
	MOVE.L	D0,(A1)+
	DBRA	D1,BjaLop2
	ADD.L	#10240-bjasize*4,A0
	DBRA	D2,BjaLop1

	LEA	CopListInsPos,A0
	LEA	CopList2,A1
	MOVEQ	#29,D1
BjaLoop	MOVE.W	(A0),D0
	MOVE.W	(A1),(A0)+
	MOVE.W	D0,(A1)+
	DBRA	D1,BjaLoop
	MOVEQ	#-1,D0
	RTS

ClearSamScr
	MOVE.W	#5121,TextOffset
	MOVE.W	#2,TextLength
	MOVE.L	#blnktxt,ShowTextPtr
	BSR	ShowText
	MOVE.W	#4964,TextOffset
	MOVE.W	#3,TextLength
	BSR	ShowText
	MOVE.W	#130*10-1,D0
	MOVE.L	TextBplPtr,A0
	LEA	$15B8(A0),A0
	MOVE.L	A0,LineScreen
	MOVEQ	#0,D1
clrsslp	MOVE.L	D1,(A0)+
	DBRA	D0,clrsslp
	RTS

blnktxt	dc.b "    "

ClearSamArea
	MOVE.W	#64*10-1,D0
	MOVE.L	TextBplPtr,A0
	LEA	$15B8(A0),A0
	MOVE.L	A0,LineScreen
	MOVEQ	#0,D1
clrsare	MOVE.L	D1,(A0)+
	DBRA	D0,clrsare
	RTS

CheckSamGadgets
	MOVEM.W	MouseX2,D0/D1
	CMP.W	#139,D1
	BLO	SamTopBar
	CMP.W	#139+64,D1
	BLO	SamplePressed
	CMP.W	#201+11,D1
	BLO	SamDragBar
	CMP.W	#201+22,D1
	BLO	SamMenu1
	CMP.W	#201+33,D1
	BLO	SamMenu2
	CMP.W	#201+44,D1
	BLO	SamMenu3
	CMP.W	#201+66,D1
	BLO	SamMenu4
	RTS

SamTopBar
	CMP.W	#32,D0
	BLO	ExitFromSam
	RTS

SamMenu1
	CMP.W	#32,D0
	BLO	Return2
	CMP.W	#96,D0
	BLO	PlayWaveform
	CMP.W	#176,D0
	BLO	ShowRange
	CMP.W	#246,D0
	BLO	ZoomOut
	BRA	DispBox

SamMenu2
	CMP.W	#32,D0
	BLO	StopPlaying
	CMP.W	#96,D0
	BLO	PlayDisplay
	CMP.W	#176,D0
	BLO	ShowAll
	CMP.W	#246,D0
	BLO	RangeAll
	BRA	LoopToggle

SamMenu3
	CMP.W	#32,D0
	BLO	StopPlaying
	CMP.W	#96,D0
	BLO	PlayRange
	CMP.W	#116,D0
	BLO	CurToStart
	CMP.W	#136,D0
	BLO	CurToEnd
	CMP.W	#176,D0
	BLO	SwapBuffer
	CMP.W	#246,D0
	BLO	Sampler
	BRA	SetSamNote

SamMenu4
	CMP.W	#32,D0
	BLO	SamCut
	CMP.W	#64,D0
	BLO	SamCopy
	CMP.W	#96,D0
	BLO	SamPaste
	CMP.W	#136,D0
	BLO	RampVolume
	CMP.W	#176,D0
	BLO	TuningTone
	CMP.W	#246,D0
	BLO	Resample
	BRA	SetResamNote


PlayWaveform
	JSR	PlayNote
	JMP	WaitForButtonUp
PlayDisplay
	LEA	SampleInfo,A0
	MOVE.L	SamOffset(PC),StartOfs
	MOVE.L	SamDisplay(PC),D0
	LSR.L	#1,D0
	MOVE.W	D0,(A0)
	CLR.W	4(A0)
	MOVE.W	#1,6(A0)
	MOVE.W	PlayInsNum,-(SP)
	CLR.W	PlayInsNum
	JSR	PlayNote
	MOVE.W	(SP)+,PlayInsNum
	BSR	ShowSampleInfo
	JMP	WaitForButtonUp
PlayRange
	MOVE.L	MarkStartOfs(PC),D1
	BMI	NoRangeError
	MOVE.L	MarkEndOfs(PC),D0
	CMP.L	D0,D1
	BEQ	LargerRangeError
	LEA	SampleInfo,A0
	MOVE.L	D1,StartOfs
	SUB.L	D1,D0
	LSR.L	#1,D0
	MOVE.W	D0,(A0)
	CLR.W	4(A0)
	MOVE.W	#1,6(A0)
	MOVE.W	PlayInsNum,-(SP)
	CLR.W	PlayInsNum
	JSR	PlayNote
	MOVE.W	(SP)+,PlayInsNum
	BSR	ShowSampleInfo
	JMP	WaitForButtonUp
StopPlaying
	BRA	TurnOffVoices

StartOfs
	dc.l	0

;----

ShowRange
	MOVE.L	MarkStartOfs(PC),D0
	BMI	NoRangeError
	MOVE.L	MarkEndOfs(PC),D1
	CMP.L	D1,D0
	BEQ	LargerRangeError
	SUB.L	D0,D1
	BNE	shorano
	MOVEQ	#1,D1
shorano	MOVE.L	D1,SamDisplay
	MOVE.L	D0,SamOffset
	MOVEQ	#-1,D0
	MOVE.L	D0,MarkStartOfs
	CLR.W	MarkStart
	BSR	DisplaySample
	JMP	WaitForButtonUp

ZoomOut	MOVE.L	SamDisplay(PC),D0
	MOVE.L	SamLength(PC),D1
	MOVE.L	SamOffset(PC),D2
	MOVE.L	D0,D3
	LSL.L	#1,D3
	CMP.L	D1,D3
	BHI	ShowAll
	LSR.L	#1,D0
	CMP.L	D2,D0
	BLO	zoomou2
	MOVEQ	#0,D0
zoomou2	SUB.L	D0,D2
	MOVE.L	D2,D0
	ADD.L	D3,D0
	CMP.L	D1,D0
	BLS	zoomou3
	SUB.L	D3,D1
	MOVE.L	D1,D2
zoomou3	MOVE.L	D2,SamOffset
	MOVE.L	D3,SamDisplay
	BSR	OffsetToMark
	MOVE.L	MarkStartOfs(PC),D0
	CMP.L	MarkEndOfs(PC),D0
	BNE	zoomouo
	MOVEQ	#-1,D0
	MOVE.L	D0,MarkStartOfs
zoomouo	BSR	DisplaySample
	JMP	WaitForButtonUp

ShowAll	CLR.L	SamOffset
	MOVE.L	SamLength(PC),SamDisplay
	BSR	OffsetToMark
	MOVE.L	MarkStartOfs(PC),D0
	CMP.L	MarkEndOfs(PC),D0
	BNE	shoallo
	MOVEQ	#-1,D0
	MOVE.L	D0,MarkStartOfs
	CLR.W	MarkStart
shoallo	BSR	DisplaySample
	JMP	WaitForButtonUp

RangeAll
	BSR	InvertRange
	MOVE.W	#3,MarkStart
	MOVE.W	#316,MarkEnd
	BSR	MarkToOffset
ranall2	BSR	InvertRange
	JMP	WaitForButtonUp


CurToStart
	BSR	InvertRange
	MOVEQ	#3,D0
	MOVE.W	D0,MarkStart
	MOVE.W	D0,MarkEnd
	MOVE.L	SamOffset(PC),D0
	MOVE.L	D0,MarkStartOfs
	MOVE.L	D0,MarkEndOfs
	BRA.S	ranall2

CurToEnd
	BSR	InvertRange
	MOVE.W	#316,D0
	MOVE.W	D0,MarkStart
	MOVE.W	D0,MarkEnd
	MOVE.L	SamOffset(PC),D0
	ADD.L	SamDisplay(PC),D0
	MOVE.L	D0,MarkStartOfs
	MOVE.L	D0,MarkEndOfs
	BRA.S	ranall2

SwapBuffer
	MOVE.W	InsNum(PC),D1
	BEQ	NotSampleNull
	LSL.W	#2,D1
	LEA	SamplePtrs,A0
	LEA	(A0,D1.W),A0

	MOVE.L	CopyBufPtr(PC),D0
	MOVE.L	(A0),CopyBufPtr
	MOVE.L	D0,(A0)
	MOVE.L	D0,A1
	CLR.W	(A1)

	MOVE.L	CopyBufSize(PC),D0
	MOVE.L	124(A0),CopyBufSize
	MOVE.L	D0,124(A0)

	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	InsNum(PC),D1
	MULU	#30,D1
	LEA	12(A0,D1.W),A0
	LSR.L	#1,D0
	MOVE.W	D0,(A0)
	MOVE.L	RepBuf(PC),D0
	MOVE.L	4(A0),RepBuf
	MOVE.L	D0,4(A0)

	MOVE.W	RepBuf2(PC),D0
	MOVE.W	2(A0),RepBuf2
	TST.B	D0
	BNE.S	swabuf2
	MOVE.B	#$40,D0
swabuf2	MOVE.W	D0,2(A0)
	JSR	TurnOffVoices
	BSR	ValidateLoops
	BSR	ShowSampleInfo
	BSR	UpdateRepeats
	BSR	RedrawSample
	JMP	WaitForButtonUp

RepBuf	dc.l	1
RepBuf2	dc.w	$0040

;----

NoRangeError
	LEA	NoRangeText(PC),A0
nres2	BSR	ShowStatusText
	JMP	SetErrorPtrCol

LargerRangeError
	LEA	LargerRangeText(PC),A0
	BRA.S	nres2

SetCursorError
	LEA	SetCursorText(PC),A0
	BRA.S	nres2

BufIsEmptyError
	LEA	BufIsEmptyText(PC),A0
	BRA.S	nres2

EmptySampleError
	LEA	EmptySampleText(PC),A0
	BRA.S	nres2

NoRangeText	dc.b "no range selected",0
LargerRangeText	dc.b "set larger range",0
SetCursorText	dc.b "set cursor pos",0
BufIsEmptyText	dc.b "buffer is empty",0
EmptySampleText	dc.b "sample is empty",0

;----

DispBox
	RTS

LoopToggle
	JSR	WaitForButtonUp
	MOVE.W	InsNum(PC),D1
	BEQ	NotSampleNull
	MOVE.L	SongDataPtr(PC),A0
	MULU	#30,D1
	LEA	12(A0,D1.W),A0
	TST.W	LoopOnOffFlag
	BEQ.S	loopton
	MOVE.L	4(A0),SavSamInf	
	MOVEQ	#1,D0
	MOVE.L	D0,4(A0)
	BSR	ValidateLoops
	JSR	TurnOffVoices
looptlo	BSR	ShowSampleInfo
	BSR	UpdateRepeats
	BRA	DisplaySample
loopton	MOVE.L	SavSamInf(PC),D0
	BNE.S	loopto2
	MOVE.W	(A0),D0
loopto2	MOVE.L	D0,4(A0)
	BSR	ValidateLoops
	BRA	looptlo

ShowLoopToggle
	LEA	ToggleOFFText(PC),A0
	TST.W	LoopOnOffFlag
	BEQ.S	sltskip
	LEA	ToggleONText(PC),A0
sltskip	MOVEQ	#3,D0
	MOVE.W	#9076,D1
	BRA	ShowText3

LoopOnOffFlag	dc.w 0
SavSamInf	dc.l 0

SetSamNote
	BSR	ShowResamNote
	MOVE.W	#9516,TextOffset
	MOVE.W	#3,TextLength
	MOVE.L	#SpcNoteText,ShowTextPtr
	BSR	ShowText
	MOVE.W	#1,SamNoteType
	MOVE.L	#SampleNote,SplitAddress
	JMP	WaitForButtonUp

SetResamNote
	BSR	ShowSamNote
	MOVE.W	#9956,TextOffset
	MOVE.W	#3,TextLength
	MOVE.L	#SpcNoteText,ShowTextPtr
	BSR	ShowText
	MOVE.W	#2,SamNoteType
	MOVE.L	#ResampleNote,SplitAddress
	JMP	WaitForButtonUp

ResampleText	dc.b "Resample?",0
ResamplingText	dc.b "Resampling...",0

Resample
	LEA	ResampleText(PC),A0
	JSR	AreYouSure
	BNE	Return2
	JSR	StorePtrCol
	JSR	SetWaitPtrCol
	LEA	ResamplingText(PC),A0
	BSR	ShowStatusText
	LEA	SamplePtrs,A0
	MOVE.W	InsNum(PC),D1
	BEQ	NotSampleNull
	LSL.W	#2,D1
	LEA	(A0,D1.W),A0
	MOVE.L	(A0),D0
	BEQ	ERC2
	MOVE.L	D0,A2
	MOVE.L	124(A0),D6
	CMP.L	#2,D6
	BLS	ERC2

	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	InsNum(PC),D1
	MULU	#30,D1
	MOVEQ	#0,D0
	MOVE.B	12+2(A0,D1.W),D0 ; finetune
	MULU	#37*2,D0
	LEA	PeriodTable(PC),A0
	ADD.L	D0,A0
	MOVEQ	#0,D1
	MOVE.W	ResampleNote(PC),D1
	LSL.W	#1,D1
	MOVE.W	(A0,D1.W),D1
	BEQ	ERC2

	LEA	PeriodTable,A0
	MOVEQ	#0,D5
	MOVE.W	TuneNote,D5
	LSL.W	#1,D5
	MOVE.W	(A0,D5.W),D5
	BEQ	ERC2

; D1=resamperiod, D5=tuneperiod

	MOVE.L	D5,D0 ; tune
	LSL.L	#8,D0
	LSL.L	#4,D0 ; * 4096
	DIVU	D1,D0 ; / resample
	MOVEQ	#0,D4
	MOVE.W	D0,D4
	LSL.L	#4,D4 ; hi-word=add value, lo-word=fraction

	MOVE.W	D6,D7
	LSR.W	#1,D7
	MULU	D1,D7
	DIVU	D5,D7
	AND.L	#$FFFF,D7
	CMP.W	#2,D7
	BLO	ERC2
	CMP.W	#$7FFF,D7
	BLO	resamlo
	MOVE.W	#$7FFF,D7
resamlo	ADD.L	D7,D7
	MOVE.L	D7,D0
	MOVE.L	#MEMF_CHIP!MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	TST.L	D0
	BEQ	SamMemError
	MOVE.L	D0,A1
	MOVE.L	D0,A3
	JSR	TurnOffVoices
	MOVE.L	A2,A0
; D4=resample value, D6=old length, D7=new length
; A0/A2=old sample, A1/A3=new sample

	MOVE.W	#32767,D1
	MOVEQ	#0,D3
	MOVEQ	#0,D5
resampleloop
	MOVE.B	1(A0),D0
	EXT.W	D0
	MULS	D1,D0
	MOVE.B	(A0),D2
	EXT.W	D2
	MULS	D3,D2
	ADD.L	D2,D0
	ASR.L	#8,D0
	ASR.L	#7,D0
	MOVE.B	D0,(A1)+

	ADD.L	D4,D5
	SWAP	D5
	MOVE.L	A2,A0
	MOVEQ	#0,D0
	MOVE.W	D5,D0
	ADD.L	D0,A0
	SWAP	D5
	MOVE.W	D5,D1
	LSR.W	#1,D1
	MOVE.W	#32767,D3
	SUB.W	D1,D3

	MOVE.L	A1,A4
	SUB.L	A3,A4
	CMP.L	D7,A4
	BHS.S	resamskip
	MOVE.L	A0,A4
	SUB.L	A2,A4
	CMP.L	D6,A4
	BLO.S	resampleloop

resamskip
	MOVE.L	A2,A1
	MOVE.L	D6,D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	MOVE.W	InsNum(PC),D1
	LSL.W	#2,D1
	LEA	SamplePtrs,A0
	LEA	(A0,D1.W),A0
	MOVE.L	A3,(A0)
	MOVE.L	D7,124(A0)

	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	InsNum(PC),D1
	MULU	#30,D1
	LEA	12(A0,D1.W),A0
	LSR.L	#1,D7
	MOVE.W	D7,(A0)
	CLR.B	2(A0)
	CLR.W	4(A0)
	MOVE.W	#1,6(A0)
	JSR	RestorePtrCol
	BSR	ClearSamStarts
	BSR	ShowSampleInfo
	BSR	ShowAllRight
	BRA	RedrawSample

ERC2	JMP	ErrorRestoreCol

SamCut	CLR.B	RawKeyCode
	MOVE.L	MarkStartOfs(PC),D0
	BMI	NoRangeError
	CMP.L	MarkEndOfs(PC),D0
	BEQ	LargerRangeError
	TST.B	CutToBufFlag
	BEQ.S	samcut2
	BSR	SamCopy
samcut2	BSR	TurnOffVoices
	LEA	SamplePtrs(PC),A0
	MOVE.W	InsNum(PC),D1
	BEQ	NotSampleNull
	LSL.W	#2,D1
	LEA	(A0,D1.W),A0
	MOVE.L	(A0),D0
	BEQ	EmptySampleError
	MOVE.L	124(A0),D2
	CMP.L	#2,D2
	BLO	EmptySampleError

	MOVE.L	D0,A1
	MOVE.L	D0,A2		; sample start
	MOVE.L	D0,A3
	MOVE.L	D0,A4
	MOVE.L	D0,A5
	ADD.L	MarkStartOfs(PC),A3	; mark start
	MOVE.L	MarkEndOfs(PC),D0
	SUB.L	MarkStartOfs(PC),D0
	CMP.L	D2,D0
	BHS	Destroy
	MOVE.L	MarkEndOfs(PC),D0
	CMP.L	D2,D0
	BLO.S	samsome
	MOVE.L	D2,D0
	SUBQ.L	#1,D0
samsome	ADD.L	D0,A4	; mark end
	ADD.L	D2,A5		; sample end

	MOVE.L	A3,D0
	SUB.L	A2,D0
	ADD.L	A5,D0
	SUB.L	A4,D0
	BNE	sacoklen
	MOVEQ	#0,D3
	MOVEQ	#0,D4
	BRA	sacfree

sacoklen
	MOVE.L	D0,D3
	MOVEQ	#MEMF_CHIP,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,D4
	BEQ	OutOfMemErr ; No memory

	MOVE.L	D0,A0
	MOVE.L	A2,A1
	MOVE.L	A3,D0
	SUB.L	A2,D0
	BRA.S	sacskp1
saclop1	MOVE.B	(A2)+,(A0)+
sacskp1	DBRA	D0,saclop1

	MOVE.L	A5,D0
	SUB.L	A4,D0
	BRA.S	sacskp2
smclop2	MOVE.B	(A4)+,(A0)+
sacskp2	DBRA	D0,smclop2

sacfree	MOVE.L	D2,D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)

	MOVE.W	InsNum(PC),D1
	LSL.W	#2,D1
	LEA	SamplePtrs(PC),A0
	LEA	(A0,D1.W),A0
	MOVE.L	D4,(A0)
	MOVE.L	D3,124(A0)

	MOVE.L	D4,SamStart
	MOVE.L	D3,SamLength
	MOVE.L	SamOffset(PC),D4
	ADD.L	SamDisplay(PC),D4
	CMP.L	D3,D4
	BLO	samcuto		; display ok
	MOVE.L	SamDisplay(PC),D4
	CMP.L	D3,D4
	BLO	samnils		; if display < length, move offset
samsall	CLR.L	SamOffset	; else show all
	MOVE.L	D3,SamDisplay
	BRA	samcuto
samnils	MOVE.L	D3,D4
	SUB.L	SamDisplay(PC),D4
	BMI.S	samsall		; if offset < 0, show all
	MOVE.L	D4,SamOffset
samcuto	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	InsNum(PC),D1
	MULU	#30,D1
	LEA	12(A0,D1.W),A0

	LSR.L	#1,D3
	MOVE.W	D3,(A0)
	MOVE.W	4(A0),D0
	CMP.W	D3,D0
	BLS.S	samcuxx	
	MOVEQ	#1,D0
	MOVE.L	D0,4(A0)
	BRA.S	samcuex
samcuxx	ADD.W	6(A0),D0
	CMP.W	D3,D0
	BLS.S	samcuex
	SUB.W	4(A0),D3
	MOVE.W	D3,6(A0)

samcuex	MOVE.L	MarkStartOfs(PC),MarkEndOfs
	BSR	ClearSamStarts
	BSR	ValidateLoops
	BSR	ShowSampleInfo
	BRA	DisplaySample


SamCopy	CLR.B	RawKeyCode
	MOVE.L	MarkStartOfs(PC),D0
	BMI	NoRangeError
	CMP.L	MarkEndOfs(PC),D0
	BEQ	LargerRangeError
	LEA	SamplePtrs(PC),A0
	MOVE.W	InsNum(PC),D1
	BEQ	NotSampleNull
	LSL.W	#2,D1
	LEA	(A0,D1.W),A0
	MOVE.L	(A0),D0
	BEQ	EmptySampleError
	MOVE.L	124(A0),D2
	BEQ	EmptySampleError
	MOVE.L	D0,A3
	MOVE.L	D0,A4
	ADD.L	MarkStartOfs(PC),A3	; mark start
	MOVE.L	MarkEndOfs(PC),D0	; mark end
	CMP.L	D2,D0
	BLO.S	csamsom
	MOVE.L	D2,D0
	SUBQ.L	#1,D0
csamsom	ADD.L	D0,A4
	BSR.S	FreeCopyBuf
	MOVE.L	A4,D0
	SUB.L	A3,D0
	ADDQ.L	#1,D0
	MOVE.L	D0,CopyBufSize
	MOVEQ	#MEMF_CHIP,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	MOVE.L	D0,CopyBufPtr
	BEQ	OutOfMemErr ; No memory
	MOVE.L	D0,A5
csamlop	MOVE.B	(A3)+,(A5)+
	CMP.L	A4,A3
	BLS.S	csamlop
	BSR	InvertRange
	BSR	InvertRange
	JMP	WaitForButtonUp

FreeCopyBuf
	MOVE.L	CopyBufPtr(PC),D0
	BEQ	Return2
	MOVE.L	D0,A1
	MOVE.L	CopyBufSize(PC),D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
	CLR.L	CopyBufPtr
	RTS

CopyBufPtr	dc.l	0
CopyBufSize	dc.l	0

SamPaste
	CLR.B	RawKeyCode
	MOVE.L	MarkStartOfs(PC),D0
	BMI	SetCursorError
	LEA	SamplePtrs(PC),A0
	MOVE.W	InsNum(PC),D1
	BEQ	NotSampleNull
	LSL.W	#2,D1
	LEA	(A0,D1.W),A0
	MOVEQ	#0,D2
	MOVE.L	(A0),D0
	BEQ.S	sapanul
	MOVE.L	124(A0),D2
sapanul	MOVE.L	D0,A2
	MOVE.L	CopyBufPtr(PC),D3
	BEQ	BufIsEmptyError
	MOVE.L	D3,A3
	MOVE.L	CopyBufSize(PC),D3
	BEQ	BufIsEmptyError

	MOVE.L	D3,D4 ; copysize
	ADD.L	D2,D4 ; + origsize
	CMP.L	#$FFFE,D4
	BLO.S	sapaok
	MOVE.L	#$FFFE,D4
sapaok	MOVE.L	D4,D0
	MOVE.L	#MEMF_CHIP!MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	TST.L	D0
	BEQ	OutOfMemErr
	MOVE.L	D0,A4
	MOVEQ	#0,D0
	TST.L	D2
	BEQ.S	sapask1
	MOVE.L	MarkStartOfs(PC),D0
sapask1	MOVE.L	D0,MarkStartOfs
	MOVE.L	A2,A1
	MOVE.L	D2,D1
	MOVE.L	A4,A5
	MOVE.L	A4,A0
	ADD.L	D4,A0
; D0    = paste position
; A0    = end of new sample
; A1/D1 = copy of A2/D2
; A2/D2 = original sample
; A3/D3 = copy buffer
; A4/D4 = new sample
; A5    = copy of A4
	BRA.S	sapask2
sapalp1	MOVE.B	(A2)+,(A4)+ ; copy first part
	CMP.L	A0,A4
	BHS.S	sapaski
	SUBQ.L	#1,D2
sapask2	DBRA	D0,sapalp1
	BRA.S	sapask3
sapalp2	MOVE.B	(A3)+,(A4)+ ; copy from buffer
	CMP.L	A0,A4
	BHS.S	sapaski
sapask3	DBRA	D3,sapalp2
	TST.L	D2
	BEQ.S	sapaski
	BMI.S	sapaski
sapalp3	MOVE.B	(A2)+,(A4)+ ; copy last part
	CMP.L	A0,A4
	BHS.S	sapaski
	SUBQ.L	#1,D2
	BNE.S	sapalp3

sapaski	MOVE.L	D1,D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)

	MOVE.W	InsNum(PC),D1
	LSL.W	#2,D1
	LEA	SamplePtrs(PC),A0
	LEA	(A0,D1.W),A0
	MOVE.L	A5,(A0)
	MOVE.L	D4,124(A0)

	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	InsNum(PC),D1
	MULU	#30,D1
	LEA	12(A0,D1.W),A0
	LSR.L	#1,D4
	MOVE.W	D4,(A0)

	MOVE.L	MarkStartOfs(PC),MarkEndOfs
	BSR	OffsetToMark
	BSR	ClearSamStarts
	BSR	ShowSampleInfo
	BRA	RedrawSample

RampVolume
	MOVE.L	TextBplPtr(PC),A0
	LEA	6209(A0),A0
	MOVEQ	#32,D3
ravlap2	MOVEQ	#16,D2
ravlap1	CLR.B	(A0)+
	DBRA	D2,ravlap1
	LEA	23(A0),A0
	DBRA	D3,ravlap2
	LEA	VolBoxPos,A0
	LEA	VolBoxData,A1
	MOVEQ	#1,D4
ravlop3	MOVEQ	#32,D3
ravlop2	MOVEQ	#16,D2
ravlop1	MOVE.B	(A1)+,(A0)+
	DBRA	D2,ravlop1
	ADDQ.L	#1,A1
	LEA	23(A0),A0
	DBRA	D3,ravlop2
	LEA	8920(A0),A0
	DBRA	D4,ravlop3
	BSR	ShowVolSliders
	JSR	WaitForButtonUp
ravloop	BTST	#2,$DFF016
	BEQ.S	ExitVolBox
	JSR	DoKeyBuffer
	MOVE.B	RawKeyCode(PC),D2
	CMP.B	#68,D2
	BEQ.S	ExitVolBox
	BTST	#6,$BFE001
	BNE.S	ravloop
	MOVEM.W	MouseX,D0-D1
	CMP.W	#72,D0
	BLO.S	ravloop
	CMP.W	#72+136,D0
	BHS.S	ravloop
	CMP.W	#155,D1
	BLO.S	ravloop
	CMP.W	#166,D1
	BLO.S	Vol1Slider
	CMP.W	#177,D1
	BLO	Vol2Slider
	CMP.W	#188,D1
	BLO	VolGadgs
	BRA.S	ravloop

ExitVolBox
	LEA	VolBoxPos,A0
	MOVEQ	#1,D2
revlap3	MOVEQ	#32,D1
revlap2	MOVEQ	#16,D0
revlap1	CLR.B	(A0)+
	DBRA	D0,revlap1
	LEA	23(A0),A0
	DBRA	D1,revlap2
	LEA	8920(A0),A0
	DBRA	D2,revlap3
	BRA	DisplaySample

Vol1Slider
	CMP.W	#167,D0
	BHI.S	v1skip
	LEA	Vol1(PC),A4
	MOVEQ	#0,D7
v1loop1	BTST	#6,$BFE001
	BNE	ravloop
	MOVE.W	MouseX(PC),D0
	CMP.W	D7,D0
	BEQ.S	v1loop1
	MOVE.W	D0,D7
	SUB.W	#107,D0
	BPL.S	v1skp2
	MOVEQ	#0,D0
v1skp2	CMP.W	#60,D0
	BLS.S	v1skp3
	MOVEQ	#60,D0
v1skp3	MULU	#200,D0
	DIVU	#60,D0
	MOVE.W	D0,(A4)
shvosl	BSR	ShowVolSliders
	BRA.S	v1loop1

v1skip	MOVE.W	#180,LineCurX
	MOVE.W	#164,LineCurY
	MOVE.W	#6342,TextOffset
	BSR	GetDec3Dig
	TST.W	AbortDecFlag
	BNE	shvosl
	CMP.W	#200,D0
	BLS.S	v1sk2
	MOVE.W	#200,D0
v1sk2	MOVE.W	D0,Vol1
	BRA.S	shvosl

Vol2Slider
	CMP.W	#167,D0
	BHI.S	v2skip
	LEA	Vol2(PC),A4
	MOVEQ	#0,D7
	BRA	v1loop1
v2skip	MOVE.W	#180,LineCurX
	MOVE.W	#175,LineCurY
	MOVE.W	#6782,TextOffset
	BSR	GetDec3Dig
	TST.W	AbortDecFlag
	BNE	shvosl
	CMP.W	#200,D0
	BLS.S	v2sk2
	MOVE.W	#200,D0
v2sk2	MOVE.W	D0,Vol2
	BRA	shvosl


VolGadgs
	CMP.W	#100,D0
	BLO	DoRampVol
	CMP.W	#144,D0
	BLO	Normalize
	CMP.W	#154,D0
	BLO.S	SetRampDown
	CMP.W	#164,D0
	BLO.S	SetRampUp
	CMP.W	#174,D0
	BLO.S	SetRampUnity
	BRA	ExitVolBox

SetRampDown
	MOVE.W	#100,Vol1
	CLR.W	Vol2
	BRA.S	sru2
SetRampUp
	CLR.W	Vol1
	MOVE.W	#100,Vol2
	BRA.S	sru2
SetRampUnity
	MOVE.W	#100,Vol1
	MOVE.W	#100,Vol2
sru2	BSR.S	ShowVolSliders
	JSR	WaitForButtonUp
	BRA	ravloop

ShowVolSliders
	MOVE.L	TextBplPtr(PC),A0
	LEA	6209(A0),A0
	MOVEQ	#21,D3
ravlip2	MOVEQ	#12,D2
ravlip1	CLR.B	(A0)+
	DBRA	D2,ravlip1
	LEA	27(A0),A0
	DBRA	D3,ravlip2
	MOVEQ	#0,D4
	MOVE.W	Vol1(PC),D4
	MOVEQ	#20,D5
	BSR	OneSlider
	MOVEQ	#0,D4
	MOVE.W	Vol2(PC),D4
	MOVEQ	#31,D5
	BSR	OneSlider
	MOVE.W	Vol1(PC),WordNumber
	MOVE.W	#6342,TextOffset
	BSR	Print3DecDigits
	MOVE.W	Vol2(PC),WordNumber
	MOVE.W	#6782,TextOffset
	BRA	Print3DecDigits

OneSlider
	MOVE.W	D4,D6
	ADD.W	D4,D4
	ADD.W	D6,D4
	DIVU	#10,D4
	ADD.W	#105,D4
	MOVEQ	#2,D6
oneslop	MOVE.W	D4,D0
	MOVE.W	D4,D2
	ADDQ.W	#5,D2
	MOVE.W	D5,D1
	MOVE.W	D5,D3
	BSR	DrawLine
	ADDQ.W	#1,D5
	DBRA	D6,oneslop
	RTS

DoRampVol
	CLR.B	RawKeyCode
	LEA	SamplePtrs(PC),A0
	MOVE.W	InsNum(PC),D1
	BEQ	nozerr1
	LSL.W	#2,D1
	LEA	(A0,D1.W),A0
	MOVEQ	#0,D2
	MOVE.L	(A0),D0
	BEQ	nozerr2
	MOVE.L	D0,A2
	MOVE.L	124(A0),D2
	BEQ	nozerr2
	MOVE.L	MarkStartOfs(PC),D0
	BMI	drvskip
	MOVE.L	MarkEndOfs(PC),D1
	SUB.L	D0,D1
	BEQ.S	drvskip
	ADD.L	D0,A2
	MOVE.L	D1,D2
	ADDQ.L	#1,D2
drvskip	JSR	StorePtrCol
	JSR	SetWaitPtrCol
	MOVE.L	D2,D5
	MOVEQ	#0,D3
drvloop	MOVE.W	D3,D1
	MULU	Vol2(PC),D1
	DIVU	D2,D1
	MOVE.W	D2,D4
	SUB.W	D3,D4
	MULU	Vol1(PC),D4
	DIVU	D2,D4
	ADD.W	D4,D1
	ADDQ.W	#1,D3
	MOVE.B	(A2),D0
	EXT.W	D0
	MULS	D1,D0
	DIVS	#100,D0
	CMP.W	#127,D0
	BLT.S	drvskp2
	MOVEQ	#127,D0
drvskp2	CMP.W	#-128,D0
	BGT.S	drvskp3
	MOVEQ	#-128,D0
drvskp3	MOVE.B	D0,(A2)+
	SUBQ.L	#1,D5
	BNE	drvloop
	JSR	RestorePtrCol
	BRA	ExitVolBox

nozerr1	BSR	NotSampleNull
	BRA	ravloop
nozerr2	BSR	EmptySampleError
	BRA	ravloop

Normalize
	CLR.B	RawKeyCode
	LEA	SamplePtrs(PC),A0
	MOVE.W	InsNum(PC),D1
	BEQ	nozerr1
	LSL.W	#2,D1
	LEA	(A0,D1.W),A0
	MOVEQ	#0,D2
	MOVE.L	(A0),D0
	BEQ	nozerr2
	MOVE.L	D0,A2
	MOVE.L	124(A0),D2
	BEQ	nozerr2
	MOVE.L	MarkStartOfs(PC),D0
	BMI	nozskip
	MOVE.L	MarkEndOfs(PC),D1
	SUB.L	D0,D1
	BEQ.S	nozskip
	ADD.L	D0,A2
	MOVE.L	D1,D2
	ADDQ.L	#1,D2
nozskip	JSR	StorePtrCol
	JSR	SetWaitPtrCol
	MOVEQ	#0,D0
nozloop	MOVE.B	(A2)+,D1
	EXT.W	D1
	BPL.S	nozskp2
	NEG.W	D1
nozskp2	CMP.W	D0,D1
	BLO.S	nozskp3
	MOVE.W	D1,D0
nozskp3	SUBQ.L	#1,D2
	BNE	nozloop
	JSR	RestorePtrCol
	TST.W	D0
	BEQ	SetRampUnity
	CMP.W	#127,D0
	BHI	SetRampUnity
	CMP.W	#64,D0
	BLO.S	nozmax
	MOVE.L	#12700,D1
	DIVU	D0,D1
	MOVE.W	D1,Vol1
	MOVE.W	D1,Vol2
	BRA	sru2
nozmax	MOVE.W	#200,Vol1
	MOVE.W	#200,Vol2
	BRA	sru2

Vol1	dc.w	100
Vol2	dc.w	100

TuningTone
	JSR	WaitForButtonUp
	CLR.B	RawKeyCode
	TST.W	TToneFlag
	BNE	TToneOff
	MOVE.W	#1,TToneFlag
	MOVE.W	PattCurPos,D2
	DIVU	#6,D2
	ADDQ.W	#1,D2
	AND.W	#3,D2
	MOVEQ	#1,D0
	LSL.W	D2,D0
	MOVE.W	D0,TToneBit
	LEA	$DFF0A0,A0
	LSL.W	#4,D2
	LEA	(A0,D2.W),A0
	LEA	PeriodTable(PC),A1
	MOVE.W	TuneNote,D1
	LSL.W	#1,D1
	MOVE.W	(A1,D1.W),D1
	LEA	TToneData,A2
	MOVE.W	D0,$DFF096 ; DMACON
	MOVE.L	A2,(A0)
	MOVE.W	#16,4(A0) ; 32 bytes
	MOVE.W	D1,6(A0)
	MOVE.W	TToneVol,8(A0)
	MOVE.W	DMAWait,D2
ttowait	DBRA	D2,ttowait
	BSET	#15,D0
	MOVE.W	D0,$DFF096
	RTS

TToneOff
	CLR.W	TToneFlag
	MOVE.W	TToneBit(PC),$DFF096
	RTS

TToneFlag	dc.w 0
TToneBit	dc.w 0

SamplePressed
	CMP.W	#144,D1
	BHS.S	spruskp
	MOVE.W	LoopStartPos(PC),D2
	BEQ.S	sprusk5
	SUBQ.W	#3,D2
	CMP.W	D2,D0
	BLT.S	sprusk5
	ADDQ.W	#4,D2
	CMP.W	D2,D0
	BLO	LoopStartDrag
sprusk5	MOVE.W	LoopEndPos(PC),D2
	BEQ.S	spruskp
	SUBQ.W	#3,D2
	CMP.W	D2,D0
	BLT.S	spruskp
	ADDQ.W	#4,D2
	CMP.W	D2,D0
	BLO	LoopEndDrag
spruskp	CMP.W	#3,D0
	BLO	Return2
	CMP.W	#317,D0
	BHS	Return2
	MOVE.W	D0,LastSamPos
	BSR	InvertRange
	MOVE.W	LastSamPos(PC),D0
	MOVE.W	D0,MarkStart
	MOVE.W	D0,MarkEnd
	BSR	InvertRange
	BSR	MarkToOffset
	MOVE.L	MarkEndOfs(PC),SamplePos
	BSR	ShowPos
sprulop	BTST	#6,$BFE001
	BNE	spruend
	MOVE.W	MouseX(PC),D0
	CMP.W	#3,D0
	BLO	sprusk3
	CMP.W	#317,D0
	BHS	sprusk2
	BRA	sprusk4
sprusk2	MOVE.W	#316,D0
	BRA	sprusk4
sprusk3	MOVEQ	#3,D0
sprusk4	CMP.W	LastSamPos(PC),D0
	BEQ	sprulop
	MOVE.W	D0,LastSamPos
	BSR	InvertRange
	MOVE.W	LastSamPos(PC),MarkEnd
	BSR	InvertRange
	BSR	MarkToOffset
	MOVE.L	MarkEndOfs(PC),SamplePos
	BSR	ShowPos
	BRA	sprulop
spruend	MOVE.W	MarkStart(PC),D0
	MOVE.W	MarkEnd(PC),D1
	CMP.W	D0,D1
	BHS	MarkToOffset
	MOVE.W	D0,MarkEnd
	MOVE.W	D1,MarkStart
	BRA	MarkToOffset

InvertRange
	MOVE.W	MarkStart(PC),D4
	BEQ	Return2
	MOVE.W	MarkEnd(PC),D5
	MOVE.L	GfxBase(PC),A6
	JSR	LVOOwnBlitter(A6)
	JSR	LVOWaitBlit(A6)
invran3	MOVE.W	D4,D0
	MOVEQ	#0,D1
	MOVE.W	D4,D2
	MOVEQ	#64,D3
	BSR	DrawInvertLine
	CMP.W	D5,D4
	BEQ	invranx
	BHI	invran2
	ADDQ.W	#1,D4
	BRA	invran3
invran2	SUBQ.W	#1,D4
	BRA	invran3

invranx	MOVE.L	GfxBase(PC),A6
	JSR	LVODisownBlitter(A6)
	RTS

LoopStartPos	dc.w 0
LoopEndPos	dc.w 0

LoopStartDrag
	CLR.W	DragType
LopDrgLop
	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	InsNum(PC),D0
	BEQ	Return2
	MULU	#30,D0
	LEA	12(A0,D0.W),A0
	MOVE.W	MouseX(PC),D0
lsdlop1	BTST	#6,$BFE001
	BNE	lsdexit
	MOVE.W	MouseX(PC),D1
	CMP.W	D0,D1
	BEQ.S	lsdlop1
	SUB.W	DragType,D1
	BPL.S	lsdmsk1
	MOVEQ	#0,D1
lsdmsk1	CMP.W	#314,D1
	BLO.S	lsdmsk2
	MOVE.W	#314,D1
lsdmsk2	MOVE.L	SamDisplay(PC),D0
	MULU	D0,D1
	DIVU	#314,D1
	MOVE.L	SamOffset(PC),D0
	ADD.W	D1,D0    ; new repeat
	BCLR	#0,D0
	MOVEQ	#0,D1
	MOVE.W	4(A0),D1 ; old repeat
	ADD.W	D1,D1
	TST.W	DragType
	BNE.S	drgrepl
	MOVE.W	D1,D3
	SUB.W	D1,D0    ; offset
	ADD.W	D0,D1
	MOVE.W	6(A0),D2
	ADD.W	D2,D2
	ADD.W	D2,D3
	SUBQ.W	#2,D3
	SUB.W	D0,D2
	CMP.W	D3,D1
	BLS.S	lsdok
	MOVE.W	D3,D1
	MOVEQ	#2,D2
lsdok	LSR.W	#1,D1
	MOVE.W	D1,4(A0)
	LSR.W	#1,D2
	MOVE.W	D2,6(A0)
	BSR.S	lsdexit
	BRA	LopDrgLop
lsdexit	BSR	ShowSampleInfo
	BSR	UpdateRepeats
	BRA	SetLoopSprites2

LoopEndDrag
	MOVE.W	#3,DragType
	BRA	LopDrgLop

drgrepl	MOVE.L	D0,D2 ; repend
	SUB.L	D1,D2 ; subtract repstart
	CMP.L	#2,D2
	BGT.S	ledskp1
	MOVEQ	#2,D2
	BRA.S	lsdok

ledskp1	MOVE.W	D1,D0 ; repeat
	ADD.W	D2,D0 ; + replen
	MOVE.W	(A0),D3
	ADD.W	D3,D3
	CMP.W	D3,D0
	BLS.S	lsdok
	SUB.W	D1,D3
	MOVE.W	D3,D2
	BRA.S	lsdok

DragType dc.w 0

SamDragBar
	CMP.W	#4,D0
	BLO	Return2
	CMP.W	#316,D0
	BHS	Return2
	CMP.W	DragStart(PC),D0
	BLO.S	draglo
	CMP.W	DragEnd(PC),D0
	BHI.S	draghi
	MOVE.W	MouseX(PC),D0
	MOVE.W	D0,D1
	SUB.W	DragStart(PC),D1
	MOVE.W	D1,SaveMX
sdrlop1	BTST	#6,$BFE001
	BNE	Return2
	MOVE.W	MouseX(PC),D1
	CMP.W	D0,D1
	BEQ.S	sdrlop1
	SUB.W	SaveMX(PC),D0
	SUBQ.W	#4,D0
	BPL.S	sdrskp1
	MOVEQ	#0,D0
sdrskp1	MOVE.L	SamLength(PC),D1
	BEQ	Return2
	MULU	D1,D0
	DIVU	#311,D0
	AND.L	#$FFFF,D0
	BSR	dragchk
	MOVE.W	MouseX(PC),D0
	BRA.S	sdrlop1

draglo	MOVE.L	SamOffset(PC),D0
	SUB.L	SamDisplay(PC),D0
	BPL.S	draglo2
	MOVEQ	#0,D0
draglo2	CMP.L	SamOffset(PC),D0
	BEQ	Return2
	MOVE.L	D0,SamOffset
	BRA	DisplaySample

draghi	MOVE.L	SamOffset(PC),D0
	ADD.L	SamDisplay(PC),D0
dragchk	MOVE.L	D0,D1
	ADD.L	SamDisplay(PC),D1
	CMP.L	SamLength(PC),D1
	BLS.S	draglo2
	MOVE.L	SamLength(PC),D0
	SUB.L	SamDisplay(PC),D0
	BRA.S	draglo2

SaveMX	dc.w	0

;----

MarkToOffset
	MOVE.L	SamDisplay(PC),D0
	MOVE.W	MarkStart(PC),D1
	BEQ	Return2
	SUBQ.W	#3,D1
	MULU	D0,D1
	DIVU	#314,D1
	AND.L	#$FFFF,D1
	ADD.L	SamOffset(PC),D1
	MOVE.L	D1,MarkStartOfs
	MOVE.W	MarkEnd(PC),D1
	CMP.W	#316,D1
	BLO.S	mtosome
	MOVE.L	SamOffset(PC),D1
	ADD.L	D0,D1
	BRA.S	mtoexit
mtosome	SUBQ.W	#3,D1
	MULU	D0,D1
	DIVU	#314,D1
	AND.L	#$FFFF,D1
	ADD.L	SamOffset(PC),D1
mtoexit	MOVE.L	D1,MarkEndOfs
	RTS

OffsetToMark
	MOVE.L	MarkStartOfs(PC),D0
	BMI.S	otmout
	MOVE.W	#3,MarkStart
	SUB.L	SamOffset(PC),D0
	BMI.S	otmskip	; set to start if before offset
	MULU	#314,D0
	MOVE.L	SamDisplay(PC),D1
	BEQ.S	otmskip
	DIVU	D1,D0
	CMP.W	#314,D0
	BHI.S	otmout	; if start after display
	ADD.W	D0,MarkStart
otmskip	MOVE.W	#3,MarkEnd
	MOVE.L	MarkEndOfs(PC),D0
	SUB.L	SamOffset(PC),D0
	BMI.S	otmout	; if end before offset
	MULU	#314,D0
	MOVE.L	SamDisplay(PC),D1
	BEQ	Return2
	DIVU	D1,D0
	CMP.W	#313,D0
	BLS.S	otmok
	MOVE.W	#313,D0	; set to end if after display
otmok	ADD.W	D0,MarkEnd
	RTS

otmout	CLR.W	MarkStart
	RTS

SamScrEnable	dc.w	0
SamMemPtr	dc.l	0
SamMemSize	dc.l	0
LastSamPos	dc.w	0
MarkStart	dc.w	0
MarkEnd		dc.w	0
MarkStartOfs	dc.l	0
MarkEndOfs	dc.l	0

;---- Sample graphing stuff ----

DisplaySample
	TST.W	SamScrEnable
	BEQ	Return2
	BSR	rdsdoit
	TST.L	MarkStartOfs
	BMI	Return2
	BSR	OffsetToMark
	BRA	InvertRange

RedrawSample
	TST.W	SamScrEnable
	BEQ	Return2
	MOVEQ	#-1,D0
	MOVE.L	D0,MarkStartOfs
	CLR.W	MarkStart
	MOVE.W	InsNum(PC),D0
	BEQ	rdsblnk
	LEA	SampleStarts(PC),A1
	SUBQ.W	#1,D0
	LSL.W	#2,D0
	MOVE.L	(A1,D0.W),SamStart
	BEQ	rdsblnk
	MOVE.L	124(A1,D0.W),D1
	BEQ	rdsblnk
	MOVE.L	D1,SamLength
	CLR.L	SamOffset
	MOVE.L	D1,SamDisplay
	BRA	rdsdoit
rdsblnk	LEA	BlankSample,A0
	MOVE.L	A0,SamStart
	MOVE.L	#314,SamLength
	CLR.L	SamOffset
	MOVE.L	#314,SamDisplay
rdsdoit	MOVE.L	GfxBase(PC),A6
	JSR	LVOOwnBlitter(A6)
	JSR	LVOWaitBlit(A6)
	BSR	ClearSamArea
	MOVE.L	SamStart(PC),A0
	MOVEQ	#0,D4
	MOVE.L	SamOffset(PC),D5
	MOVE.L	SamDisplay(PC),D6
	MOVE.L	D5,D7
	MOVE.L	A0,D0
	ADD.L	D5,D0
	MOVE.L	D0,SamDrawStart
	ADD.L	D6,D0
	MOVE.L	D0,SamDrawEnd
rdsloop	MOVE.W	D4,D0
	ADDQ.W	#3,D0
	MOVEQ	#127,D1
	SUB.B	(A0,D7.L),D1
	LSR.W	#2,D1
	TST.W	D4
	BNE	rdsdraw
	BSR	MoveTo
	BRA	rdsupdt
rdsdraw	BSR	DrawTo
rdsupdt	ADDQ.W	#1,D4
	MOVE.L	D4,D7
	MULU	D6,D7
	DIVU	#314,D7
	AND.L	#$FFFF,D7
	ADD.L	D5,D7
	CMP.W	#314,D4
	BLO	rdsloop

	MOVE.L	GfxBase(PC),A6
	JSR	LVODisownBlitter(A6)
	BSR.S	SetDragBar
	MOVE.L	SamDisplay(PC),D0
	LEA	BlankSample,A0
	CMP.L	SamStart,A0
	BNE.S	rdsslwo
	MOVEQ	#0,D0
rdsslwo	MOVE.W	#215*40+33,TextOffset
	BSR	Print6DecDigits
	BRA	SetLoopSprites

SamStart	dc.l	0
SamLength	dc.l	0
SamOffset	dc.l	0
SamDisplay	dc.l	0
SavSamIns	dc.w	0

SetDragBar
	MOVE.W	#4*10-1,D0
	MOVE.L	TextBplPtr,A0
	LEA	$15B8+2720(A0),A0
	MOVEQ	#0,D1
sdblop2	MOVE.L	D1,(A0)+
	DBRA	D0,sdblop2
	MOVE.L	SamLength(PC),D0
	BEQ	Return2
	MOVE.L	SamOffset(PC),D4
	MOVE.L	SamDisplay(PC),D5
	CMP.L	D0,D5
	BEQ	Return2
	ADD.L	D4,D5
	MULU	#311,D4
	DIVU	D0,D4
	ADDQ.W	#4,D4
	MOVE.W	D4,DragStart
	MULU	#311,D5
	DIVU	D0,D5
	ADDQ.W	#5,D5
	MOVE.W	D5,DragEnd
	MOVE.L	GfxBase(PC),A6
	JSR	LVOOwnBlitter(A6)
	JSR	LVOWaitBlit(A6)
	MOVEQ	#68,D6
	MOVEQ	#3,D7
sdbloop	MOVE.W	D4,D0
	MOVE.L	D6,D1
	MOVE.W	D5,D2
	MOVE.L	D6,D3
	BSR.S	DrawLine
	ADDQ.L	#1,D6
	DBRA	D7,sdbloop
	MOVE.L	GfxBase(PC),A6
	JSR	LVODisownBlitter(A6)
	RTS

DragStart	dc.w	0
DragEnd		dc.w	0

;---- Line Routine ----

DrawInvertLine
	MOVE.B	#$48,MinTerm
	BSR.S	DrawLine
	MOVE.B	#$C8,Minterm
	RTS

MoveTo	MOVE.W	D0,PenX
	MOVE.W	D1,PenY
	RTS

DrawTo	MOVE.W	PenX(PC),D2
	MOVE.W	PenY(PC),D3
	MOVE.W	D0,PenX
	MOVE.W	D1,PenY
DrawLine
	MOVEM.L	D4-D7,-(SP)
	MOVEQ	#0,D4
	SUB.W	D1,D3
	BGE.S	dypos
	NEG.W	D3
	BRA.S	dyneg
dypos	BSET	#0,D4
dyneg	SUB.W	D0,D2
	BGE.S	dxpos
	NEG.W	D2
	BRA.S	dxneg
dxpos	BSET	#1,D4
dxneg	MOVE.W	D2,D5
	SUB.W	D3,D5
	BGE.S	dxdypos
	EXG	D2,D3
	BRA.S	dxdyneg
dxdypos	BSET	#2,D4
dxdyneg	MOVEQ	#0,D5
	ROR.W	#4,D0
	OR.W	#$0B00,D0
	MOVE.B	D0,D5
	MOVE.B	MinTerm(PC),D0
	LSL.W	#1,D5
	MULS	#ScrWidth,D1
	ADD.W	D5,D1
	ADD.L	LineScreen(PC),D1
blitrdy	BTST	#14,$DFF002 ; DMACONR
	BNE.S	blitrdy
	MOVE.W	$DFF01C,-(SP)
	BSET	#7,(SP)
	MOVE.W	#$7FFF,$DFF09A
	MOVE.B	Octants(PC,D4),D4
	ADD.L	D3,D3
	MOVE.W	D3,$DFF062 ; BLTBMOD
	SUB.W	D2,D3
	BGE.S	dldspos
	OR.B	#$40,D4
dldspos	MOVE.L	D3,$DFF050 ; BLTAPTR
	SUB.W	D2,D3
	MOVE.W	D3,$DFF064 ; BLTAMOD
	MOVE.W	D4,$DFF042 ; BLTCON1
	MOVE.W	D0,$DFF040 ; BLTCON0
	MOVE.L	D1,$DFF048 ; BLTCPTR
	MOVE.L	D1,$DFF054 ; BLTDPTR
	MOVE.W	#ScrWidth,$DFF060 ; BLTCMOD
	MOVE.W	#ScrWidth,$DFF066 ; BLTDMOD
	MOVE.W	#$8000,$DFF074 ; BLTADAT
	MOVE.W	#$FFFF,$DFF044 ; BLTAFWM
	MOVE.W	LinMask(PC),$DFF072 ; BLTBDAT
	ASL.W	#6,D2
	ADD.W	#2,D2
	MOVE.W	D2,$DFF058 ; BLTSIZE
	MOVE.W	(SP)+,$DFF09A
	MOVEM.L	(SP)+,D4-D7
	RTS

ScrWidth = 40

Octants	dc.b	3*4+1,2*4+1,1*4+1,0*4+1,7*4+1,5*4+1,6*4+1,4*4+1
LinMask	dc.w	$FFFF
MinTerm	dc.b	$C8 ;was $CA
	dc.b	0   ;free 0
PenX	dc.w	0
PenY	dc.w	0
LineScreen dc.l	0

;---- Loop Sprites ----

SetLoopSprites
	MOVEQ	#-1,D6
	MOVEQ	#-1,D7
	MOVE.W	InsNum(PC),D0
	BEQ	slsset
	MULU	#30,D0
	MOVE.L	SongDataPtr(PC),A0
	LEA	12(A0,D0.W),A0
SetLoopSprites2
	TST.W	SamScrEnable
	BEQ	Return2
	CLR.W	LoopOnOffFlag
	MOVEQ	#-1,D6
	MOVEQ	#-1,D7
	MOVEQ	#0,D0
	MOVE.W	4(A0),D0
	ADD.L	D0,D0
	MOVEQ	#0,D1
	MOVE.W	6(A0),D1
	ADD.L	D1,D1
	MOVE.L	D0,D5
	ADD.L	D1,D5
	CMP.W	#2,D5
	BLS.S	slsset
	MOVE.W	#1,LoopOnOffFlag
	MOVE.L	SamOffset(PC),D2
	MOVE.L	SamDisplay(PC),D3
	MOVE.L	D2,D4
	ADD.L	D3,D4
	CMP.L	D2,D0
	BLO.S	1$
	CMP.L	D4,D0
	BHI.S	1$
	SUB.L	D2,D0
	MULU	#314,D0
	DIVU	D3,D0
	ADDQ.W	#3,D0
	MOVE.W	D0,D6
1$	CMP.L	D2,D5
	BLO.S	slsset
	CMP.L	D4,D5
	BHI.S	slsset
	SUB.L	D2,D5
	MULU	#314,D5
	DIVU	D3,D5
	ADDQ.W	#6,D5
	MOVE.W	D5,D7
slsset	MOVE.W	#139,D1
	MOVE.W	D6,D0
	BPL.S	1$
	MOVEQ	#0,D0
	MOVE.W	#270,D1
1$	MOVEQ	#64,D2
	LEA	LoopSpriteData1,A0
	MOVE.W	D0,LoopStartPos
	BSR	SetSpritePos
	MOVE.W	#139,D1
	MOVE.W	D7,D0
	BPL.S	2$
	MOVEQ	#0,D0
	MOVE.W	#270,D1
2$	MOVEQ	#64,D2
	LEA	LoopSpriteData2,A0
	MOVE.W	D0,LoopEndPos
	BSR	SetSpritePos
	BRA	ShowLoopToggle

;---- Playroutine ----

audchan1temp	dc.l	0,0,0,0,0,$00010000,0,  0,0,0,0
audchan2temp	dc.l	0,0,0,0,0,$00020000,0,  0,0,0,0
audchan3temp	dc.l	0,0,0,0,0,$00040000,0,  0,0,0,0
audchan4temp	dc.l	0,0,0,0,0,$00080000,0,  0,0,0,0

IntMusic
	MOVEM.L	D0-D7/A0-A6,-(SP)
	MOVE.L	RunMode(PC),D0
	BEQ	NoNewPositionYet
	CMP.L	#'patt',D0
	BEQ.S	1$
	MOVE.L	SongPosition(PC),CurrPos
1$	MOVE.L	SongDataPtr(PC),A0
	MOVE.B	sd_numofpatt(A0),SongNumOfPatt+1
	TST.W	StepPlayEnable
	BNE.S	2$
	ADDQ.L	#1,Counter
	MOVE.L	Counter(PC),D0
	CMP.L	CurrSpeed(PC),D0
	BLO.S	NoNewNote
2$	CLR.L	Counter
	TST.B	PattDelayTime2
	BEQ.S	GetNewNote
	BSR.S	NoNewAllChannels
	BRA	dskip

NoNewNote
	BSR.S	NoNewAllChannels
	BRA	NoNewPositionYet

NoNewAllChannels
	LEA	audchan1toggle,A4
	LEA	audchan1temp(PC),A6
	LEA	$DFF0A0,A5
	BSR	CheckEffects
	LEA	audchan2toggle,A4
	LEA	audchan2temp(PC),A6
	LEA	$DFF0B0,A5
	BSR	CheckEffects
	LEA	audchan3toggle,A4
	LEA	audchan3temp(PC),A6
	LEA	$DFF0C0,A5
	BSR	CheckEffects
	LEA	audchan4toggle,A4
	LEA	audchan4temp(PC),A6
	LEA	$DFF0D0,A5
	BRA	CheckEffects


GetNewNote
	LEA	12(A0),A3
	LEA	sd_pattpos(A0),A2
	LEA	sd_patterndata(A0),A0
	MOVEQ	#0,D1
	MOVE.L	SongPosition(PC),D0
	MOVE.B	(A2,D0.W),D1

	CMP.L	#'patt',RunMode
	BNE.S	1$
	MOVE.L	PatternNumber(PC),D1
1$	ASL.L	#8,D1

	ASL.L	#2,D1
	ADD.L	PatternPosition(PC),D1
	MOVE.L	D1,PatternPtr
	CLR.W	DMACONtemp
	LEA	audchan1toggle,A4
	LEA	$DFF0A0,A5
	LEA	audchan1temp(PC),A6
	MOVEQ	#1,D2
	BSR	PlayVoice
	MOVEQ	#0,D0
	MOVE.B	n_volume(A6),D0
	MOVE.W	D0,8(A5)
	LEA	audchan2toggle,A4
	LEA	$DFF0B0,A5
	LEA	audchan2temp(PC),A6
	MOVEQ	#2,D2
	BSR.S	PlayVoice
	MOVEQ	#0,D0
	MOVE.B	n_volume(A6),D0
	MOVE.W	D0,8(A5)
	LEA	audchan3toggle,A4
	LEA	$DFF0C0,A5
	LEA	audchan3temp(PC),A6
	MOVEQ	#3,D2
	BSR.S	PlayVoice
	MOVEQ	#0,D0
	MOVE.B	n_volume(A6),D0
	MOVE.W	D0,8(A5)
	LEA	audchan4toggle,A4
	LEA	$DFF0D0,A5
	LEA	audchan4temp(PC),A6
	MOVEQ	#4,D2
	BSR.S	PlayVoice
	MOVEQ	#0,D0
	MOVE.B	n_volume(A6),D0
	MOVE.W	D0,8(A5)
	BRA	SetDMA

CheckMetronome
	CMP.B	MetroChannel,D2
	BNE	Return2
	MOVE.B	MetroSpeed,D2
	BEQ	Return2
	MOVE.L	PatternPosition,D3
	LSR.L	#4,D3
	DIVU	D2,D3
	SWAP	D3
	TST.W	D3
	BNE	Return2
	AND.L	#$00000FFF,(A6)
	OR.L	#$10D6F000,(A6) ; Play sample $1F at period $0D6 (214)
	RTS

PlayVoice
	TST.L	(A6)
	BNE.S	plvskip
	BSR	PerNop
plvskip	MOVE.L	(A0,D1.L),(A6)	; Read note from pattern
	BSR.S	CheckMetronome
	ADDQ.L	#4,D1
	MOVEQ	#0,D2
	MOVE.B	n_cmd(A6),D2	; Get lower 4 bits of instrument
	AND.B	#$F0,D2
	LSR.B	#4,D2
	MOVE.B	(A6),D0		; Get higher 4 bits of instrument
	AND.B	#$F0,D0
	OR.B	D0,D2
	TST.B	D2
	BEQ	SetRegisters	; Instrument was zero
	MOVEQ	#0,D3
	LEA	SampleStarts(PC),A1
	MOVE	D2,D4
	MOVE.B	D2,n_samplenum(A6)
	SUBQ.L	#1,D2
	LSL.L	#2,D2
	MULU	#30,D4
	MOVE.L	(A1,D2.L),n_start(A6)
	MOVE.W	(A3,D4.L),n_length(A6)
	MOVE.W	(A3,D4.L),n_reallength(A6)
	MOVE.B	2(A3,D4.L),n_finetune(A6)
	MOVE.B	3(A3,D4.L),n_volume(A6)
	MOVE.W	4(A3,D4.L),D3 ; Get repeat
	TST.W	D3
	BEQ.S	NoLoop
	MOVE.L	n_start(A6),D2	; Get start
	ASL.W	#1,D3
	ADD.L	D3,D2		; Add repeat
	MOVE.L	D2,n_loopstart(A6)
	MOVE.L	D2,n_wavestart(A6)
	MOVE.W	4(A3,D4.L),D0	; Get repeat
	ADD.W	6(A3,D4.L),D0	; Add replen
	MOVE.W	D0,n_length(A6)
	MOVE.W	6(A3,D4.L),n_replen(A6)	; Save replen
	BRA.S	SetRegisters

NoLoop	MOVE.L	n_start(A6),D2
	ADD.L	D3,D2
	MOVE.L	D2,n_loopstart(A6)
	MOVE.L	D2,n_wavestart(A6)
	MOVE.W	6(A3,D4.L),n_replen(A6)	; Save replen
SetRegisters
	MOVE.W	(A6),D0
	AND.W	#$0FFF,D0
	BEQ	CheckMoreEffects	; If no note
	MOVE.W	2(A6),D0
	AND.W	#$0FF0,D0
	CMP.W	#$0E50,D0 ; finetune
	BEQ.S	DoSetFineTune
	MOVE.B	2(A6),D0
	AND.B	#$0F,D0
	CMP.B	#3,D0	; TonePortamento
	BEQ.S	ChkTonePorta
	CMP.B	#5,D0	; TonePortamento + VolSlide
	BEQ.S	ChkTonePorta
	CMP.B	#9,D0	; Sample Offset
	BNE.S	SetPeriod
	BSR	CheckMoreEffects
	BRA.S	SetPeriod

DoSetFineTune
	BSR	SetFineTune
	BRA.S	SetPeriod

ChkTonePorta
	BSR	SetTonePorta
	BRA	CheckMoreEffects

SetPeriod
	MOVEM.L	D0-D1/A0-A1,-(SP)
	MOVE.W	(A6),D1
	AND.W	#$0FFF,D1
	LEA	PeriodTable(PC),A1
	MOVEQ	#0,D0
	MOVEQ	#$24,D7
ftuloop	CMP.W	(A1,D0.W),D1
	BHS	ftufound
	ADDQ.L	#2,D0
	DBRA	D7,ftuloop
ftufound
	MOVEQ	#0,D1
	MOVE.B	n_finetune(A6),D1
	MULU	#37*2,D1
	ADD.L	D1,A1
	MOVE.W	(A1,D0.W),n_period(A6)
	MOVEM.L	(SP)+,D0-D1/A0-A1

	MOVE.W	2(A6),D0
	AND.W	#$0FF0,D0
	CMP.W	#$0ED0,D0
	BEQ	CheckMoreEffects

	MOVE.W	n_dmabit(A6),$DFF096
	BTST	#2,n_wavecontrol(A6)
	BNE	vibnoc
	CLR.B	n_vibratopos(A6)
vibnoc	BTST	#6,n_wavecontrol(A6)
	BNE	trenoc
	CLR.B	n_tremolopos(A6)
trenoc	MOVE.W	n_length(A6),4(A5)	; Set length
	MOVE.L	n_start(A6),(A5)	; Set start
	BNE	sdmaskp
	CLR.L	n_loopstart(A6)
	MOVEQ	#1,D0
	MOVE.W	D0,4(A5)
	MOVE.W	D0,n_replen(A6)
sdmaskp	MOVE.W	n_period(A6),D0
	MOVE.W	D0,6(A5)		; Set period
	JSR	SpectrumAnalyzer	; Do the analyzer
	ST	n_trigger(A6)
	MOVE.W	n_dmabit(A6),D0
	OR.W	D0,DMACONtemp
	BRA	CheckMoreEffects
 
SetDMA	MOVE.W	DMAWait,D0
WaitDMA	DBRA	D0,WaitDMA
	MOVE.W	DMACONtemp,D0
	AND.W	ActiveChannels,D0
	OR.W	#$8000,D0
	MOVE.W	D0,$DFF096
	MOVE.W	DMAWait,D0
WaitDMA2
	DBRA	D0,WaitDMA2
	LEA	$DFF000,A5
	LEA	audchan4temp(PC),A6
	MOVE.L	n_loopstart(A6),$D0(A5)
	MOVE.W	n_replen(A6),$D4(A5)
	LEA	audchan3temp(PC),A6
	MOVE.L	n_loopstart(A6),$C0(A5)
	MOVE.W	n_replen(A6),$C4(A5)
	LEA	audchan2temp(PC),A6
	MOVE.L	n_loopstart(A6),$B0(A5)
	MOVE.W	n_replen(A6),$B4(A5)
	LEA	audchan1temp(PC),A6
	MOVE.L	n_loopstart(A6),$A0(A5)
	MOVE.W	n_replen(A6),$A4(A5)

dskip	TST.L	RunMode
	BEQ	dskipx
	BSR	SetPatternPos
dskipx	MOVE.L	PatternPosition(PC),D0
	LSR.L	#4,D0
	MOVE.W	D0,ScrPattPos
	ADD.L	#16,PatternPosition
	MOVE.B	PattDelayTime,D0
	BEQ	dskpc
	MOVE.B	D0,PattDelayTime2
	CLR.B	PattDelayTime
dskpc	TST.B	PattDelayTime2
	BEQ	dskpa
	SUBQ.B	#1,PattDelayTime2
	BEQ	dskpa
	SUB.L	#16,PatternPosition
dskpa	TST.B	PBreakFlag
	BEQ	nnpysk
	SF	PBreakFlag
	MOVEQ	#0,D0
	MOVE.B	PBreakPosition(PC),D0
	LSL.W	#4,D0
	MOVE.L	D0,PatternPosition
	CLR.B	PBreakPosition
nnpysk	TST.W	StepPlayEnable
	BEQ	nnpysk2
	BSR	DoStopIt
	CLR.W	StepPlayEnable
	MOVE.L	PatternPosition(PC),D0
	LSR.L	#4,D0
	AND.W	#63,D0
	MOVE.W	D0,ScrPattPos
nnpysk2	CMP.L	#1024,PatternPosition
	BNE	NoNewPositionYet
NextPosition	
	MOVEQ	#0,D0
	MOVE.B	PBreakPosition(PC),D0
	LSL.W	#4,D0
	MOVE.L	D0,PatternPosition
	CLR.B	PBreakPosition
	CLR.B	PosJumpAssert
	CMP.L	#'patp',RunMode
	BNE	NoNewPositionYet
	ADDQ.L	#1,SongPosition
	AND.L	#$7F,SongPosition
	MOVE.L	SongPosition(PC),D1
	MOVE.L	SongDataPtr(PC),A0
	CMP.B	sd_numofpatt(A0),D1
	BLO	NoNewPositionYet
	CLR.L	SongPosition

	TST.W	StepPlayEnable
	BEQ	NoNewPositionYet
	BSR	DoStopIt
	CLR.W	StepPlayEnable
	MOVE.L	PatternPosition(PC),D0
	LSR.L	#4,D0
	MOVE.W	D0,ScrPattPos

NoNewPositionYet
	TST.B	PosJumpAssert
	BNE	NextPosition
	MOVEM.L	(SP)+,D0-D7/A0-A6
	RTS

CheckEffects
	TST.W	(A4)
	BEQ	Return2
	BSR	chkefx2
	MOVEQ	#0,D0
	MOVE.B	n_volume(A6),D0
	MOVE.W	D0,8(A5)
	RTS

chkefx2	BSR	UpdateFunk
	MOVE.W	n_cmd(A6),D0
	AND.W	#$0FFF,D0
	BEQ	Return2
	MOVE.B	n_cmd(A6),D0
	AND.B	#$0F,D0
	TST.B	D0
	BEQ	Arpeggio
	CMP.B	#1,D0
	BEQ	PortaUp
	CMP.B	#2,D0
	BEQ	PortaDown
	CMP.B	#3,D0
	BEQ	TonePortamento
	CMP.B	#4,D0
	BEQ	Vibrato
	CMP.B	#5,D0
	BEQ	TonePlusVolSlide
	CMP.B	#6,D0
	BEQ	VibratoPlusVolSlide
	CMP.B	#$E,D0
	BEQ	E_Commands
SetBack	MOVE.W	n_period(A6),6(A5)
	CMP.B	#7,D0
	BEQ	Tremolo
	CMP.B	#$A,D0
	BEQ	VolumeSlide
Return2	RTS

PerNop	MOVE.W	n_period(A6),6(A5)
	RTS

Arpeggio
	MOVEQ	#0,D0
	MOVE.L	Counter(PC),D0
	DIVS	#3,D0
	SWAP	D0
	CMP.W	#1,D0
	BEQ	Arpeggio1
	CMP.W	#2,D0
	BEQ	Arpeggio2
Arpeggio0
	MOVE.W	n_period(A6),D2
	BRA	ArpeggioSet

Arpeggio1
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	LSR.B	#4,D0
	BRA	ArpeggioFind

Arpeggio2
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#15,D0
ArpeggioFind
	ASL.W	#1,D0
	MOVEQ	#0,D1
	MOVE.B	n_finetune(A6),D1
	MULU	#37*2,D1
	LEA	PeriodTable(PC),A0
	ADD.L	D1,A0
	MOVEQ	#0,D1
	MOVE.W	n_period(A6),D1
	MOVEQ	#$24,D7
arploop	MOVE.W	(A0,D0.W),D2
	CMP.W	(A0),D1
	BHS	ArpeggioSet
	ADDQ.L	#2,A0
	DBRA	D7,arploop
	RTS

ArpeggioSet
	MOVE.W	D2,6(A5)
	RTS

FinePortaUp
	TST.L	Counter
	BNE	Return2
	MOVE.B	#$0F,LowMask
PortaUp	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	AND.B	LowMask,D0
	MOVE.B	#$FF,LowMask
	SUB.W	D0,n_period(A6)
	MOVE.W	n_period(A6),D0
	AND.W	#$0FFF,D0
	CMP.W	#$0071,D0
	BPL	PortaUskip
	AND.W	#$F000,n_period(A6)
	OR.W	#$0071,n_period(A6)
PortaUskip	MOVE.W	n_period(A6),D0
	AND.W	#$0FFF,D0
	MOVE.W	D0,6(A5)
	RTS

FinePortaDown
	TST.L	Counter
	BNE	Return2
	MOVE.B	#$0F,LowMask
PortaDown
	CLR.W	D0
	MOVE.B	n_cmdlo(A6),D0
	AND.B	LowMask,D0
	MOVE.B	#$FF,LowMask
	ADD.W	D0,n_period(A6)
	MOVE.W	n_period(A6),D0
	AND.W	#$0FFF,D0
	CMP.W	#$0358,D0
	BMI	PortaDskip
	AND.W	#$F000,n_period(A6)
	OR.W	#$0358,n_period(A6)
PortaDskip	MOVE.W	n_period(A6),D0
	AND.W	#$0FFF,D0
	MOVE.W	D0,6(A5)
	RTS

SetTonePorta
	MOVE.L	A0,-(SP)
	MOVE.W	(A6),D2
	AND.W	#$0FFF,D2
	MOVEQ	#0,D0
	MOVE.B	n_finetune(A6),D0
	MULU	#37*2,D0
	LEA	PeriodTable(PC),A0
	ADD.L	D0,A0
	MOVEQ	#0,D0
StpLoop	CMP.W	(A0,D0.W),D2
	BHS	StpFound
	ADDQ.W	#2,D0
	CMP.W	#37*2,D0
	BLO	StpLoop
	MOVEQ	#35*2,D0
StpFound
	MOVE.B	n_finetune(A6),D2
	AND.B	#8,D2
	BEQ	StpGoss
	TST.W	D0
	BEQ	StpGoss
	SUBQ.W	#2,D0
StpGoss	MOVE.W	(A0,D0.W),D2
	MOVE.L	(SP)+,A0
	MOVE.W	D2,n_wantedperiod(A6)
	MOVE.W	n_period(A6),D0
	CLR.B	n_toneportdirec(A6)
	CMP.W	D0,D2
	BEQ	ClearTonePorta
	BGE	Return2
	MOVE.B	#1,n_toneportdirec(A6)
	RTS

ClearTonePorta
	CLR.W	n_wantedperiod(A6)
	RTS	

 
TonePortamento
	MOVE.B	n_cmdlo(A6),D0
	BEQ	TonePortNoChange
	MOVE.B	D0,n_toneportspeed(A6)
	CLR.B	n_cmdlo(A6)
TonePortNoChange
	TST.W	n_wantedperiod(A6)
	BEQ	Return2
	MOVEQ	#0,D0
	MOVE.B	n_toneportspeed(A6),D0
	TST.B	n_toneportdirec(A6)
	BNE	TonePortaUp
TonePortaDown
	ADD.W	D0,n_period(A6)
	MOVE.W	n_wantedperiod(A6),D0
	CMP.W	n_period(A6),D0
	BGT	TonePortaSetPer
	MOVE.W	n_wantedperiod(A6),n_period(A6)
	CLR.W	n_wantedperiod(A6)
	BRA	TonePortaSetPer

TonePortaUp
	SUB.W	D0,n_period(A6)
	MOVE.W	n_wantedperiod(A6),D0
	CMP.W	n_period(A6),D0
	BLT	TonePortaSetPer
	MOVE.W	n_wantedperiod(A6),n_period(A6)
	CLR.W	n_wantedperiod(A6)

TonePortaSetPer
	MOVE.W	n_period(A6),D2
	MOVE.B	n_glissfunk(A6),D0
	AND.B	#$0F,D0
	BEQ	GlissSkip
	MOVEQ	#0,D0
	MOVE.B	n_finetune(A6),D0
	MULU	#37*2,D0
	LEA	PeriodTable(PC),A0
	ADD.L	D0,A0
	MOVEQ	#0,D0
GlissLoop
	CMP.W	(A0,D0.W),D2
	BHS	GlissFound
	ADDQ.W	#2,D0
	CMP.W	#37*2,D0
	BLO	GlissLoop
	MOVEQ	#35*2,D0
GlissFound
	MOVE.W	(A0,D0.W),D2
GlissSkip
	MOVE.W	D2,6(A5) ; Set period
	RTS

Vibrato	MOVE.B	n_cmdlo(A6),D0
	BEQ	Vibrato2
	MOVE.B	n_vibratocmd(A6),D2
	AND.B	#$0F,D0
	BEQ	vibskip
	AND.B	#$F0,D2
	OR.B	D0,D2
vibskip	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$F0,D0
	BEQ	vibskip2
	AND.B	#$0F,D2
	OR.B	D0,D2
vibskip2
	MOVE.B	D2,n_vibratocmd(A6)
Vibrato2
	MOVE.L	A4,-(SP)
	MOVE.B	n_vibratopos(A6),D0
	LEA	VibratoTable(PC),A4
	LSR.W	#2,D0
	AND.W	#$001F,D0
	MOVEQ	#0,D2
	MOVE.B	n_wavecontrol(A6),D2
	AND.B	#$03,D2
	BEQ	vib_sine
	LSL.B	#3,D0
	CMP.B	#1,D2
	BEQ	vib_rampdown
	MOVE.B	#255,D2
	BRA	vib_set
vib_rampdown
	TST.B	n_vibratopos(A6)
	BPL	vib_rampdown2
	MOVE.B	#255,D2
	SUB.B	D0,D2
	BRA	vib_set
vib_rampdown2
	MOVE.B	D0,D2
	BRA	vib_set
vib_sine
	MOVE.B	0(A4,D0.W),D2
vib_set
	MOVE.B	n_vibratocmd(A6),D0
	AND.W	#15,D0
	MULU	D0,D2
	LSR.W	#7,D2
	MOVE.W	n_period(A6),D0
	TST.B	n_vibratopos(A6)
	BMI	VibratoNeg
	ADD.W	D2,D0
	BRA	Vibrato3
VibratoNeg
	SUB.W	D2,D0
Vibrato3
	MOVE.W	D0,6(A5)
	MOVE.B	n_vibratocmd(A6),D0
	LSR.W	#2,D0
	AND.W	#$003C,D0
	ADD.B	D0,n_vibratopos(A6)
	MOVE.L	(SP)+,A4
	RTS

TonePlusVolSlide
	BSR	TonePortNoChange
	BRA	VolumeSlide

VibratoPlusVolSlide
	BSR	Vibrato2
	BRA	VolumeSlide

Tremolo	MOVE.L	A4,-(SP)
	MOVE.B	n_cmdlo(A6),D0
	BEQ	Tremolo2
	MOVE.B	n_tremolocmd(A6),D2
	AND.B	#$0F,D0
	BEQ	treskip
	AND.B	#$F0,D2
	OR.B	D0,D2
treskip	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$F0,D0
	BEQ	treskip2
	AND.B	#$0F,D2
	OR.B	D0,D2
treskip2
	MOVE.B	D2,n_tremolocmd(A6)
Tremolo2
	MOVE.B	n_tremolopos(A6),D0
	LEA	VibratoTable(PC),A4
	LSR.W	#2,D0
	AND.W	#$001F,D0
	MOVEQ	#0,D2
	MOVE.B	n_wavecontrol(A6),D2
	LSR.B	#4,D2
	AND.B	#$03,D2
	BEQ	tre_sine
	LSL.B	#3,D0
	CMP.B	#1,D2
	BEQ	tre_rampdown
	MOVE.B	#255,D2
	BRA	tre_set
tre_rampdown
	TST.B	n_vibratopos(A6)
	BPL	tre_rampdown2
	MOVE.B	#255,D2
	SUB.B	D0,D2
	BRA	tre_set
tre_rampdown2
	MOVE.B	D0,D2
	BRA	tre_set
tre_sine
	MOVE.B	0(A4,D0.W),D2
tre_set
	MOVE.B	n_tremolocmd(A6),D0
	AND.W	#15,D0
	MULU	D0,D2
	LSR.W	#6,D2
	MOVEQ	#0,D0
	MOVE.B	n_volume(A6),D0
	TST.B	n_tremolopos(A6)
	BMI	TremoloNeg
	ADD.W	D2,D0
	BRA	Tremolo3
TremoloNeg
	SUB.W	D2,D0
Tremolo3
	BPL	TremoloSkip
	CLR.W	D0
TremoloSkip
	CMP.W	#$40,D0
	BLS	TremoloOk
	MOVE.W	#$40,D0
TremoloOk
	MOVE.W	D0,8(A5)
	MOVE.B	n_tremolocmd(A6),D0
	LSR.W	#2,D0
	AND.W	#$003C,D0
	ADD.B	D0,n_tremolopos(A6)
	MOVE.L	(SP)+,A4
	ADDQ.L	#4,SP
	RTS

SampleOffset
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	BEQ	sononew
	MOVE.B	D0,n_sampleoffset(A6)
sononew	MOVE.B	n_sampleoffset(A6),D0
	LSL.W	#7,D0
	CMP.W	n_length(A6),D0
	BGE	sofskip
	SUB.W	D0,n_length(A6)
	LSL.W	#1,D0
	ADD.L	D0,n_start(A6)
	RTS
sofskip MOVE.W	#$0001,n_length(A6)
	RTS

VolumeSlide
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	LSR.B	#4,D0
	TST.B	D0
	BEQ	VolSlideDown
VolSlideUp
	ADD.B	D0,n_volume(A6)
	CMP.B	#$40,n_volume(A6)
	BMI	vsuskip
	MOVE.B	#$40,n_volume(A6)
vsuskip	MOVE.B	n_volume(A6),D0
;	MOVE.W	D0,8(A5)
	RTS

VolSlideDown
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
VolSlideDown2
	SUB.B	D0,n_volume(A6)
	BPL	vsdskip
	CLR.B	n_volume(A6)
vsdskip	MOVE.B	n_volume(A6),D0
;	MOVE.W	D0,8(A5)
	RTS

PositionJump
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	SUBQ.B	#1,D0
	MOVE.L	D0,SongPosition
pj2	CLR.B	PBreakPosition
	ST 	PosJumpAssert
	RTS

VolumeChange
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	CMP.B	#$40,D0
	BLS	VolumeOk
	MOVEQ	#$40,D0
VolumeOk
	MOVE.B	D0,n_volume(A6)
;	MOVE.W	D0,8(A5)
	RTS

PatternBreak
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	MOVE.L	D0,D2
	LSR.B	#4,D0
	MULU	#10,D0
	AND.B	#$0F,D2
	ADD.B	D2,D0
	CMP.B	#63,D0
	BHI	pj2
	MOVE.B	D0,PBreakPosition
	ST	PosJumpAssert
	RTS

SetSpeed
	MOVE.B	3(A6),D0
	AND.W	#$FF,D0
	BEQ.S	SpeedNull
	TST.B	IntMode
	BEQ.S	normspd
	CMP.W	#32,D0
	BLO.S	normspd
	MOVE.W	D0,RealTempo
	MOVEM.L	D0-D7/A0-A6,-(SP)
	MOVE.W	SamScrEnable,-(SP)
	ST	SamScrEnable
	ST	UpdateTempo
	JSR	SetTempo
	MOVE.W	(SP)+,SamScrEnable
	MOVEM.L	(SP)+,D0-D7/A0-A6
	RTS
normspd	CLR.L	Counter
	MOVE.W	D0,CurrSpeed+2
	RTS
SpeedNull
	CLR.L	RunMode
	JSR	SetNormalPtrCol
	RTS


CheckMoreEffects
	MOVE.B	2(A6),D0
	AND.B	#$0F,D0
	CMP.B	#$9,D0
	BEQ	SampleOffset
	CMP.B	#$B,D0
	BEQ	PositionJump
	CMP.B	#$D,D0
	BEQ	PatternBreak
	CMP.B	#$E,D0
	BEQ	E_Commands
	CMP.B	#$F,D0
	BEQ	SetSpeed
	TST.W	(A4)
	BEQ	Return2
	CMP.B	#$C,D0
	BEQ	VolumeChange
	BRA	PerNop

E_Commands
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$F0,D0
	LSR.B	#4,D0
	BEQ	FilterOnOff
	CMP.B	#1,D0
	BEQ	FinePortaUp
	CMP.B	#2,D0
	BEQ	FinePortaDown
	CMP.B	#3,D0
	BEQ	SetGlissControl
	CMP.B	#4,D0
	BEQ	SetVibratoControl
	CMP.B	#5,D0
	BEQ	SetFineTune
	CMP.B	#6,D0
	BEQ	JumpLoop
	CMP.B	#7,D0
	BEQ	SetTremoloControl
	CMP.B	#8,D0
	BEQ	KarplusStrong
	CMP.B	#$E,D0
	BEQ	PatternDelay
	TST.W	(A4)
	BEQ	Return2
	CMP.B	#9,D0
	BEQ	RetrigNote
	CMP.B	#$A,D0
	BEQ	VolumeFineUp
	CMP.B	#$B,D0
	BEQ	VolumeFineDown
	CMP.B	#$C,D0
	BEQ	NoteCut
	CMP.B	#$D,D0
	BEQ	NoteDelay
	CMP.B	#$F,D0
	BEQ	FunkIt
	RTS

FilterOnOff
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#1,D0
	ASL.B	#1,D0
	AND.B	#$FD,$BFE001
	OR.B	D0,$BFE001
	RTS	

SetGlissControl
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	AND.B	#$F0,n_glissfunk(A6)
	OR.B	D0,n_glissfunk(A6)
	RTS

SetVibratoControl
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	AND.B	#$F0,n_wavecontrol(A6)
	OR.B	D0,n_wavecontrol(A6)
	RTS

SetFineTune
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	MOVE.B	D0,n_finetune(A6)
	RTS

JumpLoop
	TST.L	Counter
	BNE	Return2
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	BEQ	SetLoop
	TST.B	n_loopcount(A6)
	BEQ	jumpcnt
	SUB.B	#1,n_loopcount(A6)
	BEQ	Return2
jmploop	MOVE.B	n_pattpos(A6),PBreakPosition
	ST	PBreakFlag
	RTS

jumpcnt	MOVE.B	D0,n_loopcount(A6)
	BRA	jmploop

SetLoop	MOVE.L	PatternPosition,D0
	LSR.L	#4,D0
	AND.B	#63,D0
	MOVE.B	D0,n_pattpos(A6)
	RTS

SetTremoloControl
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	LSL.B	#4,D0
	AND.B	#$0F,n_wavecontrol(A6)
	OR.B	D0,n_wavecontrol(A6)
	RTS

KarplusStrong
	MOVEM.L	D1-D2/A0-A1,-(SP)
	MOVE.L	n_loopstart(A6),A0
	MOVE.L	A0,A1
	MOVE.W	n_replen(A6),D0
	ADD.W	D0,D0
	SUBQ.W	#2,D0
karplop	MOVE.B	(A0),D1
	EXT.W	D1
	MOVE.B	1(A0),D2
	EXT.W	D2
	ADD.W	D1,D2
	ASR.W	#1,D2
	MOVE.B	D2,(A0)+
	DBRA	D0,karplop
	MOVE.B	(A0),D1
	EXT.W	D1
	MOVE.B	(A1),D2
	EXT.W	D2
	ADD.W	D1,D2
	ASR.W	#1,D2
	MOVE.B	D2,(A0)
	MOVEM.L	(SP)+,D1-D2/A0-A1
	RTS

RetrigNote
	MOVE.L	D1,-(SP)
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	BEQ	rtnend
	MOVE.L	Counter,D1
	BNE	rtnskp
	MOVE.W	n_note(A6),D1
	AND.W	#$0FFF,D1
	BNE	rtnend
	MOVE.L	Counter,D1
rtnskp	DIVU	D0,D1
	SWAP	D1
	TST.W	D1
	BNE	rtnend
DoRetrg	MOVE.W	n_dmabit(A6),$DFF096	; Channel DMA off
	MOVE.L	n_start(A6),(A5)	; Set sampledata pointer
	MOVE.W	n_length(A6),4(A5)	; Set length
	MOVE.W	n_period(A6),6(A5)
	MOVEQ	#0,D0
	MOVE.B	n_volume(A6),D0
;	MOVE.W	D0,8(A5)
	MOVE.W	DMAWait,D0
rtnloop1
	DBRA	D0,rtnloop1
	MOVE.W	n_dmabit(A6),D0
	BSET	#15,D0
	MOVE.W	D0,$DFF096
	MOVE.W	DMAWait,D0
rtnloop2
	DBRA	D0,rtnloop2
	MOVE.L	n_loopstart(A6),(A5)
	MOVE.L	n_replen(A6),4(A5)
rtnend	MOVE.L	(SP)+,D1
	RTS

VolumeFineUp
	TST.L	Counter
	BNE	Return2
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$F,D0
	BRA	VolSlideUp

VolumeFineDown
	TST.L	Counter
	BNE	Return2
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	BRA	VolSlideDown2

NoteCut	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	CMP.L	Counter,D0
	BNE	Return2
	CLR.B	n_volume(A6)
;	MOVE.W	#0,8(A5)
	RTS

NoteDelay
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	CMP.L	Counter,D0
	BNE	Return2
	MOVE.W	(A6),D0
	AND.W	#$0FFF,D0
	BEQ	Return2
	MOVE.L	D1,-(SP)
	BRA	DoRetrg

PatternDelay
	TST.L	Counter
	BNE	Return2
	MOVEQ	#0,D0
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	TST.B	PattDelayTime2
	BNE	Return2
	ADDQ.B	#1,D0
	MOVE.B	D0,PattDelayTime
	RTS

FunkIt	TST.L	Counter
	BNE	Return2
	MOVE.B	n_cmdlo(A6),D0
	AND.B	#$0F,D0
	LSL.B	#4,D0
	AND.B	#$0F,n_glissfunk(A6)
	OR.B	D0,n_glissfunk(A6)
	TST.B	D0
	BEQ	Return2
UpdateFunk
	MOVEM.L	A0/D1,-(SP)
	MOVEQ	#0,D0
	MOVE.B	n_glissfunk(A6),D0
	LSR.B	#4,D0
	BEQ	funkend
	LEA	FunkTable(PC),A0
	MOVE.B	(A0,D0.W),D0
	ADD.B	D0,n_funkoffset(A6)
	BTST	#7,n_funkoffset(A6)
	BEQ	funkend
	CLR.B	n_funkoffset(A6)
	MOVE.L	n_loopstart(A6),D0
	MOVEQ	#0,D1
	MOVE.W	n_replen(A6),D1
	ADD.L	D1,D0
	ADD.L	D1,D0
	MOVE.L	n_wavestart(A6),A0
	ADDQ.L	#1,A0
	CMP.L	D0,A0
	BLO.S	funkok
	MOVE.L	n_loopstart(A6),A0
funkok	MOVE.L	A0,n_wavestart(A6)
	MOVEQ	#-1,D0
	SUB.B	(A0),D0
	MOVE.B	D0,(A0)
funkend	MOVEM.L	(SP)+,A0/D1
	RTS

;************************* End of Code ******************************

FunkTable dc.b 0,5,6,7,8,10,11,13,16,19,22,26,32,43,64,128

VibratoTable	
	dc.b   0, 24, 49, 74, 97,120,141,161
	dc.b 180,197,212,224,235,244,250,253
	dc.b 255,253,250,244,235,224,212,197
	dc.b 180,161,141,120, 97, 74, 49, 24

AllRightText	dc.b	'All right',0
PLSTFullText	dc.b	'Plst is full!',0
AreYouSureText	dc.b	'Are you sure?',0
NoDiskInDriveText
		dc.b	'No disk in df0!',0
WriteProtectedText
		dc.b	'Write protected',0
OprAbortedText	dc.b	'Print aborted!',0
EnterDataText	dc.b	'Enter data',0
AddingPathText	dc.b	'Adding path...',0
DiskErrorText	dc.b	'Disk error !!',0
LoadingText	dc.b	'Loading...',0
LoadingSongText	dc.b	'Loading song',0
LoadingModuleText
		dc.b	'Loading module',0
LoadingSampleText
		dc.b	'Loading sample',0
SavingText	dc.b	'Saving...',0
;SaveSongText	dc.b	'Save song ?',0
SavingSongText	dc.b	'Saving song',0
;SaveModuleText	dc.b	'Save module ?',0
SavingModuleText
		dc.b	'Saving module',0
;SaveSampleText	dc.b	'Save sample ?',0
SavingSampleText
		dc.b	'Saving sample',0
LoadingPLSTText	dc.b	'Loading plst',0
SavingPLSTText	dc.b	'Saving plst',0
DeletingSongText
		dc.b	'Deleting song',0
DeletingModuleText
		dc.b	'Deleting module',0
DeletingSampleText
		dc.b	'Deleting sample',0
RenamingFileText
		dc.b	'Renaming file',0
DecrunchingText	dc.b	'Decrunching...',0
CrunchingText	dc.b	'Crunching...',0
SelectEntryText	dc.b	'Select entry',0
SelectSongText	dc.b	'Select song',0
SelectModuleText
		dc.b	'Select module',0
SelectSampleText
		dc.b	'Select sample',0
SelectFileText	dc.b	'Select file',0
ReadingDirText	dc.b	'Reading dir...',0
PosSetText	dc.b	'Position set',0
PrintingSongText
		dc.b	'Printing song',0
PrintingPLSTText
		dc.b	'Printing plst',0

SaveSongText	dc.b	'Save song ?',0
DeleteSongText	dc.b	'Delete song ?',0
SaveModuleText	dc.b	'Save module ?',0
DeleteModuleText dc.b	'Delete module ?',0
SaveSampleText	dc.b	'Save sample ?',0
DeleteSampleText dc.b	'Delete sample ?',0
PrintPLSTText	dc.b	'Print plst ?',0
PrintSongText	dc.b	'Print song ?',0
QuitPTText	dc.b	'Really quit ?',0
UpsampleText	dc.b	'Upsample ?',0
DownSampleText	dc.b	'Downsample ?',0
PleaseSelectText dc.b	'Please select',0
ClearSplitText	dc.b	'Clear split ?',0
ResetAllText	dc.b	'Reset config ?',0
DeletePresetText dc.b	'Delete preset ?',0
LoadPLSTText	dc.b	'Load presetlist?',0
SavePLSTText	dc.b	'Save presetlist?',0
KillSampleText	dc.b	'Kill sample ?',0
AbortLoadText	dc.b	'Abort loading ?',0
LoadConfigText	dc.b	'Load config ?',0
SaveConfigText	dc.b	'Save config ?',0

DiskFormatText	dc.b	'Disk format'
InProgressText	dc.b	'in progress'
FormattingCylText
		dc.b	'Formatting cyl ',0
VerifyingText	dc.b	'Verifying  cyl ',0
InitDiskText	dc.b	'Initializing',0
PlsEntNamText	dc.b	'Please enter name'
OfVolumeText	dc.b	'of volume:  ST-__',0,0
	cnop 0,2
DiskNumText1	dc.b	'0'
DiskNumText2	dc.b	'0  '
PEdDefaultPath	dc.b	'df0:',0
		dcb.b	47,0
	
PEdDefaultVol	dc.b	'st-'
SndDiskNum0	dc.b	'0'
SndDiskNum1	dc.b	'1:'

PresetName	dc.b	'      '
PsetNameText	dc.b	'                '
		dc.b	' '
PsetVolText	dc.b	'  '
		dc.b	' '
PsetLenText	dc.b	'    '
		dc.b	' '
PsetRepeatText	dc.b	'    '
		dc.b	' '
PsetReplenText	dc.b	'    '

InsertPsetText	dc.b	'ST-01:                  0 0000 0000 0002'
PsetPLSTtext	dc.b	'No.    Samplename               '
		dc.b	'Length  Repeat  RepLen',$A,$A
PsetPrtNumText	dc.b	'    :  '
PsetPrtNameText	dc.b	'                             '
PsetPrtLenText	dc.b	'        '
PsetPrtRepeatText	dc.b	'        '
PsetPrtRepLenText	dc.b	10
	dc.b	0
SongDumpText
	dc.b	9
	dc.b	'Protracker Song-Dump -- Made with '
	dc.b	'Protracker V1.2A  ',$D,$A,$A
	dc.b	9
	dc.b	'Songname:  '
CRLF_Text	dc.b	13,10
FF_Text		dc.b	12,0

	cnop 0,2
PatternNumText	dc.b	9,9,9,'Pattern: '
PattNumText1	dc.b	'0'
PattNumText2	dc.b	'0',$D,$A,$D,$A

PtotText	dc.b	9
PattXText1	dc.b	"0"
PattXText2	dc.b	"0 : "
PpText		dc.b	"                      "
Prafs		dc.b	"  0000  0000  0000    ",$D,$A
PnText2		dc.b	"        "
	cnop 0,2
		dc.b	0
PattPosText	dc.b	'00  :                                                       '
PnText		dc.b	'    ',0

	cnop 0,2
FmtSongDirName	dc.b	'DF0:songs',0
FmtModuleDirName	dc.b	'DF0:modules',0

	cnop 0,2
STText1		dc.b	'ST'
STText1Num	dc.b	'-'
STText1Number	dc.b	0,0,':'
STText2		dc.b	'ST'
STText2Num	dc.b	'-'
STText2Number	dc.b	0,0,':'
STText3		dc.b	'ST'
STText3Num	dc.b	'-'
STText3Number	dc.b	0,0,':'

EmptyLineText	dc.b	'                       ',0

FastHexTable	dc.b	'000102030405060708090A0B0C0D0E0F'
		dc.b	'101112131415161718191A1B1C1D1E1F'
		dc.b	'202122232425262728292A2B2C2D2E2F'
		dc.b	'303132333435363738393A3B3C3D3E3F'
		dc.b	'404142434445464748494A4B4C4D4E4F'
		dc.b	'505152535455565758595A5B5C5D5E5F'
		dc.b	'606162636465666768696A6B6C6D6E6F'
		dc.b	'707172737475767778797A7B7C7D7E7F'
		dc.b	'808182838485868788898A8B8C8D8E8F'
		dc.b	'909192939495969798999A9B9C9D9E9F'
		dc.b	'A0A1A2A3A4A5A6A7A8A9AAABACADAEAF'
		dc.b	'B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF'
		dc.b	'C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF'
		dc.b	'D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF'
		dc.b	'E0E1E2E3E4E5E6E7E8E9EAEBECEdEEEF'
		dc.b	'F0F1F2F3F4F5F6F7F8F9FAFBFCFdFEFF'

TrackdiskName	dc.b	'trackdisk.device',0
InputDevName	dc.b	'input.device',0
DOSbase		dc.l	0
IntuitionBase	dc.l	0
GfxBase		dc.l	0

RawKeyHexTable	
	dc.b	10,1,2,3,4,5,6,7
	dc.b	8,9,32,53,51,34,18,35
RawKeyScaleTable	
	dc.b	49,33,50,34,51,52,36,53,37,54,38,55,56,40,57
	dc.b	41,58,16,2,17,3,18,19,5,20,6,21,7,22,23,9,24,10
	dc.b	25,26,12,27,70,70,0

KbdTransTable1	
	dc.b	 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16
	dc.b	12,13,14,15,16,17,18,19,20,21,22,23
	dc.b	24,25,26,27,28,29,30,31,36,36
KbdTransTable2
	dc.b	12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28
	dc.b	24,25,26,27,28,29,30,31,32,33,34,35
	dc.b	36,36,36,36,36,36,36,36,36,36
NoteNames1
	dc.b	'C-1 C#1 D-1 D#1 E-1 F-1 F#1 G-1 G#1 A-1 A#1 B-1 '
	dc.b	'C-2 C#2 D-2 D#2 E-2 F-2 F#2 G-2 G#2 A-2 A#2 B-2 '
	dc.b	'C-3 C#3 D-3 D#3 E-3 F-3 F#3 G-3 G#3 A-3 A#3 B-3 '
SpcNoteText
	dc.b	'--- '
NoteNames2
	dc.b	'C-1 DĄ1 D-1 EĄ1 E-1 F-1 GĄ1 G-1 AĄ1 A-1 BĄ1 B-1 '
	dc.b	'C-2 DĄ2 D-2 EĄ2 E-2 F-2 GĄ2 G-2 AĄ2 A-2 BĄ2 B-2 '
	dc.b	'C-3 DĄ3 D-3 EĄ3 E-3 F-3 GĄ3 G-3 AĄ3 A-3 BĄ3 B-3 '
	dc.b	'--- '

PeriodTable
; Tuning 0
	dc.w	856,808,762,720,678,640,604,570,538,508,480,453
	dc.w	428,404,381,360,339,320,302,285,269,254,240,226
	dc.w	214,202,190,180,170,160,151,143,135,127,120,113,0
; Tuning 1
	dc.w	850,802,757,715,674,637,601,567,535,505,477,450
	dc.w	425,401,379,357,337,318,300,284,268,253,239,225
	dc.w	213,201,189,179,169,159,150,142,134,126,119,113,0
; Tuning 2
	dc.w	844,796,752,709,670,632,597,563,532,502,474,447
	dc.w	422,398,376,355,335,316,298,282,266,251,237,224
	dc.w	211,199,188,177,167,158,149,141,133,125,118,112,0
; Tuning 3
	dc.w	838,791,746,704,665,628,592,559,528,498,470,444
	dc.w	419,395,373,352,332,314,296,280,264,249,235,222
	dc.w	209,198,187,176,166,157,148,140,132,125,118,111,0
; Tuning 4
	dc.w	832,785,741,699,660,623,588,555,524,495,467,441
	dc.w	416,392,370,350,330,312,294,278,262,247,233,220
	dc.w	208,196,185,175,165,156,147,139,131,124,117,110,0
; Tuning 5
	dc.w	826,779,736,694,655,619,584,551,520,491,463,437
	dc.w	413,390,368,347,328,309,292,276,260,245,232,219
	dc.w	206,195,184,174,164,155,146,138,130,123,116,109,0
; Tuning 6
	dc.w	820,774,730,689,651,614,580,547,516,487,460,434
	dc.w	410,387,365,345,325,307,290,274,258,244,230,217
	dc.w	205,193,183,172,163,154,145,137,129,122,115,109,0
; Tuning 7
	dc.w	814,768,725,684,646,610,575,543,513,484,457,431
	dc.w	407,384,363,342,323,305,288,272,256,242,228,216
	dc.w	204,192,181,171,161,152,144,136,128,121,114,108,0
; Tuning -8
	dc.w	907,856,808,762,720,678,640,604,570,538,508,480
	dc.w	453,428,404,381,360,339,320,302,285,269,254,240
	dc.w	226,214,202,190,180,170,160,151,143,135,127,120,0
; Tuning -7
	dc.w	900,850,802,757,715,675,636,601,567,535,505,477
	dc.w	450,425,401,379,357,337,318,300,284,268,253,238
	dc.w	225,212,200,189,179,169,159,150,142,134,126,119,0
; Tuning -6
	dc.w	894,844,796,752,709,670,632,597,563,532,502,474
	dc.w	447,422,398,376,355,335,316,298,282,266,251,237
	dc.w	223,211,199,188,177,167,158,149,141,133,125,118,0
; Tuning -5
	dc.w	887,838,791,746,704,665,628,592,559,528,498,470
	dc.w	444,419,395,373,352,332,314,296,280,264,249,235
	dc.w	222,209,198,187,176,166,157,148,140,132,125,118,0
; Tuning -4
	dc.w	881,832,785,741,699,660,623,588,555,524,494,467
	dc.w	441,416,392,370,350,330,312,294,278,262,247,233
	dc.w	220,208,196,185,175,165,156,147,139,131,123,117,0
; Tuning -3
	dc.w	875,826,779,736,694,655,619,584,551,520,491,463
	dc.w	437,413,390,368,347,328,309,292,276,260,245,232
	dc.w	219,206,195,184,174,164,155,146,138,130,123,116,0
; Tuning -2
	dc.w	868,820,774,730,689,651,614,580,547,516,487,460
	dc.w	434,410,387,365,345,325,307,290,274,258,244,230
	dc.w	217,205,193,183,172,163,154,145,137,129,122,115,0
; Tuning -1
	dc.w	862,814,768,725,684,646,610,575,543,513,484,457
	dc.w	431,407,384,363,342,323,305,288,272,256,242,228
	dc.w	216,203,192,181,171,161,152,144,136,128,121,114,0

CursorPosTable	
	dc.b	3,6,7,8,9,10,12
	dc.b	15,16,17,18,19,21
	dc.b	24,25,26,27,28,30
	dc.b	33,34,35,36,37
UnshiftedKeymap
	dc.b	'`1234567890-=\*0'
	dc.b	'qwertyuiop[]*123'
	dc.b	'asdfghjkl;',39,'#',0,'456'
	dc.b	'<zxcvbnm,./',0,'.','789 '
ShiftedKeymap
	dc.b	'~!@#$%^&*()_+|*0'
	dc.b	'qwertyuiop{}*123'
	dc.b	'asdfghjkl:',34,'^',0,'456'
	dc.b	'>zxcvbnm<>?',0,'.','789 '

SongNumOfPatt	dc.w	1
NoteNamesPtr	dc.l	0
SpritePtrsPtr	dc.l	0
LineCurPosPtr	dc.l	0
NoSpritePtr	dc.l	0
CursorPosPtr	dc.l	0
Ch1SpritePtr	dc.l	0
Ch2SpritePtr	dc.l	0
Ch3SpritePtr	dc.l	0
Ch4SpritePtr	dc.l	0
PPattPos	dc.l	0
SaveEditMode	dc.l	0
EditMode	dc.l	0
RunMode		dc.l	0
CurrCmds	dc.l	0
TextBplPtr	dc.l	0
TextOffset	dc.w	0
TextLength	dc.w	0
TextLenTemp	dc.l	0
ShowTextPtr	dc.l	0
PatternNumber	dc.l	0
CurrPos		dc.l	0
CurrSpeed	dc.l	0
KeyTransTabPtr	dc.l	0
CmdOffset	dc.w	0
RawKeyCode	dc.b	0
PattRfsh	dc.b	0
MountFlag	dc.w	0
GetlineFlag	dc.w	0
LoadInProgress	dc.w	0
KbIntSave	dc.l	0
KbIntSavePtr	dc.l	0
CurrentPlayNote	dc.w	214
LineCurX	dc.w	0
LineCurY	dc.w	0
TextEndPtr	dc.l	0
ScrPattPos	dc.w	0
PattCurPos	dc.w	0
GadgRepeat	dc.w	0
WordNumber	dc.w	0
HighPattNum	dc.w	0
InsNum	dc.w	0
LastInsNum	dc.w	1
PlayInsNum	dc.w	1
HiLowInstr	dc.w	0
PEDpos		dc.w	0
PLSTpos		dc.w	0
MaxPLSTOffset	dc.l	0
PresetMarkTotal	dc.w	0
PLSTOffset	dcb.b	14*4,0
CurrentPreset	dc.w	1
UpOrDown	dc.w	0
Joy0Low		dc.w	0
Joy0High	dc.w	0
MouseX	dc.w	0
MouseY	dc.w	0
MouseX2	dc.w	0
MouseY2	dc.w	0
MouseX3	dc.w	0
MouseY3	dc.w	0
DecompMemPtr	dc.l	0
DecompMemSize	dc.l	0

SampleInfo	dc.w	0
si_volume	dc.w	0
si_long		dc.l	0
si_pointer	dc.l	0,0,0,0,0
SampleInfo2	dc.w	0
si_volume2	dc.w	0
si_long2	dc.l	0
si_pointer2	dc.l	0,0,0,0,0

LongFFFF	dc.l	0
PosJumpAssert	dc.b	0
PBreakPosition	dc.b	0
PBreakFlag	dc.b	0
LEDStatus	dc.b	0
PattDelayTime	dc.b	0
PattDelayTime2	dc.b	0
PattRfshNum	dc.l	0
DidQuantize	dc.w	0
MessagePtr	dc.l	0
FileNamePtr	dc.l	0
DiskDataPtr	dc.l	0
FileHandle	dc.l	0
DiskDataLength	dc.l	0
LowMask		dc.b	$FF
PatternPosition	dc.l	0
SongPosition	dc.l	0
Counter		dc.l	0
PatternPtr	dc.l	0
SongDataPtr	dc.l	0
SampleStarts	dcb.l	31,0
SamplePtrs	EQU	SampleStarts-4
SampleLengths	dcb.l	32,0
DMACONtemp	dc.w	0
StackSave	dc.l	0
InputDevMouse	dc.l	0,0,0
AnalyzerHeights	dcb.w	23,0
AnalyzerOpplegg	dcb.w	23,0
Action		dc.w	0
PathPtr		dc.l	0
FileLock	dc.l	0
CurrScreen	dc.w	0
SaveCurrScreen	dc.w	0
PointerCol1Save	dc.w	0
PointerCol2Save	dc.w	0
PointerCol3Save	dc.w	0
GetDecTemp	dc.b	0
UpdateTempo	dc.b	0
MoreInstrFlag	dc.w	0
SampleSortList	dcb.l	32,0
PLSTmem		dc.l	0
PresetTotal	dc.w	0
SaveScope	dc.b	0
		dc.b	0 ;free 0
DisableAnalyzer	dc.b	0
SaveDA		dc.b	0
AutoReqRunning	dc.b	0
NoSampleInfo	dc.b	0
FormatDataPtr	dc.l	0
CylinderNumber	dc.w	0
TrackdiskPort	dc.l	0,0,0,0
ProcessPtr	dc.l	0,0,0,0
DateStamp	dc.l	0,0,0,0
EnterTextFlag	dc.w	0
PresetLength	dc.w	0
PresetFineTune	dc.w	0
PresetRepeat	dc.w	0
PresetReplen	dc.w	0
PED_Action	dc.w	0
PsetNumTemp	dc.w	0
PsetPtrTemp	dc.l	0
FreeDiskSpace	dc.l	0
SplitAddress	dc.l	0
LaHeTx		dc.l	$12345678
PLSTallocsize	dc.l	0
MaxPLSTEntries2	dc.w	0

;---- Setup Data ----

DefaultSetupData
	dc.b	'PT1.2 Configuration File',$A,0
	dc.b	'ST-00:songs'
	dcb.b	21,0
	dc.b	'ST-00:modules'
	dcb.b	19,0
	dc.b	'DF0:'
	dcb.b	28,0
	dc.b	'PRT:'
	dcb.b	28,0
DefCol	dc.w	$000,$BBB,$888,$555,$FD0,$D04,$000,$34F
	dc.b	1   ; Song Pack Mode
	dc.b	0   ; Module Pack Mode
	dc.b	0   ; Split  0=off, 1=on
	dc.b	0   ; Filter
	dc.b	0   ; TransDel
	dc.b	1   ; ShowDec
	dc.b	1   ; AutoDir
	dc.b	1   ; AutoExit
	dc.b	1   ; ModOnly
	dc.b	0   ; MIDI
	dc.b	1,$18,$18,0	; SplitData
	dc.b	2,$1A,$18,0
	dc.b	3,$1C,$18,0
	dc.b	4,$1D,$18,0
	dc.w	20,2    ; KeyRepDelay/Speed
	dc.b	0	; Accidental
	dc.b	0	; not in use
	dc.b	'newshell',0	; ExtCmd 0
	dcb.b	23,0
	dc.b	'add21k',0	; ExtCmd 1
	dcb.b	25,0
	dc.b	'info',0	; ExtCmd 2
	dcb.b	27,0
	dc.b	'list',0	; ExtCmd 3
	dcb.b	27,0
	dc.b	'loadwb -debug',0 ; ExtCmd 4
	dcb.b	18,0
	dc.b	'diskmaster1.3',0 ; ExtCmd 5
	dcb.b	18,0
	dc.b	'run AudioMasterIII',0 ; ExtCmd 6
	dcb.b	13,0
	dc.b	'ced',0	; ExtCmd 7
	dcb.b	28,0
	dc.w	0	; Not in use
	dc.w	2500	; Max PLST Entries
	dc.b	2,3,4,1	; Multi Mode Next
	dc.w	$102,$202,$037,$047,$304, $F06,$C10,$C20,$E93,$A0F
	dc.b	0 ; RAW/IFF Save, 0=RAW, 1=IFF
	dc.b	1 ; IntMode, 0=VBLANK, 1=CIA
	dc.b	0 ; Override
	dc.b	0 ; Nosamples
	dc.b	0 ; BlankZero
	dc.b	1 ; ShowDirs
	dc.b	0 ; CutToBuf
	dc.b	0 ; ShowPublic
	dc.b	1 ; IFFLoop
	dc.b	0 ; HHFlag
	dc.w	125 ; Tempo
	dc.w	300 ; DMAWait
	dc.w	24  ; TuneNote (C-3)
	dc.w	$20 ; TToneVol
	dc.b	0,0,0,0,0,0,0,0 ; (Pad to 512 bytes)
	dc.b	"ST-00:" ; Protracker Path
	dcb.b	26
	dc.w	6 ; DefaultSpeed
	dc.w	$0F00,$0F00,$0F10,$0F10,$0F20,$0F20,$0F30,$0F30 ; VU-Meter
	dc.w	$0F40,$0F50,$0F60,$0F70,$0F80,$0F90,$0FA0,$0FB0 ; colors
	dc.w	$0FC0,$0FD0,$0FE0,$0FF0,$0FF0,$0EF0,$0EF0,$0DF0
	dc.w	$0DF0,$0CF0,$0CF0,$0BF0,$0BF0,$0AF0,$09F0,$09F0
	dc.w	$08F0,$08F0,$07F0,$07F0,$06F0,$06F0,$05F0,$05F0
	dc.w	$04F0,$04F0,$03F0,$03F0,$02F0,$01F0,$00F0,$00E0

	dc.w	$0F00,$0F10,$0F20,$0F30,$0F40,$0F50,$0F60,$0F70 ; Analyzer
	dc.w	$0F80,$0F90,$0FA0,$0FB0,$0FC0,$0FD0,$0FE0,$0FF0 ; colors
	dc.w	$0EF0,$0DF0,$0CF0,$0BF0,$0AF0,$09F0,$08F0,$07F0
	dc.w	$06F0,$05F0,$04F0,$03F0,$02F0,$01F0,$00F0,$00E0
	dc.w	$00D0,$00C0,$00B0,$00A0,$0090,$0080,$0070,$0060
	dcb.b	302  ; Pad to 1024 bytes

;---- Sampler routines ----

_custom	EQU	$00DFF000
intreq	EQU	$0000009C
intreqr	EQU	$0000001E
intena	EQU	$0000009A
joy0dat	EQU	$0000000A
vhposr	EQU	$00000006
dmacon	EQU	$00000096
dmaconr	EQU	$00000002

SamInfoPtr	dc.l	0
SamInfoLen	dc.l	0
SampleNote	dc.w	24 ; C-3
ResampleNote	dc.w	24 ; C-3
SamIntSave	dc.w	0
SamDMASave	dc.w	0

ClearTempSampArea
	LEA	TempSampArea,A0
	MOVE.W	#379,D0
	MOVEQ	#0,D1
ctsalop	MOVE.W	D1,(A0)+
	DBRA	D0,ctsalop
	RTS

Sampler	CLR.B	RawKeyCode
	JSR	StopIt
	JSR	TopExit
	JSR	WaitForButtonUp
	JSR	SetWaitPtrCol
	BSR	ShowMon
	JSR	Wait_4000
	JSR	ClearRightArea
	JSR	ClearAnalyzerColors
	BSR	ClearTempSampArea
	BSR	WaitForDiskDrive
	MOVE.W	$DFF01C,SamIntSave ; intenar
	MOVE.W	#$7FFF,_custom+intena
	JSR	TurnOffVoices
	MOVE.W	SampleNote,D0
	ASL.W	#1,D0
	LEA	PeriodTable,A0
	MOVE.W	(A0,D0.W),D0
	LSR.W	#1,D0
	LEA	$00DFF000,A0
	LEA	$AA(A0),A5
	LEA	$BA(A0),A6
	MOVE.W	D0,$A6(A0)
	MOVE.W	D0,$B6(A0)
	MOVEQ.L	#$20,D0
	MOVE.W	D0,$A8(A0)
	MOVE.W	D0,$B8(A0)
	MOVEQ.L	#0,D0
	MOVE.W	D0,(A5)
	MOVE.W	D0,(A6)
	MOVE.B	#6,$00BFD200
	MOVEQ.L	#2,D0
	MOVE.B	D0,$00BFD000
	MOVE.B	#0,$00BFE301
	LEA	GraphOffsets(PC),A0
	LEA	TempSampArea,A1
	MOVE.L	TextBplPtr,A2
	LEA	$09E8-40(A2),A2
	LEA	$00BFE101,A3
	LEA	_custom+intreqr,A4
	MOVE.W	#$0180,D7
	MOVEQ.L	#6,D6
	MOVEQ.L	#10,D3
monilop2
	MOVEQ.L	#22,D5
monilop4
	MOVEQ.L	#7,D4
monilop3
	BTST	D7,(A4)
	BEQ.S	monilop3
	MOVE.W	D7,$7E(A4)
	MOVEQ.L	#0,D0
	MOVE.B	(A3),D0
	MOVE.W	D0,D1
	SUB.B	D7,D0
	MOVE.B	D0,(A5)
	MOVE.B	D0,(A6)
	LSR.W	#3,D1
	ADD.W	D1,D1
	MOVE.W	0(A0,D1.W),D0
	MOVE.W	(A1),D1
	MOVE.W	D0,(A1)+
	BCLR	D4,0(A2,D1.W)
	BSET	D4,0(A2,D0.W)
	DBRA	D4,monilop3
	ADDQ.L	#1,A2
	DBRA	D5,monilop4
	SUB.L	#23,A2
	SUB.L	#368,A1
	BTST	D6,-$0100(A3)
	BEQ	sampexit
	BTST	D3,-8(A4)
	BNE.S	monilop2

;-- start sampling --

	MOVE.W	InsNum(PC),D1
	BEQ	sampexit
	LSL.W	#2,D1
	LEA	SamplePtrs,A0
	MOVE.L	(A0,D1.W),D0
	BEQ.S	samaok
	CLR.L	(A0,D1.W)
	MOVE.L	D0,A1
	MOVE.L	124(A0,D1.W),D0
	BEQ.S	samaok
	CLR.L	124(A0,D1.W)
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
samaok	MOVE.L	#$FFFE,D6	; try 64k
samalclop
	MOVE.L	D6,D0
	MOVE.L	#MEMF_CHIP!MEMF_CLEAR,D1
	MOVE.L	4.W,A6
	JSR	LVOAllocMem(A6)
	TST.L	D0
	BNE.S	samalcok
	SUB.L	#2048,D6	; try 2k less
	BPL.S	samalclop
	BSR	OutOfMemErr
	BRA	sampexit
 
samalcok
	MOVE.W	InsNum(PC),D1
	LSL.W	#2,D1
	LEA	SamplePtrs(PC),A0
	MOVE.L	D0,(A0,D1.W)
	MOVE.L	D6,124(A0,D1.W)

	MOVE.W	$DFF002,SamDMASave
	MOVE.W	#$03FF,_custom+dmacon
	BSR	GetSampleInfo
	MOVE.L	SamInfoLen,D4
	CLR.W	$DFF100
	CLR.W	$DFF180
	MOVE.L	SamInfoPtr,A1
	LEA	_custom+intreq,A2
	LEA	$00BFE101,A3 ; parallel port
	LEA	_custom+intreqr,A4
	LEA	$DFF0BA,A6

	MOVE.W	#$0180,D7
	MOVEQ.L	#6,D6
	MOVEQ.L	#0,D5
samploop
	BTST	D7,(A4)
	BEQ.S	samploop
	MOVE.W	D7,(A2)
	MOVE.B	(A3),D0
	SUB.B	D7,D0
	MOVE.B	D0,(A5)
	MOVE.B	D0,(A6)
	MOVE.B	D0,(A1)+
	ADDQ.W	#1,D5
	CMP.W	D4,D5
	BEQ.S	sampend
	BTST	D6,-$0100(A3)
	BNE.S	samploop
sampend	MOVE.W	SamDMASave,D0
	OR.W	#$8000,D0
	MOVE.W	D0,$00DFF096

	MOVE.L	A1,D0
	SUB.L	SamInfoPtr(PC),D0
	MOVE.L	D0,SamInfoLen

	MOVE.L	SongDataPtr(PC),A0
	MOVE.W	InsNum(PC),D1
	MULU	#30,D1
	LEA	12(A0,D1.W),A0
	MOVE.L	SamInfoLen(PC),D0
	LSR.L	#1,D0
	MOVE.W	D0,(A0)+
	MOVE.W	#$0040,(A0)+
	CLR.W	(A0)+
	MOVE.W	#1,(A0)

	MOVE.L	SamInfoLen(PC),D1
	NEG.L	D1
	AND.L	#$FFFFFFF8,D1
	NEG.L	D1
	MOVE.W	InsNum(PC),D0
	ASL.W	#2,D0
	LEA	SamplePtrs,A0
	CMP.L	124(A0,D0.W),D1
	BGE.S	sampexit
	MOVE.L	124(A0,D0.W),D2
	MOVE.L	D1,124(A0,D0.W)
	SUB.L	D1,D2
	MOVE.L	0(A0,D0.W),A1
	MOVE.L	124(A0,D0.W),D0
	ADD.L	D0,A1
	MOVE.L	D2,D0
	MOVE.L	4.W,A6
	JSR	LVOFreeMem(A6)
sampexit
	MOVE.W	SamIntSave,D0
	BSET	#15,D0
	MOVE.W	D0,$DFF09A
	JSR	TurnOffVoices
	JSR	DisplayMainScreen
	BSR	ClearSamStarts
	BSR	ShowSampleInfo
	BSR	RedrawSample
	JSR	WaitForButtonUp
	CLR.W	KeyBufPos
	CLR.B	RawKeyCode
	CLR.W	ShiftKeyStatus
	JMP	Wait_4000

SampleNullInfo
	CLR.L	SamInfoPtr
	CLR.L	SamInfoLen
	MOVEQ	#-1,D0
	RTS	
 
GetSampleInfo
	MOVE.W	InsNum(PC),D0
	BEQ.S	SampleNullInfo
	LSL.W	#2,D0
	LEA	SamplePtrs(PC),A0
	MOVE.L	(A0,D0.W),SamInfoPtr
	MOVE.L	124(A0,D0.W),SamInfoLen
	MOVEQ	#0,D0
	RTS

ShowMon	SF	ScopeEnable
	ST	DisableAnalyzer
	JSR	ClearRightArea
	LEA	MonitorData,A0
	MOVE.L	#MonitorSize,D0
	MOVEQ	#-1,D4
	JMP	cgjojo

WaitForDiskDrive
	JSR	StorePtrCol
	MOVE.L	4.W,A6
	LEA	$15E(A6),A0	; DeviceList
	LEA	TrackdiskName,A1	; trackdisk.device
	JSR	LVOFindName(A6)
	MOVE.L	D0,A6
	LEA	$24(A6),A6	; dn_GlobalVec
trdloop2
	MOVE.L	A6,A1
	MOVEQ.L	#3,D6
tdrloop	TST.L	(A1)+
	BEQ.S	tdrskip
	MOVE.L	-4(A1),A2
	BTST	#7,$0041(A2)
	BEQ.S	tdrnotset
tdrskip	DBRA	D6,tdrloop
	JMP	RestorePtrCol

tdrnotset
	JSR	SetDiskPtrCol
	BRA.S	trdloop2

ClearSamStarts
	LEA	SamplePtrs+4,A0
	MOVEQ	#30,D0
cssLoop	MOVE.L	(A0)+,D1
	BEQ	cssSkip
	MOVE.L	D1,A1
	CLR.W	(A1)
cssSkip	DBRA	D0,cssLoop
	RTS

GraphOffsets
	dc.w 31*40,30*40,29*40,28*40,27*40,26*40,25*40,24*40
	dc.w 23*40,22*40,21*40,20*40,19*40,18*40,17*40,16*40
	dc.w 15*40,14*40,13*40,12*40,11*40,10*40,09*40,08*40
	dc.w 07*40,06*40,05*40,04*40,03*40,02*40,01*40,00*40

SamNoteType	dc.w	0

ShowSamNote
	TST.W	SamScrEnable
	BEQ	Return2
	MOVE.L	NoteNamesPtr,A4
	MOVE.W	#237*40+36,TextOffset
	MOVE.W	SampleNote,D0
	LSL.W	#2,D0
	LEA	(A4,D0.W),A0
	MOVE.L	A0,ShowTextPtr
	MOVE.W	#4,TextLength
	BRA	ShowText

ShowResamNote
	TST.W	SamScrEnable
	BEQ	Return2
	MOVE.L	NoteNamesPtr,A4
	MOVE.W	#248*40+36,TextOffset
	MOVE.W	ResampleNote,D0
	LSL.W	#2,D0
	LEA	(A4,D0.W),A0
	MOVE.L	A0,ShowTextPtr
	MOVE.W	#4,TextLength
	BRA	ShowText

;---- Graphics Data ----

FontSize	= 568
SpectrumAnaSize	= 808
ScopeSize	= 1814
SureBoxSize	= 1100
ClearBoxSize	= 1100
FormatBoxSize	= 1412
DirScreenSize	= 2861
PLSTSize	= 1728-8
Edit1Size	= 1748
Edit2Size	= 1902
Edit3Size	= 2145
SetupScreenSize	= 5468
Setup2Size	= 5074
PresetEdSize	= 5161
SampScreenSize  = 3843
MonitorSize	= 817
HelpScreenSize	= 900

	INCDIR	"df0:"
FontData	INCBIN "raw/ptfont.raw"
	cnop 0,2
SpectrumAnaData	INCBIN "pak/ptspectrumana.pak"
	cnop 0,2
ScopeData	INCBIN "pak/ptscope.pak"
	cnop 0,2
SureBoxData	INCBIN "raw/ptsurebox.raw"
	cnop 0,2
ClearBoxData	INCBIN "raw/ptclearbox.raw"
	cnop 0,2
FormatBoxData	INCBIN "raw/ptformatbox.raw"
	cnop 0,2
DirScreenData	INCBIN "pak/ptfilereq.pak"
	cnop 0,2
PLSTData	INCBIN "pak/ptplst.pak"
	cnop 0,2
Edit1Data	INCBIN "pak/ptedit1.pak"
	cnop 0,2
Edit2Data	INCBIN "pak/ptedit2.pak"
	cnop 0,2
Edit3Data	INCBIN "pak/ptedit3.pak"
	cnop 0,2
SetupScreenData	INCBIN "pak/ptsetup.pak"
	cnop 0,2
Setup2Data	INCBIN "pak/ptsetup2.pak"
	cnop 0,2
PresetEdData	INCBIN "pak/ptpreseted.pak"
	cnop 0,2
SampScreenData	INCBIN "pak/ptsampler.pak"
	cnop 0,2
MonitorData	INCBIN "pak/ptmonitor.pak"
	cnop 0,2
HelpScreenData	INCBIN "pak/pthelpscreen.pak"
	cnop 0,2
VolBoxData	INCBIN "raw/ptvolbox.raw"
ToggleONdata	INCBIN "raw/PTToggleON.raw"
ToggleOFFdata	INCBIN "raw/PTToggleOFF.raw"
S_BoxData	INCBIN "raw/PTLetters.raw"

A_BoxData	EQU	S_BoxData+22
T_BoxData	EQU	S_BoxData+44
P_BoxData	EQU	S_BoxData+66
H_BoxData	EQU	S_BoxData+88
C_BoxData	EQU	S_BoxData+110


;************************** CHIP DATA HUNK **************************

	SECTION ptdata,DATA_C

BitplaneData	INCBIN "raw/PTMainScreen.raw"

TopMenusPos	EQU	BitPlaneData+55
SpectrumAnaPos	EQU	BitPlaneData+1815
FormatBoxPos	EQU	BitPlaneData+2090
SureBoxPos	EQU	BitPlaneData+2100
SamScrPos	EQU	BitPlaneData+4880
VolBoxPos	EQU	BitPlaneData+6209

TToneData	; Tuning Tone (Sine Wave)
	dc.b    0,  25,  49,  71,  91, 106, 118, 126
	dc.b  127, 126, 118, 106,  91,  71,  49,  25
	dc.b    0, -25, -49, -71, -91,-106,-118,-126
	dc.b -127,-126,-118,-106, -91, -71, -49, -25

CopList	dc.w	$0120	; Sprite 0 high
	dc.w	0
	dc.w	$0122	; Sprite 0 low
	dc.w	0
	dc.w	$0124	; Sprite 1 high
	dc.w	0
	dc.w	$0126	; Sprite 1 low
	dc.w	0
	dc.w	$0128	; Sprite 2 high
	dc.w	0
	dc.w	$012A	; Sprite 2 low
	dc.w	0
	dc.w	$012C	; Sprite 3 high
	dc.w	0
	dc.w	$012E	; Sprite 3 low
	dc.w	0
	dc.w	$0130	; Sprite 4 high
	dc.w	0
	dc.w	$0132	; Sprite 4 low
	dc.w	0
	dc.w	$0134	; Sprite 5 high
	dc.w	0
	dc.w	$0136	; Sprite 5 low
	dc.w	0
	dc.w	$0138	; Sprite 6 high
	dc.w	0
	dc.w	$013A	; Sprite 6 low
	dc.w	0
	dc.w	$013C	; Sprite 7 high
	dc.w	0
	dc.w	$013E	; Sprite 7 low
	dc.w	0
CopListBitplanes
	dc.w	$00E0	; Bitplane 0 high
	dc.w	0
	dc.w	$00E2	; Bitplane 0 low
	dc.w	0
	dc.w	$00E4	; Bitplane 1 high
	dc.w	0
	dc.w	$00E6	; Bitplane 1 low
	dc.w	0
	dc.w	$00E8	; Bitplane 2 high
NoteBplptrHigh
	dc.w	0
	dc.w	$00EA	; Bitplane 2 low
NoteBplptrLow
	dc.w	0
CopListColors
	dc.w	$01A2	; Color 17
	dc.w	$0AAA
	dc.w	$01A4	; Color 18
	dc.w	$0888
	dc.w	$01A6	; Color 19
	dc.w	$0666
	dc.w	$01A0	; Color 16
	dc.w	0
	dc.w	$0180	; Color 0
CopCol0	dc.w	0
	dc.w	$0182	; Color 1
	dc.w	$0AAA
	dc.w	$0184	; Color 2
	dc.w	$0777
	dc.w	$0186	; Color 3
	dc.w	$0444
	dc.w	$0188	; Color 4
	dc.w	$0CCC
	dc.w	$018A	; Color 5
	dc.w	$0A00
	dc.w	$018C	; Color 6
	dc.w	$0000
	dc.w	$018E	; Color 7
	dc.w	$004D
	dc.w	$01AE	; Color 23
	dc.w	$0A00
	dc.w	$01AA	; Color 21
	dc.w	$0700
	dc.w	$01AC	; Color 22
	dc.w	$0400
	dc.w	$008E	; Display Window Start
	dc.w	$2C81	; 2C / 81
	dc.w	$0090	; Display Window Stop
	dc.w	$2CC1	; 2C / C1
	dc.w	$0092	; Display Data Fetch Start
	dc.w	$0038
	dc.w	$0094	; Display Data Fetch Stop
	dc.w	$00D0
	dc.w	$0108
	dc.w	0
	dc.w	$010A
	dc.w	0
	dc.w	$0102	; bplcon1
	dc.w	0
	dc.w	$0104	; bplcon2
	dc.w	$0024
	dc.w	$2C07	; Wait for line $2C, pos $07
	dc.w	$FFFE
	dc.w	$0100	; bplcon0
	dc.w	$3200
CopListAnalyzer
	dcb.w	320,0
CopListInsPos
	dc.w	$B807	; Wait for line $88, pos $07
	dc.w	$FFFE
	dc.w	$0100	; bplcon0
	dc.w	$2200
CopListBpl4
	dc.w	$00E8	; Bitplane 4 high
	dc.w	0
	dc.w	$00EA	; Bitplane 4 low
	dc.w	0
	dc.w	$0188	; Color 4
NoteCol	dc.w	$004D
	dc.w	$B907	; Wait for line $89, pos $07
	dc.w	$FFFE
	dc.w	$0100	; bplcon0
	dc.w	$3200
CopListMark2	
	dcb.w	672,0

	dc.w	$E907	; Wait for line $E9, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	$FFD8
	dc.w	$0108	; bpl1mod
	dc.w	$FFD8
	dc.w	$EA07	; Wait for line $EA, pos $07
	dc.w	$FFFE
	dc.w	$0104	; bplcon2
	dc.w	$0010
	dc.w	$010A	; bpl2mod
	dc.w	0
	dc.w	$0108	; bpl1mod
	dc.w	0
	dc.w	$EB07	; Wait for line $E8, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	$FFD8
	dc.w	$0108	; bpl1mod
	dc.w	$FFD8
	dc.w	$EC07	; Wait for line $EC, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	0
	dc.w	$0108	; bpl1mod
	dc.w	0
	dc.w	$ED07	; Wait for line $ED, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	$FFD8
	dc.w	$0108	; bpl1mod
	dc.w	$FFD8
	dc.w	$EE07	; Wait for line $EE, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	0
	dc.w	$0108	; bpl1mod
	dc.w	0
	dc.w	$EF07	; Wait for line $EF, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	$FFD8
	dc.w	$0108	; bpl1mod
	dc.w	$FFD8
	dc.w	$F007	; Wait for line $F0, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	0
	dc.w	$0108	; bpl1mod
	dc.w	0
	dc.w	$F107	; Wait for line $F1, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	$FFD8
	dc.w	$0108	; bpl1mod
	dc.w	$FFD8
	dc.w	$F207	; Wait for line $F2, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	0
	dc.w	$0108	; bpl1mod
	dc.w	0
	dc.w	$F307	; Wait for line $F3, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	$FFD8
	dc.w	$0108	; bpl1mod
	dc.w	$FFD8
	dc.w	$F407	; Wait for line $F4, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	0
	dc.w	$0108	; bpl1mod
	dc.w	0
	dc.w	$F507	; Wait for line $F5, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	$FFD8
	dc.w	$0108	; bpl1mod
	dc.w	$FFD8
	dc.w	$F607	; Wait for line $F6, pos $07
	dc.w	$FFFE
	dc.w	$010A	; bpl2mod
	dc.w	0
	dc.w	$0108	; bpl1mod
	dc.w	0
	dc.w	$01B2	; Color 1
	dc.w	0
	dc.w	$01B4	; Color 2
	dc.w	0
	dc.w	$01B6	; Color 3
	dc.w	0
	dc.w	$01BA	; Color 5
	dc.w	0
	dc.w	$01BC	; Color 6
	dc.w	0
	dc.w	$01BE	; Color 7
	dc.w	0
	dc.w	$FFDF	; Wait for line $FF, pos $DF
	dc.w	$FFFE
	dc.w	$2907	; Wait for line $29, pos $07
	dc.w	$FFFE
	dc.w	$0100	; bplcon0
	dc.w	$2200
	dc.w	$2C07	; Wait for line $2C, pos $07
	dc.w	$FFFE
	dc.w	$0100	; bplcon0
	dc.w	$0200
	dc.w	$FFFF	; Wait for line $FF, pos $FF
	dc.w	$FFFE	; End of copperlist


CopList2
	dc.w	$B607	; Wait for line $B6, pos $07
	dc.w	$FFFE
	dc.w	$0100	; bplcon0
	dc.w	$2200
CopList2Bpl4Ptr
	dc.w	$00E8	; Bitplane 4 high
	dc.w	0
	dc.w	$00EA	; Bitplane 4 low
	dc.w	0
	dc.w	$0188	; Color 4
	dc.w	$004D
	dc.w	$01B2	; LoopSprite color
	dc.w	$00F0
	dc.w	$01BA	; PlayPos Sprite color
	dc.w	$00FF
	dc.w	$B707	; Wait for line $B7, pos $07
	dc.w	$FFFE
	dc.w	$0100	; bplcon0
	dc.w	$3200
	dc.w	$FFDF	; Wait for line $FF, pos $DF
	dc.w	$FFFE
	dc.w	$2907	; Wait for line $29, pos $07
	dc.w	$FFFE
	dc.w	$0100	; bplcon0
	dc.w	$2200
	dc.w	$2C07	; Wait for line $2C, pos $07
	dc.w	$FFFE
	dc.w	$0100	; bplcon0
	dc.w	$0200
	dc.w	$FFFF	; Wait for line $FF, pos $FF
	dc.w	$FFFE	; End of copperlist


PointerSpriteData
	dc.w	0,0
	dc.w	$FFFF,$FFFF,$8002,$FFFE,$BFF4,$C00C,$BFE8,$C018
	dc.w	$BFD0,$C030,$BFE8,$C018,$BFF4,$C00C,$BFFA,$C006
	dc.w	$BFFD,$C003,$BFFA,$C006,$B7F4,$C80C,$ABE8,$DC18
	dc.w	$95D0,$F630,$A2A0,$E360,$C140,$C1C0,$8080,$8080
NoSpriteData
	dc.w	0,0,0,0
CursorSpriteData
	dc.w	0,0
	dc.w	$FFE0,$FFE0,$FFE0,$FFE0,$8020,$0000,$8020,$0000
	dc.w	$8020,$0000,$8020,$0000,$8020,$0000,$8020,$0000
	dc.w	$8020,$0000,$8020,$0000,$8020,$0000,$8020,$0000
	dc.w	$0000,$FFE0,$0000,$FFE0
	dc.w	0,0
LineCurSpriteData
	dc.w	0,0,$0000,$FE00,$0000,$FE00,$0000,$0000,0,0
VUSpriteData1
	dc.w	$E95B,$EA01
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	0,0
VUSpriteData2
	dc.w	$E97F,$EA01
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	0,0
VUSpriteData3
	dc.w	$E9A3,$EA01
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	0,0
VUSpriteData4
	dc.w	$E9C7,$EA01
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0,$C0C0,$3FC0
	dc.w	0,0
LoopSpriteData1
	dc.w 0,0
	dc.w $F000,0,$F000,0,$F000,0,$F000,0,$1000,0,$1000,0,$1000,0,$1000,0
	dc.w $1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0
	dc.w $1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0
	dc.w $1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$7000,0
	dc.w $1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0
	dc.w $1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0
	dc.w $1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0
	dc.w $1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0,$1000,0
	dc.w 0,0
LoopSpriteData2
	dc.w 0,0
	dc.w $F000,0,$F000,0,$F000,0,$F000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$E000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w 0,0
PlayPosSpriteData
	dc.w 0,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w $8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0,$8000,0
	dc.w 0,0

;******** BSS HUNK ********

	SECTION ptbss,BSS

FileInfoBlock	ds.b	256 ;,0
FIB_EntryType	EQU	FileInfoBlock+4
FIB_FileName	EQU	FileInfoBlock+8
FIB_FileSize	EQU	FileInfoBlock+124
FIB_DateStamp	EQU	FileInfoBlock+132

InfoData	ds.b	36
TopMenusBuffer	ds.b	2200
TextDataBuffer	ds.b	546  
TrackBuffer	ds.b	256
CmdsBuffer	ds.b	256
BlockBuffer	ds.b	256
PatternBuffer	ds.b	1024
UndoBuffer	ds.b	1024

TrackdiskIOExtTD
		ds.w	1
		ds.l	3
TDPortPtr	ds.l	1
		ds.w	1
Fmt_tja		ds.w	1
		ds.l	2
		ds.b	3
Fmt_hmmm	ds.b	1
		ds.w	1
FmtDataSize	ds.w	2
FmtDataPtr	ds.w	2
FmtDiskOffset	ds.l	9

SplitTransTable	ds.b	38
SplitInstrTable	ds.b	38

ModulesPath2	ds.b	64
SongsPath2	ds.b	64
SamplePath2	ds.b	64

		ds.l	1

SetupData	ds.b	26
SongsPath	ds.b	32
ModulesPath	ds.b	32
SamplePath	ds.b	32
PrintPath	ds.b	32
ColorTable	ds.w	8
PackMode	ds.b	1
ModPackMode	ds.b	1
SplitFlag	ds.b	1
FilterFlag	ds.b	1
TransDelFlag	ds.b	1
ShowDecFlag	ds.b	1
AutoDirFlag	ds.b	1
AutoExitFlag	ds.b	1
ModOnlyFlag	ds.b	1
MIDIFlag	ds.b	1
SplitData	ds.b	4
		ds.b	4
		ds.b	4
		ds.b	4
KeyRepDelay	ds.w	1
KeyRepSpeed	ds.w	1
Accidental	ds.b	1
		ds.b	1
ExtCommands	ds.b	32*8 ; 256 bytes
MaxDirEntries	ds.w	1
MaxPLSTEntries	ds.w	1
MultiModeNext	ds.b	4
EffectMacros	ds.w	10
RAWIFFMode	ds.b	1
IntMode		ds.b	1
OverrideFlag	ds.b	1
NosamplesFlag	ds.b	1
BlankZeroFlag	ds.b	1
ShowDirsFlag	ds.b	1
ShowPublicFlag	ds.b	1
CutToBufFlag	ds.b	1
IFFLoopFlag	ds.b	1
HHFlag		ds.b	1
Tempo		ds.w	1
DMAWait		ds.w	1
TuneNote	ds.w	1
TToneVol	ds.w	1
SalvageAddress	ds.b	8
PTPath		ds.b	32  ; 512 bytes
DefaultSpeed	ds.w	1
VUmeterColors	ds.w	48
AnalyzerColors	ds.w	40
		ds.b	302 ; Pad to 1024 bytes

HelpTextIndex	ds.l	256
HelpTextData	ds.b	656

InpEvPort	ds.b	34
InpEvIOReq	ds.b	48

ScopeInfo	ds.b	20*4
ScopeSamInfo	ds.b	16*31
BlankSample	ds.b	320
FileName	ds.b	96
NewFileName	ds.b	96
SampleFileName	ds.b	28
PresetNameText	ds.b	22
DirInputName	ds.b	30
NewInputName	ds.b	30
TempSampArea	ds.w	380
SaveColors	ds.w	40+48

END

; /* End of File */
