From 369b5cb21e63159181ef7730a4641191f36f683d Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 6 Jul 2021 22:29:26 +0900 Subject: [PATCH] ibmpc: ringbuf optimization for cpp --- tmk_core/protocol/ibmpc.cpp | 31 +++++++++++++------------- tmk_core/protocol/ibmpc.hpp | 44 ++++++++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/tmk_core/protocol/ibmpc.cpp b/tmk_core/protocol/ibmpc.cpp index 7d6dfcbd..34b8458d 100644 --- a/tmk_core/protocol/ibmpc.cpp +++ b/tmk_core/protocol/ibmpc.cpp @@ -69,7 +69,6 @@ void IBMPC::host_init(void) inhibit(); int_init(); int_off(); - ringbuf_init(&rb, rbuf, RINGBUF_SIZE); host_isr_clear(); } @@ -174,14 +173,14 @@ int16_t IBMPC::host_recv(void) int16_t ret = -1; // Enable ISR if buffer was full - if (ringbuf_is_full(&rb)) { + if (ringbuf_is_full()) { host_isr_clear(); int_on(); idle(); } ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - ret = ringbuf_get(&rb); + ret = ringbuf_get(); } if (ret != -1) dprintf("r%02X ", ret&0xFF); return ret; @@ -204,7 +203,7 @@ void IBMPC::host_isr_clear(void) protocol = 0; error = 0; isr_state = 0x8000; - ringbuf_reset(&rb); + ringbuf_reset(); } inline void IBMPC::isr(void) @@ -281,8 +280,8 @@ inline void IBMPC::isr(void) uint8_t us = 100; // wait for rising and falling edge of b7 of XT_IBM if (!protocol) { - while (!(IBMPC_CLOCK_PIN&(1< #include "wait.h" -#include "ringbuf.h" /* * IBM PC keyboard protocol @@ -91,8 +90,6 @@ POSSIBILITY OF SUCH DAMAGE. #define IBMPC_LED_NUM_LOCK 1 #define IBMPC_LED_CAPS_LOCK 2 -#define RINGBUF_SIZE 16 - class IBMPC { @@ -127,8 +124,14 @@ class IBMPC private: volatile uint16_t isr_state; uint8_t timer_start; - ringbuf_t rb; - uint8_t rbuf[RINGBUF_SIZE]; + + /* ring buffer */ + // Size should be power of 2 + #define RINGBUF_SIZE 16 + uint8_t rb_head; + uint8_t rb_tail; + uint8_t rb_buffer[RINGBUF_SIZE]; + const uint8_t clock_bit, data_bit; const uint8_t clock_mask, data_mask; @@ -237,6 +240,37 @@ class IBMPC { EIMSK &= ~clock_mask; } + + /* + * ring buffer + */ + inline int16_t ringbuf_get(void) __attribute__((__always_inline__)) + { + if (ringbuf_is_empty()) return -1; + uint8_t data = rb_buffer[rb_tail]; + rb_tail++; + rb_tail &= (RINGBUF_SIZE - 1); + return data; + } + inline void ringbuf_put(uint8_t data) __attribute__((__always_inline__)) + { + rb_buffer[rb_head] = data; + rb_head++; + rb_head &= (RINGBUF_SIZE - 1); + } + inline bool ringbuf_is_empty(void) __attribute__((__always_inline__)) + { + return (rb_head == rb_tail); + } + inline bool ringbuf_is_full(void) __attribute__((__always_inline__)) + { + return (((rb_head + 1) & (RINGBUF_SIZE - 1)) == rb_tail); + } + inline void ringbuf_reset(void) __attribute__((__always_inline__)) + { + rb_head = 0; + rb_tail = 0; + } }; #endif