Problem18
2.2.9 More replication(Vector5)
现在要求你构建一个电路,将一个 8bit 有符号数扩展为 32bit 数。
out[24] = ~a ^ a; // a == a, so out[24] is always 1.
out[23] = ~a ^ b;
out[22] = ~a ^ c;
...
out[ 1] = ~e ^ d;
out[ 0] = ~e ^ e;
根据上图,这题使用位连接符和重复操作符是再舒服不过了。
module top_module (
input a, b, c, d, e,
output [24:0] out );//
assign out = {{5{a}}, {5{b}}, {5{c}}, {5{d}}, {5{e}}} ~^ {5{a, b, c, d, e}};
// second way
/*
wire [24:0] in1;
wire [24:0] in2;
assign in1 = {{5{a}}, {5{b}}, {5{c}}, {5{d}}, {5{e}}};
assign in2 = {5{a, b, c, d, e}};
assign out = in1 ~^ in2;
*/
//third way
/*
assign out = {{a ~^ a}, {a ~^ b}, {a ~^ c}, {a ~^ d}, {a ~^ e},
{b ~^ a}, {b ~^ b}, {b ~^ c}, {b ~^ d}, {b ~^ e},
{c ~^ a}, {c ~^ b}, {c ~^ c}, {c ~^ d}, {c ~^ e},
{d ~^ a}, {d ~^ b}, {d ~^ c}, {d ~^ d}, {d ~^ e},
{e ~^ a}, {e ~^ b}, {e ~^ c}, {e ~^ d}, {e ~^ e}};
*/
endmodule
本道题一开始想用循环语句做来着,但是不知道为什么一直报错,最后找了一下参考答案。
本人的循环语句实现方法如下,如果有那位同学会的话希望指点一下:
module top_module (
input a, b, c, d, e,
output [24:0] out );//
// The output is XNOR of two vectors created by
// concatenating and replicating the five inputs.
// assign out = ~{ ... } ^ { ... };
assign in[4:0] = {{a},{b},{c},{d},{e}};
genvar i,j;
generate
for(j = 20,i = 4;j => 0,i => 0;j - 5,i - 1)
begin: xxx
//assign i = 4;
assign out[j] = a ~^ in[i];
assign out[j+1] = b ~^ in[i] ;
assign out[j+2] = c ~^ in[i];
assign out[j+3] = d ~^ in[i];
assign out[j+4] = e ~^ in[i];
//assign i = i - 1;
end
endgenerate
endmodule
Problem 19 : Modules
模块信号连接的两种方式
在实例化模块时,有两种常用的方式来进行模块端口的信号连接:按端口顺序以及按端口名称连接端口。
按端口顺序,
mod_a instance1 ( wa, wb, wc );
wa, wb, wc 分别连接到模块的 第一个端口(in1),第二个端口(in2)以及第三个端口(out)。这里所谓的端口顺序指的是模块端口的定义顺序。这种方式的弊端在于,一旦端口列表发生改变,所有模块实例化中的端口连接都需要改变。按端口名称,
mod_a instance2 ( .out(wc), .in1(wa), .in2(wb) );
在这种方式中根据端口名称指定外部信号的连接。这样一来就和端口声明的顺序完全没有关系。一旦模块出现改动,只要修改相应的部分即可。实际上,一般都使用这种方式来进行模块实例化。
Problem 20: Connecting ports by position(Module pos)
mod_a name(out1,out2,a,b,c,d);
Problem 21: Connecting ports by name(Module name)
mod_a name(
.out1(out1),
.out2(out2),
.in1(a),
.in2(b),
.in3(c),
.in4(d));
problem20、21的内容对应着(笔记中2:50两种引用表示)
Problem 23: Modules and vectors(Module shift8)
在进行引用时,上面有些线没有被定义出来,定义时定义:
wire定义时,可以不加位宽.
module top_module ( input clk, input d, output q );
wire a;
wire b;
my_dff(.d(d),.clk(clk),.q(a));
my_dff(.d(a),.clk(clk),.q(b));
my_dff(.d(b),.clk(clk),.q(q));
endmodule