megaed-sv: download memory command
[megadrive.git] / megaed-sv / main.c
index 7c0955f..6486a05 100644 (file)
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include <stdarg.h>
 
 #define u8      unsigned char
@@ -5,10 +6,15 @@
 #define u32     unsigned int
 
 #define noinline __attribute__((noinline))
+#define _packed __attribute__((packed))
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
 
 #include "edos.h"
 #include "asmtools.h"
 
+extern u16 start_hvc;
+
 #define GFX_DATA_PORT    0xC00000
 #define GFX_CTRL_PORT    0xC00004
 
@@ -18,7 +24,7 @@
 #define TILE_FONT_BASE   (TILE_MEM_END / 32  - FONT_LEN)
 
 /* note: using ED menu's layout here.. */
-#define WPLAN            (TILE_MEM_END + 0x0000)
+#define WPLANE           (TILE_MEM_END + 0x0000)
 #define HSCRL            (TILE_MEM_END + 0x0800)
 #define SLIST            (TILE_MEM_END + 0x0C00)
 #define APLANE           (TILE_MEM_END + 0x1000)
@@ -100,7 +106,7 @@ static void printf_line(int x, const char *buf)
 
         /* scroll plane */
         write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
-        write16(GFX_DATA_PORT, (printf_ypos - 27) * 8);
+        write16(GFX_DATA_PORT, (printf_ypos - CSCREEN_H + 1) * 8);
     }
 }
 
@@ -173,7 +179,7 @@ static noinline int printf(const char *fmt, ...)
             break;
         case 'x':
             uval = va_arg(ap, int);
-            while (fwidth > 0 && uval < (1 << (fwidth - 1) * 4)) {
+            while (fwidth > 1 && uval < (1 << (fwidth - 1) * 4)) {
                 buf[d++] = prefix0 ? '0' : ' ';
                 fwidth--;
             }
@@ -207,13 +213,94 @@ static noinline int printf(const char *fmt, ...)
     return d; // wrong..
 }
 
-void exception(void)
+static const char *exc_names[] = {
+    NULL,
+    NULL,
+    "Bus Error",
+    "Address Error",
+    "Illegal Instruction",
+    "Zero Divide",
+    "CHK Instruction",
+    "TRAPV Instruction",
+    "Privilege Violation",  /*  8  8 */
+    "Trace",
+    "Line 1010 Emulator",
+    "Line 1111 Emulator",
+    NULL,
+    NULL,
+    NULL,
+    "Uninitialized Interrupt",
+    NULL,                   /* 10 16 */
+    NULL,
+    NULL,
+    NULL,
+    NULL,
+    NULL,
+    NULL,
+    NULL,
+    "Spurious Interrupt",   /* 18 24 */
+    "l1 irq",
+    "l2 irq",
+    "l3 irq",
+    "l4 irq",
+    "l5 irq",
+    "l6 irq",
+    "l7 irq",
+};
+
+struct exc_frame {
+    u32 dr[8];
+    u32 ar[8];
+    u16 ecxnum; // from handler
+    union {
+        struct {
+            u16 sr;
+            u32 pc;
+        } g _packed;
+        struct {
+            u16 fc;
+            u32 addr;
+            u16 ir;
+            u16 sr;
+            u32 pc;
+        } bae _packed; // bus/address error frame
+    };
+} _packed;
+
+int xtttt(void) { return sizeof(struct exc_frame); }
+
+void exception(const struct exc_frame *f)
 {
-    VDP_drawTextML("============", APLANE, 0, 0);
-    VDP_drawTextML(" exception! ", APLANE, 0, 1);
-    VDP_drawTextML("============", APLANE, 0, 2);
-    while (1)
+    int i;
+
+    while (read16(GFX_CTRL_PORT) & 2)
         ;
+    write16(GFX_CTRL_PORT, 0x8000 | (VDP_MODE1 << 8) | 0x04);
+    write16(GFX_CTRL_PORT, 0x8000 | (VDP_MODE2 << 8) | 0x44);
+    /* adjust scroll */
+    write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
+    write16(GFX_DATA_PORT,
+      printf_ypos >= CSCREEN_H ?
+        (printf_ypos - CSCREEN_H + 1) * 8 : 0);
+
+    printf("exception %i ", f->ecxnum);
+    if (f->ecxnum < ARRAY_SIZE(exc_names) && exc_names[f->ecxnum] != NULL)
+        printf("(%s)", exc_names[f->ecxnum]);
+    if (f->ecxnum < 4)
+        printf(" (%s)", (f->bae.fc & 0x10) ? "r" : "w");
+    printf("    \n");
+
+    if (f->ecxnum < 4) {
+        printf("  PC: %08x SR: %04x    \n", f->bae.pc, f->bae.sr);
+        printf("addr: %08x IR: %04x FC: %02x   \n",
+               f->bae.addr, f->bae.ir, f->bae.fc);
+    }
+    else {
+        printf("  PC: %08x SR: %04x    \n", f->g.pc, f->g.sr);
+    }
+    for (i = 0; i < 8; i++)
+        printf("  D%d: %08x A%d: %08x    \n", i, f->dr[i], i, f->ar[i]);
+    printf("                       \n");
 }
 
 void vbl(void)
@@ -221,10 +308,10 @@ void vbl(void)
 }
 
 static int usb_read_while_ready(OsRoutine *ed,
-    void *buf_, int maxlen)
+    void *buf_, unsigned int maxlen)
 {
     u8 *buf = buf_;
-    int r = 0;
+    unsigned int r = 0;
 
     while (ed->usbRdReady() && r < maxlen)
         buf[r++] = ed->usbReadByte();
@@ -232,6 +319,28 @@ static int usb_read_while_ready(OsRoutine *ed,
     return r;
 }
 
+static int usb_read(OsRoutine *ed, void *buf_, unsigned int maxlen)
+{
+    u8 *buf = buf_;
+    unsigned int r = 0;
+
+    while (r < maxlen)
+        buf[r++] = ed->usbReadByte();
+
+    return r;
+}
+
+static int usb_write(OsRoutine *ed, const void *buf_, unsigned int maxlen)
+{
+    const u8 *buf = buf_;
+    unsigned int r = 0;
+
+    while (r < maxlen)
+        ed->usbWriteByte(buf[r++]);
+
+    return r;
+}
+
 /*
  * TH = 1 : ?1CBRLDU    3-button pad return value (not read)
  * TH = 0 : ?0SA00DU    3-button pad return value
@@ -291,6 +400,29 @@ static int do_test(OsRoutine *ed, u8 b3)
     return -1;
 }
 
+static int do_custom(OsRoutine *ed, u8 b3)
+{
+    struct {
+        unsigned int addr;
+        unsigned int size;
+    } d;
+
+    switch (b3)
+    {
+    case 'd':
+        usb_read(ed, &d, sizeof(d));
+        ed->usbWriteByte('k');
+        printf("sending %i bytes from %06x..\n", d.size, d.addr);
+        usb_write(ed, (void *)d.addr, d.size);
+        printf("done.\n");
+        return 1;
+    default:
+        break;
+    }
+
+    return -1;
+}
+
 #define MTYPE_OS 0
 #define MTYPE_MD 1
 #define MTYPE_SSF 2
@@ -299,7 +431,7 @@ static int do_test(OsRoutine *ed, u8 b3)
 #define MTYPE_10M 5
 #define MTYPE_32X 6
 
-static int do_run(OsRoutine *ed, u8 b3)
+static int do_run(OsRoutine *ed, u8 b3, int tas_sync)
 {
     u8 mapper = 0;
 
@@ -327,14 +459,16 @@ static int do_run(OsRoutine *ed, u8 b3)
         return -1;
     }
 
-    while (read32(GFX_CTRL_PORT) & 2)
+    printf("starting mapper %x..\n", mapper);
+
+    while (read16(GFX_CTRL_PORT) & 2)
         ;
     ed->VDP_setReg(VDP_MODE1, 0x04); 
     ed->VDP_setReg(VDP_MODE2, 0x44); 
 
     ed->usbWriteByte('k');
 
-    run_game(mapper);
+    run_game(mapper, tas_sync);
     /* should not get here.. */
 
     return -1;
@@ -364,9 +498,15 @@ int main()
     for (i = 0; i < PLANE_W * PLANE_H / 2; i++)
         write32(GFX_DATA_PORT, 0);
 
+    /* scroll planes */
+    write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0));
+    write32(GFX_DATA_PORT, 0);
+
     /* note: relying on ED menu's font setup here.. */
 
-    printf("version: %02x\n", read8(0xa10001));
+    printf("\n");
+    printf("version: %02x, start_hvc: %04x\n",
+           read8(0xa10001), start_hvc);
     printf("ED os/fw: %x/%x\n\n", ed->osGetOsVersion(),
            ed->osGetFirmVersion());
 
@@ -399,8 +539,9 @@ int main()
             printf("done\n");
             break;
         case 'r':
+        case 'R':
             buf[2] = ed->usbReadByte();
-            ret = do_run(ed, buf[2]);
+            ret = do_run(ed, buf[2], buf[1] == 'R');
             if (ret != 0) {
                 d = 3;
                 goto bad_input;
@@ -418,6 +559,17 @@ int main()
             }
             ed->usbWriteByte('k');
             break;
+        case 'x':
+            buf[2] = ed->usbReadByte();
+            ret = do_custom(ed, buf[2]);
+            if (ret == 1)
+                break;
+            if (ret != 0) {
+                d = 3;
+                goto bad_input;
+            }
+            ed->usbWriteByte('k');
+            break;
         default:
             d = 2;
             goto bad_input;