Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************************************************************
  2. *
  3. * Project: DD2 - Assig. 1.2 SAR ADC Ctrl
  4. * Module: sar_adc_ctrl_tb.v
  5. * Description:
  6. > Parametrized in # of bits and # of sampling cycles
  7. > Uses a ring counter (can also use a decoder but that results in more area)
  8. > > 64-128 transistors Vs. 80-160
  9. > State diagram:
  10.  
  11.             !go                       sample_cyc times
  12.           +----+                  +------+
  13.           |    |                  |      |
  14.         +-+----v--+         +-----+--+   |
  15.         |   IDLE  |         |Sampling|   |
  16. !rst---->         +---go--->+        | <-+
  17.         +----+----+         +----+---+
  18.              ^                   |
  19.              |                   |
  20.           !go|                   |
  21.              |                   v
  22.         +----+----+          +--------+
  23.         | DONE    |          |Conv.   |
  24.         |         +<---------+        +---+
  25.         +-+----+--+          +-----+--+   |
  26.           |    ^                   ^      |#bits times
  27.           |    |                   +------+
  28.           +----+
  29.             go
  30.  
  31. * Author: Ahmed Ghazy (ax3ghazy@aucegypt.edu)
  32. *
  33. * Changelog:
  34.   > Fri Feb 16 - Done
  35. ********************************************************************/
  36. `timescale 1ns/1ns
  37. `default_nettype none
  38.  
  39.  
  40. //A simulation of V_REF, V_IN, etc using real (float)
  41. module sar_adc_ctrl_tb;
  42.     parameter resolution = 8;
  43.     parameter sample_cyc = 2;
  44.  
  45.     //Inputs
  46.     reg clk;
  47.     reg rst_n;
  48.     reg go;
  49.     reg cmp;
  50.  
  51.     //Outputs
  52.     wire sample;
  53.     wire valid;
  54.     wire [resolution-1:0] result;
  55.     wire [resolution-1:0] value;
  56.  
  57.     wire [resolution-1:0] result_correct = V_IN/step_size;
  58.     //it can be shown that the output produced can differ from
  59.     //the optimal result by at most 1 (being less by 1)
  60.     wire error = valid && (result_correct-result > 1);
  61.  
  62.     //Instantiation of Unit Under Test
  63.     sar_adc_ctrl #(.resolution(resolution), .sample_cyc(sample_cyc))
  64.     uut (
  65.         .clk(clk),
  66.         .rst_n(rst_n),
  67.         .go(go),
  68.         .cmp(cmp),
  69.         .sample(sample),
  70.         .valid(valid),
  71.         .result(result),
  72.         .value(value)
  73.     );
  74.  
  75.  
  76.     always #5 clk = ~clk;
  77.    
  78.     real V_REF, V_IN, V_DAC, step_size;
  79.     initial begin
  80.         V_REF = 50.0;
  81.         V_IN = 13.5;
  82.         step_size = V_REF/((1<<resolution)-1);
  83.     end
  84.     always @ *
  85.         V_DAC = value*step_size;
  86.     always @ *
  87.         cmp = V_DAC > V_IN;
  88.    
  89.     integer i;
  90.     initial begin
  91.     //Inputs initialization
  92.         clk = 0;
  93.         rst_n = 1;
  94.         go = 0;
  95.         cmp = 0;
  96.  
  97.  
  98.     //Wait for the reset
  99.         #50;
  100.         @ (negedge clk) rst_n = 0;
  101.         @ (negedge clk) rst_n = 1;
  102.         for (i = 0; i < 128; i = i + 1) begin
  103.             @ (negedge clk) go = 1;
  104.             @ (negedge clk) go = 0;
  105.             #200;
  106.             V_IN = V_REF/((1<<16)-1)*($urandom%(1<<16));
  107.         end
  108.            
  109.  
  110.  
  111.  
  112.     end
  113.  
  114. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement