From d4b7ee243f9ddda676d6a3fadaf51f9723414847 Mon Sep 17 00:00:00 2001 From: FantasyPvP <80643031+FantasyPvP@users.noreply.github.com> Date: Tue, 31 Oct 2023 18:40:30 +0000 Subject: [PATCH] e E --- src/system/kernel/serial.rs | 13 ++++++++++ src/system/std/io.rs | 9 +++++++ src/user/bin/calc/calc.rs | 6 +---- src/user/bin/calc/functions.rs | 47 +++++++++++++++++++++++++++++++--- src/user/bin/rickroll.rs | 2 +- src/user/bin/shell.rs | 6 ++++- src/user/lib/coords.rs | 2 +- 7 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/system/kernel/serial.rs b/src/system/kernel/serial.rs index de56cfd..ae74b9f 100644 --- a/src/system/kernel/serial.rs +++ b/src/system/kernel/serial.rs @@ -24,6 +24,19 @@ pub fn _print(args: core::fmt::Arguments) { }) } +pub fn serial_reply(chr: char) -> char { + use core::fmt::Write; + use x86_64::instructions::interrupts; + + let mut chr_return: char = 'X'; + + interrupts::without_interrupts(|| { + SERIAL1.lock().send(chr as u8); + chr_return = SERIAL1.lock().receive() as char ; + }); + + chr_return +} #[macro_export] macro_rules! serial_print { diff --git a/src/system/std/io.rs b/src/system/std/io.rs index 774b76f..d3d4ba5 100644 --- a/src/system/std/io.rs +++ b/src/system/std/io.rs @@ -8,6 +8,7 @@ use alloc::vec::Vec; pub use crate::{print, println, serial_print, serial_println}; pub use crate::kernel::render::Color; +use crate::kernel::serial::serial_reply; use lazy_static::lazy_static; use spin::Mutex; @@ -30,6 +31,14 @@ impl Stdin { } } +pub struct Serial {} + +impl Serial { + pub fn reply_char(c: char) -> char { + serial_reply(c) + } +} + pub struct Screen {} impl Screen { pub fn terminal_mode() { diff --git a/src/user/bin/calc/calc.rs b/src/user/bin/calc/calc.rs index 942494d..d776f79 100755 --- a/src/user/bin/calc/calc.rs +++ b/src/user/bin/calc/calc.rs @@ -126,11 +126,7 @@ impl Interpreter { }, Operator::Exp => { return Ok(Value::Number({ - let mut val = 1.0; - for _ in 0..(right as i64) { - val *= left - }; - val + super::functions::exp(left, right) })) } } diff --git a/src/user/bin/calc/functions.rs b/src/user/bin/calc/functions.rs index 55f4486..d07f0ec 100644 --- a/src/user/bin/calc/functions.rs +++ b/src/user/bin/calc/functions.rs @@ -2,6 +2,8 @@ use alloc::format; use alloc::string::String; use crate::println; +const PI: f64 = 3.14159265358979323846264338327950288419716939937510; + pub fn run_func(func: String, x: f64) -> Result { match func.as_str() { "sqrt" => sqrt(x), @@ -36,14 +38,51 @@ fn factorial(x: f64) -> Result { Ok((1..=x).fold(1, |a, b| a * b) as f64) } -fn sin(x: f64) -> Result { - Ok(libm::sin(x)) +fn cos(mut x: f64) -> Result { + while x > PI { + x -= PI; + } + + let res = 1.0 - trig_term(x, 2) + trig_term(x, 4) - trig_term(x, 6) + trig_term(x, 8) - trig_term(x, 10); + if res >= -1.0 && res <= 1.0 { + Ok(res) + } else { + panic!("something is very wrong with the cos function : {}", res); + } } -fn cos(x: f64) -> Result { - Ok(libm::cos(x)) +fn sin(mut x: f64) -> Result { + while x > PI { + x -= PI; + } + + + let res = x - trig_term(x, 3) + trig_term(x, 5) - trig_term(x, 7) + trig_term(x, 9) - trig_term(x, 11); + if res >= -1.0 && res <= 1.0 { + Ok(res) + } else { + panic!("something is very wrong with the sin function: {}", res); + } } fn tan(x: f64) -> Result { Ok(libm::tan(x)) +} + +pub fn exp(x: f64, y: f64) -> f64 { + let mut res = 1.0; + for _ in 0..(y as usize) { + res *= x + } + res +} + +fn trig_term(x: f64, y: usize) -> f64 { + let mut ex = 1.0; + for _ in 0..y { + ex *= x; + } + let fact = (1..=y).fold(1, |a, b| a*b); + + ex as f64 / fact as f64 } \ No newline at end of file diff --git a/src/user/bin/rickroll.rs b/src/user/bin/rickroll.rs index 3f6e05c..05c14aa 100644 --- a/src/user/bin/rickroll.rs +++ b/src/user/bin/rickroll.rs @@ -43,7 +43,7 @@ const RICKROLL2: &str = " 1a###w#*mObo*oatXkW*oo#*###p `--' `---' `----' `-----' | |-' `--'"; -use crate::{println}; +use crate::{println, serial_println}; use alloc::{string::String, boxed::Box, vec::Vec}; diff --git a/src/user/bin/shell.rs b/src/user/bin/shell.rs index 5a7593c..0e259a2 100644 --- a/src/user/bin/shell.rs +++ b/src/user/bin/shell.rs @@ -7,7 +7,7 @@ use alloc::{boxed::Box, string::{String, ToString}, vec, vec::Vec}; use vga::writers::{GraphicsWriter, PrimitiveDrawing}; use crate::{print, printerr, println, std, std::application::{Application, Error}, user::bin::*}; -use crate::std::io::{Color, write, Screen, Stdin}; +use crate::std::io::{Color, write, Screen, Stdin, Serial}; use crate::std::random::Random; use crate::user::bin::gigachad_detector::GigachadDetector; use crate::user::bin::grapher::Grapher; @@ -122,6 +122,10 @@ async fn exec() -> Result<(), Error> { let mut game = snake::Game::new(); game.run(args).await?; } + "serial" => { + let c = Serial::reply_char('e'); + println!("{}", c); + } "gameoflife" => { let mut game = gameoflife::GameOfLife::new(); game.run(Vec::new()).await?; diff --git a/src/user/lib/coords.rs b/src/user/lib/coords.rs index 678d83a..843b63d 100644 --- a/src/user/lib/coords.rs +++ b/src/user/lib/coords.rs @@ -62,7 +62,7 @@ impl Position { } - pub fn rotated_around(&self, angle: Direction, p: Position) -> Position { // rotates by an angle around a point + pub fn rotated_aroundte(&self, angle: Direction, p: Position) -> Position { // rotates by an angle around a point // gets coords relative to point to rotate around let mut p_offset = self.get_offset(&p);