OSDN Git Service

Step 10 added.
[kozos-expbrd/kozos_expbrd.git] / firm / tools / kz_h8write / motdump.c
1 /**
2  * @file motdump.c
3  * @author Shinichiro Nakamura
4  * @brief motファイルダンププログラムの実装。
5  */
6
7 /*
8  * ===============================================================
9  *  mot dump utility
10  *  Version 0.0.3
11  * ===============================================================
12  * Copyright (c) 2010-2011 Shinichiro Nakamura
13  *
14  * Permission is hereby granted, free of charge, to any person
15  * obtaining a copy of this software and associated documentation
16  * files (the "Software"), to deal in the Software without
17  * restriction, including without limitation the rights to use,
18  * copy, modify, merge, publish, distribute, sublicense, and/or
19  * sell copies of the Software, and to permit persons to whom the
20  * Software is furnished to do so, subject to the following
21  * conditions:
22  *
23  * The above copyright notice and this permission notice shall be
24  * included in all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  * OTHER DEALINGS IN THE SOFTWARE.
34  * ===============================================================
35  */
36
37 #include <stdio.h>
38 #include <ctype.h>
39 #include "mot.h"
40
41 unsigned char memory_image[0x100000 + 0x100];
42 unsigned int memory_lastaddr = 0;
43
44 void cb_data(
45         const unsigned int addr,
46         const unsigned char *buf,
47         const int siz)
48 {
49     for (int i = 0; i < siz; i++) {
50         memory_image[addr + i] = buf[i];
51     }
52     if (memory_lastaddr < addr) {
53         memory_lastaddr = addr;
54     }
55 }
56
57 /**
58  * @brief 1行に表示する桁数。
59  */
60 #define COLSIZ 16
61
62 /**
63  * @brief 切り上げ処理。
64  * @details 数値AをBで切り上げる。
65  */
66 #define ROUND_OUT(A,B) ((((A)+((B)-1))/(B))*(B))
67
68 /**
69  * @brief エントリポイント。
70  */
71 int main(int argc, char **argv) {
72     if (argc != 2) {
73         printf("%s [filename]\n", argv[0]);
74         return 1;
75     }
76     mot_t mot;
77     mot.cb_data = cb_data;
78     if (mot_read(argv[1], &mot) == 0) {
79         unsigned char cbuf[COLSIZ];
80         unsigned int ccnt = 0;
81         for (unsigned int addr = 0; addr < ROUND_OUT(memory_lastaddr, COLSIZ); addr++) {
82             /*
83              * 行頭表示処理。
84              */
85             if ((addr % COLSIZ) == 0) {
86                 printf("\n");
87                 printf("0x%04X :", addr);
88             }
89             /*
90              * データ表示処理。
91              */
92             printf(" %02X", memory_image[addr] & 0xff);
93             cbuf[addr % COLSIZ] = memory_image[addr] & 0xff;
94             ccnt++;
95             /*
96              * 行端表示処理。
97              */
98             if (((addr + 1) % COLSIZ) == 0) {
99                 if (ccnt > 0) {
100                     printf(" | ");
101                     for (int i = 0; i < COLSIZ; i++) {
102                         if (i < (int)ccnt) {
103                             printf("%c", isprint(cbuf[i] & 0xff)
104                                     ? (cbuf[i] & 0xff) : '.');
105                         } else {
106                             printf(" ");
107                         }
108                     }
109                 }
110                 ccnt = 0;
111             }
112         }
113     } else {
114         printf("Read failed.\n");
115     }
116     printf("\n");
117     return 0;
118 }
119