Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
remove unnecessary unsafes
Browse files Browse the repository at this point in the history
  • Loading branch information
u8cat committed Jul 6, 2022
1 parent 32d1ef9 commit 2002ed4
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 148 deletions.
2 changes: 1 addition & 1 deletion example/sudoku/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ authors = [
rkgpu = { path = "../../lib/rkgpu" }
rkplat = { path = "../../lib/rkplat", features = ["has_smp", "driver_virtio_all", "driver_ns16550", "driver_goldfish_rtc"] }
rkalloc = { path = "../../lib/rkalloc" }
rkboot = { path = "../../lib/rkboot", features = ["alloc_buddy", "sched_coop"] }
rkboot = { path = "../../lib/rkboot", features = ["alloc_buddy", "sched_preem"] }
rkswrand = { path = "../../lib/rkswrand" }
rksched = { path = "../../lib/rksched" }
rklock = { path = "../../lib/rklock" }
Expand Down
10 changes: 4 additions & 6 deletions example/sudoku/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@
// POSSIBILITY OF SUCH DAMAGE.

use core::cmp::{max, min};
use rkplat::drivers::virtio::__GPU_DEIVCE;
use crate::DIRECTION::*;
use crate::*;
use rkgpu::*;


pub fn update_cursor(start_x: u32, start_y: u32, is_init: bool) {
unsafe {
let (width, height) = __GPU_DEIVCE.as_mut().unwrap().resolution();
let (width, height) = rkgpu::resolution();
if !is_init {
for i in 1..(FB_CURSOR[0] + 1) as usize {
FB[FB_CURSOR[5 * i - 4] as usize + 2] = FB_CURSOR[5 * i - 4 + 1] as u8;
Expand Down Expand Up @@ -81,14 +79,14 @@ pub fn update_cursor(start_x: u32, start_y: u32, is_init: bool) {
FB_CURSOR[0] = (idx_cursor / 5) as u32;
draw_line(Horizontal, (max(10, start_x) - 10) as u32, (max(1, start_y) - 1) as u32, min(21, start_x + 10), BLACK, 255, 3);
draw_line(Vertical, (max(1, start_x) - 1) as u32, (max(10, start_y) - 10) as u32, min(21, start_y + 10), BLACK, 255, 3);
__GPU_DEIVCE.as_mut().unwrap().flush().expect("failed to flush");
rkgpu::screen_flush();
}
}

pub unsafe fn draw_select(start_x: u32, start_y: u32, color: Color) {
pub fn draw_select(start_x: u32, start_y: u32, color: Color) {
draw_line(Horizontal, start_x + 5, start_y + 5, 65, color, 255, 1);
draw_line(Horizontal, start_x + 5, start_y + 70, 65, color, 255, 1);
draw_line(Vertical, start_x + 5, start_y + 5, 65, color, 255, 1);
draw_line(Vertical, start_x + 70, start_y + 5, 65, color, 255, 1);
__GPU_DEIVCE.as_mut().unwrap().flush().expect("failed to flush");
super::screen_flush();
}
184 changes: 81 additions & 103 deletions example/sudoku/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,16 @@

#![no_std]
#![no_main]
#![allow(unused)]
#![allow(non_upper_case_globals)]
extern crate rkboot;
extern crate alloc;

use rkplat::time::wall_clock;
use rkgpu::*;
use rkswrand::fast_random;

use rksched::*;
use core::time::Duration;
use core::ptr::null_mut;
use rklock::*;
use rktimeconv::TimePoint;

pub mod key;
pub mod input;
Expand All @@ -57,6 +53,7 @@ pub use cursor::*;
pub use output::*;
use rkplat::println;

#[allow(non_upper_case_globals)]
static mutex: Semaphore = Semaphore::new(0);

pub struct Sudoku {
Expand Down Expand Up @@ -188,83 +185,68 @@ pub fn if_fit_check(map: &[[usize; 9]; 9], x: usize, y: usize, number: usize, if
* 返回 true 为有解,返回 false 为无解
*/

static mut timepoint: Duration = Duration::new(0, 0);

pub fn sudoku_solve(map: &mut [[usize; 9]; 9], row: usize, col: usize, depth: usize, is_init: bool) -> (bool, bool) {
unsafe {
let mut nextrow: usize = 0;
let mut nextcol: usize = 0;
if is_init {
if depth > 81 {
//println!("Out of depth!");
return (false, true);
let mut nextrow: usize = 0;
let mut nextcol: usize = 0;
if is_init {
if depth > 81 {
//println!("Out of depth!");
return (false, true);
}
let mut number = 0;
loop {
number += 1;
if number >= 10 {
break;
}
let mut number = 0;
loop {
number += 1;
if number >= 10 {
break;
}
if !(if_fit_check(map, row, col, number, false)) {
continue;
}
map[row][col] = number;
if !(findnext_empty(map, row, &mut nextrow, &mut nextcol)) {
// 没有空位了,数独求解完成
return (true, true);
}
let result = sudoku_solve(map, nextrow, nextcol, depth + 1, is_init);
if !result.0 {
map[row][col] = 0;
continue;
} else {
return (true, true);
}
if !(if_fit_check(map, row, col, number, false)) {
continue;
}
map[row][col] = number;
if !(findnext_empty(map, row, &mut nextrow, &mut nextcol)) {
// 没有空位了,数独求解完成
return (true, true);
}
(false, true)
} else {
let mut current_time: Duration = Duration::new(0, 0);
if depth == 0 {
timepoint = wall_clock();
let result = sudoku_solve(map, nextrow, nextcol, depth + 1, is_init);
if !result.0 {
map[row][col] = 0;
continue;
} else {
current_time = wall_clock();
if current_time.as_secs() > timepoint.as_secs() + 1 {
timepoint = Duration::new(0, 0);
return (false, false);
}
return (true, true);
}
}
(false, true)
} else {
if depth > 81 {
return (false, true);
}
let mut number = 0;
loop {
number += 1;
if number >= 10 {
break;
}
if depth > 81 {
//println!("Out of depth!");
return (false, true);
if !(if_fit_check(map, row, col, number, false)) {
continue;
}
let mut number = 0;
loop {
number += 1;
if number >= 10 {
break;
}
if !(if_fit_check(map, row, col, number, false)) {
continue;
}
map[row][col] = number;
if !(findnext_empty(map, row, &mut nextrow, &mut nextcol)) {
// 没有空位了,数独求解完成
return (true, true);
}
let result = sudoku_solve(map, nextrow, nextcol, depth + 1, is_init);
map[row][col] = number;
if !(findnext_empty(map, row, &mut nextrow, &mut nextcol)) {
// 没有空位了,数独求解完成
return (true, true);
}
let result = sudoku_solve(map, nextrow, nextcol, depth + 1, is_init);

if !result.1 {
map[row][col] = 0;
return (false, false);
} else if !result.0 {
map[row][col] = 0;
continue;
} else {
return (true, true);
}
if !result.1 {
map[row][col] = 0;
return (false, false);
} else if !result.0 {
map[row][col] = 0;
continue;
} else {
return (true, true);
}
(false, true)
}
(false, true)
}
}

Expand Down Expand Up @@ -318,15 +300,15 @@ pub fn add_num(map: &mut [[usize; 9]; 9], row: usize, col: usize, num: usize, if
return false;
}
if map[row][col] != 0 {
unsafe { mutex.signal(); }
mutex.signal();
return false;
}
if num > 9 || num <= 0 {
unsafe { mutex.signal(); }
mutex.signal();
return false;
}
if ifcheck && !(if_fit_check(map, row, col, num, false)) {
unsafe { mutex.signal(); }
mutex.signal();
return false;
}

Expand Down Expand Up @@ -405,47 +387,43 @@ pub fn hint(map: &mut [[usize; 9]; 9]) -> bool {
return false;
}

unsafe { show_sudoku_number(nextrow as u8, nextcol as u8, map_allzero[nextrow][nextcol] as u8, TAN); }
show_sudoku_number(nextrow as u8, nextcol as u8, map_allzero[nextrow][nextcol] as u8, TAN);

return true;
}

pub fn error_hinter(_null: *mut u8) {
unsafe {
loop {
mutex.wait();
printg("You can't write this number HERE!", 700, 500, RED, 255, 2);
rksched::this_thread::sleep_for(Duration::from_secs(1));
printg(" ", 700, 500, RED, 255, 2);
}
loop {
mutex.wait();
printg("You can't write this number HERE!", 700, 500, RED, 255, 2);
rksched::this_thread::sleep_for(Duration::from_secs(1));
printg(" ", 700, 500, RED, 255, 2);
}
}

fn init(sudoku: &mut Sudoku) {
unsafe {
sched::create_thread("", rkalloc::get_default().unwrap(), thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(), input_tracer, null_mut()).unwrap();

sched::create_thread("", rkalloc::get_default().unwrap(), thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(), error_hinter, null_mut()).unwrap();

sched::create_thread("", rkalloc::get_default().unwrap(), thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(), show_time, null_mut()).unwrap();
rkgpu::init();
printg("Hello, world!\nHello, OSH-2022!\nHello, Runikraft!\n", 700, 10, RED, 255, 4);
printg("Use W, A, S, and D to move selecting rectangle.\nUse up, left, down, and right to move cursor.\nUse H for hint, use O for solution.", 0, 700, BLACK, 255, 2);
update_cursor(900, 400, true);
draw_select(0, 0, RED);
draw_sudoku_lattices(PURPLE, BLACK);
screen_flush();
row_random(&mut sudoku.map, 0);
sudoku_solve(&mut sudoku.map, 1, 1, 0, true);
hole_dig(&mut sudoku.map, 15, &mut sudoku.tag);
sudoku.map_print();
println!("Hello sudoku!\n");
}
unsafe {rkgpu::init();}
sched::create_thread("", rkalloc::get_default().unwrap(), thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(), input_tracer, null_mut()).unwrap();

sched::create_thread("", rkalloc::get_default().unwrap(), thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(), error_hinter, null_mut()).unwrap();

sched::create_thread("", rkalloc::get_default().unwrap(), thread::ThreadAttr::default(), rksched::thread::ThreadLimit::default(), show_time, null_mut()).unwrap();

printg("Hello, world!\nHello, OSH-2022!\nHello, Runikraft!\n", 700, 10, RED, 255, 4);
printg("Use W, A, S, and D to move selecting rectangle.\nUse up, left, down, and right to move cursor.\nUse H for hint, use O for solution.", 0, 700, BLACK, 255, 2);
update_cursor(900, 400, true);
draw_select(0, 0, RED);
draw_sudoku_lattices(PURPLE, BLACK);
screen_flush();
row_random(&mut sudoku.map, 0);
sudoku_solve(&mut sudoku.map, 1, 1, 0, true);
hole_dig(&mut sudoku.map, 15, &mut sudoku.tag);
sudoku.map_print();
println!("Hello sudoku!\n");
}

#[no_mangle]
fn main() {
//println!("sudoku main\n");
let mut sudoku: Sudoku = sudoku_init_zero();
init(&mut sudoku);
unsafe {
Expand Down
21 changes: 9 additions & 12 deletions example/sudoku/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
// POSSIBILITY OF SUCH DAMAGE.

use core::time::Duration;
use rkplat::drivers::virtio::__GPU_DEIVCE;
use rkgpu::*;
use rkplat::time::wall_clock;
use rktimeconv::*;
use crate::{mutex, Sudoku};

pub unsafe fn draw_sudoku_lattices(color0: Color, color1: Color) -> u8 {
let (width, height) = __GPU_DEIVCE.as_mut().unwrap().resolution();
pub fn draw_sudoku_lattices(color0: Color, color1: Color) -> u8 {
let (width, height) = super::resolution();
if width >= 750 && height >= 750 {
for x in 0..10 {
if x % 3 == 0 {
Expand All @@ -58,7 +57,7 @@ pub unsafe fn draw_sudoku_lattices(color0: Color, color1: Color) -> u8 {
} else { 0 }
}

pub unsafe fn show_sudoku_number(pos_x: u8, pos_y: u8, number: u8, color: Color) -> u8 {
pub fn show_sudoku_number(pos_x: u8, pos_y: u8, number: u8, color: Color) -> u8 {
if pos_x <= 8 && pos_y <= 8 {
let start_x: u32 = 75 * pos_x as u32 + 20;
let start_y: u32 = 75 * pos_y as u32 + 6;
Expand All @@ -84,20 +83,18 @@ pub fn show_time(_null: *mut u8) {
}

pub fn error_hinter(_null: *mut u8) {
unsafe {
loop {
mutex.wait();
printg("You can't write this number HERE!", 700, 500, RED, 255, 2);
rksched::this_thread::sleep_for(Duration::from_secs(1));
printg(" ", 700, 500, RED, 255, 2);
}
loop {
mutex.wait();
printg("You can't write this number HERE!", 700, 500, RED, 255, 2);
rksched::this_thread::sleep_for(Duration::from_secs(1));
printg(" ", 700, 500, RED, 255, 2);
}
}


impl Sudoku {
// 打印当前数独信息
pub unsafe fn map_print(&self) {
pub fn map_print(&self) {
for i in 0..9 {
for j in 0..9 {
// show_sudoku_number(pos_x: u8, pos_y: u8, number: u8);
Expand Down
8 changes: 7 additions & 1 deletion lib/rkgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,20 @@ pub static mut FB: &mut [u8] = unsafe { &mut _EMPTY };
pub static mut FB_CURSOR: &mut [u32] = &mut [0; 1000];

pub unsafe fn init() {
let flag = rkplat::lcpu::save_irqf();
FB = __GPU_DEIVCE.as_mut().unwrap().setup_framebuffer().expect("failed to get FB");
draw_clear(LIGHT_CYAN);
rkplat::lcpu::restore_irqf(flag);
}

pub fn resolution() -> (u32, u32) {
unsafe { __GPU_DEIVCE.as_mut().unwrap().resolution() }
}

pub fn draw_clear(color: Color) {
let flag = rkplat::lcpu::save_irqf();
unsafe {
let (width, height) = __GPU_DEIVCE.as_mut().unwrap().resolution();
let (width, height) = resolution();
for y in 0..height as usize {
for x in 0..width as usize {
let idx = (y * width as usize + x) * 4;
Expand All @@ -69,10 +72,13 @@ pub fn draw_clear(color: Color) {
}
__GPU_DEIVCE.as_mut().unwrap().flush().expect("failed to flush");
}
rkplat::lcpu::restore_irqf(flag);
}

pub fn screen_flush() {
let flag = rkplat::lcpu::save_irqf();
unsafe {
__GPU_DEIVCE.as_mut().unwrap().flush().expect("failed to flush");
}
rkplat::lcpu::restore_irqf(flag);
}
Loading

0 comments on commit 2002ed4

Please sign in to comment.