You are on page 1of 269

THIẾT KẾ

HỆ THỐNG NHÚNG

TS. Vu-Duc Ngo


© DHBK 2010

About your lecturer


• Bộ môn điện tử và kỹ thuật máy tính, ĐHBK Hà nội
 Office: C9-401
 Email: duc.ngovu@hust.edu.vn
• Research:
 FPGA, embedded systems
 Wireless Communications, Multimedia Codec
 NoC
• Education:
 K37 điện tử-ĐHBK Hà nội (1997)
 Ph.D @KAIST

2
© DHBK 2010

Mục đích của môn học


• Nắm được quy trình thiết kế hệ thống nhúng, phần
cứng, phần mềm hệ thống nhúng
• Nắm được công nghệ IC khả trình PLD và quy trình
thiết kế hệ thống số với PLD
• Có khả năng thiết kế hệ nhúng trên FPGA sử dụng
công cụ thiết kế của Xilinx và Altera

3
© DHBK 2010

• Tài liệu tham khảo:


 Slides
 E-books
 VHDL programming by Example 4th edition by Douglas L. Perry
 Website
 www.xilinx.com
 www.Altera.com
 Course email: thietkevoifpga@gmail.com password: hut123456
• Đánh giá
 Điều kiện dự thi: hoàn thành bài tập lớn và lên lớp đầy đủ
 Kiểm tra giữa kỳ (30%)
 Bài tập lớn (30 %) (làm theo nhóm 4-5 sinh viên)
 Thi cuối kỳ (40 %)

4
© DHBK 2010

Acknowledgement
• Trong bài trình bày này có sử dụng một số slides của
giáo sư Prabhat Mishra, Đại học Florida và của các
đồng nghiệp khác

5
© DHBK 2010

PHẦN 1:
GiỚI THIỆU VỀ HỆ THỐNG NHÚNG

6
© DHBK 2010

NỘI DUNG

1. Giới thiệu về hệ thống nhúng


2. Phần cứng hệ nhúng
3. Phần mềm hệ nhúng
4. Các vấn đề trong quy trình thiết kế hệ nhúng
5. Tình hình đào tạo và phát triển hệ nhúng ở VN
6. Kết luận

7
© DHBK 2010

1. Giới thiệu về hệ thống nhúng


• Định nghĩa
• Các loại hệ thống nhúng
• Các thành phần của hệ thống nhúng
• Các đặc điểm của hệ thống nhúng
• Yêu cầu thiết kế
• Quy trình phát triển hệ nhúng
• Xu hướng phát triển

8
© DHBK 2010

1. Giới thiệu về hệ thống nhúng


• Định nghĩa
• Các loại hệ thống nhúng
• Các thành phần của hệ thống nhúng
• Các đặc điểm của hệ thống nhúng
• Yêu cầu thiết kế
• Quy trình phát triển hệ nhúng
• Xu hướng phát triển

9
© DHBK 2010

Định nghĩa

10
© DHBK 2010

Định nghĩa
• Vậy hệ thống nhúng là gì?
 Hệ thống bất kỳ sử dụng vi xử lý mà không phải là máy tính đa
năng và được nhúng vào một hệ thống lớn hơn
Hệ thống nhúng đầu tiên: Máy tính dẫn đường cho Apollo

11
© DHBK 2010

Ví dụ: BMW 745i

• *Intelligent
2, 000, 000 LOC Transportation *Cabin Air *Lighting
System
Quality
• Windows CE OS *Safety
(ITS)       

Systems
• 53 8-bit P
*Engine
• 11 32-bit P Performance
and Emission
Control
• 7 16-bit P (Traction

• Multiple Networks * Control)


Suspension
*Steering Controls *Digital Car
and
*Gear * Entertainmen
Radio
Braking Box t
Control

12
© DHBK 2010

1. Giới thiệu về hệ thống nhúng


• Định nghĩa
• Các loại hệ thống nhúng
• Các thành phần của hệ thống nhúng
• Các đặc điểm của hệ thống nhúng
• Yêu cầu thiết kế
• Quy trình phát triển hệ nhúng
• Xu hướng phát triển

13
© DHBK 2010

Các lọai hệ thống nhúng


• Các hệ thống xử lý tín hiệu
 Real-time video, set-top boxes, DVD players, medical
equipment, residential gateways
• Các hệ thống điều khiển phân tán
 Network routers, switches, firewalls, mass transit systems,
elevators
• Các hệ thống “nhỏ”
 Mobile phones, pagers, home appliances, toys, smartcards,
MP3 players, PDAs, digital cameras, sensors, smart badges

14
© DHBK 2010

1. Giới thiệu về hệ thống nhúng


• Định nghĩa
• Các loại hệ thống nhúng
• Các thành phần của hệ thống nhúng
• Các đặc điểm của hệ thống nhúng
• Yêu cầu thiết kế
• Quy trình phát triển hệ nhúng
• Xu hướng phát triển

15
© DHBK 2010
Các thành phần của hệ thống nhúng
Memory Controllers Interface

Software
(Application Programs)

Processor
Coprocessors

ASIC

Converters

Analog Digital Analog


16
© DHBK 2010

Các thành phần của hệ thống nhúng


• Các phần tử tương tự
 Sensors, Actuators, …
• Các phần tử số
 Bộ vi xử lý, Bộ đồng xử lý, Bộ nhớ, Buses
 Khối điều khiển, IC chuyên dụng (ASIC)
• Phần tử chuyển đổi
 ADC, DAC
• Phần mềm
 Hệ điều hành
 Middleware
 Các chương trình ứng dụng (MPEG-x, GSM-kernel, …)

17
© DHBK 2010

1. Giới thiệu về hệ thống nhúng


• Định nghĩa
• Các loại hệ thống nhúng
• Các thành phần của hệ thống nhúng
• Các đặc điểm của hệ thống nhúng
• Yêu cầu thiết kế
• Quy trình phát triển hệ nhúng
• Xu hướng phát triển

18
© DHBK 2010

Các đặc điểm của hệ thống nhúng


• Chuyên dụng
 Ứng dụng được định nghĩa trước
 Tối ưu về giá thành, về tài nguyên phần cứng, về công
suất tiêu thụ và về hiệu năng hoạt động.
• Xử lý tín hiệu số
 Tín hiệu được biểu diễn và xử lý số
• Có tính đáp ứng
 Đáp ứng với những thay đổi của môi trường xung quanh
• Thời gian thực
 Các công việc phải được xử lý và hoàn thành trước một
thời gian định trước
• Tính phân tán, kết nối mạng, …
19
© DHBK 2010

1. Giới thiệu về hệ thống nhúng


• Định nghĩa
• Các loại hệ thống nhúng
• Các thành phần của hệ thống nhúng
• Các đặc điểm của hệ thống nhúng
• Yêu cầu thiết kế
• Quy trình phát triển hệ nhúng
• Xu hướng phát triển

20
© DHBK 2010

Yêu cầu thiết kế


• Các thông số cơ bản
 Unit cost: the monetary cost of manufacturing each copy of the system,
excluding NRE cost
 NRE cost (Non-Recurring Engineering cost): The one-time
monetary cost of designing the system
 Size: the physical space required by the system
 Performance: the execution time or throughput of the system
 Power: the amount of power consumed by the system
 Flexibility: the ability to change the functionality of the system without
incurring heavy NRE cost

21
© DHBK 2010

Yêu cầu thiết kế


• Các thông số cơ bản (tiếp)
 Time-to-prototype: the time needed to build a working version of the
system
 Time-to-market: the time required to develop a system to the point that it
can be released and sold to customers
 Maintainability: the ability to modify the system after its initial release
 Correctness, safety, many more

22
© DHBK 2010
Yêu cầu thiết kế
Time-to-Market
• Time required to develop a
product to the point it can be
sold to customers
• Market window
 Period during which the product
Revenues ($)

would have highest sales


• Average time-to-market
constraint is about 8 months
Time (months)
• Delays can be costly

23
© DHBK 2010
Yêu cầu thiết kế
Thiệt hại khi đưa ra thị trường chậm

• Simplified revenue model


Peak revenue
 Product life = 2W, peak at W
 Time of market entry defines a
Peak revenue from
Revenues ($)

delayed entry triangle, representing market


On-time
penetration
Market rise Market fall
 Triangle area equals revenue
Delayed

• Loss
D W 2W
 The difference between the on-time
On-time Delayed Time
entry entry and delayed triangle areas (shaded
region)

24
© DHBK 2010
Yêu cầu thiết kế
Thiệt hại khi đưa ra thị trường chậm

• Area = 1/2 * base * height


• On-time = 1/2 * 2W * W
Peak revenue  Delayed = 1/2 * (W-D+W)*(W-D)

Peak revenue from • Percentage revenue loss =


Revenues ($)

On-time
delayed entry
(D(3W-D)/2W2)*100%
Market rise Market fall • Try some examples
Delayed

1. Lifetime 2W=52 wks, delay D=4 wks Loss =


D W 2W (4*(3*26 –4)/2*262) = 22%
Time
On-time Delayed
2. Lifetime 2W=52 wks, delay D=10 wks
entry entry
Loss = (10*(3*26 –10)/2*262) = 50%

 Delays are costly!

25
© DHBK 2010
Yêu cầu thiết kế
Giá thành (cost)
• Unit cost
 the monetary cost of manufacturing each copy of the system, excluding NRE
cost
• NRE cost (Non-Recurring Engineering cost)
 The one-time monetary cost of designing the system
• Total cost
 NRE cost + unit cost * # of unit
• Per-product cost
 total cost / # of units = (NRE cost / # of units) + unit cost

• Example
– NRE=$2000, unit=$100
– For 10 units
– total cost = $2000 + 10*$100 = $3000
– per-product cost = $2000/10 + $100 = $300

Amortizing NRE cost over the units results in


an additional $200 per unit
26
© DHBK 2010

1. Giới thiệu về hệ thống nhúng


• Định nghĩa
• Các loại hệ thống nhúng
• Các thành phần của hệ thống nhúng
• Các đặc điểm của hệ thống nhúng
• Yêu cầu thiết kế
• Quy trình phát triển hệ nhúng
• Xu hướng phát triển

27
© DHBK 2010

Quy trình phát triển hệ nhúng


Các phần tử phần cứng
Phần cứng

g
c ứn
n
p hầ …)
k ế t ,
Ý tưởng Phân chia HW/SW Thiết a yo
u
Specification
s i s, L
e
y nth
(S

Th
iết
kế
(C ph
Estimation - om ần
pil mề
Exploration a ti m
on
,…
)

Phần mềm
Phần mềm

28
Validation and Evaluation (area, power, performance, …)
© DHBK 2010

1. Giới thiệu về hệ thống nhúng


• Định nghĩa
• Các loại hệ thống nhúng
• Các thành phần của hệ thống nhúng
• Các đặc điểm của hệ thống nhúng
• Yêu cầu thiết kế
• Quy trình phát triển hệ nhúng
• Xu hướng phát triển

29
© DHBK 2010

Xu hướng phát triển


Number of transistors
1M

100K

10K
55%/year
1K

100
10

1 Time
1980 1985 1990 1995 2000 2005

Exponential growth for 3 decades!


This is called ‘Moore’s law’: number of transistors
doubles every 18 months
(Gordon Moore, founder Intel Corp.)
30
© DHBK 2010

Xu hướng phát triển

1000 10 1

Mainframe PC Smart Things


0.01 1 >100 # / human
Compute + DSP
Power

+ Communications

+ Ambient
Intelligence

1960 1970 1980 1990 2000 2010

31
© DHBK 2010
Xu hướng phát triển
Các thiết bị thông minh

Cochlear actuator(Cochlear Edegem) NeuroSensor (KNS) Bloodgas sensor (IMEC)

32
© DHBK 2010
Xu hướng phát triển
Môi trường thông minh

33 © Emile Aarts, HomeLab, Philips


© DHBK 2010

2. Phần cứng hệ nhúng


• Tổng quan
• Khối vào ra
• Khối truyền thông
• Khối xử lý

34
© DHBK 2010

2. Phần cứng hệ nhúng


• Tổng quan
• Khối vào ra
• Khối truyền thông
• Khối xử lý

35
© DHBK 2010

Tổng quan

36
© DHBK 2010

Tổng quan
• Các linh kiện rời rạc (COTS)
 e.g. wireless radios, sensors, I/O devices
 Cheap
• Các IC chuyên dụng (ASICs)
 ICs tailored to meet application needs
 Good performance for their intended tasks
• Vi xử lý
 DSPs
 Microcontrollers
 Microprocessors
• Các IC khả trình (PLD)

37
© DHBK 2010

Tổng quan
• Xu hướng phần cứng mới:
System-On-Chip (SOC)
Usual (or desired) specs:
 32-bit RISC CPU
 Built-in interfaces to RAM and ROM
 Built-in DMA, interrupt and timing controllers
 Built-in interfaces to disk or flash memory
 Built-in Ethernet/802.11 interfaces
 Built-in LCD/CRT interfaces
New SOCs appearing almost every week!
 Examples
 Intel StrongARM SA-1110
 Motorola PowerPC MPC823e
 NEC VR4181
 Many, many more
38
© DHBK 2010

2. Phần cứng hệ nhúng


• Tổng quan
• Khối vào ra
• Khối truyền thông
• Khối xử lý

39
© DHBK 2010

Khối vào ra
• Các loại cảm biến:
 Nhiệt, quang, áp suất, âm thanh, sinh học, CCD…
• Các bộ biến đổi
 ADC, DAC
• Các thiết bị ra
 Động cơ, van, xi lanh, LCD …

40
© DHBK 2010
Khối vào ra
Charge-Coupled Devices (CCD)

Image Sensors: Based on charge transfer to next pixel cell

41
© DHBK 2010
Khối vào ra
Biometrical Sensors
Example: Fingerprint sensor (© Siemens, VDE):

42
© DHBK 2010
Khối vào ra
Artificial eyes

© Dobelle Institute
(www.dobelle.com)
43
© DHBK 2010
Khối vào ra
Artificial eyes

He looks hale, hearty, and healthy — except for the


wires. They run from the laptops into the signal
processors, then out again and across the table and
up into the air, flanking his face like curtains before
disappearing into holes drilled through his skull.
Since his hair is dark and the wires are black, it's
hard to see the actual points of entry. From a
distance the wires look like long ponytails.

© Dobelle Institute
(www.dobelle.com)
44
© DHBK 2010
Khối vào ra

45
© DHBK 2010

2. Phần cứng hệ nhúng


• Tổng quan
• Khối vào ra
• Khối truyền thông
• Khối xử lý

46
© DHBK 2010

Khối truyền thông

Sensor/actuator busses

47
© DHBK 2010
Khối truyền thông
Sensor/Actuator Bus

Many wires less wires expensive & flexible

CNC:
48
Computerized Numerical Control
© DHBK 2010
Khối truyền thông
Field bus
• More powerful/expensive than sensor
interfaces; serial busses preferred.
•Examples:
Process Field Bus (Profibus)
http://www.profibus.com
Token passing;
9.6 kbit/s (1200 m) to 500 kbits/s (200m);
too slow to be used for hard time constraints.

49
© DHBK 2010
Khối truyền thông
Field busses
• Controller area network (CAN)
 Designed by Bosch and Intel in 1981;
 Used in cars and other equipment;
 Differential signaling with twisted pairs,
 Arbitration using CSMA/CA,
 Throughput between 10kbit/s and 1 Mbit/s,
 Low and high-priority signals,
 Max. latency of 134 µs for high priority signals,
 Coding similar to that of serial (RS-232) lines of PCs,
with modifications for differential signaling.
 http://www.can.bosch.com
• IEEE 488

50
© DHBK 2010
Khối truyền thông
Wireless Communication

• IEEE 802.11 a/b/g


• UMTS (Universal Mobile Telecommunications System)
 Bandwidth is becoming a scarce resource.
• DECT (Digital Enhanced Cordless Telecommunications)
 Standard used for wireless phones in Europe
• Bluetooth
 Connect devices e.g., mobile phone and headset

51
© DHBK 2010

2. Phần cứng hệ nhúng


• Tổng quan
• Khối vào ra
• Khối truyền thông
• Khối xử lý

52
© DHBK 2010

Khối xử lý

• IC chuyên dụng ASIC (Mpeg-2 decoder, Mp3


encoder…)
• Vi xử lý
 Vi xử lý đa năng (Intel, AMD, ARM, Power PC)
 Vi điều khiển
 DSP
• Reconfigurable Hardware
 PAL, PLA, CPLD, FPGA
• Memory

53
© DHBK 2010

Vi điều khiển
• Vi điều khiển = CPU + Bộ nhớ + các khối ghép nối ngoại vi +
các khối chức năng
 EEPROM
 RAM
 ADC/DAC
 Timer
 Bộ tạo xung nhịp
 PWM
 UART
 USB
 ...
• Word length
 4 bit: 2%
 8 bit: 36%
 16 bit: 25%
 32 bit: 34%
 64 bit: 3%
• Not pushing the limits of performance for cost reasons
54
© DHBK 2010

Vi điều khiển
• Shipments- > 8 Billion in 2000, 8 bit > 1/2 market
• Major Players: Microchip 16Fxx, Intel 8051, Motorola
MC68HC05, National COP800, SGS/Thomson ST62, Zilog
Z86Cxx

55
© DHBK 2010

PIC16C5x

56
© DHBK 2010

PIC16C5x
• High-Performance RISC CPU:
 Only 33 single word instructions to learn
 All instructions are single cycle (200 ns) except for
program branches which are two-cycle
 Operating speed: DC - 20 MHz clock input
 DC - 200 ns instruction cycle
 12-bit wide instructions
 8-bit wide data path
 Seven or eight special function hardware registers
 Two-level deep hardware stack
 Direct, indirect and relative addressing modes for
data and instructions
57
© DHBK 2010

PIC16C5x
• Peripheral Features:
 8-bit real time clock/counter (TMR0) with 8-bit
programmable prescaler
 Power-On Reset (POR)
 Device Reset Timer (DRT)
 Watchdog Timer (WDT) with its own on-chip
 RC oscillator for reliable operation
 Programmable code-protection
 Power saving SLEEP mode
• Applications:
 high-speed automotive and appliance motor control
 low-power remote ransmitters/receivers
58
 pointing devices and telecom processors.
© DHBK 2010

Họ vi điều khiển 8051

• Hiện nay có hơn 40 công ty sản xuất các loại vi điều


khiển khác nhau của họ 8051.
• Một số công ty có trên 40 version 8051.
• Các CORE 8051 có thể được tổ hợp trong các FPGA
hay ASIC.
• Trên 100 triệu vi điều khiển 8051 được bán ra mỗi
năm.
• Họ 8051 gặt hái được rất nhiều thành công và nó
cũng trực tiếp ảnh hưởng đến cấu trúc của các họ vi
điều khiển hiện nay.

59
© DHBK 2010

MCS-51
• 8051 thuộc họ vi điều khiển MCS-51.
• MCS-51 được phát triển bởi Intel và các nhà sản xuất
khác (như Siemens, Philips) là các nhà cung cấp
đứng thứ hai của họ này.
• Tóm tắt một số đặc điểm chính của họ 8051:
 4K bytes ROM trong
 128 bytes RAM trong
 4 cổng I/O 8-bit
 2 bộ định thời 16 bit
 Giao diện nối tiếp
 Quản lý được 64K bộ nhớ code bên ngoài
 Quản lý được 64K bộ nhớ dữ liệu bên ngoài
60
© DHBK 2010

Họ vi điều khiển 8051


C¸c ng¾t ngoµi C¸c sù kiÖn cÇn ®Õm

Ram trong
FF
Thanh ghi 128 byte
chøc n¨ng ram
®Æc biÖt
80
§iÒu khiÓn Nguån 7F
ng¾t Ng¾t 128 byte
Trong RAM
00

CPU

Bé Qu¶n Port Port Port Port Cæng


dao ®éng lý Bus 0 1 2 3 nèi
tiÕp

PSEN ALE §Þa chØ


XTAL 1,2 §Þa Më réng
thÊp/d÷ liÖu chØ cao trong 8032
61
© DHBK 2010

Họ vi điều khiển AVR


Bus d÷ liÖu 8 bÝt
Bé nhí flash Bé ®Õm Tr¹ng th¸i vµ Thanh ghi
1k x 16 cho ch­¬ng tr×nh kiÓm tra
ch­¬ng tr×nh
®iÒu khiÓn

Thanh ghi
§¬n vÞ ng¾t
Thanh ghi ®a n¨ng
lÖnh 32 x 8 §¬n vÞ SPI

UART nèi tiÕp


Bé gi¶i
m· lÖnh ALU
Timer/Counter
8 bÝt
C¸c ®­êng ®iÒu Timer/Counter
khiÓn 128 x 8
Data 16 bit víi PWM
SRAM Bé ®Þnh thêi
watchdog

128x8 Bé so s¸nh
EEPROM analog
15 ®­êng dÉn
vµo/ra
62
© DHBK 2010

PSoC

• Do hãng Cypress sản xuất


• Một loại công nghệ IC mới phát triển trong vài năm
gần đây.
• Khả năng tích hợp động các loại linh kiện số và tương
tự để tạo ra các khối số hoặc tương tự với chức năng
tuỳ thuộc người dùng.
• Kết hợp với một vi điều khiển trung tâm.

63
© DHBK 2010

Cấu trúc PSoC


• Lõi PSoC
 Vi xử lý 8bit, 24MHz, 4MIPS
 Flash ROM (từ 16K)
 RAM (128b-2kb)
 Bộ điều khiển ngắt
 Bus
• Các khốI số
 Flip-Flop, cổng logic
• Các khốI tương tự
 Các bộ khuếch đạI thuật toán
 Điện trở
 Tụ điện điều khiển đóng ngắt
64
© DHBK 2010
DSP
Texas Instruments TMS320C20x
Low end consumer Fixed Point
Dual access fixed MAC 16
data RAM 16x16+32->32 data

PROM Selection of 18
peripherals: address
serial comm.,
Loop controller timers,...
I/O
16
data

16
address

• Series continued; typical app.: Digital camera, feature-phones, disk drives,


Point-of-Sales Terminal
• 40 MHz, 3.3-5V, 3LM
• Available as core
65
© DHBK 2010
DSP
Texas Instruments TMS320C24x
Low end consumer Fixed Point
Dual access fixed MAC 16
data RAM 16x16+32->32 data

PROM 8 output PWM 16


address
Loop controller 8 channel A/D

Selection of CAN bus


peripherals: controller
serial comm.,
timers,... watchdog

• Series continued; typical app.: electrical motor control


• 50 MHz, 5V

66
© DHBK 2010

Bộ nhớ

permanence
Storage Mask-programmed ROM Ideal memory

Life of OTP ROM


product

Tens of EPROM EEPROM FLASH


years
Battery Nonvolatile NVRAM
life (10
years)

In-system
programmable SRAM/DRAM

Near
zero Write
ability

During External External External External


In-system, fast
fabrication programmer, programmer, programmer programmer
writes,
only one time only 1,000s OR in-system, OR in-system,
unlimited
of cycles 1,000s block-oriented
cycles
of cycles writes, 1,000s
of cycles

Write ability and storage permanence of memories,


showing relative degrees along each axis (not to scale).

67
© DHBK 2010

3. Phần mềm hệ thống nhúng


• Các loại phần mềm nhúng
• Đặc điểm của phần mềm nhúng
• Hệ điều hành nhúng

68
© DHBK 2010

3. Phần mềm hệ thống nhúng


• Các loại phần mềm nhúng
• Đặc điểm của phần mềm nhúng
• Hệ điều hành nhúng

69
© DHBK 2010

Các loại phần mềm nhúng

RTOS Standard OS

70
© DHBK 2010

3. Phần mềm hệ thống nhúng


• Các loại phần mềm nhúng
• Đặc điểm của phần mềm nhúng
• Hệ điều hành nhúng

71
© DHBK 2010

Đặc điểm của phần mềm nhúng

•Written in high level programming languages:


• Typically in C but increasingly in Java or C++.

•The engineers that write embedded software are rarely


computer scientists
•Very stringent dependability requirements:
• human safety, consumer expectations, liability and government
regulation
•BMW recalled 15,000 7-series sedans in 2002 at an estimated
cost of $50 million.
•Very difficult to debug because of: concurrency, interrupts,
exceptions,
72
process scheduling and hardware-in-the-loop.
© DHBK 2010

3. Phần mềm hệ thống nhúng


• Các loại phần mềm nhúng
• Đặc điểm của phần mềm nhúng
• Hệ điều hành nhúng

73
© DHBK 2010

Hệ điều hành nhúng


• Non RTOS
 PalmOS: 68K Freescale, ARM9
 Symbian: ARM
 WindowsCE: x86, ARM, MIPS..
 MAC OS X
• RTOS
 VxWorks
 QNX
 TRON/iTRON
 eCOS
• Linux
 Linux is already ubiquitous
Hundreds of different devices are using it
 Several variations-from ‘soft real time’ to ‘hard real time’
 Numerous commercial + open source products: LynxOS, RTAI, Android
• Others
74
 TinyOS
© DHBK 2010

Hệ điều hành nhúng

The Embedded OS Market 2006

75
© DHBK 2010

Lập trình cho PC và lập trình nhúng

76
© DHBK 2010

Lập trình cho PC và lập trình nhúng

77
© DHBK 2010

Lập trình cho PC và lập trình nhúng

78
© DHBK 2010

Lập trình cho PC và lập trình nhúng

79
© DHBK 2010
4. Các vấn đề trong quy trình thiết kế hệ
nhúng
• Mô tả hệ thống (specification)
• Phân chia phần cứng, phần mềm (HW/SW
partitioning)
• Lựa chọn phương án thiết kế (Design space
exploration)

80
© DHBK 2010
4. Các vấn đề trong quy trình thiết kế hệ
nhúng
• Mô tả hệ thống (specification)
• Phân chia phần cứng, phần mềm (HW/SW
partitioning)
• Lựa chọn phương án thiết kế (Design space
exploration)

81
© DHBK 2010

Các phần tử phần cứng


Mô tả hệ thống
Phần cứng

g
c ứn
n
p hầ …)
k ế t ,
Ý tưởng Phân chia HW/SW Thiết a yo
u
Specification
s i s, L
e
y nth
(S

Th
iết
kế
(C ph
Estimation - om ần
pil mề
Exploration a ti m
on
,…
)

Phần mềm
Phần mềm

82
Validation and Evaluation (area, power, performance, …)
© DHBK 2010

Mô tả hệ thống
• Các ngôn ngữ mô tả hệ thống
 State Charts
 SDL
 Petri Nets
 UML, MSC
 VHDL, Verilog
 SpecC, SystemC, SystemVerilog

83
© DHBK 2010
Mô tả hệ thống
StateCharts: D. Harel, 1987

Hierarchy

FSM will be in exactly


one of the substates of
S if S is active
(either in A or in B or ..)

superstate

substates

84
© DHBK 2010
Mô tả hệ thống
SDL
• Specification and Description Language
 Designed for specification of distributed systems.

• Defined by International Telecommunication Union (ITU): Z.100


recommendation in 1980
• Provides textual and graphical formats.
• Like StateCharts, it is based on CFSM model of computation; each FSM is
called a process,
• It uses message passing for communications, and supports operations on
data.

85
© DHBK 2010
Mô tả hệ thống
FSMs/Processes in SDL

state

input
output

86
© DHBK 2010
Mô tả hệ thống
SDL

87
© DHBK 2010
Mô tả hệ thống
Petri Nets
• Carl Adam Petri, PhD thesis, 1962.
• Focus on modeling causal dependencies; no global
synchronization assumed (message passing only).
• Applications:
 Modeling of resources;
 Modeling of mutual exclusion;
 Modeling of synchronization.
• Key elements:
 Conditions: Either met or not met.
 Events: May take place if certain conditions are met.
 Flow relations: Relates conditions and events.
• Conditions, events and the flow relation form a bipartite graph
(graph with two kinds of nodes).
88
© DHBK 2010
Mô tả hệ thống
Petri Nets

Preconditions

89
© DHBK 2010
Mô tả hệ thống
UML (Unified Modeling Language)

90
© DHBK 2010

Mô tả hệ thống
Verilog

• HW description language competing with VHDL


• Standardized:
 IEEE 1364-1995 (Verilog version 1.0)
 IEEE 1364-2001 (Verilog version 2.0)
• Less flexible than VHDL.
• More popular in the US (VHDL common in Europe)
module mux
(output f,
a input a, b, sel);

and #5 g1 (f1, a, nsel),


f
g2 (f2, b, sel);
b or #5 g3 (f, f1, f2);
not g4 (nsel, sel);
sel
endmodule

91
© DHBK 2010
Mô tả hệ thống
VHDL

• HDL = hardware description language


 VHDL = VHSIC hardware description language
 VHSIC = very high speed integrated circuit
• 1987: IEEE standard 1076; 1992 revision;
• Recently: VHDL-AMS models analog

entity full_adder is
port(a, b, carry_in: in Bit; -- input ports
sum,carry_out: out Bit); --output
ports
end full_adder;

92
© DHBK 2010

Mô tả hệ thống
SystemC
#include <systemc .h>
#include <iostream>

SC _MODULE( hello ) {
void say_hello () {
cout << " hello " << name () << endl ;}
SC_CTOR( hello ) {
SC THREAD( say_hello );}
};
int sc_main ( int argc , char ** argv ) {
hello hello_inst (" world ");
sc_start ();
}

93
93
© DHBK 2010
Mô tả hệ thống
SystemVerilog
• Corresponds to Verilog versions 3.0 and 3.1.
• Includes:
 Additional language elements to model behavior
 C data types such as int
 Type definition facilities
 Definition of interfaces of hardware components as
separate entities
 Mechanism for calling C/C++-functions from Verilog
 Limited mechanism for calling Verilog functions from C.

94
© DHBK 2010
4. Các vấn đề trong quy trình thiết kế hệ
nhúng
• Mô tả hệ thống (specification)
• Phân chia phần cứng, phần mềm (HW/SW
partitioning)
• Lựa chọn phương án thiết kế (Design space
exploration)

95
© DHBK 2010

HW/SW Partitioning

96
© DHBK 2010

Hardware/Software Co-design

Specification

Mapping
Processor Processor
P1 P2 Hardware

[Niemann, Hardware/Software Co-Design for Data Flow Dominated Embedded Systems, Kluwer
Academic Publishers, 1998 (Comprehensive mathematical model)]

97
© DHBK 2010
4. Các vấn đề trong quy trình thiết kế hệ
nhúng
• Mô tả hệ thống (specification)
• Phân chia phần cứng, phần mềm (HW/SW
partitioning)
• Lựa chọn phương án thiết kế (Design space
exploration)

98
© DHBK 2010
Lựa chọn phương án thiết kế
Traditional HW/SW Co-Design Flow
Design
Specification Estimation

HW/SW Partitioning

HW SW
VHDL, Verilog C

Synthesis Compilation

Co-Simulation

On-Chip Processor
Memory Core
Off-Chip
Memory
Synthesized
Interface
HW

99
© DHBK 2010

Lựa chọn phương án thiết kế


ADL-Driven SOC Design Flow
IP Library
Design ADL
Specification Estimation
Specification M1
P2
P1

HW/SW Partitioning

HW SW
VHDL, Verilog
C

Synthesis Compilation

Co-Simulation
ADL: Architecture
On-Chip Processor
Memory Core
Off-Chip Description Language
Memory
Synthesized
Interface
HW

100
© DHBK 2010
Lựa chọn phương án thiết kế
ADL-driven Design Space Exploration
Processor Memory Co-processor
IP Library IP Library IP Library

Processor Memory Coprocessors


Core Subsystem Feedback

ADL Specification

Success
Verify
Failed

Application Compiler Obj Simulator

101
© DHBK 2010

Lựa chọn phương án thiết kế


Exploration Methodology
• Four steps
 Architecture Specification
 Use of Architecture Description Language (ADL)
 Software Toolkit Generation
 Compiler, simulator, assembler, debugger
 Generation of Hardware Models (Prototypes)
 Design space exploration
 Find the best possible architecture for the given set of application
programs under area, power, performance constraints

102
© DHBK 2010
5. Tình hình đào tạo và phát triển hệ
nhúng ở VN
• Phát triển hệ thống nhúng trên thế giới
• Phát triển hệ thống nhúng ở Việt Nam
• Tình hình đào tạo hệ thống nhúng ở Việt Nam

103
© DHBK 2010
5. Tình hình đào tạo và phát triển hệ thống
nhúng ở VN
• Phát triển hệ thống nhúng trên thế giới
• Phát triển hệ thống nhúng ở Việt Nam
• Tình hình đào tạo hệ thống nhúng ở Việt Nam

104
© DHBK 2010
Phát triển hệ thống nhúng trên thế giới

105
© DHBK 2010
Phát triển hệ thống nhúng trên thế giới

106
© DHBK 2010
Phát triển hệ nhúng trên thế giới

Nhật Bản: 40% doanh thu phần mềm là từ phần mềm nhúng

107
© DHBK 2010
Phát triển hệ thống nhúng trên thế giới

• Tỷ lệ doanh thu phần hệ nhúng trong toàn bộ sản


phẩm cuối cùng:
 Telecommunications (37%)
 Consumer Electronics and Intelligent Homes (41%)
 Industrial Automation (22%)
 Health/Medical Equipment (33%)
 Automotive industry (40%)

108
© DHBK 2010
5. Tình hình đào tạo và phát triển hệ
nhúng ở VN
• Phát triển hệ nhúng trên thế giới
• Phát triển hệ thống nhúng ở Việt Nam
• Tình hình đào tạo hệ nhúng ở Việt Nam

109
© DHBK 2010

Phát triển hệ thống nhúng ở Việt Nam


• Video clip
• Fsoft
 Outsource cho thị trường Nhật Bản và Mỹ: < 10% doanh thu từ phần
mềm
• Panasonic R&D Center in Vietnam
• Một số công ty khác: ETS, Applistar

110
© DHBK 2010
5. Tình hình đào tạo và phát triển hệ thống
nhúng ở VN
• Phát triển hệ thống nhúng trên thế giới
• Phát triển hệ thống nhúng ở Việt Nam
• Tình hình đào tạo hệ thống nhúng ở Việt Nam

111
© DHBK 2010
Tình hình đào tạo hệ thống nhúng ở Việt Nam

• Đại học công nghệ-Đại học quốc gia Hà Nội


 Lập trình phần mềm nhúng và thời gian thực
• ĐH Bách Khoa Hà Nội
 Viện công nghệ thông tin và truyền thông
Thiết kế hệ nhúng
Hệ điều hành thời gian thực
 Khoa Điện tử Viễn thông
Môn học:
 Thiết kế hệ nhúng
 Các hệ thống thời gian thực
 Thiết kế hệ thống số với vi mạch khả trình
Khóa học ngắn hạn
 Pclass (2 tuần)
Lab
 Samsung Embedded Software
 Embedded Systems and Reconfigurable Computing
112
© DHBK 2010
Tình hình đào tạo hệ thống nhúng ở Việt Nam

• Đại học Bách khoa Đà Nẵng


 Chương trình tiên tiến Hệ thống nhúng (hợp tác với
Porland State University, Oregan, USA)
• Đại học Cần Thơ
 Hệ thống nhúng
• Khoa Điện tử Viễn thông, ĐH KHTN- ĐH QGTPHCM
 Khóa học ngắn hạn thiết kế hệ nhúng

113
© DHBK 2010

PHẦN 2:
THIẾT KẾ HỆ NHÚNG TRÊN
FPGA

114
© DHBK 2010
Chương 1. Giới thiệu chung về công nghệ IC
khả trình PLD
1.1 Công nghệ IC khả trình
1.2 Ứng dụng của công nghệ IC khả trình

115
© DHBK 2010

1.1 Công nghệ IC khả trình


• Realisation as AND-OR: • Realisation as OR-AND:
F1=xy+xy’z+x’yz F1=((x’+y’) (x’+y+z’)
(x+y’+z’))’

x y z x y z

F1 F1

116
© DHBK 2010
1.1 Công nghệ IC khả trình
Programmable logic array
• PLA

Programmable
And
Input
plane

Or Output
plane

117
© DHBK 2010
1.1 Công nghệ IC khả trình
Programmable logic array
• PLA

118
© DHBK 2010
1.1 Công nghệ IC khả trình
Programmable Array Logic
• PAL

Programmable
And
Input
plane

Fixed

Or Output
plane

119
© DHBK 2010
1.1 Công nghệ IC khả trình
Programmable Array Logic

120
© DHBK 2010

1.1 Công nghệ IC khả trình


Complex Programmable Logic Devices

• CPLD

121
© DHBK 2010

1.1 Công nghệ IC khả trình


Complex Programmable Logic Devices

• Các công nghệ lập trình


 PROM: Lập trình 1 lần
 EPROM, flash, EEPROM: lập trình nhiều lần

Non-volatile

122
© DHBK 2010

Ví dụ: PROM
Vcc Vcc Vcc Vcc Fuse
After manufacturing
Address

2-to-4 Decoder

2
MSB

2
LSB

2-to-4 Mux

123 Data
© DHBK 2010

Ví dụ: PROM
Vcc Vcc Vcc Vcc
After programming
Address

2-to-4 Decoder

2
MSB

2
LSB

2-to-4 Mux

124 Data
© DHBK 2010
1.1 Công nghệ IC khả trình
Field-programmable Gate Array
• FPGA: XC40xx Routing viaLong
switching
lines matrices

I/O I/O I/O I/O


I/O
SM SM SM SM

CLB CLB CLB


I/O

SM SM SM SM

CLB CLB CLB


I/O

SM SM SM SM
125
© DHBK 2010
1.1 Công nghệ IC khả trình
Field-programmable Gate Array
• Cấu tạo của một CLB (Configurable Logic Block)

16x1 G
LUT: GQ
FF
Bool-function
of 4
G
variables

16x1
F
LUT: FQ
FF
Bool-function
of 4
variables F

126
© DHBK 2010
1.1 Công nghệ IC khả trình
Field-programmable Gate Array
• FPGA: Switching Matrix SM
Pass
TOR

127
© DHBK 2010
1.1 Công nghệ IC khả trình
Field-programmable Gate Array
• Công nghệ lập trình:
 SRAM-based:
Volatile
Reprogrammble
 Antifuse
Non-volatile
Programmed only-one
IP security

128
© DHBK 2010
1.1Công nghệ IC khả trình
Field-programmable Gate Array
• Hãng cung cấp FPGA hàng đầu thế giới
 Công ty Xilinx: http://www.xilinx.com/
 Công ty Altera: http://www.altera.com/
 Công ty Lattice Semiconductor: http://www.latticesemi.com/
 Công ty Actel: http://www.actel.com/
 Công ty Crypress: http://www.cypress.com/
 Công ty Atmel: http://www.atmel.com/
 Công ty QuickLogic: http://www.quicklogic.com/

129
© DHBK 2010
1.1 Công nghệ IC khả trình
Ưu điểm

130
© DHBK 2010
1.1 Công nghệ IC khả trình
Ưu điểm
• FPGA vs Custom ASIC
 FPGAs are more flexible
 FPGAs are more cost effective for small quantities
 ASICs have higher densities
• FPGA vs Parallel Computer
 FPGAs are more cost effective
 FPGAs are smaller
 Parallel Computers are easier to program

131
© DHBK 2010

1.1 FPGA vs. ASIC Cost


ASIC: High volumes needed to recover design cost

Total cost FPGA .09µ


FPGA .13µ
ASIC .09µ ASIC cost/part
is lower
ASIC Design
Cost is much ASIC .13µ
higher
(and increasing)!!

For each technology advance,


Volume
crossover volume moves higher Courtesy: Richard Sevcik, Xilinx

132
© DHBK 2010

1.2 Ứng dụng của công nghệ IC khả trình

• Aerospace & Defense


• Automotive
• Consumer
• Digital Video Technologies
• Industrial/Scientific & Medical
• Test & Measurement
• Wired Communications
• Wireless Communications

133
© DHBK 2010

1.2 Ứng dụng của công nghệ IC khả trình

134
© DHBK 2010

1.2 Ứng dụng của công nghệ IC khả trình

135
© DHBK 2010

1.2 Ứng dụng của công nghệ IC khả trình

136
© DHBK 2010

1.2 Ứng dụng của công nghệ IC khả trình

137
© DHBK 2010

1.2 Ứng dụng của công nghệ IC khả trình

138
© DHBK 2010

1.2 Ứng dụng của công nghệ IC khả trình

139
© DHBK 2010

1.2 Ứng dụng của công nghệ IC khả trình

140
© DHBK 2010

1.2 Ứng dụng của công nghệ IC khả trình

141
© DHBK 2010

1.2 Ứng dụng của công nghệ IC khả trình

142
© DHBK 2010
Chương 2: Thiết kế dùng IC khả trình của
Xilinx và Altera
2.1 Các họ PLD của Xilinx
2.2 Cấu trúc PLD của Xilinx
2.3 Các bước thiết kế với PLD của Xilinx
2.4 Các họ PLD của Altera
2.5 Cấu trúc PLD của Altera
2.6 Các bước thiết kế với PLD của Altera

143
© DHBK 2010

2.1 Các họ PLD của Xilinx

144
© DHBK 2010

2.1 Các họ PLD của Xilinx

145
© DHBK 2010
2.1 Các họ PLD của Xilinx
A Decade of Progress
1000x

• 200x More Logic


 Plus memory, µP XC4000 & Virtex-4
Spartan
etc. 100x
• 40x Faster CLB Capacity Virtex-II &
• 50x Lower Power Speed Virtex-II Pro
Power per MHz Virtex &
• 500x Lower Cost Price Virtex-E
10x

Spartan-2

XC4000 Spartan-3

1x
'91 '92 '93 '94 '95 '96 '97 '98 '99 '00 '01 '02 '03 '04

Year
Courtesy: Richard Sevcik, Xilinx
146
© DHBK 2010

2.1 Các họ PLD của Xilinx


• Họ Virtex:
 Virtex-5
 2006, 1 V, 65 nm
 550 MHz, low power
 330,000 logic cells, DSP, PowerPC
 1200 I/O pins
 Virtex-4:
 2004, 1.2 V, 90nm
 500 MHz, low power
 200.000 logic cells, DSP, PowerPC
 Thay thế ASIC, ASSP
 Virtex-II Pro /X
 2002, 1.5 V, 130nm
 400 MHz
 3K to 99K logic cells + DSP, PowerPC

147
© DHBK 2010

2.1 Các họ PLD của Xilinx


• Họ Virtex:
 Virtex-II
2001, 1.5 V, 150nm
300 MHz
3K to 99K logic cells
 Virtex /E
1998/1999, 2.5 /1.8 V, 220 / 180 nm
150/ 200 MHz
3K to 70K logic cells

148
© DHBK 2010

2.1 Các họ PLD của Xilinx


• Họ Spartan:
 Spartan-3A DSP/AN/A/E /L
 2003, 1.5 V, 90nm
 300 MHz
 5M gates

149
© DHBK 2010

2.1 Các họ PLD của Xilinx


• CPLD- Họ Coolrunner
 CoolRunner-II
 1.8V, 180 nm
 303 MHz
 32-512 macrocells
 Ultra Low power
 CoolRunner-XPLA3
 3.3 V
 200 MHz
 32-512 macrocells
 Low power

150
© DHBK 2010

2.1 Các họ PLD của Xilinx


• CPLD- Họ XC9500
 XC9500XV
 2.5 V
 250 MHz
 36-288 macrocells
 Low cost
 XC9500XL
 3.3 V
 200 MHz
 36-288 macrocells
 Low cost
 XC9500
 5 V, 200 MHz
 36-288 macrocells
 Low cost

151
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx

• All Xilinx FPGAs contain the same basic resources


 Slices (grouped into CLBs)
Contain combinatorial logic and register resources
 IOBs
Interface between the FPGA and the outside world
 Programmable interconnect
 Other resources
Memory
Multipliers
Global clock buffers
Boundary scan logic

152
© DHBK 2010
2.2 Cấu trúc PLD của Xilinx
Slices and CLBs
• Each Virtex-II CLB contains
four slices COUT COUT
BUFT
 Local routing provides BUF T
feedback between slices in the Slice S3
same CLB, and it provides
routing to neighboring CLBs Slice S2
Switch SHIFT
 A switch matrix provides Matrix
access to general routing Slice S1
resources
Slice S0 Local Routing

CIN CIN

154
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Simplified Slice Structure

• Each slice has four


outputs
 Two registered outputs,
two non- Slice 0
registered outputs PRE
LUT Carry D Q
 Two BUFTs associated CE

with each CLB, accessible CLR

by all 16 CLB outputs


• Carry logic runs vertically,
LUT Carry D PRE
CE Q

up only CLR

 Two independent
carry chains
155
per CLB
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Detailed Slice Structure

 LUTs
 MUXF5, MUXF6,
MUXF7, MUXF8
(only the F5 and
F6 MUX are shown
in this diagram)
 Carry Logic
 MULT_ANDs
 Sequential Elements

156
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Look-Up Tables

• Combinatorial logic is stored in Look-Up


Tables (LUTs) A B C D Z
 Also called Function Generators (FGs) 0 0 0 0 0
 Capacity is limited by the number of inputs, 0 0 0 1 0
not by the complexity 0 0 1 0 0
0 0 1 1 1
• Delay through the LUT is constant
0 1 0 0 1
0 1 0 1 1
Combinatorial Logic
. . .
A 1 1 0 0 0
B
Z 1 1 0 1 0
C
D 1 1 1 0 0
1 1 1 1 1

157
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Connecting Look-Up Tables

MUXF8 combines the two


CLB

F8
MUXF7 outputs (from the CLB
above or below)

F5
Slice S3
MUXF6 combines slices S2

F6
and S3

F5
Slice S2

MUXF7 combines the two


F7

MUXF6 outputs
Slice S1
F5

MUXF6 combines slices S0 and S1


F6

Slice S0
F5

MUXF5 combines LUTs in each slice

158
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Flexible Sequential Elements

• Either flip-flops or latches


• Two in each slice; eight in each CLB
FDRSE_1
• Inputs come from LUTs or from an D S Q
independent CLB input CE

• Separate set and reset controls R

 Can be synchronous or FDCPE

asynchronous D PRE Q
CE
• All controls are shared within a slice
CLR
 Control signals can be inverted
locally within a slice LDCPE

D PRE Q
CE
G
CLR

159
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Shift Register LUT (SRL16CE)

• Dynamically addressable serial shift


registers
 Maximum delay of 16 clock LUT
cycles per LUT (128 per CLB) D D Q
CE CE
 Cascadable to other LUTs or CLK
CLBs for longer shift registers
D Q
 Dedicated connection from CE
Q15 to D input of the next
SRL16CE
D Q Q
 Shift register length can CE
be changed asynchronously
by toggling address A

D Q
LUT CE

A[3:0]
Q15 (cascade out)

160
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


IOB Element

• Input path
 Two DDR registers
IOB
• Output path Input
Reg DDR MUX
 Two DDR registers OCK1 Reg
ICK1
 Two 3-state enable Reg
DDR registers OCK2 3-state Reg
ICK2
• Separate clocks and
clock enables for I and O Reg DDR MUX
OCK1
• Set and reset signals PAD
are shared Reg
OCK2 Output

161
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


SelectIO Standard

• Allows direct connections to external signals of varied


voltages and thresholds
 Optimizes the speed/noise tradeoff
 Saves having to place interface components onto your board
• Differential signaling standards
 LVDS, BLVDS, ULVDS
 LDT
 LVPECL
• Single-ended I/O standards
 LVTTL, LVCMOS (3.3V, 2.5V, 1.8V, and 1.5V)
 PCI-X at 133 MHz, PCI (3.3V at 33 MHz and 66 MHz)
 GTL, GTLP
 and more!
162
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Other Virtex-II Features

• Distributed RAM and block RAM


 Distributed RAM uses the CLB resources (1 LUT = 16 RAM
bits)
 Block RAM is a dedicated resources on the device (18-kb
blocks)
• Dedicated 18 x 18 multipliers next to block RAMs
• Clock management resources
 Sixteen dedicated global clock multiplexers
 Digital Clock Managers (DCMs)

163
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Distributed SelectRAM Resources

• Uses a LUT in a slice as memory


• Synchronous write
RAM16X1S
• Asynchronous read D
WE

 Accompanying flip-flops LUT


WCLK
A0 O
can be used to create A1
A2
synchronous read A3

• RAM and ROM are initialized during


configuration D
RAM32X1S
D
RAM16X1D

 Data can be written to RAM Slice


WE
WCLK
WE
WCLK

after configuration A0
A1
O A0
A1
SPO

• Emulated dual-port RAM


A2 A2
LUT A3 A3
A4 DPRA0 DPO
 One read/write port DPRA1
DPRA2

 One read-only port DPRA3

LUT

164
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Block SelectRAM Resources

• Up to 3.5 Mb of RAM in 18-kb


blocks
 Synchronous read and write 18-kb block SelectRAM memory
DIA
• True dual-port memory DIPA
ADDRA
 Each port has synchronous WEA
ENA
read and write capability SSRA DOA
 Different clocks for each port CLKA DOPA

• Supports initial values DIB


DIPB
• Synchronous reset on output ADDRB
WEB
latches ENB
SSRB DOB
• Supports parity bits CLKB DOPB

 One parity bit per eight data bits

165
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Dedicated Multiplier Blocks

• 18-bit twos complement signed operation


• Optimized to implement Multiply and Accumulate functions
• Multipliers are physically located next to block SelectRAM™ memory

Data_A
(18 bits) 4 x 4 signed
8 x 8 signed
18 x 18 Output
Multiplier (36 bits) 12 x 12 signed
18 x 18 signed
Data_B
(18 bits)

166
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Global Clock Routing Resources

• Sixteen dedicated global clock multiplexers


 Eight on the top-center of the die, eight on the bottom-center
 Driven by a clock input pad, a DCM, or local routing
• Global clock multiplexers provide the following:
 Traditional clock buffer (BUFG) function
 Global clock enable capability (BUFGCE)
 Glitch-free switching between clock signals (BUFGMUX)
• Up to eight clock nets can be used in each clock
region of the device
 Each device contains four or more clock regions

167
© DHBK 2010

2.2 Cấu trúc PLD của Xilinx


Digital Clock Manager (DCM)

• Up to twelve DCMs per device


 Located on the top and bottom edges of the die
 Driven by clock input pads
• DCMs provide the following:
 Delay-Locked Loop (DLL)
 Digital Frequency Synthesizer (DFS)
 Digital Phase Shifter (DPS)
• Up to four outputs of each DCM can drive onto global
clock buffers
 All DCM outputs can drive general routing

168
© DHBK 2010

2.3 Các bước thiết kế với PLD của Xilinx


CPLD/FPGA design flow

• Design Flow

169
© DHBK 2010

2.3 Các bước thiết kế với PLD của Xilinx


Design Entry

• Using schematic editor:


 Works well with small designs: 5k to 10 k gates
 Schematic can be compiled to HDL
• Using HDL (Hardware Description Language)
 VHDL, Verilog
 Others: Abel
 Works with large design: > 100 k gates
• Using State Editor:
 Mostly for controller
 FSM can be compiled to HDL
• Higher level design language
 System C: C based
 Ocapi-xl: C and C++ based

170
© DHBK 2010

2.3 Các bước thiết kế với PLD của Xilinx


Design Entry

• Design tools:
 Webpack ISE (miễn phí)
Thiết kế cho CPLD, FPGA
Có công cụ thiết kế dùng schematic, ngôn ngữ mô tả phần
cứng, công cụ tổng hợp, mô phỏng và vật lý
http://www.xilinx.com/ise/logic_design_prod/webpack.htm
 ISE Foundation™ (không miễn phí)
chức năng tương tự như Webpack
 EDK and Platform studio
Dùng để thiết kế hệ thống nhúng với FPGA

171
© DHBK 2010

Schematic diagram: example

Language based
refinement:
VHDL editor

172
© DHBK 2010

VHDL: Example

173
© DHBK 2010

2.3 Các bước thiết kế với PLD của Xilinx


Design Synthesis

• Synthesis process:
 Check code syntax
 Analyze the hierarchy of the design
 Compile RTL to gate level
 Create netlist of the design
 Take into account the architecture of the target FPGA
• Synthesis tools:
 XST (Xilinx synthesis technology)
Generate a NGC file
 LeonardoSpectrum from Mentor Graphics, Inc
 Synplify and Synplify Pro from Synplicity Inc.

174
© DHBK 2010

2.3 Các bước thiết kế với PLD của Xilinx


Design Implementation

• Translate:
 Translate the design netlist and constraints into (Xilinx) primitives
 Input: NGC file, Output: NGD file (Native Generic Database)
• Map:
 Map the primitives to the actual physic circuit of the device
 Input: NGD file, Output: NCD file ( Native Circuit Description)
• Place and Route (PAR):
 Place the circuits on the CLB and find a good route to connect them
 Input: NCD file, Output: NCD file

175
© DHBK 2010

2.3 Các bước thiết kế với PLD của Xilinx


Xilinx device programming

• Create a bitstream to program the device


• Input: NCD file, Output: bit file
• Programming tools:
 Download software + download cable

176
© DHBK 2010

2.3 Các bước thiết kế với PLD của Xilinx


Design verification

• Mô phỏng chức năng với behavioral simulation


• Mô phỏng về thời gian với timing simulation
• Kiểm tra phần cứng
• Verification tools:
 ModelSim
 ChipScope Pro

177
© DHBK 2010
2.4 Các họ PLD của Altera

178
© DHBK 2010
2.4 Các họ PLD của Altera

• Họ Cyclone:
 Cyclone III:
 2007, 65 nm FPGA
 Low power, low cost
 Embedded memory, embedded multipliers

179
© DHBK 2010
2.4 Các họ PLD của Altera

• Họ Cyclone:
 Cyclone II:
 90 nm FPGA
 Low power, low cost
 Embedded memory, embedded multipliers

180
© DHBK 2010
2.4 Các họ PLD của Altera

• Họ Cyclone:
 Cyclone:
 2003, 0.13 um
 Low power, low cost
 Embedded memory

181
© DHBK 2010
2.4 Các họ PLD của Altera

• Họ Stratix:
 Stratix III:
 2007, 65 nm
 Low power, high performance, high density
 Embedded memory, multipliers

182
© DHBK 2010
2.4 Các họ PLD của Altera

• Họ Stratix:
 Stratix II:
 90 nm
 high performance, high density
 Embedded memory, multipliers, DSP
blocks

183
© DHBK 2010
2.4 Các họ PLD của Altera

• Họ Stratix:
 Stratix:
 0.13-µm
 high performance, high density
 Embedded memory, multipliers, DSP blocks

184
© DHBK 2010
2.4 Các họ PLD của Altera

• Họ CPLD:
 Max II:
 Low cost, high performance

185
© DHBK 2010

2.5 Cấu trúc PLD của Altera

186
© DHBK 2010

2.5 Cấu trúc PLD của Altera


• Logic array block (LAB): 10 Logic elements(LE)

187
© DHBK 2010

2.5 Cấu trúc PLD của Altera


• Logic element (LE)

188
© DHBK 2010

2.5 Cấu trúc PLD của Altera


• Global clock and PLL

189
© DHBK 2010

2.5 Cấu trúc PLD của Altera


• IOE structure

190
© DHBK 2010

2.6 Các bước thiết kế với PLD của Altera


FPGA design flow

191
© DHBK 2010

2.6 Các bước thiết kế với PLD của Altera


CPLD design flow

192
© DHBK 2010

2.6 Các bước thiết kế với PLD của Altera

• Software tools

193
© DHBK 2010

Chương 3. Ngôn ngữ mô tả phần cứng VHDL

3.1 Giới thiệu về VHDL


3.2 Tín hiệu và các kiểu dữ liệu
3.3 Các phép toán số học và logic
3.4 Các lệnh song song và tuần tự
3.5 Các cấu trúc tuần tự
3.6 Chương trình con

194
© DHBK 2010

3.1 Giới thiệu về VHDL


• VHDL = VHSIC Hardware Description Language
• VHSIC = Very High Speed Integrated Circuit
• Là ngôn ngữ lập trình dùng để mô tả hoạt động của
hệ thống số
• Được quy định trong chuẩn IEEE 1076 từ năm 1983
• Các ngôn ngữ mô tả phần cứng khác:
 Verilog
 Abel

195
© DHBK 2010
library IEEE; -- Su dung thu vien chuan IEEE
use IEEE.STD_LOGIC_1164.ALL; --Su dung tat ca cac thanh phan trong goi STD_LOGIC_1164

entity hex2led is
Port ( HEX : in std_logic_vector(3 downto 0);
LED : out std_logic_vector(6 downto 0));
end hex2led;
-- Khai bao hoat dong cua hex2Led
architecture Behavioral of hex2led is

begin
with HEX SELect
LED<= "1111001" when "0001", --1
"0100100" when "0010", --2
"0110000" when "0011", --3
"0011001" when "0100", --4
"0010010" when "0101", --5
"0000010" when "0110", --6
"1111000" when "0111", --7
"0000000" when "1000", --8
"0010000" when "1001", --9
"0001000" when "1010", --A
"0000011" when "1011", --b
"1000110" when "1100", --C
"0100001" when "1101", --d
"0000110" when "1110", --E
"0001110" when "1111", --F
"1000000" when others; --0

end Behavioral;
196
© DHBK 2010
3.1 Giới thiệu về VHDL
Ví dụ 1
• Thiết kế mạch ‘Test’ với 3 đầu vào 8-bit (In1, In2, In3)
và hai đầu ra 1 bit (Out1, Out2). Out1=‘1’ khi In1=In2
và Out2=‘1’ khi In1 = In3

Test
Compare
In1 Out1
A
EQ
B
In2
Compare
A
In3 EQ Out2
B

197 Test là một khối gồm 2 bản copy của khối compare
© DHBK 2010
3.1 Giới thiệu về VHDL
Ví dụ 1
Thiết kế khối compare dùng mạch tổ hợp
Compare

A[0] XNOR
A
B[0]

A[1] AND
EQ
B[1] EQ

B
A[7]
B[7]

198
© DHBK 2010
4.1 Giới thiệu về VHDL
Ví dụ 1
• Thiết kế khối compare dùng VHDL
‘Entity’ xác định giao diện
với bên ngoài của khối cần thiết kế

- -Eight bit comparator


entity Compare is
port( A,B: in bit_vector(0 to 7);
EQ: out bit);
end Compare; đầu vào và ra được gọi là port
architecture Behav1 of Compare is
begin ‘Architecture’ miêu tả hoạt động
EQ <= ‘1’ when (A=B) else ‘0’; và cấu trúc bên trong của
end Behav1;
khối cần thiết kế

Chú ý:
-Một entity có thể có nhiều architecture, mỗi architecture là một
cách thể hiện khác nhau của cùng một chức năng
- Các Ports là vector có chiều: vào (in), ra (out), hoặc cả vào cả ra (inout)
199
© DHBK 2010
3.1 Giới thiệu về VHDL
Component và Instantiation
• Biểu diễn Test bằng VHDL

entity Test is
port( In1,In2,In3: in bit_vector(0 to 7);
Out1,Out2: out bit);
end Test;

architecture Struct1 of Test is


component Comparator is
port( X,Y: in bit_vector(0 to 7); 2 bản copy của cùng một
Z: out bit); component
end component; ‘Comparator’
begin
Compare1: Comparator port map (X=>In1, Y=>In2, Z=>Out1);
Compare2: Comparator port map (X=>In1,Y=>In3,Z=>Out2);
end Struct1;
Chú ý:
- Hai bản comparator chạy song song với nhau !!!

200
- Đây là architecture miêu tả cấu trúc của entity Test
© DHBK 2010
3.1 Giới thiệu về VHDL
Cấu hình (Configuration)
• Khi một entity có nhiều architectures, ta sẽ xử dụng architecture nào?
• Làm thế nào để gắn ‘Components’ với ‘Entities’?

-- Configuration information: architecture selection


-- and component-entity binding

configuration Build1 of Test is


for Struct1
for Compare1: Comparator use entity Compare(Behav1)
port map (A => X, B => Y, EQ => Z);
end for;
for others: Comparator use entity Compare(Behav1)
port map (A => X, B => Y, EQ => Z);
end for;
end for;
end Build1;

201
© DHBK 2010
3.1 Giới thiệu về VHDL
Khai báo Entity và Architecture
ENTITY:

entity Entity_name is
port(Signal_name: in Signal_type;
Signal_name: out Signal_type);
end Entity_name;

ARCHITECTURE:

architecture Architecture_name of Entity_name is


Khai báo các tín hiệu cục bộ;
Khai báo các components;
begin
Các câu lệnh;
202 end Architecture_name;
© DHBK 2010
3.1 Giới thiệu về VHDL
Khai báo component
COMPONENT:
component Component_name is
port( Signal_name: in Signal_type;
Signal_name: out Signal_type);
end component;

Khai báo copy của COMPONENT :

Instance_name: Component_name
port map (Signal_list);

Hoặc cách thứ 2, copy trực tiếp:

Instance_name: Entity_name(Architecture_name)
port map (Signal_list);
203
© DHBK 2010
3.1 Giới thiệu về VHDL
Khai báo cấu hình
CONFIGURATION:

configuration Config_name of Entity_name is


for Architecture_name
for Instance_name: Component_name use entity
Entity_name(Architecture_name)
port map (Signal_list);
end for;
end for;
end Config_name;

204
© DHBK 2010
3.1 Giới thiệu về VHDL
Ví dụ 2
• Biểu diễn cổng AND bằng VHDL
A
Y
B
C

-- 3-input AND gate

entity AND3 is
port ( A,B,C: in bit;
Y: out bit);
end AND3;

architecture RTL of AND3 is


begin
Y <= ‘1’ when ((A=‘1’) and (B=‘1’) and (C=‘1’)) else ‘0’;
end RTL;

205
© DHBK 2010
3.1 Giới thiệu về VHDL
Ví dụ 2
• Biểu diễn cổng OR bằng VHDL
A
Y
B
C

-- 3-input OR gate

entity OR3 is
port ( A,B,C: in bit;
Y: out bit);
end OR3;

architecture RTL of OR3 is


begin
Y <= ‘0’ when ((A=‘0’) and (B=‘0’) and (C=‘0’)) else ‘1’;
end RTL;

206
© DHBK 2010
3.1 Giới thiệu về VHDL
Ví dụ 2
• Biểu diễn cổng INV bằng VHDL

A Y

-- INV gate

entity INV is
port ( A: in bit;
Y: out bit);
end INV;

architecture RTL of INV is


begin
Y <= ‘1’ when (A=‘0’) else ‘0’;
end RTL;

207
© DHBK 2010
3.1 Giới thiệu về VHDL
Ví dụ 3
• Thiết kế bộ MUX 2-1 dùng VHDL

A
Y
B

S
entity MUX21 is
port ( A,B,S: in bit;
Y: out bit);
end MUX21;

architecture Behav of MUX21 is


begin
Y <= A when (S=‘1’) else B;
end Behav;

208
© DHBK 2010
3.1 Giới thiệu về VHDL
Ví dụ 3

A
Y
architecture Struct of MUX21 is B
signal U,V,W : bit;
component AND2 S
port ( X,Y: in bit;
Z: out bit);
end component;
component OR2 A
port ( X,Y: in bit; W
Z: out bit);
S Y
end component;
component INV U V
port ( X: in bit;
Z: out bit); B
end component;
begin
Gate1: INV port map (X=>S,Z=>U);
Gate2: AND2 port map (X=>A,Y=>S,Z=>W);
Gate3: AND2 port map (X=>U,Y=>B,Z=>V);
Gate4: OR2 port map (X=>W,Y=>V,Z=>Y);
209 end Struct;
© DHBK 2010
3.1 Giới thiệu về VHDL
Ví dụ 3
• Giả sử ta muốn sử dụng các cổng AND, OR và INV ở
ví dụ 2 trong MUX21

configuration Use3InputGates of MUX21 is


for Behav Entities
end for; A
Y
for Struct B
for Gate1:INV use entity INV(RTL) C
port map (A=>X,Y=>Z);
end for; A Y
for All:AND2 use entity AND3(RTL)
port map (A=>X,B=>Y,C=>’1’,Y=>Z);
end for;
for Gate4:OR2 use entity OR3(RTL) Components
port map (A=>X,B=>Y,C=>’0’,Y=>Z);
X
end for; Z
end for;
Y
end Use3InputGates;
X Z
210
© DHBK 2010
3.1 Giới thiệu về VHDL
Tạo Testbench
• Testbench là entity dùng để mô phỏng và kiểm tra thiết
kế
entity Testbench is A
end Testbench; Y
B MUX21

Testbench không có port S

architecture BehavTest of Testbench is


Signal In1,In2,Select,Out : bit;
begin
mux21_copy: entity MUX21(Behav) port map (In1, In2, Select, Out);
Thu: process is
begin
In1<=‘0’;In2<=‘1’;Select<=‘0’; wait for 20 ns;
Select<=‘1’; wait for 20 ns;
In1<=‘1’;In2<=‘0’; wait for 20 ns;
...
end process;
end BehavTest;

211
© DHBK 2010
3.1 Giới thiệu về VHDL
Sử dụng lại
• Thông thường, các bộ phận của thiết kế này có thể
được dùng lại trong các thiết kế khác
• Các sản phẩm công nghiệp thường bao gồm 95% bộ
phận dùng lại và chỉ có 5% là phải thiết kế mới
• VHDL khuyến khích dùng lại bằng khái niệm
‘Packages’
• Một ‘Package’ chứa các định nghĩa về hằng số, khai
báo component, các kiểu dữ liệu của người sử dụng,
các chương trình con viết bằng VHDL
• Package được cất ở trong ‘Library’: library thực chất
là một thư mục

212
© DHBK 2010
3.1 Giới thiệu về VHDL
Sử dụng lại
Package interface declaration:

package Package_name is
-- constants
-- user defined types
-- component declarations
-- sub programs
end Package_name;

How to use a package?

use Library_name.Package_name.all;

U1: entity Package_name.Entity_name(Architecture_name);

213
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Các kiểu dữ liệu đã được định nghĩa

package Standard is
type Bit is (‘0’,’1’);
type Boolean is (False, True);
type Character is (--ASCII set);
type Integer is range implementation_defined;
type Real is range implementation_defined;
type Bit_vector is (--array of bits);
type String is (--array of characters);
type Time is range implementation_defined;
end Standard;

Bit, Boolean và Character là kiểu dữ liệu liệt kê

214
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Các kiểu dữ liệu đã được định nghĩa

Ví dụ về khai báo các kiểu số nguyên:


type Year is range 0 to 99;
type Memory_address is range 65535 downto 0;

Ví dụ về khai báo các kiểu số thực


type Probability is range 0.0 to 1.0;
type Input_level is range -5.0 to 5.0;

Khai báo bit_vector, giá trị được đặt trong dấu nháy kép
constant State1: bit_vector(4 downto 0) := “00100”;

MSB, bit 4 LSB

Kiểu chuỗi String là một chuỗi ký tự characters; giá trị của nó


được đặt trong dấu nháy kép
constant Error_message: string
:= “Unknown error: ask your poor sysop for help”;
215
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Các kiểu dữ liệu đã được định nghĩa

ARCHITECTURE test OF test IS


ARCHITECTURE test OF test IS
BEGIN
SIGNAL a : REAL;
PROCESS(X)
BEGIN
VARIABLE a : INTEGER;
a <= 1.0; --Ok 1
BEGIN
a <= 1; --error 2
a := 1; --Ok 1
a <= -1.0E10; --Ok 3
a := -1; --Ok 2
a <= 1.5E-20; --Ok 4
a := 1.0; --error 3
a <= 5.3 ns; --error 5
END PROCESS;
END test;
END test;

216
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Các kiểu dữ liệu đã được định nghĩa

Kiểu Time là kiểu vật lý:


type Time is range implementation_defined
units
fs; Primary unit:
ps = 1000 fs; resolution limit
ns = 1000 ps;
us = 1000 ns;
ms = 1000 us; Secondary units
sec = 1000 ms;
min = 60 sec;
hr = 60 min;
end units;

Được sử dụng rất nhiều trong chạy mô phỏng


wait for 20 ns;
constant Sample_period: time := 2 ms;
constant Clock_period: time := 50 ns;

217
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Các kiểu dữ liệu của người sử dụng

Người sử dụng có thể định nghĩa kiểu vật lý như sau:


type Length is range 0 to 1E9
units Primary unit:
um; resolution limit
mm = 1000 um;
m = 1000 mm; Metric secondary units
km = 1000 m;
mil = 254 um;
inch = 1000 mil;
foot = 12 inch; Imperial secondary units
yard = 3 foot;
end units;

218
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Kiểu liệt kê của người sử dụng
Người sử dụng có thể định nghĩa kỉêu liệt kê như sau:

type FSM_states is (reset, wait, input, calculate, output);

constant reset: bit_vector := “10000”;


constant wait: bit_vector := “01000”;
constant input: bit_vector := “00100”;
constant calculate: bit_vector := “00010”;
constant output: bit_vector := “00001”;

219
© DHBK 2010 PACKAGE example IS
TYPE current IS RANGE 0 TO 1000000000
UNITS
na; --nano amps
ua = 1000 na; --micro amps
ma = 1000 ua; --milli amps
a = 1000 ma; --amps
END UNITS;
TYPE load_factor IS (small, med, big );
END example;

USE WORK.example.ALL;
ENTITY delay_calc IS
PORT ( out_current : OUT current;
load : IN load_factor;
delay : OUT time);
END delay_calc;
ARCHITECTURE delay_calc OF delay_calc IS
BEGIN
delay <= 10 ns WHEN (load = small) ELSE
delay <= 20 ns WHEN (load = med) ELSE
delay <= 30 ns WHEN (load = big) ELSE
delay <= 10 ns;
out_current <= 100 ua WHEN (load = small)ELSE
out_current <= 1 ma WHEN (load = med) ELSE
out_current <= 10 ma WHEN (load = big) ELSE
out_current <= 100 ua;
220
END delay_calc;
© DHBK 2010
4.2 Tín hiệu và các kiểu dữ liệu:
Kiểu mảng array

type 1D_array is array (1 to 10) of integer;


type 2D_array is array (5 downto 0, 1 to 10) of real;

TYPE data_bus IS ARRAY(0 TO 31) OF BIT;

VARIABLE X: data_bus;
VARIABLE Y: BIT;
Y := X(0);
Y := X(15);

221
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Kiểu bản ghi record
TYPE optype IS ( add, sub, mpy, div, jmp );

TYPE instruction IS
PROCESS(X)
RECORD
VARIABLE inst : instruction;
opcode : optype; VARIABLE source, dest : INTEGER;
src : INTEGER; VARIABLE operator : optype;
dst : INTEGER; BEGIN
END RECORD; source := inst.src; --Ok line 1
dest := inst.src; --Ok line 2
source := inst.opcode; --error line 3
operator := inst.opcode; --Ok line 4
inst.src := dest; --Ok line 5
inst.dst := dest; --Ok line 6
inst := (add, dest, 2); --Ok line 7
inst := (source); --error line 8
END PROCESS;
222
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Standard logic
library IEEE;
use IEEE.Std_logic_1164.All;

type std_logic is (
‘U’, -- uninitialized e.g. after power-up
‘X’, -- strongly driven unknown e.g. after setup violation
‘0’, -- strongly driven logic zero
‘1’, -- strongly driven logic one
‘Z’, -- high impedance e.g. not driven at all
‘W’, -- weakly driven unknown
‘L’, -- weakly driven logic zero
‘H’, -- weakly driven logic one
‘-’); -- don’t care

223
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Standard logic
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
PACKAGE memory IS
CONSTANT width : INTEGER := 3;
CONSTANT memsize : INTEGER := 7;
TYPE data_out IS ARRAY(0 TO width) OF std_logic;
TYPE mem_data IS ARRAY(0 TO memsize) OF data_out;
END memory;

LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE WORK.memory.ALL;
ENTITY rom IS
PORT( addr : IN INTEGER;
data : OUT data_out;
cs : IN std_logic);
END rom;

224
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Standard logic
ARCHITECTURE basic OF rom IS
CONSTANT z_state : data_out := (‘Z’, ‘Z’, ‘Z’, ‘Z’);
CONSTANT x_state : data_out := (‘X’, ‘X’, ‘X’, ‘X’);
CONSTANT rom_data : mem_data :=
( ( ‘0’, ‘0’, ‘0’, ‘0’),
( ‘0’, ‘0’, ‘0’, ‘1’),
( ‘0’, ‘0’, ‘1’, ‘0’),
( ‘0’, ‘0’, ‘1’, ‘1’),
( ‘0’, ‘1’, ‘0’, ‘0’),
( ‘0’, ‘1’, ‘0’, ‘1’),
( ‘0’, ‘1’, ‘1’, ‘0’),
( ‘0’, ‘1’, ‘1’, ‘1’) );
BEGIN
ASSERT addr <= memsize
REPORT “addr out of range”
SEVERITY ERROR;
data <= rom_data(addr) AFTER 10 ns WHEN cs = ‘1’ ELSE
data <= z_state AFTER 20 ns WHEN cs = ‘0’ ELSE
data <= x_state AFTER 10 ns;
END basic;
225
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Gán tín hiệu
• Gán theo vị trí, không gán theo chỉ số của mảng

signal Down: std_logic_vector (3 downto 0);


signal Up: std_logic_vector (0 to 3);

Up <= Down;

Phương án nào sau đây là phương án đúng?

Up(0) Down(3) Up(0) Down(0)

Up(1) Down(2) Up(1) Down(1)


OR
Up(2) Down(1) Up(2) Down(2)

Up(3) Down(0) Up(3) Down(3)

226 Tương ứng theo vị trí


© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Gán tín hiệu
• Có thể gán biến cho một phần của mảng
• Chú ý chiều (to hoặc downto) phải giống nhau trong
các phép khai báo
signal Bus: std_logic_vector (7 downto 0);
signal A: std_logic_vector (0 to 3);

Câu lệnh nào sau đây đúng?


Bus(0 to 3) <= A; Chiều bus không giống như khai báo
Bus <= A; Kích thứơc mảng khác nhau
Bus(3 downto 0) <= A; OK! Bus(3) is driven by A(0)

Bus(5 downto 4) <= A(0 to 1); OK! Bus(5) is driven by A(0)

227
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Gán tín hiệu
• Có thể kết hợp hai mảng khác nhau

signal Byte_bus: std_logic_vector(7 downto 0);


signal Nibble_busA, Nibble_busB: std_logic_vector(3 downto 0);

Byte_bus <= Nibble_busA & Nibble_busB;

Nibble_busA(3)
Nibble_busA(2)
Byte_bus(7) Nibble_busA(1)
Byte_bus(6) Nibble_busA(0)
Byte_bus(5)
Byte_bus(4)
Byte_bus(3)
Byte_bus(2)
Byte_bus(1) Nibble_busB(3)
Byte_bus(0) Nibble_busB(2)
Nibble_busB(1)
Nibble_busB(0)

228
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Gán tín hiệu
• Not supported by all synthesis tools!!

signal X,Y,Z,T: std_logic_vector(3 downto 0);


signal A,B,C: std_logic;

X <= (A,B,C,C); -- correspondence by position

Y <= (3 => A, 1 downto 0 => C, 2 => B);

Z <= (3 => A, 2 => B, others => C);

T <= (others => ‘0’); -- initialization irrespective of width of T

229
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Tham số dùng chung (Generic)
•Dùng để truyền tham số từ entity tới các bản copy của nó
•Các bản copy có thể có các giá trị tham số khác nhau

entity General_mux is
generic (width : integer);
port ( Input : in std_logic_vector (width - 1 downto 0);
Select : in integer range 0 to width - 1;
Output : out std_logic);
end General_mux;

230
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Tham số dùng chung (Generic)
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;

ENTITY test IS
GENERIC(rise, fall : TIME; load : INTEGER);
PORT ( ina, inb, inc, ind : IN std_logic;
out1, out2 : OUT std_logic);
END test;

ARCHITECTURE test_arch OF test IS


COMPONENT AND2
GENERIC(rise, fall : TIME; load : INTEGER);
PORT ( a, b : IN std_logic;
c : OUT std_logic);
END COMPONENT;
BEGIN
U1: AND2 GENERIC MAP(10 ns, 12 ns, 3 )
PORT MAP (ina, inb, out1 );
U2: AND2 GENERIC MAP(9 ns, 11 ns, 5 )
PORT MAP (inc, ind, out2 );
END test_arch;

231
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Tham số dùng chung (Generic)
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
ENTITY test IS
GENERIC(rise, fall : TIME;
GENERIC(load : INTEGER);
PORT ( ina, inb, inc, ind : IN std_logic;
PORT ( out1, out2 : OUT std_logic);
END test;
ARCHITECTURE test_arch OF test IS
COMPONENT and2
GENERIC(rise, fall : TIME := 10 NS;
GENERIC(load : INTEGER := 0);
PORT ( a, b : IN std_logic;
PORT ( c : OUT std_logic);
END COMPONENT;
BEGIN
U1: and2 GENERIC MAP(10 ns, 12 ns, 3 )
PORT MAP (ina, inb, out1 );
U2: and2 PORT MAP (inc, ind, out2 );
END test_arch;

232
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Thuộc tính (attributes)
TYPE state IS (0 TO 7);

4 thuộc tính (attributes) được định nghĩa sẵn

_ T’LEFT, which returns the left bound of a type or subtype


_ T’RIGHT, which returns the right bound of a type or subtype
_ T’HIGH, which returns the upper bound of a type or subtype
_ T’LOW, which returns the lower bound of a type or subtype

PROCESS(x)
SUBTYPE smallreal IS REAL RANGE -1.0E6 TO 1.0E6;
VARIABLE q : real;
BEGIN
q := smallreal’LEFT; -- use of ’left returns 1.0E6
END PROCESS;

233
© DHBK 2010
3.2 Tín hiệu và các kiểu dữ liệu:
Thuộc tính (attributes)

PROCESS(a)
TYPE bit4 IS ARRAY(0 TO 3) of BIT;
TYPE bit_strange IS ARRAY(10 TO 20) OF BIT;
VARIABLE len1, len2 : INTEGER;
BEGIN
len1 := bit4’LENGTH; -- returns 4
len2 := bit_strange’LENGTH; -- returns 11
END PROCESS;

Xem “VHDL programming by example” để biết chi tiết hơn về thuộc tính
234
© DHBK 2010
3.3 Các phép toán số học và logic:
Các phép toán logic
• Các phép toán logic gồm: not, and, or, xor, nand, nor
• Thứ tự ưu tiên:
 ‘not’ được thực hiện đầu tiên
 Các lệnh còn lại có cùng mức ưu tiên
• Các phép toán logic được dùng cho các kiểu dữ liệu
sau : bit, bit_vector, boolean, std_logic,
std_logic_vector, std_ulogic, std_ulogic_vector
• Các phép toán logic có thể được dùng với mảng:
 Các mảng phải có cùng kích thước
 Các phần tử của mảng tương ứng theo vị trí

235
© DHBK 2010
3.3 Các phép toán số học và logic:
Các phép toán logic

library IEEE;
use IEEE.Std_Logic_1164.All;

entity Gate is
port( A,B,C: in std_logic;
Z: out std_logic);
end Gate;

architecture Logical of Gate is


begin
Z <= A and not(B or C);
end Logical;

236
© DHBK 2010
3.3 Các phép toán số học và logic:
Các phép toán logic

library IEEE;
use IEEE.Std_Logic_1164.All;

entity Gate is
generic(width : integer range 0 to 31);
port( A,B,C: in std_logic_vector(width-1 downto 0);
Z: out std_logic_vector(width-1 downto 0));
end Gate;

architecture Logical of Gate is


begin
Z <= A and not(B or C);
end Logical;

237
© DHBK 2010
Các phép toán số học và logic:
Các phép toán so sánh
• Các phép toán so sánh gồm: <, <=, =>, >, =, /=
• Kết quả của phép so sánh là kiểu boolean
• Hai toán hạng phải có cùng kiểu dữ liêụ
• Phép toán so sánh có thể được dùng cho mảng
 Mảng có thể có kích thước khác nhau!
 Các phần tử của mảng sẽ được so sánh từng bit, từ trái
sang phải
 Bit vector có thể được so sánh nếu như có cùng độ dài và
khi so sánh giá trị của bit vector sẽ được đổi sang số nguyên
không dấu để so sánh

238
Các phép toán so sánh
© DHBK 2010

library IEEE
use IEEE.Std_Logic_1164.All;
What is the
entity Compare is
value of Z?
port( A: in std_logic_vector(3 downto 0);
B: in std_logic_vector(0 to 4); TRUE?
Z: out boolean);
end Compare; FALSE?

architecture Relational of Compare is


begin 1110
Z <= TRUE when A<B else FALSE; is compared to
end Relational; 1011
by bit position
entity Testbench from left to
end entity Testbench; right;
in the 2nd
architecture Build1 of Testbench is position
signal A: std_logic_vector(3 downto 0) := “1110”; A(2) > B(1)
signal B: std_logic_vector(0 to 4) := “10111”; hence (A<B)
signal Z: boolean; is FALSE
begin
DUT: entity Compare(Relational)
port map (A => A, B => B, Z => Z);
end Build1;
239
© DHBK 2010
3.3 Các phép toán số học và logic:
Các phép toán số học
• Các phép toán số học gồm: +, -, *, /, ** (exponential),
abs (absolute value), mod (modulus), rem (remainder)
• Các phép toán này dùng cho kiểu số nguyên và số
thực (trừ mod and rem không dùng cho số thực),
không dùng cho kiểu bit vector
• Cả hai toán hạng phải có cùng kiểu dữ liệu và có thể
có dải giá trị khác nhau
• Một biến kiểu vật lý (ví dụ: time) có thể nhân với một
số nguyên và một số thực, kết quả sẽ vẫn là kiểu vật

240
© DHBK 2010
3.3 Các phép toán số học và logic:
Các phép toán số học
entity Add is
port ( A,B: in integer range 0 to 7;
Z: out integer range 0 to 14);
end Add;

architecture Behav of Add is


begin
Z <= A + B;
end Behav;

241
© DHBK 2010
3.4 Các lệnh song song và tuần tự :
Các lệnh song song
• Tất cả các lệnh trong architecture đều được thực hiện
đồng thời (song song) giống như trong phần cứng,
các cổng logic hoạt động đồng thời

entity Concurrent is
port ( A,B,C,D: in std_logic; Schematic:
Y,Z: out std_logic);
end Concurrent;
A
architecture Struct of Concurrent is Y
B
begin
NAND1: entity NAND2 port map (A,B,Y); C
Z
NAND2: entity NAND2 port map (C,D,Z); D
end Struct;

Mạch sẽ hoạt động thế nào nếu như khai báo NAND1 sau NAND2?
242
© DHBK 2010
3.4 Các lệnh song song và tuần tự :
Các lệnh song song

entity Concurrent is
port ( A,B,C,D: in std_logic; Schematic:
Y,Z: out std_logic);
end Concurrent;
A
architecture Struct of Concurrent is Y
B
begin
NAND2: entity NAND2 port map (C,D,Z); C
Z
NAND1: entity NAND2 port map (A,B,Y); D
end Struct;

Mạch vần hoạt động như cũ!!!


243
© DHBK 2010
3.4 Các lệnh song song và tuần tự :
Các lệnh song song

Schematic:

A
entity Concurrent is B T1
port ( A,B, D: in std_logic;
Z: out std_logic); Z
end Concurrent; D

architecture Struct of Concurrent is


signal T1: std_logic;
begin
NAND2: entity NAND2 port map (T1,D,Z);
NAND1: entity NAND2 port map (A,B,T1);
end Struct;

244
© DHBK 2010
3.4 Các lệnh song song và tuần tự :
Process
• Nhiều khi phương trình tổ hợp trong một câu lệnh rất
phức tạp, ví dụ:
A
B
C
D Y

entity Complex is E
port( A,B,C,D,E,F,G,H,I,J: F S
in std_logic; G
Y,Z: out std_logic);
H
end Complex;
I Z
architecture Struct of Complex is J
begin
Y <= ((A nand B) nand (C nand D))
when (S = ‘1’) else
((E nand F) nand (G nand H));
Z <= I nand J;
end Struct;
245
© DHBK 2010
3.4 Các lệnh song song và tuần tự :
Process
• Vì vậy người ta đưa ra khái niệm process:
 Một process hoạt động như một câu lệnh và được thực hiện
đồng thời với các câu lệnh khác
 Bên trong một process, các lệnh được thực hiện tuần tự
theo thứ tự từ trên xuống dưới. Điều này giúp cho việc chia
nhỏ một lệnh rất phức tạp thành các lệnh đơn giản hơn.
 Để truyền dữ liệu giữa các lệnh trong một process chúng ta
có thể dùng các biến tạm thời variables. Các biến này không
nhất thiết phải là một tín hiệu vật lý nào.
 process sẽ được thực hiện lại mỗi khi có một sự kiện
(event) xảy ra đối với một tín hiệu (signal) nào đó trong danh
sách tín hiệu nhạy cảm (sensitivity list).

246
© DHBK 2010
3.4 Các lệnh song song và tuần tự :
Process
Cú pháp khai báo Process:

Process_name: process (sensitivity_list) is


-- variable declarations;
begin
-- sequential commands
end process;

Cú pháp khai báo biến:

variable Variable_name: type;

Cú pháp gán giá cho một biến:

Variable_name := expression;

Gán biến  :=
247
Gán tín hiệu  <=
© DHBK 2010
3.4 Các lệnh song song và tuần tự :
Process
entity Complex is
port( A,B,C,D,E,F,G,H,I,J: T1 and T2 have no
in std_logic; physical meaning since
Y,Z: out std_logic); each refers to 2 different
end Complex; physical wires

architecture Struct of Complex is T1 T2


Sensitivity list
begin
Y_process: process (A,B,C,D,E,F,G,H,S) is
variable T1,T2: std_logic;
begin
if (S=‘1’) then A
T1 := A nand B; B
T2 := C nand D; C
else Y
T1 := E nand F; D
T2 := G nand H; E
end if; F S
Y <= T1 nand T2; G
end process;
Z <= I nand J; H
end Struct; I Z
248 J
© DHBK 2010
3.4 Các lệnh song song và tuần tự :
Process
• Xét ví dụ sau:

Example: process (A,B,M) is


begin
Y <= A; Giá trị M cũ !!! M chỉ nhận
M <= B;
Z <= M;
giá trị mới ở cuối process
end process;
1. Giả thiết B có giá trị mới B’
2. Process Example được thực hiện. Các tín hiệu ra được ghi nhớ:
Y’ <= A; M’ <= B’; Z’ <= M;
3. Process Example tạm dừng
Y, M và Z nhận các giá trị mới Y’, M’, Z’.
4. Vì M ở trong sensitivity list, process Example lại đựơc thực hiện
5. Process Example thực hiện: Y” <= A; M” <= B’; Z” <= M’;
6. Y, M and Z nhận giá trị mới Y”, M”, Z”.

249
7. Không có tín hiệu nào trong sensitivity list thay đổi, process dừng
© DHBK 2010

3.5 Các cấu trúc tuần tự


• Các cấu trúc tuần tự chỉ được sử dụng trong process và chương trình con!!!
• Các cấu trúc tuần tự bao gồm: IF, CASE, FOR, NEXT, EXIT, WAIT, ASSERT

IF statement:

if condition then
-- sequential statements multiple IF statements:
else
-- sequential statements if condition1 then
end if; -- sequential statements
elsif condition2 then
-- sequential statements
elsif condition3 then
-- sequential statements
else
-- sequential statements
end if;
250
© DHBK 2010

3.5 Các cấu trúc tuần tự


case Expression is
when Value_1 =>
-- sequential statements
when Value_2 =>
-- sequential statements
-- etc.
end case;
Yêu cầu:
1. Tất cả các trường hợp
Example: process (A,B,C,X) is phải đựơc xác định
begin
case X is 2. Giá trị value phải là hằng số
when 0 to 4 => và phải biết khi thiết kế
Z <= B;
when 5 => 3. Giá trị phải có cùng kiểu với
Z <= C;
when 7 | 9 => expression
Z <= A;
when others =>
Z <= ‘0’;
end case;
end process;
251
© DHBK 2010

3.5 Các cấu trúc tuần tự

TYPE vectype IS ARRAY(0 TO 1) OF BIT;


VARIABLE bit_vec : vectype;

CASE bit_vec IS
WHEN “00” =>
RETURN 0;
WHEN “01” =>
RETURN 1;
WHEN “10” =>
RETURN 2;
WHEN “11” =>
RETURN 3;
END CASE;
252
© DHBK 2010

3.5 Các cấu trúc tuần tự

for I in 0 to 3 loop
-- sequential statements
end loop;

Chú ý:
1. Không được khai báo biến dùng trong vòng for và không
được gán giá trị cho biến đó

FOR i IN 1 to 10 LOOP
i_squared(i) := i * i;
END LOOP;

253
© DHBK 2010

3.5 Các cấu trúc tuần tự

entity General_mux is
generic (width : integer);
port ( Input : in std_logic_vector (width - 1 downto 0);
Select : in integer range 0 to width - 1;
Output : out std_logic);
end General_mux;

architecture Behav of General_mux is


begin
Selector: process (Input, Select) is
begin
for I in 0 to width-1 loop
if Select=I then
Output <= Input(I);
end if;
end loop;
end process;
end Behav;

254
© DHBK 2010

3.5 Các cấu trúc tuần tự


PROCESS(A, B)
CONSTANT max_limit : INTEGER := 255;
BEGIN
FOR i IN 0 TO max_limit LOOP
IF (done(i) = TRUE) THEN
NEXT;
ELSE
done(i) := TRUE;
END IF;
q(i) <= a(i) AND b(i);
END LOOP;
END PROCESS;

Cấu trúc NEXT được dùng để bỏ qua vòng lặp hiện tại để nhảy tới
vòng lặp tiếp theo
255
© DHBK 2010

3.5 Các cấu trúc tuần tự


PROCESS(a)
variable int_a : integer;
BEGIN
int_a := a;
FOR i IN 0 TO max_limit LOOP
IF (int_a <= 0) THEN -- less than or
EXIT; -- equal to
ELSE
int_a := int_a -1;
q(i) <= 3.1416 / REAL(int_a * i); -- signal
END IF; -- assign
END LOOP;
y <= q;
END PROCESS;

Cấu trúc EXIT được dùng để thoát ra khỏi vòng lặp for
256
© DHBK 2010

3.5 Các cấu trúc tuần tự

PROCESS
BEGIN
WAIT UNTIL clock = ‘1’ AND clock’EVENT;
q <= d;
END PROCESS;

PROCESS
BEGIN
WAIT ON a; -- 1.
.
WAIT ON b; -- 2.
.
END PROCESS;

WAIT FOR 10 ns;


WAIT FOR ( a * ( b + c ));

257
© DHBK 2010
3.5 Các cấu trúc tuần tự
Biến (Variables)
• Variable chỉ được sử dụng trong process hoặc
chương trình con
• Giá trị của variable được cập nhật ngay lập tức trong
khi giá trị của tín hiệu signal sẽ được cập nhật khi
process kết thúc
• Biến và tín hiệu có thể đựơc gán giá trị cho nhau
• Biến chỉ được dùng tạm thời trong process, nếu giá trị
của nó muốn được truy cập ngoài process ta phải gán
giá trị đó cho một tín hiệu

258
© DHBK 2010
3.5 Các cấu trúc tuần tự
Biến (Variables)
• LIBRARY IEEE;
• USE IEEE.std_logic_1164ALL;

• ENTITY mux IS
• PORT (I0, I1, I2, I3, A, B : IN std_logic;
Q : OUT std_logic);
Tìm chỗ sai trong đoạn mã !
• END mux;

• ARCHITECTURE mux_behave OF mux IS


• SIGNAL sel : INTEGER RANGE 0 TO 3;
• BEGIN
• B : PROCESS(A, B, I0, I1, I2, I3)
• BEGIN
• sel <= 0;
• IF (A = ‘1’) THEN sel <= sel + 1; END IF;
• IF (B = ‘1’) THEN sel <= sel + 2; END IF;
• CASE sel IS
• WHEN 0 =>
• Q <= I0;
• WHEN 1 =>
• Q <= I1;
• WHEN 2 =>
• Q <= I2;
• WHEN 3 =>
• Q <= I3;
• END CASE;
• END PROCESS;
• END mux_behave;

259
© DHBK 2010
3.5 Các cấu trúc tuần tự
Biểu diễn sườn lên của xung nhịp
Tìm chức năng của entity What
entity What is
port (D,Clk: in std_logic;
Q: out std_logic);
end What; Since there is no ELSE part
the previous Q value has
architecture RTL of What is to be remembered for the case
begin where Clk=‘0’.
process (D, Clk) is The synthesis tool will hence
begin infer a latch instead of just
if (Clk=‘1’) then combinatorial logic!!!
Q <= D;
end if; Beware of unintended latches
end process; when ELSE parts are omitted
end RTL;
With a latch, not with a D-flip-flop!!

When a Clk-event occurs and Clk is low, nothing happens


When a Clk-event occurs and Clk is high, the D input is copied
to the Q output
When a D-event occurs and Clk is high, the D input is copied to
260 the Q output => hence a latch: when Clk is high, Q follows D
© DHBK 2010
3.5 Các cấu trúc tuần tự
Biểu diễn sườn lên của xung nhịp

Cách 1: WAIT UNTIL

entity DFlipFlop is
port (D,Clk: in std_logic;
Q: out std_logic);
end DFlipFlop;

architecture RTL of DFlipFlop is


begin This is not synthesisable
process is
begin
wait until Clk’event and Clk=‘1’;
Q <= D;
end process;
end RTL;

261
© DHBK 2010
3.5 Các cấu trúc tuần tự
Biểu diễn sườn lên của xung nhịp
Cách 2: Sensitivity list

entity DFlipFlop is
port (D,Clk: in std_logic;
Q: out std_logic);
end entity DFlipFlop;

architecture RTL of DFlipFlop is


begin
process (D,Clk) is
begin
if (Clk’event and Clk=‘1’) then
Q <= D;
end if;
end process;
end architecture RTL;

262
© DHBK 2010
3.5 Các cấu trúc tuần tự
Biểu diễn Finite State Machine
Start=0

Wait
Reset Start=1 00
Out Output Start=1
Up=1 Up=0
put
logic
Up1 Down3
State 01 11
Reg CurrentState

Next NextState Up2 Down2


state 10 10
logic

Up3 Down1
11 01
Start Up

263
© DHBK 2010
3.5 Các cấu trúc tuần tự
Biểu diễn Finite State Machine
Start=0
entity FSM is
port ( Start, Up, Reset, Clk: in std_logic; Wait
Output: out std_logic_vector(0 to 1)); 00
Start=1 Start=1
end FSM;
Up=1 Up=0
architecture Behav of FSM is
type FSM_States is (Wait,Up1,Up2,Up3,Down1,Down2,Down3); Up1 Down3
signal CurrentState, NextState : FSM_States; 01 11
begin
OutputLogic:
process(CurrentState) is
end process; Up2 Down2
NextStateLogic: 10 10
process(CurrentState,Start,Up) is

end process;
StateRegister: Up3 Down1
process(NextState,Clk,Reset) is 11 01

end process;
end Behav;
264
© DHBK 2010
3.5 Các cấu trúc tuần tự
Biểu diễn Finite State Machine
Start=0

Wait
Start=1 00 Start=1
OutputLogic:
process(CurrentState) is Up=1 Up=0
begin
case CurrentState is Up1 Down3
when Wait => 01 11
Output <= “00”;
when Up1|Down1 =>
Output <= “01”;
when Up2|Down2 => Up2 Down2
Output <= “10”; 10 10
when Up3|Down3 =>
Output <= “11”;
end case;
end process; Up3 Down1
11 01

265
© DHBK 2010
3.5 Các cấu trúc tuần tự
Biểu diễn Finite State Machine
NextStateLogic:
process(CurrentState,Start,Up) is Start=0
begin
case CurrentState is
Wait
when Wait =>
Start=1 00 Start=1
if (Start=‘0’) then
NextState <= Wait; Up=1 Up=0
elsif (Up=‘1’) then
NextState <= Up1; Up1 Down3
else 01 11
NextState <= Down3;
end if;
when Up1 =>
NextState <= Up2; Up2 Down2
when Up2 => 10 10
NextState <= Up3;
when Up3|Down1 =>
NextState <= Wait;
when Down3 => Up3 Down1
NextState <= Down2; 11 01
when Down2 =>
NextState <= Down1;
end case;
266 end process;
© DHBK 2010
3.5 Các cấu trúc tuần tự
Biểu diễn Finite State Machine
Start=0

Wait
Start=1 00 Start=1
Up=1 Up=0
StateRegister:
process(NextState,Clk,Reset) is Up1 Down3
begin 01 11
if Reset=‘1’ then
CurrentState <= Wait;
elseif (Clk’event and Clk=‘1’) then
CurrentState <= NextState; Up2 Down2
end if; 10 10
end process;

Up3 Down1
11 01

267
© DHBK 2010

3.6 Chương trình con


• Gồm 2 loại: thủ tục (procedure) và hàm (function)
• Thủ tục: có thể trả về nhiều giá trị, có thể có các biến
số là các tín hiệu vào, ra hoặc vào\ra
• Hàm: chỉ trả về một giá trị và tất cả các biến số chỉ có
thể là tín hiệu vào
• Thủ tục và hàm có 2 loại: song song và tuần tự. Thủ
tục hoặc hàm song song được đặt ngoài process và
thủ tục hoặc hàm tuần tự chỉ được đặt trong process
• Các lệnh trong chương trình con được thực hiện tuần
tự giống như process

268
© DHBK 2010

3.6 Chương trình con


• ENTITY convert IS
• PORT(I1 : IN log8; --line 2
O1 : OUT INTEGER); --line 3
• END convert;
• ARCHITECTURE behave OF convert IS
• FUNCTION vector_to_int(S : log8) --line 4
• RETURN INTEGER is --line 5
• VARIABLE result : INTEGER := 0; --line 6
• BEGIN
• FOR i IN 0 TO 7 LOOP --line 7
• result := result * 2; --line 8
• IF S(i) = ‘1’ THEN --line 9
• result := result + 1; --line 10
• END IF;
• END LOOP;
• RETURN result; --line 11
• END vector_to_int;
• BEGIN
• O1 <= vector_to_int(I1); --line 12
• END behave;
269
© DHBK 2010

3.6 Chương trình con


Ví dụ khai báo thủ tục:

procedure increment_reg (variable reg : inout word_32; constant


incr : in integer := 1);

Ví dụ gọi thủ tục:

increment_reg(index_reg, offset–2); -- add value to index_reg


increment_reg(prog_counter); -- add 1 (default) to prog_counter

270

You might also like