setclock.c 13.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
/********************************************************************************
						   NINTENDO64 Disk Drive IPL4

							  clock setting module

								February 27, 1997
 ********************************************************************************/

#include <ultra64.h>
#include "ipl4.h"
#include "AUDIO/cpuaudio.h"
#include "graphics.h"


#define	MODE_CONT_LEFT			1
#define	MODE_CONT_RIGHT			2
#define	MODE_CONT_UP			3
#define	MODE_CONT_DOWN			4
#define	MODE_STCK_LEFT			5
#define	MODE_STCK_RIGHT			6
#define	MODE_STCK_UP			7
#define	MODE_STCK_DOWN			8

typedef struct {
	char		 	stickx;				/* analog stick position x				*/
	char		 	sticky;				/* analog stick position y				*/
	ushort		 	button;				/* controller button status				*/
	ushort		 	trigger;			/* controller button trigger			*/
	short			repeat;				/* auto repeat counter					*/
	short			curmode;			/* current process mode					*/
} Controller;

static int process;
static int counter;

static OSContPad *	activePad;
static Controller	controller;


static ClockInfo clockinfo;			// BUG 1997.1.31

//	static ClockInfo clockinfo = {
//		1, 0, 97, 1, 10, 4, 22, 0
//	};


static char monthTable[2][12] = {
	31,28,31,30,31,30,31,31,30,31,30,31,
	31,29,31,30,31,30,31,31,30,31,30,31
};

#define	IsDiskLoading()			((diskStatus == DISK_STAT_LOADING) || (diskStatus == DISK_STAT_LOADEND))
#define	IsClockWorking()		(diskClock.status == CLOCK_STAT_ACTIVE)


/********************************************************************************/
/*																				*/
/*	Get length of month.														*/
/*																				*/
/********************************************************************************/
static int
GetMonthLength(ClockInfo *clock)
{
	int year = 1900 + clock->year;
	int leap = ((year - 1996) % 4 == 0);

	return((int)monthTable[leap][clock->month-1]);
}
/********************************************************************************/
/*																				*/
/*	Set week.																	*/
/*																				*/
/********************************************************************************/
static void
SetWeek(ClockInfo *clock)
{
	int index;
	int year = 1900 + clock->year;
	int leap = ((year - 1996) % 4 == 0);
	int week = ((year - 1996) + (year - 1993) / 4) % 7;


	if (clock->day > monthTable[leap][clock->month-1]) {
		clock->day = monthTable[leap][clock->month-1];
	}
	for (index = 0; index < clock->month-1; index++) {
		week += monthTable[leap][index];
	}
	clock->week = (week + (clock->day-1)) % 7;
}
/********************************************************************************/
/*																				*/
/*	Read clock from disk drive.													*/
/*																				*/
/********************************************************************************/
static void
ReadClock(ClockInfo *clock)
{
	clock->cursor = 1;								// BUG 1997.1.31
	clock->blink  = 0;								// BUG 1997.1.31
	clock->year	  = BcdToBin(diskClock.year	 );
	clock->month  = BcdToBin(diskClock.month );
	clock->day	  = BcdToBin(diskClock.day	 );
	clock->hour	  = BcdToBin(diskClock.hour	 );
	clock->minute = BcdToBin(diskClock.minute);

	if (clock->year < 21)  clock->year += 100;
	if (clock->year < 96)  clock->year  =  96;
	SetWeek(clock);
}
/********************************************************************************/
/*																				*/
/*	Read clock from disk drive.													*/
/*																				*/
/********************************************************************************/
static void
WriteClock(ClockInfo *clock)
{
	DDClock dskclk;

	dskclk.year	  = BinToBcd(clock->year - ((clock->year >= 100) ? 100 : 0));
	dskclk.month  = BinToBcd(clock->month );
	dskclk.day	  = BinToBcd(clock->day	  );
	dskclk.hour	  = BinToBcd(clock->hour  );
	dskclk.minute = BinToBcd(clock->minute);
	dskclk.second = 0;

	WriteDDClock(&dskclk);
}
/********************************************************************************/
/*																				*/
/*	Add value.																	*/
/*																				*/
/********************************************************************************/
static void
AddValue(ClockInfo *clock)
{
	switch (clock->cursor) {
		case 1:	if (++clock->year > 120)  clock->year  = 96;
				SetWeek(clock);
				break;

		case 2: if (++clock->month > 12)  clock->month = 1;
				SetWeek(clock);
				break;

		case 3: if (++clock->day > GetMonthLength(clock))  clock->day = 1;
				SetWeek(clock);
				break;

		case 4: if (++clock->hour > 23)  clock->hour = 0;
				break;

		case 5: if (++clock->minute > 59)  clock->minute = 0;
				break;
	}
	Nacp_SeStart(NACP_CHANGE);
}
/********************************************************************************/
/*																				*/
/*	Sub value.																	*/
/*																				*/
/********************************************************************************/
static void
SubValue(ClockInfo *clock)
{
	switch (clock->cursor) {
		case 1:	if (--clock->year < 96)  clock->year  = 120;
				SetWeek(clock);
				break;

		case 2: if (--clock->month < 1)  clock->month = 12;
				SetWeek(clock);
				break;

		case 3: if (--clock->day < 1)  clock->day = GetMonthLength(clock);
				SetWeek(clock);
				break;

		case 4: if (--clock->hour < 0)  clock->hour = 23;
				break;

		case 5: if (--clock->minute < 0)  clock->minute = 59;
				break;
	}
	Nacp_SeStart(NACP_CHANGE);
}
/********************************************************************************/
/*																				*/
/*	Set controller stick level.													*/
/*																				*/
/********************************************************************************/
static void
SetController(void)
{
	uchar	status = 0;

	controller.stickx  = 0;
	controller.sticky  = 0;
	controller.trigger = activePad->button & (activePad->button ^ controller.button);
	controller.button  = activePad->button;

	if (activePad->stick_x < -7)  controller.stickx = (activePad->stick_x + 6);
	if (activePad->stick_x >  7)  controller.stickx = (activePad->stick_x - 6);
	if (activePad->stick_y < -7)  controller.sticky = (activePad->stick_y + 6);
	if (activePad->stick_y >  7)  controller.sticky = (activePad->stick_y - 6);

	if (controller.stickx < -64)  controller.stickx = -64;
	if (controller.stickx >  64)  controller.stickx =  64;
	if (controller.sticky < -64)  controller.sticky = -64;
	if (controller.sticky >  64)  controller.sticky =  64;
}
/********************************************************************************/
/*																				*/
/*	Check stick neutral.														*/
/*																				*/
/********************************************************************************/
static int
IsStickNeutral(void)
{
	return(-8 <= controller.stickx && controller.stickx <= 8 && 
		   -8 <= controller.sticky && controller.sticky <= 8	);
}
/********************************************************************************/
/*																				*/
/*	Check stick neutral.														*/
/*																				*/
/********************************************************************************/
static void
SetCurrentMode(short mode, short repeat, short blink)
{
	controller.curmode = mode;
	controller.repeat  = repeat;
	clockinfo.blink    = blink;
	if (repeat == 0)  Nacp_SeStart(NACP_CMOVE);			/* when move cursor		*/
}
/********************************************************************************/
/*																				*/
/*	Begin button process.														*/
/*																				*/
/********************************************************************************/
static void
BeginButtonProcess(void)
{
	if (controller.button & CONT_LEFT) {
		SetCurrentMode(MODE_CONT_LEFT, 0, 0);
		if (--clockinfo.cursor < 1)  clockinfo.cursor = 5;
	}
	else if (controller.button & CONT_RIGHT) {
		SetCurrentMode(MODE_CONT_RIGHT, 0, 0);
		if (++clockinfo.cursor > 5)  clockinfo.cursor = 1;
	}
	else if (controller.button & CONT_UP) {
		SetCurrentMode(MODE_CONT_UP, 20, 10);
		AddValue(&clockinfo);
	}
	else if (controller.button & CONT_DOWN) {
		SetCurrentMode(MODE_CONT_DOWN, 20, 10);
		SubValue(&clockinfo);
	}
	else if (controller.stickx < -32) {
		SetCurrentMode(MODE_STCK_LEFT, 0, 0);
		if (--clockinfo.cursor < 1)  clockinfo.cursor = 5;
	}
	else if (controller.stickx > 32) {
		SetCurrentMode(MODE_STCK_RIGHT, 0, 0);
		if (++clockinfo.cursor > 5)  clockinfo.cursor = 1;
	}
	else if (controller.sticky > 32) {
		SetCurrentMode(MODE_STCK_UP, 30, 10);
		AddValue(&clockinfo);
	}
	else if (controller.sticky < -32) {
		SetCurrentMode(MODE_STCK_DOWN, 30, 10);
		SubValue(&clockinfo);
	}
}
/********************************************************************************/
/*																				*/
/*	Repeat button process.														*/
/*																				*/
/********************************************************************************/
static void
RepeatButtonProcess(void)
{
	int reset = FALSE;

	switch (controller.curmode) {
		case MODE_CONT_LEFT : reset = !(controller.button & CONT_LEFT );			break;
		case MODE_CONT_RIGHT: reset = !(controller.button & CONT_RIGHT);			break;
		case MODE_CONT_UP   : reset = !(controller.button & CONT_UP	  );			break;
		case MODE_CONT_DOWN : reset = !(controller.button & CONT_DOWN );			break;
		default				: reset = controller.curmode && IsStickNeutral();		break;
	}
	if (reset) {
		controller.curmode = 0;
	} else {
		switch (controller.curmode) {
			case MODE_CONT_UP:
						if (--controller.repeat == 0) {
							SetCurrentMode(MODE_CONT_UP, 2, 10);
							AddValue(&clockinfo);
						}
						break;

			case MODE_CONT_DOWN:
						if (--controller.repeat == 0) {
							SetCurrentMode(MODE_CONT_DOWN, 2, 10);
							SubValue(&clockinfo);
						}
						break;

			case MODE_STCK_UP:
						if (controller.sticky  > 8) {
							if (--controller.repeat == 0) {
								SetCurrentMode(MODE_STCK_UP, (64-controller.sticky)/6 + 2, 10);
								AddValue(&clockinfo);
							}
						}
						break;

			case MODE_STCK_DOWN:
						if (controller.sticky < -8) {
							if (--controller.repeat == 0) {
								SetCurrentMode(MODE_STCK_DOWN, (64+controller.sticky)/6 + 2, 10);
								SubValue(&clockinfo);
							}
						}
						break;
		}
	}
}
/********************************************************************************/
/*																				*/
/*	Scan active controller (pressed start button)								*/
/*																				*/
/********************************************************************************/
static void
ScanActiveController(void)
{
	int cont;

	if (!IsClockWorking() || !IsDiskLoading()) {
		for (cont = 0; cont < MAXCONTROLLERS; cont++) {
			if (contStatus[cont].type == (CONT_JOYPORT|CONT_ABSOLUTE)) {
				if (contRdData[cont].button & CONT_A) {
					activePad = &contRdData[cont];
					counter	  = 30;
					process   = 1;
					break;
				}
			}
		}
	}
}
/********************************************************************************/
/*																				*/
/*	Check pressing start button													*/
/*																				*/
/********************************************************************************/
static void
CheckStartButton(void)
{
	if (activePad->button & CONT_A) {
		if (!IsClockWorking() || !IsDiskLoading()) {
			counter -= 1;
		}
		if (counter == 0) {
			ReadClock(&clockinfo);
			controller.button  = CONT_A;
			controller.curmode = 0;
			process			   = 2;
			Nacp_SeStart(NACP_OPEN);
		}
	}
	else {
		activePad = NULL;
		process	  = 0;
	}
}		
/********************************************************************************/
/*																				*/
/*	Do clock setting															*/
/*																				*/
/********************************************************************************/
static void
DoClockSetting(void)
{
	SetController();
	if (controller.trigger & (CONT_START|CONT_A|CONT_B)) {
		Nacp_SeStart(NACP_DECIDE);
		if (controller.trigger & (CONT_START|CONT_A)) {
			WriteClock(&clockinfo);
		}
		activePad = NULL;
		process	  = 0;
	}
	else {
		RepeatButtonProcess();
		if (controller.curmode == 0)  BeginButtonProcess();
	}
	DrawClockSetting(&clockinfo);
}
/********************************************************************************/
/*																				*/
/*	Clock setting main															*/
/*																				*/
/********************************************************************************/
extern int
SetClock(void)
{
	switch (process) {
		case 0: ScanActiveController();		break;
		case 1: CheckStartButton();			break;
		case 2: DoClockSetting();			break;
	}
	clockSetting = (process == 2);
	return(clockSetting);
}
/********************************************************************************/
/*																				*/
/*	Clock setting main															*/
/*																				*/
/********************************************************************************/
extern void
InitClock(void)
{
	process      = 0;
	counter	     = 0;
	clockSetting = FALSE;
	activePad    = NULL;
}
/********************************************************************************/
/*																				*/
/*	Display disk drive clock													*/
/*																				*/
/********************************************************************************/
extern void
DisplayClock(int alpha)
{
	ClockInfo clock;

	ReadClock(&clock);

	DrawClock(144, 212, 											/* for TEST */
		clock.month, clock.day   , clock.week,
		clock.hour , clock.minute, BcdToBin(diskClock.second), alpha
	);
}