Add ability to load Intel HEX files. Add sample sdcc LED blinker.
[fw/altos] / ccdbg.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 "ccdbg.h"
20
21 #define MOV_direct_data         0x75
22 #define LJMP                    0x02
23 #define MOV_Rn_data(n)          (0x78 | (n))
24 #define DJNZ_Rn_rel(n)          (0xd8 | (n))
25
26 #if 0
27 static uint8_t instructions[] = {
28         3, MOV_direct_data, 0xfe, 0x02,
29         3, MOV_direct_data, 0x90, 0xff,
30         0
31 };
32 #endif
33
34 static uint8_t mem_instr[] = {
35         MOV_direct_data, 0xfe, 0x02,
36         MOV_direct_data, 0x90, 0xff,
37         MOV_Rn_data(2), 0x10,
38         MOV_Rn_data(0), 0xff,
39         MOV_Rn_data(1), 0xff,
40         DJNZ_Rn_rel(1), 0xfe,
41         DJNZ_Rn_rel(0), 0xfa,
42         DJNZ_Rn_rel(2), 0xf6,
43         MOV_direct_data, 0x90, 0xfd,
44         MOV_Rn_data(2), 0x10,
45         MOV_Rn_data(0), 0xff,
46         MOV_Rn_data(1), 0xff,
47         DJNZ_Rn_rel(1), 0xfe,
48         DJNZ_Rn_rel(0), 0xfa,
49         DJNZ_Rn_rel(2), 0xf6,
50         LJMP, 0xf0, 0x03
51 };
52
53 static uint8_t jump_mem[] = {
54         3, LJMP, 0xf0, 0x00,
55         0
56 };
57
58 int
59 main (int argc, char **argv)
60 {
61         struct ccdbg    *dbg;
62         uint8_t         status;
63         uint16_t        chip_id;
64         uint16_t        pc;
65         uint8_t         memory[0x10];
66         int             i;
67         struct hex_file *hex;
68
69         dbg = ccdbg_open("/dev/ttyUSB0");
70         if (!dbg)
71                 exit (1);
72 #if 0
73         ccdbg_manual(dbg, stdin);
74 #endif
75         hex = ccdbg_hex_file_read(stdin, "<stdin>");
76         if (!hex)
77                 exit (1);
78         ccdbg_reset(dbg);
79         ccdbg_debug_mode(dbg);
80         status = ccdbg_read_status(dbg);
81         printf("Status: 0x%02x\n", status);
82         chip_id = ccdbg_get_chip_id(dbg);
83         printf("Chip id: 0x%04x\n", chip_id);
84         status = ccdbg_halt(dbg);
85         printf ("halt status: 0x%02x\n", status);
86         
87         ccdbg_write_hex(dbg, hex);
88         ccdbg_hex_file_free(hex);
89         for (i = 0; i < sizeof (memory); i++)
90                 printf (" %02x", memory[i]);
91         printf ("\n");
92         ccdbg_execute(dbg, jump_mem);
93         pc = ccdbg_get_pc(dbg);
94         printf ("pc starts at 0x%04x\n", pc);
95         status = ccdbg_resume(dbg);
96         printf ("resume status: 0x%02x\n", status);
97 #if 0
98 /*      ccdbg_execute(dbg, instructions); */
99         ccdbg_write_memory(dbg, 0xf000, mem_instr, sizeof (mem_instr));
100         ccdbg_read_memory(dbg, 0xf000, memory, sizeof (memory));
101 #endif
102         ccdbg_close(dbg);
103         exit (0);
104 }