From d3df23a43d39dba9126e4a2a332cfe0c10d36cd8 Mon Sep 17 00:00:00 2001 From: Kitty-Cricket Piapiac Date: Fri, 17 Mar 2023 14:25:20 -0700 Subject: [PATCH] restructure --- .gitignore | 1 + README | 16 +++++++++------- a.c | 9 +++++++++ t/t.h => a.h | 24 ------------------------ t/kmm32-as.c => as.c | 2 +- kip-sdl2.c | 24 +++++++++++------------- kip.c | 2 +- kip.h | 13 ++++++------- kmm32.c | 11 +++++------ meson.build | 26 +++++++++++++++----------- t/README | 5 +---- t/meson.build | 3 --- t/{tt.c => u.c} | 2 +- t/t.c => u.c | 15 +-------------- u.h | 24 ++++++++++++++++++++++++ 15 files changed, 85 insertions(+), 92 deletions(-) create mode 100644 a.c rename t/t.h => a.h (50%) rename t/kmm32-as.c => as.c (99%) delete mode 100644 t/meson.build rename t/{tt.c => u.c} (97%) rename t/t.c => u.c (72%) create mode 100644 u.h diff --git a/.gitignore b/.gitignore index 9a9c68a..535a74c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.o *.rom /build +/b diff --git a/README b/README index 34a2dfd..0cd0165 100644 --- a/README +++ b/README @@ -5,11 +5,13 @@ depends c11 compiler ninja sdl2 build meson compile -C b +test meson test -C b usage ./b/kip [ROM FILE] - ./b/t/kmm32-as [ASM FILE] [ROM FILE] -test ./b/t/tt -dirs ./d/ -- documentation - ./t/ -- tools - ./x/ -- examples -contributions are welcome and encouraged -patches, questions, comments --> kitty+kip@piapiac.org + ./b/as [ASM FILE] [ROM FILE] +layout ./kip* -- sdl2-based emulator + ./as -- assembler + ./d/ -- documentation + ./t/ -- tests + ./x/ -- examples +contributions, questions, comments are welcome and encouraged +send --> kitty+kip@piapiac.org diff --git a/a.c b/a.c new file mode 100644 index 0000000..a49029f --- /dev/null +++ b/a.c @@ -0,0 +1,9 @@ +#include"a.h" +#undef malloc +#undef realloc +#undef free +NR V die(IM C*f,...)VA(f,vfprintf(stderr,f,ap),fputc('\n',stderr),exit(1)) +V asrt(B b,IM C*m){N(b,die(m))} +V*mk(W sz){V*p;N(p=calloc(1,sz),die("oom"))R p;} +V*remk(V*p,W sz){N(p=realloc(p,sz),die("oom"))R p;} +V del(V*p){free(p);} diff --git a/t/t.h b/a.h similarity index 50% rename from t/t.h rename to a.h index 476279a..c6d28a0 100644 --- a/t/t.h +++ b/a.h @@ -12,7 +12,6 @@ #define VA(a,...){va_list ap;va_start(ap,a);__VA_ARGS__;va_end(ap);} #define WH(a,...)while((a)){__VA_ARGS__;} #define _ static - typedef void V; typedef int I; typedef char C; @@ -28,26 +27,3 @@ V del(V*); #define malloc B use_mk_instead[-1] #define realloc B use_remk_instead[-1] #define free B use_del_instead[-1] - -typedef struct{W z,c,l;B*p;}Vec; -#define Vget(T,v,i)*(T*)vget((v),i) -#define Vfor(T,v,x,i,...)for(W i=0;i<(v).l;++i){T x=Vget(T,v,i);__VA_ARGS__;} -V vini(Vec*,W,W); -V vgrw(Vec*,W); -V vput(Vec*,V*); -V*vget(Vec,W); - -typedef struct{W l;B*p;}S; -#define Sl(s)(S){SZ(s)-1,(B*)(s)} -#define Sfor(s,x,i,...)W i;for(i=0;i<(s).l;++i){B x=s.p[i];__VA_ARGS__;} -B seql(S,S); -W fnv1a(S); - -typedef struct{W z,c,l;S*k;B*v;}Ht; -#define Hget(T,h,i)(T*)hget((h),(i)) -#define Hfor(h,n,v,i,...)for(W i=0;i<(h).c;++i){S n=(h).k[i];Q(k.l,B*v=(B*)((h).v+i*(h).z);__VA_ARGS__;)} -V hini(Ht*,W,W); -V hgrw(Ht*,W); -V hput(Ht*,S,V*); -W hind(Ht,S); -V*hget(Ht,S); diff --git a/t/kmm32-as.c b/as.c similarity index 99% rename from t/kmm32-as.c rename to as.c index dc09f72..c8fc23c 100644 --- a/t/kmm32-as.c +++ b/as.c @@ -1,4 +1,4 @@ -#include"t.h" +#include"u.h" #define MEMSZ 0x40000 _ B*inb,meb[MEMSZ]; diff --git a/kip-sdl2.c b/kip-sdl2.c index a9c6345..d24248b 100644 --- a/kip-sdl2.c +++ b/kip-sdl2.c @@ -1,17 +1,13 @@ #include #include"kip.h" +#define hig 480 +#define wid 640 SDL_Window*win; SDL_Surface*surf; SDL_Renderer*ren; SDL_Texture*tex; -#define err printf("sdl:%s\n",SDL_GetError()),exit(1) -#define hig 480 -#define wid 640 -#define use(x)(void)(x) -#define sz sizeof - _ W palette[0x10]={0x212425,0x212425,0x86215f,0xc33479,0x392874,0x0a5c9f,0xa0c5c6,0x0e8743, 0xe67216,0xe1202d,0xeb5b2b,0xed7275,0xb1739a,0xbf98ac,0xf3e46d,0xfef6dd}; _ B layer[4][wid*hig]={0},redraw; @@ -20,13 +16,15 @@ _ W computed[wid*hig]={0}; _ Ws min(Ws x,Ws y){R xy?x:y;} +_ NR V err(V){die("sdl:%s\n",SDL_GetError());} + V front_init(V){ - Q(0!=SDL_Init(SDL_INIT_VIDEO),err) + Q(SDL_Init(SDL_INIT_VIDEO),err()) SDL_ShowCursor(SDL_DISABLE); - Q(!(win=SDL_CreateWindow("kip emulator",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,wid,hig,0)),err) - Q(!(ren=SDL_CreateRenderer(win,-1,0)),err) - Q(0!=SDL_RenderSetLogicalSize(ren,wid,hig),err) - Q(!(tex=SDL_CreateTexture(ren,SDL_PIXELFORMAT_XRGB8888,SDL_TEXTUREACCESS_STREAMING,wid,hig)),err) + N(win=SDL_CreateWindow("kip emulator",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,wid,hig,0),err()) + N(ren=SDL_CreateRenderer(win,-1,0),err()) + Q(SDL_RenderSetLogicalSize(ren,wid,hig),err()) + N(tex=SDL_CreateTexture(ren,SDL_PIXELFORMAT_XRGB8888,SDL_TEXTUREACCESS_STREAMING,wid,hig),err()) redraw=1;} _ H mx,my,px,py; @@ -42,7 +40,7 @@ V front_events(V){_ SDL_Event ev;W t; V front_redraw(V){W i,j,k; Q(redraw,for(i=0;i>2)?0:(B)c&0xf;)} _ V out_1bpp(B l,W c,H x,H y){W i,j,k;for(i=0;i<8;++i)for(j=0,k=lob(blit+1*i);j<8;k<<=1,++j)out_pixel(l,(c&0xf)*(1&(k>>7)),x+j,y+i);} _ V out_2bpp(B l,W c,H x,H y){W i,j,k;for(i=0;i<8;++i)for(j=0,k=loh(blit+2*i);j<8;k<<=2,++j)out_pixel(l,0xf&(c>>4*(3&(k>>14))),x+j,y+i);} -_ V out_4bpp(B l,W c,H x,H y){W i,j,k;for(i=0;i<8;++i)for(j=0,k=low(blit+4*i);j<8;k<<=4,++j)out_pixel(l,(k>>28),x+j,y+i);use(c);} +_ V out_4bpp(B l,W c,H x,H y){W i,j,k;for(i=0;i<8;++i)for(j=0,k=low(blit+4*i);j<8;k<<=4,++j)out_pixel(l,(k>>28),x+j,y+i);(V)c;} _ V(*outs[])(B,W,H,H)={out_pixel,out_1bpp,out_2bpp,out_4bpp}; DEV(display_pos,(px=x>>16,py=x),(R px<<16|py)) diff --git a/kip.c b/kip.c index b8f2702..6a57d61 100644 --- a/kip.c +++ b/kip.c @@ -17,7 +17,7 @@ int main(int ac,char*av[]){ #undef X f=fopen(av[1],"rb"),fseek(f,0,SEEK_END),l=ftell(f),fseek(f,0,SEEK_SET), - b=malloc(l+1),fread(b,1,l,f),fclose(f),b[l]=0,lod(b,l),front_init(); + b=mk(l+1),fread(b,1,l,f),fclose(f),b[l]=0,lod(b,l),front_init(); for(;;)front_events(),emu(STEP),front_redraw(); front_deinit(); } diff --git a/kip.h b/kip.h index 4e0e152..5270f1a 100644 --- a/kip.h +++ b/kip.h @@ -1,13 +1,12 @@ #include"kmm32.h" - -extern V front_init(V); -extern V front_events(V); -extern V front_redraw(V); -extern V front_deinit(V); - +#include"a.h" #define D(...){__VA_ARGS__;} #define DEV(n,i,o)_ V n##io(W x)D i _ W n##ii(V)D o IoDevice io_##n={n##io,n##ii}; #define X(x)extern IoDevice x; #include"kip-io.def" -#undef B #undef X +V fatal(IM C*,...); +V front_init(V); +V front_events(V); +V front_redraw(V); +V front_deinit(V); diff --git a/kmm32.c b/kmm32.c index 64bb56a..779e181 100644 --- a/kmm32.c +++ b/kmm32.c @@ -1,15 +1,17 @@ #include"kmm32.h" +#define A a=pop() +#define AB b=pop(),A +#define DS ((W*)(mem+(MEMSZ-0x800))) +#define RS ((W*)(mem+(MEMSZ-0x400))) B dp,rp,mem[MEMSZ]; W ip; -#define DS ((W*)(mem+(MEMSZ-0x800))) -#define RS ((W*)(mem+(MEMSZ-0x400))) - _ W pop(V){R DS[--dp];} _ W por(V){R RS[--rp];} _ V put(W i){DS[dp++]=i;} _ V pur(W i){RS[rp++]=i;} + B lob(W i){R mem[i];} H loh(W i){R lob(i)<<8|lob(i+1);} W low(W i){R loh(i)<<16|loh(i+2);} @@ -19,8 +21,6 @@ V stw(W i,W w){sth(i,w>>16),sth(i+2,w);} V vec(W i){Q(0x100<(i=low(i)),pur(ip),ip=i)} _ W a,b; -#define A a=pop() -#define AB b=pop(),A _ V np(V){} _ V ex(V){ip+=0x40001;} _ V pb(V){put(lob(ip)),ip+=1;} @@ -65,6 +65,5 @@ _ V(*ops[])(V)= /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37*/ {np,ex,pb,ph,pw,fb,fh,fw,mb,mh,mw,io,ii,md,mr,dd,dr,du,sw,ov,ad,su,mu,di,an,or,xr,sl,sr,sa,eq,lt,gt,no,ju,jc,ca,cc}; -#include V emu(W c){while(c--&&ip<0x40000)ops[mem[ip++]]();} V lod(B*c,W l){for(ip=0;ipp=remk(v->p,c*v->z),v->c=c;} V vput(Vec*v,V*a){Q(v->l>=v->c,vgrw(v,(v->c*3)/2))memcpy(vget(*v,v->l++),a,v->z);} V*vget(Vec v,W i){R v.p+(v.z*i);} - B seql(S a,S b){Q(a.l!=b.l,R 0)Sfor(a,c,i,Q(c!=b.p[i],R 0))R 1;} - W fnv1a(S s){W h=16777619;Sfor(s,c,i,h^=c,c*=2166136261)R h;} - V hini(Ht*h,W c,W z){*h=(Ht){z,c,0,mk(c*SZ(S)),mk(c*z)};} V hgrw(Ht*h,W c){Ht j=*h;h->k=mk(c*SZ(S)),h->v=mk(c*h->z),h->c=c;Hfor(j,k,v,i,hput(h,k,v));del(j.k),del(j.v);} V hput(Ht*h,S k,V*v){W c;W i=hind(*h,k);Q((c=h->c*3/2)l++,hgrw(h,c))h->k[i]=k,memcpy(h->v+i*h->z,v,h->z);} diff --git a/u.h b/u.h new file mode 100644 index 0000000..02a750c --- /dev/null +++ b/u.h @@ -0,0 +1,24 @@ +#include"a.h" + +typedef struct{W z,c,l;B*p;}Vec; +#define Vget(T,v,i)*(T*)vget((v),i) +#define Vfor(T,v,x,i,...)for(W i=0;i<(v).l;++i){T x=Vget(T,v,i);__VA_ARGS__;} +V vini(Vec*,W,W); +V vgrw(Vec*,W); +V vput(Vec*,V*); +V*vget(Vec,W); + +typedef struct{W l;B*p;}S; +#define Sl(s)(S){SZ(s)-1,(B*)(s)} +#define Sfor(s,x,i,...)W i;for(i=0;i<(s).l;++i){B x=s.p[i];__VA_ARGS__;} +B seql(S,S); +W fnv1a(S); + +typedef struct{W z,c,l;S*k;B*v;}Ht; +#define Hget(T,h,i)(T*)hget((h),(i)) +#define Hfor(h,n,v,i,...)for(W i=0;i<(h).c;++i){S n=(h).k[i];Q(k.l,B*v=(B*)((h).v+i*(h).z);__VA_ARGS__;)} +V hini(Ht*,W,W); +V hgrw(Ht*,W); +V hput(Ht*,S,V*); +W hind(Ht,S); +V*hget(Ht,S);