-
Notifications
You must be signed in to change notification settings - Fork 1
/
transmitter.c
88 lines (80 loc) · 1.93 KB
/
transmitter.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdbool.h>
#include <string.h>
#define NUM_MICRO 25000
#define SYNC_TIME 50000
void transmitChar(char c);
int probe(char* adrs);
void sync();
int main(int argc, char** argv) {
// Get string
char* transmitString;
transmitString = argv[1];
char currChar;
int i = 0;
i = 0;
// Initial synchronization
sync();
// Signal start of transmission
transmitChar(0xff);
printf("Start signal transmitted.\n");
sync();
while ((currChar = transmitString[i]) != 0) {
transmitChar(currChar);
i++;
}
// Signal end of transmission
transmitChar(0x00);
return 0;
}
// Code for transmitting a single character
void transmitChar(char c) {
bool bit;
struct timeval newTime, oldTime;
for (int j = 7; j >= 0; j--) {
sync();
gettimeofday(&oldTime, NULL);
bit = (c & (1 << j)) >> j;
if (bit) {
do {
// Seemingly useless code designed to ensure continuous cache hits
int m = 5;
m++;
gettimeofday(&newTime, NULL);
} while ((newTime.tv_sec - oldTime.tv_sec) * 1000000
+ (newTime.tv_usec - oldTime.tv_usec) < NUM_MICRO);
}
else {
usleep(NUM_MICRO);
}
}
}
// Currently unused, but here in case
/*int probe(char* adrs) {
volatile unsigned long time;
asm __volatile__ (
" mfence \n"
" lfence \n"
" rdtsc \n"
" lfence \n"
" movl %%eax, %%esi \n"
" movl (%1), %%eax \n"
" lfence \n"
" rdtsc \n"
" subl %%esi, %%eax \n"
" clflush 0(%1) \n"
: "=a" (time)
: "c" (adrs)
: "%esi", "%edx");
return time;
}*/
// Synchronize receiver and transmitter using global clock
void sync() {
struct timeval syncer;
do {
gettimeofday(&syncer, NULL);
} while ((syncer.tv_sec * 1000000 + syncer.tv_usec) % SYNC_TIME > 14000);
}