base:spectrometer
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | base:spectrometer [2017-02-15 17:05] (current) – created trap_bonzai | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | This is the code from T.P.C.T.S. demo that calculates the index values for the spectrometer. There are a few tricks in there, but I am sure many people could have written a more optimized version. | ||
+ | I hope it will help you understand how I did it. | ||
+ | |||
+ | Code can be compiled using KickAss. | ||
+ | |||
+ | /Trap | ||
+ | |||
+ | |||
+ | < | ||
+ | .const SID_Ghostbytes = $40 // Location of SID Ghostbytes (16 bytes) | ||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////////// | ||
+ | // Spectrometer | ||
+ | ///////////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | Spectrometer: | ||
+ | |||
+ | DecreaseBarIndexes: | ||
+ | !: lda MeterTemp, | ||
+ | beq dBMeterUpdate_NoDec | ||
+ | dec MeterTemp,x | ||
+ | dBMeterUpdate_NoDec: | ||
+ | lda SoundbarSine, | ||
+ | sta dBMeterValue, | ||
+ | dex | ||
+ | bpl !- | ||
+ | |||
+ | ldx SID_Ghostbytes | ||
+ | lda SID_Ghostbytes+1 | ||
+ | jsr GetNote | ||
+ | lda SID_Ghostbytes+6 | ||
+ | and #$f0 | ||
+ | lsr | ||
+ | lsr | ||
+ | lsr | ||
+ | lsr | ||
+ | clc | ||
+ | adc #6 | ||
+ | sta MeterTemp, | ||
+ | jsr CalculateSurroundings | ||
+ | |||
+ | ldx SID_Ghostbytes+7 | ||
+ | lda SID_Ghostbytes+8 | ||
+ | jsr GetNote | ||
+ | lda SID_Ghostbytes+$d | ||
+ | and #$f0 | ||
+ | lsr | ||
+ | lsr | ||
+ | lsr | ||
+ | lsr | ||
+ | clc | ||
+ | adc #6 | ||
+ | sta MeterTemp,x | ||
+ | jsr CalculateSurroundings | ||
+ | |||
+ | ldx SID_Ghostbytes+$e | ||
+ | lda SID_Ghostbytes+$f | ||
+ | jsr GetNote | ||
+ | lda SID_Ghostbytes+$14 | ||
+ | and #$f0 | ||
+ | lsr | ||
+ | lsr | ||
+ | lsr | ||
+ | lsr | ||
+ | clc | ||
+ | adc #6 | ||
+ | sta MeterTemp,x | ||
+ | jsr CalculateSurroundings | ||
+ | rts | ||
+ | |||
+ | GetNote: | ||
+ | sta NoteHi | ||
+ | ldx #8 | ||
+ | ldy #0 // Search iteration counter | ||
+ | IterateBinarySearch: | ||
+ | sta CompareHi+1 | ||
+ | lda FreqTableLookupHimsb, | ||
+ | sta CompareHi+2 | ||
+ | lda FreqTableLookupLolsb, | ||
+ | sta CompareLo+1 | ||
+ | lda FreqTableLookupLomsb, | ||
+ | sta CompareLo+2 | ||
+ | |||
+ | lda NoteHi | ||
+ | CompareHi: | ||
+ | bcc Lower1 | ||
+ | bne Higher1 | ||
+ | lda NoteLo | ||
+ | CompareLo: | ||
+ | bcs Higher1 | ||
+ | rts | ||
+ | |||
+ | Lower1: | ||
+ | sec | ||
+ | sbc Delta,y | ||
+ | tax | ||
+ | iny | ||
+ | cpy #4 // Control max number of iterations | ||
+ | beq Done | ||
+ | jmp IterateBinarySearch | ||
+ | |||
+ | Higher1: | ||
+ | clc | ||
+ | adc Delta,y | ||
+ | tax | ||
+ | iny | ||
+ | cpy #4 | ||
+ | beq Done | ||
+ | jmp IterateBinarySearch | ||
+ | |||
+ | Done: | ||
+ | |||
+ | CalculateSurroundings: | ||
+ | cmp MeterTemp, | ||
+ | bcc LeftIsLower | ||
+ | lda MeterTemp-2, | ||
+ | sec | ||
+ | sbc MeterTemp,x | ||
+ | lsr | ||
+ | clc | ||
+ | adc MeterTemp,x | ||
+ | sta MeterTemp-1, | ||
+ | jmp LeftSurround | ||
+ | LeftIsLower: | ||
+ | sec | ||
+ | sbc MeterTemp-2, | ||
+ | lsr | ||
+ | clc | ||
+ | adc MeterTemp-2, | ||
+ | LeftSurround: | ||
+ | bcc !+ | ||
+ | lda #21 | ||
+ | !: sta MeterTemp-1, | ||
+ | |||
+ | lda MeterTemp+2, | ||
+ | cmp MeterTemp,x | ||
+ | bcc LeftIsLower2 | ||
+ | lda MeterTemp+2, | ||
+ | sec | ||
+ | sbc MeterTemp,x | ||
+ | lsr | ||
+ | clc | ||
+ | adc MeterTemp,x | ||
+ | sta MeterTemp+1, | ||
+ | jmp LeftSurround2 | ||
+ | LeftIsLower2: | ||
+ | sec | ||
+ | sbc MeterTemp+2, | ||
+ | lsr | ||
+ | clc | ||
+ | adc MeterTemp+2, | ||
+ | LeftSurround2: | ||
+ | bcc !+ | ||
+ | lda #21 | ||
+ | !: sta MeterTemp+1, | ||
+ | rts | ||
+ | |||
+ | |||
+ | FreqTableLookupHilsb: | ||
+ | .byte < | ||
+ | } | ||
+ | FreqTableLookupHimsb: | ||
+ | .byte > | ||
+ | } | ||
+ | FreqTableLookupLolsb: | ||
+ | .byte < | ||
+ | } | ||
+ | FreqTableLookupLomsb: | ||
+ | .byte > | ||
+ | } | ||
+ | |||
+ | temp: .byte 0 | ||
+ | NoteLo: | ||
+ | NoteHi: | ||
+ | Delta: | ||
+ | |||
+ | FreqTablePalLo: | ||
+ | .byte $17, | ||
+ | .byte $2d, | ||
+ | .byte $5a, | ||
+ | .byte $b4, | ||
+ | .byte $68, | ||
+ | .byte $d0, | ||
+ | .byte $a1, | ||
+ | .byte $42, | ||
+ | FreqTablePalHi: | ||
+ | .byte $01, | ||
+ | .byte $02, | ||
+ | .byte $04, | ||
+ | .byte $08, | ||
+ | .byte $11, | ||
+ | .byte $22, | ||
+ | .byte $45, | ||
+ | .byte $8b, | ||
+ | |||
+ | .byte 0,0 | ||
+ | dBMeterValue: | ||
+ | .byte 0,0 | ||
+ | MeterTemp: | ||
+ | .byte 0,0 | ||
+ | |||
+ | // Quick' | ||
+ | SoundbarSine: | ||
+ | |||
+ | /////////////////////////////////////////////////////////////////////////////////////////// | ||
+ | // Play music using ghostbytes | ||
+ | /////////////////////////////////////////////////////////////////////////////////////////// | ||
+ | PlayMusic: | ||
+ | pha | ||
+ | lda #$30 | ||
+ | sta $01 | ||
+ | jsr $1003 | ||
+ | ldx #$19 | ||
+ | !CopySIDData: | ||
+ | sta SID_Ghostbytes, | ||
+ | dex | ||
+ | bpl !CopySIDData- | ||
+ | pla | ||
+ | sta $01 | ||
+ | ldx #$19 | ||
+ | !CopyToSID: | ||
+ | sta $d400,x | ||
+ | dex | ||
+ | bpl !CopyToSID- | ||
+ | rts | ||
+ | </ |
base/spectrometer.txt · Last modified: 2017-02-15 17:05 by trap_bonzai