                .Model Tiny
		Smart
                Stack 100h

                .DATA

SongLength      EQU     0A13Bh+8
SongLoopStart   EQU     015Ch+8
NotePos		dw	12
DelayCt		db	50

SongSeg		Segment	Public

Include         PlayMus1.inc

SongSeg		EndS

                .CODE

OutAdlib	Proc	Near
		Push	ax
		Push	dx
		Mov	dx, 388h
		Xchg	al, ah
		Out	dx, al
		Inc	dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		Mov	al, ah
		Out	dx, al
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
		In	al, dx
                Pop     dx
                Pop     ax
                Ret
OutAdlib        EndP

InitAdlib       Proc    Near
		Mov	cx, 0F0h
		Mov	ax, 1000h
Loop_1:
                Call	OutAdlib
		Inc	ah
		Loop	Loop_1


		Mov	cx, 20h
		Mov	ax, 403Fh
Loop_2:
                Call	OutAdlib
		Inc	ah
		Loop	Loop_2


		Mov	cx, 0A0h
		Mov	ax, 6000h
Loop_3:
                Call	OutAdlib
		Inc	ah
		Loop	Loop_3


		Mov	ax, 120h
		Call	OutAdlib


		Mov	ax, 0800h
		Call	OutAdlib


		Mov	ax, 0BD00h
		Call	OutAdlib

		Mov	cx, 9
		Xor	di, di

Loop_4:
                Push	cx
		Xor	ax, ax
		Mov	bx, di
		Mov	bh, ah
		Mov	ah, 0A0h
		Add	ah, bl
		Call	OutAdlib

		Mov	al, bh
		Add	ah, 10h
		Call	OutAdlib

		Inc	di
		Pop	cx
		Loop	Loop_4

		Ret
InitAdlib       EndP

PlayNote        Proc	Near
		Push	ax
		Push	bx
		Mov	ax, SongSeg
		Mov	ES, ax
		Dec	DelayCt
		Cmp	DelayCt, 0
		Jne	DelayNOTDone
		Mov	bx, NotePos
NextCommand:
		Mov	ax, ES:[bx]
		Add	bx, 2
		Cmp	bx, SongLength
		Jae	SongDone
		Cmp	ah, 0
		Je	RowDone
		Call	OutAdlib
		Jmp	NextCommand
RowDone:
		Mov	DelayCt, al
		Mov	NotePos, bx
DelayNOTDone:
		Pop	bx
		Pop	ax
		Ret
SongDone:
		Mov	bx, SongLoopStart
		Jmp	NextCommand
PlayNote	EndP


WaitRetrace	Proc	Near
		Mov	dx, 3DAh
NoRetrace:
		In	al, dx
		Test	al, 8
		Jz	NoRetrace

TestRetrace:
		In	al, dx
		test	al, 8
		jnz	TestRetrace

		Ret
WaitRetrace	EndP

Init		Proc	Near
		Mov	ax, @DATA
		Mov	DS, ax
		Call    InitAdlib
NoKeyPressed:
                Call    PlayNote

		Call	WaitRetrace

                Mov     ah, 01
                Int     16h
                Jz      NoKeyPressed

		Mov	ah, 00
		Int	16h
		Cmp	al, 27
		Jne	NoKeyPressed

		Call	InitAdlib

                Mov     ax, 4C00h
                Int     21h
Init            EndP
End Init