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

Subversion Repositories aemb

[/] [aemb/] [trunk/] [sw/] [cc/] [aemb/] [hook.hh] - Blame information for rev 151

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

Line No. Rev Author Line
1 151 sybreon
/* $Id: hook.hh,v 1.10 2008-04-28 20:29:15 sybreon Exp $
2 107 sybreon
**
3
** AEMB2 HI-PERFORMANCE CPU
4
** Copyright (C) 2004-2007 Shawn Tan Ser Ngiap 
5
**
6
** This file is part of AEMB.
7
**
8
** AEMB is free software: you can redistribute it and/or modify it
9 145 sybreon
** under the terms of the GNU General Public License as published by
10
** the Free Software Foundation, either version 3 of the License, or
11
** (at your option) any later version.
12 107 sybreon
**
13
** AEMB is distributed in the hope that it will be useful, but WITHOUT
14
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 145 sybreon
** or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16
** License for more details.
17 107 sybreon
**
18 145 sybreon
** You should have received a copy of the GNU General Public License
19
** along with AEMB.  If not, see .
20 107 sybreon
*/
21
 
22
/**
23
   Basic begin/end hooks
24
   @file hook.hh
25
 
26 116 sybreon
   These routines hook themselves onto parts of the main programme to
27
   enable the hardware threads to work properly.
28 107 sybreon
 */
29
 
30
#include "aemb/stack.hh"
31
#include "aemb/heap.hh"
32
#include "aemb/thread.hh"
33
 
34 151 sybreon
#ifndef _AEMB_HOOK_HH
35
#define _AEMB_HOOK_HH
36 107 sybreon
 
37 139 sybreon
#ifdef __cplusplus
38 151 sybreon
extern "C" {
39 139 sybreon
#endif
40
 
41 121 sybreon
    void _program_init();
42
    void _program_clean();
43 135 sybreon
 
44
    // newlib locks
45
    void __malloc_lock(struct _reent *reent);
46
    void __malloc_unlock(struct _reent *reent);
47
    //void __env_lock(struct _reent *reent);
48
    //void __env_unlock(struct _reent *reent);
49
 
50 107 sybreon
  /**
51 116 sybreon
     Finalisation hook
52
 
53
     This function executes during the shutdown phase after the
54
     finalisation routine is called. It will merge the changes made
55
     during initialisation.
56 121 sybreon
  */
57 135 sybreon
 
58 111 sybreon
  void _program_clean()
59 107 sybreon
  {
60 151 sybreon
    _aembLockMTX(); // enter critical section
61 121 sybreon
 
62 151 sybreon
    // unify the stack backwards
63
    if (aembIsThread0())
64 121 sybreon
      {
65 151 sybreon
        aembSetStack(aembGetStack() + (aembGetStackSize() >> 1));
66 128 sybreon
      }
67 121 sybreon
 
68 151 sybreon
    _aembFreeMTX(); // exit critical section
69 107 sybreon
  }
70
 
71
  /**
72 116 sybreon
     Initialisation hook
73 107 sybreon
 
74 116 sybreon
     This function executes during the startup phase before the
75
     initialisation routine is called. It splits the stack between the
76 135 sybreon
     threads. For now, it will lock up T0 for compatibility purposes.
77 121 sybreon
  */
78 135 sybreon
 
79 111 sybreon
  void _program_init()
80 107 sybreon
  {
81 151 sybreon
    _aembLockMTX(); // enter critical section
82 121 sybreon
 
83 116 sybreon
    // split and shift the stack for thread 1
84 151 sybreon
    if (aembIsThread0()) // main thread
85 116 sybreon
      {
86 135 sybreon
        // NOTE: Dupe the stack otherwise it will FAIL!
87 151 sybreon
        int oldstk = aembGetStack();
88
        int newstk = aembSetStack(aembGetStack() - (aembGetStackSize() >> 1));
89
        aembDupStack((unsigned int *)newstk,
90
                     (unsigned int *)oldstk,
91
                     (unsigned int *)aembGetStackTop());
92
        _aembFreeMTX(); // exit critical section
93 139 sybreon
        while (1) asm volatile ("nop"); // lock thread
94 116 sybreon
      }
95 121 sybreon
 
96 151 sybreon
    _aembFreeMTX(); // exit critical section
97 107 sybreon
  }
98
 
99 128 sybreon
  semaphore __malloc_mutex = 1;
100
 
101 116 sybreon
  /**
102
     Heap Lock
103
 
104
     This function is called during malloc() to lock out the shared
105
     heap to avoid data corruption.
106
   */
107 135 sybreon
 
108
  void __malloc_lock(struct _reent *reent)
109 116 sybreon
  {
110 151 sybreon
    _aembLockMTX();
111 116 sybreon
  }
112
 
113
  /**
114
     Heap Unlock
115
 
116
     This function is called during malloc() to indicate that the
117
     shared heap is now available for another thread.
118
  */
119 135 sybreon
 
120
  void __malloc_unlock(struct _reent *reent)
121 116 sybreon
  {
122 151 sybreon
    _aembFreeMTX();
123 116 sybreon
  }
124 121 sybreon
 
125 139 sybreon
#ifdef __cplusplus
126 116 sybreon
}
127 139 sybreon
#endif
128 116 sybreon
 
129 107 sybreon
#endif
130
 
131 116 sybreon
#ifndef __OPTIMIZE__
132
// The main programme needs to be compiled with optimisations turned
133 135 sybreon
// on (at least -O1). If not, the MUTEX value will be written to the
134
// same RAM location, giving both threads the same value.
135 141 sybreon
OPTIMISATION_REQUIRED OPTIMISATION_REQUIRED
136 116 sybreon
#endif
137
 
138 107 sybreon
/*
139
  $Log: not supported by cvs2svn $
140 151 sybreon
  Revision 1.9  2008/04/27 16:33:42  sybreon
141
  License change to GPL3.
142
 
143 145 sybreon
  Revision 1.8  2008/04/27 16:04:42  sybreon
144
  Minor cosmetic changes.
145
 
146 141 sybreon
  Revision 1.7  2008/04/26 19:31:35  sybreon
147
  Made headers C compatible.
148
 
149 139 sybreon
  Revision 1.6  2008/04/26 18:04:31  sybreon
150
  Updated software to freeze T0 and run T1.
151
 
152 135 sybreon
  Revision 1.5  2008/04/23 14:19:39  sybreon
153
  Fixed minor bugs.
154
  Initial use of hardware mutex.
155
 
156 128 sybreon
  Revision 1.4  2008/04/20 16:35:53  sybreon
157
  Added C/C++ compatible #ifdef statements
158
 
159 121 sybreon
  Revision 1.3  2008/04/12 13:46:02  sybreon
160
  Added malloc() lock and unlock routines
161
 
162 116 sybreon
  Revision 1.2  2008/04/11 15:20:31  sybreon
163
  added static assert hack
164
 
165 111 sybreon
  Revision 1.1  2008/04/09 19:48:37  sybreon
166
  Added new C++ files
167
 
168 107 sybreon
*/

powered by: WebSVN 2.1.0

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