OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [sound/] [opl3.c] - Blame information for rev 1626

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/*
2
 * sound/opl3.c
3
 *
4
 * A low level driver for Yamaha YM3812 and OPL-3 -chips
5
 */
6
/*
7
 * Copyright (C) by Hannu Savolainen 1993-1996
8
 *
9
 * USS/Lite for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10
 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11
 * for more info.
12
 */
13
#include <linux/config.h>
14
 
15
 
16
/*
17
 * Major improvements to the FM handling 30AUG92 by Rob Hooft,
18
 */
19
/*
20
 * hooft@chem.ruu.nl
21
 */
22
 
23
#include "sound_config.h"
24
 
25
#if defined(CONFIG_YM3812)
26
 
27
#include "opl3.h"
28
 
29
#define MAX_VOICE       18
30
#define OFFS_4OP        11
31
 
32
struct voice_info
33
  {
34
    unsigned char   keyon_byte;
35
    long            bender;
36
    long            bender_range;
37
    unsigned long   orig_freq;
38
    unsigned long   current_freq;
39
    int             volume;
40
    int             mode;
41
  };
42
 
43
typedef struct opl_devinfo
44
  {
45
    int             base;
46
    int             left_io, right_io;
47
    int             nr_voice;
48
    int             lv_map[MAX_VOICE];
49
 
50
    struct voice_info voc[MAX_VOICE];
51
    struct voice_alloc_info *v_alloc;
52
    struct channel_info *chn_info;
53
 
54
    struct sbi_instrument i_map[SBFM_MAXINSTR];
55
    struct sbi_instrument *act_i[MAX_VOICE];
56
 
57
    struct synth_info fm_info;
58
 
59
    int             busy;
60
    int             model;
61
    unsigned char   cmask;
62
 
63
    int             is_opl4;
64
    int            *osp;
65
  }
66
opl_devinfo;
67
 
68
static struct opl_devinfo *devc = NULL;
69
 
70
static int      force_opl3_mode = 0;
71
 
72
static int      detected_model;
73
 
74
static int      store_instr (int instr_no, struct sbi_instrument *instr);
75
static void     freq_to_fnum (int freq, int *block, int *fnum);
76
static void     opl3_command (int io_addr, unsigned int addr, unsigned int val);
77
static int      opl3_kill_note (int dev, int voice, int note, int velocity);
78
 
79
void
80
enable_opl3_mode (int left, int right, int both)
81
{
82
  force_opl3_mode = 1;
83
}
84
 
85
static void
86
enter_4op_mode (void)
87
{
88
  int             i;
89
  static int      v4op[MAX_VOICE] =
90
  {0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17};
91
 
92
  devc->cmask = 0x3f;           /* Connect all possible 4 OP voice operators */
93
  opl3_command (devc->right_io, CONNECTION_SELECT_REGISTER, 0x3f);
94
 
95
  for (i = 0; i < 3; i++)
96
    pv_map[i].voice_mode = 4;
97
  for (i = 3; i < 6; i++)
98
    pv_map[i].voice_mode = 0;
99
 
100
  for (i = 9; i < 12; i++)
101
    pv_map[i].voice_mode = 4;
102
  for (i = 12; i < 15; i++)
103
    pv_map[i].voice_mode = 0;
104
 
105
  for (i = 0; i < 12; i++)
106
    devc->lv_map[i] = v4op[i];
107
  devc->v_alloc->max_voice = devc->nr_voice = 12;
108
}
109
 
110
static int
111
opl3_ioctl (int dev,
112
            unsigned int cmd, caddr_t arg)
113
{
114
  switch (cmd)
115
    {
116
 
117
    case SNDCTL_FM_LOAD_INSTR:
118
      {
119
        struct sbi_instrument ins;
120
 
121
        memcpy_fromfs ((char *) &ins, &((char *) arg)[0], sizeof (ins));
122
 
123
        if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
124
          {
125
            printk ("FM Error: Invalid instrument number %d\n", ins.channel);
126
            return -(EINVAL);
127
          }
128
 
129
        pmgr_inform (dev, PM_E_PATCH_LOADED, ins.channel, 0, 0, 0);
130
        return store_instr (ins.channel, &ins);
131
      }
132
      break;
133
 
134
    case SNDCTL_SYNTH_INFO:
135
      devc->fm_info.nr_voices = (devc->nr_voice == 12) ? 6 : devc->nr_voice;
136
 
137
      memcpy_tofs (&((char *) arg)[0], &devc->fm_info, sizeof (devc->fm_info));
138
      return 0;
139
      break;
140
 
141
    case SNDCTL_SYNTH_MEMAVL:
142
      return 0x7fffffff;
143
      break;
144
 
145
    case SNDCTL_FM_4OP_ENABLE:
146
      if (devc->model == 2)
147
        enter_4op_mode ();
148
      return 0;
149
      break;
150
 
151
    default:
152
      return -(EINVAL);
153
    }
154
 
155
}
156
 
157
int
158
opl3_detect (int ioaddr, int *osp)
159
{
160
  /*
161
   * This function returns 1 if the FM chip is present at the given I/O port
162
   * The detection algorithm plays with the timer built in the FM chip and
163
   * looks for a change in the status register.
164
   *
165
   * Note! The timers of the FM chip are not connected to AdLib (and compatible)
166
   * boards.
167
   *
168
   * Note2! The chip is initialized if detected.
169
   */
170
 
171
  unsigned char   stat1, stat2, signature;
172
  int             i;
173
 
174
  if (devc != NULL)
175
    return 0;
176
 
177
 
178
  devc = (struct opl_devinfo *) (sound_mem_blocks[sound_nblocks] = vmalloc (sizeof (*devc)));
179
  if (sound_nblocks < 1024)
180
    sound_nblocks++;;
181
 
182
  if (devc == NULL)
183
    {
184
      printk ("OPL3: Can't allocate memory for the device control structure\n");
185
      return 0;
186
    }
187
 
188
  devc->osp = osp;
189
  devc->base = ioaddr;
190
 
191
  /* Reset timers 1 and 2 */
192
  opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
193
 
194
  /* Reset the IRQ of the FM chip */
195
  opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
196
 
197
  signature = stat1 = inb (ioaddr);     /* Status register */
198
 
199
  if ((stat1 & 0xE0) != 0x00)
200
    {
201
      return 0;                  /*
202
                                 * Should be 0x00
203
                                 */
204
    }
205
 
206
  opl3_command (ioaddr, TIMER1_REGISTER, 0xff);         /* Set timer1 to 0xff */
207
 
208
  opl3_command (ioaddr, TIMER_CONTROL_REGISTER,
209
                TIMER2_MASK | TIMER1_START);    /*
210
                                                 * Unmask and start timer 1
211
                                                 */
212
 
213
  /*
214
   * Now we have to delay at least 80 usec
215
   */
216
 
217
  for (i = 0; i < 50; i++)
218
    tenmicrosec (devc->osp);
219
 
220
  stat2 = inb (ioaddr);         /*
221
                                   * Read status after timers have expired
222
                                 */
223
 
224
  /*
225
   * Stop the timers
226
   */
227
 
228
  /* Reset timers 1 and 2 */
229
  opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
230
  /* Reset the IRQ of the FM chip */
231
  opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
232
 
233
  if ((stat2 & 0xE0) != 0xc0)
234
    {
235
      return 0;                  /*
236
                                 * There is no YM3812
237
                                 */
238
    }
239
 
240
  /*
241
   * There is a FM chip in this address. Detect the type (OPL2 to OPL4)
242
   */
243
 
244
  if (signature == 0x06 && !force_opl3_mode)    /* OPL2 */
245
    {
246
      detected_model = 2;
247
    }
248
  else if (signature == 0x00)   /* OPL3 or OPL4 */
249
    {
250
      unsigned char   tmp;
251
 
252
      detected_model = 3;
253
 
254
      /*
255
       * Detect availability of OPL4 (_experimental_). Works probably
256
       * only after a cold boot. In addition the OPL4 port
257
       * of the chip may not be connected to the PC bus at all.
258
       */
259
 
260
      opl3_command (ioaddr + 2, OPL3_MODE_REGISTER, 0x00);
261
      opl3_command (ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
262
 
263
      if ((tmp = inb (ioaddr)) == 0x02)         /* Have a OPL4 */
264
        {
265
          detected_model = 4;
266
        }
267
 
268
      if (!check_region (ioaddr - 8, 2))        /* OPL4 port is free */
269
        {
270
          int             tmp;
271
 
272
          outb (0x02, ioaddr - 8);      /* Select OPL4 ID register */
273
          tenmicrosec (devc->osp);
274
          tmp = inb (ioaddr - 7);       /* Read it */
275
          tenmicrosec (devc->osp);
276
 
277
          if (tmp == 0x20)      /* OPL4 should return 0x20 here */
278
            {
279
              detected_model = 4;
280
 
281
              outb (0xF8, ioaddr - 8);  /* Select OPL4 FM mixer control */
282
              tenmicrosec (devc->osp);
283
              outb (0x1B, ioaddr - 7);  /* Write value */
284
              tenmicrosec (devc->osp);
285
            }
286
          else
287
            detected_model = 3;
288
        }
289
 
290
      opl3_command (ioaddr + 2, OPL3_MODE_REGISTER, 0);
291
 
292
    }
293
 
294
  for (i = 0; i < 9; i++)
295
    opl3_command (ioaddr, KEYON_BLOCK + i, 0);   /*
296
                                                 * Note off
297
                                                 */
298
 
299
  opl3_command (ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
300
  opl3_command (ioaddr, PERCUSSION_REGISTER, 0x00);     /*
301
                                                         * Melodic mode.
302
                                                         */
303
 
304
  return 1;
305
}
306
 
307
static int
308
opl3_kill_note (int devno, int voice, int note, int velocity)
309
{
310
  struct physical_voice_info *map;
311
 
312
  if (voice < 0 || voice >= devc->nr_voice)
313
    return 0;
314
 
315
  devc->v_alloc->map[voice] = 0;
316
 
317
  map = &pv_map[devc->lv_map[voice]];
318
 
319
  DEB (printk ("Kill note %d\n", voice));
320
 
321
  if (map->voice_mode == 0)
322
    return 0;
323
 
324
  opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, devc->voc[voice].keyon_byte & ~0x20);
325
 
326
  devc->voc[voice].keyon_byte = 0;
327
  devc->voc[voice].bender = 0;
328
  devc->voc[voice].volume = 64;
329
  devc->voc[voice].bender_range = 200;  /*
330
                                         * 200 cents = 2 semitones
331
                                         */
332
  devc->voc[voice].orig_freq = 0;
333
  devc->voc[voice].current_freq = 0;
334
  devc->voc[voice].mode = 0;
335
 
336
  return 0;
337
}
338
 
339
#define HIHAT                   0
340
#define CYMBAL                  1
341
#define TOMTOM                  2
342
#define SNARE                   3
343
#define BDRUM                   4
344
#define UNDEFINED               TOMTOM
345
#define DEFAULT                 TOMTOM
346
 
347
static int
348
store_instr (int instr_no, struct sbi_instrument *instr)
349
{
350
 
351
  if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || devc->model != 2))
352
    printk ("FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
353
  memcpy ((char *) &(devc->i_map[instr_no]), (char *) instr, sizeof (*instr));
354
 
355
  return 0;
356
}
357
 
358
static int
359
opl3_set_instr (int dev, int voice, int instr_no)
360
{
361
  if (voice < 0 || voice >= devc->nr_voice)
362
    return 0;
363
 
364
  if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
365
    return 0;
366
 
367
  devc->act_i[voice] = &devc->i_map[instr_no];
368
  return 0;
369
}
370
 
371
/*
372
 * The next table looks magical, but it certainly is not. Its values have
373
 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
374
 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
375
 * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
376
 * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
377
 * volume -8 it was implemented as a table because it is only 128 bytes and
378
 * it saves a lot of log() calculations. (RH)
379
 */
380
char            fm_volume_table[128] =
381
{-64, -48, -40, -35, -32, -29, -27, -26,
382
 -24, -23, -21, -20, -19, -18, -18, -17,
383
 -16, -15, -15, -14, -13, -13, -12, -12,
384
 -11, -11, -10, -10, -10, -9, -9, -8,
385
 -8, -8, -7, -7, -7, -6, -6, -6,
386
 -5, -5, -5, -5, -4, -4, -4, -4,
387
 -3, -3, -3, -3, -2, -2, -2, -2,
388
 -2, -1, -1, -1, -1, 0, 0, 0,
389
 0, 0, 0, 1, 1, 1, 1, 1,
390
 1, 2, 2, 2, 2, 2, 2, 2,
391
 3, 3, 3, 3, 3, 3, 3, 4,
392
 4, 4, 4, 4, 4, 4, 4, 5,
393
 5, 5, 5, 5, 5, 5, 5, 5,
394
 6, 6, 6, 6, 6, 6, 6, 6,
395
 6, 7, 7, 7, 7, 7, 7, 7,
396
 7, 7, 7, 8, 8, 8, 8, 8};
397
 
398
static void
399
calc_vol (unsigned char *regbyte, int volume, int main_vol)
400
{
401
  int             level = (~*regbyte & 0x3f);
402
 
403
  if (main_vol > 127)
404
    main_vol = 127;
405
 
406
  volume = (volume * main_vol) / 127;
407
 
408
  if (level)
409
    level += fm_volume_table[volume];
410
 
411
  if (level > 0x3f)
412
    level = 0x3f;
413
  if (level < 0)
414
    level = 0;
415
 
416
  *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
417
}
418
 
419
static void
420
set_voice_volume (int voice, int volume, int main_vol)
421
{
422
  unsigned char   vol1, vol2, vol3, vol4;
423
  struct sbi_instrument *instr;
424
  struct physical_voice_info *map;
425
 
426
  if (voice < 0 || voice >= devc->nr_voice)
427
    return;
428
 
429
  map = &pv_map[devc->lv_map[voice]];
430
 
431
  instr = devc->act_i[voice];
432
 
433
  if (!instr)
434
    instr = &devc->i_map[0];
435
 
436
  if (instr->channel < 0)
437
    return;
438
 
439
  if (devc->voc[voice].mode == 0)
440
    return;
441
 
442
  if (devc->voc[voice].mode == 2)
443
    {
444
 
445
      vol1 = instr->operators[2];
446
      vol2 = instr->operators[3];
447
 
448
      if ((instr->operators[10] & 0x01))
449
        {
450
          calc_vol (&vol1, volume, main_vol);
451
          calc_vol (&vol2, volume, main_vol);
452
        }
453
      else
454
        {
455
          calc_vol (&vol2, volume, main_vol);
456
        }
457
 
458
      opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);
459
      opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);
460
    }
461
  else
462
    {                           /*
463
                                 * 4 OP voice
464
                                 */
465
      int             connection;
466
 
467
      vol1 = instr->operators[2];
468
      vol2 = instr->operators[3];
469
      vol3 = instr->operators[OFFS_4OP + 2];
470
      vol4 = instr->operators[OFFS_4OP + 3];
471
 
472
      /*
473
       * The connection method for 4 OP devc->voc is defined by the rightmost
474
       * bits at the offsets 10 and 10+OFFS_4OP
475
       */
476
 
477
      connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
478
 
479
      switch (connection)
480
        {
481
        case 0:
482
          calc_vol (&vol4, volume, main_vol);
483
          break;
484
 
485
        case 1:
486
          calc_vol (&vol2, volume, main_vol);
487
          calc_vol (&vol4, volume, main_vol);
488
          break;
489
 
490
        case 2:
491
          calc_vol (&vol1, volume, main_vol);
492
          calc_vol (&vol4, volume, main_vol);
493
          break;
494
 
495
        case 3:
496
          calc_vol (&vol1, volume, main_vol);
497
          calc_vol (&vol3, volume, main_vol);
498
          calc_vol (&vol4, volume, main_vol);
499
          break;
500
 
501
        default:;
502
        }
503
 
504
      opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);
505
      opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);
506
      opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], vol3);
507
      opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], vol4);
508
    }
509
}
510
 
511
static int
512
opl3_start_note (int dev, int voice, int note, int volume)
513
{
514
  unsigned char   data, fpc;
515
  int             block, fnum, freq, voice_mode;
516
  struct sbi_instrument *instr;
517
  struct physical_voice_info *map;
518
 
519
  if (voice < 0 || voice >= devc->nr_voice)
520
    return 0;
521
 
522
  map = &pv_map[devc->lv_map[voice]];
523
 
524
  if (map->voice_mode == 0)
525
    return 0;
526
 
527
  if (note == 255)              /*
528
                                 * Just change the volume
529
                                 */
530
    {
531
      set_voice_volume (voice, volume, devc->voc[voice].volume);
532
      return 0;
533
    }
534
 
535
  /*
536
   * Kill previous note before playing
537
   */
538
  opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], 0xff);     /*
539
                                                                 * Carrier
540
                                                                 * volume to
541
                                                                 * min
542
                                                                 */
543
  opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], 0xff);      /*
544
                                                                 * Modulator
545
                                                                 * volume to
546
                                                                 */
547
 
548
  if (map->voice_mode == 4)
549
    {
550
      opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
551
      opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
552
    }
553
 
554
  opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00);       /*
555
                                                                         * Note
556
                                                                         * off
557
                                                                         */
558
 
559
  instr = devc->act_i[voice];
560
 
561
  if (!instr)
562
    instr = &devc->i_map[0];
563
 
564
  if (instr->channel < 0)
565
    {
566
      printk (
567
               "OPL3: Initializing voice %d with undefined instrument\n",
568
               voice);
569
      return 0;
570
    }
571
 
572
  if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
573
    return 0;                    /*
574
                                 * Cannot play
575
                                 */
576
 
577
  voice_mode = map->voice_mode;
578
 
579
  if (voice_mode == 4)
580
    {
581
      int             voice_shift;
582
 
583
      voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3;
584
      voice_shift += map->voice_num;
585
 
586
      if (instr->key != OPL3_PATCH)     /*
587
                                         * Just 2 OP patch
588
                                         */
589
        {
590
          voice_mode = 2;
591
          devc->cmask &= ~(1 << voice_shift);
592
        }
593
      else
594
        {
595
          devc->cmask |= (1 << voice_shift);
596
        }
597
 
598
      opl3_command (devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
599
    }
600
 
601
  /*
602
   * Set Sound Characteristics
603
   */
604
  opl3_command (map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
605
  opl3_command (map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
606
 
607
  /*
608
   * Set Attack/Decay
609
   */
610
  opl3_command (map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
611
  opl3_command (map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
612
 
613
  /*
614
   * Set Sustain/Release
615
   */
616
  opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
617
  opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
618
 
619
  /*
620
   * Set Wave Select
621
   */
622
  opl3_command (map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
623
  opl3_command (map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
624
 
625
  /*
626
   * Set Feedback/Connection
627
   */
628
  fpc = instr->operators[10];
629
  if (!(fpc & 0x30))
630
    fpc |= 0x30;                /*
631
                                 * Ensure that at least one chn is enabled
632
                                 */
633
  opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num,
634
                fpc);
635
 
636
  /*
637
   * If the voice is a 4 OP one, initialize the operators 3 and 4 also
638
   */
639
 
640
  if (voice_mode == 4)
641
    {
642
 
643
      /*
644
       * Set Sound Characteristics
645
       */
646
      opl3_command (map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
647
      opl3_command (map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
648
 
649
      /*
650
       * Set Attack/Decay
651
       */
652
      opl3_command (map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
653
      opl3_command (map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
654
 
655
      /*
656
       * Set Sustain/Release
657
       */
658
      opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
659
      opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
660
 
661
      /*
662
       * Set Wave Select
663
       */
664
      opl3_command (map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
665
      opl3_command (map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
666
 
667
      /*
668
       * Set Feedback/Connection
669
       */
670
      fpc = instr->operators[OFFS_4OP + 10];
671
      if (!(fpc & 0x30))
672
        fpc |= 0x30;            /*
673
                                 * Ensure that at least one chn is enabled
674
                                 */
675
      opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
676
    }
677
 
678
  devc->voc[voice].mode = voice_mode;
679
 
680
  set_voice_volume (voice, volume, devc->voc[voice].volume);
681
 
682
  freq = devc->voc[voice].orig_freq = note_to_freq (note) / 1000;
683
 
684
  /*
685
   * Since the pitch bender may have been set before playing the note, we
686
   * have to calculate the bending now.
687
   */
688
 
689
  freq = compute_finetune (devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range);
690
  devc->voc[voice].current_freq = freq;
691
 
692
  freq_to_fnum (freq, &block, &fnum);
693
 
694
  /*
695
   * Play note
696
   */
697
 
698
  data = fnum & 0xff;           /*
699
                                 * Least significant bits of fnumber
700
                                 */
701
  opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
702
 
703
  data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
704
  devc->voc[voice].keyon_byte = data;
705
  opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
706
  if (voice_mode == 4)
707
    opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
708
 
709
  return 0;
710
}
711
 
712
static void
713
freq_to_fnum (int freq, int *block, int *fnum)
714
{
715
  int             f, octave;
716
 
717
  /*
718
   * Converts the note frequency to block and fnum values for the FM chip
719
   */
720
  /*
721
   * First try to compute the block -value (octave) where the note belongs
722
   */
723
 
724
  f = freq;
725
 
726
  octave = 5;
727
 
728
  if (f == 0)
729
    octave = 0;
730
  else if (f < 261)
731
    {
732
      while (f < 261)
733
        {
734
          octave--;
735
          f <<= 1;
736
        }
737
    }
738
  else if (f > 493)
739
    {
740
      while (f > 493)
741
        {
742
          octave++;
743
          f >>= 1;
744
        }
745
    }
746
 
747
  if (octave > 7)
748
    octave = 7;
749
 
750
  *fnum = freq * (1 << (20 - octave)) / 49716;
751
  *block = octave;
752
}
753
 
754
static void
755
opl3_command (int io_addr, unsigned int addr, unsigned int val)
756
{
757
  int             i;
758
 
759
  /*
760
   * The original 2-OP synth requires a quite long delay after writing to a
761
   * register. The OPL-3 survives with just two INBs
762
   */
763
 
764
  outb ((unsigned char) (addr & 0xff), io_addr);
765
 
766
  if (!devc->model != 2)
767
    tenmicrosec (devc->osp);
768
  else
769
    for (i = 0; i < 2; i++)
770
      inb (io_addr);
771
 
772
  outb ((unsigned char) (val & 0xff), io_addr + 1);
773
 
774
  if (devc->model != 2)
775
    {
776
      tenmicrosec (devc->osp);
777
      tenmicrosec (devc->osp);
778
      tenmicrosec (devc->osp);
779
    }
780
  else
781
    for (i = 0; i < 2; i++)
782
      inb (io_addr);
783
}
784
 
785
static void
786
opl3_reset (int devno)
787
{
788
  int             i;
789
 
790
  for (i = 0; i < 18; i++)
791
    devc->lv_map[i] = i;
792
 
793
  for (i = 0; i < devc->nr_voice; i++)
794
    {
795
      opl3_command (pv_map[devc->lv_map[i]].ioaddr,
796
                    KSL_LEVEL + pv_map[devc->lv_map[i]].op[0], 0xff);
797
 
798
      opl3_command (pv_map[devc->lv_map[i]].ioaddr,
799
                    KSL_LEVEL + pv_map[devc->lv_map[i]].op[1], 0xff);
800
 
801
      if (pv_map[devc->lv_map[i]].voice_mode == 4)
802
        {
803
          opl3_command (pv_map[devc->lv_map[i]].ioaddr,
804
                        KSL_LEVEL + pv_map[devc->lv_map[i]].op[2], 0xff);
805
 
806
          opl3_command (pv_map[devc->lv_map[i]].ioaddr,
807
                        KSL_LEVEL + pv_map[devc->lv_map[i]].op[3], 0xff);
808
        }
809
 
810
      opl3_kill_note (devno, i, 0, 64);
811
    }
812
 
813
  if (devc->model == 2)
814
    {
815
      devc->v_alloc->max_voice = devc->nr_voice = 18;
816
 
817
      for (i = 0; i < 18; i++)
818
        pv_map[i].voice_mode = 2;
819
 
820
    }
821
 
822
}
823
 
824
static int
825
opl3_open (int dev, int mode)
826
{
827
  int             i;
828
 
829
  if (devc->busy)
830
    return -(EBUSY);
831
  devc->busy = 1;
832
 
833
  devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
834
  devc->v_alloc->timestamp = 0;
835
 
836
  for (i = 0; i < 18; i++)
837
    {
838
      devc->v_alloc->map[i] = 0;
839
      devc->v_alloc->alloc_times[i] = 0;
840
    }
841
 
842
  devc->cmask = 0x00;           /*
843
                                 * Just 2 OP mode
844
                                 */
845
  if (devc->model == 2)
846
    opl3_command (devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
847
  return 0;
848
}
849
 
850
static void
851
opl3_close (int dev)
852
{
853
  devc->busy = 0;
854
  devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
855
 
856
  devc->fm_info.nr_drums = 0;
857
  devc->fm_info.perc_mode = 0;
858
 
859
  opl3_reset (dev);
860
}
861
 
862
static void
863
opl3_hw_control (int dev, unsigned char *event)
864
{
865
}
866
 
867
static int
868
opl3_load_patch (int dev, int format, const char *addr,
869
                 int offs, int count, int pmgr_flag)
870
{
871
  struct sbi_instrument ins;
872
 
873
  if (count < sizeof (ins))
874
    {
875
      printk ("FM Error: Patch record too short\n");
876
      return -(EINVAL);
877
    }
878
 
879
  memcpy_fromfs (&((char *) &ins)[offs], &(addr)[offs], sizeof (ins) - offs);
880
 
881
  if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
882
    {
883
      printk ("FM Error: Invalid instrument number %d\n", ins.channel);
884
      return -(EINVAL);
885
    }
886
  ins.key = format;
887
 
888
  return store_instr (ins.channel, &ins);
889
}
890
 
891
static void
892
opl3_panning (int dev, int voice, int pressure)
893
{
894
}
895
 
896
static void
897
opl3_volume_method (int dev, int mode)
898
{
899
}
900
 
901
#define SET_VIBRATO(cell) { \
902
      tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
903
      if (pressure > 110) \
904
        tmp |= 0x40;            /* Vibrato on */ \
905
      opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
906
 
907
static void
908
opl3_aftertouch (int dev, int voice, int pressure)
909
{
910
  int             tmp;
911
  struct sbi_instrument *instr;
912
  struct physical_voice_info *map;
913
 
914
  if (voice < 0 || voice >= devc->nr_voice)
915
    return;
916
 
917
  map = &pv_map[devc->lv_map[voice]];
918
 
919
  DEB (printk ("Aftertouch %d\n", voice));
920
 
921
  if (map->voice_mode == 0)
922
    return;
923
 
924
  /*
925
   * Adjust the amount of vibrato depending the pressure
926
   */
927
 
928
  instr = devc->act_i[voice];
929
 
930
  if (!instr)
931
    instr = &devc->i_map[0];
932
 
933
  if (devc->voc[voice].mode == 4)
934
    {
935
      int             connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
936
 
937
      switch (connection)
938
        {
939
        case 0:
940
          SET_VIBRATO (4);
941
          break;
942
 
943
        case 1:
944
          SET_VIBRATO (2);
945
          SET_VIBRATO (4);
946
          break;
947
 
948
        case 2:
949
          SET_VIBRATO (1);
950
          SET_VIBRATO (4);
951
          break;
952
 
953
        case 3:
954
          SET_VIBRATO (1);
955
          SET_VIBRATO (3);
956
          SET_VIBRATO (4);
957
          break;
958
 
959
        }
960
      /*
961
       * Not implemented yet
962
       */
963
    }
964
  else
965
    {
966
      SET_VIBRATO (1);
967
 
968
      if ((instr->operators[10] & 0x01))        /*
969
                                                 * Additive synthesis
970
                                                 */
971
        SET_VIBRATO (2);
972
    }
973
}
974
 
975
#undef SET_VIBRATO
976
 
977
static void
978
bend_pitch (int dev, int voice, int value)
979
{
980
  unsigned char   data;
981
  int             block, fnum, freq;
982
  struct physical_voice_info *map;
983
 
984
  map = &pv_map[devc->lv_map[voice]];
985
 
986
  if (map->voice_mode == 0)
987
    return;
988
 
989
  devc->voc[voice].bender = value;
990
  if (!value)
991
    return;
992
  if (!(devc->voc[voice].keyon_byte & 0x20))
993
    return;                     /*
994
                                 * Not keyed on
995
                                 */
996
 
997
  freq = compute_finetune (devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range);
998
  devc->voc[voice].current_freq = freq;
999
 
1000
  freq_to_fnum (freq, &block, &fnum);
1001
 
1002
  data = fnum & 0xff;           /*
1003
                                 * Least significant bits of fnumber
1004
                                 */
1005
  opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
1006
 
1007
  data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);     /*
1008
                                                                 * *
1009
                                                                 * KEYON|OCTAVE|MS
1010
                                                                 *
1011
                                                                 * * bits * *
1012
                                                                 * of * f-num
1013
                                                                 *
1014
                                                                 */
1015
  devc->voc[voice].keyon_byte = data;
1016
  opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
1017
}
1018
 
1019
static void
1020
opl3_controller (int dev, int voice, int ctrl_num, int value)
1021
{
1022
  if (voice < 0 || voice >= devc->nr_voice)
1023
    return;
1024
 
1025
  switch (ctrl_num)
1026
    {
1027
    case CTRL_PITCH_BENDER:
1028
      bend_pitch (dev, voice, value);
1029
      break;
1030
 
1031
    case CTRL_PITCH_BENDER_RANGE:
1032
      devc->voc[voice].bender_range = value;
1033
      break;
1034
 
1035
    case CTL_MAIN_VOLUME:
1036
      devc->voc[voice].volume = value / 128;
1037
      break;
1038
    }
1039
}
1040
 
1041
static int
1042
opl3_patchmgr (int dev, struct patmgr_info *rec)
1043
{
1044
  return -(EINVAL);
1045
}
1046
 
1047
static void
1048
opl3_bender (int dev, int voice, int value)
1049
{
1050
  if (voice < 0 || voice >= devc->nr_voice)
1051
    return;
1052
 
1053
  bend_pitch (dev, voice, value - 8192);
1054
}
1055
 
1056
static int
1057
opl3_alloc_voice (int dev, int chn, int note, struct voice_alloc_info *alloc)
1058
{
1059
  int             i, p, best, first, avail, best_time = 0x7fffffff;
1060
  struct sbi_instrument *instr;
1061
  int             is4op;
1062
  int             instr_no;
1063
 
1064
  if (chn < 0 || chn > 15)
1065
    instr_no = 0;
1066
  else
1067
    instr_no = devc->chn_info[chn].pgm_num;
1068
 
1069
  instr = &devc->i_map[instr_no];
1070
  if (instr->channel < 0 ||      /* Instrument not loaded */
1071
      devc->nr_voice != 12)     /* Not in 4 OP mode */
1072
    is4op = 0;
1073
  else if (devc->nr_voice == 12)        /* 4 OP mode */
1074
    is4op = (instr->key == OPL3_PATCH);
1075
  else
1076
    is4op = 0;
1077
 
1078
  if (is4op)
1079
    {
1080
      first = p = 0;
1081
      avail = 6;
1082
    }
1083
  else
1084
    {
1085
      if (devc->nr_voice == 12) /* 4 OP mode. Use the '2 OP only' operators first */
1086
        first = p = 6;
1087
      else
1088
        first = p = 0;
1089
      avail = devc->nr_voice;
1090
    }
1091
 
1092
  /*
1093
     *    Now try to find a free voice
1094
   */
1095
  best = first;
1096
 
1097
  for (i = 0; i < avail; i++)
1098
    {
1099
      if (alloc->map[p] == 0)
1100
        {
1101
          return p;
1102
        }
1103
      if (alloc->alloc_times[p] < best_time)    /* Find oldest playing note */
1104
        {
1105
          best_time = alloc->alloc_times[p];
1106
          best = p;
1107
        }
1108
      p = (p + 1) % avail;
1109
    }
1110
 
1111
  /*
1112
     *    Insert some kind of priority mechanism here.
1113
   */
1114
 
1115
  if (best < 0)
1116
    best = 0;
1117
  if (best > devc->nr_voice)
1118
    best -= devc->nr_voice;
1119
 
1120
  return best;                  /* All devc->voc in use. Select the first one. */
1121
}
1122
 
1123
static void
1124
opl3_setup_voice (int dev, int voice, int chn)
1125
{
1126
  struct channel_info *info =
1127
  &synth_devs[dev]->chn_info[chn];
1128
 
1129
  opl3_set_instr (dev, voice,
1130
                  info->pgm_num);
1131
 
1132
  devc->voc[voice].bender = info->bender_value;
1133
  devc->voc[voice].volume =
1134
    info->controllers[CTL_MAIN_VOLUME];
1135
}
1136
 
1137
static struct synth_operations opl3_operations =
1138
{
1139
  NULL,
1140
  0,
1141
  SYNTH_TYPE_FM,
1142
  FM_TYPE_ADLIB,
1143
  opl3_open,
1144
  opl3_close,
1145
  opl3_ioctl,
1146
  opl3_kill_note,
1147
  opl3_start_note,
1148
  opl3_set_instr,
1149
  opl3_reset,
1150
  opl3_hw_control,
1151
  opl3_load_patch,
1152
  opl3_aftertouch,
1153
  opl3_controller,
1154
  opl3_panning,
1155
  opl3_volume_method,
1156
  opl3_patchmgr,
1157
  opl3_bender,
1158
  opl3_alloc_voice,
1159
  opl3_setup_voice
1160
};
1161
 
1162
void
1163
opl3_init (int ioaddr, int *osp)
1164
{
1165
  int             i;
1166
 
1167
  if (num_synths >= MAX_SYNTH_DEV)
1168
    {
1169
      printk ("OPL3 Error: Too many synthesizers\n");
1170
      return;
1171
    }
1172
 
1173
  if (devc == NULL)
1174
    {
1175
      printk ("OPL3: Device control structure not initialized.\n");
1176
      return;
1177
    }
1178
 
1179
  memset ((char *) devc, 0x00, sizeof (*devc));
1180
  devc->osp = osp;
1181
  devc->base = ioaddr;
1182
 
1183
  devc->nr_voice = 9;
1184
  strcpy (devc->fm_info.name, "OPL2");
1185
 
1186
  devc->fm_info.device = 0;
1187
  devc->fm_info.synth_type = SYNTH_TYPE_FM;
1188
  devc->fm_info.synth_subtype = FM_TYPE_ADLIB;
1189
  devc->fm_info.perc_mode = 0;
1190
  devc->fm_info.nr_voices = 9;
1191
  devc->fm_info.nr_drums = 0;
1192
  devc->fm_info.instr_bank_size = SBFM_MAXINSTR;
1193
  devc->fm_info.capabilities = 0;
1194
  devc->left_io = ioaddr;
1195
  devc->right_io = ioaddr + 2;
1196
 
1197
  if (detected_model <= 2)
1198
    devc->model = 1;
1199
  else
1200
    {
1201
      devc->model = 2;
1202
      if (detected_model == 4)
1203
        devc->is_opl4 = 1;
1204
    }
1205
 
1206
  opl3_operations.info = &devc->fm_info;
1207
 
1208
  synth_devs[num_synths++] = &opl3_operations;
1209
  devc->v_alloc = &opl3_operations.alloc;
1210
  devc->chn_info = &opl3_operations.chn_info[0];
1211
 
1212
  if (devc->model == 2)
1213
    {
1214
      if (devc->is_opl4)
1215
        conf_printf2 ("Yamaha OPL4/OPL3 FM", ioaddr, 0, -1, -1);
1216
      else
1217
        conf_printf2 ("Yamaha OPL3 FM", ioaddr, 0, -1, -1);
1218
 
1219
      devc->v_alloc->max_voice = devc->nr_voice = 18;
1220
      devc->fm_info.nr_drums = 0;
1221
      devc->fm_info.synth_subtype = FM_TYPE_OPL3;
1222
      devc->fm_info.capabilities |= SYNTH_CAP_OPL3;
1223
      strcpy (devc->fm_info.name, "Yamaha OPL-3");
1224
 
1225
      for (i = 0; i < 18; i++)
1226
        if (pv_map[i].ioaddr == USE_LEFT)
1227
          pv_map[i].ioaddr = devc->left_io;
1228
        else
1229
          pv_map[i].ioaddr = devc->right_io;
1230
 
1231
      opl3_command (devc->right_io, OPL3_MODE_REGISTER, OPL3_ENABLE);
1232
      opl3_command (devc->right_io, CONNECTION_SELECT_REGISTER, 0x00);
1233
    }
1234
  else
1235
    {
1236
      conf_printf2 ("Yamaha OPL2 FM", ioaddr, 0, -1, -1);
1237
      devc->v_alloc->max_voice = devc->nr_voice = 9;
1238
      devc->fm_info.nr_drums = 0;
1239
 
1240
      for (i = 0; i < 18; i++)
1241
        pv_map[i].ioaddr = devc->left_io;
1242
    };
1243
 
1244
  for (i = 0; i < SBFM_MAXINSTR; i++)
1245
    devc->i_map[i].channel = -1;
1246
 
1247
}
1248
 
1249
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.