forked from sgadrat/super-tilt-bro-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesen-0.9.5_patch_nrom_udp_windows.patch
163 lines (157 loc) · 4.89 KB
/
mesen-0.9.5_patch_nrom_udp_windows.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
diff --git a/Core/NROM.h b/Core/NROM.h
index dff0624e..ddc672ea 100644
--- a/Core/NROM.h
+++ b/Core/NROM.h
@@ -1,18 +1,144 @@
#pragma once
#include "stdafx.h"
#include "BaseMapper.h"
+#define _WIN32_WINNT 0x0601
+#include <boost/asio.hpp>
+#include <vector>
+#include <map>
+#include <string>
+#include <fstream>
+#include <cstdlib>
+
+static std::map<std::string, std::string> GetConfig() {
+ // Fill default fields
+ std::map<std::string, std::string> config = {
+ {"server_address", "127.0.0.1"},
+ {"server_port", "1234"},
+ };
+
+ // Parse config file
+ std::cout << "config file: '" << (std::getenv("USERPROFILE") + std::string("\\nes_net.cfg")) << "'" << std::endl;
+ std::ifstream ifs(std::getenv("USERPROFILE") + std::string("\\nes_net.cfg"));
+ while (ifs.good()) {
+ std::string key, value;
+ ifs >> key >> value;
+ if (ifs.good()) {
+ config[key] = value;
+ std::cout << " '" << key << "' => '" << value << "'" << std::endl;
+ }
+ }
+
+ // debug
+ std::cout << "Parsed config:" << std::endl;
+ for (std::pair<std::string, std::string> const& record : config) {
+ std::cout << " " << record.first << " = " << record.second << std::endl;
+ }
+
+ return config;
+}
class NROM : public BaseMapper
{
+ public:
+ NROM()
+ : BaseMapper()
+ , mSocket(mIoContext)
+ {}
+
protected:
virtual uint16_t GetPRGPageSize() override { return 0x4000; }
- virtual uint16_t GetCHRPageSize() override { return 0x2000; }
+ virtual uint16_t GetCHRPageSize() override { return 0x2000; }
+ virtual bool AllowRegisterRead() override { return true; }
virtual void InitMapper() override
{
+ std::cout << "init NROM" << std::endl;
SelectPRGPage(0, 0);
SelectPRGPage(1, 1);
SelectCHRPage(0, 0);
+
+ std::map<std::string, std::string> config = GetConfig();
+
+ RemoveRegisterRange(0x8000, 0xFFFF, MemoryOperation::Read);
+ AddRegisterRange(0x5000, 0x5FFF, MemoryOperation::Any);
+
+ mReg = 0;
+ mWriteBuffer.resize(256);
+
+ boost::asio::ip::udp::resolver resolver(mIoContext);
+ mServerEndpoint = *resolver.resolve(config["server_address"], config["server_port"]).begin();
+ //mSocket.open(boost::asio::ip::udp::v4());
+ mSocket.connect(mServerEndpoint);
+
+ mReadBufferFresh = false;
+ mTmpReadBuffer.resize(255);
+ mSocket.async_receive(boost::asio::buffer(mTmpReadBuffer), [&] (const boost::system::error_code& e, std::size_t bytes_transferred) -> void {
+ std::cout << "received " << bytes_transferred << " bytes: " << e << std::endl;
+ this->ReceivedPacket(bytes_transferred);
+ });
+ }
+
+ virtual uint8_t ReadRegister(uint16_t addr) override
+ {
+ if (addr >= 0x5100) {
+ mIoContext.poll();
+ uint8_t res = mReadBufferFresh ? mReadBuffer.size() : 0;
+ mReadBufferFresh = false;
+ return res;
+ }
+ if (addr < 0x5000) {
+ return 0;
+ }
+ uint16_t offset = addr - 0x5000;
+ if (offset >= mReadBuffer.size()) {
+ return 0;
+ }
+ return mReadBuffer[offset];
}
+
+ virtual void WriteRegister(uint16_t addr, uint8_t value) override
+ {
+ if (addr >= 0x5101) {
+ SendPacket(static_cast<uint16_t>(value) + 1);
+ return;
+ }
+ if (addr < 0x5000) {
+ return;
+ }
+ mWriteBuffer[addr - 0x5000] = value;
+ }
+
+ private:
+ void SendPacket(uint16_t payload_size)
+ {
+ mSocket.send(boost::asio::buffer(mWriteBuffer, payload_size));
+ }
+
+ void ReceivedPacket(std::size_t bytes_transferred)
+ {
+ std::cout << "received packet" << std::endl;
+ mTmpReadBuffer.resize(bytes_transferred);
+ mReadBuffer = mTmpReadBuffer;
+ mReadBufferFresh = true;
+
+ mTmpReadBuffer.resize(255);
+ mSocket.async_receive(boost::asio::buffer(mTmpReadBuffer), [&] (const boost::system::error_code& e, std::size_t bytes_transferred) -> void {
+ std::cout << "received " << bytes_transferred << " bytes: " << e << std::endl;
+ this->ReceivedPacket(bytes_transferred);
+ });
+ }
+
+ private:
+ uint8_t mReg;
+ std::vector<uint8_t> mReadBuffer;
+ std::vector<uint8_t> mTmpReadBuffer;
+ bool mReadBufferFresh;
+
+ std::vector<uint8_t> mWriteBuffer;
+
+
+ boost::asio::io_context mIoContext;
+ boost::asio::ip::udp::endpoint mServerEndpoint;
+ boost::asio::ip::udp::socket mSocket;
};
diff --git a/makefile b/makefile
index 0ae3dae1..2ae20e0b 100644
--- a/makefile
+++ b/makefile
@@ -106,7 +106,7 @@ Linux/$(OBJFOLDER)/%.o: Linux/libevdev/%.c
InteropDLL/$(OBJFOLDER)/$(SHAREDLIB): $(SEVENZIPOBJ) $(LUAOBJ) $(UTILOBJ) $(COREOBJ) $(LIBEVDEVOBJ) $(LINUXOBJ) InteropDLL/ConsoleWrapper.cpp InteropDLL/DebugWrapper.cpp
mkdir -p InteropDLL/$(OBJFOLDER)
- $(CPPC) $(GCCOPTIONS) -Wl,-z,defs -shared -o $(SHAREDLIB) InteropDLL/*.cpp $(SEVENZIPOBJ) $(LUAOBJ) $(LINUXOBJ) $(LIBEVDEVOBJ) $(UTILOBJ) $(COREOBJ) -pthread -lSDL2 -lstdc++fs
+ $(CPPC) $(GCCOPTIONS) -Wl,-z,defs -shared -o $(SHAREDLIB) InteropDLL/*.cpp $(SEVENZIPOBJ) $(LUAOBJ) $(LINUXOBJ) $(LIBEVDEVOBJ) $(UTILOBJ) $(COREOBJ) -pthread -lSDL2 -lstdc++fs -lboost_system
mv $(SHAREDLIB) InteropDLL/$(OBJFOLDER)