Probably just me derping and echoing nothing into the file.

Whole set of commands with the fixes we've covered to this point:
Code:
$ cat > stub.S << EOF
.global main
main:
    rts
EOF
$ sh3eb-elf-gcc -c -o stub.o stub.S
$ sh3eb-elf-ld -o stub.bin -nostdlib -Lpath/to/libfxcg.a -lfxcg -Tpath/to/prizm.x -static -gc-sections
$ mkg3a stub.bin
I do have that at the top of the program. Here's the entire code:

Code:
.global main
main:
    rts

I did however get it to assemble like this:
Quote:
sh3eb-elf-ld -o stub.bin -nostdlib -L/home/crush/Programmation/SuperH/PrizmSDK/lib -lfxcg -T../../toolchain/prizm.x -static -gc-sections -shared -fleading-underscore
I don't think it worked though as i just got my first crash when running it oncalc Wink
Using -shared at link-time is probably doing something entirely wrong. You should only have -static in there.
I have no idea what it does, to use "-fleading-underscore" it told me i needed it in there.
Just change 'main' to '_main' in your code (and remove -fleading-underscore and -shared from the arguments) and all will be well. Whatever you got -fleading-underscore from, it was some sort of strange shared-library workaround.
I already tried that and it still gives me the same error :/
Tari wrote:
Probably just me derping and echoing nothing into the file.

Whole set of commands with the fixes we've covered to this point:
Code:
$ cat > stub.S << EOF
.global main
main:
    rts
EOF
$ sh3eb-elf-gcc -c -o stub.o stub.S
$ sh3eb-elf-ld -o stub.bin -nostdlib -Lpath/to/libfxcg.a -lfxcg -Tpath/to/prizm.x -static -gc-sections stub.o --entry=main
$ mkg3a stub.bin


Above just tested
Great, it assembled without errors this time and i got my .g3a file (and 2-byte .bin). Running it oncalc crashed, though. Can you not return directly from a program?

Thank you so much for all your help!

EDIT: An infinite loop also crashed.
You can return from a program, but when you do, the program can't run again until you run a different add-in first. Can you verify that the program starts before crashing, perhaps? Dump some bytes out the serial port or something?
iirc, you need to call a syscall and possibly other stuff before running your code. Look at the crt0.s file for the startup procedure.
I think it's not even running, but i don't know how to test for sure. I've tried adding in the crt0.S code (essentially just changing the "main: \ .long _main" to the address of the start of my program "main: \ .long start") but it still crashes on start. I'm not sure what the C program adds at the start. I'm not exactly sure what crt0.S is doing, it looks like it's copying the program into RAM? I don't know what the .data section is (where it's supposed to point to), it seems it's defined in prizm.x. Then runs a syscall (_GlibAddinAplExecutionCheck) and jumps to the program.

And what is the delay slot? After syscalls the compiler seems to put a nop after it.
Yes, that's important. In SH4 assembly, jumps and rets take place one instruction late, so the instruction after a jump and the instruction after a ret always occur before the jump or ret. It's important to put a nop (or an opcode that finishes your code) in that slot to avoid unexpected behavior.
Ah, that's interesting. Thanks for the information. I still can't seem to get the binary to run on calc, however :/
do you call that one syscall on startup?
Here's the code i'm trying to use:

Code:
! C runtime initialization for Prizm addins
! By Tari and calc84maniac, based on Kristaba's reverse-engineered crt0.
    .global main
    .section ".pretext"
    .align 2

main:
! Preserve things on the stack
    mov.l r14, @-r15    ! Frame pointer
    sts.l pr, @-r15     ! Return address
    mov.l r4, @-r15     ! Parameter 1
   
! Copy .data section into RAM
    mov.l v_datald, r0      ! From
    mov.l v_sdata, r2       ! To
    mov.l v_edata, r3       ! Limit
dataLoop:
    cmp/hs r3, r2
    bt dataDone             ! Stop when r2 >= r3
    mov.l @r0+, r1
    mov.l r1, @r2
    bra dataLoop
    add #4, r2              ! Delay slot
dataDone:
 
! Zero out .bss
    mov.l v_ram_ebss, r2    ! To
    mov.l v_ram_bbss, r3    ! Limit
    mov #0, r1              ! Constant
bssLoop:
    cmp/hi r3, r2
    bf bssDone              ! Stop when r2 <= r3
    nop
    bra bssLoop
    mov.b r1, @-r2          ! Delay slot
bssDone:
 
! RAM is now initialized
 
    mov r5, r14             ! Save parameter 2
    mov #1, r6
    mov #0, r4
    bsr _GlibAddinAplExecutionCheck
    mov r6, r5
   
! main(r4, r5) with same state as input (returns to our caller)
   
    mov.l main_loc, r7
    extu.w r14, r5
    mov.l @r15+, r5
    lds.l @r15+, pr
    jmp @r7
    mov.l @r15+, r14        ! Delay slot
   
_GlibAddinAplExecutionCheck:
    mov.l v_syscall, r2
    jmp @r2                 ! _GlibAddinAplExecutionCheck
    mov #0x29, r0           ! Delay slot
   
! Constants
    .align 4
main_loc:
    .long _main
v_syscall:
    .long 0x80020070
v_datald:
    .long _datald
v_edata:
    .long _edata
v_sdata:
    .long _sdata
v_ram_bbss:
    .long _bbss
v_ram_ebss:
    .long _ebss

_main:
   mov.l BDISP,r9         !D904
   add   #-4,r15            !7FFC
   mov.l GETKEY,r8         !D804
loop:
   jsr   @r9               !490B
   nop                  !0009
   jsr   @r8               !480B
   mov   r15,r4            !64F3
   bra   loop            !AFFA
   nop                  !0009
   .align 2

BDISP:
   .long   300070      !_Bdisp_AllClr_VRAM
GETKEY:
   .long   300080      !_GetKey

The syscall i assume is " _GlibAddinAplExecutionCheck"? When i run it i get a system error: TLB ERROR, saying the PC is at 00300000 (that's where programs are run from, right?). That should be the header, is there something wrong with my header? It says TARGET = 00049426, i don't know what that means.
Sorry for the double post, i just wanted to say i got it assembling with the makefile, but it's still giving me the TLB error at 00300000. I can't for the life of me figure out what's wrong, as the C programs all run fine (and a C program of nothing but asm()'s also runs fine). Maybe i'll just write a converter to change every line of my asm code into an asm() command Razz
chickendude wrote:
Sorry for the double post, i just wanted to say i got it assembling with the makefile, but it's still giving me the TLB error at 00300000. I can't for the life of me figure out what's wrong, as the C programs all run fine (and a C program of nothing but asm()'s also runs fine). Maybe i'll just write a converter to change every line of my asm code into an asm() command Razz
Can you use something like XVI32 to compare the two binaries and see wherein the difference(s) lie?
Can you verify that the first op in your asm file is what the first op is in the g3a?
I've gone through the HEX and my program does start at +$7000 (the opcodes match). The files are exactly the same size, but diff says the two files are different.

EDIT: So i got the infinite loop to compile, however it required that i add an rts \ mov #00,r0 to the end, for some reason. Now my next program (getkey loop) is also crashing (or rather, failing to run) Crying But it's late here so that'll be another mystery for another day.
I suspect these issues may be due to failing to tear down the stack frame that crt0 creates. Need to investigate by seeing what the generated code for a stub C addin does.

When in doubt, build something in C and examine the code it generates to see what's necessary.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 3
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement