Added loadspr.c with examples/wgt29.c which exercises it - a scrolly thing

This commit is contained in:
Adam Greenwood-Byrne 2021-03-15 22:04:22 +00:00
parent 6911e4630d
commit c0f33240cb
7 changed files with 299 additions and 2 deletions

View file

@ -17,11 +17,14 @@ bin/wgt1blk.o: bin/wgt1.blk
bin/wgt2blk.o: bin/wgt2.blk
$(LLVMPATH)/llvm-objcopy -I binary -O elf64-littleaarch64 -B aarch64 $< $@
bin/lettersspr.o: bin/letters.spr
$(LLVMPATH)/llvm-objcopy -I binary -O elf64-littleaarch64 -B aarch64 $< $@
%.o: %.c
$(LLVMPATH)/clang --target=aarch64-elf $(CLANGFLAGS) -c $< -o $@
kernel8.img: boot/boot.o $(OFILES) bin/wgt1pal.o bin/wgt1blk.o bin/wgt2blk.o
$(LLVMPATH)/ld.lld -m aarch64elf -nostdlib boot/boot.o $(OFILES) bin/wgt1pal.o bin/wgt1blk.o bin/wgt2blk.o -T boot/link.ld -o kernel8.elf
kernel8.img: boot/boot.o $(OFILES) bin/wgt1pal.o bin/wgt1blk.o bin/wgt2blk.o bin/lettersspr.o
$(LLVMPATH)/ld.lld -m aarch64elf -nostdlib boot/boot.o $(OFILES) bin/wgt1pal.o bin/wgt1blk.o bin/wgt2blk.o bin/lettersspr.o -T boot/link.ld -o kernel8.elf
$(LLVMPATH)/llvm-objcopy -O binary kernel8.elf kernel8.img
clean:

BIN
part12-wgt/bin/letters.spr Normal file

Binary file not shown.

110
part12-wgt/examples/wgt29.c Normal file
View file

@ -0,0 +1,110 @@
#include "wgt.h"
#include "include/mem.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 wgt29()
{
color p[256]; /* Our palette */
block sprites[1001]; /* Pointers to blocks */
char *scrolltext="THIS IS A SMALL TEXT SCROLLER CREATED WITH THE WCOPYSCREEN COMMAND. PRESS A KEY TO END THIS SCROLLER... ";
int scrnum; /* Counter for current letter of scroller */
int nextlet, i, j; /* Counters */
set_clock_rate(get_max_clock());
mem_init();
vga256 (); /* Initialize graphics mode */
for (i = 0; i < 180; i++) /* Draws a background for the screen */
{
wsetcolor (vgapal[i]);
wline (0, 0, 319, i);
wline (319, 199, 0, 180 - i);
}
wsetcolor (vgapal[0]); /* Draw with black */
wbar (0, 189, 319, 199); /* Clear area off bottom for scroller */
extern unsigned char _binary_bin_letters_spr_start[];
unsigned char *lettersspr = &_binary_bin_letters_spr_start[0];
wloadsprites (p, lettersspr, sprites, 0, 1000); // Load our blocks
do {
scrnum = 0; /* Start at first character of scroller string */
do {
/* Find pixel width of current letter in string */
nextlet = wgetblockwidth (sprites[scrolltext[scrnum] + 1]);
for (j = 0; j <= nextlet + 1; j += 2)
{
wbar (318, 189, 319, 199); /* Erase right-hand side of scroller area */
/* Now copy the last letter on at the end of the string */
wputblock (319 - j, 189, sprites[scrolltext[scrnum] + 1], 0);
/* Wait for monitor to finish vertical retrace */
wait_msec(0x3E9F); // Ugly - this needs to sync better... could use the timer I suppose... NITV!
/* Shift scroller to the left */
wcopyscreen (2, 189, 319, 199, NULL, 0, 189, NULL);
}
scrnum++; /* Advance to next letter */
} while (scrolltext[scrnum + 1] != 0);
} while (1);
wfreesprites (sprites, 0, 1000); /* Free our sprites */
}
void main()
{
wgt29();
while (1);
}

90
part12-wgt/loadspr.c Normal file
View file

@ -0,0 +1,90 @@
#include "wgt.h"
#include "include/mem.h"
short wloadsprites (color *pal, unsigned char *file, block *image_array, short start,
short end)
{
short maxsprite; /* maximum # of sprites in the file */
short startingsprite; /* First sprite in the file. Version <=3 = 1
Version 4 = 0 */
unsigned int a, b, i, spritemade;
char buf[14];
int size;
a = (unsigned int)*file;
file += 2;
/* Get the version number, and change the startingsprite accordingly.
If version <= 3, maxsprite contains the maximum number of sprites
that can be stored in a file. If version > 4, maxsprite contains
the number of the highest sprite in the file. (empty sprites at
the end are not kept in the file. */
if (a <= 3)
startingsprite = 1;
else
startingsprite = 0;
memcpy_char(&buf, file, 13);
file += 13;
if (0 == memcmp (" Sprite File ", &buf[0], 13))
/* see if it is a sprite file */
{
for (int k=0;k<256;k++) {
pal[k].r = 4 * *file++;
pal[k].g = 4 * *file++;
pal[k].b = 4 * *file++;
}
wsetpalette (0, 255, pal);
maxsprite = (short)*file; /* maximum sprites in this file */
file += 2;
for (i = start; i <= end; i++) /* make all sprites null
(still need to call wfreesprites) */
image_array[i] = NULL;
for (i = startingsprite; i <= maxsprite; i++) /* load them in */
{
spritemade = (unsigned int)*file; /* flag to see if sprite exists */
file += 2;
if (spritemade == 1)
{
a = (unsigned int)*file; /* get width and height */
file += 2;
b = (unsigned int)*file;
file += 2;
if ((i >= start) && (i <= end)) /* Load this one */
{
size = a * b;
image_array[i] = (block)malloc ((size * 4) + 12);
block arr = image_array[i];
*arr++ = a;
*arr++ = b;
for (int k=0;k<size;k++) *arr++ = vgapal[*file++];
}
else /* Don't load this one */
file += size;
}
}
}
return (0);
}
void wfreesprites (block *image_array, short start, short end)
{
short i;
for (i = start; i <= end; i++)
{
if (image_array[i] != NULL)
{
free (image_array[i]);
image_array[i] = NULL;
}
}
}

View file

@ -45,6 +45,29 @@ void *memcpy_xray(void *dest, const void *src, unsigned len)
return dest;
}
void *memcpy_char(void *dest, const void *src, unsigned len)
{
unsigned char *d = dest;
const unsigned char *s = src;
while (len--)
*d++ = *s++;
return dest;
}
int memcmp(char *str1, char *str2, unsigned count)
{
char *s1 = str1;
char *s2 = str2;
while (count-- > 0)
{
if (*s1++ != *s2++)
return s1[-1] < s2[-1] ? -1 : 1;
}
return 0;
}
int abs(int i)
{
return i < 0 ? -i : i;

View file

@ -56,6 +56,8 @@ extern short bx,by,tx,ty;
void *memset(void *dest, unsigned int val, unsigned len);
void *memcpy(void *dest, const void *src, unsigned len);
void *memcpy_xray(void *dest, const void *src, unsigned len);
void *memcpy_char(void *dest, const void *src, unsigned len);
int memcmp(char *str1, char *str2, unsigned count);
int abs(int i);
int strlen(const char *str);
@ -118,3 +120,6 @@ void wnormscreen (void);
void wvertres (short x, short y, short y2, block image);
void wwipe (short x, short y, short x2, short y2, block image);
void wskew (short x, short y, block image, short degrees);
short wloadsprites (color *pal, unsigned char *file, block *image_array, short start, short end);
void wfreesprites (block *image_array, short start, short end);
void wcopyscreen (short x, short y, short x2, short y2, block source, short destx, short desty, block dest);

View file

@ -1,5 +1,7 @@
#include "wgt.h"
#define fastcopy memcpy
void wsetscreen (block image)
{
if (image == NULL)
@ -31,3 +33,67 @@ void wnormscreen (void)
bx = WGT_SYS.xres - 1;
by = WGT_SYS.yres - 1;
}
void wcopyscreen (short x, short y, short x2, short y2, block source, short destx, short desty, block dest)
{
unsigned int swidth, dwidth;
short width, height, display;
short ctr; /* Y line counter */
if (source == NULL)
{
source = abuf; /* Set to visual screen */
swidth = 320;
}
else {
swidth = *source;
source += 2; /* Skip over width and height */
}
width = abs (x - x2) + 1; /* Width of each row to copy */
height = abs (y - y2) + 1; /* Height of area to copy */
if (destx + width > bx)
display = bx + 1 - destx;
else display = width; /* Clip width */
if (height > (by + 1 - desty))
height = (by + 1 - desty);
source += x + (y * swidth); /* Clip height */
if (desty < ty)
{
source += (ty - desty) * swidth;
height -= ty - desty;
desty = ty;
} /* clip y */
if (destx < tx)
{
source += tx - destx;
display -= tx - destx;
destx = tx;
} /* clip x */
if (dest == NULL)
{
dest = abuf; /* Set to visual screen */
dwidth = 320;
}
else
{
dwidth = *dest;
dest += 2;
}
/* Skip over width & height in image data */
dest += destx + (desty * dwidth); /* Find row offset in image data */
if ((height > 0) && (display > 0))
for (ctr = y; ctr < y + height; ctr++)
{
fastcopy (dest, source, display); /* Copy DISPLAY bytes to dest */
source += swidth; /* Advance one row */
dest += dwidth;
}
}