Presentation is loading. Please wait.

Presentation is loading. Please wait.

COE 202 Introduction to Verilog

Similar presentations


Presentation on theme: "COE 202 Introduction to Verilog"— Presentation transcript:

1 COE 202 Introduction to Verilog
Computer Engineering Department College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals

2 Outline Behavioral Modeling Verilog Operators
Behavioral Description of an Adder Always block Procedural Assignment If Statements Case Statements Comparator, Arithmetic & Logic Unit Multiplexor, Encoder, Priority Encoder. Decoder

3 Behavioral Modeling Behavioral modeling describes the functionality of a design What the design will do Not how the design will be built in hardware Behavioral models specify the input-output model of a logic circuit and suppress details about its low level internal structure. 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

4 Data Types for Behavioral Modeling
All variables in Verilog have a predefined type. There are two families of data types: nets and registers. Net variables act like wires in physical circuit and establish connectivity between design objects. Register variables act like variables in ordinary procedural languages – they store information while the program executes. Register types include: reg, integer, real, realtime, time. A wire and a reg have a default size of 1 bit. Size of integer is the size of word length in a computer, at least 32.

5 Integer Numbers in Verilog
constant numbers can be specified in decimal, hexadecimal, octal, or binary format integer numbers can be specified as: Syntax: <size>'<radix><value> (size is in number of bits) Sized or unsized numbers ( Unsized size is 32 bits ) In a radix of binary, octal, decimal, or hexadecimal Radix and hex digits (a,b,c,d,e,f) are case insensitive Spaces are allowed between the size, radix and value The character (_) is legal anywhere in a number except as the first character (e.g. 12'b1011_1100_0010, 'hA8, 8'd15) When <size> is smaller than <value>, then left-most bits of <value> are truncated

6 Verilog Operators { } concatenation + - * / ** arithmetic % modulus
> >= < <= relational ! logical NOT && logical AND || logical OR == logical equality != logical inequality === case equality !== case inequality ? : conditional ~ bit-wise NOT & bit-wise AND | bit-wise OR ^ bit-wise XOR ^~ ~^ bit-wise XNOR & reduction AND | reduction OR ~& reduction NAND ~| reduction NOR ^ reduction XOR ~^ ^~ reduction XNOR << shift left >> shift right

7 Verilog Operators Arithmetic Operators:
Each operator takes two operands. + and – could also take a 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 (0s 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)

8 Verilog Operators Examples of shift operations:
If both operands of a shift operator are signals, as in a << b, 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:

9 Verilog Operators Relational and equality operators:
Compare two operands and return a 1-bit logical (Boolean) value: either 0 or 1 4 relational operators: >, <, <=, and >= Equality operators: ==, ! =, The relational operators and the == and ! = operators infer comparators during synthesis

10 Verilog Operators Bitwise operators: The statement: assign c = a I b ;
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];

11 Verilog Operators Reduction operators: &, I , and ^ operators may have only one operand and then 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] ;

12 Verilog Operators Logical operators: && (logical and), II (logical or), and ! (logical negate) operands of a logical operator are interpreted as false (when 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

13 Verilog Operators Conditional operator: ? : takes three operands and its general format is [signal] = [boolean-exp] ? [true-exp] : [false-exp]; The [boolean-exp] is a Boolean expression that returns true (1) or false (0). Ex.: assign max = (a>b) ? a : b; //max will get the maximum of the signals a and b The operator can be thought of as a simplified if-then-else statement. Can be cascaded or nested

14 Verilog Operators Concatenation operator: { }
{ } combines segments of elements and small arrays to form a large array: wire [7:0] a, rot, srl , sra; assign rot = {a[2:0], a[7:3]} ; // Rotate a to right 3 bits assign srl = {3'b000, a[7:3]} ; // shift a to right 3 bits and insert 0s (logical shift) assign sra = {a[7] , a[7] , a[7] , a[7:3]} ; // arithmetic shift a to right 3 bits

15 Behavioral Description of an Adder
module adder #(parameter width = 4) (output cout, [width-1:0] sum, input cin, [width-1:0] a, b); assign {cout, sum} = a + b + cin; // note: Verilog treats wires as ‘unsigned’ numbers endmodule 4-bit operands, 5-bit result { Cout, S } is a 5 bit bus: Cout S[3] S[2] S[1] S[0]

16 Always Block always blocks are procedural blocks that contain sequential statements. Syntax list) begin end sensitivity list prevents the always block from executing again until another change occurs on a signal in the sensitivity list. Level type , b , c) //when any signal level changes Edge type clock) clock)

17 Procedural Assignment
Assignments inside an always block are called procedural assignments Can only be used within an always block Two types : blocking assignment and nonblocking assignment. Basic syntax : [variable-name] = [expression] ; // blocking assignment [variable-name] <= [expression] ; // nonblocking assignment 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.

18 Procedural Assignment
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). The basic rule of thumb is: Use blocking assignments for a combinational circuit. Use nonblocking assignments for a sequential circuit There are two types of variables in Verilog: wire (all outputs of assign statements must be wire) reg (all outputs modified in always blocks must be reg) if-else and case statement are only in always block.

19 If Statements if (expression) begin ...procedural statements... end else if (expression) ...statements more else if blocks else module ALU #(parameter n=8) (output reg [n-1:0] c, input [1:0] s, input [n-1:0] a, b); begin if (s==2'b00) c = a + b; else if (s==2'b01) c = a - b; else if (s==2'b10) c = a & b; else c = a | b; end //since this is a single statement block we could // have omitted the begin end endmodule

20 Case Statements case (expression) case_choice1: begin ...statements...
end case_choice2: ...more case choices blocks... default: endcase module ALU2 #(parameter n=8) (output reg [n-1:0] c, input [1:0] s, input [n-1:0] a, b); //we used the * instead of listing (a,b,S) case (s) //no need for begin end 2'b00: c = a + b; : c = a - b; // we used decimal 1 instead of 2’b01 – // works the same 2'b10: c = a & b; default: c = a | b; //we could have used 2’b11 or simply 3 endcase endmodule

21 Full Adder module fadd2 (output reg S, Cout, input A, B, Cin); , B , Cin) //better to just use begin S = (A ^ B ^ Cin); Cout = (A & B) | (A & Cin) | (B & Cin); end endmodule

22 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

23 Comparator module comp2 #(parameter width=2) (input [width-1:0] A, B, output reg A_gt_B, A_lt_B, A_eq_B); begin A_gt_B = 0; //default values 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; end endmodule

24 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); , B , Sel) begin // or we could say 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; end endcase end endmodule

25 Logic Unit module logic #(parameter width=4) (input [width-1:0] A, B, input [2:0] Sel, output reg [width-1:0] Y); //we could have also said (Sel,A,B) 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

26 2x1 Multiplexer Method 1 module mux2x1 (input b, c, select, output a);
assign a = (select ? b : c); endmodule Method 2 module mux2x1 (input b, c, select, output reg a); if (select) a=b; else a=c; Method 3 module mux2x1 (input b, c, select, output reg a); case (select) 1’b1: a=b; 1’b0: a=c; endcase endmodule

27 Encoder module encoder (output reg [2:0] Code, input [7:0] Data); //we could have also said (Data) if (Data==8'b ) Code = 0; else if (Data==8'b ) Code = 1; else if (Data==8'b ) Code = 2; else if (Data==8'b ) Code = 3; else if (Data==8'b ) Code = 4; else if (Data==8'b ) Code = 5; else if (Data==8'b ) Code = 6; else if (Data==8'b ) Code = 7; else Code = 0; endmodule

28 Priority Encoder module priority_encoder (output reg [2:0] Code, output valid_data, input [7:0] Data); assign valid_data = | Data; //we could have also said (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 = 0; endmodule

29 Priority Encoder module priority_encoder2 (output reg [2:0] Code, output valid_data, input [7:0] Data); assign valid_data = | Data; //we could have also said * casex (Data) //casex is similar to case except that it treats ? And x as don’t cares 8'b1??????? : Code = 7; 8'b01??? ??? : Code = 6; 8'b001????? : Code = 5; 8'b0001???? : Code = 4; 8'b00001??? : Code = 3; 8'b000001?? : Code = 2; 8'b ? : Code = 1;8'b : Code = 0; default: Code = 0; endcase endmodule

30 Decoder module decoder (output reg [7:0] Data, input [2:0] Code); //we could have also said (code) if (Code == 0 ) Data= 8'b ; else if (Code == 1 ) Data= 8'b ; else if (Code == 2 ) Data= 8'b ; else if (Code == 3 ) Data= 8'b ; else if (Code == 4 ) Data= 8'b ; else if (Code == 5 ) Data= 8'b ; else if (Code == 6 ) Data= 8'b ; else if (Code == 7 ) Data= 8'b ; else Data = 0; endmodule

31 Seven Segment Display Decoder
module Seven_Segment_Display (output reg [6:0] Display, input [3:0] BCD); // localparam are constant definitions; we assign the required values of the 7-outputs that control the 7 // segnments abc_defg – a 0 means the segment is ON, a 1 means it is OFF localparam BLANK = 7’b111_1111; //all are OFF localparam ZERO= 7’b000_0001; // all ON, except g is OFF localparam ONE= 7’b100_1111; // b and c are ON, rest are OFF localparam TWO= 7’b001_0010; // c and f are OFF, rest are ON localparam THREE= 7’b000_0110; // e and f are OFF, rest are ON localparam FOUR= 7’b100_1100; // b, c, f and g are ON, rest are OFF localparam FIVE= 7’b010_0100; // … localparam SIX= 7’b010_0000; // … localparam SEVEN= 7’b000_1111; // … localparam EIGHT= 7’b000_0000; // … localparam NINE= 7’b000_0100; // … 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


Download ppt "COE 202 Introduction to Verilog"

Similar presentations


Ads by Google