diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt index 57679696b..049e573d4 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt @@ -1873,7 +1873,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, internal fun inplaceNegate(assign: AsmAssignment) { val target = assign.target - when (assign.target.datatype) { + when (target.datatype) { DataType.BYTE -> { when (target.kind) { TargetStorageKind.VARIABLE -> { diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 37cde05f5..1b3f9a55e 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,7 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- try to get the cx16 adpcm example to output audio +- fix compiler crash cx16.r0 = (-(cx16.r0 as word) as uword) - duplicate diskio for cx16 (get rid of cx16diskio, just copy diskio and tweak everything) + documentation - get f_seek_w working like in the BASIC program - this needs the changes to diskio.f_open to use suffixes ,p,m - attempt to fix the expression codegen bug with reused temp vars (github #89) diff --git a/examples/cx16/adpcm-mono.bin b/examples/cx16/adpcm-mono.bin deleted file mode 100644 index 33e5e7a00..000000000 Binary files a/examples/cx16/adpcm-mono.bin and /dev/null differ diff --git a/examples/cx16/adpcm.p8 b/examples/cx16/adpcm.p8 deleted file mode 100644 index 88873bf61..000000000 --- a/examples/cx16/adpcm.p8 +++ /dev/null @@ -1,197 +0,0 @@ -%import textio -%option no_sysinit -%zeropage basicsafe - -main { - - sub start() { - cx16.VERA_AUDIO_CTRL = %10101111 ; mono 16 bit - cx16.VERA_AUDIO_RATE = 0 ; halt playback - cx16.VERA_IEN &= %11110111 ; disable AFLOW - repeat 4096 { - cx16.VERA_AUDIO_DATA = 0 - } - - cx16.VERA_AUDIO_RATE = 16 - - uword pcm_size = &pcm_data_end - &pcm_data - uword num_blocks = pcm_size / 1024 - uword pcm_ptr = &pcm_data - - txt.print("audio on.\n") - - repeat num_blocks { - ; wait till the fifo is empty (fully empty??) - while not (cx16.VERA_AUDIO_CTRL & %01000000) { - ; wait - } - - ; fill more audio data - repeat 512 { - cx16.VERA_AUDIO_DATA = @(pcm_ptr) - pcm_ptr++ - cx16.VERA_AUDIO_DATA = @(pcm_ptr) - pcm_ptr++ - } - } - - cx16.VERA_AUDIO_CTRL = %00100000 - cx16.VERA_AUDIO_RATE = 0 - txt.print("audio off.\n") - - -; uword adpcm_size = &adpcm_data_end - &adpcm_data -; uword num_adpcm_blocks = adpcm_size / 256 -; uword nibblesptr = &adpcm_data -; -; nibblesptr = &nibbles -; repeat num_adpcm_blocks { -; ; wait till the fifo is empty (fully empty??) -; while not (cx16.VERA_AUDIO_CTRL & %01000000) { -; ; wait -; } -; -; ; decode another adpcm block -; uword @zp sample = peekw(nibblesptr) -; nibblesptr += 2 -; adpcm.init(sample, @(nibblesptr)) -; nibblesptr += 2 -; -; repeat 252 { -; ubyte @zp nibble = @(nibblesptr) -; adpcm.decode_nibble(nibble & 15) -; ; adpcm.predict as word -; cx16.VERA_AUDIO_DATA = lsb(adpcm.predict) -; cx16.VERA_AUDIO_DATA = msb(adpcm.predict) -; adpcm.decode_nibble(nibble>>4) -; ; adpcm.predict as word -; cx16.VERA_AUDIO_DATA = lsb(adpcm.predict) -; cx16.VERA_AUDIO_DATA = msb(adpcm.predict) -; nibblesptr++ -; } -; } - - -; cx16.set_rasterirq(irqhandler, 10) -; uword adpcm_size = &adpcm_data_end - &adpcm_data -; uword num_adpcm_blocks = adpcm_size / 256 -; uword nibblesptr = &adpcm_data -; -; num_adpcm_blocks = 1 -; nibblesptr = &nibbles -; -; repeat num_adpcm_blocks { -; uword @zp sample = peekw(nibblesptr) -; nibblesptr += 2 -; adpcm.init(sample, @(nibblesptr)) -; nibblesptr += 2 -; -; repeat 252 { -; ubyte @zp nibble = @(nibblesptr) -; adpcm.decode_nibble(nibble & 15) -; txt.print_w(adpcm.predict as word) -; txt.spc() -; adpcm.decode_nibble(nibble>>4) -; txt.print_w(adpcm.predict as word) -; txt.spc() -; nibblesptr++ -; } -; } - } - -; sub irqhandler() { -; cx16.vpoke(1, $fa00, 255) -; -; uword @zp sample = peekw(nibbles) -; adpcm.init(sample, nibbles[2]) -; -; ubyte @zp idx = 4 -; while idx<200 { -; ubyte @zp nibble = nibbles[idx] -; adpcm.decode_nibble(nibble & 15) -; sample = adpcm.predict -; nibble >>= 4 -; adpcm.decode_nibble(nibble) -; sample = adpcm.predict -; idx++ -; } -; -; cx16.vpoke(1, $fa00,0) -; } - -;adpcm_data: -; %asmbinary "adpcm-mono.bin" -;adpcm_data_end: - -; ubyte[256] nibbles = [216, 227, 59, 0, 32, 2, 186, 139, 24, 34, 84, 115, 19, 185, 173, 8, 0, 56, 162, -; 169, 144, 113, 55, 130, 186, 12, 33, 144, 10, 136, 10, 129, 41, 66, 210, 207, -; 9, 0, 153, 129, 185, 173, 155, 0, 37, 162, 191, 27, 36, 129, 144, 137, 82, 18, -; 87, 18, 152, 153, 136, 0, 32, 23, 0, 40, 24, 117, 19, 202, 138, 8, 176, 171, -; 24, 131, 234, 9, 32, 39, 160, 10, 153, 33, 52, 209, 137, 200, 73, 33, 233, 155, -; 136, 66, 146, 8, 152, 56, 131, 113, 23, 168, 169, 185, 9, 160, 65, 37, 49, 22, -; 64, 68, 168, 138, 153, 1, 171, 88, 35, 25, 65, 1, 176, 173, 185, 175, 168, 154, -; 153, 205, 138, 144, 152, 154, 204, 43, 53, 51, 35, 154, 184, 96, 71, 34, 152, -; 172, 138, 160, 9, 24, 16, 204, 88, 48, 54, 160, 172, 152, 65, 132, 168, 48, 4, -; 0, 190, 9, 161, 155, 218, 139, 154, 185, 207, 9, 50, 130, 184, 252, 155, 24, -; 53, 144, 172, 170, 88, 51, 1, 176, 173, 17, 40, 115, 129, 136, 200, 11, 70, -; 129, 8, 0, 0, 48, 37, 160, 42, 35, 32, 17, 18, 220, 174, 170, 143, 9, 137, 160, -; 171, 104, 4, 169, 154, 219, 142, 65, 129, 128, 168, 9, 69, 50, 130, 219, 128, -; 138, 49, 52, 146, 237, 141, 50, 21, 8, 136, 8, 153, 35, 193, 170, 2, 169, 224, -; 186, 170, 40, 253, 12, 17, 25] - -pcm_data: - %asmbinary "pcm-mono.bin" -pcm_data_end: - -} - - -adpcm { - ubyte[] t_index = [ -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8] - uword[] t_step = [ - 7, 8, 9, 10, 11, 12, 13, 14, - 16, 17, 19, 21, 23, 25, 28, 31, - 34, 37, 41, 45, 50, 55, 60, 66, - 73, 80, 88, 97, 107, 118, 130, 143, - 157, 173, 190, 209, 230, 253, 279, 307, - 337, 371, 408, 449, 494, 544, 598, 658, - 724, 796, 876, 963, 1060, 1166, 1282, 1411, - 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, - 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, - 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, - 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, - 32767] - - uword @zp predict - ubyte @zp index - uword @zp pstep - - sub init(uword startPredict, ubyte startIndex) { - predict = startPredict - index = startIndex - pstep = t_step[index] - } - - sub decode_nibble(ubyte nibble) { - word @zp difference = 0 - if nibble & 4 - difference += pstep - pstep >>= 1 - if nibble & 2 - difference += pstep - pstep >>= 1 - if nibble & 1 - difference += pstep - pstep >>= 1 - difference += pstep - if nibble & 8 - difference = -difference - predict += difference as uword - index += t_index[nibble] - if index & 128 - index = 0 - else if index > 88 - index = 88 - pstep = t_step[index] - } -} diff --git a/examples/cx16/pcmaudio/adpcm-mono.bin b/examples/cx16/pcmaudio/adpcm-mono.bin new file mode 100644 index 000000000..ddd6028f0 Binary files /dev/null and b/examples/cx16/pcmaudio/adpcm-mono.bin differ diff --git a/examples/cx16/pcmaudio/adpcm.p8 b/examples/cx16/pcmaudio/adpcm.p8 new file mode 100644 index 000000000..1917207d2 --- /dev/null +++ b/examples/cx16/pcmaudio/adpcm.p8 @@ -0,0 +1,187 @@ +%import textio +%import floats +%option no_sysinit +%zeropage basicsafe + +main { + + ubyte num_adpcm_blocks + ubyte adpcm_blocks_left + uword @requirezp nibblesptr + + sub start() { + uword adpcm_size = &audiodata.adpcm_data_end - &audiodata.adpcm_data + num_adpcm_blocks = (adpcm_size / 256) as ubyte ; NOTE: THE ADPCM DATA NEEDS TO BE ENCODED IN 256-byte BLOCKS ! + + txt.print_uw(adpcm_size) + txt.print(" adpcm data size = ") + txt.print_ub(num_adpcm_blocks) + txt.print(" blocks\n(b)enchmark or (p)layback? ") + + when c64.CHRIN() { + 'b' -> benchmark() + 'p' -> playback() + } + } + + sub benchmark() { + nibblesptr = &audiodata.adpcm_data + + txt.print("\ndecoding all blocks...\n") + c64.SETTIM(0,0,0) + repeat num_adpcm_blocks { + adpcm.init(peekw(nibblesptr), @(nibblesptr+2)) + nibblesptr += 4 + repeat 252 { + ubyte @zp nibble = @(nibblesptr) + adpcm.decode_nibble(nibble & 15) ; first word + adpcm.decode_nibble(nibble>>4) ; second word + nibblesptr++ + } + } + float duration_secs = (c64.RDTIM16() as float) / 60.0 + float words_per_second = 505.0 * (num_adpcm_blocks as float) / duration_secs + txt.print_uw(words_per_second as uword) + txt.print(" words/sec\n") + } + + sub playback() { + nibblesptr = &audiodata.adpcm_data + adpcm_blocks_left = num_adpcm_blocks + + cx16.VERA_AUDIO_CTRL = %10101111 ; mono 16 bit + cx16.VERA_AUDIO_RATE = 0 ; halt playback + repeat 1024 { + cx16.VERA_AUDIO_DATA = 0 + } + + sys.set_irqd() + cx16.CINV = &irq_handler + cx16.VERA_IEN = %00001000 ; enable AFLOW + sys.clear_irqd() + + cx16.VERA_AUDIO_RATE = 21 ; start playback at ~8000 hz + + float rate = (cx16.VERA_AUDIO_RATE as float) * (25e6 / 65536.0) + txt.print("\naudio via irq at ") + txt.print_uw(rate as uword) + txt.print(" hz mono\n") + + repeat { + ; audio will play via the IRQ. + } + + ; not reached: +; cx16.VERA_AUDIO_CTRL = %00100000 +; cx16.VERA_AUDIO_RATE = 0 +; txt.print("audio off.\n") + } + + sub irq_handler() { + if cx16.VERA_ISR & %00001000 { + ; AFLOW irq. + ;; cx16.vpoke(1,$fa0c, $a0) ; paint a screen color + + ; refill the fifo buffer with two decoded adpcm blocks (252 nibbles -> 1008 bytes per block) + repeat 2 { + adpcm.init(peekw(nibblesptr), @(nibblesptr+2)) + nibblesptr += 4 + repeat 252 { + ubyte @zp nibble = @(nibblesptr) + adpcm.decode_nibble(nibble & 15) ; first word + cx16.VERA_AUDIO_DATA = lsb(adpcm.predict) + cx16.VERA_AUDIO_DATA = msb(adpcm.predict) + adpcm.decode_nibble(nibble>>4) ; second word + cx16.VERA_AUDIO_DATA = lsb(adpcm.predict) + cx16.VERA_AUDIO_DATA = msb(adpcm.predict) + nibblesptr++ + } + + adpcm_blocks_left-- + if adpcm_blocks_left==0 { + ; restart adpcm data from the beginning + nibblesptr = &audiodata.adpcm_data + adpcm_blocks_left = num_adpcm_blocks + } + } + + } else { + ; TODO not AFLOW, handle other IRQ + } + + ;; cx16.vpoke(1,$fa0c, 0) ; back to other screen color + + %asm {{ + ply + plx + pla + rti + }} + } + +} + +adpcm { + + ; IMA ADPCM decoder. + ; https://wiki.multimedia.cx/index.php/IMA_ADPCM + ; https://wiki.multimedia.cx/index.php/Microsoft_IMA_ADPCM + + ubyte[] t_index = [ -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8] + uword[] t_step = [ + 7, 8, 9, 10, 11, 12, 13, 14, + 16, 17, 19, 21, 23, 25, 28, 31, + 34, 37, 41, 45, 50, 55, 60, 66, + 73, 80, 88, 97, 107, 118, 130, 143, + 157, 173, 190, 209, 230, 253, 279, 307, + 337, 371, 408, 449, 494, 544, 598, 658, + 724, 796, 876, 963, 1060, 1166, 1282, 1411, + 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, + 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, + 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, + 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, + 32767] + + uword @zp predict + ubyte @zp index + uword @zp pstep + + sub init(uword startPredict, ubyte startIndex) { + predict = startPredict + index = startIndex + pstep = t_step[index] + } + + sub decode_nibble(ubyte nibble) { + ; this is the hotspot of the decoder algorithm! + cx16.r0s = 0 ; difference + if nibble & %0100 + cx16.r0s += pstep + pstep >>= 1 + if nibble & %0010 + cx16.r0s += pstep + pstep >>= 1 + if nibble & %0001 + cx16.r0s += pstep + pstep >>= 1 + cx16.r0s += pstep + if nibble & %1000 + cx16.r0s = -cx16.r0s + predict += cx16.r0s as uword + index += t_index[nibble] + if index & 128 + index = 0 + else if index > len(t_step)-1 + index = len(t_step)-1 + pstep = t_step[index] + } +} + + +audiodata { + ;; %option align_page +adpcm_data: + %asmbinary "adpcm-mono.bin" +adpcm_data_end: + +} diff --git a/examples/cx16/pcmaudio/pcm-mono.bin b/examples/cx16/pcmaudio/pcm-mono.bin new file mode 100644 index 000000000..809470f7a Binary files /dev/null and b/examples/cx16/pcmaudio/pcm-mono.bin differ diff --git a/examples/cx16/pcmaudio/pcmplay1.asm b/examples/cx16/pcmaudio/pcmplay1.asm new file mode 100644 index 000000000..0053ff611 --- /dev/null +++ b/examples/cx16/pcmaudio/pcmplay1.asm @@ -0,0 +1,149 @@ +; +; This program plays a short mono 16 bit PCM sample on repeat +; It uses no IRQs but just checks the Fifo Full/Empty bits to see when it needs to copy more data. +; The flashing green bar is when the routine is busy copying sample data into the fifo. +; +; source code is for 64TASS, assemble with: 64tass pcmplay1.asm -o pcmplay1.prg +; + +.cpu 'w65c02' +.enc 'none' + +* = $0801 + ; basic launcher + .word $080b, 2022 + .text $9e, "2061", $00 + .word 0 +entrypoint: + + +CHROUT = $ffd2 +VERA_ADDR_L = $9f20 +VERA_ADDR_M = $9f21 +VERA_ADDR_H = $9f22 +VERA_DATA0 = $9f23 +VERA_CTRL = $9f25 +VERA_IEN = $9f26 +VERA_ISR = $9f27 +VERA_AUDIO_CTRL = $9f3b +VERA_AUDIO_RATE = $9f3c +VERA_AUDIO_DATA = $9f3d +IRQ_VECTOR = $0314 +r0 = $02 +r0L = $02 +r0H = $03 +r1 = $04 +r1L = $04 +r1H = $05 +pcm_ptr = $06 + + + ; stop playback and select mono 16 bit, max volume + stz VERA_AUDIO_RATE + lda #%10101111 + sta VERA_AUDIO_CTRL + + ; fill the fifo with some of silence + ldy #1024/256 +_z1: ldx #0 +_z2: stz VERA_AUDIO_DATA + dex + bne _z2 + dey + bne _z1 + + ldx #audio_on_txt + jsr print + + ldx #pcm_data + stx pcm_ptr + sty pcm_ptr+1 + + lda #21 + sta VERA_AUDIO_RATE ; start playing + +play_loop: +_wait_for_empty_fifo: + bit VERA_AUDIO_CTRL ; fifo empty? + bvc _wait_for_empty_fifo + + ; fifo is empty, we go ahead and fill it with more sound data. + ; paint a screen color + stz VERA_CTRL + lda #$0c + sta VERA_ADDR_L + lda #$fa + sta VERA_ADDR_M + lda #$01 + sta VERA_ADDR_H + lda #$a0 + sta VERA_DATA0 + + +_copy_samples: + ; this assumes all samples are aligned to page size! + ; so we can at least copy a full page at once here. + ldy #0 +_c1 lda (pcm_ptr),y + sta VERA_AUDIO_DATA ; lsb + iny + lda (pcm_ptr),y + sta VERA_AUDIO_DATA ; msb + iny + bne _c1 + inc pcm_ptr+1 + + ; have we reached the end of the sample data? (pcm_ptr >= pcm_data_end) + ; due to page size alignment only the MSB has to be checked + lda pcm_ptr+1 + cmp #>pcm_data_end + bcc _continue + bne _end_reached +; lda pcm_ptr ; uncomment if lsb also needs to be checked +; cmp #pcm_data + sta pcm_ptr+1 + bra _continue + +stop_playback: + ; stop playback + stz VERA_AUDIO_RATE + stz VERA_AUDIO_CTRL + ldx #audio_off_txt + jsr print + rts + +audio_on_txt: .text "AUDIO ON", $0d, $00 +audio_off_txt: .text "AUDIO OFF", $0d, $00 + +print: ; -- print string pointed to by X/Y + stx r0L + sty r0H + ldy #0 +_chr: lda (r0),y + beq _done + jsr $ffd2 + iny + bne _chr +_done: rts + + .align $0100 +pcm_data: + .binary "pcm-mono.bin" +pcm_data_end: diff --git a/examples/cx16/pcmaudio/pcmplay2.asm b/examples/cx16/pcmaudio/pcmplay2.asm new file mode 100644 index 000000000..a1a448b2e --- /dev/null +++ b/examples/cx16/pcmaudio/pcmplay2.asm @@ -0,0 +1,167 @@ +; +; This program plays a short mono 16 bit PCM sample on repeat +; It uses the AFLOW IRQ that signals when the Fifo is about to drain empty. +; The flashing green bar is when the routine is busy copying sample data into the fifo. +; +; source code is for 64TASS, assemble with: 64tass pcmplay2.asm -o pcmplay2.prg +; + +.cpu 'w65c02' +.enc 'none' + +* = $0801 + ; basic launcher + .word $080b, 2022 + .text $9e, "2061", $00 + .word 0 +entrypoint: + + +CHROUT = $ffd2 +VERA_ADDR_L = $9f20 +VERA_ADDR_M = $9f21 +VERA_ADDR_H = $9f22 +VERA_DATA0 = $9f23 +VERA_CTRL = $9f25 +VERA_IEN = $9f26 +VERA_ISR = $9f27 +VERA_AUDIO_CTRL = $9f3b +VERA_AUDIO_RATE = $9f3c +VERA_AUDIO_DATA = $9f3d +IRQ_VECTOR = $0314 +r0 = $02 +r0L = $02 +r0H = $03 +r1 = $04 +r1L = $04 +r1H = $05 +pcm_ptr = $06 + + + ; stop playback and select mono 16 bit, max volume + stz VERA_AUDIO_RATE + lda #%10101111 + sta VERA_AUDIO_CTRL + + ; fill the fifo with some silence + ldy #1024/256 +_z1: ldx #0 +_z2: stz VERA_AUDIO_DATA + dex + bne _z2 + dey + bne _z1 + + ldx #audio_on_txt + jsr print + + ldx #pcm_data + stx pcm_ptr + sty pcm_ptr+1 + + ; set interrupt handler + sei + ldx #irq_handler + stx IRQ_VECTOR + sty IRQ_VECTOR+1 + lda #%00001000 ; enable the AFLOW irq + sta VERA_IEN + cli + + lda #21 + sta VERA_AUDIO_RATE ; start playback + +_wait: + wai + bra _wait + + +irq_handler: + lda VERA_ISR + and #%00001000 ; is aflow? + bne _aflow_irq + ; TODO other irq + bra _exit_irq + +_aflow_irq: + ; paint a screen color + stz VERA_CTRL + lda #$0c + sta VERA_ADDR_L + lda #$fa + sta VERA_ADDR_M + lda #$01 + sta VERA_ADDR_H + lda #$a0 + sta VERA_DATA0 + + ; refill fifo buffer (minimum 1Kb = 1/4) + ; this assumes all samples are aligned to page size! + ; so we can at least copy a full page at once here. + ldx #8 ; number of pages to copy +_copy_more_pages: + ldy #0 +_c1: lda (pcm_ptr),y + sta VERA_AUDIO_DATA ; lsb + iny + lda (pcm_ptr),y + sta VERA_AUDIO_DATA ; msb + iny + ; (in case of 16 bit stereo output, this can be unrolled by 2 bytes again). + bne _c1 + inc pcm_ptr+1 + + ; have we reached the end of the sample data? (pcm_ptr >= pcm_data_end) + ; due to page size alignment only the MSB has to be checked + lda pcm_ptr+1 + cmp #>pcm_data_end + bcc _not_at_end + bne _end_reached +; lda pcm_ptr ; uncomment if lsb also needs to be checked +; cmp #pcm_data + sta pcm_ptr+1 + bra _not_at_end + + +audio_on_txt: .text "AUDIO ON (IRQ)", $0d, $00 + +print: ; -- print string pointed to by X/Y + stx r0L + sty r0H + ldy #0 +_chr: lda (r0),y + beq _done + jsr $ffd2 + iny + bne _chr +_done: rts + + .align $0100 +pcm_data: + .binary "pcm-mono.bin" +pcm_data_end: