• 沒有找到結果。

VHDL應用實作遊戲 PING PONG

N/A
N/A
Protected

Academic year: 2021

Share "VHDL應用實作遊戲 PING PONG"

Copied!
32
0
0

加載中.... (立即查看全文)

全文

(1)

逢 甲 大 學

資 訊 工 程 學 系 專 題 報 告

VHDL 應用實作遊戲

PING PONG

生: 梁 浩 銘 (四乙)

授 : 陳 德 生

中華民國九十一年十二月

(2)

目 錄

第一章 前 言 ...1

第二章 實 驗 目 的 及 內 容... 2

第三章 Altera’s UP1 Education Board 介紹...3

第四章 V H D L 簡 介...11

第五章 MAX_PLUS II Introduction...13

第六章 Program structure and execute result...17

第七章 遭 遇 的 問 題 與 解 決 方 法...25

第八章 心 得...27

(3)

圖 表 目 錄

圖一 UP1 board 及 其 週 邊...4

圖二 V G A 營幕...5

圖三 V G A Horizontal refresh cycle...5

圖四 V G A Vertical refresh cycle...6

圖五 System Transmission of a Command to PS/2 Device...8

圖六 Scan Codes for PS/2 Keyboard...9

圖七 Scan Codes for PS/2 Keyboard...10

圖八 MAX+Plus II 支援的功能及其工作環境...14

圖九 Chose Device Family...15

圖十 MAX+Plus II compile program...15

圖十一 MAX+Plus II 設定 pin 腳...16

圖十二 MAX+Plus II 進行燒錄...16

圖十三 Flow diagram...17

圖十四 signal transition diagram...18

圖十五 遊戲初始畫面...23

(4)
(5)

第一章 前言

前提: 在大二時接觸了數位電路,對於課堂上的實驗不外乎利用電路板 和邏輯閘來做出一些電路設計,猶記得當初曾為了找不出錯誤必須一 做再做,直到學習了用 VHDL 直接對所需的硬體作描述,進而更可 以做波形的模擬也可以看出邏輯閘間的延遲,除此之外又不須插電 路,直接利用相關的硬體進行燒錄便能看出結果。 課堂上,對於利用程式語言來描述各種元件如 mutilplexer、 decoder、flip-flop,再將這些元件組合 adder,counter,simple processor… 等,感到滿有趣一方面要程式的寫作一方面也必須對電路有一定的了 解,可說是滿具挑戰感但相對的成就感更值得高興。

因為以上的緣故,專題的內容便選擇做一些 VHDL 相關的應用, 希望能多學到一些知識。

(6)

第二章 實驗目的及內容

目的及題目解說:

這個實驗希望利用 VHDL 實作一個常見的小遊戲 Ping Pong(打 磚塊),對遊戲的規則和說明如下: 1. 在遊戲初始時,會設有四個邊界、一堆磚塊、一顆球、和一個接 球的橫竿。 2. 遊戲進行時,當球撞擊到左右和上方邊界時,球將依照物理的碰 撞規則運動,在下方的橫竿依照鍵盤的控制左右移動,當橫竿漏 接球時遊戲結束。 3. 對於球撞擊磚塊則較有變化,一般的磚塊當球撞擊到時則消失, 而球依照碰撞規則運動,但也可以設想某些磚塊為讓球碰撞用 的,其效果如同碰撞四周,主要讓球在螢幕上能多點彈跳的視覺 感受。 4. 關卡的設置,當撞擊完所有的磚塊則轉入下一個關卡。

(7)

第三章 Altera’s UP1 Education Board 介紹

使用裝置介紹:

This project use Altera’s UP1 board and MAX+PLUS II software. The UP1 education board is a stand-alone experiment board base on two of Altera’s leading families: MAX 7000 and FlEX 10K. Its simple design when used with MAX+Plus II software, provides a superior platform for learning digital logic using high-level development tools and PLDs. EPF10K20 Device

THE EPF10K20 device is based on reconfigurable SRAM elements. The EPF10K20 device is available in a 240-pin RQFP package and has 1,152 Les and six EABs. Each Le consist of a four input LUT( a

programmable flipflop),and dedicated signal paths for carry-and-cascade function. Each EAB provides 2048 bits of memory which can be used to create RAM,ROM, or FIFO function. The EABs can implement logic functions, such as multiplier, microcontroller, state machine, and DSP functions. With 20,000 typical gates, the EPF10K device is ideal for introductory digital design course.

This time my program will be compiled and I need to indicate program’s output signal to FLEX 10K’s pins. After this steps, program will be downloaded into FLEX 10K on UP1 board, then connect

keyboard, monitor we can see the result on screen. The pins in the program is needed as follows:

FLEX_SW1 Pin Assignments

Switch EPF10K20Pin FLEX_SWITCH-1 41 FLEX_SWITCH-2 40 FLEX_SWITCH-3 39 FLEX_SWITCH-4 38 FLEX_SWITCH-5 36 FLEX_SWITCH-6 35 FLEX_SWITCH-7 34 FLEX_SWITCH-8 33

(8)

D-Sub Connections

Signal D-Sub Connector Pin EPF10k20 Pin

RED 1 236 GREEN 2 237 BLUE 3 238 GND 6,7,8,9,10,11 - HORIZ_SYNC 13 240 VERT_SYNC 14 239 NO Connect 4,5,9,15 - Mouse Connections

Mouse Signal Mini-DIN Pin EPF10K20 Pin

MOUSE_CLK 1 30

MOUSE_DATA 3 31

VCC 5 -

GND 2 -

(9)

About VGA :

Each standard VGA monitor has an internal clock, its frequency is 25.175Mhz .The monitor total has 640*480 pixels, row has 480 pixels, column has 640 pixels. Each pixel can display red, green, and blue three kind colors.

圖二 Vga 營幕

VGA timing:

For the VGA monitor to work properly, it must receive data at specific times with specific pulses. Horizontal and vertical

synchronization pulses must occur at specified times to synchronize the monitor while it is receiving color data. Timing waveforms for the color information with respect to the horizontal and vertical synchronization signals.

圖三 VGA Horizontal refresh cycle (0,0)

(640,480) 640 pixels

(10)

圖四 VGA Vertical refresh cycle

Following equations explain how to get the time for the monitor to display well.

Time for each pixel : 1/clock frequency = 40ns

Time for each row : ( 40ns * 640 ) + row guild bands = B+C+D+E = 31.77us

Time for screen : 31.77us*480 + guild bands = P+Q+R+S =16.6ms so be careful when coding the VGA code, must bigger or equal than the time required, else will occur unexpected error.

(11)

About PS/2 Keyboard:

The Altera UP1 board supports the use of either a mousse or keyboard using the PS/2 connector on the board.

The PS/2 port consists of 6 pins including ground, vdd, keyboard data, and a keyboard clock line. The UP1 board supplies the power to the mouse or keyboard. Two lines are not used. The keyboard data line is pin 31 on the flex chip, and the keyboard clock line is pin 30.

Make and Break Codes

The keyboard scan codes consists of ‘Make’ and ‘Break’ codes. One Make code is sent every time a key is pressed. When a key is released, a break code is sent. For most keys, the break code is a data stream of F0 followed by the make code for the key. Be aware that when typing, it is common to hit the next key before releasing the first key hit.

Using this configuration, the system can tell whether or not the key has been pressed, and if more than one key is being held down, it can also distinguish which key has been released.

When implementing the interface code, it will be necessary to filter the slow keyboard clock to ensure reliable operation with the fast logic inside the FLEX chip. Whenever an electrical pulse is transmitted on a wire, electromagnetic properties of the wire cause the pulse to be

distorted and some portions of the pulse may be reflected from the end of wire. On some PS/2 keyboards there is a reflected pulse on the cable that is strong enough to cause additional clocks to appear on the clock line.

Here is one approach that solves the reflected pulse problem. Feed the PS/2 clock signal into a 8 bit shift register that use the 25Mhz clock. AND the bits of the shift register together and use the output of the AND gate as the new “filtered” clock. This prevents noise and ringing on the clock line from causing occasional extra clocks during the serial to parallel conversion in the FLEX chip.

The computer system or FLEX chip sends commands to the PS/2 keyboard as follows:

1. System drives the clock line Low for approximately 60us to inhibit any new keyboard data transmissions.

(12)

2. System drives the data line Low and then release the clock line to signal that it has data for the keyboard.

3. The keyboard will generate clock signals in order to clock out the remaining serial bits in the command.

4. The system will send its 8-bit command followed by a paritybit and a stop bit.

5. After the stop bit is driven High, the data line is released.

Upon completion of each command byte, the keyboard will send an acknowledge signal, FA, if it received the data successfully. If the system dos not release the data line, the keyboard will continue to generate the clock, and upon completion, it will send a ‘re-send command’ signal, FE or FC, to the system. A parity error or missing stop bit will also generate a re-send command signal.

圖五 System Transmission of a Command to PS/2 Device

Scan code set for the PS/2 Keyboard

PS/2 keyboard are available in several languages with different characters printed on the keys. A two-step process is required to find the scan code. A key number is used to lookup the scan code. Each key sends out a make code when hit and a break code when released. When several keys are hit at the same time, several make codes will be sent before a break code. The keyboard powers up using this scan code as the default. Commands must be sent to the keyboard to use other scan code sets. The PC sends out an initialization command that forces the keyboard to use the other scan code.

(13)

Key# Make Code Break Code Key# Make Code Break Code Key# Make Code Break Code 1 0E F00E 31 1C F01C 90 77 F077 2 16 F016 32 1B F01B 91 6C F06C 3 1E F01E 33 23 F023 92 6B F06B 4 26 F026 34 2B F02B 93 69 F069 5 25 F025 35 34 F034 96 75 F075 6 2E F02E 36 33 F033 97 73 F073 7 36 F036 37 3B F03B 98 72 F072 8 3D F03D 38 42 F042 99 70 F070 9 3E F03E 39 4B F04B 100 7C F07C 10 46 F046 40 4C F04C 101 7D F07D 11 45 F045 41 52 F052 102 74 F074 12 4E F04E 43 5A F05A 103 7A F07A 13 55 F055 44 12 F012 104 71 F071 15 66 F066 46 1A F01A 105 7B F07B 16 0D F00D 47 22 F022 106 79 F079 17 15 F015 48 21 F021 110 76 F076 18 1D F01D 49 2A F02A 112 05 F005 19 24 F024 50 32 F032 113 06 F006 20 2D F02D 51 31 F031 114 04 F004 21 2C F02C 52 3A F03A 115 0C F00C 22 35 F035 53 41 F041 116 03 F003 23 3C F03C 54 49 F049 117 0B F00B 24 43 F043 55 4A F04A 118 83 F083 25 44 F044 57 59 F059 119 0A F00A 26 4D F04D 58 14 F014 120 01 F001 27 54 F054 60 11 F011 121 09 F009 28 5B F05B 61 29 F029 122 78 F078 29 5D F05D 62 E011 F0E011 123 07 F007 The remaining key codes are a function of the shift, control, alt, num-lock

(14)

key No Shift or Num Lock

Shift*

# Make Break Make Break 75 E070 E0F070 E0F012E070 E0F070E012 76 E071 E0F071 E0F012E071 E0F071E012 79 E06B E0F06B E0F012E06B E0F06BE012 80 E06C E0F06C E0F012E06C E0F06CE012 81 E069 E0F069 E0F012E069 E0F069E012 83 E075 E0F075 E0F012E075 E0F075E012 84 E072 E0F072 E0F012E072 E0F072E012 85 E07D E0F07D E0F012E07D E0F07DE012 86 E07A E0F07A E0F012E07A E0F07AE012 89 E074 E0F074 E0F012E074 E0F074E012

Num Lock On Make Break E012E070 E0F070E0F012 E012E071 E0F071E0F012 E012E06B E0F06BE0F012 E012E06C E0F06CE0F012 E012E069 E0F069E0F012 E012E075 E0F075E0F012 E012E072 E0F072E0F012 E012E07D E0F07DE0F012 E012E07A E0F07AE0F012 E012E074 E0F074E0F012

Key Scan Code Shift Case*

# Make Break Make Break 95 E04A E0F04A E0F012E04A E012F04A

Key Scan Code Control, Shift case # Make Break Make Break 124 E012E07C E0F07CE0F012 E07C E0F07C

Alt case

Make Break 84 F084

(15)

第四章 VHDL 簡介

VHDL 是 Very High Speed Integrated Circuit Hardware Description 的英文縮寫。VHDL 電路設計語言規範的目的,即是要提供一個高 階且快速的設計工具,它含蓋了電路的描述,電路之合成與電路之模 擬等三大電路設計工作。在 1970 年到 1980 年間,美國國防部為了 電子電路的設計意涵,以文件方式將其儲存起來,以便其他人能輕易 了解電路的設計意義,此為 VHDL 電路設計孕育的由來。由於 VHDL 電路設計語言所能函蓋的範圍相當廣,它能適應於各種不同階層的設 計工程師的需求。從 ASIC 的設計到 PCB 系統的設計,VHDL 電路 描述語言,均能派上用場。所以 VHDL 電路設計語言實為硬體設計 工程師之必備工具。 VHDL 電路設計語言當作電路設計的工具,則可以由下面的幾點來 分別予以說明: 功能強大: 它包含了電路描述,電路合成 與電路模擬等功能。它自 ASIC 設計 到電路板設計,甚或大型系統之設計 VHDL 電路設計語言均能派上 用場。 設計靈活: 它將電子電路的設計方式,改便程以行為化描述的方式來設計。因此

(16)

具有設計快速、更改容易、犯錯機率低和除錯容易等優點。 個種不同的描述風格: VHDL 提供許多不同的描述風格,來適應大小,複雜性不同的電路 設計。 可流通性或可攜性: 因為 VHDL 是一種工業界標準的電路設計語言,它所提供的可攜性 能力,可以使你的設計可以利用不同編譯軟體來編譯,可以利用不同 模擬器來模擬以及適用於任何種類的邏輯元件與半導體製程技術。

(17)

第五章 MAX_PLUS II Introduction

MAX+Plus II 是由可程式邏輯元件的供應商 Altera Inc,公司所提供 的設計、驗證與燒錄軟體。接下來針對 MAX+PlusII 所支援的功能項 目做簡略說明: Hierarchy Display:用來顯示一個大型電路設計的階層化架構 Graphic Editor:這是用來提供電路圖輸入的介面。使用者可用 此圖形編輯器,利用繪製電路圖(schematics)的方法,來作為電 路功能的設計輸入。 Symbol Editor:在利用電路圖的方式來做電路的設計輸入時, 要用一些符號來代表一些合成一組的電路功能。

Text Editor:為 MAX+PlusII 內建的文字編輯器,其可用來當作 HDL 語言的設計輸入。

Waveform Editor:用來編輯訊號的波形圖。

Floorplan Editor:用來直接在 FPGA 或 PLD 的佈局上來編輯 cell 的功能與連結關係,此疌供 FPGA 或 PLD 在實現邏輯功能的實 際物理架構之說明或可以直接修改其物理上的佈局架構。 Compiler:此為電路功能的編輯器,用來將設計輸入,經過編

譯後,產生實際電路功能的可燒錄程式檔。

(18)

的正確性。

Timing Analyzer:用來分析電路元件的輸出入接腳間的時序延 遲的狀況。

Programmer:此為 MAX+PlusII 所提供的邏輯元件燒錄器,可 支援 Altera 提供的 Byte Blaster 及 Bit Blaster 等之邏輯燒錄器。 Message Processor:此為程式編譯過程或訊號波形的模擬過程

中產生的一些訊息,如錯誤(Error)、警告(Warnings)…等,的訊 息處理器,其可以將這些訊息與所設計的電路或訊號做一連結。

(19)

圖九 Chose Device Family

(20)

圖十一 MAX+Plus II 設定 pin 腳

(21)

第六章 Program structure and execute result

圖十三 Flow diagram Program start

Initialize variable

Reset press

Game running, get keyboard data, send signal to VGA, each module working Judge missing ball No Yes Program terminate No Yes

(22)

Signal transition:

圖十四 signal transition diagram

VGA Module Keyboard Module Move Module Ping Module Reset, clo ck H co unt er , v co unt er Hs, vs Kbdata, kbclk Kdata, Rst, reset, clk Hcounter,vcounter ored,ogreen,oblue Reset,clk Kdata, red,green,blue Reset, clo ck Red,gr een,blue Kd ata,k cl k UP1 Board Keyboard Monitor Red,gr een,blue,hs ,vs

(23)
(24)
(25)

判斷球的彈跳 判斷球的彈跳 判斷球的彈跳 判斷球的彈跳 根據鍵盤的輸入 根據鍵盤的輸入根據鍵盤的輸入 根據鍵盤的輸入 控制 控制 控制 控制 bar 的移動的移動的移動的移動 顯示營幕的游戲畫面 顯示營幕的游戲畫面顯示營幕的游戲畫面 顯示營幕的游戲畫面 和磚塊的情況並在畫面 和磚塊的情況並在畫面 和磚塊的情況並在畫面 和磚塊的情況並在畫面 掃瞄後顯示球的下一位 掃瞄後顯示球的下一位 掃瞄後顯示球的下一位 掃瞄後顯示球的下一位 置 置 置 置

(26)
(27)

執行結果 執行結果 執行結果 執行結果:

(28)

圖十六遊戲進行畫面

(29)

第七章 遭遇的問題與解決方法

當一開始決定專題時,對於 UP1 board 要如何對 monitor 做輸出, 及對 keyboard 做輸入並沒有概念,而程式更是無從下手,故先找出 學長做過相關的專題來研究,當然並無法全盤了解,故又和指導老師 討論,老師建議我找研究所的學長討論,並先將學長的程式跑跑看, 會較容易瞭解,學長向我推薦了一本書,表示讀過則會較容易理解原 理。自己經過一段時間的研讀,夾雜著到實驗室去實作,終能理解原 理,進而能夠開始 coding。 Coding 的過程中配合著書本及讀過的資料倒是滿順利的,但因為 做的遊戲對於碰撞的表現須要做出類似物理的碰撞,且撞擊磚塊的判 斷,一直無法構思出較好的演算法,在此花了滿多精力並且試了許多 方法,最後,利用數學不等式的方法,判斷球心與二條直線的關係來 決定球撞擊磚磈的情形,得到不錯的效果。 圖十八碰撞解釋 此外還有每當球通過營幕上的磚塊區域會有一點延遲,這導因於 在計算球碰撞的過程時間不夠快。

最後又遇到了 UP1 板子 logic cells 不足的問題,這個問題始終無

equation1

(30)

法得到好的解決,我的想法是可能需要另外再設計一個專對磚塊撞擊 的模組,或是改變磚塊顯示的方式應該可以減少 logic cell 的使用, 再不然換另外一塊板子,在實驗室看到有一塊 nios 用的板子,它可 以放 nios c 的程式高達 512K 的容量,且速度快、在 coding 時,也能 快速看到結果。 未來的改進方向: 1. logic cell 能更有效的利用,如此便能設計較多的磚塊,及較多的 碰撞玩法,以及關卡的設計。 2. 遊戲開始和結束的畫面設計,還有遊戲進行時的背景設計,我本 來是有設計結束的畫面,但是實在很難看且 Vga 在描繪時有些閃 爍,就將畫面拿掉,這部分可能要由要有美工較好的同學來設計。 3. 速度的變化,其實這已經有想法了,可以設計說打掉幾個磚塊後, 或是每過一關就改變速度,希望以後能夠在有較多的磚塊或關卡 時將此元素加入。 程式所使用的 Logic Cell 分析 9block Component LCs Utilized Ping.vhd 999/1152 86% Move.vhd 888/1152 77% Vga.vhd 50/1152 4% Keyboard.vhd 65/1152 5%

(31)

第八章 心得

對於此次的專題製作,我覺得過程是我最大的收穫,由於是一個 人製作,所以找資料,研究問題,coding,…等都是自己來,感覺自 己學到的並不只是前述的技術,更學到的是一套製作 project 的系統, 決定題目、收集資訊、研讀資料、發掘問題、構思解決的方法,實作、 再遇到問題、再構思解決方法, 直至最後寫 paper,這些都是我在這 次專題中學到的,唯一的遺憾,因為沒有一起做事的同學,少了彼此 爭執的情況。

(32)

參考資料:

[1] James O. Hanblen and Michael D. Furman “RAPID PROTOTYPING OF DIGITAL SYSTEMS” pp.134-163

[2] Brown Vranestic “ FUNDAMENTALS OF DIGITAL LOGIC WITH VHDL DESIGN” PP.686-787

[3] 許宏華、簡士偉 專題報告,2001 [4] 邱聖富、呂欣樺 專題報告,2002 [5] 王國全、戴正中 專題報告,2002

參考文獻

相關文件

– The The readLine readLine method is the same method used to read method is the same method used to read  from the keyboard, but in this case it would read from a 

了⼀一個方案,用以尋找滿足 Calabi 方程的空 間,這些空間現在通稱為 Calabi-Yau 空間。.

2.1.1 The pre-primary educator must have specialised knowledge about the characteristics of child development before they can be responsive to the needs of children, set

 Promote project learning, mathematical modeling, and problem-based learning to strengthen the ability to integrate and apply knowledge and skills, and make. calculated

In section29-8,we saw that if we put a closed conducting loop in a B and then send current through the loop, forces due to the magnetic field create a torque to turn the loopÆ

The tree’s roots can easily find water in the ground and send it up to the leaves.. Because they are wide and thin, leaves lose

This kind of algorithm has also been a powerful tool for solving many other optimization problems, including symmetric cone complementarity problems [15, 16, 20–22], symmetric

Regarding the course timetable and the commencement arrangement, information will be sent to you via email upon completion of online registration in mid-September 2022.