summaryrefslogtreecommitdiff
path: root/csim/util.c
blob: c815e8e3f76ae04a48bae6f01baf22ece36fda5a (plain) (blame)
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
/*
	Système Digital
	2013-2014
	Alex AUVOLAT

	util.c	Various utility functions used elsewhere
*/


#include "sim.h"

int pow2(int exp) {
	return (1 << exp);
}

t_value read_bool(FILE *stream, t_value *mask) {
	t_value r = 0;
	t_value pow = 1;

	char c;
	if (mask != NULL) *mask = 0;

	for(;;) {
		fscanf(stream, "%c", &c);
		if (c == '1') {
			r |= pow;
		} else if (c != '0') {
			break;
		}
		if (mask != NULL) (*mask) |= pow;

		pow *= 2;
	}

	return r;
}

int is_prefix(const char *prefix, const char *str) {
	while (*prefix) {
		if (*prefix != *str) return 0;
		prefix++;
		str++;
	}
	return 1;
}