musix 0.3.5

Music player library for esoteric audio formats (music from C64,Amiga etc)
Documentation
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
/*
 * @file    paula_io.c
 * @brief   Paula I/O plugin (Amiga soundchip)
 * @author  http://sourceforge.net/users/benjihan
 *
 * Copyright (C) 1998-2011 Benjamin Gerard
 *
 * Time-stamp: <2011-10-06 14:14:38 ben>
 *
 * 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 3 of the
 * License, or (at your option) any later version.
 *
 * 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.
 *
 * If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "paula_io.h"

typedef struct {
  io68_t io;
  paula_t paula;
} paula_io68_t;

static void reload(paulav_t * const v, const u8 * const p, const int fix);

static inline int clearset(const int v, const int clrset)
{
  if (clrset & 0x8000) {
    return (v | clrset) & 0x7FFF;
  } else {
    return v & ~clrset;
  }
}

static inline int DMACON(const int dmacon) {
  return (-!!(dmacon&(1<<9))) & dmacon & 0xF;
}

static inline int INTREQ(const int intreq) {
  return intreq & (0xF<<7);
}

static inline int INTENA(const int intena) {
  return (-!!(intena&(1<<14))) & intena & (0xF<<7);
}

/* $$$ I am not sure what really happen in case of byte access on the
   Amiga hardware Word registers. Currently I assume a ``normal''
   behavior.
*/

static int68_t _paula_readB(paula_io68_t * const paulaio,
                            addr68_t const addr)
{
  const int i = (u8)addr;
  paula_t * const paula = &paulaio->paula;
  u8 v;

  switch(i) {
  case PAULA_VHPOSR:
    /* This is an ugly HAXXX: some player use this register in order
     * to wait for an actual DMA access to occur. Here the register is
     * just incremented each time it is read so that these players do
     * not loop forever.
     */
    v = paula->vhpos++;
    break;

  case PAULA_DMACONRH:
    v = /* paula->map[PAULA_DMACONRH] = */ (paula->dmacon>>8)&0x7f;
    break;
  case PAULA_DMACONRL:
    v = /* paula->map[PAULA_DMACONRL] = */ (u8)paula->dmacon;
    break;

  case PAULA_INTENARH:
    v = /* paula->map[PAULA_INTENARH] =  */(paula->intena>>8)&0x7f;
    break;
  case PAULA_INTENARL:
    v = /* paula->map[PAULA_INTENARL] =  */(u8)paula->intena;
    break;

  case PAULA_INTREQRH:
    v = /* paula->map[PAULA_INTREQRH] =  */(paula->intreq>>8)&0x7f;
    break;
  case PAULA_INTREQRL:
    v = /* paula->map[PAULA_INTREQRL] =  */(u8)paula->intreq;
    break;

  case PAULA_ADKCONRH:
    v = /* paula->map[PAULA_ADKCONRH] =  */(paula->adkcon>>8)&0x7f;
    break;
  case PAULA_ADKCONRL:
    v = /* paula->map[PAULA_ADKCONRL] =  */(u8)paula->adkcon;
    break;

  default:
    v = paula->map[i];
    break;
  }
  return v;
}

static int68_t _paula_readW(paula_io68_t * const paulaio,
                            addr68_t const addr)
{
  const int i = (u8)addr;
  paula_t * const paula = &paulaio->paula;
  u16 v;

  switch (i) {
  case PAULA_DMACONR:
    v =  paula->dmacon & 0x7fff;
    break;
  case PAULA_INTENAR:
    v =  paula->intena & 0x7fff;
    break;
  case PAULA_INTREQR:
    v =  paula->intreq & 0x7fff;
    break;
  case PAULA_ADKCON:
    v =  paula->adkcon & 0x7fff;
    break;
  default:
    v = (paula->map[i]<<8) | paula->map[i+1];
    break;
  }
  return v;
}

static void paulaio_readB(io68_t * const io)
{
  io->emu68->bus_data =
    _paula_readB((paula_io68_t *)io, io->emu68->bus_addr);
}

static void paulaio_readW(io68_t * const io)
{
  io->emu68->bus_data =
    _paula_readW((paula_io68_t *)io, io->emu68->bus_addr);
}

static void paulaio_readL(io68_t * const io)
{
  io->emu68->bus_data = 0
    |(_paula_readW((paula_io68_t *)io, io->emu68->bus_addr+0)<<16)
    |(_paula_readW((paula_io68_t *)io, io->emu68->bus_addr+2)    )
    ;
}

#if 0
/* start DMA on voice n ( DMA disable -> enable ) */
static void start_DMA(paula_t * const paula, const int bit)
{
  int old,chg,cur;

  old = DMACON(paula->dmacon);
  paula->dmacon |= bit;
  cur = DMACON(paula->dmacon);
  chg = cur & ~old;

  if (chg&1) reload(paula->voice+0, paula->map+PAULA_VOICEA, paula->ct_fix);
  if (chg&2) reload(paula->voice+1, paula->map+PAULA_VOICEB, paula->ct_fix);
  if (chg&4) reload(paula->voice+2, paula->map+PAULA_VOICEC, paula->ct_fix);
  if (chg&8) reload(paula->voice+3, paula->map+PAULA_VOICED, paula->ct_fix);
}
#endif

/* stop DMA bit to be stopped */
static inline void stop_DMA(paula_t * const paula, const int bit)
{
  paula->dmacon &= ~bit;
}

/* Reload paula internal register with current value */
static void reload(paulav_t * v, const u8 * p, const int fix)
{
  plct_t len;

  v->start = v->adr = (plct_t) ( (p[1]<<16) | (p[2]<<8) | p[3] ) << fix;
  len = (p[4]<<8) | p[5];
  len |= (!len) << 16;
  len <<= 1+fix;
  v->end = v->start + len;
}

/* Write INTREQ :
 *
 * - If clearing bits just release the interrupt. Nothing more to do.
 * - If setting bit checks whether the interrupt is denied or not.
 *   When denied it seems that the internal pointer and length register
 *   are reloaded however its is not an official practice.
 */
static void write_intreq(paula_t * const paula, const int intreq)
{
  if ( !(intreq & 0x8000) ) {
    /* Clearing ... */
    paula->intreq &= ~intreq;
    return;
  } else {
    int intdenied;

    /* Master interrupt not set : DENIED */
    intdenied = ~INTENA(paula->intena);
    /* Already requested : DENIED */
    intdenied |= paula->intreq;
    /* Only interrested by requested bits */
    intdenied &= intreq;
    /* Reload for each denied channel */

    /* $$$ May be should not reload if DMA is OFF $$$ */

    if(intdenied & (1<< 7))
      reload(paula->voice+0, paula->map+PAULA_VOICEA, paula->ct_fix);
    if(intdenied & (1<< 8))
      reload(paula->voice+1, paula->map+PAULA_VOICEB, paula->ct_fix);
    if(intdenied & (1<< 9))
      reload(paula->voice+2, paula->map+PAULA_VOICEC, paula->ct_fix);
    if(intdenied & (1<<10))
      reload(paula->voice+3, paula->map+PAULA_VOICED, paula->ct_fix);

    paula->intreq |= intreq;
  }
}

static void _paula_writeB(paula_io68_t * const paulaio,
                          addr68_t const addr, const int68_t data)
{
  const int i = (u8)addr;
  paula_t * const paula = &paulaio->paula;

  paula->map[i] = data;
  switch (i) {

  case PAULA_INTREQL:
    write_intreq(paula,
                 ( paula->map[PAULA_INTREQH] << 8 )
                 |
                 paula->map[PAULA_INTREQL] );
    break;

  }
}

static void _paula_writeW(paula_io68_t * const paulaio,
                          addr68_t const addr, const int68_t data)
{
  const int i = (u8) addr;
  paula_t * const paula = &paulaio->paula;
  const int v = (u16) data;

  /* Copy into hw-reg */
  paula->map[i] = v >> 8;
  paula->map[(u8)(i+1)] = v;

  switch (i) {
  case PAULA_ADKCON: {
    int old_adkcon = paula->adkcon;
    paula->adkcon = clearset(old_adkcon, v);
    if (paula->adkcon & ~old_adkcon & 0xFF) {
      /* Modulation is active !!! */
    }
  } break;

  case PAULA_DMACON: {
    int old_dmacon = paula->dmacon;
    int old_dmaena = DMACON(old_dmacon);
    int new_dmaena;
    int start;

    paula->dmacon = clearset(old_dmacon, v);
    new_dmaena = DMACON(paula->dmacon);

    start = new_dmaena & ~old_dmaena;

    if (start&1) reload(paula->voice+0,paula->map+PAULA_VOICEA,paula->ct_fix);
    if (start&2) reload(paula->voice+1,paula->map+PAULA_VOICEB,paula->ct_fix);
    if (start&4) reload(paula->voice+2,paula->map+PAULA_VOICEC,paula->ct_fix);
    if (start&8) reload(paula->voice+3,paula->map+PAULA_VOICED,paula->ct_fix);

  } break;

  case PAULA_INTENA: {
    int old_intena = INTENA(paula->intena), new_intena;
    //old_intena=old_intena;
    paula->intena = clearset(paula->intena, v);
    new_intena = INTENA(paula->intena);

    if ( new_intena & ~old_intena ) {
      /*Amiga Audio IRQ enabled */
    }
  } break;

  case PAULA_INTREQ:
    write_intreq(paula, v);
    break;

  default:
    break;
  }
}

static void paulaio_writeB(io68_t * const io)
{
  _paula_writeB((paula_io68_t *)io,
                io->emu68->bus_addr, io->emu68->bus_data);
}

static void paulaio_writeW(io68_t * const io)
{
  _paula_writeW((paula_io68_t *)io,
                io->emu68->bus_addr, io->emu68->bus_data);
}

static void paulaio_writeL(io68_t * const io)
{
  _paula_writeW((paula_io68_t *)io,
                io->emu68->bus_addr+0, io->emu68->bus_data>>16);
  _paula_writeW((paula_io68_t *)io,
                io->emu68->bus_addr+2, io->emu68->bus_data);
}

static interrupt68_t * paulaio_interrupt(io68_t * const io, cycle68_t cycle)
{
  return 0;
}

static cycle68_t paulaio_next_interrupt(io68_t * const io, cycle68_t cycle)
{
  return IO68_NO_INT;
}

static void paulaio_adjust_cycle(io68_t * const io, cycle68_t cycle)
{
}

static int paulaio_reset(io68_t * const io)
{
  return paula_reset(&((paula_io68_t *)io)->paula);
}

static void paulaio_destroy(io68_t * const io)
{
  if (io) {
    paula_cleanup(&((paula_io68_t *)io)->paula);
    emu68_free(io);
  }
}

static io68_t paula_io = {
  0,
  "AMIGA Paula",
  0xFFDFF000, 0xFFDFF0DF,
  paulaio_readB,  paulaio_readW,  paulaio_readL,
  paulaio_writeB, paulaio_writeW, paulaio_writeL,
  paulaio_interrupt, paulaio_next_interrupt,
  paulaio_adjust_cycle,
  paulaio_reset,
  paulaio_destroy
};


io68_t * paulaio_create(emu68_t * const emu68, paula_parms_t * const parms)
{
  paula_io68_t * paulaio = 0;

  if (emu68) {
    paulaio = emu68_alloc(sizeof(*paulaio));
    if (paulaio) {
      paula_setup_t setup;
      if (parms) {
        setup.parms = *parms;
      } else {
        setup.parms.engine = PAULA_ENGINE_DEFAULT;
        setup.parms.hz     = 0;
        setup.parms.clock  = PAULA_CLOCK_DEFAULT;
      }
      setup.mem     = emu68->mem;
      setup.log2mem = emu68->log2mem;
      paulaio->io = paula_io;
      paula_setup(&paulaio->paula, &setup);
    }
  }
  return &paulaio->io;
}

int paulaio_init(int * argc, char ** argv)
{
  return paula_init(argc, argv);
}

void paulaio_shutdown(void)
{
  paula_shutdown();
}

paula_t * paulaio_emulator(io68_t * const io)
{
  return io
    ? &((paula_io68_t*)io)->paula
    : 0
    ;
}

int paulaio_sampling_rate(io68_t * const io, int hz)
{
  return paula_sampling_rate(paulaio_emulator(io),hz);
}