-
Notifications
You must be signed in to change notification settings - Fork 12
/
receiver.c
87 lines (72 loc) · 1.97 KB
/
receiver.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
#include "util.h"
// Cache miss latency threshold
int CACHE_MISS_LATENCY = 100;
/*
* Detects a bit by repeatedly measuring the access time of the load from config->addr
* and counting the number of misses for the clock length of config->interval.
*
* Detect a bit 1 if misses >= hits
* Detect a bit 0 otherwise
*/
bool detect_bit(struct config *config)
{
int misses = 0;
int hits = 0;
// Sync with sender
CYCLES start_t = cc_sync();
while ((get_time() - start_t) < config->interval) {
// Load data from config->addr and measure latency
CYCLES access_time = measure_one_block_access_time(config->addr);
// Count if it's a miss or hit depending on latency
if (access_time > CACHE_MISS_LATENCY) {
misses++;
} else {
hits++;
}
}
return misses >= hits;
}
int main(int argc, char **argv)
{
// Initialize config and local variables
struct config config;
init_config(&config, argc, argv);
char msg_ch[MAX_BUFFER_LEN + 1];
uint32_t bitSequence = 0;
uint32_t sequenceMask = ((uint32_t) 1<<6) - 1;
uint32_t expSequence = 0b101011;
printf("Listening...\n");
fflush(stdout);
while (1) {
bool bitReceived = detect_bit(&config);
// Detect the sequence '101011' that indicates sender is sending a message
bitSequence = ((uint32_t) bitSequence<<1) | bitReceived;
if ((bitSequence & sequenceMask) == expSequence) {
int binary_msg_len = 0;
int strike_zeros = 0;
for (int i = 0; i < MAX_BUFFER_LEN; i++) {
binary_msg_len++;
if (detect_bit(&config)) {
msg_ch[i] = '1';
strike_zeros = 0;
} else {
msg_ch[i] = '0';
if (++strike_zeros >= 8 && i % 8 == 0) {
break;
}
}
}
msg_ch[binary_msg_len - 8] = '\0';
// Print out message
int ascii_msg_len = binary_msg_len / 8;
char msg[ascii_msg_len];
printf("> %s\n", conv_char(msg_ch, ascii_msg_len, msg));
// Terminate loop if received "exit" message
if (strcmp(msg, "exit") == 0) {
break;
}
}
}
printf("Receiver finished\n");
return 0;
}