From 76e5f8f22d8a831b4fbd36245d7f82643d441a9a Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 1 Mar 2025 09:41:36 -0800 Subject: [PATCH] Minor cleanup --- tetros/src/lib.rs | 55 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/tetros/src/lib.rs b/tetros/src/lib.rs index 8431b8f..73c6df1 100644 --- a/tetros/src/lib.rs +++ b/tetros/src/lib.rs @@ -4,6 +4,7 @@ #![feature(abi_x86_interrupt)] #![allow(internal_features)] +use core::arch::asm; use lazy_static::lazy_static; use rand::{rngs::SmallRng, SeedableRng}; use spin::Mutex; @@ -25,6 +26,7 @@ pub(crate) static PIC: Mutex = Mutex::new(PICDriver::new(32, 32 + 8)) pub(crate) static TICK_COUNTER: Mutex = Mutex::new(0); pub(crate) static BOARD: Mutex = Mutex::new(TetrisBoard::new()); pub(crate) static FALLING: Mutex> = Mutex::new(None); +pub(crate) static RUN_TICKS: Mutex = Mutex::new(false); lazy_static! { static ref RNG: Mutex = 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) { - let mut t = TICK_COUNTER.lock(); - *t = (*t).wrapping_add(1); - drop(t); + if !*RUN_TICKS.lock() { + PIC.lock().send_eoi(32); + 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 t = *TICK_COUNTER.lock(); let mut board = BOARD.lock(); let mut fall = FALLING.lock(); v.swap(); - board.draw(&mut v, fall.as_ref()); if let Some(fall_inner) = fall.as_mut() { @@ -84,6 +79,15 @@ fn tick() { } else { *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] @@ -118,16 +122,11 @@ pub unsafe extern "C" fn start( pic.init(); } - // Clear screen - let mut v = VGA.lock(); - let board = BOARD.lock(); + *RUN_TICKS.lock() = true; - board.draw(&mut v, None); - v.swap(); - - // Important to drop these, or tick() will deadlock - drop(v); - drop(board); - - loop {} + // Do-nothing loop, + // frames are driven by interrupts + loop { + asm!("hlt") + } }