From: notaz Date: Sat, 27 Sep 2014 21:18:03 +0000 (+0300) Subject: megaed-sv: add exception handler X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=39ac9835e099583fcf38aa92ff83d8515c745104;p=megadrive.git megaed-sv: add exception handler --- diff --git a/megaed-sv/Makefile b/megaed-sv/Makefile index 483307b..24f0965 100644 --- a/megaed-sv/Makefile +++ b/megaed-sv/Makefile @@ -17,6 +17,11 @@ all: $(TARGET).bin $(TARGET).elf: $(OBJS) $(LD) -o $@ -Ted_app.ld -Map $(TARGET).map $^ $(LDLIBS) +$(TARGET)_e.bin: $(TARGET).bin + dd if=/dev/zero of=$@ bs=1M count=1 + dd if=$^ of=$@ bs=1M seek=1 + dd if=$^ of=$@ conv=notrunc + clean: $(RM) $(TARGET).bin $(OBJS) $(TARGET).elf $(TARGET).map diff --git a/megaed-sv/main.c b/megaed-sv/main.c index 7c0955f..a5ba918 100644 --- a/megaed-sv/main.c +++ b/megaed-sv/main.c @@ -1,3 +1,4 @@ +#include #include #define u8 unsigned char @@ -5,6 +6,9 @@ #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" @@ -100,7 +104,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 +177,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 +211,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) diff --git a/megaed-sv/sega_gcc.s b/megaed-sv/sega_gcc.s index 0702158..514f4c8 100644 --- a/megaed-sv/sega_gcc.s +++ b/megaed-sv/sega_gcc.s @@ -1,27 +1,28 @@ - dc.l 0x0,0x200 - dc.l INT,INT,INT,INT,INT,INT,INT - dc.l INT,INT,INT,INT,INT,INT,INT,INT - dc.l INT,INT,INT,INT,INT,INT,INT,INT - dc.l INT,INT,INT,HBL,INT,VBL,INT,INT - dc.l INT,INT,INT,INT,INT,INT,INT,INT - dc.l INT,INT,INT,INT,INT,INT,INT,INT - dc.l INT,INT,INT,INT,INT,INT,INT,INT - dc.l INT,INT,INT,INT,INT,INT,INT - .ascii "SEGA EVERDRIVE " - .ascii "MEGA-ED host " - .ascii "MEGA-ED host " - .ascii "GM 00000000-00" - .byte 0x00,0x00 - .ascii "JD " - .byte 0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00 - .byte 0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff - .ascii " " - .ascii " " - .ascii " " - .ascii "JUE " +exc_tab: + dc.l 0, 0x200, exc02, exc03, exc04, exc05, exc06, exc07 + dc.l exc08, exc09, exc0a, exc0b, exc0c, exc0d, exc0e, exc0f + dc.l exc10, exc11, exc12, exc13, exc14, exc15, exc16, exc17 + dc.l exc18, exc19, exc1a, exc1b, HBL, exc1d, VBL, exc1f + dc.l exc20, exc21, exc22, exc23, exc24, exc25, exc26, exc27 + dc.l exc28, exc29, exc2a, exc2b, exc2c, exc2d, exc2e, exc2f + dc.l exc30, exc31, exc32, exc33, exc34, exc35, exc3e, exc37 + dc.l exc38, exc39, exc3a, exc3b, exc3c, exc3d, exc3e, exc3f + + .ascii "SEGA EVERDRIVE " + .ascii "MEGA-ED host " + .ascii "MEGA-ED host " + .ascii "GM 00000000-00" + .byte 0x00,0x00 + .ascii "JD " + .byte 0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00 + .byte 0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff + .ascii " " + .ascii " " + .ascii " " + .ascii "JUE " RST: - move.w #0x2700, sr + move.w #0x2700, %sr /* magic ED app init */ move.w #0x0000, (0xA13006) jmp init_ed.l @@ -39,21 +40,21 @@ init_ed: dbra %d0, 0b move.w #0x10af, (0xA13006) - move.l #HBL, (0x70) - move.l #VBL, (0x78) + + lea exc_tab, %a0 + adda.l #4*2, %a0 + movea.l #4*2, %a1 + move.l #64-2-1, %d0 +0: + move.l (a0)+, (a1)+ + dbra %d0, 0b moveq #0, %d0 movea.l %d0, %a7 move %a7, %usp - move.w #0x2000, sr + move.w #0x2000, %sr bra main -INT: - movem.l %d0-%d1/%a0-%a1,-(%sp) - jsr exception - movem.l (%sp)+,%d0-%d1/%a0-%a1 - rte - HBL: rte @@ -63,6 +64,15 @@ VBL: movem.l (%sp)+,%d0-%d1/%a0-%a1 rte +pre_exception: + move.w #0x2700, %sr + movem.l %d0-%d7/%a0-%a7,-(%sp) + move.l %sp, %d0 + move.l %d0,-(%sp) + jsr exception +0: + bra 0b + * Standard 32X startup code for MD side at 0x3F0 .org 0x3F0 @@ -133,3 +143,73 @@ VBL: .word 0xFFC0,0x4CD6,0x7FF9,0x44FC,0x0000,0x6014,0x43F9,0x00A1 .word 0x5100,0x3340,0x0006,0x303C,0x8000,0x6004,0x44FC,0x0001 +.macro exc_stub num +exc\num: + move.w #0x\num, -(%sp) + jmp pre_exception +.endm + +exc_stub 02 +exc_stub 03 +exc_stub 04 +exc_stub 05 +exc_stub 06 +exc_stub 07 +exc_stub 08 +exc_stub 09 +exc_stub 0a +exc_stub 0b +exc_stub 0c +exc_stub 0d +exc_stub 0e +exc_stub 0f +exc_stub 10 +exc_stub 11 +exc_stub 12 +exc_stub 13 +exc_stub 14 +exc_stub 15 +exc_stub 16 +exc_stub 17 +exc_stub 18 +exc_stub 19 +exc_stub 1a +exc_stub 1b +# exc_stub 1c +exc_stub 1d +# exc_stub 1e +exc_stub 1f +exc_stub 20 +exc_stub 21 +exc_stub 22 +exc_stub 23 +exc_stub 24 +exc_stub 25 +exc_stub 26 +exc_stub 27 +exc_stub 28 +exc_stub 29 +exc_stub 2a +exc_stub 2b +exc_stub 2c +exc_stub 2d +exc_stub 2e +exc_stub 2f +exc_stub 30 +exc_stub 31 +exc_stub 32 +exc_stub 33 +exc_stub 34 +exc_stub 35 +exc_stub 36 +exc_stub 37 +exc_stub 38 +exc_stub 39 +exc_stub 3a +exc_stub 3b +exc_stub 3c +exc_stub 3d +exc_stub 3e +exc_stub 3f + +# vim:filetype=asmM68k:ts=4:sw=4:expandtab