并行输入串行输出

利用有限状态机将并行输入的数据转换为串行输出,初学veirlog,如有错误,敬请指出。

1.RTL代码

module par2ser(in,
	       en,
	       clk,
	       rst,
	       ack,
       	   out,
       	   valid);

input in;
input clk;
input rst;
input en;
output out;
output valid;//when output signal is becoming valid, valid = 1
output ack;// ask for new info

wire[3:0] in;
reg out;
reg[3:0] curr_state;
reg[3:0] next_state;

parameter IDLE = 3'b000,
	  BIT1 = 3'b001,
	  BIT2 = 3'b011,
	  BIT3 = 3'b010,
	  BIT4 = 3'b110,
	  FINISH = 3'b111,//finish recieving info
	  READY = 3'b101;//ready for recieve info

always@(posedge clk or negedge rst) begin 

	if(~rst)
		curr_state <= IDLE;
	else
		curr_state <= next_state;

end

always@(*) begin

	case(curr_state)
	IDLE:begin
		if(en)
			next_state = READY;
		else
			next_state = IDLE;
	end
	READY:
		next_state = BIT1;
	BIT1:
		next_state = BIT2;
	BIT2:
		next_state = BIT3;
	BIT3:
		next_state = BIT4;
	BIT4:
		next_state = FINISH;
	FINISH:
		next_state = IDLE;

	default:next_state = IDLE;
	endcase
end

always@(posedge clk or negedge rst) begin

	if(~rst)
		out <= 0;
	else begin
		case(curr_state)
			BIT1:
				out <= in[0];
			BIT2:
				out <= in[1];
			BIT3:
				out <= in[2];
			BIT4:
				out <= in[3];
			default:out <= 0;
		endcase
	end
end

assign ack = (curr_state == FINISH);
assign valid = (curr_state == BIT1);

endmodule

2.testbench

module par2sertb;

reg clk;
reg rst;
reg en;
reg[3:0]in;
wire out;
wire ack;
wire valid;

initial begin
	
	`ifdef VCD 
		$vcdpluson;
	`endif
	rst = 0;
	en = 0;
	in = 0;
	#100 rst = 1;
	#100 en = 1;
	#10000 $finish;
end

initial begin
	
	clk = 0;
	forever #10 clk = ~clk;
end

always@(negedge ack) begin

	in = {$random} % 15;

end

par2ser u(.clk(clk),.rst(rst),.en(en),.out(out),.in(in),.ack(ack),.valid(valid));

endmodule

3.波形
波形

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值