// compile command line: java -jar KickAss.jar sampler8.asm
// Kick Assembler: http://www.theweb.dk/KickAssembler/Main.php
// SID init and sample output code from http://csdb.dk/release/?id=111961

.pc = $0801
		.byte $0f, $08, $dd, $07, $9e, $20, $bd, $28, $37, $2e, $36, $34, $29, $00, $00
		.byte $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00

.var samples = $57
.var count = $59

start:
		sei
		
		// disable ROM and enable RAM
		lda #$35
		sta 1
		
		// screen off
		ldy #0
		sty $d011

                // full volume with lowpass filter
                lda #$1f
                sta $d418
                
                // filter cut-off frequency
		lda #160
		sta $d416
		
		// enable filter for all voices
		lda #7
		sta $d417

                // attack  = 0, decay   = 0
                // sustain = f, release = 0
                lda #$f0
                sta $d406
                sta $d40d
                sta $d414

initLoop:
		// load sample start address
		sty samples
		lda #>[blockCount + 1]
		sta samples + 1
		
		// init end block count
		clc
		adc blockCount
		sta count
		dec count

loop:
		// check for samples end
		lda samples + 1		// 3
		cmp count		// 3
		beq initLoop		// 2

		// play next sample

		lda (samples),y		// 5
		tax			// 2
                lda #$11		// 2  ($11) triangle, start adsr
                sta $D404+0*7		// 4
                sta $D404+1*7		// 4
                sta $D404+2*7		// 4
                lda #$09		// 2  ($09) testbit, start adsr
                sta $D404+0*7		// 4
                sta $D404+1*7		// 4
                sta $D404+2*7		// 4
                stx $D401+0*7		// 4  freq hi
                stx $D401+1*7		// 4  freq hi
                stx $D401+2*7		// 4  freq hi
                lda #$01		// 2  ($01) start adsr
                sta $D404+0*7		// 4
                sta $D404+1*7		// 4
                sta $D404+2*7		// 4
                
                lda colors, x		// 4
                sta $d020		// 4

		// increment samples pointer
		iny			// 2
		bne loop2		// 3, if branch to loop, 2 otherwise
		inc samples + 1		// 5
		jmp loop		// 3, sum from bne: 10
		// loop sum: 89

loop2:		nop			// 2
		nop			// 2
		jmp loop		// sum from bne: 10

.align $100
colors:
.fill 16, LIGHT_RED
.fill 16, CYAN
.fill 32, LIGHT_GREY
.fill 32, DARK_GREY
.fill 16, DARK_GREY
.fill 16, BLACK
.fill 16, BLACK
.fill 16, DARK_GREY
.fill 32, DARK_GREY
.fill 32, LIGHT_GREY
.fill 16, CYAN
.fill 15, LIGHT_RED

blockCount:
