Skip to content

Commit

Permalink
Small performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
KimJorgensen committed Apr 30, 2022
1 parent e748485 commit fbbc609
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
16 changes: 13 additions & 3 deletions firmware/stm32f4xx/c64_interface.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021 Kim Jørgensen
* Copyright (c) 2019-2022 Kim Jørgensen
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
Expand Down Expand Up @@ -194,7 +194,8 @@ static void c64_diag_handler(void)
{
// Clear the interrupt flag
TIM1->SR = ~TIM_SR_CC3IF;
__DSB();
// Ensure interrupt flag is cleared
(void)TIM1->SR;

if (diag_state == DIAG_RUN)
{
Expand Down Expand Up @@ -230,9 +231,18 @@ static void c64_diag_handler(void)
*************************************************/
static void c64_interface_enable_no_check(void)
{
u32 dier = TIM1->DIER | TIM_DIER_CC3IE;
COMPILER_BARRIER();

__disable_irq();
// Wait for next interrupt
TIM1->SR = ~TIM_SR_CC3IF;
while (!(TIM1->SR & TIM_SR_CC3IF));

// Capture/Compare 3 interrupt enable
TIM1->SR = ~(TIM_SR_CC3IF|TIM_SR_CC4IF);
TIM1->DIER |= TIM_DIER_CC3IE;
TIM1->DIER = dier;
__enable_irq();
}

static void c64_interface(bool state)
Expand Down
7 changes: 3 additions & 4 deletions firmware/stm32f4xx/c64_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ static void handler(void)
/* We need to clear the interrupt flag early otherwise the next */ \
/* interrupt may be delayed */ \
TIM1->SR = ~TIM_SR_CC3IF; \
__DSB(); \
u32 phi2_high = DWT->COMP0; \
COMPILER_BARRIER(); \
/* Use debug cycle counter which is faster to access than timer */ \
DWT->CYCCNT = TIM1->CNT; \
COMPILER_BARRIER(); \
u32 phi2_high = DWT->COMP0; \
while (DWT->CYCCNT < phi2_high); \
u32 addr = C64_ADDR_READ(); \
COMPILER_BARRIER(); \
u32 control = C64_CONTROL_READ(); \
if (control & C64_WRITE) \
{ \
COMPILER_BARRIER(); \
if (read_handler(control, addr)) \
{ \
/* Wait for phi2 to go low */ \
Expand All @@ -149,7 +149,6 @@ static void handler(void)
} \
else \
{ \
COMPILER_BARRIER(); \
u32 data = C64_DATA_READ(); \
write_handler(control, addr, data); \
} \
Expand Down

0 comments on commit fbbc609

Please sign in to comment.