                ORG	$3f00
; *****************************************************************************
; * The Music Studio Player Engine
; *
; * Based on code written by Saša Pušica for the utility, The Music Studio.
; * Modified by Chris Cowley
; *
; * Produced by Beepola v1.06.01
; ******************************************************************************
 
start		lda	#$3f
		tfr	a,dp
		setdp	$3f

		pshs	a,b,x,y,u,cc
		ldy	#$ff20
		ldx	#MUSICDATA	;LD    HL,MUSICDATA          ; <- Pointer to Music Data. Change
					                             ; this to play a different song
		lda	,x+		;LD   A,(HL)                 ; Get the loop start pointer
		sta	patloopbegin+1	;LD   (PATTERN_LOOP_BEGIN),A
					;INC  HL
		lda	,x+		;LD   A,(HL)                 ; Get the song end pointer
		sta	patloopend+1	;LD   (PATTERN_LOOP_END),A
					;INC  HL
		stx	patdata1+1	;LD   (PATTERNDATA1),HL
		stx	patdata2+1	;LD   (PATTERNDATA2),HL
		lda	#254		;LD   A,254
		sta	patptr+1	;LD   (PATTERN_PTR),A        ; Set the pattern pointer to zero
		orcc	#$50		;DI
					;EXX
					;PUSH  HL
; insert sound enable code here
	lda	$ff23
	ora	#$8
	sta	$ff23	
	
		bsr	nextpat		;CALL  NEXT_PATTERN
nextnote
		bsr	playnote	;CALL  PLAYNOTE
; insert check for keypress here
					;XOR   A                     ; This checks for a key on keyboard....
					;IN    A,($FE)               ; and aborts playback if a key is pressed
					;AND   $1F
					;CP    $1F
		bra	nextnote	;JR    Z,NEXTNOTE            ; Play next note if no key pressed

					;POP   HL
		puls	a,b,x,y,u,cc	;EXX                         ; Restore HL' for return to BASIC
					;EI
		rts			;RET                         ; Return from playing tune


tempb		.byte	00
tempc		.byte	00
ch1count	.word	00
ch2count	.word	00
ltemp1		.byte	00
ltemp2		.byte	00


; ********************************************************************************************************
; * NEXT_PATTERN
; *
; * Select the next pattern in sequence (and handle looping if we've reached PATTERN_LOOP_END
; * Execution falls through to PLAYNOTE to play the first note from our next pattern
; ********************************************************************************************************
nextpat
		ldb	<patptr+1	;LD   A,(PATTERN_PTR)
		addb	#2		;INC  A
					;INC  A
patloopend	cmpb	#00		;CP n
		bne	nopatloop	;JR   NZ,NO_PATTERN_LOOP
patloopbegin	ldb	#00		;LD A,n
nopatloop	stb	<patptr+1	;LD   (PATTERN_PTR),A

patdata1	ldx	#0000		;LD HL,nn
					;LD   E,A                    ; (this is the first byte of the pattern)
					;LD   D,0                    ; and store it at TEMPO
		abx			;ADD  HL,DE
					;LD   E,(HL)
					;INC  HL
					;LD   D,(HL)
		lda	[,x]		;LD   A,(DE)                 ; Pattern TEMPO -> A
		sta	TEMPO		;LD   (TEMPO),A              ; Store it at TEMPO

		lda	#1		;LD   A,1
		sta	<noteptr+1	;LD   (NOTE_PTR),A

playnote 
patdata2	ldx	#0000		;.byte $21                    ; LD HL,nn
					;.word $0000
patptr		ldb	#00		;LD   A,(PATTERN_PTR)
					;LD   E,A
					;LD   D,0
		abx			;ADD  HL,DE
		ldx	,x		;LD   E,(HL)
					;INC  HL
					;LD   D,(HL)                 ; Now DE|x = Start of Pattern data
		ldb	<noteptr+1	;LD   A,(NOTE_PTR)
					;LD   L,A
					;LD   H,0
		abx			;ADD  HL,DE                  ; Now HL|x = address of note data
		lda	,x		;LD   D,(HL)
					;LD   E,1

; IF D = $0 then were at the end of the pattern so increment PATTERN_PTR by 2 and set NOTE_PTR=0
					;LD   A,D
		cmpa	#$fe		;CP   $FE                    ; $FE indicates end of pattern
		beq	nextpat		;JR   Z,NEXT_PATTERN

continue	sta	<ch1freq+1	;PUSH DE
					;INC  HL
		lda	1,x		;LD   D,(HL)
					;LD   E,1
noteptr		ldb	#00		;LD   A,(NOTE_PTR)
		addb	#2		;INC  A
					;INC  A
		stb	noteptr+1	;LD   (NOTE_PTR),A           ; Increment the note pointer by 2 (one note per chan)
		sta	<ch2freq+1	;EXX
					;POP  DE                     ; Now CH1 freq is in DE|ch1freq and CH2 freq is in DE'|ch2freq

		ldb	TEMPO		;LD   A,(TEMPO)
		stb	tempc		;LD   C,A
		clr	tempb		;LD   B,0
		lda	BORDER_COL	;LD   A,BORDER_COL
		sta	<bord1+1	;EX   AF,AF'
		sta	<bord2+1	;LD   A,BORDER_COL           ; So now BC|tempbc = TEMPO, A|bord1 and A'|bord2 = BORDER_COL
					;EXX
outputnote
		ldb	<ch1freq+1	;LD   IXH,D                  ; Put note frequency for chan 1 into IXH
		stb	<ch1ix+1	;LD   H,D
		stb	ch1count	;LD   L,H
		decb			;DEC  L
		stb	ltemp1		;LD   E,L
		beq	continue1	;JR   Z,CONTINUE1
		ldb	#$40		;LD   E,$10
continue1	stb	<xore1+1
		stb	<xore1b+1

		ldb	<ch2freq+1	;LD   IXL,D                  ; Put note frequency for chan 2 into IXL
		stb	<ch2ix+1	;LD   H,D
		stb	ch2count	;LD   L,H
		decb			;DEC  L
		stb	ltemp2		;LD   E,L
		beq	continue2	;JR   Z,CONTINUE2
		ldb	#$40		;LD   E,$10
continue2	stb	<xore2+1
		stb	<xore2b+1
continue3
bord1		lda	#00		;EX   AF,AF'
					;OUT  ($FE),A
		dec	ch1count	;DEC  H                      ; Dec H, which also holds the frequency value
		bne	l8055		;JR   NZ,L8055
xore1		eora	#00		;XOR  E
		sta	bord1+1
ch1freq		ldb	#00		;LD   H,D
		stb	ch1count
					;PUSH AF
ch1ix		ldb	#00		;LD   A,IXH
		cmpb	#$20		;CP   $20
		bcc	l8054		;JR   NC,L8054               ; if A > $20 then this is not a drum effect, skip the INC D
		inc	ch1freq+1	;INC  D                      ; create the "fast falling pitch" percussion effect
l8054					;POP  AF
l8055		dec	ltemp1		;DEC  L
		bne	l805b		;JR   NZ,L805B
xore1b		eora	#00		;XOR  E
		sta	bord1+1
		ldb	ch1freq+1	;LD   L,D
		decb			;DEC  L
		stb	ltemp1
l805b					;EXX
bord2		adda	#00		;EX   AF,AF'
		anda	#$c0
		sta	,y		;OUT  ($FE),A
		lda	bord2+1
		dec	ch2count	;DEC  H
		bne	l806d		;JR   NZ,L806D
xore2		eora	#00		;XOR  E
		sta	bord2+1
ch2freq		ldb	#00		;LD   H,D
		stb	ch2count
					;PUSH AF
ch2ix		ldb	#00		;LD   A,IXL
		cmpb	#$20		;CP   $20
		bcc	l806c		;JR   NC,L806C               ; if A > $20 then this is not a drum effect, skip the INC D
		inc	ch2freq+1	;INC  D                      ; create the "fast falling pitch" percussion effect
l806c					;POP  AF
l806d		dec	ltemp2		;DEC  L
		bne	l8073		;JR   NZ,L8073
xore2b		eora	#00		;XOR  E
		sta	bord2+1
		ldb	ch2freq+1	;LD   L,D
		decb			;DEC  L
		stb	ltemp2
l8073		dec	tempb		;DJNZ CONTINUE2
		bne	continue3
		dec	tempc		;DEC  C
		bne	continue3	;JR   NZ,CONTINUE2
		rts			;RET





; *** DATA ***
BORDER_COL:               EQU $0
TEMPO:                    .byte 248

MUSICDATA:
                    .byte 0   ; Loop start point * 2
                    .byte 48   ; Song Length * 2
PATTERNDATA:        .word      PAT0
                    .word      PAT1
                    .word      PAT2
                    .word      PAT3
                    .word      PAT0
                    .word      PAT1
                    .word      PAT2
                    .word      PAT12
                    .word      PAT4
                    .word      PAT5
                    .word      PAT6
                    .word      PAT7
                    .word      PAT4
                    .word      PAT5
                    .word      PAT6
                    .word      PAT7
                    .word      PAT8
                    .word      PAT9
                    .word      PAT10
                    .word      PAT11
                    .word      PAT8
                    .word      PAT9
                    .word      PAT10
                    .word      PAT13

; *** Pattern data consists of pairs of frequency values CH1,CH2 with a single $FE to
; *** Mark the end of the pattern, and $01 for a rest
PAT0:
         .byte 4  ; Pattern tempo
             .byte 215,72
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 108,72
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 215,72
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 108,81
             .byte 1,1
             .byte 215,72
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 215,72
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 215,72
             .byte 1,1
             .byte 1,81
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
         .byte $FE
PAT1:
         .byte 4  ; Pattern tempo
             .byte 215,91
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 108,91
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 215,91
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 108,96
             .byte 1,1
             .byte 215,91
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
         .byte $FE
PAT2:
         .byte 4  ; Pattern tempo
             .byte 240,81
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 121,81
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 1,81
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 121,91
             .byte 1,1
             .byte 240,81
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 240,81
             .byte 1,1
             .byte 121,1
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 1,81
             .byte 1,1
             .byte 240,91
             .byte 1,1
             .byte 121,1
             .byte 1,1
             .byte 240,1
             .byte 1,1
         .byte $FE
PAT3:
         .byte 4  ; Pattern tempo
             .byte 215,91
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,91
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 1,91
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,121
             .byte 1,1
             .byte 215,108
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
         .byte $FE
PAT4:
         .byte 4  ; Pattern tempo
             .byte 215,1
             .byte 1,1
             .byte 1,54
             .byte 1,1
             .byte 108,48
             .byte 1,1
             .byte 215,45
             .byte 1,1
             .byte 215,48
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 108,61
             .byte 1,1
             .byte 215,54
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 215,72
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 215,81
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 108,91
             .byte 1,1
             .byte 1,1
             .byte 1,1
         .byte $FE
PAT5:
         .byte 4  ; Pattern tempo
             .byte 215,72
             .byte 1,1
             .byte 1,72
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 215,72
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 108,81
             .byte 1,1
             .byte 215,72
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
         .byte $FE
PAT6:
         .byte 4  ; Pattern tempo
             .byte 240,1
             .byte 1,1
             .byte 240,54
             .byte 1,1
             .byte 121,48
             .byte 1,1
             .byte 240,45
             .byte 1,1
             .byte 1,48
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 121,61
             .byte 1,1
             .byte 240,54
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 240,72
             .byte 1,1
             .byte 121,1
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 1,72
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 121,81
             .byte 1,1
             .byte 240,1
             .byte 1,1
         .byte $FE
PAT7:
         .byte 4  ; Pattern tempo
             .byte 215,72
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 215,144
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 215,161
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
         .byte $FE
PAT8:
         .byte 4  ; Pattern tempo
             .byte 180,72
             .byte 1,1
             .byte 180,61
             .byte 1,1
             .byte 91,48
             .byte 1,1
             .byte 180,45
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 180,61
             .byte 1,1
             .byte 91,48
             .byte 1,1
             .byte 180,45
             .byte 1,1
             .byte 180,1
             .byte 1,1
             .byte 180,45
             .byte 1,1
             .byte 91,48
             .byte 1,1
             .byte 180,45
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 180,48
             .byte 1,1
             .byte 91,61
             .byte 1,1
             .byte 180,72
             .byte 1,1
         .byte $FE
PAT9:
         .byte 4  ; Pattern tempo
             .byte 161,108
             .byte 1,1
             .byte 161,108
             .byte 1,1
             .byte 81,108
             .byte 1,1
             .byte 161,108
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 161,108
             .byte 1,1
             .byte 81,108
             .byte 1,1
             .byte 161,121
             .byte 1,1
             .byte 161,1
             .byte 1,1
             .byte 161,1
             .byte 1,1
             .byte 81,1
             .byte 1,1
             .byte 161,108
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 161,1
             .byte 1,1
             .byte 81,1
             .byte 1,1
             .byte 161,1
             .byte 1,1
         .byte $FE
PAT10:
         .byte 4  ; Pattern tempo
             .byte 180,72
             .byte 1,1
             .byte 180,61
             .byte 1,1
             .byte 91,48
             .byte 1,1
             .byte 180,45
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 180,61
             .byte 1,1
             .byte 91,48
             .byte 1,1
             .byte 180,45
             .byte 1,1
             .byte 180,1
             .byte 1,1
             .byte 180,45
             .byte 1,1
             .byte 91,48
             .byte 1,1
             .byte 180,45
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 180,45
             .byte 1,1
             .byte 91,48
             .byte 1,1
             .byte 180,61
             .byte 1,1
         .byte $FE
PAT11:
         .byte 4  ; Pattern tempo
             .byte 240,96
             .byte 1,1
             .byte 240,96
             .byte 1,1
             .byte 121,96
             .byte 1,1
             .byte 240,96
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 240,108
             .byte 1,1
             .byte 121,96
             .byte 1,1
             .byte 240,96
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 121,1
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 121,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
         .byte $FE
PAT12:
         .byte 4  ; Pattern tempo
             .byte 215,91
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,91
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 1,91
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 108,81
             .byte 1,1
             .byte 215,108
             .byte 1,1
             .byte 215,1
             .byte 1,1
             .byte 215,108
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 215,108
             .byte 1,1
             .byte 108,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
         .byte $FE
PAT13:
         .byte 4  ; Pattern tempo
             .byte 240,81
             .byte 1,1
             .byte 240,81
             .byte 1,1
             .byte 121,81
             .byte 1,1
             .byte 240,81
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 240,91
             .byte 1,1
             .byte 121,81
             .byte 1,1
             .byte 240,81
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 121,1
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
             .byte 240,1
             .byte 1,1
             .byte 121,1
             .byte 1,1
             .byte 1,1
             .byte 1,1
         .byte $FE
