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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
diff --git a/apps/debug_menu.c b/apps/debug_menu.c
index 1ea27b4..59cd773 100644
--- a/apps/debug_menu.c
+++ b/apps/debug_menu.c
@@ -23,7 +23,6 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdbool.h>
-#include <string.h>
 #include "lcd.h"
 #include "menu.h"
 #include "debug_menu.h"
@@ -52,7 +51,6 @@
 #include "tagcache.h"
 #endif
 #include "lcd-remote.h"
-#include "crc32.h"
 #include "logf.h"
 #ifndef SIMULATOR
 #include "disk.h"
@@ -95,7 +93,6 @@
 #ifdef IAUDIO_X5
 #include "ds2411.h"
 #endif
-#include "hwcompat.h"
 #include "button.h"
 #if CONFIG_RTC == RTC_PCF50605
 #include "pcf50605.h"
@@ -104,7 +101,7 @@

 #if CONFIG_CPU == DM320 || CONFIG_CPU == S3C2440 || CONFIG_CPU == TCC7801 \
     || CONFIG_CPU == IMX31L || CONFIG_CPU == AS3525 || CONFIG_CPU == JZ4732 \
-    || defined(CPU_S5L870X)
+    || defined(CPU_S5L870X) || CONFIG_CPU == SH7034
 #include "debug-target.h"
 #endif

@@ -412,62 +409,6 @@ static bool dbg_buffering_thread(void)
 #endif /* HAVE_LCD_BITMAP */


-#if (CONFIG_CPU == SH7034 || defined(CPU_COLDFIRE))
-/* Tool function to read the flash manufacturer and type, if available.
-   Only chips which could be reprogrammed in system will return values.
-   (The mode switch addresses vary between flash manufacturers, hence addr1/2) */
-   /* In IRAM to avoid problems when running directly from Flash */
-static bool dbg_flash_id(unsigned* p_manufacturer, unsigned* p_device,
-                         unsigned addr1, unsigned addr2)
-                         ICODE_ATTR __attribute__((noinline));
-static bool dbg_flash_id(unsigned* p_manufacturer, unsigned* p_device,
-                         unsigned addr1, unsigned addr2)
-
-{
-    unsigned not_manu, not_id; /* read values before switching to ID mode */
-    unsigned manu, id; /* read values when in ID mode */
-
-#if CONFIG_CPU == SH7034
-    volatile unsigned char* flash = (unsigned char*)0x2000000; /* flash mapping */
-#elif defined(CPU_COLDFIRE)
-    volatile unsigned short* flash = (unsigned short*)0; /* flash mapping */
-#endif
-    int old_level; /* saved interrupt level */
-
-    not_manu = flash[0]; /* read the normal content */
-    not_id   = flash[1]; /* should be 'A' (0x41) and 'R' (0x52) from the "ARCH" marker */
-
-    /* disable interrupts, prevent any stray flash access */
-    old_level = disable_irq_save();
-
-    flash[addr1] = 0xAA; /* enter command mode */
-    flash[addr2] = 0x55;
-    flash[addr1] = 0x90; /* ID command */
-    /* Atmel wants 20ms pause here */
-    /* sleep(HZ/50); no sleeping possible while interrupts are disabled */
-
-    manu = flash[0]; /* read the IDs */
-    id   = flash[1];
-
-    flash[0] = 0xF0; /* reset flash (back to normal read mode) */
-    /* Atmel wants 20ms pause here */
-    /* sleep(HZ/50); no sleeping possible while interrupts are disabled */
-
-    restore_irq(old_level); /* enable interrupts again */
-
-    /* I assume success if the obtained values are different from
-        the normal flash content. This is not perfectly bulletproof, they
-        could theoretically be the same by chance, causing us to fail. */
-    if (not_manu != manu || not_id != id) /* a value has changed */
-    {
-        *p_manufacturer = manu; /* return the results */
-        *p_device = id;
-        return true; /* success */
-    }
-    return false; /* fail */
-}
-#endif /* (CONFIG_CPU == SH7034 || CPU_COLDFIRE) */
-
 #ifndef SIMULATOR
 #ifdef CPU_PP
 static int perfcheck(void)
@@ -505,71 +446,9 @@ static int perfcheck(void)
 }
 #endif

-#ifdef HAVE_LCD_BITMAP
 static bool dbg_hw_info(void)
 {
-#if CONFIG_CPU == SH7034
-    char buf[32];
-    int bitmask = HW_MASK;
-    int rom_version = ROM_VERSION;
-    unsigned manu, id; /* flash IDs */
-    bool got_id; /* flag if we managed to get the flash IDs */
-    unsigned rom_crc = 0xffffffff; /* CRC32 of the boot ROM */
-    bool has_bootrom; /* flag for boot ROM present */
-    int oldmode;  /* saved memory guard mode */
-
-    oldmode = system_memory_guard(MEMGUARD_NONE);  /* disable memory guard */
-
-    /* get flash ROM type */
-    got_id = dbg_flash_id(&manu, &id, 0x5555, 0x2AAA); /* try SST, Atmel, NexFlash */
-    if (!got_id)
-        got_id = dbg_flash_id(&manu, &id, 0x555, 0x2AA); /* try AMD, Macronix */
-
-    /* check if the boot ROM area is a flash mirror */
-    has_bootrom = (memcmp((char*)0, (char*)0x02000000, 64*1024) != 0);
-    if (has_bootrom)  /* if ROM and Flash different */
-    {
-        /* calculate CRC16 checksum of boot ROM */
-        rom_crc = crc_32((unsigned char*)0x0000, 64*1024, 0xffffffff);
-    }
-
-    system_memory_guard(oldmode);  /* re-enable memory guard */
-
-    lcd_setfont(FONT_SYSFIXED);
-    lcd_clear_display();
-
-    lcd_puts(0, 0, "[Hardware info]");
-
-    snprintf(buf, 32, "ROM: %d.%02d", rom_version/100, rom_version%100);
-    lcd_puts(0, 1, buf);
-
-    snprintf(buf, 32, "Mask: 0x%04x", bitmask);
-    lcd_puts(0, 2, buf);
-
-    if (got_id)
-        snprintf(buf, 32, "Flash: M=%02x D=%02x", manu, id);
-    else
-        snprintf(buf, 32, "Flash: M=?? D=??"); /* unknown, sorry */
-    lcd_puts(0, 3, buf);
-
-    if (has_bootrom)
-    {
-        if (rom_crc == 0x56DBA4EE) /* known Version 1 */
-            snprintf(buf, 32, "Boot ROM: V1");
-        else
-            snprintf(buf, 32, "ROMcrc: 0x%08x", rom_crc);
-    }
-    else
-    {
-        snprintf(buf, 32, "Boot ROM: none");
-    }
-    lcd_puts(0, 4, buf);
-
-    lcd_update();
-
-    while (!(action_userabort(TIMEOUT_BLOCK)));
-
-#elif CONFIG_CPU == MCF5249 || CONFIG_CPU == MCF5250
+#if CONFIG_CPU == MCF5249 || CONFIG_CPU == MCF5250
     char buf[32];
     unsigned manu, id; /* flash IDs */
     int got_id; /* flag if we managed to get the flash IDs */
@@ -695,97 +574,12 @@ static bool dbg_hw_info(void)
     /* Define this function in your target tree */
     return __dbg_hw_info();
 #endif /* CONFIG_CPU */
-    lcd_setfont(FONT_UI);
-    return false;
-}
-#else /* !HAVE_LCD_BITMAP */
-static bool dbg_hw_info(void)
-{
-    char buf[32];
-    int button;
-    int currval = 0;
-    int rom_version = ROM_VERSION;
-    unsigned manu, id; /* flash IDs */
-    bool got_id; /* flag if we managed to get the flash IDs */
-    unsigned rom_crc = 0xffffffff; /* CRC32 of the boot ROM */
-    bool has_bootrom; /* flag for boot ROM present */
-    int oldmode;  /* saved memory guard mode */
-
-    oldmode = system_memory_guard(MEMGUARD_NONE);  /* disable memory guard */
-
-    /* get flash ROM type */
-    got_id = dbg_flash_id(&manu, &id, 0x5555, 0x2AAA); /* try SST, Atmel, NexFlash */
-    if (!got_id)
-        got_id = dbg_flash_id(&manu, &id, 0x555, 0x2AA); /* try AMD, Macronix */
-
-    /* check if the boot ROM area is a flash mirror */
-    has_bootrom = (memcmp((char*)0, (char*)0x02000000, 64*1024) != 0);
-    if (has_bootrom)  /* if ROM and Flash different */
-    {
-        /* calculate CRC16 checksum of boot ROM */
-        rom_crc = crc_32((unsigned char*)0x0000, 64*1024, 0xffffffff);
-    }
-
-    system_memory_guard(oldmode);  /* re-enable memory guard */
-
-    lcd_clear_display();
-
-    lcd_puts(0, 0, "[HW Info]");
-    while(1)
-    {
-        switch(currval)
-        {
-            case 0:
-                snprintf(buf, 32, "ROM: %d.%02d",
-                         rom_version/100, rom_version%100);
-                break;
-            case 1:
-                if (got_id)
-                    snprintf(buf, 32, "Flash:%02x,%02x", manu, id);
-                else
-                    snprintf(buf, 32, "Flash:??,??"); /* unknown, sorry */
-                break;
-            case 2:
-                if (has_bootrom)
-                {
-                    if (rom_crc == 0x56DBA4EE) /* known Version 1 */
-                        snprintf(buf, 32, "BootROM: V1");
-                    else if (rom_crc == 0x358099E8)
-                        snprintf(buf, 32, "BootROM: V2");
-                        /* alternative boot ROM found in one single player so far */
-                    else
-                        snprintf(buf, 32, "R: %08x", rom_crc);
-                }
-                else
-                    snprintf(buf, 32, "BootROM: no");
-        }
-
-        lcd_puts(0, 1, buf);
-        lcd_update();
-
-        button = get_action(CONTEXT_SETTINGS,TIMEOUT_BLOCK);

-        switch(button)
-        {
-            case ACTION_STD_CANCEL:
-                return false;
-
-            case ACTION_SETTINGS_DEC:
-                currval--;
-                if(currval < 0)
-                    currval = 2;
-                break;
-
-            case ACTION_SETTINGS_INC:
-                currval++;
-                if(currval > 2)
-                    currval = 0;
-                break;
-        }
-    }
+#ifdef HAVE_LCD_BITMAP
+    lcd_setfont(FONT_UI);
+#endif
     return false;
 }
-#endif /* !HAVE_LCD_BITMAP */
 #endif /* !SIMULATOR */

 #ifndef SIMULATOR
@@ -995,18 +789,11 @@ static bool dbg_spdif(void)
 #endif /* CPU_COLDFIRE */

 #ifndef SIMULATOR
-#ifdef HAVE_LCD_BITMAP
  /* button definitions */
 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
    (CONFIG_KEYPAD == IRIVER_H300_PAD)
 #   define DEBUG_CANCEL  BUTTON_OFF

-#elif CONFIG_KEYPAD == RECORDER_PAD
-#   define DEBUG_CANCEL  BUTTON_OFF
-
-#elif CONFIG_KEYPAD == ONDIO_PAD
-#   define DEBUG_CANCEL  BUTTON_MENU
-
 #elif (CONFIG_KEYPAD == IPOD_1G2G_PAD) || \
     (CONFIG_KEYPAD == IPOD_3G_PAD) || \
     (CONFIG_KEYPAD == IPOD_4G_PAD)
@@ -1045,42 +832,7 @@ static bool dbg_spdif(void)
 /* Test code!!! */
 bool dbg_ports(void)
 {
-#if CONFIG_CPU == SH7034
-    char buf[32];
-    int adc_battery_voltage, adc_battery_level;
-
-    lcd_setfont(FONT_SYSFIXED);
-    lcd_clear_display();
-
-    while(1)
-    {
-        snprintf(buf, 32, "PADR: %04x", (unsigned short)PADR);
-        lcd_puts(0, 0, buf);
-        snprintf(buf, 32, "PBDR: %04x", (unsigned short)PBDR);
-        lcd_puts(0, 1, buf);
-
-        snprintf(buf, 32, "AN0: %03x AN4: %03x", adc_read(0), adc_read(4));
-        lcd_puts(0, 2, buf);
-        snprintf(buf, 32, "AN1: %03x AN5: %03x", adc_read(1), adc_read(5));
-        lcd_puts(0, 3, buf);
-        snprintf(buf, 32, "AN2: %03x AN6: %03x", adc_read(2), adc_read(6));
-        lcd_puts(0, 4, buf);
-        snprintf(buf, 32, "AN3: %03x AN7: %03x", adc_read(3), adc_read(7));
-        lcd_puts(0, 5, buf);
-
-        battery_read_info(&adc_battery_voltage, &adc_battery_level);
-        snprintf(buf, 32, "Batt: %d.%03dV %d%%  ", adc_battery_voltage / 1000,
-                 adc_battery_voltage % 1000, adc_battery_level);
-        lcd_puts(0, 6, buf);
-
-        lcd_update();
-        if (button_get_w_tmo(HZ/10) == (DEBUG_CANCEL|BUTTON_REL))
-        {
-            lcd_setfont(FONT_UI);
-            return false;
-        }
-    }
-#elif defined(CPU_COLDFIRE)
+#if defined(CPU_COLDFIRE)
     unsigned int gpio_out;
     unsigned int gpio1_out;
     unsigned int gpio_read;
@@ -1325,82 +1077,6 @@ extern unsigned char serbuf[];
 #endif /* CPU */
     return false;
 }
-#else /* !HAVE_LCD_BITMAP */
-bool dbg_ports(void)
-{
-    char buf[32];
-    int button;
-    int adc_battery_voltage;
-    int currval = 0;
-
-    lcd_clear_display();
-
-    while(1)
-    {
-        switch(currval)
-        {
-        case 0:
-            snprintf(buf, 32, "PADR: %04x", (unsigned short)PADR);
-            break;
-        case 1:
-            snprintf(buf, 32, "PBDR: %04x", (unsigned short)PBDR);
-            break;
-        case 2:
-            snprintf(buf, 32, "AN0: %03x", adc_read(0));
-            break;
-        case 3:
-            snprintf(buf, 32, "AN1: %03x", adc_read(1));
-            break;
-        case 4:
-            snprintf(buf, 32, "AN2: %03x", adc_read(2));
-            break;
-        case 5:
-            snprintf(buf, 32, "AN3: %03x", adc_read(3));
-            break;
-        case 6:
-            snprintf(buf, 32, "AN4: %03x", adc_read(4));
-            break;
-        case 7:
-            snprintf(buf, 32, "AN5: %03x", adc_read(5));
-            break;
-        case 8:
-            snprintf(buf, 32, "AN6: %03x", adc_read(6));
-            break;
-        case 9:
-            snprintf(buf, 32, "AN7: %03x", adc_read(7));
-            break;
-        }
-        lcd_puts(0, 0, buf);
-
-        battery_read_info(&adc_battery_voltage, NULL);
-        snprintf(buf, 32, "Batt: %d.%03dV", adc_battery_voltage / 1000,
-                 adc_battery_voltage % 1000);
-        lcd_puts(0, 1, buf);
-        lcd_update();
-
-        button = get_action(CONTEXT_SETTINGS,HZ/5);
-
-        switch(button)
-        {
-            case ACTION_STD_CANCEL:
-            return false;
-
-        case ACTION_SETTINGS_DEC:
-            currval--;
-            if(currval < 0)
-                currval = 9;
-            break;
-
-        case ACTION_SETTINGS_INC:
-            currval++;
-            if(currval > 9)
-                currval = 0;
-            break;
-        }
-    }
-    return false;
-}
-#endif /* !HAVE_LCD_BITMAP */
 #endif /* !SIMULATOR */

 #if (CONFIG_RTC == RTC_PCF50605) && !defined(SIMULATOR)
diff --git a/firmware/SOURCES b/firmware/SOURCES
index 67bb5ad..77145dd 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -336,6 +336,10 @@ target/sh/system-sh.c
 target/sh/archos/descramble.S
 #endif /* SIMULATOR */

+#if CONFIG_CPU == SH7034
+target/sh/archos/debug-sh7034.c
+#endif
+
 #elif defined(CPU_COLDFIRE)

 #ifndef SIMULATOR
diff --git a/firmware/debug.c b/firmware/debug.c
index 9440458..1880c4f 100644
--- a/firmware/debug.c
+++ b/firmware/debug.c
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include "config.h"
 #include "cpu.h"
 #ifdef HAVE_GDB_API
@@ -257,4 +258,57 @@ void debugf(const char *fmt, ...)
 #endif
 }

+
+#if (CONFIG_CPU == SH7034 || defined(CPU_COLDFIRE))
+/* Tool function to read the flash manufacturer and type, if available.
+   Only chips which could be reprogrammed in system will return values.
+   (The mode switch addresses vary between flash manufacturers, hence addr1/2) */
+   /* In IRAM to avoid problems when running directly from Flash */
+bool dbg_flash_id(unsigned* p_manufacturer, unsigned* p_device,
+                         unsigned addr1, unsigned addr2)
+
+{
+    unsigned not_manu, not_id; /* read values before switching to ID mode */
+    unsigned manu, id; /* read values when in ID mode */
+
+#if CONFIG_CPU == SH7034
+    volatile unsigned char* flash = (unsigned char*)0x2000000; /* flash mapping */
+#elif defined(CPU_COLDFIRE)
+    volatile unsigned short* flash = (unsigned short*)0; /* flash mapping */
+#endif
+    int old_level; /* saved interrupt level */
+
+    not_manu = flash[0]; /* read the normal content */
+    not_id   = flash[1]; /* should be 'A' (0x41) and 'R' (0x52) from the "ARCH" marker */
+
+    /* disable interrupts, prevent any stray flash access */
+    old_level = disable_irq_save();
+
+    flash[addr1] = 0xAA; /* enter command mode */
+    flash[addr2] = 0x55;
+    flash[addr1] = 0x90; /* ID command */
+    /* Atmel wants 20ms pause here */
+    /* sleep(HZ/50); no sleeping possible while interrupts are disabled */
+
+    manu = flash[0]; /* read the IDs */
+    id   = flash[1];
+
+    flash[0] = 0xF0; /* reset flash (back to normal read mode) */
+    /* Atmel wants 20ms pause here */
+    /* sleep(HZ/50); no sleeping possible while interrupts are disabled */
+
+    restore_irq(old_level); /* enable interrupts again */
+
+    /* I assume success if the obtained values are different from
+        the normal flash content. This is not perfectly bulletproof, they
+        could theoretically be the same by chance, causing us to fail. */
+    if (not_manu != manu || not_id != id) /* a value has changed */
+    {
+        *p_manufacturer = manu; /* return the results */
+        *p_device = id;
+        return true; /* success */
+    }
+    return false; /* fail */
+}
+#endif /* (CONFIG_CPU == SH7034 || CPU_COLDFIRE) */
 #endif
diff --git a/firmware/export/debug.h b/firmware/export/debug.h
index f9f93fd..b3b96bc 100644
--- a/firmware/export/debug.h
+++ b/firmware/export/debug.h
@@ -21,13 +21,24 @@
 #ifndef DEBUG_H
 #define DEBUG_H

+#include "config.h"
 #include "../include/_ansi.h"
+#include <stdbool.h>

 extern void debug_init(void);
 extern void debugf(const char* fmt,...) ATTRIBUTE_PRINTF(1, 2);
 extern void ldebugf(const char* file, int line, const char *fmt, ...)
                     ATTRIBUTE_PRINTF(3, 4);

+#if (CONFIG_CPU == SH7034 || defined(CPU_COLDFIRE))
+/* Tool function to read the flash manufacturer and type, if available.
+   Only chips which could be reprogrammed in system will return values.
+   (The mode switch addresses vary between flash manufacturers, hence addr1/2) */
+   /* In IRAM to avoid problems when running directly from Flash */
+bool dbg_flash_id(unsigned* p_manufacturer, unsigned* p_device,
+                   unsigned addr1, unsigned addr2);
+#endif /* (CONFIG_CPU == SH7034 || CPU_COLDFIRE) */
+
 #ifndef CODEC                    
 #ifdef __GNUC__