I have an Arithmetic Logic Unit, as shown below (I also included a CircuitJS simulation of it, so it will be much easier to understand the inner working of said ALU).
‘x’ and ‘y’ are the 4-bit input nibbles (operands). (The ALU accepts Natural Binary Code as well as U2 negative numbers [Unfortunately I don’t know why, but “it just works…” – my lecturer has not explained it thoroughly, probably because of the lack of time] ); ‘zx’ basically zeroes the entire ‘x’ nibble regardless of what is currently written to it; ‘nx’ negates the ‘x’ nibble (it, however, may be possible to firstly zero the ‘x’ nibble and then negate it to achieve a nibble consisting of ones – 1111).
The same logic applies to ‘zy’ and ‘ny’. The ‘f’ input controls the operation done by the ALU. If ‘f’ is equal to 0, the ALU performs a logical AND operation. If, however, it is equal to 1, then the ALU performs an arithmetical sum operation (there is a full adder inside the ALU). The ‘no’ input simply negates the output of the ALU.
Below I attached an example table consisting of input values and expected results. Now my question is: How can I work out the combination of the inputs ‘zx’, ‘nx’, ‘zy’, ‘ny’, ‘f’ and ‘no’ that will give me these results: -x, x-y, y-x, x+1, y+1? The answers are shown below, in the after-mentioned table.
I understand the logic behind the other combinations, they are pretty trivial to work out (besides the ‘x OR y’, that requires the De’Morgan’s theorem), but I have no idea, how one could work out the required combinations for the mentioned results. I checked the combinations shown in the attached table, and they all seem to work, but I cannot imagine that someone plugged random combinations, observed the output and, using his/her mind worked out what they must have done… It’s pretty hard to wrap my head around this stuff. Thanks for every, even little bit of help :).
The table with inputs and expected outputs:
Here is the link to a paste that includes the code, which can be used in CircuitJS simulator: ALU simulation code
You can test the ALU by yourself by going to this website: CircuitJS, pressing File → Import From Text… and pasting the copied code.
Best Answer
Important is to understand how negative numbers in binary work.
2 bit Two's complement (how to pass from positive 1 to negative 1):
3 bit two's complement (same as before)
So from here we can see that just inverting the variable/output is the negative/inverse (multiplied by -1) representation of the output -1.
Here is an approach (with thought process) that might help you.
Starting from the output, working your way to the input you can see the following: Output is Q Before the last Mux lets call it Q'
Lets call the last muxer M and the before last M'
Looking at what comes out of the initial MUXs on the left you can see that n(x/y) and z(x/y) select what gets input to the AND and the ADDER.
so given that you are looking for each of the following cases:
" 0-x, x-y, y-x, x+1, y+1 "
you can already conclude that f needs to select the output from the adder, so f=1
y+1, x+1, 0-x only involve one variable either x or y so the other one will be a fixed value either 0 or 1 (+5V)
y+1: --> zx = 1
x+1, 0-x : --> zy = 1
y+1 and x+1 seems as simple additions but you have no way of representing a +1, if you set nx or ny to 1 you will respresent a "-1" , so you can change x+1 to x-(-1)
so after solving the x-y or y-x you should be able to figure that out.
so x - y is adding x to the 2 complement negative representation of y, that is x+!y+1 , but where are you going to get that +1 from?
Think about what inverting the output of the adder will do...If you negate the output of the adder you will get the negative representation of the sum -1
e.g. 2+1 = 3 --> !3 = -4 --> -(2+1)-1
e.g. -2-1 = -3 --> !(-3) = 2 --> -(-2-1)-1
!(x - y) --> -(x-y)-1 = -x + y -1 = y-x-1 = y +(-x) -1
-x --> !x +1
y + !x +1 -1 --> y + !x
so, that defines that for x-y we want
y pass through , ny = zy = 0,
x inverted , nx = 1, zy =0
f = 1 , output of the adder
n0 = 1, adder output inverted
I believe you should be able to figure out the rest from here on.
Cheers, Pau