;=======================================================================|
;									|
;		Wander Swan Total Sound Driver				|
;				MML Compiler Program			|
;				Heder Compile Routine			|
;									|
;					Programmed by			|
;						A.Watanabe		|
;									|
;=======================================================================|
;===============================================================|
;		FX峔憿懱乮幚懱乯				|
;===============================================================|
.data
FX		db	23h,21h,77h,73h	;#!ws
		db	60	Dup(0FFh)
FX_filename	db	16	dup(0)	;僼傽僀儖柤
FX_title	db	24	dup(0)	;僞僀僩儖
FX_fileadr	dd	0FFFFFFFFh	;僼傽僀儖偺傾僪儗僗丅
FX_filesize	dd	?		;僼傽僀儖僒僀僘
FX_block	dw	?		;僽儘僢僋悢
FX_mode		dw	0006h		;儌乕僪
FX_data		dd	?		;擭丒帪娫
FX_type		dd	000000000h	;僼傽僀儖偺庬椶
FX_resouce	dd	0FFFFFFFFh	;儕僜乕僗偺埵抲丅

;===============================================================|
;		僼儔僌						|
;===============================================================|
cHCD_FlagFx	db	00h		;僼儔僌 FX僼傽僀儖惗惉

;===============================================================|
;			柦椷	弶婜				|
;===============================================================|
.const
c_Head_Com_Adrs		dw	((offset c_Head_Com_Adr_E)-(offset c_Head_Com_Adr_S))/2 + 1
c_Head_Com_Adr_S	dw	offset cHCD_0		;TB
			dw	offset cHCD_1		;PA
			dw	offset cHCD_2		;FX
			dw	offset cHCD_3		;TI
			dw	offset cHCD_4		;FI
			dw	offset cHCD_5		;IN
			dw	offset cHCD_6		;#timebase
			dw	offset cHCD_7		;#part
			dw	offset cHCD_8		;#version
			dw	offset cHCD_9		;#Switch
			dw	offset cHCD_10		;#title
			dw	offset cHCD_11		;#filename
c_Head_Com_Adr_E	dw	offset cHCD_12		;#include
;---------------------------------------------------------------|
;	嘆僐儅儞僪暥帤楍					|
;		嵟屻偼24h偱廔傢傞				|
;	嘇弌椡偡傞応強						|
;	    00h	廔傢傝						|
;	    1xh	CS	僾儘僌儔儉				|
;		x	Segment		0:CS / 1:DS(WTD) / 2:ES(MML)
;		word	Offset		Offset Address		|
;		byte	Output Type	00h:Byte / 01:Word	|
;					20h:暥帤楍+24h		|
;	    F0h	僾儘僌儔儉					|
;		nnnn	Offset Address				|
;---------------------------------------------------------------
cHCD_0	db	0dh,0ah,'TB',24h, 011h	;Seg = WTD
	dw	WTD_Mus_TimeBase	;Time base
	db	00h,00h			;Byte , End
cHCD_1	db	0dh,0ah,'PA',24h, 011h	;Seg = WTD
	dw	WTD_Mus_Part		;Music Part count
	db	00h,00h			;Byte , End
cHCD_2	db	0dh,0ah,'FX',24h, 010h	;Seg = CS
	dw	offset cHCD_FlagFx	;FX File Make
	db	00h,00h			;Byte , End
cHCD_3	db	0dh,0ah,'TI',24h, 0f0h	;
	dw	offset cHCD_Set_title	;
	db	00h			;
cHCD_4	db	0dh,0ah,'FI',24h, 0f0h	;
	dw	offset cHCD_Set_filename
	db	00h			;
cHCD_5	db	0dh,0ah,'IN',24h, 0f0h	;
	dw	offset cHCD_include	;
	db	00h			;
cHCD_6	db	'#timebase',24h, 11h	;Seg = WTD
	dw	WTD_Mus_TimeBase	;Time base
	db	00h,00h			;Byte , End
cHCD_7	db	'#part',24h, 11h	;Seg = WTD
	dw	WTD_Mus_Part		;Music Part count
	db	00h,00h			;Byte , End
cHCD_8	db	'#version',24h, 11h	;Seg = WTD
	dw	WTD_Mus_Version + 0	;Music Version
	db	00h,11h			;Seg = WTD
	dw	WTD_Mus_Version + 1	;Music Version
	db	00h,00h			;Byte , End
cHCD_9	db	'#switch',24h, 10h	;Seg = CS
	dw	cHCD_FlagFx		;FX File Make
	db	00h,00h			;Byte , End
cHCD_10	db	'#title',24h, 0f0h	;
	dw	offset cHCD_Set_title	;
	db	00h			;
cHCD_11	db	'#filename',24h, 0f0h	;
	dw	offset cHCD_Set_filename;
	db	00h			;
cHCD_12	db	'#include',24h, 0f0h	;
	dw	offset cHCD_include	;
	db	0			;
.code
;---------------------------------------------------------------
;	
;---------------------------------------------------------------
cHCD_Set_Loop	proc	near	uses ax cx di,
		szBuff:word,
		ptBuff:word

	.repeat
	  call	cread_l			;嬻敀偺僠僃僢僋
	.until	(al>=21h)
	dec	cs:[pMML_File]		;億僀儞僞丒僨傿僋儕儊儞僩

	;夵峴僠僃僢僋
	mov	cx,szBuff
	mov	di,ptBuff
	.repeat
		call	cread_l			;ah仼僨乕僞
		dec	cs:[pMML_File]		;億僀儞僞丒僨傿僋儕儊儞僩
		.break	.if	(al == 0dh)

		;揮憲
		mov	cs:[di],al		;暥帤偺揮憲
		inc	di			;
	;	stosb

		;暥帤悢僠僃僢僋
		inc	cs:[pMML_File]		;億僀儞僞丒僀儞僋儕儊儞僩
	.untilcxz

	ret				;
cHCD_Set_Loop	endp
;---------------------------------------------------------------
;	
;---------------------------------------------------------------
cHCD_Set_title		proc	near

	invoke	cHCD_Set_Loop,	24,	addr FX_title

	ret
cHCD_Set_title	endp		;
;---------------------------------------------------------------
;	
;---------------------------------------------------------------
cHCD_Set_filename	proc	near

	invoke	cHCD_Set_Loop,	16,	addr FX_filename

	ret
cHCD_Set_filename	endp		;
;---------------------------------------------------------------|
;		僀儞僋儖乕僪僼傽僀儖偺憓擖			|
;---------------------------------------------------------------|
cHCD_include	proc	near	uses	ds es

	local	cFilename[128]:BYTE	;

	pusha

	;-----------------------
	;僼傽僀儖柤傑偱億僀儞僞傪恑傑偣傞丅
	.repeat
	    CALL	CREAD_L		;
	.until	(al>20h)		;俀侽枹枮偺僐乕僪偼旘偽偡

	;-----------------------
	;僼傽僀儖柤偺庢摼
	dec	CS:[pMML_File]		;
	xor	di,di			
	mov	cx,128			;
	.repeat				;
	   CALL	CREAD_L			;
	   .break  .if	(al==00dh)	;夵峴偩偭偨傜奿擺廔椆
	   mov	cFilename[di],al	;
	   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	;
	CALL	CREAD_L			;億僀儞僞傪恑傑偣傞

	;-----------------------
	;僼傽僀儖丒僆乕僾儞
	invoke	Memory_Open,0800h	;儊儌儕妋曐
	mov	ds,ax			;
	invoke	File_Open,	addr ss:cFilename,0	;僀儞僋儖乕僪僼傽僀儖丒僆乕僾儞
	push	ax			;僴儞僪儖曐懚
	invoke	File_Load,	ax,addr ds:0		;僀儞僋儖乕僪僼傽僀儖丒儘乕僪
	mov	cx,ax			;cx仼僀儞僋儖乕僪僼傽僀儖僒僀僘
	pop	ax			;ax仼僴儞僪儖
	invoke	File_Close,	ax	;僀儞僋儖乕僪僼傽僀儖丒僋儘乕僘


	;-----------------------
	;尰MML僼傽僀儖傪丄僀儞僋儖乕僪僼傽僀儖暘偢傜偡
	push	cx			;
	push	ds			;
	STD				;僽儘僢僋揮憲帪僨傿僋儕儊儞僩
	mov	ax, cs:[segMml_File]	;
	mov	bx, cs:[szMml_File]	;尰MML嵟屻旜
	mov	ds, ax			;
	lea	si, [bx - 1]		;ds:si = 揮憲尦
	mov	es, ax			;
	lea	di, [bx - 1]		;es:di = 揮憲愭
	add	di, cx			;
	mov	cx, bx			;
	sub	cx, CS:[pMML_File]	;cx = 揮憲検
	rep	MOVSB			;
	pop	ds			;
	pop	cx			;

	;-----------------------
	;僀儞僋儖乕僪僼傽僀儖偺憓擖
	push	cx			;
	CLD				;僽儘僢僋揮憲帪僀儞僋儕儊儞僩
	xor	si,si			;DS:SI 仼揮憲尦
	MOV	es,CS:[segMml_File]	;
	MOV	di,CS:[pMML_File]	;ES:DI 仼憲傝愭
	REP	MOVSB			;僽儘僢僋揮憲
	pop	cx			;

	;-----------------------
	;EOF傪徚偡
	DEC	DI			;
	.if	(byte ptr es:[di]==01ah)
		MOV	byte ptr es:[di],020h	;
	.endif

	;-----------------------
	;僒僀僘暘偩偗戝偒偔偡傞丅
	add	CS:[szMml_File],cx	;

	;-----------------------
	;妱傝摉偰偨儊儌儕偺夝曻
	invoke	Memory_Close,	ds

	popa
	RET				;
cHCD_include	endp
;---------------------------------------------------------------|
;		僿僢僟乕僐儞僷僀儖	弶婜			|
;---------------------------------------------------------------|
;	堷悢							|
;		柍偟						|
;	曉傝抣							|
;		柍偟						|
;	巊梡儗僕僗僞						|
;		cx	僐儅儞僪悢僇僂儞僞			|
;		di	c_Head_Com_Adrs億僀儞僞			|
;		si	c_Hear_Com_Data億僀儞僞			|
;---------------------------------------------------------------|
c_Head_first	proc	near	uses	ds

	pusha

	;億僀儞僞乕傪愭摢偵偡傞丅
	xor	ax,ax			;
	mov	cs:[pMML_File],ax	;
	mov	cs:[pWTD_File],ax	;億僀儞僞乕傪愭摢偵偡傞丅

	;僿僢僟乕弶婜抣偺愝掕
	mov	word ptr ds:[0].WTD_Mus_Name + 0	,5457h	;
	mov	word ptr ds:[0].WTD_Mus_Name + 2	,0044h	;'WTD',00h
	mov	word ptr ds:[0].WTD_Mus_Version		,0100h	;Version
	mov	word ptr ds:[0].WTD_Mus_Extr		,ax	;0
	mov	byte ptr ds:[0].WTD_Mus_Emb		,al	;0
	mov	byte ptr ds:[0].WTD_Mus_Voice		,al	;0
	mov	byte ptr ds:[0].WTD_Mus_Part		,al	;0
	mov	byte ptr ds:[0].WTD_Mus_TimeBase	,48	;
	mov	word ptr ds:[0].WTD_Mus_ExtrAdr		,ax	;0
	mov	word ptr ds:[0].WTD_Mus_DataAdr		,ax	;0

	push	cs			;
	pop	ds			;ds 仼 cs

	.repeat

	   ;僐儅儞僪専嶕
	   ;僐儅儞僪悢検偺撉傒崬傒
	   xor	di,di				;di仼曄姺掕媊傾僪儗僗偺億僀儞僞
	   ;cx仼僐儅儞僪偺悢検
	   mov	cx,cs:[offset c_Head_Com_Adrs + di]
	   .repeat
		;億僀儞僞撉傒崬傒
		push	cs:[pMML_File]		;億僀儞僞曐懚
		add	di,2			;億僀儞僞僀儞僋儕儊儞僩(Word)
		mov	si,cs:[offset c_Head_Com_Adrs + di]	;si仼曄姺掕媊傾僪儗僗
		;柦椷堦抳僠僃僢僋
		.repeat
		   call	cread			;MML僼傽僀儖偐傜侾暥帤撉傒崬傒
		   lodsb
		.until	(al != ah)		;暥帤堦抳側傜偽丄師傕僠僃僢僋丅
		.if	(al == 24h)		;暥帤廔椆丠
		   pop	ax			;億僀儞僞偺暅婣(抣偼栠偝偢丅)
		   dec	cs:[pMML_File]		;億僀儞僞丒僨傿僋儕儊儞僩
						;億僀儞僞偼僐儅儞僪偺師偵偁傞丅
		   ;僐儅儞僪張棟丅
		   .repeat
			;僙僌儊儞僩偺愝掕
			lodsb
			.if	(al==00h)		
				.break			;廔椆
			.elseif	(al==010h)
				mov	ax,cs		;
			.elseif	(al==011h)
				mov	ax,cs:[segWTD_File]
			.elseif	(al==012h)
				mov	ax,cs:[segMML_File]
			.elseif	(al==0f0h)
				lodsw			;
				call	ax		;
				.continue
			.endif

			push	ds			;DS曐懚
			;僆僼僙僢僩偺愝掕
			push	ax			;
			lodsw				;
			mov	bx,ax			;bx 仼 Offset Address
			lodsb				;al 仼 惂屼僐乕僪
			pop	ds			;DS 仼 曐懚愭
			;僨乕僞偺彂偒崬傒
			.if	(al==000h)
				call	A2H8R		;
				mov	ds:[bx],ah	;
			.elseif	(al==001h)
				call	A2H16R		;
				mov	ds:[bx],ax	;
			.elseif	(al==020h)
				;
				;	暥帤楍堦妵揮憲(枹懳墳)
				;
			.endif
			pop	ds			;DS暅婣
		   .until	0
		   dec	cs:[pMML_File]		;
		   .break			;柦椷僨僐乕僪廔椆
		.else
		   ;暥帤僠僃僢僋偱堦抳偟側偐偭偨応崌丅
		   pop	cs:[pMML_File]		;億僀儞僞暅婣
		.endif
		dec	cx
	   .until	(zero?)			;師偺僐儅儞僪偱斾妑
	   inc	CS:[pMML_File]			;億僀儞僞丒僀儞僋儕儊儞僩
	   mov	ax,CS:[szMML_File]		;MML僼傽僀儖偺廔椆僠僃僢僋
	.until	(ax < CS:[pMML_File])		;MML僼傽僀儖偑[EOF]側傜偽廔椆

	popa
	ret				;
c_Head_first	endp
;---------------------------------------------------------------|
;		僿僢僟乕僐儞僷僀儖	屻婜			|
;---------------------------------------------------------------|
;	堷悢							|
;		柍偟						|
;	曉傝抣							|
;		柍偟						|
;	巊梡儗僕僗僞						|
;		cx	僐儅儞僪悢僇僂儞僞			|
;		di	c_Head_Com_Adrs億僀儞僞			|
;		si	c_Hear_Com_Data億僀儞僞			|
;---------------------------------------------------------------|
;仠暥帤楍
.const
c_Head_Version	db	'This Music Data is version $'
c_Head_CR	db	0dh,0ah,24h
c_Head_Size	db	'Filesize = '     ,24h
c_Head_Byte	db	' [Byte]   ( $'
c_Head_Block	db	' [Block] )',0dh,0ah,24h
;---------------------------------------------------------------
.code
c_Head_end	proc	near	uses ds es
	pusha				;儗僕僗僞曐懚

	push	ds			;
	pop	es			;es仼ds
	push	cs			;
	pop	ds			;ds仼cs
;---------------------------------------------------------------
;僶乕僕儑儞忣曬偺昞帵

	xor	bx,bx			;ds:bx仼WTD File Segment Address

	mov	dx,offset c_Head_Version
	mov	ah,09h			;
	int	21h			;"This Music Data is Version "

	mov	ax,word ptr es:[bx].WTD_Mus_Version

	push	ax			;
	xchg	ah,al			;
	call	HEX2ASC8		;
	mov	ah,09h			;
	int	21h			;悢抣偺昞帵
	mov	dl,'.'
	mov	ah,02h
	int	21h			;乽揰乿偺昞帵
	pop	ax			;
	call	HEX2ASC8		;
	mov	ah,09h			;
	int	21h			;悢抣偺昞帵
	mov	dx,offset c_Head_CR	;
	mov	ah,09h			;
	int	21h			;夵峴偺昞帵

;---------------------------------------------------------------
;FX宍幃僿僢僟乕偺嶌惉
;仸filename,title偼嶌惉嵪傒丅

;仠僼傽僀儖僒僀僘丒僽儘僢僋悢
	mov	dx,offset c_Head_Size	;
	mov	ah,09h			;
	int	21h			;"Filesize = "偺昞帵

	xor	dx,dx			;
	mov	ax,cs:[pWTD_File]	;
	dec	ax			;dx:ax仼僼傽僀儖僒僀僘丅
	mov	word ptr cs:[FX_filesize + 0],ax
	mov	word ptr cs:[FX_filesize + 2],dx

	push	ax			;
	push	dx			;
	call	HEX2ASC16		;
	mov	ah,09h			;
	int	21h			;悢抣偺昞帵
	mov	dx,offset c_Head_Byte	;
	mov	ah,09h			;
	int	21h			;" [Byte]"偺昞帵
	pop	dx			;
	pop	ax			;

	mov	cx,128			;
	div	cx			;128偱妱傞丅
	cmp	dx,0			;
	jz	c_Head_end_Step0	;梋傝偑偁偭偨傜丄
	inc	ax			;夝偵侾傪懌偡丅
c_Head_end_Step0:			;
	mov	word ptr cs:[FX_block],ax

	call	HEX2ASC16		;悢抣偺昞帵
	mov	ah,09h			;
	int	21h			;
	mov	dx,offset c_Head_Block	;
	mov	ah,09h			;
	int	21h			;" [Byte]"偺昞帵

;---------------------------------------------------------------
;仠擭丒帪娫偺嶌惉
;	bit 25-31	擭-2000	(7)
;	bit 21-24	寧	(4)
;	bit 16-20	擔	(5)
;	bit 11-15	帪	(5)
;	bit  5-10	暘	(6)
;	bit  0- 4	昩/2	(5)

	mov	ah,02ah			;
	int	21h			;擔晅偺庢摼
	xor	ax,ax			;

	sub	cx,2000			;擭-2000
	and	cx,007Fh		;
	or	ax,cx			;

	shl	ax,4			;寧
	mov	bx,dx			;
	xchg	bh,bl			;
	and	bx,000fh		;
	or	ax,bx			;

	shl	ax,5			;擔
	and	dx,001fh		;
	or	ax,dx			;

	push	ax			;擔晅偺曐懚

	mov	ah,02ch			;
	int	21h			;帪娫偺庢摼
	xor	ax,ax			;

	mov	bx,cx			;帪
	xchg	bh,bl			;
	and	bx,001fh		;
	or	ax,bx			;

	shl	ax,6			;暘
	and	cx,003fh		;
	or	ax,cx			;

	shl	ax,5			;昩/2
	xchg	dh,dl			;
	shr	dx,1			;
	and	dx,001fh		;
	or	ax,dx			;

	pop	dx			;擔晅偺暅婣

	mov	word ptr cs:[FX_data + 0],ax
	mov	word ptr cs:[FX_data + 2],dx

;---------------------------------------------------------------
;廔椆
	popa				;
	ret				;Return
c_Head_end	endp
