VGA output

After working out how to do a timer interrupt I've had a go at making a VGA framebuffer. It is rather low-res at present(160x240) and fuzzy but I hope to be able to improve that. It has 8-bit colour (RRRGGGBB).

I cannot get Eagle to run right now so will have to describe the schematic in text: Due pin 2 -> VGA pin 13 (HSync) Due pin 3 -> VGA pin 14 (VSync)

Due pin 25 -> 820R resistor -> VGA pin 3 (blue) Due pin 26 -> 390R resistor -> VGA pin 3 (blue)

Due pin 27 -> 2k2 resistor -> VGA pin 2 (green) Due pin 28 -> 1k resistor -> VGA pin 2 (green) Due pin 14 -> 470R resistor -> VGA pin 2 (green)

Due pin 15 -> 2k2 resistor -> VGA pin 1 (red) Due pin 29 -> 1k resistor -> VGA pin 1 (red) Due pin 11 -> 470R resistor -> VGA pin 1(red)

Due pin GND -> VGA pins 5,6,7,8,10

inline void digitalWriteDirect(int pin, boolean val){
  if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
  else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

volatile short line;
byte fb[240][160];

void TC0_Handler()
{
    long dummy=REG_TC0_SR0; 
                           
    if(line < 480){
        int i,j=line>>1;
        for(i=0;i<160;i++) REG_PIOD_ODSR = fb[j][i];
        REG_PIOD_ODSR = 0;
    }      
    if(line==490) digitalWriteDirect(3,1);
    if(line==492) digitalWriteDirect(3,0);
    
    line++; if(line == 525) line=0;
}

void setup(){
  for(int i=0;i<160;i++)for(int j=0;j<240;j++)fb[j][i]=j+i;
  
  pinMode(3,OUTPUT);  pinMode(2,OUTPUT);                      // vsync=3 hsync=2
  pinMode(25,OUTPUT); pinMode(26,OUTPUT);                     // blue  (26=msb,25=lsb)
  pinMode(27,OUTPUT); pinMode(28,OUTPUT); pinMode(14,OUTPUT); // green (14=msb,28,27=lsb)
  pinMode(15,OUTPUT); pinMode(29,OUTPUT); pinMode(11,OUTPUT); // red   (11=msb,29,15=lsb)

  REG_PIOD_OWER= 0xff;
  REG_PMC_PCER0= 1<<27;  
  REG_PIOB_PDR = 1<<25; 
  REG_PIOB_ABSR= 1<<25; 
  REG_TC0_WPMR = 0x54494D00; 
  REG_TC0_CMR0 = 0b00000000000010011100010000000000;
  REG_TC0_RC0  = 1334; 
  REG_TC0_RA0  = 1174;  
  REG_TC0_CCR0 = 0b101;    
  REG_TC0_IER0 = 0b00010000; 
  REG_TC0_IDR0 = 0b11101111; 
  NVIC_EnableIRQ(TC0_IRQn);
}

#include 
using namespace std;

const byte cmap[]={0b00000000,0b11100000,0b11100100,0b11101000,0b11101100,0b11110000,0b11110100,0b11111000,0b11111100,
                   0b11011100,0b10111100,0b10011100,0b01111100,0b01011100,0b00111100,0b00011100,0b00011101,0b00011110,
                   0b00011111,0b00011011,0b00010111,0b00010011,0b00001111,0b00001011,0b00000111,0b00000011,0b00100011,
                   0b01000011,0b01100011,0b10000011,0b10100011,0b11000011,0b11100011,0b11100010,0b11100001,0b11100000,0b00000000};
void loop(){
  for(int i=0;i<160;i++){
    for(int j=0;j<240;j++){     
      complex z(0,0),c(i/320.0,(j+240.0)/640.0);
      int n;
      for(n=1;n4.0)break;
      }
      fb[j][i]=cmap[sizeof(cmap)-n];
    }
  }
  for(;;);
}

vgaout.jpg|640x603

I unrolled a loop and now it is 320x240 8) The resolution could go higher but 320x240x8 is 77Kbytes of RAM already.

/* Arduino Due VGA-Out v0.2 by stimmer
http://arduino.cc/forum/index.php/topic,130742.0.html */

inline void digitalWriteDirect(int pin, boolean val){
  if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
  else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

volatile short line;
byte fb[240][320];

#define do20(x) x x x x x x x x x x x x x x x x x x x x
#define do80(x)  do20(x) do20(x) do20(x) do20(x) 
#define do320(x) do80(x) do80(x) do80(x) do80(x) 
#define MNOP(x) asm volatile (" .rept " #x "\n\t nop \n\t .endr \n\t")

void TC0_Handler()
{
    long dummy=REG_TC0_SR0; 
                           
    if(line < 480){        
        byte * p=fb[line>>1];
        MNOP(160);
        do320(REG_PIOD_ODSR = *p++;MNOP(2);)
        REG_PIOD_ODSR = 0;
    }      
    if(line==490) digitalWriteDirect(3,1); //or digitalWriteDirect(3,0); to invert vsync
    if(line==492) digitalWriteDirect(3,0); //or digitalWriteDirect(3,1); to invert vsync
    
    line++; if(line == 525) line=0;
}

void setup(){
  for(int i=0;i<320;i++)for(int j=0;j<240;j++)fb[j][i]=j+i;
  
  pinMode(3,OUTPUT);  pinMode(2,OUTPUT);                      // vsync=3 hsync=2
  pinMode(25,OUTPUT); pinMode(26,OUTPUT);                     // blue  (26=msb,25=lsb)
  pinMode(27,OUTPUT); pinMode(28,OUTPUT); pinMode(14,OUTPUT); // green (14=msb,28,27=lsb)
  pinMode(15,OUTPUT); pinMode(29,OUTPUT); pinMode(11,OUTPUT); // red   (11=msb,29,15=lsb)

  REG_PIOD_OWER= 0xff;
  REG_PMC_PCER0= 1<<27;  
  REG_PIOB_PDR = 1<<25; 
  REG_PIOB_ABSR= 1<<25; 
  REG_TC0_WPMR = 0x54494D00; 
  REG_TC0_CMR0 = 0b00000000000010011100010000000000;
//  REG_TC0_CMR0 = 0b00000000000001101100010000000000; // this inverts hsync
  REG_TC0_RC0  = 1334; 
  REG_TC0_RA0  = 1174;  
  REG_TC0_CCR0 = 0b101;    
  REG_TC0_IER0 = 0b00010000; 
  REG_TC0_IDR0 = 0b11101111; 
  NVIC_EnableIRQ(TC0_IRQn);
}

#include 
using namespace std;

const byte cmap[]={0b00000000,0b11100000,0b11100100,0b11101000,0b11101100,0b11110000,0b11110100,0b11111000,0b11111100,
                   0b11011100,0b10111100,0b10011100,0b01111100,0b01011100,0b00111100,0b00011100,0b00011101,0b00011110,
                   0b00011111,0b00011011,0b00010111,0b00010011,0b00001111,0b00001011,0b00000111,0b00000011,0b00100011,
                   0b01000011,0b01100011,0b10000011,0b10100011,0b11000011,0b11100011,0b11100010,0b11100001,0b11100000,0b00000000};
void loop(){
  for(int i=0;i<320;i++){
    for(int j=0;j<240;j++){     
      complex z(0,0),c((i+180.0)/1280.0,(j+640.0)/1280.0);
      int n;
      for(n=1;n4.0)break;
      }
      fb[j][i]=cmap[sizeof(cmap)-n];
    }
  }
  for(;;);
}

vgaout2.jpg|640x413

:open_mouth:

stimmer, you've got some serious skills here! nice work!

C

it is possible to only display black and white and get a better resolution?

640x480 black and white only should be possible. It might even be possible with 2 bit greyscale.

There's a chance 800x600 black and white might work. Anything more than that takes too much memory.

that would be nice 640x480! but just one bit!

This project can work with arduino uno? Thanks.

canter: This project can work with arduino uno? Thanks.

No, the UNO only has enough memory for something like a 120x96 black and white image.

If that is fine, there are two shields that use the TVout library that are supposed to make the job easier:

I have the Video Experimenter, but it is on my to-do pile, and I haven't tried it out yet

Superb!

This is very usefull for me - thanks

Is possible post source code for 640x480 or 800x600 black/white ?

Many thanks

Kamil

How long before we see a GPU or a graphics card from 1995 get modified to work with due/uno and a VGA monitor? :)

why 95? Someone have done it with stm32 based boards: http://hackaday.com/2012/10/08/stm32-driving-a-pcie-video-card/ and Gameduino produces vga(I think) using an FPGA adapterboard.

and, btw, the Propeller, with its 8 "core's" produces vga for games and such, if one could interface more memory on the Due (wish that could be possible on the next SAM based card) one could have kinda ok graphics.

Simple Game of Life example :-)

#define GRID_X 100
#define GRID_Y 92

#define MAX_GENERATIONS 250

uint8_t grid[2][GRID_X][GRID_Y];
uint8_t current_grid = 0;
uint8_t generations = 0;

inline void digitalWriteDirect(int pin, boolean val){
  if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
  else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

volatile short line;
byte fb[240][320];

#define do20(x) x x x x x x x x x x x x x x x x x x x x
#define do80(x)  do20(x) do20(x) do20(x) do20(x) 
#define do320(x) do80(x) do80(x) do80(x) do80(x) 
#define MNOP(x) asm volatile (" .rept " #x "\n\t nop \n\t .endr \n\t")

void TC0_Handler()
{
    long dummy=REG_TC0_SR0; 
                           
    if(line < 480){        
        byte * p=fb[line>>1];
        MNOP(160);
        do320(REG_PIOD_ODSR = *p++;MNOP(2);)
        REG_PIOD_ODSR = 0;
    }      
    if(line==490) digitalWriteDirect(3,1); //or digitalWriteDirect(3,0); to invert vsync
    if(line==492) digitalWriteDirect(3,0); //or digitalWriteDirect(3,1); to invert vsync
    
    line++; if(line == 525) line=0;
}

void setup(){
  
  pinMode(3,OUTPUT);  pinMode(2,OUTPUT);                      // vsync=3 hsync=2
  pinMode(25,OUTPUT); pinMode(26,OUTPUT);                     // blue  (26=msb,25=lsb)
  pinMode(27,OUTPUT); pinMode(28,OUTPUT); pinMode(14,OUTPUT); // green (14=msb,28,27=lsb)
  pinMode(15,OUTPUT); pinMode(29,OUTPUT); pinMode(11,OUTPUT); // red   (11=msb,29,15=lsb)

  REG_PIOD_OWER= 0xff;
  REG_PMC_PCER0= 1<<27;  
  REG_PIOB_PDR = 1<<25; 
  REG_PIOB_ABSR= 1<<25; 
  REG_TC0_WPMR = 0x54494D00; 
  REG_TC0_CMR0 = 0b00000000000010011100010000000000;
//  REG_TC0_CMR0 = 0b00000000000001101100010000000000; // this inverts hsync
  REG_TC0_RC0  = 1334; 
  REG_TC0_RA0  = 1174;  
  REG_TC0_CCR0 = 0b101;    
  REG_TC0_IER0 = 0b00010000; 
  REG_TC0_IDR0 = 0b11101111; 
  NVIC_EnableIRQ(TC0_IRQn);
  
  initGrid();
  drawGrid(); 
}


void loop(){
  
  runGrid();
 drawGrid();
 generations++;
 if (generations > MAX_GENERATIONS) {
 generations = 0;
 initGrid();
 }

}

void initGrid()
{
 int i, j;
 int t;

 current_grid = 0;
 for (i = 0; i < GRID_X; i++) {
 for (j = 0; j < GRID_Y; j++) {

 if (rand()%2) {
 grid[0][i][j] = 2;
 } else {
 grid[0][i][j] = 0;
 }
 }
 }
}

void runGrid()
{
 uint8_t x, y;
 int count;
 uint8_t value = 0;
 uint8_t new_grid;

 new_grid = 1 - current_grid;
 for (y = 0; y < GRID_Y; y++) {
 for (x = 0; x < GRID_X; x++) {
 count = count_neighbours(x, y);
 if (count < 2 || count > 3) { value = 0; }
 else if (count == 3) { value = 3; }
 else { value = grid[current_grid][x][y]; }
 grid[new_grid][x][y] = value;
 }
 }
 current_grid = new_grid;
}

int count_neighbours(int x, int y)
{
 int i, j;
 int sx;
 int result = 0;

 x--;
 y--;
 for (i = 0; i < 3; i++) {
 if (y < 0 || y > (GRID_Y - 1)) { continue; }
 for (j = 0; j < 3; j++) {
 if (x < 0 || x > (GRID_X - 1)) { continue; }
 if (i==1 && j == 1) { x++; continue; }
 if (grid[current_grid][x][y]) { result++; }
 x++;
 }
 y++;
 x -= 3;
 }
 return result;
}

void drawGrid()
{
 uint8_t  x,  y;
 uint8_t cx, cy;
 uint8_t grid_next_colour = 0;
 cx = 0;
 cy = 0;
 for (y = 0; y < GRID_Y; y++) {
 cx = 0;
 for (x = 0; x < GRID_X; x++) {
 if (grid[1-current_grid][x][y] != grid[current_grid][x][y]) {
  if(grid[current_grid][x][y]) {
    fb[cy][cx]=rand()%255;
 }
    else { 
    fb[cy][cx]=0;
 }

 }
 cx += 2;
 }
 cy += 2;
}
}

JLS1: Simple Game of Life example :-)

Nice work :D (I accidentally left it running all night, it's still going strong!)

Here's a quick status update for the VGA output project. Basically to get any further with this I need to learn ARM inline assembler, which is what I've been doing. With the code above the output is shimmering slightly and if you look closely some of the pixels are bigger than others. I do have code which solves the wide pixels problem and reduces the shimmering, however it is unreliable (sometimes it doesn't compile, sometimes it compiles but doesn't work, sometimes it works perfectly). I guess I have a little more to learn.

I haven't started on 640x480 1bpp yet but I still think it is viable. I don't know about 800x600 (the pixel rate would be 40Mpixels/sec) but there might be a way...

Finally when reading the datasheet I noticed that the SAM3X has some DMA controllers. I am strongly tempted to evaluate whether I can use them rather than the inline assembler. This is even more to learn (I've never used DMA before) but if it does work it would be a much better solution. It would use much less CPU for one thing :grin:

Simple Wolfram 1D Cellular Automata example :-)

/* Wolfram Cellular Automata 1D v1.0 */

inline void digitalWriteDirect(int pin, boolean val){
  if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
  else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

volatile short line;
byte fb[240][320];

#define do20(x) x x x x x x x x x x x x x x x x x x x x
#define do80(x)  do20(x) do20(x) do20(x) do20(x) 
#define do320(x) do80(x) do80(x) do80(x) do80(x) 
#define MNOP(x) asm volatile (" .rept " #x "\n\t nop \n\t .endr \n\t")

void TC0_Handler()
{
    long dummy=REG_TC0_SR0; 
                           
    if(line < 480){        
        byte * p=fb[line>>1];
        MNOP(160);
        do320(REG_PIOD_ODSR = *p++;MNOP(2);)
        REG_PIOD_ODSR = 0;
    }      
    if(line==490) digitalWriteDirect(3,1); //or digitalWriteDirect(3,0); to invert vsync
    if(line==492) digitalWriteDirect(3,0); //or digitalWriteDirect(3,1); to invert vsync
    
    line++; if(line == 525) line=0;
}

void setup(){
  
  pinMode(3,OUTPUT);  pinMode(2,OUTPUT);                      // vsync=3 hsync=2
  pinMode(25,OUTPUT); pinMode(26,OUTPUT);                     // blue  (26=msb,25=lsb)
  pinMode(27,OUTPUT); pinMode(28,OUTPUT); pinMode(14,OUTPUT); // green (14=msb,28,27=lsb)
  pinMode(15,OUTPUT); pinMode(29,OUTPUT); pinMode(11,OUTPUT); // red   (11=msb,29,15=lsb)

  REG_PIOD_OWER= 0xff;
  REG_PMC_PCER0= 1<<27;  
  REG_PIOB_PDR = 1<<25; 
  REG_PIOB_ABSR= 1<<25; 
  REG_TC0_WPMR = 0x54494D00; 
  REG_TC0_CMR0 = 0b00000000000010011100010000000000;
//  REG_TC0_CMR0 = 0b00000000000001101100010000000000; // this inverts hsync
  REG_TC0_RC0  = 1334; 
  REG_TC0_RA0  = 1174;  
  REG_TC0_CCR0 = 0b101;    
  REG_TC0_IER0 = 0b00010000; 
  REG_TC0_IDR0 = 0b11101111; 
  NVIC_EnableIRQ(TC0_IRQn);
  
}

void loop(){
  
  int iteration = 240;
  int lenght = 320;
  int row = 0;
  int state[lenght];
  int newstate[lenght];
  int i,j,k;
  int rules[8] = {
  int (rand()%2),
  int (rand()%2),
  int (rand()%2),
  int (rand()%2),
  int (rand()%2),
  int (rand()%2),
  int (rand()%2),
  int (rand()%2)};

  for (i=0;i

Chua chaotic oscillator example :-)

/* Chua Chaotic Oscillator v1.0 */

inline void digitalWriteDirect(int pin, boolean val){
  if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
  else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

volatile short line;
byte fb[240][320];

#define do20(x) x x x x x x x x x x x x x x x x x x x x
#define do80(x)  do20(x) do20(x) do20(x) do20(x) 
#define do320(x) do80(x) do80(x) do80(x) do80(x) 
#define MNOP(x) asm volatile (" .rept " #x "\n\t nop \n\t .endr \n\t")

void TC0_Handler()
{
    long dummy=REG_TC0_SR0; 
                           
    if(line < 480){        
        byte * p=fb[line>>1];
        MNOP(160);
        do320(REG_PIOD_ODSR = *p++;MNOP(2);)
        REG_PIOD_ODSR = 0;
    }      
    if(line==490) digitalWriteDirect(3,1); //or digitalWriteDirect(3,0); to invert vsync
    if(line==492) digitalWriteDirect(3,0); //or digitalWriteDirect(3,1); to invert vsync
    
    line++; if(line == 525) line=0;
}

void setup(){
  
  pinMode(3,OUTPUT);  pinMode(2,OUTPUT);                      // vsync=3 hsync=2
  pinMode(25,OUTPUT); pinMode(26,OUTPUT);                     // blue  (26=msb,25=lsb)
  pinMode(27,OUTPUT); pinMode(28,OUTPUT); pinMode(14,OUTPUT); // green (14=msb,28,27=lsb)
  pinMode(15,OUTPUT); pinMode(29,OUTPUT); pinMode(11,OUTPUT); // red   (11=msb,29,15=lsb)

  REG_PIOD_OWER= 0xff;
  REG_PMC_PCER0= 1<<27;  
  REG_PIOB_PDR = 1<<25; 
  REG_PIOB_ABSR= 1<<25; 
  REG_TC0_WPMR = 0x54494D00; 
  REG_TC0_CMR0 = 0b00000000000010011100010000000000;
//  REG_TC0_CMR0 = 0b00000000000001101100010000000000; // this inverts hsync
  REG_TC0_RC0  = 1334; 
  REG_TC0_RA0  = 1174;  
  REG_TC0_CCR0 = 0b101;    
  REG_TC0_IER0 = 0b00010000; 
  REG_TC0_IDR0 = 0b11101111; 
  NVIC_EnableIRQ(TC0_IRQn);
  
}

  float x = 0.5;
  float y = 0.25;
  float z = 0.125;
  float h = 0;
       
  float dt = 0.005;

  float alpha = 15.6;
  float beta = 28.58;
  float a = -1.14286;
  float b = -0.714286;

void loop(){
  
  float oldx = x;
  float oldy = y;
  float oldz = z;

  h = (b * x) + (0.5 * (a - b) * (abs(x+1) - abs(x-1)));
               
  x = oldx + dt * (alpha * (oldy - oldx - h));
  y = oldy + dt * (oldx - oldy + oldz);
  z = oldz + dt * (-beta * oldy);

  fb[120+int(65*4*y)][160+int(65*x)]=255;

}

Nice :-) It looks great in colour too, change the last line to this:

  static int c=0;
  fb[120+int(65*4*y)][160+int(65*x)]=(c++)>>12;

stimmer do you think your code can be packaged up as a library? it would make it very simple to make nice graphics apps with the Due

m

Yes, but please not just yet. I am still evaluating better ways of generating the VGA signal, which may be more steady and use less CPU. This might mean changing the pins the signal is output on. However I am not likely to change the format of the framebuffer (240x320 array) so any nice demo code people make now should still work and would be easy to use as example code in a library if authors wanted.

There's also some design decisions to make - if it turns out 640x480 works, should it be a separate library or the same one? Being the same library could make code larger and slower, but might allow mode changes at runtime. Also a library would need standard graphics functions (lines, text, bitmaps) - this is different in an 8bpp mode to a 1bpp mode. And whether to support composite output - 320x200 or 240 8-bit greyscale would be possible. I think that a single library would be more easily maintainable.

Attached are some screenshots of JLS1's demo programs. Chua oscillator has my modification for colour. Life is Conway's famous 'game' we all know and love.

Cellular automata are explained in Wolfram's 'A New Kind Of Science' chapter 6 - he classifies them into four main types (uniform, structured, random, and complex) and others which are rarer and hard to classify. Attached are two examples, cell2 is complex and cell1 is one that doesn't fit the main types.

chua.jpg|800x600

life.jpg|800x635

cell2.jpg|800x509

cell1.jpg|800x512