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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
diff --git a/apps/plugin.c b/apps/plugin.c
index 767c593..e0455c5 100644
--- a/apps/plugin.c
+++ b/apps/plugin.c
@@ -109,7 +109,9 @@ static const struct plugin_api rockbox_api = {
     lcd_clear_display,
     lcd_getstringsize,
     lcd_putsxy,
+    lcd_putsxyf,
     lcd_puts,
+    lcd_putsf,
     lcd_puts_scroll,
     lcd_stop_scroll,
 #ifdef HAVE_LCD_CHARCELLS
@@ -382,8 +384,10 @@ static const struct plugin_api rockbox_api = {
     trigger_cpu_boost,
     cancel_cpu_boost,
 #endif
-#if NUM_CORES > 1
+#ifdef HAVE_CPUCACHE_FLUSH
     cpucache_flush,
+#endif
+#ifdef HAVE_CPUCACHE_INVALIDATE
     cpucache_invalidate,
 #endif
     timer_register,
@@ -620,9 +624,6 @@ static const struct plugin_api rockbox_api = {
     plugin_get_audio_buffer,
     plugin_tsr,
     plugin_get_current_filename,
-#ifdef PLUGIN_USE_IRAM
-    plugin_iram_init,
-#endif
 #if defined(DEBUG) || defined(SIMULATOR)
     debugf,
 #endif
@@ -721,9 +722,6 @@ static const struct plugin_api rockbox_api = {

     /* new stuff at the end, sort into place next time
        the API gets incompatible */
-
-    lcd_putsf,
-    lcd_putsxyf,
 };

 int plugin_load(const char* plugin, const void* parameter)
@@ -919,23 +917,6 @@ void* plugin_get_audio_buffer(size_t *buffer_size)
 #endif
 }

-#ifdef PLUGIN_USE_IRAM
-/* Initializes plugin IRAM */
-void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
-                      char *iedata, size_t iedata_size)
-{
-    /* We need to stop audio playback in order to use codec IRAM */
-    audio_hard_stop();
-    memcpy(iramstart, iramcopy, iram_size);
-    memset(iedata, 0, iedata_size);
-    memset(iramcopy, 0, iram_size);
-#if NUM_CORES > 1
-    /* writeback cleared iedata and iramcopy areas */
-    cpucache_flush();
-#endif
-}
-#endif /* PLUGIN_USE_IRAM */
-
 /* The plugin wants to stay resident after leaving its main function, e.g.
    runs from timer or own thread. The callback is registered to later
    instruct it to free its resources before a new plugin gets loaded. */
diff --git a/apps/plugin.h b/apps/plugin.h
index 2f187f1..422f58f 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -147,12 +147,12 @@ void* plugin_get_buffer(size_t *buffer_size);
 #define PLUGIN_MAGIC 0x526F634B /* RocK */

 /* increase this every time the api struct changes */
-#define PLUGIN_API_VERSION 191
+#define PLUGIN_API_VERSION 192

 /* update this to latest version if a change to the api struct breaks
    backwards compatibility (and please take the opportunity to sort in any
    new function which are "waiting" at the end of the function table) */
-#define PLUGIN_MIN_API_VERSION 191
+#define PLUGIN_MIN_API_VERSION 192

 /* plugin return codes */
 /* internal returns start at 0x100 to make exit(1..255) work */
@@ -183,7 +183,9 @@ struct plugin_api {
     void (*lcd_clear_display)(void);
     int  (*lcd_getstringsize)(const unsigned char *str, int *w, int *h);
     void (*lcd_putsxy)(int x, int y, const unsigned char *string);
+    void (*lcd_putsxyf)(int x, int y, const unsigned char *fmt, ...);
     void (*lcd_puts)(int x, int y, const unsigned char *string);
+    void (*lcd_putsf)(int x, int y, const unsigned char *fmt, ...);
     void (*lcd_puts_scroll)(int x, int y, const unsigned char* string);
     void (*lcd_stop_scroll)(void);
 #ifdef HAVE_LCD_CHARCELLS
@@ -496,8 +498,10 @@ struct plugin_api {
     void (*trigger_cpu_boost)(void);
     void (*cancel_cpu_boost)(void);
 #endif
-#if NUM_CORES > 1
+#ifdef HAVE_CPUCACHE_FLUSH
     void (*cpucache_flush)(void);
+#endif
+#ifdef HAVE_CPUCACHE_INVALIDATE
     void (*cpucache_invalidate)(void);
 #endif
     bool (*timer_register)(int reg_prio, void (*unregister_callback)(void),
@@ -769,10 +773,6 @@ struct plugin_api {
     void* (*plugin_get_audio_buffer)(size_t *buffer_size);
     void (*plugin_tsr)(bool (*exit_callback)(bool reenter));
     char* (*plugin_get_current_filename)(void);
-#ifdef PLUGIN_USE_IRAM
-    void (*plugin_iram_init)(char *iramstart, char *iramcopy, size_t iram_size,
-                             char *iedata, size_t iedata_size);
-#endif
 #if defined(DEBUG) || defined(SIMULATOR)
     void (*debugf)(const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2);
 #endif
@@ -894,9 +894,6 @@ struct plugin_api {

     /* new stuff at the end, sort into place next time
        the API gets incompatible */
-
-    void (*lcd_putsf)(int x, int y, const unsigned char *fmt, ...);
-    void (*lcd_putsxyf)(int x, int y, const unsigned char *fmt, ...);
 };

 /* plugin header */
@@ -928,31 +925,10 @@ extern unsigned char plugin_end_addr[];
         PLUGIN_MAGIC, TARGET_ID, PLUGIN_API_VERSION, \
         NULL, NULL, plugin__start, &rb };
 #endif /* CONFIG_PLATFORM */
-
-#ifdef PLUGIN_USE_IRAM
-/* Declare IRAM variables */
-#define PLUGIN_IRAM_DECLARE \
-    extern char iramcopy[]; \
-    extern char iramstart[]; \
-    extern char iramend[]; \
-    extern char iedata[]; \
-    extern char iend[];
-/* Initialize IRAM */
-#define PLUGIN_IRAM_INIT(api) \
-    (api)->plugin_iram_init(iramstart, iramcopy, iramend-iramstart, \
-                            iedata, iend-iedata);
-#else
-#define PLUGIN_IRAM_DECLARE
-#define PLUGIN_IRAM_INIT(api)
-#endif /* PLUGIN_USE_IRAM */
 #endif /* PLUGIN */

 int plugin_load(const char* plugin, const void* parameter);
 void* plugin_get_audio_buffer(size_t *buffer_size);
-#ifdef PLUGIN_USE_IRAM
-void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
-                      char *iedata, size_t iedata_size);
-#endif

 /* plugin_tsr,
     callback returns true to allow the new plugin to load,
diff --git a/apps/plugins/beatbox/beatbox.c b/apps/plugins/beatbox/beatbox.c
index 9cb7dd4..34c7257 100644
--- a/apps/plugins/beatbox/beatbox.c
+++ b/apps/plugins/beatbox/beatbox.c
@@ -27,8 +27,6 @@
 #include "midi/midifile.h"


-PLUGIN_IRAM_DECLARE
-
 /* variable button definitions */
 #if CONFIG_KEYPAD == RECORDER_PAD
 #define BTN_QUIT     BUTTON_OFF
@@ -249,8 +247,6 @@ enum plugin_status plugin_start(const void* parameter)
 {
     int retval = 0;

-    PLUGIN_IRAM_INIT(rb)
-
     rb->lcd_setfont(0);

 #if defined(HAVE_ADJUSTABLE_CPU_FREQ)
diff --git a/apps/plugins/doom/rockdoom.c b/apps/plugins/doom/rockdoom.c
index 324cb24..4267a61 100644
--- a/apps/plugins/doom/rockdoom.c
+++ b/apps/plugins/doom/rockdoom.c
@@ -41,9 +41,6 @@
 #include "st_stuff.h"
 #include "lib/helper.h"

-
-PLUGIN_IRAM_DECLARE
-
 extern boolean timingdemo, singledemo, demoplayback, fastdemo; // killough

 int filearray[9];
@@ -663,8 +660,6 @@ enum plugin_status plugin_start(const void* parameter)
    /* Disable all talking before initializing IRAM */
    rb->talk_disable(true);

-   PLUGIN_IRAM_INIT(rb)
-
    (void)parameter;

    doomexit=0;
diff --git a/apps/plugins/midi/midiplay.c b/apps/plugins/midi/midiplay.c
index 442a747..5a3c461 100644
--- a/apps/plugins/midi/midiplay.c
+++ b/apps/plugins/midi/midiplay.c
@@ -26,8 +26,6 @@
 #include "midifile.h"


-PLUGIN_IRAM_DECLARE
-
 /* variable button definitions */
 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
 #define BTN_QUIT         BUTTON_OFF
@@ -451,8 +449,6 @@ enum plugin_status plugin_start(const void* parameter)
 {
     int retval;

-    PLUGIN_IRAM_INIT(rb)
-
     if (parameter == NULL)
     {
         rb->splash(HZ*2, " Play .MID file ");
diff --git a/apps/plugins/mp3_encoder.c b/apps/plugins/mp3_encoder.c
index d265008..392fc36 100644
--- a/apps/plugins/mp3_encoder.c
+++ b/apps/plugins/mp3_encoder.c
@@ -15,8 +15,6 @@
 #include "plugin.h"


-PLUGIN_IRAM_DECLARE
-
 #define SAMP_PER_FRAME       1152
 #define SAMPL2                576
 #define SBLIMIT                32
@@ -2504,8 +2502,6 @@ enum plugin_status plugin_start(const void* parameter)
     if (parameter == NULL)
         return PLUGIN_ERROR;

-    PLUGIN_IRAM_INIT(rb)
-
     enc_buffer = rb->plugin_get_audio_buffer(&enc_buffer_size);

 #ifdef CPU_COLDFIRE
diff --git a/apps/plugins/mpegplayer/mpegplayer.c b/apps/plugins/mpegplayer/mpegplayer.c
index 8142c6f..5b333c3 100644
--- a/apps/plugins/mpegplayer/mpegplayer.c
+++ b/apps/plugins/mpegplayer/mpegplayer.c
@@ -109,8 +109,6 @@
 #include "stream_mgr.h"


-PLUGIN_IRAM_DECLARE
-
 /* button definitions */
 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
 #define MPEG_MENU       BUTTON_MODE
@@ -1761,9 +1759,6 @@ enum plugin_status plugin_start(const void* parameter)
     /* Disable all talking before initializing IRAM */
     rb->talk_disable(true);

-    /* Initialize IRAM - stops audio and voice as well */
-    PLUGIN_IRAM_INIT(rb)
-
 #ifdef HAVE_LCD_COLOR
     rb->lcd_set_backdrop(NULL);
     rb->lcd_set_foreground(LCD_WHITE);
diff --git a/apps/plugins/pacbox/pacbox.c b/apps/plugins/pacbox/pacbox.c
index e942841..5165ff3 100644
--- a/apps/plugins/pacbox/pacbox.c
+++ b/apps/plugins/pacbox/pacbox.c
@@ -32,9 +32,6 @@
 #include "lib/configfile.h"
 #include "lib/playback_control.h"

-
-PLUGIN_IRAM_DECLARE
-
 struct pacman_settings {
     int difficulty;
     int numlives;
@@ -493,8 +490,6 @@ enum plugin_status plugin_start(const void* parameter)
 {
     (void)parameter;

-    PLUGIN_IRAM_INIT(rb)
-
 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
     rb->cpu_boost(true);
 #endif
diff --git a/apps/plugins/pdbox/pdbox.c b/apps/plugins/pdbox/pdbox.c
index 7f4a7ac..3ba6f4e 100644
--- a/apps/plugins/pdbox/pdbox.c
+++ b/apps/plugins/pdbox/pdbox.c
@@ -29,8 +29,6 @@

 /* Welcome to the PDBox plugin */

-PLUGIN_IRAM_DECLARE
-
 /* Name of the file to open. */
 char* filename;

@@ -159,8 +157,6 @@ void core_thread(void)
 /* Plug-in entry point */
 enum plugin_status plugin_start(const void* parameter)
 {
-    PLUGIN_IRAM_INIT(rb)
-
     /* Memory pool variables. */
     size_t mem_size;
     void* mem_pool;
diff --git a/apps/plugins/pitch_detector.c b/apps/plugins/pitch_detector.c
index 22e8f75..2b80f5c 100644
--- a/apps/plugins/pitch_detector.c
+++ b/apps/plugins/pitch_detector.c
@@ -69,8 +69,6 @@
 #include "pluginbitmaps/pitch_notes.h"


-PLUGIN_IRAM_DECLARE
-
 /* Some fixed point calculation stuff */
 typedef int32_t fixed_data;
 struct _fixed
@@ -1056,12 +1054,10 @@ static void init_everything(void)
     /* Disable all talking before initializing IRAM */
     rb->talk_disable(true);

-    PLUGIN_IRAM_INIT(rb);
-
     load_settings();
     rb->storage_sleep();

-    /* Stop all playback (if no IRAM, otherwise IRAM_INIT would have) */
+    /* Stop all playback */
     rb->plugin_get_audio_buffer(NULL);

     /* --------- Init the audio recording ----------------- */
diff --git a/apps/plugins/plugin_crt0.c b/apps/plugins/plugin_crt0.c
index e34124c..cd80884 100644
--- a/apps/plugins/plugin_crt0.c
+++ b/apps/plugins/plugin_crt0.c
@@ -63,10 +63,35 @@ enum plugin_status plugin__start(const void *param)
     int exit_ret;
     enum plugin_status ret;

-    /* zero out the bss section */
 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
+
+/* IRAM must be copied before clearing the BSS ! */
+#ifdef PLUGIN_USE_IRAM
+    extern char iramcopy[], iramstart[], iramend[], iedata[], iend[];
+    size_t iram_size = iramend - iramstart;
+    size_t ibss_size = iend - iedata;
+    if (iram_size > 0 || ibss_size > 0)
+    {
+        /* We need to stop audio playback in order to use codec IRAM */
+        rb->audio_stop();
+        rb->memcpy(iramstart, iramcopy, iram_size);
+        rb->memset(iedata, 0, ibss_size);
+#ifdef HAVE_CPUCACHE_FLUSH
+        /* writeback cleared iedata and iramcopy areas */
+        rb->cpucache_flush();
+#endif /* HAVE_CPUCACHE_FLUSH */
+    }
+
+    /* barrier to prevent reordering iram copy and BSS clearing,
+     * because the BSS segment alias the IRAM copy.
+     */
+    asm volatile ("" ::: "memory");
+#endif /* PLUGIN_USE_IRAM */
+
+    /* zero out the bss section */
     rb->memset(plugin_bss_start, 0, plugin_end_addr - plugin_bss_start);
 #endif
+
     /* we come back here if exit() was called or the plugin returned normally */
     exit_ret = setjmp(__exit_env);
     if (exit_ret == 0)
diff --git a/apps/plugins/rockboy/rockboy.c b/apps/plugins/rockboy/rockboy.c
index 7fc9199..e7d56b5 100644
--- a/apps/plugins/rockboy/rockboy.c
+++ b/apps/plugins/rockboy/rockboy.c
@@ -26,9 +26,6 @@
 #include "hw.h"
 #include "pcm.h"

-
-PLUGIN_IRAM_DECLARE
-
 int shut,cleanshut;
 char *errormsg;

@@ -380,8 +377,6 @@ static int gnuboy_main(const char *rom)
 /* this is the plugin entry point */
 enum plugin_status plugin_start(const void* parameter)
 {
-    PLUGIN_IRAM_INIT(rb)
-
     rb->lcd_setfont(0);

     rb->lcd_clear_display();
diff --git a/apps/plugins/test_fps.c b/apps/plugins/test_fps.c
index f57f45b..c03adc4 100644
--- a/apps/plugins/test_fps.c
+++ b/apps/plugins/test_fps.c
@@ -22,8 +22,6 @@
 #include "lib/helper.h"
 #include "lib/grey.h"

-PLUGIN_IRAM_DECLARE
-
 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
     (CONFIG_KEYPAD == IPOD_1G2G_PAD)
 #define FPS_QUIT BUTTON_MENU
@@ -344,7 +342,6 @@ enum plugin_status plugin_start(const void* parameter)
 #endif

     /* standard stuff */
-    PLUGIN_IRAM_INIT(rb)
     (void)parameter;

     log_init();
diff --git a/apps/plugins/test_sampr.c b/apps/plugins/test_sampr.c
index 85525c5..db8301b 100644
--- a/apps/plugins/test_sampr.c
+++ b/apps/plugins/test_sampr.c
@@ -29,8 +29,6 @@
  */


-PLUGIN_IRAM_DECLARE;
-
 static int hw_freq IDATA_ATTR = HW_FREQ_DEFAULT;
 static unsigned long hw_sampr IDATA_ATTR = HW_SAMPR_DEFAULT;

@@ -298,8 +296,6 @@ enum plugin_status plugin_start(const void *parameter)
     /* Disable all talking before initializing IRAM */
     rb->talk_disable(true);

-    PLUGIN_IRAM_INIT(rb);
-
     while (!exit)
     {
         int result = rb->do_menu(&menu, &selected, NULL, false);
diff --git a/apps/plugins/zxbox/zxbox.c b/apps/plugins/zxbox/zxbox.c
index 459e062..edaa7cf 100644
--- a/apps/plugins/zxbox/zxbox.c
+++ b/apps/plugins/zxbox/zxbox.c
@@ -20,10 +20,6 @@
  ****************************************************************************/

 #include "zxconfig.h"
-
-
-PLUGIN_IRAM_DECLARE
-
 #include "spkey_p.h"

 spkeyboard kb_mkey;
@@ -61,8 +57,6 @@ long start_time IBSS_ATTR = 0;

 enum plugin_status plugin_start(const void* parameter)
 {
-    PLUGIN_IRAM_INIT(rb)
-
 #if LCD_DEPTH > 1
     rb->lcd_set_backdrop(NULL);
 #endif