diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/interne.c | 164 | 
1 files changed, 120 insertions, 44 deletions
| diff --git a/lib/interne.c b/lib/interne.c index 9e59944..642ce16 100644 --- a/lib/interne.c +++ b/lib/interne.c @@ -1,4 +1,5 @@  #include <stdio.h> +#include <stdlib.h>      #include "interne.h"  #include "simulator.h"  #include "exceptions.h" @@ -18,50 +19,6 @@ void Set(Uint32 * i)      *i &= 1;  }				/* Ok */ -/* Met le bit 'position' à zéro */ -void ResetBit(Uint32 * i, int position) -{ -    if (position < 0 || position > 31) { -	exception(1, _("ResetBit: Incorrect Value")); -    } else { -	Uint32 aux = VAL_MAX - (1 << position); - -	*i &= aux; -    } -}				/* Ok */ - -/* Met le bit 'position' à un */ -void SetBit(Uint32 * i, int position) -{ -    if (position < 0 || position > 31) { -	exception(1, _("SetBit: Incorrect Value")); -    } else { -	Uint32 aux = 1 << position; - -	*i |= aux; -    } -}				/* Ok */ - -/* 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) & 1); -}				/* Ok */ - -/* 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"); -}				/* Ok */ -  /* Extrait un champ dans un mot */  Uint32 champ(Uint32 nombre, int taille)  { @@ -92,3 +49,122 @@ 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); +} + + | 
