7ced4a5d2d1206dfd030f09a99e56fbe75520be5
[jump.git] / dol / src / dol / visitor / rtems / lib / appsupport.c
1 ///////////////////////////////////////////////////////////////////////////////\r
2 // Copyright 2003 DEIS - Universita' di Bologna\r
3 //\r
4 // name         appsupport.c\r
5 // author       DEIS - Universita' di Bologna\r
6 //              Davide Bertozzi - dbertozzi@deis.unibo.it\r
7 //              Mirko Loghi - mloghi@deis.unibo.it\r
8 //              Federico Angiolini - fangiolini@deis.unibo.it\r
9 //              Francesco Poletti - fpoletti@deis.unibo.it\r
10 // portions by  Massimo Scardamaglia - mascard@vizzavi.it\r
11 // info         Provides support for testbench compilation\r
12 //\r
13 ///////////////////////////////////////////////////////////////////////////////\r
14 #include <bsp.h>\r
15 \r
16 #include "appsupport.h"\r
17 \r
18 volatile char *time_low_ptr   = (char *)(SIMSUPPORT_BASE + GET_TIME_ADDRESS_LO);\r
19 volatile char *time_high_ptr  = (char *)(SIMSUPPORT_BASE + GET_TIME_ADDRESS_HI);\r
20 volatile char *time_stop_ptr  = (char *)(SIMSUPPORT_BASE + STOP_TIME_ADDRESS);\r
21 volatile char *time_rel_ptr   = (char *)(SIMSUPPORT_BASE + RELEASE_TIME_ADDRESS);\r
22 \r
23 volatile char *cycle_low_ptr  = (char *)(SIMSUPPORT_BASE + GET_CYCLE_ADDRESS_LO);\r
24 volatile char *cycle_high_ptr = (char *)(SIMSUPPORT_BASE + GET_CYCLE_ADDRESS_HI);\r
25 volatile char *cycle_stop_ptr = (char *)(SIMSUPPORT_BASE + STOP_CYCLE_ADDRESS);\r
26 volatile char *cycle_rel_ptr  = (char *)(SIMSUPPORT_BASE + RELEASE_CYCLE_ADDRESS);\r
27 \r
28 \r
29 ///////////////////////////////////////////////////////////////////////////////\r
30 // pr - Allows printing debug info even without support from an OS. See\r
31 //      user_swi.cpp for printable messages, or to create your own\r
32 \r
33 void pr(int proc, int msg_num, int num_arg)\r
34 {\r
35 #ifndef __OPTIMIZE__\r
36     __asm ("ldr r1, %0" : : "g" (proc) : "r1" );\r
37     __asm ("ldr r2, %0" : : "g" (msg_num) : "r2" );\r
38     __asm ("ldr r3, %0" : : "g" (num_arg) : "r3" );\r
39     __asm ("swi " SWI_PRINTstr);\r
40 #else\r
41     __asm ("mov r1, %0" : : "g" (proc) : "r1" );\r
42     __asm ("mov r2, %0" : : "g" (msg_num) : "r2" );\r
43     __asm ("mov r3, %0" : : "g" (num_arg) : "r3" );\r
44     __asm ("swi " SWI_PRINTstr);\r
45 #endif\r
46 }\r
47 \r
48 ///////////////////////////////////////////////////////////////////////////////\r
49 // get_proc_id - Allows getting the processor's ID (from 1 onwards)\r
50 unsigned int get_proc_id()\r
51 {\r
52   char *ptr = (char *)(SIMSUPPORT_BASE + GET_CPU_ID_ADDRESS);\r
53   return (*(unsigned long int *)ptr);\r
54 }\r
55 \r
56 ///////////////////////////////////////////////////////////////////////////////\r
57 // get_proc_num - Allows getting the number of processors in the platform\r
58 unsigned int get_proc_num()\r
59 {\r
60   char *ptr = (char *)(SIMSUPPORT_BASE + GET_CPU_CNT_ADDRESS);\r
61   return (*(unsigned long int *)ptr);\r
62 }\r
63 \r
64 // ---------------------------\r
65 // Frequency scaling functions\r
66 // ---------------------------\r
67 volatile unsigned int *freqdevice      = (unsigned int *)FREQ_BASE;\r
68 \r
69 ///////////////////////////////////////////////////////////////////////////////\r
70 // scale_this_core_frequency - Scales the frequency of the core on which\r
71 //                             the application is running\r
72 void scale_this_core_frequency(unsigned short int divider)\r
73 {\r
74   freqdevice[get_proc_id() - 1] = divider;\r
75 }\r
76 ///////////////////////////////////////////////////////////////////////////////\r
77 // scale_device_frequency - Scales the frequency of any device in the system\r
78 void scale_device_frequency(unsigned short int divider, int ID)\r
79 {\r
80   freqdevice[ID] = divider;\r
81 }\r
82 ///////////////////////////////////////////////////////////////////////////////\r
83 // get_this_core_frequency - Gets the frequency divider of the core on which\r
84 //                           the application is running\r
85 unsigned short int get_this_core_frequency()\r
86 {\r
87   return (freqdevice[get_proc_id() - 1]);\r
88 }\r
89 ///////////////////////////////////////////////////////////////////////////////\r
90 // get_device_frequency - Gets the frequency divider of any device in the system\r
91 unsigned short int get_device_frequency(int ID)\r
92 {\r
93   return (freqdevice[ID]);\r
94 }\r
95 \r
96 ///////////////////////////////////////////////////////////////////////////////\r
97 // get_time - Allows getting the current simulation time\r
98 unsigned long long int get_time()\r
99 {\r
100   unsigned long long int time;\r
101   \r
102   *time_stop_ptr = 1;\r
103   time = (((unsigned long long int)(*(unsigned long int *)time_high_ptr)) << 32) + *(unsigned long int *)time_low_ptr;\r
104   *time_rel_ptr = 1;\r
105   \r
106   return (time);\r
107 }\r
108 \r
109 ///////////////////////////////////////////////////////////////////////////////\r
110 // get_cycle - Allows getting the current simulation cycle\r
111 unsigned long long int get_cycle1()\r
112 {\r
113   unsigned long long int cycle;\r
114   \r
115   *cycle_stop_ptr = 1;\r
116   cycle = (((unsigned long long int)(*(unsigned long int *)cycle_high_ptr)) << 32) + *(unsigned long int *)cycle_low_ptr;\r
117   *cycle_rel_ptr = 1;\r
118   \r
119   return (cycle);\r
120 }\r
121 \r
122 extern rtems_id timer_sem_id;\r
123 unsigned long long int get_cycle()\r
124 {\r
125   unsigned long long int cycle;\r
126   rtems_status_code rtems_status;\r
127 \r
128   rtems_status = rtems_semaphore_obtain(timer_sem_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT);\r
129   *cycle_stop_ptr = 1;\r
130   cycle = (((unsigned long long int)(*(unsigned long int *)cycle_high_ptr))\r
131            << 32) + *(unsigned long int *)cycle_low_ptr;\r
132   *cycle_rel_ptr = 1;\r
133   rtems_status =  rtems_semaphore_release(timer_sem_id);\r
134 \r
135   return (cycle);\r
136 }\r