Telemetry code was mis-computing RSSI
[fw/altos] / ao-tools / lib / ccdbg-rom.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 uint8_t
22 ccdbg_set_rom(struct ccdbg *dbg, struct hex_image *rom)
23 {
24         if (dbg->rom)
25                 ccdbg_hex_image_free(dbg->rom);
26         dbg->rom = rom;
27         return 0;
28 }
29
30 uint8_t
31 ccdbg_rom_contains(struct ccdbg *dbg, uint16_t addr, int nbytes)
32 {
33         struct hex_image *rom = dbg->rom;
34         if (!rom)
35                 return 0;
36         if (addr < rom->address || rom->address + rom->length < addr + nbytes)
37                 return 0;
38         return 1;
39 }
40
41 uint8_t
42 ccdbg_rom_replace_xmem(struct ccdbg *dbg,
43                        uint16_t addr, uint8_t *bytes, int nbytes)
44 {
45         struct hex_image *rom = dbg->rom;
46         if (!rom)
47                 return 0;
48
49         if (rom->address < addr + nbytes && addr < rom->address + rom->length) {
50                 int     start, stop;
51
52                 start = addr;
53                 if (addr < rom->address)
54                         start = rom->address;
55                 stop = addr + nbytes;
56                 if (rom->address + rom->length < stop)
57                         stop = rom->address + rom->length;
58                 memcpy(bytes + start - addr, rom->data + start - rom->address,
59                        stop - start);
60                 return 1;
61         }
62         return 0;
63 }