-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.c
executable file
·162 lines (125 loc) · 3.89 KB
/
controller.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/time.h>
/* Inserite eventuali extern modules qui */
extern void ciclocontrollore_asm( char bufferin[], char bufferout_asm[]);
/* ************************************* */
enum { MAXLINES = 400 };
long long current_timestamp() {
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
/* te.tv_nsec nanoseconds divide by 1000 to get microseconds*/
long long nanoseconds = tp.tv_sec*1000LL + tp.tv_nsec; // caculate nanoseconds
return nanoseconds;
}
int main(int argc, char *argv[]) {
int i = 0;
char bufferin[MAXLINES*8+1] ;
char line[1024] ;
long long tic_c, toc_c, tic_asm, toc_asm;
char bufferout_c[MAXLINES*8+1] = "" ;
char bufferout_asm[MAXLINES*8+1] = "" ;
FILE *inputFile = fopen(argv[1], "r");
if(argc != 3)
{
fprintf(stderr, "Syntax ./test <input_file> <output_file>\n");
exit(1);
}
if (inputFile == 0)
{
fprintf(stderr, "failed to open the input file. Syntax ./test <input_file> <output_file>\n");
exit(1);
}
while (i < MAXLINES && fgets(line, sizeof(line), inputFile))
{
i = i + 1;
strcat( bufferin, line) ;
}
bufferin[MAXLINES*8] = '\0' ;
fclose(inputFile);
/* ELABORAZIONE in C */
tic_c = current_timestamp();
int c = 0, j = 0;
int init, reset, ph, nck=0 ;
char st = '-', vlv[3] = "--", oldst = '-' ;
char tmpout[9] ;
char nckstr[3] = "--" ;
i = 0;
while ( bufferin[i] != '\0' ) {
init = bufferin[i] - '0' ;
reset = bufferin[i+2] - '0' ;
ph = (bufferin[i+4]-'0')*100 + (bufferin[i+5]-'0')*10 + (bufferin[i+6]-'0') ;
strcpy(tmpout, "-,--,--\n") ;
/* printf("i=%d, init: %d, reset: %d, ph: %d, tmpout: %s", i, init, reset, ph, tmpout) ; */
if ( init == 0 || reset == 1) {
oldst = '-' ;
}
else
{
/* determino lo stato attuale */
if ( ph < 60 ) {
st = 'A' ;
}
else if ( ph >= 60 && ph <= 80 ) {
st = 'N' ;
}
else if ( ph > 80 ) {
st = 'B' ;
}
/* aggiorno il contatore dei cicli */
if ( st != oldst ) {
nck = 0 ;
}
else
{
nck = nck + 1 ;
}
sprintf(nckstr,"%.2d",nck) ;
/* determino lo stato della valvola */
if ( st == 'A' && nck>4 ) {
strcpy(vlv,"BS") ;
}
else if ( st == 'B' && nck>4 ) {
strcpy(vlv,"AS") ;
}
else {
strcpy(vlv,"--") ;
}
/* aggiorno oldst */
oldst = st ;
/* genero la stringa di output */
tmpout[0] = st ;
tmpout[2] = nckstr[0] ;
tmpout[3] = nckstr[1] ;
tmpout[5] = vlv[0] ;
tmpout[6] = vlv[1] ;
}
strcat( bufferout_c, tmpout) ;
i = i + 8 ;
}
toc_c = current_timestamp();
long long c_time_in_nanos = toc_c - tic_c;
/* FINE ELABORAZIONE C */
/* INIZIO ELABORAZIONE ASM */
tic_asm = current_timestamp();
/* Assembly inline:
Inserite qui il vostro blocco di codice assembly inline o richiamo a funzioni assembly.
Il blocco di codice prende come input 'bufferin' e deve restituire una variabile stringa 'bufferout_asm' che verrà poi salvata su file. */
// Il mio codice inline consiste solo in una chiamata a funzione Assembly
ciclocontrollore_asm( bufferin, bufferout_asm );
toc_asm = current_timestamp();
long long asm_time_in_nanos = toc_asm - tic_asm;
/* FINE ELABORAZIONE ASM */
printf("C time elapsed: %lld ns\n", c_time_in_nanos);
printf("ASM time elapsed: %lld ns\n", asm_time_in_nanos);
/* Salvataggio dei risultati ASM */
FILE *outputFile;
outputFile = fopen (argv[2], "w");
fprintf (outputFile, "%s", bufferout_asm);
fclose (outputFile);
return 0;
}