/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#include "stdafx.h"
#include "sky/debug.h"

static const char *section_0_compacts[] = {
	"UNDEFINED",
	"joey",
	"joey_park",
	"foster",
	"std_menu_logic",
	"text_mouse",
	"gallcard_menu",
	"rs_mega_alive",
	"citycard_menu",
	"shades_menu",
	"putty_menu",
	"lightbulb_menu",
	"low_get_seq",
	"mini_shrug_seq",
	"sml_up_mid_get_seq",
	"new_grid",
	"lamb",
	"floor",
	"coordinate_test",
	"save_restore_mouse",
	"whole_screen",
	"l_talk_s2",
	"r_talk_s2",
	"text_1",
	"text_2",
	"text_3",
	"text_4",
	"text_5",
	"text_6",
	"text_7",
	"text_8",
	"text_9",
	"text_10",
	"text_11",
	"wd40_menu",
	"skey_menu",
	"secateurs_menu",
	"rope_menu",
	"plaster_menu",
	"new_cable_menu",
	"shrug_seq",
	"rad_shrug_seq",
	"brick_menu",
	"tongs_menu",
	"talk1",
	"talk2",
	"menu_bar",
	"left_arrow",
	"right_arrow",
	"dog_food_menu",
	"UNDEFINED",
	"blank1",
	"blank2",
	"blank3",
	"blank4",
	"blank5",
	"blank6",
	"blank7",
	"blank8",
	"blank9",
	"blank10",
	"blank11",
	"UNDEFINED",
	"crow_bar_menu",
	"sarnie_menu",
	"spanner_menu",
	"joeyb_menu",
	"low_floor",
	"UNDEFINED",
	"stairs",
	"upstairs",
	"anita_card_menu",
	"rs_lamb_to_three",
	"rs_lamb_start_2",
	"anchor_menu",
	"magazine_menu",
	"tape_menu",
	"glass_menu",
	"rs_lamb_start_3",
	"ticket_menu",
	"s29_fast_list",
	"s6_fast_list",
	"fast_list_sc3",
	"s9_fast_list",
	"s10_fast_list",
	"bar",
	"s11_fast_list",
	"fast_list_0",
	"s0_fast_list",
	"s7_fast_list",
	"door",
	"s28_fast_list",
	"swing_list",
	"UNDEFINED",
	"UNDEFINED",
	"outside_ledge",
	"UNDEFINED",
	"r1_door",
	"UNDEFINED",
	"UNDEFINED",
	"UNDEFINED",
	"UNDEFINED",
	"fast_list_sc90",
	"UNDEFINED",
	"UNDEFINED",
	"small_door",
	"sc85_fast_list",
	"sc85_chip_list",
	"sc85_logic_list",
	"sc85_mouse_list",
	"sc85_palette",
	"right_exit0",
	"UNDEFINED",
	"UNDEFINED",
	"UNDEFINED",
	"s2_floor",
	"UNDEFINED",
	"s101_chip_list",
	"s101_pal",
	"s101_mouse",
	"s101_logic",
	"full_screen",
	"cancel_button",
	"button_0",
	"button_1",
	"button_2",
	"button_3",
	"button_4",
	"button_5",
	"button_6",
	"button_7",
	"button_8",
	"button_9",
	"rs_left_arrow",
	"rs_right_arrow",
	"rs_blank",
	"monitor",
	"anita",
	"UNDEFINED",
	"UNDEFINED",
	"UNDEFINED",
	"baby_logic_list",
	"rs_l_arr_linc",
	"rs_r_arr_linc",
	"rs_blanks_linc",
	"s5_fast_list",
	"but_e",
	"but_0",
	"but_1",
	"but_2",
	"but_3",
	"but_4",
	"but_5",
	"but_6",
	"but_7",
	"but_8",
	"but_9",
	"UNDEFINED",
	"s102_chip_list",
	"s102_pal",
	"s102_logic",
	"s102_mouse",
	"restart_butt",
	"restart_seq",
	"restore_butt",
	"restore_seq",
	"seq1_pal",
	"seq2_pal",
	"seq3_pal",
	"fast_intro",
	"chip_intro",
	"fast_intro_2",
	"fast_intro_3",
	"retina_scan",
	"retina_scan_cdt",
	"exit_butt",
	"exit_seq",
	"forklift_cpt",
	"forklift1_cdt",
	"forklift2_cdt",
};

static const char *logic_table_names[] = {
	"return",
	"SkyLogic::script",
	"SkyLogic::auto_route",
	"SkyLogic::ar_anim",
	"SkyLogic::ar_turn",
	"SkyLogic::alt",
	"SkyLogic::anim",
	"SkyLogic::turn",
	"SkyLogic::cursor",
	"SkyLogic::talk",
	"SkyLogic::listen",
	"SkyLogic::stopped",
	"SkyLogic::choose",
	"SkyLogic::frames",
	"SkyLogic::pause",
	"SkyLogic::wait_sync",
	"SkyLogic::simple_anim",
};

static const char opcode_par[] = {
	1,
	0,
	1,
	0,
	0,
	1,
	1,
	0,
	0,
	1,
	0,
	2,
	0,
	0,
	1,
	1,
	1,
	0,
	1,
	0,
	0,
};

static const char *opcodes[] = {
	"push_variable",
	"less_than",
	"push_number",
	"not_equal",
	"if_and",
	"skip_zero",
	"pop_var",
	"minus",
	"plus",
	"skip_always",
	"if_or",
	"call_mcode",
	"more_than",
	"script_exit",
	"switch",
	"push_offset",
	"pop_offset",
	"is_equal",
	"skip_nz",
	"script_exit",
	"restart_script",
};

static const char *mcodes[] = {
	"fn_cache_chip",
	"fn_cache_fast",
	"fn_draw_screen",
	"fn_ar",
	"fn_ar_animate",
	"fn_idle",
	"fn_interact",
	"fn_start_sub",
	"fn_they_start_sub",
	"fn_assign_base",
	"fn_disk_mouse",
	"fn_normal_mouse",
	"fn_blank_mouse",
	"fn_cross_mouse",
	"fn_cursor_right",
	"fn_cursor_left",
	"fn_cursor_down",
	"fn_open_hand",
	"fn_close_hand",
	"fn_get_to",
	"fn_set_to_stand",
	"fn_turn_to",
	"fn_arrived",
	"fn_leaving",
	"fn_set_alternate",
	"fn_alt_set_alternate",
	"fn_kill_id",
	"fn_no_human",
	"fn_add_human",
	"fn_add_buttons",
	"fn_no_buttons",
	"fn_set_stop",
	"fn_clear_stop",
	"fn_pointer_text",
	"fn_quit",
	"fn_speak_me",
	"fn_speak_me_dir",
	"fn_speak_wait",
	"fn_speak_wait_dir",
	"fn_chooser",
	"fn_highlight",
	"fn_text_kill",
	"fn_stop_mode",
	"fn_we_wait",
	"fn_send_sync",
	"fn_send_fast_sync",
	"fn_send_request",
	"fn_clear_request",
	"fn_check_request",
	"fn_start_menu",
	"fn_unhighlight",
	"fn_face_id",
	"fn_foreground",
	"fn_background",
	"fn_new_background",
	"fn_sort",
	"fn_no_sprite_engine",
	"fn_no_sprites_a6",
	"fn_reset_id",
	"fn_toggle_grid",
	"fn_pause",
	"fn_run_anim_mod",
	"fn_simple_mod",
	"fn_run_frames",
	"fn_await_sync",
	"fn_inc_mega_set",
	"fn_dec_mega_set",
	"fn_set_mega_set",
	"fn_move_items",
	"fn_new_list",
	"fn_ask_this",
	"fn_random",
	"fn_person_here",
	"fn_toggle_mouse",
	"fn_mouse_on",
	"fn_mouse_off",
	"fn_fetch_x",
	"fn_fetch_y",
	"fn_test_list",
	"fn_fetch_place",
	"fn_custom_joey",
	"fn_set_palette",
	"fn_text_module",
	"fn_change_name",
	"fn_mini_load",
	"fn_flush_buffers",
	"fn_flush_chip",
	"fn_save_coods",
	"fn_plot_grid",
	"fn_remove_grid",
	"fn_eyeball",
	"fn_cursor_up",
	"fn_leave_section",
	"fn_enter_section",
	"fn_restore_game",
	"fn_restart_game",
	"fn_new_swing_seq",
	"fn_wait_swing_end",
	"fn_skip_intro_code",
	"fn_blank_screen",
	"fn_print_credit",
	"fn_look_at",
	"fn_linc_text_module",
	"fn_text_kill2",
	"fn_set_font",
	"fn_start_fx",
	"fn_stop_fx",
	"fn_start_music",
	"fn_stop_music",
	"fn_fade_down",
	"fn_fade_up",
	"fn_quit_to_dos",
	"fn_pause_fx",
	"fn_un_pause_fx",
	"fn_printf",
};

void SkyDebug::fetchCompact(uint32 a) {
	uint32 sectionNum = (a & 0xf000) >> 12;
	uint32 compactNum = (a & 0x0fff);

	if (sectionNum == 0)
		printf("Loading Compact %s from section %d\n", section_0_compacts[compactNum], sectionNum);
	else
		printf("Loading Compact %d from section %d\n", compactNum, sectionNum);
}

void SkyDebug::logic(uint32 logic) {
	printf("LOGIC: %s\n", logic_table_names[logic]);
}

void SkyDebug::script(uint32 command, uint16 *scriptData) {
	int i;
	printf("SCRIPT: %s", opcodes[command]);
	for (i = 0; i < opcode_par[command]; i++)
		printf(" %d", READ_LE_UINT16(scriptData + i));
	printf("\n");
}

void SkyDebug::mcode(uint32 mcode, uint32 a, uint32 b, uint32 c) {
	printf("MCODE: %s(%d, %d, %d)\n", mcodes[mcode], a, b, c);
};