as:add -emb,kip-io:actually add disk

This commit is contained in:
Kitty-Cricket Piapiac 2023-03-21 13:29:33 -07:00
parent 70cda6bb79
commit ba5bc4cc2e
5 changed files with 29 additions and 10 deletions

2
a.c
View File

@ -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;}

6
a.h
View File

@ -1,7 +1,9 @@
#include<libgen.h>
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#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]

15
as.c
View File

@ -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;}

12
kip-io-disk.c Normal file
View File

@ -0,0 +1,12 @@
#include<stdio.h>
#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))

4
kip.c
View File

@ -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();}