Handle GPS satellite tracking data
[fw/altos] / ao-tools / lib / 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 static uint8_t  memory_init[] = {
26         3,      MOV_DPTR_data16,        0,      0,
27 #define HIGH_START      2
28 #define LOW_START       3
29         0,
30 };
31
32
33 static uint8_t write8[] = {
34         2,      MOV_A_data,     0,
35 #define DATA_BYTE       2
36         1,      MOVX_atDPTR_A,
37         1,      INC_DPTR,
38         0
39 };
40
41 static uint8_t read8[] = {
42         1,      MOVX_A_atDPTR,
43         1,      INC_DPTR,
44         0,
45 };
46
47 uint8_t
48 ccdbg_write_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
49 {
50         int i, nl = 0;
51         struct ccstate state;
52
53         if (dbg->usb)
54                 return cc_usb_write_memory(dbg->usb, addr, bytes, nbytes);
55         ccdbg_state_save(dbg, &state, CC_STATE_ACC | CC_STATE_PSW | CC_STATE_DP);
56         memory_init[HIGH_START] = addr >> 8;
57         memory_init[LOW_START] = addr;
58         (void) ccdbg_execute(dbg, memory_init);
59         for (i = 0; i < nbytes; i++) {
60                 write8[DATA_BYTE] = *bytes++;
61                 ccdbg_execute(dbg, write8);
62                 if ((i & 0xf) == 0xf) {
63                         ccdbg_debug(CC_DEBUG_MEMORY, ".");
64                         ccdbg_flush(CC_DEBUG_MEMORY);
65                         nl = 1;
66                 }
67                 if ((i & 0xff) == 0xff) {
68                         ccdbg_debug(CC_DEBUG_MEMORY, "\n");
69                         nl = 0;
70                 }
71         }
72         ccdbg_state_restore(dbg, &state);
73         if (nl)
74                 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
75         return 0;
76 }
77
78 uint8_t
79 ccdbg_read_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes)
80 {
81         int i, nl = 0;
82         struct ccstate state;
83
84         if (ccdbg_rom_contains(dbg, addr, nbytes)) {
85                 ccdbg_rom_replace_xmem(dbg, addr, bytes, nbytes);
86                 return 0;
87         }
88         if (dbg->usb)
89                 return cc_usb_read_memory(dbg->usb, addr, bytes, nbytes);
90         ccdbg_state_save(dbg, &state, CC_STATE_ACC | CC_STATE_PSW | CC_STATE_DP);
91         memory_init[HIGH_START] = addr >> 8;
92         memory_init[LOW_START] = addr;
93         (void) ccdbg_execute(dbg, memory_init);
94         for (i = 0; i < nbytes; i++) {
95                 *bytes++ = ccdbg_execute(dbg, read8);
96                 if ((i & 0xf) == 0xf) {
97                         ccdbg_debug(CC_DEBUG_MEMORY, ".");
98                         ccdbg_flush(CC_DEBUG_MEMORY);
99                         nl = 1;
100                 }
101                 if ((i & 0xff) == 0xff) {
102                         ccdbg_debug(CC_DEBUG_MEMORY, "\n");
103                         nl = 0;
104                 }
105         }
106         ccdbg_state_replace_xmem(dbg, &state, addr, bytes, nbytes);
107         ccdbg_state_restore(dbg, &state);
108         if (nl)
109                 ccdbg_debug(CC_DEBUG_MEMORY, "\n");
110         return 0;
111 }
112
113 uint8_t
114 ccdbg_write_uint8(struct ccdbg *dbg, uint16_t addr, uint8_t byte)
115 {
116         return ccdbg_write_memory(dbg, addr, &byte, 1);
117 }
118
119 uint8_t
120 ccdbg_write_hex_image(struct ccdbg *dbg, struct hex_image *image, uint16_t offset)
121 {
122         ccdbg_write_memory(dbg, image->address + offset, image->data, image->length);
123         return 0;
124 }
125
126 struct hex_image *
127 ccdbg_read_hex_image(struct ccdbg *dbg, uint16_t address, uint16_t length)
128 {
129         struct hex_image *image;
130
131         image = calloc(sizeof(struct hex_image) + length, 1);
132         image->address = address;
133         image->length = length;
134         memset(image->data, 0xff, length);
135         ccdbg_read_memory(dbg, address, image->data, length);
136         return image;
137 }
138
139 static uint8_t sfr_read[] = {
140         2,      MOV_A_direct,           0,
141 #define SFR_READ_ADDR   2
142         0,
143 };
144
145 static uint8_t sfr_write[] = {
146         3,      MOV_direct_data,        0,      0,
147 #define SFR_WRITE_ADDR  2
148 #define SFR_WRITE_DATA  3
149         0,
150 };
151
152 uint8_t
153 ccdbg_read_sfr(struct ccdbg *dbg, uint8_t addr, uint8_t *bytes, int nbytes)
154 {
155         int     i;
156         struct ccstate state;
157
158         ccdbg_state_save(dbg, &state, CC_STATE_ACC);
159         for (i = 0; i < nbytes; i++) {
160                 sfr_read[SFR_READ_ADDR] = addr + i;
161                 *bytes++ = ccdbg_execute(dbg, sfr_read);
162         }
163         ccdbg_state_replace_sfr(dbg, &state, addr, bytes, nbytes);
164         ccdbg_state_restore(dbg, &state);
165         return 0;
166 }
167
168 uint8_t
169 ccdbg_write_sfr(struct ccdbg *dbg, uint8_t addr, uint8_t *bytes, int nbytes)
170 {
171         int     i;
172
173         for (i = 0; i < nbytes; i++) {
174                 sfr_write[SFR_WRITE_ADDR] = addr + i;
175                 sfr_write[SFR_WRITE_DATA] = *bytes++;
176                 ccdbg_execute(dbg, sfr_write);
177         }
178         return 0;
179 }