GameBoy Advance Multiboot & Dumper

Dependencies:   SDFileSystem mbed

Files at this revision

API Documentation at this revision

Comitter:
akkera102
Date:
Mon Nov 24 13:28:15 2014 +0000
Commit message:
1st;

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
crc.cpp Show annotated file Show diff for this revision Revisions of this file
crc.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
multiboot.cpp Show annotated file Show diff for this revision Revisions of this file
multiboot.h Show annotated file Show diff for this revision Revisions of this file
readme.txt Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 66a93ef88e4e SDFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Mon Nov 24 13:28:15 2014 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
diff -r 000000000000 -r 66a93ef88e4e crc.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crc.cpp	Mon Nov 24 13:28:15 2014 +0000
@@ -0,0 +1,35 @@
+#include "crc.h"
+
+static uint32_t r;
+
+uint32_t crcU32n(uint32_t d)
+{
+    uint32_t bit;
+
+    for(bit=0; bit<32; bit++)
+    {
+        if((r ^ d) & 0x01)
+        {
+            r = (r >> 1) ^ 0x0000c37b;
+        }
+        else
+        {
+            r = (r >> 1);
+        }
+
+        d = d >> 1;
+    }
+
+    return r;
+}
+
+uint32_t crcU32(uint32_t d)
+{
+    crcReset();
+    return crcU32n(d);
+}
+
+void crcReset(void)
+{
+    r = 0x0000c387;
+}
diff -r 000000000000 -r 66a93ef88e4e crc.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crc.h	Mon Nov 24 13:28:15 2014 +0000
@@ -0,0 +1,11 @@
+#ifndef CRC_H
+#define CRC_H
+ 
+#include "mbed.h"
+
+uint32_t crcU32n(uint32_t d);
+uint32_t crcU32(uint32_t d);
+void     crcReset(void);
+
+
+#endif
diff -r 000000000000 -r 66a93ef88e4e main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Nov 24 13:28:15 2014 +0000
@@ -0,0 +1,82 @@
+#include "main.h"
+#include "crc.h"
+#include "multiboot.h"
+#include "SDFileSystem.h"
+
+static SDFileSystem sd(p11, p12, p13, p8, "sd");
+static Serial pc(USBTX, USBRX);
+ 
+int main(void)
+{
+    int ret = MultiBoot("/local/test.gba");
+    if(ret == EXIT_FAILURE)
+    {
+        SystemError("MultiBoot", ret);
+    }
+
+    WaitSPI32(0x0, 0x1234abcd, "Wait for GBA(Calculate dump size)");
+
+    uint32_t size    = WriteSPI32(0x0, "size");
+    uint32_t sizeCRC = WriteSPI32(0x0, "sizeCRC");
+    uint32_t crc     = crcU32(size);
+
+    if(crc != sizeCRC)
+    {
+        SystemError("sizeCRC", crc);
+    }
+    pc.printf("sizeCRC OK!\n");
+
+    FILE *fp = fopen("/sd/data.bin", "w");
+    if(fp == NULL)
+    {
+        SystemError("Could not open file for write", 0);
+    }
+
+    uint32_t table[256/4];
+    uint32_t cnt = 0;
+    int i;
+
+    pc.printf("data sending...(NoDebug)\n");
+    crcReset();
+
+    for(i=0; i<size/4; i++)
+    {
+        table[cnt] = WriteSPI32NoDebug(0x0);
+        crc = crcU32n(table[cnt]);
+        cnt++;
+
+        if(cnt >= 256/4)
+        {
+            fwrite(&table, sizeof(uint32_t), 256/4, fp);
+            cnt = 0;
+        }
+
+        wait_us(100);   // GBA too slow...
+    }
+    fclose(fp);
+
+    uint32_t dataCRC = WriteSPI32(0x0, "dataCRC");
+    if(crc != dataCRC)
+    {
+        SystemError("dataCRC", crc);
+    }
+    pc.printf("dataCRC OK!\n");
+
+
+    pc.printf("All done\n");
+    for(;;)
+    {
+        wait(1);
+    }
+}
+
+void SystemError(char* str, uint32_t hex)
+{
+    pc.printf("[Err][0x%x] %s\n", hex, str);
+
+    for(;;)
+    {
+        wait(1);
+    }
+}
+    
diff -r 000000000000 -r 66a93ef88e4e main.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Mon Nov 24 13:28:15 2014 +0000
@@ -0,0 +1,8 @@
+#ifndef MAIN_H
+#define MAIN_H
+ 
+#include "mbed.h"
+
+void SystemError(char* str, uint32_t hex);
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 66a93ef88e4e mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Nov 24 13:28:15 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89
\ No newline at end of file
diff -r 000000000000 -r 66a93ef88e4e multiboot.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/multiboot.cpp	Mon Nov 24 13:28:15 2014 +0000
@@ -0,0 +1,171 @@
+//  mbed GBA Loader - Boot up a GameBoy Advance using Multiboot (no cart req.)
+//  
+//  Ken Kaarvik    kkaarvik@yahoo.com    Nov 13, 2010
+//  akkera102                            Nov 08, 2014(modified)
+//
+//  mbed(LPC1768)   gba serial port(color)
+//  p1-0V           6-GND (blue)
+//  p5-mosi         3-SI  (orange)
+//  p6-miso         2-SO  (red)
+//  p7-sck          5-SC  (green)
+//
+//  The GBA file that you wish to load into the GBA is stored on the mbed's local file system.
+//  Apply power to the mbed (or reset) before turning on the GBA.
+
+#include "multiboot.h"
+
+static LocalFileSystem local("local");
+static Serial pc(USBTX, USBRX);
+static SPI spi(p5, p6, p7);
+
+
+uint32_t WriteSPI32(uint32_t w, char* msg)
+{
+    uint32_t r = WriteSPI32NoDebug(w);
+
+    pc.printf("0x%08x 0x%08x  ; %s\n",r , w, msg);
+    return  r;
+}
+
+uint32_t WriteSPI32NoDebug(uint32_t w)
+{
+    uint16_t w1 = w >> 16;
+    uint16_t w2 = w & 0xffff;
+
+    uint32_t r;
+    r = spi.write(w1) << 16;
+    r = spi.write(w2) | r;
+    
+    return r;
+}
+
+void WaitSPI32(uint32_t w, uint32_t comp, char* msg)
+{
+    pc.printf("%s 0x%08x\n", msg, comp);
+    uint32_t r;
+
+    do
+    {
+        r = WriteSPI32NoDebug(w);
+        wait(0.1);
+
+    } while(r != comp);
+}
+
+int MultiBoot(char* filename) 
+{
+    spi.format(16, 3);
+    spi.frequency(100000);
+
+
+    FILE *fp = fopen(filename, "rb");
+    if(fp == NULL)
+    {
+        pc.printf("Err: Can't open file\n");
+        return EXIT_FAILURE;
+    }
+
+    fseek(fp, 0L, SEEK_END);
+    long fsize = (ftell(fp) + 0x0f) & 0xfffffff0;
+
+    if(fsize > 0x40000)
+    {
+        pc.printf("Err: Max file size 256kB\n");
+        return EXIT_FAILURE;
+    }
+
+    fseek(fp, 0L, SEEK_SET);
+    long fcnt = 0;
+
+
+    uint32_t r, w, w2;
+    int i, bit;
+
+    WaitSPI32(0x00006202, 0x72026202, "Looking for GBA");
+
+    r = WriteSPI32(0x00006202, "Found GBA");
+    r = WriteSPI32(0x00006102, "Recognition OK");
+
+    pc.printf("Send Header(NoDebug)\n");
+    for(i=0; i<=0x5f; i++)
+    {
+        w = getc(fp);
+        w = getc(fp) << 8 | w;
+        fcnt += 2;
+
+        r = WriteSPI32NoDebug(w);
+    }
+
+    r = WriteSPI32(0x00006200, "Transfer of header data complete");
+    r = WriteSPI32(0x00006202, "Exchange master/slave info again");
+
+    r = WriteSPI32(0x000063d1, "Send palette data");
+    r = WriteSPI32(0x000063d1, "Send palette data, receive 0x73hh****");  
+
+    uint32_t m = ((r & 0x00ff0000) >>  8) + 0xffff00d1;
+    uint32_t h = ((r & 0x00ff0000) >> 16) + 0xf;
+
+    r = WriteSPI32((((r >> 16) + 0xf) & 0xff) | 0x00006400, "Send handshake data");
+    r = WriteSPI32((fsize - 0x190) / 4, "Send length info, receive seed 0x**cc****");
+
+    uint32_t f = (((r & 0x00ff0000) >> 8) + h) | 0xffff0000;
+    uint32_t c = 0x0000c387;
+
+
+    pc.printf("Send encrypted data(NoDebug)\n");
+
+    while(fcnt < fsize)
+    {
+        w = getc(fp);
+        w = getc(fp) <<  8 | w;
+        w = getc(fp) << 16 | w;
+        w = getc(fp) << 24 | w;
+
+        w2 = w;
+
+        for(bit=0; bit<32; bit++)
+        {
+            if((c ^ w) & 0x01)
+            {
+                c = (c >> 1) ^ 0x0000c37b;
+            }
+            else
+            {
+                c = c >> 1;
+            }
+
+            w = w >> 1;
+        }
+
+        m = (0x6f646573 * m) + 1;
+        WriteSPI32NoDebug(w2 ^ ((~(0x02000000 + fcnt)) + 1) ^m ^0x43202f2f);
+
+        fcnt = fcnt + 4;
+    }
+    fclose(fp);
+
+
+    for(bit=0; bit<32; bit++)
+    {
+        if((c ^ f) & 0x01)
+        {
+            c =( c >> 1) ^ 0x0000c37b;
+        }
+        else
+        {
+            c = c >> 1;
+        }
+
+        f = f >> 1;
+    }
+
+    WaitSPI32(0x00000065, 0x00750065, "Wait for GBA to respond with CRC");
+
+    r = WriteSPI32(0x00000066, "GBA ready with CRC");
+    r = WriteSPI32(c,          "Let's exchange CRC!");
+
+    pc.printf("CRC ...hope they match!\n");
+    pc.printf("MulitBoot done\n");
+
+    return EXIT_SUCCESS;
+}
diff -r 000000000000 -r 66a93ef88e4e multiboot.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/multiboot.h	Mon Nov 24 13:28:15 2014 +0000
@@ -0,0 +1,13 @@
+#ifndef MULTIBOOT_H
+#define MULTIBOOT_H
+ 
+#include "mbed.h"
+
+uint32_t WriteSPI32(uint32_t w, char* msg);
+uint32_t WriteSPI32NoDebug(uint32_t w);
+void     WaitSPI32(uint32_t w, uint32_t comp, char* msg);
+
+int      MultiBoot(char* filename);
+
+
+#endif
diff -r 000000000000 -r 66a93ef88e4e readme.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/readme.txt	Mon Nov 24 13:28:15 2014 +0000
@@ -0,0 +1,30 @@
+# MBED --- MicroSD slot
+
+AE-MICRO-SD-DIP K-05488
+http://akizukidenshi.com/catalog/g/gK-05488/
+
+PIN                     MBED
+
+1       DAT2            NoUse
+2       CD/DAT3         P8  (DigitalOut cs)
+3       CMD             P11 (SPI mosi)
+4       VDD             3.3V
+5       CLK             P13 (SPI sclk)
+6       VSS             GND
+7       DAT0            P12 (SPI miso)
+8       DAT1            NoUse
+9                       NoUse
+10                      NoUse
+
+
+# MBED --- MulitBoot Cable
+
+AGB-005
+http://www.nintendo.co.jp/n08/hardware/syuhen/
+
+mbed(LPC1768)   gba serial port(color)
+p1-0V           6-GND (blue)
+p5-mosi         3-SI  (orange)
+p6-miso         2-SO  (red)
+p7-sck          5-SC  (green)
+