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

Subversion Repositories aemb

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

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

Line No. Rev Author Line
1 141 sybreon
/* $Id: hook.hh,v 1.8 2008-04-27 16:04:42 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
** under the terms of the GNU Lesser General Public License as
10
** published by the Free Software Foundation, either version 3 of the
11
** License, or (at your option) any later version.
12
**
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
** or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
16
** Public License for more details.
17
**
18
** You should have received a copy of the GNU Lesser General Public
19
** License along with AEMB. If not, see .
20
*/
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
#ifndef AEMB_HOOK_HH
35
#define AEMB_HOOK_HH
36
 
37 139 sybreon
#ifdef __cplusplus
38 107 sybreon
namespace aemb {
39 121 sybreon
  extern "C" {
40 139 sybreon
#endif
41
 
42 121 sybreon
    void _program_init();
43
    void _program_clean();
44 135 sybreon
 
45
    // newlib locks
46
    void __malloc_lock(struct _reent *reent);
47
    void __malloc_unlock(struct _reent *reent);
48
    //void __env_lock(struct _reent *reent);
49
    //void __env_unlock(struct _reent *reent);
50
 
51 139 sybreon
#ifdef __cplusplus
52 135 sybreon
  }
53 139 sybreon
#endif
54 135 sybreon
 
55 107 sybreon
  /**
56 116 sybreon
     Finalisation hook
57
 
58
     This function executes during the shutdown phase after the
59
     finalisation routine is called. It will merge the changes made
60
     during initialisation.
61 121 sybreon
  */
62 135 sybreon
 
63 111 sybreon
  void _program_clean()
64 107 sybreon
  {
65 121 sybreon
    waitMutex(); // enter critical section
66
 
67 116 sybreon
    // unify the stack backwards for thread 1
68 135 sybreon
    if (isThread0())
69 121 sybreon
      {
70 135 sybreon
        // TODO: Unify the stack
71 121 sybreon
        setStack(getStack() + (getStackSize() >> 1));
72 128 sybreon
      }
73 121 sybreon
 
74
    signalMutex(); // exit critical section
75 107 sybreon
  }
76
 
77
  /**
78 116 sybreon
     Initialisation hook
79 107 sybreon
 
80 116 sybreon
     This function executes during the startup phase before the
81
     initialisation routine is called. It splits the stack between the
82 135 sybreon
     threads. For now, it will lock up T0 for compatibility purposes.
83 121 sybreon
  */
84 135 sybreon
 
85 111 sybreon
  void _program_init()
86 107 sybreon
  {
87 121 sybreon
    waitMutex(); // enter critical section
88
 
89 116 sybreon
    // split and shift the stack for thread 1
90 135 sybreon
    if (isThread0()) // main thread
91 116 sybreon
      {
92 135 sybreon
        // NOTE: Dupe the stack otherwise it will FAIL!
93 141 sybreon
        int oldstk = getStack();
94
        int newstk = setStack(getStack() - (getStackSize() >> 1));
95
        dupStack((unsigned int *)newstk,
96
                 (unsigned int *)oldstk,
97
                 (unsigned int *)getStackTop());
98 135 sybreon
        signalMutex(); // exit critical section
99 139 sybreon
        while (1) asm volatile ("nop"); // lock thread
100 116 sybreon
      }
101 121 sybreon
 
102
    signalMutex(); // exit critical section
103 107 sybreon
  }
104
 
105 128 sybreon
  semaphore __malloc_mutex = 1;
106
 
107 116 sybreon
  /**
108
     Heap Lock
109
 
110
     This function is called during malloc() to lock out the shared
111
     heap to avoid data corruption.
112
   */
113 135 sybreon
 
114
  void __malloc_lock(struct _reent *reent)
115 116 sybreon
  {
116 128 sybreon
    waitMutex();
117 116 sybreon
  }
118
 
119
  /**
120
     Heap Unlock
121
 
122
     This function is called during malloc() to indicate that the
123
     shared heap is now available for another thread.
124
  */
125 135 sybreon
 
126
  void __malloc_unlock(struct _reent *reent)
127 116 sybreon
  {
128 121 sybreon
    signalMutex();
129 116 sybreon
  }
130 121 sybreon
 
131 139 sybreon
#ifdef __cplusplus
132 116 sybreon
}
133 139 sybreon
#endif
134 116 sybreon
 
135 107 sybreon
#endif
136
 
137 116 sybreon
#ifndef __OPTIMIZE__
138
// The main programme needs to be compiled with optimisations turned
139 135 sybreon
// on (at least -O1). If not, the MUTEX value will be written to the
140
// same RAM location, giving both threads the same value.
141 141 sybreon
OPTIMISATION_REQUIRED OPTIMISATION_REQUIRED
142 116 sybreon
#endif
143
 
144 107 sybreon
/*
145
  $Log: not supported by cvs2svn $
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.