File: hex.c

package info (click to toggle)
gpasm 0.0.7-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 536 kB
  • ctags: 310
  • sloc: ansic: 2,011; perl: 1,330; yacc: 368; lex: 331; sh: 152; makefile: 34
file content (121 lines) | stat: -rw-r--r-- 2,523 bytes parent folder | download
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
/* ".HEX" file output for gpasm
   Copyright (C) 1998 James Bowman

This file is part of gpasm.

gpasm 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, or (at your option)
any later version.

gpasm 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 gpasm; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include "stdhdr.h"

#include "gpasm.h"
#include "hex.h"

static int sum;
static FILE *hex;

static void new_record()
{
  fprintf(hex, ":");
  sum = 0;
}

static void write_byte(int b)
{
  sum += b;
    
  assert((0 <= b) && (b <= 255));
  fprintf(hex, "%02X", b);
}

/* Write big-endian word */
static void write_bg_word(int w)
{
  write_byte((w >> 8) & 0xff);
  write_byte(w & 0xff);
}

/* Write little-endian word */
static void write_word(int w)
{
  write_byte(w & 0xff);
  write_byte((w >> 8) & 0xff);
}

static void end_record()
{
  write_byte((-sum) & 0xff);
  fprintf(hex, "\n");
}

void dump_hex()
{
  int i, j;
  char *pc;

  switch (state.hexfile) {
  case normal:
    strcpy(state.hexfilename, state.srcfilename);
    pc = strrchr(state.hexfilename, '.');
    if (pc == NULL)
      pc = state.hexfilename + strlen(state.hexfilename);
    strcpy(pc, ".hex");
    break;
  case suppress:
    return;
  case named:
    /* Don't need to do anything - name is already set up */
    ;
  }

  if (state.num.errors) {
    /* Remove the hex file (if any) */
    unlink(state.hexfilename);
    return;
  }

  /* No error: overwrite the hex file */

  hex = fopen(state.hexfilename, "w");
  i = 0;
  while (i < MAX_I_MEM) {
    if ((state.i_memory[i] & MEM_USED_MASK) == 0) {
      ++i;
    } else {
      j = i;
      while ((state.i_memory[i] & MEM_USED_MASK)) {
	++i;
	if ((i & 7) == 0)
	  break;
      }
      /* Now we have a run of (i - j) occupied memory locations. */
      new_record();
      write_byte(2 * (i - j));
      write_bg_word(2 * j);
      write_byte(0);
      while (j < i)
	write_word(state.i_memory[j++]);
      end_record();
    }
  }

  new_record();
  write_byte(0);
  write_word(0);
  write_byte(1);
  end_record();

  fclose(hex);
}