#include <stdio.h>
#include <stdlib.h>

#include "system.h"
#include "io.h"

#define VGA_WIDTH       800
#define VGA_HEIGHT      600
#define VGA_BUFFER_SIZE (VGA_WIDTH * VGA_HEIGHT)
#define VGA_SET_PIXEL(x, y, color) IOWR_8DIRECT((unsigned long)vga_buffer, (y * VGA_WIDTH) + x, color)

unsigned char vga_buffer[VGA_BUFFER_SIZE];

void nios_vga_init(void)
{
    int x, y;
    IOWR(VGA_3BITS_BASE, 1, 0);
    IOWR(VGA_3BITS_BASE, 0, (unsigned long)vga_buffer);
    for (y = 0;y < VGA_HEIGHT;y ++) {
        for (x = 0;x < VGA_WIDTH;x ++) {
            if (y < 200) VGA_SET_PIXEL(x, y, 0x01);
            else if (y < 400) VGA_SET_PIXEL(x, y, 0x02);
            else VGA_SET_PIXEL(x, y, 0x04);
        }
    }
    IOWR(VGA_3BITS_BASE, 1, 1);
}

int main()
{
    nios_vga_init();
    printf("Hello from Nios II!\n");
    while (1);
    return 0;
}
