Skip to content

Commit dde5322

Browse files
committed
Implement disk image readiing in the JS/Emscripten port
Also remove some stubs for unused file functions.
1 parent b2104ab commit dde5322

3 files changed

Lines changed: 68 additions & 48 deletions

File tree

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ js)
375375
# fragmentation prevents us from getting the same area), plus 32 MB of emulator
376376
# overhead.
377377
JS_LDFLAGS="-s INITIAL_MEMORY=570425344" # 256 MB max for emulated Mac RAM, plus 32 MB of emulator overhead
378+
JS_LDFLAGS="$JS_LDFLAGS -s STACK_SIZE=131072" # 128K to handle large arrays in mapped_load_chrp
378379
JS_LDFLAGS="$JS_LDFLAGS -s MODULARIZE -s EXPORT_ES6 -s EXPORT_NAME=emulator"
379380
JS_LDFLAGS="$JS_LDFLAGS -s EXPORTED_RUNTIME_METHODS=FS"
380381
JS_LDFLAGS="$JS_LDFLAGS -s ENVIRONMENT=worker"

src/debug/tracers.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
//#define IO_USB_TRACE(msg...) ht_printf("[IO/USB] " msg)
4848
#define IO_SERIAL_TRACE(msg...) ht_printf("[IO/SERIAL] " msg)
4949
#define IO_CORE_TRACE(msg...) ht_printf("[IO/Generic] " msg)
50+
//#define SYS_FILE_TRACE(msg...) ht_printf("[sys/file] " msg)
5051

5152
#define PPC_CPU_WARN(msg...) ht_printf("[CPU/CPU] <Warning> " msg)
5253
#define PPC_ALU_WARN(msg...) ht_printf("[CPU/ALU] <Warning> " msg)
@@ -70,8 +71,9 @@
7071
#define IO_USB_WARN(msg...) ht_printf("[IO/USB] <Warning> " msg)
7172
#define IO_SERIAL_WARN(msg...) ht_printf("[IO/SERIAL] <Warning> " msg)
7273
#define IO_CORE_WARN(msg...) ht_printf("[IO/Generic] <Warning> " msg)
74+
#define SYS_FILE_WARN(msg...) ht_printf("[sys/file] <Warning>" msg)
7375

74-
#define PPC_CPU_ERR(msg...) {ht_printf("[CPU/CPU] <Error> " msg);exit(1); }
76+
#define PPC_CPU_ERR(msg...) {ht_printf("[CPU/CPU] <Error> " msg);exit(1); }
7577
#define PPC_ALU_ERR(msg...) {ht_printf("[CPU/ALU] <Error> " msg);exit(1); }
7678
#define PPC_FPU_ERR(msg...) {ht_printf("[CPU/FPU] <Error> " msg);exit(1); }
7779
#define PPC_DEC_ERR(msg...) {ht_printf("[CPU/DEC] <Error> " msg);exit(1); }
@@ -93,6 +95,7 @@
9395
#define IO_USB_ERR(msg...) {ht_printf("[IO/IDE] <Error> " msg);exit(1); }
9496
#define IO_SERIAL_ERR(msg...) {ht_printf("[IO/SERIAL] <Error> " msg);exit(1); }
9597
#define IO_CORE_ERR(msg...) {ht_printf("[IO/Generic] <Error> " msg);exit(1); }
98+
#define SYS_FILE_ERR(msg...) ht_printf("[sys/file] <Error>" msg)
9699

97100
/*
98101
*
@@ -193,5 +196,9 @@
193196
#define IO_RTL8139_TRACE(msg...)
194197
#endif
195198

199+
#ifndef SYS_FILE_TRACE
200+
#define SYS_FILE_TRACE(msg...)
201+
#endif
202+
196203
#endif
197204

src/system/osapi/js/sysfile.cc

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
#include <emscripten.h>
55
#include <sys/stat.h>
66

7+
#include "debug/tracers.h"
78
#include "system/file.h"
89

9-
int sys_canonicalize(char *result, const char *filename) {
10-
if (!sys_filename_is_absolute(filename)) return ENOENT;
11-
return (realpath(filename, result)==result) ? 0 : ENOENT;
12-
}
10+
typedef struct {
11+
int diskId;
12+
FileOfs pos;
13+
} JSFile;
1314

1415
int sys_file_mode(int mode) {
1516
int m = 0;
@@ -42,21 +43,6 @@ int sys_file_mode(int mode) {
4243
return m;
4344
}
4445

45-
int sys_findclose(pfind_t &pfind) {
46-
printf("sys_findclose()\n");
47-
return 0;
48-
}
49-
50-
int sys_findfirst(pfind_t &pfind, const char *dirname) {
51-
printf("sys_findfirst(%s)\n", dirname);
52-
return 0;
53-
}
54-
55-
int sys_findnext(pfind_t &pfind) {
56-
printf("sys_findnext()\n");
57-
return 0;
58-
}
59-
6046
static void stat_to_pstat_t(const struct stat &st, pstat_t &s) {
6147
s.caps = pstat_ctime|pstat_mtime|pstat_atime|pstat_uid|pstat_gid|pstat_mode_all|pstat_size|pstat_inode;
6248
s.ctime = st.st_ctime;
@@ -98,62 +84,88 @@ int sys_truncate_fd(int fd, FileOfs ofs) {
9884
return 0;
9985
}
10086

101-
int sys_deletefile(const char *filename) {
102-
printf("sys_deletefile(%s)\n", filename);
103-
return 0;
104-
}
105-
10687
bool sys_is_path_delim(char c) {
10788
return c == '/';
10889
}
10990

110-
int sys_filename_cmp(const char *a, const char *b) {
111-
while (*a && *b) {
112-
if (sys_is_path_delim(*a) && sys_is_path_delim(*b)) {
113-
} else if (*a != *b) {
114-
break;
115-
}
116-
a++;
117-
b++;
118-
}
119-
return *a - *b;
120-
}
121-
12291
bool sys_filename_is_absolute(const char *filename) {
12392
return sys_is_path_delim(filename[0]);
12493
}
12594

12695
SYS_FILE *sys_fopen(const char *filename, int openmode) {
127-
printf("sys_fopen(%s, %d)\n", filename, openmode);
128-
return nullptr;
96+
int diskId = EM_ASM_INT({
97+
return workerApi.disks.open(UTF8ToString($0));
98+
}, filename);
99+
if (diskId == -1) {
100+
SYS_FILE_WARN("Failing non-disk sys_fopen(%s, %d)\n", filename, openmode);
101+
return nullptr;
102+
}
103+
104+
SYS_FILE_TRACE("sys_fopen %s -> disk ID %d\n", filename, diskId);
105+
106+
return new JSFile{diskId, 0};
129107
}
130108

131109
void sys_fclose(SYS_FILE *file) {
132-
printf("sys_fclose()\n");
110+
JSFile *jsFile = static_cast<JSFile*>(file);
111+
EM_ASM_({ workerApi.disks.close($0); }, jsFile->diskId);
112+
delete jsFile;
133113
}
134114

135115
int sys_fread(SYS_FILE *file, byte *buf, int size) {
136-
printf("sys_fread(%d)\n", size);
137-
return 0;
116+
JSFile *jsFile = static_cast<JSFile*>(file);
117+
uint64 readSize = EM_ASM_DOUBLE({
118+
return workerApi.disks.read($0, $1, $2, $3);
119+
}, jsFile->diskId, buf, double(jsFile->pos), double(size));
120+
SYS_FILE_TRACE("[disk %d] sys_fread %d bytes at %d\n", jsFile->diskId, size, jsFile->pos);
121+
jsFile->pos += readSize;
122+
return readSize;
138123
}
139124

140125
int sys_fwrite(SYS_FILE *file, byte *buf, int size) {
141-
printf("sys_fwrite(%d)\n", size);
142-
return 0;
126+
JSFile *jsFile = static_cast<JSFile*>(file);
127+
uint64 writeSize = EM_ASM_DOUBLE({
128+
return workerApi.disks.write($0, $1, $2, $3);
129+
}, jsFile->diskId, buf, double(jsFile->pos), double(size));
130+
SYS_FILE_TRACE("[disk %d] sys_fwrite %d bytes at %llu\n", jsFile->diskId, size, jsFile->pos);
131+
jsFile->pos += writeSize;
132+
return writeSize;
143133
}
144134

145135
int sys_fseek(SYS_FILE *file, FileOfs newofs, int seekmode) {
146-
printf("sys_fseek(%lld, %d)\n", newofs, seekmode);
136+
JSFile *jsFile = static_cast<JSFile*>(file);
137+
switch (seekmode) {
138+
case SYS_SEEK_SET: {
139+
SYS_FILE_TRACE("[disk %d] sys_fseek SEEK_SET to %llu\n", jsFile->diskId, newofs);
140+
jsFile->pos = newofs;
141+
break;
142+
}
143+
case SYS_SEEK_REL: {
144+
jsFile->pos += newofs;
145+
SYS_FILE_TRACE("[disk %d] sys_fseek SEEK_CUR by %llu -> %llu\n", jsFile->diskId, newofs, jsFile->pos);
146+
break;
147+
}
148+
case SYS_SEEK_END: {
149+
FileOfs size = EM_ASM_DOUBLE({ return workerApi.disks.size($0); }, jsFile->diskId);
150+
jsFile->pos = size - newofs;
151+
SYS_FILE_TRACE("[disk %d] sys_fseek SEEK_END by %llu -> %llu\n", jsFile->diskId, newofs, jsFile->pos);
152+
break;
153+
}
154+
default:
155+
SYS_FILE_TRACE("disk %d] unknown sys_fseek mode %d\n", jsFile->diskId, seekmode);
156+
break;
157+
}
147158
return 0;
148159
}
149160

150161
FileOfs sys_ftell(SYS_FILE *file) {
151-
printf("sys_ftell()\n");
152-
return 0;
162+
JSFile *jsFile = static_cast<JSFile*>(file);
163+
SYS_FILE_TRACE("[disk %d] sys_ftell -> %lld\n", jsFile->diskId, jsFile->pos);
164+
return jsFile->pos;
153165
}
154166

155167
void sys_flush(SYS_FILE *file) {
156-
printf("sys_flush()\n");
168+
// No-op in JS
157169
}
158170

159171
void sys_suspend() {

0 commit comments

Comments
 (0)