Tuesday, June 26, 2012

Mixing VHDL and verilog modules

INTRO
I have successfully been able to mix both VHDL modules and Verilog modules together.  Honestly it is a lot easier then I had thought it would be.  Since I could not find detailed examples of this phenomenon, I had to experiment with how it would work out.  The examples that I found were all wrong, but they gave enough of a hint to figure it out on my own.  I'm quite sure there are many people that need to know this so if you are here, you are in a good place.

ABSTRACT
A simple Verilog top module was crafted as a 2 bit full adder.  The internal components would be a verilog one bit adder and VHDL one bit adder.  They got connected in the verilog top module and were successfully testbenched and synthesized.  Did I mention that I used a testbench written in vhdl to test the 2 bit FA verilog top module?

Also as a second pass, I created a simple VHDL top module that was crafted as a 2 bit full adder.  The internal components would ALSO be a verilog 1 bit full adder and vhdl 1 bit full adder.  They got connected in the vhdl top module and they successfully testbenched and synthesized.

CODE
Below is the code that was used to create the described 2 bit adders.  Hopefully it will demonstrate how easy it is to go from one language to the other.
=====================
-- 1 bit FA in VHDL
--------------------------------

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity FA_VHDL is
    Port ( A,B,CIN : in  STD_LOGIC;
           COUT,S : out  STD_LOGIC);
end FA_VHDL;
architecture Behavioral of FA_VHDL is
begin
--concurrent implementation of a 1 bit full adder.
--Statements outside of a process are considered concurrent
S <= A xor B xor CIN;
COUT <= (A and B) or (A and CIN) or (B and CIN);

--Alternative implementation with a use of sensitivity list.
--This block will only execute when the items in the sensitivity list change
-- FA : process (A,B,CIN)
-- begin
-- S <= A xor B xor CIN;
-- COUT <= (A and B) or (A and CIN) or (B and CIN);
-- end process;
end Behavioral;



=====================
--1 bit FA in verilog
--------------------------------

`timescale 1ns / 1ps
module FA_v(
A,B,CIN,
COUT,S);
//------------
input A,B,CIN;
output COUT, S;
// reg COUT,S; // uncomment if using "always block"
//----
//This makes the logic happen continuously
assign S = A ^ B ^ CIN;
assign COUT =  (A & B)  | (A & CIN) | (B & CIN);

//Alternative implementation with a use of sensitivity list.
//This "always block" will only execute when the items in the sensitivity list change
// always @ (A or B or CIN)
// begin
// S <= A ^ B ^ CIN;
// COUT <=  (A & B)  | (A & CIN) | (B & CIN);
// end
endmodule


=====================
--2 bit FA in verilog with internal components: 1 bit verilog fa and 1 bit vhdl fa
--------------------------------

`timescale 1ns / 1ps
`default_nettype none

module Verilog_2bit_adder(
A2b,B2b,CIN2b,
COUT2b,S2b
);
//define pins
input [1:0] A2b,B2b;
input CIN2b;
//define ports
output COUT2b;
output [1:0] S2b;
//Specify internal wire
wire carry_w;
FA_v verilog_full_adder(
.A    (A2b[0]), // ".A" specifies the Pin of the component. "A2b[0]" specifies the thing that is connecting to that pin
.B    (B2b[0]),
.CIN  (CIN2b),
.COUT (carry_w),
.S    (S2b[0])
); //for bit0
FA_VHDL VHDL_full_adder(
.A    (A2b[1]),
.B    (B2b[1]),
.CIN  (carry_w),
.COUT (COUT2b),
.S    (S2b[1])
);//for bit1
endmodule


=====================
--2 bit FA in VHDL with internal components: 1 bit verilog fa and 1 bit vhdl fa
--------------------------------

library ieee;
use ieee.std_logic_1164.ALL;
use ieee.numeric_std.ALL;

entity VHDL_2bit_adder is
   port ( A2b    : in    std_logic_vector (1 downto 0);
          B2b    : in    std_logic_vector (1 downto 0);
          CIN2b  : in    std_logic;
          COUT2b : out   std_logic;
          S2b    : out   std_logic_vector (1 downto 0));
end VHDL_2bit_adder;

architecture BEHAVIORAL of VHDL_2bit_adder is
   signal carry_s : std_logic;
   component FA_v
      port ( A    : in    std_logic;
             B    : in    std_logic;
             CIN  : in    std_logic;
             COUT : out   std_logic;
             S    : out   std_logic);
   end component;
 
   component FA_VHDL
      port ( A    : in    std_logic;
             B    : in    std_logic;
             CIN  : in    std_logic;
             COUT : out   std_logic;
             S    : out   std_logic);
   end component;
 
begin
   verilog_full_adder : FA_v
      port map (
               A=>A2b(0),
               B=>B2b(0),
               CIN=>CIN2b,
               COUT=>carry_s,
               S=>S2b(0)
);
 
   VHDL_full_adder : FA_VHDL
      port map (
               A=>A2b(1),
               B=>B2b(1),
               CIN=>carry_s,
               COUT=>COUT2b,
               S=>S2b(1)
);
 
end BEHAVIORAL;




=====================
--vhdl test bench
--------------------------------

-- Notes:
-- This testbench has been automatically generated using types std_logic and
-- std_logic_vector for the ports of the unit under test.  Xilinx recommends
-- that these types always be used for the top-level I/O of a design in order
-- to guarantee that the testbench will bind correctly to the post-implementation
-- simulation model.
--------------------------------------------------------------------------------
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--USE ieee.numeric_std.ALL;

ENTITY VHDL_two_bit_adder_TB IS
END VHDL_two_bit_adder_TB;

ARCHITECTURE behavior OF VHDL_two_bit_adder_TB IS
    -- Component Declaration for the Unit Under Test (UUT)
    --COMPONENT Verilog_2bit_adder --testing uncomment to test verilog
COMPONENT VHDL_2bit_adder
    PORT(
         A2b : IN  std_logic_vector(1 downto 0);
         B2b : IN  std_logic_vector(1 downto 0);
         CIN2b : IN  std_logic;
         COUT2b : OUT  std_logic;
         S2b : OUT  std_logic_vector(1 downto 0)
        );
    END COMPONENT;
   --Inputs
   signal A2b : std_logic_vector(1 downto 0) := (others => '0');
   signal B2b : std_logic_vector(1 downto 0) := (others => '0');
   signal CIN2b : std_logic := '0';

  --Outputs
   signal COUT2b : std_logic;
   signal S2b : std_logic_vector(1 downto 0);

signal clock : std_logic := '0';
   constant clock_period : time := 10 ns;

BEGIN

-- Instantiate the Unit Under Test (UUT)
--      uut: Verilog_2bit_adder PORT MAP ( --uncomment if using verilog component
        uut: VHDL_2bit_adder PORT MAP ( -- uncomment if using VHDL component
          A2b => A2b,
          B2b => B2b,
          CIN2b => CIN2b,
          COUT2b => COUT2b,
          S2b => S2b
        );

   -- Clock process definitions
   clock_process :process
   begin
clock <= '0';
wait for clock_period/2;
clock <= '1';
wait for clock_period/2;
   end process;


   -- Stimulus process
   stim_proc: process
   begin
      -- hold reset state for 100 ns.
      wait for 100 ns;
A_loop: for A in 0 to 3 loop
B_loop: for B in 0 to 3 loop
CIN_loop: for CIN in 0 to 1 loop
A2b <= conv_std_logic_vector(A, 2);
B2b <= conv_std_logic_vector(B, 2);
if CIN = 1 then
CIN2b <= '1';
else
CIN2b <= '0';
end if;
wait for clock_period;
assert (A+B+CIN = conv_integer(COUT2b&S2b)) report "This isn't right" severity error;
end loop CIN_loop;
end loop B_loop;
end loop A_loop;
      -- insert stimulus here

      wait;
   end process;

END;









Saturday, June 23, 2012

python in xcode 4



  1. Open Xcode 4.
  2. In the menu bar, click "File" → "New" → "New Project…".
  3. Select "Other" under "Mac OS X".
  4. Select "External Build System" and click "Next".
  5. Enter the product name.
  6. For the "Build Tool" field, type in /usr/local/bin/python3 for Python 3 or /usr/bin/python for Python 2 and then click "Next". Note that this assumes you have Python installed in the typical location(s). if you are unsure as to where your Python executables are enter these commands into Terminal: which python3 and which python.
  7. Choose where to save it and click "Create".
  8. In the menu bar, click "File" → "New" → "New File…".
  9. Select "Other" under "Mac OS X".
  10. Select "Empty" and click "Next".
  11. Navigate to the project folder (it will not work, otherwise), enter the name of the Python file (include the ".py" extension), and click "Save".
  12. In the menu bar, click "Product" → "Edit Scheme…".
  13. Click "Run" in the left column.
  14. In the "Info" tab, click the "Executable" field and then click "Other…".
  15. Navigate to the executable from Step 6. You may need to use ⇧⌘G to type in the directory if it is hidden.
  16. Select the executable and click "Choose".
  17. For the "Debugger" field, select "None".
  18. In the "Arguments" tab, click the "Base Expansions On" field and select the target that is named the same as your project.
  19. Click the "+" icon under "Arguments Passed On Launch". You may have to expand that section by clicking on the triangle pointing to the right.
  20. Type in $(SOURCE_ROOT)/ and then the name of the Python file you want to test. Remember, the Python program must be in the project folder. Otherwise, you will have to type out the full path (or relative path if it's in a subfolder of the project folder) here. If there are spaces anywhere in the full path, you must include quotation marks at the beginning and end of this.
  21. Click "OK".
  22. Start coding.
Note that if you open the "Utilities" panel, with the "Show the File inspector" tab active, the file type is automatically set to "Default - Python script". Feel free to look through all the file type options it has to gain an idea as to what all it is capable of doing. The method above can be applied to any interpreted language. As of right now, I have yet to figure out exactly how to get it to work with Java; then again, I haven't done too much research. Surely there is some documentation floating around on the web about all of this.
Say, "Hello, code completion, auto-indentation, and syntax highlighting." Note that it's not as advanced as it is with C, C++, or Objective-C but it is better than using IDLE in my opinion.
---------------------


original link
http://stackoverflow.com/questions/5276967/python-in-xcode-4


terminal missing gcc and g++

Usually I expect gcc and g++ to be automatically installed in my mac.  But it came to me as a surprise to find out that it was gone and missing.  I have lion and I had just upgraded to the latest Xcode.  In Xcode 4, I was able to run the c/c++ programs.  If this was the only way to do this then I would be mad at apple for giving me a handicap.

I figured that the reason for the absence was due to me decision to install only the minimum amount.  that is.... installing osx lion.  I did the minimum install rather then the regular one.  I felt like I had to somehow install all that extra stuff.  So I popped in my lion dvd and played around with it.  It wasn't there.  Sure there were a bunch of nice packages but unfortunately i had no clue what they did.  Instead of risking it and gambling the well being of my mac, I decided to search for a solution online.

which I found btw..... =)

basically the reason gcc was missing was because of the new way that apple decided to deal with Xcode.  They decided to make it one app and by doing so, they displaced all the necessary files in to the Xcode app.

To get my gcc back I had to go to

preferences > downloads > command line tools

and install the "command line tools"

I went back to the terminal and LO and behold.... it compiles =)

Wednesday, June 13, 2012

Good Verilog Tutorials

After much searching on the web, I found scarce tutorials of verilog.
When ever I searched for VHDL I usually found what I was looking for.

This page will have some links that have been useful for my purposes

A very good tutorial with lots of examples.


Some info on how to use a vhdl component in a verilog entity


Side by side comparison of VHDL and VERILOG language


Verilog by example

VHDL vs VERILOG (an unbiased comparison)


Later on I will post some code that I produced

Tuesday, May 22, 2012

Triple partitioning mac book pro with osx windows and ubuntu

I made a lot of mistake before I got it mix right.

Originally I had osx with no boot camp installed.  So I thought I'd make a new partition using disk utility and then install the windows xp I had.  My plan was to have it become upgraded to windows seven.  Once I created the fat partition, I popped in the xp disk and restarted.  The the blue screen showed up and asked me to  partition or leave it alone.  I chose to leave it alone since it was already partitioned as FAT32.  The computer restarted and instead of going to start up, the computer told me to press "ANY" key to start from disk.  I pressed buttons and found myself back at the partition selection.  It was like deja vu since I had just been here.  I selected the option to partition the FAT.  I had thought that maybe it required a reformat.  I chose this option because it was 32 GB in size; he same size of my FAT partition.  Little did I know, that it would wipe my entire HD.  Windows installed normally.  I discovered my mistake when I tried booting up mac.  it was gone.....I was momentarily horrified but was soon relieved upon realizing that I still had dropbox.  Oh, trusty drop box.  This is the reason I use you in the first place!  Ive had a history of reinstalls and drop box had been there to make getting my precious files back more painless.  The only sacrifice would be time.

I had to pull up my old snow leopard install disk since Lion was out there somewhere and i probably couldn't get it back until I downloaded it again from the app store.  SL installed normally and I was able to redownload LION from the app store.  lion installed normally.

since everything was fresh, it would be a good time to make a partition.  3 partitions to be exact.  I wanted ubuntu and windows 7.  I added windows and installed it.  was able to load both osx and windows.

Then I created a new partition for the ubuntu.  Unfortuanately, windows refused to work.  all I did was add a partition.  I didn't even install ubuntu.  So then I thought maybe, I need to make 3 partitions in the beginning.  So i resized osx, and added two FAT partitions.  One for windows and one for ubuntu.  I started with windows 7....again....and it installed alright.  then i tried ubuntu.  Set up was painless  but the install method was weird.  I chose the option to install ubuntu along side osx.  It took me to a slider that asked me how much space to put into this and into that.  The problem was that I had no idea what these two pieces were for!  50-50 seemed appropriate.  I had thought that I would find myself with the option of which partition to install ubuntu in, but I was not graced with such an option.  It started installing without hesitation.  osx and  ubuntu were good, but win seven was gone..... =_____=

disk utility revealed that ubuntu wrote itself right on TOP of of windows 7.  Nice... thank you ubuntu... thank you....

back to square one.  two fat partitions.  install ubuntu first and be more careful about how to install it in the right partition.  No luck.  Had the bright idea to make one FAT partition and one FREE SPACE partition.  ubuntu seized that space without asking me anything.  The good thing was that it filled out that entire partition for its own; which was exactly what I was shooting for.

now for windows.  installed it normally.  but ubuntu disappeared.. so I reinstalled it... then win 7 disappeared.  so i reinstalled it... and then ubuntu disappeared AGAIN!  GOSH was i tired of this....

Did some searching for a solution and many people used this THING called rEFIt.

oh did I mention that my recovery disk disappeared too?  Yeah, the one that lets you reinstalled lion.  If this were to crash that I'd have to install everything starting from SL all over again, so as a precaution I burned lion on to a dvd-r.  It took longer to load up, but worth it.  after all, how often would I find myself in this position?

rEFIt was a painless install.  there was a nice script that installed it automatically.  I restarted the computer and poof! all the partitions are there!!!!!  Ya know how one of them was missing?  well, REFIT made it visible! and it worked!!!!

The only problem was that by default, the machine started up with grub recovery and once it got there I had no clue what to do except restart.  After a while I got fed up with always manually choosing OSX as a start up disk.

The refit.config file has the commands needed to choose mac as start up by default.
i.e.  "default_selection M"


I found it funny how if I selected ubuntu or windows they both would lead to GNU GRUB which gave me options to  start up start up as ubuntu , osx, or windows.  a bit redundant, don't ya think?  I'll try finding a way to fix it, but for now I'm happy.


below is a quick guide on how to triple partition your mac osx.
1) installed osx on the entries disk if you haven't already.

2)use boot camp to get windows support software.  NOT NOT USE BOOT CAMPTO INSTALL WINDOWS

3)use disk utility and make two partitions.  One shall be FAT32 and the other shall be free space.

3.5) download and install rEFIt

4) install windows on the fat32.  You may need to format it to ntfs

5)install ubuntu and choose the option to install along side osx and it will take up the entire free space partition.

Saturday, May 12, 2012

GNU companion

Another day of research.  Sam tells me that he wants me to run some test software.  Probably some sort of script to send a simple msg.  Right now they are able to send wireless messages but they aren't able to receive it.  Maybe they are receiving junk.  They aren't sure cause there is no manuel.  Guess and Check is your friend....

Sam sent me a link to a gnuradio tutorial and it instructed me to open gnuradio companion; a GUI.  They said that the keyword was "grc" but when I tried it, it would only open the command line version.
"gnuradio-companion" was the command line that opened upi the GUI.

wit lab research special counter

After working with the grad students in the wit lab for a few weeks, I became bored of how stuff failed to work.  I felt like I was spending a lot of time on this project but not getting any real work done.

I got fed up with waiting and took matters into my ow hands.  I came to this wit lab because I wanted to contribute my VHDL and FPGA knowledge.  So I asked the professor G chen Who was working on the VHDL stuff.  At the time there was no one working on it.

He knew that I desired to work on an FPGA project.  so he told me to make a "special counter".  The idea seemed simple enough.  count a few signals and TADA! magic.....I wasn't sure what was so special about the counter so I pressed for details as to what this was.  Was there a state machine to describe this?  Was there some documentation?  He pointed to a box with knobs on it.  It looked like a VCR except it wasn't.  This machine was the special counter that professor wanted.  What bothered him was that the counter wasn't customizable enough, so he wanted an fpga implementation of it to have more control.

The only problem was I had no idea how to start.

I needed some advice from someone more knowledgeable them me.  Professor Vahid probably knew what to do.  As I explained the problem to him, I began to answer my own question.  Seems to be a habit of mine to ask questions I have at least ONE answer to.

The problem was balancing what the professor wanted verses what the actual machine does.  It confused me as to what this machine is suppose to do and I wasn't sure how the professor wanted to customize it.  I asked, but no details surfaced.

"as a base.....start with something you do know how to make.  make it similar to the machine on your professors desk.  Ask him if thats want he wanted and if he says no, then you can make changes to accommodate" said Vahid.

It was what I needed to hear.  I was trying to swallow the entire meal instead of taking it one bite at a time.  What professor G chen wanted was a mystery so what I had to do was make something that wasn't a mystery.

Mistakes were made in the process of creating this device.  I wasn't sure what it was suppose to do.  My first assumption was that a transmitter would shoot out a trigger signal to the receiver and then after a short delay it would send a byte of information to the receiver.

I made a bunch of fancy state machines which took a LOT of time to create and conceptualize.  only to realize that it wasn't what the professor wanted.  Its not as bad as it sounds cause I was close.  Instead of  sending a byte of information, he actually wanted a "rising edge" counter.

*face palm*  that would have saved me soooooo much time and effort..... if I did a rising edge counter, instead of what I was TRYing to do  then I would have been done a week earlier.

I made some modifications such that the number of rising edges would show up on the seven segment led screen on my spartan 6.  The numbers showed up obviously, but they were inconsistent.  I used a wave form generator as input to my device and my device would count the rising edges it would get.

why was the numbers in consistent?  was my spartan board too slow?  was it my programming?
I attempted to simplify my design and the error eventually shrank, but it wasn't good enough for me.  I wanted 99.99% accuracy.

I anazlyed the test bench results again and again and noticed a ridicules amount of delay.  This delay may have been responsible for over counting or under counting.


It must have had to do with the nested state machines I was using.  My last card was to get down and dirty with low level implementation.  Gates.

I went ahead and made my own adder and register and all the parts needed to make my counter come to life.  The amount of code for this ended up being WAY smaller then my previous implementation with state machines.  I even paid attention to what RTL design xilinx was generating for my code.  if I had used a state machine it would have been a huge mess.  But since I was making everything, it would be much easier to read and I was have certain expectations as to what SHOULD be there.  If a latch formed and a combinatorial loop formed, I knew there was a problem in my design.

Doing it in the low level really really really paid off.  I had no errors and there was hardly any delay.  If there was a delay, it was all expected.  Seeing as how my device runs at 100 mhz, a delay of one cycle probably wouldn't matter much especially when my input device wasn't going to be any faster than 10khz.

I was able to create a generic mux that used one hot encoding in its select line.
a decoder was made to translate binary number into their hexadecimal equivalent.
I made a counter that would count from zero to 9 and then would over flow.

There are only two things left for me to do.  I need to add another register between the four digit decimal counter and the interface to the seven segment.  while the counter counts, it will need to be saved somewhere and that register is where it will need to be saved.

A state machine would need to be made (either literally or figuratively) to control when the counter would count ,  pause or reset.  it would need to control the register like when it would receive the new value.  shouldn't bee much longer before i am complete.

One thing that would could also do is to make a clock divider in the way that gang specified.

one input has the clock  and it would have several outputs.  the first would be
clock/256 then
clock/256/256 then
clock/256/256/256 and so one.

You want your AN sm to rotate slowly rather then as fast a possible.  It probably doesn't matter that much but for some reason I'm concerned with using too much power.