Está viendo la página Spain versión del sitio.
Le gustaría cambiar a su sitio local?
13 MIN TIEMPO DE LECTURA
EASY 6502

Write and run C64-style 6502 code

David Bolton shows how to get started writing 6502 code and provides a useful example that sorts numbers.
An Easy 6502 screen with a short listing, showing the pixels on the right.

OUR EXPERT

David Bolton used to write Commodore 64 games for a living back in the mists of time, and taught himself Z80 by writing a 6502 assembler in Z80.

OUR EXPERT

David Bolton used to write Commodore 64 games for a living back in the mists of time, and taught himself Z80 by writing a 6502 assembler in Z80.

How long do you think it took to modify a 2,200-line C program that runs on Windows H to have it run on Linux? Just one hour. The main changes were switching some string routines such as strcpy_s to strncpy, altering paths for includes and changing the high-speed timing code. It might take between a week and a month or longer to do that in assembly. And moving that C code from, say, Ubuntu on x86 to Raspberry Pi on ARM would take almost no time at all. In assembler, it would take a complete rewrite; more months of work.

Having tried to put you off modern assembly, let’s try some old-fashioned assembler from 40 years ago. With roughly 60 instructions, the 6502 CPU is simpler than modern CPUs, which can have several hundred.

What is a 6502?

Pretend it’s 1983 and you have bought a brand new Commodore 64 and assembler cartridge that will ‘compile’ 6502 assembler into machine code. In this article, we are learning some 6502 assembly language, writing a short program in it and then running it on a web emulator.

The 6502 has just three registers: A (known as the accumulator), X and Y. Each register is 8-bit, so can hold a number between 0 and 255. It’s easier to deal with values and addresses in base 16 – hexadecimal – so a byte holds a value between $00 and $FF.

You can find a list of all the 6502 instructions and details about addressing modes and lots more at www. masswerk.at/6502/6502_instruction_set.html, but here are a few useful things you should know about the Commodore 64 and the 6502 CPU.

On the Commodore 64, the processor is actually a 6510 (a 6502 with a bit of hardware mapped in). It has 6,553610 bytes of RAM, but some RAM addresses are used for the hardware sprites. In hexadecimal, 6,553610 is $10000 and the memory addresses run from $0000-$FFFF. However, the Commodore 64 charset graphics are mapped at $1000-$2000, sprite hardware is mapped at addresses D000-D02E, the sound hardware at D400-D7FF, parallel port at DC00-DCFF, and so on. So, your program should use RAM in the region between $2000 and $CFFF, which is 45,055 bytes – enough for most games. And, of course, page zero, which is the first 256 bytes, and runs from $00-$FF. By putting variables and data in page zero, the instructions to access them are only two bytes long – the opcode is the first byte and the address is the second byte. Accessing memory in addresses after page zero is slower because it requires two byte addresses and those instructions are three bytes long.

An Easy 6502 results window after a successful assemble.

Machine code, assemble!

Desbloquea este artículo y mucho más con
Puedes disfrutar:
Disfrute de esta edición al completo
Acceso instantáneo a más de 600 títulos
Miles de números atrasados
Sin contrato ni compromiso
Inténtalo €1.09
SUSCRÍBETE AHORA
30 días de acceso, luego sólo €11,99 / mes. Cancelación en cualquier momento. Sólo para nuevos abonados.


Más información
Pocketmags Plus
Pocketmags Plus

Este artículo es de...


View Issues
Linux Format
March 2024
VER EN TIENDA

Otros artículos de este número


Linux Format
codeclub
www.codeclub.org.uk
Linux Format
The #1 open source mag Future Publishing Limited,
WELCOME
MEET THE TEAM
This issue, we’re looking at exciting new distributions, so what new distro have you tried recently that you would recommend readers try for themselves?
Cutting edges
Living in the open source world means if
REGULARS AT A GLANCE
Linux to save Windows 10 PCs from the apocalypse?
240 million PCs could be scrapped after October 2025, when free support ends for Windows 10. Or users could just switch to Linux.
Cosmic looking out of this world
Pop!_OS Cosmic DE is to be released as an alpha, with a new terminal and overhauled graphics support.
Chromium Wayland accelerated!
VA-API now works on Linux Ozone/Wayland platform.
NEW YEAR RELEASE!
Italo Vignoli is one of the founders of
DATABASE BASES
David Stokes is a technology evangelist at Percona.
Unity bans VLC plugin
Unity management making friends and influencing people.
OpenWrt router?
OpenWrt floats the idea of its own hardware router.
Man writes kernel in Rust
Maestro is a Unix-like kernel written from scratch in Rust.
Distro watch
What’s behind the free software sofa?
WILY WESTON
Marius Vlad is a senior software engineer at
REISER’S GUILT
Jon Masters is a kernel hacker who’s been
Kernel Watch
Jon Masters keeps up with all the latest happenings in the Linux kernel, so you don’t have to.
Answers
Got a burning question about open source or the kernel? Whatever your level, email it to answers@linuxformat.com
Mailserver
WRITE TO US Do you have a burning
HotPicks
Shotwell YouTube Downloader Plus Blue Recorder EncryptPad Buttercup Resources Rnote heXon EDuke32 Delta Chat Proton VPN
OLD-SCHOOL HACKING 101
CREDIT: Magictorch LXF313 will be on sale Tuesday
REVIEWS
Intel Core i7 14700K
Jacob Ridley thinks this is a perfectly good stand-in for a Core i9.
RlxOS 2023.11
Nate Drake finds himself washed up on the rock of ages that is RlxOS , only to discover that it still needs work.
Emmabuntüs 5
Nate Drake finds that despite being hard to pronounce, the name is the only complicated thing about this Debian-based distro.
Manjaro 23.1 Vulcan
Nate Drake boldly goes to take Vulcan for a test spin and finds an incredibly user-friendly and efficient operating system.
ROUNDUP
Password managers
RoboForm Keeper NordPass KeePass 2 Bitwarden
THE 5 BEST NEW DISTROS
THE 5 BEST NEW DISTROS
Blast off into the future with Matt Holder as he explores the technology powering a new generation of Linux distributions.
Pi USER
Let them eat lots of Pi!
Hundreds of thousands of Raspberry Pis are hitting the market, with production ramped to the max!
ProPico
Better by design.
Cyberdeck
Survive anything!
PEERLESS PIN NUMBERS
Les Pounder works with groups such as the
Official Pi 5 Case
Can the new official Raspberry Pi 5 case keep the Pi 5 cool? Les Pounder is – brace yourselves readers – on the case…
Geniatech XPI 3566 Zero
All brains and no brawn applies equally to this SBC and to Les Pounder.
Start using MQTT for sensor logs
Les Pounder is a sensitive soul and his new office needs sensors to record data. He’d better break out the maker kit!
IN DEPTH
Orange Pi vs Raspberry Pi
Tam Hanna compares the highest-end single-board computer offerings from Shenzhen Xunlong and the Raspberry Pi Foundation to find out which one is best suited for you.
TUTORIALS
Monitor systems in style
Shashank Sharma doesn’t look dapper in his quickly fading imitation leather jacket, but his terminal is always a sight to behold.
Secure and private comms made easy
Nick Peers takes a fresh look at an old favourite – Jami, a tool that aims to cover all your communication and sharing needs.
Make a Mealie of it
David Rutland dons a tall hat to prepare you a portion of Mealie – a sophisticated recipe management server with a side of meal planning.
Rescue and back up your retro media
Christian Cawley has some old Commodore 64 5.25-inch disks, and wants to revisit some personal files from 40 years ago.
Create NPCs and their conversations
Nate Drake invites you to relive the glory days of point-and-click adventure games by creating your very own.
Emulate an analogue computer digitally
In our continuing journey into analogue computing, Mike Bedford looks at another emulation approach, and how to get a real analogue computer.
The wire to faster Ethernet
Searching for faster than Gigabit Ethernet, the ever frugal Neil Mohr does that compromise thing his ex told him about.
ADMINISTERIA
Do Docker like an adult!
Stuart Burns is as mad as hell and he’s not going to stand for your shoddy Docker deployments any longer!
Starlink
Wherever he may roam, Nate Drake can enjoy fast internet at a fair price.
Hatch
An easy-to-use builder that empowers Mirza Bahic’s creative expression.
PROTECT YOUR SYSTEMS
Davey Winder reveals the biggest threats that hit systems in 2023 and what’s likely to happen over 2024.
CODING ACADEMY
The LXF Shell in… the redirection redemption
Ferenc Deák continues writing shell-enhancing redirection features and hiding the odd film title for you to spot, the little scamp!
ADVERTISEMENT
GO. COMPARE
Linux Format
www.magazinesdirect.com/LIN/C75J
Linux Format
magazinesdirect.com
techradar
www, techradar.com
magazinesdirect
www.magazinesdirect.com
magazinesdirect
www.magazinesdirect.com/linux-format
Python
magazinesdirect.com
EFF
EFF.ORG
The Brain Tumour Charity
thebraintumourcharity.org
Chat
X
Soporte Pocketmags