image/svg+xml
Brteve's API for EveApps
Version 0.0.1
The reference document for common folder of EveApps project.
 
Loading...
Searching...
No Matches
EVE_HalImpl_BT8XXEMUC.c
Go to the documentation of this file.
1
32#ifdef _MSC_VER
33#pragma warning(push)
34#pragma warning(disable : 26812) // Unscoped enum
35#pragma warning(disable : 6262) // Large stack
36#endif
37
38#ifdef BT8XXEMU_REMOTE
39
40#include <bt8xxemu.h>
41#include <stdlib.h>
42
43#include <stdio.h>
44#include <assert.h>
45
46#ifdef _WIN32
47#ifndef NOMINMAX
48#define NOMINMAX
49#endif
50#if !defined(NTDDI_VERSION) && !defined(_WIN32_WINNT) && !defined(WINVER)
51#define NTDDI_VERSION 0x05010000
52#define _WIN32_WINNT 0x0501
53#define WINVER 0x0501
54#endif
55#ifndef WIN32_LEAN_AND_MEAN
56#define WIN32_LEAN_AND_MEAN
57#endif
58#include <Windows.h>
59#endif
60
61#define BUFSIZE 64 * 1024
62
63#define BT8XXEMU_PIPE_OPEN 0xFF02
64#define BT8XXEMU_PIPE_CLOSE 0xFF04
65#define BT8XXEMU_PIPE_ECHO 0xFF10
66
67#define BT8XXEMU_CALL_VERSION 0x00
68#define BT8XXEMU_CALL_DEFAULTS 0x01
69#define BT8XXEMU_CALL_RUN 0x02
70#define BT8XXEMU_CALL_STOP 0x03
71#define BT8XXEMU_CALL_DESTROY 0x04
72#define BT8XXEMU_CALL_IS_RUNNING 0x05
73#define BT8XXEMU_CALL_TRANSFER 0x06
74#define BT8XXEMU_CALL_CHIP_SELECT 0x07
75#define BT8XXEMU_CALL_HAS_INTERRUPT 0x08
76
77#define BT8XXEMU_CALL_FLASH_DEFAULTS 0x0101
78#define BT8XXEMU_CALL_FLASH_CREATE 0x0102
79#define BT8XXEMU_CALL_FLASH_DESTROY 0x0104
80#define BT8XXEMU_CALL_FLASH_TRANSFER_SPI4 0x0109
81
82typedef struct BT8XXEMUC_Remote BT8XXEMUC_Remote;
83struct BT8XXEMUC_Remote
84{
85 LONG atomicLock;
86 HANDLE pipe;
87 union
88 {
89 uint32_t flash;
90 uint32_t emulator;
91 };
92};
93
94#pragma pack(push, 1)
95typedef struct
96{
97 int32_t flags;
99 uint32_t mousePressure;
100 uint32_t externalFrequency;
101 uint32_t reduceGraphicsThreads;
102 wchar_t romFilePath[260];
103 wchar_t otpFilePath[260];
104 wchar_t coprocessorRomFilePath[260];
105 int64_t userContext;
106 int32_t flash;
107} BT8XXEMUC_RemoteEmulatorParameters;
108typedef struct
109{
110 wchar_t deviceType[26];
111 uint64_t sizeBytes;
112 wchar_t dataFilePath[260];
113 wchar_t statusFilePath[260];
114 int persistent;
115 int stdOut;
116 int64_t userContext;
117} BT8XXEMUC_RemoteFlashParameters;
118typedef union
119{
120 char buffer[BUFSIZE];
121 struct
122 {
123 uint32_t messageType;
124 union
125 {
126 uint32_t versionApi;
127 uint32_t emulator;
128 uint32_t flash;
129 };
130 union
131 {
132 struct
133 {
134 BT8XXEMUC_RemoteEmulatorParameters remoteParams;
136 };
137 // BT8XXEMU_FlashParameters flashParams;
138 BT8XXEMUC_RemoteFlashParameters remoteFlashParams;
139 char str[1024];
140 uint8_t data;
141 uint8_t signal;
142 uint8_t chipSelect;
143 uint8_t isRunning;
144 uint8_t hasInterrupt;
145 };
146 };
147} BT8XXEMUC_Data;
148#pragma pack(pop)
149
150#define MESSAGE_SIZE(param) (DWORD)((ptrdiff_t)(void *)(&(data.param)) + (ptrdiff_t)sizeof(data.param) - (ptrdiff_t)(void *)(&data.buffer[0]))
151#define STRING_MESSAGE_SIZE() (DWORD)((ptrdiff_t)(void *)(&data.str[0]) + (ptrdiff_t)strlen(&data.str[0]) + 1 - (ptrdiff_t)(void *)(&data.buffer[0]))
152
153static void BT8XXEMUC_readParams(BT8XXEMU_EmulatorParameters *params, const BT8XXEMUC_RemoteEmulatorParameters *remoteParams)
154{
155 memset(params, 0, sizeof(BT8XXEMU_EmulatorParameters));
156 params->Flags = remoteParams->flags;
157 params->Mode = remoteParams->mode;
158 params->MousePressure = remoteParams->mousePressure;
159 params->ReduceGraphicsThreads = remoteParams->reduceGraphicsThreads;
160 wcscpy(params->RomFilePath, remoteParams->romFilePath);
161 wcscpy(params->OtpFilePath, remoteParams->otpFilePath);
162 wcscpy(params->CoprocessorRomFilePath, remoteParams->coprocessorRomFilePath);
163 params->UserContext = (void *)(intptr_t)remoteParams->userContext;
164 assert(remoteParams->flash == 0);
165 // params->Flash = 0;
166}
167
168static void BT8XXEMUC_writeParams(BT8XXEMUC_RemoteEmulatorParameters *remoteParams, const BT8XXEMU_EmulatorParameters *params)
169{
170 remoteParams->flags = params->Flags;
171 remoteParams->mode = params->Mode;
172 remoteParams->mousePressure = params->MousePressure;
173 remoteParams->reduceGraphicsThreads = params->ReduceGraphicsThreads;
174 wcscpy(remoteParams->romFilePath, params->RomFilePath);
175 wcscpy(remoteParams->otpFilePath, params->OtpFilePath);
176 wcscpy(remoteParams->coprocessorRomFilePath, params->CoprocessorRomFilePath);
177 remoteParams->userContext = (int64_t)(intptr_t)params->UserContext;
178 remoteParams->flash = (params->Flash) ? params->Flash->flash : 0;
179}
180
181static void BT8XXEMUC_readFlashParams(BT8XXEMU_FlashParameters *flashParams, const BT8XXEMUC_RemoteFlashParameters *remoteFlashParams)
182{
183 wcscpy(flashParams->DeviceType, remoteFlashParams->deviceType);
184 flashParams->SizeBytes = remoteFlashParams->sizeBytes;
185 wcscpy(flashParams->DataFilePath, remoteFlashParams->dataFilePath);
186 wcscpy(flashParams->StatusFilePath, remoteFlashParams->statusFilePath);
187 flashParams->Persistent = remoteFlashParams->persistent;
188 flashParams->StdOut = remoteFlashParams->stdOut;
189}
190
191static void BT8XXEMUC_writeFlashParams(BT8XXEMUC_RemoteFlashParameters *remoteFlashParams, const BT8XXEMU_FlashParameters *flashParams)
192{
193 wcscpy(remoteFlashParams->deviceType, flashParams->DeviceType);
194 remoteFlashParams->sizeBytes = flashParams->SizeBytes;
195 wcscpy(remoteFlashParams->dataFilePath, flashParams->DataFilePath);
196 wcscpy(remoteFlashParams->statusFilePath, flashParams->StatusFilePath);
197 remoteFlashParams->persistent = flashParams->Persistent;
198 remoteFlashParams->stdOut = flashParams->StdOut;
199 remoteFlashParams->userContext = (int64_t)(intptr_t)flashParams->UserContext;
200}
201
202static LONG s_AtomicLock = 0;
203static LONG s_RefCount = 0;
204static HANDLE s_Process = INVALID_HANDLE_VALUE;
205static HANDLE s_ProcessThread = INVALID_HANDLE_VALUE;
206static HANDLE s_Pipe = INVALID_HANDLE_VALUE;
207static BT8XXEMUC_Data s_VersionData;
208static int s_PipeNb = 0;
209
210static void BT8XXEMUC_lockProcessPipe()
211{
212 while (InterlockedExchange(&s_AtomicLock, 1))
213 SwitchToThread();
214}
215
216static void BT8XXEMUC_unlockProcessPipe()
217{
218 InterlockedExchange(&s_AtomicLock, 0);
219}
220
222static bool BT8XXEMUC_openProcess()
223{
224 BT8XXEMUC_lockProcessPipe();
225
226 if (!s_RefCount)
227 {
228 char pipeHandle[MAX_PATH];
229 sprintf(pipeHandle, "\\\\.\\pipe\\bt8xxemus_%i", (int)GetCurrentProcessId());
230
231 s_Pipe = CreateNamedPipeA(pipeHandle, PIPE_ACCESS_DUPLEX,
232 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
233 1, BUFSIZE, BUFSIZE, 1000, NULL);
234
235 if (s_Pipe == INVALID_HANDLE_VALUE)
236 {
237 BT8XXEMUC_unlockProcessPipe();
238 return false;
239 }
240
241 STARTUPINFO si = { 0 };
242 PROCESS_INFORMATION pi = { 0 };
243 char processCommand[MAX_PATH];
244 sprintf(processCommand, "bt8xxemus.exe %s", pipeHandle);
245 if (!CreateProcessA(NULL, processCommand, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
246 {
247 CloseHandle(s_Pipe);
248 s_Pipe = INVALID_HANDLE_VALUE;
249 BT8XXEMUC_unlockProcessPipe();
250 return false;
251 }
252
253 s_Process = pi.hProcess;
254 s_ProcessThread = pi.hThread;
255
256 if (!ConnectNamedPipe(s_Pipe, NULL))
257 {
258 CloseHandle(s_Pipe);
259 s_Pipe = INVALID_HANDLE_VALUE;
260 TerminateProcess(s_Process, EXIT_FAILURE);
261 CloseHandle(s_Process);
262 s_Process = INVALID_HANDLE_VALUE;
263 CloseHandle(s_ProcessThread);
264 s_ProcessThread = INVALID_HANDLE_VALUE;
265 BT8XXEMUC_unlockProcessPipe();
266 return false;
267 }
268 }
269 ++s_RefCount;
270
271 BT8XXEMUC_unlockProcessPipe();
272 return true;
273}
274
276static void BT8XXEMUC_closeProcess()
277{
278 BT8XXEMUC_lockProcessPipe();
279
280 --s_RefCount;
281 if (!s_RefCount)
282 {
283 DWORD nb;
284 DWORD len;
285 BT8XXEMUC_Data data;
286
287 data.messageType = BT8XXEMU_PIPE_CLOSE;
288 len = MESSAGE_SIZE(messageType);
289
290 if (!WriteFile(s_Pipe, data.buffer, len, &nb, NULL) || len != nb)
291 {
292 CloseHandle(s_Pipe);
293 s_Pipe = INVALID_HANDLE_VALUE;
294 TerminateProcess(s_Process, EXIT_FAILURE);
295 CloseHandle(s_Process);
296 s_Process = INVALID_HANDLE_VALUE;
297 CloseHandle(s_ProcessThread);
298 s_ProcessThread = INVALID_HANDLE_VALUE;
299 BT8XXEMUC_unlockProcessPipe();
300 return;
301 }
302
303 CloseHandle(s_Pipe);
304 s_Pipe = INVALID_HANDLE_VALUE;
305
306 WaitForSingleObject(s_Process, INFINITE);
307 CloseHandle(s_Process);
308 s_Process = INVALID_HANDLE_VALUE;
309 CloseHandle(s_ProcessThread);
310 s_ProcessThread = INVALID_HANDLE_VALUE;
311 }
312
313 BT8XXEMUC_unlockProcessPipe();
314}
315
319static HANDLE BT8XXEMUC_openPipe()
320{
321 BT8XXEMUC_lockProcessPipe();
322
323 ++s_PipeNb;
324
325 DWORD nb;
326 DWORD len;
327 BT8XXEMUC_Data data;
328
329 data.messageType = BT8XXEMU_PIPE_OPEN;
330 data.versionApi = BT8XXEMU_VERSION_API;
331 sprintf(data.str, "\\\\.\\pipe\\bt8xxemus_%i_%i", (int)GetCurrentProcessId(), s_PipeNb);
332 len = STRING_MESSAGE_SIZE();
333
334 HANDLE pipe = CreateNamedPipeA(data.str, PIPE_ACCESS_DUPLEX,
335 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
336 1, BUFSIZE, BUFSIZE, 1000, NULL);
337
338 if (pipe == INVALID_HANDLE_VALUE
339 || !WriteFile(s_Pipe, data.buffer, len, &nb, NULL) || len != nb
340 || !ReadFile(s_Pipe, data.buffer, BUFSIZE, &nb, NULL)
341 || !ConnectNamedPipe(pipe, NULL))
342 {
343 BT8XXEMUC_unlockProcessPipe();
345 }
346
347 BT8XXEMUC_unlockProcessPipe();
348 return pipe;
349}
350
351static void BT8XXEMUC_lockPipe(BT8XXEMUC_Remote *emulator)
352{
353 while (InterlockedExchange(&emulator->atomicLock, 1))
354 SwitchToThread();
355}
356
357static void BT8XXEMUC_unlockPipe(BT8XXEMUC_Remote *emulator)
358{
359 InterlockedExchange(&emulator->atomicLock, 0);
360}
361
363static void BT8XXEMUC_closePipe(BT8XXEMUC_Remote *emulator)
364{
365 BT8XXEMUC_lockPipe(emulator);
366
367 DWORD nb;
368 DWORD len;
369 BT8XXEMUC_Data data;
370
371 data.messageType = BT8XXEMU_PIPE_CLOSE;
372 len = MESSAGE_SIZE(messageType);
373 WriteFile(emulator->pipe, data.buffer, len, &nb, NULL);
374 CloseHandle(emulator->pipe);
375 emulator->pipe = INVALID_HANDLE_VALUE;
376
377 BT8XXEMUC_unlockPipe(emulator);
378}
379
380const char *BT8XXEMU_version()
381{
382 if (BT8XXEMUC_openProcess())
383 {
384 BT8XXEMUC_lockProcessPipe();
385
386 DWORD nb;
387 DWORD len;
388
389 s_VersionData.messageType = BT8XXEMU_CALL_VERSION;
390 s_VersionData.versionApi = BT8XXEMU_VERSION_API;
391
392 ;
393 {
394 BT8XXEMUC_Data data;
395 len = MESSAGE_SIZE(versionApi);
396 }
397
398 if (!WriteFile(s_Pipe, s_VersionData.buffer, len, &nb, NULL) || len != nb
399 || !ReadFile(s_Pipe, s_VersionData.buffer, BUFSIZE, &nb, NULL))
400 {
401 BT8XXEMUC_unlockProcessPipe();
402 BT8XXEMUC_closeProcess();
403 return "BT8XX Emulator Library\nNot Responding";
404 }
405
406 BT8XXEMUC_unlockProcessPipe();
407 BT8XXEMUC_closeProcess();
408
409 return s_VersionData.str;
410 }
411
412 return "BT8XX Emulator Library\nNot Installed";
413}
414
416{
417 if (BT8XXEMUC_openProcess())
418 {
419 BT8XXEMUC_lockProcessPipe();
420
421 DWORD nb;
422 DWORD len;
423 BT8XXEMUC_Data data;
424
425 data.messageType = BT8XXEMU_CALL_DEFAULTS;
426 data.versionApi = BT8XXEMU_VERSION_API;
427 // BT8XXEMUC_writeParams(&data.remoteParams, params);
428 memset(&data.remoteParams, 0, sizeof(BT8XXEMUC_RemoteEmulatorParameters));
429 data.mode = mode;
430 len = MESSAGE_SIZE(mode);
431
432 if (!WriteFile(s_Pipe, data.buffer, len, &nb, NULL) || len != nb
433 || !ReadFile(s_Pipe, data.buffer, BUFSIZE, &nb, NULL))
434 {
435 BT8XXEMUC_unlockProcessPipe();
436 BT8XXEMUC_closeProcess();
437 memset(params, 0, sizeof(BT8XXEMU_EmulatorParameters));
438 return;
439 }
440
441 BT8XXEMUC_unlockProcessPipe();
442 BT8XXEMUC_closeProcess();
443
444 BT8XXEMUC_readParams(params, &data.remoteParams);
445 return;
446 }
447
448 memset(params, 0, sizeof(BT8XXEMU_EmulatorParameters));
449}
450
451void BT8XXEMU_run(uint32_t versionApi, BT8XXEMU_Emulator **emulator, const BT8XXEMU_EmulatorParameters *params)
452{
453 /*
454 if (params->Close) printf("Close callback is not permitted in service mode\n");
455 if (params->Graphics) printf("Graphics callback is not permitted in service mode\n");
456 if (params->Log) printf("Log callback is not permitted in service mode\n");
457 // if (params.Main) printf("Main callback is not permitted in service mode\n");
458 if (params->MCUSleep) printf("MCUSleep callback is not permitted in service mode\n");
459 */
460
461 if (params->Close
462 || params->Graphics
463 || params->Log
464 || params->MCUSleep)
465 {
466 // TODO: Callback handling
467 *emulator = NULL;
468 return;
469 }
470
471 if (BT8XXEMUC_openProcess())
472 {
473 *emulator = malloc(sizeof(BT8XXEMU_Emulator));
474 if (!*emulator)
475 {
476 BT8XXEMUC_closeProcess();
477 return;
478 }
479 memset(*emulator, 0, sizeof(BT8XXEMU_Emulator));
480 (*emulator)->pipe = BT8XXEMUC_openPipe(); // Create a separate pipe for each emulator instance
481
482 if ((*emulator)->pipe == INVALID_HANDLE_VALUE)
483 {
484 BT8XXEMUC_closeProcess();
485 free(*emulator);
486 *emulator = NULL;
487 return;
488 }
489
490 DWORD nb;
491 DWORD len;
492 BT8XXEMUC_Data data;
493
494 data.messageType = BT8XXEMU_CALL_RUN;
495 data.versionApi = BT8XXEMU_VERSION_API;
496 BT8XXEMUC_writeParams(&data.remoteParams, params);
497 len = MESSAGE_SIZE(remoteParams);
498
499 // data.params.Main = NULL;
500 // data.params.Close = NULL; // Temporary
501
502 if (!WriteFile((*emulator)->pipe, data.buffer, len, &nb, NULL) || len != nb
503 || !ReadFile((*emulator)->pipe, data.buffer, BUFSIZE, &nb, NULL))
504 {
505 BT8XXEMUC_closePipe(*emulator);
506 BT8XXEMUC_closeProcess();
507 free(*emulator);
508 *emulator = NULL;
509 return;
510 }
511
512 (*emulator)->emulator = data.emulator;
513
514 if (params->Main)
515 {
516 params->Main((*emulator), params->UserContext);
517 }
518
519 return;
520 }
521
522 *emulator = NULL;
523}
524
525void BT8XXEMU_stop(BT8XXEMU_Emulator *emulator)
526{
527 BT8XXEMUC_lockPipe(emulator);
528
529 DWORD nb;
530 DWORD len;
531 BT8XXEMUC_Data data;
532
533 data.messageType = BT8XXEMU_CALL_STOP;
534 data.emulator = emulator->emulator;
535 len = MESSAGE_SIZE(emulator);
536
537 if (!WriteFile(emulator->pipe, data.buffer, len, &nb, NULL) || len != nb
538 || !ReadFile(emulator->pipe, data.buffer, BUFSIZE, &nb, NULL))
539 {
540 // ...
541 }
542
543 BT8XXEMUC_unlockPipe(emulator);
544}
545
547{
548 BT8XXEMUC_lockPipe(emulator);
549
550 DWORD nb;
551 DWORD len;
552 BT8XXEMUC_Data data;
553
554 data.messageType = BT8XXEMU_CALL_DESTROY;
555 data.emulator = emulator->emulator;
556 len = MESSAGE_SIZE(emulator);
557
558 if (!WriteFile(emulator->pipe, data.buffer, len, &nb, NULL) || len != nb
559 || !ReadFile(emulator->pipe, data.buffer, BUFSIZE, &nb, NULL))
560 {
561 // ...
562 }
563
564 BT8XXEMUC_unlockPipe(emulator);
565
566 BT8XXEMUC_closePipe(emulator);
567 BT8XXEMUC_closeProcess();
568 free(emulator);
569}
570
572{
573 BT8XXEMUC_lockPipe(emulator);
574
575 DWORD nb;
576 DWORD len;
577 BT8XXEMUC_Data data;
578
579 data.messageType = BT8XXEMU_CALL_IS_RUNNING;
580 data.emulator = emulator->emulator;
581 len = MESSAGE_SIZE(emulator);
582
583 if (!WriteFile(emulator->pipe, data.buffer, len, &nb, NULL) || len != nb
584 || !ReadFile(emulator->pipe, data.buffer, BUFSIZE, &nb, NULL))
585 {
586 BT8XXEMUC_unlockPipe(emulator);
587 return 0;
588 }
589
590 BT8XXEMUC_unlockPipe(emulator);
591 return data.isRunning;
592}
593
595{
596 BT8XXEMUC_lockPipe(emulator);
597
598 DWORD nb;
599 DWORD len;
600 BT8XXEMUC_Data data;
601
602 data.messageType = BT8XXEMU_CALL_TRANSFER;
603 data.emulator = emulator->emulator;
604 data.data = value;
605 len = MESSAGE_SIZE(data);
606
607 if (!WriteFile(emulator->pipe, data.buffer, len, &nb, NULL) || len != nb
608 || !ReadFile(emulator->pipe, data.buffer, BUFSIZE, &nb, NULL))
609 {
610 BT8XXEMUC_unlockPipe(emulator);
611 return 0;
612 }
613
614 BT8XXEMUC_unlockPipe(emulator);
615 return data.data;
616}
617
618void BT8XXEMU_chipSelect(BT8XXEMU_Emulator *emulator, int cs)
619{
620 BT8XXEMUC_lockPipe(emulator);
621
622 DWORD nb;
623 DWORD len;
624 BT8XXEMUC_Data data;
625
626 data.messageType = BT8XXEMU_CALL_CHIP_SELECT;
627 data.emulator = emulator->emulator;
628 data.chipSelect = cs;
629 len = MESSAGE_SIZE(chipSelect);
630
631 if (!WriteFile(emulator->pipe, data.buffer, len, &nb, NULL) || len != nb
632 || !ReadFile(emulator->pipe, data.buffer, BUFSIZE, &nb, NULL))
633 {
634 // ...
635 }
636
637 BT8XXEMUC_unlockPipe(emulator);
638}
639
641{
642 BT8XXEMUC_lockPipe(emulator);
643
644 DWORD nb;
645 DWORD len;
646 BT8XXEMUC_Data data;
647
648 data.messageType = BT8XXEMU_CALL_HAS_INTERRUPT;
649 data.emulator = emulator->emulator;
650 len = MESSAGE_SIZE(emulator);
651
652 if (!WriteFile(emulator->pipe, data.buffer, len, &nb, NULL) || len != nb
653 || !ReadFile(emulator->pipe, data.buffer, BUFSIZE, &nb, NULL))
654 {
655 BT8XXEMUC_unlockPipe(emulator);
656 return 0;
657 }
658
659 BT8XXEMUC_unlockPipe(emulator);
660 return data.hasInterrupt;
661}
662
664{
665 if (BT8XXEMUC_openProcess())
666 {
667 BT8XXEMUC_lockProcessPipe();
668
669 DWORD nb;
670 DWORD len;
671 BT8XXEMUC_Data data;
672
673 data.messageType = BT8XXEMU_CALL_FLASH_DEFAULTS;
674 data.versionApi = BT8XXEMU_VERSION_API;
675 // BT8XXEMUC_writeFlashParams(&data.remoteFlashParams, params);
676 memset(&data.remoteFlashParams, 0, sizeof(BT8XXEMUC_RemoteFlashParameters));
677 len = MESSAGE_SIZE(remoteFlashParams);
678
679 if (!WriteFile(s_Pipe, data.buffer, len, &nb, NULL) || len != nb
680 || !ReadFile(s_Pipe, data.buffer, BUFSIZE, &nb, NULL))
681 {
682 BT8XXEMUC_unlockProcessPipe();
683 BT8XXEMUC_closeProcess();
684 memset(params, 0, sizeof(BT8XXEMU_FlashParameters));
685 return;
686 }
687
688 BT8XXEMUC_unlockProcessPipe();
689 BT8XXEMUC_closeProcess();
690
691 BT8XXEMUC_readFlashParams(params, &data.remoteFlashParams);
692 return;
693 }
694
695 memset(params, 0, sizeof(BT8XXEMU_FlashParameters));
696}
697
699{
700 if (BT8XXEMUC_openProcess())
701 {
702 BT8XXEMU_Flash *flash = malloc(sizeof(BT8XXEMU_Flash));
703 if (!flash)
704 return NULL;
705 memset(flash, 0, sizeof(BT8XXEMU_Flash));
706 flash->pipe = BT8XXEMUC_openPipe(); // Create a separate pipe for each emulator instance
707
708 if (flash->pipe == INVALID_HANDLE_VALUE)
709 {
710 BT8XXEMUC_closeProcess();
711 free(flash);
712 return NULL;
713 }
714
715 DWORD nb;
716 DWORD len;
717 BT8XXEMUC_Data data;
718
719 data.messageType = BT8XXEMU_CALL_FLASH_CREATE;
720 data.versionApi = BT8XXEMU_VERSION_API;
721 BT8XXEMUC_writeFlashParams(&data.remoteFlashParams, params);
722 len = MESSAGE_SIZE(remoteFlashParams);
723
724 if (!WriteFile(flash->pipe, data.buffer, len, &nb, NULL) || len != nb
725 || !ReadFile(flash->pipe, data.buffer, BUFSIZE, &nb, NULL))
726 {
727 BT8XXEMUC_closePipe(flash);
728 BT8XXEMUC_closeProcess();
729 free(flash);
730 return NULL;
731 }
732
733 flash->flash = data.flash;
734
735 return flash;
736 }
737
738 return NULL;
739}
740
742{
743 BT8XXEMUC_lockPipe(flash);
744
745 DWORD nb;
746 DWORD len;
747 BT8XXEMUC_Data data;
748
749 data.messageType = BT8XXEMU_CALL_FLASH_DESTROY;
750 data.flash = flash->flash;
751 len = MESSAGE_SIZE(emulator);
752
753 if (!WriteFile(flash->pipe, data.buffer, len, &nb, NULL) || len != nb
754 || !ReadFile(flash->pipe, data.buffer, BUFSIZE, &nb, NULL))
755 {
756 // ...
757 }
758
759 BT8XXEMUC_unlockPipe(flash);
760
761 BT8XXEMUC_closePipe(flash);
762 BT8XXEMUC_closeProcess();
763 free(flash);
764}
765
767{
768 BT8XXEMUC_lockPipe(flash);
769
770 DWORD nb;
771 DWORD len;
772 BT8XXEMUC_Data data;
773
774 data.messageType = BT8XXEMU_CALL_FLASH_TRANSFER_SPI4;
775 data.flash = flash->flash;
776 data.signal = signal;
777 len = MESSAGE_SIZE(signal);
778
779 if (!WriteFile(flash->pipe, data.buffer, len, &nb, NULL) || len != nb
780 || !ReadFile(flash->pipe, data.buffer, BUFSIZE, &nb, NULL))
781 {
782 BT8XXEMUC_unlockPipe(flash);
783 return 0;
784 }
785
786 BT8XXEMUC_unlockPipe(flash);
787 return data.data;
788}
789
790#endif
791
792#ifdef _MSC_VER
793#pragma warning(pop)
794#endif
795
796/* end of file */
int int32_t
unsigned int uint32_t
long long int64_t
unsigned long long uint64_t
unsigned char uint8_t
static ft_uint32_t ft_uint8_t * buffer
Definition FT_Gpu_Hal.h:139
static ft_void_t ft_uint32_t ft_size_t nb
Definition FT_Gpu_Hal.h:185
#define INVALID_HANDLE_VALUE
Definition WinTypes.h:117
void * HANDLE
Definition WinTypes.h:54
unsigned int LONG
Definition WinTypes.h:55
#define FALSE
Definition Platform.h:198
BT8XX Emulator Library.
BT8XXEMU_API uint8_t BT8XXEMU_Flash_transferSpi4(BT8XXEMU_Flash *flash, uint8_t signal)
BT8XXEMU_API void BT8XXEMU_defaults(uint32_t versionApi, BT8XXEMU_EmulatorParameters *params, BT8XXEMU_EmulatorMode mode)
BT8XXEMU_API void BT8XXEMU_Flash_defaults(uint32_t versionApi, BT8XXEMU_FlashParameters *params)
BT8XXEMU_API int BT8XXEMU_isRunning(BT8XXEMU_Emulator *emulator)
#define BT8XXEMU_VERSION_API
Definition bt8xxemu.h:52
BT8XXEMU_API void BT8XXEMU_stop(BT8XXEMU_Emulator *emulator)
BT8XXEMU_API void BT8XXEMU_Flash_destroy(BT8XXEMU_Flash *flash)
BT8XXEMU_API BT8XXEMU_Flash * BT8XXEMU_Flash_create(uint32_t versionApi, const BT8XXEMU_FlashParameters *params)
BT8XXEMU_EmulatorMode
Definition bt8xxemu.h:111
BT8XXEMU_API int BT8XXEMU_hasInterrupt(BT8XXEMU_Emulator *emulator)
BT8XXEMU_API uint8_t BT8XXEMU_transfer(BT8XXEMU_Emulator *emulator, uint8_t data)
BT8XXEMU_API void BT8XXEMU_destroy(BT8XXEMU_Emulator *emulator)
BT8XXEMU_API void BT8XXEMU_chipSelect(BT8XXEMU_Emulator *, int cs)
BT8XXEMU_API const char * BT8XXEMU_version()
void BT8XXEMU_Flash
Definition bt8xxemu.h:99
void BT8XXEMU_Emulator
Definition bt8xxemu.h:98
BT8XXEMU_API void BT8XXEMU_run(uint32_t versionApi, BT8XXEMU_Emulator **emulator, const BT8XXEMU_EmulatorParameters *params)
unsigned long DWORD
Definition ff.h:61
void(* Close)(BT8XXEMU_Emulator *sender, void *context)
Definition bt8xxemu.h:233
eve_tchar_t OtpFilePath[260]
Definition bt8xxemu.h:202
void(* MCUSleep)(BT8XXEMU_Emulator *sender, void *context, int ms)
Definition bt8xxemu.h:195
BT8XXEMU_EmulatorMode Mode
Definition bt8xxemu.h:184
int(* Graphics)(BT8XXEMU_Emulator *sender, void *context, int output, const argb8888 *buffer, uint32_t hsize, uint32_t vsize, BT8XXEMU_FrameFlags flags)
Definition bt8xxemu.h:224
eve_tchar_t RomFilePath[260]
Definition bt8xxemu.h:199
void(* Log)(BT8XXEMU_Emulator *sender, void *context, BT8XXEMU_LogType type, const char *message)
Definition bt8xxemu.h:230
void(* Main)(BT8XXEMU_Emulator *sender, void *context)
Definition bt8xxemu.h:180
eve_tchar_t CoprocessorRomFilePath[260]
Definition bt8xxemu.h:205
BT8XXEMU_Flash * Flash
Definition bt8xxemu.h:239
eve_tchar_t DataFilePath[260]
Definition bt8xxemu.h:252
eve_tchar_t DeviceType[26]
Definition bt8xxemu.h:246
eve_tchar_t StatusFilePath[260]
Definition bt8xxemu.h:255