classを使う

概要

SystemVerilogのクラスをつかうサンプルです。
勉強用。

動作確認

ツール バージョン 結果
ncverilog 06.11-s004 OK
VCS-MX 未確認
ModelSim 未確認

テストコード

mapsクラスを2個生成し、値を代入します。
それぞれ個別に動作していることを確認。

  1. program test1;
  2. class maps;
  3. local int hoge[];
  4. local int bar [];
  5. local int size_x;
  6. local int size_y;
  7.  
  8. function new (int x,int y);
  9. this.size_x = x;
  10. this.size_y = y;
  11. this.hoge = new[x*y];
  12. this.bar = new[x*y];
  13. endfunction
  14.  
  15. function int get_hoge(int x,int y);
  16. get_hoge = hoge[y*size_x+x];
  17. endfunction
  18.  
  19. function void set_hoge(int x,int y,int val);
  20. hoge[y*size_x+x]=val;
  21. endfunction
  22. endclass
  23.  
  24. ///////////////////////////////////
  25. initial begin
  26. maps tr;
  27. maps tr2;
  28. int a;
  29. tr = new(5,3);
  30. tr2= new(10,10);
  31.  
  32. tr.set_hoge(0,0, 5);
  33. tr.set_hoge(1,0,10);
  34. tr.set_hoge(2,0,13);
  35.  
  36. tr2.set_hoge(0,6, 5);
  37. tr2.set_hoge(1,6,10);
  38. tr2.set_hoge(2,6,13);
  39.  
  40. for(int i = 0;i<=2 ; i++)begin
  41. a = tr.get_hoge(i,0);
  42. $display("a=%0d i=%0d",a,i);
  43. end
  44.  
  45. //tr.hoge[1]=2;
  46.  
  47. for(int y = 0;y<3 ; y++)begin
  48. for(int x = 0;x<5 ; x++)begin
  49. a = tr.get_hoge(x,y);
  50. $display("tr[%0d,%0d]=%0d ",x,y,a);
  51. end
  52. end
  53.  
  54. for(int y = 0;y<10 ; y++)begin
  55. for(int x = 0;x<10 ; x++)begin
  56. a = tr2.get_hoge(x,y);
  57. $display("tr2[%0d,%0d]=%0d ",x,y,a);
  58. end
  59. end
  60. end
  61. endprogram
















最終更新:2008年11月21日 11:22