mmc5_snd.c 9.62 KB
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
/*
** Nofrendo (c) 1998-2000 Matthew Conte (matt@conte.com)
**
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of version 2 of the GNU Library General 
** Public License as published by the Free Software Foundation.
**
** This program is distributed in the hope that it will be useful, 
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
** Library General Public License for more details.  To obtain a 
** copy of the GNU Library General Public License, write to the Free 
** Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** Any permitted reproduction of these routines, in whole or in part,
** must bear this legend.
**
**
** mmc5_snd.c
**
** Nintendo MMC5 sound emulation
** $Id: mmc5_snd.c,v 1.2 2001/04/27 14:37:11 neil Exp $
*/

#include <string.h>
#include <noftypes.h>
#include "mmc5_snd.h"
#include <nes_apu.h>

/* TODO: encapsulate apu/mmc5 rectangle */

#define  APU_OVERSAMPLE
#define  APU_VOLUME_DECAY(x)  ((x) -= ((x) >> 7))

/* look up table madness */
static int32 decay_lut[16];
static int vbl_lut[32];

/* various sound constants for sound emulation */
/* vblank length table used for rectangles, triangle, noise */
static const uint8 vbl_length[32] =
{
   5, 127, 10, 1, 19,  2, 40,  3, 80,  4, 30,  5, 7,  6, 13,  7,
   6,   8, 12, 9, 24, 10, 48, 11, 96, 12, 36, 13, 8, 14, 16, 15
};

/* ratios of pos/neg pulse for rectangle waves
** 2/16 = 12.5%, 4/16 = 25%, 8/16 = 50%, 12/16 = 75% 
** (4-bit adder in rectangles, hence the 16)
*/
static const int duty_lut[4] =
{
   2, 4, 8, 12
};


#define  MMC5_WRA0    0x5000
#define  MMC5_WRA1    0x5001
#define  MMC5_WRA2    0x5002
#define  MMC5_WRA3    0x5003
#define  MMC5_WRB0    0x5004
#define  MMC5_WRB1    0x5005
#define  MMC5_WRB2    0x5006
#define  MMC5_WRB3    0x5007
#define  MMC5_SMASK   0x5015

typedef struct mmc5rectangle_s
{
   uint8 regs[4];

   bool enabled;
   
   float accum;
   int32 freq;
   int32 output_vol;
   bool fixed_envelope;
   bool holdnote;
   uint8 volume;

   int32 env_phase;
   int32 env_delay;
   uint8 env_vol;

   int vbl_length;
   uint8 adder;
   int duty_flip;
} mmc5rectangle_t;

typedef struct mmc5dac_s
{
   int32 output;
   bool enabled;
} mmc5dac_t;


static struct
{
   float incsize;
   uint8 mul[2];
   mmc5rectangle_t rect[2];
   mmc5dac_t dac;
} mmc5;


#define  MMC5_RECTANGLE_OUTPUT   chan->output_vol
static int32 mmc5_rectangle(mmc5rectangle_t *chan)
{
   int32 output;

#ifdef APU_OVERSAMPLE
   int num_times;
   int32 total;
#endif /* APU_OVERSAMPLE */

   /* reg0: 0-3=volume, 4=envelope, 5=hold, 6-7=duty cycle
   ** reg1: 0-2=sweep shifts, 3=sweep inc/dec, 4-6=sweep length, 7=sweep on
   ** reg2: 8 bits of freq
   ** reg3: 0-2=high freq, 7-4=vbl length counter
   */

   APU_VOLUME_DECAY(chan->output_vol);

   if (false == chan->enabled || 0 == chan->vbl_length)
      return MMC5_RECTANGLE_OUTPUT;

   /* vbl length counter */
   if (false == chan->holdnote)
      chan->vbl_length--;

   /* envelope decay at a rate of (env_delay + 1) / 240 secs */
   chan->env_phase -= 4; /* 240/60 */
   while (chan->env_phase < 0)
   {
      chan->env_phase += chan->env_delay;

      if (chan->holdnote)
         chan->env_vol = (chan->env_vol + 1) & 0x0F;
      else if (chan->env_vol < 0x0F)
         chan->env_vol++;
   }

   if (chan->freq < 4)
      return MMC5_RECTANGLE_OUTPUT;

   chan->accum -= mmc5.incsize; /* # of cycles per sample */
   if (chan->accum >= 0)
      return MMC5_RECTANGLE_OUTPUT;

#ifdef APU_OVERSAMPLE
   num_times = total = 0;

   if (chan->fixed_envelope)
      output = chan->volume << 8; /* fixed volume */
   else
      output = (chan->env_vol ^ 0x0F) << 8;
#endif

   while (chan->accum < 0)
   {
      chan->accum += chan->freq;
      chan->adder = (chan->adder + 1) & 0x0F;

#ifdef APU_OVERSAMPLE
      if (chan->adder < chan->duty_flip)
         total += output;
      else
         total -= output;

      num_times++;
#endif
   }

#ifdef APU_OVERSAMPLE
   chan->output_vol = total / num_times;
#else
   if (chan->fixed_envelope)
      output = chan->volume << 8; /* fixed volume */
   else
      output = (chan->env_vol ^ 0x0F) << 8;

   if (0 == chan->adder)
      chan->output_vol = output;
   else if (chan->adder == chan->duty_flip)
      chan->output_vol = -output;
#endif

   return MMC5_RECTANGLE_OUTPUT;
}

static uint8 mmc5_read(uint32 address)
{
   uint32 retval;

   retval = (uint32) (mmc5.mul[0] * mmc5.mul[1]);

   switch (address)
   {
   case 0x5205:
      return (uint8) retval;

   case 0x5206:
      return (uint8) (retval >> 8);

   default:
      return 0xFF;
   }
}

/* mix vrcvi sound channels together */
static int32 mmc5_process(void)
{
   int32 accum;

   accum = mmc5_rectangle(&mmc5.rect[0]);
   accum += mmc5_rectangle(&mmc5.rect[1]);
   if (mmc5.dac.enabled)
      accum += mmc5.dac.output;

   return accum;
}

/* write to registers */
static void mmc5_write(uint32 address, uint8 value)
{
   int chan;

   switch (address)
   {
   /* rectangles */
   case MMC5_WRA0:
   case MMC5_WRB0:
      chan = (address & 4) ? 1 : 0;
      mmc5.rect[chan].regs[0] = value;

      mmc5.rect[chan].volume = value & 0x0F;
      mmc5.rect[chan].env_delay = decay_lut[value & 0x0F];
      mmc5.rect[chan].holdnote = (value & 0x20) ? true : false;
      mmc5.rect[chan].fixed_envelope = (value & 0x10) ? true : false;
      mmc5.rect[chan].duty_flip = duty_lut[value >> 6];
      break;

   case MMC5_WRA1:
   case MMC5_WRB1:
      break;

   case MMC5_WRA2:
   case MMC5_WRB2:
      chan = (address & 4) ? 1 : 0;
      mmc5.rect[chan].regs[2] = value;
      if (mmc5.rect[chan].enabled)
         mmc5.rect[chan].freq = (((mmc5.rect[chan].regs[3] & 7) << 8) + value) + 1;
      break;

   case MMC5_WRA3:
   case MMC5_WRB3:
      chan = (address & 4) ? 1 : 0;
      mmc5.rect[chan].regs[3] = value;

      if (mmc5.rect[chan].enabled)
      {
         mmc5.rect[chan].vbl_length = vbl_lut[value >> 3];
         mmc5.rect[chan].env_vol = 0;
         mmc5.rect[chan].freq = (((value & 7) << 8) + mmc5.rect[chan].regs[2]) + 1;
         mmc5.rect[chan].adder = 0;
      }
      break;
   
   case MMC5_SMASK:
      if (value & 0x01)
      {
         mmc5.rect[0].enabled = true;
      }
      else
      {
         mmc5.rect[0].enabled = false;
         mmc5.rect[0].vbl_length = 0;
      }

      if (value & 0x02)
      {
         mmc5.rect[1].enabled = true;
      }
      else
      {
         mmc5.rect[1].enabled = false;
         mmc5.rect[1].vbl_length = 0;
      }

      break;

   case 0x5010:
      if (value & 0x01)
         mmc5.dac.enabled = true;
      else
         mmc5.dac.enabled = false;
      break;

   case 0x5011:
      mmc5.dac.output = (value ^ 0x80) << 8;
      break;

   case 0x5205:
      mmc5.mul[0] = value;
      break;

   case 0x5206:
      mmc5.mul[1] = value;
      break;

   case 0x5114:
   case 0x5115:
      /* ???? */
      break;
   
   default:
      break;
   }
}

/* reset state of vrcvi sound channels */
static void mmc5_reset(void)
{
   int i;
   apu_t apu;


   /* get the phase period from the apu */
   apu_getcontext(&apu);
   mmc5.incsize = apu.cycle_rate;

   for (i = 0x5000; i < 0x5008; i++)
      mmc5_write(i, 0);

   mmc5_write(0x5010, 0);
   mmc5_write(0x5011, 0);
}

static int mmc5_init(void)
{
   int i, num_samples;
   apu_t apu;

   apu_getcontext(&apu);
   num_samples = apu.num_samples;

   /* lut used for enveloping and frequency sweeps */
   for (i = 0; i < 16; i++)
      decay_lut[i] = num_samples * (i + 1);

   /* used for note length, based on vblanks and size of audio buffer */
   for (i = 0; i < 32; i++)
      vbl_lut[i] = vbl_length[i] * num_samples;

   return 0;
}

static apu_memread mmc5_memread[] =
{
   { 0x5205, 0x5206, mmc5_read },
   {     -1,     -1, NULL }
};

static apu_memwrite mmc5_memwrite[] =
{
   { 0x5000, 0x5015, mmc5_write },
   { 0x5114, 0x5115, mmc5_write },
   { 0x5205, 0x5206, mmc5_write },
   {     -1,     -1, NULL }
};

apuext_t mmc5_ext =
{
   mmc5_init,
   NULL, /* no shutdown */
   mmc5_reset,
   mmc5_process,
   mmc5_memread,
   mmc5_memwrite
};

/*
** $Log: mmc5_snd.c,v $
** Revision 1.2  2001/04/27 14:37:11  neil
** wheeee
**
** Revision 1.1  2001/04/27 12:54:40  neil
** blah
**
** Revision 1.1.1.1  2001/04/27 07:03:54  neil
** initial
**
** Revision 1.2  2000/11/13 00:57:08  matt
** doesn't look as nasty now
**
** Revision 1.1  2000/10/24 12:19:59  matt
** changed directory structure
**
** Revision 1.16  2000/10/17 11:56:42  matt
** selectable apu base frequency
**
** Revision 1.15  2000/10/10 14:04:29  matt
** make way for bjarne
**
** Revision 1.14  2000/10/10 13:58:18  matt
** stroustrup squeezing his way in the door
**
** Revision 1.13  2000/10/08 17:50:18  matt
** appears $5114/$5115 do something
**
** Revision 1.12  2000/10/03 11:56:20  matt
** better support for optional sound ext routines
**
** Revision 1.11  2000/09/27 12:26:03  matt
** changed sound accumulators back to floats
**
** Revision 1.10  2000/09/15 13:38:40  matt
** changes for optimized apu core
**
** Revision 1.9  2000/09/15 04:58:07  matt
** simplifying and optimizing APU core
**
** Revision 1.8  2000/07/30 04:32:59  matt
** no more apu_getcyclerate hack
**
** Revision 1.7  2000/07/17 01:52:31  matt
** made sure last line of all source files is a newline
**
** Revision 1.6  2000/07/04 04:51:41  matt
** cleanups
**
** Revision 1.5  2000/07/03 02:18:53  matt
** much better external module exporting
**
** Revision 1.4  2000/06/28 22:03:51  matt
** fixed stupid oversight
**
** Revision 1.3  2000/06/20 20:46:58  matt
** minor cleanups
**
** Revision 1.2  2000/06/20 04:06:16  matt
** migrated external sound definition to apu module
**
** Revision 1.1  2000/06/20 00:06:47  matt
** initial revision
**
*/