Merge pull request #160 from antonblanchard/tomtor-rust-2
Rebase Tom's rust demo patchespull/162/head
						commit
						afbb99cfd4
					
				@ -0,0 +1,2 @@
 | 
			
		||||
[build]
 | 
			
		||||
target = "powerpc64le-unknown-linux-gnu"
 | 
			
		||||
@ -0,0 +1,28 @@
 | 
			
		||||
[package]
 | 
			
		||||
name = "microwatt"
 | 
			
		||||
version = "0.1.4"
 | 
			
		||||
authors = ["Tom Vijlbrief <tvijlbrief@gmail.com>"]
 | 
			
		||||
edition = "2018"
 | 
			
		||||
description = "Running Rust on the microwatt"
 | 
			
		||||
license = "GPL-3.0"
 | 
			
		||||
 | 
			
		||||
[lib]
 | 
			
		||||
name = "mylib"
 | 
			
		||||
crate_type = ["staticlib"]
 | 
			
		||||
 | 
			
		||||
[profile.dev]
 | 
			
		||||
panic = "abort"
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
heapless = "0.5.4"
 | 
			
		||||
cty = "0.2"
 | 
			
		||||
#embedded-hal = "0.2.3"
 | 
			
		||||
#panic-halt = "0.2.0"
 | 
			
		||||
linked_list_allocator = "0.8.2"
 | 
			
		||||
 | 
			
		||||
[profile.release]
 | 
			
		||||
panic = "abort"
 | 
			
		||||
codegen-units = 1 # better optimizations
 | 
			
		||||
opt-level = 'z'  # Optimize for size.
 | 
			
		||||
debug = true # symbols are nice and they don't increase the size on Flash
 | 
			
		||||
lto = true # better optimizations
 | 
			
		||||
@ -0,0 +1,44 @@
 | 
			
		||||
ARCH = $(shell uname -m)
 | 
			
		||||
ifneq ("$(ARCH)", "ppc64")
 | 
			
		||||
ifneq ("$(ARCH)", "ppc64le")
 | 
			
		||||
	CROSS_COMPILE ?= powerpc64le-linux-
 | 
			
		||||
endif
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
CC = $(CROSS_COMPILE)gcc
 | 
			
		||||
LD = $(CROSS_COMPILE)ld
 | 
			
		||||
OBJCOPY = $(CROSS_COMPILE)objcopy
 | 
			
		||||
 | 
			
		||||
CFLAGS = -Os -g -Wall -std=c99 -msoft-float -mno-string -mno-multiple -mno-vsx -mno-altivec -mlittle-endian -fno-stack-protector -mstrict-align -ffreestanding -fdata-sections -ffunction-sections
 | 
			
		||||
ASFLAGS = $(CFLAGS)
 | 
			
		||||
LDFLAGS = -T powerpc.lds
 | 
			
		||||
 | 
			
		||||
RUSTLIB = target/powerpc64le-unknown-linux-gnu/release/libmylib.a
 | 
			
		||||
 | 
			
		||||
all: hello_world.hex
 | 
			
		||||
 | 
			
		||||
run:
 | 
			
		||||
	-ln -sf hello_world.bin main_ram.bin
 | 
			
		||||
	../core_tb > /dev/null
 | 
			
		||||
 | 
			
		||||
$(RUSTLIB): src/lib.rs
 | 
			
		||||
	RUSTFLAGS="-C target-feature=-vsx,-altivec,-hard-float" xargo build --release
 | 
			
		||||
 | 
			
		||||
size:
 | 
			
		||||
	size hello_world.elf
 | 
			
		||||
 | 
			
		||||
dump:
 | 
			
		||||
	powerpc64le-linux-gnu-objdump -S hello_world.elf | less
 | 
			
		||||
 | 
			
		||||
hello_world.elf: hello_world.o console.o head.o $(RUSTLIB)
 | 
			
		||||
	$(LD) $(LDFLAGS) -o $@ $^
 | 
			
		||||
 | 
			
		||||
hello_world.bin: hello_world.elf
 | 
			
		||||
	$(OBJCOPY) -O binary $^ $@
 | 
			
		||||
 | 
			
		||||
hello_world.hex: hello_world.bin
 | 
			
		||||
	../scripts/bin2hex.py $^ > $@
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	cargo clean
 | 
			
		||||
	@rm -f *.o hello_world.elf hello_world.bin hello_world.hex
 | 
			
		||||
@ -0,0 +1,33 @@
 | 
			
		||||
I made a minimal Rust demo.
 | 
			
		||||
 | 
			
		||||
This needs a rebuild of the `core` library with `xargo` (https://github.com/japaric/xargo) for working soft floating-point support.
 | 
			
		||||
 | 
			
		||||
Steps:
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
$ rustup default nightly
 | 
			
		||||
 | 
			
		||||
$ rustup target add powerpc64le-unknown-linux-gnu
 | 
			
		||||
```
 | 
			
		||||
$ rustup component add rust-src
 | 
			
		||||
 | 
			
		||||
$ cargo install xargo
 | 
			
		||||
 | 
			
		||||
$ make
 | 
			
		||||
$ make run
 | 
			
		||||
 | 
			
		||||
ln -sf hello_world.bin main_ram.bin
 | 
			
		||||
../core_tb > /dev/null
 | 
			
		||||
Hello World
 | 
			
		||||
Rust
 | 
			
		||||
i 2
 | 
			
		||||
5
 | 
			
		||||
5
 | 
			
		||||
i 3
 | 
			
		||||
3.3333333333333335
 | 
			
		||||
3.3333333333333335
 | 
			
		||||
[-9, -6, 2, 3]
 | 
			
		||||
!panicked at 'test', src/lib.rs:58:5
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,4 @@
 | 
			
		||||
[target.powerpc64le-unknown-linux-gnu.dependencies]
 | 
			
		||||
 | 
			
		||||
[dependencies.alloc]
 | 
			
		||||
features = ["compiler-builtins-mem"]
 | 
			
		||||
@ -0,0 +1,17 @@
 | 
			
		||||
#!/usr/bin/python3
 | 
			
		||||
 | 
			
		||||
import sys
 | 
			
		||||
import subprocess
 | 
			
		||||
import struct
 | 
			
		||||
 | 
			
		||||
with open(sys.argv[1], "rb") as f:
 | 
			
		||||
        while True:
 | 
			
		||||
            word = f.read(8)
 | 
			
		||||
            if len(word) == 8:
 | 
			
		||||
                print("%016x" % struct.unpack('Q', word));
 | 
			
		||||
            elif len(word) == 4:
 | 
			
		||||
                print("00000000%08x" % struct.unpack('I', word));
 | 
			
		||||
            elif len(word) == 0:
 | 
			
		||||
                exit(0);
 | 
			
		||||
            else:
 | 
			
		||||
                raise Exception("Bad length")
 | 
			
		||||
@ -0,0 +1 @@
 | 
			
		||||
../hello_world/console.c
 | 
			
		||||
@ -0,0 +1 @@
 | 
			
		||||
../hello_world/console.h
 | 
			
		||||
@ -0,0 +1,95 @@
 | 
			
		||||
/* Copyright 2013-2014 IBM Corp.
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 * 	http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 | 
			
		||||
 * implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#define STACK_TOP 0x1f000
 | 
			
		||||
 | 
			
		||||
#define FIXUP_ENDIAN						   \
 | 
			
		||||
	tdi   0,0,0x48;	  /* Reverse endian of b . + 8		*/ \
 | 
			
		||||
	b     191f;	  /* Skip trampoline if endian is good	*/ \
 | 
			
		||||
	.long 0xa600607d; /* mfmsr r11				*/ \
 | 
			
		||||
	.long 0x01006b69; /* xori r11,r11,1			*/ \
 | 
			
		||||
	.long 0x05009f42; /* bcl 20,31,$+4			*/ \
 | 
			
		||||
	.long 0xa602487d; /* mflr r10				*/ \
 | 
			
		||||
	.long 0x14004a39; /* addi r10,r10,20			*/ \
 | 
			
		||||
	.long 0xa64b5a7d; /* mthsrr0 r10			*/ \
 | 
			
		||||
	.long 0xa64b7b7d; /* mthsrr1 r11			*/ \
 | 
			
		||||
	.long 0x2402004c; /* hrfid				*/ \
 | 
			
		||||
191:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Load an immediate 64-bit value into a register */
 | 
			
		||||
#define LOAD_IMM64(r, e)			\
 | 
			
		||||
	lis     r,(e)@highest;			\
 | 
			
		||||
	ori     r,r,(e)@higher;			\
 | 
			
		||||
	rldicr  r,r, 32, 31;			\
 | 
			
		||||
	oris    r,r, (e)@h;			\
 | 
			
		||||
	ori     r,r, (e)@l;
 | 
			
		||||
 | 
			
		||||
	.section ".head","ax"
 | 
			
		||||
 | 
			
		||||
	. = 0
 | 
			
		||||
.global _start
 | 
			
		||||
_start:
 | 
			
		||||
	FIXUP_ENDIAN
 | 
			
		||||
	b	boot_entry
 | 
			
		||||
 | 
			
		||||
.global boot_entry
 | 
			
		||||
boot_entry:
 | 
			
		||||
	/* setup stack */
 | 
			
		||||
	LOAD_IMM64(%r1, STACK_TOP - 0x100)
 | 
			
		||||
	LOAD_IMM64(%r12, main)
 | 
			
		||||
	mtctr	%r12,
 | 
			
		||||
	bctrl
 | 
			
		||||
	b .
 | 
			
		||||
 | 
			
		||||
#define EXCEPTION(nr)		\
 | 
			
		||||
	.= nr			;\
 | 
			
		||||
	b	.
 | 
			
		||||
 | 
			
		||||
	/* More exception stubs */
 | 
			
		||||
	EXCEPTION(0x300)
 | 
			
		||||
	EXCEPTION(0x380)
 | 
			
		||||
	EXCEPTION(0x400)
 | 
			
		||||
	EXCEPTION(0x480)
 | 
			
		||||
	EXCEPTION(0x500)
 | 
			
		||||
	EXCEPTION(0x600)
 | 
			
		||||
	EXCEPTION(0x700)
 | 
			
		||||
	EXCEPTION(0x800)
 | 
			
		||||
	EXCEPTION(0x900)
 | 
			
		||||
	EXCEPTION(0x980)
 | 
			
		||||
	EXCEPTION(0xa00)
 | 
			
		||||
	EXCEPTION(0xb00)
 | 
			
		||||
	EXCEPTION(0xc00)
 | 
			
		||||
	EXCEPTION(0xd00)
 | 
			
		||||
	EXCEPTION(0xe00)
 | 
			
		||||
	EXCEPTION(0xe20)
 | 
			
		||||
	EXCEPTION(0xe40)
 | 
			
		||||
	EXCEPTION(0xe60)
 | 
			
		||||
	EXCEPTION(0xe80)
 | 
			
		||||
	EXCEPTION(0xf00)
 | 
			
		||||
	EXCEPTION(0xf20)
 | 
			
		||||
	EXCEPTION(0xf40)
 | 
			
		||||
	EXCEPTION(0xf60)
 | 
			
		||||
	EXCEPTION(0xf80)
 | 
			
		||||
#if 0
 | 
			
		||||
	EXCEPTION(0x1000)
 | 
			
		||||
	EXCEPTION(0x1100)
 | 
			
		||||
	EXCEPTION(0x1200)
 | 
			
		||||
	EXCEPTION(0x1300)
 | 
			
		||||
	EXCEPTION(0x1400)
 | 
			
		||||
	EXCEPTION(0x1500)
 | 
			
		||||
	EXCEPTION(0x1600)
 | 
			
		||||
#endif
 | 
			
		||||
@ -0,0 +1,37 @@
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
#include "console.h"
 | 
			
		||||
 | 
			
		||||
void rust_main();
 | 
			
		||||
 | 
			
		||||
void crash()
 | 
			
		||||
{
 | 
			
		||||
	void (*fun_ptr)() = (void(*)()) 0xdeadbeef;
 | 
			
		||||
	(*fun_ptr)();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void init_bss()
 | 
			
		||||
{
 | 
			
		||||
	extern int _bss, _ebss;
 | 
			
		||||
	int *p = &_bss;
 | 
			
		||||
	while (p < &_ebss) {
 | 
			
		||||
		*p++ = 0;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define HELLO_WORLD "Hello World\r\n"
 | 
			
		||||
 | 
			
		||||
int main(void)
 | 
			
		||||
{
 | 
			
		||||
	init_bss();
 | 
			
		||||
	potato_uart_init();
 | 
			
		||||
 | 
			
		||||
	putstr(HELLO_WORLD, strlen(HELLO_WORLD));
 | 
			
		||||
 | 
			
		||||
	rust_main();
 | 
			
		||||
	crash();
 | 
			
		||||
 | 
			
		||||
	while (1)
 | 
			
		||||
		;
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,17 @@
 | 
			
		||||
SECTIONS
 | 
			
		||||
{
 | 
			
		||||
	_start = .;
 | 
			
		||||
	. = 0;
 | 
			
		||||
	.head : {
 | 
			
		||||
		KEEP(*(.head))
 | 
			
		||||
 	}
 | 
			
		||||
	. = 0x1000;
 | 
			
		||||
	.text : { *(.text) }
 | 
			
		||||
	. = 0x1f000;
 | 
			
		||||
	.data : { *(.data) }
 | 
			
		||||
	. = ALIGN(4);
 | 
			
		||||
        _bss = .;         /* define a global symbol at bss start; used by startup code */
 | 
			
		||||
	.bss : { *(.bss) }
 | 
			
		||||
	. = ALIGN(4);
 | 
			
		||||
        _ebss = .;         /* define a global symbol at bss end; used by startup code */
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,78 @@
 | 
			
		||||
#![no_std]
 | 
			
		||||
#![feature(alloc_error_handler)]
 | 
			
		||||
 | 
			
		||||
use core::fmt::Write;
 | 
			
		||||
use core::panic::PanicInfo;
 | 
			
		||||
 | 
			
		||||
use heapless::consts::*;
 | 
			
		||||
use heapless::String;
 | 
			
		||||
 | 
			
		||||
extern crate linked_list_allocator;
 | 
			
		||||
use linked_list_allocator::*;
 | 
			
		||||
#[global_allocator]
 | 
			
		||||
static mut HEAP: LockedHeap = LockedHeap::empty();
 | 
			
		||||
 | 
			
		||||
extern crate alloc;
 | 
			
		||||
use alloc::vec::Vec;
 | 
			
		||||
 | 
			
		||||
extern crate cty;
 | 
			
		||||
 | 
			
		||||
extern "C" {
 | 
			
		||||
    fn putchar(c: cty::c_char) -> ();
 | 
			
		||||
    fn crash() -> ();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn print(s: &str) {
 | 
			
		||||
    for c in s.bytes() {
 | 
			
		||||
        unsafe { putchar(c) };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[no_mangle]
 | 
			
		||||
pub extern "C" fn rust_main() -> ! {
 | 
			
		||||
    print("Rust\r\n");
 | 
			
		||||
 | 
			
		||||
    const HEAP_SIZE: usize = 2048;
 | 
			
		||||
    static mut HEAP_AREA: [u8; HEAP_SIZE] = [0; HEAP_SIZE];
 | 
			
		||||
    unsafe { HEAP = LockedHeap::new(&HEAP_AREA[0] as *const u8 as usize, HEAP_AREA.len()) };
 | 
			
		||||
 | 
			
		||||
    let mut s: String<U128> = String::new();
 | 
			
		||||
    let mut xs = Vec::new();
 | 
			
		||||
    for i in 2..=3 {
 | 
			
		||||
        xs.push(i);
 | 
			
		||||
        xs.push(-3 * i);
 | 
			
		||||
        xs.push(i);
 | 
			
		||||
        writeln!(s, "i {}\r", i).unwrap();
 | 
			
		||||
        print(&s);
 | 
			
		||||
        s.clear();
 | 
			
		||||
        writeln!(s, "{}\r", 10.0 / i as f64).ok();
 | 
			
		||||
        print(&s);
 | 
			
		||||
        if xs.pop().unwrap() != i {
 | 
			
		||||
            panic!("??");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    xs.sort();
 | 
			
		||||
    writeln!(s, "{:?}\r", xs).unwrap();
 | 
			
		||||
    print(&s);
 | 
			
		||||
 | 
			
		||||
    panic!("test");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[panic_handler]
 | 
			
		||||
fn panic(panic_info: &PanicInfo) -> ! {
 | 
			
		||||
    unsafe {
 | 
			
		||||
        putchar('!' as u8);
 | 
			
		||||
    }
 | 
			
		||||
    let mut s: String<U128> = String::new();
 | 
			
		||||
    writeln!(s, "{}\r", panic_info).ok();
 | 
			
		||||
    print(&s);
 | 
			
		||||
    unsafe {
 | 
			
		||||
        crash();
 | 
			
		||||
    }
 | 
			
		||||
    loop {}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[alloc_error_handler]
 | 
			
		||||
fn alloc_error(_: core::alloc::Layout) -> ! {
 | 
			
		||||
    panic!("Heap");
 | 
			
		||||
}
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue