From: notaz Date: Fri, 24 Oct 2014 23:21:53 +0000 (+0300) Subject: megaed-stop-md - standalone test X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=megadrive.git;a=commitdiff_plain;h=25055eb3669856af621f38756215f56bdb1ce9e0 megaed-stop-md - standalone test --- diff --git a/megaed-stop-md/Makefile b/megaed-stop-md/Makefile new file mode 100644 index 0000000..56894ab --- /dev/null +++ b/megaed-stop-md/Makefile @@ -0,0 +1,40 @@ +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_md +OBJS = sega_gcc.o main.o data.o + +all: $(TARGET).bin + +$(TARGET).elf: $(OBJS) + $(LD) -o $@ -Tsega.ld -Map $(TARGET).map $^ $(LDLIBS) + +clean: + $(RM) $(TARGET).bin $(OBJS) $(TARGET).elf $(TARGET).map + $(RM) maketest test.bin + +%.bin: %.elf + $(OBJCOPY) -I elf32-m68k -O binary $^ $@ + +%.o: %.S + $(CC) -c -o $@ $^ $(ASFLAGS_CC) + +data.s: test.bin + +test.bin: + gcc -o maketest maketest.c + ./maketest + +rel: $(TARGET).bin + mkdir -p /tmp/$(TARGET)/src/ + cp $^ /tmp/$(TARGET)/ + $(MAKE) clean + cp -a * /tmp/$(TARGET)/src/ diff --git a/megaed-stop-md/UNLICENSE b/megaed-stop-md/UNLICENSE new file mode 100644 index 0000000..68a49da --- /dev/null +++ b/megaed-stop-md/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-md/data.s b/megaed-stop-md/data.s new file mode 100644 index 0000000..ceac4b8 --- /dev/null +++ b/megaed-stop-md/data.s @@ -0,0 +1,17 @@ +.section .rodata + +# I don't know why multiple different .align don't work.. +.align 0x8000 + +.global font_base +font_base: +.incbin "font.bin" + +.align 0x8000 +.global test_data +test_data: +.incbin "test.bin" +.global test_data_end +test_data_end: + +# vim:filetype=asmM68k:ts=4:sw=4:expandtab diff --git a/megaed-stop-md/font.bin b/megaed-stop-md/font.bin new file mode 100644 index 0000000..c11cc37 Binary files /dev/null and b/megaed-stop-md/font.bin differ diff --git a/megaed-stop-md/main.c b/megaed-stop-md/main.c new file mode 100644 index 0000000..618f3b5 --- /dev/null +++ b/megaed-stop-md/main.c @@ -0,0 +1,333 @@ +/* + * 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 + +#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 - FONT_LEN * 32) + +/* 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) +#define GFX_WRITE_CRAM_ADDR(adr) \ + (((0xC000 | ((adr) & 0x3FFF)) << 16) | ((adr) >> 14) | 0x00) + +#define VDP_setReg(r, v) \ + write16(GFX_CTRL_PORT, 0x8000 | ((r) << 8) | (v)) + +enum { + VDP_MODE1 = 0x00, + VDP_MODE2 = 0x01, + VDP_NT_SCROLLA = 0x02, + VDP_NT_WIN = 0x03, + VDP_NT_SCROLLB = 0x04, + VDP_SAT_BASE = 0x05, + VDP_BACKDROP = 0x07, + VDP_MODE3 = 0x0b, + VDP_MODE4 = 0x0c, + VDP_HSCROLL = 0x0d, + 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 / 32)); + } +} + +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.. +} + +extern u32 font_base[]; +extern u8 test_data[]; +extern u8 test_data_end[]; + +int main() +{ + volatile u32 *vptr; + u32 old; + u8 bad = 0; + u8 *p, val; + int i, t, len; + + /* z80 */ + write16(0xa11100, 0x100); + write16(0xa11200, 0); + + /* setup VDP */ + while (read16(GFX_CTRL_PORT) & 2) + ; + + VDP_setReg(VDP_MODE1, 0x04); + VDP_setReg(VDP_MODE2, 0x64); + VDP_setReg(VDP_MODE3, 0x00); + VDP_setReg(VDP_MODE4, 0x81); + VDP_setReg(VDP_NT_SCROLLA, APLANE >> 10); + VDP_setReg(VDP_NT_SCROLLB, BPLANE >> 13); + VDP_setReg(VDP_SAT_BASE, SLIST >> 9); + VDP_setReg(VDP_HSCROLL, HSCRL >> 10); + VDP_setReg(VDP_AUTOINC, 2); + VDP_setReg(VDP_SCROLLSZ, 0x01); + VDP_setReg(VDP_BACKDROP, 0); + + /* 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); + + /* SAT, h. scroll */ + write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(SLIST)); + write32(GFX_DATA_PORT, 0); + + write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(HSCRL)); + write32(GFX_DATA_PORT, 0); + + /* scroll plane vscroll */ + write32(GFX_CTRL_PORT, GFX_WRITE_VSRAM_ADDR(0)); + write32(GFX_DATA_PORT, 0); + printf_ypos = 0; + + /* load font */ + write32(GFX_CTRL_PORT, GFX_WRITE_VRAM_ADDR(TILE_FONT_BASE)); + for (i = 0; i < FONT_LEN * 32 / 4; i++) + write32(GFX_DATA_PORT, font_base[i]); + + /* set colors */ + write32(GFX_CTRL_PORT, GFX_WRITE_CRAM_ADDR(0)); + write32(GFX_DATA_PORT, 0); + write32(GFX_CTRL_PORT, GFX_WRITE_CRAM_ADDR(15 * 2)); // font + write16(GFX_DATA_PORT, 0xeee); + + printf("\n"); + printf("MD version: %02x\n", read8(0xa10001)); + printf("ROM witable? "); + + vptr = (void *)0x120; + old = *vptr; + *vptr ^= ~0; + printf(*vptr == old ? "no" : "yes"); + printf("\n\n"); + + p = test_data; + len = test_data_end - test_data; + + for (t = 1; ; t++) { + printf("executing stop.. "); + for (i = 0; i < 5 * 60; i++) + asm volatile("stop #0x2000" ::: "cc"); + printf("done\n"); + + printf("checking memory..\n"); + + val = 0; + 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"); + } + + write32(GFX_CTRL_PORT, GFX_WRITE_CRAM_ADDR(0)); + write16(GFX_DATA_PORT, 0x008); + + /* there are not enough STOP opcodes in this world :D */ + while (1) + asm volatile("stop #0x2000" ::: "cc"); + + return 0; +} + +// vim:ts=4:sw=4:expandtab diff --git a/megaed-stop-md/maketest.c b/megaed-stop-md/maketest.c new file mode 100644 index 0000000..9ebe038 --- /dev/null +++ b/megaed-stop-md/maketest.c @@ -0,0 +1,17 @@ +#include + +int main() +{ + FILE *f; + int i; + + f = fopen("test.bin", "wb"); + if (f == NULL) + return 1; + + for (i = 0; i < 0x200000 - 0x10000; i++) + fwrite(&i, 1, 1, f); + fclose(f); + + return 0; +} diff --git a/megaed-stop-md/sega.ld b/megaed-stop-md/sega.ld new file mode 100644 index 0000000..0921912 --- /dev/null +++ b/megaed-stop-md/sega.ld @@ -0,0 +1,117 @@ +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 = 0x00000000, LENGTH = 0x00400000 + ram : ORIGIN = 0x00ff0000, LENGTH = 0x00010000 +} + +/* + * 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 0x00000000: + { + *(.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) + + __FINI_SECTION__ = . ; + *(.fini) + SHORT (0x4e75) +*/ + _etext = .; + *(.lit) + } > rom + + .data 0xff0000 : + { + *(.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-md/sega_gcc.s b/megaed-stop-md/sega_gcc.s new file mode 100644 index 0000000..5619334 --- /dev/null +++ b/megaed-stop-md/sega_gcc.s @@ -0,0 +1,41 @@ + dc.l 0, RST, 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 " + .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 + + move.b (0xA10001), %d0 + andi.b #0x0F, %d0 + beq.s 0f + move.l #0x53454741, (0xA14000) /* 'SEGA' */ +0: + moveq.l #0, %d0 + movea.l %d0, %a7 + move.l %a7, %usp + bra main + +HBL: +VBL: +exc__: + rte + +# vim:filetype=asmM68k:ts=4:sw=4:expandtab