CPLD中可读可写寄存器的设计

一、需求分析:

      在CLPD中加入可读可写寄存器,通过操作读使能、写使能、地址和数据总线实现。

二、设计实现:

     (1) verilog实现代码如下:

//filename=test.v;
//author=shin;
//date=20191220;

module test(clk,rst_n,re,we,addr,data);
	input clk;
	input rst_n;
	input re;
	input we;
	input [7:0] addr;
	inout [7:0] data;
	
	reg [7:0] r_0x88;
	reg [7:0] r_0x80;
	
	reg [7:0] r_addr;
	reg [7:0] r_data;
	
	reg r_re,r_we;
	reg r0,r1,r2,r3;
	
	always@(posedge clk or negedge rst_n) begin	
		if(!rst_n) begin
			r0<=1'b0;
			r1<=1'b0;
			r_re<=1'b0;
		end
		else begin
			r0<=re;
			r1<=r0;
			r_re<=(!r1)&&(r0);
		end
	end
	always@(posedge clk or negedge rst_n) begin	
		if(!rst_n) begin
			r2<=1'b0;
			r3<=1'b0;
			r_we<=1'b0;
		end
		else begin
			r2<=we;
			r3<=r2;
			r_we<=(!r3)&&(r2);
		end
	end
	
	
	always@(posedge clk or negedge rst_n) begin
		if(!rst_n) begin
			r_0x88<=8'h00;
			r_0x80<=8'h00;
		end
		else begin
			if(r_we) begin
				case(addr)
					8'h80: r_0x80<=data;
					8'h88: r_0x88<=data;
					
					
					//......
					
					default:;
					
				endcase		
			end
		end
	end

	always@(posedge clk or negedge rst_n) begin
			if(~rst_n)
				begin
					r_data<=8'h00;
				end
			else
				begin
					if(r_re) begin
						case(addr)
							8'h80:	r_data<=r_0x80;
							8'h88:	r_data<=r_0x88;
							
							// ......
							default: ;
						endcase

				end
		end
	end
	assign data=(r_re)?r_data:8'bzzzz_zzzz;
	
endmodule

	

      (2)testbench实现代码如下:

//filename=test_tb.v;
//author=shin;
//date=20191220;

`timescale 1ns/1ps

module test_tb();

	reg clk;
	reg re,we;
	reg rst_n;
	reg [7:0] addr;
	wire [7:0] data;
	reg [7:0] bi_data_reg;

	
	
	test test_ins0(clk,rst_n,re,we,addr,data);
	
	initial begin
		clk=1'b0;
		forever
			#0.5 clk=~clk;
	end
	initial begin
		#800 $finish;
	end
	initial begin
        $dumpfile("test.vcd");  //这两行主要是给gtkwave这个工具使用的...
        $dumpvars(0,test_tb);
	end
	initial begin
		rst_n=1'b0;
		#10 rst_n=1'b1;
	end
	
	initial begin
		rst_n=1'b0;
		#10 rst_n=1'b1;
	end
	
	initial begin
		we=1'b0;	//初始时we电平要确定,不然testbench仿真结果中寄存器值未知
		#100 we=1'b1;
		#10 we=1'b0;
	end
	
	initial begin
		re=1'b0;	//初始时we电平要确定,不然testbench仿真结果中寄存器值未知
		#38 re=1'b1;
		#18 re=1'b0;
		#160 re=1'b1;
		#10 re=1'b0;
		#188 re=1'b1;
		#38 re=1'b0;
	end
	
	initial begin
		#100 addr=8'h88;
		#200 addr=8'h80;
	end
	
	initial begin
		#100 bi_data_reg=8'hce;
		#10 bi_data_reg=8'hzz;
	end
	
	assign data=we?bi_data_reg:8'hzz;
	
endmodule



      (3)综合后的电路图如下:

图1. 综合后电路图

三、仿真验证

   test文件通过test_tb仿真后的波形图如下所示:

图1. 0x88寄存器写入0xce

图2. 读取寄存器0x88

图3. 读取寄存器0x80

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值