Home » Archimedes archive » Acorn User » AU 1995-12 B.adf » Regulars » C/c/AcornSWI
C/c/AcornSWI
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 1995-12 B.adf » Regulars |
Filename: | C/c/AcornSWI |
Read OK: | ✔ |
File size: | 059F bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
/* SWI calls from C - an example by Steve Mumford */ /* for Acorn User, December 1995 */ #include <stdio.h> /* The next two #includes are needed to supply the definitions of data structures and functions necessary for SWI calling */ #include "kernel.h" #include "swis.h" int main(void) { /* The next line declares a structure used to hold any error information passed back from the SWI - we shouldn't really need it.. */ _kernel_oserror error; /* The next two lines declare two structures to hold the registers - each structure contains an array of 10 ints named 'r'. These can be accessed by 'in.r[0]' and 'out.r[3]', for example */ _kernel_swi_regs in; _kernel_swi_regs out; printf("This program will repeatedly call SWI OS_Mouse, and print out\n"); printf("the coordinates of the mouse and the button state when a mouse\n"); printf("button is pressed. Press the right-hand mouse button to leave\n"); printf("the program.\n\n"); do { /* The following line actually calls the SWI, and 'OS_Mouse' has been #defined to the correct SWI number in swis.h */ error = *_kernel_swi(OS_Mouse, &in, &out); if (out.r[2] != 0) { printf("The mouse coordinates are: x = %d, y = %d\n", out.r[0], out.r[1]); printf("The button value is: %d\n", out.r[2]); } } while (out.r[2] != 1); /* Quit when the right hand button is pressed */ }
00000000 2f 2a 20 53 57 49 20 63 61 6c 6c 73 20 66 72 6f |/* SWI calls fro| 00000010 6d 20 43 20 2d 20 61 6e 20 65 78 61 6d 70 6c 65 |m C - an example| 00000020 20 62 79 20 53 74 65 76 65 20 4d 75 6d 66 6f 72 | by Steve Mumfor| 00000030 64 20 2a 2f 0a 2f 2a 20 66 6f 72 20 41 63 6f 72 |d */./* for Acor| 00000040 6e 20 55 73 65 72 2c 20 44 65 63 65 6d 62 65 72 |n User, December| 00000050 20 31 39 39 35 20 2a 2f 0a 0a 23 69 6e 63 6c 75 | 1995 */..#inclu| 00000060 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 0a 2f 2a |de <stdio.h>../*| 00000070 20 54 68 65 20 6e 65 78 74 20 74 77 6f 20 23 69 | The next two #i| 00000080 6e 63 6c 75 64 65 73 20 61 72 65 20 6e 65 65 64 |ncludes are need| 00000090 65 64 20 74 6f 20 73 75 70 70 6c 79 20 74 68 65 |ed to supply the| 000000a0 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 | definitions of | 000000b0 64 61 74 61 0a 20 20 20 73 74 72 75 63 74 75 72 |data. structur| 000000c0 65 73 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 73 |es and functions| 000000d0 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20 53 | necessary for S| 000000e0 57 49 20 63 61 6c 6c 69 6e 67 20 2a 2f 0a 0a 23 |WI calling */..#| 000000f0 69 6e 63 6c 75 64 65 20 22 6b 65 72 6e 65 6c 2e |include "kernel.| 00000100 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 73 77 69 |h".#include "swi| 00000110 73 2e 68 22 0a 0a 69 6e 74 0a 6d 61 69 6e 28 76 |s.h"..int.main(v| 00000120 6f 69 64 29 0a 7b 0a 20 20 20 2f 2a 20 54 68 65 |oid).{. /* The| 00000130 20 6e 65 78 74 20 6c 69 6e 65 20 64 65 63 6c 61 | next line decla| 00000140 72 65 73 20 61 20 73 74 72 75 63 74 75 72 65 20 |res a structure | 00000150 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 61 6e 79 |used to hold any| 00000160 20 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 | error informati| 00000170 6f 6e 0a 20 20 20 20 20 20 70 61 73 73 65 64 20 |on. passed | 00000180 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 53 57 |back from the SW| 00000190 49 20 2d 20 77 65 20 73 68 6f 75 6c 64 6e 27 74 |I - we shouldn't| 000001a0 20 72 65 61 6c 6c 79 20 6e 65 65 64 20 69 74 2e | really need it.| 000001b0 2e 20 2a 2f 0a 0a 20 20 20 5f 6b 65 72 6e 65 6c |. */.. _kernel| 000001c0 5f 6f 73 65 72 72 6f 72 20 20 20 65 72 72 6f 72 |_oserror error| 000001d0 3b 0a 0a 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 |;.. /* The nex| 000001e0 74 20 74 77 6f 20 6c 69 6e 65 73 20 64 65 63 6c |t two lines decl| 000001f0 61 72 65 20 74 77 6f 20 73 74 72 75 63 74 75 72 |are two structur| 00000200 65 73 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 |es to hold the r| 00000210 65 67 69 73 74 65 72 73 20 2d 0a 20 20 20 20 20 |egisters -. | 00000220 20 65 61 63 68 20 73 74 72 75 63 74 75 72 65 20 | each structure | 00000230 63 6f 6e 74 61 69 6e 73 20 61 6e 20 61 72 72 61 |contains an arra| 00000240 79 20 6f 66 20 31 30 20 69 6e 74 73 20 6e 61 6d |y of 10 ints nam| 00000250 65 64 20 27 72 27 2e 0a 20 20 20 20 20 20 54 68 |ed 'r'.. Th| 00000260 65 73 65 20 63 61 6e 20 62 65 20 61 63 63 65 73 |ese can be acces| 00000270 73 65 64 20 62 79 20 27 69 6e 2e 72 5b 30 5d 27 |sed by 'in.r[0]'| 00000280 20 61 6e 64 20 27 6f 75 74 2e 72 5b 33 5d 27 2c | and 'out.r[3]',| 00000290 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 2a 2f 0a | for example */.| 000002a0 0a 20 20 20 5f 6b 65 72 6e 65 6c 5f 73 77 69 5f |. _kernel_swi_| 000002b0 72 65 67 73 20 20 69 6e 3b 0a 20 20 20 5f 6b 65 |regs in;. _ke| 000002c0 72 6e 65 6c 5f 73 77 69 5f 72 65 67 73 20 20 6f |rnel_swi_regs o| 000002d0 75 74 3b 0a 0a 20 20 20 70 72 69 6e 74 66 28 22 |ut;.. printf("| 000002e0 54 68 69 73 20 70 72 6f 67 72 61 6d 20 77 69 6c |This program wil| 000002f0 6c 20 72 65 70 65 61 74 65 64 6c 79 20 63 61 6c |l repeatedly cal| 00000300 6c 20 53 57 49 20 4f 53 5f 4d 6f 75 73 65 2c 20 |l SWI OS_Mouse, | 00000310 61 6e 64 20 70 72 69 6e 74 20 6f 75 74 5c 6e 22 |and print out\n"| 00000320 29 3b 0a 20 20 20 70 72 69 6e 74 66 28 22 74 68 |);. printf("th| 00000330 65 20 63 6f 6f 72 64 69 6e 61 74 65 73 20 6f 66 |e coordinates of| 00000340 20 74 68 65 20 6d 6f 75 73 65 20 61 6e 64 20 74 | the mouse and t| 00000350 68 65 20 62 75 74 74 6f 6e 20 73 74 61 74 65 20 |he button state | 00000360 77 68 65 6e 20 61 20 6d 6f 75 73 65 5c 6e 22 29 |when a mouse\n")| 00000370 3b 0a 20 20 20 70 72 69 6e 74 66 28 22 62 75 74 |;. printf("but| 00000380 74 6f 6e 20 69 73 20 70 72 65 73 73 65 64 2e 20 |ton is pressed. | 00000390 50 72 65 73 73 20 74 68 65 20 72 69 67 68 74 2d |Press the right-| 000003a0 68 61 6e 64 20 6d 6f 75 73 65 20 62 75 74 74 6f |hand mouse butto| 000003b0 6e 20 74 6f 20 6c 65 61 76 65 5c 6e 22 29 3b 0a |n to leave\n");.| 000003c0 20 20 20 70 72 69 6e 74 66 28 22 74 68 65 20 70 | printf("the p| 000003d0 72 6f 67 72 61 6d 2e 5c 6e 5c 6e 22 29 3b 0a 0a |rogram.\n\n");..| 000003e0 20 20 20 64 6f 20 7b 0a 0a 20 20 20 20 20 20 2f | do {.. /| 000003f0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 |* The following | 00000400 6c 69 6e 65 20 61 63 74 75 61 6c 6c 79 20 63 61 |line actually ca| 00000410 6c 6c 73 20 74 68 65 20 53 57 49 2c 20 61 6e 64 |lls the SWI, and| 00000420 20 27 4f 53 5f 4d 6f 75 73 65 27 20 68 61 73 20 | 'OS_Mouse' has | 00000430 62 65 65 6e 0a 20 20 20 20 20 20 20 20 20 23 64 |been. #d| 00000440 65 66 69 6e 65 64 20 74 6f 20 74 68 65 20 63 6f |efined to the co| 00000450 72 72 65 63 74 20 53 57 49 20 6e 75 6d 62 65 72 |rrect SWI number| 00000460 20 69 6e 20 73 77 69 73 2e 68 20 2a 2f 0a 0a 20 | in swis.h */.. | 00000470 20 20 20 20 20 65 72 72 6f 72 20 3d 20 2a 5f 6b | error = *_k| 00000480 65 72 6e 65 6c 5f 73 77 69 28 4f 53 5f 4d 6f 75 |ernel_swi(OS_Mou| 00000490 73 65 2c 20 26 69 6e 2c 20 26 6f 75 74 29 3b 0a |se, &in, &out);.| 000004a0 0a 20 20 20 20 20 20 69 66 20 28 6f 75 74 2e 72 |. if (out.r| 000004b0 5b 32 5d 20 21 3d 20 30 29 20 7b 0a 20 20 20 20 |[2] != 0) {. | 000004c0 20 20 20 20 20 70 72 69 6e 74 66 28 22 54 68 65 | printf("The| 000004d0 20 6d 6f 75 73 65 20 63 6f 6f 72 64 69 6e 61 74 | mouse coordinat| 000004e0 65 73 20 61 72 65 3a 20 78 20 3d 20 25 64 2c 20 |es are: x = %d, | 000004f0 79 20 3d 20 25 64 5c 6e 22 2c 20 6f 75 74 2e 72 |y = %d\n", out.r| 00000500 5b 30 5d 2c 20 6f 75 74 2e 72 5b 31 5d 29 3b 0a |[0], out.r[1]);.| 00000510 20 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 28 | printf(| 00000520 22 54 68 65 20 62 75 74 74 6f 6e 20 76 61 6c 75 |"The button valu| 00000530 65 20 69 73 3a 20 25 64 5c 6e 22 2c 20 6f 75 74 |e is: %d\n", out| 00000540 2e 72 5b 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a |.r[2]);. }.| 00000550 20 20 20 7d 20 77 68 69 6c 65 20 28 6f 75 74 2e | } while (out.| 00000560 72 5b 32 5d 20 21 3d 20 31 29 3b 20 2f 2a 20 51 |r[2] != 1); /* Q| 00000570 75 69 74 20 77 68 65 6e 20 74 68 65 20 72 69 67 |uit when the rig| 00000580 68 74 20 68 61 6e 64 20 62 75 74 74 6f 6e 20 69 |ht hand button i| 00000590 73 20 70 72 65 73 73 65 64 20 2a 2f 0a 7d 0a |s pressed */.}.| 0000059f