forked from vhelin/wla-dx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
841 lines (702 loc) · 21.3 KB
/
main.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
/*
wla - part of wla dx gb-z80/z80/6502/65c02/6510/6800/6801/6809/65816/huc6280/spc-700/8008/8080
macro assembler package by ville helin <[email protected]>. this is gpl software.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef UNIX
#include <unistd.h>
#elif defined(WIN32)
/* Windows.h can't be included since it relies on compiler extensions */
typedef unsigned long DWORD;
DWORD __stdcall GetCurrentProcessId(void);
#endif
#include "main.h"
#include "defines.h"
#include "parse.h"
#include "include_file.h"
#include "pass_1.h"
#include "pass_2.h"
#include "pass_3.h"
#include "pass_4.h"
#include "listfile.h"
#include "hashmap.h"
#include "printf.h"
FILE *file_out_ptr = NULL;
/* amiga specific definitions */
#ifdef AMIGA
__near long __stack = 200000;
#endif
char version_string[] = "$VER: wla-" WLA_NAME " 9.12a (9.1.2021)";
char wla_version[] = "9.12";
char *tmp_name = NULL;
extern struct incbin_file_data *incbin_file_data_first, *ifd_tmp;
extern struct file_name_info *file_name_info_first;
extern struct label_def *label_tmp, *labels;
extern struct map_t *global_unique_label_map;
extern struct macro_static *macros_first;
extern struct definition *tmp_def;
extern struct map_t *defines_map;
extern struct export_def *export_first, *export_last;
extern struct stack *stacks_first, *stacks_tmp, *stacks_last, *stacks_header_first, *stacks_header_last;
extern struct repeat_runtime *repeat_stack;
extern struct section_def *sections_first;
extern struct macro_runtime *macro_stack;
extern struct label_def *unknown_labels;
extern struct filepointer *filepointers;
extern struct map_t *namespace_map;
extern struct append_section *append_sections;
extern struct label_sizeof *label_sizeofs;
extern struct block_name *block_names;
extern struct stringmaptable *stringmaptables;
extern char mem_insert_action[MAX_NAME_LENGTH*3 + 1024];
extern char *unfolded_buffer, *label_stack[256];
extern char *include_in_tmp, *tmp_a;
extern char *rom_banks, *rom_banks_usage_table;
extern char *include_dir, *buffer, *full_name;
extern int include_in_tmp_size, tmp_a_size, *banks, *bankaddress;
int output_format = OUTPUT_NONE, verbose_mode = OFF, test_mode = OFF;
int extra_definitions = OFF, commandline_parsing = ON, makefile_rules = NO;
int listfile_data = NO, quiet = NO, use_incdir = NO, little_endian = YES;
int create_sizeof_definitions = YES;
char *final_name = NULL, *asm_name = NULL;
struct ext_include_collection ext_incdirs;
int main(int argc, char *argv[]) {
int parse_flags_result, n_ctr, q, include_size = 0;
if (sizeof(double) != 8) {
fprintf(stderr, "MAIN: sizeof(double) == %d != 8. WLA will not work properly.\n", (int)sizeof(double));
return -1;
}
atexit(procedures_at_exit);
/* init the randon number generator */
init_genrand((unsigned long)time(NULL));
/* initialize our external include dir collection */
ext_incdirs.count = 0;
ext_incdirs.names = NULL;
ext_incdirs.max_name_size_bytes = MAX_NAME_LENGTH + 1;
/* select little/big endianess */
#if defined(MC6800) || defined(MC6801) || defined(MC6809)
little_endian = NO;
#else
little_endian = YES;
#endif
/* init mem_insert() buffer */
mem_insert_action[0] = 0;
/* init hashmaps */
defines_map = hashmap_new();
global_unique_label_map = hashmap_new();
namespace_map = hashmap_new();
/* init label stack */
for (q = 0; q < 256; q++)
label_stack[q] = NULL;
for (q = 0; q < 256; q++) {
label_stack[q] = calloc(MAX_NAME_LENGTH + 1, 1);
if (label_stack[q] == NULL)
return 1;
}
parse_flags_result = FAILED;
if (argc >= 2) {
parse_flags_result = parse_flags(argv, argc);
if (output_format == OUTPUT_NONE) {
if (parse_flags_result == SUCCEEDED) {
/* assume object file output name */
output_format = OUTPUT_OBJECT;
final_name = calloc(strlen(asm_name)+1, 1);
for (n_ctr = 0; n_ctr < (int)strlen(asm_name) && *((asm_name) + n_ctr) != '.'; n_ctr++)
final_name[n_ctr] = *((asm_name) + n_ctr);
final_name[n_ctr++] = '.';
final_name[n_ctr++] = 'o';
final_name[n_ctr] = 0;
}
}
}
if (output_format == OUTPUT_NONE || parse_flags_result == FAILED) {
printf("\nWLA " ARCH_STR " Macro Assembler v9.12a\n");
printf("Written by Ville Helin in 1998-2008 - In GitHub since 2014: https://github.com/vhelin/wla-dx\n");
#ifdef WLA_DEBUG
printf("*** WLA_DEBUG defined - this executable is running in DEBUG mode ***\n");
#endif
printf("%s\n\n", version_string);
printf("USAGE: %s [OPTIONS] <OUTPUT> <ASM FILE>\n\n", argv[0]);
printf("Options:\n");
printf("-i Add list file information\n");
printf("-M Output makefile rules\n");
printf("-q Quiet\n");
printf("-s Don't create _sizeof_* definitions\n");
printf("-t Test compile\n");
printf("-v Verbose messages\n");
printf("-x Extra compile time labels & definitions\n");
printf("-I <DIR> Include directory\n");
printf("-D <DEF> Declare definition\n\n");
printf("Output:\n");
printf("-o <FILE> Output object file\n");
printf("-l <FILE> Output library file\n\n");
printf("EXAMPLE: %s -D VERSION=1 -D TWO=2 -v -o main.obj main.asm\n\n", argv[0]);
return 0;
}
if (strcmp(asm_name, final_name) == 0) {
fprintf(stderr, "MAIN: Input and output files share the same name!\n");
return 1;
}
generate_tmp_name(&tmp_name);
file_out_ptr = fopen(tmp_name, "wb");
if (file_out_ptr == NULL) {
fprintf(stderr, "MAIN: Error opening file \"%s\" for writing.\n", tmp_name);
return 1;
}
/* small inits */
if (extra_definitions == ON)
generate_extra_definitions();
commandline_parsing = OFF;
/* start the process */
if (include_file(asm_name, &include_size, NULL) == FAILED)
return 1;
if (pass_1() == FAILED)
return 1;
if (pass_2() == FAILED)
return 1;
if (pass_3() == FAILED)
return 1;
if (listfile_data == YES) {
if (listfile_collect() == FAILED)
return 1;
}
if (pass_4() == FAILED)
return 1;
return 0;
}
int parse_flags(char **flags, int flagc) {
int asm_name_def = 0, count;
char *str_build;
for (count = 1; count < flagc; count++) {
if (!strcmp(flags[count], "-o")) {
if (output_format != OUTPUT_NONE)
return FAILED;
output_format = OUTPUT_OBJECT;
if (count + 1 < flagc) {
/* set output */
final_name = calloc(strlen(flags[count+1])+1, 1);
strcpy(final_name, flags[count+1]);
}
else
return FAILED;
count++;
continue;
}
else if (!strcmp(flags[count], "-l")) {
if (output_format != OUTPUT_NONE)
return FAILED;
output_format = OUTPUT_LIBRARY;
if (count + 1 < flagc) {
/* set output */
final_name = calloc(strlen(flags[count+1])+1, 1);
strcpy(final_name, flags[count+1]);
}
else
return FAILED;
count++;
continue;
}
else if (!strcmp(flags[count], "-D")) {
if (count + 1 < flagc) {
if (count + 3 < flagc) {
if (!strcmp(flags[count+2], "=")) {
int length = (int)strlen(flags[count+1])+(int)strlen(flags[count+3])+2;
str_build = calloc(length, 1);
snprintf(str_build, length, "%s=%s", flags[count+1], flags[count+3]);
parse_and_add_definition(str_build, NO);
free(str_build);
count += 2;
}
else
parse_and_add_definition(flags[count+1], NO);
}
else
parse_and_add_definition(flags[count+1], NO);
}
else
return FAILED;
count++;
continue;
}
else if (!strcmp(flags[count], "-I")) {
if (count + 1 < flagc) {
/* get arg */
parse_and_add_incdir(flags[count+1], NO);
}
else
return FAILED;
count++;
continue;
}
else if (!strcmp(flags[count], "-i")) {
listfile_data = YES;
continue;
}
else if (!strcmp(flags[count], "-v")) {
verbose_mode = ON;
continue;
}
else if (!strcmp(flags[count], "-s")) {
create_sizeof_definitions = NO;
continue;
}
else if (!strcmp(flags[count], "-t")) {
test_mode = ON;
continue;
}
else if (!strcmp(flags[count], "-M")) {
makefile_rules = YES;
test_mode = ON;
verbose_mode = OFF;
quiet = YES;
continue;
}
else if (!strcmp(flags[count], "-q")) {
quiet = YES;
continue;
}
else if (!strcmp(flags[count], "-x")) {
extra_definitions = ON;
continue;
}
else {
if (count == flagc - 1) {
asm_name = calloc(strlen(flags[count]) + 1, 1);
strcpy(asm_name, flags[count]);
count++;
asm_name_def++;
}
else {
/* legacy support? */
if (strncmp(flags[count], "-D", 2) == 0) {
/* old define */
parse_and_add_definition(flags[count], YES);
continue;
}
else if (strncmp(flags[count], "-I", 2) == 0) {
/* old include directory */
parse_and_add_incdir(flags[count], YES);
continue;
}
else
return FAILED;
}
}
}
if (asm_name_def <= 0)
return FAILED;
return SUCCEEDED;
}
void procedures_at_exit(void) {
struct file_name_info *f, *ft;
struct export_def *export_tmp;
struct section_def *s1, *s2;
struct label_def *l1, *l2;
struct macro_static *m;
struct filepointer *f1, *f2;
struct append_section *as;
struct label_sizeof *ls;
struct block_name *bn;
int i, index;
/* free all the dynamically allocated data structures and close open files */
if (file_out_ptr != NULL)
fclose(file_out_ptr);
free(macro_stack);
free(repeat_stack);
free(final_name);
free(asm_name);
free(include_dir);
free(full_name);
for (i = 0; i < 256; i++)
free(label_stack[i]);
if (defines_map != NULL) {
hashmap_free_all_elements(defines_map);
hashmap_free(defines_map);
}
if (global_unique_label_map != NULL) {
/* don't free_all_elements, since labels contains _all_ labels. */
hashmap_free(global_unique_label_map);
}
if (namespace_map != NULL) {
hashmap_free_all_elements(namespace_map);
hashmap_free(namespace_map);
}
m = macros_first;
while (m != NULL) {
/* free the argument labels */
if (m->nargument_names > 0) {
for (i = 0; i < m->nargument_names; i++)
free(m->argument_names[i]);
free(m->argument_names);
}
macros_first = m->next;
free(m);
m = macros_first;
}
label_tmp = labels;
while (label_tmp != NULL) {
labels = label_tmp->next;
free(label_tmp);
label_tmp = labels;
}
l1 = unknown_labels;
while (l1 != NULL) {
l2 = l1->next;
free(l1);
l1 = l2;
}
ls = label_sizeofs;
while (ls != NULL) {
label_sizeofs = ls->next;
free(ls);
ls = label_sizeofs;
}
bn = block_names;
while (bn != NULL) {
block_names = bn->next;
free(bn);
bn = block_names;
}
export_tmp = export_first;
while (export_tmp != NULL) {
export_last = export_tmp->next;
free(export_tmp);
export_tmp = export_last;
}
ifd_tmp = incbin_file_data_first;
while(ifd_tmp != NULL) {
incbin_file_data_first = ifd_tmp->next;
free(ifd_tmp->data);
free(ifd_tmp->name);
free(ifd_tmp);
ifd_tmp = incbin_file_data_first;
}
stacks_tmp = stacks_first;
while (stacks_tmp != NULL) {
free(stacks_tmp->stack);
stacks_first = stacks_tmp->next;
free(stacks_tmp);
stacks_tmp = stacks_first;
}
stacks_tmp = stacks_header_first;
while (stacks_tmp != NULL) {
free(stacks_tmp->stack);
stacks_first = stacks_tmp->next;
free(stacks_tmp);
stacks_tmp = stacks_first;
}
as = append_sections;
while (as != NULL) {
append_sections = as->next;
free(as);
as = append_sections;
}
free(unfolded_buffer);
free(buffer);
free(include_in_tmp);
free(tmp_a);
free(rom_banks);
free(rom_banks_usage_table);
free(banks);
free(bankaddress);
f = file_name_info_first;
while (f != NULL) {
free(f->name);
ft = f->next;
free(f);
f = ft;
}
s1 = sections_first;
while (s1 != NULL) {
free(s1->data);
free(s1->listfile_cmds);
free(s1->listfile_ints);
hashmap_free(s1->label_map);
s2 = s1->next;
free(s1);
s1 = s2;
}
f1 = filepointers;
while (f1 != NULL) {
f2 = f1->next;
if (f1->f != NULL)
fclose(f1->f);
free(f1->filename);
free(f1);
f1 = f2;
}
while (stringmaptables != NULL) {
struct stringmaptable *sm = stringmaptables;
while (sm->entries != NULL) {
struct stringmap_entry *e = sm->entries;
free(e->bytes);
free(e->text);
sm->entries = e->next;
free(e);
}
stringmaptables = sm->next;
free(sm);
}
/* remove the tmp files */
if (tmp_name != NULL)
remove(tmp_name);
/* cleanup any incdirs we added */
for (index = 0; index < ext_incdirs.count; index++)
free(ext_incdirs.names[index]);
free(ext_incdirs.names);
}
int generate_tmp_name(char **filename) {
#if defined(UNIX) || defined(WIN32)
static char name[32]; /* should be enough */
int status;
int pid;
#if defined(UNIX)
pid = (int)getpid();
#elif defined(WIN32)
pid = GetCurrentProcessId();
#else
#error "Invalid configuration!"
#endif
status = snprintf(name, sizeof(name)-1, ".wla%da", pid) + 1;
if (status >= (int)sizeof(name)) {
fprintf(stderr, "MAIN: Temp filename exceeded limit: %d >= %d! "
"Aborting...\n", status, (int)sizeof(name));
abort();
}
*filename = name;
#else /* AMIGA, WIN32, MSDOS and others */
*filename = "wla_a.tmp";
#endif
return SUCCEEDED;
}
int generate_extra_definitions(void) {
char *q, tmp[256];
time_t ti;
int i, length;
/* generate WLA_TIME */
time(&ti);
q = ctime(&ti);
strcpy(tmp, q);
/* remove the linefeed */
length = (int)strlen(tmp);
for (i = 0; i < length; i++) {
if (tmp[i] == 0x0A) {
tmp[i] = 0;
break;
}
}
if (add_a_new_definition("WLA_TIME", 0.0, tmp, DEFINITION_TYPE_STRING, (int)strlen(tmp)) == FAILED)
return FAILED;
if (add_a_new_definition("wla_time", 0.0, tmp, DEFINITION_TYPE_STRING, (int)strlen(tmp)) == FAILED)
return FAILED;
if (add_a_new_definition("WLA_VERSION", 0.0, wla_version, DEFINITION_TYPE_STRING, (int)strlen(wla_version)) == FAILED)
return FAILED;
if (add_a_new_definition("wla_version", 0.0, wla_version, DEFINITION_TYPE_STRING, (int)strlen(wla_version)) == FAILED)
return FAILED;
return SUCCEEDED;
}
int parse_and_add_definition(char *c, int contains_flag) {
char n[MAX_NAME_LENGTH + 1], *value;
int i;
/* skip the flag? */
if (contains_flag == YES)
c += 2;
for (i = 0; i < MAX_NAME_LENGTH && *c != 0 && *c != '='; i++, c++)
n[i] = *c;
n[i] = 0;
if (*c == 0)
return add_a_new_definition(n, 0.0, NULL, DEFINITION_TYPE_VALUE, 0);
else if (*c == '=') {
c++;
if (*c == 0)
return FAILED;
value = c;
/* hexadecimal value? */
if (*c == '$' || ((c[strlen(c)-1] == 'h' || c[strlen(c)-1] == 'H') && (*c >= '0' && *c <= '9'))) {
if (*c == '$')
c++;
for (i = 0; *c != 0; c++) {
if (*c >= '0' && *c <= '9')
i = (i << 4) + *c - '0';
else if (*c >= 'a' && *c <= 'f')
i = (i << 4) + *c - 'a' + 10;
else if (*c >= 'A' && *c <= 'F')
i = (i << 4) + *c - 'A' + 10;
else if ((*c == 'h' || *c == 'H') && *(c+1) == 0)
break;
else {
fprintf(stderr, "PARSE_AND_ADD_DEFINITION: Error in value (%s).\n", value);
return FAILED;
}
}
return add_a_new_definition(n, (double)i, NULL, DEFINITION_TYPE_VALUE, 0);
}
/* decimal value? */
if (*c >= '0' && *c <= '9') {
for (i = 0; *c != 0; c++) {
if (*c >= '0' && *c <= '9')
i = (i * 10) + *c - '0';
else {
fprintf(stderr, "PARSE_AND_ADD_DEFINITION: Error in value (%s).\n", value);
return FAILED;
}
}
return add_a_new_definition(n, (double)i, NULL, DEFINITION_TYPE_VALUE, 0);
}
/* quoted string? */
if (*c == '"' && c[strlen(c) - 1] == '"') {
int t;
char *s = calloc(strlen(c) + 1, 1);
int result;
c++;
for (t = 0; *c != 0; c++, t++) {
if (*c == '\\' && *(c + 1) == '"') {
c++;
s[t] = '"';
}
else if (*c == '"') {
c++;
break;
}
else
s[t] = *c;
}
s[t] = 0;
if (*c == 0)
result = add_a_new_definition(n, 0.0, s, DEFINITION_TYPE_STRING, (int)strlen(s));
else {
fprintf(stderr, "PARSE_AND_ADD_DEFINITION: Incorrectly terminated quoted string (%s).\n", value);
result = FAILED;
}
free(s);
return result;
}
/* unquoted string */
return add_a_new_definition(n, 0.0, c, DEFINITION_TYPE_STRING, (int)strlen(c));
}
return FAILED;
}
int parse_and_add_incdir(char* c, int contains_flag) {
int old_count = ext_incdirs.count, index, buffer_size, j;
char **new_array;
char n[MAX_NAME_LENGTH + 1];
/* increment for the new entry, then re-allocate the array */
ext_incdirs.count++;
new_array = calloc(ext_incdirs.count * sizeof(char*), 1);
for (index = 0; index < old_count; index++)
new_array[index] = ext_incdirs.names[index];
free(ext_incdirs.names);
ext_incdirs.names = new_array;
buffer_size = ext_incdirs.max_name_size_bytes;
ext_incdirs.names[old_count] = calloc(buffer_size, 1);
/* skip the flag? */
if (contains_flag == YES)
c += 2;
for (j = 0; j < MAX_NAME_LENGTH && *c != 0; j++, c++)
n[j] = *c;
n[j] = 0;
if (*c != 0)
return FAILED;
localize_path(n);
#if defined(MSDOS)
snprintf(ext_incdirs.names[old_count], buffer_size, "%s\\", n);
#else
snprintf(ext_incdirs.names[old_count], buffer_size, "%s/", n);
#endif
use_incdir = YES;
return SUCCEEDED;
}
/*
*
*
* MERSENNE TWISTER
*
*
*/
/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.
Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Any feedback is very welcome.
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
*/
/* Period parameters */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
static unsigned long mt[N]; /* the array for the state vector */
static int mti = N+1; /* mti==N+1 means mt[N] is not initialized */
/* initializes mt[N] with a seed */
void init_genrand(unsigned long s) {
mt[0]= s & 0xffffffffUL;
for (mti = 1; mti < N; mti++) {
mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt[]. */
/* 2002/01/09 modified by Makoto Matsumoto */
mt[mti] &= 0xffffffffUL;
/* for >32 bit machines */
}
}
/* generates a random number on [0,0xffffffff]-interval */
unsigned long genrand_int32(void) {
unsigned long y;
static unsigned long mag01[2] = {0x0UL, MATRIX_A};
/* mag01[x] = x * MATRIX_A for x=0,1 */
if (mti >= N) { /* generate N words at one time */
int kk;
if (mti == N+1) /* if init_genrand() has not been called, */
init_genrand(5489UL); /* a default initial seed is used */
for (kk = 0; kk < N-M; kk++) {
y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
for (; kk < N-1; kk++) {
y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
mti = 0;
}
y = mt[mti++];
/* tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18);
return y;
}