Undefined instruction

  • Thread starter Thread starter elle-p
  • Start date Start date
  • Views Views 546
  • Replies Replies 6

elle-p

Member
Newcomer
Joined
Aug 28, 2025
Messages
13
Reaction score
2
Trophies
0
Age
20
XP
57
Country
United Kingdom
I managed to compile and run a good chunk of code, and am working on another bit in a new project to put together after it's done. It's a kind of random maze generator. It's not the best, but it compiles fine. But playing it in Desmume makes gives the error in the console:

THUMB9: Undefined instruction: 0x0000BACD PC=0x0000160
ARM9: Undefined instruction: 0x0000BACD PC=0x0000160

MelonDS also fails at the exact same point, this time with the error:

ARM9: prefetch abort (00000002)

The code involves a loop, and seemingly manages to complete one before erroring, so I have no idea what's going on. I'll post some of my code if that would help.
 
So I changed a bit of my code and now get the same error, but with a bunch of different values depending on what I change. I think what the error is is that it doesn't finish my code before it gets the VBlank interrupt. Is there any solution to this? A way to tell it to keep doing what it's doing and not update when not finished and just reshow the previous frame, especially since this is something that would only be done once during a transition, or am I in the completely wrong place? This is not making any sense to me.
 
So I changed a bit of my code and now get the same error, but with a bunch of different values depending on what I change. I think what the error is is that it doesn't finish my code before it gets the VBlank interrupt. Is there any solution to this? A way to tell it to keep doing what it's doing and not update when not finished and just reshow the previous frame, especially since this is something that would only be done once during a transition, or am I in the completely wrong place? This is not making any sense to me.
Does this happen on hardware or just emulators? Many emulators have issues with new libnds if jit is enabled.
 
  • Wow
Reactions: cearp
On real hardware, it gives the error 'Data abort'. With no console, it's hard to say where it crashes(there could be errors later in my code that I can't fix), just that it does.
 
This is most likely memory or stack corruption. Without code it is not possible to say more given the extremely limited information you provided.
 
Sorry, here's some of my code.

u8 Floor::setDirec(u8 y, u8 x, u8 direc) {
printf("set direc ");
if (direc == -1 or randomChance(direc_change, 100)) { direc = randomRange(0, 3); }
for (u8 i = 0; i < 4; i++) {
switch (direc) {
default:
if (y+1 <= FLOOR_HEIGHT and not floor[y+1][x].set) { return direc; }
case 1:
if (x+1 <= FLOOR_WIDTH and not floor[y][x+1].set) { return direc; }
case 2:
if (y-1 >= 0 and not floor[y-1][x].set) { return direc; }
case 3:
if (x-1 >= 0 and not floor[y][x-1].set) { return direc; }
}
direc += 1;
if (direc > 3) {
direc = 0;
}
}
return -1;
}
void Floor::generatePath(u8 y, u8 x, u8 direc) {
u8 length = randomRange(path_length[0], path_length[1]);
printf("length is %d ", length);
for (u8 i = 0; i <= length; i++) {
printf("start stuff ");
direc = setDirec(y, x, direc);
if (direc == -1) { break; } // no viable directions.....

floor[y][x].set = true;
set_cells[set_index][1] = x;
set_cells[set_index][0] = y;
set_index += 1;
switch (direc) {
default: y += 1; break;
case 1: x += 1; break;
case 2: y -= 1; break;
case 3: x -= 1; break;
}
printf("finished stuff ");
}
printf("finished path ");
// set the last as the stairs
floor[y][x].set = true;
set_cells[set_index][1] = x;
set_cells[set_index][0] = y;
set_index += 1;
floor[y][x].special = CELL_STAIRS;
floor[y][x].roat = direc;
floor[y][x].type = 6;
}

void Floor::generate() {
printf("generate ");
Cell normal = {CELL_NORMAL, false, 0, 0};
for (u8 i = 0; i < FLOOR_HEIGHT; i++) {
for (u8 j = 0; j < FLOOR_WIDTH; j++) {
floor[j] = normal;
}
}

printf("start to stairs ");
// set the start
u8 x = randomRange(0, FLOOR_WIDTH-1);
u8 y = randomRange(0, FLOOR_HEIGHT-1);
floor[y][x].special = CELL_ENTRANCE;
u8 direc = setDirec(y, x, -1);
floor[y][x].roat = direc;
floor[y][x].type = 5;
generatePath(y, x, direc);

if (randomChance(room_chance, 100)) {
printf("room ");
generateRoom();
}

u8 am = randomRange(leaf_amount[0], leaf_amount[1]);
for (u8 i = 0; i < am; i++) {
printf("leaf ");
generateLeaf(CELL_NORMAL);
}

for (u8 i = 0; i < set_index; i++) {
if (floor[set_cells[set_index][0]][set_cells[set_index][1]].special == CELL_NORMAL) {
setType(set_cells[set_index][0], set_cells[set_index][1]);
}
}
}

There's a bit more, but I'm either sure it works(due to using it in other programs too) or it never does it. It manages to print "generate start to stairs set direc length is 4 start stuff set direc finished stuff start stuff set dire" of this before getting the undefined instruction.
 
Sorry, here's some of my code.

u8 Floor::setDirec(u8 y, u8 x, u8 direc) {
printf("set direc ");
if (direc == -1 or randomChance(direc_change, 100)) { direc = randomRange(0, 3); }
for (u8 i = 0; i < 4; i++) {
switch (direc) {
default:
if (y+1 <= FLOOR_HEIGHT and not floor[y+1][x].set) { return direc; }
case 1:
if (x+1 <= FLOOR_WIDTH and not floor[y][x+1].set) { return direc; }
case 2:
if (y-1 >= 0 and not floor[y-1][x].set) { return direc; }
case 3:
if (x-1 >= 0 and not floor[y][x-1].set) { return direc; }
}
direc += 1;
if (direc > 3) {
direc = 0;
}
}
return -1;
}
void Floor::generatePath(u8 y, u8 x, u8 direc) {
u8 length = randomRange(path_length[0], path_length[1]);
printf("length is %d ", length);
for (u8 i = 0; i <= length; i++) {
printf("start stuff ");
direc = setDirec(y, x, direc);
if (direc == -1) { break; } // no viable directions.....

floor[y][x].set = true;
set_cells[set_index][1] = x;
set_cells[set_index][0] = y;
set_index += 1;
switch (direc) {
default: y += 1; break;
case 1: x += 1; break;
case 2: y -= 1; break;
case 3: x -= 1; break;
}
printf("finished stuff ");
}
printf("finished path ");
// set the last as the stairs
floor[y][x].set = true;
set_cells[set_index][1] = x;
set_cells[set_index][0] = y;
set_index += 1;
floor[y][x].special = CELL_STAIRS;
floor[y][x].roat = direc;
floor[y][x].type = 6;
}

void Floor::generate() {
printf("generate ");
Cell normal = {CELL_NORMAL, false, 0, 0};
for (u8 i = 0; i < FLOOR_HEIGHT; i++) {
for (u8 j = 0; j < FLOOR_WIDTH; j++) {
floor[j] = normal;
}
}

printf("start to stairs ");
// set the start
u8 x = randomRange(0, FLOOR_WIDTH-1);
u8 y = randomRange(0, FLOOR_HEIGHT-1);
floor[y][x].special = CELL_ENTRANCE;
u8 direc = setDirec(y, x, -1);
floor[y][x].roat = direc;
floor[y][x].type = 5;
generatePath(y, x, direc);

if (randomChance(room_chance, 100)) {
printf("room ");
generateRoom();
}

u8 am = randomRange(leaf_amount[0], leaf_amount[1]);
for (u8 i = 0; i < am; i++) {
printf("leaf ");
generateLeaf(CELL_NORMAL);
}

for (u8 i = 0; i < set_index; i++) {
if (floor[set_cells[set_index][0]][set_cells[set_index][1]].special == CELL_NORMAL) {
setType(set_cells[set_index][0], set_cells[set_index][1]);
}
}
}

There's a bit more, but I'm either sure it works(due to using it in other programs too) or it never does it. It manages to print "generate start to stairs set direc length is 4 start stuff set direc finished stuff start stuff set dire" of this before getting the undefined instruction.
I don’t see anything obvious in what you provided - but I can’t say I spent a lot of time looking at it - since what you provided is not really sufficient to the task. However, keep in mind the ds has a very small default stack size. I think it is 16k by default. The second thing would be a suggestion - when I get stuck with random crashes only on the ds - I create a “wait” function that prints a string and waits for a key press. Then a put that all through the code. It is brute force - but sometimes that is the only way to move forward. Also make sure to have the function wait for the button to be released too - otherwise it will just release a bunch of waits at once. Also use defaultExceptionHandler. This put a stack dump on the lower screen. The current instruction may not be helpful but you can usually see some addresses in the stack that you can trace with addr2line.
 
  • Like
Reactions: cearp

Site & Scene News

Popular threads in this forum