Minor cleanup
This commit is contained in:
parent
10b0939930
commit
76e5f8f22d
@ -4,6 +4,7 @@
|
|||||||
#![feature(abi_x86_interrupt)]
|
#![feature(abi_x86_interrupt)]
|
||||||
#![allow(internal_features)]
|
#![allow(internal_features)]
|
||||||
|
|
||||||
|
use core::arch::asm;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use rand::{rngs::SmallRng, SeedableRng};
|
use rand::{rngs::SmallRng, SeedableRng};
|
||||||
use spin::Mutex;
|
use spin::Mutex;
|
||||||
@ -25,6 +26,7 @@ pub(crate) static PIC: Mutex<PICDriver> = Mutex::new(PICDriver::new(32, 32 + 8))
|
|||||||
pub(crate) static TICK_COUNTER: Mutex<u32> = Mutex::new(0);
|
pub(crate) static TICK_COUNTER: Mutex<u32> = Mutex::new(0);
|
||||||
pub(crate) static BOARD: Mutex<TetrisBoard> = Mutex::new(TetrisBoard::new());
|
pub(crate) static BOARD: Mutex<TetrisBoard> = Mutex::new(TetrisBoard::new());
|
||||||
pub(crate) static FALLING: Mutex<Option<FallingTetromino>> = Mutex::new(None);
|
pub(crate) static FALLING: Mutex<Option<FallingTetromino>> = Mutex::new(None);
|
||||||
|
pub(crate) static RUN_TICKS: Mutex<bool> = Mutex::new(false);
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref RNG: Mutex<SmallRng> = Mutex::new(SmallRng::seed_from_u64(1337));
|
static ref RNG: Mutex<SmallRng> = Mutex::new(SmallRng::seed_from_u64(1337));
|
||||||
@ -44,30 +46,23 @@ extern "x86-interrupt" fn divide_handler(stack_frame: InterruptStackFrame) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extern "x86-interrupt" fn timer_handler(_stack_frame: InterruptStackFrame) {
|
extern "x86-interrupt" fn timer_handler(_stack_frame: InterruptStackFrame) {
|
||||||
let mut t = TICK_COUNTER.lock();
|
if !*RUN_TICKS.lock() {
|
||||||
*t = (*t).wrapping_add(1);
|
PIC.lock().send_eoi(32);
|
||||||
drop(t);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
tick();
|
// Step tick counter
|
||||||
|
let t = {
|
||||||
|
let mut t = TICK_COUNTER.lock();
|
||||||
|
*t = (*t).wrapping_add(1);
|
||||||
|
*t
|
||||||
|
};
|
||||||
|
|
||||||
PIC.lock().send_eoi(32);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn double_fault_handler(
|
|
||||||
stack_frame: InterruptStackFrame,
|
|
||||||
error_code: u32,
|
|
||||||
) -> ! {
|
|
||||||
panic!("DOUBLE FAULT (err = 0x{error_code:x}\n{:#?}", stack_frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tick() {
|
|
||||||
let mut v = VGA.lock();
|
let mut v = VGA.lock();
|
||||||
let t = *TICK_COUNTER.lock();
|
|
||||||
let mut board = BOARD.lock();
|
let mut board = BOARD.lock();
|
||||||
let mut fall = FALLING.lock();
|
let mut fall = FALLING.lock();
|
||||||
|
|
||||||
v.swap();
|
v.swap();
|
||||||
|
|
||||||
board.draw(&mut v, fall.as_ref());
|
board.draw(&mut v, fall.as_ref());
|
||||||
|
|
||||||
if let Some(fall_inner) = fall.as_mut() {
|
if let Some(fall_inner) = fall.as_mut() {
|
||||||
@ -84,6 +79,15 @@ fn tick() {
|
|||||||
} else {
|
} else {
|
||||||
*fall = Some(FallingTetromino::random(5, 1))
|
*fall = Some(FallingTetromino::random(5, 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PIC.lock().send_eoi(32);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "x86-interrupt" fn double_fault_handler(
|
||||||
|
stack_frame: InterruptStackFrame,
|
||||||
|
error_code: u32,
|
||||||
|
) -> ! {
|
||||||
|
panic!("DOUBLE FAULT (err = 0x{error_code:x}\n{:#?}", stack_frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -118,16 +122,11 @@ pub unsafe extern "C" fn start(
|
|||||||
pic.init();
|
pic.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear screen
|
*RUN_TICKS.lock() = true;
|
||||||
let mut v = VGA.lock();
|
|
||||||
let board = BOARD.lock();
|
|
||||||
|
|
||||||
board.draw(&mut v, None);
|
// Do-nothing loop,
|
||||||
v.swap();
|
// frames are driven by interrupts
|
||||||
|
loop {
|
||||||
// Important to drop these, or tick() will deadlock
|
asm!("hlt")
|
||||||
drop(v);
|
}
|
||||||
drop(board);
|
|
||||||
|
|
||||||
loop {}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user