aboutsummaryrefslogtreecommitdiff
path: root/dumptxt.c
blob: 1c1c3cd683695eeebc96f6a68cd54dbd50f586ec (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
#include <stdio.h>
#include <stdlib.h>

/* extract DSCII text from rom image, print in human-readable form.
   usage: ./dumptxt startaddr endaddr
   addresses are hex.

	$C0: copyright symbol (double-wide), we print as 'c)'
	$C1 - $DA: alphabet (but J Q Z are bogus-looking)
	$B0 - $BA: numbers
	$8D - carriage return (and newline), we print as 'nl'
	$A0 - space

	$F0-$FF set the color mask, but only in text drawn by draw_title_text.
	We print these as cN where N is the low nybble.
 */

#define ROM "defender.rom"

int main(int argc, char **argv) {
	int i, start, end, needaddr = 1;
	unsigned char buf[16384];
	FILE *f = fopen(ROM, "rb");

	if(argc != 3) {
		fprintf(stderr, "Usage: %s startaddr endaddr\n  give addresses in hex, e.g. %s b844 b86c\n", argv[0], argv[0]);
		exit(1);
	}

	if(!f) {
		perror(ROM);
		exit(1);
	}

	fread(buf, 16384, 1, f);

	start = (int)strtol(argv[1], 0, 16);
	end = (int)strtol(argv[2], 0, 16);

	if(start < 0x8000 || start >= 0xc000 || end < 0x8000 || end >= 0xc000) {
		fprintf(stderr, "start/end addr(s) invalid or out of range, must be 8000-bfff\n");
		exit(1);
	}

	fprintf(stderr, "dumping text from $%04x to $%04x\n", start, end);

	for(i = start; i <= end; i++) {
		unsigned char c = buf[i - 0x8000];

		if(needaddr) {
			printf("%04x: '", start);
			needaddr = 0;
		}

		if(!c) {
			putchar('\'');
			putchar('\n');
			needaddr = 1;
		} else if(c >= 0xb0 && c <= 0xba) {
			putchar(c - 0xb0 + '0');
		} else if(c == 0xc0) {
			putchar('c');
			putchar(')');
		} else if(c == 0xa0) {
			putchar(' ');
		} else if(c == 0x8d) {
			putchar('n');
			putchar('l');
		} else if(c >= 0xc1 && c <= 0xda) {
			putchar(c - 0xc1 + 'A');
		} else if(c >= 0xf0) {
			putchar('c');
			if(c > 0xf9)
				putchar(c - 0xfa + 'A');
			else
				putchar(c - 0xf0 + '0');
		} else {
			putchar('?');
		}
	}

	if(!needaddr) {
		putchar('\'');
		putchar('\n');
		fprintf(stderr, "last string not null-terminated!\n");
	}
	return 0;
}