Skip to content
This repository has been archived by the owner on Nov 21, 2017. It is now read-only.

Commit

Permalink
Add Rust example. Closes #73
Browse files Browse the repository at this point in the history
  • Loading branch information
IanSeyler committed Nov 12, 2014
1 parent 069bbb2 commit 740d2b2
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions programs/rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Rust on BareMetal - Tested with Rust 0.13.0-nightly
// rustc -O --crate-type lib -o rust.o --emit obj rust.rs
// ld -T app.ld -o rust.app rust.o

#![no_std]
#![allow(ctypes)]

#![feature(lang_items)]
#[lang="sized"]

This comment has been minimized.

Copy link
@edef1c

edef1c Nov 12, 2014

Why not link to libcore instead? libcore provides everything from the Rust stdlib that works in a freestanding environment.

This comment has been minimized.

Copy link
@IanSeyler

IanSeyler Nov 12, 2014

Author Member

This was just a quick port of ragingSloth/rustboot@0a55ab5

I don't know much about Rust myself but this is open for updates.

trait Sized {}

enum Color {
Black = 0,
Red = 1,
Green = 2,
Blue = 3,
Yellow = 4,
Purple = 5,
Cyan = 6,
LightGray = 7,
DarkGray = 8,
LightRed = 9,
LightGreen = 10,
LightBlue = 11,
LightYellow = 12,
LightPurple = 13,
LightCyan = 14,
White = 15,
}

enum Option<T> {
None,
Some(T)
}

struct IntRange {
cur: int,
max: int
}

impl IntRange {
fn next(&mut self) -> Option<int> {
if self.cur < self.max {
self.cur += 1;
Some(self.cur - 1)
} else {
None
}
}
}

fn range(lo: int, hi: int) -> IntRange {
IntRange { cur: lo, max: hi }
}

fn clear_screen(background: Color) {
let mut r = range(0, 80 * 25);
loop {
match r.next() {
Some(x) => {
unsafe {
*((0xb8000 + x * 2) as *mut u16) = (background as u16) << 12;
}
},
None => {break}
}
}
}

#[no_mangle]
#[no_split_stack]
pub fn main() {
clear_screen(LightRed);
}

0 comments on commit 740d2b2

Please sign in to comment.