summaryrefslogtreecommitdiff
path: root/lib/interne.c
blob: 642ce16999df2a26e99c85fa10a66c38829db2fb (plain)
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
#include <stdio.h>
#include <stdlib.h>    
#include "interne.h"
#include "simulator.h"
#include "exceptions.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define _(x) x
#endif

void Reset(Uint32 * i)
{				/*  met tous les bits d'un mot à zéro   */
    *i &= 0;
}				/* Ok */

void Set(Uint32 * i)
{				/* met le bit de poids faible à 1       */
    *i &= 1;
}				/* Ok */

/* Extrait un champ dans un mot */
Uint32 champ(Uint32 nombre, int taille)
{
    return (nombre & (taille - 1));
}				/* Ok */

Uint32 Opcode(Uint32 ins)
{
    return (champ(ins >> 0, 256));
}				/* Ok */

Uint32 Extension(Uint32 ins)
{
    return (champ(ins >> 8, 64));
}				/* Ok */

Uint32 Champ1(Uint32 ins)
{
    return (champ(ins >> 14, 64));
}				/* Ok */

Uint32 Champ2(Uint32 ins)
{
    return (champ(ins >> 20, 64));
}				/* Ok */

Uint32 Champ3(Uint32 ins)
{
    return (champ(ins >> 26, 64));
}				/* Ok */
Uint32 ZERO(void)   {   return (0); }
Uint32 ONE(void)    {   return (1); }

/* Met le bit 'position' à zéro */
void ResetBit(Uint32 * i, int position) {
    if (position < 0 || position > 31)
        exception(1, _("ResetBit: Incorrect Value"));          
    else {
        int k;
        Uint32 VAL_MAX = ZERO();
        for (k = 0; k < 32; k++)
            SetBit(&VAL_MAX, k);
        * i &= VAL_MAX - (ONE() << position);
    }
}

/* Met le bit 'position' à un */
void SetBit(Uint32 * i, int position) {
    if (position < 0 || position > 31)
        exception(1, _("SetBit: Incorrect Value"));          
    else {
        Uint32 aux = ONE() << position;
        * i |= aux;
    }
}

/* Met le bit 'position' à la meme valeur que 'v' */
void AffecteBit(Uint32 * a, char v, int position) { (v == 0) ?  ResetBit(&(* a), position) : SetBit(&(* a), position);  }

/* Donne la valeur du bit 'position' */
int ValeurBit(Uint32 nombre, int position) {
    if (position < 0 || position > 31) {
        exception(1, _("ValeurBit: Incorrect Value"));          
        return (-1);
    }
    return ((nombre >> position) & ONE());
}

int Neg(Uint32 a) {
    return (ValeurBit(a, 31));
}

/* Met le flag d'overflow à zéro */
void    SetOverflow(int * o)    {   * o = ONE();    }

/* Met le flag d'overflow à un */
void    ResetOverflow(int * o)  {   * o = ZERO();   }

/* Donne la valeur du flag d'overflow */
int     Overflow(int o)         {   return (o);  }

/* Affiche tous les bits d'un mot */
void Uint32toBin(Uint32 i) {
    int k;
    for (k = 31; k >= 0; k--)
        printf("%d", ValeurBit(i, k));
    printf("\n");
}

/* Effectue le Not d'un bit */
char Not(char a) {  return ((char) ((a == 0) ? 1 : 0));  }

/* Effectue le And de deux bits */
char And(char a, char b) {
    if (a == 0 && b == 0)       return (0);
    if (a == 0 && b == 1)       return (0);
    if (a == 1 && b == 0)       return (0);
                                return (1);
}

/* Effectue le Or de deux bits */
char Or(char a, char b) {
    if (a == 0 && b == 0)       return (0);
    if (a == 0 && b == 1)       return (1);
    if (a == 1 && b == 0)       return (1);
                                return (1);
}

/* Effectue le Xor de deux bits */
char Xor(char a, char b) {
    if (a == 0 && b == 0)       return (0);
    if (a == 0 && b == 1)       return (1);
    if (a == 1 && b == 0)       return (1);
                                return (0);
}

/* Effectue le Nand de deux bits */
char Nand(char a, char b) {
    if (a == 0 && b == 0)       return (1);
    if (a == 0 && b == 1)       return (1);
    if (a == 1 && b == 0)       return (1);
                                return (0);
}

/* Effectue le Nor de deux bits */
char Nor(char a, char b) {
    if (a == 0 && b == 0)       return (1);
    if (a == 0 && b == 1)       return (0);
    if (a == 1 && b == 0)       return (0);
                                return (0);
}

char OrWord(Uint32 a) {
    int i;
    char resultat = (char) ValeurBit(a, 0);
    for (i = 1; i < 32; i++)
        resultat = Or((char) ValeurBit(a, i), resultat);
    return (resultat);
}

char AndWord(Uint32 a) {
    int i;
    char resultat = (char) ValeurBit(a, 0);
    for (i = 1; i < 32; i++)
        resultat = And((char) ValeurBit(a, i), resultat);
    return (resultat);
}