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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/* *************************************
* Includes
* *************************************/
#include "Serial.h"
/* *************************************
* Defines
* *************************************/
#define SERIAL_BAUDRATE 115200
#define SERIAL_TX_RX_TIMEOUT 20000
#define SERIAL_RX_FIFO_EMPTY 0
#define SERIAL_TX_NOT_READY 0
/* *************************************
* Local Variables
* *************************************/
static volatile SERIAL_STATE SerialState;
static volatile size_t bytesRead;
static volatile uint32_t initPC_Address;
static volatile uint32_t RAMDest_Address;
static volatile size_t ExeSize;
static volatile size_t totalBytes;
static volatile size_t exeBytesRead;
static volatile bool serial_busy;
/* *************************************
* Local Prototypes
* *************************************/
void ISR_Serial(void)
{
enum
{
SERIAL_BG_X0 = 0,
SERIAL_BG_X1 = X_SCREEN_RESOLUTION - SERIAL_BG_X0,
SERIAL_BG_X2 = SERIAL_BG_X0,
SERIAL_BG_X3 = SERIAL_BG_X1,
SERIAL_BG_Y0 = 0,
SERIAL_BG_Y1 = SERIAL_BG_Y0,
SERIAL_BG_Y2 = Y_SCREEN_RESOLUTION - SERIAL_BG_Y0,
SERIAL_BG_Y3 = SERIAL_BG_Y2,
SERIAL_BG_R = 0,
SERIAL_BG_G = NORMAL_LUMINANCE,
SERIAL_BG_B = NORMAL_LUMINANCE,
};
static GsGPoly4 SerialBg = { .x[0] = SERIAL_BG_X0,
.x[1] = SERIAL_BG_X1,
.x[2] = SERIAL_BG_X2,
.x[3] = SERIAL_BG_X3,
.y[0] = SERIAL_BG_Y0,
.y[1] = SERIAL_BG_Y1,
.y[2] = SERIAL_BG_Y2,
.y[3] = SERIAL_BG_Y3,
.r[0] = 0,
.r[1] = 0,
.r[2] = SERIAL_BG_R,
.r[3] = SERIAL_BG_R,
.g[0] = 0,
.g[1] = 0,
.g[2] = SERIAL_BG_G,
.g[3] = SERIAL_BG_G,
.b[0] = 0,
.b[1] = 0,
.b[2] = SERIAL_BG_B,
.b[3] = SERIAL_BG_B, };
enum
{
SERIAL_STATE_TEXT_X = 148,
SERIAL_STATE_TEXT_Y = Y_SCREEN_RESOLUTION >> 1,
};
if((GfxIsGPUBusy() == true) || (serial_busy == true) )
{
return;
}
GsSortGPoly4(&SerialBg);
FontSetFlags(&SmallFont, FONT_BLEND_EFFECT | FONT_H_CENTERED);
GfxSetGlobalLuminance(0);
switch(SerialState)
{
case SERIAL_STATE_INIT:
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y, "Serial initialization");
break;
case SERIAL_STATE_STANDBY:
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y, "Waiting for PC...");
break;
case SERIAL_STATE_WRITING_ACK:
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y, "Writing ACK");
break;
case SERIAL_STATE_READING_HEADER:
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y, "Reading data from header (%d/%d bytes)...", bytesRead, totalBytes);
break;
case SERIAL_STATE_READING_EXE_SIZE:
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y, "Getting PSX-EXE size from PC...");
break;
case SERIAL_STATE_READING_EXE_DATA:
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y, "Reading PSX-EXE data (%d/%d bytes)...", exeBytesRead, ExeSize);
break;
case SERIAL_STATE_WAITING_USER_INPUT:
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y, "Press any key to continue");
break;
case SERIAL_STATE_CLEANING_MEMORY:
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y, "Cleaning RAM before EXE data transfer...");
break;
default:
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y, "Unknown state");
break;
}
FontSetFlags(&SmallFont, FONT_NOFLAGS);
if(RAMDest_Address != 0)
{
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y + 16, "RAM Dest address: 0x%08X", RAMDest_Address);
}
if(initPC_Address != 0)
{
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y + 32, "Init PC address: 0x%08X", initPC_Address);
}
if(ExeSize != 0)
{
FontPrintText(&SmallFont, SERIAL_STATE_TEXT_X, SERIAL_STATE_TEXT_Y + 48, "PSX-EXE size: 0x%08X", ExeSize);
}
GfxDrawScene_Fast();
}
void SerialSetState(SERIAL_STATE state)
{
SerialState = state;
}
void SerialSetPCAddress(uint32_t addr)
{
initPC_Address = addr;
}
void SerialSetRAMDestAddress(uint32_t addr)
{
RAMDest_Address = addr;
}
void SerialSetExeSize(size_t size)
{
ExeSize = size;
}
void SerialInit(void)
{
uint8_t receivedBytes;
SerialState = SERIAL_STATE_INIT;
dprintf("SerialInit...\n");
SetVBlankHandler(&ISR_Serial);
SIOStart(SERIAL_BAUDRATE);
SerialState = SERIAL_STATE_STANDBY;
// ------------------------------------
// Protocol description
// ------------------------------------
// 1. Wait to receive magic byte "99" from PC.
SerialRead(&receivedBytes, sizeof(uint8_t) );
if(receivedBytes != 99)
{
dprintf("Did not receive input magic number!\n");
SetVBlankHandler(&ISR_SystemDefaultVBlank);
return;
}
// 2. Send ACK (magic byte is ASCII code for 'b').
SerialState = SERIAL_STATE_WRITING_ACK;
SerialWrite(ACK_BYTE_STRING, sizeof(uint8_t) );
}
void SerialSetExeBytesReceived(uint32_t bytes_read)
{
exeBytesRead += bytes_read;
}
bool SerialRead(uint8_t* ptrArray, size_t nBytes)
{
bytesRead = 0;
totalBytes = nBytes;
serial_busy = true;
SystemDisableVBlankInterrupt();
if(nBytes == 0)
{
dprintf("SerialRead: invalid size %d\n", nBytes);
return false;
}
do
{
//uint16_t timeout = SERIAL_TX_RX_TIMEOUT;
while( (SIOCheckInBuffer() == SERIAL_RX_FIFO_EMPTY)); // Wait for RX FIFO not empty
*(ptrArray++) = SIOReadByte();
bytesRead++;
}while(--nBytes);
serial_busy = false;
SystemEnableVBlankInterrupt();
return true;
}
bool SerialWrite(void* ptrArray, size_t nBytes)
{
serial_busy = true;
SystemDisableVBlankInterrupt();
if(nBytes == 0)
{
dprintf("SerialWrite: invalid size %d\n", nBytes);
return false;
}
do
{
//uint16_t timeout = SERIAL_TX_RX_TIMEOUT;
while( (SIOCheckOutBuffer() == SERIAL_TX_NOT_READY)); // Wait for TX FIFO empty.
SIOSendByte(*(uint8_t*)ptrArray++);
}while(--nBytes);
serial_busy = false;
SystemEnableVBlankInterrupt();
return true;
}
|