1
0
Fork 0
forked from AbleOS/ableos
ableos_time/ableos/src/kmain.rs

287 lines
7.5 KiB
Rust
Raw Normal View History

2021-11-16 00:09:27 -06:00
#![allow(clippy::empty_loop)]
2021-11-28 14:50:14 -06:00
2021-12-24 03:30:27 -06:00
use {
crate::{
2022-01-22 00:01:16 -06:00
arch::{init, sloop},
boot_conf,
experiments::{
info::master,
systeminfo::{KERNEL_VERSION, RELEASE_TYPE},
},
2022-01-22 00:01:16 -06:00
graphics::{VgaBuffer, SCREEN_BUFFER},
2021-12-24 03:30:27 -06:00
relib::math::rand::RAND_HANDLE,
2022-01-16 19:42:11 -06:00
relib::network::socket::Socket,
2022-01-22 00:01:16 -06:00
relib::network::socket::{SimpleSock, SocketReturns},
2021-12-24 03:30:27 -06:00
scheduler::{test_fn, Thread, ThreadList},
2022-01-22 00:01:16 -06:00
vga_e::{self, num_to_vga16},
},
alloc::{
2022-01-22 00:01:16 -06:00
format,
string::{String, ToString},
vec,
2021-12-24 03:30:27 -06:00
},
2022-01-22 00:01:16 -06:00
core::sync::atomic::{AtomicU64, Ordering::*},
2021-12-24 03:30:27 -06:00
lazy_static::lazy_static,
2022-01-22 00:01:16 -06:00
log::*,
picorand::PicoRandGenerate,
rkyv::{ser::serializers::AllocSerializer, Deserialize},
shadeable::pixel_format::from_vga_16,
y_compositor_protocol::Version,
2021-11-16 00:09:27 -06:00
};
2022-01-13 08:54:33 -06:00
2021-11-16 00:09:27 -06:00
#[no_mangle]
#[allow(unconditional_recursion)]
pub extern "C" fn stack_overflow() -> u8 {
stack_overflow();
// meme number
2022-01-07 10:31:47 -06:00
69 // NOTE: Any specific reason for this number aside from memes?
2021-11-16 00:09:27 -06:00
}
lazy_static! {
2022-01-07 10:31:47 -06:00
pub static ref KEY_BUFFER: spin::Mutex<String> = spin::Mutex::new("".to_string());
2021-12-24 03:30:27 -06:00
pub static ref THREAD_LIST: spin::Mutex<ThreadList> = spin::Mutex::new(vec![]);
2022-01-22 00:01:16 -06:00
pub static ref TICK: AtomicU64 = AtomicU64::new(0);
2021-11-16 00:09:27 -06:00
}
2021-11-23 06:01:42 -06:00
2021-11-16 00:09:27 -06:00
#[no_mangle]
2021-11-23 06:01:42 -06:00
pub fn kernel_main() -> ! {
2022-01-22 00:01:16 -06:00
let boot_conf = boot_conf::BootConfig::new();
2021-11-16 00:09:27 -06:00
init::init();
2022-01-22 00:01:16 -06:00
log::set_max_level(boot_conf.log_level());
// info!("Initialized");
2022-01-07 10:31:47 -06:00
2022-01-22 00:01:16 -06:00
graphics_pipe_startup();
2022-01-16 19:42:11 -06:00
2022-01-22 00:01:16 -06:00
/*
2022-01-18 08:30:09 -06:00
2022-01-22 00:01:16 -06:00
{
let mut proc_1 = SimpleSock::new();
proc_1.register_protocol("ProcSpawner".to_string());
2021-12-24 13:00:20 -06:00
2022-01-22 00:01:16 -06:00
let version = 0;
use rkyv::ser::Serializer;
let mut serializer = AllocSerializer::<0>::default();
serializer.serialize_value(&version).unwrap();
2021-12-24 03:30:27 -06:00
2022-01-22 00:01:16 -06:00
let bytes = serializer.into_serializer().into_inner();
proc_1.write(bytes.into_vec());
unsafe {
let the_bytes = proc_1.read(3).unwrap();
let archived = rkyv::archived_root::<Version>(&the_bytes);
let deserialized: Version = archived.deserialize(&mut rkyv::Infallible).unwrap();
// trace!("{:?}", deserialized);
}
2022-01-13 08:54:33 -06:00
}
2022-01-22 00:01:16 -06:00
*/
2022-01-16 14:55:58 -06:00
2022-01-22 00:01:16 -06:00
let mut graphics = SCREEN_BUFFER.lock();
graphics.force_redraw();
graphics.clear();
2022-01-22 00:01:16 -06:00
graphics.draw_filled_circle(100, 100, 5, 0x9900ff00);
screen_writer_test();
2022-01-22 00:01:16 -06:00
graphics.copy_to_buffer();
2022-01-22 00:01:16 -06:00
if boot_conf.run_tests {
// quick and dirty testing framework
socket_test();
graphics_pipe_test();
socket_startup_rng();
socket_test_rng();
}
2022-01-22 00:01:16 -06:00
if boot_conf.run_demos {
graphics_api_demo();
2022-01-16 14:55:58 -06:00
}
2022-01-18 06:15:51 -06:00
2022-01-22 00:01:16 -06:00
if boot_conf.run_shader_tests {
shader_tests();
2022-01-16 19:42:11 -06:00
}
2022-01-22 00:01:16 -06:00
// graphics.copy_to_buffer();
2022-01-16 19:42:11 -06:00
2022-01-22 00:01:16 -06:00
// info!("buffer copied");
2022-01-13 08:54:33 -06:00
sloop()
2021-11-16 00:09:27 -06:00
}
2021-11-23 08:49:41 -06:00
2021-11-16 00:09:27 -06:00
/// called by arch specific timers to tick up all kernel related functions
pub fn tick() {
2022-01-22 00:01:16 -06:00
let mut data = TICK.load(Relaxed);
data += 1;
RAND_HANDLE.lock().seed_entropy_timer(data);
2022-01-13 08:54:33 -06:00
crate::kernel_state::KERNEL_STATE.lock().update_state();
2022-01-22 00:01:16 -06:00
TICK.store(data, Relaxed)
2021-11-16 00:09:27 -06:00
}
2021-11-23 08:49:41 -06:00
2021-11-16 00:09:27 -06:00
pub fn key_entropy(key: u8) {
RAND_HANDLE.lock().seed_entropy_keyboard(key);
}
2022-01-18 06:15:51 -06:00
pub fn test_threads() {
let mut a_thread = Thread::new();
a_thread.new_task(test_fn);
a_thread.new_task(test_fn);
THREAD_LIST.lock().push(a_thread);
}
2022-01-22 00:01:16 -06:00
pub fn cpu_socket_startup() {
let mut cpu_info_socket = SimpleSock::new();
cpu_info_socket.register_protocol("CPU_INFO".to_string());
2022-01-18 06:15:51 -06:00
2022-01-22 00:01:16 -06:00
let x = master().unwrap();
let xyz = x.brand_string().unwrap();
2022-01-18 06:15:51 -06:00
}
pub fn log_version_data() {
info!("{} v{}", RELEASE_TYPE, KERNEL_VERSION);
info!(
"Brand String: {:?}",
master().unwrap().brand_string().unwrap()
);
}
2022-01-22 00:01:16 -06:00
pub fn socket_test() {
let mut xyz = SimpleSock::new();
xyz.peek();
xyz.write(vec![0, 1, 2, 3]);
let x = "simple 🧦".to_string().into_bytes();
xyz.write(x);
info!("{:?}", &xyz.read(4).unwrap());
match &xyz.peek() {
SocketReturns::ReadOk(strr) => {
let out = String::from_utf8_lossy(strr);
info!("{}", out);
}
SocketReturns::ReadIndexOutOfBounds => todo!(),
SocketReturns::WriteOk => todo!(),
}
}
pub fn socket_test_rng() {
let mut test_sock = SimpleSock::grab_socket("RNGProvider".to_string()).unwrap();
info!(
"Recieving {} from protocol {}",
test_sock.read(1).unwrap()[0],
test_sock.protocol().unwrap()
);
}
pub fn socket_startup_rng() {
use picorand::{WyRand, RNG};
let mut random_socket = SimpleSock::new();
random_socket.register_protocol("RNGProvider".to_string());
let bruh = TICK.load(core::sync::atomic::Ordering::Relaxed);
let mut rng = RNG::<WyRand, u8>::new(bruh);
for _ in 0..512 {
let x = rng.generate();
random_socket.write(vec![x]);
}
}
pub fn screen_writer_test() {
let mut sock_print_id = SimpleSock::new();
sock_print_id.register_protocol("Screen Printer".to_string());
// sock_print_id.write(format!("a原 b画 cフ dァ eイ fル 集").into());
sock_print_id.write(format!("λ³ Half Life 3 booting up ㎣").into());
// sock_print_id.write(format!("Happy birthday 🎉").into());
// sock_print_id.write(format!("1....2....3....4....5....6....7....8....9").into());
let mut prev = None;
// let mut graphics = SCREEN_BUFFER.lock();
// graphics.clear();
// graphics.copy_to_buffer();
for current in (*String::from_utf8_lossy(&sock_print_id.peek().unwrap())).chars() {
vga_e::draw_char(prev, current, 0);
prev = Some(current);
}
}
pub fn vga_boot_screen() {
let mut abcde = SCREEN_BUFFER.lock();
for y in 0..480 {
for x in 0..640 {
let segment_x = x * 4 / 640;
let segment_y = y * 4 / 480;
let segment = segment_x + segment_y * 4;
abcde.set_pixel(x, y, from_vga_16(num_to_vga16(segment as u8)));
}
}
}
pub fn graphics_pipe_test() {
let mut graphics_pipe = SimpleSock::grab_socket("YCompositor".to_string()).unwrap();
let version = Version {
major: 13,
minor: 123,
patch: 0,
};
use rkyv::ser::Serializer;
let mut serializer = AllocSerializer::<0>::default();
serializer.serialize_value(&version).unwrap();
// let bytes = serializer.into_serializer().into_inner();
// info!("{:?}", bytes);
let bytes = serializer.into_serializer().into_inner();
graphics_pipe.write(bytes.into_vec());
unsafe {
let the_bytes = graphics_pipe.read(3).unwrap();
let archived = rkyv::archived_root::<Version>(&the_bytes);
let deserialized: Version = archived.deserialize(&mut rkyv::Infallible).unwrap();
trace!("{:?}", deserialized);
}
}
pub fn graphics_pipe_startup() {
let mut graphics_pipe = SimpleSock::new();
graphics_pipe.register_protocol("YCompositor".to_string());
}
pub fn graphics_api_demo() {
let mut abcde = SCREEN_BUFFER.lock();
abcde.force_redraw();
abcde.draw_filled_circle(100, 100, 300, 0x0000ff00);
abcde.draw_unfilled_rect(100, 100, 400, 200, 0xff000000);
abcde.draw_filled_rect(300, 300, 400, 400, 0xff000000);
abcde.draw_line(100, 100, 400, 200, 0xff000000);
abcde.copy_to_buffer();
}
pub fn shader_tests() {
let mut graphics = SCREEN_BUFFER.lock();
graphics.clear();
graphics.force_redraw();
graphics.draw_filled_rect(25, 25, 50, 50, 0xff000000);
graphics.set_pixel(50, 50, 0xffffff00);
graphics.shade();
graphics.copy_to_buffer();
// drop(graphics)
}