6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri May 17, 2024 12:32 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Thu May 02, 2024 5:31 pm 
Offline

Joined: Thu May 02, 2024 5:23 pm
Posts: 2
Hello all. I'm just sinking my teeth into 6502 and I thought a good idea for a simple starter program was to write one that could fill a screen. My various attempts failed because I'm an idiot and didn't know how to properly reference 16 bit addresses. So, I found a program that does what I was trying to do so I could try to reverse engineer it a bit, but it just left me with more questions. Here's the code I found:
Code:
lda #1
ldx #$02

fill_screen:
stx $01

draw_segment:
sta ($00),Y
iny
bne draw_segment

inx
cpx #$06
bne fill_screen

As I understand it, the X register being stored at $01 represents the first half of the 16 bit address being referenced, which is then combined(?) with the increasing Y value. Starting at $0200 and increasing with each loop until reaching the end of the vram at $05FF. However, I don't understand how this works. The indirect indexed reference
Code:
sta ($00),Y
is referencing $00, which stores nothing. Shouldn't it be
Code:
sta ($01),Y
? And in draw_segment, there is a branch not equal instruction with no compare instruction before it? What is the branching condition?

All of this is probably very simple to anyone versed in machine languages, but that's why I'm in the newbies board. Try and go easy on me.


Top
 Profile  
Reply with quote  
PostPosted: Thu May 02, 2024 5:44 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 9:02 pm
Posts: 1686
Location: Sacramento, CA
Code:
sta ($00),Y


Will take the 16 bit address stored at $00 & $01 ($00 low byte, $01 high byte) and add the value of Y to that. The value in the Accumulator "A" register is then stored to memory at the resulting address.

The
Code:
INY
opcode will affect the Zero Flag in the status register so once Y goes from $FF to $00, the flag is set and the branch is not taken.

I think they are assuming the value of $00 to be $0 and storing $02 at $01 to make the effective start address $0200 + Y. The innner loop then fills all locations from $0200 to $02FF then increments $01 and repeats until it reaches $05FF.

Hope this helps!

Daryl

_________________
Please visit my website -> https://sbc.rictor.org/


Top
 Profile  
Reply with quote  
PostPosted: Thu May 02, 2024 5:50 pm 
Offline

Joined: Thu May 02, 2024 5:23 pm
Posts: 2
Ohh, that makes a lot more sense now, thank you! The syntax is just a bit confusing. Does it just use the next location in memory as the high byte and the referenced byte as the low one? If I were to shift addresses by one, and store the high byte in $02 and use
Code:
sta ($01),Y
would it have the same effect?

edit: also, yeah, documentation told me that branching runs on the Z flag, so it makes sense now that I know it's triggered by an overflow


Top
 Profile  
Reply with quote  
PostPosted: Thu May 02, 2024 6:00 pm 
Offline

Joined: Fri Mar 18, 2022 6:33 pm
Posts: 443
gluesniffing wrote:
...in draw_segment, there is a branch not equal instruction with no compare instruction before it? What is the branching condition?
This is an excellent newbie question. Many 6502 instructions set status flags automatically so you can test them without needing any compare instructions. In this common looping pattern:
Code:
    ldy     #0
loop:
    ;   do some stuff here
    iny
    bne     loop
When .Y contains $FF, adding 1 to it will cause it to "wrap around" to 0. The iny instruction automatically sets the Z flag when this happens. So, this loop will do some stuff 256 times (.Y = 0 through 255). A good instruction set reference will tell you what flags an instruction affects.

_________________
"The key is not to let the hardware sense any fear." - Radical Brad


Top
 Profile  
Reply with quote  
PostPosted: Thu May 02, 2024 7:30 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8438
Location: Southern California
An automatic compare-to-zero instruction is built into the following 65c02 instructions:  LDA, LDX, LDY, INC, INX, INY, DEC, DEX, DEY, INA, DEA, AND, ORA, EOR, ASL, LSR, ROL, ROR, PLA, PLX, PLY, SBC, ADC, TAX, TXA, TAY, TYA, and TSX.  (This is from the middle of the programming-tips page of the 6502 primer, at http://wilsonminesco.com/6502primer/PgmTips.html .)

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Thu May 02, 2024 7:54 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8188
Location: Midwestern USA
GARTHWILSON wrote:
An automatic compare-to-zero instruction is built into the following 65c02 instructions...

The fact that the 6502 family handles instructions in this fashion is one of the reasons it produces good throughput at relatively sedate clock speeds.  Not having to do an explicit comparison each time a register or memory value is incremented/decremented can eliminate a lot of clock cycles out of a loop.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Thu May 02, 2024 8:17 pm 
Offline

Joined: Wed Jan 03, 2007 3:53 pm
Posts: 55
Location: Sunny So Cal
gluesniffing wrote:
Ohh, that makes a lot more sense now, thank you! The syntax is just a bit confusing. Does it just use the next location in memory as the high byte and the referenced byte as the low one? If I were to shift addresses by one, and store the high byte in $02 and use
Code:
sta ($01),Y
would it have the same effect?


Yes. Your address pair is now in $01/$02.

_________________
Machine room: http://www.floodgap.com/etc/machines.html


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: