diff --git a/a.c b/a.c index a49029f..ac23ecd 100644 --- a/a.c +++ b/a.c @@ -2,8 +2,10 @@ #undef malloc #undef realloc #undef free +#undef fopen 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);} +F xopen(IM C*n,IM C*p){F f;N(f=fopen(n,p),die("unable to open file `%s`",n))R f;} diff --git a/a.h b/a.h index c6d28a0..6d3bf05 100644 --- a/a.h +++ b/a.h @@ -1,7 +1,9 @@ +#include #include #include #include #include +#include #define IM const #define N(a,...)Q(!(a),__VA_ARGS__) #define NR _Noreturn @@ -11,6 +13,7 @@ #define SZ sizeof #define VA(a,...){va_list ap;va_start(ap,a);__VA_ARGS__;va_end(ap);} #define WH(a,...)while((a)){__VA_ARGS__;} +#define WIF(f,n,p,...){f=xopen(n,p);__VA_ARGS__;fclose(f);} #define _ static typedef void V; typedef int I; @@ -18,12 +21,15 @@ typedef char C; typedef uint8_t B; typedef uint16_t H; typedef uint32_t W; +typedef FILE*F; NR V die(IM C*,...); V asrt(B,IM C*); V*mk(W); V*remk(V*,W); V del(V*); +F xopen(IM C*,IM C*); #define malloc B use_mk_instead[-1] #define realloc B use_remk_instead[-1] #define free B use_del_instead[-1] +#define fopen B use_xopen_instead[-1] diff --git a/as.c b/as.c index c8fc23c..1de4957 100644 --- a/as.c +++ b/as.c @@ -33,9 +33,11 @@ Ht labels; Ht macros; Vec refs; -_ V builtin(S*t){S x=*t;++x.p,--x.l; - Q(seql(Sl("org"),x),tasrt(next(t),'#'),mel=tton(*t))OR - Q(seql(Sl("res"),x),tasrt(next(t),'#'),mel+=tton(*t))OR +#define BE(y,...)Q(seql(Sl(y),x),__VA_ARGS__)OR +_ V builtin(S*t){FILE*f;S y,x=*t;++x.p,--x.l; + BE("org",tasrt(next(t),'#'),mel=tton(*t)) + BE("res",tasrt(next(t),'#'),mel+=tton(*t)) + BE("emb",tasrt(y=next(t),'"'),y.p[y.l-1]=0;WIF(f,(C*)y.p+1,"rb",mel+=fread(meb+mel,1,(W)0-1,f))) tw(x),die(" unknown builtin");} _ V pass1(S*t){S x=*t;B c,o;c=*x.p; @@ -61,10 +63,7 @@ _ V run(V){S t={0,inb}; I main(I ac,C**av){FILE *f; Q(3!=ac,die("usage: as INPUT.ASM OUTPUT.ROM")) /*todo:this is undefined behaviour for binary streams (SEEK_END)*/ - N(f=fopen(av[1],"rb"),die("couldn't open input file")) - fseek(f,0,SEEK_END),inl=ftell(f),fseek(f,0,SEEK_SET),inb=mk(inl+1),fread(inb,1,inl,f),fclose(f),inb[inl]=0; - run(),del(inb); - N(f=fopen(av[2],"wb"),die("couldn't open output file")) - fwrite(meb+0x100,1,mel-0x100,f); + WIF(f,av[1],"rb",fseek(f,0,SEEK_END),inl=ftell(f),fseek(f,0,SEEK_SET),inb=mk(inl+1),fread(inb,1,inl,f)) + WIF(f,av[2],"wb",Q(chdir(dirname(av[1])),die("?"))inb[inl]=0,run(),del(inb),fwrite(meb+0x100,1,mel-0x100,f)) printf("done: %d bytes wrote\n",mel-0x100); R 0;} diff --git a/kip-io-disk.c b/kip-io-disk.c new file mode 100644 index 0000000..2c4769f --- /dev/null +++ b/kip-io-disk.c @@ -0,0 +1,12 @@ +#include +#include"kip.h" +#define BLK 0x1000 +_ FILE*f; +_ W z,s,a; + +V disk_open(IM C*n){f=xopen(n,"r+b"),fseek(f,0,SEEK_END),z=ftell(f),fseek(f,0,SEEK_SET);} +V disk_close(V){Q(f,fclose(f));} + +DEV(disk_size,((V)x),(R z)) +DEV(disk_seek,(fseek(f,BLK*(s=x),SEEK_SET)),(R s)) +DEV(disk_read,(a=fread(mem+x,1,BLK,f)),(R a)) diff --git a/kip.c b/kip.c index be3de61..ac04930 100644 --- a/kip.c +++ b/kip.c @@ -12,8 +12,8 @@ I main(I ac,IM C*av[]){B*b;W l;FILE*f; #include"kip-io.def" }; #undef X - f=fopen(av[1],"rb"),fseek(f,0,SEEK_END),l=ftell(f),fseek(f,0,SEEK_SET), - b=mk(l+1),fread(b,1,l,f),fclose(f),b[l]=0,lod(b,l),front_init(); + WIF(f,av[1],"rb",fseek(f,0,SEEK_END),l=ftell(f),fseek(f,0,SEEK_SET),b=mk(l+1),fread(b,1,l,f)) + b[l]=0,lod(b,l),front_init(); WH(1,front_events(),emu(STEP),front_redraw()) kip_deinit();}