AS3525: Implement a true audio pause and full-resolution audio tick. Take care of...
[kugel-rb.git] / firmware / buffer.c
blob4ede24df21ce1fe2a42748c821f8eda6f58a99eb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include "buffer.h"
23 #include "panic.h"
24 #include "logf.h"
26 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
27 unsigned char audiobuffer[(MEM*1024-256)*1024];
28 unsigned char *audiobufend = audiobuffer + sizeof(audiobuffer);
29 #else
30 /* defined in linker script */
31 extern unsigned char audiobuffer[];
32 #endif
34 unsigned char *audiobuf;
36 #ifdef BUFFER_ALLOC_DEBUG
37 static unsigned char *audiobuf_orig_start;
39 struct buffer_start_marker
41 unsigned int magic;
42 size_t buffer_size;
44 #define BUF_MAGIC 0xDEADD0D0
46 struct buffer_end_marker
48 unsigned int magic;
49 int last;
51 #endif /* BUFFER_ALLOC_DEBUG */
53 void buffer_init(void)
55 /* 32-bit aligned */
56 audiobuf = (void *)(((unsigned long)audiobuffer + 3) & ~3);
57 #ifdef BUFFER_ALLOC_DEBUG
58 audiobuf_orig_start = audiobuf;
59 #endif /* BUFFER_ALLOC_DEBUG */
62 void *buffer_alloc(size_t size)
64 void *retval = audiobuf;
65 #ifdef BUFFER_ALLOC_DEBUG
66 struct buffer_start_marker *start;
67 struct buffer_end_marker *end;
68 #endif /* BUFFER_ALLOC_DEBUG */
70 /* 32-bit aligned */
71 size = (size + 3) & ~3;
73 #ifdef BUFFER_ALLOC_DEBUG
74 retval +=sizeof(struct buffer_start_marker);
75 if(size>0)
77 end=(struct buffer_end_marker*)(audiobuf - sizeof(struct buffer_end_marker));
78 if(end->magic == BUF_MAGIC)
80 end->last=0;
82 start=(struct buffer_start_marker*)audiobuf;
83 start->magic = BUF_MAGIC;
84 start->buffer_size = size;
85 end=(struct buffer_end_marker*)(audiobuf+sizeof(struct buffer_start_marker)+size);
86 end->magic = BUF_MAGIC;
87 end->last = 1;
89 audiobuf = ((unsigned char *)end) + sizeof(struct buffer_end_marker);
92 logf("Alloc %x %d",(unsigned int)retval,size);
93 #else /* !BUFFER_ALLOC_DEBUG */
94 audiobuf += size;
95 #endif /* BUFFER_ALLOC_DEBUG */
97 if (audiobuf > audiobufend) {
98 panicf("OOM: %d bytes", (int) size);
101 return retval;
104 #ifdef BUFFER_ALLOC_DEBUG
105 void buffer_alloc_check(char *name)
107 unsigned char *buf_ptr = audiobuf_orig_start;
108 struct buffer_start_marker *start;
109 struct buffer_end_marker *end;
112 while(buf_ptr < audiobuf)
114 start=(struct buffer_start_marker*)buf_ptr;
115 if(start->magic != BUF_MAGIC)
117 panicf("%s corrupted buffer %x start", name,(unsigned int)buf_ptr+sizeof(struct buffer_start_marker));
119 end=(struct buffer_end_marker*)(buf_ptr+sizeof(struct buffer_start_marker)+start->buffer_size);
120 if(end->magic != BUF_MAGIC)
122 panicf("%s corrupted %x end", name,(unsigned int)buf_ptr+sizeof(struct buffer_start_marker));
124 if(end->last)
125 break;
126 buf_ptr=((unsigned char *)end)+sizeof(struct buffer_end_marker);
129 #endif /* BUFFER_ALLOC_DEBUG */