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_LoadFile_STDIO.c
Go to the documentation of this file.
1
32#include "EVE_LoadFile.h"
33#include "EVE_Platform.h"
34#if !defined(FT9XX_PLATFORM) && !defined(RP2040_PLATFORM)
35
36#include <stdio.h>
37
46{
47 /* no-op */
48 return true;
49}
50
52{
53 /* no-op */
54 return true;
55}
56
66#ifdef _WIN32
67static bool loadRawFile(EVE_HalContext *phost, uint32_t address, const char *filename, const wchar_t *filenameW)
68#else
69EVE_HAL_EXPORT bool EVE_Util_loadRawFile(EVE_HalContext *phost, uint32_t address, const char *filename)
70#endif
71{
72 FILE *afile;
73 uint32_t ftsize = 0;
74 uint8_t pbuff[8192];
75 uint16_t blocklen;
76 uint32_t addr = address;
77 errno_t err = 0;
78
79#ifdef _WIN32
80 err = filename ? fopen_s(&afile, filename, "rb") : _wfopen_s(&afile, filenameW, L"rb");
81#else
82 err = fopen_s(&afile, filename, "rb"); // read Binary (rb)
83#endif
84 if (err || afile == NULL)
85 {
86#ifdef _WIN32
87 if (!filename)
88 eve_printf_debug("Unable to open: %ls\n", filenameW);
89 else
90#endif
91 eve_printf_debug("Unable to open: %s\n", filename);
92 return false;
93 }
94 fseek(afile, 0, SEEK_END);
95 ftsize = ftell(afile);
96 fseek(afile, 0, SEEK_SET);
97 while (ftsize > 0)
98 {
99 blocklen = ftsize > 8192 ? 8192 : (uint16_t)ftsize;
100 blocklen = (uint16_t)fread(pbuff, 1, blocklen, afile);
101 ftsize -= blocklen;
102 EVE_Hal_wrMem(phost, addr, pbuff, blocklen);
103 addr += blocklen;
104 }
105 fclose(afile);
106
107 return true;
108}
109
110#ifdef _WIN32
111
112EVE_HAL_EXPORT bool EVE_Util_loadRawFile(EVE_HalContext *phost, uint32_t address, const char *filename)
113{
114 return loadRawFile(phost, address, filename, NULL);
115}
116
117EVE_HAL_EXPORT bool EVE_Util_loadRawFileW(EVE_HalContext *phost, uint32_t address, const wchar_t *filename)
118{
119 return loadRawFile(phost, address, NULL, filename);
120}
121
122#endif
123
133#ifdef _WIN32
134static bool loadInflateFile(EVE_HalContext *phost, uint32_t address, const char *filename, const wchar_t *filenameW)
135#else
136EVE_HAL_EXPORT bool EVE_Util_loadInflateFile(EVE_HalContext *phost, uint32_t address, const char *filename)
137#endif
138{
139 FILE *afile;
140 uint32_t ftsize = 0;
141 uint8_t pbuff[8192];
142 uint16_t blocklen;
143 errno_t err = 0;
144
145 if (!EVE_Cmd_waitSpace(phost, 8))
146 return false; // Space for CMD_INFLATE
147
148#ifdef _WIN32
149 // afile = filename ? fopen(filename, "rb") : _wfopen(filenameW, L"rb");
150 err = filename ? fopen_s(&afile, filename, "rb") : _wfopen_s(&afile, filenameW, L"rb");
151#else
152 err = fopen_s(&afile, filename, "rb"); // read Binary (rb)
153#endif
154 if (err || afile == NULL)
155 {
156#ifdef _WIN32
157 if (!filename)
158 eve_printf_debug("Unable to open: %ls\n", filenameW);
159 else
160#endif
161 eve_printf_debug("Unable to open: %s\n", filename);
162 return false;
163 }
165 EVE_Cmd_wr32(phost, address);
166 fseek(afile, 0, SEEK_END);
167 ftsize = ftell(afile);
168 fseek(afile, 0, SEEK_SET);
169 while (ftsize > 0)
170 {
171 blocklen = ftsize > 8192 ? 8192 : (uint16_t)ftsize;
172 blocklen = (uint16_t)fread(pbuff, 1, blocklen, afile); /* copy the data into pbuff and then transfter it to command buffer */
173 ftsize -= blocklen;
174 blocklen += 3;
175 blocklen &= ~3U;
176
177 if (!EVE_Cmd_wrMem(phost, pbuff, blocklen)) /* copy data continuously into command memory */
178 break;
179 }
180
181 fclose(afile); /* close the opened compressed file */
182
183 return EVE_Cmd_waitFlush(phost);
184}
185
186#ifdef _WIN32
187
188EVE_HAL_EXPORT bool EVE_Util_loadInflateFile(EVE_HalContext *phost, uint32_t address, const char *filename)
189{
190 return loadInflateFile(phost, address, filename, NULL);
191}
192
193EVE_HAL_EXPORT bool EVE_Util_loadInflateFileW(EVE_HalContext *phost, uint32_t address, const wchar_t *filename)
194{
195 return loadInflateFile(phost, address, NULL, filename);
196}
197
198#endif
199
210#ifdef _WIN32
211static bool loadImageFile(EVE_HalContext *phost, uint32_t address, const char *filename, const wchar_t *filenameW, uint32_t *format)
212#else
213EVE_HAL_EXPORT bool EVE_Util_loadImageFile(EVE_HalContext *phost, uint32_t address, const char *filename, uint32_t *format)
214#endif
215{
216 FILE *afile;
217 uint32_t ftsize = 0;
218 uint8_t pbuff[8192];
219 uint16_t blocklen;
220 errno_t err = 0;
221
222 if (phost->CmdFault)
223 return false;
224
225#ifdef _WIN32
226 // afile = filename ? fopen(filename, "rb") : _wfopen(filenameW, L"rb");
227 err = filename ? fopen_s(&afile, filename, "rb") : _wfopen_s(&afile, filenameW, L"rb");
228#else
229 err = fopen_s(&afile, filename, "rb"); // read Binary (rb)
230#endif
231 if (err || afile == NULL)
232 {
233#ifdef _WIN32
234 if (!filename)
235 eve_printf_debug("Unable to open: %ls\n", filenameW);
236 else
237#endif
238 eve_printf_debug("Unable to open: %s\n", filename);
239 return 0;
240 }
242 EVE_Cmd_wr32(phost, address);
243 EVE_Cmd_wr32(phost, OPT_NODL);
244 // TODO: Let it write into the scratch display list handle,
245 // and read it out and write into the bitmapInfo the proper
246 // values to use. Replace compressed bool with uint8 enum to
247 // specify the loading mechanism
248 fseek(afile, 0, SEEK_END);
249 ftsize = ftell(afile);
250 fseek(afile, 0, SEEK_SET);
251 while (ftsize > 0)
252 {
253 blocklen = ftsize > 8192 ? 8192 : (uint16_t)ftsize;
254 blocklen = (uint16_t)fread(pbuff, 1, blocklen, afile); /* copy the data into pbuff and then transfter it to command buffer */
255 ftsize -= blocklen;
256 blocklen += 3;
257 blocklen &= ~3U;
258
259 if (!EVE_Cmd_wrMem(phost, pbuff, blocklen))
260 break;
261 }
262
263 fclose(afile); /* close the opened jpg file */
264
265 if (!EVE_Cmd_waitFlush(phost))
266 return false;
267
268 if (format)
269 *format = EVE_Hal_rd32(phost, 0x3097e8);
270
271 return true;
272}
273
274#ifdef _WIN32
275
276EVE_HAL_EXPORT bool EVE_Util_loadImageFile(EVE_HalContext *phost, uint32_t address, const char *filename, uint32_t *format)
277{
278 return loadImageFile(phost, address, filename, NULL, format);
279}
280
281EVE_HAL_EXPORT bool EVE_Util_loadImageFileW(EVE_HalContext *phost, uint32_t address, const wchar_t *filename, uint32_t *format)
282{
283 return loadImageFile(phost, address, NULL, filename, format);
284}
285
286#endif
287
288#ifdef _WIN32
289static bool loadCmdFile(EVE_HalContext *phost, const char *filename, const wchar_t *filenameW, uint32_t *transfered)
290#else
291EVE_HAL_EXPORT bool EVE_Util_loadCmdFile(EVE_HalContext *phost, const char *filename, uint32_t *transfered)
292#endif
293{
294 FILE *afile;
295 uint32_t ftsize = 0;
296 uint8_t pbuff[8192];
297 uint16_t blocklen;
298 errno_t err = 0;
299
300#ifdef _WIN32
301 // afile = filename ? fopen(filename, "rb") : _wfopen(filenameW, L"rb");
302 err = filename ? fopen_s(&afile, filename, "rb") : _wfopen_s(&afile, filenameW, L"rb");
303#else
304 err = fopen_s(&afile, filename, "rb"); // read Binary (rb)
305#endif
306 if (err || afile == NULL)
307 {
308#ifdef _WIN32
309 if (!filename)
310 eve_printf_debug("Unable to open: %ls\n", filenameW);
311 else
312#endif
313 eve_printf_debug("Unable to open: %s\n", filename);
314 return false;
315 }
316 fseek(afile, 0, SEEK_END);
317 ftsize = ftell(afile);
318 fseek(afile, 0, SEEK_SET);
319 while (ftsize > 0)
320 {
321 blocklen = ftsize > 8192 ? 8192 : (uint16_t)ftsize;
322 blocklen = (uint16_t)fread(pbuff, 1, blocklen, afile); /* copy the data into pbuff and then transfter it to command buffer */
323 ftsize -= blocklen;
324 blocklen += 3;
325 blocklen &= ~3U;
326 if (!EVE_Cmd_wrMem(phost, pbuff, blocklen)) /* copy data continuously into command memory */
327 break;
328 if (transfered)
329 *transfered += blocklen;
330 }
331
332 fclose(afile); /* close the opened file */
333
334 return EVE_Cmd_waitFlush(phost);
335}
336
337#ifdef _WIN32
338
339EVE_HAL_EXPORT bool EVE_Util_loadCmdFile(EVE_HalContext *phost, const char *filename, uint32_t *transfered)
340{
341 return loadCmdFile(phost, filename, NULL, transfered);
342}
343
344EVE_HAL_EXPORT bool EVE_Util_loadCmdFileW(EVE_HalContext *phost, const wchar_t *filename, uint32_t *transfered)
345{
346 return loadCmdFile(phost, NULL, filename, transfered);
347}
348
349#endif
350
351#ifdef _WIN32
352static size_t readFile(EVE_HalContext *phost, uint8_t *buffer, size_t size, const char *filename, const wchar_t *filenameW)
353#else
354EVE_HAL_EXPORT size_t EVE_Util_readFile(EVE_HalContext *phost, uint8_t *buffer, size_t size, const char *filename)
355#endif
356{
357 // Read up to `size` number of bytes from the file into `buffer`, then return the number of read bytes
358 FILE *afile;
359 size_t read;
360 errno_t err = 0;
361
362#ifdef _WIN32
363 // afile = filename ? fopen(filename, "rb") : _wfopen(filenameW, L"rb");
364 err = filename ? fopen_s(&afile, filename, "rb") : _wfopen_s(&afile, filenameW, L"rb");
365#else
366 err = fopen_s(&afile, filename, "rb"); // read Binary (rb)
367#endif
368 if (err || afile == NULL)
369 {
370#ifdef _WIN32
371 if (!filename)
372 eve_printf_debug("Unable to open: %ls\n", filenameW);
373 else
374#endif
375 eve_printf_debug("Unable to open: %s\n", filename);
376 return 0;
377 }
378 read = fread(buffer, 1, size, afile);
379 fclose(afile);
380 return read;
381}
382
383#ifdef _WIN32
384
385EVE_HAL_EXPORT size_t EVE_Util_readFile(EVE_HalContext *phost, uint8_t *buffer, size_t size, const char *filename)
386{
387 return readFile(phost, buffer, size, filename, NULL);
388}
389
390EVE_HAL_EXPORT size_t EVE_Util_readFileW(EVE_HalContext *phost, uint8_t *buffer, size_t size, const wchar_t *filename)
391{
392 return readFile(phost, buffer, size, NULL, filename);
393}
394
395#endif
396
397#if (EVE_SUPPORT_CHIPID >= EVE_FT810)
398#ifdef _WIN32
399static bool loadMediaFile(EVE_HalContext *phost, const char *filename, const wchar_t *filenameW, uint32_t *transfered)
400#else
401EVE_HAL_EXPORT bool EVE_Util_loadMediaFile(EVE_HalContext *phost, const char *filename, uint32_t *transfered)
402#endif
403{
404 FILE *afile;
405 uint32_t remaining = 0;
406 uint32_t blockSize = ((phost->MediaFifoSize >> 3) << 2) - 4;
407#pragma warning(push)
408#pragma warning(disable : 6255)
409 uint8_t *pbuff = (uint8_t *)_alloca(blockSize);
410#pragma warning(pop)
411 uint16_t blocklen;
412 errno_t err = 0;
413 if (!transfered)
414 EVE_Util_closeFile(phost);
415 if (phost->CmdFault)
416 return false;
417 if (phost->LoadFileHandle)
418 {
419 afile = phost->LoadFileHandle;
420 }
421#ifdef _WIN32
422 else if (!filename)
423 {
424 err = _wfopen_s(&afile, filenameW, L"rb");
425 }
426#endif
427 else
428 {
429 err = fopen_s(&afile, filename, "rb");
430 }
431 if (err || afile == NULL)
432 {
433#ifdef _WIN32
434 if (!filename)
435 eve_printf_debug("Unable to open: %ls\n", filenameW);
436 else
437#endif
438 eve_printf_debug("Unable to open: %s\n", filename);
439 return false;
440 }
441 if (!phost->LoadFileHandle)
442 {
443 fseek(afile, 0, SEEK_END);
444 remaining = ftell(afile);
445 fseek(afile, transfered ? *transfered : 0, SEEK_SET);
446 if (transfered)
447 phost->LoadFileHandle = afile;
448 }
449 else
450 {
451 remaining = (uint32_t)phost->LoadFileRemaining;
452 }
453 while (remaining > 0)
454 {
455 blocklen = (uint16_t)min(blockSize, remaining);
456 blocklen = (uint16_t)fread((void *)pbuff, 1, blocklen, afile); /* Copy the data into pbuff and then transfter it to command buffer */
457
458 if (blocklen == 0)
459 {
460 eve_printf_debug("Read 0 bytes, unexpected end of file, %i bytes remaining", (int)remaining);
461 break;
462 }
463
464 remaining -= blocklen;
465 blocklen += 3;
466 blocklen &= ~3U;
467
468 if (transfered)
469 {
470 uint32_t transferedPart = 0;
471 bool wrRes = EVE_MediaFifo_wrMem(phost, pbuff, blocklen, &transferedPart); /* Copy data continuously into media fifo memory */
472 *transfered += transferedPart;
473 if (transferedPart < blocklen)
474 {
475 long offset = (long)transferedPart - (long)blocklen; /* Negative */
476 fseek(afile, offset, SEEK_CUR); /* Seek back */
477 remaining -= offset; /* Increments remaining (double negative) */
478 break; /* Early exit, processing done */
479 }
480 if (!wrRes)
481 break;
482 }
483 else
484 {
485 if (!EVE_MediaFifo_wrMem(phost, pbuff, blocklen, NULL)) /* Copy data continuously into media fifo memory */
486 break; /* Coprocessor fault */
487 }
488 }
489 if (!transfered)
490 {
491 fclose(afile); /* Close the opened file */
492 }
493 else if (remaining)
494 {
495 phost->LoadFileRemaining = remaining; /* Save remaining */
496 }
497 else
498 {
499 EVE_Util_closeFile(phost);
500 }
501 return transfered ? EVE_Cmd_waitFlush(phost) : EVE_MediaFifo_waitFlush(phost, false);
502}
503
505{
506 if (phost->LoadFileHandle)
507 {
508 fclose(phost->LoadFileHandle);
509 phost->LoadFileHandle = NULL;
510 phost->LoadFileRemaining = 0;
511 }
512}
513
514#ifdef _WIN32
515
516EVE_HAL_EXPORT bool EVE_Util_loadMediaFile(EVE_HalContext *phost, const char *filename, uint32_t *transfered)
517{
518 return loadMediaFile(phost, filename, NULL, transfered);
519}
520
521EVE_HAL_EXPORT bool EVE_Util_loadMediaFileW(EVE_HalContext *phost, const wchar_t *filename, uint32_t *transfered)
522{
523 return loadMediaFile(phost, NULL, filename, transfered);
524}
525
526#endif
527#endif
528
529#endif
530
531/* end of file */
EVE_HAL_EXPORT bool EVE_Cmd_wrMem(EVE_HalContext *phost, const uint8_t *buffer, uint32_t size)
Write buffer to Coprocessor's comand fifo.
Definition EVE_Cmd.c:291
EVE_HAL_EXPORT uint32_t EVE_Cmd_waitSpace(EVE_HalContext *phost, uint32_t size)
Definition EVE_Cmd.c:580
EVE_HAL_EXPORT bool EVE_Cmd_wr32(EVE_HalContext *phost, uint32_t value)
Write 4 bytes to Coprocessor's command fifo.
Definition EVE_Cmd.c:394
#define EVE_HAL_EXPORT
#define OPT_NODL
#define CMD_INFLATE
#define CMD_LOADIMAGE
EVE_HAL_EXPORT uint32_t EVE_Hal_rd32(EVE_HalContext *phost, uint32_t addr)
Read 4 bytes from Coprocessor's memory.
Definition EVE_Hal.c:189
EVE_HAL_EXPORT void EVE_Hal_wrMem(EVE_HalContext *phost, uint32_t addr, const uint8_t *buffer, uint32_t size)
Write a buffer to Coprocessor's memory.
Definition EVE_Hal.c:263
unsigned short uint16_t
unsigned int uint32_t
#define false
unsigned char uint8_t
Eve_Hal framework APIs for loading file.
EVE_HAL_EXPORT bool EVE_Util_loadInflateFile(EVE_HalContext *phost, uint32_t address, const char *filename)
Load file into RAM_G by CMD_INFLATE.
EVE_HAL_EXPORT bool EVE_Util_loadCmdFile(EVE_HalContext *phost, const char *filename, uint32_t *transfered)
EVE_HAL_EXPORT bool EVE_Util_loadRawFile(EVE_HalContext *phost, uint32_t address, const char *filename)
Load a raw file into RAM_G.
EVE_HAL_EXPORT void EVE_Util_closeFile(EVE_HalContext *phost)
EVE_HAL_EXPORT bool EVE_Util_loadImageFile(EVE_HalContext *phost, uint32_t address, const char *filename, uint32_t *format)
Load image into RAM_G.
EVE_HAL_EXPORT bool EVE_Util_loadMediaFile(EVE_HalContext *phost, const char *filename, uint32_t *transfered)
EVE_HAL_EXPORT bool EVE_Util_loadSdCard(EVE_HalContext *phost)
Mount the SDcard.
EVE_HAL_EXPORT size_t EVE_Util_readFile(EVE_HalContext *phost, uint8_t *buffer, size_t size, const char *filename)
EVE_HAL_EXPORT bool EVE_Util_sdCardReady(EVE_HalContext *phost)
bool EVE_MediaFifo_waitFlush(EVE_HalContext *phost, bool orCmdFlush)
Wait for the media FIFO to fully empty.
bool EVE_MediaFifo_wrMem(EVE_HalContext *phost, const uint8_t *buffer, uint32_t size, uint32_t *transfered)
Write a buffer to the media FIFO. Waits if there is not enough space in the media FIFO.
Platform selector.
#define eve_printf_debug(fmt,...)
static ft_uint32_t ft_uint8_t * buffer
Definition FT_Gpu_Hal.h:139
static ft_uint32_t addr
Definition FT_Gpu_Hal.h:139
void EVE_Cmd_waitFlush(EVE_HalContext *host)
Definition Gpu_Hal.cpp:775
uint32_t MediaFifoSize
ptrdiff_t LoadFileRemaining