aboutsummaryrefslogtreecommitdiff
path: root/bfd/cpu-h8300.c
blob: 79761d33912e1a2b0716e26c6c8ac2b6a69711d4 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* BFD library support routines for the Hitachi H8/300 architecture.
   Copyright (C) 1990-1991 Free Software Foundation, Inc.
   Hacked by Steve Chamberlain of Cygnus Support.

This file is part of BFD, the Binary File Descriptor library.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "bfd.h"
#include "sysdep.h"
#include "libbfd.h"

#define DEFINE_TABLE 
#include "opcode/h8300.h"

#define MAXSAME 14

static struct h8_opcode * h8_opcodes_sorted[256][MAXSAME];

/* Run through the opcodes and sort them into order to make them easy
   to disassemble
 */
static void
DEFUN_VOID(bfd_h8_disassemble_init)
{
  unsigned int i;	
  struct h8_opcode *p; 
    
  for (p = h8_opcodes; p->name; p++) {	
    int where = 0;
    int n1 = 0;
    int n2 = 0;
    int n3 = 0;
    int n4= 0;
    if ((int)p->data.nib[0] < 16) {
      n1 =(int) p->data.nib[0] ;
    } else n1 = 0;
    if ((int)p->data.nib[1] < 16) {
      n2 = (int) p->data.nib[1];
    }else n2 = 0;

    for (i = 0; i < MAXSAME; i++) {
      int j = n1 * 16 + n2;
      if (h8_opcodes_sorted[j][i] == (struct h8_opcode *)NULL) {
	h8_opcodes_sorted[j][i] = p;
	break;
      }
    }
    
    if (i==MAXSAME)abort();

    /* Just make sure there are an even number of nibbles in it, and
       that the count is the same s the length */
    for (i = 0; p->data.nib[i] != E; i++) ;
    if (i & 1) abort();
    if (i/2 != p->length) abort();
  }
  for (i = 0; i < 256; i++) 
      {
	if (h8_opcodes_sorted[i][0]) 
	  p = h8_opcodes_sorted[i][0];
	else h8_opcodes_sorted[i][0] = p;
      }
}


unsigned int
DEFUN(bfd_h8_disassemble,(addr, data, stream),
bfd_vma addr AND
CONST bfd_byte *data AND
FILE *stream)
{
  /* Find the first entry in the table for this opcode */
  CONST static char *regnames[] = {
    "r0h","r1h","r2h","r3h","r4h","r5h","r6h","r7h",
    "r0l","r1l","r2l","r3l","r4l","r5l","r6l","r7l" };

  int rs = 0;
  int rd = 0;
  int rdisp = 0;
  int abs = 0;
  struct h8_opcode **p = h8_opcodes_sorted[(unsigned)(data[0])];
  struct h8_opcode *q;

  /* Find the exact opcode/arg combo */
  while (*p) {
    op_type *nib;
    unsigned int len = 0;
    q = *p++;
    nib  =q->data.nib;
    while (*nib != E) {
      op_type looking_for = *nib;
      int thisnib = data[len>>1] ;
      thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
      if ((int)looking_for & (int)B31) {
	if (((int)thisnib & 0x8) ==0) goto fail;
	looking_for = (op_type)((int)looking_for &  ~(int)B31);
      }
      if ((int)looking_for & (int)B30) {
	if (((int)thisnib & 0x8) !=0) goto fail;
	looking_for = (op_type)((int)looking_for &  ~(int)B30);
      }
      switch (looking_for) {
      case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: 
      case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15:
	if ((int)looking_for != thisnib) goto fail;
	break;
      case ABS16SRC:
      case ABS16DST:
      case DISPSRC:
      case DISPDST:
      case IMM16:
	abs = (data[len>>1]) * 256 + data[(len+2)>>1];
	len+=3;
	nib+=3;
	break;
      case DISPREG:
	rdisp = thisnib; 
	break;
      case KBIT:
	abs = thisnib == 0x8 ? 2:1;
	break;
      case IMM8:
      case ABS8SRC:
      case ABS8DST:
      case MEMIND:
      case DISP8:
	abs= data[len>>1];
	len++;
	nib++;
	break;
      case IMM3:
	abs = thisnib ;
	break;
      case RS8:
      case RS16:
      case RSINC:
      case RSIND:
	rs = thisnib;
	break;
      case RD16:
      case RDDEC:
      case RD8:
      case RDIND:
	rd = thisnib;
	break;
      default:
	fprintf(stream, "Dont understand \n");
	goto found;
      }
      len++;
      nib++;
    }
    goto found;
  fail: 
    ;

  }
  fprintf(stream, "%02x %02x        .word\tH'%x,H'%x",
	  data[0], data[1],
	  data[0], data[1]);
  return 2;
 found:;
    { int i;

      for (i = 0; i < q->length; i++) {
	fprintf(stream, "%02x ", data[i]);
      }
      for (; i < 6; i++) {
	fprintf(stream, "   ");
      }
    }
  fprintf(stream, "%s\t",q->name);
  /* Now print out the args */
    {
      op_type *args = q->args.nib;
      int hadone = 0;
      while (*args != E) {
	if (hadone)
	  fprintf(stream, ",");
	switch ((int)(*args) & ~((int)B30|(int)B31)) {
	case IMM16:
	case IMM8:
	case IMM3:
	  fprintf(stream, "#0x%x", (unsigned)abs); 
	  break;
	case RD8:
	  fprintf(stream, "%s", regnames[rd]); 
	  break;
	case RS8:
	  fprintf(stream, "%s",regnames[rs]);
	  break;
	case RD16:
	  fprintf(stream, "r%d", rd& 0x7);
	  break;
	case RS16:
	  fprintf(stream, "r%d", rs & 0x7);
	  break;
	case RSINC:
	  fprintf(stream, "@r%d+", rs & 0x7);
	  break;
	case RDDEC:
	  fprintf(stream, "@-r%d", rd & 0x7);
	  break;
	case RDIND: 
	  fprintf(stream, "@r%d", rd & 0x7);
	  break;
	case RSIND:
	  fprintf(stream, "@r%d",rs & 0x7);
	  break;
	case ABS8SRC:
	case ABS16SRC:
	case ABS16DST:
	case ABS8DST:
	  fprintf(stream, "@0x%x", (unsigned)abs);
	  break;
	case MEMIND:
	  fprintf(stream, "@@%d (%x)",abs, abs);
	  break;
	case DISP8:
	  fprintf(stream, ".%s%d (%x)",(char)abs>0 ? "+" :"", (char)abs,
		  addr + (char)abs);
	  break;
	case DISPSRC:
	case DISPDST:
	  fprintf(stream, "@(0x%x,r%d)", abs, rdisp & 0x7); 
	  break;
	case CCR:
	  fprintf(stream, "ccr"); 
	  break;
	case KBIT:
	  fprintf(stream, "#%d",abs); 
	  break;
	default:
	  abort();
	}
	hadone = 1;
	args++;
      }
    }
  return q->length;
}



unsigned int DEFUN(print_insn_h8300,(addr, data, file),
bfd_vma addr AND
CONST char *data AND
PTR file)
{
  static boolean init;
  if (!init) {
    bfd_h8_disassemble_init();
    init= 1;
  }

  return   bfd_h8_disassemble(addr, (bfd_byte *)data, file);
}

/* 
Relocations for the H8

*/
static bfd_reloc_status_type 
DEFUN(howto16_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section),
bfd *abfd AND
arelent *reloc_entry AND
asymbol *symbol_in AND
unsigned char *data AND
asection *ignore_input_section)
{
  long relocation = 0;
  bfd_vma addr = reloc_entry->address;
  long x = bfd_get_16(abfd, (bfd_byte *)data + addr);

  HOWTO_PREPARE(relocation, symbol_in);

  x = (x + relocation + reloc_entry->addend);

  bfd_put_16(abfd, x, (bfd_byte *)data + addr);
  return bfd_reloc_ok;
}


static bfd_reloc_status_type 
DEFUN(howto8_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section),
bfd *abfd AND
arelent *reloc_entry AND
asymbol *symbol_in AND
unsigned char *data AND
asection *ignore_input_section)
{
  long relocation = 0;
  bfd_vma addr = reloc_entry->address;
  long x = bfd_get_8(abfd, (bfd_byte *)data + addr);

  HOWTO_PREPARE(relocation, symbol_in);

  x = (x + relocation + reloc_entry->addend);

  bfd_put_8(abfd, x, (bfd_byte *)data + addr);
  return bfd_reloc_ok;
}


static bfd_reloc_status_type 
DEFUN(howto8_FFnn_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section),
bfd *abfd AND
arelent *reloc_entry AND
asymbol *symbol_in AND
unsigned char *data AND
asection *ignore_input_section)
{
  long relocation = 0;
  bfd_vma addr = reloc_entry->address;

  long x = bfd_get_8(abfd, (bfd_byte *)data + addr);
  abort();
  HOWTO_PREPARE(relocation, symbol_in);

  x = (x + relocation + reloc_entry->addend);

  bfd_put_8(abfd, x, (bfd_byte *)data + addr);
  return bfd_reloc_ok;
}

static bfd_reloc_status_type 
DEFUN(howto8_pcrel_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section),
bfd *abfd AND
arelent *reloc_entry AND
asymbol *symbol_in AND
unsigned char *data AND
asection *ignore_input_section)
{
  long relocation = 0;
  bfd_vma addr = reloc_entry->address;
  long x = bfd_get_8(abfd, (bfd_byte *)data + addr);
  abort();
  HOWTO_PREPARE(relocation, symbol_in);

  x = (x + relocation + reloc_entry->addend);

  bfd_put_8(abfd, x, (bfd_byte *)data + addr);
  return bfd_reloc_ok;
}


static reloc_howto_type howto_16
  = NEWHOWTO(howto16_callback,"abs16",1,0,0);
static reloc_howto_type howto_8
  = NEWHOWTO(howto8_callback,"abs8",0,0,0);

static reloc_howto_type howto_8_FFnn
  = NEWHOWTO(howto8_FFnn_callback,"ff00+abs8",0,0,0);

static reloc_howto_type howto_8_pcrel
  = NEWHOWTO(howto8_pcrel_callback,"pcrel8",0,1,1);



static CONST struct reloc_howto_struct *
DEFUN(local_bfd_reloc_type_lookup,(arch, code),
 CONST struct bfd_arch_info *arch AND
 bfd_reloc_code_type code)
{
  switch (code) {
  case BFD_RELOC_16:
    return &howto_16;
  case BFD_RELOC_8_FFnn:
    return &howto_8_FFnn;
  case BFD_RELOC_8:
    return &howto_8;
  case BFD_RELOC_8_PCREL:
    return &howto_8_pcrel;
  }
  return (reloc_howto_type *)NULL;
}

int bfd_default_scan_num_mach();

static boolean 
DEFUN(h8300_scan,(info, string),
CONST struct bfd_arch_info *info AND
CONST char *string)
{
  if (strcmp(string,"h8300") == 0) return true;
  if (strcmp(string,"H8300") == 0) return true;
  if (strcmp(string,"h8/300") == 0) return true;
  if (strcmp(string,"H8/300") == 0) return true;
  return false;
}

static bfd_arch_info_type arch_info_struct = 
  {
    16,	/* 16 bits in a word */
    16,	/* 16 bits in an address */
    8,	/* 8 bits in a byte */
    bfd_arch_h8300,
    0,	/* only 1 machine */
    "H8/300", /* arch_name  */
    "H8/300", /* printable name */
    true,	/* the default machine */
    bfd_default_compatible,
    h8300_scan,
    print_insn_h8300,
    local_bfd_reloc_type_lookup,
    0,
  };

void
DEFUN_VOID(bfd_h8300_arch)
{
  bfd_arch_linkin(&arch_info_struct);
}