Replace busy-wait with time-based sleep in sweden.c

This commit is contained in:
Chris 2026-02-08 14:52:19 +01:00
parent e2cf01bb4a
commit b245dc5f00
5 changed files with 20 additions and 2 deletions

View file

@ -1,4 +1,5 @@
#include "cli_utils.h" #include "cli_utils.h"
#include "wm.h"
// Forward declarations - these will be provided by cmd.c // Forward declarations - these will be provided by cmd.c
extern void cmd_putchar(char c); extern void cmd_putchar(char c);
@ -78,3 +79,14 @@ void cli_delay(int iterations) {
__asm__ __volatile__("nop"); __asm__ __volatile__("nop");
} }
} }
void cli_sleep(int ms) {
// Timer is ~60Hz, so 1 tick = 16.66ms
uint32_t ticks = ms / 16;
if (ticks == 0 && ms > 0) ticks = 1;
uint32_t target = wm_get_ticks() + ticks;
while (wm_get_ticks() < target) {
__asm__ __volatile__("hlt");
}
}

View file

@ -19,6 +19,7 @@ void cli_putchar(char c);
// Timing utility // Timing utility
void cli_delay(int iterations); void cli_delay(int iterations);
void cli_sleep(int ms);
// CLI Command declarations // CLI Command declarations
void cli_cmd_shutdown(char *args); void cli_cmd_shutdown(char *args);

View file

@ -13,10 +13,10 @@ void play_note(int freq, int duration_ms) {
} }
cli_delay(duration_ms * 300000); cli_sleep(duration_ms);
outb(0x61, inb(0x61) & 0xFC); outb(0x61, inb(0x61) & 0xFC);
cli_delay(2000000); cli_sleep(20);
} }
void cli_cmd_minecraft(char *args) { void cli_cmd_minecraft(char *args) {

View file

@ -1729,6 +1729,10 @@ void wm_init(void) {
force_redraw = true; force_redraw = true;
} }
uint32_t wm_get_ticks(void) {
return timer_ticks;
}
// Called by timer interrupt ~60Hz // Called by timer interrupt ~60Hz
void wm_timer_tick(void) { void wm_timer_tick(void) {
timer_ticks++; timer_ticks++;

View file

@ -54,6 +54,7 @@ void wm_refresh(void);
void wm_paint(void); void wm_paint(void);
void wm_refresh_desktop(void); void wm_refresh_desktop(void);
void wm_timer_tick(void); void wm_timer_tick(void);
uint32_t wm_get_ticks(void);
int wm_get_desktop_icon_count(void); int wm_get_desktop_icon_count(void);
void wm_show_message(const char *title, const char *message); void wm_show_message(const char *title, const char *message);