summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libgme/nes_fme7_apu.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libgme/nes_fme7_apu.h')
-rw-r--r--lib/rbcodec/codecs/libgme/nes_fme7_apu.h90
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/nes_fme7_apu.h b/lib/rbcodec/codecs/libgme/nes_fme7_apu.h
new file mode 100644
index 0000000000..c0eac4c765
--- /dev/null
+++ b/lib/rbcodec/codecs/libgme/nes_fme7_apu.h
@@ -0,0 +1,90 @@
1// Sunsoft FME-7 sound emulator
2
3// Game_Music_Emu 0.6-pre
4#ifndef NES_FME7_APU_H
5#define NES_FME7_APU_H
6
7#include "blargg_common.h"
8#include "blip_buffer.h"
9
10enum { fme7_reg_count = 14 };
11
12// Mask and addresses of registers
13enum { fme7_addr_mask = 0xE000 };
14enum { fme7_data_addr = 0xE000 };
15enum { fme7_latch_addr = 0xC000 };
16enum { fme7_osc_count = 3 };
17
18enum { amp_range = 192 }; // can be any value; this gives best error/quality tradeoff
19
20struct osc_t {
21 struct Blip_Buffer* output;
22 int last_amp;
23};
24
25// static unsigned char const amp_table [16];
26
27struct Nes_Fme7_Apu {
28 // fme7 apu state
29 uint8_t regs [fme7_reg_count];
30 uint8_t phases [3]; // 0 or 1
31 uint8_t latch;
32 uint16_t delays [3]; // a, b, c
33
34 struct osc_t oscs [fme7_osc_count];
35 blip_time_t last_time;
36
37 struct Blip_Synth synth;
38};
39
40// See Nes_Apu.h for reference
41void Fme7_init( struct Nes_Fme7_Apu* this );
42void Fme7_reset( struct Nes_Fme7_Apu* this );
43
44static inline void Fme7_volume( struct Nes_Fme7_Apu* this, int v )
45{
46 Synth_volume( &this->synth, (v/2 - (v*3)/25) / amp_range ); // to do: fine-tune
47}
48
49static inline void Fme7_osc_output( struct Nes_Fme7_Apu* this, int i, struct Blip_Buffer* buf )
50{
51 assert( (unsigned) i < fme7_osc_count );
52 this->oscs [i].output = buf;
53}
54
55static inline void Fme7_output( struct Nes_Fme7_Apu* this, struct Blip_Buffer* buf )
56{
57 int i;
58 for ( i = 0; i < fme7_osc_count; i++ )
59 Fme7_osc_output( this, i, buf );
60}
61
62// (addr & addr_mask) == latch_addr
63static inline void Fme7_write_latch( struct Nes_Fme7_Apu* this, int data ) { this->latch = data; }
64
65// (addr & addr_mask) == data_addr
66void Fme7_run_until( struct Nes_Fme7_Apu* this, blip_time_t end_time );
67static inline void Fme7_write_data( struct Nes_Fme7_Apu* this, blip_time_t time, int data )
68{
69 if ( (unsigned) this->latch >= fme7_reg_count )
70 {
71 #ifdef debug_printf
72 debug_printf( "FME7 write to %02X (past end of sound registers)\n", (int) latch );
73 #endif
74 return;
75 }
76
77 Fme7_run_until( this, time );
78 this->regs [this->latch] = data;
79}
80
81static inline void Fme7_end_frame( struct Nes_Fme7_Apu* this, blip_time_t time )
82{
83 if ( time > this->last_time )
84 Fme7_run_until( this, time );
85
86 assert( this->last_time >= time );
87 this->last_time -= time;
88}
89
90#endif