This commit is contained in:
2025-12-20 17:23:40 +01:00
parent b5d3417572
commit 78c4cdc684
12 changed files with 135 additions and 123 deletions

12
Cargo.lock generated
View File

@@ -2,10 +2,6 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "NovaError"
version = "0.1.0"
[[package]]
name = "cfg-if"
version = "1.0.4"
@@ -28,7 +24,7 @@ dependencies = [
name = "heap"
version = "0.1.0"
dependencies = [
"NovaError",
"nova_error",
"rand",
]
@@ -48,11 +44,15 @@ checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
name = "nova"
version = "0.1.0"
dependencies = [
"NovaError",
"heap",
"libm",
"nova_error",
]
[[package]]
name = "nova_error"
version = "0.1.0"
[[package]]
name = "ppv-lite86"
version = "0.2.21"

View File

@@ -15,10 +15,10 @@ panic = "abort"
[dependencies]
libm = "0.2.15"
heap = {path = "heap"}
NovaError = {path = "NovaError"}
nova_error = {path = "nova_error"}
[workspace]
members = [ "NovaError",
members = [ "nova_error",
"heap"
]

View File

@@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
NovaError = {path = "../NovaError"}
nova_error = {path = "../nova_error"}
[dev-dependencies]
rand = "0.9.2"

View File

@@ -9,7 +9,7 @@ use core::{
result::Result,
};
use NovaError::NovaError;
use nova_error::NovaError;
extern crate alloc;
@@ -33,8 +33,8 @@ pub struct Heap {
impl Heap {
pub const fn empty() -> Self {
Self {
start_address: null_mut() as *mut HeapHeader,
end_address: null_mut() as *mut HeapHeader,
start_address: null_mut(),
end_address: null_mut(),
raw_size: 0,
}
}
@@ -60,11 +60,13 @@ impl Heap {
unsafe fn find_first_fit(&self, size: usize) -> Result<*mut HeapHeader, NovaError> {
let mut current = self.start_address;
while !fits(size, current) {
if let Some(next) = (*current).next {
current = next;
} else {
return Err(NovaError::HeapFull);
unsafe {
while !fits(size, current) {
if let Some(next) = (*current).next {
current = next;
} else {
return Err(NovaError::HeapFull);
}
}
}
Ok(current)
@@ -106,23 +108,22 @@ impl Heap {
let new_address = unsafe { current.byte_add(byte_offset) };
// Handle case where fragmenting center free space
let next = (*current).next;
if let Some(next) = next {
(*next).before = Some(new_address);
}
unsafe {
let next = (*current).next;
if let Some(next) = next {
(*next).before = Some(new_address);
}
ptr::write(
new_address as *mut HeapHeader,
new_address,
HeapHeader {
next,
before: Some(current),
size: (*current).size - byte_offset,
free: true,
},
)
};
unsafe {
);
(*current).next = Some(new_address);
(*current).free = false;
(*current).size = size;
@@ -135,21 +136,21 @@ impl Heap {
// IF prev is free:
// Delete header, add size to previous and fix pointers.
// Move Head left
if let Some(before_head) = (*segment).before {
if (*before_head).free {
(*before_head).size += (*segment).size + HEAP_HEADER_SIZE;
delete_header(segment);
segment = before_head;
}
if let Some(before_head) = (*segment).before
&& (*before_head).free
{
(*before_head).size += (*segment).size + HEAP_HEADER_SIZE;
delete_header(segment);
segment = before_head;
}
// IF next is free:
// Delete next header and merge size, fix pointers
if let Some(next_head) = (*segment).next {
if (*next_head).free {
(*segment).size += (*next_head).size + HEAP_HEADER_SIZE;
delete_header(next_head);
}
if let Some(next_head) = (*segment).next
&& (*next_head).free
{
(*segment).size += (*next_head).size + HEAP_HEADER_SIZE;
delete_header(next_head);
}
// Neither: Set free
@@ -177,19 +178,21 @@ unsafe impl GlobalAlloc for Heap {
unsafe impl Sync for Heap {}
unsafe fn fits(size: usize, header: *mut HeapHeader) -> bool {
(*header).free && size <= (*header).size
unsafe { (*header).free && size <= (*header).size }
}
unsafe fn delete_header(header: *mut HeapHeader) {
let before_opt = (*header).before;
let next_opt = (*header).next;
unsafe {
let before_opt = (*header).before;
let next_opt = (*header).next;
if let Some(before) = before_opt {
(*before).next = next_opt;
}
if let Some(before) = before_opt {
(*before).next = next_opt;
}
if let Some(next) = next_opt {
(*next).before = before_opt;
if let Some(next) = next_opt {
(*next).before = before_opt;
}
}
}

View File

@@ -1,4 +1,4 @@
[package]
name = "NovaError"
name = "nova_error"
version = "0.1.0"
edition = "2024"

View File

@@ -1,9 +1,10 @@
static SCTLR_EL1_MMU_DISABLED: u64 = 0 << 0; //M
static SCTLR_EL1_MMU_DISABLED: u64 = 0; //M
static SCTLR_EL1_DATA_CACHE_DISABLED: u64 = 0 << 2; //C
static SCTLR_EL1_INSTRUCTION_CACHE_DISABLED: u64 = 0 << 12; //I
static SCTLR_EL1_LITTLE_ENDIAN_EL0: u64 = 0 << 24; //E0E
static SCTLR_EL1_LITTLE_ENDIAN_EL1: u64 = 0 << 25; //EE
#[allow(clippy::identity_op)]
static SCTLR_EL1_RES: u64 = (0 << 6) | (1 << 11) | (0 << 17) | (1 << 20) | (1 << 22); //Res0 & Res1
#[no_mangle]

View File

@@ -16,6 +16,7 @@ const SET_PIXEL_ORDER: u32 = 0x0004_8006;
const GET_PITCH: u32 = 0x000_40008;
const SET_FB_OFFSET: u32 = 0x0004_8009;
#[allow(dead_code)]
pub struct FrameBuffer {
pixel_depth: u32, // Bits per pixel
pitch: u32, // Pixel per row
@@ -31,74 +32,6 @@ pub const ORANGE: u32 = 0x00FFA500;
pub const YELLOW: u32 = 0x00FFFF00;
impl FrameBuffer {
pub fn new() -> Self {
let mut mailbox = Mailbox([0; 36]);
mailbox.0[0] = 35 * 4;
mailbox.0[1] = 0;
mailbox.0[2] = SET_PHYSICAL_DISPLAY_WH;
mailbox.0[3] = 8;
mailbox.0[4] = 8;
mailbox.0[5] = 1920;
mailbox.0[6] = 1080;
mailbox.0[7] = SET_VIRTUAL_DISPLAY_WH;
mailbox.0[8] = 8;
mailbox.0[9] = 8;
mailbox.0[10] = 1920;
mailbox.0[11] = 1080;
mailbox.0[12] = SET_PIXEL_DEPTH;
mailbox.0[13] = 4;
mailbox.0[14] = 4;
mailbox.0[15] = 32; // 32 bit per pixel
mailbox.0[16] = SET_PIXEL_ORDER;
mailbox.0[17] = 4;
mailbox.0[18] = 4;
mailbox.0[19] = 0x0; // RGB
mailbox.0[20] = SET_FB_OFFSET;
mailbox.0[21] = 8;
mailbox.0[22] = 8;
mailbox.0[23] = 0; // X in pixels
mailbox.0[24] = 0; // Y in pixels
mailbox.0[25] = ALLOCATE_BUFFER;
mailbox.0[26] = 8;
mailbox.0[27] = 4;
mailbox.0[28] = 4096; // Alignment
mailbox.0[29] = 0;
mailbox.0[30] = GET_PITCH;
mailbox.0[31] = 4;
mailbox.0[32] = 0;
mailbox.0[33] = 0;
mailbox.0[34] = 0; // End tag
// TODO: validate responses
let addr = core::ptr::addr_of!(mailbox.0[0]) as u32;
write_mailbox(8, addr);
let _ = read_mailbox(8);
if mailbox.0[1] == 0 {
println!("Failed");
}
mailbox.0[28] &= 0x3FFFFFFF;
Self {
pixel_depth: mailbox.0[15],
pitch: mailbox.0[33] / (mailbox.0[15] / 8),
rows: mailbox.0[29] / mailbox.0[33],
start_addr: mailbox.0[28] as *mut u32,
size: mailbox.0[29],
}
}
pub fn draw_pixel(&self, x: u32, y: u32, color: u32) {
let offset = x + y * self.pitch;
unsafe {
@@ -109,6 +42,7 @@ impl FrameBuffer {
/*Bresenham's line algorithm
TODO: check if its possible to optimize y1==y2 case (ARM neon?)
*/
#[allow(clippy::collapsible_else_if)]
pub fn draw_line(&self, x1: u32, y1: u32, x2: u32, y2: u32, color: u32) {
if x1 == x2 {
for y in y1..=y2 {
@@ -218,7 +152,7 @@ impl FrameBuffer {
}
fn draw_ascii(&self, x: u32, y: u32, char: usize, scale: u32, color: u32) {
for (y_offset, row) in (&BASIC_LEGACY[char]).iter().enumerate() {
for (y_offset, row) in BASIC_LEGACY[char].iter().enumerate() {
for bit in 0..8 {
match row & (1 << bit) {
0 => {}
@@ -241,3 +175,73 @@ impl FrameBuffer {
}
}
}
impl Default for FrameBuffer {
fn default() -> Self {
let mut mailbox = Mailbox([0; 36]);
mailbox.0[0] = 35 * 4;
mailbox.0[1] = 0;
mailbox.0[2] = SET_PHYSICAL_DISPLAY_WH;
mailbox.0[3] = 8;
mailbox.0[4] = 8;
mailbox.0[5] = 1920;
mailbox.0[6] = 1080;
mailbox.0[7] = SET_VIRTUAL_DISPLAY_WH;
mailbox.0[8] = 8;
mailbox.0[9] = 8;
mailbox.0[10] = 1920;
mailbox.0[11] = 1080;
mailbox.0[12] = SET_PIXEL_DEPTH;
mailbox.0[13] = 4;
mailbox.0[14] = 4;
mailbox.0[15] = 32; // 32 bit per pixel
mailbox.0[16] = SET_PIXEL_ORDER;
mailbox.0[17] = 4;
mailbox.0[18] = 4;
mailbox.0[19] = 0x0; // RGB
mailbox.0[20] = SET_FB_OFFSET;
mailbox.0[21] = 8;
mailbox.0[22] = 8;
mailbox.0[23] = 0; // X in pixels
mailbox.0[24] = 0; // Y in pixels
mailbox.0[25] = ALLOCATE_BUFFER;
mailbox.0[26] = 8;
mailbox.0[27] = 4;
mailbox.0[28] = 4096; // Alignment
mailbox.0[29] = 0;
mailbox.0[30] = GET_PITCH;
mailbox.0[31] = 4;
mailbox.0[32] = 0;
mailbox.0[33] = 0;
mailbox.0[34] = 0; // End tag
// TODO: validate responses
let addr = core::ptr::addr_of!(mailbox.0[0]) as u32;
write_mailbox(8, addr);
let _ = read_mailbox(8);
if mailbox.0[1] == 0 {
println!("Failed");
}
mailbox.0[28] &= 0x3FFFFFFF;
Self {
pixel_depth: mailbox.0[15],
pitch: mailbox.0[33] / (mailbox.0[15] / 8),
rows: mailbox.0[29] / mailbox.0[33],
start_addr: mailbox.0[28] as *mut u32,
size: mailbox.0[29],
}
}
}

View File

@@ -54,7 +54,7 @@ fn esr_uart_dump() {
let esr: u32;
unsafe {
asm!(
"mrs {esr}, ESR_EL1",
"mrs {esr:x}, ESR_EL1",
esr = out(reg) esr
);
}
@@ -80,6 +80,7 @@ fn handle_gpio_interrupt() {
let val = read_gpio_event_detect_status(i);
if val {
#[allow(clippy::single_match)]
match i {
26 => print!("Button Pressed"),
_ => {}

View File

@@ -1,5 +1,5 @@
#![no_std]
#![allow(clippy::missing_safety_doc)]
use core::{
panic::PanicInfo,
ptr::{read_volatile, write_volatile},
@@ -19,7 +19,8 @@ pub unsafe fn init_heap() {
let start = core::ptr::addr_of_mut!(__heap_start) as usize;
let end = core::ptr::addr_of_mut!(__heap_end) as usize;
GLOBAL_ALLOCATOR.init(start, end);
let heap = core::ptr::addr_of_mut!(GLOBAL_ALLOCATOR);
(*heap).init(start, end);
}
#[panic_handler]

View File

@@ -1,9 +1,10 @@
use crate::{mmio_read, mmio_write};
use nova_error::NovaError;
const MBOX_BASE: u32 = 0x3F00_0000 + 0xB880;
// MB0
const MBOX_READ: u32 = MBOX_BASE + 0x00;
const MBOX_READ: u32 = MBOX_BASE;
const MBOX_STATUS: u32 = MBOX_BASE + 0x18;
// MB1
@@ -29,7 +30,7 @@ macro_rules! mailbox_command {
/// More information at: https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
pub fn $name(
request_data: [u32; $request_len / 4],
) -> Result<[u32; $response_len / 4], NovaError::NovaError> {
) -> Result<[u32; $response_len / 4], NovaError> {
let mut mailbox =
[0u32; (HEADER_LENGTH + max!($request_len, $response_len) + FOOTER_LENGTH) / 4];
mailbox[0] = (HEADER_LENGTH + max!($request_len, $response_len) + FOOTER_LENGTH) as u32; // Total length in Bytes
@@ -48,7 +49,7 @@ macro_rules! mailbox_command {
let _ = read_mailbox(8);
if mailbox[1] == 0 {
return Err(NovaError::NovaError::Mailbox);
return Err(NovaError::Mailbox);
}
let mut out = [0u32; $response_len / 4];

View File

@@ -2,6 +2,7 @@
#![no_std]
#![feature(asm_experimental_arch)]
#![allow(static_mut_refs)]
#![allow(clippy::missing_safety_doc)]
use core::{
arch::{asm, global_asm},
ptr::write_volatile,
@@ -94,7 +95,7 @@ pub extern "C" fn kernel_main() -> ! {
gpio_pull_up(26);
set_falling_edge_detect(26, true);
let fb = FrameBuffer::new();
let fb = FrameBuffer::default();
fb.draw_square(500, 500, 600, 700, RED);
fb.draw_square_fill(800, 800, 900, 900, GREEN);