Add files via upload

This commit is contained in:
John Snape 2019-06-13 22:21:20 -07:00 committed by GitHub
parent 5a954f98d2
commit 868979de0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1486 additions and 0 deletions

540
6502 opcodes.txt Normal file
View File

@ -0,0 +1,540 @@
ADC (ADd with Carry)
Affects Flags: N V Z C
MODE SYNTAX HEX LEN TIM
Immediate ADC #$44 $69 2 2
Zero Page ADC $44 $65 2 3
Zero Page,X ADC $44,X $75 2 4
Absolute ADC $4400 $6D 3 4
Absolute,X ADC $4400,X $7D 3 4+
Absolute,Y ADC $4400,Y $79 3 4+
Indirect,X ADC ($44,X) $61 2 6
Indirect,Y ADC ($44),Y $71 2 5+
+ add 1 cycle if page boundary crossed
ADC results are dependant on the setting of the decimal flag. In decimal mode, addition is carried out on the assumption that the values involved are packed BCD (Binary Coded Decimal).
There is no way to add without carry.
AND (bitwise AND with accumulator)
Affects Flags: N Z
MODE SYNTAX HEX LEN TIM
Immediate AND #$44 $29 2 2
Zero Page AND $44 $25 2 3
Zero Page,X AND $44,X $35 2 4
Absolute AND $4400 $2D 3 4
Absolute,X AND $4400,X $3D 3 4+
Absolute,Y AND $4400,Y $39 3 4+
Indirect,X AND ($44,X) $21 2 6
Indirect,Y AND ($44),Y $31 2 5+
+ add 1 cycle if page boundary crossed
ASL (Arithmetic Shift Left)
Affects Flags: N Z C
MODE SYNTAX HEX LEN TIM
Accumulator ASL A $0A 1 2
Zero Page ASL $44 $06 2 5
Zero Page,X ASL $44,X $16 2 6
Absolute ASL $4400 $0E 3 6
Absolute,X ASL $4400,X $1E 3 7
ASL shifts all bits left one position. 0 is shifted into bit 0 and the original bit 7 is shifted into the Carry.
BIT (test BITs)
Affects Flags: N V Z
MODE SYNTAX HEX LEN TIM
Zero Page BIT $44 $24 2 3
Absolute BIT $4400 $2C 3 4
BIT sets the Z flag as though the value in the address tested were ANDed with the accumulator. The S and V flags are set to match bits 7 and 6 respectively in the value stored at the tested address.
BIT is often used to skip one or two following bytes as in:
CLOSE1 LDX #$10 If entered here, we
.BYTE $2C effectively perform
CLOSE2 LDX #$20 a BIT test on $20A2,
.BYTE $2C another one on $30A2,
CLOSE3 LDX #$30 and end up with the X
CLOSEX LDA #12 register still at $10
STA ICCOM,X upon arrival here.
Beware: a BIT instruction used in this way as a NOP does have effects: the flags may be modified, and the read of the absolute address, if it happens to access an I/O device, may cause an unwanted action.
Branch Instructions
Affect Flags: none
All branches are relative mode and have a length of two bytes. Syntax is "Bxx Displacement" or (better) "Bxx Label". See the notes on the Program Counter for more on displacements.
Branches are dependant on the status of the flag bits when the op code is encountered. A branch not taken requires two machine cycles. Add one if the branch is taken and add one more if the branch crosses a page boundary.
MNEMONIC HEX
BPL (Branch on PLus) $10
BMI (Branch on MInus) $30
BVC (Branch on oVerflow Clear) $50
BVS (Branch on oVerflow Set) $70
BCC (Branch on Carry Clear) $90
BCS (Branch on Carry Set) $B0
BNE (Branch on Not Equal) $D0
BEQ (Branch on EQual) $F0
There is no BRA (BRanch Always) instruction but it can be easily emulated by branching on the basis of a known condition. One of the best flags to use for this purpose is the oVerflow which is unchanged by all but addition and subtraction operations.
A page boundary crossing occurs when the branch destination is on a different page than the instruction AFTER the branch instruction. For example:
SEC
BCS LABEL
NOP
A page boundary crossing occurs (i.e. the BCS takes 4 cycles) when (the address of) LABEL and the NOP are on different pages. This means that
CLV
BVC LABEL
LABEL NOP
the BVC instruction will take 3 cycles no matter what address it is located at.
BRK (BReaK)
Affects Flags: B
MODE SYNTAX HEX LEN TIM
Implied BRK $00 1 7
BRK causes a non-maskable interrupt and increments the program counter by one. Therefore an RTI will go to the address of the BRK +2 so that BRK may be used to replace a two-byte instruction for debugging and the subsequent RTI will be correct.
CMP (CoMPare accumulator)
Affects Flags: N Z C
MODE SYNTAX HEX LEN TIM
Immediate CMP #$44 $C9 2 2
Zero Page CMP $44 $C5 2 3
Zero Page,X CMP $44,X $D5 2 4
Absolute CMP $4400 $CD 3 4
Absolute,X CMP $4400,X $DD 3 4+
Absolute,Y CMP $4400,Y $D9 3 4+
Indirect,X CMP ($44,X) $C1 2 6
Indirect,Y CMP ($44),Y $D1 2 5+
+ add 1 cycle if page boundary crossed
Compare sets flags as if a subtraction had been carried out. If the value in the accumulator is equal or greater than the compared value, the Carry will be set. The equal (Z) and negative (N) flags will be set based on equality or lack thereof and the sign (i.e. A>=$80) of the accumulator.
CPX (ComPare X register)
Affects Flags: N Z C
MODE SYNTAX HEX LEN TIM
Immediate CPX #$44 $E0 2 2
Zero Page CPX $44 $E4 2 3
Absolute CPX $4400 $EC 3 4
Operation and flag results are identical to equivalent mode accumulator CMP ops.
CPY (ComPare Y register)
Affects Flags: N Z C
MODE SYNTAX HEX LEN TIM
Immediate CPY #$44 $C0 2 2
Zero Page CPY $44 $C4 2 3
Absolute CPY $4400 $CC 3 4
Operation and flag results are identical to equivalent mode accumulator CMP ops.
DEC (DECrement memory)
Affects Flags: N Z
MODE SYNTAX HEX LEN TIM
Zero Page DEC $44 $C6 2 5
Zero Page,X DEC $44,X $D6 2 6
Absolute DEC $4400 $CE 3 6
Absolute,X DEC $4400,X $DE 3 7
EOR (bitwise Exclusive OR)
Affects Flags: N Z
MODE SYNTAX HEX LEN TIM
Immediate EOR #$44 $49 2 2
Zero Page EOR $44 $45 2 3
Zero Page,X EOR $44,X $55 2 4
Absolute EOR $4400 $4D 3 4
Absolute,X EOR $4400,X $5D 3 4+
Absolute,Y EOR $4400,Y $59 3 4+
Indirect,X EOR ($44,X) $41 2 6
Indirect,Y EOR ($44),Y $51 2 5+
+ add 1 cycle if page boundary crossed
Flag (Processor Status) Instructions
Affect Flags: as noted
These instructions are implied mode, have a length of one byte and require two machine cycles.
MNEMONIC HEX
CLC (CLear Carry) $18
SEC (SEt Carry) $38
CLI (CLear Interrupt) $58
SEI (SEt Interrupt) $78
CLV (CLear oVerflow) $B8
CLD (CLear Decimal) $D8
SED (SEt Decimal) $F8
Notes:
The Interrupt flag is used to prevent (SEI) or enable (CLI) maskable interrupts (aka IRQ's). It does not signal the presence or absence of an interrupt condition. The 6502 will set this flag automatically in response to an interrupt and restore it to its prior status on completion of the interrupt service routine. If you want your interrupt service routine to permit other maskable interrupts, you must clear the I flag in your code.
The Decimal flag controls how the 6502 adds and subtracts. If set, arithmetic is carried out in packed binary coded decimal. This flag is unchanged by interrupts and is unknown on power-up. The implication is that a CLD should be included in boot or interrupt coding.
The Overflow flag is generally misunderstood and therefore under-utilised. After an ADC or SBC instruction, the overflow flag will be set if the twos complement result is less than -128 or greater than +127, and it will cleared otherwise. In twos complement, $80 through $FF represents -128 through -1, and $00 through $7F represents 0 through +127. Thus, after:
CLC
LDA #$7F ; +127
ADC #$01 ; + +1
the overflow flag is 1 (+127 + +1 = +128), and after:
CLC
LDA #$81 ; -127
ADC #$FF ; + -1
the overflow flag is 0 (-127 + -1 = -128). The overflow flag is not affected by increments, decrements, shifts and logical operations i.e. only ADC, BIT, CLV, PLP, RTI and SBC affect it. There is no op code to set the overflow but a BIT test on an RTS instruction will do the trick.
INC (INCrement memory)
Affects Flags: N Z
MODE SYNTAX HEX LEN TIM
Zero Page INC $44 $E6 2 5
Zero Page,X INC $44,X $F6 2 6
Absolute INC $4400 $EE 3 6
Absolute,X INC $4400,X $FE 3 7
JMP (JuMP)
Affects Flags: none
MODE SYNTAX HEX LEN TIM
Absolute JMP $5597 $4C 3 3
Indirect JMP ($5597) $6C 3 5
JMP transfers program execution to the following address (absolute) or to the location contained in the following address (indirect). Note that there is no carry associated with the indirect jump so:
AN INDIRECT JUMP MUST NEVER USE A
VECTOR BEGINNING ON THE LAST BYTE
OF A PAGE
For example if address $3000 contains $40, $30FF contains $80, and $3100 contains $50, the result of JMP ($30FF) will be a transfer of control to $4080 rather than $5080 as you intended i.e. the 6502 took the low byte of the address from $30FF and the high byte from $3000.
JSR (Jump to SubRoutine)
Affects Flags: none
MODE SYNTAX HEX LEN TIM
Absolute JSR $5597 $20 3 6
JSR pushes the address-1 of the next operation on to the stack before transferring program control to the following address. Subroutines are normally terminated by a RTS op code.
LDA (LoaD Accumulator)
Affects Flags: N Z
MODE SYNTAX HEX LEN TIM
Immediate LDA #$44 $A9 2 2
Zero Page LDA $44 $A5 2 3
Zero Page,X LDA $44,X $B5 2 4
Absolute LDA $4400 $AD 3 4
Absolute,X LDA $4400,X $BD 3 4+
Absolute,Y LDA $4400,Y $B9 3 4+
Indirect,X LDA ($44,X) $A1 2 6
Indirect,Y LDA ($44),Y $B1 2 5+
+ add 1 cycle if page boundary crossed
LDX (LoaD X register)
Affects Flags: N Z
MODE SYNTAX HEX LEN TIM
Immediate LDX #$44 $A2 2 2
Zero Page LDX $44 $A6 2 3
Zero Page,Y LDX $44,Y $B6 2 4
Absolute LDX $4400 $AE 3 4
Absolute,Y LDX $4400,Y $BE 3 4+
+ add 1 cycle if page boundary crossed
LDY (LoaD Y register)
Affects Flags: N Z
MODE SYNTAX HEX LEN TIM
Immediate LDY #$44 $A0 2 2
Zero Page LDY $44 $A4 2 3
Zero Page,X LDY $44,X $B4 2 4
Absolute LDY $4400 $AC 3 4
Absolute,X LDY $4400,X $BC 3 4+
+ add 1 cycle if page boundary crossed
LSR (Logical Shift Right)
Affects Flags: N Z C
MODE SYNTAX HEX LEN TIM
Accumulator LSR A $4A 1 2
Zero Page LSR $44 $46 2 5
Zero Page,X LSR $44,X $56 2 6
Absolute LSR $4400 $4E 3 6
Absolute,X LSR $4400,X $5E 3 7
LSR shifts all bits right one position. 0 is shifted into bit 7 and the original bit 0 is shifted into the Carry.
Wrap-Around
Use caution with indexed zero page operations as they are subject to wrap-around. For example, if the X register holds $FF and you execute LDA $80,X you will not access $017F as you might expect; instead you access $7F i.e. $80-1. This characteristic can be used to advantage but make sure your code is well commented.
It is possible, however, to access $017F when X = $FF by using the Absolute,X addressing mode of LDA $80,X. That is, instead of:
LDA $80,X ; ZeroPage,X - the resulting object code is: B5 80
which accesses $007F when X=$FF, use:
LDA $0080,X ; Absolute,X - the resulting object code is: BD 80 00
which accesses $017F when X = $FF (a at cost of one additional byte and one additional cycle). All of the ZeroPage,X and ZeroPage,Y instructions except STX ZeroPage,Y and STY ZeroPage,X have a corresponding Absolute,X and Absolute,Y instruction. Unfortunately, a lot of 6502 assemblers don't have an easy way to force Absolute addressing, i.e. most will assemble a LDA $0080,X as B5 80. One way to overcome this is to insert the bytes using the .BYTE pseudo-op (on some 6502 assemblers this pseudo-op is called DB or DFB, consult the assembler documentation) as follows:
.BYTE $BD,$80,$00 ; LDA $0080,X (absolute,X addressing mode)
The comment is optional, but highly recommended for clarity.
In cases where you are writing code that will be relocated you must consider wrap-around when assigning dummy values for addresses that will be adjusted. Both zero and the semi-standard $FFFF should be avoided for dummy labels. The use of zero or zero page values will result in assembled code with zero page opcodes when you wanted absolute codes. With $FFFF, the problem is in addresses+1 as you wrap around to page 0.
Program Counter
When the 6502 is ready for the next instruction it increments the program counter before fetching the instruction. Once it has the op code, it increments the program counter by the length of the operand, if any. This must be accounted for when calculating branches or when pushing bytes to create a false return address (i.e. jump table addresses are made up of addresses-1 when it is intended to use an RTS rather than a JMP).
The program counter is loaded least signifigant byte first. Therefore the most signifigant byte must be pushed first when creating a false return address.
When calculating branches a forward branch of 6 skips the following 6 bytes so, effectively the program counter points to the address that is 8 bytes beyond the address of the branch opcode; and a backward branch of $FA (256-6) goes to an address 4 bytes before the branch instruction.
Execution Times
Op code execution times are measured in machine cycles; one machine cycle equals one clock cycle. Many instructions require one extra cycle for execution if a page boundary is crossed; these are indicated by a + following the time values shown.
NOP (No OPeration)
Affects Flags: none
MODE SYNTAX HEX LEN TIM
Implied NOP $EA 1 2
NOP is used to reserve space for future modifications or effectively REM out existing code.
ORA (bitwise OR with Accumulator)
Affects Flags: N Z
MODE SYNTAX HEX LEN TIM
Immediate ORA #$44 $09 2 2
Zero Page ORA $44 $05 2 3
Zero Page,X ORA $44,X $15 2 4
Absolute ORA $4400 $0D 3 4
Absolute,X ORA $4400,X $1D 3 4+
Absolute,Y ORA $4400,Y $19 3 4+
Indirect,X ORA ($44,X) $01 2 6
Indirect,Y ORA ($44),Y $11 2 5+
+ add 1 cycle if page boundary crossed
Register Instructions
Affect Flags: N Z
These instructions are implied mode, have a length of one byte and require two machine cycles.
MNEMONIC HEX
TAX (Transfer A to X) $AA
TXA (Transfer X to A) $8A
DEX (DEcrement X) $CA
INX (INcrement X) $E8
TAY (Transfer A to Y) $A8
TYA (Transfer Y to A) $98
DEY (DEcrement Y) $88
INY (INcrement Y) $C8
ROL (ROtate Left)
Affects Flags: N Z C
MODE SYNTAX HEX LEN TIM
Accumulator ROL A $2A 1 2
Zero Page ROL $44 $26 2 5
Zero Page,X ROL $44,X $36 2 6
Absolute ROL $4400 $2E 3 6
Absolute,X ROL $4400,X $3E 3 7
ROL shifts all bits left one position. The Carry is shifted into bit 0 and the original bit 7 is shifted into the Carry.
ROR (ROtate Right)
Affects Flags: N Z C
MODE SYNTAX HEX LEN TIM
Accumulator ROR A $6A 1 2
Zero Page ROR $44 $66 2 5
Zero Page,X ROR $44,X $76 2 6
Absolute ROR $4400 $6E 3 6
Absolute,X ROR $4400,X $7E 3 7
ROR shifts all bits right one position. The Carry is shifted into bit 7 and the original bit 0 is shifted into the Carry.
RTI (ReTurn from Interrupt)
Affects Flags: all
MODE SYNTAX HEX LEN TIM
Implied RTI $40 1 6
RTI retrieves the Processor Status Word (flags) and the Program Counter from the stack in that order (interrupts push the PC first and then the PSW).
Note that unlike RTS, the return address on the stack is the actual address rather than the address-1.
RTS (ReTurn from Subroutine)
Affects Flags: none
MODE SYNTAX HEX LEN TIM
Implied RTS $60 1 6
RTS pulls the top two bytes off the stack (low byte first) and transfers program control to that address+1. It is used, as expected, to exit a subroutine invoked via JSR which pushed the address-1.
RTS is frequently used to implement a jump table where addresses-1 are pushed onto the stack and accessed via RTS eg. to access the second of four routines:
LDX #1
JSR EXEC
JMP SOMEWHERE
LOBYTE
.BYTE <ROUTINE0-1,<ROUTINE1-1
.BYTE <ROUTINE2-1,<ROUTINE3-1
HIBYTE
.BYTE >ROUTINE0-1,>ROUTINE1-1
.BYTE >ROUTINE2-1,>ROUTINE3-1
EXEC
LDA HIBYTE,X
PHA
LDA LOBYTE,X
PHA
RTS
SBC (SuBtract with Carry)
Affects Flags: N V Z C
MODE SYNTAX HEX LEN TIM
Immediate SBC #$44 $E9 2 2
Zero Page SBC $44 $E5 2 3
Zero Page,X SBC $44,X $F5 2 4
Absolute SBC $4400 $ED 3 4
Absolute,X SBC $4400,X $FD 3 4+
Absolute,Y SBC $4400,Y $F9 3 4+
Indirect,X SBC ($44,X) $E1 2 6
Indirect,Y SBC ($44),Y $F1 2 5+
+ add 1 cycle if page boundary crossed
SBC results are dependant on the setting of the decimal flag. In decimal mode, subtraction is carried out on the assumption that the values involved are packed BCD (Binary Coded Decimal).
There is no way to subtract without the carry which works as an inverse borrow. i.e, to subtract you set the carry before the operation. If the carry is cleared by the operation, it indicates a borrow occurred.
STA (STore Accumulator)
Affects Flags: none
MODE SYNTAX HEX LEN TIM
Zero Page STA $44 $85 2 3
Zero Page,X STA $44,X $95 2 4
Absolute STA $4400 $8D 3 4
Absolute,X STA $4400,X $9D 3 5
Absolute,Y STA $4400,Y $99 3 5
Indirect,X STA ($44,X) $81 2 6
Indirect,Y STA ($44),Y $91 2 6
Stack Instructions
These instructions are implied mode, have a length of one byte and require machine cycles as indicated. The "PuLl" operations are known as "POP" on most other microprocessors. With the 6502, the stack is always on page one ($100-$1FF) and works top down.
MNEMONIC HEX TIM
TXS (Transfer X to Stack ptr) $9A 2
TSX (Transfer Stack ptr to X) $BA 2
PHA (PusH Accumulator) $48 3
PLA (PuLl Accumulator) $68 4
PHP (PusH Processor status) $08 3
PLP (PuLl Processor status) $28 4
STX (STore X register)
Affects Flags: none
MODE SYNTAX HEX LEN TIM
Zero Page STX $44 $86 2 3
Zero Page,Y STX $44,Y $96 2 4
Absolute STX $4400 $8E 3 4
STY (STore Y register)
Affects Flags: none
MODE SYNTAX HEX LEN TIM
Zero Page STY $44 $84 2 3
Zero Page,X STY $44,X $94 2 4
Absolute STY $4400 $8C 3 4

View File

@ -0,0 +1,946 @@
A New Shape Subroutine for the Apple
by Richard T. Simoni Jr.
August 1983 (c) BYTE Publications Inc
Athletes pole-vault, race cars spin, and fighter planes
fire at enemy aircraft. Is this the real world? No, I'm
talking about fast, smooth animation on the Apple II
high-resolution graphics screen. In the past year, dozens of
new Apple II programs have achieved such awesome animation
capabilities that several years ago most Apple programmers
would scarcely have believed them possible. After trying
unsuccessfully to match the quality of the commercially
produced animation in my own assembly-language programs, I
realized that the problem stemmed from the standard Apple
shape subroutine that I was using to display the shapes I
wanted to animate.
Standard Hi-Res Package
The hi-res (high-resolution) graphics package I was
using is the standard package supplied by Apple Computer. It
once was supplied with all Apple II computers sold, and it
can now be found on the volume 3 disk of the Apple Software
Bank Contributed Programs, available from Apple dealers.
Indeed, this package was eventually incorporated into the
Applesoft language to add hi-res commands. Written in
machine language, the package includes subroutines to clear
the screen, plot a point, draw a line, and draw a shape on
the hi-res screen. Although the clear, plot, and line
subroutines work well in animation, the shape subroutine is
much too slow to allow shapes to move across the screen
quickly, smoothly, and without flickering.
The speed of the shape subroutine is the most important
factor in animation for two main reasons. First, the speed
with which the subroutine can plot the shape, erase it, and
plot it again in its next position limits how fast any shape
can move across the screen. Second, in a typical animation
scheme, a shape moves from one position to the next in four
phases, which correspond to the time required to plot the
shape, the time the shape remains on the screen, the time
required to erase the shape, and the time that the shape is
not on the screen at all. These four phases repeat each time
a shape moves to a new position. The time spent during each
phase of the process determines how fast the shape moves and
how smooth and flicker-free the animation looks. To maximize
the smoothness, the time used in plotting the shape, erasing
the shape, and leaving the shape off the screen must be
minimized, for the human eye perceives these phases as
contributing to the flicker of the image. On the other hand,
if the amount of time the eye sees the image whole on the
screen is significantly greater than the time required for
the other phases, the image appears to move smoothly across
the screen. Minimizing the time the image is totally off the
screen is not difficult, for all calculations for the next
plot can be done while the image is on the screen; when the
image is erased, it can then be immediately plotted in the
new position. The times required to plot and erase the
shape, however, are directly determined by the speed of the
image subroutine. If the subroutine is slow, the plot and
erase times are long, and the image appears to flicker as it
moves across the screen.
Representing Shapes
To understand why the standard Apple shape subroutine
is too slow for most animation purposes, you must know how
the subroutine works and especially how it expects a shape
to be represented in memory. A shape is represented by a
series of vectors in memory, with each vector specifying if
a given pixel should be turned on. It also specifies which
of the four adjacent pixels should be addressed by the next
vector. This scheme best suits the representation of simple,
single-line shapes such as those in figure 1. Unfortunately,
if a shape must be filled in or if the shape has any detail
drawn within its boundaries, as in figure 2, the shape's
representation is awkward and inefficient at best. In these
cases it is often necessary to overplot points and use many
vectors that specify motion without plotting. Moreover, if
the shape is large, the sheer size of the vector table
becomes unwieldy. When the time comes to plot these shapes,
the subroutine steps through the table, and each vector
takes up a certain amount of time. If the vector table
represents the shape inefficiently, the end result is wasted
time in the plotting of the shape.
Similarly contributing to the slow speed of the shape
subroutine is the inclusion of scaling and rotation factors.
In order to plot a shape, a calling routine must specify a
scaling factor that determines the plotted shape's size
(actual size, double size, triple size, etc.) and a rotation
factor that determines the angle through which the shape is
rotated before plotting. Although these factors are useful
in some applications, using them with shape animation rarely
produces satisfying results, and these calculations slow the
subroutine considerably.
A New Shape Subroutine
After realizing that the speed bottleneck in my
programs was caused by the shape subroutine, I went about
designing my own subroutine with two criteria in mind.
First, the subroutine had to be high speed to minimize image
flicker, and second, the method of representing a shape in
memory had to allow complicated images to be plotted as
quickly as simple single-line shapes of the same overall
size. One way to meet these criteria is to use a bit picture
to represent the shape in memory. In other words, the shape
is represented in main memory in the same form in which it
is ultimately represented in the hi-res screen memory when
the shape is plotted on the hi-res screen. Plotting the
shape is then simple and fast: the bytes representing the
shape in main memory need only be transferred to the hi-res
screen memory. I used this technique in writing a fast shape
subroutine suitable for animation.
The table of bytes that make up the bit picture is
called the shape table. A shape table is most easily formed
through the use of the shape-editor program presented later
in this article. To form a shape table manually, start by
drawing the shape on a piece of graph paper with one pixel
per square, as in figure 3a. Use 1s to represent on pixels
and 0s to represent off pixels. Draw the smallest possible
rectangle that still encloses the entire figure. Then split
each line of binary digits enclosed by the rectangle into
7-bit groups. If, as in figure 3b, the last group doesn't
have afull 7 bits, add enough 0s to the end of each line to
bring the total up to 7 bits. Due to limitations to the
subroutine, no more than seven 7-bit groups per line are
allowed. Reverse the order of the bits in each group, as
shown in figure 3c. Convert each new 7-bit group into its
hexadecimal or decimal equivalent, whichever is preferred
(figure 3d shows the hexadecimal equivalent) and, reading
across each line left to rightfrom the top to the bottom
line, recopythe list of numbers in table (linear) form. The
table is now complete except for two bytes that belong at
the top of the table. The first of these butes represents
the height of the shaps -- in other words, the number of
lines of digits in figure 3b; the second byte represents the
width of the shape in 7-bit groups that is, the number of
7-bit groups used in each line in figure 3b. As previously
mentioned, this width should be no more than seven groups.
The complete table in hexadecimal form for the sample shape
used in figure 3 is as follows:
05 02 78 07 14 04 12 02
11 01 7F 00
The shape subroutine itself is shown in listing 1, and
the lookup tables used by the subroutine are shown in
listing 2. Before calling the subroutine, several registers
and memory locations must be set up with certain parameters,
including the hi-res screen coordinates of the pixel where
the upper left corner of the bit picture should be
positioned. The low-order byte of the xcoordinate should be
placed in theX register, and the corresponding highorder
byte of the x coordinate (either 1 or 0) goes in the Y
register. The ycoordinate goes in the A
register(accumulator). The low- and high-order bytes of the
shape-table starting address should be stored in hexadecimal
memory locations EB and EC, respectively. The subroutine can
then be called with the usual JSR instruction. A summary of
the parameter setup is given in table 1.
The subroutine works by taking the exclusive-OR of each
affected bit in page-1 hi-res screen memory with the
corresponding bit of the bit picture. This exclusive-OR
plotting has several advantages. First, a color need not be
specified; the shape is drawn by calling the subroutine once
and is erased by simply calling it again with the same
screen coordinates. Second, any shape drawn using
exclusive-OR plotting is nondestructive; that is, whatever
the shape happens to plot over is restored when the shape is
erased. This property can be used to form interesting
backgrounds that need not be redrawn after shapes are
plotted and moved on top of them. Cross-hair cursors are
also free to move around without destroying the screen's
previous contents.
Several details about the subroutine need to be
explained. Zero page (hexadecimal locations 00 through FF)
of memory is used for temporary storage; the particular
locations used were chosen to avoid destruction of locations
used by the Apple Monitor, Applesoft, Integer Basic, and the
DOS (disk operating system). The subroutine does not operate
correctly without the tables shown in listing 2. These
tables may be stored anywhere in memory, but are best
located immediately after the subroutine in memory. Three
pertinent Text continued from page 303: tables are named
QUOTBL, LOSTRT, and HISTRT. QUOTBL is a lookup table used
internally by the subroutine to divide the x-coordinate by
7. LOSTRT and HISTRT are each 192 bytes long, and they
contain the low- and high-order bytes of the address of the
leftmost byte of each y-coordmate in page 1 of hi-res screen
memory. For plotting on page 2 of the hi-res memory, a
hexadecimal 20 should be added to each byte in the table
HISTRT Although I wanted the subroutine to be fully
relocatable, I compromised this requirement in favor of
additional speed. However, as I have written it, relocating
the subroutine requires changing only the two locations
referencing QUOTBL in lines 38 and 41 of listing 1.
A Note on Color
One of the most difficult aspects of using the Apple
high-resolution graphics mode is trying to control the color
of objects displayed on the screen. This difficulty arises
because a color cannot be individually assigned to each
pixel on the screen; the color depends instead on such
factors as whether an object is drawn with pixels
horizontally alternating between on and off and whether the
on pixels have even or odd x coordinates. Through careful
programming and shape-table composition, you can control
colors in this manner using the shape subroutine presented
in this article. In newer Apples, however, two more colors
are added to the hi-res screen by defin-. ing the previously
unused high-order bit in each word in hi-res screen memory.
Unfortunately, these colors cannot be easily displayed using
the shape subroutine because the subroutine forces the extra
bit in the hi-res screen to 0. For a complete description of
color in the Apple hi-res screen, see page 19 of the Apple
II Reference Manual (Cupertino: Apple Computer Inc., 1979).
The Shape-Editor Program
Although it is not difficult to form the shape table
for a given shape, it is often time consuming. When writing
a program that uses shapes, you rarely know in advance the
exact pixel pattern that makes up the shape. Even if you
know the pattern, you're probably not sure whether the shape
will look good on the hi-res screen. It might take you hours
to develop a suitable shape if you have to write out each
trial on graph paper, form the shape table, and use the
subroutine to display the shape before you can tell if it is
satisfactory. This time-consuming method can bring the
creative process to a halt. A more desirable situation would
be one in which you could easily experiment with different
shapes on the hi-res screen until you were satisfied with
the results and then form the shape table directly from the
screen image. I had this concept in mind when writing the
shape-editor program shown in listing 3. The program
features complete hi-res editing, both actual size and a
blown-up view of the shape being drawn, disk storage of the
current shape (the source file) for future editing, and
assembly of a shape table from any portion of the current
screen.
The editor program requires an Apple II with 32K bytes
of memory, a disk drive, and Applesoft in ROM (read-only
memory). When you run the program, the list of commands
shown in photo 1 comes up on the screen. After you press the
space bar, the left area of the screen becomes blank, and a
grid appears on the right. The blank area is the "slate" on
which you can draw different shapes actual size. Anything
drawn also appears enlarged on the grid, making it easier to
see details of the shape. Once the grid has been drawn, a
small horizontal line appears in one of the small squares in
the grid. This is the cursor, which always shows the current
drawing position of the program.
Once the cursor appears on the screen, you can execute
any of the commands listed in the menu (photo 1) by pressing
the corresponding letter on the keyboard. The letters I, J,
K, and M are used for moving the graphics cursor up, left,
right, and down, respectively. The Plot command plots a
point at the current cursor position, and the Erase command
erases the point at the current cursor position. Neither the
Plot nor the Erase command causes any harm if the command
has already been used at the cursor position (e.g., if the
Plot command is used at a position where a point already
exists). The Clear command clears the screen after prompting
you to verify that the screen should indeed be cleared. By
using the cursor-movement, Plot, Erase, and Clear commands,
you can draw the desired shape on the screen and modify it
as many times as necessary. A shape being drawn in this
screen-edit mode is shown in photo 2.
With the Table command, you can make a shape table from
any segment of the screen where you have drawn a shape.
After choosing the Table command by pressing the T key, you
must choose the smallest rectangle that encloses the shape;
this is the same rectangle chosen when forming the shape
table manually as previously described. You specify the
boundaries of this rectangle by moving the cursor to the
upper left position of the rectangle and pressing the Return
key and then doing the same for the lower right corner of
the rectangle. The corners are inclusive; that is, the rows
and columns that contain the corners become the outermost
edges included in the shape table. A portion of the
rectangle selection process is shown in photo 3. After you
select the desired rectangle, the program will form the
shape table. The time this takes (typically 15 to 30
seconds) depends on the size of the shape. The completed
shape table is displayed on the screen in either decimal or
hexadecimal form, depending on how you answer a prompt. The
program will then save this object-file shape table on disk
as a standard binary file if you so desire. You are then
asked whether to return to the screen-edit mode or end the
program. Photo 4 shows the final shape table formed from the
sample shape used in photo 3.
The Save and Get commands let you store on disk and
later retrieve any picture drawn in the screen-edit mode.
The Save command prompts you for a file name and then saves
to disk a representation of the shape drawn on the grid. The
Get command can then be used to retrieve and display the
picture as long as the saved file remains on disk. Because
the Get command erases any drawing previously on the screen,
you are first asked to confirm that a file is to be loaded.
Once the picture is retrieved, it can be modified or
assembled into a shape table just as if the picture had been
entered using the keyboard commands.
The Help command (executed by pressing the H or ? key)
returns you from the screen-edit mode to the menu shown at
the beginning of the program for a quick command-letter
check. Pressing the space bar returns you to screen-edit
mode with the contents of the screen unaltered. The Quit
command ends the program. Because any drawing on the screen
is lost once the program is ended, you are asked to confirm
the Quit directive.
Summing Up
Using the techniques and programs described in this
article, you can implement professional-looking animation on
the Apple without having to work around the limitations of
the standard Apple shape subroutine. Although I wrote my
shape subroutine with animation in mind, the subroutine is
useful in any graphics applications where detailed shapes
must be drawn. Using the graphics editor as a development
tool, virtually any shape can be easily displayed on the
hi-res screen.
Richard T Simoni Jr. (29 Farnham Park Dr., Houston, TX
77024) is currently enrolled as a senior electrical
engineering/computer science/math science major at Rice
University in Houston, Texas.
Figure 1: Because they are easily represented in memory by a
series of vectors, these simple single-line closed shapes
are suitable for display by the standard Apple shape
subroutine on the hi-res graphics screen.
(1a) (1b) (1c)
........... ........... ...............
........... ........... ....*******....
..*******.. ..*........ ...*.......*...
..*.....*.. ..**....... ..*.........*..
..*.....*.. ..*.*...... .*...........*.
..*.....*.. ..*..*..... .*...........*.
..*.....*.. ..*...*.... .*...........*.
..*******.. ..*....*... .*...........*.
........... ..*******.. .*...........*.
........... ........... .*...........*.
........... .*...........*.
.*************.
...............
Figure 2: The detail within these shapes makes their
representation as vectors in memory inefficient; therefore,
the standard Apple shape subroutine is neither well suited
nor easy to use for the display of these shapes on the
hi-res screen.
(2a) (2b) (2c)
........... ..................... ...............
........... .***************..... ....*******....
..*******.. .*............*.*.... ...*.......*...
..*******.. .*............*..*... ..*.........*..
..*******.. .*............*...*.. .*...........*.
..*******.. .*............******. .*************.
..*******.. .*.................*. .*...........*.
..*******.. .*******************. .*...........*.
........... ..*****.......*****.. .*....***....*.
........... ...***.........***... .*....*.*....*.
..................... .*....*.*....*.
.*************.
...............
Figure 3: To form a shape table, start by drawing the
desired shape on graph paper, using 1s and 0s to represent
"on" and "off" pixels (3a). Next, split each line of bits
into 7-bit groups, padding the last group of each line with
0s if necessary (3b). Then, reverse the order of the binary
digits in each 7-bit group (3c) and convert to hexadecimal
(3d). Later you must add height and width bytes as described
in the text.
(3a) (3b) (3c)
............ split|here 1111000 0000111
............ v 0010100 0000100
....*******. 000000000000 0010010 0000010
...*.*....*. 000000000000 0010001 0000001
..*..*...*.. 000011111110 1111111 0000000
.*...*..*... 000101000010
.*******.... 001001000100
............ 010001001000
............ 011111110000
000000000000
000000000000 (3d)
| |\ | 78 07
0001111 1110000 14 04
0010100 0010000 12 02
0100100 0100000 11 01
1000100 1000000 7F 00
1111111 0000000
| |
added
zeros
Listing 1: A fast shape subroutine that plots
high-resolution shapes on the Apple II.
0000: 1 OBJ $1B00
1B00: 2 ORG $1B00 ;ASSEMBLY LOCATION
1B00: 3 *********************************************************
1B00: 4 * SHAPE SUBROUTINE WRITTEN BY RICHARD T. SIMONI, JR. *
1B00: 5 * *
1B00: 6 * SHAPE WORKS BY STEPPING THROUGH THE USER TABLE ONE *
1B00: 7 * HI-RES LINE AT A TIME, SHIFTING THE BIT PATTERN THE *
1B00: 8 * APPROPRIATE NUMBER OF TIMES (DEPENDING ON THE *
1B00: 9 * X-COORDINATE PASSED IN THE X- AND Y-REGISTERS), AND *
1B00: 10 * MOVING THE PATTERN TO THE PROPER PLACE IN THE HI-RES *
1B00: 11 * SCREEN MEMORY. *
1B00: 12 *********************************************************
1B00: 13 STARTZ EQU $19 ;START OF LINE STORAGE
1B00: 14 YCOORD EQU $E3 ;LINE COUNTER
1B00: 15 START EQU $EB ;USER TABLE POINTER
1B00: 16 ADDRL EQU $ED ;1ST SCREEN BYTE TO USE
1B00: 17 ADDRH EQU $EE ; IN LINE COORD
1B00: 18 ADDRADD EQU $EF ;OFFSET FROM LEFT BYTE
1B00: 19 SHFTNUM EQU $F9 ;NUMBER OF SHIFTS
1B00: 20 ENDLN EQU $FD ;LAST LINE + 1
1B00: 21 WIDTH EQU $FB ;WIDTH OF USER TABLE
1B00: 22 INDEX EQU $FC ;POINTER IN USER TABLE
1B00: 23 *
1B00: 24 * DIVIDE X-COORD BY 7 TO GET BYTE OFFSET FROM LEFTMOST
1B00: 25 * BYTE IN ANY HI-RES LINE. REMAINDER WILL BE CORRECT
1B00: 26 * NUMBER OF SHIFTS TO PERFORM ON BIT PATTERN.
1B00: 27 * DIVISION IS PERFORMED USING LOOKUP TABLE FOR SPEED.
1B00: 28 *
1B00: 85 E3 29 STA YCOORD ;STORE Y-COORD (COUNTER)
1B02: 8A 30 TXA
1B03: 0A 31 ASL A
1B04: AA 32 TAX
1B05: 98 33 TYA
1B06: 2A 34 ROL A
1B07: A8 35 TAY ;MULTIPLY X-COORD BY TWO
1B08: 18 36 CLC
1B09: 8A 37 TXA ;A-REG = X-COORD*2 LO-BYTE
1B0A: 69 83 38 ADC #>QUOTBL ;ADD TABLE ADDRESS LO-BYTE
1B0C: 85 ED 39 STA ADDRL ;STORE RESULT
1B0E: 98 40 TYA ;A-REG = X-COORD*2 HI-BYTE
1B0F: 69 18 41 ADC #<QUOTBL ;ADD TABLE ADDRESS HI-BYTE
1B11: 85 EE 42 STA ADDRH ;STORE RESULT
1B13: A0 00 43 LDY #$00 ;ZERO Y-REG FOR INDEXING
1B15: B1 ED 44 LDA (ADDRL),Y ;LOAD X-COORD/7 FROM TABLE
1B17: 85 EF 45 STA ADDRADD ;ADDRADD = XCOORD/7
1B19: C8 46 INY ;REMAINDER FOLLOWS IN TABLE
1B1A: B1 ED 47 LDA (ADDRL),Y ;LOAD REMAINDER FROM TABLE
1B1C: 85 F9 48 STA SHFTNUM ;SHFTNUM = REMAINDER
1B1E: 49 *
1B1E: 50 * INITIALIZE LOCATIONS ENDLN AND WIDTH. ENDLN CONTAINS
1B1E: 51 * THE Y-COORD OF THE LAST LINE + 1. WIDTH CONTAINS THE
1B1E: 52 * WIDTH (IN BYTES) OF EACH LINE.
1B1E: 53 *
1B1E: A5 E3 54 LDA YCOORD
1B20: A0 00 55 LDY #$00
1B22: 18 56 CLC
1B23: 71 EB 57 ADC (START),Y
1B25: 85 FD 58 STA ENDLN ;ENDLN = Y-COORD+LENGTH
1B27: C8 59 INY
1B28: B1 EB 60 LDA (START),Y
1B2A: 85 FB 61 STA WIDTH ;GET & STORE WIDTH
1B2C: C8 62 INY
1B2D: 84 FC 63 STY INDEX ;INDEX=2
1B2F: 64 *
1B2F: 65 * LOOP1 IS THE LOOP THAT IS CYCLED THROUGH ONCE FOR EACH
1B2F: 66 * LINE ON THE HI-RES SCREEN
1B2F: 67 *
1B2F: A6 FB 68 LOOP1 LDX WIDTH ;X-REG=0 (COUNTER)
1B31: A4 FC 69 LDY INDEX
1B33: 70 *
1B33: 71 * MOVE BYTES FOR LINE YCOORD FROM USER TABLE TO ZERO PAGE
1B33: 72 *
1B33: B1 EB 73 LOOP2 LDA (START),Y ;GET XTH BYTE OF LINE
1B35: 95 19 74 STA STARTZ,X ;STORE IN STARTZ+X
1B37: C8 75 INY
1B38: CA 76 DEX ;MOVED ALL BYTES YET?
1B39: D0 F8 77 BNE LOOP2 ;NO, LOOP
1B3B: 86 19 78 STX STARTZ ;STARTZ=0
1B3D: 84 FC 79 STY INDEX
1B3F: 80 *
1B3F: 81 * SHIFT THE BIT PATTERN SHIFTNUM TIMES
1B3F: 82 *
1B3F: A4 F9 83 LDY SHFTNUM ;IS SHFTNUM=0?
1B41: F0 16 84 BEQ SKIP ;YES, SKIP THE SHIFTING
1B43: 18 85 LOOP3 CLC ;NO, START SHIFTING
1B44: A6 FB 86 LDX WIDTH
1B46: 08 87 PHP ;KEEP STACK IN ORDER
1B47: 28 88 LOOP4 PLP ;RESTORE CARRY
1B48: B5 19 89 LDA STARTZ,X ;LOAD ORIGINAL PATTERN
1B4A: 2A 90 ROL A
1B4B: 2A 91 ROL A ;ROTATE LEFT TWICE
1B4C: 08 92 PHP ;SAVE CARRY
1B4D: 4A 93 LSR A ;SHIFT RIGHT ONCE
1B4E: 95 19 94 STA STARTZ,X ;STORE SHIFTED PATTERN
1B50: CA 95 DEX
1B51: E0 FF 96 CPX #$FF ;ROTATED EACH BYTE?
1B53: D0 F2 97 BNE LOOP4 ;NO, LOOP
1B55: 28 98 PLP ;KEEP STACK IN ORDER
1B56: 88 99 DEY
1B57: D0 EA 100 BNE LOOP3 ;LOOP IF Y<>0
1B59: 101 *
1B59: 102 * CALCULATE HI-RES SCREEN ADDRESS FOR FIRST BYTE TO
1B59: 103 * BE USED IN LINE YCOORD
1B59: 104 *
1B59: A4 E3 105 LDY YCOORD
1B5B: B9 B3 1D 106 LDA LOSTRT,Y
1B5E: 18 107 CLC
1B5F: 65 EF 108 ADC ADDRADD
1B61: 85 ED 109 STA ADDRL
1B63: B9 73 1E 110 LDA HISTRT,Y
1B66: 69 00 111 ADC #$00
1B68: 85 EE 112 STA ADDRH ;GET ADDR FOR 1ST BYTE
1B6A: 113 *
1B6A: 114 * MOVE SHIFTED BYTES FROM ZERO PAGE TO HI-RES SCREEN
1B6A: 115 * MEMORY. FOR NON-EXCLUSIVE-OR PLOTTING, CHANGE
1B6A: 116 * 'EOR (ADDRL),Y' TO 'ORA (ADDRL),Y' (OPCODE $11).
1B6A: 117 *
1B6A: A0 00 118 LDY #$00
1B6C: A6 FB 119 LDX WIDTH
1B6E: B5 19 120 LOOP5 LDA STARTZ,X
1B70: F1 ED 121 SBC (ADDRL),Y
1B72: 91 ED 122 STA (ADDRL),Y ;PLOT BYTE ON SCREEN
1B74: C8 123 INY
1B75: CA 124 DEX
1B76: E0 FF 125 CPX #$FF ;THROUGH PLOTTING LINE?
1B78: D0 F4 126 BNE LOOP5 ;NO, LOOP
1B7A: E6 E3 127 INC YCOORD ;YES, GO TO NEXT LINE
1B7C: A5 E3 128 LDA YCOORD
1B7E: C5 FD 129 CMP ENDLN ;MORE LINES?
1B80: D0 AD 120 BNE $1B2F ;YES, LOOP
1B82: 60 131 RTS ;NO, RETURN
1B83: 132 QUOTBL EQU *
1B83: 133 LOSTRT EQU *+560
1B83: 134 HISTRT EQU *+752
*** SUCCESSFUL ASSEMBLY: NO ERRORS
Listing 2: Lookup tables used by the listing 1 subroutine.
1b83: 00 00 00 01 00 02 00 03 00 04 00 05 00
1b90: 06 01 00 01 01 01 02 01 03 01 04 01 05 01 06 02
1ba0: 00 02 01 02 02 02 03 02 04 02 05 02 06 03 00 03
1bb0: 01 03 02 03 03 03 04 03 05 03 06 04 00 04 01 04
1bc0: 02 04 03 04 04 04 05 04 06 05 00 05 01 05 02 05
1bd0: 03 05 04 05 05 05 06 06 00 06 01 06 02 06 03 06
1be0: 04 06 05 06 06 07 00 07 01 07 02 07 03 07 04 07
1bf0: 05 07 06 08 00 08 01 08 02 08 03 08 04 08 05 08
1c00: 06 09 00 09 01 09 02 09 03 09 04 09 05 09 06 0a
1c10: 00 0a 01 0a 02 0a 03 0a 04 0a 05 0a 06 0b 00 0b
1c20: 01 09 02 0b 03 0b 04 0b 05 0b 06 0c 00 0c 01 0c
1c30: 02 0c 03 0c 04 0c 05 0c 06 0d 00 00 01 00 02 00
1c40: 03 00 04 00 05 00 06 08 00 0e 01 0e 02 0e 03 0e
1c50: 04 0e 05 0e 06 0f 00 0f 01 0f 02 0f 03 0f 04 0f
1c60: 05 0f 06 10 00 10 01 10 02 10 03 10 04 10 05 10
1c70: 06 11 00 11 01 11 02 11 03 11 04 11 05 11 06 12
1c80: 00 12 01 12 02 12 03 12 04 12 05 12 06 13 00 13
1c90: 01 13 02 13 03 13 04 13 05 13 06 14 00 14 01 14
1ca0: 02 14 03 14 04 14 05 14 06 15 00 15 01 15 02 15
1cb0: 03 15 04 15 05 15 06 16 00 16 01 16 02 16 03 16
1cc0: 04 16 05 16 06 17 00 17 01 17 02 17 03 17 04 17
1cd0: 05 17 06 18 00 18 01 18 02 18 03 18 04 18 05 18
1ce0: 06 19 00 19 01 19 02 19 03 19 04 19 05 19 06 1a
1cf0: 00 1a 01 1a 02 1a 03 1a 04 1a 05 1a 06 1b 00 1b
1d00: 01 1b 02 1b 03 1b 04 18 05 18 06 1c 00 1c 01 1c
1d10: 02 1c 03 1c 04 1c 05 1c 06 10 00 10 01 10 02 10
1d20: 03 10 04 10 05 10 06 1e 00 1e 01 1e 02 1f 03 1f
1d30: 04 1e 05 1e 06 1f 00 1f 01 1f 02 1f 03 1f 04 1f
1d40: 05 1f 06 20 00 20 01 20 02 20 03 20 04 20 05 20
1d50: 06 21 00 21 01 21 02 21 03 21 04 21 05 21 06 22
1d60: 00 22 01 22 02 22 03 22 04 22 05 22 06 23 00 23
1d70: 01 23 02 23 03 23 04 23 05 23 06 24 00 24 01 24
1d80: 02 24 03 24 04 24 05 24 06 25 00 25 01 25 02 25
1d90: 03 25 04 25 05 25 06 26 00 26 01 26 02 26 03 26
1da0: 04 26 05 26 06 27 00 27 01 27 02 27 03 27 04 27
1db0: 05 27 06 00 00 00 00 00 00 00 00 80 80 80 80 80
1dc0: 80 80 80 00 00 00 00 00 00 00 00 80 80 80 80 80
1dd0: 80 80 80 00 00 00 00 00 00 00 00 80 80 80 80 80
1de0: 80 80 80 00 00 00 00 00 00 00 00 80 80 80 80 80
1df0: 80 80 80 28 28 28 28 28 28 28 28 a8 a8 a5 a5 a5
1e00: a8 a8 a8 28 28 28 28 28 28 28 28 a8 a8 a8 a8 a8
1e10: a5 a8 a8 28 28 28 28 28 28 28 28 a5 a8 a5 a5 a5
1e20: a5 a5 a8 28 28 28 28 28 28 28 28 a8 a5 a8 a8 a8
1e30: a8 a8 a5 50 50 50 50 50 50 50 50 d0 00 00 d0 d0
1e40: d0 d0 d0 50 50 50 50 50 50 50 50 d0 d0 d0 d0 d0
1e50: 00 00 00 50 50 50 50 50 50 50 50 00 d0 dd d0 d0
1e60: d0 d0 d0 50 50 50 50 50 50 50 50 d0 d0 d0 d0 d0
1e70: d0 d0 d0 20 24 28 2c 30 34 38 3c 20 24 28 2c 30
1e80: 34 38 3c 21 25 29 20 31 35 39 3d 21 25 29 20 31
1e90: 35 39 30 22 26 2a 26 32 36 3a 3e 22 26 2a 2f 32
1ea0: 36 3a 3e 23 27 29 2f 33 37 3b 3f 23 27 2b 2f 33
1eb0: 37 3b 36 20 24 28 2c 30 34 38 3c 20 24 28 2c 30
1ec0: 34 38 3c 21 25 29 20 31 35 39 3d 21 25 29 20 31
1ed0: 35 39 3d 22 26 2a 2e 32 36 3a 3e 22 26 2a 26 32
1ee0: 36 3a 36 23 27 2b 2f 33 37 3b 3f 23 27 2b 2f 33
1ef0: 37 38 36 20 24 28 2c 30 34 35 3c 20 24 28 2c 30
1f00: 34 38 3c 21 25 29 20 31 35 39 3d 21 25 29 2d 31
1f10: 35 39 3d 22 26 2a 2e 32 36 3a 3e 22 26 2a 2e 32
1f20: 36 3a 36 23 27 28 2f 33 37 3b 3f 23 27 2b 2f 33
1f30: 37 38 3f
Listing 3: This shape-editor program forms a shape table
directly from a high-resolution screen image.
100 TEXT:HOME:POKE-16298,0:POKE-16300,0
110 RESTORE:FOR I=768 TO 774:READ J:POKE I,J:NEXT I:POKE 232,0:POKE 233,3:DATA1,0,3,0,45,5,0
120 DIM S%(105),T%(212)
130 XMAX=42:YMAX=35:ML=101:MT=10
140 H$="0123456789ABCDEF"
150 D$=CHR$(4)
160 GOSUB 3100:GOSUB 3300:GOSUB 3400
400 REM SHOW CURSOR POSITION ON GRID
410 XDRAW 1 AT CL+1,CT+3
420 REM WAIT FOR KEYBOARD COMMAND
430 Q=PEEK(-16384):IF Q<128 THEN 430
440 POKE-16368,0:Q=Q-128
500 REM
501 REM CURSOR MOVEMENT COMMANDS
502 REM
510 IF Q<>ASC("I") THEN 550
520 XDRAW 1 AT CL+1,CT+3
530 IF Y>1 THEN Y=Y-1:CT=CT-4
540 GOTO 410
550 IF Q<>ASC("M") THEN 590
560 XDRAW 1 AT CL+1,CT+3
570 IF Y<YMAX THEN Y=Y+1:CT=CT+4
580 GOTO 410
590 IF Q<>ASC("J") THEN 630
600 XDRAW 1 AT CL+1,CT+3
610 IF X>1 THEN X=X-1:CL=CL-4
620 GOTO 410
630 IF Q<>ASC("K") THEN 700
640 XDRAW 1 AT CL+1,CT+3
650 IF X<XMAX THEN X=X+1:CL=CL+4
660 GOTO 410
700 REM
701 REM PLOT COMMAND
702 REM
710 IF Q<>ASC("P") THEN 810
720 ELE=INT((X-1)/14)+3*(Y-1)
730 BIT=(X-1)-INT((X-1)/14)*14
740 A=INT(S%(ELE)/2^BIT)
750 IF A/2<>INT(A/2) THEN 810
760 S%(ELE)=S%(ELE)+2^BIT
770 FOR I=2 TO 4:XDRAW 1 AT CL+1,CT+I:NEXT I
780 HCOLOR=3:HPLOT 29+X,62+Y
790 GOTO 430
800 REM
801 REM ERASE COMMAND
802 REM
810 IF Q<>ASC("E") THEN 900
820 ELE=INT((X-1)/14)+3*(Y-1)
830 BIT=(X-1)-INT((X-1)/14)*14
840 A=INT(S%(ELE)/2^BIT)
850 IF A/2=INT(A/2) THEN 900
860 S%(ELE)=S%(ELE)-2^BIT
870 FOR I=2 TO 4:XDRAW 1 AT CL+1,CT+I:NEXT I
880 HCOLOR=0:HPLOT 29+X,62+Y
890 GOTO 430
900 REM
901 REM CLEAR SCREEN COMMAND
902 REM
910 IF Q<>ASC("C") THEN 1030
920 XDRAW 1 AT CL+1,CT+3
930 VTAB 23:PRINT"SURE YOU WANT TO ERASE THE SCREEN?"
940 GOSUB 3500
950 VTAB 22:CALL-958: IF Q<>ASC("Y") THEN 410
960 FOR I=0 TO 105:S%(1)=0:NEXT I
970 GOSUB 3300:GOSUB 3400:GOTO 410
1000 REM
1010 REM TABLE COMMAND
1020 REM
1030 IF Q<>ASC("T") THEN 1520
1040 VTAB 23:PRINT"SET CURSOR TO TOP LEFT CORNER OF":PRINT"DESIRED BIT MAP AND HIT RETURN";
1050 L5=1
1060 GOTO 430
1070 PL=X:PT=Y
1080 VTAB 22: CALL-958:PRINT:PRINT"SET CURSOR TO BOTTOM RIGHT CORNER OF":PRINT"DESIRED BIT MAP AND HIT RETURN";
1090 L5=2
1100 GOTO 430
1110 PR=X:PB=Y:L5=0
1120 XDRAW 1 AT CL+1,CT+3
1130 VTAB 22:CALL-958
1140 IF PL>PR OR PT>PB THEN VTAB 23:HTAB 1:POKE 50,63:PRINT"ILLEGAL BIT MAP CORNERS":POKE 50,255:FOR I=1 TO 2000:NEXT I:VTAB 22:CALL-958:GOTO 410
1150 VTAB 23:HTAB 1:PRINT"NOW FORMING SHAPE TABLE"
1160 FOR I=0 TO 212:T%(I)=0:NEXT I
1170 L=PB-PT+1:W=(PR-PL+1)/7:IF W<>INT(W) THEN W=INT(W)+1
1180 T%(0)=L:T%(1)=W:N=2:Q=0
1190 FOR Y=PT TO PB
1200 FOR X=PL TO PL+W*7-1
1210 IF X>PR THEN BN=0:GOTO 1250
1220 ELE=INT((X-1)/14)+3*(Y-1)
1230 BIT=(X-1)-INT((X-1)/14)*14
1240 BN=0:A=INT(S%(ELE)/2^BIT):IF INT(A/2)<>A/2 THEN BN=1
1250 IF BN=1 THEN T%(N)=T%(N)+2^Q
1260 Q=Q+1:IF Q=7 THEN Q=0:N=N+1
1270 NEXT X:NEXT Y
1280 HOME:POKE-16303,0
1290 VTAB 2:PRINT"DO YOU WANT TO SEE THE TABLE IN HEX":PRINT" OR IN DECIMAL?":PRINT:PRINT
1300 GOSUB 3500
1310 IF Q<>ASC("D") AND Q<>ASC("H") THEN 1280
1320 Z=0:FOR I=0 TO L*W+1
1330 Z=Z+1
1340 IF Q=ASC("D") THEN PRINT TAB(Z*4);T%(I);:GOTO 1360
1350 PRINT TAB(Z*3);MID$(H$,INT(T%(1)/16)+1,1);MID$(H$,T%(I)-INT(T%(I)/16)*16+1,1);
1360 IF Z=8 THEN Z=0:PRINT
1370 NEXT I
1380 PRINT:PRINT:IF PEEK(37)<21 THEN POKE 34,PEEK(17)
1390 PRINT"DO YOU WANT TO SAVE THE OBJECT TABLE":PRINT" ON DISK?"
1400 GOSUB 3500
1410 IF Q<>ASC("Y") THEN 1470
1420 PRINT:PRINT"WHAT DO YOU WANT TO NAME":INPUT" THE FILE? ";N$
1430 FOR I=0 TO L*W+1:POKE 16384+I,T%(I):NEXT I
1440 PRINT D$;"BSAVE";N$;",A$4000,L";L*W+2
1450 PRINT"FILE SAVED USING NAME ";N$
1460 PRINT:PRINT:GOTO 1390
1470 POKE 34,0:HOME:VTAB 2:PRINT"DO YOU WANT TO RETURN TO THE":PRINT"SCREEN EDIT MODE?"
1480 GOSUB 3500
1490 IF Q<>ASC("Y") THEN 2260
1500 GOSUB 3100:POKE-16304,0:GOSUB 3310:GOTO 410
1510 REM 'RETURN' PSEUDO-COMMAND
1520 IF Q<>13 THEN 1600
1530 ON L5+1 GOTO 430,1070,1110
1600 REM
1601 REM SAVE TABLE COMMAND
1602 REM
1610 IF Q<>ASC("S") THEN 1800
1620 XDRAW 1 AT CL+1,CT+3
1630 VTAB 23:INPUT"FILE NAME FOR SAVE? ";N$
1640 VTAB 24:PRINT"NOW SCANNING IMAGE";:HTAB 1
1650 Z1=0
1660 IF S%(Z1)=0 AND Z1<105 THEN Z1=Z1+1:GOTO 1660
1670 Z2=105
1680 IF S%(Z2)=0 AND Z2>0 THEN Z2=Z2-1:GOTO 1680
1690 IF Z1>Z2 THEN Z1=0:Z2=1
1700 VTAB 24:PRINT"NOW SAVING IMAGE TO DISK";:VTAB 23:PRINT
1710 PRINT D$;"OPEN";N$:PRINT D$;"WRITE";N$
1720 PRINT Z1:PRINT Z2
1730 FOR I=Z1 TO Z2
1740 PRINT S%(I)
1750 NEXT I
1760 PRINT D$;"CLOSE";N$
1770 VTAB 22:CALL-958:GOTO 410
1800 REM
1801 REM LOAD TABLE COMMAND
1802 REM
1810 IF Q<>ASC("G") THEN 2100
1820 XDRAW 1 AT CL+1,CT+3
1830 VTAB 23:PRINT"SURE YOU WANT TO LOAD?"
1840 GOSUB 3500
1850 VTAB 22:CALL-958:IF Q<>ASC("Y") THEN 410
1860 VTAB 23:INPUT"FILE NAME FOR LOAD? ";N$
1870 PRINT D$;"OPEN"N$:PRINT D$;"READ";N$
1880 INPUT Z1:INPUT Z2
1890 FOR I=0 TO Z1:S%(I)=0:NEXT I:FOR I=Z2 TO 105:S%(I)=0:NEXT I
1900 FOR I=Z1 TO Z2
1910 INPUT S%(I)
1920 NEXT I
1930 PRINT D$;"CLOSE";N%
1940 GOSUB 3300: GOSUB 3400
1950 VTAB 22:CALL-958:VTAB 23:PRINT"NOW RETRACING IMAGE ON SCREEN"
1960 ELE=Z1:BIT=0:CL=ML+4*((ELE-INT(ELE/3)*3)*14)
1970 CT=MT+4*INT(ELE/3)
1980 A=INT(S%(ELE)/2^BIT): IF INT(A/2)=A/2 THEN 2000
1990 FOR I=2 TO 4:XDRAW 1 AT CL+1,CT+I:NEXT I:HPLOT 30+(CL-ML)/4,63+(CT-MT)/4
2000 CL=CL+4:BIT=BIT+1:IF BIT<>14 THEN 1980
2010 IF ELE>=Z2 THEN GOSUB 3310:GOTO 410
2020 BIT=0:ELE=ELE+1
2030 IF ELE/3=INT(ELE/3) THEN CL=ML:CT=CT+4
2040 GOTO 1980
2100 REM
2101 REM HELP COMMAND
2102 REM
2110 IF Q<>ASC("H") AND Q<>ASC("/") AND Q<>ASC("?") THEN 2200
2120 VTAB21:CALL-958:POKE-16303,0
2130 GOSUB 3170
2140 POKE-16304,0
2150 VTAB 20:PRINT:CALL-958:HTAB 2:PRINT"ACTUAL SIZE";:HTAB 21:PRINT"VIEWING WINDOW"
2160 GOTO 430
2200 REM
2201 REM QUIT OMMAND
2202 REM
2210 IF Q<>ASC("Q") THEN 430
2220 XDRAW 1 AT CL+1,CT+3
2230 VTAB 23:PRINT"SURE YOU WANT TO QUIT?"
2240 GOSUB 3500
2250 IF Q<>ASC("Y") THEN VTAB 22:CALL-958:GOTO 410
2260 HOME:POKE-16303,0:POKE-16298,0:VTAB 24
2270 GOTO 9999
3000 REM
3010 REM SUBROUTINES
3320 REM
3100 HOME
3110 HTAB 15:PRINT"COMMAND MENU":HTAB 15:PRINT"------- ----"
3120 VTAB 4:PRINT"I,J,K,M"; TAB(9);"CURSOR MOVEMENT":PRINT:PRINT"P";TAB(9);"PLOT POINT AT CURSOR POSITION":PRINT
3130 PRINT"E";TAB(9);"ERASE POINT AT CURSOR POSITION":PRINT:PRINT"C";TAB(9);"CLEAR SCREEN":PRINT
3140 PRINT"T";TAB(9);"MAKE SHAPE TABLE":PRINT:PRINT"S";TAB(9);"SAVE SHAPE SOURCE FILE TO DISK":PRINT
3150 PRINT"G";TAB(9);"GET SHAPE SOURCE FILE FROM DISK":PRINT:PRINT"H OR ?";TAB(9);"HELP (RETURN TO THIS MENU)":PRINT
3160 PRINT:PRINT"Q";TAB(9);"QUIT PROGRAM EXECUTION"
3170 VTAB 24:HTAB 10:PRINT"HIT SPACE TO EXIT MENU";
3180 GOSUB 3500:IF Q<>ASC(" ") THEN 3180
3190 VTAB 21:CALL-958
3200 RETURN
3300 POKE 230,32:CALL 62450:HGR:SCALE=1:ROT=0
3310 PT=YMAX+1:PB=0:PL=XMAX+1:PR=0
3320 VTAB 20:PRINT:CALL-958:HTAB 2:PRINT"ACTUAL SIZE";:HTAB 21:PRINT"VIEWING WINDOW"
REM 3320 VTAB 21:HTAB 2:PRINT"ACTUAL SIZE";:HTAB 21:PRINT"VIEWING WINDOW";:CALL-958:PRINT
3330 X=INT(XMAX/2):Y=INT(YMAX/2)
3340 MR=ML+XMAX*4:MB=MT+YMAX*4
3350 CL=ML+(X-1)*4:CT=MT+(Y-1)*4
3360 RETURN
3400 HCOLOR=3
3410 FOR I=ML TO MR STEP 4:HPLOT I,MT TO I,MB:NEXT I
3420 FOR I=MT TO MB STEP 4:HPLOT ML,I TO MR,I:NEXT I
3430 RETURN
3500 Q=PEEK(-16384):IF Q<128 THEN 3500
3510 POKE-16368,0:Q=Q-128
3520 RETURN
9999 END
Table 1: Summary of parameters that must be set up prior to
calling the shape subroutine: coordinates of upper left
corner of bit picture (1a) and starting address
(hexadecimal) of shape table (1b).
(1a)
Coordinate 6502 Register
x low-order byte X
x high-order byte Y
y A
(1b)
Address Byte Memory Location
low-order byte EB
high-order byte EC
Photo 1: The command menu that appears at the beginning of
the shape-editor program (listing 3). This menu also appears
whenever the Help key is pressed.
+----------------------------------------+
| COMMAND MENU |
| ------- ---- |
| |
|I,J,K,M CURSOR MOVEMENT |
| |
|P PLOT POINT AT CURSOR POSITION |
| |
|E ERASE POINT AT CURSOR POSITION |
| |
|C CLEAR SCREEN |
| |
|T MAKE SHAPE TABLE |
| |
|S SAVE SHAPE SOURCE FILE TO DISK |
| |
|G GET SHAPE SOURCE FILE FROM DISK |
| |
|H OR ? HELP (RETURN TO THIS MENU) |
| |
| |
|Q QUIT PROGRAM EXECUTION |
| |
| |
| HIT SPACE TO EXIT MENU |
+----------------------------------------+
Photo 2: A view of the screen-edit mode of the shape-editor
program. The figure on the grid is an enlarged view of the
actual-size shape on the left side of the screen. The cursor
is the small horizontal line in a square above the lower
left corner of the displayed shape.
Photo 3: A view of the first step in forming a shape table.
The desired shape is selected by defining a rectangle
enclosing the shape. Here, the user has positioned the
cursor to the correct position to define the upper left
corner of the rectangle.
Photo 4: A view of the screen after the shape-editor program
has formed the shape table for the shape shown in photo 3.
+----------------------------------------+
| |
| |
|DO YOU WANT TO SEE THE TABLE IN HEX |
| OR IN DECIMAL? |
| |
| 08 02 70 07 04 88 12 12 |
| 01 29 79 27 01 20 7F 3F |
| 00 04 00 04 00 04 0E 1C |
| |
|DO YOU WANT TO SAVE THE OBJECT TABLE |
| ON DISK? |
| |
|WHAT DO YOU WANT TO NAME |
| THE FILE? |
| |
| |
+----------------------------------------+