From 338f13e57bd52c9353fe43bb705b60f75c35564b Mon Sep 17 00:00:00 2001 From: Tim Allen Date: Sat, 25 Feb 2012 19:52:42 +1100 Subject: [PATCH] Update to v086r05 release. byuu says: USART implements reading and writing, but I don't yet have code to test SNES reading yet. So ... obviously I need to do that next. Went ahead and required nall::function, so the modules will have to be C++11. I don't see anyone else making these, and it avoids the annoyance of deducing the correct controller port based on dynamic casting the active thread. Apparently a library can have a main() function to no ill effect, so there's no need for USART_HARDWARE. Same exact code with different flags will make the binary and the library. --- bsnes/base/base.hpp | 2 +- bsnes/snes/controller/usart/usart.cpp | 45 ++++++++++++++++++++------- bsnes/snes/controller/usart/usart.hpp | 10 ++++++ 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/bsnes/base/base.hpp b/bsnes/base/base.hpp index c62c606e..d3aec8e5 100755 --- a/bsnes/base/base.hpp +++ b/bsnes/base/base.hpp @@ -1,7 +1,7 @@ #ifndef BASE_HPP #define BASE_HPP -const char Version[] = "086.04"; +const char Version[] = "086.05"; #include #include diff --git a/bsnes/snes/controller/usart/usart.cpp b/bsnes/snes/controller/usart/usart.cpp index 26637658..063d3fda 100755 --- a/bsnes/snes/controller/usart/usart.cpp +++ b/bsnes/snes/controller/usart/usart.cpp @@ -16,14 +16,35 @@ // IOBit --- // GND GND -static uint8 usart_read(); -static void usart_write(uint8 data); +void USART::enter() { + main({ &USART::usleep, this }, { &USART::read, this }, { &USART::write, this }); + while(true) step(1000000); //fallback; main should never return +} + +void USART::usleep(unsigned milliseconds) { + step(milliseconds); +} + +//SNES -> USART +uint8 USART::read() { + while(txbuffer.size() == 0) step(1); + uint8 data = txbuffer[0]; + txbuffer.remove(0); + return data; +} + +//USART -> SNES +void USART::write(uint8 data) { + rxbuffer.append(data); +} //USART -> SNES uint2 USART::data() { - if(rxlength == 0) { + if(rxlength == 0 && rxbuffer.size()) { data1 = 0; - rxdata = usart_read(); + rxdata = rxbuffer[0]; + rxbuffer.remove(0); + rxlength++; } else if(rxlength <= 8) { data1 = rxdata & 1; rxdata >>= 1; @@ -43,7 +64,7 @@ void USART::latch(bool data) { txdata = (data << 7) | (txdata >> 1); txlength++; } else { - if(data == 1) usart_write(txdata); + if(data == 1) txbuffer.append(txdata); txlength = 0; } @@ -60,16 +81,16 @@ USART::USART(bool port) : Controller(port) { txlength = 0; txdata = 0; + + string filename = interface->path(Cartridge::Slot::Base, "usart.so"); + if(open_absolute(filename)) { + main = sym("usart_main"); + if(main) create(Controller::Enter, 1000000); + } } USART::~USART() { -} - -static uint8 usart_read() { - return 0xff; -} - -static void usart_write(uint8 data) { + if(opened()) close(); } #endif diff --git a/bsnes/snes/controller/usart/usart.hpp b/bsnes/snes/controller/usart/usart.hpp index 385e6fd1..17de61a5 100755 --- a/bsnes/snes/controller/usart/usart.hpp +++ b/bsnes/snes/controller/usart/usart.hpp @@ -1,6 +1,12 @@ struct USART : Controller, public library { + void enter(); + void usleep(unsigned milliseconds); + uint8 read(); + void write(uint8 data); + uint2 data(); void latch(bool data); + USART(bool port); ~USART(); @@ -11,7 +17,11 @@ private: uint8 rxlength; uint8 rxdata; + vector rxbuffer; uint8 txlength; uint8 txdata; + vector txbuffer; + + function, function, function)> main; };