1 /*
2  *        Pasopia generator
3  *
4  *        $Id: pasopia7.c,v 1.6 2016-06-26 00:46:55 aralbrec Exp $
5  */
6 
7 #include "appmake.h"
8 
9 
10 static char             *binname      = NULL;
11 static char             *crtfile      = NULL;
12 static char             *outfile      = NULL;
13 static int               origin       = -1;
14 static char              help         = 0;
15 
16 
17 /* Options that are available for this module */
18 option_t pasopia7_options[] = {
19     { 'h', "help",     "Display this help",          OPT_BOOL,  &help},
20     { 'b', "binfile",  "Linked binary file",         OPT_STR,   &binname },
21     { 'c', "crt0file", "crt0 file used in linking",  OPT_STR,   &crtfile },
22     { 'o', "output",   "Name of output file",        OPT_STR,   &outfile },
23     {  0 , "org",      "Origin of the binary",       OPT_INT,   &origin },
24     {  0 ,  NULL,       NULL,                        OPT_NONE,  NULL }
25 };
26 
27 static disc_spec pasopia_spec = {
28     .name = "Pasopia7",
29     .sectors_per_track = 16,
30     .tracks = 40,
31     .sides = 1,
32     .sector_size = 256,
33     .gap3_length = 0x17,
34     .filler_byte = 0xe5,
35     .boottracks = 2,
36     .directory_entries = 128,
37     .extent_size = 1024,
38     .byte_size_extents = 0,
39     .first_sector_offset = 1,
40     .alternate_sides = 0
41 };
42 
43 
44 int pasopia7_exec(char *target)
45 {
46     char    *buf;
47     char    bootbuf[512];
48     char    filename[FILENAME_MAX+1];
49     char    bootname[FILENAME_MAX+1];
50     FILE    *fpin, *bootstrap_fp;
51     disc_handle *h;
52     long    pos, bootlen;
53     int     t,s,w;
54 
55     if ( help )
56         return -1;
57 
58     if ( binname == NULL ) {
59         return -1;
60     }
61 
62     strcpy(bootname, binname);
63     suffix_change(bootname, "_BOOTSTRAP.bin");
64     if ( (bootstrap_fp=fopen_bin(bootname, crtfile) ) == NULL ) {
65         exit_log(1,"Can't open input file %s\n",bootname);
66     }
67     if ( fseek(bootstrap_fp,0,SEEK_END) ) {
68         fclose(bootstrap_fp);
69         fprintf(stderr,"Couldn't determine size of file\n");
70     }
71     bootlen = ftell(bootstrap_fp);
nec_bit(FILE * fpout,unsigned char bit)72     fseek(bootstrap_fp,0L,SEEK_SET);
73 
74     if ( bootlen > 512 ) {
75         exit_log(1, "Bootstrap has length %d > 512", bootlen);
76     }
77     memset(bootbuf, 0, sizeof(bootbuf));
78     if ( fread(bootbuf, 1, bootlen, bootstrap_fp) != bootlen ) {
79         exit_log(1, "Cannot read whole bootstrap file");
80     }
81     fclose(bootstrap_fp);
82 
83 
84     strcpy(filename, binname);
85     if ( ( fpin = fopen_bin(binname, crtfile) ) == NULL ) {
86         exit_log(1,"Cannot open binary file <%s>\n",binname);
87     }
88 
89     if (fseek(fpin, 0, SEEK_END)) {
90         fclose(fpin);
91         exit_log(1,"Couldn't determine size of file\n");
92     }
93 
94     pos = ftell(fpin);
95     fseek(fpin, 0L, SEEK_SET);
96     buf = must_malloc(pos);
97     if (pos != fread(buf, 1, pos, fpin)) { fclose(fpin); exit_log(1, "Could not read required data from <%s>\n",binname); }
98     fclose(fpin);
99 
100 
101     h = cpm_create(&pasopia_spec);
102 
nec_rawout(FILE * fpout,unsigned char b)103     // Write the bootstrap to track 1
104     disc_write_sector(h, 1, 0, 0, bootbuf);
105     disc_write_sector(h, 1, 1, 0, bootbuf + 256);
106 
107     // Write input file
108     t = 2;
109     s = 0;
110     w = 0;
111     while ( w < pos ) {
112         disc_write_sector(h, t, s, 0, buf + w);
113         s++;
114         if ( s == 8) {
115             s = 0;
116             t++;
117         }
118         w += 256;
119     }
120 
121     suffix_change(filename, ".dsk");
122     disc_write_edsk(h, filename);
123     free(buf);
124 
125     return 0;
126 }
127 
128