n A simple FPGA model n Full-adder realization n Demos FPGA Circuits.
Fpga practice full
Transcript of Fpga practice full
VIETNAM NATIONAL UNIVERSITY, HANOIVIETNAM NATIONAL UNIVERSITY, HANOI
College of TechnologyCollege of Technology
Apr 12, 2023
FPGA Design Course
Part 2: Practice – Working with ModelSim
Nguyen Ngoc Mai
Tran Van Huan
Researchers, SIS Laboratory – Coltech (VNU)
http://www.coltech.vnu.vn/sis
Apr 12, 2023April 12, 2023
2
Topics
1. ModelSim Quick Guide• Introduction• Getting start with Projects
2. Practice:• Lab 1: Design a 2-to-1 Multiplexer
Apr 12, 2023April 12, 2023
3
Goals
• Be familiar with using ModelSim• Be able to program some basic digital circuits by VHDL
Apr 12, 2023April 12, 2023
4
Section 1: ModelSim Quick Guide
1. Introduction2. Getting start with Projects
Apr 12, 2023April 12, 2023
5
Introduction to ModelSim
• (C) Mentor Graphics• A Simulator and Debugger HDL Tool• Support: VHDL, Verilog, SystemC, …• Docs (pdf):
• User’s Guide• Reference Manual
Apr 12, 2023April 12, 2023
6
Getting start with ModelSim Projects
Apr 12, 2023
1 – Creating a new project
Launch ModelSim Create new folder for each project
Command: mkdir <folder_name> Example: mkdir counter_demo
Select File > New > Project Enter Project Name OK
April 12, 2023
7
Apr 12, 2023
2 – Adding Items to the project
Example: Add counter.vhd file to the project
April 12, 2023
8
Apr 12, 2023
3 – Compiling the project
GUI: Select Compile > Compile All or
April 12, 2023
9
Apr 12, 2023
4 – Simulating the design
GUI: Select Simulate > Start Simulation… or Double-click on Library.work.counter.only or Command: vsim counter.vhd
April 12, 2023
10
Apr 12, 2023
4 – Simulating the design (cnt.)
Add All signals to wave: GUI: Right-click on Objects window > Add to wave >
Signals in Design Command: add wave –r /*
Create wave for a signal
Modify/Edit Waveform
Run Simulation
April 12, 2023
11
Apr 12, 2023
5 – Analyzing Waveforms
Add cursor
April 12, 2023
12
Apr 12, 2023
Section 2: Practice
A 8-bit wide 2-to-1 Multiplexer
See Laboratory Exercise 1.doc
April 12, 2023
13
Apr 12, 2023Apr 12, 2023
14
Các bước thiết kế
Tổng kết
Hợp kênh 8-bit 2 lối vào dữ liệu
Hợp kênh 3-bit 5 lối vào dữ liệu
Bộ cộng Ripple-Carry Adder
Apr 12, 2023Apr 12, 2023
15
Các bước thiết kế
Thiết kế mạch tổ hợp:
- Bảng trị các lối vào-ra (bảng chân lý)
- Lập phương trình logic
- Rút gọn phương trình (pp đại số, Karnaugh …)
- Mô tả mạch (hình vẽ, ngôn ngữ HDL …)
Ví dụ: các mạch mã hóa, giải mã, hợp kênh, phân kênh, bộ cộng, bộ so sánh …
Thiết kế mạch dãy:
- Xây dựng đồ hình trạng thái
- Tối thiểu hóa trạng thái
- Mô tả mạch (hình vẽ, ngôn ngữ HDL …)
Ví dụ: các mạch đếm, bộ ghi dịch, giám sát dữ liệu nối tiếp, các mạch điều khiển dạng máy trạng thái hữu hạn …
(mạch đếm & mạch nhớ là các mạch dãy đơn giản)
Thiết kế hệ thống số:
- Xác định các lối vào-ra
- Mô tả trực tiếp hoạt động của hệ thống
- Phân chia hệ thống thành các mạch số đơn giản
- 2 loại mạch số: mạch tổ hợp và mạch dãy
Apr 12, 2023Apr 12, 2023
16
Các bước thiết kế
Tổng kết
Hợp kênh 8-bit 2 lối vào dữ liệu
Hợp kênh 3-bit 5 lối vào dữ liệu
Bộ cộng Ripple-Carry Adder
Apr 12, 2023Apr 12, 2023
17
LabEx1 Part1
m <= (NOT (s) AND x) OR (s AND y);
Apr 12, 2023Apr 12, 2023
18
1. library ieee;
2. use ieee.std_logic_1164.all;
3. entity mux8bit2to1 is
4. port(
5. < … >
6. );
7. end mux8bit2to1;
8. architecture behavior of mux8bit2to1 is
9. begin
10. < … >
11. end behavior;
LabEx1 Part1
Apr 12, 2023Apr 12, 2023
19
LabEx1 Part1
1. architecture behavior of mux8bit2to1 is
2. begin
3. < … >
4. end behavior;
Apr 12, 2023Apr 12, 2023
20
LabEx1 Part1
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity mux8bit2to1 is4. port(5. X,Y : in std_logic_vector(7 downto 0);6. s : in std_logic;7. M : out std_logic_vector(7 downto 0)8. );9. end mux8bit2to1;
10. architecture behavior of mux8bit2to1 is11. begin12. M(7) <= (NOT (s) AND X(7)) OR (s AND Y(7));13. M(6) <= (NOT (s) AND X(6)) OR (s AND Y(6));14. M(5) <= (NOT (s) AND X(5)) OR (s AND Y(5));15. M(4) <= (NOT (s) AND X(4)) OR (s AND Y(4));16. M(3) <= (NOT (s) AND X(3)) OR (s AND Y(3));17. M(2) <= (NOT (s) AND X(2)) OR (s AND Y(2));18. M(1) <= (NOT (s) AND X(1)) OR (s AND Y(1));19. M(0) <= (NOT (s) AND X(0)) OR (s AND Y(0));20. end behavior;
Apr 12, 2023Apr 12, 2023
21
Các bước thiết kế
Tổng kết
Hợp kênh 8-bit 2 lối vào dữ liệu
Hợp kênh 3-bit 5 lối vào dữ liệu
Bộ cộng Ripple-Carry Adder
Apr 12, 2023Apr 12, 2023
22
Apr 12, 2023Apr 12, 2023
23
LabEx1 Part2
Apr 12, 2023Apr 12, 2023
24
LabEx1 Part2
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity mux1bit5to1 is4. port(5. u,v,w,x,y : in std_logic;6. s2,s1,s0 : in std_logic;7. m : out std_logic8. );9. end mux1bit5to1;
10. architecture behavior of mux1bit5to1 is11. signal m2,m1,m0: std_logic;12. begin13. m2 <= (NOT (s0) AND u) OR (s0 AND v);14. m1 <= (NOT (s0) AND w) OR (s0 AND x);15. m0 <= (NOT (s1) AND m2) OR (s1 AND
m1);16. m <= (NOT (s2) AND m0) OR (s2 AND y);17. end behavior;
m2
m1
m0
Apr 12, 2023Apr 12, 2023
25
LabEx1 Part2
000001010011100
000001010011100
000001010011100
M(2)
M(1)
M(0)
U(2)V(2)W(2)
Y(2)X(2)
U(1)V(1)W(1)
Y(1)X(1)
U(0)V(0)W(0)
Y(0)X(0)
s0 s1 s2
Apr 12, 2023Apr 12, 2023
26
LabEx1 Part2
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity mux3bit5to1 is4. port(5. U,V,W,X,Y : in std_logic_vector(2 downto 0);6. s2,s1,s0 : in std_logic;7. M : out std_logic_vector(2 downto 0)8. );9. end mux3bit5to1;
10. architecture structure of mux3bit5to1 is11. component mux1bit5to112. port(13. u,v,w,x,y : in std_logic;14. s2,s1,s0 : in std_logic;15. m : out std_logic16. );17. end component;18. begin19. mux_2: mux1bit5to1 port map (U(2),V(2),W(2),X(2),Y(2),s2,s1,s0,M(2));20. mux_1: mux1bit5to1 port map (U(1),V(1),W(1),X(1),Y(1),s2,s1,s0,M(1));21. mux_0: mux1bit5to1 port map (U(0),V(0),W(0),X(0),Y(0),s2,s1,s0,M(0));22. end structure;
Apr 12, 2023Apr 12, 2023
27
Các bước thiết kế
Tổng kết
Hợp kênh 8-bit 2 lối vào dữ liệu
Hợp kênh 3-bit 5 lối vào dữ liệu
Bộ cộng Ripple-Carry Adder
Apr 12, 2023Apr 12, 2023
28
LabEx2 Part1
Apr 12, 2023Apr 12, 2023
29
LabEx2 Part1
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity full_adder is4. port(5. a,b,ci : in std_logic;6. co,s : out std_logic7. );8. end full_adder;
9. architecture behavior of full_adder is10. signal ab : std_logic;11. begin12. ab <= a xor b;13. s <= ab xor ci;14. co <= (NOT (ab) AND b) OR (ab AND
ci);15. end behavior;
Apr 12, 2023Apr 12, 2023
30
LabEx2 Part1
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity RCA_4bit is4. port(5. A,B : in std_logic_vector(3 downto 0);6. cin : in std_logic;7. cout : out std_logic;8. S : out std_logic_vector(3 downto 0)9. );10. end RCA_4bit;
11. architecture structure of RCA_4bit is12. signal c3,c2,c1 : std_logic;13. component full_adder14. port(15. a,b,ci : in std_logic;16. co,s : out std_logic17. );18. end component;19. Begin -- <….>
Apr 12, 2023Apr 12, 2023
31
LabEx2 Part1
19. begin20. FA_0: full_adder port map( a => A(0),21. b => B(0),22. ci => cin,23. co => c1,24. s => S(0)25. );26. FA_1: full_adder port map ( a => A(1),27. b => B(1),28. ci => c1,29. co => c2,30. s => S(1)31. );32. FA_2: full_adder port map ( a => A(2),33. b => B(2),34. ci => c2,35. co => c3,36. s => S(2)37. );38. FA_3: full_adder port map ( a => A(3),39. b => B(3),40. ci => c3,41. co => cout,42. s => S(3)43. );44. end structure;
Apr 12, 2023Apr 12, 2023
32
LabEx2 Part2
FA
S(1)
c(1)a(1)b(1)
FA
S(0)
C_ina(0)b(0)
FA
S(n-1)
c(n-1)a(n-1)b(n-1)
C_out
FA
S(2)
c(2)a(2)b(2)
. . .
Bộ cộng n bit (n-bit ripple carry adder circuit)
Apr 12, 2023Apr 12, 2023
33
LabEx2 Part2
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity RCA_Nbit is4. generic( N : integer := 4 );5. port( A,B : in std_logic_vector(N-1 downto 0);6. cin : in std_logic;7. cout : out std_logic;8. S : out std_logic_vector(N-1 downto 0) );9. end RCA_Nbit;
10. architecture structure of RCA_Nbit is11. signal C : std_logic_vector(N downto 0);12. component full_adder13. port( a,b,ci : in std_logic;14. co,s : out std_logic );15. end component;16. begin17. C(0) <= cin;18. RCAdder: for i in 0 to N-1 generate19. FA: full_adder port map (A(i),B(i),C(i),C(i+1),S(i));20. end generate;21. cout <= C(N);22. end structure;
Apr 12, 2023Apr 12, 2023
34
LabEx2 Part2
1. entity RCA_8bit is2. port( A,B : in std_logic_vector(7 downto 0);3. cin : in std_logic;4. cout : out std_logic;5. S : out std_logic_vector(7 downto 0) );6. end RCA_8bit;
7. architecture structure of RCA_8bit is8. component RCA_Nbit9. generic( N : integer := 4 );10. port( A,B : in std_logic_vector(N-1 downto 0);11. cin : in std_logic;12. cout : out std_logic;13. S : out std_logic_vector(N-1 downto 0) );14. end component;15. begin16. adder: RCA_Nbit generic map (N => 8)17. port map( A => A,18. B => B,19. cin => cin,20. cout => cout,21. S => S );22. end structure;
Apr 12, 2023Apr 12, 2023
35
Các bước thiết kế
Tổng kết
Hợp kênh 8-bit 2 lối vào dữ liệu
Hợp kênh 3-bit 5 lối vào dữ liệu
Bộ cộng Ripple-Carry Adder
Apr 12, 2023Apr 12, 2023
36
Công cụ phần mềm (ModelSim): tạo project, viết file mô tả VHDL, biên dịch, mô phỏng
Ngôn ngữ VHDL:
- cấu trúc một file VHDL
- Có khái niệm về entity, port, signal, component, generic…
- Cú pháp khai báo và sử dụng các đối tượng trên
- Cú pháp vòng for … generate Thiết kế các mạch số: một vài mạch TỔ HỢP đơn giản (hợp
kênh và bộ cộng)
Apr 12, 2023
Buổi thực hành 2
Thiết kế một số phần tử nhớ, bộ đếm Mô phỏng trên ModelSim Thực thi các mạch tổ hợp và mạch dãy đã thiết kế trên FPGA
EP2C35F672C6 của hãng Altera
37
Apr 12, 2023
Thực thi các mạch trên FPGA
Sử dụng phần mềm Quartus của hãng Altera
Tạo project Add các file nguồn VHDL Gán các port của entity top-level với các chân
của FPGA Biên dịch-Tổng hợp Nạp chương trình lên chip
38
Apr 12, 2023
LabEx0 Part1
Mạch giải mã 7 đoạn: với đầu vào là tín hiệu biểu diễn số nhị phân 4 bit, đầu ra là các mã hiển thị LED 7 đoạn với Anode chung.
Thực hiện nạp lên FPGA trên kit DE2 sao cho LED7 đoạn HEX0 hiển thị giá trị số nhị phân được cho bới các công tắc SW3, SW2, SW1, SW0.
D(0)
D(6)
D(3)
D(5)
D(4)
D(1)
D(2)
Decoder4 7
BN
(binary number)
D
(Display code)
39
Apr 12, 2023
Bảng chân lý:BN3 BN2 BN1 BN0 D6 D5 D4 D3 D2 D1 D0
0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 1 1 1 1 0 0 1
0 0 1 0 0 1 0 0 1 0 0
0 0 1 1 0 1 1 0 0 0 0
0 1 0 0 0 0 1 1 0 0 1
0 1 0 1 0 0 1 0 0 1 0
0 1 1 0 0 0 0 0 0 1 0
0 1 1 1 1 1 1 1 0 0 0
1 0 0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 1 0 0 0 0
1 0 1 0 0 0 0 1 0 0 0
1 0 1 1 0 0 0 0 0 1 1
1 1 0 0 1 0 0 0 1 1 0
1 1 0 1 0 1 0 0 0 0 1
1 1 1 0 0 0 0 0 1 1 0
1 1 1 1 0 0 0 1 1 1 0
40
Apr 12, 2023
LabEx0 Part1
Có thể viết 7 phương trình logic để mô tả mạch giải mã trênhoặc viết đoạn code sau:
1. architecture simple of LED7seg_decoder is
2. begin3. process(BN)4. begin5. case BN is6. when "0000" => D <="1000000";7. when "0001" => D <="1111001";8. when "0010" => D <="0100100";9. when "0011" => D <="0110000";10. when "0100" => D <="0011001";11. when "0101“ => D <="0010010";12. when "0110" => D <="0000010";13. when "0111" => D <="1111000";14. when "1000" => D <="0000000";15. when "1001" => D <="0010000";16. when "1010“ => D <="0001000";17. when "1011“ => D <="0000011";18. when "1100“ => D <="1000110";19. when "1101“ => D <="0100001";20. when "1110“ => D <="0000110";21. when "1111“ => D <="0001110";22. when others => D <="1111111";23. end case;24. end process;25. end simple;
41
Combinational process:<optional_label>:process(<sensitivity_list>)-- Declaration(s)begin-- Sequential Statement(s)end process;
Sensitivity list: danh sách các tín hiệu màsự thay đổi của chúng có ảnh hưởngtới mạch (gây ra thay đổi trên lối ra)Declaration: khai báo các tín hiệu vàbiến dùng trong processSequential statement: các câu lệnh tuần tựnhư if, case, for … loop, …
Apr 12, 2023
LabEx0 Part1
Tạo project
(chú ý: tên project, tên file top-level.vhd, tên entity top-level phải giống nhau)
File>New project wizard…
Chọn thư mục và đặt tên project
Đường dẫntới thư mụcchứa project
Tên project
42
Apr 12, 2023
LabEx0 Part1
Nếu đã có file nguồn VHDL, có thể add vào project:
43
Apr 12, 2023
LabEx0 Part1
Lựa chọn Family CycloneII và tên FPGA là EP2C35F672C6
44
Apr 12, 2023
LabEx0 Part1
File>New để tạo file VHDL mới Hoặc Project>Add/Remove Files In Project… để add các file
VHDL vào project Processing>Start Complitation để biên dịch Trên kit DE2, các chân của FPGA được kết nối với các phần cứng
(như công tắc SW, KEY, các LED …)(xem file DE2_UserManual.pdf)Để mạch số được thiết kế có thể hoạt động trên kit DE2, ta cần thực hiện gán các đầu vào và đầu ra của mạch với các chân tương ứng của FPGA.Trong bài này cần gán các port BN của entity với các công tắc SW3, SW2, SW1, SW0 và các lối ra D được gán với LED 7 đoạn HEX0.Assignments > Assignment Editor để thực hiện gán chân
Tại cửa sổ Assignment Editor, chọn Category là Pin (xem hình slide sau)
45
Apr 12, 2023
LabEx0 Part1
Double Click vào <<new>> ở cột To để lựa chọn port cần gán sau đó lựa chọn chân tương ứng của FPGA ở cột Location bên cạnh(xem file DE2_UserManual.pdf)
46
Apr 12, 2023
LabEx0 Part1
Save file Assignment Editor như sau:
Biên dịch & tổng hợp lại với file gán chân
47
Apr 12, 2023
LabEx0 Part1
Tools>Programmer để nạp vào FPGA: đánh dấu vào program/configure rồi chọn Start
48
Apr 12, 2023
LabEx3 part1
Yêu cầu thiết kế 3 phần tử nhớ:a. Chốt Db. Flipflop hoạt động
theo sườn lênc. Flipflop hoạt động
theo sườn xuốngQa có giá trị = D khi Clk=1Khi Clk=0, Qa nhớ trạng thái
trước đóQb chỉ nhận giá trị của D tại
thời điểm có sườn lêncủa Clk, sau đó giá trị nàyđược nhớ tới khi có sườn lêntiếp theo trên Clk
Qc tương tự với Qb nhưnghoạt động theo sườn xuốngcủa Clk
49
Apr 12, 2023
LabEx3 part1
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity latch is4. port(5. D,Clk : in std_logic;6. Q,notQ : out std_logic7. );8. end latch;
9. architecture behavior of latch is10. begin11. process (D,Clk)12. begin13. if Clk ='1' then14. Q <= D;15. notQ <= not D;16. end if;17. end process;18. end behavior;
50
Apr 12, 2023
LabEx3 part1
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity PE_FlipFlop is4. port(5. D,Clk : in std_logic;6. Q,notQ : out std_logic7. );8. end PE_FlipFlop;
9. architecture behavior of PE_FlipFlop is10. begin11. process (Clk)12. begin13. if Clk'event and Clk ='1' then
-- if rising_edge(Clk) then14. Q <= D;15. notQ <= not D;16. end if;17. end process;18. end behavior;
51
Sequential process:<optional_label>:process(reset, clk)-- Declaration(s)beginif(reset = '1') then-- Asynchronous Sequential Statement(s)elsif(rising_edge(clk)) then-- Synchronous Sequential Statement(s)end if;end process;
- Asynchronous Sequential Statement:các lệnh tuần tự hoạt động không theo clock(không đồng bộ/dị bộ)- Synchronous Sequential Statement:các lệnh tuần tự hoạt động theo clock(đồng bộ)
Apr 12, 2023
LabEx3 part1
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity NE_FlipFlop is4. port(5. D,Clk : in std_logic;6. Q,notQ : out std_logic7. );8. end NE_FlipFlop;
9. architecture behavior of NE_FlipFlop is10. begin11. process (Clk)12. begin13. if Clk'event and Clk ='0' then-- if falling_edge(Clk) then14. Q <= D;15. notQ <= not D;16. end if;17. end process;18. end behavior;
52
Apr 12, 2023
LabEx3 part2
Thiết kế bộ đếm 16 bit có cấu trúc tương tự bộ đếm 4 bit dưới đây (gồm các flip-flop kiểu T):
Các flip-flop kiểu T có dạng:sau mỗi xung clock,nếu en ở mức tích cực,clr không tích cựcgiá trị lối ra Q sẽ bị đảoso với trước
53
D-typeflip-flop
D Q
notQ
en
clrclk
Apr 12, 2023
LabEx3 part2
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity D_FlipFlop is4. port(5. D,Clk,En,Rst : in std_logic;6. Q,notQ : out std_logic7. );8. end D_FlipFlop;
9. architecture behavior of D_FlipFlop is10. begin11. process (Clk)12. begin13. if Clk'event and Clk ='1' then -- if rising_edge(Clk) then14. if Rst = '0' then15. Q <= '0';16. notQ <= '1';17. elsif En = '1' then18. Q <= D;19. notQ <= not D;20. end if;21. end if;22. end process;23. end behavior;
54
Apr 12, 2023
LabEx3 part2
1. library ieee;2. use ieee.std_logic_1164.all;
3. entity T_FlipFlop is4. port(5. Clk,T,Rst : in std_logic;6. Q : out std_logic7. );8. end T_FlipFlop;
9. architecture structure of T_FlipFlop is10. component D_FlipFlop11. port(12. D,Clk,En,Rst : in std_logic;13. Q,notQ : out std_logic14. );15. end component;16. signal notQ : std_logic;17. begin18. comp1: D_Flipflop port map (D => notQ, Clk => Clk, En => T, Rst => Rst,
Q => Q, notQ => notQ);19. end structure;
55
Apr 12, 2023
LabEx3 part2
1. library ieee;2. use ieee.std_logic_1164.all;3. entity Cntr_16bit is4. port( Clock,Enable,Clear : in std_logic;5. Count : out std_logic_vector(15 downto 0));6. end Cntr_16bit;7. architecture structure of Cntr_16bit is8. component T_FlipFlop9. port( Clk,T,Rst : in std_logic;10. Q : out std_logic );11. end component;12. signal En,Q : std_logic_vector(15 downto 0);13. Begin14. En(0) <= Enable;15. comp0: T_Flipflop port map (Clk => Clock, T => En(0), Rst => Clear, Q => Q(0));16. Count(0) <= Q(0);17. generate_label: for i in 1 to 15 generate18. En(i) <= En(i-1) and Q(i-1);19. comp: T_Flipflop port map (Clk => Clock, T => En(i), Rst => Clear, Q => Q(i));20. Count(i) <= Q(i);21. end generate;22. end structure;
56
Apr 12, 2023
LabEx0 Part2
Với các bước tương tự trên, Part2 yêu cầu thiết kế một mạch số thực hiện đếm số lần ấn công tắc KEY0 trên kit DE2 và hiển thị kết quả đếm được bằng số nhị phân 16 bit trên 4 LED 7 đoạn HEX3, HEX2, HEX1, HEX0.
Việc đếm này chỉ được thực hiện khi công tắc SW0 và SW1 được đặt bằng 1. Nếu SW0=‘0’, bộ đếm bị xóa. Nếu SW1=‘0’ thì không đếm
57