#/#6502
Short snippets of code to help you get started with 6502 programming.
#ciao.jb
Show the word "CIAO".
CPU Instructions: LDA (LoaD Accumulator), STA (STore Accumulator) and BRK (BReaK).
CPU Addressing Modes: Immediate [#n], absolute [n:n] and implied [].
IO: Video memory is at 40-79 in page 2; 1st row starts at 2:40, 2nd row starts at 2:50 and so on for a 10x4 matrix of Latin1 (extended ASCII) characters.
#loop1.jb
Keep updating a character on the screen (infinite loop). To stop the program press a soft key.
CPU Instructions: INC (INCrement memory), NOP (No OPeration) and JMP (JuMP).
IO: Writing 0 into 2:18 suspends the CPU until the screen has been redrawn (refresh rate is 10 frames per second).
Style: Even if in JBit the registers are always 0 at the beginning, it is suggested to clear them anyway.
#fill1.jb
Fill the display with Xs.
CPU Instructions: LDX (LoaD X register), INX (INcrement X register), CPX (ComPare X register) and BNE (Branch on Not Equal)
CPU Addressing Modes: Absolute indexed [n:n,X] and relative [r]; relative mode is used in branches and looks like absolute mode in the monitor.
#fill2.jb
Same as fill1, but faster.
Note that X ranges from 40 to 1 inside the loop (in fill1 the range was 0 to 39).
CPU Instructions: DEX (DEcrement X register).
Puzzle: Not Equal in BNE really means "Not Zero" and CPX really means "subtract discarding the result".
Style: This is the usual way to write loops for the 6502.
#loop2.jb
Changing letters (infinite loop).
CPU Instructions: DEC (DECrement memory), LDY, STY and TAX (Transfer/copy Accumulator to X register).
IO: Write the desired FPS * 4 into 2:17 (e.g. 4 = 1 FPS).
#loop3.jb
Bouncing letters (finite loop A-F). More of the same.
#keypad.jb
Firing characters (press any key except * to fire a character, use * to stop firing).
CPU: BEQ (Banch if EQual) and CMP (CoMPare accumulator).
IO: Read from 2:24; if the value is 0 no key has been pressed; a value different than 0 is the ASCII code of the key that has been pressed (e.g. '0' is 48). Write 1 into 2:24 to acknowledge the key and remove it from 2:24. At most 8 unacknowledged keys are kept, the others are lost.
#charset.jb
Browse the charset (2=Up, 8=Down and 0=Exit).
CPU: CLC (CLear Carry), SEC (SEt Carry), ADC (ADd with Carry), SBC (SuBtract with Carry), BCS (Branch on Carry Set), BCC (Branch on Carry Clear).
Style: This is "spaghetti" code; note how difficult it is to understand how it works. A few tricks have also been used to keep it in 64 bytes (BCC/BCS instead of JMP and branching to 3:1).
#/#GameKit
IO chip preconfigured with tiles from http://www.famfamfam.com/lab/icons/silk/
#teaser.jb#Teaser
Use 2, 4, 6 and 8 to collect the items. Hit the bomb when you get bored.
#empty.jb#Empty
A starting point for your own games.
