Useful tricks and tips for the 64/6502 programmer

Last updated: 7/2/97


$0007

Converting a numerical value $00-$0F to ASCII 0-A:

	SED
	CMP #$0A
	ADC #$30
	CLD
and that's it! (Frank Kontros)
$0006

Loading and saving from BASIC. (Yep, another Rae West trick). Instead of doing an OPEN and PEEKing everything from memory manually, just use the kernal! Example block save from $3000-$3FFF:

	1000 SYS 57812 "FILENAME",8,1: REM SET PARAMETERS FOR LOAD
	1010 POKE 193,0: POKE 194,48: REM $3000 IS START ADDRESS
	1020 POKE 174,0: POKE 175,64: REM $4000 (-1) IS END ADDRESS
	1030 SYS 62957: REM PERFORMS SAVE
Block load is very similar:

	1000 POKE 147,0: REM LOAD/VERIFY FLAG, 0 MEANS LOAD
	1010 SYS 57812 "FILENAME",8,1: REM SETLFS IN KERNAL SETS PARAMETERS
	1020 SYS 62631: REM LOAD
Alternatively:
	1000 POKE 147,0: REM LOAD/VERIFY FLAG, 0 MEANS LOAD
	1010 SYS 57812 "FILENAME",8,1: REM SETLFS IN KERNAL SETS PARAMETERS
	1020 POKE 780,0:SYS 65493: REM LOAD
This version calls the routine from the jump table, and so will work with many fastloaders, etc.
$0005

A trick communicated to me by Todd Elliott: This little trick is a way of hiding LDAs, and also provides a compact way of including many branch conditionals. The idea is that a harmless opcode like BIT or one of the compare instructions may be used to hide or skip over some code. Consider the following:

		CMP #blah
		BEQ :L1
		CMP #yak
		BEQ :L2
		CMP #piffle
		BEQ :L3

		LDA #01		;Assembles as
		HEX CC		; A9 01		LDA #01
	:L1	LDA #02		; CC A9 02	CPY $02A9
		HEX CC
	:L2	LDA #03		; CC A9 03	CPY $03A9
		HEX CC
	:L3	LDA #04		; CC A9 04	CPY $04A9
So, the LDA's get hidden, and the unwanted LDA's get skipped over. BIT or CMP or CPX could also have been used instead of CPY -- mix and match to make life confusing.
$0004

To compare a 2-byte number: use SBC (of course) instead of CMP. But the smart thing to do is to use a temporary location:

		LDA L1
		SEC
		SBC L2
		STA TEMP
		LDA H1
		SBC H2		;Carry set as normal
		ORA TEMP	;Zero flag set if result zero


$0003

A smart way to decrement a 2-byte number:

		LDA LO
		BNE :CONT
		DEC HI
	:CONT	DEC LO
(one of many useful Raeto West tricks).


$0002

ROM revisions are a funny thing. Some ROM revisions initialize the foreground color in the color RAM to the current background color. Others initialize it to the current foreground color. On some machines, a simple thing like LDA #147:JSR CHROUT:LDA #0: STA 1024 will generate an invisible @-sign in the upper left corner -- if using a SHIFT-CLEAR to initialize colors, make sure both the foreground color and background color are set. Otherwise the program may not be visible on older machines. The first version of cube3d has this problem, as do several of the 4k demos -- they work on older machines, you just can't see stuff! As Marko Mäkelä points out, this is the difference between ROM R2 and R3, but what about R1? R1, which is essentially the VIC-20 version, sets the entire screen to white.


$0001

Location $BA (on both the 64 and 128) contains the current device number. Use this instead of forcing e.g. drive #8 when doing drive accesses (use LDX $BA instead of LDX #8 before a JSR SETLFS). Actually, as Marko points out, it may be wise to check that this value is not less than 8 or greater than 30, in case the user did an OPEN 4,4:CMD4:LIST or some such.