Verilog快速入门
(1) 四选一多路器
(2)异步复位的串联T触发器
(3)奇偶校验
(4)移位运算与乘法
(5)位拆分与运算
(6)使用子模块实现三输入数的大小比较
(7)4位数值比较器电路
(8)4bit超前进位加法器电路
(9)优先编码器电路①
(10)用优先编码器①实现键盘编码电路
(11)8线-3线优先编码器
(12)使用8线-3线优先编码器实现16线-4线优先编码器
(13)用3-8译码器实现全减器
(14)使用3-8译码器①实现逻辑函数
(15)数据选择器实现逻辑函数
(16)状态机
(17)ROM的简单实现
(18)边沿检测
移位运算与乘法
一、题目描述
已知d为一个8位数,请在每个时钟周期分别输出该数乘1/3/7/8,并输出一个信号通知此时刻输入的d有效(d给出的信号的上升沿表示写入有效)
信号示意图:
二、解析与代码
3倍=2倍+1倍 or 4-1
7倍=4倍+2倍+1倍 or 8-1
代码如下:
`timescale 1ns/1ns
module multi_sel(
input [7:0]d ,
input clk,
input rst,
output reg input_grant=0,
output reg [10:0]out=0
);
//*************code***********//
//计数器
reg [1:0]cnt=0;
always@(posedge clk)begin
if(rst)
if(cnt==2'd3)
cnt <= 0;
else
cnt <= cnt+1;
else
cnt <= 0;
end
reg [7:0]din=0;
always @(posedge clk)begin
if(rst)
case(cnt)
2'b00:begin
din <= d;
input_grant <= 1;
out[10:0] <= {3'b0,d};
end
2'b01:begin
input_grant <= 0;
out[10:0] <= {3'b0,din} + {2'b0,din[7:0],1'b0};
end
2'b10:begin
input_grant <= 0;
out[10:0] <= {3'b0,din} + {1'b0,din[7:0],2'b0} + {2'b0,din[7:0],1'b0};
end
2'b11:begin
input_grant <= 0;
out[10:0] <= {din[7:0],3'b0};
end
endcase
else begin
input_grant <= 0;
out[10:0] <= 0;
din <= 0;
end
end
//*************code***********//
endmodule