ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
wait_cycles.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
49void wait_cycles( Environment * _environment, int _timing, int _parallel ) {
50
51 if ( _environment->protothread && _parallel ) {
52
53 if ( ! _environment->procedureName ) {
55 }
56
57 if ( _environment->protothreadForbid ) {
59 }
60
61 char waitVariableName[MAX_TEMPORARY_STORAGE]; sprintf(waitVariableName, "%swaitms%d", _environment->procedureName, _environment->protothreadStep );
62
64 memset( ((struct _Environment *)_environment)->arrayDimensionsEach, 0, sizeof( int ) * MAX_ARRAY_DIMENSIONS );
65 ((struct _Environment *)_environment)->arrayDimensionsEach[0] = _environment->protothreadConfig.count;
66 ((struct _Environment *)_environment)->arrayDimensions = 1;
67 variable_define( _environment, waitVariableName, VT_TARRAY, 0 );
68 variable_array_type( _environment, waitVariableName, VT_WORD );
70
71 variable_store_array_const( _environment, waitVariableName, _timing );
72
73 yield( _environment );
74
75 char protothreadLabel[MAX_TEMPORARY_STORAGE]; sprintf(protothreadLabel, "%spt%d", _environment->procedureName, _environment->protothreadStep );
76
77 variable_decrement_mt( _environment, waitVariableName );
78 Variable * result = variable_temporary( _environment, VT_WORD, "(temporary)" );
79 variable_move_from_mt( _environment, waitVariableName, result->name );
80 variable_compare_and_branch_const( _environment, result->name, 0, protothreadLabel, 1 );
81
82 cpu_protothread_save( _environment, "PROTOTHREADCT", ( _environment->protothreadStep - 1 ) );
83 cpu_protothread_set_state( _environment, "PROTOTHREADCT", PROTOTHREAD_STATUS_YIELDED );
84 cpu_return( _environment );
85
86 cpu_label( _environment, protothreadLabel );
87 cpu_protothread_set_state( _environment, "PROTOTHREADCT", PROTOTHREAD_STATUS_RUNNING );
88
89 ++_environment->protothreadStep;
90
91 } else {
92
93 char timingString[MAX_TEMPORARY_STORAGE]; sprintf(timingString, "#$%2.2x", _timing );
94
95 cpu_busy_wait( _environment, timingString );
96
97 }
98
99}
100
109void wait_cycles_var( Environment * _environment, char * _timing, int _parallel ) {
110
111 Variable * timing = variable_retrieve( _environment, _timing );
112
113 if ( _environment->protothread && _environment->procedureName && _parallel) {
114
115 if ( _environment->protothreadForbid ) {
117 }
118
119 char waitVariableName[MAX_TEMPORARY_STORAGE]; sprintf(waitVariableName, "%swaitms%d", _environment->procedureName, _environment->protothreadStep );
120
122 memset( ((struct _Environment *)_environment)->arrayDimensionsEach, 0, sizeof( int ) * MAX_ARRAY_DIMENSIONS );
123 ((struct _Environment *)_environment)->arrayDimensionsEach[0] = _environment->protothreadConfig.count;
124 ((struct _Environment *)_environment)->arrayDimensions = 1;
125 variable_define( _environment, waitVariableName, VT_TARRAY, 0 );
126 variable_array_type( _environment, waitVariableName, VT_WORD );
128
129 variable_move_to_mt( _environment, _timing, waitVariableName );
130
131 yield( _environment );
132
133 char protothreadLabel[MAX_TEMPORARY_STORAGE]; sprintf(protothreadLabel, "%spt%d", _environment->procedureName, _environment->protothreadStep );
134
135 variable_decrement_mt( _environment, waitVariableName );
136 Variable * result = variable_temporary( _environment, VT_WORD, "(temporary)" );
137 variable_move_from_mt( _environment, waitVariableName, result->name );
138 variable_compare_and_branch_const( _environment, result->name, 0, protothreadLabel, 1 );
139
140 cpu_protothread_save( _environment, "PROTOTHREADCT", ( _environment->protothreadStep - 1 ) );
141 cpu_protothread_set_state( _environment, "PROTOTHREADCT", PROTOTHREAD_STATUS_YIELDED );
142 cpu_return( _environment );
143
144 cpu_label( _environment, protothreadLabel );
145 cpu_protothread_set_state( _environment, "PROTOTHREADCT", PROTOTHREAD_STATUS_RUNNING );
146
147 ++_environment->protothreadStep;
148
149
150 } else {
151
152 cpu_busy_wait( _environment, timing->realName );
153
154 }
155
156}
void cpu_protothread_save(Environment *_environment, char *_index, int _step)
Definition 6309.c:6203
void cpu_protothread_set_state(Environment *_environment, char *_index, int _state)
Definition 6309.c:6226
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_return(Environment *_environment)
Definition 6309.c:4030
void cpu_busy_wait(Environment *_environment, char *_timing)
Definition 6309.c:4183
Variable * variable_retrieve(Environment *_environment, char *_name)
void variable_compare_and_branch_const(Environment *_environment, char *_source, int _destination, char *_name, int _positive)
Variable * variable_array_type(Environment *_environment, char *_name, VariableType _type)
Variable * variable_move_from_mt(Environment *_environment, char *_source, char *_destination)
Increment a variable by one.
void variable_store_array_const(Environment *_environment, char *_array, int _value)
Variable * variable_define(Environment *_environment, char *_name, VariableType _type, int _value)
Define a variable for the program.
Variable * variable_move_to_mt(Environment *_environment, char *_source, char *_destination)
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
void variable_decrement_mt(Environment *_environment, char *_source)
Decrement a variable by one.
void wait_cycles_var(Environment *_environment, char *_timing, int _parallel)
Emit ASM code for WAIT [expression] CYCLES.
void wait_cycles(Environment *_environment, int _timing, int _parallel)
Emit ASM code for WAIT # [integer] CYCLES.
Definition wait_cycles.c:49
Structure of compilation environment.
Definition ugbc.h:2269
int protothread
Definition ugbc.h:2830
int arrayDimensions
Definition ugbc.h:2718
int protothreadStep
Definition ugbc.h:2840
int arrayDimensionsEach[MAX_ARRAY_DIMENSIONS]
Definition ugbc.h:2723
ProtothreadConfig protothreadConfig
Definition ugbc.h:2430
char * procedureName
Definition ugbc.h:2775
int protothreadForbid
Definition ugbc.h:2845
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define MAX_ARRAY_DIMENSIONS
Definition ugbc.h:566
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_TARRAY
Definition ugbc.h:480
@ VT_WORD
Definition ugbc.h:455
#define PROTOTHREAD_STATUS_YIELDED
Definition ugbc.h:4548
#define CRITICAL_WAIT_CYCLES_PARALLEL_CANNOT_BE_CALLED_OUTSIDE_PROCEDURE()
Definition ugbc.h:3766
#define CRITICAL_MULTITASKING_FORBIDDEN()
Definition ugbc.h:3703
#define PROTOTHREAD_STATUS_RUNNING
Definition ugbc.h:4547
void yield(Environment *_environment)
Emit code for YIELD.
Definition yield.c:63