#1236 Cannot init variables in declaration

closed-works-for-me
z80 port (188)
5
2009-09-20
2006-11-14
Anonymous
No

SDCC :
mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08
2.6.0 #4309 (Jul 28 2006) (MINGW32)

Target: Z80

There is great bug in declaration (Z80 port):

char VARIABLE = 10; // or any other value

or

void Function(void)
{
static char LOC_VARIABLE = 20;

Both variables (VARIABLE, LOC_VARIABLE) contain
undefined values, which in my sources usually means
crash of compiled code. This is great bug, because
initialisation during declaration is very often used :-(

Discussion

  • Nobody/Anonymous

    Logged In: NO

    I am sorry, I forgot add concrete source code:

    static void game_NP1_DebugPoint1(void)
    {
    static UINT8 index = 0;

    ZX_Pix[index] = 0x11;
    ZX_Col[index] = 0x22;

    Values should be written to arrays at index[0], but they are
    written to index[0xff]. The same wrong result is when local
    variable is moved to global section:

    UINT8 index = 0;
    static void game_NP1_DebugPoint1(void)
    {
    ZX_Pix[index] = 0x11;
    ZX_Col[index] = 0x22;

    Temporary bugfix for SDCC is:

    static void game_NP1_DebugPoint1(void)
    {
    static UINT8 index;

    if (firstcall_flag) index = 0;

    ZX_Pix[index] = 0x11;
    ZX_Col[index] = 0x22;

     
  • Philipp Klaus Krause

    Logged In: YES
    user_id=564030
    Originator: NO

    Works for me in sdcc #4399 using my custom crt0.o.
    Do you use a custom crt0.o? If yes please check that you call gsinit, and gsinit is implemented correctly.

    Example:
    First call in my crt0.s (directly after setting the stack pointer):
    call gsinit

    At the end of my crt0.s:

    .area _GSINIT
    gsinit::
    .area _GSFINAL
    ret

     
  • Nobody/Anonymous

    Logged In: NO

    spth: I use no crt0 or any other library (I do all IO using "sfr" and "at" arrays). But initialisation of variables should be done by compiler in my modules, not by included libraries.

     
  • Raphael Neider

    Raphael Neider - 2006-11-15

    Logged In: YES
    user_id=1115835
    Originator: NO

    You need some piece of code that either (a) copies initialized variables from ROM to RAM or (b) sets their values on start up. This is different from the way a PC works, as there is no "loader" copying your .hex to RAM; you just program your ROM/Flash and assume the RAM to be undefined.
    The compiler (SDCC) does generate the required code, still you need to call it (usually done as part of RESET handling routines, in crt0).

     
  • Nobody/Anonymous

    Logged In: NO

    Thank You, I understand, where is probably problem. But I have found no information, how to call global init code ("The compiler (SDCC) does generate the required code, still you need to call it").
    "gsinit::" part in CRT0 is empty, what should I write here to force variables init?

     
  • Nobody/Anonymous

    Logged In: NO

    I have tried to implement modified CRT0 and after it none of my program runs (crash after start).
    I looked into documentation of SDCC and there is nothing about CRT0 (how to write, how to use) there. Google also found nothing about CRT0 in SDCC. It is strange, that so important necessary think such as variables init is nowhere described. Can You upload here working piece of code including CRT0.s [Date: 2006-11-14 14:08 "Works for me in sdcc #4399 using my custom crt0.o."]?

    My modified CRT0.s which crashes all my programs (without any CRT0 all software they work fine, only variables are not initialised) is here (target platform is ZX Spectrum, program begins at $5b00, stack must be at $ff44 - the same values I use without any CRT0 module and programs work with no crash):

    .module crt0
    .globl _main

    .area _HEADER (ABS)
    .org 0x5b00
    ; jp init
    init:
    ld sp,#0xff44

    ;; Initialise global variables
    call gsinit
    call _main
    jp _exit

    ;; Ordering of segments for the linker.
    .area _HOME
    .area _CODE
    .area _GSINIT
    .area _GSFINAL

    .area _DATA
    .area _BSS
    .area _HEAP

    .area _CODE
    __clock::
    ret

    _exit::
    jp 0

    .area _GSINIT
    gsinit::

    .area _GSFINAL
    ret

    My last question persists: what shoud be written into "gsinit::" block to init?

     
  • Philipp Klaus Krause

    A custom crt0.s

     
  • Philipp Klaus Krause

    I have uploaded "A custom crt0.s", which I use with the ColecoVision - program at 0x8000, stack grows downward from 0x73ff.

    You don't place anything manually into the gsinit block. the .area GSFINAL is where the linker places the initialization code generated by sdcc.

     
  • Philipp Klaus Krause

    • assigned_to: nobody --> spth
    • status: open --> closed-works-for-me
     
  • Philipp Klaus Krause

    Closing this bug. It works for me, the bug submitter hasn't replied for three years.

    Philipp

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks