aboutsummaryrefslogtreecommitdiff
path: root/src/kernel.rs
blob: 4017b5a52be756def1d90b730a5f0329a7d75bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! # Kernel Code
//!
//! ## Initializes the peripherals
//!  - UART
//!  - Allocators

#![doc(
    html_logo_url = "https://raw.githubusercontent.com/rust-embedded/wg/master/assets/logo/ewg-logo-blue-white-on-transparent.png"
)]
#![allow(non_snake_case)]
#![allow(clippy::upper_case_acronyms, dead_code)]
#![feature(format_args_nl)]
#![feature(panic_info_message)]
#![feature(trait_alias)]
#![feature(exclusive_range_pattern)]
#![feature(default_alloc_error_handler)]
#![feature(optimize_attribute)]
#![feature(naked_functions)]
#![no_main]
#![no_std]

mod bsp;
mod cpu;
mod lib;
mod util;
use crate::lib::console::console;
use crate::util::mem::alloc::allocator;
use crate::util::mem::{format, Box, String};
use core::sync::atomic::AtomicBool;
use core::sync::atomic::Ordering;

/// # Initialization Code
///
/// Initializes
///  - Allocators
///    - Box
///    - String
///    - format!
///  - UART
///    - print!
///    - serial_println!
///    - vprint!
///    - vserial_println!
///  - MMU
///    - SpinLocks
///
/// After initialization, jump to
/// the regular main.
unsafe fn kernel_init() -> ! {
    util::mem::mmu_init();
    console().init().unwrap();
    allocator().init().unwrap();
    INITIALIZED_BOOL.store(true, Ordering::Release);
    kernel_main()
}

/// # Post-initialization
///
/// TODO: Figure out what to do here
fn kernel_main() -> ! {
    #[cfg(not(feature = "verbose"))]
    {
        #[cfg(feature = "flag")]
        {
            draw::draw_ukraine_flag();
            serial_println!();
            draw::draw_american_flag();
            serial_println!();
        }

        serial_println!(
            "\x1b[91mInitialized\x1b[0m \x1b[92m{}\x1b[0m \x1b[93mv{}\x1b[0m",
            env!("CARGO_PKG_NAME"),
            env!("CARGO_PKG_VERSION")
        );
        serial_println!(
            "\x1b[94mAuthors:\x1b[0m \x1b[95m{}\x1b[0m",
            env!("CARGO_PKG_AUTHORS")
        );
        use crate::lib::console::interface::Statistics;
        serial_println!(
            "Characters written to UART: \x1b[91m{}\x1b[0m",
            console().chars_written()
        );
    }

    #[cfg(feature = "verbose")]
    run_verbose();

    loop {}
}

fn run_verbose() {
    serial_println!("U8: {:?}", util::mem::alloc::U8_GRAND_ALLOC);
    {
        let mut s = String::new();
        for _ in 0..128 {
            s += "TEST";
        }
        assert_eq!(s.capacity(), 512);
    }
    {
        let s = format!("{:X}", 0xCAFEBABE as u32);
        assert_eq!(s, "CAFEBABE");
        assert_eq!(s.capacity(), 8);
    }
    {
        let a: Box<u8> = Box::new(1);
        assert_eq!(*a, 1);
        serial_println!("{}", a);
    }
    {
        let a: Box<u8> = Box::new(2);
        let b: Box<u8> = Box::new(3);
        let c: Box<u8> = Box::new(4);
        assert_eq!(*a, 2);
        assert_eq!(*b, 3);
        assert_eq!(*c, 4);
        serial_println!("{} {} {}", a, b, c);
    }
    {
        let a: Box<u8> = Box::new(5);
        let b: Box<u8> = Box::new(6);
        let c: Box<u8> = Box::new(7);
        assert_eq!(*a, 5);
        assert_eq!(*b, 6);
        assert_eq!(*c, 7);
        serial_println!("{} {} {}", a, b, c);
    }
    use crate::lib::console::interface::Statistics;
    serial_println!(
        "Characters written to UART: \x1b[91m{}\x1b[0m",
        console().chars_written()
    );
}

#[no_mangle]
/// # SVC Handler
pub fn svc(code: u32) {
    match code {
        _ => {
            serial_println!("Unhandled Service Call!");
        }
    }
}

/// # Static for other cores
pub static INITIALIZED_BOOL: AtomicBool = AtomicBool::new(false);