Menu

#4008 loop addition adds incorrectly

closed-fixed
None
other
5
2026-06-14
2026-06-10
Under4Mhz
No

The following code returns the incorrect value for 'tile'

Seems to need the row check at the start and the pointer parameters to fail.

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

void vdu_tiles_put( uint16_t tile, const uint8_t *pixels, const uint8_t *colours ) {

    printf( "tile: %d\n", tile );
}

///< Load pixel data for patch
void vdu_patch_load_impl( int8_t row, const uint8_t *pixels, const uint8_t *colours, uint8_t rows, uint16_t tile_offset ) {

    if ( row >= 24 )
        return;

    uint16_t segment_first = 0;
    uint16_t segments = 3;
    uint16_t tile = tile_offset;

    for(uint8_t segment = 0; segment < segments; segment++ ) {

        vdu_tiles_put( tile, pixels, colours );
        tile += 256;
    }
}

void main() {

    printf( "Start\n" );

    vdu_patch_load_impl( 3, 0, 0, 0, 15 );

    printf( "End\n" );
}

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

    sif = 'p';
    sif = c;

    return c;
}
#endif
$ sdcc -mz80 --max-allocs-per-node 10000 --fverbose-asm ./loop_fail.c  -o loop_fail.ihx && ucsim_z80 -I if=outputs[0xff] loop_fail.ihx
0> Loading from loop_fail.ihx
3098 words read from loop_fail.ihx
r
Simulation started, PC=0x000000
Start
tile: 15
tile: 783
tile: 783
End

$ gcc ./loop_fail.c && ./a.out
Start
tile: 15
tile: 271
tile: 527
End
$ sdcc -v
SDCC : z80/sm83/ez80/z80n/mos6502/mos65c02 TD- 4.6.0 #16591 (Linux)

Related

Bugs: #4009

Discussion

  • Under4Mhz

    Under4Mhz - 2026-06-10

    Attached asm file

     

    Last edit: Under4Mhz 2026-06-10
  • Philipp Klaus Krause

    I can reproduce the issue on my Debian GNU/Linux testing system, using the next branch built as TD- version.

     
  • Philipp Klaus Krause

    Fixed in the next branch in [r16605].

     

    Related

    Commit: [r16605]

  • Philipp Klaus Krause

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

    • status: pending-fixed --> closed-fixed
    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    The next branch has been merged to trunk, and the fix is in SDCC 4.6.0 RC2.

     

Log in to post a comment.

MongoDB Logo MongoDB