summaryrefslogtreecommitdiff
path: root/cmddump.c
blob: dfab877aebb3a32299f8fd4ce3c15bd6a3f954de (plain)
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
/* Copyright (c) 1996-98, Timothy Mann */

/* This software may be copied, modified, and used for any purpose
 * without fee, provided that (1) the above copyright notice is
 * retained, and (2) modified versions are clearly marked as having
 * been modified, with the modifier's name and the date included.  */

/* Last modified on Thu Apr 23 23:58:52 PDT 1998 by mann */

/* Simulated TRS-80 DOS /cmd file loader.
 *
 * See the LDOS Quarterly, April 1, 1982 (Vol 1, No 4), for documentation
 *  of the TRS-80 DOS /cmd file format. 
 *
 * Usage: cmddump [-flags] infile [outfile startbyte nbytes]
 * If the optional arguments are given, the given byte range is dumped
 *  from the simulated memory after loading.
 *
 * Flags: -q      quiet: turn off -t, -m, -d, -s (later flags can override).
 *        -t      print text of module headers, pds headers, 
 *                  patch names, and copyright notices.
 *        -m      print running load map as file is parsed,
 *                  coalescing adjacent blocks (implies -t). (default)
 *        -d      print detailed map; same as -m, but don't coalesce.
 *        -s      print summary load map after file is parsed.
 *        -i n    select ISAM entry n (0x notation OK)
 *        -p foo  select PDS entry "foo" (padded to 8 bytes with spaces)
 *        -x      ignore anything after the first xfer address
 */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "load_cmd.h"

unsigned char memory[64*1024] = { 0 };
unsigned char loadmap[64*1024];

#define ARGS "qtmdsi:p:x"

int
main(int argc, char* argv[])
{
  FILE* f;
  int verbosity = VERBOSITY_MAP;
  int isam = -1;
  char *pds = NULL;
  int xferaddr, stopxfer = 0;
  int print_summary = 0;
  char pdsbuf[9];
  int res;
  int c, errflg = 0;

  optarg = NULL;
  while (!errflg && (c = getopt(argc, argv, ARGS)) != -1) {
    switch (c) {
    case 'q':
      verbosity = VERBOSITY_QUIET;
      break;
    case 't':
      verbosity = VERBOSITY_TEXT;
      break;
    case 'm':
      verbosity = VERBOSITY_MAP;
      break;
    case 'd':
      verbosity = VERBOSITY_DETAILED;
      break;
    case 's':
      print_summary = 1;
      break;
    case 'i':
      isam = strtol(optarg, NULL, 0);
      break;
    case 'p':
      strncpy(pdsbuf, optarg, 8);
      pdsbuf[8] = '\000';
      strncat(pdsbuf, "        ", 8 - strlen(pdsbuf));
      pds = pdsbuf;
      break;
    case 'x':
      stopxfer = 1;
      break;
    default:
      errflg++;
      break;
    }
  }

  if (errflg || !(argc == optind + 1 || argc == optind + 4)) {
    fprintf(stderr,
	    "Usage: %s [-%s] infile [outfile startbyte nbytes]\n",
	    argv[0], ARGS);
    exit(1);
  }
    
  f = fopen(argv[optind], "r");
  if (f == NULL) {
    perror(argv[optind]);
    exit(1);
  }	

  res = load_cmd(f, memory, print_summary ? loadmap : NULL, 
		 verbosity, stdout, isam, pds, &xferaddr, stopxfer);

  switch (res) {
  case LOAD_CMD_OK:
    if (isam != -1) {
      fprintf(stderr, "warning: not ISAM file but -i flag given\n");
    } else if (pds) {
      fprintf(stderr, "warning: not PDS file but -p flag given\n");
    }
    break;
  case LOAD_CMD_EOF:
    fprintf(stderr, "premature end of file\n");
    exit(1);
  case LOAD_CMD_NOT_FOUND:
    if (pds) {
      fprintf(stderr, "PDS entry \"%s\" not found\n", pds);
    } else {
      fprintf(stderr, "ISAM entry 0x%02x not found\n", isam);
    }
    exit(1);
  case LOAD_CMD_ISAM:
    if (isam == -1) {
      fprintf(stderr, "warning: ISAM file but -i flag not given\n");
    }
    break;
  case LOAD_CMD_PDS:
    if (pds == NULL) {
      fprintf(stderr, "warning: PDS file but -p flag not given\n");
    }
    break;
  default:
    fprintf(stderr, "load file format error, bad block type 0x%02x\n", res);
    break;
  }

  if (print_summary) {
    /* Print load map */
    int lastaddr = -1;
    int lastcount = -1;
    int addr;
    for (addr = 0; addr < sizeof(memory); addr++) {
      if (loadmap[addr] != lastcount) {
	if (lastcount == 1) {
	  printf("loaded 0x%04x - 0x%04x\n",
		 lastaddr, addr-1);
	} else if (lastcount > 1) {
	  printf("loaded 0x%04x - 0x%04x (%d times!)\n",
		 lastaddr, addr-1, lastcount);
	}
	lastaddr = addr;
	lastcount = loadmap[addr];
      }
    }
    printf("transfer address = 0x%04x\n", xferaddr);
  }

  /* Dump memory - optional */
  if (argc == optind + 4) {
    FILE* outf;
    int addr, count;
    outf = fopen(argv[optind + 1], "w");
    if (outf == NULL) {
      perror(argv[optind + 1]);
      exit(1);
    }
    addr = strtol(argv[optind + 2], (char**)NULL, 0);
    count = strtol(argv[optind + 3], (char**)NULL, 0);

    while (count-- > 0) {
      putc(memory[addr++], outf);
    }
    fclose(outf);
  }
  return 0;
}