From b0e61a103125d4eee18c802d4b5612d13722c564 Mon Sep 17 00:00:00 2001 From: Robin Krens Date: Tue, 7 Jun 2022 21:55:39 +0200 Subject: [PATCH] refactor: use for ia86-elf-gcc toolchain + linker file - use of gas style assembly in entry.S - copy to RAM func - jump to C - main.c tests for .bss (global) and .data data - makefile changes for toolchain - added swan.ld linker file to make layout sections easier --- Makefile | 60 +++++++++------- romheader | Bin 0 -> 17200 bytes src/entry.S | 54 +++++++++++++++ src/init.s | 222 ------------------------------------------------------------ src/main.c | 122 ++++++++++----------------------- swan.ld | 26 +++++++ 6 files changed, 150 insertions(+), 334 deletions(-) create mode 100755 romheader create mode 100644 src/entry.S delete mode 100644 src/init.s create mode 100644 swan.ld diff --git a/Makefile b/Makefile index de9a494..f949274 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,39 @@ -# simple makefile -INC=./inc -CC=bcc -LD=ld86 -AS=nasm -MED=mednafen - -#$(LD) -M src/*.o - -# -d delete headers -# -s strip symbols -all: - $(LD) -T0x0 -D0xF000 src/*.o -d -s -o test.wsc - -# use -x to for -ffreestanding -c-files: - $(CC) -0 -c src/*.c -I$(INC) -init: - $(AS) -fas86 src/*.s -i $(INC) +# +# makefile for swan-sdk +# -add: - cp test.wsc tools/ - cd tools && ./romheader +OUT = test.wsc +CC = ia16-elf-gcc +LD = ia16-elf-ld +ODIR = obj +ADIR = objs +SDIR = src +INC = -Iinc -run: - $(MED) tools/test.wsc +CFLAGS = -march=v30 -ffreestanding # use NEC v30 chip -clean: - rm -rf src/*.o test.wsc +_OBJS = entry.o main.o # todo: make sure entry is forced as entry point for linker + +OBJS = $(patsubst %,$(ODIR)/%,$(_OBJS)) + +LDFILE = swan.ld + + +$(ODIR)/%.o: $(SDIR)/%.c + $(CC) -c $(INC) -o $@ $< $(CFLAGS) +$(ODIR)/entry.o: $(SDIR)/entry.S + $(CC) -c $(INC) -o $@ $< $(CFLAGS) +$(OUT): $(OBJS) + $(LD) -T $(LDFILE) $^ -o $(OUT) + +add: + ./romheader +run: + mednafen test.wsc + +.PHONY: clean + +clean: + rm -f $(ODIR)/*.o $(OUT) diff --git a/romheader b/romheader new file mode 100755 index 0000000000000000000000000000000000000000..19a4bafd81b2d40885585dc08cdb479dbdcd3a86 GIT binary patch literal 17200 zcmeHOeQ+Da6<=9);v|mjKvJAQ@im0j0j$`;4o<*D@>kBO4f(M77zh_xI$Ij_g>-5h zhEivoFsO+_`d8Cw+J-+mW!nA`7^Vn$bV{2{JB5~XFbp3O1H_2JKue;&x3}+f zI!mR~cIZF19_j7-y|2A@d#hdF?zj5}`>PxdM#ag#&Nz3mj(CJ1em!Y0=3$*|F?=_% z%h^1DtKs7jVk>>QN*55V6?#sfXxE69GW0SXW<;rlNYSoRnGgYpvIbof?P{f_ zr$k9^#FNk%ip0MSI)e&Oh??y%H^CMzX2Gsf`lQIqw4h{%EZR*7y9r@ObXLS8O8La* z637kLNvpj(lKD)WL%G)FqI|I>tdA z{rY!9t=NsvsK1MFpu*Qr0GG{DunnPzC&+3V2%u{5aqee5_>=0Oi_= zSUG%E1^lWCcvA)Zjtcl`z$N%t%P0UVtswHykNnte*0Hg%a4H#OiV{zRqS~0^58lnx ziIB#|VsSOf;%XuhORzCb4To7gr6oZ#srmeOqoOkA3x!!kjU-jj_lILim5uF8gftbj z{1ZN9EEM&HL-!(!5z#WCDG^^N$~F!TboVH0-PgL?je4=R*4@FBf#D$~peEGuP*PJ9 z!$UpcSX3SMjfTvQ$JK|ryQOEI_!uC*^ zG2!O%CTqee2HMP;@Op9r^b-?K*NWmvfzO9JW`)XwCY;6)lpQqTI9^aWWWsR_qH@H9 zV+<;fm~cp_QI4AMg$ByllO`OWK`PTGypBMiFPLz+fkruP!d(VxSL_Vf8L%^8XTZ*Y zoq_*n20pC6{O|JAyS4JZnm?~$Ox`=KRTWOlQzvUr@MaY{HUXS3T=@+k^-T=*gDA=8 zUMmy|2RV-usC@2>!Q;dxpF3{wIHAes9yNHJ$mDYm8$3>6^11ICJWjmwx!n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O0H}xuT@S{{Az?9zGZ}JWe^pr~Y zxf6ikJd@HV3lDPdR;Fwmyt&&usNCG&zpZar8TNJ$_MumPTE(AC@uvRElPGv5#^a&W zEd1Px8t`d8aURAZvYeeMO54RTnj*qoS&z+F#TZFH1*0F)vC2zN$tZzjObPm;0X#u7 za5ESNLQy4^R3UH0OU9IoV?kDTqsgR*#?SDW4-FMn@k9?lJXD16TJ)1dr5GMGDn<0; zL#3Faz}X+V;dm&h>`9-}6Gx>Um^-;EqWMOFY6+bNsRpN>)I^-Q;RTcG9*?Hn@dUhQ zN@%-G&}b?IXYN7)0eQOzS~TA{E>_~nH!$d2t0em5#Yb7;fw}{LKt3nX%zEdqO|T4Vy;Vdq*uTP*IHyx^KPON$jpV- zSA?yDirM})5uYf&cQY6I9U*K0DrS3{#}k!EQBbS>t$=?FYc*;=&G(5?e6g%E*1zP| z2Nhh)l0B^ph~n=r7HlI{>H2Bi83GJ1ne1u(K=d7unTv2|KzJ?^*P>)k>kOhRg(kJ1 z^oZUMa$MUIPwNq)9${~7zkpc=DwtEUr*#X_X`x8*t=IoOVc#wEXnjMJ*86CWIkVb7 z3>fBw{Nt2>7eTEvcI*8=49aGEN!Ssc*1Z|u7WeF@7W)~YK$P^!0r5nSTI^~4MHJ=c zLUu%P9&K(v&2NaFq8lTekRADa)?!cVIimD`Bht6#59enX-v}cKO+kHvC1SOI5d>!Y zfUqOlONN44?O(Cj(>ju9CptG3>OTa%2^HAi&YwZVXnjHFyRG`f6a4^e>P_~vE{(UK zw_<gX9e3L!Vd5|AW z5yfX7q1vibNw7pHF7b6>jK0WT^5_yt*i#(Rx7-f4M8p^Bq{hhpY6H +# Date : 07.06.2022 +# Last Modified Date: 07.06.2022 +# Last Modified By : Robin Krens + +.org 0 +.code16 + +.extern main +.extern _stext +.extern _etext +.extern _sdata +.extern _edata +.global _start + +_start: + # reset flags and registers + cli + cld + xor %ax,%ax + mov %ax,%bx + mov %ax,%bp + mov %ax,%ss + + # set stack pointer for wonderswan + # color + mov $0xFE00 - 2,%ax + mov %ax,%sp + +copy_to_ram: + mov $_etext, %ax + # set data segment to ROM bank + mov $0x2000, %bx + mov %bx, %ds + mov %ax, %si + mov $_sdata, %bx + mov %bx, %di + # calculate size + mov $_edata, %ax + mov $_sdata, %bx + sub %bx, %ax + mov %ax, %cx + rep movsb + # reset data segment to RAM + xor %ax, %ax + mov %ax, %ds + + # jump to C + call main + + # should not be here +loop: nop + jmp loop diff --git a/src/init.s b/src/init.s deleted file mode 100644 index cb1bb03..0000000 --- a/src/init.s +++ /dev/null @@ -1,222 +0,0 @@ - -%include "WonderSwan.inc" - -extern _main -extern _test_copy -extern __etext -extern __seg1DL -extern __seg1DH -extern __edata -extern __end - -MYSEGMENT equ 0xF000 -USER_RAM equ 0x1000 - -; Set Maps and Sprite Table at the end of first part of ram -fgmap equ WSC_TILE_BANK1-MAP_SIZE -bgmap equ fgmap-MAP_SIZE -sprtable equ bgmap-SPR_TABLE_SIZE - - - cpu 186 - bits 16 - - ;PADDING 15 ; Pad 15 Sections - - section .text - - ;ORG 0x0000 - -..start: - cli ; Disable Interrupts - cld ; Offset in Increment mode - - xor ax,ax ; Clear all registers - mov bx,ax - mov cx,ax - mov dx,ax - mov si,ax - mov di,ax - mov ds,ax ; Clear Segments - mov es,ax - - mov bp,ax ; Setup stack - mov ss,ax - mov sp,WS_STACK - - in al,IO_HARDWARE_TYPE ; Check Wonderswan Mono/Color - test al,WS_COLOR -.mono: jz .mono ; We loop forever if Mono - - mov sp,WSC_STACK ; New stack - - ;call _main - - xor ax,ax ; Clear Ram - mov di,0x100 - mov cx,0x7E80 - rep stosw - - out IO_SRAM_BANK,al - - - ;mov cx, 0x100 - - ; Video Init - in al,IO_VIDEO_MODE - or al,VMODE_16C_CHK | VMODE_CLEANINIT - out IO_VIDEO_MODE,al - - xor ax,ax - - out IO_BG_X,al - out IO_BG_Y,al - out IO_FG_X,al - out IO_FG_Y,al - - out IO_SPR_START,al - out IO_SPR_STOP,al - - mov al,BG_MAP(bgmap) | FG_MAP(fgmap) - out IO_FGBG_MAP,al - - mov al,SPR_TABLE(sprtable) - out IO_SPR_TABLE,al - - in al,IO_LCD_CTRL - or al,LCD_ON - out IO_LCD_CTRL,al - - xor al,al - out IO_LCD_ICONS,al - - ; Setup VBlank Int - mov ax,INT_BASE - out IO_INT_BASE,al - - mov di,INTVEC_VBLANK_START - add di,ax - shl di,2 ; *4 - mov word [es:di],VBlank - mov word [es:di+0x2],MYSEGMENT - - xor ax,ax ; Clear HBL & Timer - out IOw_HBLANK_FREQ,ax - out IO_TIMER_CTRL,al - - dec al ; Ack all Int - out IO_INT_ACK,al - - mov al,INT_VBLANK_START ; Enable VBL Int - out IO_INT_ENABLE,al - - sti ; Enable Interrupts ! - -;-------------------------------- - - ; Set Segments for Copy - mov ax,0x2000 ; ! note, try 0x2000, 0x4000, etc.. - mov ds,ax - xor ax,ax - mov es,ax - - mov si, __seg1DL - mov di, USER_RAM - mov ax, __seg1DH - mov bx, __seg1DL - sub bx, ax - mov cx, ax - rep movsb - ; Make BG - - mov si,TileGfx - mov di,WSC_TILE_BANK1 - mov cx,(TileGfxE-TileGfx)/2 - rep movsw - - ;mov si,TilePal - ;mov di,WSC_PALETTES - ;mov cx,32 - ;rep movsb -;back: - call _main - ;call _main - ;call _test_copy - ;call _test_copy - ;jmp back - - mov ax,BG_CHR(0,0,0,0,0) ; Initial Tile, Pal 0, Bank 0 - mov di,bgmap - mov cx,MAP_TWIDTH*MAP_THEIGHT - rep stosw - - ; Display ! - mov al,BG_ON | FG_ON - out IO_DISPLAY_CTRL,al - -blaat: - jmp blaat - - ; Loop Read Buttons -loop: - mov al,KEYPAD_READ_BUTTONS - out IO_KEYPAD,al - nop - nop - nop - nop - in al,IO_KEYPAD - - test al,PAD_A ; Press A for Audio - jz loop - - call _main - jmp loop - -;****************************************** - -VBlank: - push ax - - mov al,[es:VBLcnt] - inc al - mov [es:VBLcnt],al - and al,3 - jnz .noscroll - - in al,IO_BG_X ; Infinite Scroll - inc al - out IO_BG_X,al - - in al,IO_BG_Y - inc al - out IO_BG_Y,al - -.noscroll: - in al,IO_INT_ACK - or al,INT_VBLANK_START - out IO_INT_ACK,al - - pop ax - iret - -;****************************************** - - align 2 - -TilePal: incbin "gfx/bgtile.pal" -TileGfx: incbin "gfx/bgtile.gfx" -TileGfxE: - -;****************************************** - - ;ROM_HEADER __end, MYSEGMENT, 0x42, RH_WS_COLOR, RH_ROM_8MBITS, RH_NO_SRAM, RH_HORIZONTAL - -;****************************************** - - ;section .bss - - ;start=0x0100 ; Keep space for Int Vectors - -VBLcnt: resb 0 ; Our datas in RAM - diff --git a/src/main.c b/src/main.c index 3763f3c..502d63c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,101 +1,51 @@ -/* #include */ -#include -void outport(c) - unsigned c; -{ -#asm - xor dx, dx - mov dx, ax - shr dx, 8 - out dx, al - ;out 0x15, al -#endasm -} - -static unsigned char data[] = { -0x65, 0x06, 0x67, 0x06, 0x79, 0x07, 0x8c, 0x08, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, -0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f +/* sprite data */ +unsigned char bgtile_gfx[] = { + 0x31, 0x22, 0x31, 0x32, 0x22, 0x31, 0x23, 0x21, 0x13, 0x13, 0x31, 0x32, + 0x32, 0x31, 0x23, 0x12, 0x21, 0x32, 0x13, 0x23, 0x23, 0x13, 0x31, 0x31, + 0x12, 0x32, 0x13, 0x22, 0x23, 0x13, 0x22, 0x13 }; -static unsigned char data2[] = { -0x65, 0x06, 0x67, 0x06, 0x79, 0x07, 0x8c, 0x08, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, -0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f +/* palette data */ +unsigned char bgtile_pal[] = { + 0x65, 0x06, 0x67, 0x06, 0x79, 0x07, 0x8c, 0x08, 0xff, 0x0f, 0xff, 0x0f, + 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, + 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f }; -static unsigned char data3[] = { -0x65, 0x06, 0x67, 0x06, 0x79, 0x07, 0x8c, 0x08, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, -0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f -}; - +/* data */ +unsigned char lol[5] = { 0xAA, 0x2, 0x3, 0x4, 0x5}; +unsigned char lol2[5] = { 0x1, 0x2, 0x3, 0x4, 0x5}; +unsigned char lol3[5] = { 0x1, 0x2, 0x3, 0x4, 0x5}; +#define PALETTE (*(volatile unsigned *)0xFE00) +#define PALETTE_T (volatile unsigned *)0xFE00 +#define SPRITE_T (volatile unsigned *)0x4000 -unsigned short *memcpy(dest, src, cnt) - unsigned short *dest; - unsigned short *src; - int cnt; -{ - - int i; -#asm - ;push ds - ;push es - mov ax, 0x0 - mov ds, ax - xor ax, ax - mov es, ax -#endasm - for (i = 0; i < cnt; ++i) { - *dest++ = *src++; - //*dest++ = 0x66; - } -#asm - ;pop es - ;pop ds - mov ds, 0xF000 -#endasm - return dest; -} - -void test_copy() -{ -#asm - ;mov ds, 0 -#endasm - -// unsigned short dest = 0xFE00; -// unsigned * ptr = dest; -// *ptr++ = 0xDEAD; -// *ptr++ = 0xAAAA; -// *ptr++ = 0xDEAD; - memcpy(0xFE00, 0x1000, 16); -#asm - ;mov ds, 0xF000 -#endasm - //wmemcpy(WSC_PALETTES, 0x2F000, 32); - return; -} +/* bss */ +static unsigned char blaat[100]; -int main() +int main(void) { - unsigned short data_seg = 0xF000; - unsigned short data_ram = 0x1000; - unsigned short rel_ram = data_seg - data_ram; + lol[0] = 0xDD; + lol[1] = 0xDD; - /* dummy */ - memcpy(0x0, 0x0, 0); - - memcpy(0xFE00, data - rel_ram, 16); - //memcpy(0xFE00, data_seg + data, 16); + blaat[0] = 0xEE; + blaat[1] = 0xEE; + blaat[32] = 0xEE; - //while(1) { - // /* outport(0x15EE); */ - // unsigned reg = 0x15; - // unsigned val = 0xEE; - // outport((reg << 8) | val); - - //} + unsigned char * ptr = PALETTE_T; + for (int i = 0; i < sizeof(bgtile_pal); ++i) { + *ptr++ = bgtile_pal[i]; + } + + ptr = SPRITE_T; + for (int i = 0; i < sizeof(bgtile_gfx); ++i) { + *ptr++ = bgtile_gfx[i]; + } + while(1) { + } return 0; } diff --git a/swan.ld b/swan.ld new file mode 100644 index 0000000..e3c7449 --- /dev/null +++ b/swan.ld @@ -0,0 +1,26 @@ +OUTPUT_FORMAT(binary) +ENTRY(_start) +SECTIONS +{ + . = 0x0; + .text : + { + _stext = .; + *(.text); + _etext = .; + } + . = 0x1000; + .data : AT(_etext) + { + _sdata = .; + *(.data); + _edata = .; + *(.bss); + *(.rodata); + } + _heap = ALIGN(4); + /DISCARD/ : + { + *(.eh_frame) + } +} -- 2.7.4