Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / i386 / pc / play.c
blob10a018181c217ed15ac2356a06241f4daef30775
1 /* play.c - command to play a tune */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 /* Lots of this file is borrowed from GNU/Hurd generic-speaker driver. */
22 #include <grub/dl.h>
23 #include <grub/file.h>
24 #include <grub/disk.h>
25 #include <grub/term.h>
26 #include <grub/misc.h>
27 #include <grub/cpu/io.h>
28 #include <grub/command.h>
29 #include <grub/i18n.h>
30 #include <grub/time.h>
32 GRUB_MOD_LICENSE ("GPLv3+");
34 #define BASE_TEMPO (60 * 1000)
36 /* The speaker port. */
37 #define SPEAKER 0x61
39 /* If 0, follow state of SPEAKER_DATA bit, otherwise enable output
40 from timer 2. */
41 #define SPEAKER_TMR2 0x01
43 /* If SPEAKER_TMR2 is not set, this provides direct input into the
44 speaker. Otherwise, this enables or disables the output from the
45 timer. */
46 #define SPEAKER_DATA 0x02
48 /* The PIT channel value ports. You can write to and read from them.
49 Do not mess with timer 0 or 1. */
50 #define PIT_COUNTER_0 0x40
51 #define PIT_COUNTER_1 0x41
52 #define PIT_COUNTER_2 0x42
54 /* The frequency of the PIT clock. */
55 #define PIT_FREQUENCY 0x1234dd
57 /* The PIT control port. You can only write to it. Do not mess with
58 timer 0 or 1. */
59 #define PIT_CTRL 0x43
60 #define PIT_CTRL_SELECT_MASK 0xc0
61 #define PIT_CTRL_SELECT_0 0x00
62 #define PIT_CTRL_SELECT_1 0x40
63 #define PIT_CTRL_SELECT_2 0x80
65 /* Read and load control. */
66 #define PIT_CTRL_READLOAD_MASK 0x30
67 #define PIT_CTRL_COUNTER_LATCH 0x00 /* Hold timer value until read. */
68 #define PIT_CTRL_READLOAD_LSB 0x10 /* Read/load the LSB. */
69 #define PIT_CTRL_READLOAD_MSB 0x20 /* Read/load the MSB. */
70 #define PIT_CTRL_READLOAD_WORD 0x30 /* Read/load the LSB then the MSB. */
72 /* Mode control. */
73 #define PIT_CTRL_MODE_MASK 0x0e
75 /* Interrupt on terminal count. Setting the mode sets output to low.
76 When counter is set and terminated, output is set to high. */
77 #define PIT_CTRL_INTR_ON_TERM 0x00
79 /* Programmable one-shot. When loading counter, output is set to
80 high. When counter terminated, output is set to low. Can be
81 triggered again from that point on by setting the gate pin to
82 high. */
83 #define PIT_CTRL_PROGR_ONE_SHOT 0x02
85 /* Rate generator. Output is low for one period of the counter, and
86 high for the other. */
87 #define PIT_CTRL_RATE_GEN 0x04
89 /* Square wave generator. Output is low for one half of the period,
90 and high for the other half. */
91 #define PIT_CTRL_SQUAREWAVE_GEN 0x06
93 /* Software triggered strobe. Setting the mode sets output to high.
94 When counter is set and terminated, output is set to low. */
95 #define PIT_CTRL_SOFTSTROBE 0x08
97 /* Hardware triggered strobe. Like software triggered strobe, but
98 only starts the counter when the gate pin is set to high. */
99 #define PIT_CTRL_HARDSTROBE 0x0a
101 /* Count mode. */
102 #define PIT_CTRL_COUNT_MASK 0x01
103 #define PIT_CTRL_COUNT_BINARY 0x00 /* 16-bit binary counter. */
104 #define PIT_CTRL_COUNT_BCD 0x01 /* 4-decade BCD counter. */
106 #define T_REST ((grub_uint16_t) 0)
107 #define T_FINE ((grub_uint16_t) -1)
109 struct note
111 grub_uint16_t pitch;
112 grub_uint16_t duration;
115 static void
116 beep_off (void)
118 unsigned char status;
120 status = grub_inb (SPEAKER);
121 grub_outb (status & ~(SPEAKER_TMR2 | SPEAKER_DATA), SPEAKER);
124 static void
125 beep_on (grub_uint16_t pitch)
127 unsigned char status;
128 unsigned int counter;
130 if (pitch < 20)
131 pitch = 20;
132 else if (pitch > 20000)
133 pitch = 20000;
135 counter = PIT_FREQUENCY / pitch;
137 /* Program timer 2. */
138 grub_outb (PIT_CTRL_SELECT_2 | PIT_CTRL_READLOAD_WORD
139 | PIT_CTRL_SQUAREWAVE_GEN | PIT_CTRL_COUNT_BINARY, PIT_CTRL);
140 grub_outb (counter & 0xff, PIT_COUNTER_2); /* LSB */
141 grub_outb ((counter >> 8) & 0xff, PIT_COUNTER_2); /* MSB */
143 /* Start speaker. */
144 status = grub_inb (SPEAKER);
145 grub_outb (status | SPEAKER_TMR2 | SPEAKER_DATA, SPEAKER);
148 /* Returns whether playing should continue. */
149 static int
150 play (unsigned tempo, struct note *note)
152 grub_uint64_t to;
154 if (note->pitch == T_FINE || grub_getkey_noblock () != GRUB_TERM_NO_KEY)
155 return 1;
157 grub_dprintf ("play", "pitch = %d, duration = %d\n", note->pitch,
158 note->duration);
160 switch (note->pitch)
162 case T_REST:
163 beep_off ();
164 break;
166 default:
167 beep_on (note->pitch);
168 break;
171 to = grub_get_time_ms () + BASE_TEMPO * note->duration / tempo;
172 while ((grub_get_time_ms () <= to)
173 && (grub_getkey_noblock () == GRUB_TERM_NO_KEY));
175 return 0;
178 static grub_err_t
179 grub_cmd_play (grub_command_t cmd __attribute__ ((unused)),
180 int argc, char **args)
183 if (argc < 1)
184 return grub_error (GRUB_ERR_BAD_ARGUMENT,
185 /* TRANSLATORS: It's musical notes, not the notes
186 you take. Play command expects arguments which can
187 be either a filename or tempo+notes.
188 This error happens if none is specified. */
189 N_("filename or tempo and notes expected"));
191 if (argc == 1)
193 struct note buf;
194 grub_uint32_t tempo;
195 grub_file_t file;
197 file = grub_file_open (args[0]);
199 if (! file)
200 return grub_errno;
202 if (grub_file_read (file, &tempo, sizeof (tempo)) != sizeof (tempo))
204 grub_file_close (file);
205 if (!grub_errno)
206 grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
207 args[0]);
208 return grub_errno;
211 tempo = grub_le_to_cpu32 (tempo);
212 grub_dprintf ("play","tempo = %d\n", tempo);
214 while (grub_file_read (file, &buf,
215 sizeof (struct note)) == sizeof (struct note))
217 buf.pitch = grub_le_to_cpu16 (buf.pitch);
218 buf.duration = grub_le_to_cpu16 (buf.duration);
220 if (play (tempo, &buf))
221 break;
224 grub_file_close (file);
226 else
228 char *end;
229 unsigned tempo;
230 struct note note;
231 int i;
233 tempo = grub_strtoul (args[0], &end, 0);
235 if (*end)
236 /* Was not a number either, assume it was supposed to be a file name. */
237 return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), args[0]);
239 grub_dprintf ("play","tempo = %d\n", tempo);
241 for (i = 1; i + 1 < argc; i += 2)
243 note.pitch = grub_strtoul (args[i], &end, 0);
244 if (grub_errno)
245 break;
246 if (*end)
248 grub_error (GRUB_ERR_BAD_NUMBER, N_("unrecognized number"));
249 break;
252 note.duration = grub_strtoul (args[i + 1], &end, 0);
253 if (grub_errno)
254 break;
255 if (*end)
257 grub_error (GRUB_ERR_BAD_NUMBER, N_("unrecognized number"));
258 break;
261 if (play (tempo, &note))
262 break;
266 beep_off ();
268 return 0;
271 static grub_command_t cmd;
273 GRUB_MOD_INIT(play)
275 cmd = grub_register_command ("play", grub_cmd_play,
276 N_("FILE | TEMPO [PITCH1 DURATION1] [PITCH2 DURATION2] ... "),
277 N_("Play a tune."));
280 GRUB_MOD_FINI(play)
282 grub_unregister_command (cmd);