Debian Codegraph @

src/engine/platform/sound/gb/apu.h

raw
1 #ifndef apu_h
2 #define apu_h
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <stddef.h>
6 #include "gb_struct_def.h"
7
8 /* Speed = 1 / Length (in seconds) */
9 #define DAC_DECAY_SPEED 20000
10 #define DAC_ATTACK_SPEED 20000
11
12
13 /* Divides nicely and never overflows with 4 channels and 8 (1-8) volume levels */
14 #ifdef WIIU
15 /* Todo: Remove this hack once https://github.com/libretro/RetroArch/issues/6252 is fixed*/
16 #define MAX_CH_AMP (0xFF0 / 2)
17 #else
18 #define MAX_CH_AMP 0xFF0
19 #endif
20 #define CH_STEP (MAX_CH_AMP/0xF/8)
21
22
23
24 /* APU ticks are 2MHz, triggered by an internal APU clock. */
25
26 typedef struct
27 {
28 int16_t left;
29 int16_t right;
30 } GB_sample_t;
31
32 typedef struct
33 {
34 double left;
35 double right;
36 } GB_double_sample_t;
37
38 enum GB_CHANNELS {
39 GB_SQUARE_1,
40 GB_SQUARE_2,
41 GB_WAVE,
42 GB_NOISE,
43 GB_N_CHANNELS
44 };
45
46 typedef struct
47 {
48 bool locked:1;
49 bool clock:1; // Represents FOSY on channel 4
50 unsigned padding:6;
51 } GB_envelope_clock_t;
52
53 typedef void (*GB_sample_callback_t)(GB_gameboy_t *gb, GB_sample_t *sample);
54
55 typedef struct
56 {
57 bool global_enable;
58 uint8_t apu_cycles;
59
60 uint8_t samples[GB_N_CHANNELS];
61 bool is_active[GB_N_CHANNELS];
62
63 uint8_t div_divider; // The DIV register ticks the APU at 512Hz, but is then divided
64 // once more to generate 128Hz and 64Hz clocks
65
66 uint8_t lf_div; // The APU runs in 2MHz, but channels 1, 2 and 4 run in 1MHZ so we divide
67 // need to divide the signal.
68
69 uint8_t square_sweep_countdown; // In 128Hz
70 uint8_t square_sweep_calculate_countdown; // In 2 MHz
71 uint16_t sweep_length_addend;
72 uint16_t shadow_sweep_sample_length;
73 bool unshifted_sweep;
74 bool enable_zombie_calculate_stepping;
75
76 struct {
77 uint16_t pulse_length; // Reloaded from NRX1 (xorred), in 256Hz DIV ticks
78 uint8_t current_volume; // Reloaded from NRX2
79 uint8_t volume_countdown; // Reloaded from NRX2
80 uint8_t current_sample_index; /* For save state compatibility,
81 highest bit is reused (See NR14/NR24's
82 write code)*/
83
84 uint16_t sample_countdown; // in APU ticks (Reloaded from sample_length, xorred $7FF)
85 uint16_t sample_length; // From NRX3, NRX4, in APU ticks
86 bool length_enabled; // NRX4
87
88 } square_channels[2];
89
90 struct {
91 bool enable; // NR30
92 uint16_t pulse_length; // Reloaded from NR31 (xorred), in 256Hz DIV ticks
93 uint8_t shift; // NR32
94 uint16_t sample_length; // NR33, NR34, in APU ticks
95 bool length_enabled; // NR34
96 bool double_length; // NR30
97 bool bank_select; // NR30
98 bool force_3; // NR32
99
100 uint16_t sample_countdown; // in APU ticks (Reloaded from sample_length, xorred $7FF)
101 uint8_t current_sample_index;
102 uint8_t current_sample; // Current sample before shifting.
103
104 int8_t wave_form[64];
105 bool wave_form_just_read;
106 } wave_channel;
107
108 struct {
109 uint16_t pulse_length; // Reloaded from NR41 (xorred), in 256Hz DIV ticks
110 uint8_t current_volume; // Reloaded from NR42
111 uint8_t volume_countdown; // Reloaded from NR42
112 uint16_t lfsr;
113 bool narrow;
114
115 uint8_t counter_countdown; // Counts from 0-7 to 0 to tick counter (Scaled from 512KHz to 2MHz)
116 uint8_t __padding;
117 uint16_t counter; // A bit from this 14-bit register ticks LFSR
118 bool length_enabled; // NR44
119
120 uint8_t alignment; // If (NR43 & 7) != 0, samples are aligned to 512KHz clock instead of
121 // 1MHz. This variable keeps track of the alignment.
122
123 } noise_channel;
124
125 #define GB_SKIP_DIV_EVENT_INACTIVE 0
126 #define GB_SKIP_DIV_EVENT_SKIPPED 1
127 #define GB_SKIP_DIV_EVENT_SKIP 2
128 uint8_t skip_div_event;
129 bool current_lfsr_sample;
130 uint8_t pcm_mask[2]; // For CGB-0 to CGB-C PCM read glitch
131 uint8_t channel_1_restart_hold;
132 int8_t channel_4_delta;
133 bool channel_4_countdown_reloaded;
134 uint8_t channel_4_dmg_delayed_start;
135 uint16_t channel1_completed_addend;
136
137 GB_envelope_clock_t square_envelope_clock[2];
138 GB_envelope_clock_t noise_envelope_clock;
139 } GB_apu_t;
140
141 typedef enum {
142 GB_HIGHPASS_OFF, // Do not apply any filter, keep DC offset
143 GB_HIGHPASS_ACCURATE, // Apply a highpass filter similar to the one used on hardware
144 GB_HIGHPASS_REMOVE_DC_OFFSET, // Remove DC Offset without affecting the waveform
145 GB_HIGHPASS_MAX
146 } GB_highpass_mode_t;
147
148 typedef struct {
149 unsigned sample_rate;
150
151 double sample_cycles; // In 8 MHz units
152 double cycles_per_sample;
153
154 // Samples are NOT normalized to MAX_CH_AMP * 4 at this stage!
155 unsigned cycles_since_render;
156 unsigned last_update[GB_N_CHANNELS];
157 GB_sample_t current_sample[GB_N_CHANNELS];
158 GB_sample_t summed_samples[GB_N_CHANNELS];
159 double dac_discharge[GB_N_CHANNELS];
160
161 GB_highpass_mode_t highpass_mode;
162 double highpass_rate;
163 GB_double_sample_t highpass_diff;
164
165 GB_sample_callback_t sample_callback;
166
167 GB_sample_t final_sample;
168
169 bool rate_set_in_clocks;
170 double interference_volume;
171 double interference_highpass;
172 } GB_apu_output_t;
173
174 void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate);
175 void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, double cycles_per_sample); /* Cycles are in 8MHz units */
176 void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode);
177 void GB_set_interference_volume(GB_gameboy_t *gb, double volume);
178 void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback);
179
180 bool GB_apu_is_DAC_enabled(GB_gameboy_t *gb, unsigned index);
181 void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value);
182 uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg);
183 void GB_apu_div_event(GB_gameboy_t *gb);
184 void GB_apu_div_secondary_event(GB_gameboy_t *gb);
185 void GB_apu_init(GB_gameboy_t *gb);
186 void GB_apu_run(GB_gameboy_t *gb);
187 void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb);
188 void GB_borrow_sgb_border(GB_gameboy_t *gb);
189
190 #endif /* apu_h */