1 /*****************************************************************************
2 
3   MAME/MESS NES APU CORE
4 
5   Based on the Nofrendo/Nosefart NES N2A03 sound emulation core written by
6   Matthew Conte (matt@conte.com) and redesigned for use in MAME/MESS by
7   Who Wants to Know? (wwtk@mail.com)
8 
9   This core is written with the advise and consent of Matthew Conte and is
10   released under the GNU Public License.  This core is freely avaiable for
11   use in any freeware project, subject to the following terms:
12 
13   Any modifications to this code must be duly noted in the source and
14   approved by Matthew Conte and myself prior to public submission.
15 
16  *****************************************************************************
17 
18    NES_DEFS.H
19 
20    NES APU internal type definitions and constants.
21 
22  *****************************************************************************/
23 
24 #pragma once
25 
26 #ifndef __NES_DEFS_H__
27 #define __NES_DEFS_H__
28 
29 /* BOOLEAN CONSTANTS */
30 #ifndef TRUE
31 #define TRUE   1
32 #define FALSE  0
33 #endif
34 
35 /* REGULAR TYPE DEFINITIONS */
36 typedef INT8          int8;
37 typedef INT16         int16;
38 typedef INT32         int32;
39 typedef UINT8         uint8;
40 typedef UINT16        uint16;
41 typedef UINT32        uint32;
42 typedef UINT8         boolean;
43 
44 
45 /* QUEUE TYPES */
46 #ifdef USE_QUEUE
47 
48 #define QUEUE_SIZE 0x2000
49 #define QUEUE_MAX  (QUEUE_SIZE-1)
50 
51 typedef struct queue_s
52 {
53 	int pos;
54 	unsigned char reg,val;
55 } queue_t;
56 
57 #endif
58 
59 /* REGISTER DEFINITIONS */
60 #define  APU_WRA0    0x00
61 #define  APU_WRA1    0x01
62 #define  APU_WRA2    0x02
63 #define  APU_WRA3    0x03
64 #define  APU_WRB0    0x04
65 #define  APU_WRB1    0x05
66 #define  APU_WRB2    0x06
67 #define  APU_WRB3    0x07
68 #define  APU_WRC0    0x08
69 #define  APU_WRC1    0x09
70 #define  APU_WRC2    0x0A
71 #define  APU_WRC3    0x0B
72 #define  APU_WRD0    0x0C
73 #define  APU_WRD2    0x0E
74 #define  APU_WRD3    0x0F
75 #define  APU_WRE0    0x10
76 #define  APU_WRE1    0x11
77 #define  APU_WRE2    0x12
78 #define  APU_WRE3    0x13
79 #define  APU_SMASK   0x15
80 #define  APU_IRQCTRL 0x17
81 
82 #define  NOISE_LONG     0x4000
83 #define  NOISE_SHORT    93
84 
85 /* CHANNEL TYPE DEFINITIONS */
86 
87 /* Square Wave */
88 typedef struct square_s
89 {
90 	uint8 regs[4];
91 	int vbl_length;
92 	int freq;
93 	float phaseacc;
94 	float output_vol;
95 	float env_phase;
96 	float sweep_phase;
97 	uint8 adder;
98 	uint8 env_vol;
99 	boolean enabled;
100 	boolean Muted;
101 } square_t;
102 
103 /* Triangle Wave */
104 typedef struct triangle_s
105 {
106 	uint8 regs[4]; /* regs[1] unused */
107 	int linear_length;
108 	int vbl_length;
109 	int write_latency;
110 	float phaseacc;
111 	float output_vol;
112 	uint8 adder;
113 	boolean counter_started;
114 	boolean enabled;
115 	boolean Muted;
116 } triangle_t;
117 
118 /* Noise Wave */
119 typedef struct noise_s
120 {
121 	uint8 regs[4]; /* regs[1] unused */
122 	int cur_pos;
123 	int vbl_length;
124 	float phaseacc;
125 	float output_vol;
126 	float env_phase;
127 	uint8 env_vol;
128 	boolean enabled;
129 	boolean Muted;
130 } noise_t;
131 
132 /* DPCM Wave */
133 typedef struct dpcm_s
134 {
135 	uint8 regs[4];
136 	uint32 address;
137 	uint32 length;
138 	int bits_left;
139 	float phaseacc;
140 	float output_vol;
141 	uint8 cur_byte;
142 	boolean enabled;
143 	boolean irq_occurred;
144 	//address_space *memory;
145 	const uint8 *memory;
146 	//signed char vol;
147 	signed short vol;
148 	boolean Muted;
149 } dpcm_t;
150 
151 /* APU type */
152 typedef struct apu
153 {
154 	/* Sound channels */
155 	square_t   squ[2];
156 	triangle_t tri;
157 	noise_t    noi;
158 	dpcm_t     dpcm;
159 
160 	/* APU registers */
161 	unsigned char regs[0x17];
162 
163 	/* Sound pointers */
164 	void *buffer;
165 
166 #ifdef USE_QUEUE
167 
168 	/* Event queue */
169 	queue_t queue[QUEUE_SIZE];
170 	int head, tail;
171 
172 #else
173 
174 	int buf_pos;
175 
176 #endif
177 
178 	int step_mode;
179 } apu_t;
180 
181 /* CONSTANTS */
182 
183 /* vblank length table used for squares, triangle, noise */
184 static const uint8 vbl_length[32] =
185 {
186    5, 127, 10, 1, 19,  2, 40,  3, 80,  4, 30,  5, 7,  6, 13,  7,
187    6,   8, 12, 9, 24, 10, 48, 11, 96, 12, 36, 13, 8, 14, 16, 15
188 };
189 
190 /* frequency limit of square channels */
191 static const int freq_limit[8] =
192 {
193    //0x3FF, 0x555, 0x666, 0x71C, 0x787, 0x7C1, 0x7E0, 0x7F0,
194    // Fixed, thanks to Delek
195    0x3FF, 0x555, 0x666, 0x71C, 0x787, 0x7C1, 0x7E0, 0x7F2,
196 };
197 
198 /* table of noise frequencies */
199 static const int noise_freq[16] =
200 {
201    //4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 2046
202    // Fixed, thanks to Delek
203    4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068
204 };
205 
206 /* dpcm transfer freqs */
207 static const int dpcm_clocks[16] =
208 {
209    428, 380, 340, 320, 286, 254, 226, 214, 190, 160, 142, 128, 106, 85, 72, 54
210 };
211 
212 /* ratios of pos/neg pulse for square waves */
213 /* 2/16 = 12.5%, 4/16 = 25%, 8/16 = 50%, 12/16 = 75% */
214 static const int duty_lut[4] =
215 {
216    2, 4, 8, 12
217 };
218 
219 #endif /* __NES_DEFS_H__ */
220