当前位置:   article > 正文

RISC_CPU模块的调试

RISC_CPU模块的调试

代码:

cpu.v

  1. `include "clk_gen.v"
  2. `include "accum.v"
  3. `include "adr.v"
  4. `include "alu.v"
  5. `include "machine.v"
  6. `include "counter.v"
  7. `include "machinectl.v"
  8. `include "register.v"
  9. `include "datactl.v"
  10. module cpu(clk,reset,halt,rd,wr,addr,data);
  11. input clk,reset;
  12. output rd,wr,addr,halt;
  13. inout data;
  14. wire clk,reset,halt;
  15. wire [7:0] data;
  16. wire [12:0] addr;
  17. wire rd,wr;
  18. wire clk1,fetch,alu_clk;
  19. wire [2:0] opcode;
  20. wire [12:0] ir_addr,pc_addr;
  21. wire [7:0] alu_out,accum;
  22. wire zero,inc_pc,load_acc,load_pc,load_ir,data_ena,contr_ena;
  23. clk_gen m_clk_gen (.clk(clk),.clk1(clk1),.fetch(fetch),
  24. .alu_clk(alu_clk),.reset(reset));
  25. register m_register (.data(data),.ena(load_ir),.rst(reset),
  26. .clk1(clk1),.opc_iraddr({opcode,ir_addr}));
  27. accum m_accum (.data(alu_out),.ena(load_acc),
  28. .clk1(clk1),.rst(reset),.accum(accum));
  29. alu m_alu (.data(data),.accum(accum),.alu_clk(alu_clk),
  30. .opcode(opcode),.alu_out(alu_out),.zero(zero));
  31. machinectl m_machinecl(.ena(contr_ena),.fetch(fetch),.rst(reset));
  32. machine m_machine (.inc_pc(inc_pc),.load_acc(load_acc),.load_pc(load_pc),
  33. .rd(rd), .wr(wr), .load_ir(load_ir), .clk1(clk1),
  34. .datactl_ena(data_ena), .halt(halt), .zero(zero),
  35. .ena(contr_ena),.opcode(opcode));
  36. datactl m_datactl (.in(alu_out),.data_ena(data_ena),.data(data));
  37. adr m_adr (.fetch(fetch),.ir_addr(ir_addr),.pc_addr(pc_addr),.addr(addr));
  38. counter m_counter (.ir_addr(ir_addr),.load(load_pc),.clock(inc_pc),
  39. .rst(reset),.pc_addr(pc_addr));
  40. endmodule
  41. //--------------------------------------- cpu.v ????? -------------------------------------------------

ram.v

  1. // --------------- RAM?ROM ----------------------------------------
  2. module ram( data, addr, ena, read, write );
  3. inout [7:0] data;
  4. input [9:0] addr;
  5. input ena;
  6. input read, write;
  7. reg [7:0] ram [10'h3ff:0];
  8. assign data = ( read && ena )? ram[addr] : 8'hzz;
  9. always @(posedge write)
  10. begin
  11. ram[addr]<=data;
  12. end
  13. endmodule

rom.v

  1. module rom( data, addr, read, ena );
  2. output [7:0] data;
  3. input [12:0] addr;
  4. input read, ena;
  5. reg [7:0] memory [13'h1fff:0];
  6. wire [7:0] data;
  7. assign data= ( read && ena )? memory[addr] : 8'bzzzzzzzz;
  8. endmodule

addr_decode.v

  1. //--------------?????----------------------
  2. module addr_decode( addr, rom_sel, ram_sel);
  3. output rom_sel, ram_sel;
  4. input [12:0] addr;
  5. reg rom_sel, ram_sel;
  6. always @( addr )
  7. begin
  8. casex(addr)
  9. 13'b1_1xxx_xxxx_xxxx:{rom_sel,ram_sel}<=2'b01;
  10. 13'b0_xxxx_xxxx_xxxx:{rom_sel,ram_sel}<=2'b10;
  11. 13'b1_0xxx_xxxx_xxxx:{rom_sel,ram_sel}<=2'b10;
  12. default:{rom_sel,ram_sel}<=2'b00;
  13. endcase
  14. end
  15. endmodule
  16. /* ????????????????ROM?RAM?
  17. FFFFH---1800H RAM
  18. 1800H---0000H ROM -----------------------------*/

cputop.v

  1. //------------------------------------------- cputop.v ????? -----------------------------------------------------
  2. /***********************************************************************
  3. *** ?????cputop ????????????????cpu???????????
  4. *** ???????????????????????????????
  5. *** ??????????????????????CPU????RTL??
  6. *** ??????????????
  7. ************************************************************************/
  8. `include "ram.v"
  9. `include "rom.v"
  10. `include "addr_decode.v"
  11. `include "cpu.v"
  12. `timescale 1ns / 100ps
  13. `define PERIOD 100 // matches clk_gen.v
  14. module cputop;
  15. reg reset_req,clock;
  16. integer test;
  17. reg [(3*8):0] mnemonic; //array that holds 3 8-bit ASCII characters
  18. reg [12:0] PC_addr,IR_addr;
  19. wire [7:0] data;
  20. wire [12:0] addr;
  21. wire rd,wr,halt,ram_sel,rom_sel;
  22. //------------------------ cpu ?????????ROM?RAM?????--------------------------------------
  23. cpu t_cpu (.clk(clock),.reset(reset_req),.halt(halt),.rd(rd),
  24. .wr(wr),.addr(addr),.data(data));
  25. ram t_ram (.addr(addr[9:0]),.read(rd),.write(wr),.ena(ram_sel),.data(data));
  26. rom t_rom (.addr(addr),.read(rd),.ena(rom_sel),.data(data));
  27. addr_decode t_addr_decode (.addr(addr),.ram_sel(ram_sel),.rom_sel(rom_sel));
  28. //--------------------cpu ?????????ROM?RAM???????----------------------------------
  29. initial
  30. begin
  31. clock=1;
  32. //display time in nanoseconds
  33. $timeformat ( -9, 1, " ns", 12);
  34. display_debug_message;
  35. sys_reset;
  36. test1;
  37. $stop;
  38. test2;
  39. $stop;
  40. test3;
  41. $stop;
  42. end
  43. task display_debug_message;
  44. begin
  45. $display("\n**************************************************");
  46. $display("* THE FOLLOWING DEBUG TASK ARE AVAILABLE: *");
  47. $display("* \"test1; \" to load the 1st diagnostic progran. *");
  48. $display("* \"test2; \" to load the 2nd diagnostic program. *");
  49. $display("* \"test3; \" to load the Fibonacci program. *");
  50. $display("*****************************************************\n");
  51. end
  52. endtask
  53. task test1;
  54. begin
  55. test = 0;
  56. disable MONITOR;
  57. $readmemb ("test1.pro", t_rom.memory);
  58. $display("rom loaded successfully!");
  59. $readmemb("test1.dat",t_ram.ram);
  60. $display("ram loaded successfully!");
  61. #1 test = 1;
  62. #14800 ;
  63. sys_reset;
  64. end
  65. endtask
  66. task test2;
  67. begin
  68. test = 0;
  69. disable MONITOR;
  70. $readmemb("test2.pro",t_rom.memory);
  71. $display("rom loaded successfully!");
  72. $readmemb("test2.dat",t_ram.ram);
  73. $display("ram loaded successfully!");
  74. #1 test = 2;
  75. #11600;
  76. sys_reset;
  77. end
  78. endtask
  79. task test3;
  80. begin
  81. test = 0;
  82. disable MONITOR;
  83. $readmemb("test3.pro",t_rom.memory);
  84. $display("rom loaded successfully!");
  85. $readmemb("test3.dat",t_ram.ram);
  86. $display("ram loaded successfully!");
  87. #1 test = 3;
  88. #94000;
  89. sys_reset;
  90. end
  91. endtask
  92. task sys_reset;
  93. begin
  94. reset_req = 0;
  95. #(`PERIOD*0.7) reset_req = 1;
  96. #(1.5*`PERIOD) reset_req = 0;
  97. end
  98. endtask
  99. always @(test)
  100. begin: MONITOR
  101. case (test)
  102. 1: begin //display results when running test 1
  103. $display("\n*** RUNNING CPUtest1 - The Basic CPU Diagnostic Program ***");
  104. $display("\n TIME PC INSTR ADDR DATA ");
  105. $display(" ---------- ---- ----- ----- ----- ");
  106. while (test == 1)
  107. @(t_cpu.m_adr.pc_addr)//fixed
  108. if ((t_cpu.m_adr.pc_addr%2 == 1)&&(t_cpu.m_adr.fetch == 1))//fixed
  109. begin
  110. # 60 PC_addr <=t_cpu.m_adr.pc_addr -1 ;
  111. IR_addr <=t_cpu.m_adr.ir_addr;
  112. # 340 $strobe("%t %h %s %h %h", $time, PC_addr,
  113. mnemonic, IR_addr,data );
  114. //HERE DATA HAS BEEN CHANGED T-CPU-M-REGISTER.DATA
  115. end
  116. end
  117. 2: begin
  118. $display("\n*** RUNNING CPUtest2 - The Advanced CPU Diagnostic Program ***");
  119. $display("\n TIME PC INSTR ADDR DATA ");
  120. $display(" ---------- --- ----- ----- ---- ");
  121. while (test == 2)
  122. @(t_cpu.m_adr.pc_addr)
  123. if ((t_cpu.m_adr.pc_addr%2 == 1)
  124. && (t_cpu.m_adr.fetch == 1))
  125. begin
  126. # 60 PC_addr <= t_cpu.m_adr.pc_addr - 1 ;
  127. IR_addr <= t_cpu.m_adr.ir_addr;
  128. # 340 $strobe("%t %h %s %h %h", $time, PC_addr,
  129. mnemonic, IR_addr, data );
  130. end
  131. end
  132. 3: begin
  133. $display("\n*** RUNNING CPUtest3 - An Executable Program ***");
  134. $display("*** This program should calculate the fibonacci ***");
  135. $display("\n TIME FIBONACCI NUMBER");
  136. $display( " --------- -----------------");
  137. while (test == 3)
  138. begin
  139. wait ( t_cpu.m_alu.opcode == 3'h1) // display Fib. No. at end of program loop
  140. $strobe("%t %d", $time,t_ram.ram[10'h2]);
  141. wait ( t_cpu.m_alu.opcode != 3'h1);
  142. end
  143. end
  144. endcase
  145. end
  146. //-------------------------------------------------------------------------
  147. always @(posedge halt) //STOP when HALT instruction decoded
  148. begin
  149. #500
  150. $display("\n*********************************************");
  151. $display( "** A HALT INSTRUCTION WAS PROCESSED !!! **");
  152. $display( "*********************************************\n");
  153. end
  154. always #(`PERIOD/2) clock=~clock;
  155. always @(t_cpu.m_alu.opcode)
  156. //get an ASCII mnemonic for each opcode
  157. case(t_cpu.m_alu.opcode)
  158. 3'b000 : mnemonic ="HLT";
  159. 3'h1 : mnemonic = "SKZ";
  160. 3'h2 : mnemonic = "ADD";
  161. 3'h3 : mnemonic = "AND";
  162. 3'h4 : mnemonic = "XOR";
  163. 3'h5 : mnemonic = "LDA";
  164. 3'h6 : mnemonic = "STO";
  165. 3'h7 : mnemonic = "JMP";
  166. default : mnemonic = "???";
  167. endcase
  168. endmodule
  169. //------------------------------------------- cputop.v ????? -----------------------------------------------------

仿真:

sim:/cputop/rom_sel
run -all

# **************************************************
# *  THE FOLLOWING DEBUG TASK ARE AVAILABLE:           *
# * "test1; " to load the 1st diagnostic progran. *
# *  "test2; " to load the 2nd diagnostic program. *
# *  "test3; " to load the Fibonacci program.      *
# *****************************************************

# rom loaded   successfully!
# ram loaded   successfully!

# *** RUNNING CPUtest1 - The Basic CPU Diagnostic Program ***

#      TIME           PC       INSTR      ADDR     DATA  
#     ----------      ----     -----     -----       ----- 
#    1200.0 ns   0000      JMP     003c  zz
#    2000.0 ns   003c      JMP     0006  zz
#    2800.0 ns   0006      LDA     1800  00
#    3600.0 ns   0008      SKZ     0000  zz
#    4400.0 ns   000c      LDA     1801  ff
#    5200.0 ns   000e      SKZ     0000  zz
#    6000.0 ns   0010      JMP     0014  zz
#    6800.0 ns   0014      STO     1802  ff
#    7600.0 ns   0016      LDA     1800  00
#    8400.0 ns   0018      STO     1802  00
#    9200.0 ns   001a      LDA     1802  00
#   10000.0 ns   001c      SKZ     0000  zz
#   10800.0 ns   0020      XOR     1801  ff
#   11600.0 ns   0022      SKZ     0000  zz
#   12400.0 ns   0024      JMP     0028  zz
#   13200.0 ns   0028      XOR     1801  ff
#   14000.0 ns   002a      SKZ     0000  zz
#   14800.0 ns   002e      HLT     0000  zz

# *********************************************
# **  A HALT INSTRUCTION WAS PROCESSED  !!!  **
# *********************************************

# Break in Module cputop at E:/FPGA/study/cpu/cputop.v line 43
quit -sim

# rom loaded  successfully!
# ram loaded  successfully!

# *** RUNNING CPUtest2 - The Advanced CPU Diagnostic Program ***

#      TIME          PC       INSTR      ADDR     DATA  
#    ----------      ---        -----       -----    ---- 
#   16200.0 ns  0000   LDA  1801 aa
#   17000.0 ns  0002   AND  1802 ff
#   17800.0 ns  0004   XOR  1801 aa
#   18600.0 ns  0006   SKZ  0000 zz
#   19400.0 ns  000a   ADD  1800 01
#   20200.0 ns  000c   SKZ  0000 zz
#   21000.0 ns  000e   JMP  0012 zz
#   21800.0 ns  0012   XOR  1802 ff
#   22600.0 ns  0014   ADD  1800 01
#   23400.0 ns  0016   STO  1803 ff
#   24200.0 ns  0018   LDA  1800 01
#   25000.0 ns  001a   ADD  1803 ff
#   25800.0 ns  001c   SKZ  0000 zz
#   26600.0 ns  0020   HLT  0000 zz

# *********************************************
# **  A HALT INSTRUCTION WAS PROCESSED  !!!  **
# *********************************************

# Break in Module cputop at E:/FPGA/study/cpu/cputop.v line 45
run -continue
# rom loaded  successfully!
# ram loaded  successfully!

# ***   RUNNING CPUtest3 - An Executable Program   ***
# *** This program should calculate the fibonacci  ***

#     TIME      FIBONACCI NUMBER
#   ---------   -----------------
#   33250.0 ns       0
#   40450.0 ns       1
#   47650.0 ns       1
#   54850.0 ns       2
#   62050.0 ns       3
#   69250.0 ns       5
#   76450.0 ns       8
#   83650.0 ns      13
#   90850.0 ns      21
#   98050.0 ns      34
#  105250.0 ns      55
#  112450.0 ns      89
#  119650.0 ns     144

# *********************************************
# **  A HALT INSTRUCTION WAS PROCESSED  !!!  **
# *********************************************

# Break in Module cputop at E:/FPGA/study/cpu/cputop.v line 47
# Break key hit 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/762658
推荐阅读
  

闽ICP备14008679号