2002-04-22 12:07:34 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Ulf Ralberg
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "thread.h"
|
|
|
|
|
|
|
|
typedef union
|
|
|
|
{
|
2002-04-22 23:41:29 +00:00
|
|
|
struct regs_t
|
|
|
|
{
|
|
|
|
unsigned int r[7]; /* Registers r8 thru r14 */
|
2002-04-25 12:46:08 +00:00
|
|
|
void *sp; /* Stack pointer (r15) */
|
2002-04-25 13:20:43 +00:00
|
|
|
unsigned int mach;
|
|
|
|
unsigned int macl;
|
2002-04-22 23:41:29 +00:00
|
|
|
unsigned int sr; /* Status register */
|
|
|
|
void* pr; /* Procedure register */
|
|
|
|
} regs;
|
2002-04-25 13:20:43 +00:00
|
|
|
unsigned int mem[12];
|
2002-04-22 12:07:34 +00:00
|
|
|
} ctx_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2002-04-22 23:41:29 +00:00
|
|
|
int created;
|
|
|
|
int current;
|
2002-05-28 13:59:24 +00:00
|
|
|
ctx_t ctx[MAXTHREADS];
|
2002-04-22 12:07:34 +00:00
|
|
|
} thread_t;
|
|
|
|
|
|
|
|
static thread_t threads = {1, 0};
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Store non-volatile context.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2002-04-22 22:48:07 +00:00
|
|
|
static inline void stctx(void* addr)
|
2002-04-22 12:07:34 +00:00
|
|
|
{
|
2002-04-25 13:20:43 +00:00
|
|
|
asm volatile ("add #48, %0\n\t"
|
|
|
|
"sts.l pr, @-%0\n\t"
|
2002-04-25 12:23:37 +00:00
|
|
|
"stc.l sr, @-%0\n\t"
|
2002-04-25 12:43:55 +00:00
|
|
|
"sts.l macl,@-%0\n\t"
|
|
|
|
"sts.l mach,@-%0\n\t"
|
2002-04-25 12:23:37 +00:00
|
|
|
"mov.l r15, @-%0\n\t"
|
|
|
|
"mov.l r14, @-%0\n\t"
|
|
|
|
"mov.l r13, @-%0\n\t"
|
|
|
|
"mov.l r12, @-%0\n\t"
|
|
|
|
"mov.l r11, @-%0\n\t"
|
|
|
|
"mov.l r10, @-%0\n\t"
|
|
|
|
"mov.l r9, @-%0\n\t"
|
2002-04-25 13:20:43 +00:00
|
|
|
"mov.l r8, @-%0" : : "r" (addr));
|
2002-04-22 12:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Load non-volatile context.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2002-04-22 22:48:07 +00:00
|
|
|
static inline void ldctx(void* addr)
|
2002-04-22 12:07:34 +00:00
|
|
|
{
|
2002-04-25 12:23:37 +00:00
|
|
|
asm volatile ("mov.l @%0+,r8\n\t"
|
|
|
|
"mov.l @%0+,r9\n\t"
|
|
|
|
"mov.l @%0+,r10\n\t"
|
|
|
|
"mov.l @%0+,r11\n\t"
|
|
|
|
"mov.l @%0+,r12\n\t"
|
|
|
|
"mov.l @%0+,r13\n\t"
|
|
|
|
"mov.l @%0+,r14\n\t"
|
|
|
|
"mov.l @%0+,r15\n\t"
|
2002-04-25 12:43:55 +00:00
|
|
|
"lds.l @%0+,mach\n\t"
|
|
|
|
"lds.l @%0+,macl\n\t"
|
2002-04-25 12:30:39 +00:00
|
|
|
"ldc.l @%0+,sr\n\t"
|
2002-04-25 13:20:43 +00:00
|
|
|
"mov.l @%0,%0\n\t"
|
|
|
|
"lds %0,pr\n\t"
|
|
|
|
"mov.l %0, @(0, r15)" : "+r" (addr));
|
2002-04-22 12:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Switch thread in round robin fashion.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
switch_thread(void)
|
|
|
|
{
|
2002-04-22 23:41:29 +00:00
|
|
|
int ct;
|
|
|
|
int nt;
|
|
|
|
thread_t* t = &threads;
|
2002-04-22 12:07:34 +00:00
|
|
|
|
2002-04-22 23:41:29 +00:00
|
|
|
nt = ct = t->current;
|
|
|
|
if (++nt >= t->created)
|
|
|
|
nt = 0;
|
|
|
|
t->current = nt;
|
|
|
|
stctx(&t->ctx[ct]);
|
|
|
|
ldctx(&t->ctx[nt]);
|
2002-04-22 12:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
2002-04-25 13:25:54 +00:00
|
|
|
* Create thread.
|
2002-04-22 12:07:34 +00:00
|
|
|
* Return 0 if context area could be allocated, else -1.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2002-04-22 19:11:17 +00:00
|
|
|
int create_thread(void* fp, void* sp, int stk_size)
|
2002-04-22 12:07:34 +00:00
|
|
|
{
|
2002-04-22 23:41:29 +00:00
|
|
|
thread_t* t = &threads;
|
2002-04-22 12:07:34 +00:00
|
|
|
|
2002-04-22 23:41:29 +00:00
|
|
|
if (t->created >= MAXTHREADS)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctx_t* ctxp = &t->ctx[t->created++];
|
|
|
|
stctx(ctxp);
|
2002-05-28 13:59:24 +00:00
|
|
|
/* Subtract 4 to leave room for the PR push in ldctx()
|
|
|
|
Align it on an even 32 bit boundary */
|
|
|
|
ctxp->regs.sp = (void*)(((unsigned int)sp + stk_size - 4) & ~3;
|
2002-04-22 23:41:29 +00:00
|
|
|
ctxp->regs.pr = fp;
|
|
|
|
}
|
|
|
|
return 0;
|
2002-04-22 12:07:34 +00:00
|
|
|
}
|