Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; ---------------------------------------------------------------------------
- ; Lifts frequency for a note from a LUT and writes it to the PSG chip.
- ;
- ; Input:
- ; a: Note ($00 to $48)
- ; ix: Pointer to channel RAM
- ; ---------------------------------------------------------------------------
- PSG_SetChannelNote:
- add a, (ix+Chan_Pitch) ; Add pitch
- add a, a ; Multiply note by 2
- ld e, a ; Copy multiplied note to e
- ld d, $0 ; Clear d
- ld hl, FrequencyPSG ; PSG freq table to bc
- add hl, de ; Advance to correct frequency data
- ld d, (hl) ; Fetch high byte
- inc hl ; Increment pointer
- ld e, (hl) ; Fetch low byte
- ex de, hl ; HL <-> DE
- ld d, $0 ; Set D to zero
- ld e, (ix+Chan_FreqDisplacement) ; Get frequency displacement to E
- adc hl, de ; Add displacement to note in hl
- ex de, hl ; Swap HL to be DE
- ld (ix+Chan_FreqHigh), d ; Write high frequency to track RAM
- ld (ix+Chan_FreqLow), e ; Write low frequency to track RAM
- ; We've got the frequency in de now, so prepare the register commands
- ld hl, PSG_ChannelToRegMap ; Load pointer to channel -> reg array
- ld a, (ix+Chan_Number) ; Get channel from track memory
- sub $7 ; Turn it into 0-based index
- add l ; Add 0-based channel number to l
- ld b, (hl) ; Read register value
- ld a, e ; Load low byte of frequency
- and $F ; Get low 4 bits only
- or b ; Apply register bitmask
- ld (PSGReg), a ; Write first byte to PSG chip
- ld a, e ; Load low byte of frequency
- and $F0 ; Get high byte of frequency only
- rrca ; Rotate 4 bits
- rrca ; ''
- rrca ; ''
- rrca ; ''
- ld e, a ; Write the low nybble to E
- ld a, d ; Copy high byte of frequency to accumulator
- rrca ; Rotate 4 bits
- rrca ; ''
- rrca ; ''
- rrca ; ''
- or e ; Apply low nybble to frequency value
- ld (PSGReg), a ; Write second byte to PSG chip
- 9:
- ret
- ; This alignment of the table ensures there won't be an overflow when adding the channel number
- align $4
- PSG_ChannelToRegMap:
- db %10000000, %10100000, %11000000
- ; ---------------------------------------------------------------------------
- ; PSG Frequency table
- ; ---------------------------------------------------------------------------
- FrequencyPSG:
- ; C C# D D# E F
- ; F# G G# A A# B
- db $03, $56, $03, $26, $02, $F9, $02, $CE, $02, $A5, $02, $80 ; 4 (01 - 06)
- db $02, $5C, $02, $3A, $02, $1A, $01, $FB, $01, $DF, $01, $C4 ; 4 (07 - 0C)
- db $01, $AB, $01, $93, $01, $7D, $01, $67, $01, $53, $01, $40 ; 5 (0D - 12)
- db $01, $2E, $01, $1D, $01, $0D, $00, $FE, $00, $EF, $00, $E2 ; 5 (13 - 18)
- db $00, $D6, $00, $C9, $00, $BE, $00, $B4, $00, $A9, $00, $A0 ; 6 (19 - 1E)
- db $00, $97, $00, $8F, $00, $87, $00, $7F, $00, $78, $00, $71 ; 6 (1F - 24)
- db $00, $6B, $00, $65, $00, $5F, $00, $5A, $00, $55, $00, $50 ; 7 (25 - 2A)
- db $00, $4B, $00, $47, $00, $43, $00, $40, $00, $3C, $00, $39 ; 7 (2B - 30)
- db $00, $36, $00, $33, $00, $30, $00, $2D, $00, $2B, $00, $28 ; 8 (31 - 36)
- db $00, $26, $00, $24, $00, $22, $00, $20, $00, $1F, $00, $1D ; 8 (37 - 3C)
- db $00, $1B, $00, $1A, $00, $18, $00, $17, $00, $16, $00, $15 ; 9 (3D - 42)
- db $00, $13, $00, $12, $00, $11, $00, $10, $00, $08, $00, $00 ; 9 (43 - 48)
Advertisement
Add Comment
Please, Sign In to add comment