Talk:Four bit adder: Difference between revisions

Line 165:
 
:: By "definition", it can be done that way. But note that C impl is sequential, so it is not possible to use as input an output produced later; on the other hand, about Go, it seems it would wait for the input to be available... the problem could be that one input will be not available until the output is generated, and it is not generated since both gates will be awaiting for a value to be set in the connection. What does it happen if a random value (0 or 1) is set preliminarly as output for the NOR gates? Does the simulated latch becomes "bistable" as it happens in reality, or rather it will expose a wrong (to be analysed) behaviour? It is of course implementation specific and can be fixed, but current implementation and in particular my sequential approach to the "description" of the circuit does not help. --[[User:ShinTakezou|ShinTakezou]] 18:23, 17 June 2010 (UTC)
 
::: I was thinking of something like this: <lang c>#include <stdio.h>
typedef char pin_t;
#define IN const pin_t *
#define OUT pin_t *
#define STATE pin_t *
#define PIN(X) pin_t _##X; pin_t *X = & _##X;
#define V(X) (*(X))
/* a NOT that does not soil the rest of the host of the single bit */
#define NOT(X) (~(X)&1)
#define NOR(X,Y) (NOT((X)|(Y)))
 
void latch(IN a, STATE b, STATE c, IN d)
{
pin_t B= V(b);
pin_t C= V(c);
V(b)= NOR(V(a),V(c));
V(c)= NOR(V(b),V(d));
}
 
int main()
{
PIN(p0); PIN(p1); PIN(p2); PIN(p3);
V(p0)= V(p1)= V(p2)= V(p3)= 0;
int j;
for (j= 0; j < 16; j++) {
int bit= 4&j ?1 :0;
V(p0)= 1&j ?bit :NOT(bit);
V(p3)= 2&j ?bit :NOT(bit);
if (V(p0) && V(p3)) continue; /* forbidden case */
else if (V(p0)) printf("%d\tset\t", j);
else if (V(p3)) printf("%d\treset\t", j);
else printf("%d\t\t", j);
printf("%d%d%d%d\t", V(p0),V(p1),V(p2),V(p3));
latch(p0, p1, p2, p3);
printf("%d%d%d%d\t", V(p0),V(p1),V(p2),V(p3));
if (V(p2)) printf("is on\n");
else if (V(p1)) printf("is off\n");
else printf("is switching\n");
printf("\t\t");
V(p0)= 0;
V(p3)= 0;
printf("%d%d%d%d\t", V(p0),V(p1),V(p2),V(p3));
latch(p0, p1, p2, p3);
printf("%d%d%d%d\t", V(p0),V(p1),V(p2),V(p3));
if (V(p2)) printf("is on\n");
if (V(p1)) printf("is off\n");
}
return 0;
}</lang> --[[User:Rdm|Rdm]] 21:05, 17 June 2010 (UTC)
6,951

edits