6502 basics

Prime

6502 basics

Post by Prime »

I was considering writing a few tutorials for new 6502 programmers but I'm unaware of what type of level most people are at.
I'd like to have new people get a sprite onscreen and control it then keep building on that.
So is something like I've shown much to basic?Or needs more comments?



This is in Kickass assembler

Code: Select all

    .pc = $801
		.byte $0b, $08, $06, $00, $9e, $32, $30, $36, $34, 0,0,0 
 		.pc = $810 

		jmp Start




Constants and Defines can't be modified


.const SpriteFrameRegister = $07f8
.const SpriteEnableRegister = $d015
.const SpriteColorRegister = $d027
.const SpriteXMostSignificantBitRegister = $d010
Start:
                                                 //First Lets clear the screen using default blank char which is $20 hex or 32 decimal
                                                 //We start by declaring the x register at zero and increase the x register to loop through and clear the screen
 ldx #0                                       //x = 0 decimal
 lda #32                                     //accumulator = 32 decimal
ClearScreenLoop:                       //ClearscreenLoop is a label
                                                 //This instruction stores the contants of the accumulator into the address $400(1024 decimal)plus the offset of the  x register
                                                 //Basically place 32 decimal at $400+x since we loaded the accumulator with decimal 32 (blank char or space if you prefer) 
                                                 //We also loaded the x register with decimal 0 so what gets stored to $400? 
                                                 //$400,x is the equivalent of $400+0 = $400 now if x register was = 1 that would point to the address $400+1 = $401
                                                 //if x register was 2 $400+2 = $402 pretty easy huh!
                                                 //So accumulator = 32,x register = 0      sta $400,x = place decimal 32 at address $400 + xregister offset 
                                                 //The contants of address $400+x becomes 32
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////                                                    //you'll notice other addresses besides starting address $400, ($500,$600,$700)
                                               //The screen is 1024 bytes in 6502 we can't access more than 256 bytes so 1024/256 = 4 now count how many addresses there are 
                                               //$400,$500,$600,$700 is four addresses in total
                                               //So in order to clear all 1024 bytes of screen memory we have to store the accumulator plus the x register offset to four  
                                               //addresses               
 sta $400,x                              //Store the value of the accumulator to $400+x
 sta $500,x                              //Store the value of the accumulator to $500+x
 sta $600,x                              //Store the value of the accumulator to $600+x
 sta $700,x                              //Store the value of the accumulator to $700+x    
 inx                                        //Increment x register used as loop counter
 bne ClearScreenLoop             //what the instruction does is "branch if zero flag is not set" (it does not care about the X register at all). so for that to make any sense, you'd have to put "increment X register, and set zero flag to 1 if the result is zero" as comment into the previous line (which is really over the top). hence reducing it to just "repeat until index overflows" Thanks Groepaz for the explanation much more clear now!

Last edited by Prime on Wed Oct 29, 2014 10:07 pm, edited 2 times in total.


MarcWalters
Member
Member
Posts: 55
Joined: Wed Jun 11, 2014 2:33 pm
Location: Lake Macquarie, NSW, Australia
Contact:

Re: 6502 basics

Post by MarcWalters »

I think the type of program and amount of code is about right for a beginner. However, there are perhaps too many comments in the program. I'd rather see narrative form in the main body of the article where it can be formatted nicely using bold, italics, lists and paragraphs.

The following are my thought on writing tutorials. Please be aware that they're my subjective opinion only.

General Style
For each part of the program that does a particular task, explain what it does, why it is used and how it works.
Keep references to technical items such as the CPU registers consistent: either use a long form always, or use a long form on the first occurrence then a short form for all subsequent occurrences.

Code Comments
Comments in the source should be brief and tightly focused.
- Place a comment at the top of a routine code that states what it does.
- Place a comment at the side of an instruction to explain its use in that routine, referring back to the routine's main comment if necessary.

Precision
My personal preference is for tightly focused language set "in the moment".
This is more a 6502 programming tutorial than a 6502 Instruction Set tutorial, so explain how and why an instruction is used in the context of the routine, but not how and why that instruction works. Any enthusiastic beginner will have a 6502 reference chart or book at hand and will (or should) already know about such things as the common addressing modes and the indexing limits imposed by 8-bit CPU registers.

For example, the comment above the loop which begins "You'll notice other addresses..." and ends "...to four addresses."
could then be written as:

1024 bytes of memory are cleared by four Store instructions within a loop of 256 iterations. The 6502's Absolute-Indexed addressing mode is used because it is fast and simple. But because that mode can only access a range of 256 addresses we need to use four Store commands within the loop.
The X Register serves two purposes here:
1) To control the number of iterations of the loop
2) As an index used by each Store command to modify a range of 256 addresses
Last edited by MarcWalters on Tue Oct 28, 2014 2:34 am, edited 1 time in total.
Prime

Re: 6502 basics

Post by Prime »

All the points you mentioned are valid and I could have gone that way but have a look here Marc http://tnd64.unikat.sk/.
Most beginners recommend these tutorials to new 6502 programmers with that I tried to simplify my wording and example


1024 bytes of memory are cleared by four Store instructions within a loop of 256 iterations.
The X Register serves two purposes here:
1) To control the number of iterations of the loop
2) As an index used by each Store command to modify a range of 256 addresses

Just as an example your comments there sounds exactly as a programmer would comment and we both understand but to someone with zero experience that might seem daunting.
MarcWalters
Member
Member
Posts: 55
Joined: Wed Jun 11, 2014 2:33 pm
Location: Lake Macquarie, NSW, Australia
Contact:

Re: 6502 basics

Post by MarcWalters »

but to someone with zero experience that might seem daunting.
Yes, you're right. I've gone back and edited the example i wrote to include the following:

The 6502's Absolute-Indexed addressing mode is used because it is fast and simple. But because that mode can only access a range of 256 addresses we need to use four Store commands within the loop.

It looks a bit better now.
Prime

Re: 6502 basics

Post by Prime »

MarcWalters wrote:
but to someone with zero experience that might seem daunting.
Yes, you're right. I've gone back and edited the example i wrote to include the following:

The 6502's Absolute-Indexed addressing mode is used because it is fast and simple. But because that mode can only access a range of 256 addresses we need to use four Store commands within the loop.

It looks a bit better now.
Gotta love programmer humour
lol ;)
satpro

Re: 6502 basics

Post by satpro »

I don't know, Rick --> I like the comments! It's nice to see what's rattling around "up there" as you're writing it. Every bit of a comment helps, especially when describing something which is complicated.

Good work. Keep it coming.
P.S. You'll be writing 32-bit 65x code soon - it's almost ready.
dudz
Member
Member
Posts: 140
Joined: Tue Jun 17, 2014 5:40 am
Contact:

Re: 6502 basics

Post by dudz »

inx //x = x +1
i find comments like that incredibly useless, especially for a beginner. if you need to explain what an instruction does, then do that first, seperately, before showing any code. in the code itself the comment should refer to what the instruction does in the context of the program.
satpro

Re: 6502 basics

Post by satpro »

dudz wrote:
inx //x = x +1
i find comments like that incredibly useless, especially for a beginner. if you need to explain what an instruction does, then do that first, seperately, before showing any code. in the code itself the comment should refer to what the instruction does in the context of the program.

I agree. On the other hand, I understand how that goes --> you want to comment your program well so you set up a system where you basically comment everything. (For what it's worth, I would take that style any day next to the one with no comments at all). So, you get to a line (in a loop, no less) where all you are doing is incrementing an index register. What should your comment be? So you end up putting x=x+1. Not a big deal in the larger context. Heck, I've tried a thousand different creative ways to comment stuff like that (from simple to elaborate) and still have no real good way to express that type of action, other than no comment at all.
dudz
Member
Member
Posts: 140
Joined: Tue Jun 17, 2014 5:40 am
Contact:

Re: 6502 basics

Post by dudz »

What should your comment be? So you end up putting x=x+1. Not a big deal in the larger context.
"increment loop counter" would be much more like it, IMHO
Prime

Re: 6502 basics

Post by Prime »

dudz wrote:
What should your comment be? So you end up putting x=x+1. Not a big deal in the larger context.
"increment loop counter" would be much more like it, IMHO
A loop counter is a variable so that statement is incorrect and misleading,i'll fix that for you


"Increment x register used as loop counter" ;)
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 11 guests