Skip to content

Commit

Permalink
Dynamic PIC timers
Browse files Browse the repository at this point in the history
  • Loading branch information
polpo committed Jul 6, 2024
1 parent 8b0c34e commit fb46bb3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
9 changes: 6 additions & 3 deletions sw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions sw/pico_pic.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

#include <stdio.h>

// 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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
28 changes: 21 additions & 7 deletions sw/pico_pic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand All @@ -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]);
}
Expand Down

0 comments on commit fb46bb3

Please sign in to comment.