Repository navigation
Expand file tree
/
Copy pathlogger.c
More file actions
50 lines (41 loc) · 1.34 KB
/
Copy pathlogger.c
File metadata and controls
50 lines (41 loc) · 1.34 KB
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
#include <svdpi.h>
#include <stdio.h>
// Global variables
int rush_start = 0;
int rush_end = 0;
FILE *logfile = NULL;
#ifdef __cplusplus
extern "C" {
#endif
// This function reads the file and tells Verilog if it's currently Rush Hour
int get_rush_status(int current_time) {
static int initialized = 0;
if (!initialized) {
FILE *cfg = fopen("input.txt", "r");
if (cfg) {
fscanf(cfg, "%d %d", &rush_start, &rush_end);
fclose(cfg);
}
initialized = 1;
}
return (current_time >= rush_start && current_time <= rush_end);
}
const char* get_label(int val) {
if (val == 1) return "GREEN ";
if (val == 2) return "YELLOW";
if (val == 4) return "RED ";
return "OFF ";
}
void log_traffic_data(int time, int state, int m1, int s, int mt, int m2, int is_rush, int emer) {
if (logfile == NULL) logfile = fopen("traffic_results.txt", "w");
const char* mode = emer ? "EMERGENCY" : (is_rush ? "RUSH_HOUR" : "NORMAL");
// Log to console
printf("[Time: %d] Mode: %s | State: S%d | MT: %s\n", time, mode, state, get_label(mt));
// Log to file
fprintf(logfile, "[%d ns] Mode: %s | State: %d | M1:%s | S:%s | MT:%s | M2:%s\n",
time, mode, state, get_label(m1), get_label(s), get_label(mt), get_label(m2));
fflush(logfile);
}
#ifdef __cplusplus
}
#endif