《verilog实现定时器函数.doc》由会员分享,可在线阅读,更多相关《verilog实现定时器函数.doc(17页珍藏版)》请在三一办公上搜索。
1、verilog实现定时器函数使用Verilog描述硬件的基本设计单元是模块(module)。构建复杂的电子电路,主要是通过模块的相互连接调用来实现的。模块被包含在关键字module、endmodule之内。实际的电路元件。Verilog中的模块类似C语言中的函数,它能够提供输入、输出端口,可以实例调用其他模块,也可以被其他模块实例调用。模块中可以包括组合逻辑部分、过程时序部分。例如,四选一的多路选择器,就可以用模块进行描述。它具有两个位选输入信号、四个数据输入,一个输出端,在Verilog中可以表示为:module mux (out, select, in0, in1, in2, in3);o
2、utput out;input 1:0 select;input in0, in1, in2, in3;/具体的寄存器传输级代码endmodule设计人员可以使用一个顶层模块,通过实例调用上面这个模块的方式来进行测试。这个顶层模块常被称为测试平台(Testbench)。为了最大程度地对电路的逻辑进行功能验证,测试代码需要尽可能多地覆盖系统所涉及的语句、分支、条件、路径、触发、状态机状态,验证人员需要在测试平台里创建足够多的输入激励,并连接到被测模块的输入端,然后检测其输出端的表现是否符合预期(诸如SystemVerilog的硬件验证语言能够提供针对验证专门优化的数据结构,以随机测试的方式进行验
3、证,这对于高度复杂的集成电路设计验证可以起到关键作用)。实例调用模块时,需要将端口的连接情况按照这个模块声明时的顺序排列。这个顶层模块由于不需要再被外界调用,因此没有输入输出端口:module tester;reg 1:0 SELECT;reg IN0, IN1, IN2, IN3;wire OUT;mux my_mux (OUT, SELECT, IN0, IN1, IN2, IN3); /实例调用mux模块,这个实例被命名为my_muxiniTIal /需要仿真的激励代码 begin endendmodule在这个测试平台模块里,设计人员可以设定仿真时的输入信号以及信号监视程序,然后观察仿
4、真时的输出情况是否符合要求,这样就可以了解设计是否达到了预期。示例中的对模块进行实例引用时,按照原模块声明时的顺序罗列了输入变量。除此之外,还可以使用或者采用命名端口连接的方式。使用这种方式,端口的排列顺序可以与原模块声明时不同,甚至可以不连接某些端口:mux my_mux (.out(OUT), .select(SELECT), .in0(IN0), .in1(IN1), .in2(IN2), .in3(IN3);/使用命名端口连接,括号外面是模块声明时的端口,括号内是实际的端口连接/括号外相当于C语言的形式参数,括号内相当于实际参数endmodule上面所述的情况是,测试平台顶层模块的测试
5、变量直接连接了所设计的功能模块。测试平台还可以是另一种形式,即测试平台并不直接连接所设计的功能模块,而是在这个测试平台之下,将激励模块和功能模块以相同的抽象级别,通过线网相互连接。这两种形式的测试平台都可以完成对功能模块的测试。大型的电路系统,正是由各个层次不同模块之间的连接、调用,来实现复杂的功能的。verilog实现定时器函数Verilog代码module alarm_block (input wire rst,clk,hrs,mins,alarm,output wire 4:0 alarm_hr,output wire 5:0 alarm_min);wire hour_s, min_s;
6、alarm_counter counter1 ( rst, hour_s, min_s, clk, alarm_hr, alarm_min );state_machine alarm_state ( rst, alarm, hrs, mins, clk, hour_s, min_s );endmodulemodule state_machine (input wire rst,alarm,hrs,mins,clk,output reg hrs_out,mins_out);parameter IDLE = 2b00,SET_HRS = 2b01,SET_MINS = 2b11;reg1:0 st
7、ate, next;always( posedge clk or posedge rst )if( rst )state = IDLE;else state = next;always( alarm or hrs or mins or state )case( state )IDLE :if( !alarm ) beginnext = IDLE;hrs_out = 0;mins_out = 0;end else if ( alarm hrs !mins ) beginnext = SET_HRS;hrs_out = 1;mins_out = 0;endelse if( alarm !hrs m
8、ins ) beginnext = SET_MINS;hrs_out = 0;mins_out = 1;end SET_HRS : beginif( alarm hrs !mins ) beginnext = SET_HRS;hrs_out = 1;end else begin next = IDLE;hrs_out = 0;endmins_out = 0;endSET_MINS : beginif( alarm !hrs mins ) beginnext = SET_MINS;mins_out = 1;endelse beginnext = IDLE;mins_out = 0;endhrs_
9、out = 0;enddefault : beginnext = 2bx;hrs_out = 1bx;mins_out = 1bx;endendcaseendmodulemodule alarm_counter (input wire rst,hr_set,min_set,clk,output reg 4:0 hrs,output reg 5:0 mins);/set alarm minutesalways ( posedge clk or posedge rst )if( rst )mins = 0;else if( min_set !hr_set )if( mins = 6b111011
10、)mins = 6b0;elsemins = mins + 6b000001;/set alarm hoursalways ( posedge clk or posedge rst )if( rst )hrs = 0;else if( hr_set !min_set)if ( hrs = 5b10111 )hrs = 5b0;elsehrs = hrs + 5b00001;endmodulemodule Alarm_sm_2 (input wire rst,clk,compare_in,toggle_on,output reg ring);parameter IDLE = 1b0,ACTI =
11、 1b1;reg state, next;always ( posedge clk or posedge rst )if ( rst )state = IDLE;elsestate = next;always ( state or compare_in or toggle_on )case ( state )IDLE :if( toggle_on compare_in ) beginnext = ACTI;ring = 1;endelse beginnext = IDLE;ring = 0;endACTI :if( toggle_on ) beginnext = ACTI;ring = 1;e
12、ndelse beginnext = IDLE;ring = 0;enddefault : beginnext = 1bx;ring = 1bx;endendcaseendmodulemodule comparator (input wire 4:0 alarm_hr,time_hr,input wire 5:0 alarm_min,time_min,output reg compare_out );always ( * )if( ( alarm_hr = time_hr ) ( alarm_min = time_min ) )compare_out = 1;else compare_out
13、= 0;endmodulemodule convertor_ckt (input 4:0hour,input 5:0min,output 13:0disp1, disp2);wire 13:0 disp1_0;segment_decoder segment_decoder_hr ( 1b0, hour, disp1_0 );segment_decoder segment_decoder_min ( min , disp2 );Hours_filter filter ( disp1_0, disp1 );endmodulemodule segment_decoder (input wire 5:
14、0 num,output reg 13:0 disp);always ( num )case ( num )6b000000 : disp = 14b0111111_0111111;6b000001 : disp = 14b0111111_0000110;6b000010 : disp = 14b0111111_1011011;6b000011 : disp = 14b0111111_1001111;6b000100 : disp = 14b0111111_1100110;6b000101 : disp = 14b0111111_1101101;6b000110 : disp = 14b011
15、1111_1111101;6b000111 : disp = 14b0111111_0000111;6b001000 : disp = 14b0111111_1111111;6b001001 : disp = 14b0111111_1101111;6b001010 : disp = 14b0000110_0111111;6b001011 : disp = 14b0000110_0000110;6b001100 : disp = 14b0000110_1011011;6b001101 : disp = 14b0000110_1001111;6b001110 : disp = 14b0000110
16、_1100110;6b001111 : disp = 14b0000110_1101101;6b010000 : disp = 14b0000110_1111101;6b010001 : disp = 14b0000110_0000111;6b010010 : disp = 14b0000110_1111111;6b010011 : disp = 14b0000110_1101111;6b010100 : disp = 14b1011011_0111111;6b010101 : disp = 14b1011011_0000110;6b010110 : disp = 14b1011011_101
17、1011;6b010111 : disp = 14b1011011_1001111;6b011000 : disp = 14b1011011_1100110;6b011001 : disp = 14b1011011_1101101;6b011010 : disp = 14b1011011_1111101;6b011011 : disp = 14b1011011_0000111;6b011100 : disp = 14b1011011_1111111;6b011101 : disp = 14b1011011_1101111;6b011110 : disp = 14b1001111_0111111
18、;6b011111 : disp = 14b1001111_0000110;6b100000 : disp = 14b1001111_1011011;6b100001 : disp = 14b1001111_1001111;6b100010 : disp = 14b1001111_1100110;6b100011 : disp = 14b1001111_1101101;6b100100 : disp = 14b1001111_1111101;6b100101 : disp = 14b1001111_0000111;6b100110 : disp = 14b1001111_1111111;6b1
19、00111 : disp = 14b1001111_1101111;6b101000 : disp = 14b1100110_0111111;6b101001 : disp = 14b1100110_0000110;6b101010 : disp = 14b1100110_1011011;6b101011 : disp = 14b1100110_1001111;6b101100 : disp = 14b1100110_1100110;6b101101 : disp = 14b1100110_1101101;6b101110 : disp = 14b1100110_1111101;6b10111
20、1 : disp = 14b1100110_0000111;6b110000 : disp = 14b1100110_1111111;6b110001 : disp = 14b1100110_1101111;6b110010 : disp = 14b1101101_0111111;6b110011 : disp = 14b1101101_0000110;6b110100 : disp = 14b1101101_1011011;6b110101 : disp = 14b1101101_1001111;6b110110 : disp = 14b1101101_1100110;6b110111 :
21、disp = 14b1101101_1101101;6b111000 : disp = 14b1101101_1111101;6b111001 : disp = 14b1101101_0000111;6b111010 : disp = 14b1101101_1111111;6b111011 : disp = 14b1101101_1101111;default : disp = 14bx;endcaseendmodulemodule Hours_filter (input wire 13:0 num,output reg 13:0 num_disp);always ( * )if ( num1
22、3:7 = 7b011_1111 )num_disp = 7b0, num6:0 ;elsenum_disp = num;endmodulemodule Mux (input wire alarm,mode,input wire 4:0alarm_hr,time_hr,input wire 5:0alarm_min,time_min,output reg 1:0am_pm_display,output reg 4:0hours,output reg 5:0minutes);always( * ) begin/output hours and minuteshours = alarm ? ala
23、rm_hr : time_hrminutes = alarm ? alarm_min : time_min;/12hours system or 24hours system and set am_pm_displayif ( mode )case ( hours )0 : begin am_pm_display = 2b10; hours = 5b01100; end1 : am_pm_display = 2b10;2 : am_pm_display = 2b10;3 : am_pm_display = 2b10;4 : am_pm_display = 2b10;5 : am_pm_disp
24、lay = 2b10;6 : am_pm_display = 2b10;7 : am_pm_display = 2b10;8 : am_pm_display = 2b10;9 : am_pm_display = 2b10;10: am_pm_display = 2b10;11: am_pm_display = 2b10;12: am_pm_display = 2b01;13: begin am_pm_display = 2b01; hours = 5b00001 end14: begin am_pm_display = 2b01; hours = 5b00010 end15: begin am
25、_pm_display = 2b01; hours = 5b00011 end16: begin am_pm_display = 2b01; hours = 5b00100 end17: begin am_pm_display = 2b01; hours = 5b00101 end18: begin am_pm_display = 2b01; hours = 5b00110 end19: begin am_pm_display = 2b01; hours = 5b00111 end20: begin am_pm_display = 2b01; hours = 5b01000 end21: be
26、gin am_pm_display = 2b01; hours = 5b01001 end22: begin am_pm_display = 2b01; hours = 5b01010 end23: begin am_pm_display = 2b01; hours = 5b01011 enddefault: begin am_pm_display = 2bx; hours = 5bx; endendcaseelseam_pm_display = 2b00;endendmodulemodule time_block (input wire rst,clk,hrs,mins,set_time,o
27、utput wire 4:0 time_hr,output wire 5:0 time_min,time_sec);wire hour_s, min_s;time_counter counter2 ( rst, hour_s, min_s, clk, time_hr, time_min, time_sec );state_machine time_state ( rst, set_time, hrs, mins, clk, hour_s, min_s );endmodulemodule time_counter (input wire rst,hr_set,min_set,clk,output
28、 reg 4:0 hrs,output reg 5:0 mins,secs);always ( posedge clk or posedge rst )if( rst )secs = 0;else if( secs = 6b111011 )secs = 6b0;elsesecs = secs + 6b000001;always ( posedge clk or posedge rst )if( rst )mins = 0;else if( (min_set !hr_set) | (!hr_set !min_set (secs = 6b111011) )if( mins = 6b111011 )
29、mins = 6b0;else mins = mins + 6b000001;always ( posedge clk or posedge rst )if( rst )hrs = 0;else if( (hr_set !min_set) |(!hr_set !min_set (secs = 6b111011) (mins = 6b111011) )if( hrs = 5b10111 )hrs = 5b0;elsehrs = hrs + 5b00001;endmodulemodule timer (input wire rst,clk,alarm,set_time,hrs,mins,toggl
30、e_switch,mode,output wire speaker_out,output wire 1:0am_pm_display,output wire 13:0 disp1,disp2);wire 5:0 time_min, time_sec, alarm_min, min;wire 4:0 time_hr, alarm_hr, hour;wire compare;time_block time_mod ( rst, clk, hrs, mins, set_time, time_hr, time_min, time_sec );alarm_block alarm_mod ( rst, c
31、lk, hrs, mins, alarm, alarm_hr, alarm_min );comparator comparator_mod ( alarm_hr, time_hr, alarm_min, time_min, compare );Alarm_sm_2 bell_mod ( rst, clk, compare, toggle_switch, speaker_out );Mux mux_mod ( alarm, mode, alarm_hr, time_hr, alarm_min, time_min, am_pm_display, hour, min );convertor_ckt
32、convertor_mod ( hour, min, disp1, disp2 );endmodulemodule top;reg rst, clk, alarm, set_time, hrs, mins, toggle_switch, mode;wire 1:0 am_pm_display;wire speaker_out;wire 13:0 disp1, disp2;timer electronic_watch( rst, clk, alarm, set_time, hrs, mins, toggle_switch, mode, speaker_out, am_pm_display, di
33、sp1, disp2 );always beginclk = 0;#10 clk = 1;#10;endinitial begin$monitor($time,disp1=%b disp2=%b am_pm_display=%b speaker_out=%b, disp1, disp2, am_pm_display, speaker_out );rst = 0;alarm = 0;set_time = 0;hrs = 0;mins = 0;toggle_switch = 0;mode = 0;#5 rst = 1;/reset #4 rst = 0;#3000 mode = 1;/12hour
34、s system display#3000 mode = 0;/24hours system display#1200 alarm = 1; mins = 1;/set alarm minutes#200 mins = 0; toggle_switch = 1;/switch on#100 alarm = 0;#10000 toggle_switch = 0;/switch off#10000 set_time = 1; hrs = 1;/set hours#40 hrs = 0; #20 mins = 1;/set minutes#200 set_time = 0; mins =0;#40 alarm = 1; hrs = 1;/set alarm hours#40 hrs = 0; #20 mins = 1;/set alarm minutes#800 mins = 0; toggle_switch = 1;#300 alarm = 0;#24000 set_time = 1; hrs = 1;toggle_switch = 0;/set hours#240 set_time = 0; hrs = 0;#40 mode = 1; /12hours system displayendendmodule