source: UsbWattMeter/trunk/asp_dcre/utils/genoffset@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 7 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

File size: 6.2 KB
Line 
1#! /usr/bin/perl
2#
3# TOPPERS Software
4# Toyohashi Open Platform for Embedded Real-Time Systems
5#
6# Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
7# Toyohashi Univ. of Technology, JAPAN
8# Copyright (C) 2004-2011 by Embedded and Real-Time Systems Laboratory
9# Graduate School of Information Science, Nagoya Univ., JAPAN
10#
11# ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
12# ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
13# •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
14# (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
15# Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
16# ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
17# (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
18# —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
19ƒƒ“ƒgi—˜—p
20# ŽÒƒ}ƒjƒ…
21ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
22# ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
23# (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
24# —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
25# ‚ƁD
26# (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
27ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
28ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
29# ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
30# (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
31# •ñ‚·‚邱‚ƁD
32# (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
33# ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
34# ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
35# —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
36# –Ɛӂ·‚邱‚ƁD
37#
38# –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
39# ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
40# ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
41# ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
42# ‚̐ӔC‚𕉂í‚È‚¢D
43#
44# @(#) $Id: genoffset 1840 2010-07-10 17:29:42Z ertl-hiro $
45#
46
47#
48# ‰Šú‰»
49#
50$infile = $ARGV[0];
51$sil_endian = "";
52
53#
54# ‰ðŽß‚Å‚«‚éƒfƒBƒŒƒNƒeƒBƒu‚̃e[ƒuƒ‹
55#
56%directives = (
57 "long", "",
58 "dword", "",
59 "word", "",
60 "hword", "",
61 "int", "",
62 "short", "",
63 "half", "",
64 "byte", "",
65 "value", "",
66 "uaword", "",
67 "uashort", "",
68 "data32", 4,
69 "data16", 2,
70 "data8", 1,
71 "zero", -1,
72 "space", -1,
73 "globl", 0,
74 "global", 0,
75 "align", 0,
76 "p2align", 0,
77 "even", 0,
78 "data", 0,
79 "stabs", 0,
80 "type", 0,
81 "size", 0,
82 "section", 0,
83 "sdata", 0,
84 "code", 0,
85);
86
87#
88# ƒIƒtƒZƒbƒg’l‚̏o—Í
89#
90sub parse_offset {
91 my($label, $val) = @_;
92
93 printf "#define %s\t%d\n",$label,$val;
94}
95
96#
97# ƒfƒBƒŒƒNƒeƒBƒu‚Ì“Ç‚ÝŽæ‚è
98#
99sub ref_bit {
100 my($size) = @_;
101 my($dir, $directive);
102
103 while ($line = <INFILE>) {
104 chomp $line;
105 next if ($line =~ /^[ \t]*[#;].*$/);
106
107 if ($line =~ /[ \t]*\.([a-zA-Z0-9]+)[ \t]*([^ \t]*)[ \t]*/
108 && defined($dir = $directives{$1})) {
109 $directive = $1;
110 if ($dir eq "") {
111 # “o˜^‚·‚ׂ«ƒfƒBƒŒƒNƒeƒBƒu
112 $directives{$directive} = $size;
113 }
114 last;
115 }
116 else {
117 # ‰ðÍ‚Å‚«‚È‚¢s
118 print STDERR "genoffset: cannot parse: $line\n";
119 $error = 1;
120 }
121 }
122}
123
124#
125# ƒrƒbƒgƒT[ƒ`
126#
127sub search_bit {
128 my($val) = @_;
129 my($val_bit);
130
131 return(-1) if ($val == 0);
132
133 $val_bit = 0;
134 while (($val & 1) == 0) {
135 $val >>= 1;
136 $val_bit++;
137 }
138 return($val_bit);
139}
140
141#
142# ƒrƒbƒgˆÊ’u‚̏o—Í
143#
144sub parse_bit {
145 my($endian, $size, $label) = @_;
146 my($offset, $dir, $val, $val_bit);
147
148 if ($sil_endian && $endian ne $sil_endian) {
149 # ƒGƒ“ƒfƒBƒAƒ“‚Ì•sˆê’v
150 print STDERR "genoffset: endian mismatch: $line\n";
151 $error = 1;
152 }
153
154 $offset = 0;
155 while ($line = <INFILE>) {
156 chomp $line;
157 next if ($line =~ /^[ \t]*[#;].*$/);
158
159 if ($line =~ /[ \t]*\.([a-zA-Z0-9]+)[ \t]*([^ \t]*)[ \t]*/
160 && defined($dir = $directives{$1})) {
161 $val = $2;
162
163 # 16i”‚Æ8i”‚̐”’l‚Ö‚Ì•ÏŠ·ˆ—
164 if ($val =~ /^0x(.+)$/) {
165 $val = hex($1);
166 }
167 elsif ($val =~ /^0(.+)$/) {
168 $val = oct($1);
169 }
170
171 if ($dir eq "") {
172 # ƒTƒCƒY‚ð’m‚ç‚È‚¢ƒfƒBƒŒƒNƒeƒBƒu
173 print STDERR "genoffset: unknown directive: $line\n";
174 $error = 1;
175 }
176 elsif ($dir == 0) {
177 # “Ç‚Ý”ò‚΂·‚ׂ«ƒfƒBƒŒƒNƒeƒBƒu
178 }
179 elsif ($dir == -1) {
180 # .zeroƒfƒBƒŒƒNƒeƒBƒu‚̏ˆ—
181 $offset += $val;
182 }
183 elsif ($val == 0) {
184 # ’l‚ª0‚̃tƒB[ƒ‹ƒh‚̏ˆ—
185 $offset += $dir;
186 }
187 else {
188 # ’l‚ª0‚Å‚È‚¢ƒtƒB[ƒ‹ƒh‚ªŒ©‚‚©‚ê‚΃‹[ƒv‚ðI—¹
189 last;
190 }
191 }
192 else {
193 # ‰ðÍ‚Å‚«‚È‚¢s
194 print STDERR "genoffset: cannot parse: $line\n";
195 $error = 1;
196 }
197 }
198
199 # ƒrƒbƒgˆÊ’u‚ð’T‚·
200 $val_bit = search_bit($val);
201
202 # ƒoƒCƒg’PˆÊ‚ÉŠ·ŽZ‚·‚é
203 if ($endian eq "B") {
204 $offset += $dir - 1;
205 $offset -= $val_bit >> 3;
206 }
207 else {
208 $offset += $val_bit >> 3;
209 }
210 $val_bit &= 0x07;
211
212 # o—Í’PˆÊ‚ÉŠ·ŽZ‚·‚é
213 if ($size eq "W") {
214 if ($endian eq "B") {
215 $val_bit += 24;
216 $val_bit -= ($offset & 0x03) << 3;
217 }
218 else {
219 $val_bit += ($offset & 0x03) << 3;
220 }
221 $offset &= ~0x03;
222 }
223 elsif ($size eq "H") {
224 if ($endian eq "B") {
225 $val_bit += 8;
226 $val_bit -= ($offset & 0x01) << 3;
227 }
228 else {
229 $val_bit += ($offset & 0x01) << 3;
230 }
231 $offset &= ~0x01;
232 }
233
234 # ’è‹`‚̏o—Í
235 $label =~ s/^_//;
236 printf "#define %s\t%d\n",$label,$offset;
237 printf "#define %s_bit\t%d\n",$label,$val_bit;
238 printf "#define %s_mask\t0x%x\n",$label,(1 << $val_bit);
239}
240
241#
242# ƒƒCƒ“ƒ‹[ƒ`ƒ“
243#
244print "/* This file is generated by genoffset. */\n";
245print "\n";
246
247$error = 0;
248open(INFILE, $infile) || die "Cannot open $infile";
249while ($line = <INFILE>) {
250 chomp $line;
251
252 if ($line =~ /^[ \t]*OFFSET_DEF ([^ \t]+) = [#\$]?([^ \t]+)/) {
253 parse_offset($1, $2);
254 }
255 elsif ($line =~ /^[ \t]*_?BIT_REF_([0-9]+):/) {
256 ref_bit($1);
257 }
258 elsif ($line =~ /^[ \t]*SIL_ENDIAN = ([BL])/) {
259 $sil_endian = $1;
260 }
261}
262#
263# ƒRƒ“ƒpƒCƒ‰‚ªƒOƒ[ƒoƒ‹•Ï”‚Ì”z’u‡˜‚ð•ÏX‚·‚é‚ƁCBIT_REF_?‚ª‰º‚É‚È
264# ‚é‰Â”\«‚ª‚ ‚邽‚߁C“ª‚©‚ç“Ç‚Ý’¼‚·D
265#
266seek(INFILE, 0, SEEK_SET);
267while ($line = <INFILE>) {
268 chomp $line;
269
270 if ($line =~ /^[ \t]*_?BIT_([BL])([BHW])_([^ \t]+):/) {
271 parse_bit($1, $2, $3);
272 }
273}
274close(INFILE);
275exit($error);
Note: See TracBrowser for help on using the repository browser.