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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [calmrisc16/] [arch/] [current/] [src/] [hal_misc.c] - Blame information for rev 791

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

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      hal_misc.c
4
//
5
//      HAL miscellaneous functions
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    nickg
43
// Contributors: nickg, jlarmour
44
// Date:         1999-01-21
45
// Purpose:      HAL miscellaneous functions
46
// Description:  This file contains miscellaneous functions provided by the
47
//               HAL.
48
//
49
//####DESCRIPTIONEND####
50
//
51
//========================================================================*/
52
 
53
#include <pkgconf/hal.h>
54
 
55
#include <cyg/infra/cyg_type.h>         // Base types
56
#include <cyg/infra/cyg_trac.h>         // tracing macros
57
#include <cyg/infra/cyg_ass.h>          // assertion macros
58
 
59
#define CYGARC_HAL_COMMON_EXPORT_CPU_MACROS
60
#include <cyg/hal/hal_arch.h>           // architectural definitions
61
 
62
#include <cyg/hal/hal_intr.h>           // Interrupt handling
63
 
64
#include <cyg/hal/hal_cache.h>          // Cache handling
65
 
66
/*------------------------------------------------------------------------*/
67
/* If required, define a variable to store the clock period.              */
68
 
69
#ifdef CYGHWR_HAL_CLOCK_PERIOD_DEFINED
70
 
71
CYG_WORD32 cyg_hal_clock_period;
72
 
73
#endif
74
 
75
/*------------------------------------------------------------------------*/
76
/* First level C exception handler.                                       */
77
 
78
externC void __handle_exception (void);
79
 
80
externC HAL_SavedRegisters *_hal_registers;
81
 
82
#if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
83
externC void* volatile __mem_fault_handler;
84
#endif
85
 
86
externC cyg_uint32 cyg_hal_exception_handler(HAL_SavedRegisters *regs)
87
{
88
    int vec = regs->vector;
89
 
90
    // adjust PC after 'break' insns
91
    if (vec == CYGNUM_HAL_VECTOR_IRQ) {
92
        if (__read_prog_uint16((void *)(regs->spc_irq - 2)) == HAL_BREAKINST)
93
            regs->spc_irq -= 2;
94
    }
95
 
96
#if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
97
 
98
    // If we caught an exception inside the stubs, see if we were expecting it
99
    // and if so jump to the saved address
100
    if (__mem_fault_handler) {
101
        switch (vec) {
102
          case CYGNUM_HAL_VECTOR_SWI:
103
            regs->a[6] = (CYG_ADDRWORD)__mem_fault_handler;
104
            break;
105
          case CYGNUM_HAL_VECTOR_FIQ:
106
            regs->spc_fiq = (CYG_ADDRWORD)__mem_fault_handler;
107
            break;
108
          default:
109
            regs->spc_irq = (CYG_ADDRWORD)__mem_fault_handler;
110
            break;
111
        }
112
        return 0; // Caught an exception inside stubs        
113
    }
114
 
115
    // Set the pointer to the registers of the current exception
116
    // context. At entry the GDB stub will expand the
117
    // HAL_SavedRegisters structure into a (bigger) register array.
118
    _hal_registers = regs;
119
    __handle_exception();
120
 
121
#elif defined(CYGFUN_HAL_COMMON_KERNEL_SUPPORT) && defined(CYGPKG_HAL_EXCEPTIONS)
122
 
123
    // We should decode the vector and pass a more appropriate
124
    // value as the second argument. For now we simply pass a
125
    // pointer to the saved registers. We should also divert
126
    // breakpoint and other debug vectors into the debug stubs.
127
 
128
    cyg_hal_deliver_exception( vec, (CYG_ADDRWORD)regs );
129
 
130
#else
131
 
132
    CYG_FAIL("Exception!!!");
133
 
134
#endif    
135
    return 0;
136
}
137
 
138
/*------------------------------------------------------------------------*/
139
/* default ISR                                                            */
140
 
141
#ifndef CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
142
externC cyg_uint32 hal_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
143
{
144
#if defined(CYGDBG_HAL_CALM32_DEBUG_GDB_CTRLC_SUPPORT) &&      \
145
    defined(CYGHWR_HAL_GDB_PORT_VECTOR) &&              \
146
    defined(HAL_CTRLC_ISR)
147
 
148
#ifndef CYGIMP_HAL_COMMON_INTERRUPTS_CHAIN    
149
    if( vector == CYGHWR_HAL_GDB_PORT_VECTOR )
150
#endif        
151
    {
152
        cyg_uint32 result = HAL_CTRLC_ISR( vector, data );
153
        if( result != 0 ) return result;
154
    }
155
 
156
#if defined(CYGSEM_HAL_USE_ROM_MONITOR_CygMon)
157
#if defined(HAL_DIAG_IRQ_CHECK)
158
    {
159
        cyg_uint32 ret;
160
        /* let ROM monitor handle unexpected interrupts */
161
        HAL_DIAG_IRQ_CHECK(vector, ret);
162
        if (ret<=0)
163
            return ret;
164
    }
165
#endif // def HAL_DIAG_IRQ_CHECK
166
#endif // def CYGSEM_HAL_USE_ROM_MONITOR_CygMon
167
#endif
168
 
169
    CYG_TRACE1(true, "Interrupt: %d", vector);
170
    CYG_FAIL("Spurious Interrupt!!!");
171
    return 0;
172
}
173
 
174
#else // CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
175
 
176
externC cyg_uint32 hal_arch_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
177
{
178
#if defined(CYGDBG_HAL_CALM16_DEBUG_GDB_CTRLC_SUPPORT) &&      \
179
    defined(CYGHWR_HAL_GDB_PORT_VECTOR) &&              \
180
    defined(HAL_CTRLC_ISR)
181
 
182
#if defined(CYGSEM_HAL_USE_ROM_MONITOR_CygMon)
183
#if defined(HAL_DIAG_IRQ_CHECK)
184
    {
185
        cyg_uint32 ret;
186
        /* let ROM monitor handle unexpected interrupts */
187
        HAL_DIAG_IRQ_CHECK(vector, ret);
188
        if (ret<=0)
189
            return ret;
190
    }
191
#endif // def HAL_DIAG_IRQ_CHECK
192
#endif // def CYGSEM_HAL_USE_ROM_MONITOR_CygMon
193
#endif
194
 
195
    return 0;
196
}
197
 
198
#endif // CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
199
 
200
/*------------------------------------------------------------------------*/
201
/* data copy and bss zero functions                                       */
202
 
203
typedef void (CYG_SYM_ADDRESS)(void);
204
 
205
// All these must use this type of address to stop them being given relocations
206
// relative to $gp (i.e. assuming they would be in .sdata)
207
extern CYG_SYM_ADDRESS __ram_data_start;
208
extern CYG_SYM_ADDRESS __ram_data_end;
209
extern CYG_SYM_ADDRESS __rom_data_start;
210
 
211
#ifdef CYG_HAL_STARTUP_ROM      
212
void hal_copy_data(void)
213
{
214
    short *p = (short *)&__ram_data_start;
215
    short *q = (short *)&__rom_data_start;
216
    short x;
217
 
218
    while( p != (short *)&__ram_data_end ) {
219
        asm volatile( "ldc %0,@%1\n" : "=r"(x) : "r"(q) );
220
        *p++ = x;
221
        q++;
222
    }
223
}
224
#endif
225
 
226
extern CYG_SYM_ADDRESS __bss_start;
227
extern CYG_SYM_ADDRESS __bss_end;
228
 
229
void hal_zero_bss(void)
230
{
231
    short *p = (short *)&__bss_start;
232
 
233
    while( p != (short *)&__bss_end )
234
        *p++ = 0;
235
}
236
 
237
/*------------------------------------------------------------------------*/
238
 
239
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
240
cyg_bool cyg_hal_stop_constructors;
241
#endif
242
 
243
typedef void (*pfunc) (void);
244
extern pfunc __CTOR_LIST__[];
245
extern pfunc __CTOR_END__[];
246
 
247
void
248
cyg_hal_invoke_constructors(void)
249
{
250
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
251
    static pfunc *p = &__CTOR_END__[-1];
252
 
253
    cyg_hal_stop_constructors = 0;
254
    for (; p >= __CTOR_LIST__; p--) {
255
        (*p) ();
256
        if (cyg_hal_stop_constructors) {
257
            p--;
258
            break;
259
        }
260
    }
261
#else
262
    pfunc *p;
263
 
264
    for (p = &__CTOR_END__[-1]; p >= __CTOR_LIST__; p--)
265
        (*p) ();
266
#endif
267
 
268
} // cyg_hal_invoke_constructors()
269
 
270
/*------------------------------------------------------------------------*/
271
/* Determine the index of the ls bit of the supplied mask.                */
272
 
273
cyg_uint32 hal_lsbit_index(cyg_uint32 mask)
274
{
275
    cyg_uint32 n = mask;
276
 
277
    static const signed char tab[64] =
278
    { -1, 0, 1, 12, 2, 6, 0, 13, 3, 0, 7, 0, 0, 0, 0, 14, 10,
279
      4, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 21, 27 , 15, 31, 11,
280
      5, 0, 0, 0, 0, 0, 9, 0, 0, 24, 0, 0 , 20, 26, 30, 0, 0, 0,
281
      0, 23, 0, 19, 29, 0, 22, 18, 28, 17, 16, 0
282
    };
283
 
284
    n &= ~(n-1UL);
285
    n = (n<<16)-n;
286
    n = (n<<6)+n;
287
    n = (n<<4)+n;
288
 
289
    return tab[n>>26];
290
}
291
 
292
/*------------------------------------------------------------------------*/
293
/* Determine the index of the ms bit of the supplied mask.                */
294
 
295
cyg_uint32 hal_msbit_index(cyg_uint32 mask)
296
{
297
    cyg_uint32 x = mask;
298
    cyg_uint32 w;
299
 
300
    /* Phase 1: make word with all ones from that one to the right */
301
    x |= x >> 16;
302
    x |= x >> 8;
303
    x |= x >> 4;
304
    x |= x >> 2;
305
    x |= x >> 1;
306
 
307
    /* Phase 2: calculate number of "1" bits in the word        */
308
    w = (x & 0x55555555) + ((x >> 1) & 0x55555555);
309
    w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
310
    w = w + (w >> 4);
311
    w = (w & 0x000F000F) + ((w >> 8) & 0x000F000F);
312
    return (cyg_uint32)((w + (w >> 16)) & 0xFF) - 1;
313
 
314
}
315
 
316
/*------------------------------------------------------------------------*/
317
/* Idle thread action                                                     */
318
 
319
#include <cyg/infra/diag.h>
320
 
321
void hal_idle_thread_action( cyg_uint32 count )
322
{
323
}
324
 
325
/*------------------------------------------------------------------------*/
326
/* End of hal_misc.c                                                      */

powered by: WebSVN 2.1.0

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