dciabrin / ngdevkit

Open source development for Neo-Geo
GNU Lesser General Public License v3.0
272 stars 26 forks source link

Address error #104

Closed city41 closed 10 months ago

city41 commented 10 months ago

I am getting an address error in my game (unibios used to get a small clue on the exception)

image

This only happens when I include some debug stuff into my game which takes up a lot more ram. I am actively trying to locate the cause of this as we speak.

One thing I noticed in ngdevkit.ld is possibly the BIOSRAM section is incorrect?

MEMORY {
  /* Regular program address space for the m68k */
  ROM1 (rx)    : ORIGIN = 0x000000, LENGTH = 1024K
  /* Additional program address space, bank-switched */
  ROM2 (rx)    : ORIGIN = 0x200000, LENGTH = 1024K
  /* BRAM defaults to 0x100000, unless overriden in the cartridge */
  BRAM (rw)    : ORIGIN = rom_backup_data_address, LENGTH = 0x1000
  /* RAM address space for the m68k */
  /* Note: 10f300 is the default supervisor's stack pointer, and
   * stack grows downwards, so do not fill RAM up to 10f300 */
  RAM  (rw)    : ORIGIN = 0x100000, LENGTH = 0xf300
  /* The end of the RAM is reserved for BIOS usage (786 bytes) */
  BIOSRAM (rw) : ORIGIN = 0x10fcee, LENGTH = 0x312
}

This section notes that the bios's ram is 786 bytes in length and starts 10fcee, but in the wiki it says it starts at 10F300 and is 3,328 bytes long?

I wonder if my game when the debug stuff is added, ends up using ram that should be reserved for the bios?

Anyway, I'll keep plugging away at this. I'll try to create a small repro as well.

btw this repros in Mame, on the mister, and on a real neo geo, but not in gngeo. I think using gngeo is dangerous, to be honest.

city41 commented 10 months ago

Doh, this was me. Trying to be too clever.

I have a very primitive "malloc", like this

#define SIZE 8000

u8 _mem[SIZE];

void* next = _mem;

void mem_reset() {
    for (u16 i = 0; i < SIZE; ++i) {
        _mem[i] = 0;
    }

    next = _mem;
}

void* mem_alloc(u16 size) {
    void* cur = next;

    next += size;

    return cur;
}

Which has happily worked for a very long time. But when I include the debug stuff into my game (it's a "terminal" on the fix layer, kinda like the quake terminal), I got that address error left and right.

To fix it, all I did was change u8 _mem[SIZE] to u16 _mem[SIZE]. Since an address error is described as "Misaligned (odd) word or longword memory access.", I thought maybe handing out pointers to odd bytes is bad. Sure enough, it is.

Welp, there's a day down the drain :)

edit: Oh and sure enough, in the unibios handler, 100fad is an odd address...