Files
mips/src/test_vga.c
T
jens b454e8dd00 - fixed libsys includes
git-svn-id: http://moon:8086/svn/mips@201 a8ebac50-d88d-4704-bea3-6648445a41b3
2021-11-23 18:42:38 +00:00

234 lines
5.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/times.h>
#include <sys/time.h>
#include <libsys/libsys.h>
#include <libsys/console.h>
#include <libsys/screen.h>
#include <libsys/uart.h>
#include <libsys/gpio.h>
#include <libsys/irq.h>
#include <libsys/mips_gfx.h>
#include <libsys/mips_ps2.h>
#define PS2_NUM_IF 2
static volatile int32_t mouse_x;
static volatile int32_t mouse_y;
static box_t box_even, box_odd, line_h_even, line_v_even, line_h_odd, line_v_odd, cross_v, cross_h;
static gfx_t gfx;
static volatile int _g_posx, _g_posy;
static volatile int g_btn_l, g_btn_r;
static volatile uint32_t g_color;
static char *ps2_name[PS2_NUM_IF] = {"PS2-0", "PS2-1"};
// -------------------------------------------------------------
void handler5(struct xcptcontext *xcp)
{
uint32_t volatile *pVGA_ctrl = (uint32_t*)SYS_VGA_CTRL;
// Ack
*pVGA_ctrl |= SYS_VGA_BIT_BUFCHG_FLAG;
}
void handler6(struct xcptcontext *xcp)
{
int p, i;
ps2_if_t *pIF;
uint8_t mouse_rsp[3];
uint32_t timeout_count, key;
int32_t dx, dy;
for (p=0; p < PS2_NUM_IF; p++)
{
pIF = (ps2_if_t*)con_getInstanceByName(ps2_name[p]);
if (SYS_PS2_BIT_RX_ERR_FLAG & *pIF->pCTRL)
fprintf(stderr, "PS2[%d]: Status = 0x%08X\n", p, *pIF->pCTRL);
if (pIF->type == ps2_type_keyb)
{
while(SYS_PS2_BIT_RX_IRQ & *pIF->pCTRL)
{
key = *pIF->pDATA;
printf("Keyboard scan code = 0x%02X\n", key);
}
}
if (pIF->type == ps2_type_mouse)
{
if(!(SYS_PS2_BIT_RX_IRQ & *pIF->pCTRL))
continue;
for (i=0; i < 3; i++)
{
timeout_count = 100;
while(!(SYS_PS2_BIT_RX_IRQ & *pIF->pCTRL))
{
timeout_count--;
if (!timeout_count)
{
break;
}
sleep(1);
}
if (!timeout_count)
{
break;
}
mouse_rsp[i] = (uint8_t)*pIF->pDATA;
}
if (!timeout_count)
{
continue;
}
dx = mouse_rsp[1];
if (mouse_rsp[0] & 0x10)
dx |= 0xFFFFFF00;
dy = mouse_rsp[2];
if (mouse_rsp[0] & 0x20)
dy |= 0xFFFFFF00;
// printf("dx = %5d, dy = %5d\n", dx, dy);
mouse_x += dx;
mouse_y -= dy;
g_btn_l = 0;
g_btn_r = 0;
if (mouse_rsp[0] & 0x01)
g_btn_l = 1;
if (mouse_rsp[0] & 0x02)
g_btn_r = 1;
if (mouse_x < 0)
mouse_x = 0;
if (mouse_y < 0)
mouse_y = 0;
if (mouse_x > (gfx.w-1))
mouse_x = gfx.w-1;
if (mouse_y >= (gfx.h-1))
mouse_y = gfx.h-1;
// Screen_csr_set_x(mouse_x);
// Screen_csr_set_y(mouse_y);
// printf("x = %5d, y = %5d\n", mouse_x, mouse_y);
}
}
}
int main(void)
{
int i;
uint32_t volatile *pVGA_ctrl = (uint32_t*)SYS_VGA_CTRL;
uint32_t volatile *pVGA_moffs_0 = (uint32_t*)SYS_VGA_FB_FRONT;
uint32_t volatile *pVGA_moffs_1 = (uint32_t*)SYS_VGA_FB_BACK;
uint32_t volatile *pVGA_cgcol = (uint32_t*)SYS_VGA_CGCOL;
uint32_t *pFlashPicture = (uint32_t*)(SYS_FLASH_MEM + 0x00600000);
uint32_t framecount;
uint32_t start, end;
uint32_t *pPic;
uint32_t rmb_posx, rmb_posy, rmb_clicked;
pPic = (uint32_t*)malloc(800*600*sizeof(uint32_t));
memcpy(pPic, pFlashPicture, 800*600*sizeof(uint32_t));
interrupt_register(5, (void*)handler5);
interrupt_register(6, (void*)handler6);
srand(clock());
UART_setbaud(con_getInstanceByName("UART-1"), 460800);
*pVGA_cgcol = 0x00FFFFFF;
_g_posx = _g_posy = 0;
rmb_posx = rmb_posy = 0;
rmb_clicked = 0;
g_color = 0xFFFFFFFF;
GFX_init(&gfx);
GFX_set_background(&gfx, pPic, 800, 600, 1, 1);
box_create(&box_even, 128, 128);
box_create(&box_odd, 126, 126);
GFX_box_draw(&gfx, &box_even, _g_posx, _g_posy, g_color);
GFX_box_draw(&gfx, &box_odd, _g_posx+1, _g_posy+1, 0);
GFX_show(&gfx);
printf("VGA status = %08X\n", *pVGA_ctrl);
PS2_init((ps2_if_t*)con_getInstanceByName(ps2_name[0]));
PS2_init((ps2_if_t*)con_getInstanceByName(ps2_name[1]));
// Flush buffer
PS2_flush((ps2_if_t*)con_getInstanceByName(ps2_name[0]));
PS2_flush((ps2_if_t*)con_getInstanceByName(ps2_name[1]));
// interrupt_enable(5);
interrupt_enable(6);
framecount = Screen_get_fps();
start = clock();
while(1)
{
if (!framecount)
{
end = clock();
framecount = Screen_get_fps();
printf("%f FPS\n", (float)framecount/((float)(end-start)/CLOCKS_PER_SEC));
start = clock();
}
GFX_frame(&gfx, GFX_FRAME_SYNC);
framecount--;
while ((_g_posx != (uint32_t)mouse_x) || (_g_posy != (uint32_t)mouse_y) || g_btn_r || g_btn_l)
{
_g_posx = (uint32_t)mouse_x;
_g_posy = (uint32_t)mouse_y;
GFX_restore(&gfx, NULL, _g_posx, _g_posy);
GFX_box_draw(&gfx, &box_even, _g_posx, _g_posy, g_color);
GFX_box_blit_bg(&gfx, &box_odd, rmb_posx+1, rmb_posy+1, _g_posx+1, _g_posy+1);
// GFX_box_draw(&gfx, &box_odd, _g_posx+1, _g_posy+1, 0);
break;
}
if (g_btn_l)
{
if (g_btn_r)
{
GFX_set_background(&gfx, pPic, 800, 600, 1, 1);
}
else
{
GFX_box_draw_bg(&gfx, &box_even, _g_posx, _g_posy, g_color);
}
}
else
{
if (g_btn_r)
{
if (!rmb_clicked)
g_color = (uint32_t)rand();
rmb_clicked = 1;
rmb_posx = _g_posx;
rmb_posy = _g_posy;
}
else
{
rmb_clicked = 0;
}
}
GFX_show(&gfx);
}
return 0;
}