//
// main.c
//
// DEFCON 14 Circuit Board Badge
//
// This file contains code for the Microchip PIC10F202/206 for CCS PCM
//
// Copyright (c) 2006 Grand Idea Studio, Inc. (http://www.grandideastudio.com)
//

#include "defcon.h"

#ORG 0x130, 0x16F
char const notice1[] = {"DEFCON 14 Badge v1.0 by Joe Grand (Kingpin)"};

#ORG 0x170, 0x1AF
char const notice2[] = {"Grand Idea Studio (www.grandideastudio.com)"};

#ORG 0x1B0, 0x1FF
char const notice3[] = {"If you can read this, you must be a hacker..."};


state_type state = SLEEP; // Default mode to start-up in
int state_change = 0;
long timer = 0;
char random_byte, random_done = FALSE;

void main (void)
{
	hardware_init();

    while(1)
	{
		timer += (get_timer0() / 16); // use a 16-bit counter to slow down our LED control

		if (!input(SW_MODE))
		{
			delay_ms(50); // poor man's debounce

			while (!input(SW_MODE)) // while the button is held down...
			{
				all_off();
    			state_change = 1;
			}
		}

		if (state_change) // if there is a change in state...
		{
			switch (state) // ...then change it
      		{
				case SLEEP:
					state = STEADY;
					break;
        		case STEADY:
          			state = BLINK;
          			break;
        		case BLINK:
          			state = ALT;
          			break;
        		case ALT:
					state = RANDOM;
				    random_byte = get_timer0(); // seed pseudo random number generator
					break;
				case RANDOM:
          			state = SLEEP;
          			break;
      		}

			state_change = 0;
		}

		switch (state)
        {
         	case STEADY:			// turn on LEDs
				all_on();
            	break;
          	case BLINK:				// blink LEDs
            	if (timer < 32768)
					all_on();
            	else
					all_off();
            	break;
          	case ALT:				// alternate LEDs
            	if (timer < 32768)
					led1_on();	
            	else
					led2_on();
            	break;
		  case RANDOM:				// pseudo-random sequence
				if ((timer * 2) < 32768)
				{
					if (random_done == FALSE)
					{
						switch (rand() & 0x03)
						{
							case 0:
								all_on();
								break;
							case 1:
								all_off();
								break;	
							case 2:
								led1_on();
								break;
							case 3:
								led2_on();
								break;
						}
						random_done = TRUE;
					}
				}
				else
					random_done = FALSE;
				break;
		  case SLEEP:				// sleepy time!
		  default:
				sleep();
        }
	}
}


void all_off (void)
{
	output_high(LED_1); // turn off LEDs
	output_high(LED_2);
}


void all_on (void)
{
	output_low(LED_1); // turn on LEDs
	output_low(LED_2);
}


void led1_on (void)
{
	output_low(LED_1); 
	output_high(LED_2);
}


void led2_on (void)
{
	output_high(LED_1); 
	output_low(LED_2);
}


char rand (void)	
{
	char sum;

	sum = 0;

	// This calculates parity on the selected bits (mask = 0xb4).
	if(random_byte & 0x80)
		sum = 1;

	if(random_byte & 0x20)
		sum ^= 1;

	if(random_byte & 0x10)
		sum ^= 1;

	if(random_byte & 0x04)
		sum ^= 1;

	random_byte <<= 1;
	random_byte |= sum;
	return(random_byte);
}


void hardware_init (void) // Configure hardware to a known state
{
#asm
	movlw	0x00	; enable internal pull-ups and wake-up on pin change
	option
#endasm

    // Setup timer for LED flashing
    setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
    set_timer0(0);

	setup_comparator(NC_NC_NC_NC); // disable comparator for 10F206 device
}
