Wednesday, December 25, 2013

No Clock SVA (Asynchronous events)

All the time temporal and concurrency have hard association with system verilog properties and thus with System Verilog Assertions (SVA). Now the question is how is one suppose to check for asynchronous events using system verilog.

Here is a quick example with is self explanatory.

event start_ev, stop_ev;
parameter LAPSED_TIME = 50;

property no_clock;
   time start_t, end_t;
   @start_ev(1'b1, start_t = $time)|=>
   @stop_ev(1'b1, stop_t = $time) ##0
   (stop_t - start_t <= LAPSED_TIME);
endproperty: no_clock

async_check:assert property (no_clock);

In the above property there is no clock but we use asynchronous start and stop events instead as clocks. 

Monday, March 4, 2013

OOPS!!! Downcasting

I always wondered what casting is all about when it comes to object oriented programming. I read few related articles and the basic idea I got to start with was:

"With system verilog we need to manually cast the variable from one type to the other type otherwise compiler will issue an error. In System Verilog there are basically two types of casting
1. Static Casting:
A data type can be changed by using a cast ( ' ) operation. In a static cast, the expression to be cast shall be enclosed in parentheses that are prefixed with the casting type and an apostrophe. If the expression is assignment compatible with the casting type, then the cast shall return the value that a variable of the casting type would hold after being assigned the expression.
Example
int’(2.0 * 3.0)
shortint'{{8’hFA,8’hCE}}
2. Dynamic Casting :
SystemVerilog provides the $cast system task to assign values to variables that might not ordinarily be valid because of differing data type. $cast can be called as either a task or a function.

The syntax for $cast is as follows:
function int $cast( singular dest_var, singular source_exp );
task $cast( singular dest_var, singular source_exp );

The dest_var is the variable to which the assignment is made. The source_exp is the expression that is to be assigned to the destination variable. Use of $cast as either a task or a function determines how invalid assignments are handled. When called as a task, $cast attempts to assign the source expression to the destination variable. If the assignment is invalid, a run-time error occurs, and the destination variable is left unchanged.
typedef enum { red, green, blue, yellow, white, black } Colors;
Colors col;
$cast( col, 2 + 3 ); "

But this was not really enough for me because, I wanted know casting in terms of classes and more importantly what is Down casting? and why at all is it needed?

First, the big question in my mind was define down casting with respect to classes, so here is simple definition:

THE TERM DOWNCAST MEANS GOING DOWN IN THE INHERITANCE TREE.

Here is some conceptual explanation using an example:


class parent;
   virtual function foo;
end 
 
class child extends parent;
   bit foo_child;
   function void foo();
      $display("child with member of values %d",foo_child);
   endfunction
endclass 
 
module foo_bar;
   parent parent_handle;
   parent parent_handle_1;
   child child_handle;
 
   initial begin
      child_handle = new();
      $cast(parent_handle, child_handle);
              // What is the difference between line below and the $cast above ? 
      parent_handle_1 = child_handle; 
      parent_handle_1.foo();
      parent_handle.foo();    
   end
endmodule



$cast is a dynamic cast. That means it will check at run-time if the assignment is legal. A static cast, sometype'(expr), or an assignment is checked for validity at compilation.

The assignment from child_handle to parent_handle is always allowed, but an assignment the reverse direction can only be checked at run-time using $cast. Suppose you had:


initial begin
   parent_handle = new();
   child_handle = parent_handle; // not legal
   $display("I am child with member of value %d", child_handle.foo_bar);
end


The reference to child_handle.foo_bar is only valid if child_handle contains a handle to a child_ class type. Sometimes you do not know what kind of object has been stored in parent_handle because it has been passed down as a function argument. You can use $cast to check the kind of object you have. eg.

...
if ($cast(child_handle, parent_handle)) begin
   $display("I can display %d",child_handle.foo_bar);
else
    $display("I don't have a child_ class type");
...



Lets try to summarize DOWNCASTING in a single sentence:


To access the variables and methods which are only in the subclass and not in the parent class, cast back the object to the subclass handle. As the methods which are added in the subclass and are not in the parent class can't be accessed using the parent class handle.

I guess now down casting makes more sense! :P

Lets have another example to solidify the understanding.

   class parent;
   endclass

   class child extends parent;
      task display ();
         $display("This is amazing");
      endtask
   endclass

   program main ;
      child my_child;
      parent my_parent;
      initial
      begin
         my_child = new();
         my_parent = my_child;
         type_cast(my_parent);
      end
   endprogram
 
   task type_cast(parent my_base);
      child ch;
      $cast(ch,my_base);
      ch.display();
   endtask

RESULT :
This is amazing

Monday, March 5, 2012

How Dams work?

Past new year's eve, we planned to have a blast at Vegas, It was an awesome night, the strip was full of people the mood was upbeat and the fireworks were just breathtaking .

Next morning we visited Hoover dam, just a 40 minutes drive from Vegas. As we were looking at this huge dam in the Black Canyon of the Colorado River, on the border between the US states of Arizona and Nevada, there popped up question from my wife, a CPA by profession and thus less of a Techie, that how and what are these dams used for. I was so excited to answer her and explain her with the subject the "dam" right in front to point her to what I am exactly talking about.


So I started, what dam is? it is a wall of solid material built across a river to block the flow of the river to store water, the dam should be watertight and it should be stable i.e. the dam wall must have sufficient strength to firstly, stand permanently under its own weight when walls are saturated with water and secondly, resist the water pressure in the lake upstream of the dam. The next important part of the dam should be the water exit point, a dam must release water in controlled amounts through an outlet valve of some type. Depending on the purpose of the dam the water may be released into a pipeline for water supply to a city, or into a hydro-electric power station to generate electricity or the water may be released into the river bed downstream of the dam and allowed to flow naturally down the river, which can be used for irrigation of crops further downstream.

The last important part of the dam I explained was the spillway, when the river on which a dam is built floods, dam should be able to manage the large amount of water, which is usually taken care by having open cut channel large enough to carry the flood water around the dam. The spillway may form part of the dam wall itself.  


Wednesday, September 21, 2011

Video Connectors

Recently one of my friends have his parents visiting him. To make them feel at home and let them enjoy the day while he is out at work,he bought a LED smart TV and a cable service connection.
The next day, he came up to me and was asking me, how he can now setup his set top box, what video connector to use?  How can he attach his laptop to his TV? He had many questions and I had answer for every question because I have keen interest in this video domain and more so because I have gone through all that stuff of connecting TV to different devices. So I decided to just write this down here to give an overall view of different video connectors we have.


There are several types of video connections on PC ,TVs, DVD etc. They all serve to the same purpose i.e. connect and transmit video signal from one device to another, but the video quality obtained by each type of connection is completely different.


1. Radio Frequency (RF)
RF is the oldest way to transmit video signals. It is used on TVs and VCRs to connect these devices to both terrestrial (antennas) and cable transmition.
RF cables are found in: 75-ohm coaxial and 300-ohm parallel. the 300-ohm parallel type was used by old terrestrial broadcasting antennas, but even this kind of antenna use the 75-ohm coaxial cable nowadays. You can install a 300-ohm connector into a 75-ohm connector by using an adapter. 
So its clear that my friend first need this connector to have the cable signal from the wall socket to the RF socket of his set top box provided by the cable service provider.


2. Composite Video (RCA)
Composite vĂ­deo is one of the most popular video connection types and uses a RCA connector. Composite video is used on the popular ”video in“ and ”video out“ connectors, found in VCRs, TVs, DVD players and video projectors. This is the best option to connect your VCR to your TV set, assuming that they don’t have a S-Video connector (usually newer TV sets have this connector, but VCRs usually don’t have S-Video output). But for other devices, like DVD players and even PCs, you should not use this connection, because S-Video, Component Video and other standards offer better video quality and usually you can find at least S-Video on those devices.
Since VCRs allow you to record videos, it has two composite connectors, one being an input and the other one being an output. You should connect the VCR output to the TV set input to make the proper connection. Its input should be used in the case you are willing to record something from another video source, like another VCR (for copying tapes) or even a PC with composite video output.
A typical RCA male connector cable will have three pins, yellow-for composite video and rest two for audio for left and right channel respectively. The color coding for different kind of RCA connectors is as follows:
  • Yellow: Composite video.
  • Red: Right channel (audio).
  • White or black: Left channel (audio).
  • Orange: Digital audio (a.k.a. SPDIF).
  • Green: Component video.
  • Blue: Component video.
  • Red: Component video.

3. Separate Video (S-video)
S-Video provides a better image quality than composite video. S-Video has three wires; one for carrying the image in black-and-white, one for carrying the color information signal and the third is the ground (as the name suggests separated video).
All latest TV this connector. One should connect the DVD player to the TV or videoprojector using an S-Video cable only if your DVD player and/or TV or videoprojector don't offer a better connection option, like component video, DVI or HDMI.


4. Component Video
Component video offers a far better quality than S-Video, being the preferred video connection to use between your DVD player or your cable/satellite converter, in the case they have this kind of connection and they don't have a better connectivity option, like DVI or HDMI. It uses three cables, which should be connected as follows:
Green connector: green input or input labeled as Y; Blue connector: blue input or input labeled as Pb or Cb or B-Y; Red connector: red input or input labeled as Pr or Cr or R-Y.


The Y connector transmits luma information (black and white image) while the color information is transmitted on the other connectors.

5. Video Graphic Adapter (VGA)
VGA is the traditional connector used to connect your PC to video monitors. It is also one option to connect your PC to HDTV sets. This connection offers a better video quality compared to component video, S-Video and composite video because it uses an independent wire for each video signal: red, green, blue, horizontal sync and vertical sync.
This uses analog signals, while on DVI and HDMI information is transmitted digitally, thus better quality. So if HDTV set has a DVI or HDMI connector , one should use one of them instead of the traditional VGA connector to connect your PC to  HDTV.


6. Digital Video Interface (DVI)
As the name suggests DVI should be an digital signal based connector, actually it can carry both analog and digital signals. Here are the types of DVI connectors available:
  1. DVI-A: Analog connection.
  2. DVI-D: Digital connection. Usually when we say “DVI” we are referring to DVI-D.
  3. DVI-I: Cable or connector that can carry both DVI-A and DVI-D signals. DVI-A cables can carry only DVI-A signals and DVI-D cables can carry only DVI-D signals.
  4. Single Link: Allows screen resolutions up to 1920 x 1080 pixels at 60 frames per second.
  5. Dual Link: Allows screen resolutions up to 2048 x 1536 pixels at 60 frames per second.
If a device has a DVI-I connector this means that it produces both analog and digital signals. One should use a cable (DVI-A or DVI-D) according to the kind of connection your display supports, analog or digital.

7. High-Definition Multimedia Interface ( HDMI)
HDMI is today the most high-end kind of video connection available . Find this connection on the latest DVD players, Blu-Ray players, HD-DVD players, HDTV sets and probably on the latest digital cable/satellite converters.
This connection works 100% in digital mode, is capable of transferring higher resolutions than DVI even compatible with resolutions not commercially released yet it even features a copy-protection mechanism called HDCP (High Bandwidth Digital Copy Protection) and the best part is transfers digital audio signal on the same cable, eliminating the need of an extra cable for connecting audio if devices being connected support this feature.



Tuesday, August 16, 2011

How 3D works?

Now a days every consumer electronic company talks about 3D, the highest grossing movies going forward will be 3D movies, these all are indications of where the technology is headed, and what direction the electronic industry is going. 


Our ability to feel the world around us in three dimensions is based on the fact that we have two eyes, and they are placed next to each other. This distance causes the images captured by the eyes to have a little shift relative to each other e.g. if you are looking at a pole that is in front of a second pole, the right eye will see the 1st pole a little-more to the right than the left eye. With 3D movies and photos, the photographer uses two cameras that are placed adjacent to each other, similar to how our eyes are. The end product is actually two films .... one for the right eye, and one for the left.


The hard part is replaying back these images or movies. The idea is to deliver the left picture or film to the left eye, and the right to the right eye. The most popular way, initially, was to use the funny red-blue glasses. This technique is based on coloring the left and right images and then imposing both on top of each other. Then, the glasses use the color filter to filter-out for each eye the image destined for the other. This way, the brain gets a shifted-image from each eye, and it analyses and builds and thus  gives us the feeling of 3D.


Another method, based on polarizer glasses. This technique is based on the fact that light can have a polarity. Normal light is a mix of waves that are both horizontal and vertical, as well as other angels, but using a special filter, one can block the waves. This is called a polarizer filter, and when you look at things through it, only light at a very specific angle comes through.

With 3D movies, this filter is applied at two angles to the two films, and then superimpose them on top of each other. This still looks fuzzy to the naked eye, but, we put special glasses with matching filters on our face. The light for the left image comes at some angle and the light for the right eye comes at another angle. Both angles arrive at both eyes, but the filter on the left eye only lets the light at the 1st angle through, while the right filter lets only light at the 2nd angle through. The result is similar i.e. each eye sees only the image that is meant for it. The filters are gray, so there’s no color distortion, thus this is better than funky red-blue glasses.


A 3rd technique, involves synchronized active glasses. This technique is based on the fact that the image on the TV is refreshed at a high rate. A movie is a series of images that are played in a series  rapidly, giving the illusion of movement (like a flip-book). With 3D, the image sequence is alternating – one image for the left eye, and then one for the right, and back to left. The viewer wears electronic glasses that are designed to block the lens in sync with the TV. During the showing of the left image, the right eye is blocked, and vice versa. This is fast enough so that most people don’t feel the blanking at all, although some people have an adverse reaction to this in the form of headaches or dizziness. Another disadvantage is that the active glasses are bulky and expensive.

The 4th technique is based on putting a special lens on the TV screen itself, which splits the screen into many vertical bars. Same as the 3D images we can often find in printed advertisements. Each of the two images is split into thin strips, and the lens shows the “left” stripes to the left eye, and the vice-versa. The result is sweet, because no glasses are needed, but the problem is that the field-of-view is limited. Move a little to the right or left, and the illusion breaks, and might even cause dizziness from the bending of the light.








Tuesday, August 9, 2011

Editor for mercurial

Last evening one of  my co-worker was sitting beside me, as I was pushing in some changes into the  project repository. And as I hit the "hg commit" command my "gvim" editor popes up for writing the commit message, and this buddy of mine sitting besides me asks me how did do you do that, howcome your gvim pops up whereas on my computer the same command opens a vi editor in the same terminal. I was like its easy,  I set the editor up in the Mercurial config file the famous ".hgrc" file.
Then he goes no, thats not the case because I have copied over your ".hgrc" file long back and if it was that file, then it would have been happening for me as well. I was like hmm let me see what I did. Then I realized that I have the UBUNTU environment variable EDITOR set for me to "/usr/bin/gvim  -f" ......that explains everything.


For making proper sense out of what I mean by above incidence, read the following explanation :

Mercurial tries to pick which program to call to edit a commit message by trying the following (in order):
  1. HGEDITOR environment variable
  2. editor configuration option in [ui] section (in hgrc or passed with --config ui.editor command-line option).
  3. VISUAL environment variable
  4. EDITOR environment variable
  5. vi, if none of the above is set

    So the above explanation suggests that I was actually using option 4 above, but I thought I was using option 2 while I explained to the co-worker 
    and on the hind side now its clear that my co-worker was using was option 5.  

Friday, July 29, 2011

Mercurial Strip


Yesterday evening in was in a bit of a fix at work, I was working on a new feature-name mercurial branch and and committed a few things into the branch and posted the changes onto a review system we have at work. after the team reviewed my changes it was time to push them over and merge it with the main "DEFAULT" branch.

So I started and did as follows:

hg st
Should show no modified files.
Make sure your working copy is based on the branch:
hg branch
It should report the name of the feature branch. 
hg update feature-name
Close out the branch if it's a Feature Branch
hg commit --close-branch -m "closed branch feature-name"
Update working copy to be based off of the default branch instead of the branch:
hg update default
Merge from the branch to default (assuming feature-name is the name of the branch you are merging to default):
hg merge feature-name
When everything looks good, commit the merge 
hg commit -m "merged feature-name branch to default"
and at last push the merge onto default.
I did the above step by step but then at last the "hg push" aborted with an error, So now what to do.
I looked back and realize that I didnt push the changes into the feature barnch (though committed them), but now have so many change sets and I am on Default branch now.

I update back to my feture branch and want to be at the same change set where I started so here comes "hg strip" to my rescue, though there can other solution too, I used hg strip to strip off all the change set and start over merging into default again.

Okay so now What is that hg strip command do...here is brief explanation below:

hg strip rev removes the rev revision and all its descendants from a repository. To remove an unwanted branch, you would specify the first revision specific to that branch. By default, hg strip will place a backup in the .hg/strip-backup/ directory. If strip turned out to be a bad idea, you can restore with hg unbundle .hg/strip-backup/filename.
If others revision in the repository have higher number than the stripped one, those one are renumbered in order to keep a sequential numbering scheme. The hash remains the same.