CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Understanding how the stack works

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

Understanding how the stack works
PostPosted: Tue Jul 17, 2018 1:24 am     Reply with quote

Goodday,

I want to know what the line 02A72 does, it seems that it should overwrite the TOS with W5 (which are the value which should move to the SPI buffer) without increment or decrementing the stack pointer for this to work correct.

Code:
                         ..............................           spi_write(FALSE,Tx.Message[MsgNo]);
02A5E 818D50         MOV     31AA,W0        : W0 = [31AA]
02A60 231AC4         MOV     #31AC,W4       : W4 = 31AC
02A62 400004         ADD     W0,W4,W0       : W0 = W0+W4
02A64 784290         MOV.B   [W0],W5L       : W5L = [W0]
02A66 A9C240         BCLR.B  240.6          : SPI1STAT.SPIROV = 0
02A68 AE0240         BTSS.B  240.0          : Skip if SPI1STAT.SPIRBF = 1
02A6A 370002         BRA     2A70           : GoTo 2A70
02A6C BFC248         MOV.B   248,W0L        : W0L = SPI1BUF
02A6E 37FFFC         BRA     2A68           : GoTo 2A68
02A70 F80248         PUSH    248            : PUSH SPI1BUF to TOS
02A72 9FFFE5         MOV.B   W5L,[W15-#2]   : [W15+-2] = W5L
02A74 F90248         POP     248            : POP TOS to SPI1BUF


Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Tue Jul 17, 2018 1:43 am     Reply with quote

Though it is talking to the stack, it is a standard 'indirect addressing' operation.

Moves the byte held in W5L, to the location addressed by W15-2. This is what the brackets mean. Now W15, just happens to be the stack pointer, but could be any of the registers (note 7 lines before, where the byte addressed by W0 is used the same way). Here W0 is loaded with 'MsgNo', then has the location of the start of Tx.Message (0x31AC) added to it, and then the byte is loaded from this address.
In the Tx.Message version, the offset has to be calculated (since it changes), while in the one using W15, the offset is a fixed value of '-2', so can be included in the instruction. The fixed offset, can be a 'Slit10'. A 10bit signed value (-512 to 511).
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Tue Jul 17, 2018 4:28 am     Reply with quote

Thank you Ttelmah.

Understand now what happens, but couldn't W5L been moved directly to 248 instead of using 3 instructions as when you use a hardcoded offset.


Regards
Code:
                         ..............................           spi_write(FALSE,Tx.Message[0]);
02A50 A9C240         BCLR.B  240.6          : SPI1STAT.SPIROV = 0
02A52 AE0240         BTSS.B  240.0          : Skip if SPI1STAT.SPIRBF = 1
02A54 370002         BRA     2A5A           : GoTo 2A5A
02A56 BFC248         MOV.B   248,W0L        : W0L = SPI1BUF
02A58 37FFFC         BRA     2A52           : GoTo 2A52
02A5A 818D60         MOV     31AC,W0        : W0 = [31AC]
02A5C B7E248         MOV.B   W0L,248        : SPI1BUFL = W0L
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Tue Jul 17, 2018 6:57 am     Reply with quote

From some PIC24/33 data sheets:
Quote:

Note: Do not perform read-modify-write operations
(such as bit-oriented instructions) on
the SPIxBUF register in either Standard or
Enhanced Buffer mode.


MOV.B, is a RMW instruction.

It's a way of coding round an issue that the SPIxBUF register has, because of it's nature as a FIFO. It is not normal memory, you have to write the whole 16bits each time, even if you are using byte wide SPI. So they would either have to read the whole register into RAM, write the byte into this, and then write the whole value back, or use the stack for the same operation....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group