summaryrefslogtreecommitdiffstats
path: root/ithabs.c
blob: e8ee8fa17043f0cb30a07ce7098ce8cbf8e4f641 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <inttypes.h>

#define MAXLINE BUFSIZ

static long gethex(char **p, uint8_t *csum)
{
  unsigned int v;
  int chi;

  if ( !sscanf(*p, "%02x%n", &v, &chi) )
    return -1L;

  if ( csum ) 
    *csum += v;
  *p += chi;

  return v;
}

static void parse_line(char *p, uint8_t *data, uint8_t *isset)
{
  uint8_t csum;
  int databytes, type, checksum;
  int a1, a2, d;
  int n;
  uint16_t address;
  uint8_t buf[256];

  csum = 0;

  if ( (databytes = gethex(&p, &csum)) < 0 )
    return;
  
  if ( (a1 = gethex(&p, &csum)) < 0 )
    return;
  if ( (a2 = gethex(&p, &csum)) < 0 )
    return;

  address = ((uint16_t)a1 << 8) + a2;
  
  if ( (type = gethex(&p, &csum)) < 0 || type != 0x00 )
    return;

  for ( n = 0 ; n < databytes ; n++ ) {
    if ( (d = gethex(&p, &csum)) < 0 )
      return;

    buf[n] = d;
  }


  if ( (checksum = gethex(&p, NULL)) < 0 )
    return;

  if ( checksum != (uint8_t) -csum )
    return;

  memcpy(data+address, buf, databytes);
  memset(isset+address, 1, databytes);
}

static void readith(FILE *in, uint8_t *data, uint8_t *isset)
{
  char buffer[MAXLINE];

  while ( fgets(buffer, sizeof buffer, in) ) {
    if ( buffer[0] != ':' )
      continue;

    parse_line(buffer+1, data, isset);
  }
}

static void write_buffer(FILE *out, uint8_t *buf, int buf_len, int buf_address, int *spaceinrecord_ptr)
{
  int spaceinrecord = *spaceinrecord_ptr;
  struct abs_data_hdr {
    uint8_t type;		/* 0 for data */
    uint8_t data_bytes;
    uint8_t zero;		/* Reserved */
    uint8_t addrh;
    uint8_t addrhx;
    uint8_t addrl;
    uint8_t addrlx;
  } hdr;
  uint8_t csum;
  int i;

  memset(&hdr, 0, sizeof hdr);

  csum = 0;
  for ( i = 0 ; i < buf_len ; i++ )
    csum += buf[i];

  hdr.data_bytes = buf_len;
  hdr.addrh = buf_address >> 8;
  hdr.addrhx = ~hdr.addrh;
  hdr.addrl = buf_address;
  hdr.addrlx = ~hdr.addrl;
      
  fwrite(&hdr, 1, sizeof hdr, out);
  fwrite(buf, 1, buf_len, out);
  putc(csum, out);
 
  spaceinrecord -= (buf_len + sizeof hdr + 1);

  if ( spaceinrecord < sizeof hdr + 4 ) {
    putc(0xff, out);
    spaceinrecord--;
    putc(0x00, out);
    spaceinrecord--;
    
    while ( spaceinrecord-- )
      putc(0xee, out);
	
    spaceinrecord = 253;
  }
 
  *spaceinrecord_ptr = spaceinrecord;
}

static void
writeabs(FILE *out, uint8_t *data, uint8_t *isset, uint16_t entry_point)
{
  int spaceinrecord = 253;
  int lastaddr      = -999;
  int addr;
  uint8_t buf[256];
  int buf_address   = 0;
  int buf_len       = 0;
  struct abs_entry_hdr {
    uint8_t type;		/* 0 for data */
    uint8_t data_bytes;		/* 0 for the entry point */
    uint8_t zero;		/* Reserved */
    uint8_t addrh;
    uint8_t addrhx;
    uint8_t addrl;
    uint8_t addrlx;
  } hdr;

  for ( addr = 0 ; addr < 65536 ; addr++ ) {
    if ( isset[addr] ) {
      if ( buf_len && (spaceinrecord < (buf_len+11) || 
		       addr != lastaddr+1) ) {
	/* Write previous buffer head */
	write_buffer(out, buf, buf_len, buf_address, &spaceinrecord);
	buf_len = 0;
      }
      
      if ( !buf_len )
	buf_address = addr;
      buf[buf_len++] = data[addr];
      lastaddr = addr;
    }
  }

  if ( buf_len )
    write_buffer(out, buf, buf_len, buf_address, &spaceinrecord);

  /* Write entry point */
  write_buffer(out, NULL, 0, entry_point, &spaceinrecord);

  /* Pad to end of record */
  while ( spaceinrecord-- )
    putc(0xee, out);
}

/* This obviously assumes a modern computer... */
int ithabs(FILE *in, FILE *out, uint16_t entry_point)
{
  uint8_t data[65536];		/* All the data read from the file */
  uint8_t isset[65536];		/* To write or not to write... */

  memset(data, 0, 65536);
  memset(isset, 0, 65536);

  readith(in, data, isset);
  writeabs(out, data, isset, entry_point);
}

int main(int argc, char *argv[])
{
  FILE *in, *out;
  uint16_t entry_point;

  if ( argc != 4 ) {
    fprintf(stderr, "Usage: %s input output entrypoint\n", argv[0]);
    exit(1);
  }

  in = fopen(argv[1], "rt");
  if ( !in ) {
    perror(argv[1]);
    exit(1);
  }

  out = fopen(argv[2], "wb");
  if ( !out ) {
    perror(argv[2]);
    exit(1);
  }

  entry_point = (uint16_t)strtoul(argv[3], NULL, 0);

  return !ithabs(in, out, entry_point);
}