home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / verilog / 343 < prev    next >
Encoding:
Text File  |  1992-09-11  |  9.3 KB  |  319 lines

  1. Newsgroups: comp.lang.verilog
  2. Path: sparky!uunet!wupost!csus.edu!netcom.com!geethu
  3. From: geethu@netcom.com (Geetha Madhavan)
  4. Subject: Re: Analogue modeling by Verilog
  5. Message-ID: <=fwnrw.geethu@netcom.com>
  6. Date: Sat, 12 Sep 92 09:05:49 GMT
  7. Organization: Netcom - Online Communication Services  (408 241-9760 guest) 
  8. Summary: Some analog models
  9. References: <1992Sep3.071552.9412@uxmail.ust.hk>
  10. Sender: geethu@netcom.com
  11. Followup-To: geethu@netcom.com
  12. Keywords: DAC, ADC, $bitstoreal
  13. Lines: 304
  14.  
  15.  
  16. >>> I am a new learner of verilog.  I want to find some information about the
  17. >>> ability of modelling analog circuit by verilog.  
  18. >>> 
  19. >>> Would someone help me please?
  20.  
  21. The following are 2 limited-capability analogue models for
  22.  
  23. 1. Analog-To-Digital and 
  24. 2. Digital-to-analog
  25.  
  26. that show some of the limited capability of verilog for
  27. analog modeling. The capability is limited and
  28. the model shows
  29.  
  30. a. The existence and the use of system tasks for transferring
  31. "binary equivalents" to real numbers, which can then be
  32. used for writing behavioral models in Verilog (i.e. using
  33. $bitstoreal...)
  34.  
  35. However, the models does not demonstrate the weakness of
  36. Verilog to describe some important analog behavior such as
  37.  
  38. 1. continuous-time/analog signals, binary equivalents are
  39. used for behavioral representation.  
  40. 2. Limitations of Verilog-HDL to represent KCL, KVL node equations.
  41. 3. Absence of differentiator-integrator operators.  
  42. 4. Absence of Analog components (inductors, voltage source
  43. ... etc.)  and functional-blocks/templates.  
  44. 5. Difficult to model transfer (Laplace and Z-domain)
  45. functions.  
  46. 6. Difficult to model non-linear voltage/current dependant
  47. sources.  
  48. 7. Control flow mechanisms that exist with current
  49. Verilog-HDL are not sufficient to meet the control
  50. requirements for both time-domain and frequency-domain
  51. analysis.
  52.  
  53. (of course similar arguments apply to CURRENT VHDL -- 1076)
  54.  
  55. The models are described below -: Please note, I do not have
  56. access to a Verilog simulator at present and hence the models
  57. have not been tested lately. A better solution, as someone has
  58. already pointed out is to look at Mixed-mode solutions (for
  59. time-domain behavioral modeling) -- assuming that you want
  60. to do some analog+digital.
  61.  
  62. /************************************************************************
  63.  Analog to Digital convertor 
  64.  
  65. Parameters:  
  66.     N - Number of bits (compiled into model)
  67. Inputs:  
  68.     Vin (analog)    - Input voltage to be converted  
  69.     Vref (analog)    - Reference voltage 
  70.     Clk (digital)    - clock  
  71.     StartConv (digital)    - Start conversion
  72. Outputs:  
  73.     Dout [N-1:0] (digital)    - digital output  
  74.     Eoc (digital)     - end of conversion
  75.  
  76. Operation: 
  77.     On the falling edge of StartConv, the input Vin
  78. is sampled and the conversion is started. If StartConv
  79. rises before the conversion is complete, the conversion
  80. will not complete and the output will show the incomplete
  81. conversion.
  82.  
  83. The conversion requires N+1 falling edges of clk to
  84. complete. On the last falling edge of clk, eoc will be set
  85. to 0. Eoc will remain 0 until StartConv is reset to 1.
  86.  
  87. With Vin <= 0, the output will be zero. With Vin >= Vref,
  88. the output will have all bits set. In between, the output
  89. is linear with the input. The output will change during
  90. the conversion until the eoc. It will then hold that value
  91. until the next StartConv.
  92.  
  93. The block represents the Verilog behavioral model for the
  94. ADC, where vin_bits is the "binary equivalent" of the
  95. analog input, vref_bits is the "binary equivalent" of the
  96. analog reference voltage, Dout is the binary output
  97. signal, eoc is the end-of-conversion flag, StartConv is
  98. the signal used to start/end the operations of the Verilog
  99. module, vin_d_bits and vin_t_bits are used to determine
  100. the value of vin at any time ($time in Verilog).
  101.  
  102. The Verilog task "ADConversion" uses $bitstoreal to
  103. convert binary versions of the analog signals to real
  104. numbers and determines the digital output Dout (if vin >
  105. vref the output Dout will have all bits set to 1 and if
  106. vin <= 0 the digital output bits are set to 0, if (0 < vin
  107. < vref) the output is linear with the input). The
  108. behavioral models utilize the ability to model
  109. mathematical equations in Verilog (using real numbers).
  110. The model uses only binary equivalents and demonstrates
  111. the weakness of Verilog to represent real analog 
  112. (continuous) signals. 
  113.  
  114. I am not sure whether $bitstoreal ..etc are part of the
  115. OVI Language Reference Manual -- do not have one handy!
  116.  
  117. ***********************************************************/
  118. `timescale 1ns / 1ns
  119. `define N 16 /* Number of bits of the ADC */
  120. `define N1 15
  121.  
  122. module adc( startConv, clk, eoc, Vin_bits, Vin_d_bits, Vin_t_bits,
  123.     Vref_bits, Dout );
  124. /*
  125.  * startConv  : start conversion signal; conversion starts at falling edge
  126.  * clk          : clock signal; digital signal
  127.  * eoc          : end of conversion; goes low when conversion done;
  128.  * Vin_bits   : binary version of real input data
  129.  * Vref_bits  : binary version of reference voltage
  130.  * stobe : samples input data on rising edge of strobe
  131.  * Dout  : binary output data
  132.  */
  133.  
  134. input startConv, clk, Vin_bits, Vin_d_bits, Vin_t_bits, Vref_bits;
  135. output Dout, eoc;
  136. wire [63:0] Vin_bits, Vin_d_bits, Vin_t_bits;
  137. wire [63:0] Vref_bits;
  138. reg [`N1:0] Dout;
  139. reg eoc;
  140.  
  141. real Vin, Vin_d, Vin_t, Vsample, Vref, Vdac, Vbit;
  142. integer i;
  143. reg [`N1:0] bit;
  144. reg [`N1:0] fullScale;
  145.  
  146. initial begin
  147.   eoc = 1;
  148.   Dout= `N'b0;
  149. end
  150.  
  151. always @ (negedge startConv) ADconversion;
  152.  
  153. always @ (posedge startConv) begin
  154.   disable ADconversion;
  155.   eoc = 1;
  156. end
  157.  
  158. task ADconversion;
  159. begin
  160.   eoc = 1;
  161.   Dout= `N'b0;
  162.   fullScale= 'b0;
  163.   fullScale= ~fullScale ; 
  164.   Vin = $bitstoreal( Vin_bits );
  165.   Vin_d = $bitstoreal( Vin_d_bits );
  166.   Vin_t = $bitstoreal( Vin_t_bits );
  167.  
  168. // find the value of Vin corresponding to the present time.
  169.   Vsample = Vin + Vin_d * ($time - Vin_t);
  170.  
  171.   Vref = $bitstoreal( Vref_bits );
  172. //  $display("%0d start ADC, input = %g", $time, Vin);
  173.  
  174.   Vdac = 0.0;
  175.   bit = `N'b1 << (`N1);
  176.   i = `N1;
  177.   repeat (`N) begin
  178.     @ (negedge clk) Vbit= (bit*Vref)/fullScale;
  179.     Vdac= Vdac + Vbit;
  180.     if (Vdac >= Vsample) begin
  181.       Dout[i] = 1'b0;
  182.       Vdac = Vdac - Vbit;
  183.     end
  184.     else begin
  185.       Dout[i] = 1'b1;
  186.     end
  187. //    $display("%0d clk= %b startConv= %b eoc= %b bit= %b Dout= %h Vdac= %g",
  188. //        $time, clk, startConv, eoc, bit, Dout, Vdac);
  189.     bit = bit >> 1;
  190.     i = i - 1;
  191.   end
  192.   @ (negedge clk) eoc = 0;
  193. end
  194. endtask
  195. endmodule 
  196.  
  197.  
  198. /********************************************************************
  199. Parameters:  N - Number of bits (compiled into model)
  200. Inputs:  
  201.     Din [N-1:0] (digital)    - digital input  
  202.     Vref (analog)    - Reference voltage  
  203.     Clk (digital)    - clock  
  204.     StartConv (digital)    - Start conversion
  205. Outputs:  
  206.     Vout (analog)    - Input voltage to be converted  
  207.     Eoc (digital)     - end of conversion 
  208.  
  209. Operation: 
  210.     On the falling edge of StartConv, the input Din is
  211. sampled and the conversion is started. If StartConv rises
  212. before the conversion is complete, the conversion will not
  213. complete and the output will not change.  The conversion
  214. requires N+1 falling edges of clk to complete. On the last
  215. falling edge of clk, eoc will be set to 0 and the output
  216. will appear. Eoc will remain 0 until StartConv is reset to
  217. 1.
  218.  
  219.  The output will be linear with the input, and in the
  220. range 0 to Vref.  The output will change only at the eoc.
  221. It will then hold that value until the next eoc.
  222.  
  223.  The Verilog task DAConversion is enabled at the negedge of
  224. StartConv and uses $bitstoreal and $realtobits (to convert
  225. the real numbers to binary equivalents of the analog
  226. output).
  227.  
  228.  
  229. DAC Verilog Behavioral model 
  230. ************************************************************/
  231.  
  232. `timescale 1ns / 1ns
  233. `define N 16 /* Number of bits of the ADC */
  234. `define N1 15
  235. `define TTIME 10 /* output transition time */
  236.  
  237. module dac( startConv, clk, eoc, Din, Vref_bits, 
  238.     Vout_bits, Vout_d_bits, Vout_t_bits );
  239. /*
  240.  * startConv  : start conversion signal; conversion starts at falling edge
  241.  * clk          : clock signal; 
  242.  * eoc          : end of conversion; goes low when conversion done;
  243.  * Din          : digital input vector 
  244.  * Vref_bits  : binary version of reference voltage
  245.  * Vout_bits  : analog output (in binary form)
  246.  */
  247.  
  248. input startConv, clk, Din, Vref_bits;
  249. output Vout_bits, Vout_d_bits, Vout_t_bits, eoc;
  250. reg [63:0] Vout_bits, Vout_d_bits, Vout_t_bits;
  251. reg eoc;
  252. wire [63:0] Vref_bits;
  253. wire [`N1:0] Din;
  254. reg [`N1:0] Din_sample;
  255.  
  256. real Vin, Vref, Vdac, Vbit, Vout_d, tmp_time;
  257. integer i;
  258. reg [`N1:0] bit;
  259. reg [`N1:0] fullScale;
  260.  
  261. initial begin
  262.   eoc = 1;
  263.   Vdac=0.0;
  264.   Vout_bits= $realtobits(Vdac);
  265. end
  266.  
  267. always @ (negedge startConv) DAconversion;
  268.  
  269. always @ (posedge startConv) begin
  270.   disable DAconversion;
  271.   eoc = 1;
  272. end
  273.  
  274. task DAconversion;
  275. begin
  276. //  $display("%0d start DAC, input = %h", $time, Din);
  277.   eoc = 1;
  278.   Vdac=0.0;
  279.   Din_sample = Din;
  280.   fullScale= 'b0;
  281.   fullScale= ~fullScale ; 
  282.   bit=1'b1 << (`N1);
  283.   Vref = $bitstoreal( Vref_bits );
  284.   Vdac = 0.0;
  285.   i = `N1;
  286.   Vbit= Vref / fullScale;
  287.   repeat (`N) begin
  288.     @ (negedge clk) 
  289.     if (Din_sample[i]) Vdac= Vdac + (bit*Vbit);
  290. //    $display("%0d clk= %b startConv= %b eoc= %b bit= %b Vdac= %g ",
  291. //    $time, clk, startConv, eoc, bit, Vdac);
  292.     i = i - 1;
  293.     bit = bit >> 1;
  294.   end
  295.   @ (negedge clk)
  296.   Vout_d = (Vdac - $bitstoreal(Vout_bits)) / `TTIME;
  297.   Vout_d_bits = $realtobits(Vout_d);
  298.   tmp_time = $time;
  299.   Vout_t_bits = $realtobits(tmp_time);
  300.   #`TTIME
  301.   Vout_bits = $realtobits(Vdac);
  302.   Vout_d_bits = $realtobits(0.0);
  303.   tmp_time = $time;
  304.   Vout_t_bits = $realtobits(tmp_time);
  305.   eoc = 0;
  306. end
  307. endtask
  308. endmodule 
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.