Menu

#3579 z80 allow-undocumented-instructions causing parameter corruption

closed-fixed
None
Z80
5
2023-05-04
2023-05-04
Under4Mhz
No

When I run the below code, I get the value 32770 printed instead of the expected value of 8.

Works when --allow-undocumented-instructions is removed.

/// GPL 2.0 or later
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

typedef struct {

    uint8_t type;
    uint8_t state;
    uint8_t left;
    uint8_t width;
    uint8_t ingress;
    uint8_t scale;

} SpriteData;

uint8_t vdu_patch_crop_impl( uint8_t id, int8_t col, int8_t row,
    int8_t width, int8_t offset, const uint8_t *counts, const uint8_t *colours,
    int8_t cols, int8_t rows, uint16_t tile_offset, bool zoom ) {

    printf( "%u\n", tile_offset );

    return 0;
}

typedef uint8_t ImageBaseType;

typedef struct {

    const ImageBaseType * const * const * const sprites;
    uint8_t bank;

} SpriteItem;

const SpriteItem * wolf_sheets[1];

uint8_t sprite_state_show( uint8_t id, SpriteData *sprite, uint8_t y, uint8_t tile ) {

    static bool zoom = false;

    // Only allow zoom on first sprite, if it's full size enemy
    if ( id == 0 ) {
        zoom = sprite->type > 1 && sprite->scale == 5;
        if ( zoom ) sprite->scale--;
    } else {
        if ( zoom ) goto finish;
    }

    const SpriteItem * sprite_data = wolf_sheets[0];

    const ImageBaseType * const * const scale_sprite_data = sprite_data[0].sprites[0];

    id = vdu_patch_crop_impl( id, sprite->left, 0, sprite->width, 0, scale_sprite_data[0], 0,0,0, tile, zoom );

 finish:

    return id;
}

void main() {

    printf( "Start\n" );

    SpriteData sprite = { 1,2,3,4,5,6};
    sprite_state_show( 0, &sprite, 7, 8 );
    printf( "End\n" );

}

#ifdef __SDCC
__sfr __at 0xff sif;
int putchar( int c ) {

    sif = 'p';
    sif = c;

    return c;
}
#endif
$ sdcc -mz80 --allow-undocumented-instructions --fverbose-asm ./static_local.c  -o static_local.ihx && ucsim_z80 -I if=outputs[0xff] static_local.ihx
....
0> Loading from static_local.ihx
3369 words read from static_local.ihx
r
Start
32770
End

$ sdcc -mz80 --fverbose-asm ./static_local.c  -o static_local.ihx && ucsim_z80 -I if=outputs[0xff] static_local.ihx
....
0> Loading from static_local.ihx
3369 words read from static_local.ihx
r
Start
8
End
$ sdcc -v
SDCC : z80/sm83/z80n/mos6502 TD- 4.2.14 #14012 (Linux)

Related

Wiki: NGI0-Entrust-SDCC

Discussion

  • Philipp Klaus Krause

    I can reproduce the issue on my Debian GNU/Linux testing on amd64 system, when using -mz80 --allow-undocumented or -mz80n, but not for -mez80_z80.

     
  • Philipp Klaus Krause

    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
     
  • Philipp Klaus Krause

    The bug looks like in principle, it could also be exposed even without using undocumented instruction (though I didn't bother trying to create such a test case). It is fixed in [r14027].

     

    Related

    Commit: [r14027]


Log in to post a comment.

MongoDB Logo MongoDB