6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 10:15 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Tue Sep 08, 2015 7:59 am 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
Reading more fine print in Programming the 65816 for my Crude Emulator, this time for the TXS instruction (because, yes, we're making progress with the stack system). And on page 416, I find this line:
Quote:
8-bit index registers (65802/65816 native mode, x=1): The stack pointer is sixteen bits but the index registers are only eight bits. A copy of the byte in X is transferred to the low stack pointer byte and the high stack pointer byte is zeroed.
(My emphasis). So: The high byte of the Stack Pointer is set to $00, it claims, not $01. I found confirmation of this in the archives (viewtopic.php?f=5&t=255&p=2894&hilit=txs+65816#p2894), but am still a bit shocked, shocked I say, about what would seem to be rather counter-intuitive for anybody coming from the 6502. This is really the actual behavior?


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 08, 2015 10:42 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
Right, so if you're in native mode (816 mode) and for some reason you set index registers to 8 bits, then you have trouble doing TXS to initialise the stack. But if you did the init before you set to 8 bit mode, that should be OK. In which case you'd initialise X to $01FF before the TXS.

It's a five-mode chip, and that's one of the things that makes it hard to get your head around how best to make use of it.


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 08, 2015 3:40 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
BigEd wrote:
It's a five-mode chip
Yes, you can set both X and M to 1 (ie select the 8-bit modes) but that's not the same as Emulation mode -- the latter has different wrap-around policy when an index potentially produces a page crossing. (There may be other differences, too -- I'm still learning this stuff!)

Something else to watch for is the asymmetry when moving between registers of different sizes. For example TAX and TXA:

    Copying 8-bit X to 16-bit A means the destination high-byte gets filled with zeroes. But...
    Copying 8-bit A to 16-bit X means the destination high-byte gets the (hidden) high-byte of the source! IOW,
    Copying 8-bit A to 16-bit X is the same as
    copying 16-bit A to 16-bit X.

If it helps (??), here's how I think about it. In the case of a mixed-size copy (X=1,M=0 or X=0,M=1) TXA and TAX are 16-bit operations. But X is incapable of containing ones in its high-byte when X =1 -- those bits are subject to a persistent and overriding reset. (Same for Y, of course.) X & Y aren't shortened when X =1 -- they remain 16-bit.

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 08, 2015 5:06 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
The general rule in native mode is that the "size" of the data that is copied is dictated by the "width" of the destination register. If x=1 and m=0 in the status register then a copy from an index register to the accumulator is 16 bits, with bits 8-15 all cleared. Hence the accumulator will contain $00xx, where xx is the content of the index register.

There are some exceptions, in that the instructions TCD, TCS TDC and TSC always cause a 16 bit copy, no matter the setting of the m bit in SR.

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


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 08, 2015 5:27 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
Yes, as you say,

if x=1 and m=0 then a copy from X/Y to A is 16 bits, with bits 8-15 all cleared. But I'm saying
if x=0 and m=1 then a copy from A to X/Y is 16 bits, with bits 8-15 preserved.

So I'm not quibbling -- merely drawing attention to some perhaps surprising behavior.

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Fri Sep 11, 2015 11:54 am 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
EDIT: This is what happens when you get too much sleep for once in your life -- read the entry for TSC while thinking about TCS. The scary part is that it made sense at the time. Thanks to Dr. J!

Ignore this below, pls:

But wait, it gets even stanger! Now we're coding TCS, which you might assume would work the same way as TXS, just with the Accumulator. But no:
Quote:
If the A accumulator is set to just eight bits (whether because the m flag is set, or because the processor is in 6502 emulation mode), then it takes the value of the low byte of the stack pointer and the hidden B accumulator takes the value of the high byte of the stack pointer.
So 8-bit native X moves "00xx" to S, when any sane person would expect "01xx", while 8-bit native A gives us "bbaa" and not "00aa", with "01aa" completely out of the question. Argh.

On days like this, I understand why people moved to the 68000 :D .


Last edited by scotws on Fri Sep 11, 2015 12:42 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Fri Sep 11, 2015 12:07 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
Maybe part of the trouble here is thinking that the stack naturally lives in page 1 - that's true in emulation mode, but not at all true in native mode. (In passing, the naming of these modes caused me brain strain at first, and to some extent still does - native to me sounds like normal, so that would be 6502 mode, whereas emulation sounds special, so that would be 816 mode. Except that's exactly wrong!)


Top
 Profile  
Reply with quote  
PostPosted: Fri Sep 11, 2015 6:55 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
Quote:
Now we're coding TCS, which you might assume would work the same way as TXS, just with the Accumulator.

It makes sense; because X can be 8 or 16 bits, whereas C means 16 bits, whether A includes B or not. It's not TAS, but TCS.

For the '816 in its native mode, page 1 has no special significance for the stack. It's no different from other pages.

_________________
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  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 32 guests


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: