/************************************************************************
 *									*
 *	.c ファイル で インクルード しなければならない			*
 *				1995年 12月26日(火曜日) 11時37分02秒 JST*
 ************************************************************************/
#ifndef _SYS_MAIN_H_
#define _SYS_MAIN_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "sys_debug.h"	/* デバッグ用 インクルード */

#include <ultra64.h>
#include <PR/ramrom.h>	/* needed for argument passing into the app */
#include <assert.h>

#include <debug.h>	/* ウルトラ ６４ ＳＲＤ マクロ */
#include <u64patch.h>	/* ウルトラ ６４ ＳＲＤ マクロ */
#include <u64macro.h>	/* ウルトラ ６４ ＳＲＤ マクロ */
#include <u64types.h>	/* ウルトラ ６４ ＳＲＤ マクロ */
#include <math64.h>	/* ウルトラ ６４ ＳＲＤ マクロ */
#include <macro.h>	/* ウルトラ ６４ ＳＲＤ マクロ */

/*======================================================================*
 *									*
 *	メッセージ用							*
 *									*
 *======================================================================*/
/*
 *	バッファ数
 */
#define		NUM_MSG_PI		50
#define		NUM_MSG_DMA		1
#define		NUM_MSG_SERIAL		1
#define		NUM_MSG_RETRACE		1
#define		NUM_MSG_EVENT		32
#define		NUM_MSG_AUDIO		1
#define		NUM_MSG_AUDIODONE	1
#define		NUM_MSG_GRAPH		4
#define		NUM_MSG_GRAPHDONE	2
#define		NUM_MSG_TASK		16
#define		NUM_MSG_TIME		16
#define		NUM_MSG_TIMEWAIT	1
#define		NUM_MSG_CONTROLLER	8
#define		NUM_MSG_EEPROM		1
#define		NUM_MSG_KEY		1

/*
 *	メッセージ
 */
#define		MSG_SERIAL		0
#define		MSG_RSPDONE		1
#define		MSG_RDPDONE		2
#define		MSG_RETRACE		3
#define		MSG_PRENMI		4

/*
 *	コントローラ コントロール メッセージ
 */
#define		MSG_CONT_READ		10
#define		MSG_EEPR_LOAD		11
#define		MSG_EEPR_SAVE		12
#define		MSG_VIBR_PACK		13

#define		MSG_EEPR_ERROR		14
#define		MSG_EEPR_CORRECT	15

#define		MSG_CONT_COMPLETE	16

/*======================================================================*
 *									*
 *	タスク用							*
 *									*
 *======================================================================*/
/*
 *	バッファ数
 */
#define		NUM_RCP_AUDIO		1
#define		NUM_RCP_GRAPH		2

/*
 *	ＲＣＰ ステータス
 */
#define		RCP_STAT_IDLE		0
#define		RCP_STAT_RUNNING	1
#define		RCP_STAT_SUSPEND	2
#define		RCP_STAT_RSPDONE	3
#define		RCP_STAT_RDPDONE	4

/*======================================================================*
 *									*
 *	構造体 宣言							*
 *									*
 *======================================================================*/
/*
 * フロート行列
 *
 *	MtxF[0][0] MtxF[1][0] MtxF[2][0] MtxF[3][0]
 *	MtxF[0][1] MtxF[1][1] MtxF[2][1] MtxF[3][1]
 *	MtxF[0][2] MtxF[1][2] MtxF[2][2] MtxF[3][2]
 *	MtxF[0][3] MtxF[1][3] MtxF[2][3] MtxF[3][3]
 */
typedef float	MtxF_t[4][4];
typedef union {
    MtxF_t		m;
} MtxF;

/*
 *	ＲＣＰ タスク
 */
typedef struct {
    OSTask		ostask;
    OSMesgQueue		*osmsgQ;
    OSMesg		osmsg;
    int			status;
} RCPTask;

/*
 * ダイナミック セグメント用
 *
 *	rcptask		: ＲＣＰタスク
 *	Vp_viewport	: ビューポート
 *	Mtx_modelview	: モデリング 行列
 *	Gfx_list00	: ディスプレイ リスト 前
 *	Gfx_list01	: ディスプレイ リスト 中
 *	Gfx_list02	: ディスプレイ リスト 後
 *	Lights7		: ライト
 */
typedef struct {
    RCPTask	Rcptask00;
    Vp		Vp_viewport[0x10];
    Mtx		Mtx_modelview[0x480];
    Gfx		Gfx_list00[0x180];
    Gfx		Gfx_list01[0x1380];
    Gfx		Gfx_list02[0xd80];
    Lights7	light[0x100];
} Dynamic;

/*======================================================================*
 *									*
 *	各 ファイル の ヘッダー						*
 *									*
 *======================================================================*/
#include "sys_segment.h"
#include "sys_skeleton.h"
#include "sys_matrix.h"
#include "sys_lib.h"
#include "sys_math.h"
#include "sys_framebuffer.h"
#include "sys_zbuffer.h"
#include "sys_light.h"
#include "sys_subdata.h"
#include "sys_controller.h"
#include "sys_fifo.h"
#include "sys_memory.h"
#include "sys_yield.h"
#include "sys_dram_stack.h"
#include "sys_time.h"
#include "sys_eeprom.h"

/*======================================================================*
 *									*
 *	セグメント 宣言							*
 *									*
 *======================================================================*/
EXTERN_DEFSEG(code);

/*======================================================================*
 *									*
 *	ゲームプログラムとのコンタクト					*
 *									*
 *======================================================================*/
void call_initial(void);
void call_main(void);
void call_reset(void);
void call_framebuffer(u16 *);

/*======================================================================*
 *									*
 *	変数 宣言							*
 *									*
 *======================================================================*/
/*======================================================================*
 *									*
 *	メインのパラメータ						*
 *									*
 *======================================================================*/
extern char		argc;		/* パラメータの数 */
extern char		*argv[32];	/* パラメータ */

/*======================================================================*
 *									*
 *	メッセージ キュー ＆ メッセージ バッファ			*
 *									*
 *======================================================================*/
/* ＰＩ */
extern OSMesgQueue	PiMsgQ;
extern OSMesg 		PiMsgBuf[NUM_MSG_PI];

/* ＤＭＡ */
extern OSMesgQueue	dmaMsgQ;
extern OSMesg		dmaMsgBuf[NUM_MSG_DMA];
extern OSIoMesg		dmaIOMsgBuf;

/* コントローラ */
extern OSMesgQueue	serialMsgQ;
extern OSMesg		serialMsgBuf[NUM_MSG_SERIAL];

/* ＲＥＴＲＡＣＥ ＆ ＲＳＰ ＆ ＲＤＰ */
extern OSMesgQueue	eventMsgQ;
extern OSMesg		eventMsgBuf[NUM_MSG_EVENT];

/* ＲＣＰＴａｓｋ構造体のポインターを受取る */
extern OSMesgQueue	taskMsgQ;
extern OSMesg		taskMsgBuf[NUM_MSG_TASK];

/* オーディオ スレッドにＲＥＴＲＡＣＥメッセージを渡す */
extern OSMesgQueue	audioMsgQ;
extern OSMesg		audioMsgBuf[NUM_MSG_AUDIO];

/* オーディオ スレッドにＲＳＰ終了メッセージを渡す */
extern OSMesgQueue	audiodoneMsgQ;
extern OSMesg		audiodoneMsgBuf[NUM_MSG_AUDIODONE];

/* グラフィック スレッドにＲＥＴＲＡＣＥメッセージを渡す */
extern OSMesgQueue	graphMsgQ;
extern OSMesg		graphMsgBuf[NUM_MSG_GRAPH];

/* グラフィック スレッドにＲＤＰ終了メッセージを渡す */
extern OSMesgQueue	graphdoneMsgQ;
extern OSMesg		graphdoneMsgBuf[NUM_MSG_GRAPHDONE];

/* コントローラへ処理リクエストを送る */
extern OSMesgQueue	controllerMsgQ;
extern OSMesg		controllerMsgBuf[NUM_MSG_CONTROLLER];

/* コントローラからキー読み込み終了メッセージを受け取る */
extern OSMesgQueue	keyMsgQ;
extern OSMesg		keyMsgBuf[NUM_MSG_KEY];

/* ＥＥＰＲＯＭから ＷＲＩＴＥ ｏｒ ＲＥＡＤ 終了メッセージを受け取メる */
extern OSMesgQueue	eepromMsgQ;
extern OSMesg		eepromMsgBuf[NUM_MSG_EEPROM];

/* タイム スレッド に タイマーメッセージを渡す */
extern OSMesgQueue	timeMsgQ;
extern OSMesg		timeMsgBuf[NUM_MSG_TIME];

/* その場所で時間が来るまで停止 */
extern OSMesgQueue	timewaitMsgQ;
extern OSMesg		timewaitMsgBuf[NUM_MSG_TIMEWAIT];

/*======================================================================*
 *									*
 *	ダイナミック バッファ						*
 *									*
 *======================================================================*/
extern Dynamic		DynamicBuffer[2];

/*======================================================================*
 *									*
 *	ダイナミック バッファ用 ポインター各種				*
 *									*
 *======================================================================*/
extern Dynamic		*DynamicBufferP;
extern RCPTask		*Rcptask00P;
extern Vp		*Vp_viewportP;
extern Mtx		*Mtx_modelviewP;
extern Gfx		*Gfx_list00P;
extern Gfx		*Gfx_list01P;
extern Gfx		*Gfx_list02P;
extern Lights7		*lightP;

/*======================================================================*
 *									*
 *	フレームバッファ ポインター					*
 *									*
 *======================================================================*/
extern u16		*FrameBufferP;

/*======================================================================*
 *									*
 *	レンダリング用 バッファ ポインター				*
 *									*
 *======================================================================*/
extern u16		*RenderBufferP;

/*======================================================================*
 *									*
 *	各種環境							*
 *									*
 *======================================================================*/
/* 最小 リトレース */
extern char		min_retrace;
/* フレーム カウント */
extern unsigned int	count;
/* リセット フラグ */
extern char		reset_flg;
/* ＲＣＰ 停止 フラグ */
extern char		rcp_stop_flg;
/* いかがわしい */
extern char		ikagawasii[MAXCONTROLLERS];
/* ブランキング カラー */
extern u16		Black_color;
/* ブランキング フラグ */
extern u16		Black_flg;

/*======================================================================*
 *									*
 *	デバッグ用							*
 *									*
 *======================================================================*/
#if 0 < FOX_DEBUG
extern char		debug;

extern OSTime		A_RCP_S, A_RCP_W, A_RCP_T;
extern OSTime		G_RSP_S, G_RSP_W, G_RSP_T;
extern OSTime		G_RCP_S, G_RCP_W, G_RCP_T;
extern OSTime		A_CPU_S, A_CPU_T;
extern OSTime		G_CPU_S, G_CPU_T;
extern OSTime		RETRACE_S, RETRACE_T;
#endif

#ifdef __cplusplus
}
#endif

#endif
