Program Counter Implementation

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

Program Counter Implementation

Zebo
I keep getting a comparison failure at line 9 with my implementation that seems perfectly fine.  All the internal pins seem to be giving what I need but I think that I need one more clock delay.  However, when I try remedy this by adding another register then I get a comparison failure at line 5.

Here is the chip:

CHIP PC {

    IN  in[16], load, inc, reset;
    OUT out[16];

    PARTS:
    Mux16(a=new, b=in, sel=load, out=out1);
    Mux16(a=false, b[1..15]=false, b[0]=true, sel=inc, out=out2);
    Add16(a=out1, b=out2, out=out3);
    Mux16(a=out3, b=false, sel=reset, out=out4);
    Register(in=out4, load=true, out=new, out=out);
}

Any general advice?

By the way, thank you Mark for your help in my other 2 threads, that helped me out a ton.
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

cadet1620
Administrator
Zebo wrote
I keep getting a comparison failure at line 9 with my implementation that seems perfectly fine.  All the internal pins seem to be giving what I need but I think that I need one more clock delay.  However, when I try remedy this by adding another register then I get a comparison failure at line 5.
I haven't looked at your HDL in detail, but it looks like you may have a priority problem with load vs. inc.  The spec says that if reset is active, load and inc are don't cares.  When load is active, inc is don't care.

inc and load might be interacting in the Add16.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

Rather Iffy
In reply to this post by Zebo
I have found a solution incorporating an ALU as part of the PC and it came through the test.
I don't know exactly how much of the solution i can give away here.


Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

cadet1620
Administrator
Rather Iffy wrote
I have found a solution incorporating an ALU as part of the PC and it came through the test.
I don't know exactly how much of the solution i can give away here.
For TECS if it passes the tests it's good.  TECS encourages encapsulated thinking; and using the ALU to compute what the next state of the PC should be is quite clever.

In general, though, it is easier in hardware to choose things based on control signals than to do conditional computation based on control signals.  Think about what chips you've built that let you make choices.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

Tom1984
In reply to this post by cadet1620
cadet1620 wrote
The spec says that if reset is active, load and inc are don't cares.  When load is active, inc is don't care.
Firstly, as a self-learner trying to follow TECS, I'd like to say a huge "thank you" to the creators of this forum and to the admins who are helping people like me! I'll try to ask my question without posting spoilers...

I've been trying to follow the advice in this thread, but am still completely stuck on this chip. Cadet's  comment above suggests to me that the logic of the circuit needs to be (working from in to out):

In > Increment? > Load? > Reset? > Out

I think I have written my HDL in this way, starting with a Mux16 to select either "in" or "in" passed through the Incrementer chip from Ch2, followed by a Register chip, and finally a Mux16 to select either the previous operations or 0. My comparison fails almost immediately.

Am I on anything near the right lines here please?
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

ybakos
You're on the right track!
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

Tom1984
Thanks for the quick reply, ybakos.

I'll have another look at my HDL for the approach I've been following to see what it is I'm missing here...
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

cadet1620
Administrator
In reply to this post by Tom1984
Tom1984 wrote
In > Increment? > Load? > Reset? > Out
Where in this sequence should the DFF go so that it is affected by all the inputs?

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

Tom1984
I'm still really struggling with this chip, and I wonder if it's because I have misunderstood the notes in the Implementation section:

Counter A w-bit counter consists of two main elements: a regular w-bit register, and combinational logic. (TECS p52)

I'm reading this as saying "the only sequential logic employed here is a single, standard, 16-bit register". But then I'm having trouble seeing how the counter can store the values set by inc and reset, when load only determines whether or not the original input is fed in.

EDIT: If load is a don't care when reset is active, how is "0" fed back into the chip to be repeated / incremented?

cadet1620 wrote
Where in this sequence should the DFF go
Do you mean there needs to be a further DFF/DFFs after the register, Mark?

Thanks
Tom
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

cadet1620
Administrator
Sorry to confuse you.  I meant Register but my traitorous fingers typed DFF.

The PC is similar to the Bit in construction, except that Register replaces the DFF. The combinatorial logic for Bit switches between reloading the DFF output or the input value depending on load.
The PC's combinatorial logic works similarly, but as you have identified, there are more choices.

If you want more explicit help, you can contact me directly via email (More>Reply to author).

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

Tom1984
Got it! :) Thanks for taking the time to help, Mark and ybakos. After sleeping on this, I saw what you were getting at.

I realised how it's possible to use gates created earlier to switch the register's load pin on or off as required, based on the other options that need to be selected.

Tom
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

AndyGeo
Just implemented my PC. This was one of the hardest chips for me so far. It took me a while to understand the DFF and I don't think i fully do. I had the same realization as Tom eventually.
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

CaptainBarnacles
Many thanks for all the advice about the program counter chip (and indeed about the course as a whole, which is fascinating) - it is very helpful to read other people's experiences.  I've now got a working version that passes the test cases but I have an uneasy feeling that my solution might be a bit more complicated than totally necessary and that there is a 'right' way of solving this that the authors are trying to guide me towards.

I guess that a good metric for complexity is the number of component parts.  My solution uses two 'OR' gates, three 'Mux16' gates, a 'Register' and an 'Inc16'.  Is this optimum or should I be looking for a simpler way of doing this?

 
Many thanks.

Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

cadet1620
Administrator
CaptainBarnacles wrote
I guess that a good metric for complexity is the number of component parts.  My solution uses two 'OR' gates, three 'Mux16' gates, a 'Register' and an 'Inc16'.  Is this optimum or should I be looking for a simpler way of doing this?
Your solution is good. There are some some things that can make it a bit better, but don't spend time trying to optimize it now. I recommend that you finish the logic part of the book (chapters 1-5) and then go back to look at your earlier circuits and you will likely see ways to improve them.

For the PC, the first question is "Do I really need logic to control the load input to the Register?"

If you want to, send me your PC.hdl and I'll send you a collection of various implementations.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

CaptainBarnacles
marvellous, I'll carry on through the first five chapters and see how I get on.  Thank you.

I'm finding this course most interesting - I worked through the Petzold 'Code' book a few years ago but while I felt I understood the text (and I do recommend his book), I struggled to really 'get it' - now with  the TECS book, by doing the hands-on construction, I'm really beginning to grasp the fundamental leap of imagination from gates to software which has always eluded me.
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

ammarr
In reply to this post by CaptainBarnacles
Thanks alot every one in this Post

seriously the PC chip was the most difficult to be implemented
it took me 2 days trying to figure out how to store the value and reuse it
and finally i realized the post mentioned by Zebo

that i can use the output of a register as an input to the same chip

Mux16(a=new, b=in, sel=load, out=out1);
Register(in=out4, load=true, out=new, out=out);

after i figured how to use the output as the input the rest is simple
just followed the rules mentioned in the chip

increment > Load > reset

so i first increment and Mux it

then Load and Mux it

last i Reset and Mux it

finally use the ORs to

        Or(a=inc, b=load, out=l1);
        Or(a=l1,b=reset, out=l2);

before Register  the final result

hope it enlightens new students

Thanks all again
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

JustinM
WHEW... got it... Just the RAM chips left in the b folder and I can move on!

Thanks Ammarr, these words made the "light" go on insight my head... a joyous feeling.

increment > Load > reset

so i first increment and Mux it

then Load and Mux it

last i Reset and Mux it


That gave me the insight that I would need to reverse the pin arrangements (work from inside the nested if statements and out). Forcing my brain to stop thinking procedurally is not easy.

But guys... there's no need for the OR chips.
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

JustinM
Here is an outline of how I designed the PC without any OR chips. I hope this doesn't give away too much.
PC_outline.JPG
Reply | Threaded
Open this post in threaded view
|

Re: Program Counter Implementation

huskeyw
In reply to this post by cadet1620
cadet1620 wrote
I haven't looked at your HDL in detail, but it looks like you may have a priority problem with load vs. inc.  The spec says that if reset is active, load and inc are don't cares.  When load is active, inc is don't care.

inc and load might be interacting in the Add16.

--Mark
best advice.. took 2 min once I saw this quote.. worked on it for an hour and it kept failing at different test numbers, saw this changed my mux order and worked first time..