diff --git a/.vscode/settings.json b/.vscode/settings.json index f5dcd8929..08bdd4b43 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,8 @@ { "files.associations": { "stddef.h": "c" + }, + "settings": { + } } \ No newline at end of file diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index e7280f936..e12a9ab16 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -41,7 +41,7 @@ lliw = "0.2.0" # qoi_rs = "*" spin = "0.5.2" vga = "*" -log= "*" +log = "*" pretty-hex = "0.2.1" unicode-width = "0.1.7" picorand = "*" @@ -67,10 +67,10 @@ version = "0.7.29" default-features = false features = ["size_64", "alloc"] -[dependencies.smoltcp] +[dependencies.smoltcp] version = "0.8.0" default-features = false -features = ["log", "proto-ipv4"] +features = ["log", "proto-ipv4"] [dependencies.y-compositor-protocol] git = "https://git.ablecorp.us:443/able/y-compositor-protocol.git" diff --git a/ableos/rust-toolchain b/ableos/rust-toolchain index bf867e0ae..c6d365efa 100644 --- a/ableos/rust-toolchain +++ b/ableos/rust-toolchain @@ -1 +1 @@ -nightly +nightly-2022-01-04 diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index c959e2177..b3d9c2cad 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -18,14 +18,17 @@ #![feature(exclusive_range_pattern)] #![feature(slice_pattern)] +/// Contains architecture specific code for aarch64. #[cfg(target_arch = "aarch64")] #[path = "arch/aarch64/mod.rs"] pub mod arch; +/// Contains architecture specific code for x86_64. #[cfg(target_arch = "x86_64")] #[path = "arch/x86_64/mod.rs"] pub mod arch; +/// Contains architecture specific code for riscv64. #[cfg(target_arch = "riscv64")] #[path = "arch/riscv/mod.rs"] pub mod arch; diff --git a/ableos/src/vga_e.rs b/ableos/src/vga_e.rs index 0f0c1bf49..2c6a1ff31 100644 --- a/ableos/src/vga_e.rs +++ b/ableos/src/vga_e.rs @@ -3,11 +3,11 @@ use shadeable::pixel_format::Rgba64; use crate::SCREEN_BUFFER; use { - ab_glyph::{Font, FontRef, Glyph}, - vga::{ - colors::Color16, - writers::{Graphics640x480x16, GraphicsWriter}, - }, + ab_glyph::{Font, FontRef, Glyph}, + vga::{ + colors::Color16, + writers::{Graphics640x480x16, GraphicsWriter}, + }, }; lazy_static::lazy_static! { @@ -20,6 +20,10 @@ lazy_static::lazy_static! { pub static ref VGAE_BUFF_OFFSET_Y: spin::Mutex = spin::Mutex::new(0); } +const FONT_SCALE: f32 = 1.6; +const GLYPH_HEIGHT: f32 = 18.0; +const GLYPH_WIDTH: f32 = 10.0; + /// Draw a glyph on the screen at the given position /// /// # Arguments @@ -27,79 +31,86 @@ lazy_static::lazy_static! { /// * `y` - the y position of the glyph /// * `glyph` - the glyph to draw /// * `color` - the color of the glyph -pub fn draw_char(x: u8, y: u8, character: char, color: Rgba64) { - // let mode = *VGAE.lock(); - let mut mode = SCREEN_BUFFER.lock(); +pub fn draw_char(mut x: u32, mut y: u32, character: char, color: Rgba64) { + // let mode = *VGAE.lock(); + let mut mode = SCREEN_BUFFER.lock(); - let font = FontRef::try_from_slice(include_bytes!( - "../../ableos/assets/fonts/unifont-14.0.01.ttf" - )) - .unwrap(); + let basic_multingual_plane = FontRef::try_from_slice(include_bytes!( + "../../ableos/assets/fonts/unifont-14.0.01.ttf" + )) + .unwrap(); - let font2 = FontRef::try_from_slice(include_bytes!( - "../../ableos/assets/fonts/unifont_upper-14.0.01.ttf" - )) - .unwrap(); + let supplementary_multilingual_plane = FontRef::try_from_slice(include_bytes!( + "../../ableos/assets/fonts/unifont_upper-14.0.01.ttf" + )) + .unwrap(); - let mut has_char = false; - for x in font.codepoint_ids() { - if x.1 == character { - has_char = true; - break; - } - } + // let mut has_char = false; + // for x in font.codepoint_ids() { + // if x.1 == character { + // has_char = true; + // break; + // } + // } - let used_font; - match has_char { - true => used_font = font, - false => used_font = font2, - } + let is_in_basic_multilingual_plane = character as u32 <= 0xffff; - let font_scale = 1.6; + let plane = match is_in_basic_multilingual_plane { + true => basic_multingual_plane, + false => supplementary_multilingual_plane, + }; - match character { - '\n' => {} - _ => { - let mut q_glyph: Glyph = used_font.glyph_id(character).with_scale_and_position( - 20.0 * font_scale, - ab_glyph::point(10.0 * font_scale, 18.0 * font_scale), - ); + match character { + '\n' => {} + _ => { + let q_glyph: Glyph = plane.glyph_id(character).with_scale_and_position( + 20.0 * FONT_SCALE, + ab_glyph::point(GLYPH_WIDTH * FONT_SCALE, GLYPH_HEIGHT * FONT_SCALE), + ); - if let Some(q) = used_font.outline_glyph(q_glyph) { - q.draw(|x, y, c| { - if c > 0.015 { - let corner = q.px_bounds().min; - mode.set_pixel( - x as usize + corner.x as usize, - y as usize + corner.y as usize, - color, - ); - } - }); - } - } - } + // elf: I don't know if GLYPH_HEIGHT is in the right units here. I'm just guessing. + if x as usize > mode.size.x { + x = 0; + y += (GLYPH_HEIGHT * FONT_SCALE) as u32; + } + + if let Some(q) = plane.outline_glyph(q_glyph) { + q.draw(|gx, gy, c| { + if c > 0.015 { + let corner = q.px_bounds().min; + mode.set_pixel( + gx as usize + corner.x as usize + x as usize, + gy as usize + corner.y as usize + y as usize, + color, + ); + } + }); + } + } + } } + +/// Converts a number to ... i forgor 💀 pub fn num_to_vga16(num: u8) -> Color16 { - use Color16::*; - match num { - 0 => Black, - 1 => Blue, - 2 => Green, - 3 => Cyan, - 4 => Red, - 5 => Magenta, - 6 => Brown, - 7 => LightGrey, - 8 => DarkGrey, - 9 => LightBlue, - 10 => LightGreen, - 11 => LightCyan, - 12 => LightRed, - 13 => Pink, - 14 => Yellow, - 15 => White, - // NOTE: Leasve the in - _ => Color16::Pink, - } + use Color16::*; + match num { + 0 => Black, + 1 => Blue, + 2 => Green, + 3 => Cyan, + 4 => Red, + 5 => Magenta, + 6 => Brown, + 7 => LightGrey, + 8 => DarkGrey, + 9 => LightBlue, + 10 => LightGreen, + 11 => LightCyan, + 12 => LightRed, + 13 => Pink, + 14 => Yellow, + 15 => White, + // NOTE: Leasve the in + _ => Color16::Pink, + } } diff --git a/repbuild/src/main.rs b/repbuild/src/main.rs index 98af974ca..51a01e6fe 100644 --- a/repbuild/src/main.rs +++ b/repbuild/src/main.rs @@ -4,46 +4,46 @@ use clap::Parser; #[clap(version = clap::crate_version!(), author = clap::crate_authors!("\n"))] /// Hello Remember this is a feature enum Command { - Run { - #[clap(long, short)] - debug: bool, + Run { + #[clap(long, short)] + debug: bool, - #[clap(long, short, arg_enum)] - machine: Option, - }, + #[clap(long, short, arg_enum)] + machine: Option, + }, - Doc { - #[clap(long, short, arg_enum)] - machine: Option, - }, + Doc { + #[clap(long, short, arg_enum)] + machine: Option, + }, } #[derive(clap::ArgEnum, Debug, Clone)] enum MachineType { - X86, - RISCV, - ARM, + X86, + RISCV, + ARM, } fn main() -> anyhow::Result<()> { - let args = Command::parse(); + let args = Command::parse(); - match args { - Command::Run { debug, machine } => { - let _dir = xshell::pushd("./ableos"); + match args { + Command::Run { debug, machine } => { + let _dir = xshell::pushd("./ableos"); - let _debug_log: &[&str] = match debug { - true => &["-D", "debug.log"], - false => &[], - }; - match machine.unwrap_or(MachineType::X86) { - MachineType::X86 => { - xshell::cmd!("cargo run --release").run()?; - } - MachineType::ARM => { - xshell::cmd!("cargo build --release --target=json_targets/aarch64-ableos.json") - .run()?; - #[rustfmt::skip] + let _debug_log: &[&str] = match debug { + true => &["-D", "debug.log"], + false => &[], + }; + match machine.unwrap_or(MachineType::X86) { + MachineType::X86 => { + xshell::cmd!("cargo run --release").run()?; + } + MachineType::ARM => { + xshell::cmd!("cargo build --release --target=json_targets/aarch64-ableos.json") + .run()?; + #[rustfmt::skip] xshell::cmd!( "qemu-system-aarch64 -machine virt @@ -53,11 +53,10 @@ fn main() -> anyhow::Result<()> { -device virtio-keyboard " ).run()?; - } - MachineType::RISCV => { - xshell::cmd!("cargo build --release --target=riscv64gc-unknown-none-elf") - .run()?; - #[rustfmt::skip] + } + MachineType::RISCV => { + xshell::cmd!("cargo build --release --target=riscv64gc-unknown-none-elf").run()?; + #[rustfmt::skip] xshell::cmd!( "qemu-system-riscv64 -machine virt @@ -67,27 +66,26 @@ fn main() -> anyhow::Result<()> { -bios src/arch/riscv/firmwear/opensbi-riscv64-generic-fw_jump.bin -kernel target/riscv64gc-unknown-none-elf/release/ableos" ).run()?; - } } - } + } + } - Command::Doc { machine } => { - let _dir = xshell::pushd("./ableos"); + Command::Doc { machine } => { + let _dir = xshell::pushd("./ableos"); - match machine.unwrap_or(MachineType::X86) { - MachineType::X86 => { - xshell::cmd!("cargo doc --open").run()?; - } - MachineType::ARM => { - xshell::cmd!("cargo doc --open --target=json_targets/aarch64-ableos.json") - .run()?; - } - MachineType::RISCV => { - xshell::cmd!("cargo doc --open --target=riscv64gc-unknown-none-elf").run()?; - } + match machine.unwrap_or(MachineType::X86) { + MachineType::X86 => { + xshell::cmd!("cargo doc --open").run()?; } - } - } + MachineType::ARM => { + xshell::cmd!("cargo doc --open --target=json_targets/aarch64-ableos.json").run()?; + } + MachineType::RISCV => { + xshell::cmd!("cargo doc --open --target=riscv64gc-unknown-none-elf").run()?; + } + } + } + } - Ok(()) + Ok(()) } diff --git a/shadeable/src/pixel_format.rs b/shadeable/src/pixel_format.rs index 11d059eb9..b14c05174 100644 --- a/shadeable/src/pixel_format.rs +++ b/shadeable/src/pixel_format.rs @@ -1,218 +1,162 @@ -#![feature(exclusive_range_pattern)] -use log::*; -use rhai::INT; use vga::colors::Color16; pub type Rgba64 = u64; pub fn get_r(rgba: Rgba64) -> u8 { - let x: u64 = rgba; - - let y = x >> 24; - - return (y & 0xff).try_into().unwrap(); + (rgba & 0xff_00_00_00 >> 0o30) as u8 } pub fn get_g(rgba: Rgba64) -> u8 { - let x: u64 = rgba; - - let y = x >> 16; - - return (y & 0xff).try_into().unwrap(); + (rgba & 0xff_00_00 >> 0o20) as u8 } + pub fn get_b(rgba: Rgba64) -> u8 { - let x: u64 = rgba; - - let y = x >> (24 - 16); - - return (y & 0xff).try_into().unwrap(); + (rgba & 0xff_00 >> 0o10) as u8 } + pub fn get_a(rgba: Rgba64) -> u8 { - let x = rgba; - - let y = x >> (24 - 16 - 8); - - return (y & 0xff).try_into().unwrap(); + (rgba & 0xff) as u8 } pub fn set_r(rgba: Rgba64, r: u8) -> Rgba64 { - let z = (r as Rgba64) << 24; - let y = rgba & 0xffffff; - return z | y; + rgba & 0xffffffff_00_ff_ff_ff | (r as Rgba64) << 0o30 } pub fn set_g(rgba: Rgba64, g: u8) -> Rgba64 { - let z = (g as Rgba64) << 16; - let y = rgba & 0xffffff; - return z | y; + rgba & 0xffffffff_ff_00_ff_ff | (g as Rgba64) << 0o20 } pub fn set_b(rgba: Rgba64, b: u8) -> Rgba64 { - let z = (b as Rgba64) << 26 - 16; - let y = rgba & 0xffffff; - return z | y; + rgba & 0xffffffff_ff_ff_00_ff | (b as Rgba64) << 0o10 } pub fn set_a(rgba: Rgba64, a: u8) -> Rgba64 { - let z = (a as Rgba64) << 8; - let y = rgba & 0xffffff; - return z | y; + rgba & 0xffffffff_ff_ff_ff_00 | (a as Rgba64) } pub fn rgba_div(a: Rgba64, b: Rgba64) -> Rgba64 { - let mut fin: Rgba64 = 0; - - // println!("{}", fin); - fin |= set_r(fin, get_r(a) / get_r(b)); - // println!("{}", fin); - - fin |= set_g(fin, get_g(a) / get_g(b)); - /* - - get_b(a) / get_b(b); - - get_a(a) / get_a(b); - */ - - return fin; + set_r(0, get_r(a) / get_r(b)) + | set_g(0, get_g(a) / get_g(b)) + | set_g(0, get_b(a) / get_b(b)) + | set_g(0, get_a(a) / get_a(b)) } pub fn new_rgba64(r: u8, g: u8, b: u8, a: u8) -> Rgba64 { - let mut x = 0; + set_r(0, r) | set_g(0, g) | set_b(0, b) | set_a(0, a) +} - x |= set_r(x, r); - x |= set_g(x, g); - x |= set_b(x, b); - x |= set_a(x, a); +enum ChannelValue { + Dark, + Low, + Mid, + High, +} - x +impl From for ChannelValue { + fn from(b: u8) -> Self { + use ChannelValue::*; + match b { + 0x00..=0x3f => Dark, + 0x40..=0x7f => Low, + 0x80..=0xbf => Mid, + 0xc0..=0xff => High, + } + } } pub fn into_vga_16(rgba_64: Rgba64) -> Color16 { - let mut fourbit: u8 = 0; - fourbit |= match get_r(rgba_64) { - 85..=170 => 0b0100, - 171..=255 => 0b1100, - _ => 0, - }; - fourbit |= match get_g(rgba_64) { - 85..=170 => 0b0010, - 171..=255 => 0b1010, - _ => 0, - }; - fourbit |= match get_b(rgba_64) { - 85..=170 => 0b0001, - 171..=255 => 0b1001, - _ => 0, - }; - - // trace!("{}", fourbit); - - use Color16::*; - match fourbit { - 0 => Black, - 1 => Blue, - 2 => Green, - 3 => Cyan, - 4 => Red, - 5 => Magenta, - 6 => Brown, - 7 => LightGrey, - 8 => DarkGrey, - 9 => LightBlue, - 10 => LightGreen, - 11 => LightCyan, - 12 => LightRed, - 13 => Pink, - 14 => Yellow, - 15 => White, - _ => Green, - } + use ChannelValue::*; + use Color16::*; + match ( + get_r(rgba_64).into(), + get_g(rgba_64).into(), + get_b(rgba_64).into(), + ) { + (Dark, Dark, Dark) => Black, + (Dark, Dark, Low) => Black, + (Dark, Dark, Mid) => Blue, + (Dark, Dark, High) => Blue, + (Dark, Low, Dark) => Black, + (Dark, Low, Low) => Black, + (Dark, Low, Mid) => Blue, + (Dark, Low, High) => Blue, + (Dark, Mid, Dark) => Green, + (Dark, Mid, Low) => Green, + (Dark, Mid, Mid) => Cyan, + (Dark, Mid, High) => Cyan, + (Dark, High, Dark) => Green, + (Dark, High, Low) => Green, + (Dark, High, Mid) => Green, + (Dark, High, High) => Cyan, + (Low, Dark, Dark) => Black, + (Low, Dark, Low) => Black, + (Low, Dark, Mid) => Blue, + (Low, Dark, High) => Blue, + (Low, Low, Dark) => Black, + (Low, Low, Low) => DarkGrey, + (Low, Low, Mid) => LightGrey, + (Low, Low, High) => Blue, + (Low, Mid, Dark) => DarkGrey, + (Low, Mid, Low) => LightGrey, + (Low, Mid, Mid) => Cyan, + (Low, Mid, High) => Cyan, + (Low, High, Dark) => Green, + (Low, High, Low) => Green, + (Low, High, Mid) => Cyan, + (Low, High, High) => Cyan, + (Mid, Dark, Dark) => Red, + (Mid, Dark, Low) => Red, + (Mid, Dark, Mid) => Magenta, + (Mid, Dark, High) => Magenta, + (Mid, Low, Dark) => Brown, + (Mid, Low, Low) => Red, + (Mid, Low, Mid) => DarkGrey, + (Mid, Low, High) => LightBlue, + (Mid, Mid, Dark) => Brown, + (Mid, Mid, Low) => Brown, + (Mid, Mid, Mid) => LightGrey, + (Mid, Mid, High) => LightBlue, + (Mid, High, Dark) => Green, + (Mid, High, Low) => Green, + (Mid, High, Mid) => LightGreen, + (Mid, High, High) => LightCyan, + (High, Dark, Dark) => Red, + (High, Dark, _) => Magenta, + (High, Low, Dark) => Red, + (High, Low, Low) => LightRed, + (High, Low, Mid) => Pink, + (High, Low, High) => Magenta, + (High, Mid, Dark) => Yellow, + (High, Mid, Low) => Yellow, + (High, Mid, Mid) => LightRed, + (High, Mid, High) => Pink, + (High, High, Dark) => Yellow, + (High, High, Low) => White, + (High, High, Mid) => White, + (High, High, High) => White, + } } pub fn from_vga_16(color: Color16) -> Rgba64 { - use Color16::*; - match color { - Black => new_rgba64(0, 0, 0, 0), - DarkGrey => new_rgba64(105, 105, 105, 0), - LightGrey => new_rgba64(211, 211, 211, 0), - // - Blue => new_rgba64(0, 0, 255, 0), - Green => new_rgba64(0, 255, 0, 0), - Red => new_rgba64(255, 0, 0, 0), - // - Yellow => new_rgba64(255, 255, 0, 0), - Cyan => new_rgba64(0, 255, 255, 0), - Magenta => new_rgba64(255, 0, 255, 0), + use Color16::*; + match color { + Black => new_rgba64(0, 0, 0, 0), + DarkGrey => new_rgba64(105, 105, 105, 0), + LightGrey => new_rgba64(211, 211, 211, 0), + // + Blue => new_rgba64(0, 0, 0xff, 0), + Green => new_rgba64(0, 0xff, 0, 0), + Red => new_rgba64(0xff, 0, 0, 0), + // + Yellow => new_rgba64(0xff, 0xff, 0, 0), + Cyan => new_rgba64(0, 0xff, 0xff, 0), + Magenta => new_rgba64(0xff, 0, 0xff, 0), - Brown => new_rgba64(165, 42, 42, 0), - Pink => new_rgba64(255, 105, 180, 0), - White => new_rgba64(255, 255, 255, 0), + Brown => new_rgba64(165, 42, 42, 0), + Pink => new_rgba64(0xff, 105, 180, 0), + White => new_rgba64(0xff, 0xff, 0xff, 0), - LightBlue => new_rgba64(173, 216, 230, 0), - LightGreen => new_rgba64(144, 238, 144, 0), - LightCyan => new_rgba64(88, 100, 100, 0), - LightRed => new_rgba64(255, 204, 203, 0), - } -} - -use libm::sqrt; - -fn euclideand(c1: (u8, u8, u8), c2: (u8, u8, u8)) -> u8 { - let (r1, g1, b1) = c1; - let (r2, g2, b2) = c2; - let (rd, gd, bd) = (r2 - r1, g2 - g1, b2 - b1); - sqrt((rd * rd + gd * gd + bd * bd) as f64) as u8 -} -pub fn get_color16(c: Rgba64) -> Color16 { - let r = get_r(c); - let g = get_g(c); - let b = get_b(c); - - let c = (r, g, b); - let palette: [(u8, u8, u8); 16] = [ - (0, 0, 0), - (105, 105, 105), - (211, 211, 211), - (0, 0, 255), - (0, 255, 0), - (255, 0, 0), - (255, 255, 0), - (0, 255, 255), - (255, 0, 255), - (165, 42, 42), - (255, 105, 180), - (255, 255, 255), - (173, 216, 230), - (144, 238, 144), - (88, 100, 100), - (255, 204, 203), - ]; - let mut minc = euclideand(c, palette[0]); - let mut retval = 0; - for i in 1..16 { - let eucd = euclideand(c, palette[i]); - if eucd < minc { - retval = i; - minc = eucd - } - } - use Color16::*; - match retval { - 0 => Black, - 1 => Blue, - 2 => Green, - 3 => Cyan, - 4 => Red, - 5 => Magenta, - 6 => Brown, - 7 => LightGrey, - 8 => DarkGrey, - 9 => LightBlue, - 10 => LightGreen, - 11 => LightCyan, - 12 => LightRed, - 13 => Pink, - 14 => Yellow, - 15 => White, - _ => Green, - } + LightBlue => new_rgba64(173, 216, 230, 0), + LightGreen => new_rgba64(144, 238, 144, 0), + LightCyan => new_rgba64(88, 100, 100, 0), + LightRed => new_rgba64(0xff, 204, 203, 0), + } }