<p>On Nov 30, 2013 11:40 AM, "Mike" <<a href="mailto:none@none.com">none@none.com</a>> wrote:<br>
><br>
> I finally succeeded in doing what I set out to do:  Write a simple hello world program for an ARM Cortex-M processor using ONLY D.<br>
><br>
> /*************************<br>
> * The D Program (start.d)<br>
> *************************/<br>
> module start;<br>
><br>
> import ldc.llvmasm;<br>
><br>
><br>
> extern(C) __gshared void * _Dmodule_ref;<br>
><br>
> //Must be stored as second 32-bit word in .text section<br>
> immutable void function() ResetHandler = &OnReset;<br>
><br>
> void SendCommand(int command, void* message)<br>
> {<br>
>   __asm<br>
>   (<br>
>     "mov r0, $0;<br>
>      mov r1, $1;<br>
>      bkpt #0xAB",<br>
>      "r,r,~{r0},~{r1}",<br>
>      command, message<br>
>    );<br>
> }<br>
><br>
> void OnReset()<br>
> {<br>
>   while(true)<br>
>   {<br>
>     // Create semihosting message message<br>
>     uint[3] message =<br>
>       [<br>
>         2,                            //stderr<br>
>         cast(uint)"hello\r\n".ptr,    //ptr to string<br>
>         7                             //size of string<br>
>       ];<br>
><br>
>     //Send semihosting command<br>
>     SendCommand(0x05, &message);<br>
>   }<br>
> }<br>
><br>
> /*****************************<br>
> * The Linker Script (link.ld)<br>
> *****************************/<br>
><br>
> MEMORY<br>
> {<br>
>   CCRAM    (rxw) : ORIGIN = 0x10000000, LENGTH =   64k<br>
>   SRAM     (rxw) : ORIGIN = 0x20000000, LENGTH =  128k<br>
>   FLASH    (rx)  : ORIGIN = 0x08000000, LENGTH = 1024k<br>
> }<br>
><br>
> _stackStart = ORIGIN(CCRAM) + LENGTH(CCRAM);<br>
><br>
> SECTIONS<br>
> {<br>
>   .text :<br>
>   {<br>
>     LONG(_stackStart);              /* Initial stack pointer */<br>
>     KEEP(start.o(.<a href="http://data.rel.ro">data.rel.ro</a>))     /* Internet vector table */<br>
><br>
>     /* the code */<br>
>     *(.text)<br>
>     *(.text*)<br>
><br>
>     /* for "hello\r\n" string constant */<br>
>     . = ALIGN(4);<br>
>     *(.rodata)<br>
>     *(.rodata*)<br>
>   }>FLASH<br>
><br>
>   /* Need .data, .bss, .ctors and probably more as program becomes<br>
>      More complex */<br>
> }<br>
><br>
> Tools used:<br>
> Operating System: Arch Linux 64-bit<br>
> Compiler:  LDC (2063b4)<br>
> Linker & Binary Utilities & Debugger: GNU Tools for ARM Embedded Processors (<a href="https://launchpad.net/gcc-arm-embedded">https://launchpad.net/gcc-arm-embedded</a>)<br>
> JTAG Emulator: JTAG-lock-pick Tiny 2 w/ OpenOCD 0.7.0<br>
><br>
> To compile:<br>
> ldc2 -march=thumb -mcpu=cortex-m4 -noruntime -nodefaultlib -c start.d<br>
><br>
> To link:<br>
> arm-none-eabi-ld -T link.ld --gc-sections start.o -o start.elf<br>
><br>
> To execute:<br>
> openocd -f interface/jtag-lock-pick_tiny_2.cfg -f target/stm32f4x.cfg<br>
> arm-none-eabi-gdb start.elf<br>
><br>
> .. in GDB:<br>
> target remote localhost:3333<br>
> monitor arm semihosting enable<br>
> monitor reset halt<br>
> load<br>
> monitor reset init<br>
> continue<br>
><br>
> Output:<br>
> hello<br>
> hello<br>
> ...<br>
><br>
> Code Size: 148 bytes (not bad)<br>
><br>
> Why I think this is significant:<br>
> 1.  It shows D can write the most low level of programs and does not require an operating system<br>
> 2.  It shows that the D runtime and D standard library are not mandatory and do not need to be fully ported to one's platform to begin programming ARM Cortex-M bare metal hardware in D (although this is not the first to do so: <a href="https://bitbucket.org/timosi/minlibd">https://bitbucket.org/timosi/minlibd</a>)<br>

> 3.  It shows linking to C code and assembly files are optional<br>
> 4.  It shows the tools are capable (although they have not been well exercised in this example) and more specifically MY toolchain is working.<br>
> 5.  It's a great start to writing very embedded systems in D, or more importantly, not C/C++ (good riddance!)<br>
><br>
> What's next for me:<br>
> 1. Get a GDC toolchain working, although I'll probably switch to LDC when LDC matures.<br>
> 2. Learn more about D.<br>
> 3. Study minlibd and the d runtime and program the bare essentials to make D a productive language for the ARM Cortex-M.<br>
> 4. Help the D community help me, by testing the toolchains for the ARM Cortex-M platform and file bug reports.<br>
><br>
> Thanks to those who commented on my previous posts. I'm quite excited about this language.</p>
<p>In before the "that's not D! That's some D, a bit of some extended inline assembly, and a custom linker script."</p>
<p>Congrats though.  :)</p>
<p>Regards<br>
-- <br>
Iain Buclaw</p>
<p>*(p < e ? p++ : p) = (c & 0x0f) + '0';</p>