COE 405 Logic Design with Behavioral Models of
COE 405 Logic Design with Behavioral Models of Combinational & Sequential Logic Dr. Aiman H. El-Maleh Computer Engineering Department King Fahd University of Petroleum & Minerals
Outline n Behavioral Modeling n Data Types for Behavioral Modeling n Assign Statement n Verilog Operators n Always Block, Procedural Assignment n If and Case Statements n Latches & D Flip-Flops n Data Flow / Register Transfer Level (RTL) n Behavioral Modeling of Control Unit n Registers & Counters 1 -2
Behavioral Modeling n Behavioral modeling describes the functionality of a design • What the design will do • Not how the design will be built in hardware n Behavioral models specify the input-output model of a logic circuit and suppress details about its low level internal structure. n Behavioral modeling encourages designers to • Rapidly create a behavioral prototype of a design • Verify its functionality • Use synthesis tool to optimize and map design into a given technology 1 -3
Data Types for Behavioral Modeling n All variables in Verilog have a predefined type. n There are two families of data types: nets and registers. n Net variables act like wires in physical circuit and establish connectivity between design objects. n Net types include: wire, tri, wand, wor, triand, trior, supply 0, supply 1, tri 0, tri 1, trireg. 1 -4
Data Types for Behavioral Modeling n Register variables act like variables in ordinary procedural languages – they store information while the program executes. n Register types include: reg, integer, realtime, time. n For synthesis, we use mainly the data types wire, reg and integer. n A wire and a reg have a default size of 1 bit. n Size of integer is the size of word length in a computer, at least 32. n A reg variable may never be the output of a primitive gate, the target of continuous assignment. 1 -5
Assign Statement n The keyword assign declares a continuous assignment. n It associates the Boolean expression on the RHS with the variable on the LHS. n The assignment is sensitive to the variables in the RHS. n Any time an event occurs on any of the variables on the RHS, the RHS expression is revaluated and the result is used to update the LHS. n A continuous assignment is said to describe implicit combinational logic. 1 -6
Assign Statement module AOI_5_CA 1 ( input x_in 1, x_in 2, x_in 3, x_in 4, x_in 5, enable, output y_out ); assign y_out = enable ? !((x_in 1 && x_in 2) || (x_in 3 && x_in 4 && x_in 5)) : 1’bz; endmodule 1 -7
Assign Statement n The conditional operator (? : ) acts like a software if-then -else switch that selects between two expressions. n If the value of enable is true, the expression to the right of the ? Is evaluated and used to assign value to y_out. n Otherwise, the expression to the right of the : is used. n Using 1’bz illustrates how to write models that include three-state outputs. n A module may contain multiple continuous assignments; the assignments are active concurrently with all other continuous assignments, primitives, behavioral statements, and instantiated modules. 1 -8
Assign Statement module Mux_2_32_CA #(parameter wordsize=32) ( output [wordsize-1: 0] mux_out, input [wordsize-1: 0] data_1, data_0, input select ); assign mux_out = select ? data_1 : data_0; endmodule 1 -9
Verilog Operators {} concatenation ~ bit-wise NOT + - * / ** arithmetic & bit-wise AND % modulus | bit-wise OR > >= < <= relational ^ bit-wise XOR ^~ ~^ bit-wise XNOR & reduction AND | reduction OR ~& reduction NAND ~| reduction NOR ^ reduction XOR ! logical NOT && logical AND || logical OR == logical equality != logical inequality === case equality ~^ ^~ reduction XNOR !== case inequality << shift left ? : conditional >> shift right 1 -10
Verilog Operators n Arithmetic Operators: • Each operator takes two operands. + and – could also take a • • • n single operand During synthesis, the + and - operators infer an adder and a subtractor Xilinx XST software can infer a block multiplier during synthesis for the multiplication operator /, %, and ** operators usually cannot be synthesized automatically Shift operators: Four shift operators • >>, << logical shift right and left (0 s inserted from the right or • the left) >>>, <<< arithmetic shift right and left (sign bits are shifted in for the >>> operation and 0's are shifted in for the <<< operation) 1 -11
Verilog Operators • If both operands of a shift operator are signals, as in a << b, • n the operator infers a barrel shifter, a fairly complex circuit If the shifted amount is fixed, as in a << 2, the operation infers no logic and involves only routing of the input signals (can also be done with {} operator) Examples of shift operations: 1 -12
Verilog Operators n Relational and equality operators: • compare two operands and return a 1 -bit logical (Boolean) • • value: either 0 or 1 4 relational operators: >, <, <=, and >= 4 equality operators: ==, !=, ===, and !== Case equality (===) and case inequality (!==) operators, take the x and z bits in the operands into consideration in the match cannot be synthesized. The relational operators and the == and != operators infer comparators during synthesis 1 -13
Verilog Operators n Bitwise operators: • 4 basic bitwise operators: & (and), I (or), ^ (xor), and ! (not) • The first three operators require two operands • Negation and xor operation can be combined, as in ~^ or ^~ • • to form the xnor Operations are performed on a bit-by-bit basis Ex. : let a, b, and c be 4 -bit signals: i. e. wire [3: 0] a , b , c ; The statement: assign c = a I b ; is the same as: assign c[3] = a[3] I b[3]; assign c[2] = a[2] I b[2]; assign c[1] = a[1] I b[1]; assign c[0] = a[0] I b[0]; 1 -14
Verilog Operators n Reduction operators: &, I , and ^ operators may have only one operand hence are known as reduction operators. • The single operand usually has an array data type. • The designated operation is performed on all elements of the • array and returns a 1 -bit result. For example, let a be a 4 -bit signal and y be a 1 -bit signal: wire [3: 0] a ; wire y ; The statement: assign y = I a ; // only one operand is the same as: assign y = a[3] | a[2] | a[1] | a[0] ; 1 -15
Verilog Operators n Logical operators: && (logical and), II (logical or), and ! (logical negate) • Operands of a logical operator are interpreted as false (when • • n all bits are 0's) or true (when at least one bit is 1), and the operation always returns a 1 -bit result Usually used as logical connectives of Boolean expressions, Bitwise and logical operators can be used interchangeably in some situations. Examples of bitwise and logical operations 1 -16
Verilog Operators n Conditional operator: ? : takes three operands and its general format is [signal] = [boolean-exp] ? [true-exp] : [false-exp]; • The [boolean-expl] is a Boolean expression that returns true • • • (1’b 1) or false ( 1'b 0). Ex. : assign max = (a>b) ? a : b; //max will get the maximum of the signals a and b The operator can be thought as a simplified if-then-else statement. Infers a mux. Can be cascaded or nested: assign max = (a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c ) ; // max of three signals 1 -17
Verilog Operators n Concatenation and replication operators: { } and {{ }} n { } combines segments of elements and small arrays to form a large array: wire a 1; wire [3: 0] a 4; wire [7: 0] b 8, c 8, d 8; assign b 8 = {a 4 , a 4} ; assign c 8 = {a 1, a 4, 2'b 00 } ; assign d 8 = {b 8[3: 0] , c 8[3: 0]} ; n Concatenation operator involves reconnection of the input and output signals and only requires "wiring”. Can be used for shifting or rotating data. 1 -18
Verilog Operators wire [7: 0] a, rot, shl , sha; assign rot = {a[2: 0], a[7: 3]} ; // Rotate a to right 3 bits assign shl = {3'b 000, a[7: 3]} ; // shift a to right 3 bits and insert 0 s (logical shift) assign sha = {a[7] , a[7: 3]} ; // arithmetic shift a to right 3 bits n The replication operator, N{ }, replicates the enclosed string. The replication constant, N, specifies the number of replications. For example: {4{2'b 01}} returns 8'b 0101. 1 -19
Behavioral Description of an Adder module adder #(parameter width = 4) (output cout, output [width-1: 0] sum, input [width-1: 0] a, b, input cin); 4 -bit operands, 5 -bit result assign {cout, sum} = a + b + cin; // note: Verilog treats wires as ‘unsigned’ numbers endmodule { Cout, S } is a 5 bit bus: Cout S[3] S[2] S[1] S[0] 1 -20
Always Block n always blocks are procedural blocks that contain sequential statements. n Syntax always @(sensitivity list) begin ………. end n sensitivity list prevents the always block from executing again until another change occurs on a signal in the sensitivity list. • Level type • always @(a or b or c) • Edge type • always @(posedge clock) • always @(negedge clock) 1 -21
Procedural Assignment n Assignments inside an always block are called procedural assignments n Can only be used within an always block or initial block n Two types : blocking assignment and nonblocking assignment. Basic syntax : • [variable-name] = [expression] ; // blocking assignment • [variable-name] <= [expression] ; // nonblocking assignment n In a blocking assignment, the expression is evaluated and then assigned to the variable immediately, before execution of the next statement (the assignment thus "blocks" the execution of other statements). It behaves like the normal variable assignment in the C language. 1 -22
Procedural Assignment n In a nonblocking assignment, the evaluated expression is assigned at the end of the always block (the assignment thus does not block the execution of other statements). n The basic rule of thumb is: n if-else and case statement are only in always block. • Use blocking assignments for a combinational circuit. • Use nonblocking assignments for a sequential circuit. 1 -23
Wire vs. Reg n There are two types of variables in Verilog: n Both variables can be used as inputs anywhere • wire (all outputs of assign statements must be wire) • reg (all outputs modified in always blocks must be reg) • Can use reg or wire as inputs (RHS) to assign statements • assign bus = Latch. Output + Immediate. Value • // bus must be a wire, but Latch. Output can be a reg • Can use reg or wire as inputs (RHS) in always blocks • always @ (in or clk) • if (clk) out = in // in can be a wire, out must be a reg 1 -24
Algorithm-Based Models n Algorithms prescribe a sequence of procedural assignments within a cyclic behavior. n The algorithm described by the model does not have explicit binding to hardware. n It does not have an implied architecture of registers, datapaths and computational resources. n This style is most challenging for a synthesis tool. n Synthesis tool needs to perform architectural synthesis which extracts the needed resources and schedules them into clock cycles. 1 -25
If Statements Syntax if (expression) begin. . . procedural statements. . . end else if (expression) begin. . . statements. . . end. . . more else if blocks else begin. . . statements. . . end module ALU #(parameter n=8) (output reg [n-1: 0] c, input [1: 0] s, input [n-1: 0] a, b); always @(s or a or b) begin if (s==2'b 00) c = a + b; else if (s==2'b 01) c = a - b; else if (s==2'b 10) c = a & b; else c = a | b; endmodule 1 -26
Case Statements Syntax case (expression) case_choice 1: begin. . . statements. . . end case_choice 2: begin. . . statements. . . end. . . more case choices blocks. . . default: begin. . . statements. . . endcase module ALU 2 #(parameter n=8) (output reg [n-1: 0] c, input [1: 0] s, input [n-1: 0] a, b); always @(s or a or b) begin case (s) 2'b 00: c = a + b; 2'b 01: c = a - b; 2'b 10: c = a & b; default: c = a | b; endcase endmodule 1 -27
Example: Full Adder module fadd 2 (output reg S, Cout, input A, B, Cin); always @(A or B or Cin) begin S = (A ^ B ^ Cin); Cout = (A & B) | (A & Cin) | (B & Cin); endmodule 1 -28
Example: Comparator module comp #(parameter width=32) (input [width-1: 0] A, B, output A_gt_B, A_lt_B, A_eq_B); assign A_gt_B = (A>B); assign A_lt_B = (A<B); assign A_eq_B = (A==B); endmodule 1 -29
Example: Comparator module comp 2 #(parameter width=2) (input [width-1: 0] A, B, output reg A_gt_B, A_lt_B, A_eq_B); always @(A, B) begin A_gt_B = 0; A_lt_B = 0; A_eq_B = 0; if (A == B) A_eq_B = 1; else if (A > B) A_gt_B = 1; else A_lt_B = 1; endmodule 1 -30
Example: 2 x 1 Multiplexer n Method 1 module mux 2 x 1 (input b, c, select, output a); assign a = (select ? b : c); endmodule n Method 2 module mux 2 x 1 (input b, c, select, output reg a); always@(select or b or c) begin if (select) a=b; else a=c; endmodule Method 3 module mux 2 x 1 (input b, c, select, output reg a); always@(select or b or c) begin case (select) 1’b 1: a=b; 1’b 0: a=c; endcase endmodule 1 -31
Example: De. Mux module demux ( input D, select, output reg y 0, y 1); always @( D or select ) begin if( select == 1’b 0) begin y 0 = D; y 1 = 1’b 0; end else begin y 0 = 1’b 0; y 1 = D; end endmodule 1 -32
Example: Arithmetic Unit module arithmetic #(parameter width=8) (input [width-1: 0] A, B, input [1: 0] Sel, output reg [width-1: 0] Y, output reg Cout); always @(A or B or Sel) begin case (Sel) 2'b 00 : {Cout, Y} = A+B; 2'b 01 : {Cout, Y} = A-B; 2'b 10 : {Cout, Y} = A+1; 2'b 11 : {Cout, Y} = A-1; default: begin Cout=0; Y=0; endcase endmodule 1 -33
Example: Logic Unit module logic #(parameter width=4) (input [width-1: 0] A, B, input [2: 0] Sel, output reg [width-1: 0] Y); always @(A or B or Sel) begin case (Sel) 3'b 000 : Y = A & B; // A and B 3'b 001 : Y = A | B; // A or B 3'b 010 : Y = A ^ B; // A xor B 3'b 011 : Y = ~A; // 1’s complement of A 3'b 100 : Y = ~(A & B); // A nand B 3'b 101 : Y = ~(A | B); // A nor B default : Y = 0; endcase endmodule 1 -34
Latches & Level-Sensitive Circuits n Latch can be modeled as: module latch (output q, qb, input set, reset); assign q = ~(set & qb); assign qb = ~(reset & q); endmodule n Some synthesis tools do not accommodate this form of feedback. n Latch is inferred by synthesis tools as follows: module dlatch (output q, input data_in, enable); assign q = enable ? data_in : q; endmodule dlatch 2 (output q, input data_in, enable, reset); assign q = (reset==1'b 0)? 0: enable ? data_in : q; endmodule 1 -35
D Latch module dlatch (output q, input data, enable); assign q = enable ? data: q; endmodule dlatch 2 (output reg q, input data, enable); always @(enable, data) if (enable == 1'b 1) q <= data; endmodule 1 -36
D Flip Flop – Synchronous Set/Reset module dff (output reg q, output q_bar, input data, set_b, reset_b, clk); assign q_bar = !q; always @(posedge clk) // Synchronous set/reset if (reset_b == 1'b 0) q <= 0; else if (set_b == 1'b 0) q <=1; else q <= data; endmodule 1 -37
D Flip Flop – Asynchronous Set/Reset module dff 2 (output reg q, output q_bar, input data, set_b, reset_b, clk); assign q_bar = !q; always @(posedge clk, negedge set_b, negedge reset_b ) // Asynchronous set/reset if (reset_b == 1'b 0) q <= 0; else if (set_b == 1'b 0) q <=1; else q <= data; endmodule 1 -38
Data Flow/ RTL Models n Data flow models describe concurrent operations on signals where computations are initiated at active edges of a clock and completed to be stored in a register at next active edge. n Also referred to as Register Transfer Level (RTL) as they describe transfer of data among registers. n A behavioral model of combinational logic can be described using concurrent assign statements or always statements. n A behavioral model of sequential logic can be described using always statements. 1 -39
Shift Register module shiftreg (output reg A, input E, clk, rst); reg B, C, D; always @(posedge clk, posedge rst) begin if (rst == 1'b 1) begin A=0; B=0; C=0; D=0; end else begin A = B; B = C; C = D; D = E; end endmodule 1 -40
Shift Register module shiftreg 2 (output reg A, input E, clk, rst); reg B, C, D; always @(posedge clk, posedge rst) begin if (rst == 1'b 1) begin A=0; B=0; C=0; D=0; end else begin D = E; What will happen in this model? C = D; B = C; A = B; end endmodule 1 -41
Shift Register module shiftreg 3 (output reg A, input E, clk, rst); reg B, C, D; always @(posedge clk, posedge rst) begin if (rst == 1'b 1) begin A<=0; B<=0; C<=0; D<=0; end else begin A <= B; B <= C; Non-blocking assignments (<=) execute concurrently. So they are order independent. C <= D; D <= E; end endmodule 1 -42
Behavioral Models of Multiplexor module Mux_4_1 #(parameter width=32) (output [width-1: 0] mux_out, input [width-1: 0] data_3, data_2, data_1, data_0, input [1: 0] select, input enable); reg [width-1: 0] mux_int; assign mux_out = enable ? mux_int : 'bz; always @(data_3, data_2, data_1, data_0, select) case (select) 0: mux_int = data_0; 1: mux_int = data_1; 2: mux_int = data_2; 3: mux_int = data_3; default: mux_int = 'bx; endcase endmodule 1 -43
Behavioral Models of Multiplexor module Mux_4_1_IF #(parameter width=32) (output [width-1: 0] mux_out, input [width-1: 0] data_3, data_2, data_1, data_0, input [1: 0] select, input enable); reg [width-1: 0] mux_int; assign mux_out = enable ? mux_int : 'bz; always @(data_3, data_2, data_1, data_0, select) if (select==0) mux_int = data_0; else if (select==1) mux_int = data_1; else if (select==2) mux_int = data_2; else if (select==3) mux_int = data_3; else mux_int = 'bx; endmodule 1 -44
Behavioral Models of Multiplexor module Mux_4_1_CA #(parameter width=32) (output [width-1: 0] mux_out, input [width-1: 0] data_3, data_2, data_1, data_0, input [1: 0] select, input enable); wire [width-1: 0] mux_int; assign mux_out = enable ? mux_int : 'bz; assign mux_int = (select==0) ? data_0: (select==1) ? data_1: (select==2) ? data_2: (select==3) ? data_3: 'bx; endmodule 1 -45
Behavioral Models of Encoder module encoder (output reg [2: 0] Code, input [7: 0] Data); always @(Data) if (Data==8'b 00000001) Code = 0; else if (Data==8'b 00000010) Code = 1; else if (Data==8'b 00000100) Code = 2; else if (Data==8'b 00001000) Code = 3; else if (Data==8'b 00010000) Code = 4; else if (Data==8'b 00100000) Code = 5; else if (Data==8'b 01000000) Code = 6; else if (Data==8'b 10000000) Code = 7; else Code = 'bx; endmodule 1 -46
Behavioral Models of Encoder module priority (output reg [2: 0] Code, output valid_data, input [7: 0] Data); assign valid_data = | Data; always @(Data) if (Data[7]) Code = 7; else if (Data[6]) Code = 6; else if (Data[5]) Code = 5; else if (Data[4]) Code = 4; else if (Data[3]) Code = 3; else if (Data[2]) Code = 2; else if (Data[1]) Code = 1; else if (Data[0]) Code = 0; else Code = 'bx; endmodule 1 -47
Behavioral Models of Encoder module priority 2 (output reg [2: 0] Code, output valid_data, input [7: 0] Data); assign valid_data = | Data; always @(Data) casex (Data) 8'b 1 xxxxxxx : Code = 7; 8'b 01 xxxxxx : Code = 6; 8'b 001 xxxxx : Code = 5; 8'b 0001 xxxx : Code = 4; 8'b 00001 xxx : Code = 3; 8'b 000001 xx : Code = 2; 8'b 0000001 x : Code = 1; 8'b 00000001 : Code = 0; default: Code = 'bx; endcase endmodule casex treats x values in the inputs as don’t care 1 -48
Behavioral Models of Decoder module decoder (output reg [7: 0] Data, input [2: 0] Code); always @(Code) if (Code == 0 ) Data= 8'b 00000001; else if (Code == 1 ) Data= 8'b 00000010; else if (Code == 2 ) Data= 8'b 00000100; else if (Code == 3 ) Data= 8'b 00001000; else if (Code == 4 ) Data= 8'b 00010000; else if (Code == 5 ) Data= 8'b 00100000; else if (Code == 6 ) Data= 8'b 01000000; else if (Code == 7 ) Data= 8'b 10000000; else Data = 'bx; endmodule 1 -49
Seven Segment Display Decoder module Seven_Segment_Display (output reg [6: 0] Display, input [3: 0] BCD); parameter BLANK = 7’b 111_1111; parameter ZERO= 7’b 000_0001; // abc_defg parameter ONE= 7’b 100_1111; parameter TWO= 7’b 001_0010; parameter THREE= 7’b 000_0110; parameter FOUR= 7’b 100_1100; parameter FIVE= 7’b 010_0100; parameter SIX= 7’b 010_0000; parameter SEVEN= 7’b 000_1111; parameter EIGHT= 7’b 000_0000; parameter NINE= 7’b 000_0100; always @(BCD) case (BCD) 0: Display = ZERO; 1: Display = ONE; 2: Display = TWO; 3: Display = THREE; 4: Display = FOUR; 5 : Display = FIVE; 6: Display = SIX; 7: Display = SEVEN; 8: Display = EIGHT; 9: Display = NINE; default: Display = BLANK; endcase endmodule 1 -50
FSM Modeling n Moore Sequence Detector: Detection sequence is 110 1 -51
FSM Modeling module moore_110_detector (output reg z, input x, clk, rst ); parameter reset = 2'b 00, got 1=2'b 01, got 11=2'b 10, got 110=2'b 11; reg [1: 0] state, next_state; always @(posedge clk) if (rst) state <= reset; else state <= next_state; always @(state, x) begin z = 0; case (state) reset: if (x) next_state=got 1; else next_state=reset; got 1: if (x) next_state=got 11; else next_state=reset; got 11: if (x) next_state=got 11; else next_state=got 110; got 110: begin z=1; if (x) next_state=got 1; else next_state=reset; endcase endmodule 1 -52
FSM Modeling: Test Benches n To test an FSM (i. e. sequential circuit), the test bench must supply an input sequence that exercise all state transitions and output combinations module moore_110_detector_TB ; //No ports for test benches reg clk, rst, x ; // Inputs declarations – have to reg type since we are going to assign // them inside a procedural block wire z ; // output declaration – Must be a wire since it is connected to the output of the // instantiated module moore_110_detector M 1 (z, x, clk, rst ) ; // instantiating the module under test initial begin // The reset and clock sequence rst = 1 ; clk = 0 ; #10 clk=1; #10 clk=0; rst = 0 ; forever #10 clk = ~ clk ; end initial begin //Input sequence – apply inputs with negative edge of clock @(negedge clk) x=0; @(negedge clk) x=1; @(negedge clk) x = 1; @(negedge clk) x = 0; endmodule 1 -53
Parallel Load Register module Par_load_reg 4 #(parameter word_size=4) ( output reg [word_size-1: 0] Data_out, input [word_size-1: 0] Data_in, input load, clock, reset); always @(posedge clock, posedge reset) if (reset) Data_out <= 0; else if (load) Data_out <= Data_in; endmodule 1 -54
Shift Register (No shift control) module Shift_reg 4 #(parameter word_size=4) (output Data_out, input Data_in, clock, reset); // Serial-in, serial-out reg [word_size-1: 0] Data_reg; assign Data_out = Data_reg[0]; always @(posedge clock, negedge reset) if (!reset) Data_reg <= 0; else Data_reg <= {Data_in, Data_reg [word_size-1: 1]}; endmodule 1 -55
Shift Register (with shift control) module Shift_reg 4 #(parameter word_size=4) //Serial-in, Parallel-out ( output reg [word_size-1: 0] Data_out, input Data_in, shift, clock, reset); always @(posedge clock, negedge reset) if (!reset) Data_out <= 0; else if (shift) Data_out <= {Data_in, Data_out[word_size -1: 1]} ; //Shifts to right endmodule 1 -56
Multi. Function Register module MFRegister #(parameter n=3) (output reg [n-1: 0] Q, input [n 1: 0] Din, input s 1, s 0, clk, reset, Si. L, , Si. R); always @(posedge clk) begin if (reset) Q <= 0; // synchronous reset else case ({s 1, s 0}) 2'b 00: Q <=Q ; // no change 2'b 01: Q <= Din ; // parallel load – Read Din into register 2'b 10: Q <= {Q [n-2: 0], Si. R} ; // shift left, Si. R (Sin from the right) is shifted in 2'b 11: Q <= {Si. L, Q [n-1: 1]} ; //shift right, Si. L (Sin from the left) is shifted in endcase endmodule 1 -57
Up-Down Counter module Up_Down_Counter 2 (output reg [2: 0] count, input load, count_up, counter_on, clock, reset, input [2: 0] Data_in); always @(negedge clock, posedge reset) // counter is –ve edge triggered if (reset) count <= 0 ; // If counter_on is 0, the counter does nothing else if (counter_on) if (load) count <= Data_in; else if (count_up) count<=count+1; else count<=count-1; endmodule 1 -58
Up-Down Counter: Testbench module TB_Counter ; reg [2: 0] Data_in ; reg load, count_up, counter_on, clock, reset ; wire [2: 0] Q ; //output Up_Down_Counter 2 (Q, load, count_up, counter_on, clock, reset, Data_in); initial begin // The reset and clock sequence reset = 1 ; clk = 0 ; #10 ; reset = 0 ; forever #10 clk = ~ clk ; end initial begin counter_on=0; load=0; count_up=0 ; Data_in=5; // initialize inputs @(posedge clk) counter_on =1 ; load=1; // load with 5 @(posedge clk) count_up=1 ; load=0; // count up to 6 @(posedge clk) ; // count up to 7 @(posedge clk) count_up=0 ; // count down to 6 @(posedge clk) ; // count down to 5 endmodule 1 -59
Modulo-n Counter module Modulo_n_Counter # (parameter M=3, N=7) (output reg [M-1: 0] count, input count_en, clock, reset); always @(posedge clock, posedge reset) if (reset) count <= 0 ; // If count_en is 0, the counter does nothing else if (count_en) if (count == N-1) count <= 0 ; else count<=count+1; endmodule 1 -60
- Slides: 60