Problem one Mux256to1
Circuit→Combinational logic→Multiplexer
遇到问题
该题目如果按照 2-to-1和9-to-1这样的思路的话,写一个由256个case的语句就会很累赘。
解答与分析
module top_module (
input [255:0] in,
input [7:0] sel,
output out
);
// Select one bit from vector in[]. The bit being selected can be variable.
assign out = in[sel];
//assign out = in >> sel;
endmodule
这里我们可以根据题目的要求,结合选择运算符的特性实现。根据提示:选择运算符的 index 可以为变量,只要变量的位宽和向量的长度匹配即可。
所以我们直接将 sel ,这个变量,作为片选向量 in 的 index。
根据题意,也可以将输入向量 in 右移 sel 位,高位被截去,输出最低位上的 in[sel]。同样,移位的长度也可以使用变量。
Problem two Mux256to1v
Circuit→Combinational logic→Multiplexer
题干
Create a 4-bit wide, 256-to-1 multiplexer. The 256 4-bit inputs are all packed into a single 1024-bit input vector. sel=0 should select bits in[3:0], sel=1 selects bits in[7:4], sel=2 selects bitsin[11:8], etc.
遇到问题
module top_module(
input [1023:0] in,
input [7:0] sel,
output [3:0] out );
//错误代码1
assign out[3:0] = in[(sel*4)+3 : sel*4];
//错误代码2
/* always @(*)begin
for(int sel = 0;sel<256;sel++)begin
out[3:0] = in[(sel*4)+3 : sel*4];
end
end*/
//正确代码
assign out = {
in[sel*4+3], in[sel*4+2], in[sel*4+1], in[sel*4+0]};
endmodule