File: scode.c

package info (click to toggle)
crasm 1.8-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 376 kB
  • sloc: ansic: 3,505; makefile: 65
file content (188 lines) | stat: -rwxr-xr-x 3,554 bytes parent folder | download | duplicates (4)
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
/*  Copyright (C) 1987- Leon Bottou
 * 
 *  This is free documentation; 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.
 * 
 *  The GNU General Public License's references to "object code"
 *  and "executables" are to be interpreted as the output of any
 *  document formatting or typesetting system, including
 *  intermediate and printed output.
 * 
 *  This manual 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 manual. Otherwise check the web site
 *  of the Free Software Foundation at http://www.fsf.org.
 */
/*
	CRASM: scode.c
	sorties du scode
	LYB 9/87
*/


#include "dcl.h"

unsigned long pc;
unsigned long codelen;
unsigned long rawstart;
int rawnumber;
int rawextended;
char raw[16];
extern int includelevel;

void
setpc(long unsigned int addr)
{
	if ( addr != pc )
	{	flushscoderaw();
		rawstart=pc=addr;
		rawnumber=0;
	}
}

void
insert8(unsigned char x)
{
	if ( ! (asmflags & F_ORG_GV) )
		error ( "no org given");
	if ( ! (asmflags & F_NOCODE) )
	{	if ( scode == NULL )
		{
			scode = fopen ( scodename,"w" );
			if ( scode == NULL )
				fatal("can't open scode file");
			rawextended = 0;
		}
	}
	if ( asmflags & F_CODE_ON )
	{	if ( scode != NULL )
		{	if ( rawnumber >= 16 )
				flushscoderaw();
			raw[rawnumber++]=x;
		}
		codelen++;
		outputbyte(x);
	}
	advance++;
	pc++;
	
}

void
insert16(short unsigned int x)
{
	if (asmflags & F_LOHI)
	{	insert8(x);
		insert8(x>>8);
	}
	else
	{	insert8(x>>8);
		insert8(x);
	}
}

void
insert32(unsigned int x)
{
	if (asmflags & F_LOHI)
	{	insert16(x);
		insert16(x>>16);
	}
	else
	{	insert16(x>>16);
		insert16(x);
	}
}

static unsigned char checksum;

static void
scodebyte(unsigned char b)
{
	checksum += b;
		
	fputc( hexa[ (b>>4) & 0xf ] , scode );
	fputc( hexa[   b & 0xf    ] , scode );
	if ( ferror(scode) )
		fileerror(scodename);
}


void
closescodefile(void)
{
	flushscoderaw();
	if ( asmflags & F_CODE_HEX )
		fputs(":00000001FF\n",scode);
	else
		fputs("S9030000FC\n",scode);
	if ( scode )
		fclose(scode);
}

void
flushscoderaw(void)
{
	register int i;
	
	if ( rawnumber )
	{
		if ( asmflags & F_CODE_HEX )
		{	
			if ((rawextended ^ rawstart) & 0xffff0000)
			{
				fputs(":02000004",scode);
	        		checksum = (unsigned char)(0xFF + 0x02 + 0x04);
				scodebyte(rawstart>>24);
				scodebyte(rawstart>>16);
				scodebyte(~checksum);
				fputc('\n',scode);
				rawextended = rawstart;
			}
			fputc(':',scode);
	        	checksum = 0xFF;
			scodebyte(rawnumber);
			scodebyte(rawstart>>8);
			scodebyte(rawstart);
			scodebyte(0x00);
		}
		else 
		{	fputc('S',scode);
	        	checksum = 0;
			if (rawstart & 0xff000000)
			{
				fputc('3',scode);
				scodebyte(rawnumber+5);
				scodebyte(rawstart>>24);
				scodebyte(rawstart>>16);
			} 
			else if (rawstart & 0xff0000)
			{
				fputc('2',scode);
				scodebyte(rawnumber+4);
				scodebyte(rawstart>>16);
			}
			else
			{
				fputc('1',scode);
				scodebyte(rawnumber+3);
			}
			scodebyte(rawstart>>8);
			scodebyte(rawstart);
		}
		for ( i=0; i<rawnumber; i++ )
			scodebyte( raw[i] );
		scodebyte  ( ~ checksum );
		fputc('\n',scode);
	}
	rawstart=pc;
	rawnumber=0;
}