StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
em_nios.h
1 #ifndef _EN_NIOS_HH_
2 #define _EN_NIOS_HH_
3 // NIOS device support from linux
4 //
5 
6 
7 
8 #include <stdlib.h>
9 
10 typedef unsigned int UINT32;
11 typedef unsigned short UINT16;
12 typedef unsigned char UINT8;
13 
14 // Prototypes the fiber read/write calls.
15 template<class T> void nios_write(UINT32 address, T val);
16 template<class T> T nios_read(UINT32 address);
17 // each of the following has identical results, however the chunksize of the copy
18 // is sizeof(T). Returns number of bytes copied.
19 template<class T> UINT32 nios_copyto(UINT32 address, UINT32 src, UINT32 bytes);
20 template<class T> UINT32 nios_copyfrom(UINT32 address, UINT32 src, UINT32 bytes);
21 
22 
23 // Test functions for use in linux testing.
24 #ifndef REAL_NIOS_WRITE_FUNCTIONS
25 template<class T> void nios_write(UINT32 address, T val)
26 {
27  T *addr = (T *)address;
28  *addr = val;
29 }
30 
31 
32 template<class T> T nios_read(UINT32 address)
33 {
34  T *addr = (T *)address;
35  T val = *addr;
36  return val;
37 }
38 
39 template<class T> UINT32 nios_copyto(UINT32 dest, UINT32 src, UINT32 sz)
40 {
41  memcpy((UINT32 *)dest, (UINT32 *)src, sz);
42  return sz;
43 }
44 
45 template<class T> UINT32 nios_copyfrom(UINT32 dest, UINT32 src, UINT32 sz)
46 {
47  memcpy((UINT32 *)dest, (UINT32 *)src, sz);
48  return sz;
49 }
50 #endif
51 
52 
53 
54 //#define LOCAL_NIOS
55 
56 // LOCAL_NIOS is designed to be used from the NIOS itself.
57 // In this case, the nios_dev class is only needed to transparently
58 // wrap the nios_read/write calls.
59 
60 // From LINUX, LOCAL_NIOS is not set. Here, I use some memory equal to the size
61 // of the NIOS device being mapped, although this should be transparent to the user
62 //
63 
64 
65 #ifndef LOCAL_NIOS
66 
67 template<class T> class nios_int {
68  public:
69  T ptr;
70 
71  T operator=(T x)
72  {
73  nios_write<T>(ptr, x);
74  return x;
75  }
76 
77  operator T() const
78  {
79  T x = nios_read<T>(ptr);
80  return x;
81  }
82 
83 };
84 
85 template<class T> class nios_device {
86  public:
87  UINT32 base_addr;
88  UINT32 size;
89  nios_int<T> *shadow;
90 
91  nios_device<T>(UINT32 base, UINT32 size) {
92 
93  base_addr = base;
94  shadow = NULL;
95  shadow = (nios_int<T> *)malloc(size*sizeof(nios_int<T>));
96 
97  for(int i=0;i<size;i++) {
98  shadow[i].ptr = base + sizeof(T) * i;
99  }
100  };
101 
102  ~nios_device<T>() {
103  if(shadow) free(shadow);
104  }
105 
106  nios_int<T> &operator[](int i) {
107  return shadow[i];
108  }
109 
110  UINT32 copyto(nios_int<T> *dest, T *src, UINT32 bytes) {
111  return nios_copyto<T>(dest->ptr, (UINT32)src, bytes);
112  }
113 
114  UINT32 copyto(UINT32 dest, T *src, UINT32 bytes) {
115  return nios_copyto<T>(dest+base_addr,(UINT32)src,bytes);
116  }
117 
118  UINT32 copyfrom(T *dest, nios_int<T> *src, UINT32 bytes) {
119  return nios_copyfrom<T>((UINT32)dest, src->ptr, bytes);
120  }
121 
122  UINT32 copyfrom(T * dest, UINT32 src, UINT32 bytes) {
123  return nios_copyfrom<T>(dest, src+base_addr, bytes);
124  }
125 }; // Over fiber
126 
127 
128 #else // LOCAL_NIOS
129 
130 template<class T> class nios_int {
131  public:
132  T val;
133 
134  T operator=(T x)
135  {
136  char *t = (char *)this; // don't want to use the int cast!
137  nios_write<T>((UINT32)t, x);
138  return x;
139  }
140 
141  operator T() const
142  {
143  T x;
144  char *t = (char *)this;
145  x = nios_read<T>((UINT32)t);
146  return x;
147  }
148 };
149 
150 template<class T> class nios_device {
151  public:
152  UINT32 base_addr;
153  nios_int<T> *shadow;
154 
155  nios_device(UINT32 base, UINT32) {
156  base_addr = base;
157  shadow = (nios_int<T> *)base;
158  };
159 
160 
161  nios_int<T> &operator[](int i) {
162  return shadow[i];
163  }
164 
165  UINT32 copyto(nios_int<T> *dest, T *src, UINT32 bytes) {
166  return nios_copyto<T>((UINT32)dest, (UINT32)src, bytes);
167  }
168 
169  UINT32 copyto(UINT32 dest, T *src, UINT32 bytes) {
170  return nios_copyto<T>(dest+base_addr,(UINT32)src,bytes);
171  }
172 
173  UINT32 copyfrom(T *dest, nios_int<T> *src, UINT32 bytes) {
174  return nios_copyfrom<T>((UINT32)dest, (UINT32)src, bytes);
175  }
176 
177  UINT32 copyfrom(T * dest, UINT32 src, UINT32 bytes) {
178  return nios_copyfrom<T>(dest, src+base_addr, bytes);
179  }
180 };
181 
182 #endif // over fiber
183 
184 #endif