From: notaz Date: Thu, 23 Oct 2014 00:50:03 +0000 (+0300) Subject: megaed-stop - testcase for mega everdrive SDRAM corruption X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=megadrive.git;a=commitdiff_plain;h=d2bfad8aa870a4fc1d11c338ea9576cf50188e5a megaed-stop - testcase for mega everdrive SDRAM corruption --- diff --git a/megaed-stop/Makefile b/megaed-stop/Makefile new file mode 100644 index 0000000..fe8f346 --- /dev/null +++ b/megaed-stop/Makefile @@ -0,0 +1,39 @@ +CROSS = m68k-elf- +CC = $(CROSS)gcc +AS = $(CROSS)as +LD = $(CROSS)ld +OBJCOPY = $(CROSS)objcopy + +ASFLAGS += -m68000 --register-prefix-optional --bitwise-or -pic +ASFLAGS_CC += -Wa,-m68000 -Wa,--register-prefix-optional -Wa,--bitwise-or -Wa,-pic +CFLAGS += -Wall -g -O1 -m68000 -fomit-frame-pointer +LDLIBS += $(shell $(CC) -print-file-name=libgcc.a) + +TARGET = megaed_stop +OBJS = sega_gcc.o main.o + +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 + + +%.bin: %.elf + $(OBJCOPY) -I elf32-m68k -O binary $^ $@ + +%.o: %.S + $(CC) -c -o $@ $^ $(ASFLAGS_CC) + +rel: $(TARGET).bin + mkdir -p /tmp/$(TARGET)/src/ + cp $^ /tmp/$(TARGET)/ + $(MAKE) clean + cp -a * /tmp/$(TARGET)/src/ diff --git a/megaed-stop/UNLICENSE b/megaed-stop/UNLICENSE new file mode 100644 index 0000000..68a49da --- /dev/null +++ b/megaed-stop/UNLICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/megaed-stop/ed_app.ld b/megaed-stop/ed_app.ld new file mode 100644 index 0000000..bd40362 --- /dev/null +++ b/megaed-stop/ed_app.ld @@ -0,0 +1,116 @@ +OUTPUT_ARCH(m68k) +SEARCH_DIR(.) +/*GROUP(-lbcc -lc -lgcc)*/ +__DYNAMIC = 0; + +/* + * Setup the memory map of the SEGA Genesis. + * stack grows down from high memory. + * + * The memory map look like this: + * +--------------------+ <- low memory + * | .text | + * | _etext | + * | ctor list | the ctor and dtor lists are for + * | dtor list | C++ support + * +--------------------+ + * | .data | initialized data goes here + * | _edata | + * +--------------------+ + * | .bss | + * | __bss_start | start of bss, cleared by crt0 + * | _end | start of heap, used by sbrk() + * +--------------------+ + * . . + * . . + * . . + * | __stack | top of stack + * +--------------------+ + */ +MEMORY +{ + rom : ORIGIN = 0x00100000, LENGTH = 0x00300000 + ram : ORIGIN = 0x00ff0100, LENGTH = 0x0000FF00 +} + +/* + * allocate the stack to be at the top of memory, since the stack + * grows down + */ + +PROVIDE (__stack = 0x00fffff0); + +/* + * Initalize some symbols to be zero so we can reference them in the + * crt0 without core dumping. These functions are all optional, but + * we do this so we can have our crt0 always use them if they exist. + * This is so BSPs work better when using the crt0 installed with gcc. + * We have to initalize them twice, so we cover a.out (which prepends + * an underscore) and coff object file formats. + */ +PROVIDE (hardware_init_hook = 0); +PROVIDE (_hardware_init_hook = 0); +PROVIDE (software_init_hook = 0); +PROVIDE (_software_init_hook = 0); + +SECTIONS +{ + .text 0x00100000: + { + *(.text) + . = ALIGN(0x4); + __CTOR_LIST__ = .; + LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2) + *(.ctors) + LONG(0) + __CTOR_END__ = .; + __DTOR_LIST__ = .; + LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2) + *(.dtors) + LONG(0) + __DTOR_END__ = .; + *(.rodata*) + *(.gcc_except_table) + + __INIT_SECTION__ = . ; + *(.init) + SHORT (0x4e75) /* rts */ + + __FINI_SECTION__ = . ; + *(.fini) + SHORT (0x4e75) /* rts */ + + _etext = .; + *(.lit) + } > rom + + .data 0xff0100 : + { + *(.shdata) + *(.data) + _edata = .; + } > ram + + /* .bss 0xff0100 : */ + .bss BLOCK (0x4) : + { + __bss_start = . ; + *(.shbss) + *(.bss) + *(COMMON) + *(.eh_fram) + *(.eh_frame) + _end = ALIGN (0x8); + __end = _end; + } > ram + + .stab 0 (NOLOAD) : + { + *(.stab) + } + + .stabstr 0 (NOLOAD) : + { + *(.stabstr) + } +} diff --git a/megaed-stop/edos.h b/megaed-stop/edos.h new file mode 100644 index 0000000..58025e3 --- /dev/null +++ b/megaed-stop/edos.h @@ -0,0 +1,153 @@ +/* + * File: edos.h + * Author: krik + * + * Created on 29 Ìàðò 2012 ã., 1:29 + */ + +#ifndef _EDOS_H +#define _EDOS_H + +//#include "segalib.h" + +/* + * saddr: address in sectors + * slen: lenght in sectors + * wr_slen: how many sectors will be written + * sector size 512 bytes + * dma reading of usb and SD available only if data destination located in rom area (0-0x3ffffff). dma reading not available for genesis ram + * destination address for dma reading must be aligned to 512b + * one mbyte of sdram can be mapped to one of four banks in rom space. osSetMemMap(u16) can be used for mapping configuration. + * os code located in last mbyte of sdram and should be mapped in bank 0 + * user code located in begin of sdram. default mapping 0x210f means that end of sdram (OS) mapped to area 0-0x0fffff, first mbyte of user code mapped to 0x100000-0x1fffff, + * memory configuration: + * genesis address: 0x000000-0x0fffff, physical sdram address: 0xf00000-0xffffff OS + * genesis address: 0x100000-0x3fffff, physical sdram address: 0x000000-0x2fffff user code (current application) + * 0xff0000-0xff00ff OS dma code + * first 256bytes of genesis ram must be reserved for OS + * */ + + +#define MEM_ERR_SPI_RD_TIMEOUT 120 + +#define ERR_FILE_TOO_BIG 140 +#define ERR_WRON_OS_SIZE 142 +#define ERR_OS_VERIFY 143 +#define ERR_OS_VERIFY2 144 +#define ERR_BAD_DMA_ADDRESS 145 +#define ERR_MUST_NOT_RET 146 + +//100 - 119 fat errors +#define FAT_ERR_NOT_EXIST 100 +#define FAT_ERR_EXIST 101 +#define FAT_ERR_NAME 102 +#define FAT_ERR_OUT_OF_FILE 103 +#define FAT_ERR_BAD_BASE_CLUSTER 104; +#define FAT_ERR_NO_FRE_SPACE 105 +#define FAT_ERR_NOT_FILE 106 +#define FAT_ERR_FILE_MODE 107 +#define FAT_ERR_ROT_OVERFLOW 108 +#define FAT_ERR_OUT_OF_TABLE 109 +#define FAT_ERR_INIT 110 +#define FAT_LFN_BUFF_OVERFLOW 111 +#define FAT_DISK_NOT_READY 112 +#define FAT_ERR_SIZE 113 +#define FAT_ERR_RESIZE 114 + + +#define DISK_ERR_INIT 50 +#define DISK_ERR_RD1 62 +#define DISK_ERR_RD2 63 + +#define DISK_ERR_WR1 64 +#define DISK_ERR_WR2 65 +#define DISK_ERR_WR3 66 +#define DISK_ERR_WR4 67 +#define DISK_ERR_WR5 68 + +typedef struct { + u32 entry_cluster; + u32 size; + u32 hdr_sector; + u16 hdr_idx; + u8 name[256]; + u16 is_dir; +} FatFullRecord; + +typedef struct { + u8(*diskWrite)(u32 saddr, u8 *buff, u16 slen); + u8(*diskRead)(u32 saddr, void *buff, u16 slen);//*dst must be alligned to 512bytes and be in range 0x100000-0x3fffff. *dst can be located in genesis ram also, but transfer will be slow in this case + u8(*usbReadByte)();//loop inside of function waiting until usbRdReady != 0 + void (*usbWriteByte)(u8 dat);//loop inside of function waiting until usbWrReady != 0 + u8(*usbReadDma)(u16 *dst, u16 slen);//*dst must be alligned to 512bytes and be in range 0x100000-0x3fffff + u8(*usbReadPio)(u16 *dst, u16 slen); + u8(*usbRdReady)(); //return 1 if some data comes from pc + u8(*usbWrReady)(); //return 1 usb ready to send byte + void (*osSetMemMap)(u16 map); //memoty configuration 4x1Mb + u16(*osGetOsVersion)(); + u16(*osGetFirmVersion)(); + void (*osGetSerial)(u32 * buff); // 8bytes uniq serial number + void (*VDP_setReg)(u8 reg, u8 value);//os vdp functions should be used, otherwise system may hang while udb/sd dma + void (*VDP_vintOn)(); + void (*VDP_vintOff)(); + void (*memInitDmaCode)();//copy dma routine to genesis ram + u8(*fatReadDir)(FatFullRecord * frec);//get next record in current dir + void(*fatOpenDir)(u32 entry_cluster);//arg is entry cluster ot dir or 0. zero arg means that need to open root dir + u8(*fatOpenFileByeName)(u8 *name, u32 wr_slen);//wr_slen write len, or 0 if reading + u8(*fatOpenFile)(FatFullRecord *rec, u32 wr_slen);//wr_slen is write len, or 0 if reading + u8(*fatReadFile)(void *dst, u32 slen); + u8(*fatWriteFile)(void *src, u32 slen); + u8(*fatCreateRecIfNotExist)(u8 *name, u8 is_dir); + u8(*fatSkipSectors)(u32 slen); +} OsRoutine; + + +extern OsRoutine *ed_os; +void edInit(); + + +//stereo dac. 8bit for each channel. +//any writing to this port enables dac output, any reading from this port disables dac output. +#define REG_DAC *((volatile u16*) (0xA1300E)) + + +//stuff for direct access to usb and spi. not recommended for use. OS function should be used for best compatibility +#define _SPI_SS 0 +#define _SPI_FULL_SPEED 1 +#define _SPI_SPI16 2 +#define _SPI_AREAD 3 + +//spi port (sd interface) +#define SPI_PORT *((volatile u16*) (0xA13000)) +#define SPI_CFG_PORT *((volatile u16*) (0xA13002)) + +//16bit or 8bit spi mode +#define SPI16_ON SPI_CFG_PORT |= (1 << _SPI_SPI16) +#define SPI16_OFF SPI_CFG_PORT &= ~(1 << _SPI_SPI16) + +//spi autoread. means that not need to write something to spi port before than read +#define SPI_AR_ON SPI_CFG_PORT |= (1 << _SPI_AREAD) +#define SPI_AR_OFF SPI_CFG_PORT &= ~(1 << _SPI_AREAD) + +//spi chip select +#define SPI_SS_OFF SPI_CFG_PORT |= (1 << _SPI_SS) +#define SPI_SS_ON SPI_CFG_PORT &= ~(1 << _SPI_SS) + +//spi speed. low speed need only for sd init +#define SPI_HI_SPEED_ON SPI_CFG_PORT |= (1 << _SPI_FULL_SPEED) +#define SPI_HI_SPEED_OFF SPI_CFG_PORT &= ~(1 << _SPI_FULL_SPEED) + +//usb-serial port +#define FIFO_PORT *((volatile u16*) (0xA1300A)) + +//spi and usb state +#define STATE_PORT *((volatile u16*) (0xA1300C)) +#define _STATE_SPI_READY 0 +#define _STATE_FIFO_WR_READY 1 +#define _STATE_FIFO_RD_READY 2 +#define IS_FIFO_RD_READY (STATE_PORT & (1 << _STATE_FIFO_RD_READY)) +#define IS_FIFO_WR_READY (STATE_PORT & (1 << _STATE_FIFO_WR_READY)) + + +#endif /* _EDOS_H */ + diff --git a/megaed-stop/main.c b/megaed-stop/main.c new file mode 100644 index 0000000..4fbac7a --- /dev/null +++ b/megaed-stop/main.c @@ -0,0 +1,291 @@ +/* + * This software is released into the public domain. + * See UNLICENSE file in top level directory. + */ +#include +#include + +#define u8 unsigned char +#define u16 unsigned short +#define u32 unsigned int + +#include "edos.h" + +#define GFX_DATA_PORT 0xC00000 +#define GFX_CTRL_PORT 0xC00004 + +#define TILE_MEM_END 0xB000 + +#define FONT_LEN 128 +#define TILE_FONT_BASE (TILE_MEM_END / 32 - FONT_LEN) + +/* note: using ED menu's layout here.. */ +#define WPLANE (TILE_MEM_END + 0x0000) +#define HSCRL (TILE_MEM_END + 0x0800) +#define SLIST (TILE_MEM_END + 0x0C00) +#define APLANE (TILE_MEM_END + 0x1000) +#define BPLANE (TILE_MEM_END + 0x3000) + +#define read8(a) \ + *((volatile u8 *) (a)) +#define read16(a) \ + *((volatile u16 *) (a)) +#define read32(a) \ + *((volatile u32 *) (a)) +#define write16(a, d) \ + *((volatile u16 *) (a)) = (d) +#define write32(a, d) \ + *((volatile u32 *) (a)) = (d) + +#define GFX_WRITE_VRAM_ADDR(adr) \ + (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00) +#define GFX_WRITE_VSRAM_ADDR(adr) \ + (((0x4000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x10) + +enum { + VDP_MODE1 = 0x00, + VDP_MODE2 = 0x01, + VDP_BACKDROP = 0x07, + VDP_MODE3 = 0x0b, + VDP_MODE4 = 0x0c, + VDP_AUTOINC = 0x0f, + VDP_SCROLLSZ = 0x10, +}; + +/* cell counts */ +#define LEFT_BORDER 1 /* lame TV */ +#define PLANE_W 64 +#define PLANE_H 32 +#define CSCREEN_H 28 + +static void VDP_drawTextML(const char *str, u16 plane_base, + u16 x, u16 y) +{ + const u8 *src = (const u8 *)str; + u16 basetile = 0; + int max_len = 40 - LEFT_BORDER; + int len; + u32 addr; + + x += LEFT_BORDER; + + for (len = 0; str[len] && len < max_len; len++) + ; + if (len > (PLANE_W - x)) + len = PLANE_W - x; + + addr = plane_base + ((x + (PLANE_W * y)) << 1); + write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr)); + + while (len-- > 0) { + write16(GFX_DATA_PORT, + basetile | ((*src++) - 32 + TILE_FONT_BASE)); + } +} + +static int printf_ypos; + +static void printf_line(int x, const char *buf) +{ + u32 addr; + int i; + + VDP_drawTextML(buf, APLANE, x, printf_ypos++ & (PLANE_H - 1)); + + if (printf_ypos >= CSCREEN_H) { + /* clear next line */ + addr = APLANE; + addr += (PLANE_W * (printf_ypos & (PLANE_H - 1))) << 1; + write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(addr)); + for (i = 0; i < 40 / 2; i++) + write32(GFX_DATA_PORT, 0); + + /* scroll plane */ + write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0)); + write16(GFX_DATA_PORT, (printf_ypos - CSCREEN_H + 1) * 8); + } +} + +#define PRINTF_LEN 40 + +static int printf(const char *fmt, ...) +{ + static const char hexchars[] = "0123456789abcdef"; + static int printf_xpos; + char c, buf[PRINTF_LEN + 11 + 1]; + const char *s; + va_list ap; + int ival; + u32 uval; + int d = 0; + int i, j; + + va_start(ap, fmt); + for (d = 0; *fmt; ) { + int prefix0 = 0; + int fwidth = 0; + + c = *fmt++; + if (d < PRINTF_LEN) + buf[d] = c; + + if (c != '%') { + if (c == '\n') { + buf[d] = 0; + printf_line(printf_xpos, buf); + d = 0; + printf_xpos = 0; + continue; + } + d++; + continue; + } + if (d >= PRINTF_LEN) + continue; + + if (*fmt == '0') { + prefix0 = 1; + fmt++; + } + + while ('1' <= *fmt && *fmt <= '9') { + fwidth = fwidth * 10 + *fmt - '0'; + fmt++; + } + + switch (*fmt++) { + case '%': + d++; + break; + case 'd': + case 'i': + ival = va_arg(ap, int); + if (ival < 0) { + buf[d++] = '-'; + ival = -ival; + } + for (i = 1000000000; i >= 10; i /= 10) + if (ival >= i) + break; + for (; i >= 10; i /= 10) { + buf[d++] = '0' + ival / i; + ival %= i; + } + buf[d++] = '0' + ival; + break; + case 'x': + uval = va_arg(ap, int); + while (fwidth > 1 && uval < (1 << (fwidth - 1) * 4)) { + buf[d++] = prefix0 ? '0' : ' '; + fwidth--; + } + for (j = 1; j < 8 && uval >= (1 << j * 4); j++) + ; + for (j--; j >= 0; j--) + buf[d++] = hexchars[(uval >> j * 4) & 0x0f]; + break; + case 's': + s = va_arg(ap, char *); + while (*s && d < PRINTF_LEN) + buf[d++] = *s++; + break; + default: + // don't handle, for now + d++; + va_arg(ap, void *); + break; + } + } + buf[d] = 0; + va_end(ap); + + if (d != 0) { + // line without \n + VDP_drawTextML(buf, APLANE, printf_xpos, + printf_ypos & (PLANE_H - 1)); + printf_xpos += d; + } + + return d; // wrong.. +} + +int main() +{ + OsRoutine *ed; + u8 seed, bad = 0; + u8 *p, val; + int i, t, len; + + ed = (OsRoutine *) *(u32 *)0x1A0; + ed->memInitDmaCode(); + + ed->VDP_setReg(VDP_MODE1, 0x04); + ed->VDP_setReg(VDP_MODE2, 0x64); + ed->VDP_setReg(VDP_AUTOINC, 2); + ed->VDP_setReg(VDP_SCROLLSZ, 0x01); + + /* clear name tables */ + write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(APLANE)); + for (i = 0; i < PLANE_W * PLANE_H / 2; i++) + write32(GFX_DATA_PORT, 0); + + write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(BPLANE)); + 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("\n"); + printf("MD version: %02x\n", read8(0xa10001)); + printf("ED os/fw: %d/%d\n\n", ed->osGetOsVersion(), + ed->osGetFirmVersion()); + + seed = read8(0xC00009); + p = (void *)0x200000; + len = 0x200000; + printf("filling SDRAM, seed=%02x.. ", seed); + + val = seed; + for (i = 0; i < len; i++) + p[i] = val++; + + printf("done.\n"); + + for (t = 1; ; t++) { + printf("executing stop.. "); + for (i = 0; i < 5 * 60; i++) + asm volatile("stop #0x2000"); + printf("done\n"); + + printf("checking memory..\n"); + + val = seed; + for (i = 0; i < len; i++) { + if (p[i] != val) { + printf("bad: %06x: got %02x, expected %02x\n", + &p[i], p[i], val); + bad = 1; + } + val++; + } + + printf("done. Try %d: test ", t); + if (bad) { + printf("FAILED\n"); + break; + } + printf("PASSED\n"); + } + + /* there are not enough STOP opcodes in this world :D */ + while (1) + asm volatile("stop #0x2000"); + + return 0; +} + +// vim:ts=4:sw=4:expandtab diff --git a/megaed-stop/sega_gcc.s b/megaed-stop/sega_gcc.s new file mode 100644 index 0000000..927da4e --- /dev/null +++ b/megaed-stop/sega_gcc.s @@ -0,0 +1,43 @@ + dc.l 0, 0x200, exc__, exc__, exc__, exc__, exc__, exc__ + dc.l exc__, exc__, exc__, exc__, exc__, exc__, exc__, exc__ + dc.l exc__, exc__, exc__, exc__, exc__, exc__, exc__, exc__ + dc.l exc__, exc__, exc__, exc__, HBL, exc__, VBL, exc__ + dc.l exc__, exc__, exc__, exc__, exc__, exc__, exc__, exc__ + dc.l exc__, exc__, exc__, exc__, exc__, exc__, exc__, exc__ + dc.l exc__, exc__, exc__, exc__, exc__, exc__, exc__, exc__ + dc.l exc__, exc__, exc__, exc__, exc__, exc__, exc__, exc__ + + .ascii "SEGA EVERDRIVE " + .ascii "MEGA-ED SDRAM vs STOP " + .ascii "MEGA-ED SDRAM vs STOP " + .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 +/* magic ED app init */ + move.w #0x0000, (0xA13006) + jmp init_ed.l +init_ed: + move.w #0x210f, (0xA13006) + move.l #HBL, (0x70) + move.l #VBL, (0x78) + + moveq #0, %d0 + movea.l %d0, %a7 + move %a7, %usp + bra main + +HBL: +VBL: +exc__: + rte + +# vim:filetype=asmM68k:ts=4:sw=4:expandtab