OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [libdbg64g/] [services/] [exec/] [cmd/] [cmd_memdump.cpp] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 sergeykhbr
/*
2
 *  Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15 3 sergeykhbr
 */
16
 
17
#include "cmd_memdump.h"
18
#include <string>
19
 
20
namespace debugger {
21
 
22
CmdMemDump::CmdMemDump(ITap *tap, ISocInfo *info)
23
    : ICommand ("memdump", tap, info) {
24
 
25
    briefDescr_.make_string("Dump memory to file");
26
    detailedDescr_.make_string(
27
        "Description:\n"
28
        "    Dump memory to file (default in Binary format).\n"
29
        "Usage:\n"
30
        "    memdump <addr> <bytes> [filepath] [bin|hex]\n"
31
        "Example:\n"
32
        "    memdump 0x0 8192 dump.bin\n"
33
        "    memdump 0x40000000 524288 dump.hex hex\n"
34
        "    memdump 0x10000000 128 \"c:/My Documents/dump.bin\"\n");
35
}
36
 
37
bool CmdMemDump::isValid(AttributeType *args) {
38
    if ((*args)[0u].is_equal(cmdName_.to_string())
39
     && (args->size() == 4 || args->size() == 5)) {
40
        return CMD_VALID;
41
    }
42
    return CMD_INVALID;
43
}
44
 
45
void CmdMemDump::exec(AttributeType *args, AttributeType *res) {
46
    res->make_nil();
47
    if (!isValid(args)) {
48
        generateError(res, "Wrong argument list");
49
        return;
50
    }
51
 
52
    const char *filename = (*args)[3].to_string();
53 4 sergeykhbr
    FILE *fd = fopen(filename, "wb");
54 3 sergeykhbr
    if (fd == NULL) {
55
        char tst[256];
56
        RISCV_sprintf(tst, sizeof(tst), "Can't open '%s' file", filename);
57
        generateError(res, tst);
58
        return;
59
    }
60
    uint64_t addr = (*args)[1].to_uint64();
61
    int len = static_cast<int>((*args)[2].to_uint64());
62
    res->make_data(len);
63
 
64
    tap_->read(addr, len, res->data());
65
    uint8_t *dumpbuf = res->data();
66
 
67
    if (args->size() == 5 && (*args)[4].is_equal("hex")) {
68
        char t1[256];
69
        int t1_cnt = 0;
70
        int idx;
71
        for (int i = 0; i < ((len + 0xf) & ~0xf); i++) {
72
            idx = (i & ~0xf) | (0xf - (i & 0xf));
73
            if (idx > len) {
74
                t1[t1_cnt++] = ' ';
75
                t1[t1_cnt++] = ' ';
76
                continue;
77
            }
78
            t1_cnt += RISCV_sprintf(&t1[t1_cnt], sizeof(t1) - t1_cnt, "%02x",
79
                                    dumpbuf[idx]);
80
            if ((i & 0xf) != 0xf) {
81
                continue;
82
            }
83
            t1_cnt += RISCV_sprintf(&t1[t1_cnt], sizeof(t1) - t1_cnt, "\n");
84
            fwrite(t1, t1_cnt, 1, fd);
85
            t1_cnt = 0;
86
        }
87
    } else {
88
        fwrite(dumpbuf, len, 1, fd);
89
    }
90
    fclose(fd);
91
}
92
 
93
}  // namespace debugger

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.