1 /*
2 * Creates ZXVGS application
3 *
4 * zxvgsapp [binary file] [startup file] [-nt]
5 *
6 * Yarek 7/3/2003
7 */
8
9
10 #include "appmake.h"
11
12
13 /* the MAX_CHUNK is 16383 or less */
14 #define MAX_CHUNK 16383
15
16 static char *binname = NULL;
17 static char *crtfile = NULL;
18 static char *outfile = NULL;
19 static char help = 0;
20
21
22 static unsigned char *memory; /* Pointer to Z80 memory */
23 static long zorg; /* Origin of compiler program */
24
25 /* Options that are available for this module */
26 option_t zxvgs_options[] = {
27 { 'h', "help", "Display this help", OPT_BOOL, &help},
28 { 'b', "binfile", "Linked binary file", OPT_STR, &binname },
29 { 'c', "crt0file", "crt0 file used in linking", OPT_STR, &crtfile },
30 { 'o', "output", "Name of output file", OPT_STR, &outfile },
31 { 0, NULL, NULL, OPT_NONE, NULL }
32 };
33
34
35 /*
36 * Execution starts here
37 */
38
zxvgs_exec(char * target)39 int zxvgs_exec(char* target)
40 {
41 FILE* binfile; /* Read in bin file */
42 FILE* fp;
43 long filesize;
44 int readlen; /* Amount read in */
45 unsigned int chunk; /* chunk size */
46 char name[FILENAME_MAX + 1];
47 char header[5]; /* header to save */
48 unsigned char* position;
49
50 if (help)
51 return -1;
52
53 if (binname == NULL || crtfile == NULL) {
54 return -1;
55 }
56
57 if (outfile == NULL)
58 outfile = binname;
59
60 zorg = get_org_addr(crtfile);
61 if (zorg == -1)
62 exit_log(1,"Could not find parameter ZORG (compiled as BASIC?)\n");
63
64 memory = calloc(1, 49152L);
65 if (memory == NULL)
66 exit_log(1,"Can't allocate memory\n");
67
68 binfile = fopen_bin(binname, crtfile);
69 if (binfile == NULL)
70 exit_log(1,"Can't open binary file\n");
71
72 if (fseek(binfile, 0, SEEK_END)) {
73 fclose(binfile);
74 exit_log(1,"Couldn't determine the size of the file\n");
75 }
76
77 filesize = ftell(binfile);
78 if (filesize > 49152L) {
79 fclose(binfile);
80 exit_log(1,"The source binary is over 49152 bytes in length.\n");
81 }
82
83 fseek(binfile, 0, SEEK_SET);
84
85 readlen = fread(memory, 1, filesize, binfile);
86 if (filesize != readlen) {
87 fclose(binfile);
88 exit_log(1,"Couldn't read in binary file\n");
89 }
90 fclose(binfile);
91
92 strcpy(name, outfile);
93 suffix_change(name, ".V00");
94
95 if ((fp = fopen(name, "wb")) == NULL) {
96 exit_log(1,"Can't open output file %s\n", name);
97 }
98 header[0] = 1;
99 header[1] = zorg / 256;
100 header[2] = zorg % 256;
101 if (fwrite(&header, 1, 3, fp) != 3)
102 exit_log(1, "Can't write to output file %s\n", name);
103 position = memory;
104 while (filesize > 0) { /* Writing chunk with no compression */
105 chunk = (filesize > MAX_CHUNK) ? MAX_CHUNK : filesize;
106 header[0] = 0xC0 + chunk / 256;
107 header[1] = chunk % 256;
108 if (fwrite(&header, 1, 2, fp) != 2)
109 exit_log(1, "Can't write to output file %s\n", name);
110 if (fwrite(position, 1, chunk, fp) != chunk)
111 exit_log(1, "Can't write to output file %s\n", name);
112 position += chunk;
113 filesize -= chunk;
114 }
115 header[0] = 0;
116 header[1] = zorg / 256;
117 header[2] = zorg % 256;
118 if (fwrite(&header, 1, 3, fp) != 3)
119 exit_log(1, "Can't write to output file %s\n", name);
120 fclose(fp);
121
122 return 0;
123 }
124