2 * Simulator of microcontrollers (utils.cc)
4 * Copyright (C) 1997 Drotos Daniel
6 * To contact author send email to dr.dkdb@gmail.com
10 /* This file is part of microcontroller simulator: ucsim.
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 #if defined(HAVE_VASPRINTF) && !defined(_GNU_SOURCE)
31 /* define before including stdio.h to enable vasprintf() declaration */
52 get_sub_opt(char **option
, const char * const *tokens
, char **valuep
)
57 if (!(end
= strchr(*option
, ',')))
58 end
= *option
+ strlen(*option
);
61 if ((equ
= strchr(*option
, '=')))
70 strcmp(*option
, tokens
[i
]))
75 return tokens
[i
]?i
:-1;
80 get_id_string(struct id_element
*ids
, int id
)
84 while (ids
[i
].id_string
&&
87 return(ids
[i
].id_string
);
91 get_id_string(struct id_element
*ids
, int id
, const char *def
)
93 const char *s
= get_id_string(ids
, id
);
99 get_string_id(struct id_element
*ids
, char *str
)
103 while (ids
[i
].id_string
&&
104 strcmp(ids
[i
].id_string
, str
) != 0)
110 get_string_id(struct id_element
*ids
, char *str
, int def
)
114 while (ids
[i
].id_string
&&
115 strcmp(ids
[i
].id_string
, str
) != 0)
117 return(ids
[i
].id_string
?ids
[i
].id
:def
);
122 vformat_string(const char *format
, va_list ap
)
125 #ifdef HAVE_VASPRINTF
126 if (0 > vasprintf(&msg
, format
, ap
))
130 #ifdef HAVE_VSNPRINTF
131 msg
= (char*)malloc(80*25);
132 vsnprintf(msg
, 80*25, format
, ap
);
134 msg
= (char*)malloc(80*25);
135 vsprintf(msg
, format
, ap
);
142 format_string(const char *format
, ...)
146 va_start(ap
, format
);
147 char *s
= vformat_string(format
, ap
);
154 print_char_octal(char c
, FILE *f
)
156 if (strchr("\a\b\f\n\r\t\v\"", c
))
159 case '\a': fprintf(f
, "\a"); break;
160 case '\b': fprintf(f
, "\b"); break;
161 case '\f': fprintf(f
, "\f"); break;
162 case '\n': fprintf(f
, "\n"); break;
163 case '\r': fprintf(f
, "\r"); break;
164 case '\t': fprintf(f
, "\t"); break;
165 case '\v': fprintf(f
, "\v"); break;
166 case '\"': fprintf(f
, "\""); break;
171 fprintf(f
, "\\%03o", (int)c
);
176 object_name(class cl_base
*o
)
190 case_string(enum letter_case lcase
, const char *str
)
192 char *p
= strdup(str
);
210 if (!p
|| *p
== '\0')
212 while (isspace(*p
)) p
++;
221 cbin(long data
, int bits
)
226 mask
= mask
<< ((bits
>= 1)?(bits
-1):0);
229 c
+= (data
&mask
)?'1':'0';
236 strispn(char *s
, char c
)
240 char *p
= strchr(s
, c
);
246 /* Return true if "serach_in" string ends with string "what" */
249 strend(const char *search_in
, const char *what
)
256 const char *start
= strstr(search_in
, what
);
259 if (start
[strlen(what
)] == '\0')
265 valid_sym_name(char *s
)
284 filename_has_ext(class cl_f
*f
, const char *ext
)
289 n
= f
->get_file_name();
301 is_hex_file(class cl_f
*f
)
306 n
= f
->get_file_name();
311 if (strend(n
, ".ihx") ||
320 is_asc_file(class cl_f
*f
)
322 return filename_has_ext(f
, ".asc");
326 is_p2h_file(class cl_f
*f
)
328 return filename_has_ext(f
, ".p2h");
332 is_omf_file(class cl_f
*f
)
334 return filename_has_ext(f
, ".omf");
338 is_cdb_file(class cl_f
*f
)
340 return filename_has_ext(f
, ".cdb");
344 is_s19_file(class cl_f
*f
)
346 return filename_has_ext(f
, ".s19");
350 is_map_file(class cl_f
*f
)
352 return filename_has_ext(f
, ".map");
356 option_name=col_opt:col_opt
359 prompt prompt_console command answer
360 dump_address dump_label dump_number dump_char
373 black red green yellow blue magenta cyan white
374 bblack bred bgreen byellow bblue bmagenta bcyan bwhite
393 colopt2ansiseq(char *opt
)
395 bool fg_rgb
= false, bg_rgb
= false;
396 bool fg_bright
= false, bg_bright
= false;
397 chars r
= "", full
= opt
, tok
= "";
400 class cl_option
*o
= application
->options
->get_option("color_bg");
402 if (o
) o
->get_value(&bgcolor
);
408 tok
= full
.token(":");
411 const char *s
= tok
.c_str();
421 else if (tok
=="bblack")
424 fg
= 0, fg_bright
= true;
426 bg
= 0, bg_bright
= true;
435 else if (tok
=="bred")
438 fg
= 1, fg_bright
= true;
440 bg
= 1, bg_bright
= true;
442 else if (tok
=="green")
449 else if (tok
=="bgreen")
452 fg
= 2, fg_bright
= true;
454 bg
= 2, bg_bright
= true;
456 else if (tok
=="yellow")
463 else if (tok
=="byellow")
466 fg
= 3, fg_bright
= true;
468 bg
= 3, bg_bright
= true;
470 else if (tok
=="blue")
477 else if (tok
=="bblue")
480 fg
= 4, fg_bright
= true;
482 bg
= 4, bg_bright
= true;
484 else if (tok
=="magenta")
491 else if (tok
=="bmagenta")
494 fg
= 5, fg_bright
= true;
496 bg
= 5, bg_bright
= true;
498 else if (tok
=="cyan")
505 else if (tok
=="bcyan")
508 fg
= 6, fg_bright
= true;
510 bg
= 6, bg_bright
= true;
512 else if (tok
=="white")
519 else if (tok
=="bwhite")
522 fg
= 7, fg_bright
= true;
524 bg
= 7, bg_bright
= true;
528 int c
= strtol(&s
[1], NULL
, 16);
537 if (strcspn(s
, "bBfFiIuUdDcCoOkKlL") == 0)
540 switch (toupper(s
[i
]))
542 case 'B': ctype
|= ct_bold
; break;
543 case 'F': ctype
|= ct_faint
; break;
544 case 'I': ctype
|= ct_italic
; break;
545 case 'U': ctype
|= ct_underl
; break;
546 case 'D': ctype
|= ct_dunderl
; break;
547 case 'C': ctype
|= ct_crossed
; break;
548 case 'O': ctype
|= ct_overl
; break;
549 case 'K': ctype
|= ct_blink
; break;
550 case 'L': ctype
|= ct_blink
; break;
554 tok
= full
.token(":");
557 /* set character rendering mode */
558 if (ctype
!= ct_none
)
560 if (ctype
& ct_bold
) r
.append("\033[1m");
561 if (ctype
& ct_faint
) r
.append("\033[2m");
562 if (ctype
& ct_italic
) r
.append("\033[3m");
563 if (ctype
& ct_underl
) r
.append("\033[4m");
564 if (ctype
& ct_dunderl
) r
.append("\033[21m");
565 if (ctype
& ct_crossed
) r
.append("\033[9m");
566 if (ctype
& ct_overl
) r
.append("\033[53m");
567 if (ctype
& ct_blink
) r
.append("\033[5m");
570 /* Background color */
575 r
.appendf("\033[48;2;%d;%d;%dm", (bg
>>16)&0xff, (bg
>>8)&0xff, bg
&0xff);
582 r
.appendf("\033[%dm", i
);
586 /* Foreground color */
591 r
.appendf("\033[38;2;%d;%d;%dm", (fg
>>16)&0xff, (fg
>>8)&0xff, fg
&0xff);
598 r
.appendf("\033[%dm", i
);
606 strtoscale(const char *scale
, const char **units
)
622 d
= 1 / 1000000000000000.0;
625 d
= 1 / 1000000000000.0;
628 d
= 1 / 1000000000.0;
637 if (!strncmp(scale
, "µ", sizeof("µ") - 1))
640 *units
= &scale
[sizeof("µ")];
661 d
= 1000000000000000.0;
680 d
= 1024.0 * 1024.0 * 1024.0;
683 d
= 1024.0 * 1024.0 * 1024.0 * 1024.0;
686 d
= 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0;
702 /* Custom random number generator */
704 #define PHI 0x9e3779b9
706 static u32_t Q
[4096], c
= 362436;
709 srnd(unsigned int seed
)
715 Q
[2] = seed
+ PHI
+ PHI
;
717 for (i
= 3; i
< 4096; i
++)
718 Q
[i
] = Q
[i
- 3] ^ Q
[i
- 2] ^ PHI
^ i
;
724 u64_t t
, a
= 18782LL;
725 static u32_t i
= 4095;
726 u32_t x
, r
= 0xfffffffe;
735 return (Q
[i
] = r
- x
);
757 /* End of utils.src/utils.cc */