source: soft/giet_vm/sys/common.c @ 159

Last change on this file since 159 was 158, checked in by alain, 12 years ago

Introducing the giet_vm and some example applications

File size: 7.7 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////
2// File     : common.c
3// Date     : 01/04/2012
4// Author   : alain greiner and joel porquet
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7// The common.c and common.h files are part of the GIET nano-kernel.
8// They contains various utilities functions.
9///////////////////////////////////////////////////////////////////////////////////
10
11#include <mapping_info.h>
12#include <common.h>
13#include <drivers.h>
14#include <stdarg.h>
15
16////////////////////////////////////////////////////////////////////////////
17// _puts()
18// used for system code debug / it uses TTY0
19////////////////////////////////////////////////////////////////////////////
20void _puts(char *buffer) 
21{
22    unsigned int* tty_address = (unsigned int*)&seg_tty_base;
23    unsigned int n;
24
25    for ( n=0; n<100; n++)
26    {
27        if (buffer[n] == 0) break;
28        tty_address[0] = (unsigned int)buffer[n];
29    }
30}
31////////////////////////////////////////////////////////////////////////////
32// _putw()
33// used for system code debug / it uses TTY0
34////////////////////////////////////////////////////////////////////////////
35void _putw(unsigned int val)
36{
37    static const char   HexaTab[] = "0123456789ABCDEF";
38    char                buf[11];
39    unsigned int        c;
40
41    buf[0]  = '0';
42    buf[1]  = 'x';
43    buf[10] = 0;
44
45    for ( c = 0 ; c < 8 ; c++ )
46    { 
47        buf[9-c] = HexaTab[val&0xF];
48        val = val >> 4;
49    }
50    _puts(buf);
51}
52////////////////////////////////////////////////////////////////////////////
53// _strncmp()
54// compare two strings s1 & s2 (no more than n characters)
55////////////////////////////////////////////////////////////////////////////
56unsigned int _strncmp(const char* s1, 
57                      const char* s2, 
58                      unsigned int n)
59{
60    unsigned int i;
61    for ( i=0 ; i<n ; i++)
62    {
63        if ( s1[i] != s2[i] ) return 1;
64        if ( s1[i] == 0 )     break;
65    }
66    return 0;
67}
68////////////////////////////////////////////////////////////////////////////
69//      _dcache_buf_invalidate()
70// Invalidate all data cache lines corresponding to a memory
71// buffer (identified by an address and a size).
72////////////////////////////////////////////////////////////////////////////
73void _dcache_buf_invalidate(const void *buffer, 
74                            unsigned int size)
75{
76    unsigned int i;
77    unsigned int tmp;
78    unsigned int line_size;
79
80    /*
81     * compute data cache line size based on config register (bits 12:10)
82     */
83    asm volatile("mfc0 %0, $16, 1" : "=r"(tmp));
84    tmp = ((tmp>>10) & 0x7);
85    line_size = 2 << tmp;
86
87    /* iterate on cache lines to invalidate each one of them */
88    for (i = 0; i < size; i += line_size)
89    {
90        asm volatile(
91                " cache %0, %1"
92                ::"i" (0x11), "R" (*((unsigned char*)buffer+i))
93                );
94    }
95}
96///////////////////////////////////////////////////////////////////////////////////
97//      _itoa_dec()
98// Convert a 32-bit unsigned integer to a string of ten decimal characters.
99///////////////////////////////////////////////////////////////////////////////////
100void _itoa_dec(unsigned int val, char *buf)
101{
102    const static char dectab[] = "0123456789";
103    unsigned int i;
104
105    for (i = 0; i < 10; i++)
106    {
107        if ((val != 0) || (i == 0))
108            buf[9-i] = dectab[val % 10];
109        else
110            buf[9-i] = 0x20;
111        val /= 10;
112    }
113}
114///////////////////////////////////////////////////////////////////////////////////
115//      _itoa_hex()
116// Convert a 32-bit unsigned integer to a string of eight hexadecimal characters.
117///////////////////////////////////////////////////////////////////////////////////
118void _itoa_hex(unsigned int val, char *buf)
119{
120    const static char hexatab[] = "0123456789ABCD";
121    unsigned int i;
122
123    for (i = 0; i < 8; i++)
124    {
125        buf[7-i] = hexatab[val % 16];
126        val /= 16;
127    }
128}
129///////////////////////////////////////////////////////////////////////////////////
130//      _get_epc()
131// Access CP0 and returns EPC register.
132///////////////////////////////////////////////////////////////////////////////////
133inline unsigned int _get_epc()
134{
135    unsigned int ret;
136    asm volatile("mfc0 %0, $14" : "=r"(ret));
137    return ret;
138}
139///////////////////////////////////////////////////////////////////////////////////
140//      _get_bar()
141// Access CP0 and returns BAR register.
142///////////////////////////////////////////////////////////////////////////////////
143inline unsigned int _get_bar()
144{
145    unsigned int ret;
146    asm volatile("mfc0 %0, $8" : "=r"(ret));
147    return ret;
148}
149///////////////////////////////////////////////////////////////////////////////////
150//      _get_cr()
151// Access CP0 and returns CR register.
152///////////////////////////////////////////////////////////////////////////////////
153inline unsigned int _get_cause()
154{
155    unsigned int ret;
156    asm volatile("mfc0 %0, $13" : "=r"(ret));
157    return ret;
158}
159///////////////////////////////////////////////////////////////////////////////////
160//      _it_mask()
161// Access CP0 and mask IRQs
162///////////////////////////////////////////////////////////////////////////////////
163inline void _it_mask()
164{
165    asm volatile(
166            "mfc0  $2, $12     \n"
167            "ori   $2, $2, 1   \n"
168            "mtc0  $2, $12     \n"
169            ::: "$2"
170            );
171}
172///////////////////////////////////////////////////////////////////////////////////
173//      _it_enable()
174// Access CP0 and enable IRQs
175///////////////////////////////////////////////////////////////////////////////////
176inline void _it_enable()
177{
178    asm volatile(
179            "mfc0  $2, $12     \n"
180            "addi  $2, $2, -1  \n"
181            "mtc0  $2, $12     \n"
182            ::: "$2"
183            );
184}
185/////////////////////////////////////////////////////////////////////////////
186//      access functions to mapping_info data structure
187/////////////////////////////////////////////////////////////////////////////
188mapping_cluster_t* _get_cluster_base( mapping_header_t* header )
189{
190    return   (mapping_cluster_t*) ((char*)header +
191                                  MAPPING_HEADER_SIZE);
192}
193/////////////////////////////////////////////////////////////////////////////
194mapping_pseg_t* _get_pseg_base( mapping_header_t* header )
195{
196    return   (mapping_pseg_t*)    ((char*)header +
197                                  MAPPING_HEADER_SIZE +
198                                  MAPPING_CLUSTER_SIZE*header->clusters);
199}
200/////////////////////////////////////////////////////////////////////////////
201mapping_vspace_t* _get_vspace_base( mapping_header_t* header )
202{
203    return   (mapping_vspace_t*)  ((char*)header +
204                                  MAPPING_HEADER_SIZE +
205                                  MAPPING_CLUSTER_SIZE*header->clusters +
206                                  MAPPING_PSEG_SIZE*header->psegs);
207}
208/////////////////////////////////////////////////////////////////////////////
209mapping_vseg_t* _get_vseg_base( mapping_header_t* header )
210{
211    return   (mapping_vseg_t*)    ((char*)header +
212                                  MAPPING_HEADER_SIZE +
213                                  MAPPING_CLUSTER_SIZE*header->clusters +
214                                  MAPPING_PSEG_SIZE*header->psegs +
215                                  MAPPING_VSPACE_SIZE*header->vspaces);
216}
217/////////////////////////////////////////////////////////////////////////////
218mapping_task_t* _get_task_base( mapping_header_t* header )
219{
220    return   (mapping_task_t*)    ((char*)header +
221                                  MAPPING_HEADER_SIZE +
222                                  MAPPING_CLUSTER_SIZE*header->clusters +
223                                  MAPPING_PSEG_SIZE*header->psegs +
224                                  MAPPING_VSPACE_SIZE*header->vspaces +
225                                  MAPPING_VSEG_SIZE*header->vsegs);
226}
227
Note: See TracBrowser for help on using the repository browser.