fa953d17ca7f45668294a71c773f6b95aff7785f
[fw/altos] / ccdbg-memory.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 /*
22  * Read and write arbitrary memory through the debug port
23  */
24
25 #define MOV_dptr_data16         0x90
26
27 static uint8_t  memory_init[] = {
28         3,      MOV_dptr_data16,        0,      0,
29 #define HIGH_START      2
30 #define LOW_START       3
31         0,
32 };
33
34 #define MOV_a_data      0x74
35 #define MOVX_atdptr_a   0xf0
36 #define MOVX_a_atdptr   0xe0
37 #define INC_dptr        0xa3
38
39 static uint8_t write8[] = {
40         2,      MOV_a_data,     0,
41 #define DATA_BYTE       2
42         1,      MOVX_atdptr_a,
43         1,      INC_dptr,
44         0
45 };
46
47 static uint8_t read8[] = {
48         1,      MOVX_a_atdptr,
49         1,      INC_dptr,
50         0,
51 };
52
53 uint8_t
54 ccdbg_write_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
55 {
56         memory_init[HIGH_START] = addr >> 8;
57         memory_init[LOW_START] = addr;
58         (void) ccdbg_execute(dbg, memory_init);
59         while (nbytes-- > 0) {
60                 write8[DATA_BYTE] = *bytes++;
61                 ccdbg_execute(dbg, write8);
62         }
63         return 0;
64 }
65
66 uint8_t
67 ccdbg_read_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
68 {
69         memory_init[HIGH_START] = addr >> 8;
70         memory_init[LOW_START] = addr;
71         (void) ccdbg_execute(dbg, memory_init);
72         while (nbytes-- > 0)
73                 *bytes++ = ccdbg_execute(dbg, read8);
74         return 0;
75 }
76
77 uint8_t
78 ccdbg_write_hex(struct ccdbg *dbg, struct hex_file *hex)
79 {
80         int     i;
81         struct hex_record *record;
82
83         for (i = 0; i < hex->nrecord; i++) {
84                 record = hex->records[i];
85                 if (record->type == HEX_RECORD_EOF)
86                         break;
87                 printf("Write %d bytes at 0x%04x\n",
88                        record->length, record->address);
89                 ccdbg_write_memory(dbg, record->address,
90                                    record->data, record->length);
91         }
92         return 0;
93 }