Add preliminary version of s51, a UI clone of the 8051 emulator.
[fw/altos] / s51 / s51-command.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "s51.h"
20
21 static enum command_result
22 parse_int(char *value, int *result)
23 {
24         char *endptr;
25
26         *result = strtol(value, &endptr, 0);
27         if (endptr == value)
28                 return command_syntax;
29         return command_proceed;
30 }
31
32 static enum command_result
33 parse_uint16(char *value, uint16_t *uint16)
34 {
35         int     v;
36         enum command_result result;
37
38         result = parse_int(value, &v);
39         if (result != command_proceed)
40                 return command_error;
41         if (v < 0 || v > 0xffff)
42                 return command_error;
43         *uint16 = v;
44         return command_proceed;
45 }
46
47 enum command_result
48 command_quit (FILE *output, int argc, char **argv)
49 {
50         exit(0);
51         return command_error;
52 }
53
54 enum command_result
55 command_di (FILE *output, int argc, char **argv)
56 {
57         return command_error;
58 }
59
60 enum command_result
61 command_ds (FILE *output, int argc, char **argv)
62 {
63         return command_error;
64 }
65
66 enum command_result
67 command_dx (FILE *output, int argc, char **argv)
68 {
69         return command_error;
70 }
71
72 enum command_result
73 command_set (FILE *output, int argc, char **argv)
74 {
75         return command_error;
76 }
77
78 enum command_result
79 command_dump (FILE *output, int argc, char **argv)
80 {
81         return command_error;
82 }
83
84 enum command_result
85 command_pc (FILE *output, int argc, char **argv)
86 {
87         uint16_t        pc;
88         if (argv[1]) {
89                 enum command_result result;
90
91                 result = parse_uint16(argv[1], &pc);
92                 if (result != command_proceed)
93                         return result;
94                 ccdbg_set_pc(s51_dbg, pc);
95         } else {
96                 pc = ccdbg_get_pc(s51_dbg);
97                 printf (" 0x%04x\n", pc);
98         }
99         return command_proceed;
100 }
101
102 enum command_result
103 command_break (FILE *output, int argc, char **argv)
104 {
105         return command_error;
106 }
107
108 enum command_result
109 command_clear (FILE *output, int argc, char **argv)
110 {
111         return command_error;
112 }
113
114 enum command_result
115 command_run (FILE *output, int argc, char **argv)
116 {
117         uint16_t start, end;
118         enum command_result result;
119         
120         if (argv[1]) {
121                 result = parse_uint16(argv[1], &start);
122                 if (result != command_proceed)
123                         return result;
124                 if (argv[2]) {
125                         result = parse_uint16(argv[2], &end);
126                         if (result != command_proceed)
127                                 return result;
128                 }
129                 ccdbg_set_pc(s51_dbg, start);
130         }
131         else
132                 start = ccdbg_get_pc(s51_dbg);
133         fprintf(output, "Resume at 0x%04x\n", start);
134         ccdbg_resume(s51_dbg);
135         return command_proceed;
136 }
137
138 enum command_result
139 command_next (FILE *output, int argc, char **argv)
140 {
141         return command_error;
142 }
143
144 enum command_result
145 command_step (FILE *output, int argc, char **argv)
146 {
147         return command_error;
148 }
149
150 enum command_result
151 command_load (FILE *output, int argc, char **argv)
152 {
153         return command_error;
154 }
155
156 enum command_result
157 command_halt (FILE *output, int argc, char **argv)
158 {
159         uint16_t        pc;
160         ccdbg_halt(s51_dbg);
161         pc = ccdbg_get_pc(s51_dbg);
162         fprintf(output, "Halted at 0x%04x\n", pc);
163         return command_proceed;
164 }
165
166 enum command_result
167 command_reset (FILE *output, int argc, char **argv)
168 {
169         ccdbg_debug_mode(s51_dbg);
170         return command_proceed;
171 }