// add to the buffer, resizing if necessary
m_buffer[m_bufend] = ch;
if ((m_bufend + 1) % m_buffer.size() == m_bufbegin)
{
if (m_buffer.size() >= KEY_BUFFER_MAX_SIZE)
return;
m_buffer.insert(m_buffer.begin() + m_bufbegin, KEY_BUFFER_CHUNK_SIZE, INVALID_CHAR);
m_bufbegin += KEY_BUFFER_CHUNK_SIZE;
}
m_bufend = (m_bufend + 1) % m_buffer.size();
I pasted your new code in and found a bug,
say we have a buffer sized 4k BUFFER_CHUNK_SIZE = 0x1000;
and our initial post size is larger than this
so bufend keeps getting incremented to 4095 and bufstart is zero
than at this point, when you execute the code, it checks
if ((m_bufend + 1) % m_buffer.size() == m_bufbegin) and says yes, we need to make the buffer bigger,
so it increases the buffersize by 4096 and also increments the bufbegin to now be 4096
0..4095 all new invalid 4096 = newbufbegin, but bufend is still 4095,
m_bufend = (m_bufend + 1) % m_buffer.size(); <--- buffer size is now 8192
so we set bufend to 4096 but bufstart is now 4096, when bufend should now be zero
Now any more characters will start stomping over the freshly moved characters.
so we need to check if bufend > bufstart and if so, also increment bufend when we increment bufstart (since we are inserting characters before where the bufend is "pointing")
// add to the buffer, resizing if necessary
m_buffer[m_bufend] = ch;
if ((m_bufend + 1) % m_buffer.size() == m_bufbegin)
{
if (m_buffer.size() >= KEY_BUFFER_MAX_SIZE)
return;
m_buffer.insert(m_buffer.begin() + m_bufbegin, KEY_BUFFER_CHUNK_SIZE, INVALID_CHAR);
if (m_bufend > m_bufbegin) m_bufend += KEY_BUFFER_CHUNK_SIZE; // this fixes the bufend
m_bufbegin += KEY_BUFFER_CHUNK_SIZE;
}
m_bufend = (m_bufend + 1) % m_buffer.size();
I also stuck a popmessage in natural_keyboard::timer to show how much keybuffer is remaining to give some feedback.
machine().popmessage(std::string("KeyBuffer: ") +
std::to_string((m_bufend < m_bufbegin ? m_buffer.size() : 0) + m_bufend - m_bufbegin) +
std::string(" / ") +
std::to_string(m_buffer.size()));