Tuesday, May 13, 2014

Interface in SystemVerilog

Interface simplifies hooking up modules/ programs.
There are 2 methods of hooking up stuff using interface.
Hardwire method:
RTL: portname is interface_name.modport_name choose_a_name.
all outputs are assigned as choose_a_name = value.
At top level: create a instance of interface_name.
interface_name instance_of_interface_name (.clk(clk));
and hook up choose_a_name with instance_of_interface_name.

An example will make it clear. I am using an example from my current assignment at the UCSC extension coursework.
module memory_core (memory_interface.core_port deepika_coreif);
   always @ (posedge deepika_coreif.clk)
   ....
endmodule

module memory_ctrl(memory_interface.ctrl_port deepika_ctrlif);
....
endmodule

program testcase(memory_interface.testcase_port deepika_tcif,
input call_finish);
............
endprogram

module top();

   bit         clk;
   memory_interface imif (.clk (clk));
   ..........
   initial begin
clk = 0;
   end

   always #5 clk = !clk;
   ....

   memory_core icore     (imif);
   memory_ctrl         ictrl       (imif);
   testcase   itest      (.deepika_tcif (imif),
     .call_finish (call_finish) );
endmodule


Generic method: 
Here at the RTL end, the porttype is simply interface. What gets hooked up is instance_of_interface.specific_modport.
module memory_core (interface deepika_coreif);
..
endmodule

module memory_ctrl(interface deepika_ctrlif);
...
endmodule

program testcase(interface deepika_tcif,
input call_finish);
..
endprogram

module top();
   bit         clk;
   ....
   memory_interface imif (.clk (clk));
 
   initial begin
clk = 0;
   end
   always #5 clk = !clk;

   memory_core icore     (imif.core_port);
   memory_ctrl ictrl     (imif.ctrl_port);
   testcase   itest   (.deepika_tcif (imif.testcase_port),
     .call_finish (call_finish) );
endmodule

The code for interface itself remains unaffected whichever way you to connect stuff.
interface memory_interface (input bit clk);
logic reset;
.....
//modports
modport core_port(
input clk, reset,...,
output ... );

modport ctrl_port(
input clk, reset, ...
        output ... );
);

modport testcase_port(
input clk, reset...,
       output ...,
       import init_mem,
       import write_mem,
       import read_mem
);

task init_mem();
reset = 1; ....
#100 reset = 0;
endtask

task write_mem(ref int passed_addr,
               ref bit [7:0] passed_data  );
...
endtask

task read_mem( ref int passed_addr);
....
endtask
endinterface

Advantages of using tasks:
In my example, I could use the tasks and fork-join to test what happens when write and read occurs simultaneously  (This could never happen because there is a single read/writebar pin but its fun to check it anyways.. Pretty cool stuff.)

Note that task only has whatever is "passed" as the inputs/outputs. Task is actively driving the outputs but enumerating that is already taken care of in the modport. A good way of simplifying the code.
Interface basically gives you a overview of stuff. Since you consolidate all wires in one file, all worry about data_width mismatch, or whether you connected little/ big endianess correctly is taken care of.

Monday, May 5, 2014

My experiments with hunger

So I was scheduled for a wisdom tooth extraction in the new place that I shifted to. Since I dont have a car here, public transport it was, and I am not allowed to eat in the bus! So the only time I could get a quick bite was between 2 buses. And then the tooth extraction happened, so I was not supposed to eat. I kind of spent 3 days on either partial lunch (day0) or liquid food (aka milk with kheericha sattva, shikaran, or jamba juice). I dont have a calorie count but my estimate from how hungry/ weak i felt and how gaunt I looked is that it was far less, may be a half of the recommended 2000. This was totally a first world problem - I could not go out for more Jamba because I didn't have a car, and I could not go out in general because I was too weak.

There are billions of people who probably eat as little as I did and on top of it do physical labor to manage to earn that food. I kind of remember reading that jowar/ bajra/ millet are easier to digest, and I do hope that they are so easy to digest that those poor people who survive only on zhunka bhakar can extract all of its nutrition from the meager food they eat.

A few days ago I had participated in the SNAP challenge to eat only in a 4.5$ a day. The background story is that the US government fixed an upper limit on food stamps that each poor person will get: food stamps amounting to only 4.5$ a day. Then there are these well-to-do challenge takers who basically realized at the end of the challenge that this limit was too little, and how hard life is for poor people. The SNAP website is full of testimonials from people who claimed how hungry and snappy they were in managing to eat in just 4.5$ and all.
Living in Florida, I shop all my groceries including Organic fruits and veggies for the week in under 30$ which means that I trump the challenge each week without even trying, inspite of shopping in Publix (which is costlier than walmart), shopping Organic (which is costlier than non-organic) and being a vegetarian (because apparently meat is cheaper than veggies in the US). What I had not factored in was that there are states with higher taxes than Florida, or states with Federal taxes on top of the state taxes. (Or the cost of electricity to actually cook that rice). My conclusion from my normal, challenge shopping without ever taking up the challenge was that poor people should buy in bulk, and cook at home, and then 3 meals for 4.5$ a day per person isn't a big deal. Actually cooking for more people got progressively cheaper.
What was unrealistic was that my first world perspective assumed that people have a place where they can store the milk and fruits in a refrigerator, or store the rice bought in bulk pestfree, and have access to stove. Dasani's story changed my opinion.

Since my grocery bills were always lower than the SNAP, I never really actively took the challenge. Being on a liquid diet for a totally different reason opened my eyes to how bad really the life is for people who survive on half meals every day.