From e86503901cf2e27d25383e1335c91440109c937f Mon Sep 17 00:00:00 2001 From: neauoire Date: Wed, 10 Feb 2021 16:41:16 -0800 Subject: [PATCH] * --- README.md | 2 +- assembler.c | 35 +++++++++++++++++++------------ build.sh | 2 +- emulator.c | 4 ++-- examples/benchmark.usm | 4 ---- examples/blank.usm | 4 ---- examples/pixel.usm | 28 +++++++++++++++---------- examples/pixels.usm | 47 ++++++++++++++++++++++++++++++++++++++++++ examples/screen.usm | 14 ++++++------- examples/test.usm | 24 +-------------------- uxn.c | 2 +- 11 files changed, 98 insertions(+), 68 deletions(-) create mode 100644 examples/pixels.usm diff --git a/README.md b/README.md index ba7d255..3d534e5 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,9 @@ BRK ## TODOs +- short variable - Implement signed flag to operators. - On-screen debugger. -- 16b mode for str/ldr - Auto-advance ldr? - Getting rid of IOR/IOW would be nice.. diff --git a/assembler.c b/assembler.c index 14af8ff..ca920c4 100644 --- a/assembler.c +++ b/assembler.c @@ -139,32 +139,40 @@ makeconst(char *id, FILE *f) return makelabel(id, shex(wv)); } +int +makevariable(char *id, Uint16 *addr, FILE *f) +{ + char wv[64]; + Uint8 origin; + fscanf(f, "%s", wv); + origin = *addr; + *addr += shex(wv); + return makelabel(id, origin); +} + int pass1(FILE *f) { - int skip = 0, vars = 0; + int skip = 0; Uint16 addr = 0; char w[64]; while(fscanf(f, "%s", w) == 1) { if(cmnt(w, &skip)) continue; - if(w[0] == '@' && !makelabel(w + 1, addr)) - return error("Pass1 failed", w); - if(w[0] == ';' && !makelabel(w + 1, vars++)) - return error("Pass1 failed", w); - if(w[0] == ':') { + if(w[0] == '@') { + if(!makelabel(w + 1, addr)) + return error("Pass1 failed", w); + } else if(w[0] == ';') { + if(!makevariable(w + 1, &addr, f)) + return error("Pass1 failed", w); + } else if(w[0] == ':') { if(!makeconst(w + 1, f)) return error("Pass1 failed", w); - else - continue; - } - if(findoperator(w) || scmp(w, "BRK")) + } else if(findoperator(w) || scmp(w, "BRK")) addr += 1; else { switch(w[0]) { case '|': addr = shex(w + 1); break; - case '@': - case ';': break; case '"': addr += slen(w + 1) + 2; break; case '#': addr += 4; break; case '.': addr += 2; break; @@ -189,12 +197,13 @@ pass2(FILE *f) Uint8 op = 0; Label *l; if(w[0] == '@') continue; - if(w[0] == ';') continue; if(cmnt(w, &skip)) continue; if(w[0] == '|') p.ptr = shex(w + 1); else if(w[0] == ':') fscanf(f, "%s", w); + else if(w[0] == ';') + fscanf(f, "%s", w); else if(w[0] == '"') pushtext(w + 1); else if(w[0] == '#') diff --git a/build.sh b/build.sh index fe00044..3763040 100755 --- a/build.sh +++ b/build.sh @@ -24,5 +24,5 @@ rm -f ./bin/emulator cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c emulator.c -L/usr/local/lib -lSDL2 -o bin/emulator # run -./bin/assembler examples/test.usm bin/boot.rom +./bin/assembler examples/pixels.usm bin/boot.rom ./bin/emulator bin/boot.rom diff --git a/emulator.c b/emulator.c index e3a6078..953d0b3 100644 --- a/emulator.c +++ b/emulator.c @@ -37,7 +37,7 @@ SDL_Renderer *gRenderer; SDL_Texture *gTexture; Uint32 *pixels; -Device *devscreen, *devmouse, *devkey; +Device *devconsole, *devscreen, *devmouse, *devkey; int error(char *msg, const char *err) @@ -288,7 +288,7 @@ main(int argc, char **argv) if(!init()) return error("Init", "Failed"); - portuxn(&u, "console", consoler, consolew); + devconsole = portuxn(&u, "console", consoler, consolew); devscreen = portuxn(&u, "screen", screenr, screenw); devmouse = portuxn(&u, "mouse", mouser, mousew); devkey = portuxn(&u, "key", keyr, keyw); diff --git a/examples/benchmark.usm b/examples/benchmark.usm index a7bc3d9..d9963bd 100644 --- a/examples/benchmark.usm +++ b/examples/benchmark.usm @@ -1,9 +1,5 @@ ( benchmark ) -;iterator -:dev1r FFF0 -:dev1w FFF1 - |0100 @RESET ( arithmetic ) diff --git a/examples/blank.usm b/examples/blank.usm index a2cbd95..663a0e9 100644 --- a/examples/blank.usm +++ b/examples/blank.usm @@ -1,9 +1,5 @@ ( blank ) -;iterator -:dev1r FFF0 -:dev1w FFF1 - |0100 @RESET BRK |c000 @FRAME BRK |d000 @ERROR BRK diff --git a/examples/pixel.usm b/examples/pixel.usm index 368f34f..4311eaa 100644 --- a/examples/pixel.usm +++ b/examples/pixel.usm @@ -1,21 +1,27 @@ ( draw pixel ) +:dev/w fff9 ( keep write port in a const ) +;x 2 +;y 2 + |0100 @RESET - ( redraw - color - x - y ) - ,00 ,01 ,0000 ,0000 ,putpixel JSR - ,00 ,02 ,0001 ,0001 ,putpixel JSR - ,01 ,03 ,0002 ,0002 ,putpixel JSR + ( set dev/write to screen ) + + ,01 ,dev/w STR + + ,0020 ,x STR^ ( set x-pos ) + ,0030 ,y STR^ ( set y-pos ) + + ( IOW will now send to screen ) + + ,y LDR^ IOW^ ( y-pos ) + ,x LDR^ IOW^ ( x-pos ) + ,02 IOW ( color ) + ,01 IOW ( redraw ) BRK -@putpixel - SWP ,01 IOW ,01 IOW ( y ) - SWP ,01 IOW ,01 IOW ( x ) - ,01 IOW ( color ) - ,01 IOW ( redraw ) - RTS - |c000 @FRAME BRK |d000 @ERROR BRK |FFFA .RESET .FRAME .ERROR diff --git a/examples/pixels.usm b/examples/pixels.usm new file mode 100644 index 0000000..4e7d897 --- /dev/null +++ b/examples/pixels.usm @@ -0,0 +1,47 @@ +( my default test file ) + +:dev/r fff8 ( std read port ) +:dev/w fff9 ( std write port ) + +;x 2 ;y 2 ;color 1 + +|0100 @RESET + + ,01 ,dev/w STR ( set dev/write to screen ) + ,01 ,color STR ( set color ) + ,0020 ,x STR^ ( set x-pos ) + ,0030 ,y STR^ ( set y-pos ) + +BRK + +|c000 @FRAME + + ,colorize JSR + ,move JSR + ( draw ) + ,01 ,color LDR ,x LDR^ ,y LDR^ ,putpixel JSR + +BRK + +@colorize + ,color LDR ,01 ADD ,color STR ( incr color ) + ,color LDR ,04 LTH RTS? + ,01 ,color STR + RTS + +@move + ,x LDR^ ,0001 ADD^ ,x STR^ ( incr x ) + ,x LDR^ ,0060 LTH^ RTS? ( if x > 60 ) + ,0020 ,x STR^ ( x = 0x0020 ) + ,y LDR^ ,0001 ADD^ ,y STR^ ( incr y ) + RTS + +@putpixel + IOW^ ( y short ) + IOW^ ( x short ) + IOW ( color byte ) + IOW ( redraw byte ) + RTS + +|d000 @ERROR BRK +|FFFA .RESET .FRAME .ERROR diff --git a/examples/screen.usm b/examples/screen.usm index e92bd8e..5b568e4 100644 --- a/examples/screen.usm +++ b/examples/screen.usm @@ -2,10 +2,8 @@ :dev/r fff8 ( std read port ) :dev/w fff9 ( std write port ) -;width0 -;width1 -;height0 -;height1 +;width 2 +;height 2 |0100 @RESET @@ -13,14 +11,14 @@ ,01 DUP ,dev/r STR ,dev/w STR ( load screen size ) - ,00 IOR^ ,width0 STR^ - ,02 IOR^ ,height0 STR^ + ,00 IOR^ ,width STR^ + ,02 IOR^ ,height STR^ ( draw pixel at screen center ) ,0101 - ,width0 LDR^ ,0002 DIV^ - ,height0 LDR^ ,0002 DIV^ + ,width LDR^ ,0002 DIV^ + ,height LDR^ ,0002 DIV^ ,putpixel JSR BRK diff --git a/examples/test.usm b/examples/test.usm index cf8d4c9..7de7ebd 100644 --- a/examples/test.usm +++ b/examples/test.usm @@ -3,34 +3,12 @@ :dev/r fff8 ( std read port ) :dev/w fff9 ( std write port ) -;i ;x0 ;x1 ;y0 ;y1 - |0100 @RESET ,01 ,dev/w STR ( set dev/write screen#01 ) BRK -|c000 @FRAME - - ,i LDR ,04 ADD ,i STR ( incr i ) - ,i LDR ,x1 STR ( set x ) - ,changerow JSR ( update y ) - ,01 ,02 ,x0 LDR^ ,y0 LDR^ ,putpixel JSR - -BRK - -@changerow - ,i LDR ,00 NEQ RTS? - ,y1 LDR ,04 ADD ,y1 STR - RTS - -@putpixel - IOW^ ( y short ) - IOW^ ( x short ) - IOW ( color byte ) - IOW ( redraw byte ) - RTS - +|c000 @FRAME BRK |d000 @ERROR BRK |FFFA .RESET .FRAME .ERROR diff --git a/uxn.c b/uxn.c index 44b31eb..b050f88 100644 --- a/uxn.c +++ b/uxn.c @@ -208,6 +208,6 @@ portuxn(Uxn *u, char *name, Uint8 (*rfn)(Device *, Uint8), Uint8 (*wfn)(Device * d->read = rfn; d->write = wfn; d->len = 0; - printf("Device#%d: %s \n", u->devices, name); + printf("Device #%d: %s \n", u->devices - 1, name); return d; }