Creating a text box

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

puppydrum64
Posts: 160
Joined: Sat Apr 24, 2021 7:25 am

Re: Creating a text box

Post by puppydrum64 »

Quietust wrote: Mon May 31, 2021 4:55 pm
puppydrum64 wrote: Mon May 31, 2021 4:42 pm What does it mean to "unroll" a loop exactly?
You could try doing a web search for it, which would've given you this answer a lot more quickly than asking here.
Fair enough. Turns out it was something I had been doing already in other places but I didn't know the name of it

As for applying it, I think it's been quite a useful tool indeed so far.

Code: Select all

		LDA #$21
		STA textBoxPointer+1
		LDA #$04
		STA textBoxPointer
		STA temp0
		LDX #$08		;use bne
loop_setDrawBuffer:
		LDA temp0
		sta text_box_draw_buffer,x
		clc
		adc #$20
		sta temp0
		dex
		bne loop_setDrawBuffer
		
		
		
		LDA UpdateFlags
		ORA #UPDATE_TEXT_BOX		;#%ABCDEFGH where H = Draw Text Box
		STA UpdateFlags
-
		LDA vblanked
		BNE -						;wait here for nmi to occur. Call stack: Status K, X, Y
		
		;This will trigger a subroutine in NMI to draw the text box.
	popXY
	RTS
	
; part of NMI
	LDA UpdateFlags
	AND #UPDATE_TEXT_BOX
	BNE notDoneWithTextBox
	JMP doneWithTextBox
notDoneWithTextBox:
	;TEXT BOX NMI CODE GOES HERE
	nmi_textBox:	;CALL STACK: STATUS K, X, Y, RETURN ADDRESS, A, X, Y
	LDX #$08
unrolled_Loop:
	LDA $2002
	LDA textBoxPointer+1
	STA $2006
	LDA text_box_draw_buffer,x
	STA $2006
	
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007	
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007	
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	LDA #NULL_TILE
	STA $2007
	DEX
	BEQ doneWithTextBox
	JMP unrolled_Loop
doneWithTextBox:

	LDA #$00
	STA $2005
	STA $2005
Image

Works a lot better now. I've only tested it with a constant location for the text box. Of course there are much more difficult steps to this such as preserving the old background, etc. but I'll get to those in due time.
Oziphantom
Posts: 1350
Joined: Tue Feb 07, 2017 2:03 am

Re: Creating a text box

Post by Oziphantom »

puppydrum64 wrote: Mon May 31, 2021 1:28 pm I thought ASL preserved bit 7 (the leftmost bit) but LSL did not. For example:

Code: Select all

LDA #%10000000
ASL A
wouldn't change anything. But maybe I'm wrong.
There is no LSL.

There is ASL and LSR no LSL. If you assembler accepts LSL then it is an alternative mnemonic for ASL nothing more.
Pokun
Posts: 2292
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Creating a text box

Post by Pokun »

An arithmetic or logical shift to the left means you shift all bits one step left, bit 7 goes into carry and bit 0 is cleared.
Logical shift to the right is the same thing, you shift all bits one step right, bit 0 goes into carry and bit 7 is cleared.
Arithmetic shift right is the only different one as it preserves the sign by setting bit 7 to a copy of the previous bit 7 to sign-extend it, instead of simply clearing it as in the case of logical shifts.

Therefore LDA #%10000000 ASL A results in A=0 and Carry=1.
Oziphantom
Posts: 1350
Joined: Tue Feb 07, 2017 2:03 am

Re: Creating a text box

Post by Oziphantom »

In the global sense, there is an Arithmetic shift Left as well. i.e

-65 Arithmetic shift left = -2
-65 Shift left = 126

-65 Arithmetic shift right = -33
-65 Shift right = 95

However on a 6502 there is no Arithmetic operations.
ASL does not stand for Arithmetic Shift Left. The opcodes name is "Shift Left One Bit (Memory of Accumulator)" probably the A stood for Accumulator but they added Memory modes later.
Pokun
Posts: 2292
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Creating a text box

Post by Pokun »

Huh, is that true? I checked Rockwell's datasheet and it doesn't seem to list the instruction names, but the 6510 datasheet (and a few other older MOS documents regarding 6502) does and it's indeed called "Shift Left One Bit (Memory or Accumulator)" there. However LSR is called "Shift Right One Bit (Memory or Accumulator)", so if "A" stood for "Accumulator" then what does "L" stand for? I wonder if these aren't more like a general description that someone made? About all other sources says ASL "Arithmetic Shift Left" and LSR "Logical Shift Right", including WDC's datasheet of the W65C02S. The 6510 datasheet's nomenclature is even more confusing than the more common one (really MOS, LSL and LSR had been the most logical).


Also I don't get the math.

Shifting the 8-bit value -65 (%1011 1111):
Logical shift left: 126 (%0111 1110)
Arithmetic shift left: 126 (%0111 1110)
Logical shift right: 95 (%0101 1111)
Arithmetic shift right: -33 (%1101 1111)

So when shifting left there is no difference between logical and arithmetic. The calculator gives me this result as well.
puppydrum64
Posts: 160
Joined: Sat Apr 24, 2021 7:25 am

Re: Creating a text box

Post by puppydrum64 »

Oziphantom wrote: Mon May 31, 2021 11:20 pm
puppydrum64 wrote: Mon May 31, 2021 1:28 pm I thought ASL preserved bit 7 (the leftmost bit) but LSL did not. For example:

Code: Select all

LDA #%10000000
ASL A
wouldn't change anything. But maybe I'm wrong.
There is no LSL.

There is ASL and LSR no LSL. If you assembler accepts LSL then it is an alternative mnemonic for ASL nothing more.
I know there's no ASL on 6502, I meant on other systems.
Pokun
Posts: 2292
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Creating a text box

Post by Pokun »

You mean no LSL as there is ASL on 6502. Though ASL and LSL are both the same thing, as explained above (until someone proves me wrong at least).
Oziphantom
Posts: 1350
Joined: Tue Feb 07, 2017 2:03 am

Re: Creating a text box

Post by Oziphantom »

Pokun wrote: Tue Jun 01, 2021 1:43 pm Huh, is that true? I checked Rockwell's datasheet and it doesn't seem to list the instruction names, but the 6510 datasheet (and a few other older MOS documents regarding 6502) does and it's indeed called "Shift Left One Bit (Memory or Accumulator)" there. However LSR is called "Shift Right One Bit (Memory or Accumulator)", so if "A" stood for "Accumulator" then what does "L" stand for? I wonder if these aren't more like a general description that someone made? About all other sources says ASL "Arithmetic Shift Left" and LSR "Logical Shift Right", including WDC's datasheet of the W65C02S. The 6510 datasheet's nomenclature is even more confusing than the more common one (really MOS, LSL and LSR had been the most logical).
6510 datashet as printed in the C64 Programmers reference guide and the 6502 guides are "from the pen of Chuck" I believe and are from the point when Chuck was locked in a room and forced to make documentation and they are basically whatever Chuck wrote at the time. It has a lot of flaws SEI has tripped me up before. Non-Masking, at least it doesn't have the Pop instead of Pull. The 6800 has ASR, ASL and LSR while the 6502 only has ASL and LSR so the mnemonics match the 6800, just maybe the language was changed due to the law suit.
Also I don't get the math.

Shifting the 8-bit value -65 (%1011 1111):
Logical shift left: 126 (%0111 1110)
Arithmetic shift left: 126 (%0111 1110)
Logical shift right: 95 (%0101 1111)
Arithmetic shift right: -33 (%1101 1111)

So when shifting left there is no difference between logical and arithmetic. The calculator gives me this result as well.
I did
Arithmetic shift left: -2 (%1111 1110)
I.e preserving the sign. But it seems looking it up more completly that every implementation does it without preserving the sign. So you are right.
Pokun
Posts: 2292
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Creating a text box

Post by Pokun »

Ah for the 6800 the syntax makes sense, ASR and ASL are Arithmetic Shift Right and Left respectively. I guess there is value in keeping the ASL from the 6800 instead of introducing the LSL mnemonic. Thanks for the explanation, I have wondered about this lack of symmetry in the 6502's syntax for a long time. The 6800 seems to fill many of the holes that the 6502 has in its syntax due to cutting down on instructions.

OK now I see what you did. I don't really understand the arithmetic reason why the sign doesn't need to be preserved when arithmetically shifting a signed number left, but that is what all the textbooks says.
Oziphantom
Posts: 1350
Joined: Tue Feb 07, 2017 2:03 am

Re: Creating a text box

Post by Oziphantom »

*slaps forehead*
arithmetic basically = "multiply by 2" thus it's a mul, and a negative * a negative = positive
Pokun
Posts: 2292
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Creating a text box

Post by Pokun »

Ah of course! And arithmetically shifting right is division by 2 in which case the sign of the dividend should be kept (-8 / 2 = -4).
calima
Posts: 1579
Joined: Tue Oct 06, 2015 10:16 am

Re: Creating a text box

Post by calima »

Uh, you're all way off. Shift left preserves sign unless you overflow, which happened here. -65 * 2 = -130, which can't fit in a signed 8-bit var. Even if you think of it as *= 2, that's a positive 2, not a negative.
unregistered
Posts: 1298
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Creating a text box

Post by unregistered »

calima wrote: Thu Jun 03, 2021 12:29 am Uh, you're all way off. Shift left preserves sign unless you overflow, which happened here. -65 * 2 = -130, which can't fit in a signed 8-bit var. Even if you think of it as *= 2, that's a positive 2, not a negative.
Pokun wrote: Tue Jun 01, 2021 3:24 am An arithmetic or logical shift to the left means you shift all bits one step left, bit 7 goes into carry and bit 0 is cleared.
Logical shift to the right is the same thing, you shift all bits one step right, bit 0 goes into carry and bit 7 is cleared.
Arithmetic shift right is the only different one as it preserves the sign by setting bit 7 to a copy of the previous bit 7 to sign-extend it, instead of simply clearing it as in the case of logical shifts.

Therefore LDA #%10000000 ASL A results in A=0 and Carry=1.
Pokun, is spot on… well… except that -128 * 2 = -256… which overflows the byte so the sign changes.

calima, that is a valid truth! 👍

-21 * 2 = -42.
11101011 * 2 = 11010110

I remember now that negative binary numbers were written in a certain way that shifting left does preserve the sign, if the result fits in a byte. Though, if you shift (rol) that carry into the high byte, the 16bit result will still be a valid negative binary number. Bc -21’s high byte would be all 1s.


EDIT: This might not be obvious to some:
When using, say, a 16bit number to hold -128… asl still works (preserves the sign) bc the sign bit is always the left most bit in a signed binary number.

i.e. -128 as a 16bit number is:
1111 1111 1000 0000
After asl and a rol to transfer the carry:
1111 1111 0000 0000 = -256

The bold bit denotes the sign bit, so the 1 changing to a 0 in the low byte means nothing bc the 7th bit of a 16bit number IS NOT its sign bit.
tepples
Posts: 22414
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Creating a text box

Post by tepples »

Think of ASL as meaning Add to SeLf.

Oh, and if you need help integrating my Popslide library, I'm available.
Pokun
Posts: 2292
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Creating a text box

Post by Pokun »

Yeah that's what I meant.
Post Reply