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@target cpc
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 = ( 4000000 / _freq ) / 16;
69
70 ay8910_start( _environment, ( _channels & 0x07 ) );
71 ay8910_set_frequency( _environment, _channels, chipsetFrequency );
72 if ( _delay ) {
73 ay8910_set_duration( _environment, _channels, _delay / 20 /* approx! */ );
74 ay8910_wait_duration( _environment, _channels );
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, 4000000 );
96 Variable * sixteen = variable_temporary( _environment, VT_WORD, "(16)" );
97 variable_store( _environment, sixteen->name, 16 );
98
99 Variable * chipsetFrequency = variable_div( _environment,
100 variable_div( _environment,
101 cpuFrequency->name,
102 freq->name,
103 NULL
104 )->name,
105 sixteen->name,
106 NULL
107 );
108
109 if ( _channels ) {
110 Variable * channels = variable_retrieve_or_define( _environment, _channels, VT_WORD, 0x07 );
111 ay8910_start_var( _environment, channels->realName );
112 ay8910_set_frequency_vars( _environment, channels->realName, chipsetFrequency->realName );
113 if ( _delay ) {
114 Variable * delay = variable_retrieve_or_define( _environment, _delay, VT_WORD, 0 );
115 Variable * durationInTicks = variable_div_const( _environment, delay->name, 20, NULL );
116 ay8910_set_duration_vars( _environment, channels->realName, durationInTicks->realName );
117 ay8910_wait_duration_vars( _environment, channels->realName );
118 }
119 } else {
120 ay8910_start_var( _environment, NULL );
121 ay8910_set_frequency_vars( _environment, NULL, chipsetFrequency->realName );
122 if ( _delay ) {
123 Variable * delay = variable_retrieve_or_define( _environment, _delay, VT_WORD, 0 );
124 Variable * durationInTicks = variable_div_const( _environment, delay->name, 20, NULL );
125 ay8910_set_duration_vars( _environment, NULL, durationInTicks->realName );
126 ay8910_wait_duration_vars( _environment, NULL );
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_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 ay8910_set_frequency_vars(Environment *_environment, char *_channels, char *_frequency)
Definition ay8910.c:969
void ay8910_wait_duration_vars(Environment *_environment, char *_channel)
Definition ay8910.c:1104
void ay8910_wait_duration(Environment *_environment, int _channel)
Definition ay8910.c:1075
void ay8910_set_frequency(Environment *_environment, int _channels, int _frequency)
Definition ay8910.c:684
void ay8910_set_duration_vars(Environment *_environment, char *_channel, char *_duration)
Definition ay8910.c:1084
void ay8910_start_var(Environment *_environment, char *_channels)
Definition ay8910.c:717
void ay8910_start(Environment *_environment, int _channels)
Definition ay8910.c:92
void ay8910_set_duration(Environment *_environment, int _channel, int _duration)
Definition ay8910.c:1066
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