forked from spc476/mc6809
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc09disasm.c
124 lines (101 loc) · 2.84 KB
/
mc09disasm.c
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
/********************************************
*
* Copyright 2012 by Sean Conner. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This library 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Comments, questions and criticisms can be sent to: sean@conman.org
*
*********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "mc6809dis.h"
/*************************************************************************/
static mc6809byte__t dis_read(
mc6809dis__t *dis,
mc6809addr__t addr __attribute__((unused))
)
{
int c;
assert(dis != NULL);
assert(dis->user != NULL);
c = fgetc((FILE *)dis->user);
if (c == EOF)
longjmp(dis->err,MC6809_DIS_DONE);
return c;
}
/*************************************************************************/
static void dis_fault(
mc6809dis__t *dis,
mc6809fault__t fault
)
{
assert(dis != NULL);
assert(dis->user != NULL);
fprintf(stderr,"fault: %d\n",fault);
fclose((FILE *)dis->user);
exit(EXIT_FAILURE);
}
/*************************************************************************/
int main(int argc,char *argv[])
{
mc6809dis__t dis;
mc6809addr__t start;
char const *fname;
FILE *fp;
int rc;
start = 0;
switch(argc)
{
case 4: start = strtoul(argv[3],NULL,16); /* fall through */
case 3: dis.pc = strtoul(argv[2],NULL,16); /* fall through */
case 2: fname = argv[1]; break;
default:
fprintf(stderr,"usage: %s bin [load = 0 [start = 0]]\n",argv[0]);
return EXIT_FAILURE;
}
fp = fopen(fname,"rb");
if (fp == NULL)
{
perror(fname);
return EXIT_FAILURE;
}
if (start != 0)
{
if (start < dis.pc)
{
fprintf(stderr,"starting address must be equal to or larger than load address\n");
fclose(fp);
return EXIT_FAILURE;
}
while(dis.pc != start)
{
fgetc(fp);
dis.pc++;
}
}
dis.user = fp;
dis.read = dis_read;
dis.fault = dis_fault;
rc = mc6809dis_run(&dis,NULL);
fclose(fp);
if (rc != MC6809_DIS_DONE)
{
fprintf(stderr,"%s: %d\n",fname,rc);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*************************************************************************/