From fb46bb360872cd9ffec17cf7ebc83690962e7006 Mon Sep 17 00:00:00 2001 From: Ian Scott Date: Sat, 6 Jul 2024 00:31:24 -0600 Subject: [PATCH] Dynamic PIC timers --- sw/CMakeLists.txt | 9 ++++++--- sw/pico_pic.c | 14 +++++++++----- sw/pico_pic.h | 28 +++++++++++++++++++++------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/sw/CMakeLists.txt b/sw/CMakeLists.txt index b5e6726..9ff20b0 100644 --- a/sw/CMakeLists.txt +++ b/sw/CMakeLists.txt @@ -113,6 +113,11 @@ function(config_target TARGET_NAME MULTIFW) # USB Mouse stuff target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/mouse) target_compile_definitions(${TARGET_NAME} PRIVATE USB_MOUSE=1) + target_sources(${TARGET_NAME} PRIVATE + mouse/8250uart.cpp + mouse/sermouse.cpp + pico_pic.c + ) endif() target_sources(${TARGET_NAME} PRIVATE M62429/M62429.cpp) @@ -296,8 +301,6 @@ function(build_usb TARGET_NAME MULTIFW) target_sources(${TARGET_NAME} PRIVATE usbplay.cpp pico_pic.c - mouse/8250uart.cpp - mouse/sermouse.cpp ) pico_enable_stdio_uart(${TARGET_NAME} 1) pico_enable_stdio_usb(${TARGET_NAME} 0) @@ -412,7 +415,7 @@ elseif(PROJECT_TYPE STREQUAL "SB") build_sb(pg-sb FALSE) elseif(PROJECT_TYPE STREQUAL "ADLIB") set(FW_TARGET pg-adlib) - build_sb(pg-adlib FALSE) + build_adlib(pg-adlib FALSE) elseif(PROJECT_TYPE STREQUAL "MPU") set(FW_TARGET pg-mpu) build_mpu(pg-mpu FALSE) diff --git a/sw/pico_pic.c b/sw/pico_pic.c index a34bed2..b77ded7 100644 --- a/sw/pico_pic.c +++ b/sw/pico_pic.c @@ -4,8 +4,12 @@ #include -// A fixed pool of only 3 events for now. gus-x only has 3 different timer events - two timers and one DMA -PIC_TimerEvent timerEvents[4]; +// A fixed pool of multiple events. +// gus-x has 3 different timer events - two timers and one DMA +// 8250uart has 3 +// mpu401 has 3 +// SB has 1 +PIC_TimerEvent timerEvents[PIC_MAX_TIMERS]; alarm_pool_t* alarm_pool; @@ -44,7 +48,7 @@ int64_t PIC_HandleEvent(alarm_id_t id, void *user_data) { void PIC_RemoveEvents(PIC_EventHandler handler) { // puts("removeevents"); - for (int i = 0; i < 4; ++i) { + for (int i = 0; i < PIC_MAX_TIMERS; ++i) { if (timerEvents[i].handler == handler) { #ifdef USE_ALARM if (timerEvents[i].alarm_id) { @@ -63,12 +67,12 @@ void PIC_Init() { #ifdef USE_ALARM alarm_pool = alarm_pool_create(2, PICO_TIME_DEFAULT_ALARM_POOL_MAX_TIMERS); irq_set_priority(TIMER_IRQ_2, PICO_HIGHEST_IRQ_PRIORITY); - for (int i = 0; i < 4; ++i) { + for (int i = 0; i < PIC_MAX_TIMERS; ++i) { timerEvents[i].alarm_id = 0; timerEvents[i].handler = 0; } #else - for (int i = 0; i < 3; ++i) { + for (int i = 0; i < PIC_MAX_TIMERS; ++i) { timerEvents[i].active = false; timerEvents[i].deadline = UINT32_MAX; timerEvents[i].handler = 0; diff --git a/sw/pico_pic.h b/sw/pico_pic.h index 03927a9..ddc41f3 100644 --- a/sw/pico_pic.h +++ b/sw/pico_pic.h @@ -30,7 +30,8 @@ typedef struct { #endif } PIC_TimerEvent; -extern PIC_TimerEvent timerEvents[4]; +#define PIC_MAX_TIMERS 8 +extern PIC_TimerEvent timerEvents[PIC_MAX_TIMERS]; extern alarm_pool_t* alarm_pool; @@ -52,15 +53,28 @@ static __force_inline void PIC_DeActivateIRQ(void) { static __force_inline void PIC_AddEvent(PIC_EventHandler handler, uint32_t delay, Bitu val) { // printf("add event: %x %x %d\n", handler, val, delay); - timerEvents[val].handler = handler; - timerEvents[val].value = val; + // find free slot - TBD if this is too jittery + int i; + for (i = 0; i < PIC_MAX_TIMERS; ++i) { + if ( +#ifdef USE_ALARM + !timerEvents[i].alarm_id +#else + !timerEvents[i].active +#endif + ) { + break; + } + } + timerEvents[i].handler = handler; + timerEvents[i].value = val; #ifdef USE_ALARM // timerEvents[val].alarm_id = add_alarm_in_us(delay, PIC_HandleEvent, timerEvents + val, true); // alarm_pool_cancel_alarm(alarm_pool, timerEvents[val].alarm_id); - timerEvents[val].alarm_id = alarm_pool_add_alarm_in_us(alarm_pool, delay, PIC_HandleEvent, timerEvents + val, true); + timerEvents[i].alarm_id = alarm_pool_add_alarm_in_us(alarm_pool, delay, PIC_HandleEvent, timerEvents + i, true); #else - timerEvents[val].deadline = time_us_32() + delay; - timerEvents[val].active = true; + timerEvents[i].deadline = time_us_32() + delay; + timerEvents[i].active = true; #endif // gpio_put(PICO_DEFAULT_LED_PIN, 1); } @@ -71,7 +85,7 @@ void PIC_Init(void); #ifndef USE_ALARM static __force_inline void PIC_HandleEvents() { - for (int i = 0; i < 4; ++i) { + for (int i = 0; i < PIC_MAX_TIMERS; ++i) { if (timerEvents[i].active && timerEvents[i].deadline <= time_us_32()) { PIC_HandleEvent(0, &timerEvents[i]); }