Repository navigation
Expand file tree
/
Copy pathemu.cpp
More file actions
374 lines (329 loc) · 9.86 KB
/
Copy pathemu.cpp
File metadata and controls
374 lines (329 loc) · 9.86 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* Copyright (c) 2020, Peter Barrett
**
** Permission to use, copy, modify, and/or distribute this software for
** any purpose with or without fee is hereby granted, provided that the
** above copyright notice and this permission notice appear in all copies.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
** SOFTWARE.
*/
#include "emu.h"
using namespace std;
// Map files into memory for carts bigger than physical RAM
// Handly for NES/SMS carts
// Uses app1 as a cache with a crappy FS on top - default arduino config gives 1280k
#ifdef ESP_PLATFORM
#include <esp_spi_flash.h>
#include <esp_attr.h>
#include <esp_partition.h>
#include "rom/miniz.h"
// only map 1 file at a time
spi_flash_mmap_handle_t _file_handle = 0;
static void print_part(const esp_partition_t *pPart)
{
printf("main: partition type = %d.\n", pPart->type);
printf("main: partition subtype = %d.\n", pPart->subtype);
printf("main: partition starting address = %x.\n", pPart->address);
printf("main: partition size = %x.\n", pPart->size);
printf("main: partition label = %s.\n", pPart->label);
printf("main: partition encrypted = %d.\n", pPart->encrypted);
printf("\n");
}
static void print_parts(esp_partition_iterator_t it)
{
while (it)
{
print_part((esp_partition_t *) esp_partition_get(it));
it = esp_partition_next(it);
}
}
typedef struct {
uint32_t sig;
uint32_t offset;
uint32_t len;
uint32_t flags;
char name[128-16];
} FlashFile;
#define FSIG ('F' | ('I' << 8) | ('L' << 16) | ('E' << 24))
class CrapFS {
public:
#define DIR_BLOCK_SIZE 0x4000 // 16k or 128 entries
// skip 64K at start of partition
const esp_partition_t* _part;
uint8_t *_buf;
FlashFile* _dir;
int _count;
CrapFS() : _buf(0),_count(0)
{
_part = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, "app1");
if (!_part) {
printf("CrapFS::CrapFS app1 not found\n");
return;
}
print_part(_part);
_buf = new uint8_t[DIR_BLOCK_SIZE];
if (!_buf)
return;
_dir = (FlashFile*)_buf;
esp_err_t err = esp_partition_read(_part, 0, _buf, DIR_BLOCK_SIZE);
if (err) {
printf("CrapFS::CrapFS dir read failed %d\n",err);
return;
}
// init directory if required
_count = DIR_BLOCK_SIZE/sizeof(FlashFile);
if (_dir[0].sig != FSIG)
reformat();
// dump dir
for (int i = 0; i < _count; i++) {
if (_dir[i].sig == FSIG)
printf("%08X %08X %s\n",_dir[i].offset,_dir[i].len,_dir[i].name);
}
}
~CrapFS()
{
delete _buf;
}
uint32_t align(uint32_t n)
{
return (n + 0xFFFF) & 0xFFFF0000;
}
void reformat()
{
esp_partition_erase_range(_part,0,_part->size); // erase entire partition
memset(_buf,0xFF,sizeof(DIR_BLOCK_SIZE)); // only use the first 16k of first block
}
uint8_t* mmap(const FlashFile* file)
{
if (!file)
return 0;
printf("CrapFS::mmap mapping %s offset:%08X len:%d\n",file->name,file->offset,file->len);
void* data = 0;
if (esp_partition_mmap(_part, file->offset, file->len, SPI_FLASH_MMAP_DATA, (const void**)&data, &_file_handle) == 0)
{
printf("CrapFS::mmap mapped to %08X\n",data);
return (uint8_t*)data;
}
return 0;
}
// see if the file exists
FlashFile* find(const std::string& path)
{
for (int i = 0; i < _count; i++) {
if (_dir[i].sig == FSIG && strcmp(_dir[i].name,path.c_str()) == 0)
return _dir + i;
}
return NULL;
}
int copy(const std::string& path, int offset, int len)
{
FILE *f = fopen(path.c_str(), "rb");
if (!f)
return -1;
#define BUF_SIZE 4096
uint8_t* buf = new uint8_t[BUF_SIZE];
esp_err_t err;
int i = 0;
while (i < len) {
int n = len-i;
if (n > BUF_SIZE)
n = BUF_SIZE;
fread(buf,1,n,f);
printf("CrapFS::copy writing %d of %d\n",i,len);
err = esp_partition_write(_part, i + offset, buf, n);
if (err)
break;
i += n;
}
fclose(f);
delete buf;
return err;
}
//
FlashFile* create(const std::string& path, int len)
{
uint32_t start = 0x10000;
for (int i = 0; i < _count; i++) {
if (_dir[i].sig != FSIG) {
_dir[i].sig = FSIG;
_dir[i].offset = start;
_dir[i].len = len; //
strcpy(_dir[i].name,path.c_str());
printf("CrapFS::created %s %08X %d\n",_dir[i].name,start,len);
esp_err_t err = esp_partition_erase_range(_part,0, DIR_BLOCK_SIZE); // erase dir
if (err == 0)
err = esp_partition_write(_part, 0, _buf, DIR_BLOCK_SIZE); // update dir
if (err) {
printf("CrapFS::create dir write failed %d\n",err);
return NULL;
}
if (copy(path,start,len))
return NULL;
return _dir+i;
}
start = align(_dir[i].offset + _dir[i].len);
}
// create failed. might want to invoke nuclear option
return NULL;
}
};
uint8_t* map_file(const char* path, int len)
{
CrapFS _fs;
FlashFile* file = _fs.find(path); // already copied?
if (!file)
file = _fs.create(path,len); // need to create a new file
if (!file) {
_fs.reformat();
file = _fs.create(path,len); // need to create a new file after reformatting the cache
}
return _fs.mmap(file);
}
void unmap_file(uint8_t* ptr)
{
if (_file_handle)
spi_flash_munmap(_file_handle);
_file_handle = 0;
}
FILE* mkfile(const char* path)
{
return fopen(path,"wb");
}
#else
#include <sys/stat.h>
#include "../miniz.h"
uint8_t* map_file(const char* path, int len)
{
uint8_t* d;
Emu::load(path,&d,&len);
return d;
}
void unmap_file(uint8_t* ptr)
{
delete ptr;
}
FILE* mkfile(const char* path)
{
std::string v = path;
std::string dir = v.substr(0,v.find_last_of("/"));
mkdir(dir.c_str(), 0755);
return fopen(path,"wb");
}
#endif
// map one bit array to another
uint32_t generic_map(uint32_t bits, const uint32_t* m)
{
uint32_t b = 0;
for (int i = 0; i < 16; i++) {
if ((0x8000 >> i) & bits)
b |= m[i];
}
return b;
}
// unpack file and write to FS, use rom miniz on esp32
// uses quite a lot of memory, call before initializing screen on atari
// could actually use the screen mem (which might look cool) or the main cpu mem for buffer
int unpack(const char* dst, const uint8_t* d, int len)
{
printf("unpacking %s\n",dst);
FILE* f = mkfile(dst);
if (!f)
return -1;
#define BUF_SIZE 0x8000
uint8_t* buf = new uint8_t[BUF_SIZE];
if (!buf) {
fclose(f);
return -1; // could use a smaller window on compression but would not generalize to other people's zips
}
tinfl_decompressor* dec = new tinfl_decompressor; // largist
size_t in_bytes, out_bytes;
tinfl_status status;
int i = 0;
tinfl_init(dec);
while (i < len) {
in_bytes = len-i;
out_bytes = BUF_SIZE;
status = tinfl_decompress(dec,d+i,&in_bytes,buf,buf,&out_bytes,11);
if (out_bytes != fwrite(buf,1,out_bytes,f)) {
status = TINFL_STATUS_FAILED;
break;
}
i += in_bytes;
}
delete [] buf;
delete dec;
fclose(f);
if (status == TINFL_STATUS_FAILED) {
remove(dst);
return -1;
}
return 0;
}
Emu::Emu(const char* n,int w,int h, int st, int aformat, int cc, int f) :
name(n),width(w),height(h),standard(st),audio_format(aformat),cc_width(cc),flavor(f)
{
//audio_frequency = 15625; // requires fixed point sampler
audio_frequency = standard == 1 ? 15720 : 15600;
audio_frame_samples = standard ? (audio_frequency << 16)/60 : (audio_frequency << 16)/50; // fixed point sampler
audio_fraction = 0;
}
Emu::~Emu()
{
}
int Emu::frame_sample_count()
{
int n = audio_frame_samples + audio_fraction;
audio_fraction = n & 0xFFFF;
return n >> 16;
}
int Emu::insert(const std::string& path, int flags, int disk_index)
{
return -1;
}
const uint32_t* Emu::composite_palette()
{
return standard ? ntsc_palette() : pal_palette();
}
// determine file type
int Emu::head(const std::string& path, uint8_t* data, int len)
{
FILE *f = fopen(path.c_str() , "rb");
if (!f)
return -1;
fread(data,1,len,f);
fseek(f, 0, SEEK_END);
int flen =(int)ftell(f);
fclose(f);
return flen;
}
int Emu::load(const std::string& path, uint8_t** data, int* len)
{
*data = 0;
*len = 0;
FILE *f = fopen(path.c_str(), "rb");
if (!f) {
printf("Emu::load failed for %s\n",path.c_str());
return -1;
}
fseek(f, 0, SEEK_END);
int fsize = (int)ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t* d = new uint8_t[fsize];
if (!d) {
printf("Emu::load failed for %s (out of memory)\n",path.c_str());
fclose(f);
return -1;
}
fread(d, 1, fsize, f);
fclose(f);
printf("Emu::load %d bytes %s\n",fsize,path.c_str());
*data = d;
*len = fsize;
return 0;
}