~ubuntu-branches/ubuntu/lucid/sdlmame/lucid

« back to all changes in this revision

Viewing changes to src/emu/sound/cdp1863.c

  • Committer: Bazaar Package Importer
  • Author(s): Cesare Falco
  • Date: 2009-11-03 17:10:15 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20091103171015-6hop4ory5lxnumpn
Tags: 0.135-0ubuntu1
* New upstream release - Closes (LP: #403212)
* debian/watch: unstable releases are no longer detected
* mame.ini: added the cheat subdirectories to cheatpath so zipped
  cheatfiles will be searched too
* renamed crsshair subdirectory to crosshair to reflect upstream change
* mame.ini: renamed references to crosshair subdirectory (see above)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
 
 
3
    RCA CDP1863 CMOS 8-Bit Programmable Frequency Generator emulation
 
4
 
 
5
    Copyright MESS Team.
 
6
    Visit http://mamedev.org for licensing and usage restrictions.
 
7
 
 
8
**********************************************************************/
 
9
 
 
10
/*
 
11
 
 
12
    TODO:
 
13
 
 
14
    - what happens if you connect both clocks?
 
15
 
 
16
*/
 
17
 
 
18
#include "driver.h"
 
19
#include "sndintrf.h"
 
20
#include "streams.h"
 
21
#include "cpu/cdp1802/cdp1802.h"
 
22
#include "cdp1863.h"
 
23
 
 
24
/***************************************************************************
 
25
    PARAMETERS
 
26
***************************************************************************/
 
27
 
 
28
#define CDP1863_DEFAULT_LATCH   0x35
 
29
 
 
30
/***************************************************************************
 
31
    TYPE DEFINITIONS
 
32
***************************************************************************/
 
33
 
 
34
typedef struct _cdp1863_t cdp1863_t;
 
35
struct _cdp1863_t
 
36
{
 
37
        int clock1;                                             /* clock 1 */
 
38
        int clock2;                                             /* clock 2 */
 
39
 
 
40
        sound_stream *stream;                   /* sound output */
 
41
 
 
42
        /* sound state */
 
43
        int oe;                                                 /* output enable */
 
44
        int latch;                                              /* sound latch */
 
45
        INT16 signal;                                   /* current signal */
 
46
        int incr;                                               /* initial wave state */
 
47
};
 
48
 
 
49
/***************************************************************************
 
50
    INLINE FUNCTIONS
 
51
***************************************************************************/
 
52
 
 
53
INLINE cdp1863_t *get_safe_token(const device_config *device)
 
54
{
 
55
        assert(device != NULL);
 
56
        assert(device->token != NULL);
 
57
        return (cdp1863_t *)device->token;
 
58
}
 
59
 
 
60
INLINE cdp1863_config *get_safe_config(const device_config *device)
 
61
{
 
62
        assert(device != NULL);
 
63
        return (cdp1863_config *)device->inline_config;
 
64
}
 
65
 
 
66
/***************************************************************************
 
67
    IMPLEMENTATION
 
68
***************************************************************************/
 
69
 
 
70
/*-------------------------------------------------
 
71
    cdp1863_str_w - tone latch write
 
72
-------------------------------------------------*/
 
73
 
 
74
WRITE8_DEVICE_HANDLER( cdp1863_str_w )
 
75
{
 
76
        cdp1863_t *cdp1863 = get_safe_token(device);
 
77
 
 
78
        cdp1863->latch = data;
 
79
}
 
80
 
 
81
/*-------------------------------------------------
 
82
    cdp1863_oe_w - output enable write
 
83
------------------------------------------------*/
 
84
 
 
85
WRITE_LINE_DEVICE_HANDLER( cdp1863_oe_w )
 
86
{
 
87
        cdp1863_t *cdp1863 = get_safe_token(device);
 
88
 
 
89
        cdp1863->oe = state;
 
90
}
 
91
 
 
92
/*-------------------------------------------------
 
93
    cdp1863_set_clk1 - set clock 1 frequency
 
94
------------------------------------------------*/
 
95
 
 
96
void cdp1863_set_clk1(const device_config *device, int frequency)
 
97
{
 
98
        cdp1863_t *cdp1863 = get_safe_token(device);
 
99
 
 
100
        cdp1863->clock1 = frequency;
 
101
}
 
102
 
 
103
/*-------------------------------------------------
 
104
    cdp1863_set_clk2 - set clock 2 frequency
 
105
------------------------------------------------*/
 
106
 
 
107
void cdp1863_set_clk2(const device_config *device, int frequency)
 
108
{
 
109
        cdp1863_t *cdp1863 = get_safe_token(device);
 
110
 
 
111
        cdp1863->clock2 = frequency;
 
112
}
 
113
 
 
114
/*-------------------------------------------------
 
115
    STREAM_UPDATE( cdp1863_stream_update )
 
116
-------------------------------------------------*/
 
117
 
 
118
static STREAM_UPDATE( cdp1863_stream_update )
 
119
{
 
120
        cdp1863_t *cdp1863 = get_safe_token(device);
 
121
 
 
122
        INT16 signal = cdp1863->signal;
 
123
        stream_sample_t *buffer = outputs[0];
 
124
 
 
125
        memset( buffer, 0, samples * sizeof(*buffer) );
 
126
 
 
127
        if (cdp1863->oe)
 
128
        {
 
129
                double frequency;
 
130
                int rate = device->machine->sample_rate / 2;
 
131
 
 
132
                /* get progress through wave */
 
133
                int incr = cdp1863->incr;
 
134
 
 
135
                if (cdp1863->clock1 > 0)
 
136
                {
 
137
                        /* CLK1 is pre-divided by 4 */
 
138
                        frequency = cdp1863->clock1 / 4 / (cdp1863->latch + 1) / 2;
 
139
                }
 
140
                else
 
141
                {
 
142
                        /* CLK2 is pre-divided by 8 */
 
143
                        frequency = cdp1863->clock2 / 8 / (cdp1863->latch + 1) / 2;
 
144
                }
 
145
 
 
146
                if (signal < 0)
 
147
                {
 
148
                        signal = -0x7fff;
 
149
                }
 
150
                else
 
151
                {
 
152
                        signal = 0x7fff;
 
153
                }
 
154
 
 
155
                while( samples-- > 0 )
 
156
                {
 
157
                        *buffer++ = signal;
 
158
                        incr -= frequency;
 
159
                        while( incr < 0 )
 
160
                        {
 
161
                                incr += rate;
 
162
                                signal = -signal;
 
163
                        }
 
164
                }
 
165
 
 
166
                /* store progress through wave */
 
167
                cdp1863->incr = incr;
 
168
                cdp1863->signal = signal;
 
169
        }
 
170
}
 
171
 
 
172
/*-------------------------------------------------
 
173
    DEVICE_START( cdp1863 )
 
174
-------------------------------------------------*/
 
175
 
 
176
static DEVICE_START( cdp1863 )
 
177
{
 
178
        cdp1863_t *cdp1863 = get_safe_token(device);
 
179
        const cdp1863_config *config = get_safe_config(device);
 
180
 
 
181
        /* set initial values */
 
182
        cdp1863->stream = stream_create(device, 0, 1, device->machine->sample_rate, cdp1863, cdp1863_stream_update);
 
183
        cdp1863->clock1 = device->clock;
 
184
        cdp1863->clock2 = config->clock2;
 
185
        cdp1863->oe = 1;
 
186
 
 
187
        /* register for state saving */
 
188
        state_save_register_device_item(device, 0, cdp1863->clock1);
 
189
        state_save_register_device_item(device, 0, cdp1863->clock2);
 
190
        state_save_register_device_item(device, 0, cdp1863->oe);
 
191
        state_save_register_device_item(device, 0, cdp1863->latch);
 
192
        state_save_register_device_item(device, 0, cdp1863->signal);
 
193
        state_save_register_device_item(device, 0, cdp1863->incr);
 
194
}
 
195
 
 
196
/*-------------------------------------------------
 
197
    DEVICE_RESET( cdp1863 )
 
198
-------------------------------------------------*/
 
199
 
 
200
static DEVICE_RESET( cdp1863 )
 
201
{
 
202
        cdp1863_t *cdp1863 = get_safe_token(device);
 
203
 
 
204
        cdp1863->latch = CDP1863_DEFAULT_LATCH;
 
205
}
 
206
 
 
207
/*-------------------------------------------------
 
208
    DEVICE_GET_INFO( cdp1863 )
 
209
-------------------------------------------------*/
 
210
 
 
211
DEVICE_GET_INFO( cdp1863 )
 
212
{
 
213
        switch (state)
 
214
        {
 
215
                /* --- the following bits of info are returned as 64-bit signed integers --- */
 
216
                case DEVINFO_INT_TOKEN_BYTES:                                   info->i = sizeof(cdp1863_t);                            break;
 
217
                case DEVINFO_INT_INLINE_CONFIG_BYTES:                   info->i = sizeof(cdp1863_config);                       break;
 
218
 
 
219
                /* --- the following bits of info are returned as pointers to data or functions --- */
 
220
                case DEVINFO_FCT_START:                                                 info->start = DEVICE_START_NAME(cdp1863);       break;
 
221
                case DEVINFO_FCT_STOP:                                                  /* Nothing */                                                           break;
 
222
                case DEVINFO_FCT_RESET:                                                 info->reset = DEVICE_RESET_NAME(cdp1863);       break;
 
223
 
 
224
                /* --- the following bits of info are returned as NULL-terminated strings --- */
 
225
                case DEVINFO_STR_NAME:                                                  strcpy(info->s, "RCA CDP1863");                         break;
 
226
                case DEVINFO_STR_FAMILY:                                                strcpy(info->s, "RCA CDP1800");                         break;
 
227
                case DEVINFO_STR_VERSION:                                               strcpy(info->s, "1.0");                                         break;
 
228
                case DEVINFO_STR_SOURCE_FILE:                                   strcpy(info->s, __FILE__);                                      break;
 
229
                case DEVINFO_STR_CREDITS:                                               strcpy(info->s, "Copyright MESS Team");         break;
 
230
        }
 
231
}