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-2024 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@target gb
55</usermanual> */
56void sound( Environment * _environment, int _freq, int _delay, int _channels ) {
57
58 //Registers 0 and 1 operate together to form channel A's final pitch. The eight least significant bits are sent
59 //to register 0 and the four most significant bits are sent to register 1. The output frequency is equal to the
60 //IC's incoming clock frequency divided by 16 and then further divided by the number written to the course and
61 //fine pitch registers, so the higher the number written to these, the lower the pitch. For example, if a
62 //frequency of 1KHz was required and the IC's clock frequency was 1MHz, a total division rate of 1000 would
63 //be needed. The sound generator itself divides by 16 so the course and fine pitch registers must provide a
64 //further division by 62.5 (due to the fact that 1000/16 is 62.5). A division rate of 62 or 63 will be
65 //accurate enough, since the registers can only store whole numbers. Therefore, 62 or 63 would be written
66 //to register 0 and 0 would be written to register 1.
67
68 int chipsetFrequency = 2048 - ( 131072 / _freq );
69
70 gb_start( _environment, ( _channels & 0x03 ) );
71 gb_set_frequency( _environment, ( _channels & 0x03 ), chipsetFrequency );
72 if ( _delay ) {
73 gb_set_duration( _environment, ( _channels & 0x03 ), _delay / 50 /* approx! */ );
74 gb_wait_duration( _environment, ( _channels & 0x03 ) );
75 }
76
77}
78
90void sound_vars( Environment * _environment, char * _freq, char * _delay, char * _channels ) {
91
92 Variable * freq = variable_retrieve_or_define( _environment, _freq, VT_WORD, 440 );
93
94 Variable * cpuFrequency = variable_temporary( _environment, VT_DWORD, "(chipsetFrequency)" );
95 variable_store( _environment, cpuFrequency->name, 131072 );
96 Variable * n2048 = variable_temporary( _environment, VT_WORD, "(2048)" );
97 variable_store( _environment, n2048->name, 2048 );
98
99 Variable * chipsetFrequency = variable_sub( _environment,
100 n2048->name,
101 variable_div( _environment,
102 cpuFrequency->name,
103 freq->name,
104 NULL
105 )->name
106 );
107
108 if ( _channels ) {
109 Variable * channels = variable_retrieve_or_define( _environment, _channels, VT_WORD, 0x03 );
110 gb_start_var( _environment, channels->realName );
111 gb_set_frequency_vars( _environment, channels->realName, chipsetFrequency->realName );
112 if ( _delay ) {
113 Variable * delay = variable_retrieve_or_define( _environment, _delay, VT_WORD, 0 );
114 Variable * durationInTicks = variable_div_const( _environment,delay->name, 20, NULL );
115 gb_set_duration_vars( _environment, channels->realName, durationInTicks->realName );
116 gb_wait_duration_vars( _environment, channels->realName );
117 }
118 } else {
119 gb_start_var( _environment, NULL );
120 gb_set_frequency_vars( _environment, NULL, chipsetFrequency->realName );
121 if ( _delay ) {
122 Variable * delay = variable_retrieve_or_define( _environment, _delay, VT_WORD, 0 );
123 Variable * durationInTicks = variable_div_const( _environment, delay->name, 20, NULL );
124 gb_set_duration_vars( _environment, NULL, durationInTicks->realName );
125 gb_wait_duration_vars( _environment, NULL );
126 }
127 }
128
129
130}
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_div_const(Environment *_environment, char *_source, int _destination, char *_remainder)
Variable * variable_sub(Environment *_environment, char *_source, char *_dest)
Make a differenze between two variable and return the difference of them.
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
void gb_start_var(Environment *_environment, char *_channels)
Definition gb.c:2681
void gb_set_duration(Environment *_environment, int _channel, int _duration)
Definition gb.c:2965
void gb_wait_duration(Environment *_environment, int _channel)
Definition gb.c:2971
void gb_wait_duration_vars(Environment *_environment, char *_channel)
Definition gb.c:2995
void gb_start(Environment *_environment, int _channels)
Definition gb.c:2303
void gb_set_frequency_vars(Environment *_environment, char *_channels, char *_frequency)
Definition gb.c:2882
void gb_set_frequency(Environment *_environment, int _channels, int _frequency)
Definition gb.c:2657
void gb_set_duration_vars(Environment *_environment, char *_channel, char *_duration)
Definition gb.c:2977
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
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