Increase and decrease the counter by one

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
belltone
Posts: 19
Joined: Wed Sep 09, 2015 6:00 pm

Increase and decrease the counter by one

Post by belltone »

I do not understand how to do so before reaching the setpoint was a countdown and then also check out and start the cycle all over again with the increase.

Code: Select all

move_right  .dsb 1

Rotate:
MoveRight:
  lda move_right
  clc
  adc #$01
  sta move_right
  cmp #$50
  bcc RotateDone
MoveLeft:
  lda move_right
  sec
  sbc #$01
  sta move_right
  bcs RotateDone
RotateDone:
Why when I prescribe in the code after the event is:

Code: Select all

  sec
  sbc #$01
the counter does not go on the decrease but simply stops?

Please help write a simple code that became clear to me what I'm doing wrong.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Increase and decrease the counter by one

Post by tokumaru »

After the increment happens, there's nothing stopping the decrement from happening too, so the value will appear to never change.

What is the desired behavior? Do you want the counter to go from $00 to $50 and back over and over? If so, you can't simply have the two operations one after the other, you need to keep track of which way the value is going (up or down), so you can either add or subtract 1, and you can check for the correct end condition (num = $50 or num = $00) before switching to the other mode.

Since the difference between the two modes here is basically how much the number changes (+1 or -1), you can keep track of the state using the delta itself. Something like this:

Code: Select all

init: ;should only run once
	lda $00
	sta value
	lda $01
	sta increment

update: ;should run every frame
	clc
	lda value
	adc increment
	sta value
	bit increment
	bmi negative
positive:
	cmp #$50
	bne done
	lda #$ff
	sta increment
	bne done
negative:
	cmp #$00
	bne done
	lda #$01
	sta increment
done:
Whenever an entity in your program needs to assume different behaviors, you need to keep track of its state somehow. This is essential in a game, where characters can walk, jump, shoot, and so on, and each of these states is handled differently.
belltone
Posts: 19
Joined: Wed Sep 09, 2015 6:00 pm

Re: Increase and decrease the counter by one

Post by belltone »

Many thanks your code good option, I did so from what I understand:

Code: Select all

movex      .dsb 1
moveleft   .dsb 1
moveright  .dsb 1

InitMove:
        lda #$01
        sta moveright
        lda #$00
        sta moveleft
InitMoveDone:

MoveLeft:
        lda moveleft
        beq MoveLeftDone
        lda movex
        sec
        sbc #$01
        sta movex

        lda movex
        cmp #$50
        bcs MoveLeftDone

        lda #$01
        sta moveright
        lda #$00
        sta moveleft
MoveLeftDone:

MoveRight:
        lda moveright
        beq MoveRightDone       
        lda movex
        clc
        adc #$01
        sta movex

        lda movex
        cmp #$FF
        bcc MoveRightDone

        lda #$00
        sta moveright
        lda #$01
        sta moveleft
MoveRightDone:
        rts
Post Reply