Explanation of coding

The favorite HDL language in Europe and in Universities

Explanation of coding

Postby knjr » Mon May 16, 2011 3:24 am

Code: Select all
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;

entity my_counter5 is
  port(count: Out std_logic_vector(7 downto 0);
    clk: in std_logic;
    reset: in std_logic);
  end my_counter5;
 
  architecture behav_my_counter5 of my_counter5 is
 
    signal c : std_logic_vector(7 downto 0) := "00000000";
   
    begin
     
      ctr:
     
      process(clk,reset)
         variable carry : std_logic_vector(7 downto 0) := "00000000";
        begin
         
          if reset'event and(reset = '1') then
            c <= (others => '0');
           
          elsif clk'event and (clk = '1') then
          --i am adding "00000001" to 'c'.
          --this is done using basic logic gates.
          -- the equation for full adder is simplified and written below.
          --full adder equations are:
          -- sum = A xor B xor Carry.
          -- carry = (A and B) or (A and carry) or (B and carry).
          --subsititue B with "00000001" here and you will get the below equations.   
            c(0) <= not c(0);
            carry(0) := c(0);
            c(1)<= c(1) xor carry(0);
            carry(1) := c(1) and carry(0);
            c(2)<= c(2) xor carry(1);
            carry(2) := c(2) and carry(1);
            c(3)<= c(3) xor carry(2);
            carry(3) := c(3) and carry(2);
            c(4)<= c(4) xor carry(3);
            carry(4) := c(4) and carry(3);
            c(5)<= c(5) xor carry(4);
            carry(5) := c(5) and carry(4);
            c(6)<= c(6) xor carry(5);
            carry(6) := c(6) and carry(5);
            c(7)<= c(7) xor carry(6);
            carry(7) := c(7) and carry(6);

          end if;
         
        end process;
       
         count <= c;
         
         end behav_my_counter5;


Hi there, can someone please explain this whole coding to me? I don't really understand what's going on with it. As I only get to know this VHDL coding a month ago without anyone's help. Thanks alot!
knjr
 
Posts: 12
Joined: Thu May 12, 2011 5:13 am

Re: Explanation of coding

Postby hamster » Mon May 16, 2011 4:47 am

An assignment I guess?

It is a binary added implemented the hard way... to make it add 5, try changing
Code: Select all
c(2)<= c(2) xor carry(1);
carry(2) := c(2) and carry(1);

to
Code: Select all
c(2)<= c(2) xor carry(1) xor '1';
carry(2) := c(2) or carry(1);


just try to work out how to explain it to the tutor....
hamster
 
Posts: 38
Joined: Mon Sep 27, 2010 10:19 pm

Re: Explanation of coding

Postby knjr » Mon May 16, 2011 5:26 am

Hi, Thanks for helping. Am I suppose to change one of it or all of it?
knjr
 
Posts: 12
Joined: Thu May 12, 2011 5:13 am

Re: Explanation of coding

Postby hamster » Mon May 16, 2011 8:06 am

Why don't you give both a try and see?

It might be a learning experience...
hamster
 
Posts: 38
Joined: Mon Sep 27, 2010 10:19 pm

Re: Explanation of coding

Postby knjr » Mon May 16, 2011 8:11 am

Hi again. Yea, I trying.

But before that, I don't understand those coding & I can't proceed with it.
I need to know what each line does, then I can understand the coding.

c(0) <= not c(0);
carry(0) := c(0);
c(1)<= c(1) xor carry(0);
carry(1) := c(1) and carry(0);
c(2)<= c(2) xor carry(1) xor '1';
carry(2) := c(2) and carry(1);
c(3)<= c(3) xor carry(2);
carry(3) := c(3) and carry(2);
c(4)<= c(4) xor carry(3);
carry(4) := c(4) and carry(3);
c(5)<= c(5) xor carry(4);
carry(5) := c(5) and carry(4);
c(6)<= c(6) xor carry(5);
carry(6) := c(6) and carry(5);
c(7)<= c(7) xor carry(6);
carry(7) := c(7) and carry(6);
knjr
 
Posts: 12
Joined: Thu May 12, 2011 5:13 am

Re: Explanation of coding

Postby hamster » Mon May 16, 2011 8:47 am

Close but you should have

carry(2) := c(2) or carry(1);

where you have

carry(2) := c(2) and carry(1);

Full explanation... you have a number consisting of 8 bits, and you want to add 5 to it.

00001111 <= this is 'C(7 downto 0)', in this case 15 in binary
00000101 <= this is what you are adding, 5 in binary.
========
????????

We are always adding 5, so we can make life simple.
First, flip c(0) (line 1)

00001111 <= this is 'C(7 downto 0)'
00000101 <= this is what you are adding.
========
???????0

If bit 0 was set then you have to carry one to the c(1) column, which is held in carry(0) . So carry(0) is now set to 1. (line 2)

Line three says if c(1) is set, or carry(0) is set, but not both, then c(1) should be set to 1.
???????0 <= this is Carry(7 downto 0)
00001111 <= this is 'C(7 downto 0)'
00000101
========
??????01

Line four says "if carry(0) was 1, and c(1) was 1, then we have to set carry(1) to 1"... and so on and so on..

Draw it on math paper, in pencil and step through it like by line. It will all become clear (I hope!)
hamster
 
Posts: 38
Joined: Mon Sep 27, 2010 10:19 pm

Re: Explanation of coding

Postby knjr » Mon May 16, 2011 9:16 am

Hi, Thanks! I guess it help me to understand clearly. If I don't understand can I ask ?
knjr
 
Posts: 12
Joined: Thu May 12, 2011 5:13 am

Re: Explanation of coding

Postby knjr » Wed May 18, 2011 1:36 am

Hi again. Let's say, if I only want to count from 0000 0000 to 0000 1111 right, what am I suppose to do to the coding? My currently one will keep on count up and so on ..
knjr
 
Posts: 12
Joined: Thu May 12, 2011 5:13 am

Re: Explanation of coding

Postby hamster » Wed May 18, 2011 10:20 am

Easy - either don't do anything to c(4), c(5), c(6) or c(7) , or always set carry(3) to '0'.
hamster
 
Posts: 38
Joined: Mon Sep 27, 2010 10:19 pm

Re: Explanation of coding

Postby knjr » Thu May 19, 2011 2:35 am

Thanks.

I still not quite understand one of the coding

c(0) <= not c(0); - why we need this?
carry(0) := c(0); - does this means the value in c(0) is carry over to carry (0) ?
knjr
 
Posts: 12
Joined: Thu May 12, 2011 5:13 am

Re: Explanation of coding

Postby hamster » Thu May 19, 2011 9:40 am

"c(0) <= not c(0)" is a simplification of "c(0) <= c(0) xor '1';" - it's the thing that is actually adding '1' to bit 0.

carry(0) = c(0) is also a simplification, this time of "carry(0) <= c(0) and '1'"
hamster
 
Posts: 38
Joined: Mon Sep 27, 2010 10:19 pm

Re: Explanation of coding

Postby knjr » Fri May 20, 2011 5:27 am

Code: Select all
      c(0) <= not c(0);
      carry(0) := c(0);

                c(1) <= c(1) xor carry(0);
      carry(1) := c(1) and carry(0);
            
      c(2)<= c(2) xor carry(1);
      carry(2) := c(2) and carry(1);


Hi, I could get the waveform from 00000000, 00000001, 000000010, 00000011.
But I don't understand how it get with the coding above. I understand the first 2 lines.
But from 3rd lines onwards, I couldn't understand.

When i work out, I get c(1) = '1' - 00000010
carry(1) = '1' - 00000010

c(2) = '1' - 00000100
carry(2) = '1' 00000100

Is this the correct way of doing?
knjr
 
Posts: 12
Joined: Thu May 12, 2011 5:13 am


Return to VHDL