URL
https://opencores.org/ocsvn/2d_game_console/2d_game_console/trunk
Subversion Repositories 2d_game_console
[/] [2d_game_console/] [trunk/] [Processor_ModelSim/] [TB_Processor.v.bak] - Rev 2
Compare with Previous | Blame | View Log
// Time unit and simulation accuracy
`timescale 1ns/1ns
module TB_Processor;
integer processor_output_file; // File handler
integer VGA_output_file; // File handler
integer six_button_select_counter;
// Sega Genesis Buttons status
reg button_up;
reg button_down;
reg button_left;
reg button_right;
reg button_a;
reg button_b;
reg button_c;
reg button_x;
reg button_y;
reg button_z;
reg button_mode;
reg button_start;
// 'TB_Processor' module outputs
// 'Processor' module inputs
reg clock;
reg reset;
reg testbench_vsync;
reg testbench_PLL_clock;
reg six_button_UP_Z;
reg six_button_DOWN_Y;
reg six_button_LEFT_X;
reg six_button_RIGHT_MODE;
reg six_button_B_A;
reg six_button_C_START;
// 'TB_Processor' module inputs
// 'Processor' module outputs
wire add_overflow;
wire sub_overflow;
wire compare_aeb;
wire compare_agb;
wire compare_alb;
wire arbiter_grant_1;
wire arbiter_grant_2;
wire arbiter_grant_3;
wire arbiter_wren;
wire cpu_ram_wren;
wire cpu_ram_req;
wire cpu_const_bool;
wire pic_mem_wren;
wire pic_mem_req;
wire pic_cpu_req;
wire cpu_int_ack;
wire pic_int_ack_0;
wire cpu_v_sync_flag;
wire VGA_CLK;
wire VGA_HS;
wire VGA_VS;
wire VGA_BLANK;
wire six_button_mem_wren;
wire six_button_mem_req;
wire six_button_v_sync_flag;
wire six_button_int_req;
wire arbiter_grant_0;
wire sprite_reader_mem_wren;
wire sprite_reader_mem_req;
wire sprite_reader_line_flag;
wire sprite_reader_cpu_sleep;
wire six_button_SELECT;
wire VGA_SYNC;
wire [15:0] add_result;
wire [15:0] arbiter_addr;
wire [3:0] arbiter_current_state;
wire [15:0] arbiter_data;
wire [3:0] arbiter_next_state;
wire [5:0] cpu_current_state;
wire [15:0] cpu_imm;
wire [15:0] cpu_int_program_counter;
wire [7:0] cpu_int_rflags;
wire [5:0] cpu_next_state;
wire [5:0] cpu_opcode;
wire [127:0] cpu_pc_stack;
wire [15:0] cpu_pc_stack_val;
wire [15:0] cpu_program_counter;
wire [15:0] cpu_ram_addr;
wire [15:0] cpu_ram_data;
wire [4:0] cpu_reg_a_num;
wire [15:0] cpu_reg_a_val;
wire [4:0] cpu_reg_b_num;
wire [15:0] cpu_reg_b_val;
wire [4:0] cpu_reg_c_num;
wire [15:0] cpu_reg_c_val;
wire [511:0] cpu_registers;
wire [7:0] cpu_rflags;
wire [2:0] cpu_rflags_index;
wire [15:0] cpu_rom_addr;
wire [1023:0] cpu_sprite_color;
wire [383:0] cpu_sprite_id;
wire [5:0] cpu_sprite_level;
wire [639:0] cpu_sprite_x;
wire [639:0] cpu_sprite_y;
wire [2:0] cpu_stack_pointer;
wire [15:0] divide_quotient;
wire [15:0] divide_remain;
wire [31:0] mult_result;
wire [3:0] pic_current_state;
wire [15:0] pic_int_mask;
wire [15:0] pic_isr_addr;
wire [15:0] pic_mem_addr;
wire [3:0] pic_next_state;
wire [15:0] ram_q;
wire [31:0] rom_q;
wire [15:0] six_button_buttons;
wire [9:0] six_button_counter;
wire [3:0] six_button_current_state;
wire [15:0] six_button_mem_addr;
wire [15:0] six_button_mem_data;
wire [3:0] six_button_next_state;
wire [4:0] sprite_reader_EstadoAtual;
wire [4:0] sprite_reader_EstadoFuturo;
wire [6:0] sprite_reader_level_counter;
wire [5:0] sprite_reader_level_sprite_id;
wire [9:0] sprite_reader_level_sprite_y;
wire [15:0] sprite_reader_mem_addr;
wire [15:0] sub_result;
wire [7:0] VGA_B;
wire [7:0] VGA_G;
wire [9:0] VGA_h_pos;
wire [19:0] VGA_oAddress;
wire [7:0] VGA_R;
wire [9:0] VGA_v_pos;
// 'Processor' module instantiation
Processor DUV(
.clock (clock),
.reset (reset),
.testbench_vsync (testbench_vsync),
.testbench_PLL_clock (testbench_PLL_clock),
.six_button_UP_Z (six_button_UP_Z),
.six_button_DOWN_Y (six_button_DOWN_Y),
.six_button_LEFT_X (six_button_LEFT_X),
.six_button_RIGHT_MODE (six_button_RIGHT_MODE),
.six_button_B_A (six_button_B_A),
.six_button_C_START (six_button_C_START),
.add_overflow (add_overflow),
.sub_overflow (sub_overflow),
.compare_aeb (compare_aeb),
.compare_agb (compare_agb),
.compare_alb (compare_alb),
.arbiter_grant_1 (arbiter_grant_1),
.arbiter_grant_2 (arbiter_grant_2),
.arbiter_grant_3 (arbiter_grant_3),
.arbiter_wren (arbiter_wren),
.cpu_ram_wren (cpu_ram_wren),
.cpu_ram_req (cpu_ram_req),
.cpu_const_bool (cpu_const_bool),
.pic_mem_wren (pic_mem_wren),
.pic_mem_req (pic_mem_req),
.pic_cpu_req (pic_cpu_req),
.cpu_int_ack (cpu_int_ack),
.pic_int_ack_0 (pic_int_ack_0),
.cpu_v_sync_flag (cpu_v_sync_flag),
.VGA_CLK (VGA_CLK),
.VGA_HS (VGA_HS),
.VGA_VS (VGA_VS),
.VGA_BLANK (VGA_BLANK),
.six_button_mem_wren (six_button_mem_wren),
.six_button_mem_req (six_button_mem_req),
.six_button_v_sync_flag (six_button_v_sync_flag),
.six_button_int_req (six_button_int_req),
.arbiter_grant_0 (arbiter_grant_0),
.sprite_reader_mem_wren (sprite_reader_mem_wren),
.sprite_reader_mem_req (sprite_reader_mem_req),
.sprite_reader_line_flag (sprite_reader_line_flag),
.sprite_reader_cpu_sleep (sprite_reader_cpu_sleep),
.six_button_SELECT (six_button_SELECT),
.VGA_SYNC (VGA_SYNC),
.add_result (add_result),
.arbiter_addr (arbiter_addr),
.arbiter_current_state (arbiter_current_state),
.arbiter_data (arbiter_data),
.arbiter_next_state (arbiter_next_state),
.cpu_current_state (cpu_current_state),
.cpu_imm (cpu_imm),
.cpu_int_program_counter (cpu_int_program_counter),
.cpu_int_rflags (cpu_int_rflags),
.cpu_next_state (cpu_next_state),
.cpu_opcode (cpu_opcode),
.cpu_pc_stack (cpu_pc_stack),
.cpu_pc_stack_val (cpu_pc_stack_val),
.cpu_program_counter (cpu_program_counter),
.cpu_ram_addr (cpu_ram_addr),
.cpu_ram_data (cpu_ram_data),
.cpu_reg_a_num (cpu_reg_a_num),
.cpu_reg_a_val (cpu_reg_a_val),
.cpu_reg_b_num (cpu_reg_b_num),
.cpu_reg_b_val (cpu_reg_b_val),
.cpu_reg_c_num (cpu_reg_c_num),
.cpu_reg_c_val (cpu_reg_c_val),
.cpu_registers (cpu_registers),
.cpu_rflags (cpu_rflags),
.cpu_rflags_index (cpu_rflags_index),
.cpu_rom_addr (cpu_rom_addr),
.cpu_sprite_color (cpu_sprite_color),
.cpu_sprite_id (cpu_sprite_id),
.cpu_sprite_level (cpu_sprite_level),
.cpu_sprite_x (cpu_sprite_x),
.cpu_sprite_y (cpu_sprite_y),
.cpu_stack_pointer (cpu_stack_pointer),
.divide_quotient (divide_quotient),
.divide_remain (divide_remain),
.mult_result (mult_result),
.pic_current_state (pic_current_state),
.pic_int_mask (pic_int_mask),
.pic_isr_addr (pic_isr_addr),
.pic_mem_addr (pic_mem_addr),
.pic_next_state (pic_next_state),
.ram_q (ram_q),
.rom_q (rom_q),
.six_button_buttons (six_button_buttons),
.six_button_counter (six_button_counter),
.six_button_current_state (six_button_current_state),
.six_button_mem_addr (six_button_mem_addr),
.six_button_mem_data (six_button_mem_data),
.six_button_next_state (six_button_next_state),
.sprite_reader_EstadoAtual (sprite_reader_EstadoAtual),
.sprite_reader_EstadoFuturo (sprite_reader_EstadoFuturo),
.sprite_reader_level_counter (sprite_reader_level_counter),
.sprite_reader_level_sprite_id (sprite_reader_level_sprite_id),
.sprite_reader_level_sprite_y (sprite_reader_level_sprite_y),
.sprite_reader_mem_addr (sprite_reader_mem_addr),
.sub_result (sub_result),
.VGA_B (VGA_B),
.VGA_G (VGA_G),
.VGA_h_pos (VGA_h_pos),
.VGA_oAddress (VGA_oAddress),
.VGA_R (VGA_R),
.VGA_v_pos (VGA_v_pos)
);
// Data Transfer Instructions
parameter opcode_lw = 6'b001001;
parameter opcode_sw = 6'b001010;
parameter opcode_limm = 6'b001100;
// Arithmetic Instructions
parameter opcode_add = 6'b010001;
parameter opcode_sub = 6'b010010;
parameter opcode_mul = 6'b010100;
parameter opcode_div = 6'b010101;
// Logical Instructions
parameter opcode_and = 6'b100001;
parameter opcode_or = 6'b100010;
parameter opcode_cmp = 6'b100100;
parameter opcode_not = 6'b100101;
// Control Transfer Instructions (Immediate)
parameter opcode_jmp = 6'b101001;
parameter opcode_brfl = 6'b101010;
parameter opcode_call = 6'b101011;
parameter opcode_ret = 6'b101100;
parameter opcode_iret = 6'b101101;
parameter opcode_nop = 6'b101110;
// Control Transfer Instructions (Register)
parameter opcode_jr = 6'b011001;
parameter opcode_brflr = 6'b011010;
parameter opcode_callr = 6'b011011;
// Graphical instructions
parameter opcode_sprite_id = 6'b110001;
parameter opcode_sprite_color = 6'b110010;
parameter opcode_sprite_pos = 6'b110100;
parameter opcode_wait_vsync = 6'b110111;
parameter breakpoint_pc_1 = 5947; // SUCCESS
parameter breakpoint_pc_2 = 5948; // ERROR
parameter breakpoint_pc_3 = 5949; // INTERRUPT
parameter breakpoint_pc_4 = 6110; // INTERRUPT RETURN
// Initial assignments
initial
begin
six_button_select_counter = -1;
testbench_PLL_clock = 0;
clock = 0;
reset = 0;
#40 reset = 1; // Reset on at 40ns
#40 reset = 0; // Reset off at 80ns
// Open file to save VGA output data
VGA_output_file = $fopen("VGA_Output_Data.out","w");
if (VGA_output_file == 0)
begin
$display("VGA_output_file handle was NULL");
$finish;
end
// Open file to save instructions output data
processor_output_file = $fopen("Processor_Output_Data.out","w");
if (processor_output_file == 0)
begin
$display("processor_output_file handle was NULL");
$finish;
end
end
// 20ns clock cycle (50 Mhz)
always #10 clock = !clock;
// 40ns clock cycle (25 Mhz) for VGA_Interface module
always #20 testbench_PLL_clock = !testbench_PLL_clock;
always @ (posedge sprite_reader_cpu_sleep)
begin
$fwrite(processor_output_file, "=====================================\n");
$fwrite(processor_output_file, "========== CPU SLEEP ON!! ==========\n");
$fwrite(processor_output_file, "=====================================\n");
$display("\n\nCPU SLEEP ON!!");
end
always @ (negedge sprite_reader_cpu_sleep)
begin
$fwrite(processor_output_file, "=====================================\n");
$fwrite(processor_output_file, "========== CPU SLEEP OFF!! ==========\n");
$fwrite(processor_output_file, "=====================================\n");
$display("\n\nCPU SLEEP OFF!!");
end
always @ (negedge clock)
begin
if(cpu_current_state == 3)
begin
$fwrite(processor_output_file, "%d: ", cpu_program_counter);
case (cpu_opcode)
opcode_add:
begin
$fwrite(processor_output_file, "ADD\n");
end
opcode_sub:
begin
$fwrite(processor_output_file, "SUB\n");
end
opcode_mul:
begin
$fwrite(processor_output_file, "MUL\n");
end
opcode_div:
begin
$fwrite(processor_output_file, "DIV\n");
end
opcode_and:
begin
$fwrite(processor_output_file, "AND\n");
end
opcode_or:
begin
$fwrite(processor_output_file, "OR\n");
end
opcode_cmp:
begin
$fwrite(processor_output_file, "CMP\n");
end
opcode_not:
begin
$fwrite(processor_output_file, "NOT\n");
end
opcode_sprite_id:
begin
$fwrite(processor_output_file, "SPRITE_ID\n");
end
opcode_sprite_color:
begin
$fwrite(processor_output_file, "SPRITE_COLOR\n");
end
opcode_sprite_pos:
begin
$fwrite(processor_output_file, "SPRITE_POS\n");
end
opcode_limm:
begin
$fwrite(processor_output_file, "LIMM\n");
end
opcode_lw:
begin
$fwrite(processor_output_file, "LW\n");
end
opcode_sw:
begin
$fwrite(processor_output_file, "SW\n");
end
opcode_jmp:
begin
$fwrite(processor_output_file, "JMP\n");
end
opcode_jr:
begin
$fwrite(processor_output_file, "JR\n");
end
opcode_brfl:
begin
$fwrite(processor_output_file, "BRFL\n");
end
opcode_brflr:
begin
$fwrite(processor_output_file, "BRFLR\n");
end
opcode_nop:
begin
$fwrite(processor_output_file, "NOP\n");
end
opcode_call:
begin
$fwrite(processor_output_file, "CALL\n");
end
opcode_callr:
begin
$fwrite(processor_output_file, "CALLR\n");
end
opcode_ret:
begin
$fwrite(processor_output_file, "RET\n");
end
opcode_wait_vsync:
begin
$fwrite(processor_output_file, "WAIT_VSYNC\n");
end
opcode_iret:
begin
$fwrite(processor_output_file, "IRET\n");
end
default:
begin
$fwrite(processor_output_file, "ILLEGAL INSTRUCTION\n");
end
endcase
if(cpu_program_counter == breakpoint_pc_1)
begin
$fwrite(processor_output_file, "=====================================\n");
$fwrite(processor_output_file, "============== SUCCESS ==============\n");
$fwrite(processor_output_file, "=====================================\n");
$display("\n\nSuccess!!");
//$fclose(processor_output_file);
//$stop;
end
if(cpu_program_counter == breakpoint_pc_2)
begin
$fwrite(processor_output_file, "=====================================\n");
$fwrite(processor_output_file, "=============== ERROR ===============\n");
$fwrite(processor_output_file, "=====================================\n");
$display("\n\nError!!");
$fclose(processor_output_file);
$stop;
end
if(cpu_program_counter == breakpoint_pc_3)
begin
$fwrite(processor_output_file, "=====================================\n");
$fwrite(processor_output_file, "============= INTERRUPT =============\n");
$fwrite(processor_output_file, "=====================================\n");
$display("\n\Interrupt!!");
//$fclose(processor_output_file);
//$stop;
end
if(cpu_program_counter == breakpoint_pc_4)
begin
$fwrite(processor_output_file, "=====================================\n");
$fwrite(processor_output_file, "========== INTERRUPT RETURN =========\n");
$fwrite(processor_output_file, "=====================================\n");
$display("\n\Interrupt Return!!");
//$fclose(processor_output_file);
//$stop;
end
end
end
always @ (negedge testbench_PLL_clock)
begin
if (VGA_v_pos == 0)
begin
if (VGA_h_pos < 799)
begin
$fwrite(VGA_output_file, "%.5d ", VGA_h_pos);
end
else
begin
$fwrite(VGA_output_file, "%.5d ", VGA_h_pos);
$fwrite(VGA_output_file, "\n");
end
end
else
begin
if (VGA_h_pos < 799)
begin
$fwrite(VGA_output_file, "%.5d ", { VGA_B[7:3],VGA_G[7:2],VGA_R[7:3] } );
end
else
begin
$fwrite(VGA_output_file, "%.5d ", { VGA_B[7:3],VGA_G[7:2],VGA_R[7:3] } );
$fwrite(VGA_output_file, "\n");
end
end
if ( (VGA_v_pos == 524) && (VGA_h_pos == 799) )
begin
$fwrite(VGA_output_file, "\n\n");
$fwrite(VGA_output_file, "*************************************************");
$fwrite(VGA_output_file, "\n\n");
end
end
//always @ (negedge VGA_HS)
//begin
// $fwrite(VGA_output_file, "\n");
//end
//always @ (negedge VGA_VS)
//begin
// $fwrite(VGA_output_file, "\n");
// $fwrite(VGA_output_file, "******************");
// $fwrite(VGA_output_file, "\n");
//end
always
begin
//#170000 ; // Wait 170 us = 170.000 ns
// Button UP and A pressed - Active low
button_up = 0;
button_down = 1;
button_left = 1;
button_right = 1;
button_a = 0;
button_b = 1;
button_c = 1;
button_x = 1;
button_y = 1;
button_z = 1;
button_mode = 1;
button_start = 1;
// Default start
//six_button_UP_Z = button_up;
//six_button_DOWN_Y = button_down;
//six_button_LEFT_X = button_left;
//six_button_RIGHT_MODE = button_right;
//six_button_B_A = button_b;
//six_button_C_START = button_c;
#20000000 ; // Wait 20 ms = 20.000.000 ns
// Button UP and A pressed - Active low
button_up = 0;
button_down = 1;
button_left = 1;
button_right = 1;
button_a = 0;
button_b = 1;
button_c = 1;
button_x = 1;
button_y = 1;
button_z = 1;
button_mode = 1;
button_start = 1;
#20000000 ; // Wait 20 ms = 20.000.000 ns
// Button LEFT and B pressed - Active low
button_up = 1;
button_down = 1;
button_left = 0;
button_right = 1;
button_a = 1;
button_b = 0;
button_c = 1;
button_x = 1;
button_y = 1;
button_z = 1;
button_mode = 1;
button_start = 1;
#20000000 ; // Wait 20 ms = 20.000.000 ns
// Button LEFT and B pressed - Active low
button_up = 1;
button_down = 1;
button_left = 0;
button_right = 1;
button_a = 1;
button_b = 0;
button_c = 1;
button_x = 1;
button_y = 1;
button_z = 1;
button_mode = 1;
button_start = 1;
#20000000 ; // Wait 20 ms = 20.000.000 ns
$display("\n\nSTOP!!");
$stop;
end
always @ (six_button_SELECT)
begin
if (six_button_select_counter < 7)
six_button_select_counter = six_button_select_counter+1;
else
six_button_select_counter = 0;
case (six_button_select_counter)
0:
begin
six_button_UP_Z = button_up;
six_button_DOWN_Y = button_down;
six_button_LEFT_X = button_left;
six_button_RIGHT_MODE = button_right;
six_button_B_A = button_b;
six_button_C_START = button_c;
end
1:
begin
six_button_UP_Z = button_up;
six_button_DOWN_Y = button_down;
six_button_LEFT_X = 0;
six_button_RIGHT_MODE = 0;
six_button_B_A = button_a;
six_button_C_START = button_start;
end
2:
begin
six_button_UP_Z = button_up;
six_button_DOWN_Y = button_down;
six_button_LEFT_X = button_left;
six_button_RIGHT_MODE = button_right;
six_button_B_A = button_b;
six_button_C_START = button_c;
end
3:
begin
six_button_UP_Z = button_up;
six_button_DOWN_Y = button_down;
six_button_LEFT_X = 0;
six_button_RIGHT_MODE = 0;
six_button_B_A = button_a;
six_button_C_START = button_start;
end
4:
begin
six_button_UP_Z = button_up;
six_button_DOWN_Y = button_down;
six_button_LEFT_X = button_left;
six_button_RIGHT_MODE = button_right;
six_button_B_A = button_b;
six_button_C_START = button_c;
end
5:
begin
six_button_UP_Z = 0;
six_button_DOWN_Y = 0;
six_button_LEFT_X = 0;
six_button_RIGHT_MODE = 0;
six_button_B_A = button_a;
six_button_C_START = button_start;
end
6:
begin
six_button_UP_Z = button_z;
six_button_DOWN_Y = button_y;
six_button_LEFT_X = button_x;
six_button_RIGHT_MODE = button_mode;
six_button_B_A = button_b;
six_button_C_START = button_c;
end
7:
begin
six_button_UP_Z = 1;
six_button_DOWN_Y = 1;
six_button_LEFT_X = 1;
six_button_RIGHT_MODE = 1;
six_button_B_A = button_a;
six_button_C_START = button_start;
end
default:
begin
six_button_UP_Z = button_up;
six_button_DOWN_Y = button_down;
six_button_LEFT_X = button_left;
six_button_RIGHT_MODE = button_right;
six_button_B_A = button_b;
six_button_C_START = button_c;
end
endcase
end
endmodule