sdcc pic18f and code generated

Help
tymop
2009-03-04
2013-03-12
  • tymop
    tymop
    2009-03-04

    Hello all:

    I have a question/problem with the sdcc compiler : it gives me a big hex code for a code I did not write (see example below):
    ---------------------------------------------------------------------
    This is the code c (very simple : necessite around 3 opcodes):

    #include <pic18f252.h>
    void main() {
        TRISA=0;
        while(1)
        {
        PORTA=0xff;
        }
    }

    -----------------------------------
    this is the asm code generated:

    ;--------------------------------------------------------
    ; File Created by SDCC : free open source ANSI-C Compiler
    ; Version 2.8.0 #5117 (Mar 23 2008) (MINGW32)
    ; This file was generated Wed Mar 04 09:29:19 2009
    ;--------------------------------------------------------
    ; PIC16 port for the Microchip 16-bit core micros
    ; * MPLAB/MPASM/MPASMWIN/MPLINK compatibility mode enabled
    ;--------------------------------------------------------
        list    p=18f252
        include <p18f252.inc>

        radix dec

    ;--------------------------------------------------------
    ; public variables in this module
    ;--------------------------------------------------------
        global _main

    ;--------------------------------------------------------
    ; extern variables in this module
    ;--------------------------------------------------------
        extern _PORTAbits
        extern _PORTBbits
        extern _PORTCbits
        extern _LATAbits
        extern _LATBbits
        extern _LATCbits
        extern _TRISAbits
        extern _TRISBbits
        extern _TRISCbits
        extern _PIE1bits
        extern _PIR1bits
        extern _IPR1bits
        extern _PIE2bits
        extern _PIR2bits
        extern _IPR2bits
        extern _EECON1bits
        extern _RCSTAbits
        extern _TXSTAbits
        extern _T3CONbits
        extern _CCP2CONbits
        extern _CCP1CONbits
        extern _ADCON1bits
        extern _ADCON0bits
        extern _SSPCON2bits
        extern _SSPCON1bits
        extern _SSPSTATbits
        extern _T2CONbits
        extern _T1CONbits
        extern _RCONbits
        extern _WDTCONbits
        extern _LVDCONbits
        extern _OSCCONbits
        extern _T0CONbits
        extern _STATUSbits
        extern _INTCON3bits
        extern _INTCON2bits
        extern _INTCONbits
        extern _STKPTRbits
        extern _PORTA
        extern _PORTB
        extern _PORTC
        extern _LATA
        extern _LATB
        extern _LATC
        extern _TRISA
        extern _TRISB
        extern _TRISC
        extern _PIE1
        extern _PIR1
        extern _IPR1
        extern _PIE2
        extern _PIR2
        extern _IPR2
        extern _EECON1
        extern _EECON2
        extern _EEDATA
        extern _EEADR
        extern _RCSTA
        extern _TXSTA
        extern _TXREG
        extern _RCREG
        extern _SPBRG
        extern _T3CON
        extern _TMR3L
        extern _TMR3H
        extern _CCP2CON
        extern _CCPR2L
        extern _CCPR2H
        extern _CCP1CON
        extern _CCPR1L
        extern _CCPR1H
        extern _ADCON1
        extern _ADCON0
        extern _ADRESL
        extern _ADRESH
        extern _SSPCON2
        extern _SSPCON1
        extern _SSPSTAT
        extern _SSPADD
        extern _SSPBUF
        extern _T2CON
        extern _PR2
        extern _TMR2
        extern _T1CON
        extern _TMR1L
        extern _TMR1H
        extern _RCON
        extern _WDTCON
        extern _LVDCON
        extern _OSCCON
        extern _T0CON
        extern _TMR0L
        extern _TMR0H
        extern _STATUS
        extern _FSR2L
        extern _FSR2H
        extern _PLUSW2
        extern _PREINC2
        extern _POSTDEC2
        extern _POSTINC2
        extern _INDF2
        extern _BSR
        extern _FSR1L
        extern _FSR1H
        extern _PLUSW1
        extern _PREINC1
        extern _POSTDEC1
        extern _POSTINC1
        extern _INDF1
        extern _WREG
        extern _FSR0L
        extern _FSR0H
        extern _PLUSW0
        extern _PREINC0
        extern _POSTDEC0
        extern _POSTINC0
        extern _INDF0
        extern _INTCON3
        extern _INTCON2
        extern _INTCON
        extern _PRODL
        extern _PRODH
        extern _TABLAT
        extern _TBLPTRL
        extern _TBLPTRH
        extern _TBLPTRU
        extern _PCL
        extern _PCLATH
        extern _PCLATU
        extern _STKPTR
        extern _TOSL
        extern _TOSH
        extern _TOSU

    ;--------------------------------------------------------
    ; interrupt vector
    ;--------------------------------------------------------

    ;--------------------------------------------------------
    ; global & static initialisations
    ;--------------------------------------------------------
    ; I code from now on!
    ; ; Starting pCode block
    S_Simple_c_programme__main    code
    _main:
    ;    .line    8; Simple_c_programme.c    TRISA=0;
        CLRF    _TRISA, ACCESS
    _00106_DS_:
    ;    .line    11; Simple_c_programme.c    PORTA=0xff;
        MOVLW    0xff
        MOVWF    _PORTA, ACCESS
        GOTO    _00106_DS_
        RETURN   

    ; Statistics:
    ; code size:       12 (0x000c) bytes ( 0.01%)
    ;                   6 (0x0006) words
    ; udata size:        0 (0x0000) bytes ( 0.00%)
    ; access size:        0 (0x0000) bytes

        end

    ----------------------------------------
    and then, this is the hex file result :
    :020000040000FA
    :0400000065EF00F0B8
    :06002A00E20EF66E000E6E
    :10003000F76E000EF86E0900F550056E0900F550D8
    :10004000066E04E1056702D064EF00F00900F55088
    :10005000006E0900F550016E0900F550026E0900AE
    :100060000900F550E96E0900F550EA6E0900090033
    :100070000900F550036E0900F550046E09000900EF
    :10008000F6CF07F0F7CF08F0F8CF09F000C0F6FF81
    :1000900001C0F7FF02C0F8FF035002E1045009E07D
    :1000A0000900F550EE6EF5CF7EFF0306F6E204067A
    :1000B000F7D707C0F6FF08C0F7FF09C0F8FF05062D
    :1000C00001E2060621EF00F0120012EEFFF022EE30
    :1000D000FFF0F86AA68EA69C15EC00F078EC00F014
    :1000E000FFD70100FC0000008000000001000000BC
    :0E00F000926AFF0E806E79EF00F012000000A1
    :00000001FF

    You can see the hex code reulted is very big. This function only make the PORTA output and put it at 0xFF.

    this is the command line and results:
    Executing: "C:\Program Files\SDCC\bin\sdcc.exe" Simple_c_programme.c -c -mpic16 -p18F252 --mplab-comp
    Executing: "C:\Program Files\SDCC\bin\sdcc.exe"  "C:\DATA\users\Sylvain\GPS_courseTracker\GPS_Embarque\Softs\Simple_c_programme\Simple_c_programme.o" -o"simple.HEX" -mpic16 -p18F252
    message: using default linker script "C:\Program Files\gputils\lkr\18f252.lkr"
    Loaded C:\DATA\users\Sylvain\GPS_courseTracker\GPS_Embarque\Softs\Simple_c_programme\simple.COD.
    BUILD SUCCEEDED: Wed Mar 04 09:29:21 2009

    I tried a lot of combination  of command line options, but it did not gave me a good hex file. Maybe I did not used "THE good combination of options".

    Maybe anybody can give me some help on this topic.
    Thanks a lot.

     
    • Raphael Neider
      Raphael Neider
      2009-03-04

      You want to compile with
      --use-crt=crt0.o

      Beware: The code currently linked into the project (from crt0i.o) takes care of initializing global and static variables. Specifying to use crt0.o instead only sets up the stack and frame pointers (FSR1, FSR2) and calls your main routine.

      You can gain even more control via
      --use-crt=" "

      (mark the space inside quotes, otherwise sdcc will complain; this is a nastiness that should be fixed in the compiler, I just found about this myself...). You will then have to include a _startup() routine, something like

      void _startup(void)
      {
        __asm lfsr 1, __stack_end __endasm; /* if desired */
        __asm lfsr 2, __stack_end __endasm; /* if desired */
        __asm goto _main __endasm;
      }

      in your project. On startup, the reset vector will jump to _startup() --- from there on it's your turn!

      Finally, you can specify
      --no-crt

      and implement the RESET vector yourself:

      void reset_handler(void) __interrupt(0) __naked
      {
        __asm nop __endasm; /* some In-Circuit debugger needs this */
        __asm goto __startup __endasm;
      }

      The function declared with __interrupt(0) will be located at code address 0x0. Beware: there is not much space there (the other interrupt vectors follow), so you should only place a goto somewhere in the handlers and mark them as __naked to eliminate function prologue and epilogue code.

      Hope that helps,
      Raphael

       
    • tymop
      tymop
      2009-03-04

      thank you very much for your so completed and quick answer.