-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
115 lines (91 loc) · 3.06 KB
/
utils.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
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
/* The code to read a memory trace and simulate it on the various
caches. */
#include "global.h"
#include "utils.h" // utility functions
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
/* n is a power of two, which one? */
/* 1 -> 0; 2 -> 1; ... */
int which_power(int n)
{
int i = 0;
int t = 1;
while (n > t)
{
t += t;
i += 1;
}
return(i);
}
/* generate a mask of n low order bits */
/* if we want a mask of two bits, shift a 1 two
bits over (100), and then subtract one (011). */
unsigned int mask_of(int n)
{
int m;
if (n <= 0) return(0);
m = (1 << n) - 1;
return(m);
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
Boolean ishex(int c)
{
if (('0' <= c) && (c <= '9')) return(TRUE);
if (('a' <= c) && (c <= 'f')) return(TRUE);
if (('A' <= c) && (c <= 'F')) return(TRUE);
return(FALSE);
}
int hexvalue(int c)
{
if (('0' <= c) && (c <= '9')) return(c - '0');
if (('a' <= c) && (c <= 'f')) return(c - 'a' + 10);
if (('A' <= c) && (c <= 'F')) return(c - 'A' + 10);
return(-1);
}
int decvalue(int c)
{
if (('0' <= c) && (c <= '9')) return(c - '0');
return(-1);
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
/* MALLOC space for a string and copy it */
String remember_string(const String name)
{
size_t n;
String p;
if (name == NULL) return(NULL);
/* get memory to remember file name */
n = strlen(name) + 1;
p = CAST(String , malloc(n));
strcpy(p, name);
return(p);
}
/* ***************************************************************** */
/* */
/* */
/* ***************************************************************** */
int skip_blanks(FILE *file)
{
int c;
while (((c = getc(file)) != EOF) && isspace(c)) /* keep reading */;
return(c);
}
int skip_line(FILE *file)
{
int c;
while (((c = getc(file)) != EOF) && (c != '\n')) /* keep reading */;
c = skip_blanks(file);
return(c);
}