-
Notifications
You must be signed in to change notification settings - Fork 1
/
Minicomms.c
97 lines (77 loc) · 1.7 KB
/
Minicomms.c
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
#include "Minicomms.h"
#include <stdlib.h>
#define SETRDY PORTDSET=1<<5
#define CLRRDY PORTDCLR=1<<5
#define ACK ((PORTD>>11) & 1)
#define WRITEDATA(k) \
TRISDCLR = 1<<6; \
PORTDCLR = 1<<6; \
PORTDSET = (k&1) << 6;
#define READDATA(k) \
TRISDSET = 1 << 6; \
(*k) &= ~1; \
(*k) |= (PORTD >> 6) & 1;
void commsinit(){
TRISDSET = (1 << 11);
TRISDCLR = (1 << 5);
PORTDCLR = (1 << 5);
CLRRDY;
return;
}
void sendBit(int b) {
WRITEDATA(b);
SETRDY;
while(!ACK);
CLRRDY;
while(ACK);
}
void recieveBit(int *b) {
while(!ACK);
SETRDY;
READDATA(b);
while(ACK);
CLRRDY;
}
void sendShot(struct packet *p) {
static int hitcounter = 0;
static int c = 0;
for(int i = 4; i --> 0;)
sendBit((p->x) >> i);
for(int i = 4; i --> 0;)
sendBit((p->y) >> i);
int didHit = 0;
recieveBit(&didHit);
p->didHit = didHit;
hitcounter += didHit;
if (hitcounter == 17)
p->didWin = 1;
else
p->didWin = 0;
return;
}
struct packet listenShot(enum tileType *board) {
struct packet p;
p.x = 0, p.y = 0;
for(int i = 0; i < 4; ++ i) {
p.x <<= 1;
recieveBit(&(p.x));
}
for(int i = 0; i < 4; ++ i) {
p.y <<= 1;
recieveBit(&(p.y));
}
if(board[p.x + p.y*COLUMNS] & TILE_SHIP)
p.didHit = 1;
else
p.didHit = 0;
board[p.x + p.y*COLUMNS] |= (p.didHit ? TILE_HIT : TILE_MISS);
p.didWin = 1;
for(int i = 0; i < ROWS*COLUMNS; ++ i)
if((board[i] & TILE_SHIP) && ((board[i]) & TILE_HIT) == 0)
p.didWin = 0;
sendBit(p.didHit);
return p;
}
// data -> 1 bit of data I/O, linked to data on other device (brown cable) - rd 6
// ack -> ready for data transfer? linked to rdy on other device (Red cable) - rd 11
// rdy -> are we ready for data transfer? linked to ack on other device (Orange cable)- rd 5