Copyright (c) Hyperion Entertainment and contributors.

Difference between revisions of "Resources"

From AmigaOS Documentation Wiki
Jump to navigation Jump to search
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Resources =
+
== Introduction ==
   
  +
Resources are responsible for low-level hardware control, see [[Exec Resources]] to have a better insight of what it is and how it's used.
The Amiga’s low-level hardware control functions are collectively referred to as “Resources”. Most applications will never need to use the hardware at the resource level—the Amiga’s device interface is much more convenient and provides for multitasking. However, some high performance applications, such as MIDI time stamping, may require direct access to the Amiga hardware registers.
 
   
== The Amiga Resources ==
+
== System Resources ==
   
  +
[[BattClock Resource]]
There are currently seven standard resources in the Amiga system. The following lists the name of each resource and its function.
 
   
  +
[[BattMem Resource]]
grants access to the battery-backed clock chip.
 
   
  +
[[CIA Resource]]
grants access to non-volatile RAM.
 
   
  +
[[Disk Resource]]
grants access to the interrupts and timer bits of the 8520 CIA (Complex Interface Adapter) chips.
 
   
  +
[[DMA Resource]]
grants temporary exclusive access to the disk hardware.
 
   
  +
[[FileSystem Resource]]
grants access to the file system.
 
   
  +
[[Misc Resource]]
grants exclusive access to functional blocks of chip registers. At present, definitions have been made for the serial and parallel hardware only.
 
   
  +
[[Potgo Resource]]
manages the bits of the proportional I/O pins on the game controller ports.
 
   
  +
[[PerformanceMonitor Resource]]
The resources allow you direct access to the hardware in a way that is compatible with multitasking. They also allow you to temporarily bar other tasks from using the resource. You may then use the associated hardware directly for your special purposes. If applicable, you must return the resource back to the system for other tasks to use when you are finished with it.
 
   
  +
[[Xena Resource]]
See the ''Amiga Hardware Reference Manual'' for detailed information on the actual hardware involved.
 
 
<sub>b</sub>oxLook Before You Leap.Resources are just one step above direct hardware manipulation. You are advised to try the higher level device and library approach before resorting to the hardware.
 
 
== Resource Interface ==
 
 
Resources provide functions that you call to do low-level operations with the hardware they access. In order to use the functions of a resource, you must obtain a pointer to the resource. This is done by calling the OpenResource() function with the resource name as its argument.
 
 
 
 
OpenResource()
 
 
returns a pointer to the resource you request or NULL if it does not exist.
 
 
<pre>#include &lt;resources/filesysres.h&gt;
 
 
struct FileSysResource *FileSysResBase = NULL;
 
 
if (!(FileSysResBase = OpenResource(FSRNAME)))
 
printf(&quot;Cannot open %s\n&quot;,FSRNAME);</pre>
 
There is no CloseResource() function. When you are done with a resource, you are done with it. However, as you will see later in this chapter, some resources provide functions to allocate parts of the hardware they access. In those cases, you will have to free those parts for anyone else to use them.
 
 
Each resource has at least one include file in the resources subdirectory of the include directory. Some of the include files contain only the name of the resource; others list structures and bit definitions used by the resource. The include files will be listed at the end of this chapter.
 
 
Calling a resource function is the same as calling any other function on the Amiga. You have to know what parameters it accepts and the return value, if any. The Autodocs for each resource lists the functions and their requirements.
 
 
<pre>#include &lt;hardware/cia.h&gt;
 
#include &lt;resources/cia.h&gt;
 
 
struct Library *CIAResource = NULL;
 
 
void main()
 
{
 
 
WORD mask = 0;
 
 
if (!(CIAResource = OpenResource(CIABNAME)))
 
printf(&quot;Cannot open %s\n&quot;,CIABNAME);
 
else
 
{
 
/* What is the interrupt enable mask? */
 
mask = AbleICR(CIAResource,0);
 
 
printf(&quot;\nThe CIA interrupt enable mask: %x \n&quot;,mask);
 
}
 
}</pre>
 
<sub>b</sub>oxLooks Can Be Deceiving.Some resources may look like libraries and act like libraries, but be assured they are not libraries.
 
 
== BattClock Resource ==
 
 
 
 
The battery-backed clock (BattClock) keeps Amiga time while the system is powered off. The time from the BattClock is loaded into the Amiga system clock as part of the boot sequence.
 
 
The battclock resource provides access to the BattClock. Three functions allow you to read the BattClock value, reset it and set it to a value you desire.
 
 
ll
 
 
2c'''BattClock Resource Functions'''<br />
 
ReadBattClock() &amp; 8.4cmRead the time from the BattClock and returns it as the number of seconds since 12:00 AM, January 1, 1978.<br />
 
ResetBattClock() &amp; 8.4cmReset the BattClock to 12:00 AM, January 1, 1978.<br />
 
WriteBattClock() &amp; 8.4cmSet the BattClock to the number of seconds you pass it relative to 12:00 AM, January 1, 1978.<br />
 
 
 
 
 
The utility.library contains time functions which convert the number of seconds since 12:00 AM, January 1, 1978 to a date and time we can understand, and vice versa. You will find these functions useful when dealing with the BattClock. The example program below uses the Amiga2Date() utility function to convert the value returned by ReadBattClock(). See the “Utility Library” chapter of the ''Amiga ROM Kernel Reference Manual: Libraries'' for a discussion of the utility.library and the ''Amiga ROM Kernel Reference Manual: Includes and Autodocs'' for a listing of its functions.
 
 
<sub>b</sub>oxSo, You Want to Be A Time Lord?This resource will allow you to set the BattClock to any value you desire. Keep in mind that this time will endure a reboot and could adversely affect your system.
 
 
<pre>/*
 
* Read_BattClock.c
 
*
 
* Example of reading the BattClock and converting its output to
 
* a useful measure of time by calling the Amiga2Date() utility function.
 
*
 
* Compile with SAS C 5.10 lc -b1 -cfistq -v -y -L
 
*
 
* Run from CLI only
 
*/
 
 
#include &lt;exec/types.h&gt;
 
#include &lt;dos/dos.h&gt;
 
#include &lt;utility/date.h&gt;
 
#include &lt;resources/battclock.h&gt;
 
 
#include &lt;clib/exec_protos.h&gt;
 
#include &lt;clib/alib_protos.h&gt;
 
#include &lt;clib/battclock_protos.h&gt;
 
#include &lt;clib/utility_protos.h&gt;
 
 
#include &lt;stdio.h&gt;
 
 
#ifdef LATTICE
 
int CXBRK(void) { return(0); } /* Disable SAS CTRL/C handling */
 
int chkabort(void) { return(0); } /* really */
 
#endif
 
 
VOID main(VOID);
 
 
struct Library *UtilityBase = NULL;
 
struct Library *BattClockBase;
 
 
VOID main(VOID)
 
{
 
UBYTE *Days[] ={&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;};
 
UBYTE *Months[] = {&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,
 
&quot;July&quot;,&quot;August&quot;,&quot;September&quot;,&quot;October&quot;,&quot;November&quot;,&quot;December&quot;};
 
UBYTE *ampm;
 
ULONG AmigaTime;
 
struct ClockData MyClock;
 
 
if (UtilityBase = (struct Library *)OpenLibrary(&quot;utility.library&quot;,33))
 
{
 
if (BattClockBase= OpenResource(BATTCLOCKNAME))
 
{
 
/* Get number of seconds till now */
 
AmigaTime = ReadBattClock();
 
 
/* Convert to a ClockData structure */
 
Amiga2Date(AmigaTime,&amp;MyClock);
 
 
printf(&quot;\nRobin, tell everyone the BatDate and BatTime&quot;);
 
 
/* Print the Date */
 
printf(&quot;\n\nOkay Batman, the BatDate is &quot;);
 
printf(&quot;%s, %s %d, %d&quot;,Days[MyClock.wday],Months[MyClock.month-1],
 
MyClock.mday,MyClock.year);
 
 
/* Convert military time to normal time and set AM/PM */
 
if (MyClock.hour &lt; 12)
 
ampm = &quot;AM&quot;; /* hour less than 12, must be morning */
 
else
 
{
 
ampm = &quot;PM&quot;; /* hour greater than 12,must be night */
 
MyClock.hour -= 12; /* subtract the extra 12 of military */
 
};
 
 
if (MyClock.hour == 0)
 
MyClock.hour = 12; /* don't forget the 12s */
 
 
/* Print the time */
 
printf(&quot;\n the BatTime is &quot;);
 
printf(&quot;%d:%02d:%02d %s\n\n&quot;,MyClock.hour,MyClock.min,MyClock.sec,ampm);
 
}
 
else
 
printf(&quot;Error: Unable to open the %s\n&quot;,BATTCLOCKNAME);
 
 
/* Close the utility library */
 
CloseLibrary(UtilityBase);
 
}
 
 
else
 
printf(&quot;Error: Unable to open utility.library\n&quot;);
 
}</pre>
 
Additional programming information on the battclock resource can be found in the include files and the Autodocs for the battclock resource and the utility library.
 
 
== BattMem Resource ==
 
 
 
 
The battery-backed memory (BattMem) preserves a small portion of Amiga memory while the system is powered off. Some of the information stored in this memory is used during the system boot sequence.
 
 
The battmem resource provides access to the BattMem. Four functions allow you to use the BattMem.
 
 
ll
 
 
2c'''BattMem Resource Functions'''<br />
 
ObtainBattSemaphore &amp; 7.3cmObtain exclusive access to the BattMem.<br />
 
ReadBattMem() &amp; 7.3cmRead a bitstring from the BattMem. You specify the bit position and the number of bits you wish to read.<br />
 
ReleaseBattSemaphore() &amp; 7.3cmRelinquish exclusive access to the BattMem.<br />
 
WriteBattMem() &amp; 7.3cmWrite a bitstring to the BattMem. You specify the bit position and the number of bits you wish to write.<br />
 
 
 
The system considers BattMem to be a set of bits rather than bytes. This is done to conserve the limited space available. All bits are reserved, and applications should not read, or write undefined bits. Writing bits should be done with extreme caution since the settings will survive power-down/power-up. You can find the bit definitions in the BattMem include files resources/battmembitsamiga.h, resources/battmembitsamix.h and resources/battmembitsshared.h. They should be consulted before you do anything with the resource.
 
 
<sub>b</sub>oxYou Don’t Need This Resource.The BattMem resource is basically for system use only. There is generally no reason for applications to use it. It is documented here simply for completeness.
 
 
 
 
Additional information on the battmem resource can be found in the include files and the Autodocs for the battmem resource.
 
 
|ll|
 
 
2c'''BattMem Resource Information'''<br />
 
Includes &amp; resources/battmem.i<br />
 
&amp; resources/battmembitsamiga.h<br />
 
&amp; resources/battmembitsamix.h<br />
 
&amp; resources/battmembitsshared.h<br />
 
AutoDocs &amp; battmem.doc<br />
 
 
 
== CIA Resource ==
 
 
 
 
The CIA resource provides access to the timers and timer interrupt bits of the ''8520 Complex Interface Adapter'' (CIA) A and B chips. This resource is intended for use by high performance timing applications such as MIDI time stamping and SMPTE time coding.
 
 
Four functions allow you to interact with the CIA hardware.
 
 
ll
 
 
2c'''CIA Resource Functions'''<br />
 
AbleICR() &amp; 8.7cmEnable or disable Interrupt Control Register interrupts. Can also return the current or previous enabled interrupt mask.<br />
 
AddICRVector() &amp; 8.7cmAllocate one of the CIA timers by assigning an interrupt handler to an interrupt bit and enabling the interrupt of one of the timers. If the timer you request is not available, a pointer to the interrupt structure that owns it will be returned.<br />
 
RemICRVector() &amp; 8.7cmRemove an interrupt handler from an interrupt bit and disable the interrupt.<br />
 
SetICR() &amp; 8.7cmCause or clear one or more interrupts, or return the current or previous interrupt status.<br />
 
 
 
Each CIA chip has two interval timers within it—Timer A and Timer B—that may be available. The CIA chips operate at different interrupt levels with the CIA-A timers at interrupt level 2 and the CIA-B timers at interrupt level 6.
 
 
<sub>b</sub>oxChoose A Timer Wisely.The timer you use should be based solely on interrupt level and availability. If the timer you request is not available, try for another. Whatever you do, do not base your decision on what you think the timer is used for by the system.
 
 
You allocate a timer by calling AddICRVector(). This is the only way you should access a timer. If the function returns zero, you have successfully allocated that timer. If it is unavailable, the owner interrupt will be returned.
 
 
<pre>/* allocate CIA-A Timer A */
 
inta = AddICRVector (CIAResource, CIAICRB_TA, &amp;tint);
 
 
if (inta) /* if allocate was not successful */
 
printf(&quot;Error: Could not allocate timer\n&quot;);
 
else
 
{
 
...ready for timing
 
}</pre>
 
The timer is deallocated by calling RemICRVector(). This is the only way you should deallocate a timer.
 
 
<pre>RemICRVector(CIAResource, CIAICRB_TA, &amp;tint);</pre>
 
Your application should not make any assumptions regarding which interval timers (if any) are available for use; other tasks or critical operating system routines may be using the interval timers. In fact, in the latest version of the operating system, the timer device may dynamically allocate one of the interval timers.
 
 
<sub>b</sub>oxTime Is Of The Essence!There are a limited number of free CIA interval timers. Applications which use the interval timers may not be able to run at the same time if all interval timers are in use. As a general rule, you should use the timer device for most interval timing.
 
 
You read from and write to the CIA interrupt control registers using SetICR() and AbleICR(). SetICR() is useful for sampling which cia interrupts (if any) are active. It can also be used to clear and generate interrupts. AbleICR() is used to disable and enable a particular CIA interrupt. Additional information about these functions can be found in the ''Amiga ROM Kernel Reference Manual: Includes and Autodocs''.
 
 
Things to keep in mind:
 
 
<ol>
 
<li><p>''Never''</p>
 
<p>directly read from or write to the CIA interrupt control registers. Always use SetICR() and AbleICR().</p></li>
 
<li><p>Your interrupt routine will be called with a pointer to your data area in register A1, and a pointer to the code being called in register A5. No other registers are set up for you. You must observe the standard convention of preserving all registers except D0–D1 and A0–A1.</p></li>
 
<li><p>Never turn off all level 2 or level 6 interrupts. The proper way to disable interrupts for an interval timer that you’ve successfully allocated is via the AbleICR() function.</p></li>
 
<li><p>Interrupt handling code should be written in assembly code and, if possible, should signal a task to do most of the work.</p></li>
 
<li><p>Do not make assumptions about which CIA interval timers (if any) are available for use. The only proper way to own an interval timer is via the AddICRVector() function.</p></li>
 
<li><p>Do not use SetICR(), AbleICR() and RemICRVector() to affect timers or other CIA hardware which your task does not own.</p></li></ol>
 
 
Changes in the CIA resource:
 
 
* In pre-V36 versions of the operating system, SetICR() could return FALSE for a particular interrupt just prior to processing the interrupt. SetICR() now returns TRUE for a particular interrupt until sometime after the interrupt has been processed.
 
* Applications which only need to read a CIA interval timer should use the ReadEClock() function of the timer device. See the “Timer Device” chapter of this manual for more information on ReadEClock().
 
* The timer device may dynamically allocate a free CIA interval timer. Do not make any assumptions regarding which interval timers are in use unless you are taking over the machine ''completely''.
 
 
<pre>/*
 
* Cia_Interval.c
 
*
 
* Demonstrate allocation and use of a cia interval timer
 
*
 
* Compile with SAS C 5.10 lc -b1 -cfistq -v -y -L
 
*
 
* Run from CLI only
 
*/
 
 
#include &lt;exec/types.h&gt;
 
#include &lt;exec/memory.h&gt;
 
#include &lt;exec/tasks.h&gt;
 
#include &lt;exec/interrupts.h&gt;
 
#include &lt;hardware/cia.h&gt;
 
#include &lt;resources/cia.h&gt;
 
 
#include &lt;clib/exec_protos.h&gt;
 
#include &lt;clib/cia_protos.h&gt;
 
 
#include &lt;stdlib.h&gt;
 
#include &lt;stdio.h&gt;
 
#include &lt;string.h&gt;
 
 
 
/* prototypes */
 
 
void StartTimer (struct freetimer *ft, struct exampledata *ed);
 
int FindFreeTimer (struct freetimer *ft, int preferA);
 
int TryTimer (struct freetimer *ft);
 
void main ( USHORT, char **);
 
 
 
/* see usage of these defines in StartTimer() below */
 
 
#define COUNTDOWN 20
 
#define HICOUNT 0xFF
 
#define LOCOUNT 0xFF
 
 
#define STOPA_AND CIACRAF_TODIN |CIACRAF_PBON | CIACRAF_OUTMODE | CIACRAF_SPMODE
 
 
/*
 
;
 
; AND mask for use with control register A
 
; (interval timer A on either CIA)
 
;
 
; STOP -
 
; START bit 0 == 0 (STOP IMMEDIATELY)
 
; PBON bit 1 == same
 
; OUT bit 2 == same
 
; RUN bit 3 == 0 (SET CONTINUOUS MODE)
 
; LOAD bit 4 == 0 (NO FORCE LOAD)
 
; IN bit 5 == 0 (COUNTS 02 PULSES)
 
; SP bit 6 == same
 
; TODIN bit 7 == same (unused on ciacra)
 
 
*/
 
 
#define STOPB_AND CIACRBF_ALARM | CIACRBF_PBON | CIACRBF_OUTMODE
 
 
/*
 
;
 
; AND mask for use with control register B
 
; (interval timer B on either CIA)
 
;
 
; STOP -
 
; START bit 0 == 0 (STOP IMMEDIATELY)
 
; PBON bit 1 == same
 
; OUT bit 2 == same
 
; RUN bit 3 == 0 (SET CONTINUOUS MODE)
 
; LOAD bit 4 == 0 (NO FORCE LOAD)
 
; IN0 bit 5 == 0 (COUNTS 02 PULSES)
 
; IN1 bit 6 == 0 (COUNTS 02 PULSES)
 
; ALARM bit 7 == same (TOD alarm control bit)
 
 
*/
 
 
#define STARTA_OR CIACRAF_START
 
 
/*
 
;
 
; OR mask for use with control register A
 
; (interval timer A on either CIA)
 
;
 
; START -
 
;
 
; START bit 0 == 1 (START TIMER)
 
;
 
; All other bits unaffected.
 
;
 
 
*/
 
 
#define STARTB_OR CIACRBF_START
 
 
/*
 
;
 
; OR mask for use with control register B
 
; (interval timer A on either CIA)
 
;
 
; START -
 
;
 
; START bit 0 == 1 (START TIMER)
 
;
 
; All other bits unaffected.
 
;
 
 
*/
 
 
 
/*
 
* Structure which will be used to hold all relevant information about
 
* the cia timer we manage to allocate.
 
*
 
*/
 
 
struct freetimer
 
{
 
struct Library *ciabase; /* CIA Library Base */
 
ULONG timerbit; /* timer bit allocated */
 
struct CIA *cia; /* ptr to hardware */
 
UBYTE *ciacr; /* ptr to control register */
 
UBYTE *cialo; /* ptr to low byte of timer */
 
UBYTE *ciahi; /* ptr to high byte of timer */
 
struct Interrupt timerint; /* Interrupt structure */
 
UBYTE stopmask; /* Stop/set-up timer */
 
UBYTE startmask; /* Start timer */
 
};
 
 
/*
 
* Structure which will be used by the interrupt routine called
 
* when our cia interval timer generates an interrupt.
 
*
 
*/
 
 
struct exampledata
 
{
 
struct Task *task; /* task to signal */
 
ULONG signal; /* Signal bit to use */
 
ULONG counter;
 
};
 
 
 
struct CIA *ciaa = (struct CIA *)0xbfe001;
 
struct CIA *ciab = (struct CIA *)0xbfd000;
 
 
 
#ifdef LATTICE
 
int CXBRK(void) { return(0); } /* Disable SAS CTRL/C handling */
 
int chkabort(void) { return(0); } /* really */
 
#endif
 
 
/*
 
* This is the interrupt routine which will be called when our CIA
 
* interval timer counts down.
 
*
 
* This example decrements a counter each time the interrupt routine
 
* is called until the counter reaches 0, at which time it signals
 
* our main task.
 
*
 
* Note that interrupt handling code should be efficient, and will
 
* generally be written in assembly code. Signaling another task
 
* such as this example does is also a useful way of handling
 
* interrupts in an expedient manner.
 
*/
 
 
void __asm ExampleInterrupt(register __a1 struct exampledata *ed)
 
{
 
if (ed-&gt;counter)
 
{
 
ed-&gt;counter--; /* decrement counter */
 
}
 
else
 
{
 
ed-&gt;counter = COUNTDOWN; /* reset counter */
 
 
Signal(ed-&gt;task,(1L &lt;&lt; ed-&gt;signal));
 
}
 
}
 
 
 
/***********************************
 
* main()
 
***********************************/
 
 
void main(USHORT argc,char **argv)
 
{
 
struct freetimer ft;
 
struct exampledata ed;
 
 
/* Set up data which will be passed to interrupt */
 
 
ed.task = FindTask(0L);
 
 
if (ed.signal = AllocSignal(-1L))
 
{
 
/* Prepare freetimer structure : set-up interrupt */
 
 
ft.timerint.is_Node.ln_Type = NT_INTERRUPT;
 
ft.timerint.is_Node.ln_Pri = 0;
 
ft.timerint.is_Node.ln_Name = &quot;cia_example&quot;;
 
 
ft.timerint.is_Data = (APTR)&amp;ed;
 
ft.timerint.is_Code = (APTR)ExampleInterrupt;
 
 
/* Call function to find a free CIA interval timer
 
* with flag indicating that we prefer a CIA-A timer.
 
*/
 
 
printf(&quot;Attempting to allocate a free timer\n&quot;);
 
 
if (FindFreeTimer(&amp;ft,TRUE))
 
{
 
if (ft.cia == ciaa)
 
{
 
printf(&quot;CIA-A timer &quot;);
 
}
 
else
 
{
 
printf(&quot;CIA-B timer &quot;);
 
}
 
 
if (ft.timerbit == CIAICRB_TA)
 
{
 
printf(&quot;A allocated\n&quot;);
 
}
 
else
 
{
 
printf(&quot;B allocated\n&quot;);
 
}
 
 
 
/* We found a free interval timer. Let's start it running. */
 
 
StartTimer(&amp;ft,&amp;ed);
 
 
/* Wait for a signal */
 
 
printf(&quot;Waiting for signal bit %ld\n&quot;,ed.signal);
 
 
Wait(1L&lt;&lt;ed.signal);
 
 
printf(&quot;We woke up!\n&quot;);
 
 
/* Release the interval timer */
 
 
RemICRVector(ft.ciabase,ft.timerbit,&amp;ft.timerint);
 
 
}
 
else
 
{
 
printf(&quot;No CIA interval timer available\n&quot;);
 
}
 
 
FreeSignal(ed.signal);
 
}
 
}
 
 
 
/*
 
* This routine sets up the interval timer we allocated with
 
* AddICRVector(). Note that we may have already received one, or
 
* more interrupts from our timer. Make no assumptions about the
 
* initial state of any of the hardware registers we will be using.
 
*
 
*/
 
 
void StartTimer(struct freetimer *ft, struct exampledata *ed)
 
{
 
register struct CIA *cia;
 
 
cia = ft-&gt;cia;
 
 
/* Note that there are differences between control register A,
 
* and B on each CIA (e.g., the TOD alarm bit, and INMODE bits.
 
*/
 
 
if (ft-&gt;timerbit == CIAICRB_TA)
 
{
 
ft-&gt;ciacr = &amp;cia-&gt;ciacra; /* control register A */
 
ft-&gt;cialo = &amp;cia-&gt;ciatalo; /* low byte counter */
 
ft-&gt;ciahi = &amp;cia-&gt;ciatahi; /* high byte counter */
 
 
ft-&gt;stopmask = STOPA_AND; /* set-up mask values */
 
ft-&gt;startmask = STARTA_OR;
 
}
 
else
 
{
 
ft-&gt;ciacr = &amp;cia-&gt;ciacrb; /* control register B */
 
ft-&gt;cialo = &amp;cia-&gt;ciatblo; /* low byte counter */
 
ft-&gt;ciahi = &amp;cia-&gt;ciatbhi; /* high byte counter */
 
 
ft-&gt;stopmask = STOPB_AND; /* set-up mask values */
 
ft-&gt;startmask = STARTB_OR;
 
}
 
 
 
/* Modify control register within Disable(). This is done to avoid
 
* race conditions since our compiler may generate code such as:
 
*
 
* value = Read hardware byte
 
* AND value with MASK
 
* Write value to hardware byte
 
*
 
* If we take a task switch in the middle of this sequence, two tasks
 
* trying to modify the same register could trash each others' bits.
 
*
 
* Normally this code would be written in assembly language using atomic
 
* instructions so that the Disable() would not be needed.
 
*/
 
 
 
Disable();
 
 
/* STOP timer, set 02 pulse count-down mode, set continuous mode */
 
 
*ft-&gt;ciacr &amp;= ft-&gt;stopmask;
 
Enable();
 
 
/* Clear signal bit - interrupt will signal us later */
 
SetSignal(0L,1L&lt;&lt;ed-&gt;signal);
 
 
/* Count-down X # of times */
 
ed-&gt;counter = COUNTDOWN;
 
 
/* Start the interval timer - we will start the counter after
 
* writing the low, and high byte counter values
 
*/
 
 
*ft-&gt;cialo = LOCOUNT;
 
*ft-&gt;ciahi = HICOUNT;
 
 
/* Turn on start bit - same bit for both A, and B control regs */
 
 
Disable();
 
*ft-&gt;ciacr |= ft-&gt;startmask;
 
 
Enable();
 
}
 
 
 
/*
 
* A routine to find a free interval timer.
 
*
 
* This routine makes no assumptions about which interval timers
 
* (if any) are available for use. Currently there are two interval
 
* timers per CIA chip.
 
*
 
* Because CIA usage may change in the future, your code should use
 
* a routine like this to find a free interval timer.
 
*
 
* Note that the routine takes a preference flag (which is used to
 
* to indicate that you would prefer an interval timer on CIA-A).
 
* If the flag is FALSE, it means that you would prefer an interval
 
* timer on CIA-B.
 
*
 
*/
 
 
FindFreeTimer(struct freetimer *ft, int preferA)
 
{
 
struct CIABase *ciaabase, *ciabbase;
 
 
/* get pointers to both resource bases */
 
 
ciaabase = OpenResource(CIAANAME);
 
ciabbase = OpenResource(CIABNAME);
 
 
/* try for a CIA-A timer first ? */
 
 
if (preferA)
 
{
 
ft-&gt;ciabase = ciaabase; /* library address */
 
ft-&gt;cia = ciaa; /* hardware address */
 
}
 
else
 
{
 
ft-&gt;ciabase = ciabbase; /* library address */
 
ft-&gt;cia = ciab; /* hardware address */
 
}
 
 
if (TryTimer(ft))
 
return(TRUE);
 
 
/* try for an interval timer on the other cia */
 
 
if (!(preferA))
 
{
 
ft-&gt;ciabase = ciaabase; /* library address */
 
ft-&gt;cia = ciaa; /* hardware address */
 
}
 
else
 
{
 
ft-&gt;ciabase = ciabbase; /* library address */
 
ft-&gt;cia = ciab; /* hardware address */
 
}
 
 
if (TryTimer(ft))
 
return(TRUE);
 
 
return(FALSE);
 
 
}
 
 
 
/*
 
* Try to obtain a free interval timer on a CIA.
 
*/
 
 
TryTimer(struct freetimer *ft)
 
{
 
 
if (!(AddICRVector(ft-&gt;ciabase,CIAICRB_TA,&amp;ft-&gt;timerint)))
 
{
 
ft-&gt;timerbit = CIAICRB_TA;
 
return(TRUE);
 
}
 
 
if (!(AddICRVector(ft-&gt;ciabase,CIAICRB_TB,&amp;ft-&gt;timerint)))
 
{
 
ft-&gt;timerbit = CIAICRB_TB;
 
return(TRUE);
 
}
 
 
return(FALSE);
 
}</pre>
 
Additional programming information on the CIA resource can be found in the include files and the Autodocs for the CIA resource and the 8520 spec. The includes files and Autodocs are in the ''Amiga ROM Kernel Reference Manual: Includes and Autodocs'' and the 8520 spec is in the ''Amiga Hardware Reference Manual''.
 
 
|ll|
 
 
2c'''CIA Resource Information'''<br />
 
Includes &amp; resources/cia.h<br />
 
&amp; resources/cia.i<br />
 
&amp; hardware/cia.h<br />
 
&amp; hardware/cia.i<br />
 
AutoDocs &amp; cia.doc<br />
 
Hardware &amp; 8520 specification<br />
 
 
 
== Disk Resource ==
 
 
 
 
The Disk resource obtains exclusive access to the floppy disk hardware There are four disk/MFM units available, units 0–3.
 
 
Six functions are available for dealing with the floppy disk hardware.
 
 
ll
 
 
2c'''Disk Resource Functions'''<br />
 
AllocUnit() &amp; Allocate one of the units of the disk resource.<br />
 
FreeUnit() &amp; Deallocate an allocated disk unit.<br />
 
GetUnit() &amp; Allocate the disk for a driver.<br />
 
GetUnitID() &amp; Return the drive ID of a specified drive unit.<br />
 
GiveUnit() &amp; Free the disk.<br />
 
ReadUnitID() &amp; Reread and return the drive ID of a specified unit.<br />
 
 
 
The disk resource provides both a gross and a fine unit allocation scheme. AllocUnit() and FreeUnit() are used to claim a unit for long term use, and GetUnit() and GiveUnit() are used to claim a unit and the disk hardware for shorter periods.
 
 
The trackdisk device uses and abides by both allocation schemes. Because a trackdisk unit is never closed for Amiga 3.5’’ drives (the file system keeps them open) the associated resource units will always be allocated for these drives. GetUnit() and GiveUnit() can still be used, however, by other applications that have not succeeded with AllocUnit().
 
 
You must not change the state of of a disk that the trackdisk device is using unless you either
 
 
<ol>
 
<li><p>force its removal before giving it up, or</p></li>
 
<li><p>return it to the original track (with no changes to the track), or</p></li>
 
<li><p>CMD_STOP</p>
 
<p>the unit before GetUnit(), update the current track number and CMD_START it after GiveUnit(). This option is only available under V36 and higher versions of the operating system.</p></li></ol>
 
 
ReadUnitID()
 
 
is provided to handle drives which use the unit number in a dynamic manner. Subsequent GetUnit() calls will return the value obtained by ReadUnitID().
 
 
It is therefore possible to prevent the trackdisk device from using units that have not yet been mounted by successfully performing an AllocUnit() for that unit. It is also possible to starve trackdisk usage by performing a GetUnit(). The appropriate companion routine (FreeUnit() or GiveUnit()) should be called to restore the resource at the end of its use.
 
 
Additional programming information on the disk resource can be found in the include files and the Autodocs for the disk resource.
 
 
|ll|
 
 
2c'''Disk Resource Information'''<br />
 
Includes &amp; resources/disk.h<br />
 
&amp; resources/disk.i<br />
 
AutoDocs &amp; disk.doc<br />
 
 
 
== FileSystem Resource ==
 
 
 
 
The FileSystem resource returns the filesystems that are available on the Amiga. It has no functions. Opening the FileSystem resource returns a pointer to a List structure containing the current filesystems in the Amiga.
 
 
Additional programming information on the FileSystem resource can be found in the include files and the Autodocs for the FileSystem resource in the ''Amiga ROM Kernel Reference Manual: Includes and Autodocs'' and the “Expansion” chapter of the ''Amiga ROM Kernel Reference Manual: Libraries''.
 
 
|ll|
 
 
2c'''FileSystem Resource Information'''<br />
 
Includes &amp; resources/filesysres.h<br />
 
&amp; resources/filesysres.i<br />
 
AutoDocs &amp; filesysres.doc<br />
 
Libraries &amp; expansion.library<br />
 
 
 
== Misc Resource ==
 
 
 
 
The misc resource oversees usage of the serial data port, the serial communication bits, the parallel data and handshake port, and the parallel communication bits. Before using serial or parallel port hardware, it first must be acquired from the misc resource.
 
 
The misc resource provides two functions for allocating and freeing the serial and parallel hardware.
 
 
ll
 
 
2c'''Misc Resource Functions'''<br />
 
AllocMiscResource() &amp; 7.8cmAllocate one of the serial or parallel misc resources.<br />
 
FreeMiscResource() &amp; 7.8cmDeallocate one of the serial or parallel misc resources.<br />
 
 
 
Once you’ve successfully allocated one of the misc resources, you are free to write directly to its hardware locations. Information on the serial and parallel hardware can be found in the ''Amiga Hardware Reference Manual'' and the hardware/custom.h include file.
 
 
The two examples below are assembly and ’C’ versions of the same code for locking the serial misc resources and waiting for CTRL-C to be pressed before releasing them.
 
 
=== Assembly Example of Allocating Misc Resources ===
 
 
<pre>* Alloc_Misc.a
 
*
 
* Assembly language fragment that grabs the two parts of the serial
 
* resource (using misc.resource). If it gets the resource, it will
 
* wait for CTRL-C to be pressed before releasing.
 
*
 
* While we are waiting, the query_serial program should be run. It will try
 
* to open the serial device and if unsuccessful, will return the name of the
 
* owner. It will be us, Serial Port Hog!
 
*
 
* When a task has successfully obtained the serial resource, it &quot;owns&quot;
 
* the hardware registers that control the serial port. No other tasks
 
* are allowed to interfere.
 
*
 
* Assemble with Adapt
 
* HX68 Allocate_Misc.a to Allocate_Misc.o
 
*
 
* Link
 
* Blink FROM Allocate_Misc.o TO Allocate_Misc LIB LIB:amiga.lib
 
*
 
 
INCDIR &quot;include:&quot;
 
INCLUDE &quot;exec/types.i&quot;
 
INCLUDE &quot;resources/misc.i&quot;
 
INCLUDE &quot;dos/dos.i&quot;
 
 
xref _AbsExecBase ; We get this from outside...
 
xref _LVOOpenResource ; We get this from outside...
 
xref _LVOWait ; We get this from outside...
 
 
;
 
; Open Exec and the misc.resource, check for success
 
;
 
move.l _AbsExecBase,a6 ;Prepare to use exec
 
lea.l MiscName(pc),a1
 
jsr _LVOOpenResource(a6) ;Open &quot;misc.resource&quot;
 
move.l d0,d7 ;Stash resource base
 
bne.s resource_ok
 
moveq #RETURN_FAIL,d0
 
rts
 
 
resource_ok exg.l d7,a6 ;Put resource base in A6
 
 
;
 
; We now have a pointer to a resource.
 
; Call one of the resource's library-like vectors.
 
;
 
move.l #MR_SERIALBITS,d0 ;We want these bits
 
lea.l MyName(pc),a1 ;This is our name
 
jsr MR_ALLOCMISCRESOURCE(a6)
 
tst.l d0
 
bne.s no_bits ;Someone else has it...
 
move.l #MR_SERIALPORT,d0
 
lea.l MyName(pc),a1
 
jsr MR_ALLOCMISCRESOURCE(a6)
 
tst.l d0
 
bne.s no_port ;Someone else has it...
 
;
 
; We just stole the serial port registers; wait.
 
; Nobody else can use the serial port, including the serial.device!
 
;
 
exg.l d7,a6 ;use exec again
 
move.l #SIGBREAKF_CTRL_C,d0
 
jsr _LVOWait(a6) ;Wait for CTRL-C
 
exg.l d7,a6 ;Get resource base back
 
;
 
; Free 'em up
 
;
 
move.l #MR_SERIALPORT,d0
 
jsr MR_FREEMISCRESOURCE(a6)
 
no_port
 
move.l #MR_SERIALBITS,d0
 
jsr MR_FREEMISCRESOURCE(a6)
 
no_bits
 
moveq #RETURN_FAIL,d0
 
rts
 
;
 
; Text area
 
;
 
MiscName dc.b 'misc.resource',0
 
MyName dc.b 'Serial Port Hog',0
 
dc.w 0
 
END</pre>
 
=== ’C’ Example of Allocating Misc Resources ===
 
 
<pre>/*
 
* Allocate_Misc.c
 
*
 
* Example of allocating a miscellaneous resource
 
* We will allocate the serial resource and wait till
 
* CTRL-C is pressed. While we are waiting, the
 
* query_serial program should be run. It will try
 
* to open the serial device and if unsuccessful, will
 
* return the name of the owner. It will be us!
 
*
 
* Compile with SAS C 5.10 lc -b1 -cfistq -v -y -L
 
*
 
* Run from CLI only
 
*/
 
 
#include &lt;exec/types.h&gt;
 
#include &lt;exec/memory.h&gt;
 
#include &lt;dos/dos.h&gt;
 
#include &lt;resources/misc.h&gt;
 
 
#include &lt;clib/exec_protos.h&gt;
 
#include &lt;clib/misc_protos.h&gt;
 
 
#include &lt;stdio.h&gt;
 
 
#ifdef LATTICE
 
int CXBRK(void) { return(0); } /* Disable SAS CTRL/C handling */
 
int chkabort(void) { return(0); } /* really */
 
#endif
 
 
struct Library *MiscBase = NULL;
 
 
void main(int argc, char **argv)
 
{
 
UBYTE *owner = NULL; /* owner of misc resource */
 
 
if (!(MiscBase= (struct Library *)OpenResource(MISCNAME)))
 
printf(&quot;Cannot open %s\n&quot;,MISCNAME);
 
else
 
{
 
/* Allocate both pieces of the serial hardware */
 
if ((owner = AllocMiscResource(MR_SERIALPORT,&quot;Serial Port Hog&quot;)) == NULL)
 
{
 
if ((owner = AllocMiscResource(MR_SERIALBITS,&quot;Serial Port Hog&quot;)) == NULL)
 
{
 
/* Wait for CTRL-C to be pressed */
 
printf(&quot;\nWaiting for CTRL-C...\n&quot;);
 
Wait(SIGBREAKF_CTRL_C);
 
 
/* We're back */
 
 
/* Deallocate the serial port register */
 
FreeMiscResource(MR_SERIALBITS);
 
}
 
else
 
printf(&quot;\nUnable to allocate MR_SERIALBITS because %s owns it\n&quot;,owner);
 
 
/* Deallocate the serial port */
 
FreeMiscResource(MR_SERIALPORT);
 
}
 
else
 
printf(&quot;\nUnable to allocate MR_SERIALPORT because %s owns it\n&quot;,owner);
 
}
 
}</pre>
 
The example below will try to open the serial device and execute the SDCMD_QUERY command. If it cannot open the serial device, it will do an AllocMiscResource() on the serial port and return the name of the owner.
 
 
<sub>b</sub>oxTake Over Everything.There are two serial.device resources to take over, MR_SERIALBITS and MR_SERIALPORT. You should get both resources when you take over the serial port to prevent other tasks from using them. The parallel.device also has two resources to take over. See the resources/misc.h include file for the relevant definitions and structures.
 
 
Under V1.3 and earlier versions of the Amiga system software the MR_GETMISCRESOURCE routine will always fail if the serial device has been used at all by another task (even if that task has finished using the resource. In other words, once a printer driver or communication package has been activated, it will keep the associated resource locked up preventing your task from using it. Under these conditions, you must get the resource back from the system yourself.
 
 
You do this by calling the function FlushDevice():
 
 
<pre>/*
 
* A safe way to expunge ONLY a certain device. The serial.device holds
 
* on to the misc serial resource until a general expunge occurs.
 
* This code attempts to flush ONLY the named device out of memory and
 
* nothing else. If it fails, no status is returned since it would have
 
* no valid use after the Permit().
 
*/
 
 
#include &lt;exec/types.h&gt;
 
#include &lt;exec/execbase.h&gt;
 
 
#include &lt;clib/exec_protos.h&gt;
 
 
void FlushDevice(char *);
 
 
extern struct ExecBase *SysBase;
 
 
void FlushDevice(char *name)
 
{
 
struct Device *devpoint;
 
 
Forbid();
 
 
if (devpoint=(struct Device *)FindName(&amp;SysBase-&gt;DeviceList,name) )
 
RemDevice(devpoint);
 
 
Permit();
 
}</pre>
 
Additional programming information on the misc resource can be found in the include files and the Autodocs for the misc resource.
 
 
|ll|
 
 
2c'''Misc Resource Information'''<br />
 
Includes &amp; resources/misc.h<br />
 
&amp; resources/misc.i<br />
 
AutoDocs &amp; misc.doc<br />
 
 
 
== Potgo Resource ==
 
 
 
 
The potgo resource is used to get control of the hardware POTGO register connected to the proportional I/O pins on the game controller ports. There are two registers, POTGO (write-only) and POTINP (read-only). These pins could also be used for digital I/O.
 
 
The potgo resource provides three functions for working with the POTGO hardware.
 
 
ll
 
 
2c'''Potgo Resource Functions'''<br />
 
AllocPotBits() &amp; 8.7cmAllocate bits in the POTGO register.<br />
 
FreePotBits() &amp; 8.7cmFree previously allocated bits in the POTGO register.<br />
 
WritePotgo() &amp; 8.7cmSet and clear bits in the POTGO register. The bits must have been allocated before calling this function.<br />
 
 
 
The example program shown below demonstrates how to use the ptogo resource to track mouse button presses on port 1.
 
 
<pre>/*
 
* Read_Potinp.c
 
*
 
* An example of using the potgo.resource to read pins 9 and 5 of
 
* port 1 (the non-mouse port). This bypasses the gameport.device.
 
* When the right or middle button on a mouse plugged into port 1 is pressed,
 
* the read value will change.
 
*
 
* Use of port 0 (mouse) is unaffected.
 
*
 
* Compile with SAS C 5.10 lc -b1 -cfistq -v -y -L
 
*
 
* Run from CLI only
 
*/
 
 
#include &lt;exec/types.h&gt;
 
#include &lt;exec/memory.h&gt;
 
#include &lt;dos/dos.h&gt;
 
#include &lt;resources/potgo.h&gt;
 
#include &lt;hardware/custom.h&gt;
 
 
#include &lt;clib/exec_protos.h&gt;
 
#include &lt;clib/potgo_protos.h&gt;
 
 
#include &lt;stdio.h&gt;
 
 
#ifdef LATTICE
 
int CXBRK(void) {return(0);} /* Disable SAS Ctrl-C checking */
 
int chkabort(void) { return(0); } /* really */
 
#endif
 
 
struct PotgoBase *PotgoBase;
 
ULONG potbits;
 
UWORD value;
 
 
#define UNLESS(x) if(!(x))
 
#define UNTIL(x) while(!(x))
 
 
#define OUTRY 1L&lt;&lt;15
 
#define DATRY 1L&lt;&lt;14
 
#define OUTRX 1L&lt;&lt;13
 
#define DATRX 1L&lt;&lt;12
 
 
extern struct Custom far custom;
 
 
void main(int argc,char **argv)
 
{
 
UNLESS (PotgoBase=(struct PotgoBase *)OpenResource(&quot;potgo.resource&quot;))
 
return;
 
 
potbits=AllocPotBits(OUTRY|DATRY|OUTRX|DATRX);
 
 
/* Get the bits for the right and middle mouse buttons on the alternate mouse port. */
 
 
if (potbits != (OUTRY|DATRY|OUTRX|DATRX))
 
{
 
printf(&quot;Pot bits are already allocated! %lx\n&quot;,potbits);
 
FreePotBits(potbits);
 
return;
 
}
 
 
/* Set all ones in the register (masked by potbits) */
 
WritePotgo(0xFFFFFFFFL,potbits);
 
 
printf(&quot;\nPlug a mouse into the second port. This program will indicate when\n&quot;);
 
printf(&quot;the right or middle button (if the mouse is so equipped) is pressed.\n&quot;);
 
printf(&quot;Stop the program with Control-C. Press return now to begin.\n&quot;);
 
 
getchar();
 
 
UNTIL (SIGBREAKF_CTRL_C &amp; SetSignal(0L,0L))
 
/* until CTRL-C is pressed */
 
{
 
/* Read word at $DFF016 */
 
value = custom.potinp;
 
 
/* Show what was read (restricted to our allocated bits) */
 
printf(&quot;POTINP = $%lx\n&quot;,value &amp; potbits);
 
}
 
 
FreePotBits(potbits);
 
}</pre>
 
Additional programming information on the potgo resource can be found in the include files and the Autodocs for the potgo resource.
 
 
|ll|
 
 
2c'''Potgo Resource Information'''<br />
 
Includes &amp; resources/potgo.h<br />
 
&amp; resources/potgo.i<br />
 
&amp; utility/hooks.h<br />
 
&amp; utility/hooks.i<br />
 
AutoDocs &amp; potgo.doc<br />
 

Latest revision as of 21:51, 18 October 2023

Introduction

Resources are responsible for low-level hardware control, see Exec Resources to have a better insight of what it is and how it's used.

System Resources

BattClock Resource

BattMem Resource

CIA Resource

Disk Resource

DMA Resource

FileSystem Resource

Misc Resource

Potgo Resource

PerformanceMonitor Resource

Xena Resource