Digital System Engineering

32
Digital System Engineering บบบบบ 4. Hardware Description Language

description

Digital System Engineering. บทที่ 4. Hardware Description Language. 4.1 Introduction. การออกแบบวงจร Combinational หรือ Sequential โดยการเขียน Truth Table และลดรูป สมการบูลีน หรือใช้ Karnaugh Map เป็นวิธีการที่เสียเวลาและเกิดความผิดพลาดได้ง่าย - PowerPoint PPT Presentation

Transcript of Digital System Engineering

Page 1: Digital System Engineering

Digital System Engineering

บทท 4.Hardware Description Language

Page 2: Digital System Engineering

4.1 Introduction• การออกแบบวงจร Combinational หรอ

Sequential โดยการเขยน Truth Table และลดรปสม การบลน หรอใช Karnaugh Map เปนวธการทเสยเวลา

และเกดความผดพลาดไดงาย• ตอมาไดมการพฒนาโปรแกรมประเภท Computer-Aid

Design (CAD) เพอชวยใหการออกแบบวงจรมประสทธภาพมากยงขน

• โดยการเขยนฟงกชนการทำางานของวงจรโดยใชHardware Description Language (HDL) จาก

นนจงสงให CAD ทำาการออกแบบวงจรทเหมาะสมทสดให

• ภาษา HDL ทนยมใชกนมากม 2 ภาษาคอ Verilog และVHDL

• Verilog และ VHDL ถกออกแบบมาจากหลกการ เดยวกนแตมโครงสรางภาษาทแตกตางกน ในบทนจะ

แสดงโปรแกรมของทง 2 ภาษาเพอใชเปรยบเทยบกน

Page 3: Digital System Engineering

4.1.1 Modules• Module หมายถงบลอกของ Hardware ทม Input

และ Output• ตวอยางเชน AND gate, Multiplexer และวงจรดจตอลของบทกอนหนานเปนตน

• เราสามารถเขยนฟงกชนของ Module ได 2 วธคอ1. Behavioral Model เปนการอธบายวา Module

ทำางานอยางไร2. Structural Model เปนการอธบายวาจะสราง

Module อยางไร

Page 4: Digital System Engineering

4.1.1 Modules• ตวอยางท 4.1 แสดง Behavioral Description ของy=/a/b/c + a/b/c + a/bc

• Verilog Module เรมจาก• ชอ Module• รายชอ input และ output• จากนนจงเปน assign ของสมการบลน

ของ Output• เครองหมาย ~ คอ NOT, & คอ AND และ | คอ OR

• สญญาณ Input และ Output เปนตวแปรแบบบลนคอ มคา 0 หรอ 1 แตเราสามารถกำาหนดใหมคาเปน Float หรอ Undefined Value ไดซงจะพดถงในหวขอท

4.2.8

Verilogmodule sillyfunction (input a, b, c, output y);

assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c;

endmodule

Page 5: Digital System Engineering

4.1.1 Modules• โปรแกรม VHDL ประกอบดวย 3 สวน

1. Library Clause สวนนจะอธบายในหวขอท 4.2.11

2. Entity Declaration แสดงชอของ Module รายชอของ

Input และ Output3. Architecture Body

กำาหนดการทำางานของ Module• เราจะตองกำาหนดชนดของสญญาณ input และ

output เปน STD_LOGIC

VHDLlibrary IEEE; use IEEE.STD_LOGIC_1164.all;entity sillyfunction is

port (a, b, c: in STD_LOGIC; y: out STD_LOGIC);

end;architecture synth of sillyfunction isbegin

y <= ((not a) and (not b) and (not c)) or (a and (not b) and (not c)) or (a and (not b) and c);

end;

Page 6: Digital System Engineering

4.1.1 Modules• ตวแปรชนด STD_LOGIC สามารถ

มคาเปน 0 หรอ 1• และสามารถมคาเปน Float หรอ

Undefined Value ซงจะพดถงใน หวขอท 4.2.8

• ชนดตวแปรแบบ STD_LOGIC ถก กำาหนดไวใน Library

IEEE.STD_LOGIC_1164 ทำาใหเราตองประกาศใช Library นใน Library

Clause

VHDLlibrary IEEE; use IEEE.STD_LOGIC_1164.all;entity sillyfunction is

port (a, b, c: in STD_LOGIC; y: out STD_LOGIC);

end;architecture synth of sillyfunction isbegin

y <= ((not a) and (not b) and (not c)) or (a and (not b) and (not c)) or (a and (not b) and c);

end;

Page 7: Digital System Engineering

4.1.1 Modules• ภาษา VHDL ไมมการกำาหนดลำาดบการ

ทำางานของ Operator ตางๆ เชนAND, NOT หรอ OR

• ทำาใหโปรแกรมไมสามารถทราบวาควรจะ ทำางานของ Operator ใดกอนหรอหลง

• ดงนนการเขยนสมการบลนในภาษาVHDL จะตองมการใสเครองหมาย

วงเลบทกครง ดงแสดงในโปรแกรมตวอยาง

VHDLlibrary IEEE; use IEEE.STD_LOGIC_1164.all;entity sillyfunction is

port (a, b, c: in STD_LOGIC; y: out STD_LOGIC);

end;architecture synth of sillyfunction isbegin

y <= ((not a) and (not b) and (not c)) or (a and (not b) and (not c)) or (a and (not b) and c);

end;

Page 8: Digital System Engineering

4.1.1 Modules• Module ของ Verilog และ VHDL เปนตวอยางทด

ของหลกการ Modularity• Module ประกอบดวย Input และ Output และทำางานตามฟงกชนทกำาหนดไว

• ตราบใดท Module ยงคงทำางานถกตอง กไมจำาเปนตอง รวาโปรแกรมของ Module ถกเขยนอยางไร

• เราสามารถเขยนโปรแกรมภาษา Verilog และ VHDL เพอสราง Hardware ใดกได ดงนนการเลอกใชภาษาใด

ขนกบความตองการของลกคา หรอขนวาโปรแกรมเดมถกเขยนดวยภาษาใด

Page 9: Digital System Engineering

4.1.3 Simulation และSynthesis• Simulation คอการปอน Input ใหกบ Module และ

ตรวจสอบ Output ของ Module เพอตรวจสอบความผดพลาด

• จากรปแสดงการทำา Simulation ของ sillyfunction• จะเหนวา y มคา Output เปน True เมอ a, b, และ c

มคาเปน 000, 100 หรอ 101 ซงทำางานถกตองตามทไดกำาหนดไวในสมการบลน

Page 10: Digital System Engineering

4.1.3 Simulation และSynthesis• Synthesis คอการเปลยน HDL เปน Netlistหรอการสรางโลจกเกทและสายไฟทใชเชอมตอโลจกเกทแตละตว

• การ Synthesizer อาจจะมขนตอนการทำาOptimize หรอการลดขนาดของวงจรใหเลกทสดเทาท

จะทำาได• Netlist อาจอยในรปของ Text หรอ Schematic เพอใหดงายขน

• คำาสงสวนใหญใน Verilog และ VHDL สามารถแปลง เปน Hardware ได แตกมบางคำาสงทไมสามารถแปลง เปน Hardware เชนกน

Page 11: Digital System Engineering

4.1.3 Simulation และSynthesis• โปรแกรม HDL สามารถแบงออกเปน 2 กลมคอ

1. Synthesizable Module คอสวนทใชกำาหนดHardware ของ Module

2. Testbench คอสวนทเพมเขาไปเพอใชตรวจสอบการ ทำางานของ Module Testbench ถกใชในการทำา

Simulation เทานน แตจะไมถก Synthesize ออกมาเปนวงจรได

Page 12: Digital System Engineering

4.1.3 Simulation และSynthesis• ขอผดพลาดสวนใหญของผเรมตนเขยน HDL ใหมๆ คอ

มกจะคดวาการเขยน HDL เหมอนกบการเขยนโปรแกรมคอมพวเตอร

• แตทจรงแลวการเขยน HDL เปรยบเสมอนกบการออกแบบวงจรดจตอล

• ดงนนถาผเขยน HDL ไมเขาใจการทำางานของวงจร ดจตอลทออกแบบ จะทำาให Module ทเขยนออกมา

• มการทำางานทผดพลาด• ทำาใหตองใช Hardware มากเกนจำาเปน• ได Module ทสามารถ Simulate ไดแตไมสามารถ

Synthesize ได• กอนเขยน HDL ควรเขยนบลอกของวงจร

Combination, Register และ Finite State Machine และเขยนวาแตละบลอกเชอมตอกนอยางไรในกระดาษเสยกอน

Page 13: Digital System Engineering

4.1.3 Simulation และSynthesis• วธทดทสดในการเรยน HDL คอการเรยนจากตวอยาง• วธทใชในการเขยนวงจรโลจกตางๆเรยกวา Idiom• ในบทนจะเรมจากการศกษาการเขยน Idiom ของแตละบลอกทถกตอง

• จากนนจงศกษาวาจะนำาเอาแตละบลอกมาเชอมตอกนอยางไร

Page 14: Digital System Engineering

4.2 Combinational Logic :4.2.1 Bitwise Operators• Bitwise Operator เปนการออกแบบวงจรท Input 1

bit หรอหลายบท โดยทแตละบทจะทำางานแยกจากกน• ตวอยางท 4.2 วงจร Inverter สำาหรบ

Bus ขนาด 4 บท• a[3:0] หมายถงบสขนาด 4 บท โดยเรยง

จากบททสำาคญมากทสด (Most Significant Bit) ไป ยงบททมความสำาคญนอยทสด (Least Significant

Bit) คอ a[3], a[2], a[1] และ a[0] ตามลำาดบ• เราเรยกการเรยงลกษณะนวา Little Endian• แตถาเรยง a[0], a[1], a[2], และ a[3] จะเรยกวา

Big Endian และเขยนเปน a[0:3]

Verilogmodule inv (input [3:0] a,

output [3:0] y);assign y = ~a;

endmodule

Page 15: Digital System Engineering

4.2 Combinational Logic :4.2.1 Bitwise Operators• สญญาณบสของ VHDL เปนตวแปร

ชนด STD_LOGIC_VECTOR• STD_LOGIC_VECTOR

(3 downto 0) หมายถงบสขนาด 4 บทแบบ Little Endian

• แตถาเขยนเปน (0 to 3) หมายถงBig endian

VHDLlibrary IEEE; use IEEE.STD_LOGIC_1164.all;entity inv is

port (a: in STD_LOGIC_VECTOR (3 downto 0);

y: out STD_LOGIC_VECTOR (3 downto 0));end;architecture synth of inv isbeginy <= not a;end;

Page 16: Digital System Engineering

4.2 Combinational Logic :4.2.1 Bitwise Operators

• เราสามารถเลอก Endian เปนแบบใดกได แตถาเลอกใชแบบใดแลวกตองใชแบบนนไปตลอด

Page 17: Digital System Engineering

4.2 Combinational Logic :4.2.1 Bitwise Operators• ตวอยางท 4.3 วงจรตวอยางการใช Gate• ~, ^ และ | เปนตวอยาง Operator

ของ Verilog โดยท a, b, และ y1 เรยกวา Operand

• การผสมกนระหวาง Operator และOperand เชน ~(a|b) เรยกวาexpression

• คำาสงในแตละบรรทดเชนassign y4 = ~(a|b) เรยกวา Statement

Verilogmodule gates (input [3:0] a, b,

output [3:0] y1, y2,y3, y4, y5);/* Five different two-input logicgates acting on 4 bit busses */assign y1 = a & b; // ANDassign y2 = a | b; // ORassign y3 = a b; // XORassign y4 = ~(a & b); // NANDassign y5 = ~(a | b); // NORendmodule

Page 18: Digital System Engineering

4.2 Combinational Logic :4.2.1 Bitwise Operators• assign out = in1 op in2; เรยกวา Continuous

Assignment Statement• ทกครงท input ดานขวามอของเครองหมาย = เปลยน

คาของ Output ดานขวามอจะเปลยนดวย• ดงนน Continuous Assignment Statement

เปนตวกำาหนการทำางานของวงจร Combination

Page 19: Digital System Engineering

4.2 Combinational Logic :4.2.1 Bitwise Operators• Operator ของ VHDL คอ not,

xor และ or• การผสมกนระหวาง Operator และ

Operand เชน a and b เรยกวาexpression

• คำาสงในแตละบรรทดเชนy4 <= a nand b เรยกวาStatement

• Out <= in1 op in2; เรยกวาConcurrent SignalAssignment

VHDLlibrary IEEE; use IEEE.STD_LOGIC_1164.all;entity gates is

port (a, b: in STD_LOGIC_VECTOR (3 downto 0);

y1, y2, y3, y4,y5: out STD_LOGIC_VECTOR (3 downto 0));end;architecture synth of gates isbegin

— — Five different two-input logic gates— —acting on 4 bit bussesy1 <= a and b;y2 <= a or b;y3 <= a xor b;y4 <= a nand b;y5 <= a nor b;

end;

Page 20: Digital System Engineering

4.2 Combinational Logic :4.2.1 Bitwise Operators• ทกครงทตวแปรดานขวามอของเครองหมาย <=

เปลยนคา ตวแปรดานซายมอจะเปลยนดวย• ดงนนเราสามารถเขยนวงจร Combination ในรปของ

Concurrent Signal Assignment

Page 21: Digital System Engineering

4.2 Combinational Logic :4.2.3 Reduction Operators• Reduction Operator หมายถงการใชเกทแบบหลาย

Input เพอประมวลผลขอมลในบส• ตวอยางท 4.4 แสดงตวอยางการใช and

เกท 8 input• เราสามารถใชวธเดยวกนในการเขยน

ในการเขยน OR (|) , XOR (^),NAND (~&) และ NOR (~|)

• XOR จะใหคา TRUE ถาจำานวน Input ทเปนโลจก 1 เปนเลขค และเปน FALSE ถาจำานวนเปนเลขค

Verilogmodule and8 (input [7:0] a, output y);

assign y = &a;// &a is much easier to write than// assign y = a[7] & a[6] & a[5] & a[4] &// a[3] & a[2] & a[1] & a[0];

endmodule

Page 22: Digital System Engineering

4.2 Combinational Logic :4.2.3 Reduction Operators• ภาษา VHDL ไมม Reduction

Operator ดงนนจงตองเขยนConcurrent SignalAssignment แบบตรงๆ

VHDLlibrary IEEE; use IEEE.STD_LOGIC_1164.all;entity and8 isport (a: in STD_LOGIC_VECTOR (7 downto 0);y: out STD_LOGIC);end;architecture synth of and8 isbegin

y <= a(7) and a(6) and a(5) and a(4) and a(3) and a(2) and a(1) and a(0);

end;

Page 23: Digital System Engineering

4.2 Combinational Logic :4.2.4 Conditional Assignment• Conditional Assignment คอการเลอกๆสญญาณ

Input ทจะออกไปยง Output• ตวอยางท 4.5 แสดงการสราง

Multiplexer 2:1 โดยใชConditional Assignment

• Conditional Operator (? : ) จะเลอกสญญาณ d1 หรอ d0 ขนกบคาของ s

• s=1 ทำาให y=d1• s=0 ทำาให y=d0

• ? : นยมใชในการสราง Multiplexer เพอใชเลอก สญญาณ Input ทจะออกไปท Output

Verilogmodule mux2 (input [3:0] d0, d1, input s,output [3:0] y);

assign y = s ? d1 : d0;endmodule

Page 24: Digital System Engineering

4.2 Combinational Logic :4.2.4 Conditional Assignment• Conditional Assignment คอการเลอกๆสญญาณ

Input ทจะออกไปยง Output• ? : เปน Operator ชนด Ternary• Operator เนองจากมนม 3 Input

Verilogmodule mux2 (input [3:0] d0, d1, input s,output [3:0] y);

assign y = s ? d1 : d0;endmodule

Page 25: Digital System Engineering

4.2 Combinational Logic :4.2.4 Conditional Assignment

VHDLlibrary IEEE; use IEEE.STD_LOGIC_1164.all;entity mux2 isport (d0, d1: in STD_LOGIC_VECTOR (3 downto 0);s: in STD_LOGIC;y: out STD_LOGIC_VECTOR (3 downto 0));end;architecture synth of mux2 isbegin

y <= d0 when s = ‘0’ else d1;end;

Page 26: Digital System Engineering

4.2 Combinational Logic :4.2.4 Conditional AssignmentVerilogmodule mux4 (input [3:0] d0, d1, d2, d3,input [1:0] s,output [3:0] y);

assign y = s[1] ? (s[0] ? d3 : d2): (s[0] ? d1 : d0);

endmodule

VHDLlibrary IEEE; use IEEE.STD_LOGIC_1164.all;entity mux4 isport (d0, d1,d2, d3: in STD_LOGIC_VECTOR (3 downto 0);s: in STD_LOGIC_VECTOR (1 downto 0);y: out STD_LOGIC_VECTOR (3 downto 0));end;architecture synthl of mux4 isbegin

y <= d0 when s = “00” elsed1 when s = “01” elsed2 when s = “10” else d3;

end;

Page 27: Digital System Engineering

4.2 Combinational Logic :4.2.4 Conditional AssignmentVerilogmodule mux4 (input [3:0] d0, d1, d2, d3,input [1:0] s,output [3:0] y);

assign y = s[1] ? (s[0] ? d3 : d2): (s[0] ? d1 : d0);

endmodule

VHDLlibrary IEEE; use IEEE.STD_LOGIC_1164.all;entity mux4 isport (d0, d1,d2, d3: in STD_LOGIC_VECTOR (3 downto 0);s: in STD_LOGIC_VECTOR (1 downto 0);y: out STD_LOGIC_VECTOR (3 downto 0));end;architecture synth2 of mux4 isbeginwith a select y <=

d0 when “00”,d1 when “01”,d2 when “10”,d3 when others;

end;

Page 28: Digital System Engineering

4.2 Combinational Logic :4.2.4 Conditional Assignment

Page 29: Digital System Engineering

4.2.5 Internal Variables• ในกรณทวงจร Combination มความซบซอนมาก เรา

นยมแบงการทำางานของวงจรออกเปนหลายๆขนตอน หรอแบงสมการบลนออกเปนหลายๆสมการ โดยใช

ตวแปรภายใน (Internal Variable) ในการเกบผลลพธการทำางานในแตละขน

• ตวอยางวงจร Full AdderS=A xor B xor Cin

Cout = AB + ACin + BCin

• เราสามารถเขยนใหมเปนP=A xor B, G = ABS=P xor Cin, Cout=G+PCin

Page 30: Digital System Engineering

4.2.5 Internal Variables• เราเรยก P และ G วาตวแปรภายใน• เราสามารถตงตวแปรภายในของ Verilog

โดยใชคำาสง wire

Verilogmodule fulladder(input a, b, cin,output s, cout);wire p, g;

assign p = a b;assign g = a & b;assign s = p cin;assign cout = g | (p & cin);

endmodule

Page 31: Digital System Engineering

4.2.5 Internal Variables• เราใชคำาสง signal ในการตงตวแปร

ภายในของ VHDLVHDLlibrary IEEE; use IEEE.STD_LOGIC_1164.all;entity fulladder isport(a, b, cin: in STD_LOGIC;s, cout: out STD_LOGIC);end;architecture synth of fulladder issignal p, g: STD_LOGIC;begin

p <= a xor b;g <= a and b;s <= p xor cin;cout <= g or (p and cin);

end;

Page 32: Digital System Engineering

4.2.5 Internal Variables• ขอสงเกต ในการเขยนโปรแกรมทวไปเชนภาษา C++

หรอ Java เราจะตองเขยน P=A xor B กอน จากนนจง เขยน S=P xor Cin

• แตลำาดบการเขยนคำาสงใน HDL ไมมความสำาคญ เนองจากคำาสงในแตละ Assignment จะถกคำานวณ

ใหมทกครงทตวแปรทางดานขวามอมการเปลยนแปลงคา