Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to upstreamed avr rust #4

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 11 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,26 @@ able to compile this crate. Also, because of some remaining bugs in
the LLVM AVR backend, Rust's stock `libcore` cannot be compiled yet;
we need to use Xargo to link to a slightly stripped down version.

## 1. Build branch of LLVM with AVR support + kludges
## 1. Install the Rust nightly compiler and the rust-src rustup component

```
$ git clone -b avr-rust-demo https://github.com/avr-rust/llvm.git llvm-avr
$ mkdir -p build/llvm
$ cd build/llvm
$ cmake ../../llvm-avr -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=AVR -DLLVM_TARGETS_TO_BUILD=X86
$ make
$ cd ../...
```
Install [rustup](https://rustup.rs/).

## 2. Build branch of `rustc` with AVR support
Then install the Rust nightly compiler, and then add the `rust-src` component to Rustup.

```
$ git clone -b avr-support https://github.com/avr-rust/rust.git rust-avr
$ mkdir -p build/rust
$ cd build/rust
$ ../../rust-avr/configure --llvm-root=$(realpath ../llvm)
$ make
$ cd ../..
```bash
# add the rust-src component which is used for building libcore
rustup component add rust-src
```

## 3. Register freshly-built Rust AVR toolchain with `rustup` (needed to work around [a Xargo bug][xargo-rustup])
## 2. Enable the nightly compiler by default with `rustup`

```
$ rustup toolchain link avr-toolchain $(realpath build/rust/build/x86_64-unknown-linux-gnu/stage1
$ rustup default avr-toolchain
```bash
$ rustup default nightly
```

## 4. Build `chip8-avr` and all its dependencies using Xargo
## 3. Build `chip8-avr` and all its dependencies using Xargo

```
```bash
$ git clone https://github.com/gergoerdi/rust-avr-chip8-avr chip8-avr
$ cd chip8-avr
$ sh build.sh
Expand Down
4 changes: 3 additions & 1 deletion avr-atmega328p.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
"cpu": "atmega328p",
"target-endian": "little",
"target-pointer-width": "16",
"target-c-int-width": "16",
"os": "unknown",
"target-env": "",
"target-vendor": "unknown",
"arch": "avr",
"data-layout": "e-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",

"executables": true,

"linker": "avr-gcc",
"linker-flavor": "gcc",
"eh-frame-header": false,
"pre-link-args": {
"gcc": ["-Os", "-mmcu=atmega328p", "-T", "lookup-text.ld"]
},
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sh roms.sh
XARGO_RUST_SRC=/ xargo build --target=avr-atmega328p --release $@
cargo build -Z build-std=core --target=avr-atmega328p.json --release $@

2 changes: 1 addition & 1 deletion src/keypad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn scan_key_row(row: u8) -> u8 {
_ => {}
};

asm!("NOP");
llvm_asm!("NOP");

let mut result = 0;
let mut buf = 0;
Expand Down
9 changes: 4 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(lang_items)]
#![feature(asm)]
#![feature(llvm_asm)]
#![feature(abi_avr_interrupt)]
#![feature(unwind_attributes)]
#![feature(core_intrinsics)]
Expand All @@ -18,10 +18,9 @@ pub mod std {
pub unsafe extern "C" fn rust_eh_personality(state: (), exception_object: *mut (), context: *mut ()) -> () {
}

#[lang = "panic_fmt"]
#[unwind]
pub extern fn rust_begin_panic(msg: (), file: &'static str, line: u32) -> ! {
loop{}
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
loop { }
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/serial_ram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn setup() {
#[inline(never)]
pub fn write_ram(addr: u16, value: u8) {
unsafe {
asm!("CLI");
llvm_asm!("CLI");

volatile_store(PORTD, volatile_load(PORTD) & !(1 << 6));
spi::sync(0x02);
Expand All @@ -30,14 +30,14 @@ pub fn write_ram(addr: u16, value: u8) {
spi::sync(value);
volatile_store(PORTD, volatile_load(PORTD) | (1 << 6));

asm!("SEI");
llvm_asm!("SEI");
}
}

#[inline(never)]
pub fn read_ram(addr: u16) -> u8 {
unsafe {
asm!("CLI");
llvm_asm!("CLI");

volatile_store(PORTD, volatile_load(PORTD) & !(1 << 6));
spi::sync(0x03);
Expand All @@ -46,7 +46,7 @@ pub fn read_ram(addr: u16) -> u8 {
let value = spi::sync(0);
volatile_store(PORTD, volatile_load(PORTD) | (1 << 6));

asm!("SEI");
llvm_asm!("SEI");

value
}
Expand Down
4 changes: 2 additions & 2 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn setup() {
volatile_store(TIMSK1, volatile_load(TIMSK1) | 1 << 1);

// Good to go!
asm!("SEI")
llvm_asm!("SEI")
}
}

Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn sleep_ms(duration_ms: u16) {
while outer < duration_ms {
let mut inner = 0;
while inner < INNER_LOOP_ITERATIONS {
unsafe { asm!(""); }
unsafe { llvm_asm!(""); }
inner += 1;
}
outer += 1;
Expand Down