Initialisation for standard print routines?

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
User avatar
Jamie
Experienced Member
Posts: 99
Joined: Mon Dec 14, 2009 1:01 pm
Contact:

Initialisation for standard print routines?

Post by Jamie »

I'm trying to get printing working with custom startup code and I'm compiling with -nostartfiles to exclude the default startup. A print call, such as printstr(..) seems to execute okay (i.e. matches the trace of the same print in a normally compiled program) but produces no output. I'm wondering whether I'm missing some initialisation step to setup printing? I'm currently just trying this out with xsim.

Any help appreciated!
Jamie


richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

All the printing routines end up calling _write() (defined in libsyscall.a which is linked in by default). I would check that calling _write() directly works (there is a prototype in syscall.h). Do other system calls such as _exit() work?

If you want to replace any of the functions defined in libsyscall and still have them work with xgdb and xsim then you need to conform to the system call interface described in the Tools development guide.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

Also how are you loading code? The simulator gets the system call address for each core from the address of the _DoSyscall symbol in the ELF files contained in the .xe. It would cause problems if the _DoSyscall code wasn't located at this address after loading.
User avatar
Jamie
Experienced Member
Posts: 99
Joined: Mon Dec 14, 2009 1:01 pm
Contact:

Post by Jamie »

The _DoSysCall label is present in the binary and doesn't move after loading. In the program that just calls _exit:

Code: Select all

#include <xs1.h>
#include <print.h>
#include <syscall.h>

#pragma unsafe arrays
void _main()
{
    _exit(1);
}
its linked right at the bottom of the executable code:

Code: Select all

Loadable 1 for node 0, core 0:

Disassembly of section section .text (size: 1576)
.................
<_exit>:
           0x00010618: 10 90:       add (2rus)      r1, r0, 0x0
           0x0001061a: 00 68:       ldc (ru6)       r0, 0x0
           0x0001061c: 00 f0 02 d0: bl (lu10)       0x2 <_DoSyscall>
           0x00010620: ed 07:       clre (0r)       
           0x00010622: ec 07:       waiteu (0r)     

<_DoSyscall>:
           0x00010624: d0 a6:       mkmsk (rus)     r0, 0x20
           0x00010626: c0 77:       retsp (u6)      0x0

Loadable 1 for node 0, core 1:

Disassembly of section section .text (size: 1474)
...............
And the execution trace looks like:

Code: Select all

0@0@0   A-.----0001057c (initMaster          +  0) : entsp   0x3 S[0x1fdf8] @2573
0@0@0   A-.----0001057e (initMaster          +  2) : stw     r4(0x0), sp[0x1] S[0x1fdf0] @2577
0@0@0   A-.----00010580 (initMaster          +  4) : stw     r5(0x0), sp[0x2] S[0x1fdf4] @2585
0@0@0   A-.----00010582 (initMaster          +  6) : getr    r5(0x1), 0x1 @2589
0@0@0   A-.----00010584 (initMaster          +  8) : ldap    r11(0x105d0), 0x24 @2593
0@0@0   A-.----00010588 (initMaster          +  c) : add     r0(0x105d0), r11(0x105d0), 0x0 @2597
0@0@0   A-.----0001058a (initMaster          +  e) : setc    res[r5(0x1)], 0x1 @2601
0@0@0   A-.----0001058c (initMaster          + 10) : in      r4(0x28a), res[r5(0x1)] @2605
0@0@0   A-.----0001058e (initMaster          + 12) : bl      0x1b @2609
0@0@0   A-.----000105c8 (runMain             +  0) : entsp   0x1 S[0x1fdec] @2613
0@0@0   A-.----000105ca (runMain             +  2) : bla     r0(0x105d0) @2617
0@0@0   A-.----000105d0 (_main               +  0) : entsp   0x1 S[0x1fde8] @2621
0@0@0   A-.----000105d2 (_main               +  2) : mkmsk   r0(0x1), 0x1 @2625
0@0@0   A-.----000105d4 (_main               +  4) : bl      0x20 @2629
0@0@0   A-.----00010618 (_exit               +  0) : add     r1(0x1), r0(0x1), 0x0 @2633
0@0@0   A-.----0001061a (_exit               +  2) : ldc     r0(0x0), 0x0 @2637
0@0@0   A-.----0001061c (_exit               +  4) : bl      0x2 @2641
0@0@0   A-.----00010624 (_DoSyscall          +  0) : mkmsk   r0(0xffffffff), 0x20 @2645
0@0@0   A-.----00010626 (_DoSyscall          +  2) : retsp   0x0 L[0x0] @2649
0@0@0   A-.----00010620 (_exit               +  8) : clre     @2657
0@0@0 P A-.----00010622 (_exit               +  a) : waiteu   @2661
Which seems like it's working fine. On the other hand, for the program:

Code: Select all

#include <xs1.h>
#include <print.h>
#include <syscall.h>

#pragma unsafe arrays
void _main()
{
    printstr("hello world");
}
The binary has the right bits linked (in the same position as the last example):

Code: Select all

<safestrlen>:
           0x00010650: 80 68:       ldc (ru6)       r2, 0x0
           0x00010652: 39 c8:       lsu (3r)        r3, r2, r1
           0x00010654: e3 4f:       ecallf (1r)     r3
           0x00010656: 32 88:       ld8u (3r)       r3, r0[r2]
           0x00010658: c2 70:       bt (ru6)        r3, 0x2 <.bt44>
.bt45      0x0001065a: 08 90:       add (2rus)      r0, r2, 0x0
           0x0001065c: c0 77:       retsp (u6)      0x0
.bt44      0x0001065e: 29 90:       add (2rus)      r2, r2, 0x1
           0x00010660: 39 c8:       lsu (3r)        r3, r2, r1
           0x00010662: e3 4f:       ecallf (1r)     r3
           0x00010664: 32 88:       ld8u (3r)       r3, r0[r2]
           0x00010666: c5 74:       bt (ru6)        r3, -0x5 <.bt44>
           0x00010668: 08 77:       bu (u6)         -0x8 <.bt45>
           0x0001066a: 00 90:       add (2rus)      r0, r0, 0x0

<_DoSyscallErrno>:
           0x0001066c: 42 77:       entsp (u6)      0x2
           0x0001066e: 00 f0 0c d0: bl (lu10)       0xc <_DoSyscall>
           0x00010672: c4 8e:       not (2r)        r1, r0
           0x00010674: 57 aa:       shr (2rus)      r1, r1, 0x7
           0x00010676: 41 78:       bf (ru6)        r1, 0x1 <.bt46>
           0x00010678: c2 77:       retsp (u6)      0x2
.bt46      0x0001067a: c4 96:       neg (2r)        r1, r0
           0x0001067c: 41 54:       stw (ru6)       r1, sp[0x1]
           0x0001067e: 00 f0 06 d0: bl (lu10)       0x6 <__errno>
           0x00010682: 41 5c:       ldw (ru6)       r1, sp[0x1]
           0x00010684: 10 00:       stw (2rus)      r1, r0[0x0]
           0x00010686: d0 a6:       mkmsk (rus)     r0, 0x20
           0x00010688: c2 77:       retsp (u6)      0x2

<_DoSyscall>:
           0x0001068a: d0 a6:       mkmsk (rus)     r0, 0x20
           0x0001068c: c0 77:       retsp (u6)      0x0

<__errno>:
           0x0001068e: ee 17:       get (0r)        r11, id
           0x00010690: 00 f0 28 60: ldaw (lru6)     r0, dp[0x28]
           0x00010694: 83 fc ec 1f: ldaw (l3r)      r0, r0[r11]
           0x00010698: c0 77:       retsp (u6)      0x0
And seems to execute correctly:

Code: Select all

0@0@0   A-.----0001058c (initMaster          + 10) : in      r4(0x28a), res[r5(0x1)] @2605
0@0@0   A-.----0001058e (initMaster          + 12) : bl      0x1b @2609
0@0@0   A-.----000105c8 (runMain             +  0) : entsp   0x1 S[0x1fdec] @2613
0@0@0   A-.----000105ca (runMain             +  2) : bla     r0(0x105d0) @2617
0@0@0   A-.----000105d0 (_main               +  0) : entsp   0x1 S[0x1fde8] @2621
0@0@0   A-.----000105d2 (_main               +  2) : ldaw    r11(0x106a4), cp[0x2] @2629
0@0@0   A-.----000105d6 (_main               +  6) : add     r0(0x106a4), r11(0x106a4), 0x0 @2633
0@0@0   A-.----000105d8 (_main               +  8) : ldc     r1(0xc), 0xc @2637
0@0@0   A-.----000105da (_main               +  a) : bl      0x21 @2641
0@0@0   A-.----00010620 (printstr            +  0) : entsp   0x3 S[0x1fde4] @2645
0@0@0   A-.----00010622 (printstr            +  2) : stw     r4(0x28a), sp[0x1] S[0x1fddc] @2649
0@0@0   A-.----00010624 (printstr            +  4) : stw     r5(0x1), sp[0x2] S[0x1fde0] @2657
0@0@0   A-.----00010626 (printstr            +  6) : add     r4(0x106a4), r0(0x106a4), 0x0 @2661
0@0@0   A-.----00010628 (printstr            +  8) : add     r5(0xc), r1(0xc), 0x0 @2665
0@0@0   A-.----0001062a (printstr            +  a) : bl      0x11 @2669
0@0@0   A-.----00010650 (safestrlen          +  0) : ldc     r2(0x0), 0x0 @2673
0@0@0   A-.----00010652 (safestrlen          +  2) : lsu     r3(0x1), r2(0x0), r1(0xc) @2677
0@0@0   A-.----00010654 (safestrlen          +  4) : ecallf  r3(0x1) @2681
0@0@0   A-.----00010656 (safestrlen          +  6) : ld8u    r3(0x68), r0(0x106a4)[r2(0x0)] L[0x106a4] @2685
0@0@0   A-.----00010658 (safestrlen          +  8) : bt      r3(0x68), 0x2 @2689
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0x1), r2(0x0), 0x1 @2693
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0x1), r1(0xc) @2697
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2701
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x65), r0(0x106a4)[r2(0x1)] L[0x106a5] @2705
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x65), -0x5 @2709
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0x2), r2(0x1), 0x1 @2713
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0x2), r1(0xc) @2717
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2721
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x6c), r0(0x106a4)[r2(0x2)] L[0x106a6] @2725
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x6c), -0x5 @2729
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0x3), r2(0x2), 0x1 @2733
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0x3), r1(0xc) @2737
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2741
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x6c), r0(0x106a4)[r2(0x3)] L[0x106a7] @2745
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x6c), -0x5 @2749
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0x4), r2(0x3), 0x1 @2753
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0x4), r1(0xc) @2757
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2761
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x6f), r0(0x106a4)[r2(0x4)] L[0x106a8] @2765
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x6f), -0x5 @2769
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0x5), r2(0x4), 0x1 @2773
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0x5), r1(0xc) @2777
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2781
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x20), r0(0x106a4)[r2(0x5)] L[0x106a9] @2785
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x20), -0x5 @2789
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0x6), r2(0x5), 0x1 @2793
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0x6), r1(0xc) @2797
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2801
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x77), r0(0x106a4)[r2(0x6)] L[0x106aa] @2805
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x77), -0x5 @2809
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0x7), r2(0x6), 0x1 @2813
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0x7), r1(0xc) @2817
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2821
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x6f), r0(0x106a4)[r2(0x7)] L[0x106ab] @2825
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x6f), -0x5 @2829
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0x8), r2(0x7), 0x1 @2833
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0x8), r1(0xc) @2837
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2841
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x72), r0(0x106a4)[r2(0x8)] L[0x106ac] @2845
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x72), -0x5 @2849
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0x9), r2(0x8), 0x1 @2853
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0x9), r1(0xc) @2857
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2861
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x6c), r0(0x106a4)[r2(0x9)] L[0x106ad] @2865
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x6c), -0x5 @2869
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0xa), r2(0x9), 0x1 @2873
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0xa), r1(0xc) @2877
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2881
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x64), r0(0x106a4)[r2(0xa)] L[0x106ae] @2885
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x64), -0x5 @2889
0@0@0   A-.----0001065e (safestrlen          +  e) : add     r2(0xb), r2(0xa), 0x1 @2893
0@0@0   A-.----00010660 (safestrlen          + 10) : lsu     r3(0x1), r2(0xb), r1(0xc) @2897
0@0@0   A-.----00010662 (safestrlen          + 12) : ecallf  r3(0x1) @2901
0@0@0   A-.----00010664 (safestrlen          + 14) : ld8u    r3(0x0), r0(0x106a4)[r2(0xb)] L[0x106af] @2905
0@0@0   A-.----00010666 (safestrlen          + 16) : bt      r3(0x0), -0x5 @2909
0@0@0   A-.----00010668 (safestrlen          + 18) : bu      -0x8 @2913
0@0@0   A-.----0001065a (safestrlen          +  a) : add     r0(0xb), r2(0xb), 0x0 @2917
0@0@0   A-.----0001065c (safestrlen          +  c) : retsp   0x0 L[0x0] @2921
0@0@0   A-.----0001062e (printstr            +  e) : add     r2(0xb), r0(0xb), 0x0 @2929
0@0@0   A-.----00010630 (printstr            + 10) : add     r1(0x106a4), r4(0x106a4), 0x0 @2933
0@0@0   A-.----00010632 (printstr            + 12) : mkmsk   r0(0x1), 0x1 @2937
0@0@0   A-.----00010634 (printstr            + 14) : add     r3(0xc), r5(0xc), 0x0 @2941
0@0@0   A-.----00010636 (printstr            + 16) : bl      0x3 @2945
0@0@0   A-.----00010640 (_write              +  0) : add     r3(0xb), r2(0xb), 0x0 @2949
0@0@0   A-.----00010642 (_write              +  2) : add     r2(0x106a4), r1(0x106a4), 0x0 @2953
0@0@0   A-.----00010644 (_write              +  4) : add     r1(0x1), r0(0x1), 0x0 @2957
0@0@0   A-.----00010646 (_write              +  6) : ldc     r0(0x6), 0x6 @2961
0@0@0   A-.----00010648 (_write              +  8) : bu      0x10 @2965
0@0@0   A-.----0001066c (_DoSyscallErrno     +  0) : entsp   0x2 S[0x1fdd8] @2969
0@0@0   A-.----0001066e (_DoSyscallErrno     +  2) : bl      0xc @2977
0@0@0   A-.----0001068a (_DoSyscall          +  0) : mkmsk   r0(0xffffffff), 0x20 @2981
0@0@0   A-.----0001068c (_DoSyscall          +  2) : retsp   0x0 L[0x0] @2985
0@0@0   A-.----00010672 (_DoSyscallErrno     +  6) : not     r1(0x0), r0(0xffffffff) @2993
0@0@0   A-.----00010674 (_DoSyscallErrno     +  8) : shr     r1(0x0), r1(0x0), 0x7 @2997
0@0@0   A-.----00010676 (_DoSyscallErrno     +  a) : bf      r1(0x0), 0x1 @3001
0@0@0   A-.----0001067a (_DoSyscallErrno     +  e) : neg     r1(0x1), r0(0xffffffff) @3005
0@0@0   A-.----0001067c (_DoSyscallErrno     + 10) : stw     r1(0x1), sp[0x1] S[0x1fdd4] @3009
0@0@0   A-.----0001067e (_DoSyscallErrno     + 12) : bl      0x6 @3017
0@0@0   A-.----0001068e (__errno             +  0) : get     r11(0x0), id @3021
0@0@0   A-.----00010690 (__errno             +  2) : ldaw    r0(0x10750), dp[0x28] @3025
0@0@0   A-.----00010694 (__errno             +  6) : ldaw    r0(0x10750), r0(0x10750)[r11(0x0)] @3029
0@0@0   A-.----00010698 (__errno             +  a) : retsp   0x0 L[0x0] @3033
0@0@0   A-.----00010682 (_DoSyscallErrno     + 16) : ldw     r1(0x1), sp[0x1] L[0x1fdd4] @3041
0@0@0   A-.----00010684 (_DoSyscallErrno     + 18) : stw     r1(0x1), r0(0x10750)[0x0] S[0x10750] @3049
0@0@0   A-.----00010686 (_DoSyscallErrno     + 1a) : mkmsk   r0(0xffffffff), 0x20 @3053
0@0@0   A-.----00010688 (_DoSyscallErrno     + 1c) : retsp   0x2 L[0x1fdd8] @3057
0@0@0   A-.----0001063a (printstr            + 1a) : ldw     r4(0x28a), sp[0x1] L[0x1fddc] @3065
0@0@0   A-.----0001063c (printstr            + 1c) : ldw     r5(0x1), sp[0x2] L[0x1fde0] @3073
0@0@0   A-.----0001063e (printstr            + 1e) : retsp   0x3 L[0x1fde4] @3077
0@0@0   A-.----000105de (_main               +  e) : retsp   0x1 L[0x1fde8] @3085
0@0@0   A-.----000105cc (runMain             +  4) : retsp   0x1 L[0x1fdec] @3093
0@0@0   A-.----00010592 (initMaster          + 16) : in      r0(0x306), res[r5(0x1)] @3101
0@0@0   A-.----00010594 (initMaster          + 18) : sub     r0(0x7c), r0(0x306), r4(0x28a) @3105
0@0@0   A-.----00010596 (initMaster          + 1a) : add     r11(0x7c), r0(0x7c), 0x0 @3109
0@0@0 P A-.----00010598 (initMaster          + 1c) : waiteu   @3113
But produces no output...
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

When you call _exit() I'd expect the simulator to exit immediately with the specified return code. The fact that it executes the clre and waiteu instructions suggests it isn't doing that. Are there any writes to address 0x10624 earlier in the trace?
User avatar
Jamie
Experienced Member
Posts: 99
Joined: Mon Dec 14, 2009 1:01 pm
Contact:

Post by Jamie »

Yes, in fact I had moved it explicitly - it was to fix a problem when before I wasn't using any system calls. All's working well now. Thanks for your help though Richard :)
Post Reply