Sunday, November 27, 2011

My 1st Verilog, FIFO/ buffer for the 8080-type parallel interface module

We learn VHDL in academics. Engg. education is not just about learning from acads, its about applying the knowledge and leverage what you learnt.
Few years back, when I 1st learnt VHDL, i preferred C over it, any day. That is, till VHDL demonstrated its potential to me. Now when I make this transition from VHDL to Verilog, the story repeats itself. I prefer VHDL over Verilog :) At least I dont have to bother about syncing with things that are sensitive to an edge + a level.

My 1st Verilog.. a FIFO that goes between processor and the interface, to temporarily buffer the data till the interface send it across. Inspired from Net + my own learning from errors additions.
http://asic-world.com/examples/verilog/syn_fifo.html#Synchronous_FIFO


`timescale 1ns / 1ps

module fifo(
    proc_input,
    data_in,
    ready,
    clk, rst, done, wr_data,
    empty,
    full
    );

output [15:0] data_in;
output ready, empty, full;

input [15:0] proc_input;
input clk, rst, done, wr_data;

// ram = 2 ^ addr_width * 16 bit wide 
parameter ADDR_WIDTH = 4;
parameter RAM_DEPTH = (1 << ADDR_WIDTH); // 2 ^addr_width
parameter WORD_LENGTH = 16;

//-----------Internal variables-------------------
reg [ADDR_WIDTH-1:0] wr_ptr;
reg [ADDR_WIDTH-1:0] rd_ptr;
reg [ADDR_WIDTH :0] count;
reg [15:0] data_in; //if not initialized, it will be trimmed during optimization 
reg ready; // otherwise while assigning you get error:  Reference to scalar wire 'ready' is not a legal reg or variable lvalue
reg [WORD_LENGTH-1:0] mem [0:RAM_DEPTH-1];

assign full = (count == (RAM_DEPTH-1));
assign empty = (count == 0);

always @ (posedge clk or posedge rst) //cant have 1 posedge and non-edge triggered in sensi list
begin : WRITE
  if (rst)
    wr_ptr <= 0;
  else if(wr_data && (count != RAM_DEPTH-1)) //not full
    begin
wr_ptr <= wr_ptr + 1; //latch implied due to incomplete if 
  end
end

always @ (posedge done or posedge rst)
begin : READ_POINTER
  if (rst) begin
    rd_ptr <= 0;
  end else if (done && (count!=0)) begin
    rd_ptr <= rd_ptr + 1;
  end
end
// check if no need of done in if condition
//Xst:2111 - Clock of counter <rd_ptr> seems to be also used in the data or control logic of that element.

// the compiler wants count, rd_ptr and mem in the sensitivity list..shouldnt be used
always  @ (done, rst)
begin : READ_DATA
  if (rst) begin
    data_in <= 0; //data_in is  a register. one cant write data directly to port
ready <= 0;
  end else if (done && (count!=0)) begin //check trigger on done
    data_in <= mem[rd_ptr];
ready <= 1;
  end else begin
data_in <= 0; //to prevent latch on data_in. //should have been high z, but dunno how to represent that
ready <= 0;
  end
end

always @ (posedge clk or posedge rst)
begin : COUNTER
  if (rst)
    count <= 0;
  else if (done && (count != 0))
    count <= count - 1;
  else if (wr_data && (count != RAM_DEPTH))
    count <= count + 1;
end
//logically read and write can occur in parallel
//but without "else if" we get error that logic doesnt match FF template

always @ (wr_ptr, proc_input, wr_data)
begin
if (wr_data)
mem[wr_ptr] <= proc_input; //again a data_width size latch on mem_0 to mem_ram depth
end
endmodule

Notes: working mostly, yet to check corner cases..empty and full stuff
Have to check post-synthesis simulation..because of warnings that some signals may be trimmed..I dont want that to happen!!

2) Top level module: the code that made the chip..
contains modifications from the code published on the blog..
https://docs.google.com/document/d/1dOrttZW5EQFiEWcgUszROuMYu9jLKAuesl3KUhRyPsE/edit

Saturday, November 26, 2011

Gifting Grandma

Its Thanksgiving!! Been shopping like crazy..gifts for family and friends. But couldn't finalize a gift for my grandma. I checked gift ideas online, Amazon has a thread posted by similarly stumped grandkids. Most replies were very warm, it was a treat even reading the replies. Most people agreed that time and memories are the best gifts you can give. I recalled the sms sent from the aeroplane.. "wish I had spent more time with you all, but lets make the best out of the time spent together."  Other gift suggestions were to cater to her hobbies.. for instance my granny is a Math wiz.. an electronic sudoku for her.

I remember a story about kids planning to throw a surprise party for their mom. They all tried to recollect their mom's favourite dish. The mom always used to cook what the kids like.. no one could remember what their mom likes.. After I had read that..I thought about my mom's favourite dish. I finally had to ask granny..

Some gifts that I liked were..
Memory box: A box full of memories.. I write memories on folded sheets of paper and she can draw them out one-by-one. That way we both revive old memories. Shouldn't be difficult right? After all I stayed with her for 16 years. Task in progress..
Memory book that she will pen to hand down to us her autobiography..simply loved this idea too..
Digital Photo Frame
Recordable time-piece: This one has always been at the back of my mind.... I so wanted to have an alarm in my cell phone with the wake-up song my mom used to sing in my childhood. (Utha utha Chiutai, by Kusumagraj)
Our parents and grandparents have made so many sacrifices so that we could have the best opportunities. They deserve the best gifts.. time, memories, caring and sharing..

Thursday, November 24, 2011

Animorphs

Pre-script (If any such word exists): This one is for you Radha..

Hermione of our Harry Potter fan gang introduced me to Animorphs when I was in High school. We all enjoyed fantastic books, but somehow I never got a chance to read Animorphs. A few days back, there was this humungous book sale with an irrestible deal..any book for a dime. Me and my roommates bought 60 books between us. I chanced upon many must-reads that had somehow slipped my notice- LOTR, Animorphs, memoirs/autobiographies etc. Its heartening to see HUGE queues for a book sale in an era of Kindle and a general apathy to books.
Friends of the Library Book Sale
Back to the story. I read the book "The Alien", narrated by the alien Ax. Usually, the cover page shows a human morphing into an animal. This book follows the reverse sequence, Ax turns from the Alien to a human. Its interesting to see human sounds, especially the inflections for sarcasm from Ax's perspective.

The book soon takes a serious tone, as the first human infested with Yeerks is killed. The humans dont force Ax to tell them the truth..they lead him to a library to suggest that humans share their knowledge. Ax reveals how in the past the knowledge/ technology was revealed to Yeerks, which made them an universal parasite.

The book was penned in post-World War decades, I found striking resemblance to the plot and factual human history. Some innocent technology leading to doom..(in this case, the power of space flight, which enabled Yeerks to conquer any planet), mind control by Yeerks..all point time and again to WW-II. The book targets teenage audience, who may be naive enough to believe that humans really put technology to constructive use..hardly the case.

Recently saw the movie, the Rise of the planet of Apes. Seriously makes we wonder if technology is bringing us closer to the doomsday.

The book ends with Ax and humans realizing that freedom is what both the species care for, and must work together to save the earth from Yeerks.

8080-type parallel interface (writing data to external chip) VHDL and Verilog codes

Coding is all about trial and errors and learning from the errors. My independent study project deals with a controller for 8080-type parallel interface. The interface protocol is:

 Kudos to my friends who helped me debug the earlier versions of this code. 
Take away 1: Write only the inputs in the sensitivity list, this code worked when signals arent included in the sensitivity list.
2: If the code stays in one state over multiple clock cycles, clock is to be included in the sensitivity list. 
3. Once you include clock in the sensitivity list, signals are updated at each clock transition. This may lead to erroneous results.  
signal was getting updated at every clock edge instead of just the positive edge

Solution:  use next_<signal name> which will be updated only at the required clock edge.
4. Biggest takeaway, simple subtraction of vectors works too, no need of converting to unsigned/ signed and then reconversion back to std_logic_vector. :)  
5. Initial red signal for sact, where i was subtracting en_active - dsu. Not to worry, it simply means inputs not present yet. 
--------------------------------------------------------------------------------------------------
asu= address setup time, between assertion of RS and when CSb=0
A cycle delay between CSb asserted and enable asserted. 
dsu= data setup time, time at which stable data is put on bus (dbus <= data_in ) 
en_active = # of clock cycles for which enable is heldlow
ahold = address hold time, time between end of en_active period and when RS is deaaerted. 
dhold = data hold time, time for which data is held on bus after end of en_active period. 
en_deactive= # clock cycles for which enable is held high. 
-------------------------------------------------------------------------------------------------


-- if reset : s0
--if ready: s1 --pull up RS
--if asu counter=0 : s1sub0 pull CSb
--s2: put en 
--en_active - dsu counter: s3: put data on bus
-- dsu counter up: s3sub0 : pull off enable, CSb
-- dhold counter: s4: pull off data;  -- ahold counter: pull off RS -- deactive counter up: restart the sequence
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
USE ieee.std_logic_unsigned.all;

entity fsm_8080 is
Port ( clk, rst, ready, rs_in : in  std_logic;
        asu, dsu, ahold, dhold : in  STD_LOGIC_VECTOR (1 downto 0);
         en_active, en_deactive: in  STD_LOGIC_VECTOR (3 downto 0);
           data_in : in  STD_LOGIC_VECTOR (15 downto 0);
           dbus : out  STD_LOGIC_VECTOR (15 downto 0);
           RS, CSb, en : out  STD_LOGIC);
end fsm_8080;

architecture Behavioral of fsm_8080 is
 type STATE_TYPE is (s0, s1, s1sub0, s2, s3, s3sub0, s4);
 signal state, next_state : STATE_TYPE;
 signal sact, next_sact, sdeact, next_sdeact: STD_LOGIC_VECTOR (3 downto 0);
 signal sasu, next_sasu, sdsu, next_sdsu, sahold, next_sahold, sdhold, next_sdhold: STD_LOGIC_VECTOR (1 downto 0);

begin
 process(clk, rst)
  begin
         if (rst = '1') then
                state <= s0;
sact <= en_active - dsu;
sdeact <= en_deactive;
sdhold <= dhold;
sahold <= ahold;
sdsu <= dsu;
sasu <= asu;
      elsif(clk'event and clk = '1') then
               state <= next_state;
sact <= next_sact;
sdeact <= next_sdeact;
sasu <= next_sasu;
sdsu <= next_sdsu;
sahold <= next_sahold;
sdhold <= next_sdhold;
      end if;
  end process;

  process(clk, ready, rs_in, state, data_in, asu, dsu, dhold, ahold, en_active, en_deactive)

  begin
   next_state <= state;  --assume this unless changed later, simplifies logic
next_sact <= sact;
next_sdeact <= sdeact;
next_sasu <= sasu;
next_sdsu <= sdsu;
next_sahold <= sahold;
next_sdhold <= sdhold;

    case state is
   when s0 => en <= '1'; RS <= '-'; CSb <= '1'; dbus <= (others => '-');
           next_sact <= en_active - dsu -1;
           next_sdeact <= en_deactive;
           next_sdhold <= dhold;
 next_sahold <= ahold;
           next_sdsu <= dsu;
           next_sasu <= asu;
 if (ready='1') then
next_state <= s1;
 end if;

when s1 => rs <= rs_in; en <='1'; dbus <= (others => '-'); --assert RS
if (asu="00") then CSb <= '0'; next_state <= s2;
else CSb <= '1'; next_state <= s1sub0;
end if;

when s1sub0 => rs <= rs_in; en <='1'; dbus <= (others => '-'); -- CSb goes low
next_sasu <= sasu - "01";
if next_sasu="00" then next_state <= s2; CSb <= '0';
else CSb <= '1';
end if;

when s2 => rs <= rs_in; en <='0'; CSb <= '0'; dbus <= (others => '-');  --enable goes low
next_sact <= sact - "0001";
if next_sact="0000" then
next_state <= s3;
end if;

when s3 => rs <= rs_in; en <='0'; CSb <= '0'; dbus <= data_in;   --put data on bus. Effective dsu=dsu+1 so that we dont mess up if user enters dsu and dhold both 0. 
if (dsu="00") then next_state <= s4;
        else next_state <= s3sub0;
end if;

when s3sub0 =>     rs <= rs_in; dbus<= data_in; --end of enable_active, pull up en and CSb
next_sdsu <= sdsu - "01";
if next_sdsu="00" then next_state <= s4; en <='1'; CSb <= '1';
else en <= '0'; CSb <= '0';
end if;

when s4 => en <='1'; CSb <= '1'; --enable deactive 
if sdhold="00" then dbus <= (others => '-');
else
next_sdhold <= sdhold - "01";
dbus <= data_in;
end if;

if sahold="00" then rs <= '-';
else
next_sahold <= sahold - "01";
rs <= rs_in;
end if;

next_sdeact <= sdeact - "0001";
if next_sdeact="0000" then
next_state <= s0; --sequence starts again
else
next_state <= s4;
end if;
end case;
end process;  
end Behavioral;

End result: Protocol followed- check.
Signals updated at the correct edge - check.

Post script: Feels great when you contribute your code on the net. You feel better about googling for the next part of the project :)

Post script 2: The same controller implemented in Verilog: my 2nd code in Verilog
8080-type parallel interface controller in Verilog
https://docs.google.com/document/d/15n1rD3f3zQXb7LQdjMZ6EaEGnNmj1GBhOC-lWoBw4Vw/edit
Could have used 3 bits to encode states. HDL compiler used one-hot encoding.
Added default state= s0.
Note always @ (posedge clk) for the 2nd always, no need of a huge sensitivity list.
This one used 2 additional things..done output to indicate that end of sequence, signal s_data_in to latch input data.
Output bus is pulled to high impedance state (by 16'bz) when data is supposedly safely latched by the reader chip.



Monday, November 14, 2011

तू Vs मी

All characters in the चारोळ्या are highly fictional.. any resemblance to any person living or dead is highly coincidental.. :)
but the human nature and the relationship dynamics reflected in the lines.. you come across it @ every nook and corner..

(1)
मी पत्त्यांचा बंगला बांधायचा
तू  फुंकर मारून वावटळ पाठवणार 
मी स्वप्नांचे मनोरे बांधायचे 
तू पायाखालून जमीन खेचणार 
एकदा तरी माझा खेळ मलाच उधळू दे ना..
--दीपिका रानडे

(2)
मला स्वप्न रंगवायला आवडायची ..
तुझं भविष्य-रंजनाशी  वाकड आहे ..
मी स्वप्न पहायची बंद केली ..
तुला त्याचाही त्रास आहे
स्वतःला तुझ्या इच्छेने चालवताना ..
मी थकले आहे ..
तुझी गती कमी होते आहे
तुला त्याचच दु:ख आहे ..
--दीपिका

(3)
मला तुझ्याशी वाद घालायला आवडतं
माझ्यासाठी तो ही एक संवादच असतो
तुझ्यासाठी मात्र..
"तू आणि मी" कधीच संपून
"तू versus मी" सुरु झालं असतं
--दीपिका

Sunday, November 13, 2011

Daily awesome things..

1. You take milk off the microwave.. leave it for a bit.. and return to find it @ the correct temperature..
2. You meet a friend randomly only at the bus stop.. you wait at the bus stop thinking if you will meet the person today..and there he/ she comes..
3. It takes two to tango..and you secretly gloat over the "connection" :)
4. When a friend asks about your hurt foot..
5. You are doing something stupid..and a baby on the shoulders of a passer-by keeps staring at you..
6. Google pulls up suggestion about the song you have in mind even when you have keyed just a word..
7. Breaking off a chunk of frozen Shrikhand with the spoon you are using to heat parathas..
8. Your Comp. Sci. roommates are discussing their Comp. Arch project.. and you know all the technical jargon they're talking about :)
9. You read 1000awesomethings.com, you strike a chord with some of the awesome things that you've experienced..
10. You share those memories with your friends half the globe apart.. and chuckle to yourself while keying that e-mail..



Friday, November 4, 2011

Being human

Morning: My roomies and I are discussing about economics of the world. One roomy maintains that corruption is the hugest problem. I feel its the bulging population. My other roomy feels that we are so morally doomed that we dont deserve a better economy. We look at her, aghast. She tells us about a photo she saw on facebook, of a baby killed with the umbilical cord and all, just because it was a female baby. She shows us the photo, which is extremely upsetting.

Evening: I recall a scene from Mani ratnam's movie, Yuva. The pregnant wife "suddenly" discovers that her husband is a mafia, and could be a killer too. She decides that she doesnt want to bring up the baby in such an atmosphere and aborts the baby. Becomes a baby killer herself.
I am jolted.
This movie passed the sensor board?
How can anyone deal so non-chalantly with such a serious issue!
Yuva is not the only one.. Fashion, Aitraaj.. treat abortion as if its no big deal..

Night: I am still upset by the photo. I google further.
Till today I thought abortions are legally done only till the 20th week. Even at the 20th week, the foetus is as human as any of us. It has a beating heart, a responding brain. Google speaks of abortions in 3rd trimester, and of "botched" abortions where the live babies are just left to die.

I read the statistics of 3 abortions per second in the US, I watch "Silent Scream"..

If this is the price we are paying for so-called feminism, choice and technology..we are better off being put back to Stone age..
That doesnt change a thing..its the mentality that needs to change.
God, if you are still the God whom we all believe in, please stop sending those innocent souls to suffer on Earth. We are already morally dead, but we still hope that you cant bear to see the suffering.