玩转FPGA山寨版

看了《玩转FPGA》,写的不错,写写山寨版和大家交流!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

好消息,DE2又出新的IP了,支持Quartus II 9.0、9.1、10.0和10.1

大家可以Altera的官方网站下载,也可以和我联系,传给你一份QQ:287687752

下面是IP里面的一个实例,在<Quartus II安装目录>\University_Program\IP_Core_Demos\DE2\DE2_Video_SOPC_Builder_Demos中的DE2_VGA_Both_Buffers文件中

工程环境:Quartus II 10.1sp1 + NIOS II 10.1sp1 + DE2

打开上述文件的工程,我们直接进入SOPC Builder配置如下:

顶层工程文件更简单:

module DE2_VGA_Both_Buffers (
 // Inputs
 CLOCK_50,
 KEY,

 // Bidirectionals
 //  Memory (SRAM)
 SRAM_DQ,
 
 // Outputs
 //  Memory (SRAM)
 SRAM_ADDR,

 SRAM_CE_N,
 SRAM_WE_N,
 SRAM_OE_N,
 SRAM_UB_N,
 SRAM_LB_N,
 
 //  VGA
 VGA_CLK,
 VGA_HS,
 VGA_VS,
 VGA_BLANK,
 VGA_SYNC,
 VGA_R,
 VGA_G,
 VGA_B
);

/*****************************************************************************
 *                           Parameter Declarations                          *
 *****************************************************************************/


/*****************************************************************************
 *                             Port Declarations                             *
 *****************************************************************************/
// Inputs
input    CLOCK_50;
input  [3:0] KEY;

// Bidirectionals
//  Memory (SRAM)
inout  [15:0] SRAM_DQ;

// Outputs
//  Memory (SRAM)
output  [17:0] SRAM_ADDR;

output    SRAM_CE_N;
output    SRAM_WE_N;
output    SRAM_OE_N;
output    SRAM_UB_N;
output    SRAM_LB_N;

//  VGA
output    VGA_CLK;
output    VGA_HS;
output    VGA_VS;
output    VGA_BLANK;
output    VGA_SYNC;
output  [ 9: 0] VGA_R;
output  [ 9: 0] VGA_G;
output  [ 9: 0] VGA_B;

/*****************************************************************************
 *                 Internal Wires and Registers Declarations                 *
 *****************************************************************************/
// Internal Wires

// Internal Registers

// State Machine Registers

/*****************************************************************************
 *                         Finite State Machine(s)                           *
 *****************************************************************************/


/*****************************************************************************
 *                             Sequential Logic                              *
 *****************************************************************************/


/*****************************************************************************
 *                            Combinational Logic                            *
 *****************************************************************************/


/*****************************************************************************
 *                              Internal Modules                             *
 *****************************************************************************/

Video_System Char_Buffer_System (
 // 1) global signals:
 .clk_0         (CLOCK_50),
 .reset_n        (KEY[0]),
 .sys_clk        (),
 .vga_clk        (),

 // the_Pixel_Buffer
 .SRAM_DQ_to_and_from_the_Pixel_Buffer (SRAM_DQ),
 .SRAM_ADDR_from_the_Pixel_Buffer  (SRAM_ADDR),
 .SRAM_LB_N_from_the_Pixel_Buffer  (SRAM_LB_N),
 .SRAM_UB_N_from_the_Pixel_Buffer  (SRAM_UB_N),
 .SRAM_CE_N_from_the_Pixel_Buffer  (SRAM_CE_N),
 .SRAM_OE_N_from_the_Pixel_Buffer  (SRAM_OE_N),
 .SRAM_WE_N_from_the_Pixel_Buffer  (SRAM_WE_N),

 // the_vga_controller
 .VGA_CLK_from_the_VGA_Controller  (VGA_CLK),
 .VGA_HS_from_the_VGA_Controller   (VGA_HS),
 .VGA_VS_from_the_VGA_Controller   (VGA_VS),
 .VGA_BLANK_from_the_VGA_Controller  (VGA_BLANK),
 .VGA_SYNC_from_the_VGA_Controller  (VGA_SYNC),
 .VGA_R_from_the_VGA_Controller   (VGA_R),
 .VGA_G_from_the_VGA_Controller   (VGA_G),
 .VGA_B_from_the_VGA_Controller   (VGA_B)
);

endmodule

看看RTL视图:

把编译好的配置文件下载DE2中,进入NIOS II

看看里面的给的C代码:

#include"system.h"

/* function prototypes */
void VGA_text (int, int, char *);
void VGA_box (int, int, int, int, short);

/********************************************************************************
 * This program demonstrates use of the character and pixel buffers
 *
 * It performs the following:
 *  1. Draws a blue box on the VGA display, and places a text string inside
 *     the box. Also, moves the word ALTERA around the display, "bouncing" off
 *     the blue box and screen edges
********************************************************************************/
int main(void)
{
 /* Declare volatile pointer to pixel DMA controller (volatile means that IO load
    and store instructions will be used to access these pointer locations,
    instead of regular memory loads and stores) */
   volatile int * Pixel_DMA_controller = (int *) PIXEL_BUFFER_DMA_BASE; // DMA controller base address

 int delay = 0; // synchronize with the screen drawing

 /* these variables are used for a blue box and a "bouncing" ALTERA on the VGA screen */
 int ALT_x1; int ALT_x2; int ALT_y;
 int ALT_inc_x; int ALT_inc_y;
 int blue_x1; int blue_y1; int blue_x2; int blue_y2;
 int screen_x; int screen_y; int char_buffer_x; int char_buffer_y;
 short color;

 /* create messages to be displayed on the VGA display */
 char text_top_VGA[20] = "Altera DE2\0";
 char text_bottom_VGA[20] = "Video Buffers\0";
 char text_ALTERA[10] = "ALTERA\0";
 char text_erase[10] = "      \0";

 /* the following variables give the size of the pixel buffer */
 screen_x = 319; screen_y = 239;
 color = 0x1863;  // a dark grey color
 VGA_box (0, 0, screen_x, screen_y, color); // fill the screen with grey
 // draw a medium-blue box around the above text, based on the character buffer coordinates
 blue_x1 = 28; blue_x2 = 52; blue_y1 = 26; blue_y2 = 34;
 // character coords * 4 since characters are 4 x 4 pixel buffer coords (8 x 8 VGA coords)
 color = 0x187F;  // a medium blue color
 VGA_box (blue_x1 * 4, blue_y1 * 4, blue_x2 * 4, blue_y2 * 4, color);
 /* output text message in the middle of the VGA monitor */
 VGA_text (blue_x1 + 5, blue_y1 + 3, text_top_VGA);
 VGA_text (blue_x1 + 5, blue_y1 + 4, text_bottom_VGA);

 char_buffer_x = 79; char_buffer_y = 59;
 ALT_x1 = 0; ALT_x2 = 5/* ALTERA = 6 chars */; ALT_y = 0; ALT_inc_x = 1; ALT_inc_y = 1;
 VGA_text (ALT_x1, ALT_y, text_ALTERA);

 *(Pixel_DMA_controller) = 0; // dummy write to start buffer swap process
 while (1)
 {
  if ( (*(Pixel_DMA_controller+3) & 1) == 0) // wait for Status register bit S == 0
  {
   /* If the screen has been drawn completely then we can draw a new image. This
    * section of the code will only be entered each time the screen is redrawn */
   delay = delay + 1;

   if (delay == 2)
   {
    delay = 0;
    /* The delay is inserted to slow down the animation */

    /* move the ALTERA text around on the VGA screen */
    VGA_text (ALT_x1, ALT_y, text_erase);  // erase
    ALT_x1 += ALT_inc_x;
    ALT_x2 += ALT_inc_x;
    ALT_y += ALT_inc_y;

    if ( (ALT_y == char_buffer_y) || (ALT_y == 0) )
     ALT_inc_y = -(ALT_inc_y);
    if ( (ALT_x2 == char_buffer_x) || (ALT_x1 == 0) )
     ALT_inc_x = -(ALT_inc_x);

    if ( (ALT_y >= blue_y1 - 1) && (ALT_y <= blue_y2 + 1) )
    {
     if ( ((ALT_x1 >= blue_x1 - 1) && (ALT_x1 <= blue_x2 + 1)) ||
      ((ALT_x2 >= blue_x1 - 1) && (ALT_x2 <= blue_x2 + 1)) )
     {
      if ( (ALT_y == (blue_y1 - 1)) || (ALT_y == (blue_y2 + 1)) )
       ALT_inc_y = -(ALT_inc_y);
      else
       ALT_inc_x = -(ALT_inc_x);
     }
    }
    VGA_text (ALT_x1, ALT_y, text_ALTERA);
   }
   /* Execute a swap buffer command. This will allow us to check if the screen has
    * been redrawn before generating a new animation frame. */
   *(Pixel_DMA_controller) = 0;
  }
 }
}

/****************************************************************************************
 * Subroutine to send a string of text to the VGA monitor
****************************************************************************************/
void VGA_text(int x, int y, char * text_ptr)
{
 int offset;
   volatile char * character_buffer = (char *) CHAR_BUFFER_WITH_DMA_AVALON_CHAR_BUFFER_SLAVE_BASE; // VGA character buffer

 /* assume that the text string fits on one line */
 offset = (y << 7) + x;
 while ( *(text_ptr) )
 {
  *(character_buffer + offset) = *(text_ptr); // write to the character buffer
  ++text_ptr;
  ++offset;
 }
}

/****************************************************************************************
 * Draw a filled rectangle on the VGA monitor
****************************************************************************************/
void VGA_box(int x1, int y1, int x2, int y2, short pixel_color)
{
 int offset, row, col;
   volatile short * pixel_buffer = (short *) PIXEL_BUFFER_BASE; // VGA pixel buffer

 /* assume that the box coordinates are valid */
 for (row = y1; row <= y2; row++)
 {
  col = x1;
  while (col <= x2)
  {
   offset = (row << 9) + col;
   *(pixel_buffer + offset) = pixel_color; // compute halfword address, set pixel
   ++col;
  }
 }
}

接上显示器,我们就可以看到跳动的文字ALTERA了,还有蓝色的BOX,呵呵!

posted on 2011-02-20 20:48  Neddy11  阅读(4257)  评论(6编辑  收藏  举报