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   INT32 pos;
54   UINT8 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_WRC2    0x0A
70 #define  APU_WRC3    0x0B
71 #define  APU_WRD0    0x0C
72 #define  APU_WRD2    0x0E
73 #define  APU_WRD3    0x0F
74 #define  APU_WRE0    0x10
75 #define  APU_WRE1    0x11
76 #define  APU_WRE2    0x12
77 #define  APU_WRE3    0x13
78 #define  APU_SMASK   0x15
79 #define  APU_IRQCTRL 0x17
80 
81 #define  NOISE_LONG     0x4000
82 #define  NOISE_SHORT    93
83 
84 /* CHANNEL TYPE DEFINITIONS */
85 
86 /* Square Wave */
87 typedef struct square_s
88 {
89    uint8 regs[4];
90    INT32 vbl_length;
91    INT32 freq;
92    float phaseacc;
93    float output_vol;
94    float env_phase;
95    float sweep_phase;
96    uint8 adder;
97    uint8 env_vol;
98    boolean enabled;
99 } square_t;
100 
101 /* Triangle Wave */
102 typedef struct triangle_s
103 {
104    uint8 regs[4]; /* regs[1] unused */
105    INT32 linear_length;
106    INT32 vbl_length;
107    INT32 write_latency;
108    float phaseacc;
109    float output_vol;
110    uint8 adder;
111    boolean counter_started;
112    boolean enabled;
113 } triangle_t;
114 
115 /* Noise Wave */
116 typedef struct noise_s
117 {
118    uint8 regs[4]; /* regs[1] unused */
119    INT32 cur_pos;
120    INT32 vbl_length;
121    float phaseacc;
122    float output_vol;
123    float env_phase;
124    uint8 env_vol;
125    boolean enabled;
126 } noise_t;
127 
128 /* DPCM Wave */
129 typedef struct dpcm_s
130 {
131    uint8 regs[4];
132    uint32 address;
133    uint32 length;
134    INT32 bits_left;
135    float phaseacc;
136    float output_vol;
137    uint8 cur_byte;
138    boolean enabled;
139    boolean irq_occurred;
140    INT8 vol;
141 } dpcm_t;
142 
143 /* APU type */
144 typedef struct apu
145 {
146    /* Sound channels */
147    square_t   squ[2];
148    triangle_t tri;
149    noise_t    noi;
150    dpcm_t     dpcm;
151 
152    /* APU registers */
153    UINT8 regs[0x18];
154 
155    /* Sound pointers */
156    void *buffer;
157 
158 #ifdef USE_QUEUE
159 
160    /* Event queue */
161    queue_t queue[QUEUE_SIZE];
162    INT32 head,tail;
163 
164 #else
165 
166    INT32 buf_pos;
167 
168 #endif
169 
170 } apu_t;
171 
172 /* CONSTANTS */
173 
174 /* vblank length table used for squares, triangle, noise */
175 static const uint8 vbl_length[32] =
176 {
177    5, 127, 10, 1, 19,  2, 40,  3, 80,  4, 30,  5, 7,  6, 13,  7,
178    6,   8, 12, 9, 24, 10, 48, 11, 96, 12, 36, 13, 8, 14, 16, 15
179 };
180 
181 /* frequency limit of square channels */
182 static const INT32 freq_limit[8] =
183 {
184    0x3FF, 0x555, 0x666, 0x71C, 0x787, 0x7C1, 0x7E0, 0x7F0,
185 };
186 
187 /* table of noise frequencies */
188 static const INT32 noise_freq[16] =
189 {
190    4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 2046
191 };
192 
193 /* dpcm transfer freqs */
194 static const INT32 dpcm_clocks[16] =
195 {
196    428, 380, 340, 320, 286, 254, 226, 214, 190, 160, 142, 128, 106, 85, 72, 54
197 };
198 
199 /* ratios of pos/neg pulse for square waves */
200 /* 2/16 = 12.5%, 4/16 = 25%, 8/16 = 50%, 12/16 = 75% */
201 static const INT32 duty_lut[4] =
202 {
203    2, 4, 8, 12
204 };
205 
206 #endif /* __NES_DEFS_H__ */
207