by Sally Forth
Here's FPLOT, the ultrahigh-speed graphics plotting routine I promised to bring you last month. It's designed exclusively for Atari's high-resolution 2-color mode "F" (GRAPHICS mode 24 in BASIC). To use it, you must have valFORTH's standard graphics library and assembler LOADed into your system. The definition eats up a hefty chunk of dictionary space, but who cares? FPLOT is fast.
Although the source screens are mostly self-documenting, a few clarifications are in order for the sake of beginners. FPLOT uses a technique known as "table lookup" to eliminate the time-consuming calculations used by the PLOT routine in Atari's Central Input/Output (CIO) utility. The twin ALLOT words in screen 1 set aside 384 bytes for the lookup tables; screens 2 and 3 set up negative and positive bit-masks for the plot logic. Screens 4 through 7 define an assembly-language word (PLOTSETUP) that initializes the lookup tables. It must be executed immediately after setting up the high-res screen (with a 24 GR. instruction) or FPLOT will not work. I could have written PLOTSETUP in FORTH, but speed is the reason for this entire exercise, right?
Screens 8 through 12 contain the assembly-language definition of FPLOT. Syntax is identical to the PLOT word in valFORTH's standard graphics package. The color to be plotted (0-1) is specified with the valFORTH word COLOR, just like the ordinary PLOT. Strange things will happen if your plotting parameters exceed 319 for the X-coordinate, or 191 for the Y. FPLOT's lack of automatic range checking is part of the price you pay for extra speed.
You can use FPLOT with the valFORTH word DR. (DRAWTO) because it saves the X and Y coordinates in the operating system locations COLCRS and ROWCRS. If you're not using DR. and you want to make FPLOT even faster, eliminate Line 9 in screen 9 and Lines 6, 7, and 9 in screen 10.
The demo words in screens 13-17 will show you just how much speed you can expect from FPLOT. I obtained screen-fill times of 9957 jiffies for the CIO plot and just 1632 jiffies for PLOT, a 6-times improvement. In case you're wondering, it takes Atari BASIC 24554 jiffies to fill a mode F screen with PLOTS!
Next month, I'll show you how to implement megaspeed line-drawing with FPLOT.
SFORTH16.TXT is available in ATASCII format.
SCREEN #1
0 ( HIGH SPEED MODE F PLOTTER )
1
2 ( Reserve space for tables )
3
4 DECIMAL
5
6 LABEL YLOWS 192 ALLOT ( lsbs )
7 LABEL YHIGHS 192 ALLOT ( msbs )
8
9 ( OS equates )
10
11 84 CONSTANT ROWCRS
12 85 CONSTANT COLCRS
13 88 CONSTANT SAVMSC
14
15 -->
SCREEN #2
0 ( HIGH SPEED MODE F PLOTTER )
1
2 2 BASE ! ( for convenience )
3
4 LABEL NMASKS ( plot masks )
5
6 11111110 C, 11111110 C,
7 11111101 C, 11111101 C,
8 11111011 C, 11111011 C,
9 11110111 C, 11110111 C,
10 11101111 C, 11101111 C,
11 11011111 C, 11011111 C,
12 10111111 C, 10111111 C,
13 01111111 C, 01111111 C,
14
15 -->
SCREEN #3
0 ( HIGH SPEED MODE F PLOTTER )
1
2 LABEL PMASKS ( more masks )
3
4 0 C, 1 C,
5 0 C, 10 C,
6 0 C, 100 C,
7 0 C, 1000 C,
8 0 C, 10000 C,
9 0 C, 100000 C,
10 0 C, 1000000 C,
11 0 C, 10000000 C,
12
13 DECIMAL
14
15 -->
SCREEN #4
0 ( HIGH SPEED MODE F PLOTTER )
1
2 ASSEMBLER ( new vocabulary )
3
4 N 4 + CONSTANT PNTR
5 N 3 + CONSTANT XHI
6 N 2 + CONSTANT XLO
7 N CONSTANT YPOS
8
9 ( PLOTSETUP initializes the
10 ( YLOWS and YHIGHS tables for
11 ( use by the high-speed plot
12 ( routine. It should be called
13 ( immediately after setting up
14 ( graphics mode 24. )
15 -->
SCREEN #5
0 ( HIGH SPEED MODE F PLOTTER )
1
2 CODE PLOTSETUP
3
4 XSAVE STX, ( preserve X )
5 SAVMSC 1+ LDA, ( get msb of )
6 YPOS STA, ( screen addr )
7 SAVMSC LDY, ( and also lsb )
8 # 0 LDX, ( init index )
9 CLD, ( for safety )
10
11 BEGIN, ( start a loop )
12
13 YPOS LDA, ( put msb into )
14 YHIGHS ,X STA, ( msb table )
15 -->
SCREEN #6
0 ( HIGH SPEED MODE F PLOTTER )
1
2 TYA, ( put lsb into )
3 YLOWS ,X STA, ( lsb table )
4 CLC, ( for addition )
5 # 40 ADC, ( new offset )
6
7 CS IF, ( if carry set, )
8 YPOS INC, ( increment msb )
9 ENDIF,
10
11 TAY, ( save new lsb )
12 INX, ( update index )
13 # 192 CPX, ( done 192 yet? )
14
15 -->
SCREEN #7
0 ( HIGH SPEED MODE F PLOTTER )
1
2 EQ UNTIL, ( loop until )
3 ( index = 192 )
4
5 XSAVE LDX, ( restore X-reg )
6 NEXT JMP, ( back to FORTH )
7
8 C; ( end PLOTSETUP )
9
10
11
12
13
14
15 -->
SCREEN #8
0 ( HIGH SPEED MODE F PLOTTER )
1
2 ( FPLOT is the actual plotting
3 ( word. Syntax is the same as
4 ( the standard CIO-type PLOT:
5
6 ( x-pos y-pos ---
7
8 ( X-pos can range fro 0-319.
9 ( Y-pos range is 0-191. No
10 ( range checks are performed
11 ( to save time, so be careful!
12 ( Plot color is controlled by
13 ( the standard valFORTH word
14 ( COLOR. Legal COLOR values
15 ( are 0 and 1. ) -->
SCREEN #9
0 ( HIGH SPEED MODE F PLOTTER )
1
2 CODE FPLOT
3
4 # 2 LDA, ( # DROP values )
5 SETUP JSR, ( move into N )
6 XSAVE STX, ( preserve X )
7
8 YPOS LDX, ( y-pos is used )
9 ROWCRS STX, ( as an index )
10 YLOWS ,X LDA, ( into lsb/msb )
11 PNTR STA, ( offset tables )
12 YHIGHS ,X LDA, ( to create a )
13 PNTR 1+ STA, ( z-page pntr )
14
15 -->
SCREEN #10
0 ( HIGH-SPEED MODE F PLOTTER )
1
2 ( PNTR now contains the
3 ( absolute RAM address of the
4 ( desired mode line. )
5
6 XHI LDA, ( get msb x-pos )
7 COLCRS 1+ STA, ( for OS usage )
8 XLO LDA, ( get lsb x-pos )
9 COLCRS STA, ( also for OS )
10 XHI LSR, ( divide x-pos )
11 .A ROR, ( by 2 )
12 .A LSR, ( then 4 )
13 .A LSR, ( and then 8 )
14 TAY, ( save x/8 in Y )
15 -->
SCREEN #11
0 ( HIGH-SPEED MODE F PLOTTER )
1
2 XLO LDA, ( get lsb again )
3 # 7 AND, ( mask to get
4 ( bit position
5 ( in plot byte )
6 .A ASL, ( multiply by 2 )
7 CLRBYT ORA, ( & superimpose
8 ( bit 0 color
9 ( data for use )
10 TAX, ( as an index
11 ( into the mask
12 ( tables )
13 PNTR )Y LDA, ( get plot byte )
14 NMASKS ,X AND, ( mask plot bit )
15 -->
SCREEN #12
0 ( HIGH-SPEED MODE F PLOTTER )
1
2 PMASKS ,X ORA, ( superimpose )
3 ( the plot bit )
4 PNTR )Y STA, ( and show the )
5 ( new byte )
6 XSAVE LDX, ( restore X-reg )
7 NEXT JMP, ( and return )
8
9 C; ( end FPLOT )
10
11
12
13
14
15 -->
SCREEN #13
0 ( FPLOT DEMONSTRATION )
1
2 ( PLOTEST and FPLOTEST compare
3 ( the speed of the standard
4 ( CIO-type PLOT with FPLOT.
5
6 ( They PLOT all 61,440 pixels
7 ( on a full Mode F screen,
8 ( using the system jiffy
9 ( timers to clock the speed.
10
11 ( Be sure you've LOADed the
12 ( valFORTH graphics library
13 ( into your system before
14 ( LOADing these words! )
15 -->
SCREEN #14
0 ( FPLOT DEMONSTRATION )
1
2 19 CONSTANT TOCK ( the timer )
3 20 CONSTANT TICK ( locations )
4
5 0 VARIABLE TIMING
6
7 ( Set up screen & colors )
8
9 : GREADY
10 24 GR.
11 1 0 14 SE. ( white pixels )
12 2 0 0 SE. ( black bkgnd )
13 1 COLOR ;
14
15 -->
SCREEN #15
0 ( FPLOT DEMONSTRATION )
1
2 ( Calculate & display
3 ( timer reading )
4
5 : SHOWTIME
6 TOCK C@ 256 *
7 TICK C@ +
8 TIMING !
9 0 GR.
10 TIMING @ .
11 ." jiffies with " ;
12
13
14
15 -->
SCREEN #16
0 ( FPLOT DEMONSTRATION )
1
2 ( Fill screen with CIO PLOTs )
3
4 : PLOTEST
5 GREADY
6 0 TOCK ! ( zero timers )
7 192 0 DO
8 320 0 DO
9 I J PLOT
10 LOOP
11 LOOP
12 SHOWTIME
13 ." CIO PLOT." CR ;
14
15 -->
SCREEN #17
0 ( FPLOT DEMONSTRATION )
1
2 ( Fill screen with FPLOTs )
3
4 : FPLOTEST
5 GREADY
6 PLOTSETUP ( init plot tables )
7 0 TOCK ! ( zero timers )
8 192 0 DO
9 320 0 DO
10 I J FPLOT
11 LOOP
12 LOOP
13 SHOWTIME
14 ." FPLOT." CR ;
15