December 23, 2011

Demo 4 bits full adder and 4 bits multiplier by Verilog

◎ 4 bits full adder


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
method_1: step by step

module add_4_v (
    input  [3:0] a,
    input  [3:0] b,
    input        cin,
    output [3:0] s,
    output       co
);

wire [2:0] carry;

function fa_s(input a, input b, input cin); 
    fa_s = a ^ b ^ cin;
endfunction

function fa_co(input a, input b, input cin);
    fa_co = a & ci | a & b | b & cin;
endfunction

assign s[0]     = fa_s (a[0], b[0], cin);
assign carry[0] = fa_co(a[0], b[0], cin);

assign s[1]     = fa_s (a[1], b[1], carry[0]);
assign carry[1] = fa_co(a[1], b[1], carry[0]);

assign s[2]     = fa_s (a[2], b[2], carry[1]);
assign carry[2] = fa_co(a[2], b[2], carry[1]);

assign s[3]     = fa_s (a[3], b[3], carry[2]);
assign co       = fa_co(a[3], b[3], carry[2]);

endmodule



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
method_2: using Mega-function "lpm_add_sub"

module add_4_v (
    input  [3:0] a,
    input  [3:0] b,
    input        cin,
    output [3:0] s,
    output       co
);

lpm_add_sub # (.lpm_width(4))
u0 (
    .dataa(a);
    .datab(b);
    .result(s);
    .cout(co);
);

endmodule


◎ 4 bits multiplier


1
2
3
4
5
6
7
8
9
10
11
12
13
14
module m(a, b, y);

input [3:0] a, b;
output [7:0] s;
wire [3:0] to, t1, t2, t3;

assign to = (b[0]==1)? a : 4'h0;
assign t1 = (b[1]==1)? a : 4'h0;
assign t2 = (b[2]==1)? a : 4'h0;
assign t3 = (b[3]==1)? a : 4'h0;

assign s = t0+(t1<<1)+(t2<<2)+(t3<<3);

endmodule

December 2, 2011

array of pointer & pointer to array

int (*p)[10];  // This type is a "pointer to array" with 10 elements.
int *p[10];    // This type is an "array of pointer" with 10 elements.

Practice: write down how many bytes the following statements need

int **P;                        // 4 bytes
int *p[];                        // 4 * sizeof array bytes
int (*p)[10];                 // 4 bytes
int *p[10];                    // 40 bytes
int (*p)[20][30];           // 4 bytes

三個逗號俱樂部

《免責聲明》 本部落格不針對任何金融商品進行買賣建議, 內容來自公開資訊觀測站之分享與各大媒體之評論為主, 投資人應審慎評估並獨立判斷,切勿以本部落格資訊作為投資依據。 靜候 時機來臨;瞬間掌握重壓;享受 獲利奔馳。 -------------------------...