main.c:
#include <stdint.h>
uint8_t overworld[1];
uint8_t rand(){
return 4;
}
void main(){
uint8_t try;
uint8_t visited = 1;
uint8_t tile = 0;
uint8_t direction;
uint8_t last_node = 0;
uint8_t last_distance = 0;// comment out
do{
uint8_t next_tile;
try = 0;
direction = 1<<(rand() & 0x3);
do{
next_tile = tile;
direction = direction & 0xF;
++try;
}while(try < 4);
if(try >= 4){
--visited;
++last_distance;// comment out
} else {
if(last_node != 0){
last_node = 0;
}
direction = direction & 0xF;
overworld[next_tile] |= direction;
++visited;
}
}while(visited);
}
compile.sh:
#!/bin/sh
CFLAGS="--debug --nolospre --no-peep --nogcse --nooverlay --noinduction --noloopreverse --noinvariant"
CC="${SDCCBIN}sdcc -mgbz80 --fsigned-char -c ${CFLAGS}"
LD="${SDCCBIN}sdldgb -y"
mkdir -p build
${CC} -o build/main.rel main.c
${LD} -nmjwxi build/main.ihx build/main.rel
$ sdcc -v
SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ez80_z80/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15 4.0.0 #11528 (Linux)
published under GNU General Public License (GPL)
the cdb file says
S:Lmain.main$direction$1_0$2({1}SC:U),R,0,0,[c]
But it’s actually mostly on stack and just gets loaded into c during the end and the discarded. This makes it hard to debug the code, since the shown value is wrong half of the time.
If the lines ending with // comment out are removed, it’s just placed in c.