Learn Multi platform 6502 Assembly Programming... For Monsters!

Super Simple Series

In this series we'll take a look at a variety of basic tasks that you may need to look at when you're starting out with programming these classic systems...

In each case the source-code will be a single file with no Includes to make things easy if you're using a different assembler (though some may include a binary.)


Lesson S19 - Keypad Reading on the Commodore PET!
Let's use the keyboard to move a simple character block 'sprite' around the screen of the Commodore Pet!

PET_keys.asm

Defining Parameters, and drawing sprites

First we're going to need some zero page bytes to define an X,Y position.

Our 'sprites' will have 2 frames of animation (stored in FrameNo), and we'll use different 'pairs' for Up+Down, Left and Right (Stored in FrameBase)
We're going to need to define some frames of animation for our test. Each frame is 4x4 characters (8x8 pixels).

There are two frames per direction, with 3 directions (Up and Down use the same sprite)
These were created with my AkuSprite editor!
Given a 'Sprite number' we need to multiply this by 16 to get the offset for the sprite. (each sprite is 4x4, 16 bytes)

We use GetVDPScreenPos to calculate a VRAM address from the X,Y co-ordinate, and GetNextLine to move down one VRAM line.

We copy all the bytes from the sprite source z_hl to the VRAM destination z_de


BlankPlayer is an alternate version which removes the sprite from the screen.
GetVDPScreenPos calculates the VRAM address of the screen position specified by X,Y registers, in Zero page entries z_d+Z_e

The screen base is $8000 in ram, and each line is 40 bytes, so our formula is:
VRAM= $8000 + Ypos *40 + Xpos

As it's hard to multiply by 40, we multiply Ypos by 8 and 32, and add the two together!
To move down a line, we add 40 to the current VRAM position
This works fine on the standard 40 character screen.

If you're using 80 character wide screen modes, you'll need alter the formula!

Reading the Keyboard

To read a line of the keyboard, we first write the line number to memory mapped port $E810,

We then read a byte from $E812 - each bit will be a different key -
A 0 means the key is pressed.
A 1 means the key is not pressed.

We test each cursor key, and Space and Enter (we can use these as fires)


Our test program

We're going to define the starting position of our character - z_h will store our joystick buttons - we initialize this to zero - as we need to run the main loop but don't want to cause any keypresses.
Next comes the joystick reading routine - we skip this the first run

We read in from the joystick into z_H - until a key is pressed we'll wait in an infinite loop... when it is pressed we start our drawing routine...

First we clear the old player sprite from the screen.
We test each of the bits in z_h (which was returned from the joystick routine)

In each case, if the bit is 0 then the button isn't down - and we skip over the routine to move the character's X or Y position

Before moving we check if we're already at the 'limit' of the screen - we don't want to go over the edges, so we skip moving if we are already at the edge.
We save the new XY position.

We then 'flip' the frame (0 and 1)... this gives a 'walking' animation.

We then draw the new player position... wait a while and repeat.
Here is the result!




 

View Options
Default Dark
Simple (Hide this menu)
Print Mode (white background)

Top Menu
***Main Menu***
Youtube channel
Forum
AkuSprite Editor
Dec/Bin/Hex/Oct/Ascii Table

Z80 Content
***Z80 Tutorial List***
Learn Z80 Assembly
Hello World
Advanced Series
Multiplatform Series
Platform Specific Series
ChibiAkumas Series
Grime Z80
Z80 Downloads
Z80 Cheatsheet
Sources.7z
DevTools kit
Z80 Platforms
Amstrad CPC
Elan Enterprise
Gameboy & Gameboy Color
Master System & GameGear
MSX & MSX2
Sam Coupe
TI-83
ZX Spectrum
Spectrum NEXT
Camputers Lynx

6502 Content
***6502 Tutorial List***
Learn 6502 Assembly
Advanced Series
Platform Specific Series
Hello World Series
Grime 6502
6502 Downloads
6502 Cheatsheet
Sources.7z
DevTools kit
6502 Platforms
Apple IIe
Atari 800 and 5200
Atari Lynx
BBC Micro
Commodore 64
Commander x16
Super Nintendo (SNES)
Nintendo NES / Famicom
PC Engine (Turbografx-16)
Vic 20

68000 Content
***68000 Tutorial List***
Learn 68000 Assembly
Hello World Series
Platform Specific Series
Grime 68000
68000 Downloads
68000 Cheatsheet
Sources.7z
DevTools kit
68000 Platforms
Amiga 500
Atari ST
Neo Geo
Sega Genesis / Mega Drive
Sinclair QL
X68000 (Sharp x68k)

8086 Content
Learn 8086 Assembly
Platform Specific Series
Hello World Series
8086 Downloads
8086 Cheatsheet
Sources.7z
DevTools kit
8086 Platforms
Wonderswan
MsDos

ARM Content
Learn ARM Assembly
Platform Specific Series
ARM Downloads
ARM Cheatsheet
Sources.7z
DevTools kit
ARM Platforms
Gameboy Advance
Nintendo DS
Risc Os

Risc-V Content
Learn Risc-V Assembly
Risc-V Downloads
Risc-V Cheatsheet
Sources.7z
DevTools kit

PDP-11 Content
Learn PDP-11 Assembly
PDP-11 Downloads
PDP-11 Cheatsheet
Sources.7z
DevTools kit

TMS9900 Content
Learn TMS9900 Assembly
TMS9900 Downloads
TMS9900 Cheatsheet
Sources.7z
DevTools kit
TMS9900 Platforms
Ti 99

6809 Content
Learn 6809 Assembly
6809 Downloads
6809/6309 Cheatsheet
Sources.7z
DevTools kit
6809 Platforms
Dragon 32/Tandy Coco
Fujitsu FM7
TRS-80 Coco 3
Vectrex

My Game projects
Chibi Aliens
Chibi Akumas

Work in Progress
Learn 65816 Assembly
Learn eZ80 Assembly

Misc bits
Ruby programming









Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book

Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!












































Recent New Content
Lesson P34 - Creating a game for the NeoGeo CD!

Sprite Movement on the Dragon 32- Simple 6502 ASM Lesson S1

Amiga - ASM PSET and POINT for Pixel Plotting

Learn 65816 Assembly: 8 and 16 bit modes on the 65816

SNES - ASM PSET and POINT for Pixel Plotting

ARM Assembly Lesson H3

Lesson P65 - Mouse reading on the Sam Coupe

Mouse Reading in MS-DOS

Risc-V Assembly Lesson 3 - Bit ops and more maths!

Mouse reading on the MSX

Hello World on RISC-OS

Atari 800 / 5200 - ASM PSET and POINT for Pixel Plotting


Gaming + more:

Emily The Strange (DS) - Live full playthrough

$150 calculator: Unboxing the Ti-84 Plus CE (eZ80 cpu)























































Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book

Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!












































Recent New Content
Lesson P34 - Creating a game for the NeoGeo CD!

Sprite Movement on the Dragon 32- Simple 6502 ASM Lesson S1

Amiga - ASM PSET and POINT for Pixel Plotting

Learn 65816 Assembly: 8 and 16 bit modes on the 65816

SNES - ASM PSET and POINT for Pixel Plotting

ARM Assembly Lesson H3

Lesson P65 - Mouse reading on the Sam Coupe

Mouse Reading in MS-DOS

Risc-V Assembly Lesson 3 - Bit ops and more maths!

Mouse reading on the MSX

Hello World on RISC-OS

Atari 800 / 5200 - ASM PSET and POINT for Pixel Plotting


Gaming + more:

Emily The Strange (DS) - Live full playthrough

$150 calculator: Unboxing the Ti-84 Plus CE (eZ80 cpu)























































Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book

Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!












































Recent New Content
Lesson P34 - Creating a game for the NeoGeo CD!

Sprite Movement on the Dragon 32- Simple 6502 ASM Lesson S1

Amiga - ASM PSET and POINT for Pixel Plotting

Learn 65816 Assembly: 8 and 16 bit modes on the 65816

SNES - ASM PSET and POINT for Pixel Plotting

ARM Assembly Lesson H3

Lesson P65 - Mouse reading on the Sam Coupe

Mouse Reading in MS-DOS

Risc-V Assembly Lesson 3 - Bit ops and more maths!

Mouse reading on the MSX

Hello World on RISC-OS

Atari 800 / 5200 - ASM PSET and POINT for Pixel Plotting


Gaming + more:

Emily The Strange (DS) - Live full playthrough

$150 calculator: Unboxing the Ti-84 Plus CE (eZ80 cpu)