!lm12
!rm76
Hi-Res SCRN Function with Color...............David Doudna

I am a 15-year-old living in St. Louis, Missouri.  While looking through the back issues of Apple Assembly Line, I found "Hi-Res SCRN Function for Applesoft" (May, 1981 issue).  I noticed the routine only returned a 0 or 1, and you challenged readers to write one to return a color value 0-7.  Well, I did it.  My version is not interfaced to Applesoft; that is an exercise for the reader!  (I use the Programmer's Aid ROM with FORTH.)

I am not going to explain how hi-res colors work, beyond the facts that two adjacent dots are white; the upper bit in each byte of the hi-res screen adds 4 to the color value; an isolated bit is color 1 or 2 (or 5 or 6) depending on the X-position.  If you want to understand my program, you should study more about hi-res plotting first.

A word about the color value....  In Applesoft you specify color value with a number from 0 to 7.  The Programmer's Aid ROM uses color values of 0, 42, 85, 127, 128, 170, 213, and 255.  My program returns both numbers for the color: the 0-7 index in HCOLOR, and the P.A.ROM color value in COLOR.BYTE.

Lines 1060-1140 define the variables used; these are in the same locations as those used by the Programmer's Aid ROM.  If you want to modify the program to work with Applesoft, be sure to put these variables in the correct locations.  Two more variables are defined at lines 2120,2130.

Lines 1160-1180 pick up the X- and Y-coordinates.  I assume you have stored the coordinates here before calling HSCRN.  Lines 1190-1390 calculate the base address for the particular horizontal line your point is on.  This code is just copied out of P.A.ROM.  Lines 1410-1530 divide the X-coordinate by 7 to get the byte offset on the line.  The quotient is left in the Y-register.  The remainder is used to pick up a bit mask to select the particular bit within the byte.

Lines 1540-1650 make the first color check.    The high-order bit of the byte (half-dot shift control).  If the bit specified = zero, the color is black.  If it = one, the color depends on whether either neighbor of this dot = one.  If neither neighbor = one, the color depends on whether this dot is in an even or odd column.  If the color is not black, I put 1 or 2 in the X-register to indicate the color it will be if it is not white.

Lines 1660-1790 check the neighbor bit on the left to see if it = one.  Notice that there are several special cases.  First, the left-neighbor might be in the same byte.  Second, it might be in the byte to the left of this one.  Third, there might not be a byte to the left of this one.

Lines 1800-1920 check the neighbor bit on the right.  The same kind of special cases exist here, and they are handled the same way.

Line 1940 sets X = 3 for white color.  Line 1960 gets the color value in the A-register.  All paths merge at line 1980, with the color index 0-3 in the A-register.  All that remains is to add 4 if the half-dot shift control = 1 (Lines 1980-2010.

Lines 2020-2060 convert the color index to a color byte (by simple table-lookup), and return.  Line 2100 is the table of color values.

Here is a table of colors (their names, index numbers, and P.A.ROM numbers):

!lm16
                Color Byte Value
Color   Index   Hex  Dec  Binary
----------------------------------
BLACK     0     00     0  00000000
GREEN     1     2A    42  00101010
VIOLET    2     55    85  01010101
WHITE     3     7F   127  01111111
BLACK2    4     80   128  10000000
ORANGE    5     AA   170  10101010
BLUE      6     D5   213  11010101
WHITE2    7     FF   255  11111111

!lm12
The program works with either page 1 or page 2 of Hi-Res.  Set HPAGE to $20 for page 1, or $40 for page 2.

To call this program from Integer BASIC, you would first POKE the X- and Y-coordinates, then CALL the program, and then PEEK the color value.  From assembly language, set up the coordinates and JSR HSCRN.  The color index is returned in the X-register, and the color byte value in the A-register.

[ Program and article modified somewhat by the editor ]
