COE 202 Introduction to Verilog

Slides:



Advertisements
Similar presentations
Verilog Overview. University of Jordan Computer Engineering Department CPE 439: Computer Design Lab.
Advertisements

Table 7.1 Verilog Operators.
Verilog Intro: Part 1.
Combinational Logic with Verilog Materials taken from: Digital Design and Computer Architecture by David and Sarah Harris & The Essentials of Computer.
SYEN 3330 Digital SystemsJung H. Kim Chapter SYEN 3330 Digital Systems Chapters 4 – Part3: Verilog – Part 1.
CSE 341 Verilog HDL An Introduction. Hardware Specification Languages Verilog  Similar syntax to C  Commonly used in  Industry (USA & Japan) VHDL 
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
 HDLs – Verilog and Very High Speed Integrated Circuit (VHSIC) HDL  „ Widely used in logic design  „ Describe hardware  „ Document logic functions.
Verilog - 1 Writing Hardware Programs in Abstract Verilog  Abstract Verilog is a language with special semantics  Allows fine-grained parallelism to.
ELEN 468 Lecture 151 ELEN 468 Advanced Logic Design Lecture 15 Synthesis of Language Construct I.
Overview Logistics Last lecture Today HW5 due today
Spring 2007W. Rhett Davis with minor modification by Dean Brock UNCA ECE 406Slide 1 ECE 406 – Design of Complex Digital Systems Lecture 7: Design Example,
Verilog Basics Nattha Jindapetch November Agenda Logic design review Verilog HDL basics LABs.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Outline Analysis of Combinational Circuits Signed Number Arithmetic
ECE 2372 Modern Digital System Design
COE 405 Introduction to Logic Design with Verilog
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Module 1.2 Introduction to Verilog
COE 202 Introduction to Verilog Computer Engineering Department College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals.
Anurag Dwivedi. Basic Block - Gates Gates -> Flip Flops.
1 Verilog Digital System Design Z. Navabi, 2006 Verilog Language Concepts.
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
Introduction to ASIC flow and Verilog HDL
ECOM 4311—Digital System Design with VHDL
Introduction to Verilog. Data Types A wire specifies a combinational signal. – Think of it as an actual wire. A reg (register) holds a value. – A reg.
Introduction to Verilog
COE 202 Introduction to Verilog Computer Engineering Department College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals.
Chapter 3: Dataflow Modeling Digital System Designs and Practices Using Verilog HDL and 2008~2010, John Wiley 3-1 Chapter 3: Dataflow Modeling.
Verilog Intro: Part 1. Hardware Description Languages A Hardware Description Language (HDL) is a language used to describe a digital system, for example,
Introduction to Verilog. Structure of a Verilog Program A Verilog program is structured as a set of modules, which may represent anything from a collection.
1 University of Jordan Computer Engineering Department CPE 439: Computer Design Lab.
COE 202 Introduction to Verilog Computer Engineering Department College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals.
Exp#5 & 6 Introduction to Verilog COE203 Digital Logic Laboratory Dr. Ahmad Almulhem KFUPM Spring 2009.
Overview Logistics Last lecture Today HW5 due today
Hardware Description Languages: Verilog
Verilog Tutorial Fall
TODAY’S OUTLINE Verilog Codings Concurrent and Sequential If-else
ELEN 468 Advanced Logic Design
Lecture 3: Combinational Logic in SystemVerilog
Introduction to Verilog
Discussion 2: More to discuss
Verilog Introduction Fall
‘if-else’ & ‘case’ Statements
Behavioral Modeling Structural modeling Behavioral modeling
Verilog-HDL-3 by Dr. Amin Danial Asham.
Hardware Description Languages: Verilog
Introduction to DIGITAL CIRCUITS MODELING & VERIFICATION using VERILOG [Part-I]
Chapter 4 Combinational Logic
Introduction to Verilog
Behavioral Modeling in Verilog
Chapter 3: Dataflow Modeling
Combinatorial Logic Design Practices
Logic Values 0:logic 0 / false 1:logic 1 / true X:unknown logic value
332:437 Lecture 10 Verilog Language Details
OPERATORS and CONCURRENT STATEMENTS
Introduction to Verilog
332:437 Lecture 10 Verilog Language Details
Lecture 2: Continuation of SystemVerilog
332:437 Lecture 10 Verilog Language Details
Introduction to Verilog
Introduction to Verilog
Introduction to Verilog
Supplement on Verilog combinational circuit examples
EEE2243 Digital System Design Chapter 1: Verilog HDL (Combinational) by Muhazam Mustapha, February 2012.
Verilog HDL Basic Syntax
COE 202 Introduction to Verilog
Combinational Circuit Design
Reconfigurable Computing (EN2911X, Fall07)
Presentation transcript:

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

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

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

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.

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

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

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)

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:

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

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];

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] ;

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

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

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

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]

Always Block always blocks are procedural blocks that contain sequential statements. Syntax always @(sensitivity 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 always @(a , b , c) always @* //when any signal level changes Edge type always @(posedge clock) always @(negedge clock)

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.

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.

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); always @(a,b,s) 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

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); always @* //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

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

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

Comparator module comp2 #(parameter width=2) (input [width-1:0] A, B, output reg A_gt_B, A_lt_B, A_eq_B); always @* 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

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 , B , Sel) begin // or we could say always @* 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

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

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); always@* if (select) a=b; else a=c; Method 3 module mux2x1 (input b, c, select, output reg a); always@* case (select) 1’b1: a=b; 1’b0: a=c; endcase endmodule

Encoder module encoder (output reg [2:0] Code, input [7:0] Data); always @* //we could have also said always @ (Data) if (Data==8'b00000001) Code = 0; else if (Data==8'b00000010) Code = 1; else if (Data==8'b00000100) Code = 2; else if (Data==8'b00001000) Code = 3; else if (Data==8'b00010000) Code = 4; else if (Data==8'b00100000) Code = 5; else if (Data==8'b01000000) Code = 6; else if (Data==8'b10000000) Code = 7; else Code = 0; endmodule

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

Priority Encoder module priority_encoder2 (output reg [2:0] Code, output valid_data, input [7:0] Data); assign valid_data = | Data; always @(Data) //we could have also said always @ * 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'b0000001? : Code = 1;8'b00000001 : Code = 0; default: Code = 0; endcase endmodule

Decoder module decoder (output reg [7:0] Data, input [2:0] Code); always @* //we could have also said always @ (code) if (Code == 0 ) Data= 8'b00000001; else if (Code == 1 ) Data= 8'b00000010; else if (Code == 2 ) Data= 8'b00000100; else if (Code == 3 ) Data= 8'b00001000; else if (Code == 4 ) Data= 8'b00010000; else if (Code == 5 ) Data= 8'b00100000; else if (Code == 6 ) Data= 8'b01000000; else if (Code == 7 ) Data= 8'b10000000; else Data = 0; endmodule

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; // … always @* 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