summaryrefslogtreecommitdiff
path: root/apps/plugins/pacbox/wsg3.c
blob: 3c861312b9b020d3c51f0bc10ae8df7f5feb8962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Pacbox - a Pacman Emulator for Rockbox
 *
 * Based on PIE - Pacman Instructional Emulator
 *
 * Namco custom waveform sound generator 3 (Pacman hardware)
 *
 * Copyright (c) 2003,2004 Alessandro Scotti
 * http://www.ascotti.org/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "wsg3.h"

struct wsg3 wsg3;

static bool wsg3_get_voice(struct wsg3_voice *voice, int index)
{
    int base = 5*index;
    const unsigned char *regs = wsg3.sound_regs;
    unsigned x;
    
    x = regs[0x15 + base] & 0x0F;

    if (x == 0)
        return false;

    voice->volume = x;

    x =            (regs[0x14 + base] & 0x0F);
    x = (x << 4) | (regs[0x13 + base] & 0x0F);
    x = (x << 4) | (regs[0x12 + base] & 0x0F);
    x = (x << 4) | (regs[0x11 + base] & 0x0F);
    x = (x << 4);

    if (index == 0)
    {
        /* The first voice has an extra 4-bit of data */
        x |= regs[0x10 + base] & 0x0F; 
    }

    if (x == 0)
        return false;

    voice->frequency = x;

    voice->waveform = regs[0x05 + base] & 0x07;

    return true;
}

void wsg3_play_sound(int * buf, int len)
{
    struct wsg3_voice voice;

    if (wsg3_get_voice(&voice, 0))
    {
        unsigned offset = wsg3.wave_offset[0];
        unsigned step = voice.frequency * wsg3.resample_step;
        int * wave_data = wsg3.sound_wave_data + 32 * voice.waveform;
        int volume = voice.volume;
        int i;

        for (i = 0; i < len; i++)
        {
            /* Should be shifted right by 15, but we must also get rid
             * of the 10 bits used for decimals */
            buf[i] += wave_data[(offset >> 25) & 0x1F] * volume;
            offset += step;
        }

        wsg3.wave_offset[0] = offset;
    }

    if (wsg3_get_voice(&voice, 1))
    {
        unsigned offset = wsg3.wave_offset[1];
        unsigned step = voice.frequency * wsg3.resample_step;
        int * wave_data = wsg3.sound_wave_data + 32 * voice.waveform;
        int volume = voice.volume;
        int i;

        for (i = 0; i < len; i++)
        {
            /* Should be shifted right by 15, but we must also get rid
             * of the 10 bits used for decimals */
            buf[i] += wave_data[(offset >> 25) & 0x1F] * volume;
            offset += step;
        }

        wsg3.wave_offset[1] = offset;
    }

    if (wsg3_get_voice(&voice, 2))
    {
        unsigned offset = wsg3.wave_offset[2];
        unsigned step = voice.frequency * wsg3.resample_step;
        int * wave_data = wsg3.sound_wave_data + 32 * voice.waveform;
        int volume = voice.volume;
        int i;

        for (i = 0; i < len; i++)
        {
            /* Should be shifted right by 15, but we must also get rid
             * of the 10 bits used for decimals */
            buf[i] += wave_data[(offset >> 25) & 0x1F] * volume;
            offset += step;
        }

        wsg3.wave_offset[2] = offset;
    }
}

void wsg3_set_sampling_rate(unsigned sampling_rate)
{
    wsg3.sampling_rate = sampling_rate;
    wsg3.resample_step = (wsg3.master_clock << 10) / sampling_rate;
}

void wsg3_set_sound_prom( const unsigned char * prom )
{
    int i;

    for (i = 0; i < 32*8; i++)
        wsg3.sound_wave_data[i] = (int)*prom++ - 8;
}

void wsg3_init(unsigned master_clock)
{
    memset(&wsg3, 0, sizeof (struct wsg3));
    wsg3.master_clock = master_clock;
}