Jump logic

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Jump logic

bwspot
This post was updated on .
It seems like comparing J bits with NG or ZR is needed.
Also to my understanding ALU will never output ng=1 and zr=1 at the same time.

My though process is like this.
Lets say I need to check for Jump JEQ where j1,j2,j3 are: 0 1 0.  (case for alu output = 0)
In such scenario ALU will output:
ZR = 1 and NG = 0.

I tried to AND:
And j1 and ng - if ng is 0 I have a match it with j1 bit.
And j2 and zr - if zr is 1 I have match for j2 bit.
And j3 and ng - if zr is either 0 or 1 I have no match for j3 bit resulting 0.

But in testing above was failing for >0 condition so i came up with below logic just for J3 bit:
J3 zr ng truth table:
1 0 0 - 1 matches >0 so i solved for it
1 0 1 - 0
1 1 0 - 0
1 1 1 - 0

J3 + !ZR + !NG

Above seems to be working but when ng and  zr are both 1 the output is 1 in some cases.
But alu will never output 1 for both zr and ng so would above be acceptable Jump logic solution?


I tried to improve above with below logic:

<0
//j1 +!zr + ng

=0
//J2 + zr  + !ng

<0
//J3 +!zr !ng

It works but it only fails in 2 cases when zr and ng are 1 which would never be a case so technically it would work. Those cases are for checking for NOT 0 and for unconditional jump.

I wonder if both of these are acceptable or i need to improve it?
Once this logic is correct i will add it to the CPU.
I hope anyone here can help me and guide me into the right direction.

thx

Edit: I removed source code per posting guidelines.
Reply | Threaded
Open this post in threaded view
|

Re: Jump logic

bwspot
So i completed my CPU and above logic worked but i am still curious about your comments.
Reply | Threaded
Open this post in threaded view
|

Re: Jump logic

eatmorepies
This post was updated on .
So, you're right that ng and zr will never be 1 at the same time. You might understand why, but in case not (you were vague): it's simply because a number cannot simultaneously be equal to 0 and less than 0 (which are handled by outputting 1 for either zr or ng, respectively).

Here's how I wrote my jump code:
Assuming that only one of our jump bits is set in a given instruction...
If zr is 1 and the middle j value is equal to 1, we should jump (our value is equal to 0, and the "equal to 0" jump bit is set)
If ng is 1 and the first j value (reading from the left) is equal to 1, we should jump (our value is less than 0, and our "less than 0" jump bit is set).

The slightly more difficult portion is when our ALU output is greater than 0. With that, we can check if either zr or ng are equal to 1. If one of them are, our ALU output is NOT greater than 0. If both of them are 0, then the only other possibility is that the ALU output IS greater than 0. Based on this, we can generate and output a bit representing whether the ALU output is greater than 0, and check if it and the relevant jump bit are 1.

We don't need to write any additional code for the >= or <= comparisons; the conditions are satisfied by the above checks, which would output whether a value is greater than, equal to, or less than 0. Likewise, if none of the jump bits are set, we won't jump at all.

When do we want to load a bit for our PC? If we outputted a 1 for either of the three above conditions (checking a jump bit and the relevant bit to see if our ALU output meets that particular criteria), then we want to jump to the instruction outputted from the a register. Otherwise, our load bit is 0, and we either increment or reset the program counter, based on the inputted reset bit.