4 #include <json-c/json.h>
15 static int litex_sim_module_pads_get( struct pad_s
*pads
, char *name
, void **signal
)
21 if(!pads
|| !name
|| !signal
) {
28 if(!strcmp(pads
[i
].name
, name
))
30 sig
=(void*)pads
[i
].signal
;
41 static int clocker_parse_args(struct session_s
*s
, const char *args
)
44 json_object
*args_json
= NULL
;
45 json_object
*freq_json
= NULL
;
46 json_object
*phase_json
= NULL
;
48 args_json
= json_tokener_parse(args
);
51 fprintf(stderr
, "[clocker] Could not parse args: %s\n", args
);
55 if(!json_object_object_get_ex(args_json
, "freq_hz", &freq_json
))
58 fprintf(stderr
, "[clocker] \"freq_hz\" not found in args: %s\n", json_object_to_json_string(args_json
));
62 if(!json_object_object_get_ex(args_json
, "phase_deg", &phase_json
))
65 fprintf(stderr
, "[clocker] \"phase_deg\" not found in args: %s\n", json_object_to_json_string(args_json
));
69 s
->freq_hz
= json_object_get_int64(freq_json
);
70 s
->phase_deg
= json_object_get_int64(phase_json
);
72 if (s
->freq_hz
== 0) {
74 fprintf(stderr
, "[clocker] \"freq_hz\" must be different than 0\n");
78 if (s
->phase_deg
>= 360) {
80 fprintf(stderr
, "[clocker] \"phase_deg\" must be in range [0, 360)\n");
84 if(args_json
) json_object_put(args_json
);
88 static int clocker_start()
90 printf("[clocker] loaded\n");
94 static int clocker_new(void **sess
, char *args
)
98 struct session_s
*s
=NULL
;
105 s
=(struct session_s
*)malloc(sizeof(struct session_s
));
110 memset(s
, 0, sizeof(struct session_s
));
112 clocker_parse_args(s
, args
);
118 static int clocker_add_pads(void *sess
, struct pad_list_s
*plist
)
121 struct session_s
*s
= (struct session_s
*)sess
;
124 if(!sess
|| !plist
) {
130 ret
= litex_sim_module_pads_get(pads
, plist
->name
, (void**)&s
->clk
);
135 s
->name
= plist
->name
;
137 printf("[clocker] %s: freq_hz=%u, phase_deg=%u\n", s
->name
, s
->freq_hz
, s
->phase_deg
);
142 static int clocker_tick(void *sess
, uint64_t time_ps
)
144 static const uint64_t ps_in_sec
= 1000000000000ull;
145 struct session_s
*s
= (struct session_s
*) sess
;
147 uint64_t period_ps
= ps_in_sec
/ s
->freq_hz
;
148 uint64_t phase_shift_ps
= period_ps
* s
->phase_deg
/ 360;
150 // phase-shifted time relative to start of current period
151 uint64_t rel_time_ps
= (time_ps
- phase_shift_ps
) % period_ps
;
152 if (rel_time_ps
< (period_ps
/2)) {
161 static struct ext_module_s ext_mod
= {
170 int litex_sim_ext_module_init(int (*register_module
)(struct ext_module_s
*))
173 ret
= register_module(&ext_mod
);