From ac74e42f24c0960ebeb124910c1e775420b87c76 Mon Sep 17 00:00:00 2001 From: Adam Greenwood-Byrne Date: Thu, 11 Mar 2021 22:33:27 +0000 Subject: [PATCH] Finished block copy example wgt09.c --- part12-wgt/examples/wgt03.c | 103 +----------------------------------- part12-wgt/examples/wgt09.c | 98 ++++++++++++++++++++++++++++++++++ part12-wgt/include/mb.h | 3 ++ part12-wgt/lib/mb.c | 59 +++++++++++++++++++++ part12-wgt/lib/mem.c | 14 ++--- part12-wgt/wgt.c | 53 +++++++++++++++++-- part12-wgt/wgt.h | 9 +++- part12-wgt/wnpblock.c | 79 +++++++++++++++++++++++---- 8 files changed, 294 insertions(+), 124 deletions(-) create mode 100644 part12-wgt/examples/wgt09.c diff --git a/part12-wgt/examples/wgt03.c b/part12-wgt/examples/wgt03.c index a533cd2..993dcaf 100644 --- a/part12-wgt/examples/wgt03.c +++ b/part12-wgt/examples/wgt03.c @@ -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 ()) { diff --git a/part12-wgt/examples/wgt09.c b/part12-wgt/examples/wgt09.c new file mode 100644 index 0000000..3f42ec3 --- /dev/null +++ b/part12-wgt/examples/wgt09.c @@ -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 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(" "); +} diff --git a/part12-wgt/wgt.h b/part12-wgt/wgt.h index a550572..84afbce 100644 --- a/part12-wgt/wgt.h +++ b/part12-wgt/wgt.h @@ -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); diff --git a/part12-wgt/wnpblock.c b/part12-wgt/wnpblock.c index be3cfcc..b93d809 100644 --- a/part12-wgt/wnpblock.c +++ b/part12-wgt/wnpblock.c @@ -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; + } +}