-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjify.c
More file actions
116 lines (111 loc) · 1.87 KB
/
objify.c
File metadata and controls
116 lines (111 loc) · 1.87 KB
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
/*
bast - ZX Basic text to tape
Copyright Edward Cree, 2010
License: GNU GPL v3+
objify: convert flat binary to .obj format
*/
#define _GNU_SOURCE // feature test macro
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
signed int len=-1, org=-1;
const char *name=NULL;
int arg;
int state=0;
for(arg=1;arg<argc;arg++)
{
const char *varg=argv[arg];
if(varg[0]=='-')
{
if(strcmp(varg, "-l")==0)
state=1;
else if(strcmp(varg, "-o")==0)
state=2;
else if(strcmp(varg, "--scr")==0)
{
len=0x1b00;
org=0x4000;
}
}
signed int num=-1;
sscanf(varg, "%d", &num);
switch(state)
{
case 0:
name=varg;
break;
case 1:
if(num>=0)
{
len=num;
state=0;
}
else
{
fprintf(stderr, "objify: bad argument %s to -l\n", varg);
return(EXIT_FAILURE);
}
break;
case 2:
if(num>=0)
{
org=num;
state=0;
}
else
{
fprintf(stderr, "objify: bad argument %s to -o\n", varg);
return(EXIT_FAILURE);
}
break;
}
}
if(name)
printf("#%s\n", name);
if(org>=0)
printf("@%04X\n", org);
if(len>=0)
printf("*%04X\n", len);
int pos=0;
unsigned char cksum=0;
while((len<0)||(pos<len))
{
signed int i=getchar();
if(i==EOF)
{
if(len<0)
{
if(pos%8)
{
while(pos++%8)
printf("$$ ");
printf("== %02X\n", cksum);
}
return(EXIT_SUCCESS);
}
fprintf(stderr, "objify: unexpected EOF (offset %04X, expected-length %04X)\n", pos, len);
return(EXIT_FAILURE);
}
unsigned char c=i;
printf("%02X ", c);
cksum^=c;
if(!(++pos%8))
{
printf("== %02X\n", cksum);
cksum=0;
}
}
if(pos%8)
{
while(pos++%8)
printf("$$ ");
printf("== %02X\n", cksum);
}
if(getchar()!=EOF)
{
fprintf(stderr, "objify: warning, excess bytes in input file\n");
}
return(EXIT_SUCCESS);
}