Home » Archimedes archive » Acorn User » AU 1996-02.adf » Regulars » C/c/_swi

C/c/_swi

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1996-02.adf » Regulars
Filename: C/c/_swi
Read OK:
File size: 089D bytes
Load address: 0000
Exec address: 0000
File contents
/* An example application that uses Acorn's _swi() function */
/* by Steve Mumford for Acorn User - February 1996 */

#include <stdio.h>
#include "swis.h"

/* #Defines */

#define POLL_MASK 1

/* Global variables */

int QUITFLAG = 0;

/* Function prototyping */

int main(void);
void wimpmsg(int, unsigned char[]);
unsigned long int btow(unsigned char[], int);

int
main(void)
{
int task_handle;
unsigned char   pollblock[257];
int pollresult;
long int    list[1] = {0};

    /* The syntax of _swi() is: SWI number, register mask, variables to pass
       to registers... More details can be found by reading the swis.h header
       file in Acorn's C libraries, but the mask is built up by OR-ing (using
       the | operator) various macros together that define which registers
       are in use - in the example below, the four bits of data are input
       through registers 0 to 3, and the function returns the value passed
       back in register 1. */

    task_handle =_swi(Wimp_Initialise, _IN(0)|_IN(1)|_IN(2)|_IN(3)|_RETURN(1) , 310, *(int *)"TASK", "_swi() example", list);

    do {
        pollresult = _swi(Wimp_Poll, _IN(0)|_IN(1)|_RETURN(0), POLL_MASK, pollblock);
        switch (pollresult) {
            case 17:
            case 18:
            case 19: wimpmsg(pollresult, pollblock);
                break;
        }
    } while (QUITFLAG == 0);

    _swi(Wimp_CloseDown, _IN(0)|_IN(1), task_handle, *(int *)"TASK");
}

void wimpmsg(int result, unsigned char pollblock[])
{
    unsigned long int   message_action;

    /* This function decides which particular message is being passed, and
       acts accordingly */
    message_action = btow(pollblock, 16);
    switch (message_action) {
        case 0: QUITFLAG = 1; /* Message_Quit */
            break;
    }
}

unsigned long int btow(unsigned char pollblock[], int index)
{
    /* This routine takes an index to an array of char ints, and converts
       the next four bytes into an int */
    unsigned long int   result = 0;
    int loop;

    index += 3;
    result = pollblock[index--];
    for(loop = 0; loop < 3; loop++) {
        result = result << 8;
        result += pollblock[index--];
    }

    return (result);
}
00000000  2f 2a 20 41 6e 20 65 78  61 6d 70 6c 65 20 61 70  |/* An example ap|
00000010  70 6c 69 63 61 74 69 6f  6e 20 74 68 61 74 20 75  |plication that u|
00000020  73 65 73 20 41 63 6f 72  6e 27 73 20 5f 73 77 69  |ses Acorn's _swi|
00000030  28 29 20 66 75 6e 63 74  69 6f 6e 20 2a 2f 0a 2f  |() function */./|
00000040  2a 20 62 79 20 53 74 65  76 65 20 4d 75 6d 66 6f  |* by Steve Mumfo|
00000050  72 64 20 66 6f 72 20 41  63 6f 72 6e 20 55 73 65  |rd for Acorn Use|
00000060  72 20 2d 20 46 65 62 72  75 61 72 79 20 31 39 39  |r - February 199|
00000070  36 20 2a 2f 0a 0a 23 69  6e 63 6c 75 64 65 20 3c  |6 */..#include <|
00000080  73 74 64 69 6f 2e 68 3e  0a 23 69 6e 63 6c 75 64  |stdio.h>.#includ|
00000090  65 20 22 73 77 69 73 2e  68 22 0a 0a 2f 2a 20 23  |e "swis.h"../* #|
000000a0  44 65 66 69 6e 65 73 20  2a 2f 0a 0a 23 64 65 66  |Defines */..#def|
000000b0  69 6e 65 20 50 4f 4c 4c  5f 4d 41 53 4b 20 31 0a  |ine POLL_MASK 1.|
000000c0  0a 2f 2a 20 47 6c 6f 62  61 6c 20 76 61 72 69 61  |./* Global varia|
000000d0  62 6c 65 73 20 2a 2f 0a  0a 69 6e 74 20 51 55 49  |bles */..int QUI|
000000e0  54 46 4c 41 47 20 3d 20  30 3b 0a 0a 2f 2a 20 46  |TFLAG = 0;../* F|
000000f0  75 6e 63 74 69 6f 6e 20  70 72 6f 74 6f 74 79 70  |unction prototyp|
00000100  69 6e 67 20 2a 2f 0a 0a  69 6e 74 20 6d 61 69 6e  |ing */..int main|
00000110  28 76 6f 69 64 29 3b 0a  76 6f 69 64 20 77 69 6d  |(void);.void wim|
00000120  70 6d 73 67 28 69 6e 74  2c 20 75 6e 73 69 67 6e  |pmsg(int, unsign|
00000130  65 64 20 63 68 61 72 5b  5d 29 3b 0a 75 6e 73 69  |ed char[]);.unsi|
00000140  67 6e 65 64 20 6c 6f 6e  67 20 69 6e 74 20 62 74  |gned long int bt|
00000150  6f 77 28 75 6e 73 69 67  6e 65 64 20 63 68 61 72  |ow(unsigned char|
00000160  5b 5d 2c 20 69 6e 74 29  3b 0a 0a 69 6e 74 0a 6d  |[], int);..int.m|
00000170  61 69 6e 28 76 6f 69 64  29 0a 7b 0a 69 6e 74 20  |ain(void).{.int |
00000180  74 61 73 6b 5f 68 61 6e  64 6c 65 3b 0a 75 6e 73  |task_handle;.uns|
00000190  69 67 6e 65 64 20 63 68  61 72 20 20 20 70 6f 6c  |igned char   pol|
000001a0  6c 62 6c 6f 63 6b 5b 32  35 37 5d 3b 0a 69 6e 74  |lblock[257];.int|
000001b0  20 70 6f 6c 6c 72 65 73  75 6c 74 3b 0a 6c 6f 6e  | pollresult;.lon|
000001c0  67 20 69 6e 74 20 20 20  20 6c 69 73 74 5b 31 5d  |g int    list[1]|
000001d0  20 3d 20 7b 30 7d 3b 0a  0a 20 20 20 20 2f 2a 20  | = {0};..    /* |
000001e0  54 68 65 20 73 79 6e 74  61 78 20 6f 66 20 5f 73  |The syntax of _s|
000001f0  77 69 28 29 20 69 73 3a  20 53 57 49 20 6e 75 6d  |wi() is: SWI num|
00000200  62 65 72 2c 20 72 65 67  69 73 74 65 72 20 6d 61  |ber, register ma|
00000210  73 6b 2c 20 76 61 72 69  61 62 6c 65 73 20 74 6f  |sk, variables to|
00000220  20 70 61 73 73 0a 20 20  20 20 20 20 20 74 6f 20  | pass.       to |
00000230  72 65 67 69 73 74 65 72  73 2e 2e 2e 20 4d 6f 72  |registers... Mor|
00000240  65 20 64 65 74 61 69 6c  73 20 63 61 6e 20 62 65  |e details can be|
00000250  20 66 6f 75 6e 64 20 62  79 20 72 65 61 64 69 6e  | found by readin|
00000260  67 20 74 68 65 20 73 77  69 73 2e 68 20 68 65 61  |g the swis.h hea|
00000270  64 65 72 0a 20 20 20 20  20 20 20 66 69 6c 65 20  |der.       file |
00000280  69 6e 20 41 63 6f 72 6e  27 73 20 43 20 6c 69 62  |in Acorn's C lib|
00000290  72 61 72 69 65 73 2c 20  62 75 74 20 74 68 65 20  |raries, but the |
000002a0  6d 61 73 6b 20 69 73 20  62 75 69 6c 74 20 75 70  |mask is built up|
000002b0  20 62 79 20 4f 52 2d 69  6e 67 20 28 75 73 69 6e  | by OR-ing (usin|
000002c0  67 0a 20 20 20 20 20 20  20 74 68 65 20 7c 20 6f  |g.       the | o|
000002d0  70 65 72 61 74 6f 72 29  20 76 61 72 69 6f 75 73  |perator) various|
000002e0  20 6d 61 63 72 6f 73 20  74 6f 67 65 74 68 65 72  | macros together|
000002f0  20 74 68 61 74 20 64 65  66 69 6e 65 20 77 68 69  | that define whi|
00000300  63 68 20 72 65 67 69 73  74 65 72 73 0a 20 20 20  |ch registers.   |
00000310  20 20 20 20 61 72 65 20  69 6e 20 75 73 65 20 2d  |    are in use -|
00000320  20 69 6e 20 74 68 65 20  65 78 61 6d 70 6c 65 20  | in the example |
00000330  62 65 6c 6f 77 2c 20 74  68 65 20 66 6f 75 72 20  |below, the four |
00000340  62 69 74 73 20 6f 66 20  64 61 74 61 20 61 72 65  |bits of data are|
00000350  20 69 6e 70 75 74 0a 20  20 20 20 20 20 20 74 68  | input.       th|
00000360  72 6f 75 67 68 20 72 65  67 69 73 74 65 72 73 20  |rough registers |
00000370  30 20 74 6f 20 33 2c 20  61 6e 64 20 74 68 65 20  |0 to 3, and the |
00000380  66 75 6e 63 74 69 6f 6e  20 72 65 74 75 72 6e 73  |function returns|
00000390  20 74 68 65 20 76 61 6c  75 65 20 70 61 73 73 65  | the value passe|
000003a0  64 0a 20 20 20 20 20 20  20 62 61 63 6b 20 69 6e  |d.       back in|
000003b0  20 72 65 67 69 73 74 65  72 20 31 2e 20 2a 2f 0a  | register 1. */.|
000003c0  0a 20 20 20 20 74 61 73  6b 5f 68 61 6e 64 6c 65  |.    task_handle|
000003d0  20 3d 5f 73 77 69 28 57  69 6d 70 5f 49 6e 69 74  | =_swi(Wimp_Init|
000003e0  69 61 6c 69 73 65 2c 20  5f 49 4e 28 30 29 7c 5f  |ialise, _IN(0)|_|
000003f0  49 4e 28 31 29 7c 5f 49  4e 28 32 29 7c 5f 49 4e  |IN(1)|_IN(2)|_IN|
00000400  28 33 29 7c 5f 52 45 54  55 52 4e 28 31 29 20 2c  |(3)|_RETURN(1) ,|
00000410  20 33 31 30 2c 20 2a 28  69 6e 74 20 2a 29 22 54  | 310, *(int *)"T|
00000420  41 53 4b 22 2c 20 22 5f  73 77 69 28 29 20 65 78  |ASK", "_swi() ex|
00000430  61 6d 70 6c 65 22 2c 20  6c 69 73 74 29 3b 0a 0a  |ample", list);..|
00000440  20 20 20 20 64 6f 20 7b  0a 20 20 20 20 20 20 20  |    do {.       |
00000450  20 70 6f 6c 6c 72 65 73  75 6c 74 20 3d 20 5f 73  | pollresult = _s|
00000460  77 69 28 57 69 6d 70 5f  50 6f 6c 6c 2c 20 5f 49  |wi(Wimp_Poll, _I|
00000470  4e 28 30 29 7c 5f 49 4e  28 31 29 7c 5f 52 45 54  |N(0)|_IN(1)|_RET|
00000480  55 52 4e 28 30 29 2c 20  50 4f 4c 4c 5f 4d 41 53  |URN(0), POLL_MAS|
00000490  4b 2c 20 70 6f 6c 6c 62  6c 6f 63 6b 29 3b 0a 20  |K, pollblock);. |
000004a0  20 20 20 20 20 20 20 73  77 69 74 63 68 20 28 70  |       switch (p|
000004b0  6f 6c 6c 72 65 73 75 6c  74 29 20 7b 0a 20 20 20  |ollresult) {.   |
000004c0  20 20 20 20 20 20 20 20  20 63 61 73 65 20 31 37  |         case 17|
000004d0  3a 0a 20 20 20 20 20 20  20 20 20 20 20 20 63 61  |:.            ca|
000004e0  73 65 20 31 38 3a 0a 20  20 20 20 20 20 20 20 20  |se 18:.         |
000004f0  20 20 20 63 61 73 65 20  31 39 3a 20 77 69 6d 70  |   case 19: wimp|
00000500  6d 73 67 28 70 6f 6c 6c  72 65 73 75 6c 74 2c 20  |msg(pollresult, |
00000510  70 6f 6c 6c 62 6c 6f 63  6b 29 3b 0a 20 20 20 20  |pollblock);.    |
00000520  20 20 20 20 20 20 20 20  20 20 20 20 62 72 65 61  |            brea|
00000530  6b 3b 0a 20 20 20 20 20  20 20 20 7d 0a 20 20 20  |k;.        }.   |
00000540  20 7d 20 77 68 69 6c 65  20 28 51 55 49 54 46 4c  | } while (QUITFL|
00000550  41 47 20 3d 3d 20 30 29  3b 0a 0a 20 20 20 20 5f  |AG == 0);..    _|
00000560  73 77 69 28 57 69 6d 70  5f 43 6c 6f 73 65 44 6f  |swi(Wimp_CloseDo|
00000570  77 6e 2c 20 5f 49 4e 28  30 29 7c 5f 49 4e 28 31  |wn, _IN(0)|_IN(1|
00000580  29 2c 20 74 61 73 6b 5f  68 61 6e 64 6c 65 2c 20  |), task_handle, |
00000590  2a 28 69 6e 74 20 2a 29  22 54 41 53 4b 22 29 3b  |*(int *)"TASK");|
000005a0  0a 7d 0a 0a 76 6f 69 64  20 77 69 6d 70 6d 73 67  |.}..void wimpmsg|
000005b0  28 69 6e 74 20 72 65 73  75 6c 74 2c 20 75 6e 73  |(int result, uns|
000005c0  69 67 6e 65 64 20 63 68  61 72 20 70 6f 6c 6c 62  |igned char pollb|
000005d0  6c 6f 63 6b 5b 5d 29 0a  7b 0a 20 20 20 20 75 6e  |lock[]).{.    un|
000005e0  73 69 67 6e 65 64 20 6c  6f 6e 67 20 69 6e 74 20  |signed long int |
000005f0  20 20 6d 65 73 73 61 67  65 5f 61 63 74 69 6f 6e  |  message_action|
00000600  3b 0a 0a 20 20 20 20 2f  2a 20 54 68 69 73 20 66  |;..    /* This f|
00000610  75 6e 63 74 69 6f 6e 20  64 65 63 69 64 65 73 20  |unction decides |
00000620  77 68 69 63 68 20 70 61  72 74 69 63 75 6c 61 72  |which particular|
00000630  20 6d 65 73 73 61 67 65  20 69 73 20 62 65 69 6e  | message is bein|
00000640  67 20 70 61 73 73 65 64  2c 20 61 6e 64 0a 20 20  |g passed, and.  |
00000650  20 20 20 20 20 61 63 74  73 20 61 63 63 6f 72 64  |     acts accord|
00000660  69 6e 67 6c 79 20 2a 2f  0a 20 20 20 20 6d 65 73  |ingly */.    mes|
00000670  73 61 67 65 5f 61 63 74  69 6f 6e 20 3d 20 62 74  |sage_action = bt|
00000680  6f 77 28 70 6f 6c 6c 62  6c 6f 63 6b 2c 20 31 36  |ow(pollblock, 16|
00000690  29 3b 0a 20 20 20 20 73  77 69 74 63 68 20 28 6d  |);.    switch (m|
000006a0  65 73 73 61 67 65 5f 61  63 74 69 6f 6e 29 20 7b  |essage_action) {|
000006b0  0a 20 20 20 20 20 20 20  20 63 61 73 65 20 30 3a  |.        case 0:|
000006c0  20 51 55 49 54 46 4c 41  47 20 3d 20 31 3b 20 2f  | QUITFLAG = 1; /|
000006d0  2a 20 4d 65 73 73 61 67  65 5f 51 75 69 74 20 2a  |* Message_Quit *|
000006e0  2f 0a 20 20 20 20 20 20  20 20 20 20 20 20 62 72  |/.            br|
000006f0  65 61 6b 3b 0a 20 20 20  20 7d 0a 7d 0a 0a 75 6e  |eak;.    }.}..un|
00000700  73 69 67 6e 65 64 20 6c  6f 6e 67 20 69 6e 74 20  |signed long int |
00000710  62 74 6f 77 28 75 6e 73  69 67 6e 65 64 20 63 68  |btow(unsigned ch|
00000720  61 72 20 70 6f 6c 6c 62  6c 6f 63 6b 5b 5d 2c 20  |ar pollblock[], |
00000730  69 6e 74 20 69 6e 64 65  78 29 0a 7b 0a 20 20 20  |int index).{.   |
00000740  20 2f 2a 20 54 68 69 73  20 72 6f 75 74 69 6e 65  | /* This routine|
00000750  20 74 61 6b 65 73 20 61  6e 20 69 6e 64 65 78 20  | takes an index |
00000760  74 6f 20 61 6e 20 61 72  72 61 79 20 6f 66 20 63  |to an array of c|
00000770  68 61 72 20 69 6e 74 73  2c 20 61 6e 64 20 63 6f  |har ints, and co|
00000780  6e 76 65 72 74 73 0a 20  20 20 20 20 20 20 74 68  |nverts.       th|
00000790  65 20 6e 65 78 74 20 66  6f 75 72 20 62 79 74 65  |e next four byte|
000007a0  73 20 69 6e 74 6f 20 61  6e 20 69 6e 74 20 2a 2f  |s into an int */|
000007b0  0a 20 20 20 20 75 6e 73  69 67 6e 65 64 20 6c 6f  |.    unsigned lo|
000007c0  6e 67 20 69 6e 74 20 20  20 72 65 73 75 6c 74 20  |ng int   result |
000007d0  3d 20 30 3b 0a 20 20 20  20 69 6e 74 20 6c 6f 6f  |= 0;.    int loo|
000007e0  70 3b 0a 0a 20 20 20 20  69 6e 64 65 78 20 2b 3d  |p;..    index +=|
000007f0  20 33 3b 0a 20 20 20 20  72 65 73 75 6c 74 20 3d  | 3;.    result =|
00000800  20 70 6f 6c 6c 62 6c 6f  63 6b 5b 69 6e 64 65 78  | pollblock[index|
00000810  2d 2d 5d 3b 0a 20 20 20  20 66 6f 72 28 6c 6f 6f  |--];.    for(loo|
00000820  70 20 3d 20 30 3b 20 6c  6f 6f 70 20 3c 20 33 3b  |p = 0; loop < 3;|
00000830  20 6c 6f 6f 70 2b 2b 29  20 7b 0a 20 20 20 20 20  | loop++) {.     |
00000840  20 20 20 72 65 73 75 6c  74 20 3d 20 72 65 73 75  |   result = resu|
00000850  6c 74 20 3c 3c 20 38 3b  0a 20 20 20 20 20 20 20  |lt << 8;.       |
00000860  20 72 65 73 75 6c 74 20  2b 3d 20 70 6f 6c 6c 62  | result += pollb|
00000870  6c 6f 63 6b 5b 69 6e 64  65 78 2d 2d 5d 3b 0a 20  |lock[index--];. |
00000880  20 20 20 7d 0a 0a 20 20  20 20 72 65 74 75 72 6e  |   }..    return|
00000890  20 28 72 65 73 75 6c 74  29 3b 0a 7d 0a           | (result);.}.|
0000089d