#include #include #include #include #include #include #include #include #include #include typedef unsigned char UINT8; typedef unsigned short UINT16; #define note(x) ((119318200+(x)/2)/(x)) void printfHelp(char *argv0) { printf("Ver: 20221223\n\n"); printf("<%s> \n", argv0); printf("For Example:\n"); printf("%s 4 \n", argv0); printf("\n%s -h:for help! \n", argv0); } UINT8 IoRead8 (UINT16 addr) { return inb(addr); } void IoWrite8 (UINT16 addr, UINT8 data) { outb(data,addr); } void DelayMs(int ms) { unsigned int i = 0, n = 656.36*ms; for (i = 0; i < n; i++) IoRead8(0x0b); } void BeepOn(UINT8 note, UINT8 octave) { // beep tones UINT16 tones[8] = { note(26163),note(29366),note(32963),note(34923),note(39200),note(44000),note(49388),note(26163 * 2) }; UINT16 Frequency = tones[(note % 8)]; if (octave - 1 >= 0) Frequency >>= octave - 1; else Frequency <<= 1 - octave; //set up channel 1 (used for delays) IoWrite8(0x43, 0x54); IoWrite8(0x41, 0x12); //set up channel 2 (used by speaker) IoWrite8(0x43, 0xb6); IoWrite8(0x42, (UINT8)Frequency); IoWrite8(0x42, (UINT8)(Frequency >> 8)); //turn the speaker on IoWrite8(0x61, IoRead8(0x61) | 3); } void BeepOff(void) { IoWrite8(0x61, IoRead8(0x61) & 0xfc); } void Beep(UINT8 note, UINT8 octave, unsigned int duration) { BeepOn(note, octave); DelayMs(duration); BeepOff(); } int main(int argc, char* argv[]) { int i = 0; UINT8 mytimes; if (argv[1] == NULL || !strcmp(argv[1], "-h")) { printfHelp(argv[0]); return 0; } if (iopl(3) == -1) { fprintf(stderr, "Failed to set IOPL: %s\nRun with sudo!\n", strerror(errno)); return EXIT_FAILURE; } mytimes = (UINT8)strtoul(argv[1], NULL, 10); for (i = 0; i < mytimes; i++) { Beep(6, 1, 120); DelayMs(120); } }