Why is my ROM writable code not working?
Posted: Wed Jul 27, 2022 6:17 pm
I have this piece of code which checks to see if the Spectrum's ROM is writable (which on an emulator it might be):
So peek the byte at 0000, poke it with the inversion, then check whether it has changed. If not, ROM isn't writable. Otherwise, put it back and return 1.
But it doesn't work. Looking at the code generated:
I can't work that out. But it always returns 1 AFAICT.
This is with 2.2 and SDCC:
Code: Select all
uint8_t is_rom_writable(void)
{
uint8_t byte0 = z80_bpeek(0);
z80_bpoke( 0, ~byte0 );
if( z80_bpeek(0) == byte0 )
return 0;
z80_bpoke( 0, byte0 );
return 1;
}
But it doesn't work. Looking at the code generated:
Code: Select all
498 ; ---------------------------------
499 ; Function is_rom_writable
500 ; ---------------------------------
501 _is_rom_writable:
502 ;tracetable.c:58: uint8_t byte0 = z80_bpeek(0);
503 00004e 210000 ld hl,0x0000
504 000051 4e ld c, (hl)
505 ;tracetable.c:60: z80_bpoke( 0, ~byte0 );
506 000052 79 ld a, c
507 000053 2f cpl
508 ;tracetable.c:61: if( z80_bpeek(0) == byte0 )
509 000054 320000 ld (0x0000),a
510 ;tracetable.c:62: return 0;
511 000057 91 sub a,c
512 000058 2003 jr NZ,l_is_rom_writable_00102
513 00005a 6f ld l,a
514 00005b 1806 jr l_is_rom_writable_00103
515 l_is_rom_writable_00102:
516 ;tracetable.c:64: z80_bpoke( 0, byte0 );
517 00005d 210000 ld hl,0x0000
518 000060 71 ld (hl), c
519 ;tracetable.c:66: return 1;
520 000061 2e01 ld l,0x01
521 l_is_rom_writable_00103:
522 ;tracetable.c:67: }
523 000063 c9 ret
This is with 2.2 and SDCC:
Code: Select all
>z88dk-zsdcc -v
ZSDCC IS A MODIFICATION OF SDCC FOR Z88DK
Build: 4.2.0 #13081 (Linux) Jul 24 2022