Sunday, 10 April 2016

AMBA AHB [BLOG UNDER CONSTRUCTION]

[BLOG UNDER CONSTRUCTION]

AMBA

AMBA is a bus protocol.

What is a protocol ?
In Practical life you can say
It is a way of communication from one entity(let mobile) to another entity (say cloud).
Now every communication system has its specific syntax,semantics and timing.

In Nasa Mars rover   protocol is so slow that rover  data takes 1 hour to reach to the earth. 




AMBA BURST


AMBA RETRY


AMBA SPLIT









'















Code for Master
  1. module ahb_master4(
  2. //transfer response
  3. input hready,
  4. //global signals
  5. input hclk,
  6. input hresetn, //active low
  7. input [1:0]hresp,
  8. //read data
  9. input [31:0]hrdata,
  10. input hgrant,
  11. //address and control

  12. output reg [31:0]haddr,
  13. output reg hwrite,
  14. output reg [2:0]hsize,
  15. output reg [2:0]hburst,
  16. output reg [1:0]htrans,
  17. output reg hreq,
  18. //DATA
  19. output reg [31:0]hwdata
  20.     );
  21. reg [31:0] rhaddr;
  22. reg [31:0]rhrdata;
  23. reg [31:0] mem[8:0];   //it means it has nine meom loc under which there is further 32 locations
  24. reg [2:0] ps,ns;
  25. integer count;
  26. parameter idle=3'b000,trans=3'b001,check=3'b010,Nonseq=3'b011,Seq=3'b100,Write=3'b101,Read=3'b110; 

  27. always @(posedge hclk)
  28. begin
  29. $readmemb("memory.txt",mem);
  30. case(ps)
  31. idle : begin
  32.  if(!hresetn)
  33. begin
  34. haddr=0;
  35. hsize=0;
  36. hburst=0;
  37. htrans=0;
  38. hwdata=0;
  39. hwrite=1;
  40. ns=idle;
  41. end
  42. else if(hready & !hresp)
  43. begin
  44. hreq=1'b1;
  45. ns=trans;
  46. end
  47. end


  48. trans : begin
  49. if(hready & hgrant & !hresp)
  50. begin
  51. htrans<=2'b10;
  52. haddr<=32'd00;
  53. rhaddr<=haddr;
  54. hburst<=3'b010;
  55. hwrite<=1;
  56. hsize<=3'b010;
  57. ns=check;
  58. end
  59. else
  60. ns=idle;
  61. end

  62. check : begin
  63. if(hready & !hresp) begin
  64. if(htrans==2'b10)
  65. begin
  66. ns=Nonseq;
  67. count=0;
  68. end
  69. else if(htrans==2'b11)
  70. begin
  71. ns=Seq;
  72. count=0;
  73. end
  74. else if(htrans==2'b00)
  75. ns=idle;
  76. end
  77. end


  78. Nonseq : begin
  79. if((hgrant==1'b1) & hready & !hresp)
  80. begin
  81. /*haddr<=haddr;
  82. hburst<=3'b000;
  83. hsize<=3'b010;
  84. hwrite<=0;
  85. end*/
  86. if(hwrite)
  87. ns=Write;
  88. else
  89. ns=Read;
  90. end
  91. end

  92. Seq : begin
  93. if((hgrant==1'b1) && hready && !hresp && htrans!=2'b01) //Busy transfer htrans=2'b01
  94. begin
  95. htrans=2'b11;
  96. case(hburst) 
  97. 3'b000 : haddr=haddr; //Single Burst
  98. 3'b001 : begin //Incrementing Burst with undefined length
  99. count=count+1;
  100. if(hsize==3'b000)
  101. haddr=haddr+1;
  102. else if(hsize==3'b001)
  103. haddr=haddr+2;
  104. else if(hsize==3'b010)
  105. haddr=haddr+4;
  106. end
  107. 3'b010 : begin
  108. count=count+1; //Wrapping Burst with 4 beats
  109. if(hsize==3'b000)
  110. begin 
  111. haddr=haddr+1;
  112. if(haddr==rhaddr+16)
  113. haddr=rhaddr;
  114. end
  115. else if(hsize==3'b001)
  116. begin 
  117. haddr=haddr+2;
  118. if(haddr==rhaddr+16)
  119. haddr=rhaddr;
  120. end
  121. else if(hsize==3'b010)
  122. begin 
  123. haddr=haddr+4;
  124. if(haddr==rhaddr+16)
  125. haddr=rhaddr;
  126. end
  127. end
  128.  
  129. 3'b011 : begin
  130. count=count+1; // Incrementing Burst with 4 beats
  131. if(hsize==3'b000)
  132. haddr=haddr+1;
  133. else if(hsize==3'b001)
  134. haddr=haddr+2;
  135. else if(hsize==3'b010)
  136. haddr=haddr+4;
  137. end
  138. 3'b100 : begin
  139. count=count+1;
  140. if(hsize==3'b000) //8 Beat wrapping burst
  141. begin 
  142. haddr=haddr+1;
  143. if(haddr!=rhaddr+32)
  144. haddr=rhaddr;
  145. end
  146. else if(hsize==3'b001)
  147. begin 
  148. haddr=haddr+2;
  149. if(haddr!=rhaddr+32)
  150. haddr=rhaddr;
  151. end
  152. else if(hsize==3'b010)
  153. begin 
  154. haddr=haddr+4;
  155. if(haddr!=rhaddr+32)
  156. haddr=rhaddr;
  157. end
  158. end

  159. 3'b101 : begin
  160. count=count+1; // Incrementing Burst with 8 beats
  161. if(hsize==3'b000)
  162. haddr=haddr+1;
  163. else if(hsize==3'b001)
  164. haddr=haddr+2;
  165. else if(hsize==3'b010)
  166. haddr=haddr+4;
  167. end
  168. 3'b110 : begin
  169. count=count+1;
  170. if(hsize==3'b000) //16 Beat wrapping burst
  171. begin 
  172. haddr=haddr+1;
  173. if(haddr!=rhaddr+64)
  174. haddr=rhaddr;
  175. end
  176. else if(hsize==3'b001)
  177. begin 
  178. haddr=haddr+2;
  179. if(haddr!=rhaddr+64)
  180. haddr=rhaddr;
  181. end
  182. else if(hsize==3'b010)
  183. begin 
  184. haddr=haddr+4;
  185. if(haddr!=rhaddr+64)
  186. haddr=rhaddr;
  187. end
  188. end
  189. 3'b111 : begin
  190. count=count+1; // Incrementing Burst with 16 beats
  191. if(hsize==3'b000)
  192. haddr=haddr+1;
  193. else if(hsize==3'b001)
  194. haddr=haddr+2;
  195. else if(hsize==3'b010)
  196. haddr=haddr+4;
  197. end
  198. endcase
  199. if(hwrite)
  200. ns=Write;
  201. else
  202. ns=Read;
  203. end
  204. else if(hresp==2'b10)
  205. begin
  206. htrans=0;
  207. ns=idle; end
  208. else if(hresp==2'b11)
  209. begin ns<=ps; htrans<=0; end
  210. end

  211. Write : begin
  212. if(hready & !hresp) begin
  213. if(hsize==3'b000)
  214. begin
  215. hwdata[7:0]=mem[7];
  216. end
  217. else if(hsize==3'b001)
  218. begin
  219. hwdata[15:0]=mem[5];
  220. end
  221. else if(hsize==3'b010)
  222. begin
  223. hwdata=mem[2];
  224. end

  225. if(hburst!=0)
  226. begin
  227. if(hburst==3'b001)
  228. ns=Seq;
  229. else if((hburst==3'b010 | hburst==3'b011) & count<3)
  230. ns=Seq;
  231. else if((hburst==3'b100 | hburst==3'b101) & count<7)
  232. ns=Seq;
  233. else if((hburst==3'b110 | hburst==3'b111) & count<15)
  234. ns=Seq;
  235. else
  236. ns=idle;
  237. end
  238. else 
  239. ns=idle;
  240. end
  241. end

  242. Read :begin if(hready & !hresp)
  243. begin
  244. if(hsize==3'b000)
  245. begin
  246. rhrdata=hrdata[7:0];
  247. end
  248. else if(hsize==3'b001)
  249. begin
  250. rhrdata=hrdata[15:0];
  251. end
  252. else if(hsize==3'b010)
  253. begin
  254. rhrdata=hrdata;
  255. end


  256. if(hburst!=0)
  257. begin
  258. if(hburst==3'b001)
  259. ns=Seq;
  260. else if((hburst==3'b010 | hburst==3'b011) & count<3)
  261. ns=Seq;
  262. else if((hburst==3'b100 | hburst==3'b101) & count<7)
  263. ns=Seq;
  264. else if((hburst==3'b110 | hburst==3'b111) & count<15)
  265. ns=Seq;
  266. else
  267. ns=idle;
  268. end
  269. else 
  270. ns=idle;
  271. end
  272. end
  273. endcase
  274. end



  275. always@(posedge hclk)
  276. begin
  277. if(!hresetn)
  278. ps<=idle;
  279. else
  280. ps<=ns;
  281. end

  282. endmodule


Testbench for Master


  1. module ahb_master4_tb;

  2. // Inputs
  3. reg hready;
  4. reg hclk;
  5. reg hresetn;
  6. reg [1:0]hresp;
  7. reg [31:0] hrdata;
  8. reg hgrant;

  9. // Outputs
  10. wire [31:0] haddr;
  11. wire hwrite;
  12. wire [2:0] hsize;
  13. wire [2:0] hburst;
  14. wire [1:0] htrans;
  15. wire hreq;
  16. wire [31:0] hwdata;

  17. // Instantiate the Unit Under Test (UUT)
  18. ahb_master4 uut (
  19. .hready(hready), 
  20. .hclk(hclk), 
  21. .hresetn(hresetn), 
  22. .hresp(hresp), 
  23. .hrdata(hrdata), 
  24. .hgrant(hgrant), 
  25. .haddr(haddr), 
  26. .hwrite(hwrite), 
  27. .hsize(hsize), 
  28. .hburst(hburst), 
  29. .htrans(htrans), 
  30. .hreq(hreq), 
  31. .hwdata(hwdata)
  32. );

  33. initial begin
  34. // Initialize Inputs
  35. hready = 0;
  36. hclk = 0;
  37. hresetn = 0;
  38. hresp = 0;
  39. hrdata = 0;
  40. hgrant = 0;

  41. // Wait 100 ns for global reset to finish
  42. #100;
  43. hready <= 1;
  44. hresetn <= 1;
  45. hresp <= 0;
  46. hrdata <= 32'd4100450780;
  47. hgrant <= 1;

  48. // Wait 100 ns for global reset to finish
  49. #200;
  50. hready <= 1;
  51. hresetn <= 1;
  52. hresp <= 0;
  53. hrdata <= 32'd4123450460;
  54. hgrant <= 1;

  55. #200;
  56. hready <= 0;
  57. hresetn <= 1;
  58. hresp <= 0;
  59. hrdata <= 32'd4123450461;
  60. hgrant <= 1;

  61. // Wait 100 ns for global reset to finish
  62. #200;
  63. hready <= 1;
  64. hresetn <= 1;
  65. hresp <= 2'b11;
  66. hrdata <= 32'd4123450461;
  67. hgrant <= 1;

  68. // Wait 100 ns for global reset to finish
  69. #200;
  70. hready <= 1;
  71. hresetn <= 1;
  72. hresp <= 0;
  73. hrdata <= 32'd4123450461;
  74. hgrant <= 1;

  75. end
  76.       always #20 hclk=~hclk;
  77. initial
  78. $monitor("time=%d,hready=%b,read_data=%d,address=%d",$time,hready,uut.rhrdata,haddr);
  79.         
  80. // Add stimulus here

  81.       
  82. endmodule

Code for slave


  1. module ahb_slave2(
  2. input hsel,
  3. //Address and control
  4. input [31:0]haddr,
  5. input hwrite,
  6. input [1:0]htrans,
  7. input [2:0]hsize,
  8. input [2:0]hburst,
  9. //Data
  10. input [31:0]hwdata,
  11. //Global Signal
  12. input hclk,
  13. input hresetn,
  14. //Output signals
  15. output reg hready,
  16. output reg [1:0]hresp,
  17. output reg [31:0]hrdata
  18.     );
  19. reg rhsel;
  20. reg [31:0]rhaddr;
  21. reg rhwrite;
  22. reg [1:0]rhtrans;
  23. reg [2:0]rhsize;
  24. reg [2:0]rhburst;
  25. reg [31:0]mem[12:0];
  26. always @(posedge hclk,negedge hresetn)
  27. begin
  28. if(!hresetn)
  29. begin
  30. rhsel<=hsel;
  31. rhaddr<=0;
  32. rhwrite<=0;
  33. rhtrans<=0;
  34. rhsize<=0;
  35. rhburst<=0;
  36. hready<=1;
  37. hresp<=0;
  38. end
  39. else if(rhsel)
  40. begin
  41. rhaddr<=haddr;
  42. rhwrite<=hwrite;
  43. rhtrans<=htrans;
  44. rhsize<=hsize;
  45. rhburst<=hburst;
  46. hready=1;
  47. end
  48. end
  49. always @(posedge hclk,rhaddr)
  50. begin
  51. if(rhsel && rhwrite)
  52. begin
  53. if(rhsize==3'b000)
  54. mem[rhaddr[29:0]]=hwdata[7:0];
  55. else if(rhsize==3'b001)
  56. mem[rhaddr[29:0]]=hwdata[15:0];
  57. else if(rhsize==3'b010)
  58.         mem[rhaddr[29:0]]=hwdata;
  59.   
  60.  if(rhaddr<=12) begin
  61. hready=1;
  62. hresp=0; 
  63. end
  64. else begin
  65. hready=0;
  66. hresp=2'b01;
  67. end
  68.  end
  69. else if(rhsel && !rhwrite)
  70. begin
  71. if(rhsize==3'b000) begin
  72. hrdata[7:0]=8'd13;
  73. hrdata[31:8]=24'bx; end
  74. else if(rhsize==3'b001) begin
  75. hrdata[15:0]=16'd34;
  76. hrdata[31:16]=16'bx; end
  77. else if(rhsize==3'b010)
  78. hrdata=32'd134;
  79. if(rhaddr<=12) begin
  80. hready=1;
  81. hresp=0; 
  82. end
  83. else begin
  84. hready=0;
  85. hresp=2'b01;
  86. end
  87. end
  88. end

  89. endmodule

Test Bench for Slave


  1. module ahb_slave2_tb;

  2. // Inputs
  3. reg hsel;
  4. reg [31:0] haddr;
  5. reg hwrite;
  6. reg [1:0] htrans;
  7. reg [2:0] hsize;
  8. reg [2:0] hburst;
  9. reg [31:0] hwdata;
  10. reg hclk;
  11. reg hresetn;

  12. // Outputs
  13. wire hready;
  14. wire [1:0] hresp;
  15. wire [31:0] hrdata;

  16. // Instantiate the Unit Under Test (UUT)
  17. ahb_slave2 uut (
  18. .hsel(hsel), 
  19. .haddr(haddr), 
  20. .hwrite(hwrite), 
  21. .htrans(htrans), 
  22. .hsize(hsize), 
  23. .hburst(hburst), 
  24. .hwdata(hwdata), 
  25. .hclk(hclk), 
  26. .hresetn(hresetn), 
  27. .hready(hready), 
  28. .hresp(hresp), 
  29. .hrdata(hrdata)
  30. );

  31. initial begin
  32. // Initialize Inputs
  33. hsel = 1;
  34. haddr = 0;
  35. hwrite = 0;
  36. htrans = 0;
  37. hsize = 0;
  38. hburst = 0;
  39. hwdata = 0;
  40. hclk = 0;
  41. hresetn = 0;

  42. // Wait 100 ns for global reset to finish
  43. #100;
  44.       
  45. hsel = 1;
  46. haddr = 32'd00;
  47. hwrite = 1;
  48. htrans = 2'b10;
  49. hsize = 3'b010;
  50. hburst = 3'b010;
  51. hwdata = 32'd49;
  52. hresetn = 1;

  53. // Wait 100 ns for global reset to finish
  54. #100;
  55. hsel = 1;
  56. haddr = 32'd04;
  57. hwrite = 1;
  58. htrans = 2'b11;
  59. hsize = 3'b010;
  60. hburst = 3'b010;
  61. hwdata = 32'd45;
  62. hresetn = 1;

  63. // Wait 100 ns for global reset to finish
  64. #100;
  65. hsel = 1;
  66. haddr = 32'd08;
  67. hwrite = 1;
  68. htrans = 2'b11;
  69. hsize = 3'b010;
  70. hburst = 3'b010;
  71. hwdata = 32'd47;
  72. hresetn = 1;

  73. // Wait 100 ns for global reset to finish
  74. #100;
  75. hsel = 1;
  76. haddr = 32'd12;
  77. hwrite = 1;
  78. htrans = 2'b11;
  79. hsize = 3'b010;
  80. hburst = 3'b010;
  81. hwdata = 32'd43;
  82. hresetn = 1;
  83. // Add stimulus here

  84. end
  85.       always #50 hclk=~hclk;
  86. endmodule

Code for AHB


  1. module ahb(
  2. input hclk,
  3. input hresetn,
  4. input hgrant,
  5. output  reg hreq,
  6. input hsel
  7.     );
  8. wire hready;
  9. wire [1:0]hresp;
  10. wire hwrite;
  11. wire [31:0]haddr;
  12. wire [31:0]hwdata;
  13. wire [31:0]hrdata;
  14. wire [2:0]hsize;
  15. wire [2:0]hburst;
  16. wire [1:0]htrans;  

  17. ahb_master4 m1(hready,hclk,hresetn,hresp,hrdata,hgrant,haddr,hwrite,hsize,hburst,htrans,hreq,hwdata);
  18. ahb_slave s1(hsel,haddr,hwrite,htrans,hsize,hburst,hwdata,hclk,hresetn,hready,hresp,hrdata);


  19. endmodule

Code for AHB test bench

module ahb_tb;

  1. // Inputs
  2. reg hclk;
  3. reg hresetn;
  4. reg hgrant;
  5. reg hsel;

  6. // Outputs
  7. wire hreq;

  8. // Instantiate the Unit Under Test (UUT)
  9. ahb uut (
  10. .hclk(hclk), 
  11. .hresetn(hresetn), 
  12. .hgrant(hgrant), 
  13. .hreq(hreq), 
  14. .hsel(hsel)
  15. );

  16. initial begin
  17. // Initialize Inputs
  18. hclk = 0;
  19. hresetn = 0;
  20. hgrant = 0;
  21. hsel = 0;

  22. // Wait 100 ns for global reset to finish
  23. #200;
  24. hresetn =0;
  25. hgrant = 1;
  26. hsel = 1;

  27. // Wait 100 ns for global reset to finish
  28. #100;        
  29. // Add stimulus here
  30. hresetn = 1;
  31. hgrant = 1;
  32. hsel = 1;

  33. // Wait 100 ns for global reset to finish
  34. end
  35.       always #50 hclk=~hclk;
  36. endmodule



No comments:

Post a Comment