Finished block copy example wgt09.c

This commit is contained in:
Adam Greenwood-Byrne 2021-03-11 22:33:27 +00:00
parent 20a02422e7
commit ac74e42f24
8 changed files with 294 additions and 124 deletions

View file

@ -55,108 +55,8 @@ void getch(void) {
int timer; /* Counts how many times it has been called */
int clearcount; /* Counts the number of screen clears. */
int curx = 0;
int cury = 0;
extern int get_el(void);
void debugstr(char *str) {
if (curx + (strlen(str) * 8) >= 1920) {
curx = 0; cury += 8;
}
if (cury + 8 >= 1080) {
cury = 0;
}
wtextcolor(vgapal[15]);
wouttextxy (curx, cury, NULL, str);
curx += (strlen(str) * 8);
}
void debugcrlf(void) {
curx = 0; cury += 8;
}
void debugch(unsigned char b) {
unsigned int n;
int c;
for(c=4;c>=0;c-=4) {
n=(b>>c)&0xF;
n+=n>9?0x37:0x30;
debugstr((char *)&n);
}
debugstr(" ");
}
void debughex(unsigned int d) {
unsigned int n;
int c;
for(c=28;c>=0;c-=4) {
n=(d>>c)&0xF;
n+=n>9?0x37:0x30;
debugstr((char *)&n);
}
debugstr(" ");
}
int get_max_clock()
{
mbox[0] = 8*4; // Length of message in bytes
mbox[1] = MBOX_REQUEST;
mbox[2] = MBOX_TAG_GETCLKMAXM; // Tag identifier
mbox[3] = 8; // Value size in bytes
mbox[4] = 0; // Value size in bytes
mbox[5] = 0x3; // Value
mbox[6] = 0; // Rate
mbox[7] = MBOX_TAG_LAST;
if (mbox_call(MBOX_CH_PROP)) {
if (mbox[5] == 0x3) {
return mbox[6];
}
}
return 0;
}
int get_clock_rate()
{
mbox[0] = 8*4; // Length of message in bytes
mbox[1] = MBOX_REQUEST;
mbox[2] = MBOX_TAG_GETCLKRATE; // Tag identifier
mbox[3] = 8; // Value size in bytes
mbox[4] = 0; // Value size in bytes
mbox[5] = 0x3; // Value
mbox[6] = 0; // Rate
mbox[7] = MBOX_TAG_LAST;
if (mbox_call(MBOX_CH_PROP)) {
if (mbox[5] == 0x3) {
return mbox[6];
}
}
return 0;
}
int set_clock_rate(unsigned int rate)
{
mbox[0] = 9*4; // Length of message in bytes
mbox[1] = MBOX_REQUEST;
mbox[2] = MBOX_TAG_SETCLKRATE; // Tag identifier
mbox[3] = 12; // Value size in bytes
mbox[4] = 0; // Value size in bytes
mbox[5] = 0x3; // Value
mbox[6] = rate; // Rate
mbox[7] = 0; // Rate
mbox[8] = MBOX_TAG_LAST;
if (mbox_call(MBOX_CH_PROP)) {
if (mbox[5] == 0x3 && mbox[6] == rate) {
return 1;
}
}
return 0;
}
void timer_routine (void)
{
timer++;
@ -185,8 +85,7 @@ void wgt03()
winittimer ();
wstarttimer (timer_routine, TIMERSPEED);
curx = 0;
cury = 0;
debugreset();
while (!kbhit ())
{

View file

@ -0,0 +1,98 @@
#include "wgt.h"
#include "include/mem.h"
#include "include/mb.h"
// ######## REQUIRED FUNCTIONS ########
unsigned long state0 = 1000;
unsigned long state1 = 2000;
unsigned long rand(void)
{
unsigned long s1 = state0;
unsigned long s0 = state1;
state0 = s0;
s1 ^= s1 << 23;
s1 ^= s1 >> 17;
s1 ^= s0;
s1 ^= s0 >> 26;
state1 = s1;
return state0 + state1;
}
void wait_msec(unsigned int n)
{
register unsigned long f, t, r;
// Get the current counter frequency
asm volatile ("mrs %0, cntfrq_el0" : "=r"(f));
// Read the current counter
asm volatile ("mrs %0, cntpct_el0" : "=r"(t));
// Calculate expire value for counter
t+=((f/1000)*n)/1000;
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r<t);
}
// ######## STUB FUNCTIONS ########
unsigned int kb = 0;
unsigned int kbhit(void) {
kb++;
return kb / 500;
}
void getch(void) {
wait_msec(0x500000);
kb = 0;
}
// ######## WGT EXAMPLES ########
void wgt09()
{
short i, x, y;
block screen1; /* a full screen */
block part1; /* part of the screen */
set_clock_rate(get_max_clock());
mem_init();
vga256 ();
for (i = 1; i < 1080; i++)
{
wsetcolor (vgapal[i % 255]);
wline (0, 0, 1919, i);
wline (1919, 1079, 0, 1079 - i);
}
screen1 = wnewblock (0, 0, 1919, 1079); /* capture the entire screen */
part1 = wnewblock (0, 0, 900, 810); /* get a part of the screen */
/* Note that wnewblock allocates the memory for the block */
wcls (vgapal[0]);
do {
x = rand() % 1920;
y = rand() % 1080;
wputblock (x, y, part1, 0); /* put the part somewhere */
} while (!kbhit ());
getch ();
wputblock (0, 0, screen1, 0); /* replace the mess with the */
/* original screen */
getch (); /* get the key */
wfreeblock (screen1); /* *** make sure to free the memory! */
wfreeblock (part1);
}
void main()
{
wgt09();
while (1);
}

View file

@ -37,3 +37,6 @@ enum {
};
unsigned int mbox_call(unsigned char ch);
int get_max_clock();
int get_clock_rate();
int set_clock_rate(unsigned int rate);

View file

@ -1,4 +1,5 @@
#include "../include/io.h"
#include "../include/mb.h"
// The buffer must be 16-byte aligned as only the upper 28 bits of the address can be passed via the mailbox
volatile unsigned int __attribute__((aligned(16))) mbox[36];
@ -37,3 +38,61 @@ unsigned int mbox_call(unsigned char ch)
}
return 0;
}
int get_max_clock()
{
mbox[0] = 8*4; // Length of message in bytes
mbox[1] = MBOX_REQUEST;
mbox[2] = MBOX_TAG_GETCLKMAXM; // Tag identifier
mbox[3] = 8; // Value size in bytes
mbox[4] = 0; // Value size in bytes
mbox[5] = 0x3; // Value
mbox[6] = 0; // Rate
mbox[7] = MBOX_TAG_LAST;
if (mbox_call(MBOX_CH_PROP)) {
if (mbox[5] == 0x3) {
return mbox[6];
}
}
return 0;
}
int get_clock_rate()
{
mbox[0] = 8*4; // Length of message in bytes
mbox[1] = MBOX_REQUEST;
mbox[2] = MBOX_TAG_GETCLKRATE; // Tag identifier
mbox[3] = 8; // Value size in bytes
mbox[4] = 0; // Value size in bytes
mbox[5] = 0x3; // Value
mbox[6] = 0; // Rate
mbox[7] = MBOX_TAG_LAST;
if (mbox_call(MBOX_CH_PROP)) {
if (mbox[5] == 0x3) {
return mbox[6];
}
}
return 0;
}
int set_clock_rate(unsigned int rate)
{
mbox[0] = 9*4; // Length of message in bytes
mbox[1] = MBOX_REQUEST;
mbox[2] = MBOX_TAG_SETCLKRATE; // Tag identifier
mbox[3] = 12; // Value size in bytes
mbox[4] = 0; // Value size in bytes
mbox[5] = 0x3; // Value
mbox[6] = rate; // Rate
mbox[7] = 0; // Rate
mbox[8] = MBOX_TAG_LAST;
if (mbox_call(MBOX_CH_PROP)) {
if (mbox[5] == 0x3 && mbox[6] == rate) {
return 1;
}
}
return 0;
}

View file

@ -1,16 +1,16 @@
extern unsigned char _end[];
// Define the heap
extern unsigned char _end[];
// unsigned char *HEAP_START = &_end[0]; // End of kernel
unsigned char *HEAP_START = (unsigned char *)0x400000; // Top of stack
unsigned int HEAP_SIZE = 0x30000000; // Max heap size is 764Mb
unsigned int HEAP_SIZE = 0x30000000; // Max heap size is 768Mb
unsigned char *HEAP_END;
// Set up some globals
// Set up some static globals
unsigned char *freeptr;
unsigned int allocated = 0;
static unsigned char *freeptr;
static unsigned int bytes_allocated = 0;
void mem_init()
{
@ -33,7 +33,7 @@ void *malloc(unsigned int size)
return 0;
} else {
freeptr += size;
allocated += size;
bytes_allocated += size;
return allocated;
}

View file

@ -4,11 +4,14 @@ block abuf; /* pointer to the active screen */
unsigned int currentcolor;
short tx = 0,ty = 0,bx = 1919,by = 1079; /* clipping variables */
int curx = 0;
int cury = 0;
wgt_sys WGT_SYS;
// ######## HELPER FUNCTIONS ########
void *memset(void *dest, int val, unsigned len)
void *memset(void *dest, unsigned int val, unsigned len)
{
unsigned int *ptr = dest;
while (len-- > 0)
@ -16,10 +19,11 @@ void *memset(void *dest, int val, unsigned len)
return dest;
}
void *memcpy(void *dest, const void *src, unsigned len)
{
char *d = dest;
const char *s = src;
unsigned int *d = dest;
const unsigned int *s = src;
while (len--)
*d++ = *s++;
return dest;
@ -44,3 +48,46 @@ void wcls (unsigned int col)
{
memset (abuf, col, WGT_SYS.xres * WGT_SYS.yres);
}
void debugstr(char *str) {
if (curx + (strlen(str) * 8) >= 1920) {
curx = 0; cury += 8;
}
if (cury + 8 >= 1080) {
cury = 0;
}
wtextcolor(vgapal[15]);
wouttextxy (curx, cury, NULL, str);
curx += (strlen(str) * 8);
}
void debugcrlf(void) {
curx = 0; cury += 8;
}
void debugreset(void) {
curx = 0; cury = 0;
}
void debugch(unsigned char b) {
unsigned int n;
int c;
for(c=4;c>=0;c-=4) {
n=(b>>c)&0xF;
n+=n>9?0x37:0x30;
debugstr((char *)&n);
}
debugstr(" ");
}
void debughex(unsigned int d) {
unsigned int n;
int c;
for(c=28;c>=0;c-=4) {
n=(d>>c)&0xF;
n+=n>9?0x37:0x30;
debugstr((char *)&n);
}
debugstr(" ");
}

View file

@ -46,13 +46,19 @@ extern short bx,by,tx,ty;
// ######## HELPER FUNCTIONS ########
void *memset(void *dest, int val, unsigned len);
void *memset(void *dest, unsigned int val, unsigned len);
void *memcpy(void *dest, const void *src, unsigned len);
int abs(int i);
int strlen(const char *str);
// ######## WGT FUNCTIONS ########
void debugstr(char *str);
void debugcrlf(void);
void debugreset(void);
void debugch(unsigned char b);
void debughex(unsigned int d);
void vga256(void);
void wsetcolor (unsigned int col);
void wline (short x, short y, short x2, short y2);
@ -85,6 +91,7 @@ void wfreeblock (block ptr);
short wgetblockwidth (block ptr);
short wgetblockheight (block ptr);
block wnewblock (short x, short y, short x2, short y2);
void wputblock (short x, short y, block src, short method);
block wallocblock (short width, short height);
void wdonetimer (void);
void winittimer (void);

View file

@ -1,6 +1,10 @@
#include "wgt.h"
#include "include/mem.h"
// We should replace this with faster version (and xray needs to do the right thing - not sure memcpy is a good replacement)
#define fastcopy memcpy
#define putxray memcpy
block wnewblock (short x, short y, short x2, short y2)
{
block ptr,orig;
@ -11,7 +15,7 @@ block wnewblock (short x, short y, short x2, short y2)
width = abs (x - x2) + 1;
height = abs (y - y2) + 1;
size = (int)width * (int)height + 5;
size = 4 * ((int)width * (int)height) + 12;
if (x2 < x)
{
temp = x; x = x2; x2 = temp;
@ -20,14 +24,16 @@ block wnewblock (short x, short y, short x2, short y2)
{
temp = y; y = y2; y2 = temp;
}
ptr = malloc (size);
if (ptr == NULL)
return NULL;
orig = ptr;
*(short *)ptr = width; /* store the width */
ptr += 2; /* and height */
*(short *)ptr = height;
ptr += 2;
*ptr = width; /* store the width */
ptr ++; /* and height */
*ptr = height;
ptr ++;
dispofs = y * WGT_SYS.xres + x;
temp = width;
@ -49,16 +55,67 @@ block wallocblock (short width, short height)
block ptr,orig;
int size;
size = (int)width * (int)height + 5;
size = 4 * ((int)width * (int)height) + 12;
ptr = malloc (size);
if (ptr == NULL)
return NULL;
orig = ptr;
*(short *)ptr = width; /* store the width */
ptr += 2;
*(short *)ptr = height; /* and height */
ptr += 2;
*ptr = width; /* store the width */
ptr ++;
*ptr = height; /* and height */
ptr ++;
memset (ptr, 0, size - 4);
memset (ptr, 0, (size - 12) / 4);
return orig;
}
void wputblock (short x, short y, block src, short method)
{
short width, height, display, maxy;
short ctr;
block dst;
if (src == NULL)
return;
width = *src;
src ++;
height = *src;
src ++;
if (x + width > bx)
display = (bx + 1) - x;
else display = width;
if (x < tx)
{
src += tx - x;
display -= tx - x;
x = tx;
} /* clip x */
if (display <= 0)
return;
maxy = y + height - 1;
if (maxy > by)
maxy = by;
if (y < ty)
{
src += (ty - y)*width;
y = ty;
} /* clip y */
dst = &abuf[y * WGT_SYS.xres + x];
if (method == 0)
for (ctr = y; ctr <= maxy; ctr++)
{
fastcopy (dst, src, display);
src += width;
dst += WGT_SYS.xres;
}
else
for (ctr = y; ctr <= maxy; ctr++)
{
putxray (dst, src, display);
src += width;
dst += WGT_SYS.xres;
}
}