; ALL MP3 CODE IN ONE FILE
; 
;
;05252011
;
;revision 0.01
;
;
;load stops when there is no data for a $FF loop count
;
;tasm -65 -l -b -h FILE.txt FILE FILE.lst
;-h may be eliminated
;
; START @ $2000 
.org $2000 
;
;MP3 / Data device in slot 4 for now
;
;
ACIA        	= $C0C0 
ACIA_CTRL   	= ACIA+3 
ACIA_CMD    	= ACIA+2 
ACIA_SR     	= ACIA+1 
ACIA_DAT    	= ACIA 
STORAGE		= $2100
STOREPTR	= $06
COUT		= $FDF0
CROUT		= $FD8E	
;THIS IS THE INITIALIZATION PART OF THE PROGRAM
;THAT PROGRAMS THE 6551 TO 9600,N,8,1
;BRUN THIS PROGRAM WILL INIT THE SERIAL PORT
;
INIT       	
		LDA	#$00		;LOAD LOW ORDER BYTE
		STA	STOREPTR
		LDA	#$21		;LOAD HIGH ORDER BYTE
		STA	STOREPTR+1
		LDA 	#$10        	;INIT ACIA TO 10=115K 1E=9600 BAUD
            	STA 	ACIA_CTRL 
            	LDA 	#$0B        	;NO PARITY ONE STOP BIT
            	STA 	ACIA_CMD
;END OF INIT PROCESS 
;NOW GO RIGHT INTO DOING A DIRECTORY

;ADIR COMMAND DOES A DIRECTORY OF THE FLASH DRIVE

ADIR		LDX	#$00		;SET CHARACTER COUNT TO 0
COMMAND		LDA	MP3DIR,X	;GET A BYTE FROM THE COMMAND
		BEQ	STARTLOAD	;IF $00 GO GET THE FILE!
		JSR	ECHO		;GO OUTPUT THE BYTE
		INX			;INCREASE BYTE COUNT
		CMP	#$00		;IS IT END OF COMMAND?
		BNE	COMMAND		;NO CONTINUE UNTIL FINISHED
;
;HERE IS WHERE WE START GETTING DATA 
;IF WE HIT THE END OF THE FILE, PADDING BYTES ARE READ
STARTLOAD	LDX	#$00
		LDY	#$00		;RESET COUNT TO 0
NEXTCHAR    	CPX	#$E9		;IS WAIT TOO LONG DONE DIR?
		BEQ	DOCR
		LDA 	ACIA_SR     	;See if we got an incoming char 
		INX
		AND 	#$08        	;Test bit 3 
            	BEQ 	NEXTCHAR    	;Wait for character 
		LDX	#$00
            	LDA 	ACIA_DAT    	;Load char		
		STA	(STOREPTR),Y	;STORE DATA AT 2100+
		INY			;INCREASE BYTE COUNT
		BNE	NEXTCHAR	;KEEP GOING IF DID NOT WRAP
		INC	STOREPTR+1	;MOVE TO NEXT MEMORY PAGE
		JMP	NEXTCHAR	;CONTINUE UNTIL LONG DELAY
DOCR		JSR	CROUT
		LDA	#$0D		;STORE EXTRA CR TO DETECT 2 CR'S AS END
		STA	(STOREPTR),Y	;STORE IT
		RTS			;RETURN TO APPLESOFT PROMP
ECHO		STA 	ACIA_DAT    	;SEND DATA 
WAIT       	LDA 	ACIA_SR     	;LOAD STATUS REGISTER FOR ACIA
            	AND 	#$10		;MASK BIT 4 
            	BEQ 	WAIT  		;ACIA NOT FINISHED, WAIT
            	RTS             	;ALL FINISHED

;ACHECK2 checks if song is still playing
ACHECK2		LDX	#$00
		LDA	#$FF
		STA	STORAGE
CONT		LDY	#$00		;RESET COUNT TO 0
CHECKNEXTCHAR  	CPX	#$FE		;IS WAIT TOO LONG DONE DIR?
		BEQ	DOCR2
		INX
		LDA 	ACIA_SR     	;See if we got an incoming char 
		AND 	#$08        	;Test bit 3 
            	BEQ 	CHECKNEXTCHAR  	;Wait for character 
		LDA 	ACIA_DAT    	;YES, GET CHARACTER		
		STA	STORAGE		;STORE DATA AT 2100+
		RTS			;GO BACK WITH FILE STILL PLAYING CODE

DOCR2		
		LDA	#$00		;CODE WITH FILE FINISHED
		STA	STORAGE		;STORE IT (WAS STOREPTR)
		RTS			;RETURN TO APPLESOFT PROGRAM


;MP3PAUSE COMMAND
;THIS COMMAND PAUSES PLAYING

MP3PAUSE
		LDX	#$00
DOPAUSE		LDA	PAUSE,X
		BEQ	RETURN
		JSR	ECHO
		INX
		JMP	DOPAUSE
RETURN		RTS			;RETURN TO APPLESOFT PROMPT
		

;MP3STOP COMMAND

MP3STOP		LDX	#$00
		LDY	#$00
READCHAR	LDA	STOP,X
		BEQ	DOCR2		;All done, go back and mp3 is stopped
		JSR	ECHO
		INX
		BNE	READCHAR
NEXTCHAR2    	LDA 	ACIA_SR     	;See if we got an incoming char 
            	AND 	#$08        	;Test bit 3 
            	BEQ 	NEXTCHAR2    	;Wait for character 
            	LDA 	ACIA_DAT    	;Load char
		CPY	#$00		;Is this the first character? 
		BNE	NOTFIRST	;No, so send character
		INY			;Make Y=1 so next time character will print
		JMP	NEXTCHAR2
NOTFIRST	ORA	#$80
		CMP	#$8D		;Is it a carriage return?
		BEQ	DOCR		;Yes, do a CR and end
		JSR	COUT		;Send it out
		JMP	NEXTCHAR2


;MP3PLAY PLAY ALL FILES ON FLASH DRIVE

MP3PLAY		LDX	#$00
PLAYME		LDA	PLAYALL,X
		BEQ	DONEPLAYALL
		JSR	ECHO
		INX
		CMP	#$00
		BNE	PLAYME
DONEPLAYALL	RTS



;APLAY PLAYS A SONG WITH THE PROGRAM PLUGGING IN THE NAME AFTER THE COMMAND

APLAY		LDX	#$00
LOADSONG	LDA	PLAY,X
		BEQ	ENDNAME
		JSR	ECHO
		INX
		CMP	#$00
		BNE	LOADSONG		
ENDNAME		LDY	#$00
		JSR	CROUT		;NEED TO DO A CR
 		RTS			;DONE NOW RETURN

;DATA FOR COMMANDS:


MP3DIR		.byte	"DIR",$0D,$00
PAUSE		.byte	"VP",$0D,$00
STOP		.byte	"VST",$0D,$00
PLAYALL		.byte	"VRA",$0D,$00
PLAY		.BYTE	"VPF "


.end

