Need help with an incrementing 16-bit pointer

Building your first 6502-based project? We'll help you get started here.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Need help with an incrementing 16-bit pointer

Post by BigEd »

I've become the maintainer of Nick's easy6502. It has a very rough-and-ready approach to parsing, which isn't as fully featured as one might want, and really needs a rework. I did take a different approach in my fork as posted over here:
65Org16 and 65Org32 emulation online

As a maintainer, I am being very slack indeed about working on the code. It would be great if someone with more energy and skills could join in. But I would very much want to keep the tutorial and the existing examples working as they are.
EvilSandwich
Posts: 36
Joined: 13 Oct 2019
Location: Pennsylvania, USA

Re: Need help with an incrementing 16-bit pointer

Post by EvilSandwich »

barrym95838 wrote:
EvilSandwich wrote:
That's actually very close to my first version of the code. The only reason I abandoned it was because the "Top to Bottom" fill was a specific effect that I was going for and this will fill the four horizontal sections at the same time.
With a 1 MHz clock drogon's code will execute in 6.4 msec ... blink and you miss it. But I get your point about learning to master different techniques to achieve the desired effects.

I have never heard of a symbolic assembler that didn't allow + or - in its operands. That would be a crippling omission IMO.
It's entirely possible that the Easy6502 does allow for it, but I'm just being dumb and don't understand how to use it in that assembler. haha

One thing I'm starting to learn very quickly is that all assemblers are slightly unique and all have their little quirks that can throw me for a loop if I'm used to using another assembler. Labels in particular are never defined the same way, I've noticed.

I've seen

Code: Select all

THIS = $01

THAT equ $01

and 

Define THING $01
All the same flipping thing, but the syntax needs to be changed if I copy/paste code written for one assembler into another. It not a huge inconvenience or anything, but man, keeps you on your toes. lol
BigEd wrote:
I've become the maintainer of Nick's easy6502. It has a very rough-and-ready approach to parsing, which isn't as fully featured as one might want, and really needs a rework. I did take a different approach in my fork as posted over here:
65Org16 and 65Org32 emulation online

As a maintainer, I am being very slack indeed about working on the code. It would be great if someone with more energy and skills could join in. But I would very much want to keep the tutorial and the existing examples working as they are.
Holy crap, do you have any idea how helpful the Easy6502 assembler is? It's been invaluable in helping me figure out the bare basics. As long as it can read and run all the opcodes, I can always work around the bare bone assembler quirks.

Its half the reason I haven't gotten completely overwhelmed.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Need help with an incrementing 16-bit pointer

Post by GARTHWILSON »

EvilSandwich wrote:
barrym95838 wrote:
I have never heard of a symbolic assembler that didn't allow + or - in its operands. That would be a crippling omission IMO.
It's entirely possible that the Easy6502 does allow for it, but I'm just being dumb and don't understand how to use it in that assembler. haha
Every assembler I've ever seen allowed expressions, for example,

Code: Select all

        LDA  #{expression_of_some_kind}

and the expression could use whatever operators the assembler recognizes, and the only limitation is line length, often 256 characters. You might have the ANDing of constants (or numbers which can be changed in the assembler, like an EQUate that can be changed many times), shifting, XORing, addition, etc.. The LDA# always results in an $A9 op code being laid down, and its operand byte gets figured out by the assembler and laid down after the op code. The 6502 computer that runs the program has no interest in how the assembler arrived at that operand byte, or what it means.
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?
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Need help with an incrementing 16-bit pointer

Post by BigEd »

EvilSandwich wrote:

Holy crap, do you have any idea how helpful the Easy6502 assembler is? It's been invaluable in helping me figure out the bare basics. As long as it can read and run all the opcodes, I can always work around the bare bone assembler quirks.

Its half the reason I haven't gotten completely overwhelmed.
That's great to hear! I'll pass the message on to Nick, who is responsible for all the excellence.
EvilSandwich
Posts: 36
Joined: 13 Oct 2019
Location: Pennsylvania, USA

Re: Need help with an incrementing 16-bit pointer

Post by EvilSandwich »

I think the most frustrating part about all this is that I actually managed to make it work before with an older and way cruder version of the subroutine. But for some reason, I can't seem to recreate it.

This program works like a dream exactly the way I designed it too.

Code: Select all

idle:	;Waiting for input
			;Press W to Pop Confetti Forever!
			;Press O to Pop Confetti Just Once
			;Press S to Pause Confetti
			;Press Q to End Program

	
	LDA $ff	;reading last keystroke
	CMP #$71	;If Q, End Program
	BEQ end
	CMP #$53
	BEQ idle
	CMP #$6f
	BEQ poponce
	CMP #$77 ;If W, Pop Confetti!  Yay! :D
	BEQ poprepeat
	JMP idle ;If no input, continue idle

	poponce:

	LDA #$00 ;clearing last keystroke
	STA $ff
	JMP clearscreen	;cleaning up old confetti
	
	poprepeat:		;prepping program for confetti	
	
	JMP clearscreen	;cleaning up old confetti
	
	finish:		;ending program

	JMP end



clearscreen:	;clearing screen for new confetti


	LDX #$00 ;initial values and start
	LDY #$01
	STX $01
	STY $02

	incrementhigh: ;increment high byte
		
	LDX $02	;checking if finished
	CPX #$05
	BEQ boom ;pop confetti!
	INC $02	 ;increment high byte
	LDX #$00 ;returning low byte to 0
	STX $01

	incrementlow: ;increment low byte
	LDA #$00 ;define floor color
	STA ($01,X) ;sweep pixel
	INC $01	;increment low byte
	LDY $01	;load low byte counter
	CPY #$00 ;check low counter to see 
		 ;if high byte needs to be
		 ;incremented
		 ;otherwise increment low byte
	BEQ incrementhigh
	BNE incrementlow



boom:			;Yay! Party Time! :D :D :D
	

	loop:		;main confetti loop
		
	LDX $fe		;load random colors and locations
	LDA $fe
	STA $0200,x	;POP!
	LDX $fe
	LDA $fe
	STA $0300,x	;POP!
	LDX $fe
	LDA $fe
	STA $0400,x	;POP!
	LDX $fe
	LDA $fe
	STA $0500,x	;POP! Yay! All done!
	INY	 ;count confetti
	CPY #$20 ;make sure you have 
		 ;32 x 4 confetti pieces
	BNE loop ;if not enough, 
		 ;restart confetti loop
	JMP idle ;else return to idle
		 ;and wait for input

end:
        BRK

The second version of the clearscreen subroutine for the above confetti program that im working on right now. This one is a nonfunctional brick:

Code: Select all

LDA #$01 ;Initial values
LDX #$00
LDY #$02
STX $00 ;low byte
STY $01 ;high byte

mainloop: ;placeholder program loop
JSR counter ;increment pointer
LDA #$00 ;define floor color
STA ($01,X) ;sweep pixel
LDY $01 ;checking high byte
CPY #$05
BNE mainloop ;loop again
LDX $00 ;checking low byte
CPX #$ff
BNE mainloop ;loop again
BRK ;when value is 05ff, end


counter: ;incrementing subroutine

l_up: ;increment low byte
LDX $00
INC $00
CPX #$ff ;check if high byte needs to be incremented
BEQ h_up
RTS ;if not, return to main loop

h_up: ;increment high byte and return to main loop
INC $01
RTS
But yeah, I can get the pointer to increment in a million different ways, but that's not really the issue. I just can't get the program to read the silly thing from the zero page. :/

As you can see, I've been playing around with keyboard inputs and randomization in this program too. $00FE randomizes to a new value every clock cycle by the virtual hardware and $00FF's value is the ASCII value of the last keystroke.

If one of you way more experienced guys can tell me the silly and obvious thing I'm overlooking to get it to read the pointer, I'm all ears. lol

EDIT:
Note that both counters work just fine, but I can't seem to read the second one at all.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Need help with an incrementing 16-bit pointer

Post by BigEd »

This jumps out:

Code: Select all

STA ($01,X) ;sweep pixel
What's the value in X at this point?
EvilSandwich
Posts: 36
Joined: 13 Oct 2019
Location: Pennsylvania, USA

Re: Need help with an incrementing 16-bit pointer

Post by EvilSandwich »

BigEd wrote:
This jumps out:

Code: Select all

STA ($01,X) ;sweep pixel
What's the value in X at this point?
I was trying to make it the value of zero page $00, aka the Low Byte.

EDIT: Whoops double post. Sorry.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Need help with an incrementing 16-bit pointer

Post by GARTHWILSON »

Remember what I said on the last page, at viewtopic.php?p=70660#p70660 . Your "counter" subroutine can leave different values in X, and in your instruction "STA ($01,X)", the X value will get added to the number $01 to get the ZP address to read another address from, and you'll store to that final resulting address.
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?
EvilSandwich
Posts: 36
Joined: 13 Oct 2019
Location: Pennsylvania, USA

Re: Need help with an incrementing 16-bit pointer

Post by EvilSandwich »

GARTHWILSON wrote:
Remember what I said on the last page, at viewtopic.php?p=70660#p70660 . Your "counter" subroutine can leave different values in X, and the value will get added to the number $01 to get the ZP address to read another address from, and you'll store to that final resulting address.
So I accidentally made the value of X not equal to the Low Byte in the second version?
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Need help with an incrementing 16-bit pointer

Post by GARTHWILSON »

EvilSandwich wrote:
So I accidentally made the value of X not equal to the Low Byte in the second version?
What you're doing is adding the value to the place you're getting the address from, the address you're going to write to. If X starts out as 0, "STA ($01,X)" will fetch an address from $0001 and $0002 and write to the address it found there, then after incrementing X, the next "STA ($01,X)" will fetch and address from $0002 and $0003 and write to the address it found there (which is certain to be invalid, since it overlaps the first one you just did), then after incrementing X again, the next "STA ($01,X)" will fetch and address from $0003 and $0004 and write to the address it found there (which is again certain to be invalid), and so on. You'll fetch addresses from all across ZP—the entire page—which is not what you wanted.
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?
EvilSandwich
Posts: 36
Joined: 13 Oct 2019
Location: Pennsylvania, USA

Re: Need help with an incrementing 16-bit pointer

Post by EvilSandwich »

I was in the middle of stepping through my code with a debugger and found what you just said. I feel foolish now.

So I guess the better question is not "Why isn't the second one working?" and actually "Why did the first one work?"
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Need help with an incrementing 16-bit pointer

Post by GARTHWILSON »

You really need to get the 6502/65816 programmer's manual "Programming the 65816—Including the 6502, 65C02 and 65802" by David Eyes and Ron Lichty. It thoroughly diagrams every addressing mode, to clear up this confusion.

It is definitely the best 65xx programming manual available, and a must-have for every 65xx programmer! It starts with the basics, followed by architecture, the CMOS 65c02's many improvements over the original NMOS 6502 including added instructions and addressing modes and fixing the NMOS's bugs and quirks, and then the natural progression to the 65816; a thorough tutorial, writing applications, then very detailed and diagrammed information on all 34 addressing modes, at least a page of very detailed description for each instruction, with info on every addressing mode available for that instruction, then instruction lists, tables, and groups, of all 255 active op codes, plus more. 469 pages. From Western Design Center. (.pdf) Note: There were many problems with the earlier .pdf version that were not in the original paper manual; but in late March 2015, WDC scanned and OCR'ed the paper manual and posted the new, repaired .pdf.
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?
EvilSandwich
Posts: 36
Joined: 13 Oct 2019
Location: Pennsylvania, USA

Re: Need help with an incrementing 16-bit pointer

Post by EvilSandwich »

Can I just say that Western Design Center seems to keep proving again and again how awesome they are? lol
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Need help with an incrementing 16-bit pointer

Post by BigEd »

In the first version of the code, X is always zero, which is what you want, as your 16 bit pointer is in 0001 and 0002.

If you use STX ($01, X) with a non-zero value of X, you are using a pointer that's somewhere different from 0001 and 0002, because X is an index into zero page.

It sounds like you might be thinking of a mode ($01), X - which doesn't exist. In that case, the pointer is always at 0001 and 0002, but the value of X is added. In that case, you have some freedom as to whether you increment the low byte of the pointer, or X, because the two will be added together. But as this mode doesn't exist, you can't be doing that. The mode does exist with Y, but you should be aiming to understand the addressing mode you are using, not one that you're not.
EvilSandwich
Posts: 36
Joined: 13 Oct 2019
Location: Pennsylvania, USA

Re: Need help with an incrementing 16-bit pointer

Post by EvilSandwich »

BigEd wrote:
In the first version of the code, X is always zero, which is what you want, as your 16 bit pointer is in 0001 and 0002.

If you use STX ($01, X) with a non-zero value of X, you are using a pointer that's somewhere different from 0001 and 0002, because X is an index into zero page.

It sounds like you might be thinking of a mode ($01), X - which doesn't exist. In that case, the pointer is always at 0001 and 0002, but the value of X is added. In that case, you have some freedom as to whether you increment the low byte of the pointer, or X, because the two will be added together. But as this mode doesn't exist, you can't be doing that. The mode does exist with Y, but you should be aiming to understand the addressing mode you are using, not one that you're not.
Haha Yeah, as I was stepping through the code, that hit me like a ton of bricks.

So I want to keep X equal to 0?
Post Reply