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

Subversion Repositories aemb

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

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

Line No. Rev Author Line
1 153 sybreon
/* $Id: hook.hh,v 1.11 2008-04-28 20:31:40 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 116 sybreon
  /**
100
     Heap Lock
101
 
102
     This function is called during malloc() to lock out the shared
103
     heap to avoid data corruption.
104
   */
105 135 sybreon
 
106
  void __malloc_lock(struct _reent *reent)
107 116 sybreon
  {
108 151 sybreon
    _aembLockMTX();
109 116 sybreon
  }
110
 
111
  /**
112
     Heap Unlock
113
 
114
     This function is called during malloc() to indicate that the
115
     shared heap is now available for another thread.
116
  */
117 135 sybreon
 
118
  void __malloc_unlock(struct _reent *reent)
119 116 sybreon
  {
120 151 sybreon
    _aembFreeMTX();
121 116 sybreon
  }
122 121 sybreon
 
123 139 sybreon
#ifdef __cplusplus
124 116 sybreon
}
125 139 sybreon
#endif
126 116 sybreon
 
127 107 sybreon
#endif
128
 
129 116 sybreon
#ifndef __OPTIMIZE__
130
// The main programme needs to be compiled with optimisations turned
131 135 sybreon
// on (at least -O1). If not, the MUTEX value will be written to the
132
// same RAM location, giving both threads the same value.
133 141 sybreon
OPTIMISATION_REQUIRED OPTIMISATION_REQUIRED
134 116 sybreon
#endif
135
 
136 107 sybreon
/*
137
  $Log: not supported by cvs2svn $
138 153 sybreon
  Revision 1.10  2008/04/28 20:29:15  sybreon
139
  Made files C compatible under C++.
140
 
141 151 sybreon
  Revision 1.9  2008/04/27 16:33:42  sybreon
142
  License change to GPL3.
143
 
144 145 sybreon
  Revision 1.8  2008/04/27 16:04:42  sybreon
145
  Minor cosmetic changes.
146
 
147 141 sybreon
  Revision 1.7  2008/04/26 19:31:35  sybreon
148
  Made headers C compatible.
149
 
150 139 sybreon
  Revision 1.6  2008/04/26 18:04:31  sybreon
151
  Updated software to freeze T0 and run T1.
152
 
153 135 sybreon
  Revision 1.5  2008/04/23 14:19:39  sybreon
154
  Fixed minor bugs.
155
  Initial use of hardware mutex.
156
 
157 128 sybreon
  Revision 1.4  2008/04/20 16:35:53  sybreon
158
  Added C/C++ compatible #ifdef statements
159
 
160 121 sybreon
  Revision 1.3  2008/04/12 13:46:02  sybreon
161
  Added malloc() lock and unlock routines
162
 
163 116 sybreon
  Revision 1.2  2008/04/11 15:20:31  sybreon
164
  added static assert hack
165
 
166 111 sybreon
  Revision 1.1  2008/04/09 19:48:37  sybreon
167
  Added new C++ files
168
 
169 107 sybreon
*/

powered by: WebSVN 2.1.0

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