Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo


#36 PIC16 reset vector

Richard G

I am finding an annoying feature of the reset vector
for the pic16 architecture in the latest sdcc build.
I am using the 2.5.0 with the
(may 22)
nightly build.
sdcc -v gives:
2.5.1 #1031 (May 22 2005) (UNIX)

there are 3 points:

1. simply declare main as interrupt 0 :
void main(void) interrupt 0
- problem: although the goto is used (in the asm)(thus
allowing interrupt vector at 00008) it saves the wreg,
bsr etc as the first instructions in the asm file (when
the chip has just switch on), why???. If using the
shadow regs keyword this only removes some of the
problem - some reg's are still saved. (main also ends
with a RETFIE - no need)

2. declaring the function as '_naked' stops the saving
of the regs but removes the 'goto' thereby writing main
code over the ISR at 00008. - in an earlier version :
0.0.0 #959 (Feb 13 2005) (UNIX)
this dosnt happen (the goto appears with the _naked

3. I have worked around this by using some inline asm,
the chip _seems_ to run ok (but it may be executing the
isr on start, i havnt checked):

void main(void) interrupt 0 _naked
ORG 000
GOTO _mainloop

I renamed my original main to (mainloop) and made that
_naked also.

It just seems strange that this problem didnt exist in
the earleir version.

It's of course possible that I have missed something
basic here, please let me know and thanks for your
time. Your SDCC compiler is a commendable undertaking
and I have been using it for part of my third year
degree project developing an ultrasonic and bump-sensor
interface to a CAN bus.

If you want to see the code, I have attached a tar file.
I thought that would be better than copying and pasting
all the code in here.

Thanks Again

Richard Gardiner
richARDg@mail.com << remove the CAPTIALS


  • Richard G
    Richard G

    makefile, source code and asm output

  • Raphael Neider
    Raphael Neider

    Logged In: YES

    From your Makefile I take that you do not link against
    crt0i.o/crt0iz.o/crt0.o. You SHOULD link against crt0i.o (or
    crt0iz.o) to have your static globals initialized (the "i"
    means initialize, "z" is for zero complete RAM (except
    initialized locations...)).

    Doing this will automatically have SDCC...
    (i) define the initializer routine as interrupt 0-handler,
    (ii) CALL main after initialization is complete and thus
    (iii) solve (most of) your problems (I believe).

    There should be no need to declare main() _naked nor will it
    be possible to declare it as interrupt 0-handler (as then
    two handlers would exists, which is a bad thing...). The
    only potential problem left is that SDCC will happily save
    all locals used in main (that's r0x?? in the .asm file) on
    the stack without need, but I might try to prevent that from
    happening (or you still use void main (void) _naked { /*
    code */ } )...

    Let me know if this solved your problem.

  • Raphael Neider
    Raphael Neider

    • milestone: --> 100455
    • assigned_to: nobody --> tecodev
  • Raphael Neider
    Raphael Neider

    • labels: 608414 -->
    • milestone: 100455 -->
  • Raphael Neider
    Raphael Neider

    Logged In: YES

    Just realized that your globals only pretend (by
    non-indentation) to be globals. So the initializer argument
    can be dropped.
    But you need to set up the stack!!! Use
    #pragma stack 0x300 0xff
    to declare the call-stack at 0x300..0x3FF and link against
    crt0/crt0i/crt0iz to setup FSR1 and FSR2 accordingly.
    I bet this is mentioned in the docs somewhere... ;-)

    Regards, Raphael

  • Maarten Brock
    Maarten Brock

    • status: open --> closed
  • Maarten Brock
    Maarten Brock

    Logged In: YES
    Originator: NO

    I assume that the comments by Raphael solved this issue.