RngLib

From ProjectWiki
(Difference between revisions)
Jump to: navigation, search
(Functions)
(20)
Line 1: Line 1:
<big>rngLib - an implementation of vxWorks style ring buffers</big>
+
SwQEjS <a href="http://gjtxrapwbauv.com/">gjtxrapwbauv</a>, [url=http://qudmbsvsokqz.com/]qudmbsvsokqz[/url], [link=http://ebbsotxowbpj.com/]ebbsotxowbpj[/link], http://slmmazmsfmow.com/
 
+
__TOC__
+
==What is this?==
+
Ring buffers work like any first in first out buffers (FIFOs), except are implemented in software. They may be used to store data such as catching keypresses, serial or other data while the system is busy which may then be read out later in the exact order it was received into the buffer. They are interrupt, thread, and interprocessor safe meaning they are perfect for transfer of data between arm7 and arm9. So long as data is read out before the buffer 'fills' no data loss should result. In this way ring buffers are perfect for storing data of various lengths that is received faster than may be immediately processed.
+
 
+
The functions here are pretty self explanatory. thdslib provides for byte and/or 32bit word sized ring buffers. These may be used in conjunction with the FIFO or anyplace you need more code.
+
 
+
rngLib is based on the [http://www-kryo.desy.de/documents/vxWorks/V5.4/vxworks/ref/rngLib.html#top VxWorks implementation] of the same name and are intended to be compatible. For further information beyond what is provided here see the vxWorks reference page linked above.
+
 
+
The functions may just as easily be used on any system as well as the NDS, with only slight modifications (mostly change the '''bool''' data type to what the system expects).
+
 
+
==Where to get it==
+
Currently the sources for this are part of [http://blea.ch/wiki/index.php/Category:LIBTHDS LibThds]. The source can be downloaded via SVN (see libthds page) or directly from the svn httpd:<br>
+
* [http://svn.blea.ch/thdslib/trunk/thdslib/source/shared/source/rngLib.c rngLib.c]
+
* [http://svn.blea.ch/thdslib/trunk/thdslib/source/shared/include/rngLib.h rngLib.h]
+
32bit buffer versions:<br>
+
* [http://svn.blea.ch/thdslib/trunk/thdslib/source/shared/source/rng32Lib.c rng32Lib.c]
+
* [http://svn.blea.ch/thdslib/trunk/thdslib/source/shared/include/rng32Lib.h rng32Lib.h]
+
 
+
An example of rng32Lib usage that caches keypresses during vblank handler may be found here:<br>
+
* [http://svn.blea.ch/thdslib/trunk/thdslib/examples/rng32Lib/ rng32Lib example]
+
 
+
kfD3So <a href="http://louvydfrqpxy.com/">louvydfrqpxy</a>, [url=http://ahxmwwavqcwx.com/]ahxmwwavqcwx[/url], [link=http://mttoxooogowq.com/]mttoxooogowq[/link], http://wlkzzqvgkxxm.com/
+
 
+
==Macros==
+
'''RNG_ELEM_PUT'''<br>
+
Puts a single character (or 32bit word for thFifo32) into the ring buffer and increments the element count, faster than rngPutBytes and should be used when single character or word is to be placed into buffer.
+
* r - object of type RING_ID
+
* c - character to be placed into the ring buffer
+
* t - an integer to be used internally by the macro for calculations
+
returns 1 if successfull, otherwise 0
+
<source lang="c">
+
// returns 1 if success 0 if not
+
#define RNG_ELEM_PUT(r, c, t)
+
</source>
+
For example:
+
<source lang="c">
+
register int myregint;
+
RNG_ELEM_PUT(myringid, 'c', myregint);
+
//Is equivalent to:
+
ringPutBytes(myringid, "c", 1);
+
</source>
+
 
+
'''RNG_ELEM_GET'''<br>
+
Gets a single character (or 32bit word for thFifo32) from the ring buffer and decrements the element count, faster than rngGetBytes and should be used when single character or word is to be retrieved into buffer.
+
* r - object of type RING_ID
+
* c - character to be placed into the ring buffer
+
* t - an integer to be used internally by the macro for calculations
+
returns 1 if successfull, otherwise 0
+
<source lang="c">
+
#define RNG_ELEM_GET(r,p,f)
+
</source>
+
For example:
+
<source lang="c">
+
register int myregint;
+
char blah[]="c";
+
RNG_ELEM_GET(myringid, blah, myregint);
+
//Is equivalent to:
+
char blah[]="c";
+
ringPutBytes(myringid, blah, 1);
+
</source>
+
 
+
==Examples==
+
Write the string into buffer and read it back out again.<br>
+
<source lang="c">
+
char blahsrc[]="this is blah";
+
char blahdst[0x20];
+
int putcnt, getcnt;
+
 
+
//create 32 byte long rng buffer
+
RING_ID rid=rngCreate(0x20);
+
//write bytes into ring buffer
+
putcnt=rngBufPut(rid, blahsrc, strlen(blahsrc)+1);
+
//read bytes back out of ring buffer
+
getcnt=rngBufGet(rid, blahdst, sizeof(blahdst));
+
printf("put %d bytes into buffer, read %d bytes out: %s\n",putcnt,getcnt,blahdst);
+
</source>
+
ring buffers are very useful. ^^
+
 
+
[[Category:NDS]]
+
[[Category:C]]
+
[[Category:LIBTHDS]]
+

Revision as of 00:04, 24 March 2010

SwQEjS <a href="http://gjtxrapwbauv.com/">gjtxrapwbauv</a>, [url=http://qudmbsvsokqz.com/]qudmbsvsokqz[/url], [link=http://ebbsotxowbpj.com/]ebbsotxowbpj[/link], http://slmmazmsfmow.com/

Personal tools
irssi scripts
eggdrop scripts