Misc functions

Other misc things
Post Reply
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Misc functions

Post by stefano »

This is a set of non-standard C functions I converted from ancient set for the Sorcerer Exidy.
They're too generic to be kept in a target specific library and rather simple to be worth for an added library.

Code: Select all

/*=======================================================
 
        Resets (clears) the bit 'bit' in the word pointed
        to by 'word'.
        No return value.
 
        Bits are numbered 0 to 15, right to left.
*/
 
#include <sorcerer.h>

void clrbit(unsigned int *word, int bit)
 
{
        *word=*word&(~(1<<bit));
}


Code: Select all


/*=======================================================
 
        Sets the bit 'bit' in the word pointed to by
        'word'.
        No return value.
 
        Bits are numbered 0 to 15, right to left.
*/

#include <sorcerer.h>
 
void setbit(unsigned int *word, int bit)
{
        *word=*word|(1<<bit);
}

Code: Select all

/*=======================================================
 
        Swaps the contents of the words pointed to by
        p1 and p2.
        ie: if w1=1234 and w2=5678 and p1 points to w1
        and p2 points to w2, then after swap(p1,p2)
        w1 will contain 5678 and w2 will contain 1234.
 
        (See K & R, p117)
        No return value.
*/
 
#include <sorcerer.h>

void swap(int *p1, int *p2)
 
{       int *temp;
 
        *temp=*p1;
        *p1=*p2;
        *p2=*temp;
}


Code: Select all

;============================================================= 
;
; int swaphl(int p1)
;
;    Returns the 16 bit word P1 with HI and LO bytes swapped.
;

SECTION code_clib

PUBLIC swaphl
PUBLIC _swaphl

swaphl:
_swaphl:
        LD      d,h             ;and swap it!
        LD      h,l
        LD      l,d
        ret


'swapin()' was a function in BDS C to load a block of memory from disk.

Code: Select all

/*=======================================================
 
        Undoes what swapin does:
        ie swaps out to a filename pointed to by name
        the contents of memory between saddr and eaddr
        (inclusive).
        Previous contents are discarded.
        Returns either
                0 if successful, or
                -1 otherwise.
*/

#define __HAVESEED

#include <sorcerer.h>
#include <stdio.h>
#include <unistd.h>

int swapout(char *name, int saddr, int eaddr)
 
{       int     nbl;
        int     fd;
 
//        if ((fd=creat(name)) == -1) return fd;
        if ((fd=fopen(filename,"wb"))==0) return fd;
        nbl=eaddr-saddr+1;
//        if (write(fd,saddr,nbl) != nbl) {
        if (fwrite(saddr, 1, nbl, fd) != nbl) {
           unlink(name);
           return -1;
        }
        fclose(fd);
        return 0;
}


Code: Select all

/*=======================================================
 
        Returns:
          1 if bit 'bit' is set in the word pointed to
            by 'word', or
          0 otherwise.
 
        Bits are numbered 0 to 15, right to left.
*/
 
#include <sorcerer.h>

int tstbit(unsigned int *word, int bit)
 
{
        if ((1<<bit)&*word)
          return 1;
        else
          return 0;
}


pjshumphreys
Member
Posts: 66
Joined: Sat Feb 06, 2021 2:32 pm

Re: Misc functions

Post by pjshumphreys »

Is it relevant to talk about other target specific libraries in this thread?

Two I know of are the AsmCall function that's part of asmlib for the msx target and the pcx image decoding functionality that's part of sgtools for the c128 target.

Both of these would be good to have all targets. It's not something I need of course, it would just be cool to have. I still want to test the new cassette routines on real hardware :S

Oh and there's an sdcard folder in /libsrc as well. Is that for generic fat32 sd card access somehow? I haven't been able to find any documentation about how to use that. It would be cool to hook up an sd card to any z80 target without a microcontroller by getting the z80 to talk SPI with just TTL logic assistance. Maybe this project does that? https://jeelabs.org/2018/z80-in-a-red-box/
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: Misc functions

Post by jorgegv »

Just a small enhancement (a classic):

Code: Select all

void swap(int *p1, int *p2)
{
         // no temps needed
         *p1^=*p2;
         *p2^=*p1;
         *p1^=*p2;
}
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: Misc functions

Post by dom »

pjshumphreys wrote: Thu Oct 27, 2022 12:31 am Two I know of are the AsmCall function that's part of asmlib for the msx target
I thought I'd made that available to every z80 target?
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Re: Misc functions

Post by stefano »

> Two I know of are the AsmCall function that's part of asmlib for the msx target...

To create a special z88dk library is a good point, I think we should.


A further comment regarding swapin(). It was a BDS C function, slightly different than the forementioned swapout().
It was inserted in the libraries to support the overlays and loaded a whole file at the given memory position, with no size limits.

The BDS C read() function is slightly differs from fread(): read(file, buffer, blocks) -> fread(buffer, 1, blocks*128, file)
By the way the code (getting up to 512 blocks !) was:

Code: Select all

/* Load a disk file into memory (typically an overlay segment): */

swapin(name,addr)
char *name;
{
	int fd;
	if (( fd = open(name,0)) == ERROR)
		return ERROR;

	if ((read(fd,addr,512)) < 0) {
		close(fd);
		return ERROR;
	}

	bdos(26, 0x80);	/* reset DMA address for benefit of certain systems */

	close(fd);
	return OK;
}

It first needs to be adjusted to work on z88dk.
The "reset DMA" is an interesting clue we could investigate on for the open issue regarding the BDOS functions used in the fcntl library and the possible speed improvements.

Regarding the BDS C library, there's a still a part in assembly which we could borrow, e.g. rename() and remove().
I've never done it because they're in two flavors, the ZCPR version being more articulated than the standard CP/M one, honestly I haven't fully understood why.

By the way, I'm not sure about introducing swapin() and swapout(), the whole file access section behaves too differently.
Generally speaking, a "load file" command could be useful, as well a save one. If we choose swapin(), I'd add a file size limit.

EDIT: the SDCARD library is borrowed from the v6z80p project. I checked the low level functionality on an emulated ZX Spectrum (RealSpec) and it all looked fine.
I remember I had to deal with a zccz80 bug we had at the time with the bit shifting with the long type, (just to underline that those functions were checked).

That project includes a FAT implementation in assembly code, well, the whole project is very cool.
derekfountain
Member
Posts: 121
Joined: Mon Mar 26, 2018 1:49 pm

Re: Misc functions

Post by derekfountain »

jorgegv wrote: Thu Oct 27, 2022 8:44 am Just a small enhancement (a classic):

Code: Select all

void swap(int *p1, int *p2)
{
         // no temps needed
         *p1^=*p2;
         *p2^=*p1;
         *p1^=*p2;
}
Have you seen what the compiler makes of that? An enhancement it is not... :)
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: Misc functions

Post by jorgegv »

derekfountain wrote: Thu Oct 27, 2022 1:17 pm Have you seen what the compiler makes of that? An enhancement it is not... :)
Whoa, indeed!
pjshumphreys
Member
Posts: 66
Joined: Sat Feb 06, 2021 2:32 pm

Re: Misc functions

Post by pjshumphreys »

I thought I'd made that available to every z80 target?
Oh cool. I only looked when I first started using it. I tried again recently and it didn't work for me, but I guess that's because I was trying to compile for 8080.
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Re: Misc functions

Post by stefano »

>>Have you seen what the compiler makes of that? An enhancement it is not... :)
>Whoa, indeed!

We could make the equivalent in asm
Post Reply