-
Notifications
You must be signed in to change notification settings - Fork 9
/
rkdump.c
138 lines (116 loc) · 3.14 KB
/
rkdump.c
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
/*
* Part of Intel 8080/KR580VM80A in JavaScript project
*
* Copyright (C) 2009, 2012 Alexander Demin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int dump_file(const char* name) {
char dump_name[1024];
FILE* f;
int start, end, entry;
unsigned char ch;
char* period;
int i;
strcpy(dump_name, name);
period = strchr(dump_name, '.');
assert(period != NULL);
f = fopen(name, "rb+");
if (!f) {
fprintf(stderr, "unable to open file [%s]\n", name);
exit(1);
}
// If the file name starts with "mon" this is an image of Monitor
// (no sync-byte, start and end addresses at front).
if (!strcmp(period, ".bin") || !strcmp(period, ".COM")) {
long sz;
assert(fseek(f, 0L, SEEK_END) == 0);
sz = ftell(f);
assert(sz > -1L);
if (!memcmp(name, "mon", 3)) {
end = 0xffff;
start = end - sz + 1;
entry = 0xf800;
} else if (!strcmp(period, ".COM")) {
start = 0x100;
end = start + sz - 1;
entry = start;
} else {
end = sz;
start = 0;
entry = 0;
}
assert(fseek(f, 0L, SEEK_SET) == 0);
} else {
// Is it the sync-byte (E6)?
fread(&ch, 1, 1, f);
if (ch == 0xe6) fread(&ch, 1, 1, f);
// start address
start = ch << 8;
fread(&ch, 1, 1, f); start |= ch;
// end address
fread(&ch, 1, 1, f); end = ch << 8;
fread(&ch, 1, 1, f); end |= ch;
entry = start;
*period = 0;
}
printf("files['%s'] = {\n", dump_name);
printf("start: 0x%04x,\n", start);
printf("end: 0x%04x,\n", end);
printf("entry: 0x%04x,\n", entry);
printf("image:\n");
printf("\"");
i = 0;
while (start <= end) {
char ch;
assert(!feof(f));
fread(&ch, 1, 1, f);
printf("%02X", (unsigned char)ch);
++i;
if (i >= 32) {
i = 0;
printf("\"");
if (start < end) printf(" +\n\"");
}
++start;
}
if (i > 0) printf("\"");
printf("\n};\n\n");
fclose(f);
return 0;
}
int main(int argc, char* argv[]) {
if (argc == 2) return dump_file(argv[1]);
printf("function preloaded_files() {\n");
printf("const files = [];\n");
while (!feof(stdin)) {
char line[1024];
int sz;
char* p;
fgets(line, sizeof(line), stdin);
sz = strlen(line);
if (!sz) break;
p = line + strlen(line) - 1;
while (p != line && (*p == '\r' || *p == '\n')) *p-- = 0;
dump_file(line);
}
printf("return files;\n");
printf("}\n");
return 0;
}