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

Subversion Repositories or1k

[/] [or1k/] [branches/] [mp3_stable/] [mp3/] [sw/] [mad-xess/] [minimad.c] - Blame information for rev 1781

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

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

powered by: WebSVN 2.1.0

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