project_files/hwc/rtl/system.c
branchwebgl
changeset 7983 02f36c3e7f6c
child 8047 25a4daa6473c
equal deleted inserted replaced
7981:aac257b77842 7983:02f36c3e7f6c
       
     1 #include "system.h"
       
     2 #include <string.h>
       
     3 #include <stdio.h>
       
     4 #include <stdlib.h>
       
     5 #include <wchar.h>
       
     6 #include <math.h>
       
     7 
       
     8 int paramCount;
       
     9 string255 params[MAX_PARAMS];
       
    10 
       
    11 double pi = M_PI;
       
    12 
       
    13 string255 fpcrtl_copy(string255 s, Integer index, Integer count) {
       
    14     string255 result = STRINIT("");
       
    15 
       
    16     if (count < 1) {
       
    17         return result;
       
    18     }
       
    19 
       
    20     if (index < 1) {
       
    21         index = 1;
       
    22     }
       
    23 
       
    24     if (index > s.len) {
       
    25         return result;
       
    26     }
       
    27 
       
    28     if (index + count > s.len + 1) {
       
    29         count = s.len + 1 - index;
       
    30     }
       
    31 
       
    32     memcpy(result.str, s.str + index - 1, count);
       
    33 
       
    34     result.str[count] = 0;
       
    35     result.len = count;
       
    36 
       
    37     return result;
       
    38 }
       
    39 
       
    40 void fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) {
       
    41     // number of chars to be move
       
    42     int num_move;
       
    43     int new_length;
       
    44 
       
    45     string255 temp = *s;
       
    46 
       
    47     if (index < 1) {
       
    48         // in fpc, if index < 1, the string won't be modified
       
    49         return;
       
    50     }
       
    51 
       
    52     if(index > s->len){
       
    53         return;
       
    54     }
       
    55 
       
    56     if (count > s->len - index + 1) {
       
    57         s->str[index - 1] = 0;
       
    58         s->len = index - 1;
       
    59         return;
       
    60     }
       
    61 
       
    62     num_move = s->len - index + 1 - count;
       
    63     new_length = s->len - count;
       
    64 
       
    65     memmove(s->str + index - 1, temp.str + index - 1 + count, num_move);
       
    66     s->str[new_length] = 0;
       
    67 
       
    68     s->len = new_length;
       
    69 
       
    70 }
       
    71 
       
    72 string255 fpcrtl_floatToStr(double n) {
       
    73     string255 t;
       
    74     sprintf(t.str, "%f", n);
       
    75     t.len = strlen(t.str);
       
    76 
       
    77     return t;
       
    78 }
       
    79 
       
    80 void fpcrtl_move__vars(void *src, void *dst, SizeInt count) {
       
    81     memmove(dst, src, count);
       
    82 }
       
    83 
       
    84 Integer __attribute__((overloadable)) fpcrtl_pos(Char c, string255 str) {
       
    85     string255 t;
       
    86     t.len = 1;
       
    87     t.str[0] = c;
       
    88     t.str[1] = 0;
       
    89     return fpcrtl_pos(t, str);
       
    90 }
       
    91 
       
    92 Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, string255 str) {
       
    93 
       
    94     char* p;
       
    95 
       
    96     FIX_STRING(substr);
       
    97     FIX_STRING(str);
       
    98 
       
    99     if (str.len == 0) {
       
   100         return 0;
       
   101     }
       
   102 
       
   103     if (substr.len == 0) {
       
   104         return 0;
       
   105     }
       
   106 
       
   107     str.str[str.len] = 0;
       
   108     substr.str[substr.len] = 0;
       
   109 
       
   110     p = strstr(str.str, substr.str);
       
   111 
       
   112     if (p == NULL) {
       
   113         return 0;
       
   114     }
       
   115 
       
   116     return strlen(str.str) - strlen(p) + 1;
       
   117 }
       
   118 
       
   119 Integer fpcrtl_length(string255 s) {
       
   120     return s.len;
       
   121 }
       
   122 
       
   123 string255 fpcrtl_lowerCase(string255 s) {
       
   124     int i;
       
   125 
       
   126     for (i = 0; i < s.len; i++) {
       
   127         if (s.str[i] >= 'A' && s.str[i] <= 'Z') {
       
   128             s.str[i] += 'a' - 'A';
       
   129         }
       
   130     }
       
   131 
       
   132     return s;
       
   133 }
       
   134 
       
   135 void fpcrtl_fillChar__vars(void *x, SizeInt count, Byte value) {
       
   136     memset(x, value, count);
       
   137 }
       
   138 
       
   139 void fpcrtl_new__vars(void **p, int size) {
       
   140     *p = malloc(size);
       
   141 }
       
   142 
       
   143 Integer fpcrtl_trunc(extended n) {
       
   144     return (int) n;
       
   145 }
       
   146 
       
   147 LongInt str_to_int(char *src)
       
   148 {
       
   149     int i;
       
   150     int len = strlen(src);
       
   151     char *end;
       
   152     for(i = 0; i < len; i++)
       
   153     {
       
   154         if(src[i] == '$'){
       
   155             // hex
       
   156             return strtol(src + i + 1, &end, 16);
       
   157         }
       
   158     }
       
   159 
       
   160     // decimal
       
   161     return atoi(src);
       
   162 }
       
   163 void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, LongInt *a,
       
   164         LongInt *c) {
       
   165     *c = 0; // no error
       
   166     FIX_STRING(s);
       
   167     *a = str_to_int(s.str);
       
   168 }
       
   169 
       
   170 void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, Byte *a,
       
   171         LongInt *c) {
       
   172     *c = 0; // no error
       
   173     FIX_STRING(s);
       
   174     *a = str_to_int(s.str);
       
   175 }
       
   176 
       
   177 void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, LongWord *a,
       
   178         LongInt *c) {
       
   179     *c = 0; // no error
       
   180     FIX_STRING(s);
       
   181     *a = str_to_int(s.str);
       
   182 }
       
   183 
       
   184 LongInt fpcrtl_random(LongInt l) {
       
   185     return (LongInt) (rand() / (double) RAND_MAX * l);
       
   186 }
       
   187 
       
   188 void __attribute__((overloadable)) fpcrtl_str__vars(float x, string255 *s) {
       
   189     sprintf(s->str, "%f", x);
       
   190     s->len = strlen(s->str);
       
   191 }
       
   192 void __attribute__((overloadable)) fpcrtl_str__vars(double x, string255 *s) {
       
   193     sprintf(s->str, "%f", x);
       
   194     s->len = strlen(s->str);
       
   195 }
       
   196 void __attribute__((overloadable)) fpcrtl_str__vars(uint8_t x, string255 *s) {
       
   197     sprintf(s->str, "%u", x);
       
   198     s->len = strlen(s->str);
       
   199 }
       
   200 void __attribute__((overloadable)) fpcrtl_str__vars(int8_t x, string255 *s) {
       
   201     sprintf(s->str, "%d", x);
       
   202     s->len = strlen(s->str);
       
   203 }
       
   204 void __attribute__((overloadable)) fpcrtl_str__vars(uint16_t x, string255 *s) {
       
   205     sprintf(s->str, "%u", x);
       
   206     s->len = strlen(s->str);
       
   207 }
       
   208 void __attribute__((overloadable)) fpcrtl_str__vars(int16_t x, string255 *s) {
       
   209     sprintf(s->str, "%d", x);
       
   210     s->len = strlen(s->str);
       
   211 }
       
   212 void __attribute__((overloadable)) fpcrtl_str__vars(uint32_t x, string255 *s) {
       
   213     sprintf(s->str, "%u", x);
       
   214     s->len = strlen(s->str);
       
   215 }
       
   216 void __attribute__((overloadable)) fpcrtl_str__vars(int32_t x, string255 *s) {
       
   217     sprintf(s->str, "%d", x);
       
   218     s->len = strlen(s->str);
       
   219 }
       
   220 void __attribute__((overloadable)) fpcrtl_str__vars(uint64_t x, string255 *s) {
       
   221     sprintf(s->str, "%llu", x);
       
   222     s->len = strlen(s->str);
       
   223 }
       
   224 void __attribute__((overloadable)) fpcrtl_str__vars(int64_t x, string255 *s) {
       
   225     sprintf(s->str, "%lld", x);
       
   226     s->len = strlen(s->str);
       
   227 }
       
   228 
       
   229 /*
       
   230  * XXX No protection currently!
       
   231  */
       
   232 void fpcrtl_interlockedIncrement__vars(int *i) {
       
   233     (*i)++;
       
   234 }
       
   235 
       
   236 void fpcrtl_interlockedDecrement__vars(int *i) {
       
   237     (*i)--;
       
   238 }
       
   239 
       
   240 /*
       
   241  * This function should be called when entering main
       
   242  */
       
   243 void fpcrtl_init(int argc, char** argv) {
       
   244     int i;
       
   245     paramCount = argc;
       
   246 
       
   247     printf("ARGC = %d\n", paramCount);
       
   248 
       
   249     for (i = 0; i < argc; i++) {
       
   250         if (strlen(argv[i]) > 255) {
       
   251             assert(0);
       
   252         }
       
   253         strcpy(params[i].str, argv[i]);
       
   254         params[i].len = strlen(params[i].str);
       
   255     }
       
   256 
       
   257 }
       
   258 
       
   259 int fpcrtl_paramCount() {
       
   260     return paramCount - 1; // ignore the first one
       
   261 }
       
   262 
       
   263 string255 fpcrtl_paramStr(int i) {
       
   264     return params[i];
       
   265 }
       
   266 
       
   267 int fpcrtl_UTF8ToUnicode(PWideChar dest, PChar src, SizeInt maxLen) {
       
   268     //return swprintf(dest, maxLen, L"%hs", "src"); //doesn't work in emscripten
       
   269     return 0;
       
   270 }
       
   271 
       
   272 uint32_t __attribute__((overloadable)) fpcrtl_lo(uint64_t i) {
       
   273     return (i & 0xFFFFFFFF);
       
   274 }
       
   275