;
;**************************************
;ENGINE pro hrani hudby v preruseni na ATMELu
;**************************************
;+ nekolik pisni
;vystup pres bit p3.1 - v klidu 1
;Krystal 12 MHz !!!
;
$MOD51
$TITLE(BYTE SIGNED MULTIPLY)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;
hi_snd	equ	30h		;adresa predvolby pro citac - vyska tonu
lo_snd	equ	hi_snd+1
length	equ	hi_snd+2	;delka aktualniho tonu
hi_len	equ	0e0h
lo_len	equ	00h
hi_dptr	equ	hi_snd+3
lo_dptr	equ	hi_snd+4
;
	org	0
	ljmp	start
;-------------------------------
	org	0bh		;preruseni od casovace 0 - vyska tonu
	ljmp	ton		;skok na podprogram

	org	1bh		;preruseni od casovace 1 - delka tonu
	ljmp	delka		;skok na podprogram

N	EQU	255
CN	EQU	0
CN_	EQU	1
DN	EQU	2
DN_	EQU	3
EN	EQU	4
FN	EQU	5
FN_	EQU	6
GN	EQU	7
GN_	EQU	8
AN	EQU	9
AN_	EQU	10
BN	EQU	10
HN	EQU	11
C1	EQU	12
C1_	EQU	13
D1	EQU	14
D1_	EQU	15
E1	EQU	16
F1	EQU	17
F1_	EQU	18
G1	EQU	19
G1_	EQU	20
A1	EQU	21
A1_	EQU	22
B1	EQU	22
H1	EQU	23
C2	EQU	24
C2_	EQU	25
D2	EQU	26
D2_	EQU	27
E2	EQU	28
F2	EQU	29
F2_	EQU	30
G2	EQU	31
G2_	EQU	32
A2	EQU	33
A2_	EQU	34
B2	EQU	34
H2	EQU	35
C3	EQU	36
C3_	EQU	37
E3	EQU	38
;-------------------------------


start:	
	mov	dptr,#hudba0	;adresa tabulky not 1.hudby
	acall	hraj
	mov	dptr,#hudba	;adresa tabulky not 1.hudby
	acall	hraj
	mov	dptr,#hudba1	;adresa tabulky not 2.hudby
	acall	hraj
	mov	dptr,#hudba2	;adresa tabulky not 3.hudby
	acall	hraj
	mov	dptr,#hudba3	;adresa tabulky not 4.hudby
	acall	hraj
	mov	dptr,#hudba4	;adresa tabulky not 5.hudby
	acall	hraj
	mov	dptr,#hudba5	;adresa tabulky not 6.hudby
	acall	hraj

	sjmp	start

hraj:	;Zahraje hudbu a chvili pocka
	acall	hudba_spust	;spusteni hudby
	jb	tr1,$		;cekej na konec pisne
	mov	dptr,#ticho	;adresa tabulky pauzy
	acall	hudba_spust	;spusteni hudby
	jb	tr1,$		;cekej na konec pisne
	ret
	
hudba0:	;Ivan z Mrazika
	DB	C1,25,E1,25,G1,50,E1,25,F1,25,A1,25,G1,50,N,25
	DB	A1,25,F1,25,E1,50,G1,50,F1,25,D1,25,C1,75,N,10
	DB	C1,25,E1,25,G1,50,C2,25,N,5,C2,25,H1,25,A1,50,N,25
	DB	C2,25,A1,25,G1,50,C2,50,F1,25,D1,25,C1,50,N,25
	DB	F1,25,A1,25,C2,50,H1,25,G1,25,E1,50
	DB	F1,25,A1,25,D2,50,C2,25,H1,25,A1,25,G1,50,C2,50,N,10
	DB	C2,25,A1,25,N,5,A1,50,C2,25,G1,25,N,5,G1,50,A1,25
	DB	C2,25,G1,50,F1,25,E1,25,D1,25,C1,50

	DB	0,0

hudba:	;Tak to chodi - Scott Joplin
	DB	H2,25,C3_,25,A2,25,F2_,50,G2_,25,E2,50
	DB	H1,25,C2_,25,A1,25,F1_,50,G1_,25,E1,50
	DB	HN,25,C1_,25,AN,25,FN_,50,GN_,25,FN_,25,FN,25,EN,50,N,50,E2,50

	DB	H1,25,C2,25,C2_,25,A2,50,C2_,25,A2,50,C2_,25,A2,125,D2,50
	DB	A2,25,H2,25,C3,25,C3_,25,A2,25,H2,25,C3_,50,G2_,25,H2,50,A2,50,N,50
	DB	H1,25,C2,25,C2_,25,A2,50,C2_,25,A2,50,C2_,25,A2,125,D2,50
	DB	F2_,25,E2,25,D2_,25,F2_,25,A2,25,C3_,50,H2,25,A2,25,F2_,25,H2,100,N,50
	DB	H1,25,C2,25,C2_,25,A2,50,C2_,25,A2,50,C2_,25,A2,125,D2,50
	DB	A2,25,H2,25,C3,25,C3_,25,A2,25,H2,25,C3_,50,G2_,25,H2,50,A2,50,N,50
	DB	A2,25,H2,25,C3_,25,A2,25,H2,25,C3_,50,A2,25,H2,25,A2,25
	DB	C3_,25,A2,25,H2,25,C3_,50,A2,25,H2,25,C3,25
	DB	C3_,25,A2,25,H2,25,C3_,50,G2_,25,H2,50,A2,50,N,100

	DB	C2_,25,D2,25,D2_,25,E2,50,F2_,25,E2,25,N,25
	DB	C2_,25,D2,25,D2_,25,E2,50,F2_,25,E2,25,N,25
	DB	C2_,25,A1,25,E1,25,F1_,25,G1_,25,A1,25,H1,25,C2_,25,H1,25,A1,25,H1,25
	DB	E1,100,N,25
	DB	C2_,25,D2,25,D2_,25,E2,50,F2_,25,E2,25,N,25
	DB	C2_,25,D2,25,D2_,25,E2,50,F2_,25,E2,25,N,25
	DB	E2,25,F2,25,F2_,25,G2_,25,N,4,G2_,75,N,4,G2_,25,F2_,25,D2_,25,H1,25
	DB	E2,100,N,25
	DB	C2_,25,D2,25,D2_,25,E2,50,F2_,25,E2,25,N,25
	DB	C2_,25,D2,25,D2_,25,E2,50,F2_,25,E2,25,N,25
	DB	C2_,25,A1,25,E1,25,F1_,25,G1_,25,A1,25,H1,25,C2_,25,H1,25,A1,25,H1,25
	DB	E1,100,N,25
	DB	A2,50,F2_,25,A2,50,F2_,25,A2,25,F2_,25,E2,25,A2,25,C3_,25,E3,50,C3_,25
	DB	A2,25,E2,25,F2_,50,G2_,50,C3_,25,H2,50,A2,100,N,50
	DB	A2,50,F2_,25,A2,50,F2_,25,A2,25,F2_,25,E2,25,A2,25,C3_,25,E3,50,C3_,25
	DB	A2,25,E2,25,F2_,50,G2_,50,C3_,25,H2,50,A2,100
	db	0,0


hudba1:	;Ukolebavka
	db	E1,70,F1,70,E1,70,D1,70,C1,70,D1,70,C1,100,N,50
	DB	C1,70,F1,70,N,5,F1,70,N,5,F1,70,G1,70,A1,70,G1,100,N,50
	DB	D1,70,N,5,D1,70,N,5,D1,70,N,5,D1,70,C1_,70,D1,70,F1,100,N,50
	DB	E1,70,N,5,E1,70,N,5,E1,70,F1,70,E1,70,F1,70,G1,100,N,50
	DB	A1,70,N,5,A1,70,N,5,A1,70,N,5,A1,70,G1_,70,A1,70,C2,100,N,50
	DB	G1,70,N,5,G1,70,N,5,G1,70,N,5,G1,70,F1_,70,G1,70,C2,100,N,50
	db	E1,70,F1,70,E1,70,D1,70,C1,70,D1,70,C1,200
	DB	0,0

hudba2:	;Menuet J.S. Bach
	db	C2,50,F1,25,G1,25,A1,25,B1,25,C2,50,F1,50,N,5,F1,50
	db	D2,50,B1,25,C2,25,D2,25,E2,25,F2,50,F1,50,N,5,F1,50
	DB	B1,50,C2,25,B1,25,A1,25,G1,25,A1,50,B1,25,A1,25,G1,25,F1,25
	DB	E1,50,F1,25,G1,25,A1,25,F1,25,A1,100,G1,50
	db	C2,50,F1,25,G1,25,A1,25,B1,25,C2,50,F1,50,N,5,F1,50
	db	D2,50,B1,25,C2,25,D2,25,E2,25,F2,50,F1,50,N,5,F1,50
	DB	B1,50,C2,25,B1,25,A1,25,G1,25,A1,50,B1,25,A1,25,G1,25,F1,25
	DB	G1,50,A1,25,G1,25,F1,25,E1,25,F1,150
	DB	A2,50,F2,25,G2,25,A2,25,F2,25,G2,50,C2,25,D2,25,E2,25,C2,25
	DB	F2,50,D2,25,E2,25,F2,25,C2,25,H1,50,A1,25,H1,25,G1,50,N,5
	DB	G1,25,A1,25,H1,25,C2,25,D2,25,E2,25,F2,50,E2,50,D2,50
	DB	E2,50,G1,50,H1,50,C2,150,N,10,C2,50,F1,25,E1,25,F1,50
	DB	D2,50,F1,25,E1,25,F1,50,C2,50,B1,50,A1,50,G1,25,F1,25,E1,25
	DB	F1,25,G1,50,C1,25,D1,25,E1,25,F1,25,G1,25,A1,25,B1,50,A1,50,G1,50
	DB	A1,25,C2,25,F1,50,E1,50,F1,150

	DB	A2,50,F2,25,G2,25,A2,25,F2,25,G2,50,C2,25,D2,25,E2,25,C2,25
	DB	F2,50,D2,25,E2,25,F2,25,C2,25,H1,50,A1,25,H1,25,G1,50,N,5
	DB	G1,25,A1,25,H1,25,C2,25,D2,25,E2,25,F2,50,E2,50,D2,50
	DB	E2,50,G1,50,H1,50,C2,150,N,10,C2,50,F1,25,E1,25,F1,50
	DB	D2,50,F1,25,E1,25,F1,50,C2,50,B1,50,A1,50,G1,25,F1,25,E1,25
	DB	F1,25,G1,50,C1,25,D1,25,E1,25,F1,25,G1,25,A1,25,B1,50,A1,50,G1,50
	DB	A1,25,C2,25,F1,50,E1,50,F1,150
	DB	0,0

hudba3:	;Serenada - J. Haydn
	db	H1,75,C2,25,D2,50,H1,50,G1,200,D2,50,H1,50,E2,50,C2,50,G1,200
	DB	N,5,C2,50,E2,50,N,5,E2,50,D2,50,C2,50,H1,50,N,5,H1,50,A1,50
	DB	G1,50,F1_,50,N,5,F1_,50,G1,50,D1,200,N,5,D2,75,N,5,D2,25
	DB	N,5,D2,50,E2,25,D2,25,C2,50,H1,50,N,5,H1,50,A1,50,G1,50,F1_,50
	db	N,5,F1_,100,G1,100,N,50
	db	H1,75,C2,25,D2,50,H1,50,G1,200,D2,50,H1,50,E2,50,C2,50,G1,200
	DB	N,5,C2,50,E2,50,N,5,E2,50,D2,50,C2,50,H1,50,N,5,H1,50,A1,50
	DB	G1,50,F1_,50,A1,25,G1,25,F1_,25,G1,25,D1,150,D2,75,N,10,D2,25
	DB	N,5,D2,50,E2,25,D2,25,C2,50,H1,50,N,5,H1,50,A1,50,G1,50,F1_,50
	db	N,5,F1_,100,G1,100,N,50
	db	H1,75,C2,25,E2,50,D2,50,C2,50,H1,50,D2,50,C2,50,H1,50,A1,50
	DB	H1,50,G2,50,N,5,G2,150,N,10
	db	H1,75,C2,25,E2,50,D2,50,C2,50,H1,50,C2,50,A1,50,G1,50,F1_,50
	DB	G1,150,H1,50,D2,50,C2,50,A1,50,F1_,50,G1,150,H1,25,D2,25
	DB	C2,50,A1,50,G1,50,F1_,50,N,5,F1_,200,G1,100
	db	0,0

hudba4:	;Menuet - Leopold Mozart
	db	E1,25,F1,25,A1,25,F1,25,B1,25,F1,25,E1,25,F1,25,C2,25,F1,25,D2,25,F1,25
	DB	E2,25,C2,25,F2,25,C2,25,G2,25,C2,25,A2,25,C2,25,G2,25,C2,25,F2,50,N,10
	db	E1,25,F1,25,A1,25,F1,25,B1,25,F1,25,E1,25,F1,25,C2,25,F1,25,D2,25,F1,25
	DB	E2,25,C2,25,F2,25,C2,25,G2,50,A2,125,N,20
	DB	A1,25,C2,25,F1,25,A1,25,G1,25,F1,25,G1,25,C2,25,E1,25,G1,25,C1,50
	DB	F1,25,E1,25,D1,25,G1,25,F1,25,G1,25,E1,25,D1,25,C1,50,N,50
	DB	H1,25,C2,25,E2,25,C2,25,F2,25,C2,25,H1,25,C2,25,G2,25,C2,25,A2,25,C2,25
	DB	E2,50,F2,25,E2,25,D2,25,E2,25,C2,125,N,20
	db	E1,25,F1,25,A1,25,F1,25,B1,25,F1,25,E1,25,F1,25,C2,25,F1,25,D2,25,F1,25
	DB	E2,25,C2,25,F2,25,C2,25,G2,25,C2,25,A2,25,C2,25,G2,25,C2,25,F2,50,N,10
	db	E1,25,F1,25,A1,25,F1,25,B1,25,F1,25,E1,25,F1,25,C2,25,F1,25,D2,25,F1,25
	DB	E2,25,F2,25,C2,50,E2,50,F2,125
	DB	0,0

hudba5:
	DB	F1,50,G1,50,A1,50,B1,100,N,10,B1,50,C2,100,N,10,C2,50,D2,75,C2,25
	DB	D2,25,D2_,50,D2,50,C2,100,B1,100,N,100
	DB	F1,50,G1,50,A1,50,B1,100,N,10,B1,50,C2,100,N,10,C2,50,D2,75,C2,25
	DB	D2,25,D2_,50,D2,50,C2,100,B1,100,N,100
	DB	D2,50,N,10,D2,50,C2,50,D2,100,C2,50,D2,50,B1,100
	DB	C2,100,B1,50,A1,50,G1,100,F1,100,N,100
	DB	D2,50,N,10,D2,50,C2,50,D2,100,C2,50,D2,50,B1,100
	DB	C2,75,D2,25,D2_,50,D2,50,C2,100,B1,150	
	DB	0,0

ticho:	db	N,255,N,255
	DB	0,0

;------------------------------------------
;Podprogramy pro tvoreni zvuku
hudba_spust:
	mov	hi_dptr,dph
	mov	lo_dptr,dpl
	mov	TMOD,#99h	;citac 0 - rezim 1, citac 1 - rezim 1
	mov	IE,#8Ah
	mov	hi_snd,#0
	mov	lo_snd,#0
	mov	length,#1
	setb	tr0		;spust casovac 0
	setb	tr1		;spust casovac 1
	ret
;-----------------------

ton:	push	PSW		;uschovej priznaky
	mov	th0,hi_snd	;nova predvolba
	mov	tl0,lo_snd	;nova predvolba
	cpl	p3.1		;invertuj bit - stridej 1 a 0
	pop	PSW		;obnov priznaky
	reti			;vrat se	
;-------------------------------
delka:
	push	acc		;uschovej Acc
	push	dph
	push	dpl
	mov	th1,#hi_len	;nova predvolba
	mov	tl1,#lo_len	;nova predvolba
	dec	length
	mov	a,length	;delka
	jnz	delka_konec

	clr	a
	mov	dph,hi_dptr	;ukazatel na melodii
	mov	dpl,lo_dptr
	movc	a,@a+dptr	;cislo noty
	cjne	a,#255,delka1	;neni pomlka
	setb	p3.1
	clr	tr0
	sjmp	delka2
delka1:
	setb	tr0
delka2:
	mov	hi_snd,a	;docasne ulozi
	clr	a
	inc	dptr
	movc	a,@a+dptr	;delka noty
	jz	konec_hudby	;prestan hrat - delka nula
	mov	length,a	;nova delka
	inc	dptr
	mov	hi_dptr,dph
	mov	lo_dptr,dpl	;novy ukazatel
	mov	a,hi_snd	;vyska noty
	rl	a		;vynasob dvema
	mov	lo_snd,a	;docasne ulozi
	mov	dptr,#tony	;tabulka tonu
	movc	a,@a+dptr	;nacti predvolbu
	mov	hi_snd,a
	mov	a,lo_snd	;vyzvedne
	inc	a
	movc	a,@a+dptr	;nacti predvolbu
	mov	lo_snd,a	;predvolba
	sjmp	delka_konec	;a hraj dal

konec_hudby:
	clr	tr0
	clr	tr1
	setb	p3.1
delka_konec:
	pop	dpl
	pop	dph
	pop	acc		;obnov Acc
	reti			;vrat se	
;////////
;tabulka tonu
tony:
	db	0f2h,0a9h	;C0
	db	0f3h,029h	;C0#
	db	0f3h,0ffh	;D0
	db	0f4h,078h	;D0#
	db	0f5h,054h	;E0
	db	0f5h,0ffh	;F0
	db	0f6h,054h	;F0#
	db	0f6h,0ffh	;G0
	db	0f7h,078h	;G0#
	db	0f7h,0ffh	;A0
	db	0f8h,054h	;A0#
	db	0f8h,0ffh	;H0
	db	0f9h,054h	;C
	db	0f9h,0a9h	;C#
	db	0f9h,0ffh	;D
	db	0fah,054h	;D#
	db	0fah,0a9h	;E
	db	0fah,0ffh	;F
	db	0fbh,035h	;F# PUVODNE 0FBH,0FFH
	db	0fbh,085h	;G
	db	0fbh,0d4h	;G#
	db	0fbh,0ffh	;A
	db	0fch,029h	;A#
	db	0fch,07fh	;H
	db	0fch,0a8h	;C2
	db	0fch,0d4h	;C2#
	db	0fch,0ffh	;D2
	db	0fdh,029h	;D2#
	db	0fdh,054h	;E2
	db	0fdh,07fh	;F2
	db	0fdh,0a8h	;F2#
	db	0fdh,0c4h	;G2
	db	0fdh,0dfh	;G2#
	db	0fdh,0ffh	;A2
	db	0feh,029h	;A2#
	db	0feh,03fh	;H2
	db	0feh,054h	;C3
	db	0feh,070h	;C3#
	DB	0feh,0b0h	;E3
	
        END
