From 15061f45c8a62afbe8cbe29293a0e466644830fb Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 7 Dec 2020 00:08:12 -0600 Subject: [PATCH] phil OPP --- .cargo/config.toml | 9 +++++++++ .gitignore | 1 + Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 15 +++++++++++++++ Makefile | 10 ++++++++++ rust-toolchain | 1 + src/main.rs | 29 +++++++++++++++++++++++++++++ src/vga_buffer.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ target.json | 15 +++++++++++++++ 9 files changed, 147 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 Makefile create mode 100644 rust-toolchain create mode 100644 src/main.rs create mode 100644 src/vga_buffer.rs create mode 100644 target.json diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..09f68c0 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,9 @@ +[build] +target = "target.json" + +[unstable] +build-std = ["core", "compiler_builtins"] + +[target.'cfg(target_os = "none")'] +runner = "bootimage runner" + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..cd4c128 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,21 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "bootloader" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83732ad599045a978528e4311539fdcb20c30e406b66d1d08cd4089d4fc8d90f" + +[[package]] +name = "toymk" +version = "0.1.0" +dependencies = [ + "bootloader", + "volatile", +] + +[[package]] +name = "volatile" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b53d47a0f279fc0a9d9d1d82f4c6f313eb63b73e2a85d9c27fd609084cfa08f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d9bf954 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "toymk" +version = "0.1.0" +authors = ["Michael Zhang "] +edition = "2018" + +[profile.dev] +panic = "abort" + +[profile.release] +panic = "abort" + +[dependencies] +bootloader = "0.9.11" +volatile = "0.4.2" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..02e4281 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +TARGET_TRIPLE = target.json + +all: + cargo build --target $(TARGET_TRIPLE) + + +watch: + cargo watch -x "check --target $(TARGET_TRIPLE)" + +.PHONY: watch diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 0000000..bf867e0 --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +nightly diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..140eec7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,29 @@ +#![no_std] +#![no_main] + +mod vga_buffer; + +use core::panic::PanicInfo; + +use crate::vga_buffer::{Color, ColorCode}; + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +} + +static HELLO: &[u8] = b"Hello World!"; + +#[no_mangle] +pub extern "C" fn _start() -> ! { + let vga_buffer = 0xb8000 as *mut u8; + + for (i, &byte) in HELLO.iter().enumerate() { + unsafe { + *vga_buffer.offset(i as isize * 2) = byte; + *vga_buffer.offset(i as isize * 2 + 1) = 0x7; + } + } + + loop {} +} diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs new file mode 100644 index 0000000..6847744 --- /dev/null +++ b/src/vga_buffer.rs @@ -0,0 +1,46 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(C)] +pub struct ScreenChar { + ascii_character: u8, + color_code: ColorCode, +} + +const BUFFER_HEIGHT: usize = 25; +const BUFFER_WIDTH: usize = 80; + +#[repr(transparent)] +pub struct Buffer { + chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT], +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(transparent)] +pub struct ColorCode(u8); + +impl ColorCode { + fn new(foreground: Color, background: Color) -> ColorCode { + ColorCode((background as u8) << 4 | (foreground as u8)) + } +} + +#[allow(dead_code)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u8)] +pub enum Color { + Black = 0, + Blue = 1, + Green = 2, + Cyan = 3, + Red = 4, + Magenta = 5, + Brown = 6, + LightGray = 7, + DarkGray = 8, + LightBlue = 9, + LightGreen = 10, + LightCyan = 11, + LightRed = 12, + Pink = 13, + Yellow = 14, + White = 15, +} diff --git a/target.json b/target.json new file mode 100644 index 0000000..7d2110d --- /dev/null +++ b/target.json @@ -0,0 +1,15 @@ +{ + "llvm-target": "x86_64-unknown-none", + "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128", + "arch": "x86_64", + "target-endian": "little", + "target-pointer-width": "64", + "target-c-int-width": "32", + "os": "none", + "executables": true, + "linker-flavor": "ld.lld", + "linker": "rust-lld", + "panic-strategy": "abort", + "disable-redzone": true, + "features": "-mmx,-sse,+soft-float" +}