每日一题-Module shift8

This exercise is an extension of module_shift. Instead of module ports being only single pins, we now have modules with vectors as ports, to which you will attach wire vectors instead of plain wires. Like everywhere else in Verilog, the vector length of the port does not have to match the wire connecting to it, but this will cause zero-padding or trucation of the vector. This exercise does not use connections with mismatched vector lengths.

You are given a module my_dff8 with two inputs and one output (that implements a set of 8 D flip-flops). Instantiate three of them, then chain them together to make a 8-bit wide shift register of length 3. In addition, create a 4-to-1 multiplexer (not provided) that chooses what to output depending on sel[1:0]: The value at the input d, after the first, after the second, or after the third D flip-flop. (Essentially, sel selects how many cycles to delay the input, from zero to three clock cycles.)

The module provided to you is: module my_dff8 ( input clk, input [7:0] d, output [7:0] q );

The multiplexer is not provided. One possible way to write one is inside an always block with a case statement inside. (See also: mux9to1v)

图1 顶层设计模块

英文的第二段是给我们说明任务目标:1.实例化3个my_dff8的模块 2. 将三个模块连接在一起 3. 用一个4选1复用器 4. 根据输入的sel选择输出什么

我个人觉得这四个任务目标中前两个,比较好实现,但是后面两个都是和复用器有关,而我接触FPGA的东西还是比较少,所以不太确定能否实现(或者说我可能写过类似的代码,但是并不知道我写过的哪些是复用器),但是在英文的第四段中对复用器进行了描述,对于一个复用器,一种可能的方法是在一个always块中编写case语句进行选择。

可能这个时候always会成为我们写这个程序的拦路虎,因为我们对这个always不太了解,是第一次见到相关描述,我用简单一点的语言描述一下这个always的用处

always @ (触发条件) begin
    执行语句
end

当always后面的括号中触发条件发生时,always就会自动执行begin到end的程序,对于begin和end可以看做我们编写C语言中的大括号的左边和右边,那么接下来就是把触发条件写在括号内部,在用case语句根据sel的值选择输出

module top_module ( 
	input clk, 
	input [7:0] d, 
	input [1:0] sel, 
	output [7:0] q 
);
	wire [7:0] d1;
	wire [7:0] d2;
	wire [7:0] d3;
	my_dff8 my_dff8_1(    //先实例化一个模块
	.clk(clk),
	.d(d),
	.q(d1)
	);
	my_dff8 my_dff8_2(			//在实例化一个
	.clk(clk),
	.d(d1),
	.q(d2)
	);
	my_dff8 my_dff8_3(			//在实例化一个
	.clk(clk),
	.d(d2),
	.q(d3)
	);
	/*现在只剩下那个4选1多路复用器,根据题目要求是在第一个或者第二个或第三个触发之后,根据sel[1:0]选择输出哪一个*/
	/*所以选择写一个always块,always@内部是触发条件,只有满足这些触发条件之后,always块内部的程序就会执行*/
	always@(*) begin        //always@(*)代表触发条件是自动添加
		case(sel)
			2'd0: q = d;
			2'd1: q = d1;
			2'd2: q = d2;
			2'd3: q = d3;
		endcase
	end
endmodule

 

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值