Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Stacks / frame debug on ATMEGA2561

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
Csaba

Bascom Member



Joined: 05 May 2012
Posts: 25

hungary.gif
PostPosted: Thu May 24, 2012 7:29 am    Post subject: Stacks / frame debug on ATMEGA2561 Reply with quote

Dear Colleagues,

Iím having a probable stack problem, and Iíd like to discover the stack/frame situation in my program. The program is big, 90% of an ATMEGA2561.

First Iíve played with the stack/frame sizes. It was a good play, without results. The BASCOM report file always gives the same start addresses for the two stacks and the frame, regardless of stating the stacks/frame 100, 100, 100 or 1000, 800, 800 or any numbers between. It seems it decides the stack/frame sizes itself, not much based on the required/stated sizes. At least there is no correlation seen in the report file with the required sizes written in the source program.

The BASCOM stack description is always:

Stack start : 21FF hex
Stack size : 320 hex
S-Stacksize : 190 hex
S-Stackstart : 1EE0 hex
Framesize : 190 hex
Framestart : 1BC0 hex
Space left : 2087 dec
(which is OK only for 800, 400, 400)

Then Iíve tried STCHECK, which always gives ERROR 3, even if STCHECK is the first instruction in the program. Iíve discovered here in the forum, that this is an already known problem.

My next hope was using the $lib "stackcheck.lib" library. Iíve copied a program snippet from the forum, and it gave strange results. It printed 7 bytes of software stack and all the other numbers were unbelievable, too.

The last hope is using $dbg. Iíve planted DBG instructions in every subroutine I thought residing deepest in the program. Unfortunately $dbg has given up after writing out 3-4 printouts. I came to know, that it works only if the program is in a single source file. This project consists of 78 files now, so merging them by hand is next to impossible. I have to do this merging several times during the program development to check the stacks & frame.

My questions are the following:

Is there a tool to merge source files to one single .BAS file?
(Or I have to write some perl code for it?)

Is there a reasonable hope that $dbg will give real results? Did somebody try it with a bigger program?

What other tool or method can be used to solve (see) the stack/frame scenario?

Best regards,

Csaba Sztrancsik / AtySoft
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 552

germany.gif
PostPosted: Thu May 24, 2012 7:41 am    Post subject: Reply with quote

Hi Csaba,
one hint:
try Steckcheck inside your main loop, then you will get top values of stack usage (after Prgrom runs trough all subs/functions)

best michael

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
Csaba

Bascom Member



Joined: 05 May 2012
Posts: 25

hungary.gif
PostPosted: Thu May 24, 2012 8:45 am    Post subject: Reply with quote

Hi Michael,

Thanks for your reply.

Of course I've tried to put STCHECK in every part of the program, also in the main loop.
I've never seen anything else but ERROR 3. It would be a delight to see an error 2 or 1. I do not hope for an error 0.
I mentioned that the first line gives ERROR 3, as I think there is no way for me to do any harm to the frame than, as I did not call any subroutines at that line. This is the first line, the beginning, so there is no "before". Afterwards I can do bad things, which I have to find. But there is no reason to check, if it begins with an error before I start my program.
I think STCHECK doesn't work at all. At least for me. (or for ATMEGA256 processors.) I've read it in some post in this forum, too.

My main problem is, that I have no any other means to check the stacks and the frame usage. My only hope is $dbg, which I haven't tried yet.

Regards,

Csaba
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 552

germany.gif
PostPosted: Thu May 24, 2012 10:06 am    Post subject: Reply with quote

Code:


   $hwstack = 100
   $swstack = 100
   $framesize = 500


   Const Show_stack = 1

   #if Show_stack = 1
   $lib "stackcheck.lib"
   $hwcheck                                                 'hw stack check on
   $framecheck
   $softcheck
   #endif

   Dim Ww As Word , Www As Word , Wwww As Word
 




Code:

do
 #if Show_stack = 1
  Ww = _hwstackstart - _hw_lowest
  Www = _hwstack_low - _sw_lowest
  Wwww = _frame_high - _fw_highest
  Wwww = 500 - Wwww                                              ' VALUE 500 depends on your $framesize Value!
  Print "HW-SW-FS: " ; Ww ; "-" ; Www ; "-" ; Wwww
 #endif
loop
end
 

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5069
Location: Holland

blank.gif
PostPosted: Thu May 24, 2012 1:48 pm    Post subject: Re: Stacks / frame debug on ATMEGA2561 Reply with quote

Quote:
Iím having a probable stack problem, and Iíd like to discover the stack/frame situation in my program. The program is big, 90% of an ATMEGA2561.

First Iíve played with the stack/frame sizes. It was a good play, without results. The BASCOM report file always gives the same start addresses for the two stacks and the frame, regardless of stating the stacks/frame 100, 100, 100 or 1000, 800, 800 or any numbers between. It seems it decides the stack/frame sizes itself, not much based on the required/stated sizes. At least there is no correlation seen in the report file with the required sizes written in the source program.

no that is not correct. it is true true that hw stack always start at the end of the sram. and that variable space starts at the begin of sram, but that is it.
since hw stack grows down it is located at the end of sram. there is no other good location for it.
soft stack is located beneath HW stack and also grows down. so the sstack start will vary, easy to test with various $jwstack sizes.
And beneath soft stack there is frame space. and this grows up.

Quote:

The BASCOM stack description is always:

Stack start : 21FF hex
Stack size : 320 hex
S-Stacksize : 190 hex
S-Stackstart : 1EE0 hex
Framesize : 190 hex
Framestart : 1BC0 hex
Space left : 2087 dec
(which is OK only for 800, 400, 400)


i think you need to recheck. otherwise send me 2 files with different stack size that give the same report values.

Quote:
Then Iíve tried STCHECK, which always gives ERROR 3, even if STCHECK is the first instruction in the program. Iíve discovered here in the forum, that this is an already known problem.

yes that is a known problem which is fixed in 2075.

Quote:
My next hope was using the $lib "stackcheck.lib" library. Iíve copied a program snippet from the forum, and it gave strange results. It printed 7 bytes of software stack and all the other numbers were unbelievable, too.

you must make sure your code runs all subs, otherwise it will not work.
in most cases, frame space is set too low. a user then increases soft space and think it helps only because frame and soft space walk in to each other. essentially it does not matter : you can increase either soft space or create more frame space. only when you have a lot of nested routines you will use a lot of soft space. and recursion can eat up a lot of space too.

Quote:
The last hope is using $dbg. Iíve planted DBG instructions in every subroutine I thought residing deepest in the program. Unfortunately $dbg has given up after writing out 3-4 printouts. I came to know, that it works only if the program is in a single source file. This project consists of 78 files now, so merging them by hand is next to impossible. I have to do this merging several times during the program development to check the stacks & frame.
My questions are the following:

Is there a tool to merge source files to one single .BAS file?
(Or I have to write some perl code for it?)

there is not such a tool, but i can have a look at adding it as an option. when it does not take much time i can add it.

Quote:
Is there a reasonable hope that $dbg will give real results? Did somebody try it with a bigger program?

What other tool or method can be used to solve (see) the stack/frame scenario?


i always use the simulator. but not with 78 modules.
the stackcheck would be the best option.
and i looked at the normal frame space allocate function : it does not check the size and will walk into soft stack. i will change it so it will set the ERR variable.
that way you can simple determine if you ran out of space. a problem will occur in the function that claims the memory which should make it easier to find it.
The stack.lib does do this check but i feel that it is better to always check this this.

you can test it youself by modding the mcs.lib

[_AddSizeToFrame]
_AddSizeToFrame:
Add _SPL, R24 ; add size to _SPL and _SPH
Clr R24 ; clear register
Adc _SPH, R24 ; add carry

* ldi r24,lbyte(_frame_high) ; get frame max value
cp r4,r24 ; compare with R4
* ldi r24,hbyte(_frame_high) ; get MS byte
cpc r5,r24 ; compare with R5
brsh _addsizetoframe_ERR ; if it is equal or greather we have an error
ret
_AddSizeToFrame_ERR:
Set ; set T-flag
Bld R6,2
Ret
[END]

add the bold code and compile the lib.

_________________
Mark
Back to top
View user's profile Visit poster's website
Csaba

Bascom Member



Joined: 05 May 2012
Posts: 25

hungary.gif
PostPosted: Thu May 24, 2012 4:22 pm    Post subject: STACK and FS checking Reply with quote

Hello,

Yes, I used exactly these routines to check my stacks and frame. Probably I misunderstood something, so I'm trying to clarify the usage.

The printed out values (HW-SW-FS) are giving me the maximum used number of the corresponding stack/frame area from the beginning of the program run. (So it can only grow.)
I can make out a sub of your code, and use it as a probe when an identifiable call is taken to it from various parts of the program.
Should any of the three numbers be bigger than the corresponding HW-SW-FS required/stated with the $HWSTACK, $SWSTACK, $framesize directives, means an error.

Sometimes there are huge numbers in SW and FS. HW-stack seems to be stable and low. Probably I can catch an error. It would be great !

Thanks,

Csaba
Back to top
View user's profile
Csaba

Bascom Member



Joined: 05 May 2012
Posts: 25

hungary.gif
PostPosted: Thu May 31, 2012 7:17 am    Post subject: Report file misunderstanding, project based editing of basco Reply with quote

Dear Mark,

Previously I've mentioned that the report file contains the same number for stack sizes, regardless of modification.

Quote:
i think you need to recheck. otherwise send me 2 files with different stack size that give the same report values.



You are right, but the problem is more sophisticated. Probably others can benefit from my experience, so I describe it here.

This work contains a lot of files, so I'm using a project based editing method. I'm having a C compiler with the notion of project. So I made my basic files to C files with a simple trick.
The first line of all basic files is REM /* and the last line is REM */. REM defined in C as a macro not doing anything. In a small C program there is nothing more then includes for all my basic files belonging to this project. This way I can use the benefits of a project: searching keywords in all project files at once, replacing all occurence of a word, saving all modified project files with one click, etc. (SouceInsight might have been used, too. It even knows basic syntax.)

This way I'm modifying my sources beneath the BASCOM source interface/editor. Bascom USUALLY translates the disc stored version of the files, so there is no problem with this method.
I've found - probably I'm not right - that BASCOM gets the $hwstack, $swstack, $crystal variables from the opened version (which is on the screen) and not from the stored version. (which is on the disc, and modified by an other editor). The normal lines BASCOM gets from the stored disc version, if it thinks that the on-screen version is not modified.

This caused my misunderstanding.

I've checked it afterwards. Writing a nonsense basic line in the source on the disc, bascom reports an error.
Writing the same nonsense word after $swstack in the same on-disc file (while it is opened in bascom), the compiler remains silent, calculates with the numbers of the on-screen version of the file.

Interesting! And better to know for those using other tools to handle source files.

Am I figuring it right?

Regards,

Csaba

PS. It is also interesting that bascom accepts only a number for $hwstack etc. It doesn't handle a Const and even accepts nonsense words after $hwstack. Then it calculates with zero size.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5069
Location: Holland

blank.gif
PostPosted: Fri Jun 01, 2012 12:55 pm    Post subject: Reply with quote

what happens if you compile : the source is saved (but only the active window). if you use other tools, you must save it first before you compile.
after the file is saved, the $hwstack,$swstack and $framesize are grabbed from the options.
Then the editor is checked if these values exist and then these are used. These are passed to the compiler dll. these values need to be known in advance.
if you use a different editor you can best call bascomp command line util.

constants are not accepted, only numbers. so yes this is treated a bit different. I will have a look at it.

_________________
Mark
Back to top
View user's profile Visit poster's website
Csaba

Bascom Member



Joined: 05 May 2012
Posts: 25

hungary.gif
PostPosted: Fri Jun 01, 2012 1:36 pm    Post subject: modifying source files beneath bascom Reply with quote

Dear Mark,

Exactly this is what happens, and it is OK. I only didn't know that it works this way.

With my method the (false) illusion is, that the compiler compiles everything from the disc sources, except the $stack, $framesize, etc. lines, for which it turns back to the on-screen version of the main file.
In reality bascom stores these variables in itself at the moment when the on-screen version is saved. (When one pushes the floppy icon on the menu line.)

In my opinion it is completely right, but those folks using some other tools to modify sources beneath BASCOM, better know it.

Otherwise they get results like me: modifying $stack on the disc file, compiling the modified file from the disc, and not seeing the effect of the $stack modification. All other modifications are appearing in the resulted compilation.

Probably my usage style is not common, and as BASCOM is already having PROJECT, there is not much need to use it.
I used it because I had no information about the BASCOM PROJECT scheme, and I was re-writing a C source to bascom, so it was convenient to have the original and the transformed files in one project.

Thanks for the answer. I think that this issue is solved and is not a problem any more. And it was only a misunderstanding from my side.

Regards,

Csaba
Back to top
View user's profile
Csaba

Bascom Member



Joined: 05 May 2012
Posts: 25

hungary.gif
PostPosted: Fri Jun 01, 2012 1:45 pm    Post subject: Const in $framesize Reply with quote

Dear Mark,

As you wrote
Quote:

constants are not accepted, only numbers. so yes this is treated a bit different. I will have a look at it.

There is one reason for it if one uses the stack-check.lib method.
Than one can use a Const Requred_framesize in the $framesize line and also in the calculation of wwww using the stack-check library.
I've forgot to modify the number in calculating the wwww variable, which caused me some headache when I reduced my frame size. But finally I've found my error.

Regards,

Csaba
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
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
You cannot attach files in this forum
You cannot download files in this forum