| 1 |
/* |
| 2 |
WonderSwan sound core |
| 3 |
|
| 4 |
Note: Neither Sound DMA nor Hyper Voice DMA is implemented. |
| 5 |
|
| 6 |
Copyright (c) 2025 Adrian "asie" Siekierka |
| 7 |
|
| 8 |
This software is provided 'as-is', without any express or implied |
| 9 |
warranty. In no event will the authors be held liable for any damages |
| 10 |
arising from the use of this software. |
| 11 |
|
| 12 |
Permission is granted to anyone to use this software for any purpose, |
| 13 |
including commercial applications, and to alter it and redistribute it |
| 14 |
freely, subject to the following restrictions: |
| 15 |
|
| 16 |
1. The origin of this software must not be misrepresented; you must not |
| 17 |
claim that you wrote the original software. If you use this software |
| 18 |
in a product, an acknowledgment in the product documentation would be |
| 19 |
appreciated but is not required. |
| 20 |
2. Altered source versions must be plainly marked as such, and must not be |
| 21 |
misrepresented as being the original software. |
| 22 |
3. This notice may not be removed or altered from any source distribution. |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifndef _DRIVER_SWAN_H |
| 26 |
#define _DRIVER_SWAN_H |
| 27 |
|
| 28 |
#include <stdbool.h> |
| 29 |
#include <stdint.h> |
| 30 |
|
| 31 |
typedef struct swan_sound { |
| 32 |
// Ports |
| 33 |
uint16_t frequency[4]; |
| 34 |
uint8_t volume[4]; |
| 35 |
int8_t sweep_amount; |
| 36 |
uint8_t sweep_ticks; |
| 37 |
uint8_t noise_ctrl; |
| 38 |
uint8_t wave_address; |
| 39 |
uint8_t ch_ctrl; |
| 40 |
uint8_t out_ctrl; |
| 41 |
uint16_t noise_lfsr; |
| 42 |
uint8_t voice_volume; |
| 43 |
// TODO: Implement test flag bits 7, 6, 4, 3, 2 |
| 44 |
uint8_t test_flags; |
| 45 |
|
| 46 |
uint8_t wave_ram[64]; |
| 47 |
|
| 48 |
uint16_t hyper_ctrl; |
| 49 |
|
| 50 |
// State |
| 51 |
uint8_t sample_index[4]; |
| 52 |
uint32_t period_counter[4]; |
| 53 |
uint32_t sweep_counter; |
| 54 |
|
| 55 |
// Outputs |
| 56 |
/// Individual channel outputs (range: 0 .. 255) |
| 57 |
int16_t ch_output_right[4]; |
| 58 |
int16_t ch_output_left[4]; |
| 59 |
|
| 60 |
/// Hyper Voice outputs (range: -32768 .. 32767) |
| 61 |
int16_t hyper_output_left; |
| 62 |
int16_t hyper_output_right; |
| 63 |
|
| 64 |
/// Stereo synth outputs (range: 0 .. 1023) |
| 65 |
uint16_t synth_output_right; |
| 66 |
uint16_t synth_output_left; |
| 67 |
|
| 68 |
/// Mono synth output (range: 0 .. 2047) |
| 69 |
uint16_t synth_output_mono; |
| 70 |
|
| 71 |
/// Headphones output (range: -32768 .. 32767) |
| 72 |
int16_t output_right; |
| 73 |
int16_t output_left; |
| 74 |
|
| 75 |
/// Internal speaker output (range: 0 .. 255) |
| 76 |
uint8_t output_speaker; |
| 77 |
} swan_sound_t; |
| 78 |
|
| 79 |
#ifdef __cplusplus |
| 80 |
extern "C" { |
| 81 |
#endif |
| 82 |
|
| 83 |
void swan_sound_init(swan_sound_t *snd, bool headphones); |
| 84 |
uint8_t swan_sound_in(swan_sound_t *snd, uint16_t port); |
| 85 |
void swan_sound_out(swan_sound_t *snd, uint16_t port, uint8_t value); |
| 86 |
|
| 87 |
// The audio chip is typically clocked at 3072000 Hz, with a sample |
| 88 |
// generated once every 128 cycles, for an audio frequency of 24000 Hz. |
| 89 |
// To emit a sample in such a configuration, one should run: |
| 90 |
// |
| 91 |
// swan_sound_tick(&snd, 128); |
| 92 |
// swan_sound_sample(&snd); |
| 93 |
void swan_sound_tick(swan_sound_t *snd, uint32_t cycles); |
| 94 |
void swan_sound_sample(swan_sound_t *snd); |
| 95 |
|
| 96 |
#ifdef __cplusplus |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
#endif |