Appendix
Appendix A: Cost Details
STK 500 (with Mega32): $15.00
White Board: $6.00
Power Supply: $5.00
B/W TV: $5.00
HM2007 Voice Recognition Chip: $10.00
Microphone: $.50
SRAM: sampled
Resistors: sampled
Capacitors: sampled
Power Regulator: sampled
Operational Amp: sampled
Copious amounts of wire: sampled
Total: $41.50
Creating a voice-input chess algorithm while working 100+ hours/wk: Priceless
Appendix B: List of Specific Tasks
Kon-Hyong Kim: backbone (controller) code implementation, including HM2007 and TV code
Tim Chung: hardware implementation, webpage creation, chip purchasing and research
Kevin Jin-Ho Ham: chess code implementation for piece control, chess rules, and verification
- All references have links in them within the webpage for your convenience.
Appendix C: Commented Code
//video gen
//D.5 is sync:1000 ohm + diode to 75 ohm resistor
//D.6 is video:330 ohm + diode to 75 ohm resistor
#pragma regalloc- //I allocate the registers myself
#pragma optsize- //optimize for speed
#include <Mega32.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <delay.h>
//cycles = 63.625 * 16 Note NTSC is 63.55
//but this line duration makes each frame exactly 1/60 sec
//which is nice for keeping a realtime clock
#define lineTime 1018
#define begin {
#define end }
#define ScreenTop 30
#define ScreenBot 230
#define COM_RECOG 0b00000001
#define COM_TRAIN 0b00000010
#define COM_RESULT 0b00000100
#define COM_UPLOAD 0b00000101
#define COM_DOWNLOAD 0b00000110
#define COM_RESET 0b00000111
#define ST_GETV 0b11111001 // 01
#define ST_GETC 0b11111010 // 10
#define ST_FIRST 0b11111011 // 11
#define ST_SECOND 0b11111000 // 00
#define CTR_GETS 0b000000010; // 010
#define CTR_SEND 0b000000100; // 100
#define CTR_GETO 0b000000011; // 011
#define CTR_000 0b000000000;
#define CTR_101 0b000000101;
#define CTR_001 0b000000001;
#define BISHOP 0
#define KING 1
#define KNIGHT 2
#define PAWN 3
#define QUEEN 4
#define ROOK 5
#define TAKEBACK 6
#define CANCEL 8
//NOTE that v1 to v8 and i must be in registers!
register char v1 @4;
register char v2 @5;
register char v3 @6;
register char v4 @7;
register char v5 @8;
register char v6 @9;
register char v7 @10;
register char v8 @11;
register int i @12;
#pragma regalloc+
char syncON, syncOFF;
int LineCount;
int j, k, n;
char screen[1600], ts[10];
unsigned char word, score, error;
signed char type, x1, y1, x2, y2;
signed char last_type, last_x1, last_y1, last_x2, last_y2, last_victim;
unsigned char board[8][8], ;
unsigned char moveDone, ready, grid, init;
unsigned char invalid, promote, back;
unsigned char turn; // black = 0 white = 1
int xsign, ysign;
//Point plot lookup table
//One bit masks
flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
//define some character bitmaps
//5x7 characters
flash char bitmap[38][7]={
//0 bishop_bb
0b00100100,
0b01000010,
0b01011010,
0b01000010,
0b00100100,
0b01000010,
0b11111111,
//1 bishop_wb
0b11100111,
0b11000011,
0b11011011,
0b11000011,
0b11100111,
0b10000001,
0b00000000,
//2 bishop_bw
0b00011000,
0b00111100,
0b00100100,
0b00111100,
0b00011000,
0b01111110,
0b11111111,
//3 bishop_ww
0b11011011,
0b10111101,
0b10100101,
0b10111101,
0b11011011,
0b10111101,
0b00000000,
//4 king_bb
0b00011000,
0b01011010,
0b10100101,
0b01000010,
0b00100100,
0b00011000,
0b01100110,
//5 king_wb
0b11100111,
0b10100101,
0b00000000,
0b10000001,
0b11000011,
0b11100111,
0b10000001,
//6 king_bw
0b00011000,
0b01011010,
0b11111111,
0b01111110,
0b00111100,
0b00011000,
0b01111110,
//7 king_ww
0b11100111,
0b10100101,
0b01011010,
0b10111101,
0b11011011,
0b11100111,
0b10011001,
//8 knight_bb
0b00101000,
0b00111100,
0b01000100,
0b10010010,
0b01100010,
0b00010001,
0b00111111,
//9 knight_wb
0b11010111,
0b11000011,
0b10000011,
0b00010001,
0b10110001,
0b11100000,
0b11000000,
//10 knight_bw
0b00101000,
0b00111100,
0b01111100,
0b11101110,
0b01001110,
0b00011111,
0b00111111,
//11 knight_ww
0b11010111,
0b11000011,
0b10111011,
0b01101101,
0b10011101,
0b11101110,
0b11000000,
//12 pawn_bb
0b00000000,
0b00000000,
0b00011000,
0b00100100,
0b00011000,
0b00100100,
0b01000010,
//13 pawn_wb
0b11111111,
0b11111111,
0b11100111,
0b11000011,
0b11100111,
0b11000011,
0b10000001,
//14 pawn_bw
0b00000000,
0b00000000,
0b00011000,
0b00111100,
0b00011000,
0b00111100,
0b01111110,
//15 pawn_ww
0b11111111,
0b11111111,
0b11100111,
0b11011011,
0b11100111,
0b11011011,
0b10111101,
//16 queen_bb
0b10100101,
0b10100101,
0b10100101,
0b01011010,
0b01000010,
0b00100100,
0b11000011,
//17 queen_wb
0b01011010,
0b01011010,
0b01011010,
0b10000001,
0b10000001,
0b11000011,
0b00000000,
//18 queen_bw
0b10100101,
0b10100101,
0b10100101,
0b01111110,
0b01111110,
0b00111100,
0b11111111,
//19 queen_ww
0b01011010,
0b01011010,
0b01011010,
0b10100101,
0b10111101,
0b11011011,
0b00111100,
//20 rook_bb
0b10011001,
0b10011001,
0b11100111,
0b00100100,
0b00100100,
0b00100100,
0b11000011,
//21 rook_wb
0b01100110,
0b01100110,
0b00000000,
0b00000000,
0b11000011,
0b11000011,
0b00000000,
//22 rook_bw
0b10011001,
0b10011001,
0b11111111,
0b11111111,
0b00111100,
0b00111100,
0b11111111,
//23 rook_ww
0b01100110,
0b01100110,
0b00011000,
0b11011011,
0b11011011,
0b11011011,
0b00111100,
//24 black
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
//25 white
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
//B
0b11110000,
0b10001000,
0b10001000,
0b11110000,
0b10001000,
0b10001000,
0b11110000,
//R
0b11110000,
0b10001000,
0b10001000,
0b11110000,
0b10100000,
0b10010000,
0b10001000,
//S
0b01111000,
0b10000000,
0b10000000,
0b01110000,
0b00001000,
0b00001000,
0b11110000,
//T
0b11111000,
0b00100000,
0b00100000,
0b00100000,
0b00100000,
0b00100000,
0b00100000,
//U
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b01110000,
//V
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b01010000,
0b00100000,
//W
0b10001000,
0b10001000,
0b10001000,
0b10101000,
0b10101000,
0b10101000,
0b01010000,
//X
0b10001000,
0b10001000,
0b01010000,
0b00100000,
0b01010000,
0b10001000,
0b10001000,
//Y
0b10001000,
0b10001000,
0b10001000,
0b01010000,
0b00100000,
0b00100000,
0b00100000,
//Z
0b11111000,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
0b11111000,
//figure1
0b01110000,
0b00100000,
0b01110000,
0b10101000,
0b00100000,
0b01010000,
0b10001000,
//figure2
0b01110000,
0b10101000,
0b01110000,
0b00100000,
0b00100000,
0b01010000,
0b10001000};
//================================
//3x5 font numbers, then letters
//packed two per definition for fast
//copy to the screen at x-position divisible by 4
flash char smallbitmap[39][5]={
//0
0b11101110,
0b10101010,
0b10101010,
0b10101010,
0b11101110,
//1
0b01000100,
0b11001100,
0b01000100,
0b01000100,
0b11101110,
//2
0b11101110,
0b00100010,
0b11101110,
0b10001000,
0b11101110,
//3
0b11101110,
0b00100010,
0b11101110,
0b00100010,
0b11101110,
//4
0b10101010,
0b10101010,
0b11101110,
0b00100010,
0b00100010,
//5
0b11101110,
0b10001000,
0b11101110,
0b00100010,
0b11101110,
//6
0b11001100,
0b10001000,
0b11101110,
0b10101010,
0b11101110,
//7
0b11101110,
0b00100010,
0b01000100,
0b10001000,
0b10001000,
//8
0b11101110,
0b10101010,
0b11101110,
0b10101010,
0b11101110,
//9
0b11101110,
0b10101010,
0b11101110,
0b00100010,
0b01100110,
//:
0b00000000,
0b01000100,
0b00000000,
0b01000100,
0b00000000,
//=
0b00000000,
0b11101110,
0b00000000,
0b11101110,
0b00000000,
//blank
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
//A
0b11101110,
0b10101010,
0b11101110,
0b10101010,
0b10101010,
//B
0b11001100,
0b10101010,
0b11101110,
0b10101010,
0b11001100,
//C
0b11101110,
0b10001000,
0b10001000,
0b10001000,
0b11101110,
//D
0b11001100,
0b10101010,
0b10101010,
0b10101010,
0b11001100,
//E
0b11101110,
0b10001000,
0b11101110,
0b10001000,
0b11101110,
//F
0b11101110,
0b10001000,
0b11101110,
0b10001000,
0b10001000,
//G
0b11101110,
0b10001000,
0b10001000,
0b10101010,
0b11101110,
//H
0b10101010,
0b10101010,
0b11101110,
0b10101010,
0b10101010,
//I
0b11101110,
0b01000100,
0b01000100,
0b01000100,
0b11101110,
//J
0b00100010,
0b00100010,
0b00100010,
0b10101010,
0b11101110,
//K
0b10001000,
0b10101010,
0b11001100,
0b11001100,
0b10101010,
//L
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b11101110,
//M
0b10101010,
0b11101110,
0b11101110,
0b10101010,
0b10101010,
//N
0b00000000,
0b11001100,
0b10101010,
0b10101010,
0b10101010,
//O
0b01000100,
0b10101010,
0b10101010,
0b10101010,
0b01000100,
//P
0b11101110,
0b10101010,
0b11101110,
0b10001000,
0b10001000,
//Q
0b01000100,
0b10101010,
0b10101010,
0b11101110,
0b01100110,
//R
0b11101110,
0b10101010,
0b11001100,
0b11101110,
0b10101010,
//S
0b11101110,
0b10001000,
0b11101110,
0b00100010,
0b11101110,
//T
0b11101110,
0b01000100,
0b01000100,
0b01000100,
0b01000100,
//U
0b10101010,
0b10101010,
0b10101010,
0b10101010,
0b11101110,
//V
0b10101010,
0b10101010,
0b10101010,
0b10101010,
0b01000100,
//W
0b10101010,
0b10101010,
0b11101110,
0b11101110,
0b10101010,
//X
0b00000000,
0b10101010,
0b01000100,
0b01000100,
0b10101010,
//Y
0b10101010,
0b10101010,
0b01000100,
0b01000100,
0b01000100,
//Z
0b11101110,
0b00100010,
0b01000100,
0b10001000,
0b11101110
};
//==================================
//This is the sync generator and raster generator. It MUST be entered from
//sleep mode to get accurate timing of the sync pulses
#pragma warn-
interrupt [TIM1_COMPA] void t1_cmpA(void)
begin
//start the Horizontal sync pulse
PORTD = syncON;
//update the curent scanline number
LineCount ++ ;
//begin inverted (Vertical) synch after line 247
if (LineCount==248)
begin
syncON = 0b00100000;
syncOFF = 0;
end
//back to regular sync after line 250
if (LineCount==251)
begin
syncON = 0;
syncOFF = 0b00100000;
end
//start new frame after line 262
if (LineCount==263)
begin
LineCount = 1;
end
delay_us(2); //adjust to make 5 us pulses
//end sync pulse
PORTD = syncOFF;
if (LineCount<ScreenBot && LineCount>=ScreenTop)
begin
//compute byte index for beginning of the next line
//left-shift 4 would be individual lines
// <<3 means line-double the pixels
//The 0xfff8 truncates the odd line bit
//i=(LineCount-ScreenTop)<<3 & 0xfff8; //
#asm
push r16
lds r12, _LineCount
lds r13, _Linecount+1
ldi r16, 30
sub r12, r16
ldi r16,0
sbc r13, r16
lsl r12
rol r13
lsl r12
rol r13
lsl r12
rol r13
mov r16,r12
andi r16,0xf0
mov r12,r16
pop r16
#endasm
//load 16 registers with screen info
#asm
push r14
push r15
push r16
push r17
push r18
push r19
push r26
push r27
ldi r26,low(_screen) ;base address of screen
ldi r27,high(_screen)
add r26,r12 ;offset into screen (add i)
adc r27,r13
ld r4,x+ ;load 16 registers and inc pointer
ld r5,x+
ld r6,x+
ld r7,x+
ld r8,x+
ld r9,x+
ld r10,x+
ld r11,x+
ld r12,x+
ld r13,x+
ld r14,x+
ld r15,x+
ld r16,x+
ld r17,x+
ld r18,x+
ld r19,x
pop r27
pop r26
#endasm
delay_us(4); //adjust to center image on screen
//blast 16 bytes to the screen
#asm
;but first a macro to make the code shorter
;the macro takes a register number as a parameter
;and dumps its bits serially to portD.6
;the nop can be eliminated to make the display narrower
.macro videobits ;regnum
BST @0,7
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,6
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,5
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,4
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,3
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,2
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,1
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,0
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
.endm
videobits r4 ;video line -- byte 1
videobits r5 ;byte 2
videobits r6 ;byte 3
videobits r7 ;byte 4
videobits r8 ;byte 5
videobits r9 ;byte 6
videobits r10 ;byte 7
videobits r11 ;byte 8
videobits r12 ;byte 9
videobits r13 ;byte 10
videobits r14 ;byte 11
videobits r15 ;byte 12
videobits r16 ;byte 13
videobits r17 ;byte 14
videobits r18 ;byte 15
videobits r19 ;byte 16
clt ;clear video after the last pixel on the line
IN R30,0x12
BLD R30,6
OUT 0x12,R30
pop r19
pop r18
pop r17
pop r16
pop r15
pop r14
#endasm
end
end
#pragma warn+
//==================================
//plot one point
//at x,y with color 1=white 0=black 2=invert
#pragma warn-
void video_pt(char x, char y, char c)
begin
#asm
; i=(x>>3) + ((int)y<<4) ; the byte with the pixel in it
push r16
ldd r30,y+2 ;get x
lsr r30
lsr r30
lsr r30 ;divide x by 8
ldd r12,y+1 ;get y
lsl r12 ;mult y by 16
clr r13
lsl r12
rol r13
lsl r12
rol r13
lsl r12
rol r13
add r12, r30 ;add in x/8
;v2 = screen[i]; r5
;v3 = pos[x & 7]; r6
;v4 = c r7
ldi r30,low(_screen)
ldi r31,high(_screen)
add r30, r12
adc r31, r13
ld r5,Z ;get screen byte
ldd r26,y+2 ;get x
ldi r27,0
andi r26,0x07 ;form x & 7
ldi r30,low(_pos*2)
ldi r31,high(_pos*2)
add r30,r26
adc r31,r27
lpm r6,Z
ld r16,y ;get c
;if (v4==1) screen[i] = v2 | v3 ;
;if (v4==0) screen[i] = v2 & ~v3;
;if (v4==2) screen[i] = v2 ^ v3 ;
cpi r16,1
brne tst0
or r5,r6
tst0:
cpi r16,0
brne tst2
com r6
and r5,r6
tst2:
cpi r16,2
brne writescrn
eor r5,r6
writescrn:
ldi r30,low(_screen)
ldi r31,high(_screen)
add r30, r12
adc r31, r13
st Z, r5 ;write the byte back to the screen
pop r16
#endasm
end
#pragma warn+
//===================================
// chess movement
void makeMove() {
invalid = 0;
promote = 0;
back = 0;
//wrong side
if ((type << 1) + turn != board[x1][y1]) invalid = 1;
//back command
if (type == TAKEBACK) {
back = 1;
invalid = 1;
}
//no move or same side
if ( ((x1 == x2) && (y1 == y2)) || !((board[x1][y1] < 12) && (board[x1][y1] %2 == turn)) ) {
invalid = 1;
}
else begin
//checks ends
if (x1 < x2) xsign = 1;
else if (x1 > x2) xsign = -1;
else xsign = 0;
if (y1 < y2) ysign = 1;
else if (y1 > y2) ysign = -1;
else ysign = 0;
//customized for types
switch (type) begin
case BISHOP:
if (abs(x2-x1)!=abs(y2-y1)) invalid = 1;
else begin
k = y1 + ysign;
for (j = x1 + xsign; j != x2; j += xsign) begin
if (board[j][k] != 12) invalid = 1;
k += ysign;
end
end
break;
case KING:
if ((abs(x2-x1) > 1) || (abs(y2-y1) > 1)) invalid = 1;
break;
case KNIGHT:
if (abs(x2-x1) == 1) begin
if (abs(y2-y1) != 2) invalid = 1;
end
else if (abs(x2-x1) == 2) begin
if (abs(y2-y1) != 1) invalid = 1;
end
else invalid = 1;
break;
case QUEEN:
if (abs(x2-x1) == abs(y2-y1)) begin
k = y1 + ysign;
for (j = x1 + xsign; j != x2; j += xsign) begin
if (board[j][k] != 12) invalid = 1;
k += ysign;
end
end
if (invalid == 1) begin
invalid = 0;
if (x2 == x1) begin
for (k = y1 + ysign; k != y2; k += ysign) begin
if (board[x1][k] != 12) invalid = 1;
end
end
else if (y2 == y1) begin
for (j = x1 + xsign; j != x2; j += xsign) begin
if (board[j][y1] != 12) invalid = 1;
end
end
else invalid = 1;
end
break;
case ROOK:
if (x2 == x1) begin
for (k = y1 + ysign; k != y2; k += ysign) begin
if (board[x1][k] != 12) invalid = 1;
end
end
else if (y2 == y1) begin
for (j = x1 + xsign; j != x2; j += xsign) begin
if (board[j][y1] != 12) invalid = 1;
end
end
else invalid = 1;
break;
case PAWN:
if (turn == 0) begin
if ((abs(x2-x1) == 1) && (y2-y1 == 1)) begin
if (board[x2][y2] == 12) {
invalid = 1;
}
end
else begin
if (x2 != x1) {
invalid = 1;
}
if (y1 == 1) begin
if ((y2 > 3) || (y2 < 2)) {
invalid = 1;
}
end
else if (y2 - y1 != 1) {
invalid = 1;
}
end
if (y2 == 7) {
promote = 1;
}
end
else begin
if ((abs(x2-x1) == 1) && (y1-y2 == 1)) begin
if (board[x2][y2] == 12) invalid = 1;
end
else begin
if (x2 != x1) invalid = 1;
if (y1 == 6) begin
if (y2 > 5 || y2 < 4) invalid = 1;
end
else if (y1 - y2 != 1) invalid = 1;
end
if (y2 == 0) promote = 1;
end
break;
end
if (invalid == 0) begin
if ((board[x2][y2]%2 == turn) && !(board[x2][y2] == 12)) invalid = 1;
if (error == 1) invalid = 1;
end
end
//saves last part or executes back function
if (invalid == 0) begin
last_victim = board[x2][y2];
last_type = board[x1][y1];
last_x1 = x1;
last_x2 = x2;
last_y1 = y1;
last_y2 = y2;
if (promote == 1) begin
board[x2][y2] = 8 + turn;
board[x1][y1] = 12;
end
else begin
board[x2][y2] = board[x1][y1];
board[x1][y1] = 12;
end
ready = 1;
turn = 1 - turn;
end
else if (back == 1) begin
board[last_x1][last_y1] = last_type;
board[last_x2][last_y2] = last_victim;
ready = 1;
turn = 1 - turn;
end
}
//==================================
// put a big character on the screen
// c is index into bitmap
void video_putchar(char x, char y, char c)
begin
v7 = x;
for (v6=0;v6<7;v6++)
begin
v1 = bitmap[c][v6];
v8 = y+v6;
video_pt(v7, v8, (v1 & 0x80)==0x80);
video_pt(v7+1, v8, (v1 & 0x40)==0x40);
video_pt(v7+2, v8, (v1 & 0x20)==0x20);
video_pt(v7+3, v8, (v1 & 0x10)==0x10);
video_pt(v7+4, v8, (v1 & 0x08)==0x08);
video_pt(v7+5, v8, (v1 & 0x04)==0x04);
video_pt(v7+6, v8, (v1 & 0x02)==0x02);
video_pt(v7+7, v8, (v1 & 0x01)==0x01);
end
end
//==================================
// put a string of big characters on the screen
void video_puts(char x, char y, char *str)
begin
char i ;
for (i=0; str[i]!=0; i++)
begin
if (str[i]>=0x30 && str[i]<=0x3a)
video_putchar(x,y,str[i]-0x30);
else video_putchar(x,y,str[i]-0x40+9);
x = x+6;
end
end
//==================================
// put a small character on the screen
// x-cood must be on divisible by 4
// c is index into bitmap
void video_smallchar(char x, char y, char c)
begin
char mask;
i=((int)x>>3) + ((int)y<<4) ;
if (x == (x & 0xf8)) mask = 0x0f; //f8
else mask = 0xf0;
screen[i] = (screen[i] & mask) | (smallbitmap[c][0] & ~mask);
screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask);
screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask);
screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask);
screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask);
end
//==================================
// put a string of small characters on the screen
// x-cood must be on divisible by 4
void video_putsmalls(char x, char y, char *str)
begin
char i ;
for (i=0; str[i]!=0; i++)
begin
if (str[i]>=0x30 && str[i]<=0x3a)
video_smallchar(x,y,str[i]-0x30);
else video_smallchar(x,y,str[i]-0x40+12);
x = x+4;
end
end
//==================================
//plot a line
//at x1,y1 to x2,y2 with color 1=white 0=black 2=invert
//NOTE: this function requires signed chars
//Code is from David Rodgers,
//"Procedural Elements of Computer Graphics",1985
void video_line(char x1, char y1, char x2, char y2, char c)
begin
int e;
signed char dx,dy,j, temp;
signed char s1,s2, xchange;
signed char x,y;
x = x1;
y = y1;
dx = cabs(x2-x1);
dy = cabs(y2-y1);
s1 = csign(x2-x1);
s2 = csign(y2-y1);
xchange = 0;
if (dy>dx)
begin
temp = dx;
dx = dy;
dy = temp;
xchange = 1;
end
e = ((int)dy<<1) - dx;
for (j=0; j<=dx; j++)
begin
video_pt(x,y,c) ;
if (e>=0)
begin
if (xchange==1) x = x + s1;
else y = y + s2;
e = e - ((int)dx<<1);
end
if (xchange==1) y = y + s2;
else x = x + s1;
e = e + ((int)dy<<1);
end
end
//==================================
//return the value of one point
//at x,y with color nonzero=white 0=black
char video_set(char x, char y)
begin
//The following construction
//detects exactly one bit at the x,y location
i=((int)x>>3) + ((int)y<<4) ;
return ( screen[i] & 1<<(7-(x & 0x7)));
end
//===================================
// communication functions for HM2007
//runs recognition state machine
void recog() {
PORTC = CTR_GETS;
printf("recog start\r\n");
while(PINB != ST_GETC);
printf(" com\r\n");
PORTC = CTR_SEND;
DDRB = 0xff;
PORTB = COM_RECOG;
PORTC = CTR_GETS;
DDRB = 0x00;
while(PINB != ST_GETV);
printf(" voice\r\n");
PINA = ~PINB;
}
//runs result state machines
void result() {
PORTC = CTR_GETS;
while (PINB != ST_GETC);
printf("result start\r\n");
printf(" com\r\n");
PORTC = CTR_SEND;
DDRB = 0xff;
PORTB = COM_RESULT;
PORTC = CTR_GETS;
DDRB = 0x00;
printf(" word\r\n");
while (PINB != ST_FIRST);
printf(" first\r\n");
PORTC = CTR_GETO;
delay_us(5);
word = 0x0f & PINB;
printf(" %X -> %X\r\n", PINB, word);
PORTC = CTR_101; // transition
PORTC = CTR_001;
PORTC = CTR_000;
PORTC = CTR_GETS;
while (PINB != ST_SECOND);
printf(" second\r\n");
PORTC = CTR_GETO;
delay_us(5);
word = word | (0x30 & (PINB << 4));
printf(" %X -> %X\r\n", PINB, word);
PORTC = CTR_101; // transition
PORTC = CTR_001;
PORTC = CTR_000;
PORTC = CTR_GETS;
printf("score\r\n");
while (PINB != ST_FIRST);
printf(" first\r\n");
PORTC = CTR_GETO;
delay_us(5);
score = 0x0f & PINB;
printf(" %X -> %X\r\n", PINB, score);
PORTC = CTR_101; // transition
PORTC = CTR_001;
PORTC = CTR_000;
PORTC = CTR_GETS;
while (PINB != ST_SECOND);
printf(" second\r\n");
PORTC = CTR_GETO;
delay_us(5);
score = score | (0xf0 & (PINB << 4));
printf(" %X -> %X\r\n", PINB, score);
PORTC = CTR_101; // transition
PORTC = CTR_001;
PORTC = CTR_000;
PORTC = CTR_GETS;
}
//runs train statemachine
void train() {
PORTC = CTR_GETS;
while (PINB != ST_GETC);
PORTC = CTR_SEND;
DDRB = 0xff;
PORTB = COM_TRAIN;
printf(" com\r\n");
PORTC = CTR_GETS;
DDRB = 0x00;
while (PINB != ST_FIRST);
PORTC = CTR_SEND;
DDRB = 0xff;
PORTB = word & 0x0f;
printf(" first\r\n");
PORTC = CTR_GETS;
DDRB = 0x00;
while (PINB != ST_SECOND);
PORTC = CTR_SEND;
DDRB = 0xff;
PORTB = (word >> 4) & 0x03;
printf(" second\r\n");
PORTC = CTR_GETS;
DDRB = 0x00;
printf(" voice\r\n");
while(PINB != ST_GETC);
printf("done %d\r\n", k);
}
//runs reset function
void reset() {
PORTC = CTR_GETS;
while (PINB != ST_GETC);
PORTC = CTR_SEND;
DDRB = 0xff;
PORTB = COM_RESET;
PORTC = CTR_SEND;
DDRB = 0x00;
}
//initializes function
void initialize() {
printf("train %d\r\n", k);
word = k;
PORTA = ~word;
train();
if (k == 9) {
printf("init end\r\n");
PORTA = ~0x55;
}
}
//receives commands from HM2007
void getMove() {
do {
error = 0;
recog();
result();
if (score < 0x80) type = word - 1;
else error = 1;
} while (error == 1);
printf(" got type : %X\r\n", type + 1);
if (type == CANCEL) {
printf("cancelled\r\n");
return;
}
do {
error = 0;
recog();
result();
if (score < 0x80) x1 = word - 1;
else error = 1;
} while (error == 1);
printf(" got x1: %X\r\n", x1 + 1);
if (x1 == CANCEL) {
printf("cancelled\r\n");
return;
}
do {
error = 0;
recog();
result();
if (score < 0x80) y1 = word - 1;
else error = 1;
} while (error == 1);
printf(" got y1 : %X\r\n", y1 + 1);
if (y1 == CANCEL) {
printf("cancelled\r\n");
return;
}
do {
error = 0;
recog();
result();
if (score < 0x80) x2 = word - 1;
else error = 1;
} while (error == 1);
printf(" got x2 : %X\r\n", x2 + 1);
if (x2 == CANCEL) {
printf("cancelled\r\n");
return;
}
do {
error = 0;
recog();
result();
if (score < 0x80) y2 = word - 1;
else error = 1;
} while (error == 1);
printf(" got y2 : %X\r\n", y2 + 1);
if (y2 == CANCEL) {
printf("cancelled\r\n");
return;
}
makeMove();
printf(" %X: (%X, %X) -> (%X, %X)\r\n", type, x1, y1, x2, y2);
printf(" invalid : %d\r\n", invalid);
}
//==================================
// set up the ports and timers
void main(void)
{
//init timer 1 to generate sync
OCR1A = lineTime; //One NTSC line
TCCR1B = 9; //full speed; clear-on-match
TCCR1A = 0x00; //turn off pwm and oc lines
TIMSK = 0x10; //enable interrupt T1 cmp
//init ports
DDRA = 0xff;
PORTA = 0xaa;
DDRB = 0x00;
DDRC = 0xff;
PORTC = CTR_GETS;
DDRD = 0xf0; //video out and switches
//D.5 is sync:1000 ohm + diode to 75 ohm resistor
//D.6 is video:330 ohm + diode to 75 ohm resistor
//initialize synch constants
LineCount = 1;
syncON = 0b00000000;
syncOFF = 0b00100000;
//lines
#define width 126
for (j = 0; j < 12; j++) {
video_line(j+4,4,j+4,99,1);
video_line(j+28,4,j+28,99,1);
video_line(j+52,4,j+52,99,1);
video_line(j+76,4,j+76,99,1);
}
for (j = 0; j < 12; j++) {
video_line(4,j+4,99,j+4,2);
video_line(4,j+28,99,j+28,2);
video_line(4,j+52,99,j+52,2);
video_line(4,j+76,99,j+76,2);
}
//draws line between board and information
video_line(102,0,102,99,1);
//1,2,3,4,5,6,7,8
video_smallchar(0, 8, 1);
video_smallchar(0, 20, 2);
video_smallchar(0, 32, 3);
video_smallchar(0, 44, 4);
video_smallchar(0, 56, 5);
video_smallchar(0, 68, 6);
video_smallchar(0, 80, 7);
video_smallchar(0, 92, 8);
//A
video_pt(8,0,1);
video_pt(9,0,1);
video_pt(10,0,1);
video_pt(11,0,1);
video_pt(12,0,1);
video_pt(8,1,1);
video_pt(8,2,1);
video_pt(9,2,1);
video_pt(10,2,1);
video_pt(11,2,1);
video_pt(12,2,1);
video_pt(10,1,1);
//B
video_pt(21,0,1);
video_pt(22,0,1);
video_pt(23,0,1);
video_pt(20,1,1);
video_pt(22,1,1);
video_pt(24,1,1);
video_pt(20,2,1);
video_pt(21,2,1);
video_pt(22,2,1);
video_pt(23,2,1);
video_pt(24,2,1);
//C
video_pt(32,0,1);
video_pt(36,0,1);
video_pt(32,1,1);
video_pt(36,1,1);
video_pt(32,2,1);
video_pt(33,2,1);
video_pt(34,2,1);
video_pt(35,2,1);
video_pt(36,2,1);
//D
video_pt(45,0,1);
video_pt(46,0,1);
video_pt(47,0,1);
video_pt(44,1,1);
video_pt(48,1,1);
video_pt(44,2,1);
video_pt(45,2,1);
video_pt(46,2,1);
video_pt(47,2,1);
video_pt(48,2,1);
//E
video_pt(56,0,1);
video_pt(58,0,1);
video_pt(60,0,1);
video_pt(56,1,1);
video_pt(58,1,1);
video_pt(60,1,1);
video_pt(56,2,1);
video_pt(57,2,1);
video_pt(58,2,1);
video_pt(59,2,1);
video_pt(60,2,1);
//F
video_pt(68,0,1);
video_pt(70,0,1);
video_pt(68,1,1);
video_pt(70,1,1);
video_pt(68,2,1);
video_pt(69,2,1);
video_pt(70,2,1);
video_pt(71,2,1);
video_pt(72,2,1);
//G
video_pt(80,0,1);
video_pt(83,0,1);
video_pt(84,0,1);
video_pt(80,1,1);
video_pt(84,1,1);
video_pt(80,2,1);
video_pt(81,2,1);
video_pt(82,2,1);
video_pt(83,2,1);
video_pt(84,2,1);
//H
video_pt(92,0,1);
video_pt(93,0,1);
video_pt(94,0,1);
video_pt(95,0,1);
video_pt(96,0,1);
video_pt(94,1,1);
video_pt(92,2,1);
video_pt(93,2,1);
video_pt(94,2,1);
video_pt(95,2,1);
video_pt(96,2,1);
for (j=0; j<8; j++) begin
board[j][1] = 6; //black pawn
board[j][2] = 12; // blank
board[j][3] = 12; // blank
board[j][4] = 12; // blank
board[j][5] = 12; // blank
board[j][6] = 7; //white pawn
end
for (j=0; j<2; j++) begin
board[0][j*7] = 10 + j;
board[1][j*7] = 4 + j;
board[2][j*7] = j;
board[3][j*7] = 8 + j;
board[4][j*7] = 2 + j;
board[5][j*7] = j;
board[6][j*7] = 4 + j;
board[7][j*7] = 10 + j;
end
video_smallchar(104, 2, 13);
video_smallchar(108, 2, 15);
video_smallchar(112, 2, 32);
video_smallchar(116, 2, 21);
video_smallchar(120, 2, 27);
video_smallchar(124, 2, 26);
video_smallchar(104, 24, 32);
video_smallchar(108, 24, 33);
video_smallchar(112, 24, 30);
video_smallchar(116, 24, 26);
video_putchar(108, 30, 26);
//bishop
video_smallchar(104, 44, 1);
video_smallchar(108, 44, 10);
video_smallchar(112, 44, 14);
video_smallchar(116, 44, 31);
video_smallchar(120, 44, 20);
video_smallchar(124, 44, 28);
//king
video_smallchar(104, 50, 2);
video_smallchar(108, 50, 10);
video_smallchar(112, 50, 23);
video_smallchar(116, 50, 21);
video_smallchar(120, 50, 26);
video_smallchar(124, 50, 19);
//knight
video_smallchar(104, 56, 3);
video_smallchar(108, 56, 10);
video_smallchar(112, 56, 23);
video_smallchar(116, 56, 26);
video_smallchar(120, 56, 19);
video_smallchar(124, 56, 32);
//pawn
video_smallchar(104, 62, 4);
video_smallchar(108, 62, 10);
video_smallchar(112, 62, 28);
video_smallchar(116, 62, 13);
video_smallchar(120, 62, 35);
video_smallchar(124, 62, 26);
//queen
video_smallchar(104, 68, 5);
video_smallchar(108, 68, 10);
video_smallchar(112, 68, 29);
video_smallchar(116, 68, 33);
video_smallchar(120, 68, 17);
video_smallchar(124, 68, 26);
//rook
video_smallchar(104, 74, 6);
video_smallchar(108, 74, 10);
video_smallchar(112, 74, 30);
video_smallchar(116, 74, 27);
video_smallchar(120, 74, 27);
video_smallchar(124, 74, 23);
//takeback
video_smallchar(104, 80, 7);
video_smallchar(108, 80, 10);
video_smallchar(112, 80, 14);
video_smallchar(116, 80, 13);
video_smallchar(120, 80, 15);
video_smallchar(124, 80, 23);
//cancel
video_smallchar(104, 86, 9);
video_smallchar(108, 86, 10);
video_smallchar(112, 86, 15);
video_smallchar(116, 86, 26);
video_smallchar(120, 86, 15);
video_smallchar(124, 86, 24);
init = 1;
k = 1;
n = 1;
moveDone = 1;
ready = 1;
error = 0;
//serial setup for debugging using printf, etc.
UCSRB = 0x18 ;
UBRRL = 103 ;
printf("\r\nStarting..\r\n");
PORTC = CTR_GETS;
while (PINB != ST_GETC);
printf("Resetting mem\r\n");
reset();
printf("\r\nInit Start\r\n");
////////////////////////////////////////////////////////////
/*
type = PAWN;
x1 = 1;
y1 = 1;
x2 = 1;
y2 = 3;
makeMove();
printf(" %X: (%X, %X) -> (%X, %X)\r\n", type, x1, y1, x2, y2);
printf(" iv : %d error : %d turn : %d\r\n", invalid, error, turn);
type = TAKEBACK;
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
makeMove();
printf(" %X: (%X, %X) -> (%X, %X)\r\n", type, x1, y1, x2, y2);
printf("last %X: (%X, %X) -> (%X, %X)\r\n", last_type, last_x1, last_y1, last_x2, last_y2);
printf(" iv : %d error : %d turn : %d\r\n", invalid, error, turn);
printf(" tb : %d\r\n", back);
*/
//////////////////////////////////////////////////////////
//enable sleep mode
MCUCR = 0b10000000;
#asm ("sei");
//The following loop executes once/video line during lines
//1-230, then does all of the frame-end processing
while(1)
begin
//stall here until next line starts
//sleep enable; mode=idle
//use sleep to make entry into sync ISR uniform time
#asm ("sleep");
//The following code executes during the vertical blanking
//Code here can be as long as
//a total of 60 lines x 63.5 uSec/line x 8 cycles/uSec
if (LineCount==231) begin
//draws entire board
if (ready == 1) {
if (moveDone == 1) {
j = 0;
moveDone = 0;
}
if (j < 8) {
for (k = 0; k < 8; k++) {
grid = (j+k)%2;
video_putchar(6+12*j, 6+12*k, 2*board[j][k] + grid);
}
}
if (j == 8) {
if (turn == 0) video_putchar(108, 30, 26);
else video_putchar(108, 30, 32);
j++;
}
else if (j == 9) {
ready = 0;
moveDone = 1;
k = 1;
}
else j++;
}
//write actions onto board
else if (init == 1) {
if (k == 1) {
video_smallchar(104, 8, 32);
video_smallchar(108, 8, 30);
video_smallchar(112, 8, 26);
}
if (k < 10) {
video_smallchar(120, 8, k);
initialize();
}
if (k == 10) {
video_smallchar(104, 8, 12);
video_smallchar(108, 8, 12);
k++;
}
else if (k == 11) {
init = 0;
k = 1;
video_smallchar(112, 8, 12);
video_smallchar(120, 8, 12);
}
else {
k++;
}
}
//writes action onto board
else {
if (n == 1) {
getMove();
if (ready == 1 && invalid == 0) {
n = 2;
ready = 0;
}
else n = 4;
}
else if (n == 2) {
video_smallchar(104, 12, type+1);
video_smallchar(108, 12, 10);
video_smallchar(112, 12, x1+1);
n = 3;
}
else if (n == 3) {
video_smallchar(116, 12, y1+1);
video_smallchar(120, 12, x2+1);
video_smallchar(124, 12, y2+1);
n = 1;
ready = 1;
}
else if (n == 4) {
video_smallchar(104, 12, 12);
video_smallchar(108, 12, 12);
video_smallchar(112, 12, 26);
n = 5;
}
else if (n == 5) {
video_smallchar(116, 12, 13);
video_smallchar(120, 12, 12);
video_smallchar(124, 12, 12);
n = 1;
ready = 1;
}
}
end //line 231
end //while
} //main