⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.v

📁 16位的RISC_CPU
💻 V
字号:
//------------------------------------------- test.v 文件的开始 -----------------------------------------------------/**************************************************************************  模块功能:test模块通过运行5个不同的汇编程序对cpu模块进行完整的逻辑测试***            和验证。仿真程序在加栽后会自动执行,运行的结果在人机交互的显示***            屏上显示。本模块是不可综合的行为模块,用于对CPU模块进行RTL级和***            门级逻辑功能的全面仿真验证。 ************************************************************************/`include "cpu.v"`include "rom.v"`include "ram.v"`include "decoder.v"`timescale 1ns/1nsmodule test;  reg         clk;  reg         reset_n;  wire        wr;  wire        rd;  wire        halt;  wire [12:0] addr;  wire [15:0] rddata;  wire        rom1_sel;  wire        ram1_sel;  wire [15:0] rom1_data;  wire [15:0] ram1_data;  reg  [23:0] mnemonic;  reg  [12:0] ip_status;  wire  [1:0] cpu_status;  integer   test;  integer   i;  parameter S0=2'd0, S1=2'd1, S2=2'd2, S3=2'd3;    assign cpu_status=cpu1.state;  always #50 clk=~clk;    always@(cpu1.opcode)  	case(cpu1.opcode)  		3'd0:  mnemonic<="HLT";  		3'd1:  mnemonic<="SKZ";  		3'd2:  mnemonic<="ADD"; 		3'd3:  mnemonic<="AND"; 		3'd4:  mnemonic<="XOR";  		3'd5:  mnemonic<="LDA";  		3'd6:  mnemonic<="STO";  		3'd7:  mnemonic<="JMP"; 		default:  mnemonic<=3'dx; 		endcase    always@(posedge halt)  	begin  		#250;  		$display("\n**********************************************");  		$display("**A halt instruction has been processed!!!");  		$display("************************************************");  	end					  	  always@(posedge clk)  	begin: MONITOR  	case(test)  		1:  begin  		     		      		         case(cpu1.state)  			      S0:  			       ip_status<=addr[12:0];  			      S1:  			      			        $strobe("%t        %s        %h        %h           %h ",  			    	      $time, mnemonic, ip_status,addr,rddata);  	            endcase  	        end  		      		2: begin	                                               case(cpu1.state)  			          S0:  			          ip_status<=addr[12:0];  			          S1:  			          $strobe("%t        %s        %h        %h        %h",  			    	      $time, mnemonic, ip_status,addr,rddata);  		            endcase  		   end                              		3:begin  		   $display("\n***   RUNNING CPUtest3 - An Executable Program   ***");                     $display("*** This program should calculate the fibonacci  ***");                   $display("\n    TIME      FIBONACCI NUMBER");                   $display(  "  ---------   -----------------");                    while (test == 3)                      begin                        wait (cpu1.opcode==3'b001)    // display Fib. No. at end of program loop                        $strobe("%t     %d", $time,ram1.ram[10'h2]);                        wait ( cpu1.opcode != 3'b001);                      end        end         		4:begin  		   $display("\n***   RUNNING CPUtest4 - An Executable Program   ***");                     $display("*** This program should calculate the N  N2   SUM  ***");                   $display("\n       TIME         N+1       N2       SUM ");                   $display(  "     ---------      ---      ----     -----");                    while (test == 4)                      begin                         wait  ({cpu1.opcode,cpu1.opaddr} == 16'b100_11000_0000_0110)                          $strobe("%t     %d     %d      %d", $time,ram1.ram[10'h2],ram1.ram[10'h3],ram1.ram[10'h1]);                        wait ({cpu1.opcode,cpu1.opaddr} != 16'b100_11000_0000_0110);                      end         end         		    		  		  	endcase	    end				    initial    begin    	clk=1;    	mnemonic=0;    	ip_status=0;  	    	$timeformat(-9,1,"ns",12);    	display_debug_message;    	sys_reset;    	test1;    	$stop;    	test2;    	$stop;    	test3;    	$stop;    	test4;    	$stop;    	test5;    	$stop;    end	    task display_debug_message;    begin    	$display("\n*************************************************");    	$display("**The following debug task are avilable:**");    	$display("* \"test1; \" to load the 1st diagnostic program. *");        $display("*  \"test2; \" to load the 2nd diagnostic program. *");        $display("*  \"test3; \" to load the Fibonacci program.      *");        $display("*  \"test4; \" to load the 自然数平方和 program.      *");        $display("*  \"test5; \" to load the 加解密 program.      *");        $display("*****************************************************\n");    end  endtask    task test1;    begin      test = 0;      disable MONITOR;      $readmemb("test1.pro", rom1.memory);      $display("Rom loading successfully!");      $readmemb("test1.dat", ram1.ram);      $display("Ram loading successfully!");                    $display("\n*** RUNNING CPUtest1 - The Basic CPU Diagnostic Program ***");                    $display("\n     TIME         INSTR       PC      ADDR         DATA  ");                    $display("    ----------      ----     -----     -----       ----- ");      #20;      test = 1;      #7400;      sys_reset;    end  endtask    task test2;    begin      test = 0;      disable MONITOR;      $readmemb("test2.pro", rom1.memory);      $display("Rom loading successfully!");      $readmemb("test2.dat", ram1.ram);      $display("Ram loading successfully!");                   $display("\n*** RUNNING CPUtest2 - The Advanced CPU Diagnostic Program ***");                   $display("\n     TIME       INSTR        PC         ADDR     DATA  ");                   $display("   ----------      ---        -----       -----    ---- ");      //#1;      test = 2;      #5800;      sys_reset;    end  endtask    task test3;    begin      test = 0;      disable MONITOR;      $readmemb("test3.pro", rom1.memory);      $display("Rom loading successfully!");      $readmemb("test3.dat", ram1.ram);      $display("Ram loading successfully!");      //#1;      test = 3;      #76000;      sys_reset;    end  endtask   task test4;    begin      test = 0;      disable MONITOR;      $readmemb("test4.pro", rom1.memory);      $display("Rom loading successfully!");      $readmemb("test4.dat", ram1.ram);      $display("Ram loading successfully!");      //#1;      test = 4;      #4238000;            sys_reset;    end  endtask    task test5;    begin      test = 0;      disable MONITOR;      $readmemb("test5.pro", rom1.memory);      $display("Rom loading successfully!");      $readmemb("test5.dat", ram1.ram);      $display("Ram loading successfully!");      //#1;      test = 5;      #3800000;      $display("\n***   RUNNING CPUtest5 - An Executable Program   ***");        $display("*** 这个程序将一块随机数据加密后再解密  计算完毕统一显示如下:  ***");      $display("        No           原始数据         加密数据          解密数据   ");      $display("-------------------------------------------------------------------");        for (i=0;i<32;i=i+1)      $display("     %d          %d              %d             %d   ",      i+1,ram1.ram[16'h60+i],ram1.ram[16'h80+i],ram1.ram[16'ha0+i]);      sys_reset;    end  endtask    task sys_reset;    begin      reset_n = 1;      #70;      reset_n = 0;       #150;      reset_n = 1;      end  endtask     CPU cpu1(.clk(clk),          .reset_n(reset_n),          .rddata(rddata),          .wr(wr),          .rd(rd),          .halt(halt),          .addr(addr)          );    decoder decoder1(.addr(addr),                  .rom_sel(rom1_sel),                  .ram_sel(ram1_sel)                  );                    rom rom1( .ena(rom1_sel),	         .read(rd),	         .addr(addr[10:0]),	         .data(rddata[15:0])	         );    ram ram1( .ena(ram1_sel),	         .write(wr),	         .read(rd),	         .addr(addr[9:0]),	         .data(rddata[15:0])	          );endmodule

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -