HDLBits

文章提供了三个Verilog模块的实现,包括一个100位BCDRipple-carry加法器,它由100个4位BCD加法器组成,以及一个检查输入向量相邻位关系的门电路,最后是一个256到1的4位宽多路复用器,根据选择信号从大输入向量中选择数据。

题目__Bcdadd100

You are provided with a BCD one-digit adder named bcd_fadd that adds two BCD digits and carry-in, and produces a sum and carry-out.

module bcd_fadd (
    input [3:0] a,
    input [3:0] b,
    input     cin,
    output   cout,
    output [3:0] sum );

Instantiate 100 copies of bcd_fadd to create a 100-digit BCD ripple-carry adder. Your adder should add two 100-digit BCD numbers (packed into 400-bit vectors) and a carry-in to produce a 100-digit sum and carry out.

module top_module( 
    input [399:0] a, b,
    input cin,
    output cout,
    output [399:0] sum );
    
   
    wire [99:0] cout_t;
    bcd_fadd bcd_0(.a(a[3:0]),.b(b[3:0]),.cin(cin),.cout(cout_t[1]),.sum(sum[3:0]));
    generate 
        genvar i;
        for(i=2;i<100;i++)
            begin:bcd_fadd_1
                bcd_fadd bcd(
                    		 .a        (a[(4*i-1):4*(i-1)]),
                             .b        (b[(4*i-1):4*(i-1)]),
                             .cin      (cout_t[i-1]),
                             .cout     (cout_t[i]),
                             .sum      (sum[(4*i-1):4*(i-1)]));			  
            end
           
    endgenerate
    bcd_fadd bcd_99(.a(a[399:396]),.b(b[399:396]),.cin(cout_t[99]),.cout(cout),.sum(sum[399:396]));
    

endmodule

题目__Gatesv100

See also the shorter version: Gates and vectors.

You are given a 100-bit input vector in[99:0]. We want to know some relationships between each bit and its neighbour:

  • out_both: Each bit of this output vector should indicate whether both the corresponding input bit and its neighbour to the left are '1'. For example, out_both[98] should indicate if in[98] and in[99] are both 1. Since in[99] has no neighbour to the left, the answer is obvious so we don't need to know out_both[99].
  • out_any: Each bit of this output vector should indicate whether any of the corresponding input bit and its neighbour to the right are '1'. For example, out_any[2] should indicate if either in[2] or in[1] are 1. Since in[0] has no neighbour to the right, the answer is obvious so we don't need to know out_any[0].
  • out_different: Each bit of this output vector should indicate whether the corresponding input bit is different from its neighbour to the left. For example, out_different[98] should indicate if in[98] is different from in[99]. For this part, treat the vector as wrapping around, so in[99]'sneighbour to the left is in[0].
module top_module( 
    input [99:0] in,
    output [98:0] out_both,
    output [99:1] out_any,
    output [99:0] out_different );
	assign out_both=in[99:1]&in[98:0];
    assign out_any=in[99:1]|in[98:0];
    
    assign out_different=in^{in[0],in[99:1]};
endmodule

题目__Mux256to1v

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 bits in[11:8], etc.

 

module top_module( 
    input [1023:0] in,
    input [7:0] sel,
    output [3:0] out );
    
    assign out={in[4*sel+3],in[4*sel+2],in[4*sel+1],in[4*sel]};
   
endmodule

HDLBits答案 

module top_module (
	input [1023:0] in,
	input [7:0] sel,
	output [3:0] out
);

	
	assign out = {in[sel*4+3], in[sel*4+2], in[sel*4+1], in[sel*4+0]};

	
	// assign out = in[sel*4 +: 4];		// Select starting at index "sel*4", then select a total width of 4 bits with increasing (+:) index number.
	// assign out = in[sel*4+3 -: 4];	// Select starting at index "sel*4+3", then select a total width of 4 bits with decreasing (-:) index number.
	// Note: The width (4 in this case) must be constant.

endmodule

 

08-10
HDLBits平台上,Verilog设计的核心内容之一是有限状态机(FSM)的设计。状态机设计通常采用三段式结构,即当前状态寄存、下一状态逻辑和输出逻辑,这种设计方式使得状态机的层次更加清晰,易于维护和扩展。在实际练习中,例如Lemmings系列题目,通过不同难度的状态机设计,可以逐步掌握状态转移逻辑、组合逻辑和时序逻辑的设计要点[^1]。 ### 有限状态机设计要点 1. **三段式结构**: - **当前状态寄存器**:用于存储当前状态,通常使用`reg`类型变量。 - **下一状态逻辑**:根据当前状态和输入信号决定下一状态,通常使用`case`语句实现。 - **输出逻辑**:根据当前状态或输入信号产生输出,可以是组合逻辑或同步逻辑。 2. **参数化状态定义**: - 使用`parameter`定义状态,例如`parameter A=2'd0, B=2'd1, C=2'd2, D=2'd3;`,这样可以提高代码的可读性和可维护性。 3. **组合逻辑与时序逻辑分离**: - **组合逻辑**:通常使用`always@(*)`块处理状态转移逻辑。 - **时序逻辑**:使用`always@(posedge clk)`块处理状态更新。 ### 示例:状态机设计 以下是一个简单的状态机示例,根据输入信号`in`和当前状态`state`决定下一状态`next_state`,并根据状态决定输出`out`: ```verilog module top_module( input in, input [1:0] state, output reg [1:0] next_state, output out ); parameter A = 2'd0, B = 2'd1, C = 2'd2, D = 2'd3; assign out = (state == D) ? 1'b1 : 1'b0; // 高速电路输出 always @(*) // 组合逻辑电路 确定下次状态 begin case(state) A: begin if (in) next_state = B; else next_state = A; end B: begin if (in) next_state = B; else next_state = C; end C: begin if (in) next_state = D; else next_state = A; end D: begin if (in) next_state = B; else next_state = C; end default: next_state = A; endcase end endmodule ``` ### 示例:多路复用器设计 HDLBits中的另一个常见练习是多路复用器设计。以下是一个简单的256选1多路复用器示例,使用`sel`作为选择信号,从输入`in`中选择一个比特输出: ```verilog module top_module( input [255:0] in, input [7:0] sel, output out ); assign out = in[sel]; // 直接使用 sel 作为索引 endmodule ``` ### 示例:半加器设计 半加器是一种基本的组合逻辑电路,用于计算两个比特的和与进位。以下是半加器的Verilog实现: ```verilog module half_adder( input a, input b, output sum, output carry ); assign sum = a ^ b; // 异或运算得到和 assign carry = a & b; // 与运算得到进位 endmodule ``` ### 相关问题 1. 有限状态机设计中,三段式结构的具体实现方式是什么? 2. 如何在HDLBits上实现一个简单的多路复用器? 3. 半加器的基本原理及其Verilog实现方法是什么? 4. 状态机设计中,如何使用参数化方式定义状态? 5. 在HDLBits练习中,如何处理状态机的组合逻辑与时序逻辑分离?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值