dol: initial dol commit
[jump.git] / dol / src / dol / visitor / rtems / lib / rtems_process_wrapper.c
1 #include <string.h>
2 #include "rtems_process_wrapper.h"
3
4 /**
5  *
6  */
7 void DOL_read(void *port, void *buf, int len, DOLProcess *process) {
8     RtemsProcessWrapper* process_wrapper = (RtemsProcessWrapper*)process->wptr;
9     int i;
10
11 #ifdef WORKLOAD_EXTRACT
12     SHOW_DEBUG("log start_read");
13     SHOW_DEBUG_INT((int)get_cycle());
14     SHOW_DEBUG_INT((int)port);
15     SHOW_DEBUG_INT((int)(process_wrapper->start_line));
16     SHOW_DEBUG_INT((int)(process_wrapper->end_line));
17     SHOW_DEBUG_INT((int)len);
18 #endif
19
20 #ifdef MPARM_SCRATCHPAD_QUEUE
21     for (i = 0; i < process_wrapper->number_of_in_ports; i++) {
22         // len not taken into account yet !!!!!!!
23         if (process_wrapper->in_port_id[i] == (int)port) {
24             SCRATCH_QUEUE_CONSUMER* queue_id =
25                 (SCRATCH_QUEUE_CONSUMER*)process_wrapper->in_queue_id[i];
26
27 #if defined (QUEUE_BUFF_IN_PRODUCER)
28             scratch_queue_read(queue_id, (char *)buf, len);
29 #elif defined (QUEUE_BUFF_IN_PRODUCER_DMA)
30             scratch_queue_read_dma(queue_id, (char *)buf, len);
31 #elif defined (QUEUE_BUFF_IN_CONSUMER)
32             scratch_queue_read(queue_id, (char *)buf, len);
33 #elif defined (QUEUE_BUFF_IN_CONSUMER_DMA)
34             scratch_queue_read(queue_id, (char *)buf, len);
35 #elif defined (QUEUE_BUFF_IN_SHARDMEM)
36             scratch_queue_read(queue_id, (char *)buf, len);
37 #elif defined (QUEUE_BUFF_SHAPER)
38             scratch_queue_read_2(queue_id, (char *)buf, len);
39 #endif
40             break;
41         }
42     }
43 #else // pc386
44     for (i = 0; i < process_wrapper->number_of_ports; i++) {
45         if (process_wrapper->port_id[i] == (int)port) {
46             int queue_id = process_wrapper->port_queue_id[i];
47             int j;
48
49             //receive message byte-per-byte
50             for (j = 0; j < len; j++) {
51                 size_t size = 1;
52                 rtems_message_queue_receive(
53                     queue_id,
54                     buf + j,
55                     &size,
56                     RTEMS_DEFAULT_ATTRIBUTES,
57                     RTEMS_NO_TIMEOUT);
58             }
59             break;
60         }
61     }
62 #endif // end MPARM_SCRATCHPAD_QUEUE
63
64     //SHOW_DEBUG("read from app");
65     //SHOW_DEBUG_INT(*(int *)buf);
66
67 #ifdef WORKLOAD_EXTRACT
68     SHOW_DEBUG("log end_read");
69     SHOW_DEBUG_INT((int)get_cycle());
70     SHOW_DEBUG_INT((int)port);
71 #endif
72 }
73
74 /**
75  *
76  */
77 void DOL_write(void *port, void *buf, int len, DOLProcess *process) {
78     RtemsProcessWrapper* process_wrapper = (RtemsProcessWrapper*)process->wptr;
79     int i;
80
81 #ifdef WORKLOAD_EXTRACT
82     SHOW_DEBUG("log start_write");
83     SHOW_DEBUG_INT((int)get_cycle());
84     SHOW_DEBUG_INT((int)port);
85     SHOW_DEBUG_INT((int)(process_wrapper->start_line));
86     SHOW_DEBUG_INT((int)(process_wrapper->end_line));
87     SHOW_DEBUG_INT((int)len);
88 #endif
89
90 #if defined MPARM_SCRATCHPAD_QUEUE
91     for (i = 0; i < process_wrapper->number_of_out_ports; i++) {
92         if (process_wrapper->out_port_id[i] == (int)port) {
93             SCRATCH_QUEUE_PRODUCER* queue_id =
94                 (SCRATCH_QUEUE_PRODUCER*)process_wrapper->out_queue_id[i];
95
96
97 #if defined (QUEUE_BUFF_IN_PRODUCER) || (QUEUE_BUFF_IN_CONSUMER)
98             scratch_queue_write(queue_id, (char *)buf, len);
99 #elif defined (QUEUE_BUFF_IN_PRODUCER_DMA) || (QUEUE_BUFF_IN_CONSUMER_DMA)
100             scratch_queue_write_dma(queue_id, (char *)buf, len);
101 #elif defined (QUEUE_BUFF_IN_SHARDMEM)
102             scratch_queue_write(queue_id, (char *)buf, len);
103 #elif defined(QUEUE_BUFF_SHAPER)
104             scratch_queue_write_2(queue_id, (char *)buf, len); // (2) shaper
105 #endif
106
107             break;
108         }
109     }
110 #else // pc386
111     for (i = 0; i < process_wrapper->number_of_ports; i++) {
112         if (process_wrapper->port_id[i] == (int)port) {
113             int queue_id = process_wrapper->port_queue_id[i];
114             rtems_status_code status;
115             int j;
116
117             //send message byte-per-byte
118             for (j = 0; j < len; j++) {
119                 do {
120                     status = rtems_message_queue_send(queue_id, buf + j, 1);
121                     if (status != RTEMS_SUCCESSFUL) {
122                         rtems_task_wake_after(0);
123                     }
124                 } while (status != RTEMS_SUCCESSFUL);
125             }
126
127             break;
128         }
129     }
130 #endif // end MPARM_SCRATCHPAD_QUEUE
131
132 #ifdef WORKLOAD_EXTRACT
133     SHOW_DEBUG("log end_write");
134     SHOW_DEBUG_INT((int)get_cycle());
135     SHOW_DEBUG_INT((int)port);
136 #endif
137 }
138
139 /**
140  *
141  */
142 void DOL_detach(DOLProcess *process) {
143     //printf("[%s] DOL_detach.\n", (char*)(process->local));
144     ((RtemsProcessWrapper*)process->wptr)->is_detached = 1;
145 }
146
147 /**
148  * Gets an index of a string, where the index must be separated by
149  * a character specified in tokens.
150  * Returns -1, when an error occurs.
151  *
152  * Example:
153  * getIndex("name_1_2", "_", 0) will return 1.
154  * getIndex("name_1_2", "_", 1) will return 2.
155  *
156  * @param string string to parse
157  * @param tokens delimiter of indices
158  * @param indexNumber position of index (starting at 0)
159  */
160 int getIndex(const char* string, char* tokens, int indexNumber) {
161     char* string_copy;
162     char* token_pointer;
163     int index = 0;
164
165     string_copy = (char*) malloc(sizeof(char) * (strlen(string) + 1));
166     if (!string_copy) {
167         fprintf(stderr, "getIndex(): could not allocate memory.\n");
168         return -1;
169     }
170
171     strcpy(string_copy, string);
172
173     token_pointer = strtok(string_copy, tokens);
174     do {
175         token_pointer = strtok(NULL, tokens);
176         index++;
177     } while (index <= indexNumber && token_pointer != 0);
178
179     if (token_pointer) {
180         index = atoi(token_pointer);
181         free(string_copy);
182         return index;
183     }
184
185     free(string_copy);
186     return -1;
187 }
188
189 /**
190  * Create the port name of an iterated port based on its basename and the
191  * given indices.
192  *
193  * @param port buffer where the result is stored (created using
194  *             CREATEPORTVAR)
195  * @param base basename of the port
196  * @param number_of_indices number of dimensions of the port
197  * @param index_range_pairs index and range values for each dimension
198  */
199 int *createPort(int *port, int base, int number_of_indices,
200                 int index_range_pairs, ...) {
201     int index[4];
202     int range[4];
203     int i;
204     int value;
205
206     va_list marker;
207     va_start(marker, index_range_pairs);
208
209     value = index_range_pairs;
210     for (i = 0; i < number_of_indices; i++) {
211         index[i] = value;
212         value = va_arg(marker, int);
213         range[i] = value;
214         if (i < number_of_indices - 1) {
215             value = va_arg(marker, int);
216         }
217     }
218
219     *port = base;
220     for (i = 0; i < number_of_indices; i++) {
221         int j;
222         int weight = 1;
223         for (j = 1; j < number_of_indices - i; j ++) {
224             weight *= range[j];
225         }
226         *port += index[i] * weight;
227     }
228
229     /*
230     switch (number_of_indices) {
231         case 1:
232             *port = base + index[0];
233             break;
234         case 2:
235             *port = base + index[0] * range[1]
236                          + index[1];
237             break;
238         case 3:
239             *port = base + index[0] * range[1] * range[2]
240                          + index[1] * range[0]
241                          + index[2];
242             break;
243         case 4:
244             *port = base + index[0] * range[1] * range[2] * range[3]
245                          + index[1] * range[2] * range[3]
246                          + index[2] * range[3]
247                          + index[3];
248         break;
249     }
250     */
251
252     return port;
253 }