O HAI THIS BLOG PURPZIEZ 2 B UZED AZ MAH PLESIOUS MEM. :)

2008/09/25

I CAN HAS VHDL? #4

VHDLについて、テキトーに書いてみるべす、第四回。

前回はロードとイネーブル付きの蝶テキトーなカウンタしかださなかったので、少しマトモなモノにしてみる。"すとりーむ"な信号処理をメインにメシを喰っているので、その辺りから。比較的簡単なブツで、しかもVHDLっぽいモノっつー訳で移動平均フィルタにしますか。

はい、まずは数学的基礎。離散時間表現では、
\overline{x[k + L]} = 1/N \sum_{n = 0}^{N} x[k - n]
ですな。移動平均長がNで実装上のレイテンシーがLね。そんだけ。

じゃあ、実装です。とりあえず、こうしようぜ。
\overline{x[k + L]} = (x[k] + x[k-1] + ... + x[k - N]) / N
完璧ですね、と思ったアナタには漏れ無くパンチをプレゼント。 :DDD

次の様に漸化式にするのが、王道です。
\delta S[k + L_1] = x[k] - x[k - L]
S[k + L_2] = S[k] + \delta S[k]
\overline{x[k + L_3]} = S[k] / N
さらに実装を容易にする為、Nを2の冪乗に限定する事で除算を算術右シフトにします。
\delta S[k + L_1] = x[k] - x[k - N]
S[k + L_2] = S[k] + \delta S[k]
\overline{x[k + L_3]} = SAR(S[k], log_{2}(N))
つまり、移動平均長Nだけ離れた入力の差分を積算してテキトーに上位ビットを切り出すだけと言う簡単な回路。

あまりに簡単過ぎるので、もう少し内容を盛り込む。
まずは、instantiateする場合にcomponent declarationを書くのがダルいのでpackageを作って、use clauseを使う。つぎに、architecture bodyで使うsignalをrecordを使ってムニゃる。あと、synchronous/asynchronous resetに対応する。

以下、ソース。
     1 --
2 -- Moving Average Filter
3 --
4
5 library ieee;
6 use ieee.std_logic_1164.all;
7
8 package MAF_PKG is
9
10 component MAF is
11 generic (
12 IS_SYNC : boolean := true;
13 DW : integer range 2 to integer'high := 12;
14 log2N : integer range 1 to integer'high := 3
15 );
16 port (
17 iCLK : in std_logic;
18 iCLR : in std_logic;
19 iD : in std_logic_vector(DW-1 downto 0);
20 oQ : out std_logic_vector(DW-1 downto 0)
21 );
22 end component MAF;
23
24 end package MAF_PKG;
25
26 package body MAF_PKG is
27
28 end package body MAF_PKG;
29
30 library ieee;
31 use ieee.std_logic_1164.all;
32 use ieee.std_logic_arith.all;
33
34 entity MAF is
35 generic (
36 IS_SYNC : boolean := true;
37 DW : integer range 2 to integer'high := 12;
38 log2N : integer range 1 to integer'high := 3
39 );
40 port (
41 iCLK : in std_logic;
42 iCLR : in std_logic;
43 iD : in std_logic_vector(DW-1 downto 0);
44 oQ : out std_logic_vector(DW-1 downto 0)
45 );
46 begin
47 end entity MAF;
48
49 architecture TP of MAF is
50
51 constant cN : integer range 2 to integer'high := 2**log2N+1;
52
53 type tD is array (0 to cN-1) of std_logic_vector(DW-1 downto 0);
54 type t is record
55 D : tD;
56 dS : std_logic_vector( DW downto 0);
57 dSE : std_logic_vector(log2N+DW-1 downto 0);
58 S : std_logic_vector(log2N+DW-1 downto 0);
59 Q : std_logic_vector( DW-1 downto 0);
60 end record t;
61
62 constant c : t := (
63 D => (others => (others => '0')),
64 dS => (others => '0'),
65 dSE => (others => '0'),
66 S => (others => '0'),
67 Q => (others => '0')
68 );
69
70 signal g : t;
71 signal r : t := c;
72
73 begin
74
75 P_COMB : process (iD, r)
76 variable v : t;
77 begin
78 -- NOTE: Shift inputted data (D).
79 F_SHIFT_D : for index in 0 to cN-1 loop
80 if (index = 0) then
81 v.D(index) := iD;
82 else
83 v.D(index) := r.D(index-1);
84 end if;
85 end loop F_SHIFT_D;
86
87 -- NOTE: Compute delta sum (dS).
88 v.dS :=
89 signed(r.D( 0)(DW-1) & r.D( 0)) -
90 signed(r.D(cN-1)(DW-1) & r.D(cN-1));
91
92 -- NOTE: Sign expansion for delta sum (dSE).
93 v.dSE(log2N+DW-1 downto DW) := (log2N -1 downto 0 => r.dS(DW));
94 v.dSE( DW-1 downto 0) := r.dS( DW-1 downto 0);
95
96 -- NOTE: Accumulate sum (S).
97 v.S := signed(r.S) + signed(r.dSE);
98
99 -- NOTE: Divide sum by length for MA (Q).
100 v.Q := r.S(log2N+DW-1 downto log2N);
101
102 g <= v;
103 oQ <= r.Q;
104 end process P_COMB;
105
106 G_SYNC : if (IS_SYNC = true) generate
107 begin
108 P_SEQ : process (iCLK)
109 begin
110 if (iCLK'event and iCLK = '1') then
111 if (iCLR = '1') then
112 r <= c;
113 else
114 r <= g;
115 end if;
116 end if;
117 end process P_SEQ;
118 end generate G_SYNC;
119
120 G_ASYNC : if (IS_SYNC = false) generate
121 begin
122 P_SEQ : process (iCLR, iCLK)
123 begin
124 if (iCLR = '1') then
125 r <= c;
126 elsif (iCLK'event and iCLK = '1') then
127 r <= g;
128 end if;
129 end process P_SEQ;
130 end generate G_ASYNC;
131
132 end architecture TP;
はい、簡単ですね。 :)

さらに、何時もの様にやる気の無いテストベンチを作る。
     1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use work.MAF_PKG.all;
5
6 entity BENCH_MAF is
7 begin
8 end entity BENCH_MAF;
9
10 architecture BENCH of BENCH_MAF is
11
12 constant cIS_SYNC : boolean := true;
13 constant cDW : integer range 2 to integer'high := 12;
14 constant clog2N : integer range 1 to integer'high := 5;
15 constant cN : integer range 2 to integer'high := 2**clog2N;
16
17 constant cCLK_CYCLE : time := 1.0 us;
18 constant cCLR_TIME : time := (cN+1)*cCLK_CYCLE;
19
20 signal sCLK : std_logic := '0';
21 signal sCLR : std_logic := '1';
22 signal sLFSR_D : std_logic_vector(cDW-1 downto 0) := (others => '0');
23 signal sTRIANGLE_D : std_logic_vector(cDW-1 downto 0) := (others => '0');
24 signal sMAF_LFSR_oQ : std_logic_vector(cDW-1 downto 0);
25 signal sMAF_TRIANGLE_oQ : std_logic_vector(cDW-1 downto 0);
26
27 function fLFSR (
28 iD : std_logic_vector;
29 iDW : integer range 2 to integer'high
30 ) return std_logic_vector is
31 begin
32 if (iDW = 12) then
33 return (iD(7) xor iD(4) xor iD(3) xor iD(0)) & iD(11 downto 1);
34 else
35 assert (iDW = 12)
36 report "fLFSR: Call with non-supported iDW!!1" -- :P
37 severity warning;
38 return iD;
39 end if;
40 end function fLFSR;
41
42 function fTRIANGLE (
43 iD : std_logic_vector;
44 iDW : integer range 2 to integer'high
45 ) return std_logic_vector is
46 begin
47 if (signed(iD) <= -2**(iDW-1)) then
48 return conv_std_logic_vector(2**(iDW-1)-2**6, iDW);
49 else
50 return signed(iD) - 2**6;
51 end if;
52 end function fTRIANGLE;
53
54 begin
55
56 P_sCLK : process
57 begin
58 sCLK <= '0'; wait for cCLK_CYCLE/2;
59 sCLK <= '1'; wait for cCLK_CYCLE/2;
60 end process P_sCLK;
61
62 P_sCLR : process
63 begin
64 sCLR <= '1'; wait for cCLR_TIME;
65 sCLR <= '0'; wait;
66 end process P_sCLR;
67
68 P_sD : process (sCLK)
69 begin
70 if (sCLK'event and sCLK = '1') then
71 if (sCLR = '1') then
72 sLFSR_D <= (others => '1');
73 sTRIANGLE_D <= (others => '0');
74 else
75 sLFSR_D <= fLFSR(sLFSR_D, cDW);
76 sTRIANGLE_D <= fTRIANGLE(sTRIANGLE_D, cDW);
77 end if;
78 end if;
79 end process P_sD;
80
81 U_MAF_LFSR : MAF
82 generic map (
83 IS_SYNC => cIS_SYNC,
84 DW => cDW,
85 log2N => clog2N
86 )
87 port map (
88 iCLK => sCLK,
89 iCLR => sCLR,
90 iD => sLFSR_D,
91 oQ => sMAF_LFSR_oQ
92 );
93
94 U_MAF_TRIANGLE : MAF
95 generic map (
96 IS_SYNC => cIS_SYNC,
97 DW => cDW,
98 log2N => clog2N
99 )
100 port map (
101 iCLK => sCLK,
102 iCLR => sCLR,
103 iD => sTRIANGLE_D,
104 oQ => sMAF_TRIANGLE_oQ
105 );
106
107 end architecture BENCH;
見ての通り、入力はLFSRとノコギリ波。

前者をMAF.vhdl、後者をBENCH_MAF.vhdlとして、GHDLとGTKWaveでモニョる。workディレクトリを作って、analyzeして、elaborate。
$ mkdir work
$ ghdl -a --std=02 --workdir=./work --ieee=synopsys MAF.vhdl
$ ghdl -a --std=02 --workdir=./work --ieee=synopsys BENCH_MAF.vhdl
$ ghdl -e --std=02 --workdir=./work --ieee=synopsys -o BENCH_MAF-BENCH BENCH_MAF BENCH
GHWなwaveファイルを吐くオプションを付けて500[us]くらいrunさせてー、GTKWaveでモニョモニョ。
$ ./BENCH_MAF-BENCH --stop-time=500us --wave=BENCH_MAF-BENCH-500us.ghw
$ gtkwave BENCH_MAF-BENCH-500us.ghw

ハイ、いい感じに移動平均されているアルよ。 :)

以上の様に、結構いい感じのtwo-process手法だが、これ一本槍だと幾つか欠点があったりする。と言う感じで次回に続くのであったのであった。 :P

2008/09/18

BModular(&NetSurf, 1);

GSoCな感じで盛り上がっていたNetSurfの成果を確かめる為、再びモニョる。 :)
[NetSurd upstream did merge bunch of GSoC student code into their svn trunk.
So, it's time to BUMB!!1 :)]
09/18 00:55:20 hiyuh
hey, !NetSurf/Resources/*/Messages are used in gtk port?
09/18 00:55:38 jmb
not at present
09/18 00:55:47 jmb
well, s/*/en/ is
09/18 00:56:01 hiyuh
only en for now?
09/18 00:56:04 jmb
yeah
09/18 00:56:27 jmb
assuming that there's some UI to select the interface language,
and some code to determine the correct default, then they can be
09/18 00:56:55 hiyuh
true
09/18 00:58:00 jmb
basically, it needs someone to put in the small amount of effort
required to make them useful :)
09/18 00:59:03 hiyuh
I'm not gtk guy, but I can translate en to ja. :)
09/18 00:59:12 tlsa
didn't we get a czech translation?
09/18 00:59:12 jmb
that'd be cool
09/18 00:59:14 tlsa
cool
09/18 00:59:39 jmb
point being that these Messages files get used on all platforms,
so only need translating once :)
09/18 01:00:15 tlsa
is ja japanese?
09/18 01:00:20 tlsa
i thought that was .jp
09/18 01:00:44 jmb
depends which registry you're looking at :)
09/18 01:00:53 jmb
ja is the ISO-639 2 letter language code
09/18 01:01:13 hiyuh
anyway, I meant japanese.
09/18 01:01:21 tlsa
ok
09/18 01:01:43 tlsa
a Japanese translation would be great, anyway :)
09/18 01:02:36 tlsa
hey, if you could translate http://www.netsurf-browser.org/welcome/
09/18 01:02:47 tlsa
then I could test the language negotiation :)
09/18 01:02:55 jmb
hee
09/18 01:03:09 tlsa
we've only had English since the site was redesigned
09/18 01:03:21 hiyuh
lol
09/18 01:03:27 hiyuh
before translate en to ja, I should make modular NS ebuilds for
gentoo, am pokin' json-c now.
09/18 01:03:47 jmb
shouldn't need json-c at all
09/18 01:03:50 tlsa
ok
09/18 01:04:04 jmb
certainly not unless you actually want to do make test in hubbub
09/18 01:05:02 hiyuh
yup, I read README, and IIRC I already told hubbub's make test hogs
my entire mem.
09/18 01:05:30 hiyuh
but someone needs to test. :p
09/18 01:05:37 jmb
yes, it does
09/18 01:05:43 jmb
I can probably lose that now, actually
09/18 01:11:36 tlsa
hiyuh: on the welcome page, the links at the bottom can be changed
to relevant stuff for the language
09/18 01:11:54 tlsa
3 items in first column, which is "News"
09/18 01:12:21 tlsa
4 items in 2nd column, which is "IT / web"
09/18 01:12:54 tlsa
4 items in 3rd column, which is "Information"
09/18 01:13:14 tlsa
3 items in 4th column, which is "RISC OS"
09/18 01:13:27 tlsa
prob. not much choice for RISC OS sites though :0
09/18 01:13:38 tlsa
s/:0/:p/
09/18 01:14:55 tlsa
the only other guidelines are that the sites should be big and
important in their field, and that you should not need to log in to
use them (so no webmail sites / forums etc)
09/18 01:16:14 tlsa
wikipedia can be easily changed to the ja one
09/18 01:16:28 tlsa
and we should have W3C in all translations
09/18 01:18:22 jmb
hiyuh: svn up libparserutils
09/18 01:18:31 jmb
should stop the memory leaks :)
09/18 01:18:38 tlsa
and if you link to pages in a different language, use "BBC News
(English)" or the common abreviation like "BBC News (en)", except
translated of course :)
09/18 01:19:09 hiyuh
jmb: ok I'll try
09/18 01:22:45 hiyuh
tlsa: translating to ja is a plan as my next todo. I'd like to check
whether recent NS still did wrong line breaking or not, at first.
09/18 01:23:00 tlsa
i think it does
09/18 01:23:23 tlsa
we don't do propper unicode line breaking
09/18 01:23:29 tlsa
at least on RISC OS
09/18 01:23:54 tlsa
i'm not sure if that's RUfl or NetSurf's problem, on RO
09/18 01:25:16 hiyuh
eew
09/18 01:30:01 tlsa
gah, I can't screenshot the sonic team page
09/18 01:30:13 tlsa
it says Parsing the document failed
09/18 01:30:19 tlsa
jmb: is that encoding related?
09/18 01:30:46 tlsa
( http://www.sonicteam.com/ )
09/18 01:33:30 tlsa
Shift_JIS
09/18 01:44:00 tlsa
can we make the RISC OS version use the homepage in Makefile.config
and override it on the autobuilder?
09/18 01:44:48 tlsa
then my local checkout will be able to use that and not need to have
my local copys which generate loads of stuff for "svn status"
09/18 02:04:53 hiyuh
jmb: json-c is 0.3? ChangeLog has history to 0.8. and its autogen.sh
breaks w/ latest auto*, I guess.
09/18 02:05:04 jmb
pass
09/18 02:05:28 jmb
I just checked out upstream's svn HEAD, then patched it
09/18 02:07:37 hiyuh
ah, ok
09/18 02:10:46 tlsa
http://source.netsurf-browser.org/?view=rev&revision=5367
09/18 02:12:42 tlsa
one thing I noticed was that BeOS doesn't have a setting for Hubbub
(has it been ported yet?) and that on GTK it's set to AUTO
09/18 02:12:59 tlsa
I thought we were aiming to get all platforms setting that to YES
09/18 02:13:17 hiyuh
lol
09/18 02:13:20 hiyuh
http://dev.gentoo.gr.jp/~hiyuh/misc/json-c-configure-in.diff
09/18 02:14:01 hiyuh
missing double quote :)
09/18 02:15:53 hiyuh
running ./configure shows mysterious message: "C: command not fond" :D
09/18 02:21:28 jmb
fun
09/18 02:26:26 hiyuh
hmm, it still some "C" appears.
09/18 02:43:49 hiyuh
jmb: libparserutils's DESTDIR fix is incomplete. in "install:"
all of $(INSTALL) lines have no $(DESTDIR). you just did $(MKDIR)'s?
09/18 02:51:38 hiyuh
jmb:
http://dev.gentoo.gr.jp/~hiyuh/misc/libparserutils-more-DESTDIR.patch
09/18 02:55:21 jmb
whoops. I guess I must've trampled those when I did the fix for
installing on OS X
09/18 02:56:32 hiyuh
:)
09/18 03:21:28 hiyuh
omg
09/18 03:23:00 hiyuh
jmb: hubbub's make test still fails at html/mangleme.1.html of
Treebuilding API.
09/18 03:25:31 jmb
yes
09/18 03:25:34 jmb
that's a known bug
09/18 03:27:21 hiyuh
there is no way to skip this test? or it's last one?
09/18 03:28:36 jmb
no, there isn't
09/18 03:30:08 hiyuh
ack
つー訳でnet-libs/hubbubの一歩手前で"たいむあっぷ"、続きはまた来週(?)な!!1 :DDD
[Sadly, I couldn't commit net-libs/hubbub. but I'll be back soonish!!1 :DDD]

2008/09/14

TLUG.NomiKai++; /* ModSecurity/Detaxe/ss */

二度目のTLUG、今回はModSecurityとDetaxeとscalable storageだった。会場は天下(?)のSun、用賀を見下ろせる27Fのミーティングルーム、エアコンがバリバリで蝶快適。自動販売機にSunのロゴが入っていて、通常の値段よりも30円とか安いのな。荷物搬入のエレベータからしか会場に行けなくて、ちょっと迷ってしまったがな!!1 :P
[It's second time to join TLUG meeting for me. This time is about ModSecurity, Detaxe and OSS scalable storage. T3h location is SUN Microsystems @ YoGa, 27th floor w/ coolest A/C, it's good. T3h automat has Sun logo is, discounts all beverage 30 JPY, it's good. To reach t3h 27th floor, we have to use a lift that looks like mainly for carry big WS in, it's not good. :P]

前回に比べるとプレゼンしてた人が非ネイティブだからか、単にゆっくり喋るのを留意してたのか、何気に聞き取れた気がする。以下、蝶意訳。
「バグってるブツをどうにかする方法はやっぱりバグを直すことだけど、ソースが無かったり、パッチ当てると余計ぶっ壊れたりしてメンドイから防火壁埋め込んで誤魔化そうぜ。だって、ぶっ壊れてるのが分かると俺がボスに怒られるんだもん!!1」
「値段の分からないOEMなソフトウェアなんか要らねーよ、3割も原価喰ってるみたいだし返品しようぜ。オプショナルなら買うかもしれんけどな!!1」
「cman、CLVM、GNBD、GFS2、DRBD、DM-MPにタダ乗りして俺は幸せになる予定だ!!1」
[All of this time's presentation were done by non-native peeps, or they might keep thier mind to speek slowly, it hepls to me to understand what they'd say more than the prev's. So, something like that, "The best way to beat vulnabilities is fixing as bugs though. In case fo w/o source code or proposed patch breaks our system, it's not fun. So just work around by using this FW. B/c everytime this crap goes wrong, boss blames me!!1", "We don't need parasitic OEM software which has unknown price. About 30% of our payment is for these craps, so refund these undesirable tax. OPTIONAL is definitely FTW!!1", "cman, CLVM, GNBD, GFS2, DRBD and DM-MP will make me happy to do OSS free riding!!1"]

一次会はわたみん家@用賀。に行く途中で、何故かSunのjimさんと名刺交換。
matsuuさんが「割り勘負けしない様に喰いまくれ!!1」と言っていたので、テキトーに喰いまくって腹一杯になると、前と同じ様に、ネイティブな方々がペラペラしゃべっている横でヒアリングの真似をするが、相変わらず分からんかった。何かもうね、単語の切れ目とか全然分からんのですけど気のせいですね、そうですね。とか思っていたら、trombikセンセーが来て、「どうよ、俺のプレゼン?」とか仰ったので、自分がネイティブな英語が聞き取れない事を棚に挙げて「話、長ーい」とか「数字を日本語だー」とか茶化した。 :P
[First NomiKai, WataminChi@Yoga, while we were going to, I got jim's name card!!1 :)
matsuu sez "eat, just eat to earn back my 3000 JPY!!1", we're stuffed. Then, we did hearing English who are native speakers sat at our side. O.K. I don't understand what they're talking about, well, they're really speaking English? It's really hard to me to recognize any gap between word-to-word. Then trombik came and asked, "how about mine?". Even if my English skill sucks, but there is no problem to say like "TOO LONG!!1" or "T3H NUMBERS ARE STILL JAPANESE!!1".]

その後、プレゼンの中休みにsmoking roomで「VHDLでメシ喰ってるけど、OpenSPARCはVerilogだから分かんね」とか言ってたのを覚えててくれたSunのshojiさんが来て、まったく関係無いSunの内部情報をリークしてくれた。曰く、「日本語が流暢でない外国人は、自分の思っている事がキチンと相手に伝わらない事にストレスを感じてるのでマネジメントすんの大変なんよ」とか「同僚の外国人と食事に行く事と飯を喰っている時は横文字で話すから、これをヨコメシと言う」とか。おー、何と言う貴重な情報。 :DDD
[After that, I met shoji at smoking room at Sun and mumbled "I prefer VHDL, but OpenSPARC is Verilog", so he came to talk us again, thanks. And he leaks awesome Sun's confidentials like "it's reallly hard to management foreigners in a office live in Japan but who can not speak Japanese very well b/c they're frustrated always, some Japanese doesn't understand completely what they'd saying." or "To lunch w/ foreigners. We, Japanese should speak English (was written on a papar directed left to right, in Japanse, it's YOKO direction) in that time. So it's called YokoMeshi (Meshi means lunch, of cource)." OMG, Sun rock0rz!!1 :DDD]

二次会は店名のフォントが崩れすぎていて読めない"あいりっしゅ"なバー@渋谷。
ちびちびやりながら、いつものtrombikセンセーの日本に対する不満を拝聴する。
以下、その模様を演出を多分に含んだ四行で表現。
「手前ら、学校で英語ならったろ、話せよ」
「ここは日本じゃ、嫌なら外国へでも高跳びしなはれ」
「もうね、この問題と戦わないといけないんよ」
「"この問題と戦わないといけないんよ"という問題と戦わないとな!!1」
とても良い漫才でした、ほんとうにありがとうございました。 :DDD
[Second NomiKai, Irish bar @ ShibuYa, its name can not be read for me b/c strongly-distorted fonts are used. It's time to listen t3h trombik's bitching Japanese like that.
"n00b, you must studied English in your school days, so explain in English!!1", "Here is Japan. There is no problem to speak Japanese. If you don't like Japan, go other countries!!1", "fsck, I'm facing those stupid problem!!1", "Nah, I'm also facing this stupid problem, you!!1"
It's nice ManZai. KTHX. :DDD]

他にも色々話したけど、とりあえず、正しい日本文化を広める為に「DEATH NOTEが好き」と言ってたオランダな人に「銀魂とJOJOがオススメ」とか言っといた。 :P
[To correct Japanese calture, we recommend GinTama and JOJO to a fun of DEATH NOTE, who comes from Netherland. It's for great justice. :P]