systemcall work

This commit is contained in:
Able 2022-02-03 21:37:51 -06:00
parent b3b79e1694
commit 39fb7f4cab
8 changed files with 324 additions and 276 deletions

View file

@ -7,8 +7,8 @@ use vga::writers::GraphicsWriter;
#[derive(Debug)] #[derive(Debug)]
pub struct ScreenSize { pub struct ScreenSize {
pub x: usize, pub x: usize,
pub y: usize, pub y: usize,
} }
const FONT_SCALE: f32 = 1.6; const FONT_SCALE: f32 = 1.6;
@ -21,240 +21,245 @@ lazy_static::lazy_static! {
} }
impl ScreenSize { impl ScreenSize {
pub fn new(x: usize, y: usize) -> Self { pub fn new(x: usize, y: usize) -> Self {
Self { x, y } Self { x, y }
} }
} }
pub enum GraphicsReturn { pub enum GraphicsReturn {
Ok, Ok,
ImproperScreenSize, ImproperScreenSize,
} }
pub struct ScreenBuffer { pub struct ScreenBuffer {
pub size: ScreenSize, pub size: ScreenSize,
pub clear_color: Rgba64, pub clear_color: Rgba64,
pub buff: Box<[Rgba64]>, // Vec<Rgba64>, pub buff: Box<[Rgba64]>, // Vec<Rgba64>,
} }
impl ScreenBuffer { impl ScreenBuffer {
// Add optional size later // Add optional size later
pub fn new(x: usize, y: usize) -> Self { pub fn new(x: usize, y: usize) -> Self {
Self { Self {
size: ScreenSize::new(x, y), size: ScreenSize::new(x, y),
clear_color: 0, clear_color: 0,
buff: vec![0u64; x * y].into_boxed_slice(), buff: vec![0u64; x * y].into_boxed_slice(),
} }
} }
pub fn draw_filled_circle(&mut self, cx: i32, cy: i32, radius: usize, color: Rgba64) { pub fn draw_filled_circle(&mut self, cx: i32, cy: i32, radius: usize, color: Rgba64) {
let r = radius as i32 * 2; let r = radius as i32 * 2;
for y in 0..640 { for y in 0..640 {
for x in 0..480 { for x in 0..480 {
let dx = cx - x as i32 * 2 - 1; let dx = cx - x as i32 * 2 - 1;
let dy = cy - y as i32 * 2 - 1; let dy = cy - y as i32 * 2 - 1;
if dx * dx + dy * dy <= r * r { if dx * dx + dy * dy <= r * r {
self.set_pixel(x, y, color); self.set_pixel(x, y, color);
}; };
}
}
}
#[inline]
pub fn set_pixel(&mut self, x: usize, y: usize, color: Rgba64) {
self.buff[y * self.size.x + x] = color;
}
pub fn clear(&mut self) {
self.buff = vec![0u64; self.buff.len()].into_boxed_slice();
}
pub fn blit(&mut self, _width: usize, _height: usize) {}
pub fn draw_filled_rect(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: Rgba64) {
for y in y1..y2 {
for x in x1..x2 {
self.set_pixel(x, y, color);
}
}
}
pub fn draw_unfilled_rect(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: Rgba64) {
// x1 y1 => x2 y1 => x2 y2 => x1 y2 => x1 y1
self.draw_line(x1, y1, x2, y1, color);
self.draw_line(x2, y1, x2, y2, color);
self.draw_line(x2, y2, x1, y2, color);
self.draw_line(x1, y2, x1, y1, color);
}
#[inline]
pub fn draw_line(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: Rgba64) {
let x = crate::graphics::get_coordinates(
x1.try_into().unwrap(),
y1.try_into().unwrap(),
x2.try_into().unwrap(),
y2.try_into().unwrap(),
);
for coord in x {
self.set_pixel(coord.0, coord.1, color);
}
}
pub fn shade(&mut self) {
for y in 0..100 {
for x in 0..100 {
let rgba_ret = evaluate_shader(x, y, self.buff[y * self.size.x + x]);
match rgba_ret {
Ok(pixel) => {
// info!("{:?}", pixel);
self.set_pixel(x, y, pixel);
}
Err(err) => error!("{}", err),
} }
} }
} }
info!("Shaders done"); #[inline]
} pub fn set_pixel(&mut self, x: usize, y: usize, color: Rgba64) {
self.buff[y * self.size.x + x] = color;
}
// TODO force clear pub fn clear(&mut self) {
pub fn force_redraw(&mut self) { self.buff = vec![0u64; self.buff.len()].into_boxed_slice();
use shadeable::pixel_format::into_vga_16; }
VGAE.lock().clear_screen(into_vga_16(self.clear_color));
}
/// Draw a glyph on the screen at the given position pub fn blit(&mut self, _width: usize, _height: usize) {}
/// pub fn draw_filled_rect(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: Rgba64) {
/// # Arguments for y in y1..y2 {
/// * `x` - the x position of the glyph for x in x1..x2 {
/// * `y` - the y position of the glyph self.set_pixel(x, y, color);
/// * `glyph` - the glyph to draw
/// * `color` - the color of the glyph
pub fn draw_char(&mut self, mut x: u32, mut y: u32, character: char, color: Rgba64) {
// trace!["Judy Hopps is thicc af"];
// let mode = *VGAE.lock();
// trace!["She got them bouncy bunny buns"];
let basic_multingual_plane = FontRef::try_from_slice(include_bytes!(
"../../../ableos/assets/fonts/unifont-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 in_supp_plane = character as u32 > 0xffff;
let plane = match in_supp_plane {
false => basic_multingual_plane,
true => supplementary_multilingual_plane,
};
match character {
'\n' => {
// x = 0;
// y += (GLYPH_HEIGHT * FONT_SCALE) as u32;
}
_ => {
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),
);
// elf: I don't know if GLYPH_HEIGHT is in the right units here. I'm just guessing.
if x as usize > self.size.x {
x = 0;
y += (GLYPH_HEIGHT * FONT_SCALE) as u32;
} }
}
}
pub fn draw_unfilled_rect(
&mut self,
x1: usize,
y1: usize,
x2: usize,
y2: usize,
color: Rgba64,
) {
// x1 y1 => x2 y1 => x2 y2 => x1 y2 => x1 y1
self.draw_line(x1, y1, x2, y1, color);
self.draw_line(x2, y1, x2, y2, color);
self.draw_line(x2, y2, x1, y2, color);
self.draw_line(x1, y2, x1, y1, color);
}
if let Some(q) = plane.outline_glyph(q_glyph) { #[inline]
q.draw(|gx, gy, c| { pub fn draw_line(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, color: Rgba64) {
if c > 0.015 { let x = crate::graphics::get_coordinates(
let corner = q.px_bounds().min; x1.try_into().unwrap(),
self.set_pixel( y1.try_into().unwrap(),
gx as usize + corner.x as usize + x as usize, x2.try_into().unwrap(),
gy as usize + corner.y as usize + y as usize, y2.try_into().unwrap(),
color, );
);
} for coord in x {
}); self.set_pixel(coord.0, coord.1, color);
}
}
pub fn shade(&mut self) {
for y in 0..100 {
for x in 0..100 {
let rgba_ret = evaluate_shader(x, y, self.buff[y * self.size.x + x]);
match rgba_ret {
Ok(pixel) => {
// info!("{:?}", pixel);
self.set_pixel(x, y, pixel);
}
Err(err) => error!("{}", err),
}
} }
} }
}
} info!("Shaders done");
}
// TODO force clear
pub fn force_redraw(&mut self) {
use shadeable::pixel_format::into_vga_16;
VGAE.lock().clear_screen(into_vga_16(self.clear_color));
}
/// Draw a glyph on the screen at the given position
///
/// # Arguments
/// * `x` - the x position of the glyph
/// * `y` - the y position of the glyph
/// * `glyph` - the glyph to draw
/// * `color` - the color of the glyph
pub fn draw_char(&mut self, mut x: u32, mut y: u32, character: char, color: Rgba64) {
// trace!["Judy Hopps is thicc af"];
// let mode = *VGAE.lock();
// trace!["She got them bouncy bunny buns"];
let basic_multingual_plane = FontRef::try_from_slice(include_bytes!(
"../../../ableos/assets/fonts/unifont-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 in_supp_plane = character as u32 > 0xffff;
let plane = match in_supp_plane {
false => basic_multingual_plane,
true => supplementary_multilingual_plane,
};
match character {
'\n' => {
// x = 0;
// y += (GLYPH_HEIGHT * FONT_SCALE) as u32;
}
_ => {
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),
);
// elf: I don't know if GLYPH_HEIGHT is in the right units here. I'm just guessing.
if x as usize > self.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;
self.set_pixel(
gx as usize + corner.x as usize + x as usize,
gy as usize + corner.y as usize + y as usize,
color,
);
}
});
}
}
}
}
} }
pub trait VgaBuffer { pub trait VgaBuffer {
fn copy_to_buffer(&self) -> GraphicsReturn; fn copy_to_buffer(&self) -> GraphicsReturn;
} }
impl VgaBuffer for ScreenBuffer { impl VgaBuffer for ScreenBuffer {
fn copy_to_buffer(&self) -> GraphicsReturn { fn copy_to_buffer(&self) -> GraphicsReturn {
let mode = VGAE.lock(); let mode = VGAE.lock();
for y in 0..self.size.y { for y in 0..self.size.y {
for x in 0..self.size.x { for x in 0..self.size.x {
use shadeable::pixel_format::into_vga_16; use shadeable::pixel_format::into_vga_16;
// let vga_color = get_color16(self.buff[y * self.size.x + x]); let vga_color = into_vga_16(self.buff[y * self.size.x + x]);
let vga_color = into_vga_16(self.buff[y * self.size.x + x]); if into_vga_16(self.clear_color) != vga_color {
mode.set_pixel(x, y, vga_color);
if into_vga_16(self.clear_color) != vga_color { }
mode.set_pixel(x, y, vga_color);
} }
} }
}
GraphicsReturn::Ok GraphicsReturn::Ok
} }
} }
pub fn get_coordinates(x1: i32, y1: i32, x2: i32, y2: i32) -> Vec<(usize, usize)> { pub fn get_coordinates(x1: i32, y1: i32, x2: i32, y2: i32) -> Vec<(usize, usize)> {
let mut coordinates: Vec<(usize, usize)> = vec![]; let mut coordinates: Vec<(usize, usize)> = vec![];
let dx: i32 = i32::abs(x2 - x1); let dx: i32 = i32::abs(x2 - x1);
let dy: i32 = i32::abs(y2 - y1); let dy: i32 = i32::abs(y2 - y1);
let sx: i32 = { let sx: i32 = {
if x1 < x2 { if x1 < x2 {
1 1
} else { } else {
-1 -1
} }
}; };
let sy: i32 = { let sy: i32 = {
if y1 < y2 { if y1 < y2 {
1 1
} else { } else {
-1 -1
} }
}; };
let mut error: i32 = (if dx > dy { dx } else { -dy }) / 2; let mut error: i32 = (if dx > dy { dx } else { -dy }) / 2;
let mut current_x: i32 = x1; let mut current_x: i32 = x1;
let mut current_y: i32 = y1; let mut current_y: i32 = y1;
loop { loop {
coordinates.push((current_x as usize, current_y as usize)); coordinates.push((current_x as usize, current_y as usize));
// info!("0 {:?}", (current_x, current_y)); // info!("0 {:?}", (current_x, current_y));
if current_x == x2 && current_y == y2 { if current_x == x2 && current_y == y2 {
break; break;
} }
let error2: i32 = error; let error2: i32 = error;
if error2 > -dx { if error2 > -dx {
error -= dy; error -= dy;
current_x += sx; current_x += sx;
} }
if error2 < dy { if error2 < dy {
error += dx; error += dx;
current_y += sy; current_y += sy;
} }
} }
coordinates coordinates
} }

View file

@ -1,5 +1,11 @@
#![allow(clippy::empty_loop)] #![allow(clippy::empty_loop)]
use alloc::{format, string::String};
use shadeable::pixel_format::from_vga_16;
use vga::colors::Color16;
use crate::{ScreenBuffer, VgaBuffer, SCREEN_BUFFER};
// use crate::scheduler; // use crate::scheduler;
use { use {
@ -17,10 +23,7 @@ use {
relib::network::socket::Socket, relib::network::socket::Socket,
scheduler::SCHEDULER, scheduler::SCHEDULER,
}, },
alloc::{ alloc::{string::ToString, vec},
string::{String, ToString},
vec,
},
core::sync::atomic::{AtomicU64, Ordering::*}, core::sync::atomic::{AtomicU64, Ordering::*},
lazy_static::lazy_static, lazy_static::lazy_static,
log::*, log::*,
@ -57,30 +60,6 @@ pub fn kernel_main() -> ! {
use crate::proto_filetable::file::FileLocations; use crate::proto_filetable::file::FileLocations;
let mut file_table = FILE_TABLE.lock(); let mut file_table = FILE_TABLE.lock();
/*
let mut new_file = File::new(FileLocations::Bin, "test".to_string(), "txt".to_string());
new_file.write_bytes(b"Hello, world!");
file_table.add_file("test", new_file);
let file = file_table.get_file("test");
match file {
Some(file) => {
let file_bytes = &file.data_pointer;
let file_string = String::from_utf8(file_bytes.to_vec()).unwrap();
info!("{}", file_string);
}
None => {
info!("File not found");
}
}
*/
let mut new_file = File::new(FileLocations::Bin, "test".to_string(), "txt".to_string()); let mut new_file = File::new(FileLocations::Bin, "test".to_string(), "txt".to_string());
new_file.write_bytes(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]); new_file.write_bytes(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
@ -101,7 +80,15 @@ pub fn kernel_main() -> ! {
} }
} }
log_version_data(); let mut abcde = SCREEN_BUFFER.lock();
abcde.force_redraw();
abcde.draw_filled_circle(100, 100, 300, 0x0000ffff);
abcde.draw_unfilled_rect(100, 100, 400, 200, 0xff0000ff);
abcde.draw_filled_rect(300, 300, 400, 400, 0xff0000ff);
abcde.draw_line(100, 100, 400, 200, 0xff0000ff);
abcde.copy_to_buffer();
sloop() sloop()
} }

View file

@ -1,37 +0,0 @@
use crate::proc::PID;
#[repr(C)]
/// Signals that can be sent to a process
pub enum Signals {
/// Terminate the process
Terminate,
/// Shutdown the process and allow it to shutdown cleanly
Quit,
}
#[repr(C)]
pub enum SystemCall {
/// Sleep the calling process for the given number of milliseconds
///
/// # Arguments
///
/// * `ticks` - The number of ticks to sleep for
Sleep(u64),
/// Send a signal to a process
///
/// # Arguments
///
/// * `pid` - The PID of the process to send the signal to
/// * `signal` - The signal to send
SendSignal(PID, Signals),
}
#[no_mangle]
pub extern "C" fn syscall(call: SystemCall) {
// Handle the system call
match call {
SystemCall::Sleep(ms) => todo!("Sleep for {} ms", ms),
SystemCall::SendSignal(process_id, signal) => todo!(),
}
}

View file

@ -0,0 +1,40 @@
//! File system related system calls.
/// Temporary representation of a file path
pub type Path = *const u8;
/// Remove a Directory from the filesystem
///
/// # Arguments
/// * `full_path` - The full path of the directory to remove
/// * `force` - Whether to remove the directory even if it is not empty
#[no_mangle]
pub extern "C" fn remove_directory(path: Path, force_delete: bool) {
unimplemented!();
}
/// Create a new directory at the given path
///
/// # Arguments
/// * `full_path` - The full path of the directory to create
#[no_mangle]
pub extern "C" fn create_directory(path: Path) -> Result<(), FileErrors> {
unimplemented!();
}
#[repr(C)]
/// Errors that can occur when messing with files
pub enum FileErrors {
/// The directory can not be created
DirectoryCouldNotBeCreated,
/// The directory could not be removed
DirectoryCouldNotBeRemoved,
///
FileCouldNotBeCreated,
///
FileCouldNotBeRemoved,
/// The file could not be opened
FileCouldNotBeOpened,
///
FileCouldNotBeClosed,
}

View file

@ -0,0 +1,25 @@
#![deny(missing_docs)]
//! The module of syscalls.
use crate::proc::PID;
pub mod file_calls;
pub mod time_calls;
#[repr(C)]
/// Signals that can be sent to a process
pub enum Signals {
/// Terminate the process
Terminate,
/// Shutdown the process and allow it to shutdown cleanly
Quit,
}
/// Send a signal to a process
///
/// # Arguments
///
/// * `pid` - The PID of the process to send the signal to
/// * `signal` - The signal to send
#[no_mangle]
pub extern "C" fn send_signal(pid: PID, signal: Signals) {}

View file

@ -0,0 +1,28 @@
//! Time related system calls.
use core::panic;
/// Seconds and milliseconds since the Unix epoch.
#[repr(C)]
pub struct SecondsTime {
seconds: u64,
milliseconds: u64,
}
/// Sleep the calling process for the given number of milliseconds
#[no_mangle]
pub extern "C" fn sleep(time: SecondsTime) {
panic!("sleep is not implemented yet");
}
#[no_mangle]
/// Get the current time in seconds, milliseconds
pub extern "C" fn get_time() -> SecondsTime {
panic!("get_time not implemented");
}
#[no_mangle]
/// Set the current time in seconds, milliseconds
pub extern "C" fn set_time(time: SecondsTime) {
panic!("set_time not implemented");
}

View file

@ -3,7 +3,7 @@
//! This module contains the virtio device structural code. //! This module contains the virtio device structural code.
//! //!
//! # Notes //! # Notes
//! https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.html#x1-20001 //! <https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.html#x1-20001>
pub struct VirtioDevice { pub struct VirtioDevice {
status: VirtioDeviceStatus, status: VirtioDeviceStatus,

View file

@ -92,7 +92,7 @@ impl WasmProgram {
/// # Examples /// # Examples
/// ``` /// ```
/// use wasm_loader::wasm::WasmProgram; /// use wasm_loader::wasm::WasmProgram;
/// let wasm_program = WasmProgram::new_from_bytes(b"\0\0\0\0\1\0\0\0"); /// let wasm_program = WasmProgram::new_from_bytes(b"\0\0\0\01\0\0\0");
/// assert_eq!(wasm_program.is_valid(), (true, false)); /// assert_eq!(wasm_program.is_valid(), (true, false));
/// ``` /// ```
pub fn validate_header(self) -> (bool, bool) { pub fn validate_header(self) -> (bool, bool) {