[1] | 1 | /*------------------------------------------------------------\ |
---|
[52] | 2 | | | |
---|
| 3 | | Tool : systemcass | |
---|
| 4 | | | |
---|
| 5 | | File : global_functions.cc | |
---|
| 6 | | | |
---|
| 7 | | Author : Buchmann Richard | |
---|
| 8 | | Nicolas Pouillon | |
---|
| 9 | | | |
---|
| 10 | | Date : 21_09_2005 | |
---|
| 11 | | | |
---|
| 12 | \------------------------------------------------------------*/ |
---|
[1] | 13 | |
---|
| 14 | /* |
---|
| 15 | * This file is part of the Disydent Project |
---|
| 16 | * Copyright (C) Laboratoire LIP6 - Département ASIM |
---|
| 17 | * Universite Pierre et Marie Curie |
---|
| 18 | * |
---|
| 19 | * Home page : http://www-asim.lip6.fr/disydent |
---|
| 20 | * E-mail : mailto:richard.buchmann@lip6.fr |
---|
| 21 | * |
---|
| 22 | * This library is free software; you can redistribute it and/or modify it |
---|
| 23 | * under the terms of the GNU Library General Public License as published |
---|
| 24 | * by the Free Software Foundation; either version 2 of the License, or (at |
---|
| 25 | * your option) any later version. |
---|
| 26 | * |
---|
| 27 | * Disydent is distributed in the hope that it will be |
---|
| 28 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
| 30 | * Public License for more details. |
---|
| 31 | * |
---|
| 32 | * You should have received a copy of the GNU General Public License along |
---|
| 33 | * with the GNU C Library; see the file COPYING. If not, write to the Free |
---|
| 34 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 35 | */ |
---|
| 36 | |
---|
[33] | 37 | #include <cstdio> |
---|
[27] | 38 | #include <iostream> |
---|
| 39 | #include <dlfcn.h> |
---|
[59] | 40 | #include <signal.h> |
---|
[52] | 41 | |
---|
| 42 | #include "schedulers.h" // get_scheduling & run_schedule_editor |
---|
| 43 | #include "sc_module.h" // check_all_method_process |
---|
| 44 | #include "gen_code.h" // gen_scheduling_code_for_dynamic_link & gen_scheduling_code_for_static_func |
---|
[27] | 45 | #include "sc_clock_ext.h" // clock list |
---|
| 46 | #include "usage.h" |
---|
| 47 | #include "module_hierarchy2dot.h" |
---|
| 48 | #include "assert.h" |
---|
[52] | 49 | |
---|
[27] | 50 | #ifdef HAVE_CONFIG_H |
---|
| 51 | #include "config.h" |
---|
| 52 | #endif |
---|
[1] | 53 | |
---|
| 54 | using namespace std; |
---|
| 55 | |
---|
| 56 | namespace sc_core { |
---|
| 57 | |
---|
[52] | 58 | const char * temporary_dir = "/tmp"; |
---|
| 59 | const char * generated_files_dir = "./generated_by_systemcass"; |
---|
[1] | 60 | |
---|
| 61 | static usage_t usage; |
---|
| 62 | |
---|
| 63 | /* ***************************** */ |
---|
| 64 | /* Dumping functions (for debug) */ |
---|
| 65 | /* ***************************** */ |
---|
| 66 | |
---|
[52] | 67 | template < typename T > ostream & operator <<(ostream & o, const vector < T * > & v) { |
---|
| 68 | typename vector < T * >::const_iterator i; |
---|
| 69 | for (i = v.begin(); i != v.end(); ++i) { |
---|
| 70 | o << **i << " "; |
---|
| 71 | } return o; |
---|
[1] | 72 | } |
---|
| 73 | |
---|
| 74 | /* ************ */ |
---|
| 75 | /* clock list */ |
---|
| 76 | /****************/ |
---|
| 77 | |
---|
[52] | 78 | typedef std::set<const sc_clock *> clock_list_t; |
---|
[1] | 79 | |
---|
[52] | 80 | ostream & operator <<(ostream & o, const clock_list_t & cl) { |
---|
| 81 | clock_list_t::const_iterator i; |
---|
| 82 | for (i = cl.begin(); i != cl.end(); ++i) { |
---|
| 83 | o << (*i)->name() << " "; |
---|
| 84 | } |
---|
| 85 | return o; |
---|
[1] | 86 | } |
---|
| 87 | |
---|
[52] | 88 | |
---|
[1] | 89 | // clock list |
---|
[52] | 90 | void create_clock_list(clock_list_t & c, const equi_list_t & el) { |
---|
| 91 | equi_list_t::const_iterator i; |
---|
| 92 | for (i = el.begin(); i != el.end(); ++i) { |
---|
| 93 | equi_t::const_iterator j; |
---|
| 94 | for (j = i->begin(); j != i->end(); ++j) { |
---|
| 95 | if (j->kind() == sc_clock::kind_string) |
---|
| 96 | c.insert((const sc_clock *) j->object); |
---|
| 97 | } |
---|
[42] | 98 | } |
---|
[1] | 99 | } |
---|
| 100 | |
---|
| 101 | /* ************ */ |
---|
| 102 | /* functions */ |
---|
| 103 | /****************/ |
---|
| 104 | |
---|
| 105 | // function pointer to the simulation core (compiled and linked dynamically) |
---|
| 106 | evaluation_fct_t func_simulate_1_cycle = NULL; |
---|
| 107 | evaluation_fct_t func_combinationals = NULL; |
---|
| 108 | |
---|
| 109 | /* ************ */ |
---|
| 110 | /* dynamic link */ |
---|
| 111 | /* ************ */ |
---|
| 112 | |
---|
[52] | 113 | static void * handle = NULL; |
---|
[1] | 114 | |
---|
[52] | 115 | static void link(const char * lib) { |
---|
| 116 | // chargement du code de simulate_1_cycle |
---|
| 117 | handle = dlopen(lib, RTLD_GLOBAL | RTLD_NOW); |
---|
| 118 | //handle = dlopen(lib, RTLD_LAZY | RTLD_GLOBAL | RTLD_NOW); |
---|
[1] | 119 | |
---|
[52] | 120 | if (handle == NULL) { |
---|
| 121 | const char * error = dlerror(); |
---|
| 122 | if (error) { |
---|
| 123 | fprintf(stderr, "dlopen: %s\n", error); |
---|
| 124 | } |
---|
| 125 | fprintf(stderr, "Is there -rdynamic option into your command line ? If not, please do it.\n"); |
---|
| 126 | exit(18); |
---|
| 127 | } |
---|
[1] | 128 | |
---|
[52] | 129 | union uni_fct_t { |
---|
| 130 | evaluation_fct_t fct_type; |
---|
| 131 | void * dl_pointer_type; |
---|
| 132 | }; |
---|
| 133 | uni_fct_t temp; |
---|
| 134 | temp.dl_pointer_type = dlsym(handle, "initialize"); |
---|
| 135 | if (temp.dl_pointer_type == NULL) { |
---|
| 136 | const char * error = dlerror(); |
---|
| 137 | if (error) { |
---|
| 138 | fprintf(stderr, "dlsym: %s\n", error); |
---|
| 139 | } |
---|
| 140 | exit(19); |
---|
| 141 | } |
---|
| 142 | evaluation_fct_t func_initialize; |
---|
| 143 | func_initialize = temp.fct_type; |
---|
| 144 | func_initialize(); |
---|
[1] | 145 | |
---|
[52] | 146 | temp.dl_pointer_type = dlsym(handle, "simulate_1_cycle"); |
---|
| 147 | if (temp.dl_pointer_type == NULL) { |
---|
| 148 | const char * error = dlerror(); |
---|
| 149 | if (error) { |
---|
| 150 | fprintf(stderr, "dlsym: %s\n", error); |
---|
| 151 | exit(20); |
---|
| 152 | } |
---|
| 153 | func_simulate_1_cycle = temp.fct_type; |
---|
| 154 | |
---|
| 155 | temp.dl_pointer_type = dlsym(handle, "mealy_generation"); |
---|
| 156 | if (temp.dl_pointer_type == NULL) { |
---|
| 157 | const char * error = dlerror(); |
---|
| 158 | if (error) { |
---|
| 159 | fprintf(stderr, "dlsym: %s\n", error); |
---|
| 160 | } |
---|
| 161 | exit(21); |
---|
| 162 | } |
---|
| 163 | func_combinationals = temp.fct_type; |
---|
| 164 | |
---|
| 165 | /* |
---|
| 166 | * |
---|
| 167 | */ |
---|
| 168 | if (dump_stage) { |
---|
| 169 | cerr << "dynamic link done\n"; |
---|
| 170 | } |
---|
| 171 | } |
---|
[1] | 172 | } |
---|
| 173 | |
---|
[52] | 174 | |
---|
| 175 | static void unlink() { |
---|
| 176 | if (handle) { |
---|
| 177 | if (dlclose(handle) != 0) { |
---|
| 178 | cerr << "Warning : dlclose returns an error.\n" << dlerror() << endl; |
---|
| 179 | } |
---|
| 180 | handle = NULL; |
---|
| 181 | } |
---|
[1] | 182 | } |
---|
| 183 | |
---|
| 184 | /* ************ */ |
---|
| 185 | /* initializing */ |
---|
| 186 | /* ************ */ |
---|
| 187 | |
---|
[52] | 188 | bool already_initialized = false; |
---|
[1] | 189 | |
---|
[52] | 190 | static void use_static_func() { |
---|
| 191 | if (dump_stage) { |
---|
| 192 | cerr << "Using static functions to schedule SystemC processes.\n"; |
---|
| 193 | } |
---|
| 194 | if (scheduling_method == CASS_SCHEDULING) { |
---|
| 195 | func_simulate_1_cycle = (evaluation_fct_t) sc_core::quasistatic_simulate_1_cycle; |
---|
| 196 | func_combinationals = (evaluation_fct_t) sc_core::quasistatic_mealy_generation; |
---|
| 197 | } |
---|
| 198 | else { |
---|
| 199 | func_simulate_1_cycle = (evaluation_fct_t) sc_core::static_simulate_1_cycle; |
---|
| 200 | func_combinationals = (evaluation_fct_t) sc_core::static_mealy_generation; |
---|
| 201 | } |
---|
[1] | 202 | } |
---|
| 203 | |
---|
[52] | 204 | |
---|
| 205 | static void compile_and_link(const char *base_name) { |
---|
| 206 | if (dump_stage) { |
---|
| 207 | cerr << "Using dynamically loaded functions to schedule SystemC processes.\n"; |
---|
| 208 | } |
---|
| 209 | // compilation du code de simulate_1_cycle |
---|
| 210 | compile_code(base_name); |
---|
| 211 | |
---|
| 212 | char lib_absolutepath[256]; |
---|
[27] | 213 | #if defined(CONFIG_OS_DARWIN) |
---|
[52] | 214 | sprintf(lib_absolutepath, "/tmp/%s.so", base_name); |
---|
[27] | 215 | #elif defined(CONFIG_OS_LINUX) |
---|
[52] | 216 | sprintf(lib_absolutepath, "/tmp/.libs/%s.so.0", base_name); |
---|
[1] | 217 | #else |
---|
[52] | 218 | cerr << "ERROR\n"; |
---|
| 219 | exit(126); |
---|
[1] | 220 | #endif |
---|
[52] | 221 | link(lib_absolutepath); |
---|
[1] | 222 | } |
---|
| 223 | |
---|
| 224 | |
---|
[52] | 225 | static void internal_sc_initialize(void) { |
---|
| 226 | sort_equi_list(); |
---|
[1] | 227 | |
---|
[52] | 228 | check_all_method_process(); |
---|
[32] | 229 | |
---|
[52] | 230 | if (dump_netlist_info) { |
---|
| 231 | cerr << "Module instance list\n--------------------\n" << instances_set << endl; |
---|
| 232 | } |
---|
[1] | 233 | |
---|
[52] | 234 | /* |
---|
| 235 | * Initialize the signals table |
---|
| 236 | */ |
---|
| 237 | create_signals_table(); |
---|
| 238 | bind_to_table(); |
---|
| 239 | if (dump_netlist_info) { |
---|
| 240 | print_table(cerr); |
---|
| 241 | cerr << endl; |
---|
| 242 | print_table_stats(cerr); |
---|
| 243 | cerr << endl; |
---|
| 244 | } |
---|
| 245 | // Init variables to be able to run combinational functions |
---|
| 246 | pending_write_vector_capacity = get_signal_table_size(); |
---|
[1] | 247 | |
---|
[60] | 248 | assert(pending_write_vector_capacity != 0); |
---|
| 249 | |
---|
| 250 | #ifdef _OPENMP |
---|
| 251 | #define LINE_SIZE 128L |
---|
| 252 | int malloc_size = (sizeof (pending_write_t) * (pending_write_vector_capacity + 1) + (LINE_SIZE - 1)) & ~(LINE_SIZE - 1); |
---|
| 253 | assert((sizeof(pending_write_t) * (pending_write_vector_capacity + 1)) <= malloc_size && "bad allocation size"); |
---|
| 254 | |
---|
| 255 | #pragma omp parallel |
---|
| 256 | { |
---|
| 257 | posix_memalign((void **) &pending_write_vector, LINE_SIZE, malloc_size); |
---|
| 258 | pending_write_vector_nb = (int32_t *) &pending_write_vector[0]; |
---|
| 259 | pending_write_vector = &pending_write_vector[1]; |
---|
| 260 | //printf("malloc 0x%x @%p, idx @0x%x\n", malloc_size, pending_write_vector, pending_write_vector_nb); |
---|
| 261 | *pending_write_vector_nb = 0; |
---|
[52] | 262 | } |
---|
[60] | 263 | #else |
---|
| 264 | pending_write_vector = (pending_write_vector_t) malloc(sizeof(pending_write_t) * pending_write_vector_capacity); |
---|
| 265 | #endif |
---|
[1] | 266 | |
---|
[60] | 267 | |
---|
[52] | 268 | // create the clock list |
---|
| 269 | clock_list_t clock_list; |
---|
| 270 | create_clock_list(clock_list, get_equi_list()); |
---|
| 271 | if (dump_netlist_info) { |
---|
| 272 | cerr << "Clock list\n" << "----------\n" << clock_list << "\n\n"; |
---|
| 273 | } |
---|
[1] | 274 | |
---|
[52] | 275 | // Check if a clock exists in the system |
---|
| 276 | if (clock_list.empty()) { |
---|
| 277 | cerr << "System need a clock.\n" << |
---|
| 278 | "Please define system clock using special type \"sc_clock\".\n"; |
---|
| 279 | exit(22); |
---|
| 280 | } |
---|
| 281 | // Check if any constructor wrote into registers |
---|
[60] | 282 | if (*pending_write_vector_nb != 0) { |
---|
[52] | 283 | cerr << |
---|
| 284 | "Error : Register/Signal writing is not allowed before sc_initialize.\n" |
---|
| 285 | "Move initializations from constructors/sc_main to module reset sequences.\n"; |
---|
| 286 | // we are unable to dump register(s) name(s) |
---|
| 287 | // because the table binding is not yet completed. |
---|
| 288 | exit(24); |
---|
| 289 | } |
---|
[32] | 290 | |
---|
[52] | 291 | string base_name = get_scheduling(scheduling_method); |
---|
[32] | 292 | |
---|
[52] | 293 | if (dynamic_link_of_scheduling_code) { |
---|
| 294 | compile_and_link(base_name.c_str()); |
---|
| 295 | } |
---|
| 296 | else { |
---|
| 297 | use_static_func(); |
---|
| 298 | } |
---|
| 299 | |
---|
[60] | 300 | *pending_write_vector_nb = 0; |
---|
[52] | 301 | |
---|
| 302 | check_all_ports(); |
---|
| 303 | usage.start(); |
---|
| 304 | |
---|
| 305 | if (dump_stage) { |
---|
| 306 | cerr << "sc_initialize () done.\n"; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | already_initialized = true; |
---|
[1] | 310 | } |
---|
| 311 | |
---|
[52] | 312 | |
---|
| 313 | inline void check_and_initialize() { |
---|
| 314 | if (already_initialized == false) { |
---|
[1] | 315 | #if defined(SYSTEMC_VERSION_1_0) |
---|
[52] | 316 | cerr << "Warning : call sc_initialize before executiong simulation.\n"; |
---|
[1] | 317 | #endif |
---|
[52] | 318 | internal_sc_initialize(); |
---|
| 319 | if (dump_module_hierarchy) { |
---|
| 320 | module_hierarchy2dot(dump_module_hierarchy); |
---|
| 321 | } |
---|
| 322 | if (nosimulation) { |
---|
| 323 | exit(0); |
---|
| 324 | } |
---|
[1] | 325 | } |
---|
| 326 | } |
---|
| 327 | |
---|
[52] | 328 | |
---|
| 329 | void sc_initialize(void) { |
---|
| 330 | cerr << "Warning : 'sc_initialize' function is deprecated since SystemC 2.1.\n"; |
---|
| 331 | cerr << "Use 'sc_start(0)' instead.\n"; |
---|
| 332 | check_and_initialize(); |
---|
[1] | 333 | } |
---|
| 334 | |
---|
| 335 | /* ********** */ |
---|
| 336 | /* simulating */ |
---|
| 337 | /* ********** */ |
---|
| 338 | |
---|
[52] | 339 | inline void sc_cycle(double duration) { |
---|
| 340 | check_and_initialize(); |
---|
| 341 | sc_core::internal_sc_cycle0(duration); |
---|
[59] | 342 | if (have_to_stop) { |
---|
| 343 | std::vector<method_process_t *>::iterator i; |
---|
| 344 | for (i = sc_core::method_process_list.begin(); i != sc_core::method_process_list.end(); i++) { |
---|
| 345 | delete *i; |
---|
| 346 | } |
---|
| 347 | std::vector<const char *>::iterator j; |
---|
| 348 | for (j = sc_core::allocated_names.begin(); j != sc_core::allocated_names.end(); j++) { |
---|
| 349 | free((char *) *j); |
---|
| 350 | } |
---|
| 351 | } |
---|
[1] | 352 | } |
---|
| 353 | |
---|
[52] | 354 | |
---|
| 355 | inline void sc_cycle(double duration, sc_time_unit time_unit) { |
---|
| 356 | check_and_initialize(); |
---|
| 357 | sc_core::internal_sc_cycle0(duration); |
---|
[59] | 358 | if (have_to_stop) { |
---|
| 359 | std::vector<method_process_t *>::iterator i; |
---|
| 360 | for (i = sc_core::method_process_list.begin(); i != sc_core::method_process_list.end(); i++) { |
---|
| 361 | delete *i; |
---|
| 362 | } |
---|
| 363 | std::vector<const char *>::iterator j; |
---|
| 364 | for (j = sc_core::allocated_names.begin(); j != sc_core::allocated_names.end(); j++) { |
---|
| 365 | free((char *) *j); |
---|
| 366 | } |
---|
| 367 | } |
---|
[1] | 368 | } |
---|
| 369 | |
---|
[52] | 370 | |
---|
| 371 | void sc_start(double d_val) { |
---|
| 372 | sc_cycle(d_val); |
---|
[1] | 373 | #ifdef DUMP_SIGNAL_STATS |
---|
[52] | 374 | print_registers_writing_stats(cerr); |
---|
[1] | 375 | #endif |
---|
| 376 | #ifdef DUMP_SCHEDULE_STATS |
---|
[52] | 377 | print_schedule_stats(cerr); |
---|
[1] | 378 | #endif |
---|
| 379 | } |
---|
| 380 | |
---|
[52] | 381 | |
---|
| 382 | void sc_start() { |
---|
| 383 | sc_cycle(-1); |
---|
[4] | 384 | #ifdef DUMP_SIGNAL_STATS |
---|
[52] | 385 | print_registers_writing_stats(cerr); |
---|
[4] | 386 | #endif |
---|
| 387 | #ifdef DUMP_SCHEDULE_STATS |
---|
[52] | 388 | print_schedule_stats(cerr); |
---|
[4] | 389 | #endif |
---|
| 390 | } |
---|
| 391 | |
---|
[52] | 392 | |
---|
| 393 | void sc_start(double d_val, sc_time_unit d_tu) { |
---|
| 394 | sc_start(sc_time(d_val, d_tu)); |
---|
[1] | 395 | } |
---|
| 396 | |
---|
[52] | 397 | |
---|
| 398 | void sc_start(const sc_time & duration) { |
---|
| 399 | sc_cycle((double) duration); |
---|
[4] | 400 | #ifdef DUMP_SIGNAL_STATS |
---|
[52] | 401 | print_registers_writing_stats(cerr); |
---|
[4] | 402 | #endif |
---|
| 403 | #ifdef DUMP_SCHEDULE_STATS |
---|
[52] | 404 | print_schedule_stats(cerr); |
---|
[4] | 405 | #endif |
---|
[1] | 406 | } |
---|
| 407 | |
---|
[52] | 408 | |
---|
[1] | 409 | /* ****************** */ |
---|
| 410 | /* stoping simulation */ |
---|
| 411 | /* ****************** */ |
---|
| 412 | |
---|
[52] | 413 | bool have_to_stop = false; |
---|
| 414 | sc_stop_mode stop_mode = SC_STOP_FINISH_DELTA; |
---|
[1] | 415 | |
---|
[52] | 416 | void sc_stop() { |
---|
| 417 | switch (stop_mode) { |
---|
| 418 | case SC_STOP_IMMEDIATE: |
---|
| 419 | exit(54); |
---|
| 420 | break; |
---|
| 421 | case SC_STOP_FINISH_DELTA: |
---|
| 422 | default: |
---|
| 423 | have_to_stop = true; |
---|
| 424 | break; |
---|
| 425 | } |
---|
[1] | 426 | } |
---|
| 427 | |
---|
[52] | 428 | |
---|
| 429 | void sc_set_stop_mode(sc_stop_mode new_mode) { |
---|
| 430 | if (new_mode == SC_STOP_IMMEDIATE) { |
---|
| 431 | stop_mode = SC_STOP_IMMEDIATE; |
---|
| 432 | } |
---|
[1] | 433 | } |
---|
| 434 | |
---|
[52] | 435 | |
---|
| 436 | sc_stop_mode sc_get_stop_mode() { |
---|
| 437 | return stop_mode; |
---|
[1] | 438 | } |
---|
| 439 | |
---|
[52] | 440 | |
---|
| 441 | void close_systemcass() { |
---|
| 442 | unlink(); |
---|
| 443 | if (print_user_resources) { |
---|
| 444 | usage.stop(); |
---|
| 445 | unsigned int d = usage; |
---|
| 446 | cerr << "Performances\n" "------------\n"; |
---|
| 447 | cerr << "Time elapsed (sec) : " << d << endl; |
---|
| 448 | cerr << "Cycles done : " << nb_cycles << endl; |
---|
| 449 | if (d == 0) { |
---|
| 450 | cerr << "Performance (c/s) : N/A" << endl; |
---|
| 451 | } |
---|
| 452 | else { |
---|
| 453 | cerr << "Performance (c/s) : " << nb_cycles / d << endl; |
---|
| 454 | } |
---|
| 455 | // cerr << "Memory used : " << usage.get_memory_size () << endl; |
---|
| 456 | } |
---|
[1] | 457 | } |
---|
| 458 | |
---|
| 459 | } // end of sc_core namespace |
---|
| 460 | |
---|
[52] | 461 | /* |
---|
| 462 | # Local Variables: |
---|
| 463 | # tab-width: 4; |
---|
| 464 | # c-basic-offset: 4; |
---|
| 465 | # c-file-offsets:((innamespace . 0)(inline-open . 0)); |
---|
| 466 | # indent-tabs-mode: nil; |
---|
| 467 | # End: |
---|
| 468 | # |
---|
| 469 | # vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 470 | */ |
---|
| 471 | |
---|