( graph.tal ) ( ) ( graphing calculator ) ( EXPRESSIONS ) ( ) ( expressions use one byte for an id, and ) ( zero or more bytes for data. ) ( ) ( ID NAME DATA DESCRIPTION ) ( 00 var variable x ) ( 01 lit const* literal value ) ( 02 neg e1* -e1 ) ( 03 pow e* k^ e^k ) ( 04 add e1* e2* e1 + e2 ) ( 05 sub e1* e2* e1 - e2 ) ( 06 mul e1* e2* e1 * e2 ) ( 07 div e1* e2* e1 / e2 ) ( ) ( const* is a fixed point value ) ( e*, e1*, and e2* are addresses of expressions. ) ( k^ is an unsigned integer ) |00 @System &vect $2 &wst $1 &rst $1 &eaddr $2 &ecode $1 &pad $1 &r $2 &g $2 &b $2 &debug $1 &halt $1 |20 @Screen &vect $2 &w $2 &h $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 |0000 ( set up variables used to configure graphing ) @width $2 ( screen width in pixels ) @height $2 ( screen height in pixels ) @epsilon $2 ( graph value per pixel ) @xmin $2 ( min x graph coord ) @xmax $2 ( max x graph coord ) @ymin $2 ( min y graph coord ) @ymax $2 ( max y graph coord ) ( we really want (xmax-xmin)*epsilon to exactly equal width ) ( and similarly (ymax-ymin)*epsilon to exactly equal height ) ( ) ( this implies a square screen. for non-square screens we'll ) ( need separate epsilons for x and y. ) ( ) ( variables used during graphing ) @step $2 ( current step size for drawing ) @expr $2 ( current equation being graphed ) @x0 $2 ( lower bound of x ) @x1 $2 ( upper bound of x ) @y0 $2 ( lower bound of y ) @y1 $2 ( upper bound of y ) |0100 ( default settings: -4.0 to +4.0 ) ( #fc00 DUP2 .xmin STZ2 .ymin STZ2 #0400 DUP2 .xmax STZ2 .ymax STZ2 ) #0800 DUP2 .xmax STZ2 DUP2 .ymax STZ2 #0000 SWP2 SUB2 DUP2 .xmin STZ2 .ymin STZ2 ( screen size ) #0200 x-range min .width STZ2 #0200 y-range min .height STZ2 ( colors ) #0f0f .System/r DEO2 #0ff0 .System/g DEO2 #00ff .System/b DEO2 ( set up screen device ) .width LDZ2 .Screen/w DEO2 .height LDZ2 .Screen/h DEO2 ( TODO: we probably want separate epsilons for x and y ) ( TODO: validate that (xmax-xmin)*eps = width exactly ) x-range .width LDZ2 DIV2 .epsilon STZ2 ( draw x and y axis on background ) draw-y-axis draw-x-axis ( prepare equation ) ;x^3-2x+1 .expr STZ2 ( ;0.5x^2-6 .expr STZ2 ) ( set up starting parameters ) .xmin LDZ2 .ymin LDZ2 x-range ( graph the equation ) draw BRK @min ( x* y* -> min* ) LTH2k JMP SWP2 POP2 JMP2r @x-range ( -> x-range* ) .xmax LDZ2 .xmin LDZ2 SUB2 JMP2r @y-range ( -> y-range* ) .ymax LDZ2 .ymin LDZ2 SUB2 JMP2r @hline ( x1* x2* y* ) #01 .Screen/auto DEO .Screen/y DEO2 OVR2 .Screen/x DEO2 SUB2 &xloop #02 .Screen/pixel DEO INC2 ORAk ?&xloop POP2 JMP2r @vline ( y1* y2* x* ) #02 .Screen/auto DEO .Screen/x DEO2 OVR2 .Screen/y DEO2 SUB2 &yloop #02 .Screen/pixel DEO INC2 ORAk ?&yloop POP2 JMP2r @center-x ( -> cx* ) .width LDZ2 #01 SFT2 JMP2r @center-y ( -> cx* ) .height LDZ2 #01 SFT2 JMP2r ( TODO: gets weird with non-powers-of-2 ) @draw-y-axis ( -> ) #0000 .height LDZ2 center-x vline ( ) center-x #0010 SUB2k ,&lmid STR2 ( ) ADD2 ,&rmid STR2 ( ) .height LDZ2 STH2 ( [h*] ) #0100 .epsilon LDZ2 DIV2 ,&vstep STR2 ( [h*] ) ,&vstep LDR2 STH2 ( [h* step*] ) &vloop ( [h* pos*] ) [ LIT2 &lmid $2 ] ( lmid* [h* pos*] ) [ LIT2 &rmid $2 ] ( lmid* rmid* [h* pos*] ) STH2kr vline ( [h* pos*] ) [ LIT2r &vstep $2 ] ADD2r ( [h* pos+step*] ) GTH2kr STHr ?&vloop ( [h* pos+step*] ) POP2r POP2r JMP2r ( ) ( TODO: gets weird with non-powers-of-2 ) @draw-x-axis ( -> ) #0000 .width LDZ2 center-y hline ( ) center-y #0010 SUB2k ,&lmid STR2 ( ) ADD2 ,&rmid STR2 ( ) .width LDZ2 STH2 ( [w*] ) #0100 .epsilon LDZ2 DIV2 ,&hstep STR2 ( [w*] ) ,&hstep LDR2 STH2 ( [h* step*] ) &hloop ( [w* pos*] ) [ LIT2 &lmid $2 ] ( lmid* [w* pos*] ) [ LIT2 &rmid $2 ] ( lmid* rmid* [w* pos*] ) STH2kr hline ( [w* pos*] ) [ LIT2r &hstep $2 ] ADD2r ( [w* pos+step*] ) GTH2kr STHr ?&hloop ( [w* pos+step*] ) POP2r POP2r JMP2r ( ) ( recursive drawing procedure ) ( ) ( this draws the area [x,y] to [x+s,y+s]. ) ( first it checks whether that area contains a point. ) ( next it checks if s=1: ) ( - if so we draw that point ) ( - if not, we split into a 2x2 grid and recurse ) @draw ( x* y* s* -> ) STH2k .step STZ2 ( x* y* [s*] ; step<-s ) OVR2 OVR2 evaluate ?&hit ( x* y* [s*] ) POP2 POP2 POP2r JMP2r ( ; miss ) &hit ( x* y* [s*] ) ( STH2kr #0001 EQU2 ?&term ( x* y* [s*] ) ) STH2kr .epsilon LDZ2 LTH2 ?&term ( x* y* [s*] ) LITr 01 SFT2r ( x* y* [h=s/2*] ) OVR2 OVR2 ( x* y* x* y* h* [h*] ) STH2kr draw ( x* y* [h*] ; south west ) OVR2 OVR2 STH2kr ADD2 ( x* y* x* y+h* [h*] ) STH2kr draw ( x* y* [h*] ; north west ) OVR2 STH2kr ADD2 OVR2 ( x* y* x+h* y* [h*] ) STH2kr draw ( x+h* y* [h*] ; south east ) STH2kr ADD2 SWP2 STH2kr ADD2 SWP2 ( x+h* y+h* [h*] ) STH2r draw ( ; upper-right ) JMP2r ( ) &term ( x* y* [s*] ) POP2r ( x* y* ) .ymax LDZ2 SWP2 SUB2 ( x* ymax-y* ) .epsilon LDZ2 SUB2 ( x* ymax-y-e* ) .epsilon LDZ2 DIV2 ( x* sy=(ymax-y-e)/e* ) .Screen/y DEO2 ( x* ; screen/y<-sy ) .xmin LDZ2 SUB2 ( x-xmin* ) .epsilon LDZ2 DIV2 ( x* sx=(x-xmin)/e* ) .Screen/x DEO2 ( ; screen/x<-sx ) #41 .Screen/pixel DEO ( ; screen/pixel<-41 ) JMP2r ( ) ( determine if the given x and y intervals intersect ) ( if they do, at least one point of the graph is in this region ) ( if not, the entire region is empty. ) @evaluate ( x* y* -> bool^ ) ( store epsilon and x/y intervals ) LITr -step LDZ2r ( x* y* [e*] ) DUP2 .y0 STZ2 ( x* y* [e*] ; y0<-y ) STH2kr x16-add .y1 STZ2 ( x* [e*] ; y1<-y+e ) DUP2 .x0 STZ2 ( x* [e*] ; x0<-x ) STH2r x16-add .x1 STZ2 ( ; x1<-x+e ) ( evaluate our equation ) .expr LDZ2 eval ( x-iv** ) .y0 LDZ2 .y1 LDZ2 ( x-iv** y-iv** ) !iv-intersects ( result^ ) ( requires .x0 and .x1 to already be set ) @eval ( expr* -> interval** ) LDAk #00 NEQ ?&o1 ( var ) POP2 .x0 LDZ2 .x1 LDZ2 JMP2r &o1 LDAk #01 NEQ ?&o2 ( lit ) INC2 LDA2 DUP2 JMP2r &o2 LDAk #02 NEQ ?&o3 ( neg ) INC2 LDA2 eval !iv-negate &o3 LDAk #03 NEQ ?&o4 ( pow ) INC2 STH2k LDA2 eval STH2r INC2 INC2 LDA !iv-pow &o4 LDAk #04 NEQ ?&o5 ( add ) ;iv-add !binop &o5 LDAk #05 NEQ ?&o6 ( sub ) ;iv-sub !binop &o6 LDAk #06 NEQ ?&o7 ( mul ) ;iv-mul !binop &o7 #0000 DIV ( TODO: div ) @binop ( addr* op* -> interval** ) STH2 INC2 STH2k ( addr+1* [op* addr+1*] ) LDA2 eval ( lhs** [op* addr+1*] ) STH2r INC2 INC2 LDA2 eval ( lhs** rhs** [op*] ) STH2r JMP2 ( result** ) @clear ( -> ) #0000 ;expr STA2 ;arena ;next STA2 JMP2r @alloc ( id^ size* -> addr* ) ;next LDA2k STH2k ( id^ size* next* addr* [addr*] ) ROT2 ADD2 SWP2 STA2 ( id^ [addr*] ; next<-addr+size ) STH2kr STA ( [addr*] ; addr<-id ) STH2r JMP2r ( addr* ) @alloc-const ( const* -> e* ) #01 #0003 alloc ( const* addr* ) STH2k INC2 STA2 ( [addr*] ; addr+1<-const ) STH2r JMP2r ( addr* ) @alloc-lit ( const* -> e* ) #01 #0003 alloc ( const* addr* ) STH2k INC2 STA2 ( [addr*] ; addr+1<-const ) STH2r JMP2r ( addr* ) @alloc-neg ( e0* -> e* ) #02 #0003 alloc ( e0 addr* ) STH2k INC2 STA2 ( [addr*] ; addr+1<-e0* ) STH2r JMP2r ( addr* ) @0.5x^2-6 05 =0.5x^2 =six @0.5x^2 06 =x^2 =0.5 @x^2 03 =x 02 @0.5 01 0080 @six 01 0600 @x^3-2x+1 04 =x^3-2x =one @x^3-2x 05 =x^3 =2x @one 01 0100 @x^3 03 =x 03 @2x 06 =x =two @two 01 0200 ~interval.tal @x 00 @next :arena @arena $1000