Blockheads HC11 Code

/*----------------------------------------------------------------------------------------
                block1.c
                Blockheads shall rule the world!

                (c) 1998  Jason Ditmars // dope-a-delic
----------------------------------------------------------------------------------------*/

/* I/O defs:

PortA: 74hc154 enable (pin 2)
PortB: 0-7 single light outputs (PNPs switch light on when pin is H)
PortC: 0-7 single light outputs
PortD: 0-3 multiplexed outputs (4 to 16)
       4-5 single light outputs
PortE: unused

total of 34 outputs for 34 blockheads

*/

#include "mytimer.h"
#include "mystdlib.h"

void wait(int);
void pass_the_ball(int,int);
void binary_counter(int,int);
void blinkfuck(int);
void back_and_forth(void);
void blink_d(void);
void srand(unsigned int);
void countdown(void);
int rand(void); 
void main(void){
                int i;
                unsigned int jooky;
                hc11setup();
                PORTA = 0xFF;                                    /* disable 74hc154 */
                while(1==1){
                                if (jooky == 32000)
                                                jooky = 1;
                                srand(jooky);
                                jooky++;
                                back_and_forth();
                                pass_the_ball((int)30,500);
                                blink_d();
                                wait(100+(rand() % 300));
                                pass_the_ball((int)40,(1400+(rand() % 6000)));
                                if ((rand() % 3) == 1)
                                                binary_counter((int)18,1700);
                                pass_the_ball((int)20,2000);
                                wait(200);
                                pass_the_ball((int)50,(400+(rand() % 300)));
                                wait(400);
                                if ((rand() % 3) == 1)
                                                binary_counter((int)10,1300);
                                wait(1000);
                                pass_the_ball((int)17,(2500+(rand() % 5000)));
                                wait(100);
                                blink_d();
                                for (i=0;i<(3+(rand() % 12));i++)
                                                pass_the_ball((int)(5+(rand() % 30)),(200+(rand() % 3000)));
                                wait(500);
                                pass_the_ball((int)50,(140+(rand() % 200)));
                                wait(1000);
                                blink_d();
                                pass_the_ball((int)14,10000);
                                wait(200);
                                i=(rand() % 4);
                                if (i==3)
                                                countdown();
                }
}
 
void countdown(){
                int c;
                for(c=25;c>-50;c--)
                                blinkfuck(c);
}
 
void blinkfuck(int c){
                PORTB = 0;
                PORTC = 0xff;
                if (c>0)
                                wait(c*6);
                else
                                wait(4);
                PORTB = 0xff;
                PORTC = 0;
                if (c>0)
                                wait(c*5);
                else
                                wait(4);
}
 
void blink_d(){
                int i,j,k;
                j=(rand() % 20);
                k=(rand() % 9);
                PORTB = 1;
                for (i=0;i<k;i++)
                                PORTB<<=1;
                k = PORTB;
                PORTA = 0;                          /* enable 74154 */
                for (i=0;i<10;i++){
                                PORTD = j;
                                PORTB = k;
                                wait(20);
                                PORTD = 0;
                                PORTB = 0;
                                wait(20);
                }
                PORTA = 0xff;
} 

void back_and_forth(){
                int i;
                for (i=0;i<3;i++){
                                PORTB = 0xff;
                                wait(50);
                                PORTB = 0x00;
                                PORTC = 0xff;
                                wait(50);
                                PORTC = 0x00;
                }
}
 
void binary_counter(int speed,int duration){
                int i;
                RTICntr = 0;
                while(duration > RTICntr){
                                for(i=0;i<256;i++){
                                                PORTB = i;
                                                wait(speed);
                                }
                                for(i=0;i<256;i++){
                                                PORTC = i;
                                                wait(speed - 10);
                                }
                }
}
 
void pass_the_ball(int speed,int duration){
                int bulk,i;
                bulk = (rand() % 6);
                if (bulk > 4)
                                bulk = 1;
                RTICntr = 0;
                while(duration > RTICntr){                                        /* do it this long */
                                PORTD = 0x10;
                                wait(speed);
                                PORTD = 0x20;
                                wait(speed);                                      /* stragglers */
                                PORTD = 0;
                                PORTB = bulk;
                                while(PORTB != 0){
                                                wait(speed);
                                                i=(rand() % 16);
                                                if (i == 10){
                                                                PORTB = (rand() % 256);
                                                                wait((10 + (rand() % 400)));
                                                }
                                                PORTB<<=1;                        /* lft shift across B */
                                }
                                PORTC = bulk;                                     /* lft shift across C */
                                while(PORTC != 0){
                                                wait(speed);
                                                i=(rand() % 20);
                                                if (i == 10){
                                                                PORTC = (rand() % 256);
                                                                wait((10 + (rand() % 200)));
                                                }
                                                PORTC<<=1;
                                }
                                PORTA=0;                                          /* enable, ya! */
                                PORTD=0;
                                while(PORTD < 16){                                /* across 74154 */
                                                wait(speed);
                                                PORTD++;
                                }
                                PORTA=0xFF;                                       /* disable, woo! */
                }
}
 
void wait(int dur){
                long i = RTICntr;
                while(RTICntr < (dur+i));
}