1 /* play.c - command to play a tune */
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. */
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. */
39 /* If 0, follow state of SPEAKER_DATA bit, otherwise enable output
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
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
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. */
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
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
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)
112 grub_uint16_t duration
;
118 unsigned char status
;
120 status
= grub_inb (SPEAKER
);
121 grub_outb (status
& ~(SPEAKER_TMR2
| SPEAKER_DATA
), SPEAKER
);
125 beep_on (grub_uint16_t pitch
)
127 unsigned char status
;
128 unsigned int counter
;
132 else if (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 */
144 status
= grub_inb (SPEAKER
);
145 grub_outb (status
| SPEAKER_TMR2
| SPEAKER_DATA
, SPEAKER
);
148 /* Returns whether playing should continue. */
150 play (unsigned tempo
, struct note
*note
)
154 if (note
->pitch
== T_FINE
|| grub_getkey_noblock () != GRUB_TERM_NO_KEY
)
157 grub_dprintf ("play", "pitch = %d, duration = %d\n", note
->pitch
,
167 beep_on (note
->pitch
);
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
));
179 grub_cmd_play (grub_command_t cmd
__attribute__ ((unused
)),
180 int argc
, char **args
)
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"));
197 file
= grub_file_open (args
[0]);
202 if (grub_file_read (file
, &tempo
, sizeof (tempo
)) != sizeof (tempo
))
204 grub_file_close (file
);
206 grub_error (GRUB_ERR_FILE_READ_ERROR
, N_("premature end of file %s"),
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
))
224 grub_file_close (file
);
233 tempo
= grub_strtoul (args
[0], &end
, 0);
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);
248 grub_error (GRUB_ERR_BAD_NUMBER
, N_("unrecognized number"));
252 note
.duration
= grub_strtoul (args
[i
+ 1], &end
, 0);
257 grub_error (GRUB_ERR_BAD_NUMBER
, N_("unrecognized number"));
261 if (play (tempo
, ¬e
))
271 static grub_command_t cmd
;
275 cmd
= grub_register_command ("play", grub_cmd_play
,
276 N_("FILE | TEMPO [PITCH1 DURATION1] [PITCH2 DURATION2] ... "),
282 grub_unregister_command (cmd
);