;=======================================================================|
;									|
;		Wander Swan Total Sound Driver				|
;				MML Compiler Program			|
;				Voice Compile Routine			|
;									|
;					Programmed by			|
;						A.Watanabe		|
;									|
;=======================================================================|
c_Voice	proc	near

	;僐儞僷僀儖嶌嬈
	call	c_Voice_Pcm_first	;
	call	c_Voice_Emb_first	;

	ret				;廔椆
c_Voice	endp
;---------------------------------------------------------------|
;		壒怓僐儞僷僀儖					|
;---------------------------------------------------------------|
;	堷悢							|
;		柍偟						|
;	曉傝抣							|
;		柍偟						|
;	巊梡儗僕僗僞						|
;		cx	僐儅儞僪悢僇僂儞僞			|
;		di	c_Voice_Pcm_Adrs億僀儞僞		|
;		si	c_Voice_Pcm_Data億僀儞僞		|
;---------------------------------------------------------------|
c_Voice_Pcm_first	proc	near		;
	pusha				;儗僕僗僞曐懚

;僿僢僟乕弶婜愝掕
	;億僀儞僞乕偺愝掕媦傃丄僩儔僢僋傾僪儗僗偺弶婜壔傪偡傞丅
	xor	bx,bx				;
	mov	cs:[pMML_File],bx		;MML僼傽僀儖偺億僀儞僞傪愭摢偵偡傞丅
	mov	cl,ds:[bx].WTD_Mus_Part		;僷乕僩悢傪撉傒崬傒丅
	.if	(cl > MusicMaxPart)		;僷乕僩悢僠僃僢僋
		mov	cl,MusicMaxPart		;
		mov	ds:[bx].WTD_Mus_Part,cl	;僷乕僩悢傪彂偒崬傒丅
	.endif					;
	mov	bx,0010h			;
	.repeat					;
		sub	cl,1			;僇僂儞僞丒僨傿僋儕儊儞僩
		.break	.if	(carry?)	;
		mov	word ptr ds:[bx],0000h	;僩儔僢僋偺傾僪儗僗偺弶婜壔
		add	bx,2			;億僀儞僞丒僀儞僋儕儊儞僩
	.until	0				;
	mov	cs:[pWTD_File],bx		;WTD僼傽僀儖偺億僀儞僞傪愝掕偡傞丅

	;傾僪儗僗愝掕
	mov	word ptr ds:[0].WTD_Mus_DataAdr,bx

;僐儅儞僪専嶕
;僐儅儞僪悢検偺撉傒崬傒
c_Voice_Pcm_first_Loop1:		;cx仼僐儅儞僪偺悢検
	xor	di,di			;di仼曄姺掕媊傾僪儗僗偺億僀儞僞
	mov	cx,cs:[offset c_Voice_Pcm_Adrs + di]
					;暥帤楍僠僃僢僋偺僇僂儞僞
;億僀儞僞撉傒崬傒
c_Voice_Pcm_first_Loop2:		;
	push	cs:[pMML_File]		;億僀儞僞曐懚
	add	di,2			;億僀儞僞僀儞僋儕儊儞僩(Word)
	mov	si,cs:[offset c_Voice_Pcm_Adrs + di]	;si仼曄姺掕媊傾僪儗僗
;侾暥帤撉傒崬傒
c_Voice_Pcm_first_Loop3:		;
	call	cread			;MML僼傽僀儖偐傜侾暥帤撉傒崬傒
c_Voice_Pcm_first_Step1:		;
;侾暥帤僠僃僢僋乮24h娷傓乯
	mov	al,cs:[si]		;
	inc	si			;
	cmp	al,24h			;暥帤廔椆丠
	jz	c_Voice_Pcm_first_Step2	;側傜偽丄暥帤楍堦抳偱張棟幚峴丅
	cmp	ah,al			;暥帤堦抳丠
	jz	c_Voice_Pcm_first_Loop3	;側傜偽丄師傕僠僃僢僋丅
;暥帤僠僃僢僋偱堦抳偟側偐偭偨応崌丅
	pop	cs:[pMML_File]		;億僀儞僞暅婣
	sub	cx,1			;僇僂儞僞乕丒僨傿僋儕儊儞僩
	jnz	c_Voice_Pcm_first_Loop2	;師偺僐儅儞僪偱斾妑
	inc	CS:[pMML_File]		;億僀儞僞丒僀儞僋儕儊儞僩
	mov	ax,CS:[szMML_File]	;
	cmp	ax,CS:[pMML_File]	;MML僼傽僀儖偺廔椆僠僃僢僋
	jnc	c_Voice_Pcm_first_Loop1	;暥帤楍斾妑奐巒丅
	jmp	c_Voice_Pcm_first_End	;MML僼傽僀儖偑[EOF]側傜偽廔椆
;-----------------------------------------------------------------------
;僐儅儞僪張棟丅
c_Voice_Pcm_first_Step2:			;
	pop	ax			;億僀儞僞偺暅婣(抣偼栠偝偢丅)
	push	ds			;僙僌儊儞僩偺曐懚

;億僀儞僞傪尦偵栠偡丅
	dec	cs:[pMML_File]		;億僀儞僞丒僨傿僋儕儊儞僩

;僐儅儞僪張棟
c_Voice_Pcm_first_Loop4:		;
	mov	al,cs:[si]		;al仼僐儅儞僪
	inc	si			;
	mov	cl,cs:[si]		;
	inc	si			;
	cmp	al,00h			;廔椆
	jnz	c_Voice_Pcm_first_Step3	;
	jmp	c_Voice_Pcm_first_Step99
c_Voice_Pcm_first_Step3:		;
	cmp	al,010h			;4bit 亊 2
	jnz	c_Voice_Pcm_first_Step4	;
c_Voice_Pcm_first_Step3_Loop:		;
	call	A2H8R			;
	and	ah,0fh			;
	mov	al,ah			;
	call	A2H8R			;
	and	ah,0fh			;
	shl	ah,4			;
	or	ah,al			;
	call	cset			;
	sub	cl,1			;
	jnz	c_Voice_Pcm_first_Step3_Loop
	jmp	c_Voice_Pcm_first_Loop4	;
c_Voice_Pcm_first_Step4:		;
	cmp	al,011h			;Byte
	jnz	c_Voice_Pcm_first_Step5	;
c_Voice_Pcm_first_Step4_Loop:		;
	call	A2H8R			;
	call	CSET			;
	sub	cl,1			;
	jnz	c_Voice_Pcm_first_Step4_Loop
	jmp	c_Voice_Pcm_first_Loop4	;
c_Voice_Pcm_first_Step5:		;
	cmp	al,012h			;Word
	jnz	c_Voice_Pcm_first_Step6	;
c_Voice_Pcm_first_Step5_Loop:		;
	call	A2H16R			;
	call	CSET16			;
	sub	cl,1			;
	jnz	c_Voice_Pcm_first_Step5_Loop
	jmp	c_Voice_Pcm_first_Loop4	;
c_Voice_Pcm_first_Step6:		;
	cmp	al,0F0h			;Call (Other Program)
	jnz	c_Voice_Pcm_first_Step7	;

	dec	si			;Count偼柍偄偺偱丄si傪栠偡丅
	mov	dx,cs:[si]		;
	add	si,2			;
	call	dx			;
;	jmp	c_Voice_Pcm_first_Loop4	;
c_Voice_Pcm_first_Step7:		;

;僐儞僷僀儖張棟儖乕僾
	jmp	c_Voice_Pcm_first_Loop4	;

;柦椷僐儞僷僀儖張棟廔椆
c_Voice_Pcm_first_Step99:		;

;僇僂儞僞僀儞僋儕儊儞僩
	inc	byte ptr ds:[0].WTD_Mus_Voice

;栠傞
	pop	ds			;僙僌儊儞僩偺暅婣
	jmp	c_Voice_Pcm_first_Loop1	;暥帤楍斾妑奐巒丅
;-----------------------------------------------------------------------
;廔椆
c_Voice_Pcm_first_End:			;
	popa				;儗僕僗僞暅婣
	ret				;
c_Voice_Pcm_first	endp
;===============================================================|
;			柦椷					|
;===============================================================|
.const
c_Voice_Pcm_Adrs	dw	((offset c_Voice_Pcm_Adrs_E)-(offset c_Voice_Pcm_Adrs_S))/2 + 1
c_Voice_Pcm_Adrs_S	dw	offset cVPD_0		;#Voice
			dw	offset cVPD_1		;@F
c_Voice_Pcm_Adrs_E	dw	offset cVPD_2		;@
;---------------------------------------------------------------|
;	嘆僐儅儞僪暥帤楍					|
;		嵟屻偼24h偱廔傢傞				|
;	嘇弌椡偡傞応強						|
;	    00h	廔傢傝						|
;	    1xh		僨乕僞彂偒崬傒				|
;		x	0:4bit亊2 / 1:Byte / 2:Word		|
;		byte	Count					|
;	    F0h	僾儘僌儔儉					|
;		nnnn	Offset Address				|
;---------------------------------------------------------------
cVPD_0	db	'#voice'    ,24h, 011h,1, 010h,16, 00h
cVPD_1	db	0dh,0ah,'@F',24h, 011h,1, 0F0h	;@偺慜偵偁傞帠両両
	dw	offset c_Voice_Pcm_File
	db	00h
cVPD_2	db	0dh,0ah,'@' ,24h, 011h,1, 010h,16, 00h
.code
;---------------------------------------------------------------|
;仠僼傽僀儖偐傜偺撉傒崬傒		;
c_Voice_Pcm_File	proc	near

	local	cFilename[128]:BYTE	;[BP+80h]

	pusha				;儗僕僗僞曐懚

	call	A2H8R			;壒怓斣崋撉傒崬傒
	push	ax			;斣崋曐懚

	;僼傽僀儖柤暥帤楍愭摢偺専嶕
	.repeat
	    CALL	CREAD_L		;
	.until	(al>20h)		;俀侽枹枮偺僐乕僪偼旘偽偡

	;僼傽僀儖柤暥帤楍傪僺僢僋傾僢僾
	dec	cs:[pMML_File]		;億僀儞僞傪堦偮栠偡丅
	xor	di,di			;
	mov	cx,128			;cl仼僼傽僀儖柤偺嵟戝暥帤悢
	.repeat				;
	   CALL	CREAD_L			;
	   .break   .if	(al == 00dh)	;夵峴偩偭偨傜奿擺廔椆
	   mov	cFilename[di],al	;mov [BP + DI + 80h],ah
	   inc	di			;
	.untilcxz			;
	mov	cFilename[di],0		;
	inc	di			;
	mov	cFilename[di],0dh	;
	inc	di			;
	mov	cFilename[di],0ah	;
	inc	di			;
	mov	cFilename[di],24h	;

	;壒怓偺揮憲
	pop	bx			;壒怓斣崋偺暅婣
	mov	bl,bh			;
	xor	bh,bh			;
	shl	bx,4			;bx 仼 壒怓僼傽僀儖偺僆僼僙僢僩

	;僼傽僀儖偺撉傒崬傒
	;壒怓僼傽僀儖梡儊儌儕僆乕僾儞
	push	es			;
	invoke	Memory_Open,	0800h	;
	mov	es,ax			;es 仼 壒怓僼傽僀儖
	invoke	File_Open,	addr ss:cFilename,0
	push	ax			;僴儞僪儖曐懚
	invoke	File_Load,	ax,addr es:0
	pop	ax			;僴儞僪儖暅婣
	invoke	File_Close,	ax	;僼傽僀儖僋儘乕僘

	mov	cx,16			;cx 仼 揮憲悢
	.repeat
		mov	ah,es:[bx]	;
		inc	bx		;
		call	CSET		;
	.untilcxz

	invoke	Memory_Close,	es	;儊儌儕偺夝曻
	pop	es			;

	dec	cs:[pMML_File]		;億僀儞僞侾偮栠偡丅

	popa				;儗僕僗僞暅婣
	ret				;
c_Voice_Pcm_File	endp
;---------------------------------------------------------------|
;		壒怓僐儞僷僀儖					|
;---------------------------------------------------------------|
;	堷悢							|
;		柍偟						|
;	曉傝抣							|
;		柍偟						|
;	巊梡儗僕僗僞						|
;		cx	僐儅儞僪悢僇僂儞僞			|
;		di	c_Voice_Emb_Adrs億僀儞僞		|
;		si	c_Voice_Emb_Data億僀儞僞		|
;---------------------------------------------------------------|
c_Voice_Emb_first	proc	near		;
	pusha				;儗僕僗僞曐懚

;僿僢僟乕弶婜愝掕
	;億僀儞僞乕偺愝掕丅
	mov	cs:[pMML_File],0	;MML僼傽僀儖偺億僀儞僞傪愭摢偵偡傞丅

;僐儅儞僪専嶕
;僐儅儞僪悢検偺撉傒崬傒
c_Voice_Emb_first_Loop1:		;cx仼僐儅儞僪偺悢検
	xor	di,di			;di仼曄姺掕媊傾僪儗僗偺億僀儞僞
	mov	cx,cs:[offset c_Voice_Emb_Adrs + di]
					;暥帤楍僠僃僢僋偺僇僂儞僞
;億僀儞僞撉傒崬傒
c_Voice_Emb_first_Loop2:		;
	push	cs:[pMML_File]		;億僀儞僞曐懚
	add	di,2			;億僀儞僞僀儞僋儕儊儞僩(Word)
	mov	si,cs:[offset c_Voice_Emb_Adrs + di]	;si仼曄姺掕媊傾僪儗僗
;侾暥帤撉傒崬傒
c_Voice_Emb_first_Loop3:		;
	call	cread			;MML僼傽僀儖偐傜侾暥帤撉傒崬傒
c_Voice_Emb_first_Step1:		;
;侾暥帤僠僃僢僋乮24h娷傓乯
	mov	al,cs:[si]		;
	inc	si			;
	cmp	al,24h			;暥帤廔椆丠
	jz	c_Voice_Emb_first_Step2	;側傜偽丄暥帤楍堦抳偱張棟幚峴丅
	cmp	ah,al			;暥帤堦抳丠
	jz	c_Voice_Emb_first_Loop3	;側傜偽丄師傕僠僃僢僋丅
;暥帤僠僃僢僋偱堦抳偟側偐偭偨応崌丅
	pop	cs:[pMML_File]		;億僀儞僞暅婣
	sub	cx,1			;僇僂儞僞乕丒僨傿僋儕儊儞僩
	jnz	c_Voice_Emb_first_Loop2	;師偺僐儅儞僪偱斾妑
	inc	CS:[pMML_File]		;億僀儞僞丒僀儞僋儕儊儞僩
	mov	ax,CS:[szMML_File]	;
	cmp	ax,CS:[pMML_File]	;MML僼傽僀儖偺廔椆僠僃僢僋
	jnc	c_Voice_Emb_first_Loop1	;暥帤楍斾妑奐巒丅
	jmp	c_Voice_Emb_first_End	;MML僼傽僀儖偑[EOF]側傜偽廔椆
;-----------------------------------------------------------------------
;僐儅儞僪張棟丅
c_Voice_Emb_first_Step2:			;
	pop	ax			;億僀儞僞偺暅婣(抣偼栠偝偢丅)
	push	ds			;僙僌儊儞僩偺曐懚

;億僀儞僞傪尦偵栠偡丅
	dec	cs:[pMML_File]		;億僀儞僞丒僨傿僋儕儊儞僩

;僐儅儞僪張棟
c_Voice_Emb_first_Loop4:		;
	mov	al,cs:[si]		;al仼僐儅儞僪
	inc	si			;
	cmp	al,0FFh			;廔椆
	jnz	c_Voice_Emb_first_Step3	;
	jmp	c_Voice_Emb_first_Step99
c_Voice_Emb_first_Step3:		;
	cmp	al,0F0h			;Call (Other Program)
	jnz	c_Voice_Emb_first_Step7	;
	mov	dx,cs:[si]		;
	add	si,2			;
	call	dx			;
	jmp	c_Voice_Emb_first_Loop4	;
c_Voice_Emb_first_Step7:		;
	cmp	al,010h			;
	jnz	c_Voice_Emb_first_Step4	;
	call	A2H8R			;
	call	CSET			;
	jmp	c_Voice_Emb_first_Loop4	;
c_Voice_Emb_first_Step4:		;
	xor	bx,bx			;
	mov	bl,al			;
	rol	bl,1			;
	shr	bl,1			;Bit 7 Reset & Bit 7 -> Cy Flag
	jc	c_Voice_Emb_first_Step5	;
	call	A2H8R			;al仼僨乕僞(MML File)
	jmp	c_Voice_Emb_first_Step6	;
c_Voice_Emb_first_Step5:		;
	mov	ah,cs:[si]		;al仼僨乕僞(愝掕抣)
	inc	si			;
c_Voice_Emb_first_Step6:		;
	add	bx,cs:[pWTD_File]	;
	mov	byte ptr ds:[bx],ah	;僨乕僞彂偒崬傒

;僐儞僷僀儖張棟儖乕僾
	jmp	c_Voice_Emb_first_Loop4	;

;柦椷僐儞僷僀儖張棟廔椆
c_Voice_Emb_first_Step99:		;

;僇僂儞僞僀儞僋儕儊儞僩
	inc	byte ptr ds:[0].WTD_Mus_Emb
;億僀儞僞憹壛
	add	cs:[pWTD_File],16	;16Byte暘憹壛(捈彂偒偟偰偄傞堊)

;栠傞
	pop	ds			;僙僌儊儞僩偺暅婣
	jmp	c_Voice_Emb_first_Loop1	;暥帤楍斾妑奐巒丅
;-----------------------------------------------------------------------
;廔椆
c_Voice_Emb_first_End:			;
	popa				;儗僕僗僞暅婣
	ret				;
c_Voice_Emb_first	endp
;===============================================================|
;			柦椷					|
;===============================================================|
.const
c_Voice_Emb_Adrs	dw	((offset c_Voice_Emb_Adrs_E)-(offset c_Voice_Emb_Adrs_S))/2 + 1
c_Voice_Emb_Adrs_S	dw	offset cVED_0		;E
c_Voice_Emb_Adrs_E	dw	offset cVED_1		;Ex
;---------------------------------------------------------------|
;	嘆僐儅儞僪暥帤楍					|
;		嵟屻偼24h偱廔傢傞				|
;	嘇弌椡偡傞応強						|
;	    0xh		Address (Emb.Sturc)			|
;	    8xh		Address (Emb.Sturc)			|
;		byte	Data					|
;	    F0h	僾儘僌儔儉					|
;		nnnn	Offset Address				|
;	    FFh	廔傢傝						|
;---------------------------------------------------------------
cVED_0	db	0dh,0ah,'Ex',24h,10h
	db	WTD_Emb_FL
	db	WTD_Emb_AR
	db	WTD_Emb_AS
	db	WTD_Emb_AL
	db	WTD_Emb_DR
	db	WTD_Emb_DS
	db	WTD_Emb_DL
	db	WTD_Emb_SR
	db	WTD_Emb_SS
	db	WTD_Emb_SL
	db	WTD_Emb_RR
	db	WTD_Emb_RS
	db	WTD_Emb_RL
	db	0FFh
cVED_1	db	0dh,0ah,'E', 24h,10h
	db	WTD_Emb_FL
	db	WTD_Emb_AR
	db	WTD_Emb_AS + 80h,001h
	db	WTD_Emb_AL + 80h,0ffh
	db	WTD_Emb_DR
	db	WTD_Emb_DS + 80h,0ffh
	db	WTD_Emb_DL
	db	WTD_Emb_SR
	db	WTD_Emb_SS + 80h,0ffh
	db	WTD_Emb_SL + 80h,000h
	db	WTD_Emb_RR
	db	WTD_Emb_RS + 80h,0ffh
	db	WTD_Emb_RL + 80h,000h
	db	0FFh
.code
