OSDN Git Service

[VM][DEVICE] Use __FASTCALL with interfaces, read_*() ,write_*(), fetch_op() and...
[csp-qt/common_source_project-fm7.git] / source / src / vm / familybasic / apu.h
1 /*
2         Nintendo Family BASIC Emulator 'eFamilyBASIC'
3
4         Origin : nester
5         Author : Takeda.Toshiya
6         Date   : 2010.08.11-
7
8         [ APU ]
9 */
10
11 #ifndef _APU_H_
12 #define _APU_H_
13
14 #include "../vm.h"
15 #include "../../emu.h"
16 #include "../device.h"
17
18
19 namespace FAMILYBASIC {
20
21 #define APUQUEUE_SIZE   4096
22 #define APUQUEUE_MASK   (APUQUEUE_SIZE - 1)
23
24 // rectangle
25 typedef struct {
26         uint8_t regs[4];
27         bool enabled;
28         int32_t phaseacc;
29         int32_t freq;
30         int32_t output_vol;
31         bool fixed_envelope;
32         bool holdnote;
33         uint8_t volume;
34         int32_t sweep_phase;
35         int32_t sweep_delay;
36         bool sweep_on;
37         uint8_t sweep_shifts;
38         uint8_t sweep_length;
39         bool sweep_inc;
40         int32_t freq_limit;
41         bool sweep_complement;
42         int32_t env_phase;
43         int32_t env_delay;
44         uint8_t env_vol;
45         int vbl_length;
46         uint8_t adder;
47         int duty_flip;
48         bool enabled_cur;
49         bool holdnote_cur;
50         int vbl_length_cur;
51 } rectangle_t;
52
53 // triangle
54 typedef struct {
55         uint8_t regs[3];
56         bool enabled;
57         int32_t freq;
58         int32_t phaseacc;
59         int32_t output_vol;
60         uint8_t adder;
61         bool holdnote;
62         bool counter_started;
63         int write_latency;
64         int vbl_length;
65         int linear_length;
66         bool enabled_cur;
67         bool holdnote_cur;
68         bool counter_started_cur;
69         int vbl_length_cur;
70 } triangle_t;
71
72 // noise
73 typedef struct {
74         uint8_t regs[3];
75         bool enabled;
76         int32_t freq;
77         int32_t phaseacc;
78         int32_t output_vol;
79         int32_t env_phase;
80         int32_t env_delay;
81         uint8_t env_vol;
82         bool fixed_envelope;
83         bool holdnote;
84         uint8_t volume;
85         int vbl_length;
86         uint8_t xor_tap;
87         bool enabled_cur;
88         bool holdnote_cur;
89         int vbl_length_cur;
90         
91         int shift_reg;
92         int noise_bit;
93 } noise_t;
94
95 // dmc
96 typedef struct {
97         uint8_t regs[4];
98         bool enabled;
99         int32_t freq;
100         int32_t phaseacc;
101         int32_t output_vol;
102         uint32_t address;
103         uint32_t cached_addr;
104         int dma_length;
105         int cached_dmalength;
106         uint8_t cur_byte;
107         bool looping;
108         bool irq_gen;
109         bool irq_occurred;
110         int32_t freq_cur;
111         int32_t phaseacc_cur;
112         int dma_length_cur;
113         int cached_dmalength_cur;
114         bool enabled_cur;
115         bool looping_cur;
116         bool irq_gen_cur;
117         bool irq_occurred_cur;
118 } dmc_t;
119
120 // queue
121 typedef struct {
122         uint32_t timestamp, addr;
123         uint32_t data;
124 } queue_t;
125
126 class APU : public DEVICE
127 {
128 private:
129         DEVICE *d_cpu, *d_mem;
130         
131         rectangle_t rectangle[2];
132         triangle_t triangle;
133         noise_t noise;
134         dmc_t dmc;
135         
136         int32_t cycle_rate;
137         int32_t decay_lut[16];
138         int vbl_lut[32];
139         int trilength_lut[128];
140         
141         uint32_t enable_reg;
142         uint32_t enable_reg_cur;
143         int count_rate;
144         
145         queue_t queue[APUQUEUE_SIZE];
146         int q_head, q_tail;
147         uint32_t elapsed_cycles;
148         double ave, max, min;
149         
150         int32_t __FASTCALL create_rectangle(rectangle_t *chan);
151         int32_t __FASTCALL create_triangle(triangle_t *chan);
152         int32_t __FASTCALL create_noise(noise_t *chan);
153         inline void __FASTCALL dmc_reload(dmc_t *chan);
154         int32_t __FASTCALL create_dmc(dmc_t *chan);
155         void __FASTCALL enqueue(queue_t *d);
156         queue_t* __FASTCALL dequeue();
157         void __FASTCALL write_data_sync(uint32_t addr, uint32_t data);
158         void __FASTCALL write_data_cur(uint32_t addr, uint32_t data);
159         
160         int volume_l, volume_r;
161
162 public:
163         APU(VM_TEMPLATE* parent_vm, EMU* parent_emu) : DEVICE(parent_vm, parent_emu)
164         {
165                 volume_l = volume_r = 1024;
166                 set_device_name(_T("APU"));
167         }
168         ~APU() {}
169         
170         // common functions
171         void initialize();
172         void reset();
173         void __FASTCALL write_data8(uint32_t addr, uint32_t data);
174         uint32_t __FASTCALL read_data8(uint32_t addr);
175         void event_frame();
176         void event_vline(int v, int clock);
177         void mix(int32_t* buffer, int cnt);
178         void set_volume(int ch, int decibel_l, int decibel_r);
179         bool process_state(FILEIO* state_fio, bool loading);
180         
181         // unique functions
182         void set_context_cpu(DEVICE* device)
183         {
184                 d_cpu = device;
185         }
186         void set_context_memory(DEVICE* device)
187         {
188                 d_mem = device;
189         }
190         void initialize_sound(int rate, int samples);
191 };
192
193 }
194 #endif
195