aboutsummaryrefslogtreecommitdiffstats
path: root/usrp2/opencores/aemb/sw/c/libaemb.h
blob: 329a327a15293edc5ec81e9ddb8da0861a2628d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* $Id: libaemb.h,v 1.3 2007/12/16 03:26:37 sybreon Exp $
**
** AEMB2 CUSTOM LIBRARY
** 
** Copyright (C) 2004-2007 Shawn Tan Ser Ngiap <shawn.tan@aeste.net>
**  
** This file is part of AEMB.
**
** AEMB is free software: you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** AEMB is distributed in the hope that it will be useful, but WITHOUT
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
** or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
** Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with AEMB. If not, see <http:**www.gnu.org/licenses/>.
*/

#ifndef LIBAEMB_H
#define LIBAEMB_H

#define AEMB_TXE 0x0100
#define AEMB_TXP 0x0400
#define AEMB_TX0 0x0300
#define AEMB_TX1 0x0700
#define AEMB_MSK 0x0F00

#define AEMB_BIP 0x0008
#define AEMB_CCC 0x0004
#define AEMB_ITE 0x0002
#define AEMB_BLE 0x0001


// Linker symbols
extern void* _STACK_SIZE;
extern void* _stack_end;


void aemb_hook_init () asm ("_program_init");
void aemb_hook_clean () asm ("_program_clean");

inline void aemb_enable_interrupt ();
inline void aemb_disable_interrupt ();
inline int aemb_isthread1();
inline int aemb_isthread0();
inline int aemb_isthreaded();

/*!
* Assembly macro to enable MSR_IE
*/
void aemb_enable_interrupt ()
{
  int msr, tmp;
  asm volatile ("mfs %0, rmsr;"
		"ori %1, %0, 0x02;"
		"mts rmsr, %1;"
		: "=r"(msr)
		: "r" (tmp)
		);
}

/*!
* Assembly macro to disable MSR_IE
*/
void aemb_disable_interrupt ()
{
  int msr, tmp;
  asm volatile ("mfs %0, rmsr;"
		"andi %1, %0, 0xFD;"
		"mts rmsr, %1;"
		: "=r"(msr)
		: "r" (tmp)
		);
}

/*!
* Bootstrap Hook Override
*/

void aemb_hook_init ()
{
  int msr, tmp;
  int stk_end, stk_siz;

  /*
  // Check to see if hardware threads are enabled
  if (((msr & AEMB_TXE) != AEMB_TXE) ||
  // Check to see if second thread is started
      ((msr & AEMB_TX1) == AEMB_TX1)) return;
  */
  //return;
  asm volatile ("mfs %0, rmsr;"
		// Check for BIP
		"andi %1, %0, %5;"
		"xori %1, %1, %5;"
		"beqi %1, 44;"

		// Check for TXE
		"andi %1, %0, %2;"
		"xori %1, %1, %2;"
		"bnei %1, 36;"	

		// Check for TX1
		"andi %1, %0, %3;"
		"xori %1, %1, %3;"
		"beqi %1, 20;"
		
		// reallocate stack pointer for T0
		"ori %1, r0, %4;"
		"sra %1, %1;"
		"rsubk r1, %1, r1;"

		// reboot the machine
		//"brki r0, _crtinit;"
		"brid 0;"
		"nop;"

		// clear BIP
		"andni %1, %0, %5;"
		"mts rmsr, %1;"
		
		:"=r"(msr), "=r"(tmp)
		:"i"(AEMB_TXE), "i"(AEMB_TX1), "i"(&_STACK_SIZE), "i"(AEMB_BIP)
		);

}

/*!
 Undo the changes made by programme init
*/

void aemb_hook_clean ()
{
  int msr, tmp;
  int stk_end, stk_siz;

  /*
  // Check to see if hardware threads are enabled
  if (((msr & AEMB_TXE) != AEMB_TXE) ||
  // Check to see if second thread is started
      ((msr & AEMB_TX1) == AEMB_TX1)) return;
  */
  return;
  asm volatile ("mfs %0, rmsr;"
		"andi %1, %0, %2;"
		"xori %1, %1, %2;"
		"bnei %1, 28;"		
		"andi %1, %0, %3;"
		"xori %1, %1, %3;"
		"beqi %1, 16;"
		
		// reallocate stack pointer for T0
		"ori %1, r0, %4;"
		"sra %1, %1;"
		"addk r1, %1, r1;"

		:"=r"(msr), "=r"(tmp)
		:"i"(AEMB_TXE), "i"(AEMB_TX1), "i"(&_STACK_SIZE)
		);

  
}

/* Checks if it's T1, and returns 0 if true */

int aemb_isthread1 ()
{
  int msr, tmp, res;
  asm volatile ("mfs %0, rmsr;"
		"andi %1, %0, %3;"
		"xori %1, %1, %3;"	       
		:"=r"(msr), "=r"(tmp)
		:"i"(AEMB_TXE), "i"(AEMB_TXP)
		);
  return tmp;  
}

/* Checks if it's T0, and returns 0 if true */

int aemb_isthread0 ()
{
  int msr, tmp, res;
  asm volatile ("mfs %0, rmsr;"
		"andi %1, %0, %3;"
		"xori %1, %1, %2;"	       
		:"=r"(msr), "=r"(tmp)
		:"i"(AEMB_TXP), "i"(AEMB_MSK)
		);
  return tmp;  
}

/* Checks if TXE is available, and returns 0 if true */

int aemb_isthreaded ()
{
  int msr, tmp, res;
  asm volatile ("mfs %0, rmsr;"
		"andi %1, %0, %2;"
		"xori %1, %1, %2;"	       
		:"=r"(msr), "=r"(tmp)
		:"i"(AEMB_TXE), "i"(AEMB_MSK)
		);
  return tmp;  
}

#endif

/* $Log: libaemb.h,v $
/* Revision 1.3  2007/12/16 03:26:37  sybreon
/* Made T0 loop.
/*
/* Revision 1.1  2007/12/11 00:44:04  sybreon
/* initial import
/* */