•     上拉电阻: 
    1、当TTL电路驱动COMS电路时,如果TTL电路输出的高电平低于COMS电路的最低高电平(一般为3.5V),这时就需要在TTL的输出端接上拉电阻,以提高输出高电平的值。
    2、OC门电路必须加上拉电阻,才能使用。
    3、为加大输出引脚的驱动能力,有的单片机管脚上也常使用上拉电阻。
    4、在COMS芯片上,为了防止静电造成损坏,不用的管脚不能悬空,一般接上拉电阻产生降低输入阻抗,提供泄荷通路。
    5、芯片的管脚加上拉电阻来提高输出电平,从而提高芯片输入信号的噪声容限增强抗干扰能力。
    6、提高总线的抗电磁干扰能力。管脚悬空就比较容易接受外界的电磁干扰。
    7、长线传输中电阻不匹配容易引起反射波干扰,加上下拉电阻是电阻匹配,有效的抑制反射波干扰。
        上拉电阻阻值的选择原则包括:
    1、从节约功耗及芯片的灌电流能力考虑应当足够大;电阻大,电流小。
    2、从确保足够的驱动电流考虑应当足够小;电阻小,电流大。
    3、对于高速电路,过大的上拉电阻可能边沿变平缓。
        综合考虑以上三点,通常在1k到10k之间选取。对下拉电阻也有类似道理。
        对上拉电阻和下拉电阻的选择应结合开关管特性和下级电路的输入特性进行设定,主要需要考虑以下几个因素:
    1.驱动能力与功耗的平衡。以上拉电阻为例,一般地说,上拉电阻越小,驱动能力越强,但功耗越大,设计是应注意两者之间的均衡。
    2.下级电路的驱动需求。同样以上拉电阻为例,当输出高电平时,开关管断开,上拉电阻应适当选择以能够向下级电路提供足够的电流。
    3.高低电平的设定。不同电路的高低电平的门槛电平会有不同,电阻应适当设定以确保能输出正确的电平。以上拉电阻为例,当输出低电平时,开关管导通,上拉电阻和开关管导通电阻分压值应确保在零电平门槛之下。
    4.频率特性。以上拉电阻为例,上拉电阻和开关管漏源级之间的电容和下级电路之间的输入电容会形成RC延迟,电阻越大,延迟越大。上拉电阻的设定应考虑电路在这方面的需求。
        下拉电阻的设定的原则和上拉电阻是一样的。
        OC门输出高电平时是一个高阻态,其上拉电流要由上拉电阻来提供,设输入端每端口不大于100uA,设输出口驱动电流约500uA,标准工作电压是5V,输入口的高低电平门限为0.8V(低于此值为低电平);2V(高电平门限值)。
        选上拉电阻时:
        500uA x 8.4K= 4.2即选大于8.4K时输出端能下拉至0.8V以下,此为最小阻值,再小就拉不下来了。如果输出口驱动电流较大,则阻值可减小,保证下拉时能低于0.8V即可。
        当输出高电平时,忽略管子的漏电流,两输入口需200uA
        200uA x15K=3V即上拉电阻压降为3V,输出口可达到2V,此阻值为最大阻值,再大就拉不到2V了。选10K可用。COMS门的可参考74HC系列
        设计时管子的漏电流不可忽略,IO口实际电流在不同电平下也是不同的,上述仅仅是原理,一句话概括为:输出高电平时要喂饱后面的输入口,输出低电平不要把输出口喂撑了(否则多余的电流喂给了级联的输入口,高于低电平门限值就不可靠了
        在数字电路中不用的输入脚都要接固定电平,通过1k电阻接高电平或接地。
        1. 电阻作用:
        接电组就是为了防止输入端悬空
        减弱外部电流对芯片产生的干扰
        保护cmos内的保护二极管,一般电流不大于10mA
        上拉和下拉、限流
        改变电平的电位,常用在TTL-CMOS匹配
        在引脚悬空时有确定的状态
        增加高电平输出时的驱动能力。
        为OC门提供电流
        那要看输出口驱动的是什么器件,如果该器件需要高电压的话,而输出口的输出电压又不够,就需要加上拉电阻。
        如果有上拉电阻那它的端口在默认值为高电平你要控制它必须用低电平才能控制如三态门电路三极管的集电极,或二极管正极去控制把上拉电阻的电流拉下来成为低电平。反之,尤其用在接口电路中,为了得到确定的电平,一般采用这种方法,以保证正确的电路状态,以免发生意外,比如,在电机控制中,逆变桥上下桥臂不能直通,如果它们都用同一个单片机来驱动,必须设置初始状态.防止直通!
        2、定义:
        上拉就是将不确定的信号通过一个电阻嵌位在高电平!电阻同时起限流作用!下拉同理!
        上拉是对器件注入电流,下拉是输出电流
        弱强只是上拉电阻的阻值不同,没有什么严格区分
        对于非集电极(或漏极)开路输出型电路(如普通门电路)提升电流和电压的能力是有限的,上拉电阻的功能主要是为集电极开路输出型电路输出电流通道。
        3、为什么要使用拉电阻:
        一般作单键触发使用时,如果IC本身没有内接电阻,为了使单键维持在不被触发的状态或是触发后回到原状态,必须在IC外部另接一电阻。
        数字电路有三种状态:高电平、低电平、和高阻状态,有些应用场合不希望出现高阻状态,可以通过上拉电阻或下拉电阻的方式使处于稳定状态,具体视设计要求而定!
        一般说的是I/O端口,有的可以设置,有的不可以设置,有的是内置,有的是需要外接,I/O端口的输出类似与一个三极管的C,当C接通过一个电阻和电源连接在一起的时候,该电阻成为上C拉电阻,也就是说,如果该端口正常时为高电平,C通过一个电阻和地连接在一起的时候,该电阻称为下拉电阻,使该端口平时为低电平,作用比如:当一个接有上拉电阻的端口设为输如状态时,他的常态就为高电平,用于检测低电平的输入。
        上拉电阻是用来解决总线驱动能力不足时提供电流的。一般说法是拉电流,下拉电阻是用来吸收电流的。

  • Verilog系统函数

    2008-04-22

        Verilog提供了丰富的系统函数,这为Testbench的编写提供了方便。尤其是IEEE1364-2005,其系统级建模的能力更强。

        以前我一般常用到的系统函数只有几个:$readmemb,$readmemh,$display,$fmonitor,$fwrite,$fopen,$fclose等。通常需要对文件作预处理,才能用于Testbench读取。今天又尝试了几个其他的文件输入输出函数,不需要对文件进行预处理,直接使用需要的文件,只对需要的部分进行读取。

        $fseek,文件定位,可以从任意点对文件进行操作;

        $fscanf,对文件一行进行读写。

        下面是一些常见的应用:
        1、读写文件
    `timescale 1 ns/1 ns

    module FileIO_tb;

    integer fp_r, fp_w, cnt;
    reg [7:0] reg1, reg2, reg3;

    initial begin
      fp_r = $fopen("data_in.txt", "r");
      fp_w = $fopen("data_out.txt", "w");
     
      while(!$feof(fp_r)) begin
        cnt = $fscanf(fp_r, "%d %d %d", reg1, reg2, reg3);
        $display("%d %d %d", reg1, reg2, reg3);
        $fwrite(fp_w, "%d %d %d\n", reg3, reg2, reg1);
      end
     
      $fclose(fp_r);
      $fclose(fp_w);
    end

    endmodule

        2、

    integer file, char;
    reg eof;

    initial begin
       file = $fopenr("myfile.txt");
       eof = 0;
       while (eof == 0) begin
           char = $fgetc(file);
           eof = $feof (file);
           $display ("%s", char); 
       end
    end

        3、文件处理定位
    `define SEEK_SET 0
    `define SEEK_CUR 1
    `define SEEK_END 2
    integer file, offset, position, r;
    r = $fseek(file, 0, `SEEK_SET); /* Beginning */
    r = $fseek(file, 0, `SEEK_CUR); /* No effect */
    r = $fseek(file, 0, `SEEK_END); /* End of file */
    r = $fseek(file, position, `SEEK_SET); /* Previous loc */

        4、
    integer r, file, start, count;
    reg [15:0] mem[0:10], r16;
    r = $fread(file, mem[0], start, count);
    r = $fread(file, r16);

        5、
    integer file, position;
    position = $ftell(file);

       6、
    integer file, r, a, b;
    reg [80*8:1] string;
    file = $fopenw("output.log");
    r = $sformat(string, "Formatted %d %x", a, b);
    r = $sprintf(string, "Formatted %d %x", a, b);
    r = $fprintf(file, "Formatted %d %x", a, b);

       7、
    integer file, r;
    file = $fopenw("output.log");
    r = $fflush(file);

        8、
    // This is a pattern file - read_pattern.pat
    // time bin dec hex
    10: 001 1 1
    20.0: 010 20 020
    50.02: 111 5 FFF
    62.345: 100 4 DEADBEEF
    75.789: XXX 2 ZzZzZzZz

    `timescale 1ns / 10 ps
    `define EOF 32'hFFFF_FFFF
    `define NULL 0
    `define MAX_LINE_LENGTH 1000

    module read_pattern;
    integer file, c, r;
    reg [3:0] bin;
    reg [31:0] dec, hex;
    real real_time;
    reg [8*`MAX_LINE_LENGTH:0] line; /* Line of text read from file */

    initial
        begin : file_block
        $timeformat(-9, 3, "ns", 6);
        $display("time bin decimal hex");
        file = $fopenr("read_pattern.pat");
        if (file == `NULL) // If error opening file
            disable file_block; // Just quit

        c = $fgetc(file);
        while (c != `EOF)
            begin
            /* Check the first character for comment */
            if (c == "/")
                r = $fgets(line, `MAX_LINE_LENGTH, file);
            else
                begin
                // Push the character back to the file then read the next time
                r = $ungetc(c, file);
                r = $fscanf(file," %f:\n", real_time);

                // Wait until the absolute time in the file, then read stimulus
                if ($realtime > real_time)
                    $display("Error - absolute time in file is out of order - %t",
                            real_time);
                    else
                        #(real_time - $realtime)
                            r = $fscanf(file," %b %d %h\n",bin,dec,hex);
                    end // if c else
                c = $fgetc(file);
            end // while not EOF

        r = $fcloser(file);
        end // initial

    // Display changes to the signals
    always @(bin or dec or hex)
        $display("%t %b %d %h", $realtime, bin, dec, hex);

    endmodule // read_pattern

        9、自动比较输出结果
    `define EOF 32'hFFFF_FFFF
    `define NULL 0
    `define MAX_LINE_LENGTH 1000
    module compare;
    integer file, r;
    reg a, b, expect, clock;
    wire out;
    reg [`MAX_LINE_LENGTH*8:1];
    parameter cycle = 20;

    initial
        begin : file_block
        $display("Time Stim Expect Output");
        clock = 0;

        file = $fopenr("compare.pat");
        if (file == `NULL)
            disable file_block;

        r = $fgets(line, MAX_LINE_LENGTH, file); // Skip comments
        r = $fgets(line, MAX_LINE_LENGTH, file);

        while (!$feof(file))
            begin
            // Wait until rising clock, read stimulus
            @(posedge clock)
            r = $fscanf(file, " %b %b %b\n", a, b, expect);

            // Wait just before the end of cycle to do compare
            #(cycle - 1)
            $display("%d %b %b %b %b", $stime, a, b, expect, out);
            $strobe_compare(expect, out);
            end // while not EOF

        r = $fcloser(file);
        $stop;
        end // initial

    always #(cycle / 2) clock = !clock; // Clock generator

    and #4 (out, a, b); // Circuit under test
    endmodule // compare

        10、从文件中读数据到mem(这个好像一般人用的最多了)

    `define EOF 32'HFFFF_FFFF
    `define MEM_SIZE 200_000
    module load_mem;
    integer file, i;
    reg [7:0] mem[0:`MEM_SIZE];
    reg [80*8:1] file_name;
    initial    
    begin    
    file_name = "data.bin";    
    file = $fopenr(file_name);    
    i = $fread(file, mem[0]);    
    $display("Loaded %0d entries \n", i);    
    i = $fcloser(file);    
    $stop;    
    end endmodule // load_mem

  •     Perl is a high-level programming language. Larry Wall invented Perl and thousands have contributed their time making it a very powerful tool. Perl borrows heavily from the C programming language and copies the really useful bits from sed, awk, Unix shell, and many other tools and languages.

        Perl Productivity
        Perl's process, file, and text manipulation facilities make it particularly well-suited for tasks involving automatic code generation, report filtering, netlist patching, generating test vectors and controlling tools. Perl is FREE so maybe you should use it too.
    Followings offer advice and examples for improving design productivity through with Perl.

        Automatic Code Generation
        Perl has been used for translating between related languages: VHDL to Verilog, Xilinx Netlist Files to VHDL, VHDL to SystemC, etc. Source files with low originality are prime candidates for automatic generation.VHDL engineers regularly write testbenches. We've written an online demonstration to show you how Perl can do it for you. Perl is also useful for Coding Style conformance checking and enforcement.

        Report Filtering
        EDA tools are notorious for creating verbose report files. Many megabytes of text may contain little interesting information. Perl is well matched to extracting key information quickly.

        Netlist Patching
        EDA tools within design flows never fit together perfectly. Often, really useful features are unsupported. Netlists generated from one tool need modifying before the next tool will read them. Perl is well matched to intelligent search and replace jobs.

        Generating Test Vectors
        Perl can apply rules for generating sophisticated test vector patterns: ATM packets, MPEG streams, QAM samples with added noise, etc.

        Controlling Tools
        Perl is able to call any tool installed on its host computer. Perl can even run Telnet and FTP sessions to control remote computers. Any task involving more than one tool can be automated using a Perl script.A Perl script might run a tool, filter the textual output then decide whether to: modify constraints and try again, move on to the next tool in the flow or abort and send email notification to a project leader.

        Where To Get Perl
        Many operating systems support Perl. Most importantly ports exist for, Linux, Windows and versions of Unix. The standard Perl release is available in source code only. However, hardware engineers don't have time to compile their own and binary distributions are freely available. You can find ports for many operating systems on CPAN (Comprehensive Perl Archive Network. Currently, Doulos use the free ActivePerl distributions from ActiveState for Solaris and Windows.

        Regular Expressions
        Almost every Perl script uses regular expressions. We've written a Perl Regular Expression Viewer tool to help you write them faster. Download your free copy now.

        Essential Perl training
        To become a skilled and efficient user of Perl for hardware design, you need to attend Essential Perl, an intensive 3 day course teaching the application of Perl specifically to ASIC, FPGA and PLD design.