llvm-mos / llvm-mos-sdk

SDK for developing with the llvm-mos compiler
https://www.llvm-mos.org
Other
281 stars 56 forks source link

PCE mkcd relocations don't seem to work. #327

Closed est77 closed 8 months ago

est77 commented 8 months ago

Is there any example PCE CD project?

I am having a hard time trying to get the pce-mkcd relocations to work correctly. I tried different things but I usually end up with some code like this in elf files:

0180407b <_GLOBAL__sub_I_main.cpp>: 180407b: ae 00 00 ldx $0 ; 0x1800000 <__heap_start+0x87dd30> 180407e: 8e 00 40 stx $4000 ; 0x1804000 1804081: 60 rts

And after running mkcd, I get this:

=> Relocating "cd_error_elfsector_count" @ 0180407C => 1

0180407b <_GLOBAL__sub_I_main.cpp>: 180407b: ae 01 00 ldx $100 180407e: 8e 00 40 stx $4000 1804081: 60 rts

And the symbol is initialized with the contents of some random hardware register.

Thanks.

asiekierka commented 8 months ago

Could you provide a sample project/sample code? I will admit wholly that the PC Engine CD relocator is only loosely tested.

est77 commented 8 months ago

My current code is quite messy, since I am experimenting still.

I first tried this:

extern uint8_t cd_error_elfsector_count;

and not defining the variable anywhere. This maps the variable to address 0x0 and access is done using lda $0000. When pce-mkcd relocs the symbol the $0000 address is replaced with the value of the symbol (not its address). The code ends up reading some random PCE hardware register, usually returning 0. I tried other variants of this idea, but I couldn't make it work.

Then I tried declaring and defining the variable myself. In this case, the symbol reference does not end up in the elf file and pce-mkcd does not patch it.

Do you have a simple example of how to make it work?

asiekierka commented 8 months ago

Then I tried declaring and defining the variable myself. In this case, the symbol reference does not end up in the elf file and pce-mkcd does not patch it.

Correct; it's not an unresolved external symbol, so it's not preserved for relocation with pce-mkcd.

extern uint8_t __cd_error_elf__sector_count;

This is the correct semantics, and yes, you're supposed to treat &__cd_error_elf__sector_count as the actual variable. So referencing __cd_error_elf__sector_count will point to an invalid address, while doing something like (uint8_t) &__cd_error_elf__sector_count will return the value you seek.

est77 commented 8 months ago

That worked. I missed the address of part.

Thanks!