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

Subversion Repositories openrisc

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

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
#if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
91
 
92
    // If we caught an exception inside the stubs, see if we were expecting it
93
    // and if so jump to the saved address
94
    if (__mem_fault_handler) {
95
        switch (vec) {
96
          case CYGNUM_HAL_VECTOR_SWI:
97
            regs->spc_swi = (CYG_ADDRWORD)__mem_fault_handler;
98
            break;
99
          case CYGNUM_HAL_VECTOR_FIQ:
100
            regs->spc_fiq = (CYG_ADDRWORD)__mem_fault_handler;
101
            break;
102
          case CYGNUM_HAL_VECTOR_IRQ:
103
            regs->spc_irq = (CYG_ADDRWORD)__mem_fault_handler;
104
            break;
105
          default:
106
            regs->spc_expt = (CYG_ADDRWORD)__mem_fault_handler;
107
            break;
108
        }
109
        return 0; // Caught an exception inside stubs        
110
    }
111
 
112
    // Set the pointer to the registers of the current exception
113
    // context. At entry the GDB stub will expand the
114
    // HAL_SavedRegisters structure into a (bigger) register array.
115
    _hal_registers = regs;
116
    __handle_exception();
117
 
118
#elif defined(CYGFUN_HAL_COMMON_KERNEL_SUPPORT) && defined(CYGPKG_HAL_EXCEPTIONS)
119
 
120
    // We should decode the vector and pass a more appropriate
121
    // value as the second argument. For now we simply pass a
122
    // pointer to the saved registers. We should also divert
123
    // breakpoint and other debug vectors into the debug stubs.
124
 
125
    cyg_hal_deliver_exception( vec, (CYG_ADDRWORD)regs );
126
 
127
#else
128
 
129
    CYG_FAIL("Exception!!!");
130
 
131
#endif    
132
    return 0;
133
}
134
 
135
/*------------------------------------------------------------------------*/
136
/* default ISR                                                            */
137
 
138
#ifndef CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
139
externC cyg_uint32 hal_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
140
{
141
#if defined(CYGDBG_HAL_CALM32_DEBUG_GDB_CTRLC_SUPPORT) &&      \
142
    defined(CYGHWR_HAL_GDB_PORT_VECTOR) &&              \
143
    defined(HAL_CTRLC_ISR)
144
 
145
#ifndef CYGIMP_HAL_COMMON_INTERRUPTS_CHAIN    
146
    if( vector == CYGHWR_HAL_GDB_PORT_VECTOR )
147
#endif        
148
    {
149
        cyg_uint32 result = HAL_CTRLC_ISR( vector, data );
150
        if( result != 0 ) return result;
151
    }
152
 
153
#if defined(CYGSEM_HAL_USE_ROM_MONITOR_CygMon)
154
#if defined(HAL_DIAG_IRQ_CHECK)
155
    {
156
        cyg_uint32 ret;
157
        /* let ROM monitor handle unexpected interrupts */
158
        HAL_DIAG_IRQ_CHECK(vector, ret);
159
        if (ret<=0)
160
            return ret;
161
    }
162
#endif // def HAL_DIAG_IRQ_CHECK
163
#endif // def CYGSEM_HAL_USE_ROM_MONITOR_CygMon
164
#endif
165
 
166
    CYG_TRACE1(true, "Interrupt: %d", vector);
167
    CYG_FAIL("Spurious Interrupt!!!");
168
    return 0;
169
}
170
 
171
#else // CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
172
 
173
externC cyg_uint32 hal_arch_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
174
{
175
#if defined(CYGDBG_HAL_CALM32_DEBUG_GDB_CTRLC_SUPPORT) &&      \
176
    defined(CYGHWR_HAL_GDB_PORT_VECTOR) &&              \
177
    defined(HAL_CTRLC_ISR)
178
 
179
#if defined(CYGSEM_HAL_USE_ROM_MONITOR_CygMon)
180
#if defined(HAL_DIAG_IRQ_CHECK)
181
    {
182
        cyg_uint32 ret;
183
        /* let ROM monitor handle unexpected interrupts */
184
        HAL_DIAG_IRQ_CHECK(vector, ret);
185
        if (ret<=0)
186
            return ret;
187
    }
188
#endif // def HAL_DIAG_IRQ_CHECK
189
#endif // def CYGSEM_HAL_USE_ROM_MONITOR_CygMon
190
#endif
191
 
192
    return 0;
193
}
194
 
195
#endif // CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
196
 
197
/*------------------------------------------------------------------------*/
198
/* data copy and bss zero functions                                       */
199
 
200
typedef void (CYG_SYM_ADDRESS)(void);
201
 
202
// All these must use this type of address to stop them being given relocations
203
// relative to $gp (i.e. assuming they would be in .sdata)
204
extern CYG_SYM_ADDRESS __ram_data_start;
205
extern CYG_SYM_ADDRESS __ram_data_end;
206
extern CYG_SYM_ADDRESS __rom_data_start;
207
 
208
#ifdef CYG_HAL_STARTUP_ROM      
209
void hal_copy_data(void)
210
{
211
    short *p = (short *)&__ram_data_start;
212
    short *q = (short *)&__rom_data_start;
213
    short x;
214
 
215
    while( p != (short *)&__ram_data_end ) {
216
        asm volatile( "ldch %0,@%1\n" : "=r"(x) : "r"(q) );
217
        *p++ = x;
218
        q++;
219
    }
220
}
221
#endif
222
 
223
extern CYG_SYM_ADDRESS __bss_start;
224
extern CYG_SYM_ADDRESS __bss_end;
225
 
226
void hal_zero_bss(void)
227
{
228
    short *p = (short *)&__bss_start;
229
 
230
    while( p != (short *)&__bss_end )
231
        *p++ = 0;
232
}
233
 
234
/*------------------------------------------------------------------------*/
235
 
236
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
237
cyg_bool cyg_hal_stop_constructors;
238
#endif
239
 
240
typedef void (*pfunc) (void);
241
extern pfunc __CTOR_LIST__[];
242
extern pfunc __CTOR_END__[];
243
 
244
void
245
cyg_hal_invoke_constructors(void)
246
{
247
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
248
    static pfunc *p = &__CTOR_END__[-1];
249
 
250
    cyg_hal_stop_constructors = 0;
251
    for (; p >= __CTOR_LIST__; p--) {
252
        (*p) ();
253
        if (cyg_hal_stop_constructors) {
254
            p--;
255
            break;
256
        }
257
    }
258
#else
259
    pfunc *p;
260
 
261
    for (p = &__CTOR_END__[-1]; p >= __CTOR_LIST__; p--)
262
        (*p) ();
263
#endif
264
 
265
} // cyg_hal_invoke_constructors()
266
 
267
/*------------------------------------------------------------------------*/
268
/* Determine the index of the ls bit of the supplied mask.                */
269
 
270
cyg_uint32 hal_lsbit_index(cyg_uint32 mask)
271
{
272
    cyg_uint32 n = mask;
273
 
274
    static const signed char tab[64] =
275
    { -1, 0, 1, 12, 2, 6, 0, 13, 3, 0, 7, 0, 0, 0, 0, 14, 10,
276
      4, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 21, 27 , 15, 31, 11,
277
      5, 0, 0, 0, 0, 0, 9, 0, 0, 24, 0, 0 , 20, 26, 30, 0, 0, 0,
278
      0, 23, 0, 19, 29, 0, 22, 18, 28, 17, 16, 0
279
    };
280
 
281
    n &= ~(n-1UL);
282
    n = (n<<16)-n;
283
    n = (n<<6)+n;
284
    n = (n<<4)+n;
285
 
286
    return tab[n>>26];
287
}
288
 
289
/*------------------------------------------------------------------------*/
290
/* Determine the index of the ms bit of the supplied mask.                */
291
 
292
cyg_uint32 hal_msbit_index(cyg_uint32 mask)
293
{
294
    cyg_uint32 x = mask;
295
    cyg_uint32 w;
296
 
297
    /* Phase 1: make word with all ones from that one to the right */
298
    x |= x >> 16;
299
    x |= x >> 8;
300
    x |= x >> 4;
301
    x |= x >> 2;
302
    x |= x >> 1;
303
 
304
    /* Phase 2: calculate number of "1" bits in the word        */
305
    w = (x & 0x55555555) + ((x >> 1) & 0x55555555);
306
    w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
307
    w = w + (w >> 4);
308
    w = (w & 0x000F000F) + ((w >> 8) & 0x000F000F);
309
    return (cyg_uint32)((w + (w >> 16)) & 0xFF) - 1;
310
 
311
}
312
 
313
/*------------------------------------------------------------------------*/
314
/* Idle thread action                                                     */
315
 
316
#include <cyg/infra/diag.h>
317
 
318
void hal_idle_thread_action( cyg_uint32 count )
319
{
320
}
321
 
322
/*------------------------------------------------------------------------*/
323
/* End of hal_misc.c                                                      */

powered by: WebSVN 2.1.0

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