summaryrefslogtreecommitdiff
path: root/other/Kermit/src/readsym.cpp
blob: 541d59490a121b04408f374b27899f7561fc78b0 (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
/*
 * readsym.cpp:
 * written by palmers / teso
 */
#include <Kermit>
#include <iostream>
#include <fstream>

#define PROGRAM	"readsym"
#define VERSION	"0.1"
#define AUTHOR	"palmers / teso"


void dump_c_array (char *arr, char *b, int x)
{
  int y;
  cout.setf (ios::hex, ios::basefield);
  cout << "char " << arr << "[] = {" << endl;
  for (y = 0; y < x; y++)
    {
      cout << "0x" << ((int) b[y] & 0xff);
      if (y != (x - 1))
	cout << ", ";
      if (!(y % 10))
	cout << endl;
    }
  cout << endl << "};" << endl;
  cout.setf (ios::dec, ios::basefield);
}


void dump (char *a, int x)
{
  int y;
  cout.setf (ios::hex, ios::basefield);

  for (y = 0; y < x; y++)
    {
      if ((a[y] & 0xff) < 16)
	cout << '0';
      cout << ((int) a[y] & 0xff) << ' ';
    }
  cout.setf (ios::dec, ios::basefield);
  cout << endl;
}

void usage (char *a)
{
  cout << PROGRAM << VERSION << " by " << AUTHOR << endl;
  cout << endl;
  cout << "Usage: " << a << " [options] <d|p> <offset> <length>" << endl;
  cout << "Options:" << endl;
  cout << "\t-x:\t where x is 1, 2, 3: the amount of ram in GB the system is ought to run" << endl;
  cout << "\t-c <name>:\t dump the read data as a c array" << endl;
  cout << endl;
  exit (0);
}


int main (int argc, char **argv)
{
  rwKernel		*rw = NULL;
  char			*inBuf = NULL,
			*arr_name = NULL;
  unsigned long		length = 0,
			offset = CONF_1GB;
  int			x = 0;

  if (argc < 4)
    usage (argv[0]);

  while (x < argc)
    {
      if (argv[x][0] == '-')
        {
          switch (argv[x][1])
            {
	    case '1':
	      offset = CONF_1GB;
	      break;
	    case '2':
	      offset = CONF_2GB;
	      break;
	    case '3':
	      offset = CONF_3GB;
	      break;
            case 'c':
              arr_name = argv[x + 1];
	      x++;
              break;
            case 'h':
              usage (argv[0]);
              break;
            default:
              usage (argv[0]);
              break;
            }
        }
      x++;
    }

  if (argv[argc - 3][0] == 'd')
    rw = new rwKernel (DEVMEM, offset);
  else if (argv[argc - 3][0] == 'p')
    rw = new rwKernel (PROCKCORE, offset);

  offset = stoi16 (string (argv[argc - 2]));
  length = stoi16 (string (argv[argc - 1]));

  inBuf = new char[length + 1];
  rw->read (inBuf, length, offset);

  if (arr_name != NULL)
    {
      dump_c_array (arr_name, inBuf, length);
      delete inBuf;
      exit (0);
    }

  dump (inBuf, length);
  delete inBuf;
  return 0;
}