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

Subversion Repositories or1k

[/] [or1k/] [tags/] [first/] [mp3/] [sw/] [mad-xess/] [minimad.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 266 lampret
/*
2
 * mad - MPEG audio decoder
3
 * Copyright (C) 2000-2001 Robert Leslie
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 * $Id: minimad.c,v 1.1.1.1 2001-11-04 19:00:46 lampret Exp $
20
 */
21
 
22
#ifdef HAVE_CONFIG_H
23
#include "config.h"
24
#endif
25
 
26
#ifndef EMBED
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <sys/types.h>
31
#include <sys/stat.h>
32
#include <unistd.h>
33
#endif
34
#include "audio.h"
35
#include "mad.h"
36
#include "fsyst.h"
37
 
38
volatile int button;
39
//#define OR1K_SIM
40
 
41
#define AUDIO_DBG       0
42
 
43
#if AUDIO_DBG
44
extern unsigned short wave_dump[(0x40000/2)];
45
extern int wave_seg_index[0x10000];
46
extern int wave_seg_nb;
47
extern int wave_index;
48
#endif
49
 
50
#define SRAM_BASE       0x80000000
51
#define SUCCESS_CODE    0x11223344
52
#define REG32(x)        (*(volatile unsigned long *)(x))
53
#define BUTTON_STATE    (!((*((volatile unsigned long *)0x40000000)) & 0x80000000))
54
//#define BUTTON_STATE  (0)
55
 
56
void report1(unsigned long value)
57
{
58
        unsigned long spr = 0x1234;
59
        asm("l.mtspr\t\t%0,%1,0x0" : : "r" (spr), "r" (value));
60
        return;
61
}
62
/*
63
int test_button(void)
64
{
65
  if(BUTTON_STATE) {
66
    button = 1;
67
    return 0;
68
  }
69
  else {
70
    if(button == 1) {
71
      while(1) {
72
        int l = 0;
73
        int i,j,k;
74
        for(i = 0; i < 3; i++) {
75
          for(j = 0; j < 100000; j++)
76
            k = BUTTON_STATE;
77
          l += k;
78
        }
79
        if(l == 0) {
80
          button = 0;
81
          return 1;
82
        }
83
      }
84
    }
85
    else {
86
      button = 0;
87
      return 0;
88
    }
89
  }
90
  return 0;
91
}
92
*/
93
int test_button(void)
94
{
95
  if(BUTTON_STATE)
96
    while(1) {
97
      int l = 0;
98
      int i,j,k;
99
      for(i = 0; i < 3; i++) {
100
        for(j = 0; j < 100000; j++)
101
          k = BUTTON_STATE;
102
        l += k;
103
      }
104
      if(l == 0) {
105
        button = 0;
106
        return 1;
107
      }
108
    }
109
  return 0;
110
}
111
 
112
# ifdef EMBED
113
int main();
114
 
115
void c_reset()
116
{
117
        register unsigned long i asm("r5");
118
        register int longgggjump asm("r3");
119
        register unsigned long main_ofs asm("r4");
120
//      memcpy((void *)0x80000000, (void *)0, 0x1000);
121
/*              for (i=0x100; i < 0x13000; i+=4)
122
                  *(unsigned long *)(0x80000000+i) = *(unsigned long *)(0x0+i);
123
              main_ofs = (unsigned long)main;
124
              asm("l.movhi r3,0x8000");
125
              asm("l.add r3,r3,%0" : : "r" (main_ofs));
126
              asm("l.jr r3");
127
              asm("l.nop");
128
*/
129
              main(0,0);
130
}
131
# endif
132
 
133
/* Root file location.  */
134
unsigned char *root_file;
135
 
136
static int nchan = 0, speed = 0;
137
# ifndef EMBED
138
FILE *fo;
139
int output_s(unsigned char const *ptr, unsigned int len);
140
# endif
141
 
142
static void
143
output(struct mad_pcm *pcm)
144
{
145
  unsigned int nchannels;
146
# ifndef EMBED
147
  union audio_control control;
148
# endif
149
  mad_fixed_t *ch1, *ch2;
150
 
151
  nchannels = pcm->channels;
152
 
153
  if (nchannels != nchan || speed != pcm->samplerate)
154
    {
155
# ifndef EMBED
156
      control.command = AUDIO_COMMAND_CONFIG;
157
 
158
      control.config.channels = nchannels;
159
      control.config.speed    = pcm->samplerate;
160
      printf ("%i ", pcm->samplerate);
161
      if (audio_oss(&control) == -1)
162
        return;
163
# endif 
164
      nchan = nchannels;
165
      speed = pcm->samplerate;
166
    }
167
  ch1 = &pcm->samples[0][0];
168
  ch2 = &pcm->samples[1][0];
169
 
170
  if (nchan == 1)
171
    ch2 = ch1;
172
 
173
  {
174
    unsigned char data[MAX_NSAMPLES * 4 * 2];
175
    unsigned int len;
176
 
177
    len = audio_pcm_s16le(data, pcm->length, ch1, ch2);
178
# ifndef EMBED
179
    output_s(data, len);
180
# endif
181
  }
182
  return;
183
}
184
#ifdef EMBED
185
void report(unsigned long value)
186
{
187
        unsigned long spr = 0x1234;
188
        asm("l.mtspr\t\t%0,%1,0x0" : : "r" (spr), "r" (value));
189
        return;
190
}
191
#endif
192
 
193
struct mad_pcm pcm;
194
/* Generates beep of 32 samples with sampling frequency
195
   freq, vould = 0..31. */
196
static void genbeep (int freq, int volume)
197
{
198
  int o = MAD_F_ONE/256 * volume, i;
199
  pcm.length = 32;
200
  pcm.samplerate = freq;
201
  pcm.channels = 1;
202
  for (i = 0; i < 8; i++)
203
 {
204
    pcm.samples[0][i] = i * o;
205
//     report(i);
206
  }
207
  for (i = 0; i < 8; i++)
208
    pcm.samples[0][i+8] = (8 - i) * o;
209
  output (&pcm);
210
  for (i = 0; i < 16; i++)
211
    pcm.samples[0][16+i] = -pcm.samples[0][i];
212
  output (&pcm);
213
}
214
 
215
static void short_beep ()
216
{
217
  int i;
218
  for (i = 0; i < 5; i++)
219
    genbeep(44100, 8);
220
}
221
 
222
/* private message buffer */
223
 
224
struct buffer {
225
  unsigned char *start;
226
  unsigned long length;
227
};
228
 
229
/* 2. called when more input is needed; refill stream buffer */
230
 
231
static
232
enum mad_flow input(void *data,
233
                    struct mad_stream *stream)
234
{
235
  struct buffer *buffer = data;
236
 
237
  if (!buffer->length)
238
    return MAD_FLOW_STOP;
239
 
240
  mad_stream_buffer(stream, buffer->start, buffer->length);
241
 
242
  buffer->length = 0;
243
 
244
  return MAD_FLOW_CONTINUE;
245
}
246
 
247
/* 4. called to handle a decoding error */
248
 
249
static
250
enum mad_flow error(void *data,
251
                    struct mad_stream *stream,
252
                    struct mad_frame *frame)
253
{
254
#ifndef EMBED
255
  struct buffer *buffer = data;
256
  fprintf(stderr, "decoding error 0x%04x at byte offset %u\n",
257
          stream->error, stream->this_frame - buffer->start);
258
#endif
259
 
260
  return MAD_FLOW_STOP;
261
}
262
 
263
/* 5. put it all together */
264
 
265
static
266
int decode(unsigned char *start, unsigned long length)
267
{
268
  struct buffer buffer;
269
  struct mad_decoder decoder;
270
  int result = 0;
271
 
272
  buffer.start  = start;
273
  buffer.length = length;
274
 
275
  /* configure input, and error functions */
276
 
277
  mad_decoder_init(&decoder, &buffer,
278
                   input, error);
279
 
280
  /* start the decoder */
281
 
282
  result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
283
 
284
  mad_decoder_finish(&decoder);
285
 
286
  return result;
287
}
288
 
289
#ifndef EMBED
290
static void *init_flash (char *filename)
291
{
292
  struct stat f_stat;
293
  void *flash;
294
  FILE *f = fopen (filename, "rb");
295
  if (!f)
296
    return NULL;
297
  stat (filename, &f_stat);
298
  flash = malloc (f_stat.st_size);
299
  if (!flash)
300
    return NULL;
301
  if (fread (flash, 1, f_stat.st_size, f) != f_stat.st_size)
302
    return NULL;
303
  fclose (f);
304
  return flash;
305
}
306
#else
307
extern unsigned char flash_data[];
308
#endif
309
 
310
 
311
void __main() {}
312
 
313
int main(int argc, char *argv[])
314
{
315
  int i;
316
  int result;
317
 
318
 
319
#ifndef EMBED
320
  union audio_control control;
321
  if (argc != 2)
322
    {
323
      printf ("Usage: minimad image_file.mfs\n");
324
      return 1;
325
    }
326
  if (!(root_file = (char *) init_flash (argv[1])))
327
    {
328
      fprintf (stderr, "Error loading image file '%s'\n", argv[1]);
329
      return 2;
330
    }
331
 
332
  control.command   = AUDIO_COMMAND_INIT;
333
  control.init.path = NULL;
334
  if (audio_oss(&control) == -1) {
335
    printf("audio %s\n", audio_error);
336
    return 3;
337
  }
338
  fo = fopen ("audio.pcm", "wb+");
339
#else
340
  /* In EMBEDDED applications we have flash data linked.  */
341
  root_file = &flash_data[0];
342
#ifdef OR1K_SIM
343
  asm volatile ("l.mtspr r0,r0,0x0FFFD");
344
#endif
345
#endif  
346
  button = 0;
347
 
348
 
349
  for (i = 1;; i++) {
350
    struct file_struct *fs, *datat;
351
    fs = find_track_no (i, ROOT_FILE);
352
//    if (!fs) break;
353
    if(!fs) {
354
      i = 1;
355
      fs = find_track_no (i, ROOT_FILE);
356
    }
357
    datat = track_data (fs);
358
    short_beep();
359
 
360
# ifndef EMBED
361
    {
362
      unsigned u, v;
363
      u = swap(datat->length);
364
      v = swap(datat->data[0]);
365
      printf ("Track %u: %s\n", i, track_name (fs));
366
      printf ("%08x:%08x:%08x\n", datat->data[0], v, u);
367
    }
368
# endif
369
    result = decode((unsigned char *)&datat->data[0], swap(datat->length) - sizeof(struct file_struct) + sizeof (unsigned int));
370
    if(result != 1) {
371
      while(!test_button());
372
    }
373
  }
374
 
375
  short_beep();
376
#ifndef EMBED
377
  control.command = AUDIO_COMMAND_FINISH;
378
  if (audio_oss(&control) == -1) {
379
    printf("audio %s\n", audio_error);
380
    return 3;
381
  }
382
  free (root_file);
383
  printf ("Done.\n");
384
  fclose (fo);
385
#else
386
#ifdef OR1K_SIM
387
  asm volatile("l.mtspr r0,r0,0x0FFFF");
388
#endif
389
#endif
390
 
391
#if AUDIO_DBG
392
report1(wave_index);
393
report1(wave_seg_nb);
394
 
395
report1(wave_seg_index[10]);
396
report1(wave_dump[wave_seg_index[10]]);
397
 
398
report1(wave_seg_index[11]);
399
report1(wave_dump[wave_seg_index[11]]);
400
 
401
report1(wave_seg_index[12]);
402
report1(wave_dump[wave_seg_index[12]]);
403
#endif
404
 
405
while(1) {
406
                REG32(SRAM_BASE + 0x00) = (SUCCESS_CODE >> 0) & 0x000000ff;
407
                REG32(SRAM_BASE + 0x40) = (SUCCESS_CODE >> 8) & 0x000000ff;
408
                REG32(SRAM_BASE + 0x80) = (SUCCESS_CODE >> 16) & 0x000000ff;
409
                REG32(SRAM_BASE + 0xc0) = (SUCCESS_CODE >> 24) & 0x000000ff;
410
        }
411
  return 0;
412
}
413
 

powered by: WebSVN 2.1.0

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