-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOptUsbDevice.cpp
155 lines (115 loc) · 4.04 KB
/
OptUsbDevice.cpp
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
#include "OptUsbDevice.hpp"
#include "Sleep.hpp"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <string>
using namespace std::literals;
OptUsbDevice::OptUsbDevice(LibUsb& libusb, unsigned int wait, unsigned int timeout) :
LibUsbDevice(libusb, VendorId, ProductId),
mWait(wait),
mTimeout(timeout)
{}
std::uint_fast32_t OptUsbDevice::Receive32(std::uint_fast8_t number) {
{
std::byte data[2] = {
static_cast<std::byte>(UsbCommandStatus),
static_cast<std::byte>(number),
};
Send(EndpointCommand, data, sizeof(data), mTimeout);
}
optusbx::Sleep(mWait);
{
std::byte data[4]{};
Receive(EndpointData, data, sizeof(data), mTimeout);
return
(static_cast<std::uint_fast32_t>(data[0]) << 0) |
(static_cast<std::uint_fast32_t>(data[1]) << 8) |
(static_cast<std::uint_fast32_t>(data[2]) << 16) |
(static_cast<std::uint_fast32_t>(data[3]) << 24);
}
}
std::uint_fast32_t OptUsbDevice::Send32(std::uint_fast8_t number, std::uint_fast32_t code) {
{
std::byte data[6] = {
static_cast<std::byte>(UsbCommandStatus),
static_cast<std::byte>(0x80 | number),
static_cast<std::byte>((code >> 0) & 0xFF),
static_cast<std::byte>((code >> 8) & 0xFF),
static_cast<std::byte>((code >> 16) & 0xFF),
static_cast<std::byte>((code >> 24) & 0xFF),
};
Send(EndpointCommand, data, sizeof(data), mTimeout);
}
optusbx::Sleep(mWait);
{
std::byte data[4]{};
Receive(EndpointData, data, sizeof(data), mTimeout);
return
(static_cast<std::uint_fast32_t>(data[0]) << 0) |
(static_cast<std::uint_fast32_t>(data[1]) << 8) |
(static_cast<std::uint_fast32_t>(data[2]) << 16) |
(static_cast<std::uint_fast32_t>(data[3]) << 24);
}
}
void OptUsbDevice::SendProgram(const std::byte* programData, std::size_t programSize) {
const auto alignedProgramSize = (programSize + 3) & ~static_cast<std::size_t>(3);
{
std::byte data[3] = {
static_cast<std::byte>(UsbCommandWrite),
// 1 DWORD : program size
static_cast<std::byte>(1),
static_cast<std::byte>(0),
};
Send(EndpointCommand, data, sizeof(data), mTimeout);
}
optusbx::Sleep(mWait);
{
std::byte data[4] = {
static_cast<std::byte>((programSize >> 0) & 0xFF),
static_cast<std::byte>((programSize >> 8) & 0xFF),
static_cast<std::byte>((programSize >> 16) & 0xFF),
static_cast<std::byte>((programSize >> 24) & 0xFF),
};
Send(EndpointData, data, sizeof(data), mTimeout);
}
optusbx::Sleep(mWait);
{
const auto alignedProgramSizeInDWord = alignedProgramSize / 4;
std::byte data[3] = {
static_cast<std::byte>(UsbCommandWrite),
// program size in DWORD
static_cast<std::byte>((alignedProgramSizeInDWord >> 0) & 0xFF),
static_cast<std::byte>((alignedProgramSizeInDWord >> 8) & 0xFF),
};
Send(EndpointCommand, data, sizeof(data), mTimeout);
}
optusbx::Sleep(mWait);
{
auto alignedProgramData = std::make_unique<std::byte[]>(alignedProgramSize);
std::memcpy(alignedProgramData.get(), programData, programSize);
std::memset(alignedProgramData.get() + programSize, 0, alignedProgramSize - programSize);
Send(EndpointData, alignedProgramData.get(), alignedProgramSize, mTimeout);
}
}
void OptUsbDevice::TransferProgram(const std::byte* programData, std::size_t programSize) {
ClaimInterface(0);
//
if (const auto code = Receive32(1); code == 0x12345678) {
throw std::runtime_error("negotiation error [1]");
}
Send32(2, NegotiationMagickNumber);
if (const auto code = Receive32(2); code != NegotiationMagickNumber) {
throw std::runtime_error("negotiation error [2]");
}
Send32(2, 0x00000000);
if (const auto code = Receive32(0); !(code & StatusBitReady) || !(code & StatusBitPower)) {
throw std::runtime_error("negotiation error [3] (GBA not ready)");
}
//
if (const auto code = Receive32(1); code == 0x12345678) {
throw std::runtime_error("negotiation error [4]");
}
SendProgram(programData, programSize);
}