ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
sound.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
52/* <usermanual>
53@keyword SOUND
54
55@english
56This command allows you to play a certain frequency, for a certain time and on certain
57channels. The command has a number of variations. First, you can omit the channel.
58In this case, the sound will be played on all channels (or on those enabled by the
59''VOICES''/''CHANNELS'' command). Then you can omit the duration, in which case the sound will
60continue to play while the next instruction is executed. Duration is expressed
61in milliseconds.
62
63@italian
64Questo comando permette di suonare una certa frequenza, per un certo tempo e su certi
65canali. Il comando ha una serie di varianti. In primo luogo, è possibile omettere il
66canale. In tal caso, il suono sarà suonato su tutti i canali (oppure su quelli abilitati
67dal comando ''VOICES''/''CHANNELS''). Poi è possibile omettere la durata, e in tal caso il suono
68continuerà ad essere suonato mentre viene eseguita l'istruzione successiva. La durata è
69espressa in millisecondi.
70
71@syntax SOUND #[freq] {, #[duration] {ON #[channels]}}
72
73@example SOUND #440
74@example SOUND #440, #250
75@example SOUND #440, #250 ON #%001
76
77@target pc1403
78</usermanual> */
79void sound( Environment * _environment, int _freq, int _delay, int _channels ) {
80
81 //Registers 0 and 1 operate together to form channel A's final pitch. The eight least significant bits are sent
82 //to register 0 and the four most significant bits are sent to register 1. The output frequency is equal to the
83 //IC's incoming clock frequency divided by 16 and then further divided by the number written to the course and
84 //fine pitch registers, so the higher the number written to these, the lower the pitch. For example, if a
85 //frequency of 1KHz was required and the IC's clock frequency was 1MHz, a total division rate of 1000 would
86 //be needed. The sound generator itself divides by 16 so the course and fine pitch registers must provide a
87 //further division by 62.5 (due to the fact that 1000/16 is 62.5). A division rate of 62 or 63 will be
88 //accurate enough, since the registers can only store whole numbers. Therefore, 62 or 63 would be written
89 //to register 0 and 0 would be written to register 1.
90
91 int chipsetFrequency = ( 3576000 / _freq ) / 16;
92
93 // ay8910_start( _environment, _channels );
94 // ay8910_set_frequency( _environment, _channels, chipsetFrequency );
95 if ( _delay ) {
96 // ay8910_set_duration( _environment, _channels, _delay / 50 /* approx! */ );
97 // ay8910_wait_duration( _environment, _channels );
98 }
99
100}
101
113/* <usermanual>
114@keyword SOUND
115
116@syntax SOUND [freq] {, [duration] {ON [channels]}}
117
118@example SOUND laDiesis
119@example SOUND solMaggiore, breve
120@example SOUND solMaggiore, lunga ON primaVoce
121
122@target pc1403
123</usermanual> */
124void sound_vars( Environment * _environment, char * _freq, char * _delay, char * _channels ) {
125
126 Variable * freq = variable_retrieve_or_define( _environment, _freq, VT_WORD, 440 );
127
128 Variable * cpuFrequency = variable_temporary( _environment, VT_DWORD, "(chipsetFrequency)" );
129 variable_store( _environment, cpuFrequency->name, 3576000 );
130 Variable * sixteen = variable_temporary( _environment, VT_WORD, "(16)" );
131 variable_store( _environment, sixteen->name, 16 );
132
133 Variable * chipsetFrequency = variable_div( _environment,
134 variable_div( _environment,
135 cpuFrequency->name,
136 freq->name,
137 NULL
138 )->name,
139 sixteen->name,
140 NULL
141 );
142
143 if ( _channels ) {
144 Variable * channels = variable_retrieve_or_define( _environment, _channels, VT_WORD, 0x07 );
145 // ay8910_start_var( _environment, channels->realName );
146 // ay8910_set_frequency_vars( _environment, channels->realName, chipsetFrequency->realName );
147 if ( _delay ) {
148 Variable * delay = variable_retrieve_or_define( _environment, _delay, VT_WORD, 0 );
149 Variable * delayScaled = variable_sr_const( _environment, delay->name, 5 /* approx! */ );
150 // ay8910_set_duration_vars( _environment, channels->realName, delayScaled->realName );
151 // ay8910_wait_duration_vars( _environment, channels->realName );
152 }
153 } else {
154 // ay8910_start_var( _environment, NULL );
155 // ay8910_set_frequency_vars( _environment, NULL, chipsetFrequency->realName );
156 if ( _delay ) {
157 Variable * delay = variable_retrieve_or_define( _environment, _delay, VT_WORD, 0 );
158 Variable * delayScaled = variable_sr_const( _environment, delay->name, 5 /* approx! */ );
159 // ay8910_set_duration_vars( _environment, NULL, delayScaled->realName );
160 // ay8910_wait_duration_vars( _environment, NULL );
161 }
162 }
163
164}
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_sr_const(Environment *_environment, char *_destination, int _bits)
Variable * variable_div(Environment *_environment, char *_source, char *_destination, char *_remainder)
Make a division between two variable and return the product of them.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * variable_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
void sound_vars(Environment *_environment, char *_freq, char *_delay, char *_channels)
Emit ASM code for SOUND ....
Definition sound.c:79
void sound(Environment *_environment, int _freq, int _delay, int _channels)
Emit ASM code for SOUND ....
Definition sound.c:57
char * name
Definition ugbc.h:979
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_WORD
Definition ugbc.h:455
@ VT_DWORD
Definition ugbc.h:460