File: dump.c

package info (click to toggle)
ts10 0.8.021004-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,572 kB
  • ctags: 11,742
  • sloc: ansic: 68,289; makefile: 466
file content (178 lines) | stat: -rw-r--r-- 4,557 bytes parent folder | download | duplicates (2)
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
// dump.c - Tools: Dump Facility
//
// Copyright (c) 2002, Timothy M. Stark
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// TIMOTHY M STARK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name of Timothy M Stark shall not
// be used in advertising or otherwise to promote the sale, use or other 
// dealings in this Software without prior written authorization from
// Timothy M Stark.

#include "emu/defs.h"
#include "emu/vtape.h"
#include <getopt.h>

#define LH18(x) ((uint32)((x) >> 18) & 0777777)
#define RH18(x) ((uint32)(x) & 0777777)

void PrintDump10(uint32 addr, uint8 *data, uint32 size)
{
	int36  data36;
	uint8  text[80];
	uint8  text6[7];  // SIXBIT
	uint8  text7[6];  // 7-bit ASCII
	uint8  text8[5];  // 8-bit ASCII
	uint8  text50[7]; // RADIX-50
	uint32 srad50;
	uint8  rad50[6];
	int    i, p;
	int    idx;

	for (idx = 0; idx < size; idx += 5) {
		data36 = ((uint36)data[idx] << 28) | (data[idx+1] << 20) |
		         (data[idx+2] << 12)       | (data[idx+3] << 4)  |
		         (data[idx+4] & 0x0F);

		for (i = 30, p = 0; i >= 0; i -= 6, p++)
			text6[p] = ((data36 >> i) & 077) + 040;
		text6[p] = 0;

		for (i = 29, p = 0; i >= 1; i -= 7, p++) {
			text7[p] = (data36 >> i) & 0177;
			if ((text7[p] < 32) || (text7[p] > 126))
				text7[p] = ' ';
		}
		text7[p] = 0;

		// Decipher RADIX-50 string.
		srad50 = data36;
		for (i = 0; i < 6; i++) {
			rad50[i] = srad50 % 050;
			srad50 /= 050;
		}
		for (i = 5, p = 0; i >= 0; i--)
			text50[p++] = // what an array of string! :-)
				" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.$%"[rad50[i]];
		text50[p] = '\0';

		printf("%06o:  %06o,,%06o ('%s' '%s' '%s')\n",
			addr++, LH18(data36), RH18(data36), text6, text7, text50);
	}
}

void PrintDump(uint32 addr, uint8 *data, uint32 size)
{
	uchar  ascBuffer[17];
	uchar  ch, *pasc;
	int    idxAddr, idx;

	for(idxAddr = 0; idxAddr < size;) {
		printf("%04X: ", addr + idxAddr);

		pasc = ascBuffer;
		for (idx = 0; (idx < 16) && (idxAddr < size); idx++) {
			ch = data[idxAddr++];
			printf("%c%02X", (idx == 8) ? '-' : ' ', ch);
			*pasc++ = ((ch >= 32) && (ch < 127)) ? ch : '.';
		}

		for (; idx < 16; idx++)
			printf("   ");
		*pasc = '\0';
		printf("  |%-16s|\n", ascBuffer);
	}
}

void DumpTape(VMT_TAPE *vmt)
{
	VMT_FORMAT *fmt = vmt->Format;
	char       buf[32768];
	int        rc, noFile = 0, noBlock = 0;

	// Rewind a tape
	fmt->Rewind(vmt);

	for (;;) {
		// Read a first block
		if ((rc = fmt->Read(vmt, buf, 32768)) < 0) {
			switch (rc) {
				case MT_MARK:
					printf("** Tape Mark **\n\n");
					noBlock = 0;
					noFile++;
					continue;

				case MT_EOT:
					printf("** End of Tape **\n\n");
					break;

				case MT_ERROR:
					printf("Error: (Read) %s\n", strerror(vmt->errCode));
					break;
			}
			break;
		}
		
		// Dump block contents.
		printf("File: %d  Block: %d  Length: %d (%04X) Bytes\n\n",
			noFile, noBlock++, rc, rc);
		PrintDump10(0, buf, rc);
		printf("\n");
	}
}

VMT_TAPE *OpenTape(char *fileName)
{
	VMT_TAPE *vmt;
	int      rc;

	if ((vmt = (VMT_TAPE *)calloc(1, sizeof(VMT_TAPE))) == NULL)
		return NULL;
	vmt->fileName = (char *)malloc(strlen(fileName)+1);
	strcpy(vmt->fileName, fileName);
	if (rc = vmt_OpenTape(vmt)) {
		printf("Reason: %d\n", rc);
		if (vmt->fileName)
			free(vmt->fileName);
		free(vmt);
		return NULL;
	}

	return vmt;
}

void CloseTape(VMT_TAPE *vmt)
{
	// Close a tape image file.
	vmt_CloseTape(vmt);
	if (vmt->fileName)
		free(vmt->fileName);
	free(vmt);
}

int main(int argc, char **argv)
{
	VMT_TAPE *vmt;

	vmt = OpenTape(argv[1]);
	DumpTape(vmt);
	CloseTape(vmt);
	return 0;
}