; 6502 - AY-3-819x PSG
;
; Copyright (c) 2021 Troy Schrapel
;
; This code is licensed under the MIT license
;
; https://github.com/visrealm/hbc-56
;

!src "sfx/ay3891x.inc"


!ifndef AY_IO_PORT { AY_IO_PORT = $40
        !warn "AY_IO_PORT not provided. Defaulting to ", AY_IO_PORT
}

HAVE_AY3891X = 1

; -------------------------
; Constants
; -------------------------
AY_PSG0 = $00
AY_PSG1 = $04

; IO Ports
AY_S0 = IO_PORT_BASE_ADDRESS | AY_IO_PORT | AY_PSG0
AY_S1 = IO_PORT_BASE_ADDRESS | AY_IO_PORT | AY_PSG1

AY_INACTIVE = $03
AY_READ     = $02
AY_WRITE    = $01
AY_ADDR     = $00

AY_S0_READ  = AY_S0 | AY_READ
AY_S0_WRITE = AY_S0 | AY_WRITE
AY_S0_ADDR  = AY_S0 | AY_ADDR

AY_S1_READ  = AY_S1 | AY_READ
AY_S1_WRITE = AY_S1 | AY_WRITE
AY_S1_ADDR  = AY_S1 | AY_ADDR

; Registers (they're named using Octal values)
AY_R0 = 0
AY_R1 = 1
AY_R2 = 2
AY_R3 = 3
AY_R4 = 4
AY_R5 = 5
AY_R6 = 6
AY_R7 = 7
AY_R10 = 8
AY_R11 = 9
AY_R12 = 10
AY_R13 = 11
AY_R14 = 12
AY_R15 = 13
AY_R16 = 14
AY_R17 = 15

AY_CHA = 0
AY_CHB = 1
AY_CHC = 2
AY_CHN = 3

AY_CHA_TONE_L   = AY_R0
AY_CHA_TONE_H   = AY_R1
AY_CHB_TONE_L   = AY_R2
AY_CHB_TONE_H   = AY_R3
AY_CHC_TONE_L   = AY_R4
AY_CHC_TONE_H   = AY_R5
AY_NOISE_GEN    = AY_R6
AY_ENABLES      = AY_R7
AY_CHA_AMPL     = AY_R10
AY_CHB_AMPL     = AY_R11
AY_CHC_AMPL     = AY_R12
AY_ENV_PERIOD_L = AY_R13
AY_ENV_PERIOD_H = AY_R14
AY_ENV_SHAPE    = AY_R15
AY_PORTA        = AY_R16
AY_PORTB        = AY_R17

AY_ENV_SHAPE_CONTINUE  = $08
AY_ENV_SHAPE_ATTACK    = $04
AY_ENV_SHAPE_ALTERNATE = $02
AY_ENV_SHAPE_HOLD      = $01

;  /\  /\  /\  /\  /\ 
; /  \/  \/  \/  \/  \
AY_ENV_SHAPE_TRIANGLE     = (AY_ENV_SHAPE_CONTINUE | AY_ENV_SHAPE_ATTACK | AY_ENV_SHAPE_ALTERNATE)

; \  /\  /\  /\  /\  /
;  \/  \/  \/  \/  \/
AY_ENV_SHAPE_TRIANGLE2    = (AY_ENV_SHAPE_CONTINUE | AY_ENV_SHAPE_ALTERNATE)

;  /------------------
; /
AY_ENV_SHAPE_FADE_IN      = (AY_ENV_SHAPE_CONTINUE | AY_ENV_SHAPE_ATTACK | AY_ENV_SHAPE_HOLD)

; \
;  \__________________
AY_ENV_SHAPE_FADE_OUT     = (AY_ENV_SHAPE_CONTINUE | AY_ENV_SHAPE_HOLD)

; \ |\ |\ |\ |\ |\ |\ |
;  \| \| \| \| \| \| \|
AY_ENV_SHAPE_SAW1         = (AY_ENV_SHAPE_CONTINUE)

;  /| /| /| /| /| /| /|
; / |/ |/ |/ |/ |/ |/ |
AY_ENV_SHAPE_SAW2         = (AY_ENV_SHAPE_CONTINUE | AY_ENV_SHAPE_ATTACK)

;  /|
; / |__________________
AY_ENV_SHAPE_FADE_IN_STOP = (AY_ENV_SHAPE_ATTACK)


AY_CLOCK_FREQ   = 2000000

ayInit:
        ; disable everything
        +ayWrite AY_PSG0, AY_ENABLES, $ff
        +ayWrite AY_PSG1, AY_ENABLES, $ff

        +aySetVolume AY_PSG0, AY_CHA, 0
        +aySetVolume AY_PSG0, AY_CHB, 0
        +aySetVolume AY_PSG0, AY_CHC, 0

        +ayPlayNote AY_PSG0, AY_CHA, 0
        +ayPlayNote AY_PSG0, AY_CHB, 0
        +ayPlayNote AY_PSG0, AY_CHC, 0

        +aySetEnvelopePeriod AY_PSG0, 0
        +aySetEnvShape AY_PSG0, 0
        +aySetNoise  AY_PSG0, 0

        +aySetVolume AY_PSG1, AY_CHA, 0
        +aySetVolume AY_PSG1, AY_CHB, 0
        +aySetVolume AY_PSG1, AY_CHC, 0

        +ayPlayNote AY_PSG1, AY_CHA, 0
        +ayPlayNote AY_PSG1, AY_CHB, 0
        +ayPlayNote AY_PSG1, AY_CHC, 0

        +aySetEnvelopePeriod AY_PSG1, 0
        +aySetEnvShape AY_PSG1, 0
        +aySetNoise  AY_PSG1, 0
        rts

; Note frequencies from https://pages.mtu.edu/~suits/notefreqs.html

NOTE_FREQ_C0  = 16.35
NOTE_FREQ_CS0 = 17.32
NOTE_FREQ_D0  = 18.35
NOTE_FREQ_DS0 = 19.45
NOTE_FREQ_E0  = 20.6
NOTE_FREQ_F0  = 21.83
NOTE_FREQ_FS0 = 23.12
NOTE_FREQ_G0  = 24.5
NOTE_FREQ_GS0 = 25.96
NOTE_FREQ_A0  = 27.5
NOTE_FREQ_AS0 = 29.14
NOTE_FREQ_B0  = 30.87
NOTE_FREQ_C1  = 32.7
NOTE_FREQ_CS1 = 34.65
NOTE_FREQ_D1  = 36.71
NOTE_FREQ_DS1 = 38.89
NOTE_FREQ_E1  = 41.2
NOTE_FREQ_F1  = 43.65
NOTE_FREQ_FS1 = 46.25
NOTE_FREQ_G1  = 49
NOTE_FREQ_GS1 = 51.91
NOTE_FREQ_A1  = 55
NOTE_FREQ_AS1 = 58.27
NOTE_FREQ_B1  = 61.74
NOTE_FREQ_C2  = 65.41
NOTE_FREQ_CS2 = 69.3
NOTE_FREQ_D2  = 73.42
NOTE_FREQ_DS2 = 77.78
NOTE_FREQ_E2  = 82.41
NOTE_FREQ_F2  = 87.31
NOTE_FREQ_FS2 = 92.5
NOTE_FREQ_G2  = 98
NOTE_FREQ_GS2 = 103.83
NOTE_FREQ_A2  = 110
NOTE_FREQ_AS2 = 116.54
NOTE_FREQ_B2  = 123.47
NOTE_FREQ_C3  = 130.81
NOTE_FREQ_CS3 = 138.59
NOTE_FREQ_D3  = 146.83
NOTE_FREQ_DS3 = 155.56
NOTE_FREQ_E3  = 164.81
NOTE_FREQ_F3  = 174.61
NOTE_FREQ_FS3 = 185
NOTE_FREQ_G3  = 196
NOTE_FREQ_GS3 = 207.65
NOTE_FREQ_A3  = 220
NOTE_FREQ_AS3 = 233.08
NOTE_FREQ_B3  = 246.94
NOTE_FREQ_C4  = 261.63
NOTE_FREQ_CS4 = 277.18
NOTE_FREQ_D4  = 293.66
NOTE_FREQ_DS4 = 311.13
NOTE_FREQ_E4  = 329.63
NOTE_FREQ_F4  = 349.23
NOTE_FREQ_FS4 = 369.99
NOTE_FREQ_G4  = 392
NOTE_FREQ_GS4 = 415.3
NOTE_FREQ_A4  = 440
NOTE_FREQ_AS4 = 466.16
NOTE_FREQ_B4  = 493.88
NOTE_FREQ_C5  = 523.25
NOTE_FREQ_CS5 = 554.37
NOTE_FREQ_D5  = 587.33
NOTE_FREQ_DS5 = 622.25
NOTE_FREQ_E5  = 659.25
NOTE_FREQ_F5  = 698.46
NOTE_FREQ_FS5 = 739.99
NOTE_FREQ_G5  = 783.99
NOTE_FREQ_GS5 = 830.61
NOTE_FREQ_A5  = 880
NOTE_FREQ_AS5 = 932.33
NOTE_FREQ_B5  = 987.77
NOTE_FREQ_C6  = 1046.5
NOTE_FREQ_CS6 = 1108.73
NOTE_FREQ_D6  = 1174.66
NOTE_FREQ_DS6 = 1244.51
NOTE_FREQ_E6  = 1318.51
NOTE_FREQ_F6  = 1396.91
NOTE_FREQ_FS6 = 1479.98
NOTE_FREQ_G6  = 1567.98
NOTE_FREQ_GS6 = 1661.22
NOTE_FREQ_A6  = 1760
NOTE_FREQ_AS6 = 1864.66
NOTE_FREQ_B6  = 1975.53
NOTE_FREQ_C7  = 2093
NOTE_FREQ_CS7 = 2217.46
NOTE_FREQ_D7  = 2349.32
NOTE_FREQ_DS7 = 2489.02
NOTE_FREQ_E7  = 2637.02
NOTE_FREQ_F7  = 2793.83
NOTE_FREQ_FS7 = 2959.96
NOTE_FREQ_G7  = 3135.96
NOTE_FREQ_GS7 = 3322.44
NOTE_FREQ_A7  = 3520
NOTE_FREQ_AS7 = 3729.31
NOTE_FREQ_B7  = 3951.07
NOTE_FREQ_C8  = 4186.01
NOTE_FREQ_CS8 = 4434.92
NOTE_FREQ_D8  = 4698.63
NOTE_FREQ_DS8 = 4978.03
NOTE_FREQ_E8  = 5274.04
NOTE_FREQ_F8  = 5587.65
NOTE_FREQ_FS8 = 5919.91
NOTE_FREQ_G8  = 6271.93
NOTE_FREQ_GS8 = 6644.88
NOTE_FREQ_A8  = 7040
NOTE_FREQ_AS8 = 7458.62
NOTE_FREQ_B8  = 7902.13
